From: "Pradhan, Sanman" <sanman.pradhan@hpe.com>
To: Srinivas Kandagatla <srini@kernel.org>,
Orson Zhai <orsonzhai@gmail.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Chunyan Zhang <zhang.lyra@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Freeman Liu <freeman.liu@unisoc.com>,
"Maxime Ripard" <mripard@kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Sanman Pradhan <psanman@juniper.net>,
"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: [PATCH v2 1/2] nvmem: core: reject reads and writes beyond the device size
Date: Mon, 21 Sep 2026 16:51:36 +0000 [thread overview]
Message-ID: <20260921165055.421933-2-sanman.pradhan@hpe.com> (raw)
In-Reply-To: <20260921165055.421933-1-sanman.pradhan@hpe.com>
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
next prev parent reply other threads:[~2026-09-21 16:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260921165055.421933-2-sanman.pradhan@hpe.com \
--to=sanman.pradhan@hpe.com \
--cc=baolin.wang@linux.alibaba.com \
--cc=freeman.liu@unisoc.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mripard@kernel.org \
--cc=orsonzhai@gmail.com \
--cc=psanman@juniper.net \
--cc=srini@kernel.org \
--cc=stable@vger.kernel.org \
--cc=zhang.lyra@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®