From: Karthikeyan KS <karthiproffesional@gmail.com>
To: Richard Weinberger <richard@nod.at>,
David Woodhouse <dwmw2@infradead.org>
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
Haris Iqbal <haris.iqbal@linux.dev>,
Karthikeyan KS <karthiproffesional@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH v2 1/2] jffs2: return -EIO when a decompressor reports failure
Date: Thu, 3 Dec 2026 03:03:09 +0000 [thread overview]
Message-ID: <20261203030310.261227-2-karthiproffesional@gmail.com> (raw)
In-Reply-To: <20261203030310.261227-1-karthiproffesional@gmail.com>
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
next prev parent reply other threads:[~2026-12-03 3:03 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 3:03 [PATCH v2 0/2] jffs2: return -EIO from failing decompressors Karthikeyan KS
2026-12-03 3:03 ` Karthikeyan KS [this message]
2026-12-03 3:03 ` [PATCH v2 2/2] jffs2: return -EIO when a zlib node does not decode completely Karthikeyan KS
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=20261203030310.261227-2-karthiproffesional@gmail.com \
--to=karthiproffesional@gmail.com \
--cc=dwmw2@infradead.org \
--cc=haris.iqbal@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=richard@nod.at \
--cc=stable@vger.kernel.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
all inboxes | Powered by JetHome®