Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
@ 2026-12-03  1:40 Eric Yun Lin Chiang
  2026-12-03  1:50 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Yun Lin Chiang @ 2026-12-03  1:40 UTC (permalink / raw)
  To: Pratyush Yadav, Michael Walle
  Cc: Takahiro Kuwano, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd, linux-kernel, Eric Yun Lin Chiang,
	stable

The spi_nor_parse_smpt() sizes its buffer from the table length the
flash reports in the SFDP parameter header, and
spi_nor_get_map_in_use() then walks the table with indices that come
from the table itself. Two of those reads are not bounded by the length.

A configuration detection command descriptor is two DWORDs, the
command and its address. If the last DWORD of the table is a command
descriptor, the address is read from one DWORD past the end of the
buffer.

The matching configuration map is returned without checking that the
region DWORDs following its header are inside the table. The
spi_nor_init_non_uniform_erase_map() then reads SMPT_MAP_REGION_COUNT()
of them, up to 256 DWORDs after the header, and builds the erase map
from whatever it finds there.

Reject a table that is too short for what it describes. The other
optional table parsers already check their length. The table is
optional, so this isn't fatal. The spi_nor_parse_sfdp() warns and
carries on.

Fixes: b038e8e3be72 ("mtd: spi-nor: parse SFDP Sector Map Parameter Table")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
---
Notes (not for the changelog):

- Testing: I have no flash with a Sector Map table (the part on my
  board, an mx66l1g45g, has none), so this is not runtime tested on
  hardware. spi_nor_get_map_in_use() was extracted unmodified, before
  and after this change, into a userspace harness built with
  AddressSanitizer, followed by the reads that
  spi_nor_init_non_uniform_erase_map() makes:
  - a table whose last DWORD is a command descriptor, and a map whose
    region count runs past the end: heap-buffer-overflow (READ of size
    4) before, -EINVAL after;
  - three well-formed tables (a detection command and two maps,
    selecting the first or the second map, and a map that exactly
    fills the table): the same map and region values before and after.
  Built for ARM (aspeed_g5_defconfig) with W=1, no warnings.
- Tool use: found, and this change and its test drafted, with the help
  of an AI coding assistant (Claude).

 drivers/mtd/spi-nor/sfdp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
index a9668c83ac5e..5c3774ffad69 100644
--- a/drivers/mtd/spi-nor/sfdp.c
+++ b/drivers/mtd/spi-nor/sfdp.c
@@ -774,6 +774,12 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
 		if (smpt[i] & SMPT_DESC_TYPE_MAP)
 			break;
 
+		/* A command descriptor is two DWORDs. */
+		if (i + 1 >= smpt_len) {
+			ret = ERR_PTR(-EINVAL);
+			goto out;
+		}
+
 		read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
 		nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]);
 		nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
@@ -805,7 +811,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;
 		}
 
-- 
2.53.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] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03  1:50 UTC (permalink / raw)
  To: Eric Yun Lin Chiang, Pratyush Yadav, Michael Walle
  Cc: Takahiro Kuwano, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  1:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox