Skip to contentSkip to conversation

erofs-utils: lib: add capacity ceiling in deflate partial decompression

PATCH New Utkal Singh 1 participant 2 messages Mar 15 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 erofs-utils: lib: add capacity ceiling in deflate partial decompression

In z_erofs_inflate_partial(), the dynamic buffer growth loop doubles decodedcapacity on every LIBDEFLATE_INSUFFICIENT_SPACE return with no upper bound. A crafted DEFLATE stream can trigger repeated realloc() calls, leading to unbounded memory consumption and an OOM crash in fsck.erofs or erofsfuse.

Introduce Z_EROFS_MAX_DECOMP_CAPACITY (64 MiB) and check the capacity before each doubling. If the limit is reached, log an error and abort with -EFSCORRUPTED via the existing out_inflate_end cleanup path.

This also prevents a theoretical integer overflow on the left-shift when decodedcapacity approaches the size_t limit.

---
 lib/decompress.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/decompress.c +6 -0
index 3e7a173..f87efd5 100644
@@ -240,6 +240,7 @@ int z_erofs_load_deflate_config(struct erofs_sb_info *sbi,
 #ifdef HAVE_LIBDEFLATE
 /* if libdeflate is available, use libdeflate instead. */
 #include <libdeflate.h>
+#define Z_EROFS_MAX_DECOMP_CAPACITY	(64U << 20)	/* 64 MiB ceiling */
 
 static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 {
@@ -281,6 +282,11 @@ static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 				ret = -EIO;
 				goto out_inflate_end;
 			}
+		if (decodedcapacity >= Z_EROFS_MAX_DECOMP_CAPACITY) {
+			erofs_err("deflate partial decompression capacity limit exceeded");
+			ret = -EFSCORRUPTED;
+			goto out_inflate_end;
+		}
 			decodedcapacity = decodedcapacity << 1;
 			dest = realloc(buff, decodedcapacity);
 			if (!dest) {
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 erofs-utils: lib: fix decodedcapacity integer overflow in inflate partial

decodedcapacity is declared as 'unsigned int' (32 bits). Two code paths can silently overflow it and corrupt the heap:

1. The initial assignment shifts rq->decodedlength left by 4 bits when partial_decoding is set. Values exceeding UINT_MAX >> 4 wrap to a small integer, causing malloc() to allocate an undersized buffer; a subsequent write overflows the heap.

2. The doubling loop left-shifts decodedcapacity by 1. Once it exceeds UINT_MAX >> 1 the result wraps to 0. On glibc, realloc(ptr, 0) returns a valid non-NULL pointer; the next write into that buffer is a silent heap overflow.

Fix both sites: change the type to size_t, cast rq->decodedlength to size_t before the initial shift to force 64-bit arithmetic, and add a guard before the doubling shift that returns -EFSCORRUPTED via out_inflate_end when the value would overflow SIZE_MAX.

---
 lib/decompress.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
lib/decompress.c +9 -3
index f87efd5..ff703ae 100644
@@ -4,6 +4,7 @@
  * Created by Huang Jianan <[email protected]>
  */
 #include <stdlib.h>
+#include <stdint.h>
 
 #include "erofs/decompress.h"
 #include "erofs/err.h"
@@ -251,13 +252,13 @@ static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 	unsigned int inputmargin;
 	struct libdeflate_decompressor *inf;
 	enum libdeflate_result ret;
-	unsigned int decodedcapacity;
+	size_t decodedcapacity;
 
 	inputmargin = z_erofs_fixup_insize(src, rq->inputsize);
 	if (inputmargin >= rq->inputsize)
 		return -EFSCORRUPTED;
 
-	decodedcapacity = rq->decodedlength << (4 * rq->partial_decoding);
+	decodedcapacity = (size_t)rq->decodedlength << (4 * rq->partial_decoding);
 	if (rq->decodedskip || rq->partial_decoding) {
 		buff = malloc(decodedcapacity);
 		if (!buff)
@@ -287,7 +288,12 @@ static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 			ret = -EFSCORRUPTED;
 			goto out_inflate_end;
 		}
-			decodedcapacity = decodedcapacity << 1;
+			if (decodedcapacity > SIZE_MAX >> 1) {
+				erofs_err("inflate: decompression buffer overflow");
+				ret = -EFSCORRUPTED;
+				goto out_inflate_end;
+			}
+			decodedcapacity <<= 1;
 			dest = realloc(buff, decodedcapacity);
 			if (!dest) {
 				ret = -ENOMEM;
Signature · 1 line
2.43.0