[PATCH 04/10] nvmem: core: Handle multi-byte bit offsets in nvmem_cell_prepare_write_buffer()

From: Lee Jones

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


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@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---

Cc: Dmitry Baryshkov <lumag@xxxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: Maxime Ripard <mripard@xxxxxxxxxx>
Cc: Srinivas Kandagatla <srini@xxxxxxxxxx>

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