[PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely
From: Karthikeyan KS
Date: Wed Dec 02 2026 - 22:03:39 EST
jffs2_zlib_decompress() returns 0 when decoding did not finish: when the
stream breaks, or when it ends properly but produces fewer than destlen
bytes. The caller then returns the unwritten part of the buffer as file
data. For a read of a whole node, that is old page contents.
Return -EIO in both cases, as lzo already does for a short result. A
valid node always decodes to exactly destlen bytes: jffs2_zlib_compress()
and mtd-utils' mkfs.jffs2 both store total_in as its data size.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Karthikeyan KS <karthiproffesional@xxxxxxxxx>
---
New in v2. Independent of patch 1: it changes different lines of
compr_zlib.c and does not need patch 1.
Examples, both with a valid data CRC:
- A stream that breaks after 2240 of its 2560 bytes. Without this patch
read() returns all 2560 bytes without an error and the log only says
"jffs2: inflate returned -3". In a test that read another file and
evicted its page from the page cache just before the read, the last
320 bytes were that file's bytes. With init_on_alloc=1 they are zeros,
but the read still succeeds.
- A valid stream that ends properly after 2240 of 2560 bytes. It is also
caught, by the total_out check.
Tested on qemu ast2500-evb with crafted flash images (details in the
cover letter): with this patch both cases make read() and pwrite() fail
with EIO and no data is returned. Nine valid zlib files (58 zlib nodes
written by mkfs.jffs2) read back with the expected checksums.
Behaviour change: a node that does not decode completely used to be read
without an error and now fails with -EIO. Nodes written by
jffs2_zlib_compress() or mkfs.jffs2 are not affected.
fs/jffs2/compr_zlib.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/fs/jffs2/compr_zlib.c b/fs/jffs2/compr_zlib.c
index 856728ed3..282811da1 100644
--- a/fs/jffs2/compr_zlib.c
+++ b/fs/jffs2/compr_zlib.c
@@ -179,12 +179,16 @@ static int jffs2_zlib_decompress(unsigned char *data_in,
while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
;
- if (ret != Z_STREAM_END) {
- pr_notice("inflate returned %d\n", ret);
+ if (ret != Z_STREAM_END || inf_strm.total_out != destlen) {
+ pr_notice("inflate returned %d, %lu of %u bytes\n",
+ ret, inf_strm.total_out, destlen);
+ ret = -EIO;
+ } else {
+ ret = 0;
}
zlib_inflateEnd(&inf_strm);
mutex_unlock(&inflate_mutex);
- return 0;
+ return ret;
}
static struct jffs2_compressor jffs2_zlib_comp = {
--
2.43.0