[PATCH 02/10] nvmem: core: Prevent integer underflow in nvmem_cell_attr_read()

From: Lee Jones

Date: Thu Dec 03 2026 - 03:26:27 EST


sysfs_kf_bin_read() skips its pos >= size check when the binary
attribute has i_size == 0. For a zero-length NVMEM cell, a userspace
read with a non-zero offset therefore reaches nvmem_cell_attr_read(),
where cell_sz - pos underflows as unsigned int, causing memcpy() to
dereference ZERO_SIZE_PTR + pos.

Check whether pos is negative or at least cell_sz before computing
read_len and copying into buf, returning 0 bytes when out of range.

Fixes: 0331c611949f ("nvmem: core: Expose cells through sysfs")
Closes: https://sashiko.dev/#/bug/linux-d9f2bf28-bfff-430b-88b3-f5d6e0263229
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---

Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: Maxime Ripard <mripard@xxxxxxxxxx>
Cc: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
Cc: Srinivas Kandagatla <srini@xxxxxxxxxx>

drivers/nvmem/core.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index cf263633a4a2..83ed6d4e3c5f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -384,8 +384,12 @@ static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
goto destroy_cell;
}

- read_len = min_t(unsigned int, cell_sz - pos, count);
- memcpy(buf, content + pos, read_len);
+ if (pos < 0 || pos >= cell_sz) {
+ read_len = 0;
+ } else {
+ read_len = min_t(size_t, cell_sz - pos, count);
+ memcpy(buf, content + pos, read_len);
+ }
kfree(content);

destroy_cell:
--
2.56.1.911.g883d8bcb20-goog