mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] jffs2: return -EIO from failing decompressors
@ 2026-12-03  3:03 Karthikeyan KS
  2026-12-03  3:03 ` [PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure Karthikeyan KS
  2026-12-03  3:03 ` [PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely Karthikeyan KS
  0 siblings, 2 replies; 3+ messages in thread
From: Karthikeyan KS @ 2026-12-03  3:03 UTC (permalink / raw)
  To: Richard Weinberger, David Woodhouse
  Cc: linux-mtd, linux-kernel, Haris Iqbal, Karthikeyan KS

Hi Richard,

JFFS2's decompressors report failure in three different ways, and none
of them is the negative errno that generic code expects. rtime and zlib
(inflateInit failure) return 1: that crashes the kernel on read, write
and garbage collection, or makes read() return 1 forever. lzo returns
-1, which userspace sees as EPERM. zlib returns 0 when a stream breaks
or ends early, so the read succeeds with stale data from the page.

Patch 1 is v2 of "jffs2: return -EIO when a decompressor reports
failure" (v1 [1], resend [2]). v1 converted a positive return value in
jffs2_decompress(). This version returns -EIO from the decompressors
themselves. It also fixes the lzo -1 (EPERM) pointed out in the Sashiko
review.

Patch 2 is new. It fixes the zlib cases that the same review pointed out
as a separate, pre-existing issue, and one more that came up while
working on it: a stream that is valid but ends early. The two patches
change different lines and can be applied in either order.

How this was found: we backported fe051552f507 ("jffs2: Prevent rtime
decompress memory corruption") to our kernel without b29bf7119d6b
("jffs2: Fix rtime decompressor"). On an AST2500 SoC, the off-by-one in
the rtime bounds check then rejected a valid node. We reproduced the
resulting failure paths on qemu, using crafted flash images that make a
node fail to decompress.

Tested on qemu ast2500-evb (ARM, TCG) with mainline 693c2e04e3f1 and
both patches. The kernel is an aspeed_g5 configuration with
CONFIG_JFFS2_ZLIB, CONFIG_JFFS2_LZO, CONFIG_JFFS2_RTIME and
CONFIG_JFFS2_RUBIN enabled:

 - rtime, zlib (inflateInit failure) and lzo nodes that fail: read() and
   pwrite() fail with EIO, a mmap fault raises SIGBUS, nothing oopses.
   The four failures listed under patch 1 (read panic, read() returning 1
   forever, BUG in jffs2_write_end(), GC fault) no longer happen.
 - A zlib node whose stream breaks partway, and one whose stream is
   valid but ends after 2240 of 2560 bytes: read() and pwrite() fail
   with EIO. Without patch 2 the broken stream was read successfully and
   returned incomplete data.
 - Valid rtime, zlib, lzo and uncompressed files read and write as
   before. Nine valid zlib files (270 kB, 58 zlib nodes written by
   mkfs.jffs2) read back with the expected checksums.

Not tested: garbage collection with zlib or lzo nodes, and real hardware.

[1] https://lore.kernel.org/20261025173152.147123-1-karthiproffesional@gmail.com
[2] https://lore.kernel.org/20261202142144.6358-1-karthiproffesional@gmail.com

Karthikeyan KS (2):
  jffs2: return -EIO when a decompressor reports failure
  jffs2: return -EIO when a zlib node does not decode completely

 fs/jffs2/compr_lzo.c   |  2 +-
 fs/jffs2/compr_rtime.c |  2 +-
 fs/jffs2/compr_zlib.c  | 12 ++++++++----
 3 files changed, 10 insertions(+), 6 deletions(-)


base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure
  2026-12-03  3:03 [PATCH v2 0/2] jffs2: return -EIO from failing decompressors Karthikeyan KS
@ 2026-12-03  3:03 ` Karthikeyan KS
  2026-12-03  3:03 ` [PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely Karthikeyan KS
  1 sibling, 0 replies; 3+ messages in thread
From: Karthikeyan KS @ 2026-12-03  3:03 UTC (permalink / raw)
  To: Richard Weinberger, David Woodhouse
  Cc: linux-mtd, linux-kernel, Haris Iqbal, Karthikeyan KS, stable

jffs2_decompress() passes the decompressor's return value straight up.
The rtime and zlib decompressors report failure by returning a positive
1, and lzo by returning -1, which userspace sees as EPERM. A positive
value breaks the kernel's error convention: a function that can fail
returns 0 or a negative errno, and generic code (the page cache, the
write path, IS_ERR()) relies on that, so a positive value is read as
success. JFFS2's own callers only check for non-zero, which hides the
violation: the value travels unchanged through jffs2_read_dnode(),
jffs2_read_inode_range() and jffs2_do_readpage_nolock() to that generic
code.

That causes a crash in read(), a BUG in jffs2_write_end() on a partial
write, a fault in garbage collection, or a read() that returns 1 on
every retry.

Return -EIO from the failure paths of the rtime, zlib and lzo
decompressors. jffs2_decompress() is their only caller.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: c799aca31bfe ("[JFFS2] Add LZO compression support.")
Fixes: fe051552f507 ("jffs2: Prevent rtime decompress memory corruption")
Cc: stable@vger.kernel.org
Signed-off-by: Karthikeyan KS <karthiproffesional@gmail.com>
---
Changes in v2:
- Return -EIO from the rtime, zlib and lzo decompressors instead of
  converting a positive value in jffs2_decompress(). The Sashiko review
  pointed out that lzo's -1 reaches userspace as EPERM and suggested
  converting every non-zero value in jffs2_decompress(); returning -EIO
  from the decompressors fixes that as well.
- Add a Fixes: tag for the lzo return value.
- Describe the read() that returns 1 forever.

v1: https://lore.kernel.org/20261025173152.147123-1-karthiproffesional@gmail.com
resend: https://lore.kernel.org/20261202142144.6358-1-karthiproffesional@gmail.com

Failures seen on qemu without this patch (crafted flash images):

On a read, filemap_get_pages() returns 1 without filling its folio
batch, filemap_read() carries on and dereferences the empty batch:

  jffs2: Decompressor "rtime" returned 1
  jffs2: Error: jffs2_decompress returned 1
  Unhandled fault: page domain fault (0x01b) at 0x00000000
  PC is at filemap_read+0x1d8/0x5b4
  Call trace:
   filemap_read from vfs_read+0x23c/0x318
   vfs_read from ksys_read+0x74/0xf0
   ksys_read from ret_fast_syscall+0x0/0x54
  Kernel panic - not syncing: Fatal exception

If the folio is already in the page cache but not uptodate, for example
after a failed mmap fault, the failed read goes through
filemap_update_page() instead. filemap_get_pages() then returns the 1 as
success, filemap_read() copies no data and returns 1, and read() returns
1 without an error. It returns 1 again on every retry, so a caller that
retries short reads never finishes.

On a partial write, jffs2_write_begin() has to read the old page in.
When that fails it returns 1, generic_perform_write() carries on, and
jffs2_write_end() is called for a folio that is not uptodate:

  jffs2: Error: jffs2_decompress returned 1
  kernel BUG at fs/jffs2/file.c:270!
  PC is at jffs2_write_end+0x50/0x3b4
  Call trace:
   jffs2_write_end from generic_perform_write+0x144/0x280
   generic_perform_write from generic_file_write_iter+0x64/0x10c
   generic_file_write_iter from vfs_write+0x230/0x3e8
   vfs_write from sys_pwrite64+0xa0/0xc8

Garbage collection has the same problem. read_cache_folio() returns
ERR_PTR(1), which IS_ERR() does not treat as an error, and the "folio"
at address 0x1 gets mapped:

  jffs2: Error: jffs2_decompress returned 1
  Unhandled fault: page domain fault (0x01b) at 0x00000001
  PC is at __kmap_local_page_prot+0x8/0x5c
  LR is at jffs2_garbage_collect_dnode+0x1dc/0x518
  Call trace:
   __kmap_local_page_prot from jffs2_garbage_collect_dnode+0x1dc/0x518
   jffs2_garbage_collect_dnode from jffs2_garbage_collect_pass+0xa54/0xec4
   jffs2_garbage_collect_pass from jffs2_reserve_space+0x12c/0x344

 fs/jffs2/compr_lzo.c   | 2 +-
 fs/jffs2/compr_rtime.c | 2 +-
 fs/jffs2/compr_zlib.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/jffs2/compr_lzo.c b/fs/jffs2/compr_lzo.c
index c553bd650..21c625423 100644
--- a/fs/jffs2/compr_lzo.c
+++ b/fs/jffs2/compr_lzo.c
@@ -74,7 +74,7 @@ static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out
 	ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
 
 	if (ret != LZO_E_OK || dl != destlen)
-		return -1;
+		return -EIO;
 
 	return 0;
 }
diff --git a/fs/jffs2/compr_rtime.c b/fs/jffs2/compr_rtime.c
index 3bd9d2f3b..b3b5bc5a0 100644
--- a/fs/jffs2/compr_rtime.c
+++ b/fs/jffs2/compr_rtime.c
@@ -96,7 +96,7 @@ static int jffs2_rtime_decompress(unsigned char *data_in,
 		positions[value]=outpos;
 		if (repeat) {
 			if ((outpos + repeat) > destlen) {
-				return 1;
+				return -EIO;
 			}
 			if (backoffs + repeat >= outpos) {
 				while(repeat) {
diff --git a/fs/jffs2/compr_zlib.c b/fs/jffs2/compr_zlib.c
index 5698dae5d..856728ed3 100644
--- a/fs/jffs2/compr_zlib.c
+++ b/fs/jffs2/compr_zlib.c
@@ -174,7 +174,7 @@ static int jffs2_zlib_decompress(unsigned char *data_in,
 	if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
 		pr_warn("inflateInit failed\n");
 		mutex_unlock(&inflate_mutex);
-		return 1;
+		return -EIO;
 	}
 
 	while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely
  2026-12-03  3:03 [PATCH v2 0/2] jffs2: return -EIO from failing decompressors Karthikeyan KS
  2026-12-03  3:03 ` [PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure Karthikeyan KS
@ 2026-12-03  3:03 ` Karthikeyan KS
  1 sibling, 0 replies; 3+ messages in thread
From: Karthikeyan KS @ 2026-12-03  3:03 UTC (permalink / raw)
  To: Richard Weinberger, David Woodhouse
  Cc: linux-mtd, linux-kernel, Haris Iqbal, Karthikeyan KS, stable

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@vger.kernel.org
Signed-off-by: Karthikeyan KS <karthiproffesional@gmail.com>
---
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03  3:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  3:03 [PATCH v2 0/2] jffs2: return -EIO from failing decompressors Karthikeyan KS
2026-12-03  3:03 ` [PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure Karthikeyan KS
2026-12-03  3:03 ` [PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely Karthikeyan KS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®