mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Dmitry Baryshkov <lumag@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Maxime Ripard <mripard@kernel.org>,
	Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 04/10] nvmem: core: Handle multi-byte bit offsets in nvmem_cell_prepare_write_buffer()
Date: Thu,  3 Dec 2026 08:26:05 +0000	[thread overview]
Message-ID: <86a3f253a3abda0064b739d546a8a05285aafc3f.1796286324.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796286324.git.lee@kernel.org>

nvmem_cell_prepare_write_buffer() assumes cell->bit_offset is less than
BITS_PER_BYTE.  When cell->bit_offset is 8 or greater, shifting by
bit_offset and passing negative shift counts to GENMASK() or right
shifts triggers undefined behaviour, leaves leading bytes overwritten
with zero, and places the payload at byte 0 instead of
bit_offset / BITS_PER_BYTE.  In addition, the carry shift used
BITS_PER_BYTE - 1 - bit_offset instead of BITS_PER_BYTE - bit_offset,
and the trailing-byte pointer p was left at byte 0 when bit_offset was
zero.

Split cell->bit_offset into bytes_offset and bit_offset modulo
BITS_PER_BYTE, reject bytes_offset >= cell->bytes, preserve existing
leading bytes via an aligned read from cell->offset, bound the payload
copy to cell->bytes - bytes_offset, fix the carry shift and trailing
byte pointer, and mask the trailing byte before preserving its upper
bits.

Fixes: 7a06ef751077 ("nvmem: core: fix bit offsets of more than one byte")
Closes: https://sashiko.dev/#/bug/linux-aad5e2e4-4b14-43ee-83a5-ed1a2760358e
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---

Cc: Dmitry Baryshkov <lumag@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Srinivas Kandagatla <srini@kernel.org>

 drivers/nvmem/core.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 83ed6d4e3c5f..e7c687cb6ce6 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1765,33 +1765,46 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 					     u8 *_buf, int len)
 {
 	struct nvmem_device *nvmem = cell->nvmem;
-	int i, rc, nbits, bit_offset = cell->bit_offset;
-	u8 v, *p, *buf, *b, pbyte, pbits;
+	int i, rc, nbits, bytes_offset, bit_offset;
+	u8 v = 0, *p, *buf, *b, pbyte, pbits;
+	size_t copy_len, lead_bytes;
 
 	nbits = cell->nbits;
+	bytes_offset = cell->bit_offset / BITS_PER_BYTE;
+	bit_offset = cell->bit_offset % BITS_PER_BYTE;
+	if (bytes_offset >= cell->bytes)
+		return ERR_PTR(-EINVAL);
+
 	buf = kzalloc(cell->bytes, GFP_KERNEL);
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	memcpy(buf, _buf, len);
-	p = b = buf;
+	lead_bytes = bytes_offset + (bit_offset ? 1 : 0);
+	if (lead_bytes) {
+		rc = nvmem_access_locked(nvmem, ops, cell->offset, buf,
+					 lead_bytes, false);
+		if (rc)
+			goto err;
+		v = buf[bytes_offset];
+	}
+
+	copy_len = min_t(size_t, len, cell->bytes - bytes_offset);
+	memcpy(buf + bytes_offset, _buf, copy_len);
+	b = buf + bytes_offset;
+	p = buf + cell->bytes - 1;
 
 	if (bit_offset) {
 		pbyte = *b;
 		*b <<= bit_offset;
 
 		/* setup the first byte with lsb bits from nvmem */
-		rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false);
-		if (rc)
-			goto err;
 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
 
 		/* setup rest of the byte if any */
-		for (i = 1; i < cell->bytes; i++) {
+		for (i = bytes_offset + 1; i < cell->bytes; i++) {
 			/* Get last byte bits and shift them towards lsb */
-			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
+			pbits = pbyte >> (BITS_PER_BYTE - bit_offset);
 			pbyte = *b;
-			p = b;
 			*b <<= bit_offset;
 			*b++ |= pbits;
 		}
@@ -1804,8 +1817,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 					 &v, 1, false);
 		if (rc)
 			goto err;
+		*p &= GENMASK(((nbits + bit_offset) % BITS_PER_BYTE) - 1, 0);
 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
-
 	}
 
 	return buf;
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03  8:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:26 [PATCH 00/10] nvmem: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03  8:26 ` [PATCH 01/10] nvmem: meson-efuse: Reject negative or zero SMC size in meson_efuse_probe() Lee Jones
2026-12-03  8:26 ` [PATCH 02/10] nvmem: core: Prevent integer underflow in nvmem_cell_attr_read() Lee Jones
2026-12-03  8:26 ` [PATCH 03/10] nvmem: sprd: Avoid out-of-bounds read on partial writes in sprd_efuse_raw_prog() Lee Jones
2026-12-03  8:26 ` Lee Jones [this message]
2026-12-03  8:26 ` [PATCH 05/10] nvmem: core: Serialise writes when toggling the write-protect GPIO Lee Jones
2026-12-03  8:26 ` [PATCH 06/10] nvmem: eeprom_93xx46: Reject unaligned writes on 16-bit devices Lee Jones
2026-12-03  8:26 ` [PATCH 07/10] nvmem: eeprom_93xx46: Hold device lock across erase/write enable sequences Lee Jones
2026-12-03  8:26 ` [PATCH 08/10] nvmem: eeprom_93xx46: Use DMA-safe command buffer for SPI transfers Lee Jones
2026-12-03  8:26 ` [PATCH 09/10] nvmem: meson-efuse: Detect short reads and writes from Secure Monitor Lee Jones
2026-12-03  8:26 ` [PATCH 10/10] nvmem: stm32: Avoid OP-TEE session leak on unaligned writes Lee Jones
2026-12-03 10:16   ` Patrick DELAUNAY

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=86a3f253a3abda0064b739d546a8a05285aafc3f.1796286324.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lumag@kernel.org \
    --cc=mripard@kernel.org \
    --cc=srini@kernel.org \
    --cc=stable@vger.kernel.org \
    /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®