[PATCH v2] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
From: Eric Yun Lin Chiang
Date: Wed Dec 02 2026 - 21:36:23 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. None of those steps is 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.
Each configuration map header is followed by SMPT_MAP_REGION_COUNT()
region DWORDs, and nothing checks that they are inside the table. For
the matching map, spi_nor_init_non_uniform_erase_map() then reads up to
256 DWORDs after the header and builds the erase map from whatever it
finds there. For a map that does not match, the index skips over the
regions, and since it is a u8, a region count of 255 adds 256 and
leaves it unchanged, so the loop never ends.
Reject a table that is too short for what it describes, checking every
map rather than only the matching one. 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
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/sashiko-outbox-246907@xxxxxxxxxx
Signed-off-by: Eric Yun Lin Chiang <ericyunlinchiang@xxxxxxxxx>
---
Changes in v2:
- Check every map descriptor, not only the matching one. A map that
does not match can also run past the table, and its region count can
wrap the u8 index so the loop never ends (Sashiko AI review).
- Link to v1: https://lore.kernel.org/r/20261203014046.3154332-1-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 matching
map whose region count runs past the end: heap-buffer-overflow
(READ of size 4) before, -EINVAL after;
- a non-matching map with a region count of 255: the loop never
ends before, -EINVAL after;
- a non-matching map whose regions run past the end: -EINVAL before
and 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 | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
index a9668c83ac5e..f2ac82420384 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]);
@@ -804,6 +810,14 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
*/
ret = ERR_PTR(-EINVAL);
while (i < smpt_len) {
+ /*
+ * The map header is followed by one DWORD per region. Checking
+ * every map, not only the matching one, also keeps the u8 index
+ * below from wrapping.
+ */
+ if (i + SMPT_MAP_REGION_COUNT(smpt[i]) >= smpt_len)
+ break;
+
if (SMPT_MAP_ID(smpt[i]) == map_id) {
ret = smpt + i;
break;
--
2.53.0