Skip to content

Commit 31507fc

Browse files
Jennifer Berringergregkh
authored andcommitted
nvmem: core: improve range check for nvmem_cell_write()
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: 69aba79 ("nvmem: Add a simple NVMEM framework for consumers") Cc: [email protected] Signed-off-by: Jennifer Berringer <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent e88f516 commit 31507fc

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

drivers/nvmem/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,8 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
17931793
return -EINVAL;
17941794

17951795
if (cell->bit_offset || cell->nbits) {
1796+
if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
1797+
return -EINVAL;
17961798
buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
17971799
if (IS_ERR(buf))
17981800
return PTR_ERR(buf);

0 commit comments

Comments
 (0)