Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Eric Yun Lin Chiang" <ericyunlinchiang@gmail.com>,
	"Pratyush Yadav" <pratyush@kernel.org>,
	"Michael Walle" <mwalle@kernel.org>
Cc: "Takahiro Kuwano" <takahiro.kuwano@infineon.com>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	linux-mtd@lists.infradead.org
Subject: Re: [PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
Date: Thu, 03 Dec 2026 01:50:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-246907@kernel.org> (raw)
In-Reply-To: <20261203014046.3154332-1-ericyunlinchiang@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtd: spi-nor: sfdp: unchecked non-matching map in
  spi_nor_get_map_in_use()
--

commit 6b0efb20b05feff35c7f8d25b658a5320e5b9774
Author: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>

mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table

This commit adds bounds checks in spi_nor_get_map_in_use() so that detection
command descriptors and the selected configuration map's region descriptors
do not read past the end of the Sector Map Parameter Table buffer.

> diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
> index e0854b8f1d551..035c155e818d3 100644
> --- a/drivers/mtd/spi-nor/sfdp.c
> +++ b/drivers/mtd/spi-nor/sfdp.c

[ ... ]

> @@ -879,7 +885,9 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
>  	ret = ERR_PTR(-EINVAL);
>  	while (i < smpt_len) {
>  		if (SMPT_MAP_ID(smpt[i]) == map_id) {
> -			ret = smpt + i;
> +			/* The map header is followed by one DWORD per region. */
> +			if (i + SMPT_MAP_REGION_COUNT(smpt[i]) < smpt_len)
> +				ret = smpt + i;
>  			break;
>  		}

[Severity: Medium]
What happens if a non-matching map descriptor has a region count that runs
past smpt_len?

Because the bounds check only runs when SMPT_MAP_ID(smpt[i]) == map_id, a
non-matching descriptor without SMPT_DESC_END reaches:

drivers/mtd/spi-nor/sfdp.c:spi_nor_get_map_in_use() {
	...
		/* increment the table index to the next map */
		i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
	}

Since i is a u8 and SMPT_MAP_REGION_COUNT(smpt[i]) + 1 can be up to 257, an
unvalidated non-matching descriptor where i + SMPT_MAP_REGION_COUNT(smpt[i])
+ 1 >= 256 wraps i modulo 256.  In particular, SMPT_MAP_REGION_COUNT() == 255
adds 256 and leaves i unchanged, spinning forever in while (i < smpt_len),
while other wrapped values can jump backwards into earlier command or region
DWORDs.

Checking that i + SMPT_MAP_REGION_COUNT(smpt[i]) < smpt_len at the start of
the while (i < smpt_len) loop (and breaking out on failure) would validate
every traversed map descriptor and prevent u8 wraparound when advancing i.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203014046.3154332-1-ericyunlinchiang@gmail.com?part=1

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

      reply	other threads:[~2026-12-03  1:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  1:40 [PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table Eric Yun Lin Chiang
2026-12-03  1:50 ` 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-246907@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ericyunlinchiang@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=takahiro.kuwano@infineon.com \
    --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