[PATCH 09/10] nvmem: core: reject reads and writes beyond the device size
From: srini
Date: Tue Dec 01 2026 - 18:27:49 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>
Signed-off-by: Srinivas Kandagatla <srini@xxxxxxxxxx>
---
drivers/nvmem/core.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index b334a64b85fc..fe9b8a3a5ea0 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.53.0