Skip to content

Commit ce8c3d1

Browse files
committed
nvmem: core: Fix OOB read for bit offsets of more than one byte
When the bit offset is BITS_PER_BYTE or larger the read postion is advanced by `bytes_offset`. This is not taken into account in the per-byte read loop which still reads `cell->bytes` resuling in an out of bounds read of `bytes_offset` bytes. The information read OOB does not leak directly as the erroneously read bits are cleared. Detected by KASAN while looking for a use-after-free in simplefb.c. Fixes: 7a06ef7 ("nvmem: core: fix bit offsets of more than one byte") Signed-off-by: Janne Grunau <[email protected]>
1 parent 17e9266 commit ce8c3d1

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/nvmem/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,12 +1618,14 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
16181618
*p = *b++ >> bit_offset;
16191619

16201620
/* setup rest of the bytes if any */
1621-
for (i = 1; i < cell->bytes; i++) {
1621+
for (i = 1; i < (cell->bytes - bytes_offset); i++) {
16221622
/* Get bits from next byte and shift them towards msb */
16231623
*p++ |= *b << (BITS_PER_BYTE - bit_offset);
16241624

16251625
*p = *b++ >> bit_offset;
16261626
}
1627+
/* point to end of the buffer unused bits will be cleared */
1628+
p = buf + cell->bytes - 1;
16271629
} else if (p != b) {
16281630
memmove(p, b, cell->bytes - bytes_offset);
16291631
p += cell->bytes - 1;

0 commit comments

Comments
 (0)