Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796286386; b=3ralGfAJw4g+JcQbf+ZAqRwqcxCw0r29O5UN7EIwEzLagrH6epI1e2ZQoXBhaU+65VnW UmY0f8PBAFBzuiXbT919Jk4Zr+R08Iz6KFZzkTYA8vHLOZIy4b+SXlulAi7tRckTEJDTr 6v21IWPRUoYiuGVRwyPsOukYj4EodHih9uuJRIukydxOAYfkbd5FMcWVFyKm/K0Y9m0RB x1u4hzTAhaoBNewWzNmJoLRTuPIwhzish/oWJNFYQLn/0otCnm31ejJ92tN1IELu0onUf LQRwzME5hmLYDPr0jvf0kGKeoYFjkP9UiW98knH9Ql/OwPfhuHvtfJQpkleNKGPdEog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796286386; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=lvK1QIx3cd4HiGCClwKe4o/9Er/ZtVVaFNkXx8SAU5c=; b=OVeSZFFYghDGSJb6/45XB/t9ZmgVm/rKZxdxBQCnqi6UUCsBs9QGSC9fRdwAQZKi+Xco 19I04tB1Ibp1wXdJRejefhGBm9VO55TCC9wEA1lAetq0w5qOZK7vuyaGxlFSkGuDeSOQM 7QbRBcBrO7CFMGscq5BU0+ScwTVnev+WJctwv7cX/0SSv31SGZKjEIWjyYR567fqKkb9l 73s+Y3q0fvUdw3+Kbq7F14unp7XRV4XdrAjN+JOoRUFI/c+uUoKIjEOwdNiz4Q4xqvLkM +LFeE6owT6gSLk2PpGuEldeCEEVDHBvtkhVFCuDHwydziwPIt2+FrBQnIHGj+is6w8A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VwdHZ9wN; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FCCF606A69A; Thu, 3 Dec 2026 08:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VwdHZ9wN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 931971F0089C; Thu, 3 Dec 2026 08:26:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796286385; bh=lvK1QIx3cd4HiGCClwKe4o/9Er/ZtVVaFNkXx8SAU5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VwdHZ9wNPWaYx+gVCfbZNHQs7NvavSJRVQ3E9nnQTphbKZ42N4wnf+Uu/tPTWJsS3 82TX6q1jcoxYMVddI6JmsZrhSDLxX5cMqMeNHOmP79WIkpaZDiLsO661aG9utEdqTZ 6ooaJYftmGk/lurlikG4lai+7Cj7AhnqmD0OCNmB77rNyOZl9l2/oejeDlVIM5p1oL f6qKipggWMuISEik/Uh3TdZd9H7NECUbf0q//QFStv43W1/OnFSdH2m4+lNPr0wVeD OAHw7IHGz19DbiyKRVTHmugp9TVBTP4wKGU2CbpE8JWSm/V8XcJxXveY9dX1E8kN6/ N761LHwKutyTg== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, Dmitry Baryshkov , Greg Kroah-Hartman , Maxime Ripard , Srinivas Kandagatla 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 Message-ID: <86a3f253a3abda0064b739d546a8a05285aafc3f.1796286324.git.lee@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Cc: Dmitry Baryshkov Cc: Greg Kroah-Hartman Cc: Maxime Ripard Cc: Srinivas Kandagatla 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