* [PATCH v2 1/2] nvmem: core: reject reads and writes beyond the device size
2026-09-21 16:51 [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads Pradhan, Sanman
@ 2026-09-21 16:51 ` Pradhan, Sanman
2026-09-21 16:51 ` [PATCH v2 2/2] nvmem: sprd: fix out-of-bounds read in sprd_efuse_read() Pradhan, Sanman
2026-11-27 11:44 ` [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads Srinivas Kandagatla
2 siblings, 0 replies; 4+ messages in thread
From: Pradhan, Sanman @ 2026-09-21 16:51 UTC (permalink / raw)
To: Srinivas Kandagatla, Orson Zhai, Baolin Wang
Cc: Chunyan Zhang, Greg Kroah-Hartman, Freeman Liu, Maxime Ripard,
linux-kernel, Sanman Pradhan, stable
From: Sanman Pradhan <psanman@juniper.net>
nvmem_reg_read() and nvmem_reg_write() pass 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 both functions 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")
Suggested-by: Srinivas Kandagatla <srini@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Sanman Pradhan <psanman@juniper.net>
---
Changes in v2:
- New patch: add the range check in the nvmem core instead of the sprd
provider.
Compile-tested only.
drivers/nvmem/core.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 0556d140170a4..ca90b24d73675 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -154,6 +154,9 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
+ if (offset > nvmem->size || bytes > nvmem->size - offset)
+ return -ERANGE;
+
if (!nvmem->nkeepout)
return __nvmem_reg_read(nvmem, offset, val, bytes);
@@ -163,6 +166,9 @@ static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
+ if (offset > nvmem->size || bytes > nvmem->size - offset)
+ return -ERANGE;
+
if (!nvmem->nkeepout)
return __nvmem_reg_write(nvmem, offset, val, bytes);
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2 2/2] nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()
2026-09-21 16:51 [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads Pradhan, Sanman
2026-09-21 16:51 ` [PATCH v2 1/2] nvmem: core: reject reads and writes beyond the device size Pradhan, Sanman
@ 2026-09-21 16:51 ` Pradhan, Sanman
2026-11-27 11:44 ` [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads Srinivas Kandagatla
2 siblings, 0 replies; 4+ messages in thread
From: Pradhan, Sanman @ 2026-09-21 16:51 UTC (permalink / raw)
To: Srinivas Kandagatla, Orson Zhai, Baolin Wang
Cc: Chunyan Zhang, Greg Kroah-Hartman, Freeman Liu, Maxime Ripard,
linux-kernel, Sanman Pradhan, stable
From: Sanman Pradhan <psanman@juniper.net>
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")
Cc: stable@vger.kernel.org
Signed-off-by: Sanman Pradhan <psanman@juniper.net>
---
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(-)
diff --git a/drivers/nvmem/sprd-efuse.c b/drivers/nvmem/sprd-efuse.c
index 1a7e4e5d8b86c..82995f8bce471 100644
--- a/drivers/nvmem/sprd-efuse.c
+++ b/drivers/nvmem/sprd-efuse.c
@@ -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);
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads
2026-09-21 16:51 [PATCH v2 0/2] nvmem: validate access ranges and fix SPRD reads Pradhan, Sanman
2026-09-21 16:51 ` [PATCH v2 1/2] nvmem: core: reject reads and writes beyond the device size Pradhan, Sanman
2026-09-21 16:51 ` [PATCH v2 2/2] nvmem: sprd: fix out-of-bounds read in sprd_efuse_read() Pradhan, Sanman
@ 2026-11-27 11:44 ` Srinivas Kandagatla
2 siblings, 0 replies; 4+ messages in thread
From: Srinivas Kandagatla @ 2026-11-27 11:44 UTC (permalink / raw)
To: Pradhan, Sanman, Srinivas Kandagatla, Orson Zhai, Baolin Wang
Cc: Chunyan Zhang, Greg Kroah-Hartman, Freeman Liu, Maxime Ripard,
linux-kernel, Sanman Pradhan
On 9/21/26 5:51 PM, Pradhan, Sanman wrote:
> From: Sanman Pradhan <psanman@juniper.net>
>
> Patch 1 adds a device-size bounds check to the two nvmem core read/write
> functions, so an out-of-range request is rejected before it reaches a
> provider. v1 open-coded this check in the sprd driver; this version puts
> it 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.
>
> Link for v1:https://lore.kernel.org/all/20260904003815.462352-1-sanman.pradhan@hpe.com/
>
> Sanman Pradhan (2):
> nvmem: core: reject reads and writes beyond the device size
> nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()
does not apply cleanly, can you resend base on nvmem-next
--srini
>
> drivers/nvmem/core.c | 6 ++++++
> drivers/nvmem/sprd-efuse.c | 17 ++++++++++++++---
> 2 files changed, 20 insertions(+), 3 deletions(-)
>
>
> base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
^ permalink raw reply [flat|nested] 4+ messages in thread