Skip to contentSkip to conversation

nvmem: validate access ranges and fix SPRD reads

PATCH New Pradhan, Sanman 1 participant 3 messages Nov 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover nvmem: validate access ranges and fix SPRD reads

From: Sanman Pradhan <[email protected]>

Patch 1 adds a device-size bounds check to the nvmem core, so an out-of-range request is rejected before it reaches a provider. v1 open-coded this check in the sprd driver; since v2 it is in the core instead.

Patch 2 fixes sprd_efuse_read(), which reads one four-byte block but copies the caller's full length from that stack variable, and never advances past the first block. Its provider-side range check is removed in favour of patch 1, so patch 2 should be applied after patch 1:
without the core check an out-of-range in-kernel request would let the new block loop read past the eFuse region.

Both patches are compile-tested only. No Spreadtrum eFuse hardware was available for runtime testing. The bounds check intentionally permits a zero-length access at offset == size and rejects all ranges extending past the device size.

Changes in v3:
- Rebase onto nvmem-next (for-next), as requested by Srinivas.
- Patch 1: nvmem-next reworked the access paths, and cell reads and writes no longer go through nvmem_reg_read()/nvmem_reg_write(). Move the check into nvmem_access_locked(), which every device and cell access now passes through, so the coverage is the same as in v2.
- Patch 2: no change.

Changes in v2:
- Add the range check in the nvmem core instead of the sprd provider (new patch 1), as suggested by Srinivas.
- Patch 2: drop the provider-side range check, qualify the sysfs note with CONFIG_NVMEM_SYSFS, and use min() instead of min_t().

Link to v2: https://lore.kernel.org/all/[email protected]/
Link to v1: https://lore.kernel.org/all/[email protected]/

Sanman Pradhan (2):
nvmem: core: reject reads and writes beyond the device size
nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()

 drivers/nvmem/core.c       |  3 +++
 drivers/nvmem/sprd-efuse.c | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

base-commit: 9c938f8aa8244009ced6a8618bac8583ad49b38a

Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 nvmem: core: reject reads and writes beyond the device size

From: Sanman Pradhan <[email protected]>

The nvmem core passes the requested offset and length to the provider callbacks without checking them against the device size. The sysfs binary attribute is bounded by its size, but the in-kernel device and cell APIs are not, so a caller can request a range that extends past the end of the device.

Add a bounds check to nvmem_access_locked(), the common entry point for device and cell reads and writes, so out-of-range requests are rejected before keepout processing or provider access, rather than relying on each provider to validate. The subtraction-based test avoids an offset + bytes overflow.

Fixes 69aba7948cbe ("nvmem: Add a simple NVMEM framework for consumers")

---

Changes in v3:
- Rebase onto nvmem-next. Cell reads and writes there no longer go through nvmem_reg_read()/nvmem_reg_write(), so move the check into nvmem_access_locked(), which all device and cell accesses share. Coverage is unchanged from v2.

Changes in v2:
- New patch: add the range check in the nvmem core instead of the sprd provider.

Compile-tested only.

Note for stable: trees without "nvmem: drain keepout and cell post-processing accesses" do not have nvmem_access_locked(). The v2 version of this patch, which adds the check to nvmem_reg_read() and nvmem_reg_write(), still applies there (checked on linux-6.12.y).

 drivers/nvmem/core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nvmem/core.c +3 -0
index 6f451293b282..cf263633a4a2 100644
@@ -93,6 +93,9 @@ static int nvmem_access_locked(struct nvmem_device *nvmem,
 	const struct nvmem_keepout *keepoutend;
 	int rc;
 
+	if (offset > nvmem->size || bytes > nvmem->size - offset)
+		return -ERANGE;
+
 	if (!nvmem->nkeepout)
 		return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write);
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/2 nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()

In reply to Pradhan, Sanman

From: Sanman Pradhan <[email protected]>

sprd_efuse_read() reads a single four-byte block into the stack variable "data" and then copies the caller's full "bytes" length from it. The provider advertises word_size and stride of 1, so a whole-device sysfs read reaches the callback as a single 96-byte request, and __nvmem_cell_read() likewise submits cell->raw_len in one call.

Reading the whole device (96 bytes for ums312_data) therefore copies 92 bytes past the end of the four-byte stack variable into the caller's buffer. The read is reachable from the nvmem sysfs file when CONFIG_NVMEM_SYSFS is enabled, and from any DT cell wider than four bytes. Only the first block is read, so the returned data is also
incorrect: later blocks are never fetched.

Read and copy one block at a time, capping each copy to the bytes left in the block so it cannot exceed sizeof(data), and account for an unaligned starting offset.

Fixes 096030e7f449 ("nvmem: sprd: Add Spreadtrum SoCs eFuse support")

---

Changes in v3:
- Rebase onto nvmem-next; no change.

Changes in v2:
- Drop the provider-side offset + bytes range check; it is now done in the nvmem core (patch 1).
- Qualify the sysfs note with CONFIG_NVMEM_SYSFS, and use min() instead of min_t() (both operands are size_t).

Compile-tested only (NVMEM_SPRD_EFUSE, ARCH_SPRD || COMPILE_TEST); no Spreadtrum eFuse hardware available for runtime testing.

 drivers/nvmem/sprd-efuse.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/nvmem/sprd-efuse.c +14 -3
index 1a7e4e5d8b86..82995f8bce47 100644
@@ -5,6 +5,7 @@
 #include <linux/delay.h>
 #include <linux/hwspinlock.h>
 #include <linux/io.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/nvmem-provider.h>
 #include <linux/of.h>
@@ -297,6 +298,7 @@ static int sprd_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 	bool blk_double = efuse->data->blk_double;
 	u32 index = offset / SPRD_EFUSE_BLOCK_WIDTH + efuse->data->blk_offset;
 	u32 blk_offset = (offset % SPRD_EFUSE_BLOCK_WIDTH) * BITS_PER_BYTE;
+	u8 *buf = val;
 	u32 data;
 	int ret;
 
@@ -308,10 +310,19 @@ static int sprd_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 	if (ret)
 		goto unlock;
 
-	ret = sprd_efuse_raw_read(efuse, index, &data, blk_double);
-	if (!ret) {
+	while (bytes) {
+		size_t avail = SPRD_EFUSE_BLOCK_WIDTH - blk_offset / BITS_PER_BYTE;
+		size_t count = min(bytes, avail);
+
+		ret = sprd_efuse_raw_read(efuse, index++, &data, blk_double);
+		if (ret)
+			break;
+
 		data >>= blk_offset;
-		memcpy(val, &data, bytes);
+		memcpy(buf, &data, count);
+		buf += count;
+		bytes -= count;
+		blk_offset = 0;
 	}
 
 	clk_disable_unprepare(efuse->clk);
Signature · 1 line
2.34.1