[PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure
From: Karthikeyan KS
Date: Wed Dec 02 2026 - 22:03:30 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Karthikeyan KS <karthiproffesional@xxxxxxxxx>
---
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@xxxxxxxxx
resend: https://lore.kernel.org/20261202142144.6358-1-karthiproffesional@xxxxxxxxx
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