Skip to contentSkip to conversation

erofs-utils: lib: fix ZSTD decompression safety issues

PATCH In review Utkal Singh 2 participants 5 messages Mar 17 Library code

lore
Share & tools
lore
#0 lore raw

v2 cover erofs-utils: lib: fix ZSTD decompression safety issues

Changes since v1:
- Added reproducer per maintainer request

This series fixes two issues in z_erofs_decompress_zstd() that can be triggered by crafted EROFS filesystem images.

Patch 1/2 validates ZSTD frame content size against decodedlength. Patch 2/2 fixes a missing error return on decompression length mismatch.

Utkal Singh (2):
erofs-utils: lib: validate ZSTD frame content size in decompression
erofs-utils: lib: return error on ZSTD decompression length mismatch

 lib/decompress.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 erofs-utils: lib: validate ZSTD frame content size in decompression

ZSTD_getFrameContentSize() reads the content size from the ZSTD frame header in the compressed data. This is untrusted on-disk metadata, independent from the extent map that provides rq->decodedlength via z_erofs_map_blocks_iter().

A crafted EROFS image can set the extent map to claim a decoded length larger than the actual ZSTD frame content size. When this happens, a buffer of the (smaller) frame content size is allocated and decompressed into, but the subsequent memcpy copies rq->decodedlength bytes from it -- a potential out-of-bounds read.

Additionally, the ZSTD_getDecompressedSize() legacy fallback returns 0 for frames without a content size field. This leads to malloc(0) followed by out-of-bounds access on the returned pointer.

Reject frames where the reported content size is zero or smaller than the expected decoded length.

Reproducer:
mkdir testdir
python3 -c "open('testdir/f','wb').write(b'A'*131072)" mkfs.erofs -zzstd test.erofs testdir/

  python3 -c "d=bytearray(open('test.erofs','rb').read());\
    p=d.find(b'\x28\xb5\x2f\xfd');d[p+4]=0x20;d[p+5]=0x01;\
    open('test.erofs','wb').write(d)"

fsck.erofs --extract=out test.erofs
# Expected: ZSTD frame content size 1 < decoded length 131072

---
 lib/decompress.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/decompress.c +7 -0
index 3e7a173..fb81039 100644
@@ -48,7 +48,14 @@ static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq)
 #else
 	total = ZSTD_getDecompressedSize(src + inputmargin,
 					 rq->inputsize - inputmargin);
+	if (!total)
+		return -EFSCORRUPTED;
 #endif
+	if (total < rq->decodedlength) {
+		erofs_err("ZSTD frame content size %llu < decoded length %u",
+			  total, rq->decodedlength);
+		return -EFSCORRUPTED;
+	}
 	if (rq->decodedskip || total != rq->decodedlength) {
 		buff = malloc(total);
 		if (!buff)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] erofs-utils: lib: validate ZSTD frame content size in decompression

Quoted 38 lines · Utkal Singh

ZSTD_getFrameContentSize() reads the content size from the ZSTD frame header in the compressed data. This is untrusted on-disk metadata, independent from the extent map that provides rq->decodedlength via z_erofs_map_blocks_iter().

A crafted EROFS image can set the extent map to claim a decoded length larger than the actual ZSTD frame content size. When this happens, a buffer of the (smaller) frame content size is allocated and decompressed into, but the subsequent memcpy copies rq->decodedlength bytes from it -- a potential out-of-bounds read.

Additionally, the ZSTD_getDecompressedSize() legacy fallback returns 0 for frames without a content size field. This leads to malloc(0) followed by out-of-bounds access on the returned pointer.

Reject frames where the reported content size is zero or smaller than the expected decoded length.

Reproducer:

   mkdir testdir
   python3 -c "open('testdir/f','wb').write(b'A'*131072)"
   mkfs.erofs -zzstd test.erofs testdir/
   python3 -c "d=bytearray(open('test.erofs','rb').read());\
     p=d.find(b'\x28\xb5\x2f\xfd');d[p+4]=0x20;d[p+5]=0x01;\
     open('test.erofs','wb').write(d)"
   fsck.erofs --extract=out test.erofs
   # Expected: ZSTD frame content size 1 < decoded length 131072
---
  lib/decompress.c | 7 +++++++
  1 file changed, 7 insertions(+)
lib/decompress.c +0 -0
index 3e7a173..fb81039 100644

lib/decompress.c static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq) · line 52

