From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796262089; b=nQLCoF6oMDefV6OnX9HRMXnmKiKlXAU6HSbt5pfVSW/n9uZkn7DomIWHUcV/Lb3EV1jQ tm7fwQaPRKehevtIX71a98sRmH7VuudCpvJ+/X5OC33upTFxye+2LFR31zfzO0si2mbXm TSMmdAfwT2bdcKmm6LC4SDz/CX2ULBq5ER6h4KDX2lGC4xA7jD9K6ibYvGC9jhDQq6uuG XPDfxFwOCkVs51Pfhl8lrswF5Lcn04CFrf+9HzdOi6KG8U62kz+bj4UzyFQiezpEB2b26 Q1l9wKwqCu6od7N4sh3PzycWEqtF9U+DKsTdn64bbFmBqCVlxVhUn0rCIEuoMgBgu/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796262089; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=V0tHf3zico7xpQ0mLfy2n8l2oSYK1SjDXNwDjEz8O6E=; b=1IKLBGgcJpqq62ejtEvQYeNwyaMgLswykVmmjEG1OpWt+HqSAmS6BetTIqyL+4/uu/g9 20ulZ3Ux4iiM6gBi+JoTeDRgtDulZCy2BtYcu23dDvVn4ynqNE0mX0OWapbrjCFNfzsAR 4klpMhZAD9PM1CPQFonIjXJeZ8r+iTQvTyRso0IfG7co8Zr9hzPdugMtWIZeMAAbQYtXB r0NPtIfKkDa30nIk6RFf2p6FndxM8s60iV35PnYRcMIqY5x63LlOj3JZQV9VPsFBWTExa nywPo7ycRlau7RdVPrq9RQYxdU9n48/hjH1BPz7kO3PO4AMpIccgyCurqEP/3OXgqpw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZMuBgIqO; arc=none smtp.remote-ip=209.85.210.181 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54F36606A322 for ; Thu, 3 Dec 2026 01:41:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 54F36606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZMuBgIqO Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-8b5c3288e6cso5495704b3a.3 for ; Wed, 02 Dec 2026 17:41:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796262089; x=1796866889; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=V0tHf3zico7xpQ0mLfy2n8l2oSYK1SjDXNwDjEz8O6E=; b=ZMuBgIqOKE07SUJWDRN/nsyQ6LV1Tf3dh6Yz1+IrhHfeVvMXr7OkWGg5NZjqwPNOcl WiVHdSnyHXs1cbwY6cfo+0WRpX/1Q/53GZAgnipE4leXaAzlc2eA6ENbc4nuEmHzaZYM tO0mshSSDNJdklQaU/3qR8S8BTjN+Shjiarr1F+3vHSDjn3IqAx7YZKfi8ETyU5FVbYz vN/XJ0iAvhKMWQgTyGu2KAzes2FO/BfZiLUEL4mczfjLLbnh/ygFwjggTZnSTdMpC1Z7 s4I0R0uD1F11kj1ygMM3+1Mli3pBXWRrT3s8BCaV5PuFp0QFn6NEGwHFzi+F3rmp80kv gXEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796262089; x=1796866889; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=V0tHf3zico7xpQ0mLfy2n8l2oSYK1SjDXNwDjEz8O6E=; b=ncbZ70ivFjSd9Rvpss9GnRitcyZBrU0lvzEF0PUjwmcoIiCLC5fvU5K22Ug1x3I7mX 2i9OJEQHUHVK6q5C4kwBbRM0hfOoeH7jvt0Fa92j50yqi4uPoENufrtHdVcsHy4rJ9mw mMwXMk5HBqE8orTd3N4rfQE8zvVhcXTxYnZVy+TIByDKth243Y3RYS2skXO0vT/hxqAW 84IpQ7599sQe3CVbqpKAkMps+gSIAuWV+a06bfzZk18IGW1m/q5yMg/hlw0J+EkoCy+0 1vHdWabkqo+ph5gEfG7tcbJJOjaZ+P+2iiKeHctSu/YIJorEQuy/KYjHkQlx9Ro8HxWK tX3w== X-Forwarded-Encrypted: i=1; AL+hErFPQD2b1FTn3CFB/0WqUXn5JXfqcWTT3BJtZjJjG5uFPtl88Ebonyry0wE3xem+oPE6BZdc+H6ESS/7CWk=@vger.kernel.org X-Gm-Message-State: APuU4Z8Ajl0yBnoxzg9FLv9Pki2vUmaHatfMvg872R10Z/SQBfNYh+Rg 2ECatzDiAhb9/uDFmTcheXTmlBoeb2SzZlA2mZAjcpj7pHFl7vxZee9H X-Gm-Gg: AZFdTZcz9QOl3kXaXED2ibWJN+TFqVMOenpenbmCYQ9vZ40fOiQrZwCUlbV2GTE6VYB MfbXL4c1UA1+IfJiinVwYDhiFwpLXZHzWHKFm1hlvjPuGSog3zsAtHy/+DYgL/UZsu5F+qixlnT A9BshFI1daU9Bsa44sjnDrjvddYtSc439zemJdtp6VLPVX4Rt9ceGkaw9XFYLEORFIlQs8q5ptC vSb5GJ8wA/fO/3mr6Mf1/llCBCg9uz9bTIJNkxhcUvmTUdmuq04I9i1q7izWBGF6tN3lzc+hyTq eFakq8N7CjRH0yDuHUkCV3PDCSwz7aXRcmQE5N7l79kLOsXJ0E9YWBc0xvEXDTPGzh7gdu5bEHa XTHeJQAKKp+W2/2c9bSZTH64psIjBzdkZP91SkJAaiqNBUwivm5Sd98SK9AlbSTIivflvDw5hLd zLGbSrbhX4pnoHACCE1A5tcX9w57ArtJPMeWEF2OPv6PeK46GEzz0hjaurd/I8T3+LqRdWpBtbE NDstfSBEypdxzf145wxz9+dhZq/vY/NPBr+HCDnAWvGcnAsHf5z+4Y0Uzh+7bUOkxxi9edoko+X 48D3ETcrXZFPSxnUPRnXBexKjYz9ZFru2UnIRKfERl43XBCPPR6PkyB8xE0e0DNj4rpeUfhbTty 0xd1DtSKdOpXX69I= X-Received: by 2002:a05:6a00:c346:20b0:8dc:aeb3:6802 with SMTP id d2e1a72fcca58-8dcaeb36965mr532067b3a.3.1796262088492; Wed, 02 Dec 2026 17:41:28 -0800 (PST) Received: from eric-N-A (61-220-246-151.hinet-ip.hinet.net. [61.220.246.151]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c87285c05fsm22963089b3a.17.2026.12.02.17.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 17:41:28 -0800 (PST) From: Eric Yun Lin Chiang To: Pratyush Yadav , Michael Walle Cc: Takahiro Kuwano , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Eric Yun Lin Chiang , 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 Message-ID: <20261203014046.3154332-1-ericyunlinchiang@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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