From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796265373; b=hpYN6Y1BOfX4CERYnYFqiQmCG5COk65IzWlQwhRXIhAtWiP3vADWyXKSxWYN5/Az5veB Cn5a8t/P89O5COGaFq3WErOHHX9kjcwZBMaA9N3B5fnq+u/XpI49ksMCD12DszypF5Ehy qgYaXaO7+jDSoEkfhpEf8kEpp/1JITJH1ny3khRYnKgxL6Yfpgn16BVAp3g7L0QcPdIeV h/Ql12sY+LmiqvvaybQ4+ek1rCha4nK61OlA2/jzBawPCW1R5tYsI3SsfLf7/Ju47oWqw sZ6CFpSniNuq2oErWuutxSrJjGj33sEl1KcH/74+qgJTiUZEdKGXMYxU/b3AMO/q97Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796265373; 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=m+2mUIz/ByRdOD5vmZ7fsNhzDNrdAl6j8HyOivjZUQ4=; b=gJcpP2NM9yc1g9uPCqO4TYW3wq2LhaIP9Kii7YPUJbL3UNvFNQ5mvyLrmNmnYeqxV/iK oCrJ/3LoM4JmMymRzY/MwBg+Jmqio1vx9eLPiL0GboHhKgK+QOHjGsOZX8GuRyTViclOn mfHj3j3ot3HTLgFt86VVwohvedBsZMBYvulilo4qGfGiin+uTFqKTPjoy4U1mhye0w94j EuloGxZjLz3ZvwCQvVoWjV+YNzbKZN6/tad9/ugjlAAUkcNT8u5wXGM01Yyw0z63sQ65d y2umROP9aWlrEVSpOoDpczDi7sdKkZ2cC4apI7Un2WosVrdVTpKV6OzqV1vZLyYcTtg== 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=b1sgJ5vR; arc=none smtp.remote-ip=209.85.214.174 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 AA5CA606A322 for ; Thu, 3 Dec 2026 02:36:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AA5CA606A322 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=b1sgJ5vR Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2f0414c1062so83227135ad.2 for ; Wed, 02 Dec 2026 18:36:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796265372; x=1796870172; 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=m+2mUIz/ByRdOD5vmZ7fsNhzDNrdAl6j8HyOivjZUQ4=; b=b1sgJ5vRo7EgfZ7+kZrwcawjDpM0QlHNFVsvEGXxTiUZUTEeZux6DBQQxJCx7+dm78 a/t7mMO0+fzPNqDKWVjKARE8Vq1SCLGrrx688YVCD9gOTJzSTBAYyMZ461rMODMyyAdt LNv2GeT2m5aJsUIvBZSkrr8JjFd07WOEVSJT/17rH2f/a3QMkH6hroAJhpDLxifUBVkq OtgM9CR/ybLeDXzqN7tHWcyuHvJ57UVloTP+moXOkdU29ed+coKCbjT6v6Tll0Ot2GBl YlWjXEque+brk+S5ohcTJAI3P/2KeH0BodoJmlS303pUCXTmvRJwjeds6DigCNF5zNvF W5qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796265372; x=1796870172; 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=m+2mUIz/ByRdOD5vmZ7fsNhzDNrdAl6j8HyOivjZUQ4=; b=Xr1Phh8a8ehpeljNomKpb0OKFLjoMb6JNrZtwo6AOc2vzD0bsPkZx8g72WCEaubNB3 3natMy9P4otXfYiDW68OmW2CircHivOcUi15yuaar36QQLwAbSzV8qrtmdz3idzdvMs0 emoz+YDelP8ywQjnDrpgfVQrRE06/Pij1vcvcahrBd4NiyMp1orgChmQBoJSh4GjKO99 Rrk3XQpQkovOIS6mzg7PO1DzGm6vczSmvjZrgJQIW3jDw3OERl1uzXWEo7LxcU+5hbGn WpKN19DiVi4pTqPEBzz5gvkya+dWSAJ9KSdIK5jjd4tHUI3vbvayCziBPgoN/1z6ts/D 2/lg== X-Forwarded-Encrypted: i=1; AL+hErE+So5OLdZf4X+NfqGYtxXjgWrDb2uyHMmeXYEz5REh8OtCyvsRr4RTLJIKe3YofzBP56Spl0dlURblNXk=@vger.kernel.org X-Gm-Message-State: APuU4Z9TMQK0wWig6daHGuZjOepjKKAB2ThzXrlVx1eZH18f35lbktGN HhFbBiiwPwForNkkCHBy9zqoXiwM2nd5WyAKKa0eBjBWiP+hpioKm20v X-Gm-Gg: AZFdTZealcW203T6WPSDxIQIx/yOkQmwdZlszYnhE8Q0hJlTFpmQiguNMPjz8cqauNv PnVofkksGFn5Ixq4UbdtHR6sGqhqMl4uFms/JRaB847EvelBWxeIYYpkvfwr9/S1D6pdXGqv/xs QZUxIY3DFg68yUrUY1BraRyJVDx6jNVEYcRbo2SMmX41Sp85Baqkv2o7PIjhb1kZpWTnqDuRXUD A3oiEPipvlqhFx4NOZszf4CqvVdR3Gg7uhsNG16XNM1R/b6xnBf9SIeJLWEUOh+0dQyErq+c4sU HxcMhqkK7dWwj1tRBPdEAz/89l4gfjciJ456lTkjblf6MS+9Dmq11i5c8MDPlZHvXK5rAwA6JCs sHRhobPGSvpitTqaqGdOOfehORm09MIjBRSJiWViNqAx41LXO/4S221H4iE5bsPA3v8uTh+MKt4 NNw9J/BpBg1/lshr+q9XvOcJfUrlAdVRuvA67CZ5yQREO8FdycbjmyelGY4Nj/P8QulkoJfscEs Mwpm6FzP9rY4+ZLMjx8sB1FsqLW4/f0F/Miwo8qk5YkWFfiNQzbgzAx2I4A0HnPTeE4xw04XeWe k3s0YKKsWOk0dWYESneZmo9AtwYpIljpMBJwdjRXmo7LOGGwgp6KLKue4yCA1QdvAQNixw== X-Received: by 2002:a17:902:f546:b0:2f0:3123:8990 with SMTP id d9443c01a7336-2f03f17dfe3mr405058785ad.50.1796265371587; Wed, 02 Dec 2026 18:36:11 -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 d9443c01a7336-2f162738c93sm40213975ad.43.2026.12.02.18.36.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 18:36:10 -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, Sashiko Subject: [PATCH v2] mtd: spi-nor: sfdp: check the length of the Sector Map Parameter Table Date: Thu, 3 Dec 2026 10:35:29 +0800 Message-ID: <20261203023529.3266848-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. 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@vger.kernel.org Reported-by: Sashiko Closes: https://lore.kernel.org/r/sashiko-outbox-246907@kernel.org Signed-off-by: Eric Yun Lin Chiang --- 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@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 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