erofs: avoid infinite loop due to incomplete zstd-compressed data
Subsystems (2)
PATCH 1/1 erofs: avoid infinite loop due to incomplete zstd-compressed data
Currently, the decompression logic incorrectly spins if compressed data is truncated in crafted (deliberately corrupted) images.
Fixes 7c35de4df105 ("erofs: Zstandard compression support")
Reported-by Robert Morris <[email protected]>
Signed-off-by Gao Xiang <[email protected]>
---
Actually I suspect multi-shot decompressor lzma may have a similar issue (but it seems deflate is not), will check later.
fs/erofs/decompressor_zstd.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
fs/erofs/decompressor_zstd.c +7 -4
@@ -172,7 +172,6 @@ static int z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq, dctx.bounce = strm->bounce; do { - dctx.avail_out = out_buf.size - out_buf.pos; dctx.inbuf_sz = in_buf.size; dctx.inbuf_pos = in_buf.pos; err = z_erofs_stream_switch_bufs(&dctx, &out_buf.dst,
@@ -188,14 +187,18 @@ static int z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq, in_buf.pos = dctx.inbuf_pos; zerr =zstd_decompress_stream (stream, &out_buf, &in_buf); -if (zstd_is_error (zerr) || (!zerr && rq->outputsize)) { + dctx.avail_out = out_buf.size - out_buf.pos; +if (zstd_is_error (zerr) || + ((rq->outputsize + dctx.avail_out) && (!zerr || (zerr >0 && + !(rq->inputsize + in_buf.size - in_buf.pos))))) {erofs_err (sb,"failed to decompress in[%u] out[%u]: %s" , rq->inputsize, rq->outputsize, - zerr ?zstd_get_error_name (zerr) :"unexpected end of stream" ); +zstd_is_error (zerr) ?zstd_get_error_name (zerr) : +"unexpected end of stream" ); err = -EFSCORRUPTED ;break ; } - }while (rq->outputsize || out_buf.pos < out_buf.size); + }while (rq->outputsize + dctx.avail_out);if (dctx.kout)kunmap_local (dctx.kout);
Signature · 1 line
2.43.5
Quoted 7 lines · Gao Xiang
Currently, the decompression logic incorrectly spins if compressed data is truncated in crafted (deliberately corrupted) images.
Fixes 7c35de4df105 ("erofs: Zstandard compression support")Reported-by Robert Morris <[email protected]>Signed-off-by Gao Xiang <[email protected]>
Reviewed-by Chao Yu <[email protected]>
Thanks,
Quoted 7 lines · Gao Xiang
Currently, the decompression logic incorrectly spins if compressed data is truncated in crafted (deliberately corrupted) images.
Fixes 7c35de4df105 ("erofs: Zstandard compression support")Reported-by Robert Morris <[email protected]>Signed-off-by Gao Xiang <[email protected]>
Reviewed-by Chunhai Guo <[email protected]>
Thanks,