From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DD7722FF4E; Mon, 10 Mar 2025 17:58:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741629522; cv=none; b=ct0DTFjxHSEn02byRQ1aJ77dCxDTw5qM1vt1vXkA3XSTldKwkvVtD+ofUQFOEKBavbOQAHpZc8kZ93iZAwiyDwfv1XFe+umhwGwp0NHuXpP+dPO6r5t+FRCVA8S03C8zSHXvR9GKZYECsdITbVsYLQGpdFOH5sDVZhm+xky+3i8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741629522; c=relaxed/simple; bh=8bILwGCvqAIqwvEZDqYeTqpwjfCAyxY7ikzLjrarxCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n8QkJu/BVwlLYDQYALPoqm75AVnGgsKOFwMvJQBDY1GlhjxTy/WBgf1SbBbQNZOKkRfF2p2c9fB3S1681lDasC1sAyLK4gpuFQktSd5MpAEZTh1Mm2Ok8kD0VXvRwHkykuuvfdTjtbhqoG/kxr1phi1yKzKdcauUw7dtDKpw5Es= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=CVCV8HeP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="CVCV8HeP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95F5EC4CEE5; Mon, 10 Mar 2025 17:58:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1741629522; bh=8bILwGCvqAIqwvEZDqYeTqpwjfCAyxY7ikzLjrarxCQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CVCV8HePUMx/h1bY9A7gztTY9i0BghGW+Gq/vWj/i9lE8qNiaU22P5gNi3LckooTN wTa5+vAoBZIeQqGXUhby9hEG9DBKz4Qyi5LmO9l3lNMplliA15/WBd3u5yM76DGbkg N0XLnl367u7O83osTH40prpmsULyCBPelKeTJm2s= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Jennifer Berringer , Srinivas Kandagatla Subject: [PATCH 5.15 316/620] nvmem: core: improve range check for nvmem_cell_write() Date: Mon, 10 Mar 2025 18:02:42 +0100 Message-ID: <20250310170558.087985102@linuxfoundation.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250310170545.553361750@linuxfoundation.org> References: <20250310170545.553361750@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jennifer Berringer commit 31507fc2ad36e0071751a710449db19c85d82a7f upstream. When __nvmem_cell_entry_write() is called for an nvmem cell that does not need bit shifting, it requires that the len parameter exactly matches the nvmem cell size. However, when the nvmem cell has a nonzero bit_offset, it was skipping this check. Accepting values of len larger than the cell size results in nvmem_cell_prepare_write_buffer() trying to write past the end of a heap buffer that it allocates. Add a check to avoid that problem and instead return -EINVAL when len doesn't match the number of bits expected by the nvmem cell when bit_offset is nonzero. This check uses cell->nbits in order to allow providing the smaller size to cells that are shifted into another byte by bit_offset. For example, a cell with nbits=8 and nonzero bit_offset would have bytes=2 but should accept a 1-byte write here, although no current callers depend on this. Fixes: 69aba7948cbe ("nvmem: Add a simple NVMEM framework for consumers") Cc: stable@vger.kernel.org Signed-off-by: Jennifer Berringer Signed-off-by: Srinivas Kandagatla Link: https://lore.kernel.org/r/20241230141901.263976-7-srinivas.kandagatla@linaro.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/nvmem/core.c | 2 ++ 1 file changed, 2 insertions(+) --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -1512,6 +1512,8 @@ int nvmem_cell_write(struct nvmem_cell * return -EINVAL; if (cell->bit_offset || cell->nbits) { + if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes) + return -EINVAL; buf = nvmem_cell_prepare_write_buffer(cell, buf, len); if (IS_ERR(buf)) return PTR_ERR(buf);