[PATCH v3 1/2] nvmem: core: reject reads and writes beyond the device size
From: Pradhan, Sanman
Date: Mon Nov 30 2026 - 11:25:51 EST
From: Sanman Pradhan <psanman@xxxxxxxxxxx>
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")
Suggested-by: Srinivas Kandagatla <srini@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Sanman Pradhan <psanman@xxxxxxxxxxx>
---
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(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 6f451293b282..cf263633a4a2 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -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);
--
2.34.1