Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Hilliard" <james.hilliard1@gmail.com>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"David Woodhouse" <dwmw2@infradead.org>
Cc: linux-mtd@lists.infradead.org, "wang wei" <a929244872@163.com>
Subject: Re: [PATCH] mtd: rawnand: nandsim: validate overridesize before shifting
Date: Sun, 08 Nov 2026 21:12:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-204066@kernel.org> (raw)
In-Reply-To: <20261108-submit-nandsim-overridesize-bound-v1-v1-1-9cde7657a69b@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] mtd: rawnand: nandsim: unsigned integer overflow in
  ns_init() [1]
--

commit 172aea198c2ee0259ce74734f4b5dab1d08fa195
Author: James Hilliard <james.hilliard1@gmail.com>

mtd: rawnand: nandsim: validate overridesize before shifting

This commit validates the overridesize shift count using
check_shl_overflow() before computing the eraseblock count and total MTD
device size.

> diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> index fe968037f75a2..6716eafb4b004 100644
> --- a/drivers/mtd/nand/raw/nandsim.c
> +++ b/drivers/mtd/nand/raw/nandsim.c

[ ... ]

> @@ -2341,21 +2342,21 @@ static int __init ns_init_module(void)
>  	}
>  
>  	if (overridesize) {
> -		uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
>  		struct nand_memory_organization *memorg;
> +		unsigned int eraseblocks;
>  		u64 targetsize;
>  
>  		memorg = nanddev_get_memorg(&chip->base);
>  
> -		if (new_size >> overridesize != nsmtd->erasesize) {
> +		if (check_shl_overflow(1U, overridesize, &eraseblocks)) {
>  			NS_ERR("overridesize is too big\n");
>  			ret = -EINVAL;
>  			goto cleanup_nand;
>  		}
>  
>  		/* N.B. This relies on nand_scan not doing anything with the size before we change it */
> -		nsmtd->size = new_size;
> -		memorg->eraseblocks_per_lun = 1 << overridesize;
> +		nsmtd->size = (u64)nsmtd->erasesize * eraseblocks;
> +		memorg->eraseblocks_per_lun = eraseblocks;

[Severity: Critical]
This problem wasn't introduced by this patch, but can a large
overridesize that passes check_shl_overflow() still overflow the 32-bit
ns->geom.pgnum in ns_init() [1]?

drivers/mtd/nand/raw/nandsim.c:ns_init() {
	...
	ns->geom.totsz    = mtd->size;
	ns->geom.pgsz     = mtd->writesize;
	...
	ns->geom.pgnum    = div_u64(ns->geom.totsz, ns->geom.pgsz);
	...
}

Because each eraseblock contains multiple pages, an eraseblock count that
fits in unsigned int can still produce a total page count exceeding
UINT_MAX.  Truncating ns->geom.pgnum causes ns_init() to allocate a
smaller page array than mtd->size requires, leading to out-of-bounds
accesses during simulated flash operations.

[1] https://sashiko.dev/#/bug/linux-3c379a73-65df-4b5f-8179-95ceb2f44717

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261108-submit-nandsim-overridesize-bound-v1-v1-1-9cde7657a69b@gmail.com?part=1

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

      reply	other threads:[~2026-11-08 21:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-08 21:01 [PATCH] mtd: rawnand: nandsim: validate overridesize before shifting James Hilliard
2026-11-08 21:12 ` 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-204066@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a929244872@163.com \
    --cc=dwmw2@infradead.org \
    --cc=james.hilliard1@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vigneshr@ti.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox