From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Subject: [PATCH] Fix CVE-2026-73209 Upstream commits: lib-compression: Don't calculate crc32 if using zlib in deflate mode https://github.com/dovecot/core/commit/cac140a9e4e12485bde4348b8a8c94a7a96a6e5e lib-compression: istream-lz4 - Use container_of() macro https://github.com/dovecot/core/commit/f54d2611567ad7c8f59e04b1190b29800898f1a1 lib-compression: istream-lz4 - Ensure uncompressed chunk size is not 0 https://github.com/dovecot/core/commit/86aba25a1159f7ff11480feec287647be78054d1 lib-compression: istream-lz4 - Try again if no data was decompressed https://github.com/dovecot/core/commit/f269b3d3051044aec31cef888fcf4c9ddd21c227 lib-compression: wrap *_read() in for(;;) loop (reindent only) https://github.com/dovecot/core/commit/a38aabc5790590af3310edc467e7c8892c089875 lib-compression: Iterate instead of recursing on zero-output decompress chunks https://github.com/dovecot/core/commit/78c0aa283ae0e4bdb205c8ef7d71f7fa938da4ab lib-compression: istream-zstd - Guard against no-progress loop in read https://github.com/dovecot/core/commit/e78ae179b9b979871175792d6cb5ef2ba7aed8b4 lib-compression: test - Add zero-len frame test for all handlers https://github.com/dovecot/core/commit/a5573eeb13f86481420be6f2c0763fda67156633 Taken from the Debian bookworm security update (dovecot 1:2.3.19.1+dfsg1-2.1+deb12u7), which carries these fixes for 2.3, and applied to 2.3.21.1. --- a/src/lib-compression/istream-bzlib.c +++ b/src/lib-compression/istream-bzlib.c @@ -51,88 +51,94 @@ static ssize_t i_stream_bzlib_read(struc size_t size, out_size; int ret; - high_offset = stream->istream.v_offset + (stream->pos - stream->skip); - if (zstream->eof_offset == high_offset) { - stream->istream.eof = TRUE; - return -1; - } - - if (!zstream->marked) { - if (!i_stream_try_alloc(stream, CHUNK_SIZE, &out_size)) - return -2; /* buffer full */ - } else { - /* try to avoid compressing, so we can quickly seek backwards */ - if (!i_stream_try_alloc_avoid_compress(stream, CHUNK_SIZE, &out_size)) - return -2; /* buffer full */ - } + /* Loop instead of recursing when a decompress step yields no output: + a crafted stream can produce an unbounded number of zero-output + steps, and tail-call recursion (not guaranteed) would exhaust the + stack. */ + for (;;) { + high_offset = stream->istream.v_offset + (stream->pos - stream->skip); + if (zstream->eof_offset == high_offset) { + stream->istream.eof = TRUE; + return -1; + } - if (i_stream_read_more(stream->parent, &data, &size) < 0) { - if (stream->parent->stream_errno != 0) { - stream->istream.stream_errno = - stream->parent->stream_errno; + if (!zstream->marked) { + if (!i_stream_try_alloc(stream, CHUNK_SIZE, &out_size)) + return -2; /* buffer full */ } else { - i_assert(stream->parent->eof); - bzlib_read_error(zstream, "unexpected EOF"); - if (!zstream->hdr_read) - stream->istream.stream_errno = EINVAL; - else - stream->istream.stream_errno = EPIPE; + /* try to avoid compressing, so we can quickly seek backwards */ + if (!i_stream_try_alloc_avoid_compress(stream, CHUNK_SIZE, &out_size)) + return -2; /* buffer full */ + } + + if (i_stream_read_more(stream->parent, &data, &size) < 0) { + if (stream->parent->stream_errno != 0) { + stream->istream.stream_errno = + stream->parent->stream_errno; + } else { + i_assert(stream->parent->eof); + bzlib_read_error(zstream, "unexpected EOF"); + if (!zstream->hdr_read) + stream->istream.stream_errno = EINVAL; + else + stream->istream.stream_errno = EPIPE; + } + return -1; + } + if (size == 0) { + /* no more input */ + i_assert(!stream->istream.blocking); + return 0; } - return -1; - } - if (size == 0) { - /* no more input */ - i_assert(!stream->istream.blocking); - return 0; - } - zstream->zs.next_in = (char *)data; - zstream->zs.avail_in = size; + zstream->zs.next_in = (char *)data; + zstream->zs.avail_in = size; - zstream->zs.next_out = (char *)stream->w_buffer + stream->pos; - zstream->zs.avail_out = out_size; - ret = BZ2_bzDecompress(&zstream->zs); - zstream->hdr_read = TRUE; - - out_size -= zstream->zs.avail_out; - stream->pos += out_size; - - i_stream_skip(stream->parent, size - zstream->zs.avail_in); - - switch (ret) { - case BZ_OK: - break; - case BZ_PARAM_ERROR: - i_unreached(); - case BZ_DATA_ERROR: - bzlib_read_error(zstream, "corrupted data"); - stream->istream.stream_errno = EINVAL; - return -1; - case BZ_DATA_ERROR_MAGIC: - bzlib_read_error(zstream, - "wrong magic in header (not bz2 file?)"); - stream->istream.stream_errno = EINVAL; - return -1; - case BZ_MEM_ERROR: - i_fatal_status(FATAL_OUTOFMEM, "bzlib.read(%s): Out of memory", - i_stream_get_name(&stream->istream)); - case BZ_STREAM_END: - zstream->eof_offset = stream->istream.v_offset + - (stream->pos - stream->skip); - stream->cached_stream_size = zstream->eof_offset; - if (out_size == 0) { - stream->istream.eof = TRUE; + zstream->zs.next_out = (char *)stream->w_buffer + stream->pos; + zstream->zs.avail_out = out_size; + ret = BZ2_bzDecompress(&zstream->zs); + zstream->hdr_read = TRUE; + + out_size -= zstream->zs.avail_out; + stream->pos += out_size; + + i_stream_skip(stream->parent, size - zstream->zs.avail_in); + + switch (ret) { + case BZ_OK: + break; + case BZ_PARAM_ERROR: + i_unreached(); + case BZ_DATA_ERROR: + bzlib_read_error(zstream, "corrupted data"); + stream->istream.stream_errno = EINVAL; return -1; + case BZ_DATA_ERROR_MAGIC: + bzlib_read_error(zstream, + "wrong magic in header (not bz2 file?)"); + stream->istream.stream_errno = EINVAL; + return -1; + case BZ_MEM_ERROR: + i_fatal_status(FATAL_OUTOFMEM, "bzlib.read(%s): Out of memory", + i_stream_get_name(&stream->istream)); + case BZ_STREAM_END: + zstream->eof_offset = stream->istream.v_offset + + (stream->pos - stream->skip); + stream->cached_stream_size = zstream->eof_offset; + if (out_size == 0) { + stream->istream.eof = TRUE; + return -1; + } + break; + default: + i_fatal("BZ2_bzDecompress() failed with %d", ret); } - break; - default: - i_fatal("BZ2_bzDecompress() failed with %d", ret); - } - if (out_size == 0) { - /* read more input */ - return i_stream_bzlib_read(stream); + if (out_size == 0) { + /* no output yet; read more input without recursing */ + continue; + } + return out_size; } - return out_size; } static void i_stream_bzlib_init(struct bzlib_istream *zstream) --- a/src/lib-compression/istream-lz4.c +++ b/src/lib-compression/istream-lz4.c @@ -25,7 +25,8 @@ struct lz4_istream { static void i_stream_lz4_close(struct iostream_private *stream, bool close_parent) { - struct lz4_istream *zstream = (struct lz4_istream *)stream; + struct lz4_istream *zstream = + container_of(stream, struct lz4_istream, istream.iostream); buffer_free(&zstream->chunk_buf); if (close_parent) @@ -77,9 +78,10 @@ static int i_stream_lz4_read_header(stru zstream->max_uncompressed_chunk_size = be32_to_cpu_unaligned(hdr->max_uncompressed_chunk_size); buffer_set_used_size(zstream->chunk_buf, 0); - if (zstream->max_uncompressed_chunk_size > ISTREAM_LZ4_CHUNK_SIZE) { + if (zstream->max_uncompressed_chunk_size == 0 || + zstream->max_uncompressed_chunk_size > ISTREAM_LZ4_CHUNK_SIZE) { lz4_read_error(zstream, t_strdup_printf( - "lz4 max chunk size too large (%u > %u)", + "invalid lz4 max chunk size (%u, max %u)", zstream->max_uncompressed_chunk_size, ISTREAM_LZ4_CHUNK_SIZE)); zstream->istream.istream.stream_errno = EINVAL; @@ -131,7 +133,8 @@ static int i_stream_lz4_read_chunk_heade static ssize_t i_stream_lz4_read(struct istream_private *stream) { - struct lz4_istream *zstream = (struct lz4_istream *)stream; + struct lz4_istream *zstream = + container_of(stream, struct lz4_istream, istream); const unsigned char *data; size_t size; int ret; @@ -148,66 +151,77 @@ static ssize_t i_stream_lz4_read(struct zstream->header_read = TRUE; } - if (zstream->chunk_left == 0) { - while ((ret = i_stream_lz4_read_chunk_header(zstream)) == 0) { - if (!stream->istream.blocking) - return 0; + /* Loop over chunks instead of recursing on zero-output chunks: a + crafted stream can contain an unbounded number of chunks that each + decompress to zero bytes, and tail-call recursion (which is not + guaranteed) would let that exhaust the stack. */ + for (;;) { + if (zstream->chunk_left == 0) { + while ((ret = i_stream_lz4_read_chunk_header(zstream)) == 0) { + if (!stream->istream.blocking) + return 0; + } + if (ret < 0) + return ret; } - if (ret < 0) - return ret; - } - /* read the whole compressed chunk into memory */ - while (zstream->chunk_left > 0 && - (ret = i_stream_read_more(zstream->istream.parent, &data, &size)) > 0) { - if (size > zstream->chunk_left) - size = zstream->chunk_left; - buffer_append(zstream->chunk_buf, data, size); - i_stream_skip(zstream->istream.parent, size); - zstream->chunk_left -= size; - } - if (zstream->chunk_left > 0) { - if (ret == -1 && zstream->istream.parent->stream_errno == 0) { - lz4_read_error(zstream, "truncated lz4 chunk"); - stream->istream.stream_errno = EPIPE; + /* read the whole compressed chunk into memory */ + while (zstream->chunk_left > 0 && + (ret = i_stream_read_more(zstream->istream.parent, &data, &size)) > 0) { + if (size > zstream->chunk_left) + size = zstream->chunk_left; + buffer_append(zstream->chunk_buf, data, size); + i_stream_skip(zstream->istream.parent, size); + zstream->chunk_left -= size; + } + if (zstream->chunk_left > 0) { + if (ret == -1 && zstream->istream.parent->stream_errno == 0) { + lz4_read_error(zstream, "truncated lz4 chunk"); + stream->istream.stream_errno = EPIPE; + return -1; + } + zstream->istream.istream.stream_errno = + zstream->istream.parent->stream_errno; + i_assert(ret != 0 || !stream->istream.blocking); + return ret; + } + /* if we already have max_buffer_size amount of data, fail here */ + if (stream->pos - stream->skip >= i_stream_get_max_buffer_size(&stream->istream)) + return -2; + if (i_stream_get_data_size(zstream->istream.parent) > 0) { + /* Parent stream was only partially consumed. Set the + stream's IO as pending to avoid hangs. */ + i_stream_set_input_pending(&zstream->istream.istream, TRUE); + } + /* allocate enough space for the old data and the new + decompressed chunk. we don't know the original compressed size, + so just allocate the max amount of memory. */ + void *dest = i_stream_alloc(stream, zstream->max_uncompressed_chunk_size); + ret = LZ4_decompress_safe(zstream->chunk_buf->data, dest, + zstream->chunk_buf->used, + zstream->max_uncompressed_chunk_size); + i_assert(ret <= (int)zstream->max_uncompressed_chunk_size); + if (ret < 0) { + lz4_read_error(zstream, "corrupted lz4 chunk"); + stream->istream.stream_errno = EINVAL; return -1; + } else if (ret == 0) { + /* chunk decompressed to zero bytes; read the next chunk + without recursing so the stack stays bounded. */ + buffer_set_used_size(zstream->chunk_buf, 0); + continue; } - zstream->istream.istream.stream_errno = - zstream->istream.parent->stream_errno; - i_assert(ret != 0 || !stream->istream.blocking); + i_assert(ret > 0); + stream->pos += ret; + i_assert(stream->pos <= stream->buffer_size); + + /* we are going to get next chunk after this, so reset here + so we can reuse the chunk buf for reading next buffer prefix */ + if (zstream->chunk_left == 0) + buffer_set_used_size(zstream->chunk_buf, 0); + return ret; } - /* if we already have max_buffer_size amount of data, fail here */ - if (stream->pos - stream->skip >= i_stream_get_max_buffer_size(&stream->istream)) - return -2; - if (i_stream_get_data_size(zstream->istream.parent) > 0) { - /* Parent stream was only partially consumed. Set the stream's - IO as pending to avoid hangs. */ - i_stream_set_input_pending(&zstream->istream.istream, TRUE); - } - /* allocate enough space for the old data and the new - decompressed chunk. we don't know the original compressed size, - so just allocate the max amount of memory. */ - void *dest = i_stream_alloc(stream, zstream->max_uncompressed_chunk_size); - ret = LZ4_decompress_safe(zstream->chunk_buf->data, dest, - zstream->chunk_buf->used, - zstream->max_uncompressed_chunk_size); - i_assert(ret <= (int)zstream->max_uncompressed_chunk_size); - if (ret < 0) { - lz4_read_error(zstream, "corrupted lz4 chunk"); - stream->istream.stream_errno = EINVAL; - return -1; - } - i_assert(ret > 0); - stream->pos += ret; - i_assert(stream->pos <= stream->buffer_size); - - /* we are going to get next chunk after this, so reset here - so we can reuse the chunk buf for reading next buffer prefix */ - if (zstream->chunk_left == 0) - buffer_set_used_size(zstream->chunk_buf, 0); - - return ret; } static void i_stream_lz4_reset(struct lz4_istream *zstream) @@ -227,7 +241,8 @@ static void i_stream_lz4_reset(struct lz static void i_stream_lz4_seek(struct istream_private *stream, uoff_t v_offset, bool mark) { - struct lz4_istream *zstream = (struct lz4_istream *) stream; + struct lz4_istream *zstream = + container_of(stream, struct lz4_istream, istream); if (i_stream_nonseekable_try_seek(stream, v_offset)) return; @@ -243,7 +258,8 @@ i_stream_lz4_seek(struct istream_private static void i_stream_lz4_sync(struct istream_private *stream) { - struct lz4_istream *zstream = (struct lz4_istream *) stream; + struct lz4_istream *zstream = + container_of(stream, struct lz4_istream, istream); const struct stat *st; if (i_stream_stat(stream->parent, FALSE, &st) == 0) { --- a/src/lib-compression/istream-zlib.c +++ b/src/lib-compression/istream-zlib.c @@ -169,153 +169,161 @@ static ssize_t i_stream_zlib_read(struct size_t size, out_size; int ret; - high_offset = stream->istream.v_offset + (stream->pos - stream->skip); - if (zstream->eof_offset == high_offset) { - /* zlib library returned EOF. */ - if (!zstream->gz) { - /* deflate - ignore if there's still more data */ - stream->istream.eof = TRUE; - return -1; + /* Loop instead of recursing when an inflate step yields no output: + a crafted stream can produce an unbounded number of zero-output + steps, and tail-call recursion (not guaranteed) would exhaust the + stack. */ + for (;;) { + high_offset = stream->istream.v_offset + (stream->pos - stream->skip); + if (zstream->eof_offset == high_offset) { + /* zlib library returned EOF. */ + if (!zstream->gz) { + /* deflate - ignore if there's still more data */ + stream->istream.eof = TRUE; + return -1; + } + /* gz format - read the trailer */ + if (!zstream->trailer_read) { + do { + ret = i_stream_zlib_read_trailer(zstream); + } while (ret == 0 && stream->istream.blocking); + if (ret <= 0) + return ret; + } + /* See if there's another concatenated gz stream. */ + if (i_stream_read_eof(stream->parent)) { + /* EOF or error */ + stream->istream.stream_errno = + stream->parent->stream_errno; + stream->istream.eof = TRUE; + return -1; + } + /* Multiple gz streams concatenated together */ + zstream->starting_concated_output = TRUE; + } + if (zstream->starting_concated_output) { + /* make sure there actually is something in parent stream. + we don't want to reset the stream unless we actually see + some concated output. */ + ret = i_stream_read_more(stream->parent, &data, &size); + if (ret <= 0) { + if (ret == 0) + return 0; + if (stream->parent->stream_errno != 0) { + stream->istream.stream_errno = + stream->parent->stream_errno; + } + stream->istream.eof = TRUE; + return -1; + } + + /* gzip file with concatenated content */ + stream->cached_stream_size = UOFF_T_MAX; + zstream->eof_offset = UOFF_T_MAX; + zstream->header_read = FALSE; + zstream->trailer_read = FALSE; + zstream->crc32 = 0; + zstream->starting_concated_output = FALSE; + + (void)inflateEnd(&zstream->zs); + i_stream_zlib_init(zstream); } - /* gz format - read the trailer */ - if (!zstream->trailer_read) { + + if (!zstream->header_read) { do { - ret = i_stream_zlib_read_trailer(zstream); + ret = i_stream_zlib_read_header(stream); } while (ret == 0 && stream->istream.blocking); if (ret <= 0) return ret; + zstream->header_read = TRUE; } - /* See if there's another concatenated gz stream. */ - if (i_stream_read_eof(stream->parent)) { - /* EOF or error */ - stream->istream.stream_errno = - stream->parent->stream_errno; - stream->istream.eof = TRUE; - return -1; + + if (!zstream->marked) { + if (!i_stream_try_alloc(stream, CHUNK_SIZE, &out_size)) + return -2; /* buffer full */ + } else { + /* try to avoid compressing, so we can quickly seek backwards */ + if (!i_stream_try_alloc_avoid_compress(stream, CHUNK_SIZE, &out_size)) + return -2; /* buffer full */ } - /* Multiple gz streams concatenated together */ - zstream->starting_concated_output = TRUE; - } - if (zstream->starting_concated_output) { - /* make sure there actually is something in parent stream. - we don't want to reset the stream unless we actually see - some concated output. */ - ret = i_stream_read_more(stream->parent, &data, &size); - if (ret <= 0) { - if (ret == 0) - return 0; + + if (i_stream_read_more(stream->parent, &data, &size) < 0) { if (stream->parent->stream_errno != 0) { stream->istream.stream_errno = stream->parent->stream_errno; + } else { + i_assert(stream->parent->eof); + zlib_read_error(zstream, "unexpected EOF"); + stream->istream.stream_errno = EPIPE; } - stream->istream.eof = TRUE; return -1; } - - /* gzip file with concatenated content */ - stream->cached_stream_size = UOFF_T_MAX; - zstream->eof_offset = UOFF_T_MAX; - zstream->header_read = FALSE; - zstream->trailer_read = FALSE; - zstream->crc32 = 0; - zstream->starting_concated_output = FALSE; - - (void)inflateEnd(&zstream->zs); - i_stream_zlib_init(zstream); - } - - if (!zstream->header_read) { - do { - ret = i_stream_zlib_read_header(stream); - } while (ret == 0 && stream->istream.blocking); - if (ret <= 0) - return ret; - zstream->header_read = TRUE; - } - - if (!zstream->marked) { - if (!i_stream_try_alloc(stream, CHUNK_SIZE, &out_size)) - return -2; /* buffer full */ - } else { - /* try to avoid compressing, so we can quickly seek backwards */ - if (!i_stream_try_alloc_avoid_compress(stream, CHUNK_SIZE, &out_size)) - return -2; /* buffer full */ - } - - if (i_stream_read_more(stream->parent, &data, &size) < 0) { - if (stream->parent->stream_errno != 0) { - stream->istream.stream_errno = - stream->parent->stream_errno; - } else { - i_assert(stream->parent->eof); - zlib_read_error(zstream, "unexpected EOF"); - stream->istream.stream_errno = EPIPE; - } - return -1; - } - if (size == 0) { - /* no more input */ - i_assert(!stream->istream.blocking); - return 0; - } - - zstream->zs.next_in = (void *)data; - zstream->zs.avail_in = size; - - zstream->zs.next_out = stream->w_buffer + stream->pos; - zstream->zs.avail_out = out_size; - ret = inflate(&zstream->zs, Z_SYNC_FLUSH); - - out_size -= zstream->zs.avail_out; - zstream->crc32 = crc32_data_more(zstream->crc32, - stream->w_buffer + stream->pos, - out_size); - stream->pos += out_size; - - size_t bytes_consumed = size - zstream->zs.avail_in; - i_stream_skip(stream->parent, bytes_consumed); - if (i_stream_get_data_size(stream->parent) > 0 && - (bytes_consumed > 0 || out_size > 0)) { - /* Parent stream was only partially consumed. Set the stream's - IO as pending to avoid hangs. */ - i_stream_set_input_pending(&stream->istream, TRUE); - } - - switch (ret) { - case Z_OK: - break; - case Z_NEED_DICT: - zlib_read_error(zstream, "can't read file without dict"); - stream->istream.stream_errno = EIO; - return -1; - case Z_DATA_ERROR: - zlib_read_error(zstream, "corrupted data"); - stream->istream.stream_errno = EINVAL; - return -1; - case Z_MEM_ERROR: - i_fatal_status(FATAL_OUTOFMEM, "zlib.read(%s): Out of memory", - i_stream_get_name(&stream->istream)); - case Z_STREAM_END: - zstream->eof_offset = stream->istream.v_offset + - (stream->pos - stream->skip); - stream->cached_stream_size = zstream->eof_offset; - zstream->zs.avail_in = 0; - - if (!zstream->trailer_read) { - /* try to read and verify the trailer, we might not - be called again. */ - if (i_stream_zlib_read_trailer(zstream) < 0) - return -1; + if (size == 0) { + /* no more input */ + i_assert(!stream->istream.blocking); + return 0; + } + + zstream->zs.next_in = (void *)data; + zstream->zs.avail_in = size; + + zstream->zs.next_out = stream->w_buffer + stream->pos; + zstream->zs.avail_out = out_size; + ret = inflate(&zstream->zs, Z_SYNC_FLUSH); + + out_size -= zstream->zs.avail_out; + /* CRC32 is only needed for GZ trailer. */ + if (zstream->gz) + zstream->crc32 = crc32_data_more(zstream->crc32, + stream->w_buffer + stream->pos, + out_size); + stream->pos += out_size; + + size_t bytes_consumed = size - zstream->zs.avail_in; + i_stream_skip(stream->parent, bytes_consumed); + if (i_stream_get_data_size(stream->parent) > 0 && + (bytes_consumed > 0 || out_size > 0)) { + /* Parent stream was only partially consumed. Set the stream's + IO as pending to avoid hangs. */ + i_stream_set_input_pending(&stream->istream, TRUE); + } + + switch (ret) { + case Z_OK: + break; + case Z_NEED_DICT: + zlib_read_error(zstream, "can't read file without dict"); + stream->istream.stream_errno = EIO; + return -1; + case Z_DATA_ERROR: + zlib_read_error(zstream, "corrupted data"); + stream->istream.stream_errno = EINVAL; + return -1; + case Z_MEM_ERROR: + i_fatal_status(FATAL_OUTOFMEM, "zlib.read(%s): Out of memory", + i_stream_get_name(&stream->istream)); + case Z_STREAM_END: + zstream->eof_offset = stream->istream.v_offset + + (stream->pos - stream->skip); + stream->cached_stream_size = zstream->eof_offset; + zstream->zs.avail_in = 0; + + if (!zstream->trailer_read) { + /* try to read and verify the trailer, we might not + be called again. */ + if (i_stream_zlib_read_trailer(zstream) < 0) + return -1; + } + break; + default: + i_fatal("inflate() failed with %d", ret); + } + if (out_size == 0) { + /* no output yet; read more input without recursing */ + continue; } - break; - default: - i_fatal("inflate() failed with %d", ret); - } - if (out_size == 0) { - /* read more input */ - return i_stream_zlib_read(stream); + return out_size; } - return out_size; } static void i_stream_zlib_init(struct zlib_istream *zstream) --- a/src/lib-compression/istream-zstd.c +++ b/src/lib-compression/istream-zstd.c @@ -175,12 +175,20 @@ static ssize_t i_stream_zstd_read(struct zstream->output.pos = 0; zstream->output.size = ZSTD_DStreamOutSize(); + size_t old_input_pos = zstream->input.pos; size_t zret = ZSTD_decompressStream(zstream->dstream, &zstream->output, &zstream->input); if (ZSTD_isError(zret) != 0) { i_stream_zstd_read_error(zstream, zret); return -1; } + if (zstream->input.pos == old_input_pos && zstream->output.pos == 0) { + io_stream_set_error(&zstream->istream.iostream, + "zstd.read(%s): decompressor made no progress", + i_stream_get_name(&zstream->istream.istream)); + zstream->istream.istream.stream_errno = EIO; + return -1; + } /* ZSTD magic number is 4 bytes, but it's only defined after v0.8 */ if (!zstream->hdr_read && zstream->input.size > 4) zstream->hdr_read = TRUE; --- a/src/lib-compression/ostream-zlib.c +++ b/src/lib-compression/ostream-zlib.c @@ -182,7 +182,9 @@ o_stream_zlib_send_chunk(struct zlib_ost } size -= zs->avail_in; - zstream->crc = crc32_data_more(zstream->crc, data, size); + /* CRC32 is only needed for GZ trailer. */ + if (zstream->gz) + zstream->crc = crc32_data_more(zstream->crc, data, size); zstream->bytes32 += size; zstream->flushed = FALSE; return size; --- a/src/lib-compression/test-compression.c +++ b/src/lib-compression/test-compression.c @@ -152,6 +152,46 @@ test_compression_handler_empty(const str } static void +test_compression_handler_zero_frame(const struct compression_handler *handler, + bool autodetect) +{ + test_begin(t_strdup_printf("compression handler %s (zero-len frame, autodetect=%s)", + handler->name, autodetect ? "yes" : "no")); + + /* Produce successive empty frames. Each frame needs its own ostream + because o_stream_finish() propagates to the parent; compress each + into a separate buffer then concatenate. + lz4 uses a custom single-stream format and does not support + concatenated frames, so limit it to one. */ + unsigned int n_frames = strcmp(handler->name, "lz4") == 0 ? 1 : 3; + buffer_t *compressed = buffer_create_dynamic(pool_datastack_create(), 256); + for (unsigned int i = 0; i < n_frames; i++) { + buffer_t *frame_buf = buffer_create_dynamic(pool_datastack_create(), 64); + struct ostream *os = test_ostream_create(frame_buf); + struct ostream *output = handler->create_ostream(os, 1); + o_stream_unref(&os); + test_assert_idx(o_stream_finish(output) == 1, i); + o_stream_unref(&output); + test_assert_idx(frame_buf->used > 0, i); + buffer_append(compressed, frame_buf->data, frame_buf->used); + } + + /* Decompress: must yield clean EOF with no error and no loop. */ + struct istream *is = test_istream_create_data(compressed->data, compressed->used); + is->blocking = TRUE; + struct istream *input = !autodetect ? handler->create_istream(is) : + i_stream_create_decompress(is, 0); + i_stream_unref(&is); + + test_assert(i_stream_read(input) == -1); + test_assert(input->eof); + test_assert(input->stream_errno == 0); + i_stream_unref(&input); + + test_end(); +} + +static void test_compression_handler_seek(const struct compression_handler *handler, bool autodetect) { @@ -739,6 +779,7 @@ static void test_compression_int(bool au test_compression_handler_detect(&compression_handlers[i]); test_compression_handler_short(&compression_handlers[i], autodetect); test_compression_handler_empty(&compression_handlers[i], autodetect); + test_compression_handler_zero_frame(&compression_handlers[i], autodetect); test_compression_handler(&compression_handlers[i], autodetect); test_compression_handler_seek(&compression_handlers[i], autodetect); test_compression_handler_reset(&compression_handlers[i], autodetect); @@ -995,6 +1036,123 @@ static void test_lz4_small_header(void) test_end(); } +static const unsigned char lz4_chunk_crash_01[] = { + 0x44, 0x6f, 0x76, 0x65, 0x63, 0x6f, 0x74, 0x2d, 0x4c, 0x5a, 0x34, 0x0d, + 0x2a, 0x9b, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x7a, + 0x32, 0x01, 0xff, 0xff, 0x00, 0x30 +}; + +static const unsigned char lz4_chunk_crash_02[] = { + 0x44, 0x6f, 0x76, 0x65, 0x63, 0x6f, 0x74, 0x2d, 0x4c, 0x5a, 0x34, 0x0d, + 0x2a, 0x9b, 0xc5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x7a, + 0x32, 0x01, 0xff, 0xff, 0x00, 0x30 +}; + +static const unsigned char lz4_chunk_eof_03[] = { + 0x44, 0x6f, 0x76, 0x65, 0x63, 0x6f, 0x74, 0x2d, 0x4c, 0x5a, 0x34, 0x0d, + 0x2a, 0x9b, 0xc5, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, +}; + +static void test_lz4_chunk_size(void) +{ + const struct compression_handler *lz4; + struct istream *file_input, *input; + + if (compression_lookup_handler("lz4", &lz4) <= 0) + return; /* not compiled in or unknown */ + + test_begin("lz4 chunk size"); + + file_input = test_istream_create_data(lz4_chunk_crash_01, + sizeof(lz4_chunk_crash_01)); + input = lz4->create_istream(file_input); + i_stream_unref(&file_input); + i_stream_read(input); + + test_assert(input->eof); + test_assert(input->stream_errno != 0); + const char *error = i_stream_get_error(input); + test_assert(strstr(error, "invalid lz4 max chunk size") != NULL); + + i_stream_unref(&input); + + file_input = test_istream_create_data(lz4_chunk_crash_02, + sizeof(lz4_chunk_crash_02)); + input = lz4->create_istream(file_input); + i_stream_unref(&file_input); + i_stream_read(input); + + test_assert(input->eof); + test_assert(input->stream_errno != 0); + error = i_stream_get_error(input); + test_assert(strstr(error, "invalid lz4 max chunk size") != NULL); + + i_stream_unref(&input); + + file_input = test_istream_create_data(lz4_chunk_eof_03, + sizeof(lz4_chunk_eof_03)); + input = lz4->create_istream(file_input); + i_stream_unref(&file_input); + i_stream_read(input); + + /* no error, but no content either */ + test_assert(input->eof); + test_assert(input->stream_errno == 0); + + i_stream_unref(&input); + + test_end(); +} + +static void test_lz4_many_empty_chunks(void) +{ + const struct compression_handler *lz4; + struct istream *file_input, *input; + + if (compression_lookup_handler("lz4", &lz4) <= 0) + return; /* not compiled in or unknown */ + + test_begin("lz4 many empty chunks"); + + /* A crafted lz4 stream can contain an unbounded number of chunks that + each decompress to zero bytes. i_stream_lz4_read() must iterate over + them rather than recurse once per chunk, or the stack is exhausted + (tail-call optimization is not guaranteed). */ + buffer_t *buf = buffer_create_dynamic(default_pool, 1024*512); + struct iostream_lz4_header hdr; + memcpy(hdr.magic, IOSTREAM_LZ4_MAGIC, IOSTREAM_LZ4_MAGIC_LEN); + /* a valid (64k) max uncompressed chunk size, big-endian */ + hdr.max_uncompressed_chunk_size[0] = 0x00; + hdr.max_uncompressed_chunk_size[1] = 0x01; + hdr.max_uncompressed_chunk_size[2] = 0x00; + hdr.max_uncompressed_chunk_size[3] = 0x00; + buffer_append(buf, &hdr, sizeof(hdr)); + + /* Each chunk: 4-byte big-endian compressed length (1), then a single + 0x00 byte, which LZ4_decompress_safe() decodes to zero bytes. */ + static const unsigned char empty_chunk[] = { + 0x00, 0x00, 0x00, 0x01, 0x00 + }; + for (unsigned int i = 0; i < 100000; i++) + buffer_append(buf, empty_chunk, sizeof(empty_chunk)); + + file_input = test_istream_create_data(buf->data, buf->used); + file_input->blocking = TRUE; + input = lz4->create_istream(file_input); + i_stream_unref(&file_input); + + /* All chunks are empty: clean EOF, no content, no error - and, with + the iterative read, no stack overflow. */ + test_assert(i_stream_read(input) == -1); + test_assert(input->eof); + test_assert(input->stream_errno == 0); + + i_stream_unref(&input); + buffer_free(&buf); + + test_end(); +} + static void test_uncompress_file(const char *path) { const struct compression_handler *handler; @@ -1107,6 +1265,8 @@ int main(int argc, char *argv[]) test_gz_header, test_gz_large_header, test_lz4_small_header, + test_lz4_chunk_size, + test_lz4_many_empty_chunks, test_compression_ext, NULL };