From: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
To: 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, linux-kernel@vger.kernel.org,
Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table
Date: Thu, 3 Dec 2026 09:40:46 +0800 [thread overview]
Message-ID: <20261203014046.3154332-1-ericyunlinchiang@gmail.com> (raw)
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/
next reply other threads:[~2026-12-03 1:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 1:40 Eric Yun Lin Chiang [this message]
2026-12-03 1:50 ` [PATCH] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table sashiko-bot
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=20261203014046.3154332-1-ericyunlinchiang@gmail.com \
--to=ericyunlinchiang@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=mwalle@kernel.org \
--cc=pratyush@kernel.org \
--cc=richard@nod.at \
--cc=stable@vger.kernel.org \
--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