Quoted 5 lines
lib/decompress.c +2 -0
@@ -48,7 +48,14 @@ static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq)
  #else
  	total = ZSTD_getDecompressedSize(src + inputmargin,
  					 rq->inputsize - inputmargin);
+	if (!total)
+		return -EFSCORRUPTED;

hmm, that is the difference between the kernel and erofs-utils implementation.

the kernel uses zstd streaming APIs, so it won't malloc() a new buffer in advance, actually I think erofs-utils should switch to streaming APIs too, in order to avoid

ZSTD_getFrameContentSize() and ZSTD_getDecompressedSize()

dependencies as you said in the commit message.

Thanks,
Gao Xiang

#3 lore raw

Re: [PATCH v2 1/2] erofs-utils: lib: validate ZSTD frame content size in decompression

Thanks for the direction, Gao Xiang.

Understood — switching to ZSTD streaming APIs (ZSTD_decompressStream) would eliminate the ZSTD_getFrameContentSize() / ZSTD_getDecompressedSize() dependency entirely and align erofs-utils with the kernel implementation.

I'll work on a v3 using the streaming approach.

- Utkal

Quoted 63 lines · Gao Xiang

lib/decompress.c static int z_erofs_decompress_zstd(struct

Quoted 38 lines · Utkal Singh

ZSTD_getFrameContentSize() reads the content size from the ZSTD frame header in the compressed data. This is untrusted on-disk metadata, independent from the extent map that provides rq->decodedlength via z_erofs_map_blocks_iter().

A crafted EROFS image can set the extent map to claim a decoded length larger than the actual ZSTD frame content size. When this happens, a buffer of the (smaller) frame content size is allocated and decompressed into, but the subsequent memcpy copies rq->decodedlength bytes from it -- a potential out-of-bounds read.

Additionally, the ZSTD_getDecompressedSize() legacy fallback returns 0 for frames without a content size field. This leads to malloc(0) followed by out-of-bounds access on the returned pointer.

Reject frames where the reported content size is zero or smaller than the expected decoded length.

Reproducer:

   mkdir testdir
   python3 -c "open('testdir/f','wb').write(b'A'*131072)"
   mkfs.erofs -zzstd test.erofs testdir/
   python3 -c "d=bytearray(open('test.erofs','rb').read());\
     p=d.find(b'\x28\xb5\x2f\xfd');d[p+4]=0x20;d[p+5]=0x01;\
     open('test.erofs','wb').write(d)"
   fsck.erofs --extract=out test.erofs
   # Expected: ZSTD frame content size 1 < decoded length 131072
---
  lib/decompress.c | 7 +++++++
  1 file changed, 7 insertions(+)
lib/decompress.c +0 -0
index 3e7a173..fb81039 100644
@@ -48,7 +48,14 @@ static int z_erofs_decompress_zstd(struct

z_erofs_decompress_req *rq)

lib/decompress.c static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq) · line 52

Quoted 5 lines
lib/decompress.c +2 -0
  #else
      total = ZSTD_getDecompressedSize(src + inputmargin,
                                       rq->inputsize - inputmargin);
+     if (!total)
+             return -EFSCORRUPTED;

hmm, that is the difference between the kernel and erofs-utils implementation.

the kernel uses zstd streaming APIs, so it won't malloc() a new buffer in advance, actually I think erofs-utils should switch to streaming APIs too, in order to avoid

ZSTD_getFrameContentSize() and ZSTD_getDecompressedSize()

dependencies as you said in the commit message.

Thanks,
Gao Xiang

#4 lore raw

PATCH v2 2/2 erofs-utils: lib: return error on ZSTD decompression length mismatch

In reply to Utkal Singh

When ZSTD_decompress() succeeds but produces a different number of bytes than expected, the code logs an error and jumps to cleanup. However, it does not set ret to an error code. Since ret still holds the positive ZSTD output size, the caller treats it as success via the 'if (ret < 0)' check in z_erofs_read_one_data(), causing silently corrupted data to be returned.

Set ret to -EIO before jumping to cleanup, consistent with the ZSTD_isError() error handling path above.

---
 lib/decompress.c | 1 +
 1 file changed, 1 insertion(+)
lib/decompress.c +1 -0
index fb81039..a27881c 100644
@@ -75,6 +75,7 @@ static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq)
 	if (ret != (int)total) {
 		erofs_err("ZSTD decompress length mismatch %d, expected %d",
 			  ret, total);
+		ret = -EIO;
 		goto out;
 	}
 	if (rq->decodedskip || total != rq->decodedlength)
Signature · 1 line
2.43.0