All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: shaos <shawnq1@163.com>, dwmw2@infradead.org, richard@nod.at
Cc: linux-mtd@lists.infradead.org
Subject: Re: [PATCH] jffs2: validate summary entry offsets before scanning
Date: Thu, 26 Nov 2026 12:42:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-235513@kernel.org> (raw)
In-Reply-To: <20261126122848.1504727-1-shawnq1@163.com>

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/

      reply	other threads:[~2026-11-26 12:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 12:28 [PATCH] jffs2: validate summary entry offsets before scanning shaos
2026-11-26 12:28 ` shaos
2026-11-26 12:42 ` sashiko-bot [this message]

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=sashiko-outbox-235513@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shawnq1@163.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.