* [PATCH] jffs2: validate summary entry offsets before scanning
@ 2026-11-26 12:28 shaos
2026-11-26 12:42 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: shaos @ 2026-11-26 12:28 UTC (permalink / raw)
To: dwmw2, richard; +Cc: linux-mtd, linux-kernel, stable
Hello,
A JFFS2 summary with valid CRCs can list the same on-flash node
offset more than once. The second entry is passed to
sum_link_node_ref(), and jffs2_link_node_ref() hits a BUG because
the new reference is not immediately after the preceding reference.
Mounting the crafted image on an MTD device therefore crashes the
kernel.
Validate summary entry sizes, offsets, ordering, and bounds before
adding any references. If validation fails, return to the existing
full eraseblock scan. The scan then uses the raw nodes, which remain
valid in the reproducer.
This is a manually constructed reproducer. It does not require a
sanitizer. Mounting the crafted image requires administrative access
to an MTD device. No unprivileged mount path, information disclosure,
or code execution is demonstrated.
HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4-g8d7fe9885c62
compiler: gcc 11.4.0, GNU ld 2.38
The Gist contains the complete console output, kernel configuration,
PoC, and summary-image builder:
https://gist.githubusercontent.com/shaos26/f9e640d9007f000a2af427056e64df76/raw/ce3f8ac28eccb971bc86f16d47083c43b9b7a6af/console-output.txt
https://gist.githubusercontent.com/shaos26/f9e640d9007f000a2af427056e64df76/raw/ce3f8ac28eccb971bc86f16d47083c43b9b7a6af/kernel-config.txt
https://gist.githubusercontent.com/shaos26/f9e640d9007f000a2af427056e64df76/raw/ce3f8ac28eccb971bc86f16d47083c43b9b7a6af/poc.c
https://gist.githubusercontent.com/shaos26/f9e640d9007f000a2af427056e64df76/raw/ce3f8ac28eccb971bc86f16d47083c43b9b7a6af/mkjffs2.py
To reproduce, build the kernel from the HEAD commit above using
kernel-config.txt. Then download poc.c and mkjffs2.py from the Gist
and run:
mkdir -p root
for kind in trigger control nosummary; do
python3 mkjffs2.py "$kind" "root/$kind.img"
done
gcc -O2 -static -Wall -Wextra -o root/init poc.c
(cd root && find . -print0 | cpio --null -o -H newc |
gzip -9) > initramfs.cpio.gz
Run the trigger in a disposable QEMU guest. The trigger is expected
to panic the unpatched kernel, so timeout is used to stop QEMU:
timeout -k 2 15 qemu-system-x86_64 -m 512 -smp 1 \
-kernel linux/build/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-append 'console=ttyS0 rdinit=/init testcase=trigger oops=panic panic=1' \
-display none -serial file:trigger.log -monitor none -no-reboot
Repeat with testcase=control and testcase=nosummary; both should
mount successfully. The Gist console output records these controls
and the patched-kernel run.
The unpatched kernel reproduces the BUG at fs/jffs2/nodelist.c:622.
With this patch, the malformed summary is rejected and the full scan
mounts the same image successfully. The valid-summary control also
mounts successfully.
Fixes: e631ddba5887 ("[JFFS2] Add erase block summary support (mount time improvement)")
Cc: stable@vger.kernel.org
Signed-off-by: shaos <shawnq1@163.com>
---
fs/jffs2/summary.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
diff --git a/fs/jffs2/summary.c b/fs/jffs2/summary.c
index 4521a7723f306..d18bfb71d9fdd 100644
--- a/fs/jffs2/summary.c
+++ b/fs/jffs2/summary.c
@@ -381,6 +381,96 @@ static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c,
return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic);
}
+/* Check summary offsets before they are used to build the node-ref list. */
+static int jffs2_sum_check_offsets(struct jffs2_sb_info *c,
+ struct jffs2_raw_summary *summary,
+ uint32_t sumsize)
+{
+ void *sp = summary->sum;
+ size_t remaining;
+ u32 prev_end, sum_start;
+ unsigned int i;
+
+ if (sumsize < sizeof(*summary) + sizeof(struct jffs2_sum_marker))
+ return -ENOTRECOVERABLE;
+
+ remaining = sumsize - sizeof(*summary) -
+ sizeof(struct jffs2_sum_marker);
+ prev_end = PAD(je32_to_cpu(summary->cln_mkr));
+ sum_start = c->sector_size - sumsize;
+
+ for (i = 0; i < je32_to_cpu(summary->sum_num); i++) {
+ u16 nodetype;
+ u32 offset, len;
+ size_t entry_size;
+
+ if (remaining < sizeof(struct jffs2_sum_unknown_flash))
+ return -ENOTRECOVERABLE;
+
+ nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype);
+ switch (nodetype) {
+ case JFFS2_NODETYPE_INODE: {
+ struct jffs2_sum_inode_flash *spi = sp;
+
+ entry_size = sizeof(*spi);
+ if (remaining < entry_size)
+ return -ENOTRECOVERABLE;
+ offset = je32_to_cpu(spi->offset);
+ len = je32_to_cpu(spi->totlen);
+ break;
+ }
+ case JFFS2_NODETYPE_DIRENT: {
+ struct jffs2_sum_dirent_flash *spd = sp;
+
+ if (remaining < offsetof(struct jffs2_sum_dirent_flash, name))
+ return -ENOTRECOVERABLE;
+ entry_size = JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
+ if (remaining < entry_size)
+ return -ENOTRECOVERABLE;
+ offset = je32_to_cpu(spd->offset);
+ len = je32_to_cpu(spd->totlen);
+ break;
+ }
+#ifdef CONFIG_JFFS2_FS_XATTR
+ case JFFS2_NODETYPE_XATTR: {
+ struct jffs2_sum_xattr_flash *spx = sp;
+
+ entry_size = sizeof(*spx);
+ if (remaining < entry_size)
+ return -ENOTRECOVERABLE;
+ offset = je32_to_cpu(spx->offset);
+ len = je32_to_cpu(spx->totlen);
+ break;
+ }
+ case JFFS2_NODETYPE_XREF: {
+ struct jffs2_sum_xref_flash *spr = sp;
+
+ entry_size = sizeof(*spr);
+ if (remaining < entry_size)
+ return -ENOTRECOVERABLE;
+ offset = je32_to_cpu(spr->offset);
+ len = sizeof(struct jffs2_raw_xref);
+ break;
+ }
+#endif
+ default:
+ /* Unknown types already require a full scan. */
+ return -ENOTRECOVERABLE;
+ }
+
+ if ((offset & 3) || offset < prev_end || offset > sum_start ||
+ !len || len > sum_start - offset ||
+ PAD(len) > sum_start - offset)
+ return -ENOTRECOVERABLE;
+
+ prev_end = offset + PAD(len);
+ sp += entry_size;
+ remaining -= entry_size;
+ }
+
+ return 0;
+}
+
/* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
@@ -613,6 +703,13 @@ int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
goto crc_err;
}
+ ret = jffs2_sum_check_offsets(c, summary, sumsize);
+ if (ret) {
+ JFFS2_WARNING("Invalid summary entries in eraseblock at 0x%08x\n",
+ jeb->offset);
+ return 0;
+ }
+
if ( je32_to_cpu(summary->cln_mkr) ) {
dbg_summary("Summary : CLEANMARKER node \n");
--
2.43.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] jffs2: validate summary entry offsets before scanning
2026-11-26 12:28 [PATCH] jffs2: validate summary entry offsets before scanning shaos
@ 2026-11-26 12:42 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26 12:42 UTC (permalink / raw)
To: shaos, dwmw2, richard; +Cc: linux-mtd
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] jffs2: unvalidated cln_mkr in jffs2_sum_check_offsets()
allows BUG()
- [Medium] jffs2: missing 4-byte alignment check on sumsize in
jffs2_sum_check_offsets()
- [Medium] jffs2: sumsize minimum check in jffs2_sum_check_offsets()
runs too late
--
commit 82ae7989db9dad47eeaf6152d81cd6b5f546ea4b
Author: shaos <shawnq1@163.com>
jffs2: validate summary entry offsets before scanning
This commit adds jffs2_sum_check_offsets() to validate summary entry
sizes, offsets, ordering, and bounds before building the node-ref list,
falling back to a full eraseblock scan when validation fails.
> diff --git a/fs/jffs2/summary.c b/fs/jffs2/summary.c
> index 4521a7723f306..d18bfb71d9fdd 100644
> --- a/fs/jffs2/summary.c
> +++ b/fs/jffs2/summary.c
> @@ -381,6 +381,96 @@ static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c,
> return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic);
> }
>
> +/* Check summary offsets before they are used to build the node-ref list. */
> +static int jffs2_sum_check_offsets(struct jffs2_sb_info *c,
> + struct jffs2_raw_summary *summary,
> + uint32_t sumsize)
> +{
> + void *sp = summary->sum;
> + size_t remaining;
> + u32 prev_end, sum_start;
> + unsigned int i;
> +
> + if (sumsize < sizeof(*summary) + sizeof(struct jffs2_sum_marker))
> + return -ENOTRECOVERABLE;
> +
> + remaining = sumsize - sizeof(*summary) -
> + sizeof(struct jffs2_sum_marker);
> + prev_end = PAD(je32_to_cpu(summary->cln_mkr));
> + sum_start = c->sector_size - sumsize;
[Severity: Medium]
Should jffs2_sum_check_offsets() also reject an unaligned sum_start (or
sumsize)?
The loop below checks (offset & 3) for each entry, but if (sum_start & 3)
is non-zero and all entries end at or before (sum_start & ~3), validation
succeeds. jffs2_sum_scan_sumnode() then calls sum_link_node_ref(c, jeb,
ofs | REF_NORMAL, sumsize, NULL), which marks dirty space only up to
(ofs & ~3) and links jeb->last_node at jeb->offset + (ofs & ~3) while
crediting only sumsize to jeb->used_size. After the remaining (ofs & 3)
bytes of jeb->free_size are moved to jeb->wasted_size and jeb->free_size
is zeroed, __ref_totlen() for jeb->last_node becomes sumsize + (ofs & 3),
exceeding jeb->used_size and triggering BUG() in
__jffs2_dbg_acct_paranoia_check_nolock() or during garbage collection in
jffs2_mark_node_obsolete().
> +
> + for (i = 0; i < je32_to_cpu(summary->sum_num); i++) {
[Severity: Medium]
Can an unvalidated summary->cln_mkr still trigger BUG() when
summary->sum_num is 0 or when PAD(je32_to_cpu(summary->cln_mkr)) wraps
around?
prev_end is only compared against sum_start inside this loop. When
summary->sum_num is 0, the loop does not run and jffs2_sum_check_offsets()
returns 0 for any summary->cln_mkr. In jffs2_sum_scan_sumnode(), a
cln_mkr with PAD(cln_mkr) > c->sector_size then hits BUG() in
jffs2_scan_dirty_space() because size > jeb->free_size, while a cln_mkr
with sum_start < PAD(cln_mkr) <= c->sector_size advances
c->sector_size - jeb->free_size past ofs before sum_link_node_ref() links
the summary node at ofs, hitting BUG() in jffs2_link_node_ref(). Also, if
je32_to_cpu(summary->cln_mkr) >= 0xfffffffd, PAD() overflows 32-bit
unsigned arithmetic and wraps prev_end to 0.
[ ... ]
> @@ -613,6 +703,13 @@ int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
> goto crc_err;
> }
>
> + ret = jffs2_sum_check_offsets(c, summary, sumsize);
> + if (ret) {
> + JFFS2_WARNING("Invalid summary entries in eraseblock at 0x%08x\n",
> + jeb->offset);
> + return 0;
> + }
[Severity: Medium]
Does the sumsize minimum check in jffs2_sum_check_offsets() run too late
to prevent an out-of-bounds read in jffs2_sum_scan_sumnode()?
When sm->offset in the on-flash jffs2_sum_marker makes sumsize smaller
than sizeof(struct jffs2_raw_summary), jffs2_sum_scan_sumnode() has
already read fields from *summary past the end of the scan buffer and
passed sumsize - sizeof(struct jffs2_raw_summary) to crc32(), which
underflows to a huge length before jffs2_sum_check_offsets() is reached:
fs/jffs2/summary.c:jffs2_sum_scan_sumnode() {
...
crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8);
if (je32_to_cpu(summary->node_crc) != crc) {
dbg_summary("Summary node is corrupt (bad CRC)\n");
goto crc_err;
}
crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary));
...
}
Checking that sumsize is at least sizeof(*summary) + sizeof(struct
jffs2_sum_marker) (and at most c->sector_size) before reading *summary at
the start of jffs2_sum_scan_sumnode() would avoid the out-of-bounds read
and length underflow.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126122848.1504727-1-shawnq1@163.com?part=1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 12:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 12:28 [PATCH] jffs2: validate summary entry offsets before scanning shaos
2026-11-26 12:42 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox