[PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
From: Eric Yun Lin Chiang
Date: Wed Dec 02 2026 - 20:41:33 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Eric Yun Lin Chiang <ericyunlinchiang@xxxxxxxxx>
---
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