Linux-EROFS Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yifan Zhao <stopire@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: Yifan Zhao <stopire@gmail.com>
Subject: [PATCH 6/8] erofs-utils: lib: fix gzran EOF handling and state leaks
Date: Thu, 12 Nov 2026 20:47:23 +0800	[thread overview]
Message-ID: <20261112124725.2774244-7-stopire@gmail.com> (raw)
In-Reply-To: <20261112124725.2774244-1-stopire@gmail.com>

An initial EOF can produce an invalid inflate input length, and the
state allocated by inflateInit2() is never freed.

Let's reject initial EOF with -EIO and call inflateEnd() on all exits
after successful initialization.

Fixes: 80b2ba447fb0 ("erofs-utils: lib: add gzran virtual file support")
Assisted-by: Codex:gpt-6-astra
Assisted-by: Claude Code:deepseek-v4.1-flash-1017
Signed-off-by: Yifan Zhao <stopire@gmail.com>
---
 lib/gzran.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/lib/gzran.c b/lib/gzran.c
index a75756c..dcc4497 100644
--- a/lib/gzran.c
+++ b/lib/gzran.c
@@ -231,7 +231,7 @@ static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t
 	bool skip = true;
 	u64 inpos, remin;
 	z_stream strm;
-	int ret;
+	ssize_t ret;
 
 	if (offset == ~0ULL) {
 		DBG_BUGON(1);
@@ -257,8 +257,10 @@ static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t
 	remin -= inpos;
 	ret = erofs_io_pread(ios->vin, src,
 			     min(remin, (u64)sizeof(src)), inpos);
-	if (ret < 0)
-		return ret;
+	if (ret <= 0) {
+		ret = ret < 0 ? ret : -EIO;
+		goto out;
+	}
 	if (u.bits) {
 		inflatePrime(&strm, u.bits, src[0] >> (8 - u.bits));
 		strm.next_in = src + 1;
@@ -294,10 +296,10 @@ static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t
 				ret = erofs_io_pread(ios->vin, src,
 						     min(remin, (u64)sizeof(src)),
 						     inpos);
-				if (ret < 0)
-					return ret;
-				if (!ret)
-					return -EIO;
+				if (ret <= 0) {
+					ret = ret < 0 ? ret : -EIO;
+					goto out;
+				}
 				inpos += ret;
 				remin -= ret;
 				strm.avail_in = ret;
@@ -306,8 +308,10 @@ static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t
 			ret = inflate(&strm, Z_NO_FLUSH);       /* normal inflate */
 			if (ret == Z_NEED_DICT)
 				ret = Z_DATA_ERROR;
-			if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
-				return -EIO;
+			if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR) {
+				ret = -EIO;
+				goto out;
+			}
 			if (ret == Z_STREAM_END)
 				break;
 		} while (strm.avail_out);
@@ -318,7 +322,10 @@ static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t
 
 		/* do until offset reached and requested data read, or stream ends */
 	} while (skip);
-	return len - strm.avail_out;
+	ret = len - strm.avail_out;
+out:
+	inflateEnd(&strm);
+	return ret;
 }
 
 static struct erofs_vfops erofs_gzran_ios_vfops = {
-- 
2.56.0



  parent reply	other threads:[~2026-11-12 12:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 12:47 [PATCH 0/8] erofs-utils: fix issues found by LLMs Yifan Zhao
2026-11-12 12:47 ` [PATCH 1/8] erofs-utils: lib: fix arithmetic overflow in large file handling Yifan Zhao
2026-11-12 12:47 ` [PATCH 2/8] erofs-utils: mkfs: fix incremental superblock stack overread Yifan Zhao
2026-11-12 12:47 ` [PATCH 3/8] erofs-utils: tar: fix out-of-bounds access when trimming PAX paths Yifan Zhao
2026-11-12 12:47 ` [PATCH 4/8] erofs-utils: lib: fix buffer leaks in error paths Yifan Zhao
2026-11-12 23:20   ` Gao Xiang
2026-11-12 12:47 ` [PATCH 5/8] erofs-utils: exclude: fix stack overflow when checking long paths Yifan Zhao
2026-11-12 12:47 ` Yifan Zhao [this message]
2026-11-12 12:47 ` [PATCH 7/8] erofs-utils: lib: fix metabox inline data reads Yifan Zhao
2026-11-12 12:47 ` [PATCH 8/8] erofs-utils: fix read error and short read handling Yifan Zhao
2026-11-12 23:41   ` Gao Xiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112124725.2774244-7-stopire@gmail.com \
    --to=stopire@gmail.com \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox