diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2011-09-21 10:30:50 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-09-21 16:39:59 -0400 |
commit | 9c1f8594df4814ebfd6822ca3c9444fb3445888d (patch) | |
tree | 23946b6d2e4afc9eaac6156812ce1423927447c5 /lib/xz | |
parent | e5b26a88f13dfe3162acd50490c0c8504af81b26 (diff) |
XZ: Fix incorrect XZ_BUF_ERROR
xz_dec_run() could incorrectly return XZ_BUF_ERROR if all of the
following was true:
- The caller knows how many bytes of output to expect and only provides
that much output space.
- When the last output bytes are decoded, the caller-provided input
buffer ends right before the LZMA2 end of payload marker. So LZMA2
won't provide more output anymore, but it won't know it yet and thus
won't return XZ_STREAM_END yet.
- A BCJ filter is in use and it hasn't left any unfiltered bytes in the
temp buffer. This can happen with any BCJ filter, but in practice
it's more likely with filters other than the x86 BCJ.
This fixes <https://bugzilla.redhat.com/show_bug.cgi?id=735408> where
Squashfs thinks that a valid file system is corrupt.
This also fixes a similar bug in single-call mode where the uncompressed
size of a block using BCJ + LZMA2 was 0 bytes and caller provided no
output space. Many empty .xz files don't contain any blocks and thus
don't trigger this bug.
This also tweaks a closely related detail: xz_dec_bcj_run() could call
xz_dec_lzma2_run() to decode into temp buffer when it was known to be
useless. This was harmless although it wasted a minuscule number of CPU
cycles.
Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
Cc: stable <stable@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/xz')
-rw-r--r-- | lib/xz/xz_dec_bcj.c | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/xz/xz_dec_bcj.c b/lib/xz/xz_dec_bcj.c index e51e2558ca9d..a768e6d28bbb 100644 --- a/lib/xz/xz_dec_bcj.c +++ b/lib/xz/xz_dec_bcj.c | |||
@@ -441,8 +441,12 @@ XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, | |||
441 | * next filter in the chain. Apply the BCJ filter on the new data | 441 | * next filter in the chain. Apply the BCJ filter on the new data |
442 | * in the output buffer. If everything cannot be filtered, copy it | 442 | * in the output buffer. If everything cannot be filtered, copy it |
443 | * to temp and rewind the output buffer position accordingly. | 443 | * to temp and rewind the output buffer position accordingly. |
444 | * | ||
445 | * This needs to be always run when temp.size == 0 to handle a special | ||
446 | * case where the output buffer is full and the next filter has no | ||
447 | * more output coming but hasn't returned XZ_STREAM_END yet. | ||
444 | */ | 448 | */ |
445 | if (s->temp.size < b->out_size - b->out_pos) { | 449 | if (s->temp.size < b->out_size - b->out_pos || s->temp.size == 0) { |
446 | out_start = b->out_pos; | 450 | out_start = b->out_pos; |
447 | memcpy(b->out + b->out_pos, s->temp.buf, s->temp.size); | 451 | memcpy(b->out + b->out_pos, s->temp.buf, s->temp.size); |
448 | b->out_pos += s->temp.size; | 452 | b->out_pos += s->temp.size; |
@@ -465,16 +469,25 @@ XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, | |||
465 | s->temp.size = b->out_pos - out_start; | 469 | s->temp.size = b->out_pos - out_start; |
466 | b->out_pos -= s->temp.size; | 470 | b->out_pos -= s->temp.size; |
467 | memcpy(s->temp.buf, b->out + b->out_pos, s->temp.size); | 471 | memcpy(s->temp.buf, b->out + b->out_pos, s->temp.size); |
472 | |||
473 | /* | ||
474 | * If there wasn't enough input to the next filter to fill | ||
475 | * the output buffer with unfiltered data, there's no point | ||
476 | * to try decoding more data to temp. | ||
477 | */ | ||
478 | if (b->out_pos + s->temp.size < b->out_size) | ||
479 | return XZ_OK; | ||
468 | } | 480 | } |
469 | 481 | ||
470 | /* | 482 | /* |
471 | * If we have unfiltered data in temp, try to fill by decoding more | 483 | * We have unfiltered data in temp. If the output buffer isn't full |
472 | * data from the next filter. Apply the BCJ filter on temp. Then we | 484 | * yet, try to fill the temp buffer by decoding more data from the |
473 | * hopefully can fill the actual output buffer by copying filtered | 485 | * next filter. Apply the BCJ filter on temp. Then we hopefully can |
474 | * data from temp. A mix of filtered and unfiltered data may be left | 486 | * fill the actual output buffer by copying filtered data from temp. |
475 | * in temp; it will be taken care on the next call to this function. | 487 | * A mix of filtered and unfiltered data may be left in temp; it will |
488 | * be taken care on the next call to this function. | ||
476 | */ | 489 | */ |
477 | if (s->temp.size > 0) { | 490 | if (b->out_pos < b->out_size) { |
478 | /* Make b->out{,_pos,_size} temporarily point to s->temp. */ | 491 | /* Make b->out{,_pos,_size} temporarily point to s->temp. */ |
479 | s->out = b->out; | 492 | s->out = b->out; |
480 | s->out_pos = b->out_pos; | 493 | s->out_pos = b->out_pos; |