Skip to content

Commit

Permalink
lib/xz: Avoid overlapping memcpy() with invalid input with in-place d…
Browse files Browse the repository at this point in the history
…ecompression

With valid files, the safety margin described in lib/decompress_unxz.c
ensures that these buffers cannot overlap. But if the uncompressed size
of the input is larger than the caller thought, which is possible when
the input file is invalid/corrupt, the buffers can overlap. Obviously
the result will then be garbage (and usually the decoder will return
an error too) but no other harm will happen when such an over-run occurs.

This change only affects uncompressed LZMA2 chunks and so this
should have no effect on performance.

Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Lasse Collin <[email protected]>
Signed-off-by: Gao Xiang <[email protected]>
  • Loading branch information
Larhzu authored and hsiangkao committed Oct 19, 2021
1 parent 3862929 commit 83d3c4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/decompress_unxz.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
* memeq and memzero are not used much and any remotely sane implementation
* is fast enough. memcpy/memmove speed matters in multi-call mode, but
* the kernel image is decompressed in single-call mode, in which only
* memcpy speed can matter and only if there is a lot of uncompressible data
* memmove speed can matter and only if there is a lot of uncompressible data
* (LZMA2 stores uncompressible chunks in uncompressed form). Thus, the
* functions below should just be kept small; it's probably not worth
* optimizing for speed.
Expand Down
21 changes: 19 additions & 2 deletions lib/xz/xz_dec_lzma2.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,14 @@ static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,

*left -= copy_size;

memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
/*
* If doing in-place decompression in single-call mode and the
* uncompressed size of the file is larger than the caller
* thought (i.e. it is invalid input!), the buffers below may
* overlap and cause undefined behavior with memcpy().
* With valid inputs memcpy() would be fine here.
*/
memmove(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
dict->pos += copy_size;

if (dict->full < dict->pos)
Expand All @@ -397,7 +404,11 @@ static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
if (dict->pos == dict->end)
dict->pos = 0;

memcpy(b->out + b->out_pos, b->in + b->in_pos,
/*
* Like above but for multi-call mode: use memmove()
* to avoid undefined behavior with invalid input.
*/
memmove(b->out + b->out_pos, b->in + b->in_pos,
copy_size);
}

Expand All @@ -421,6 +432,12 @@ static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
if (dict->pos == dict->end)
dict->pos = 0;

/*
* These buffers cannot overlap even if doing in-place
* decompression because in multi-call mode dict->buf
* has been allocated by us in this file; it's not
* provided by the caller like in single-call mode.
*/
memcpy(b->out + b->out_pos, dict->buf + dict->start,
copy_size);
}
Expand Down

0 comments on commit 83d3c4f

Please sign in to comment.