Skip to content

Commit ce989f1

Browse files
geertupalmer-dabbelt
authored andcommitted
RISC-V: Fix out-of-bounds accesses in init_resources()
init_resources() allocates an array of resources, based on the current total number of memory regions and reserved memory regions. However, allocating this array using memblock_alloc() might increase the number of reserved memory regions. If that happens, populating the array later based on the new number of regions will cause out-of-bounds writes beyond the end of the allocated array. Fix this by allocating one more entry, which may or may not be used. Fixes: 797f037 ("RISC-V: Do not allocate memblock while iterating reserved memblocks") Signed-off-by: Geert Uytterhoeven <[email protected]> Reviewed-by: Atish Patra <[email protected]> Cc: [email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent fa59030 commit ce989f1

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

arch/riscv/kernel/setup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ static void __init init_resources(void)
147147
bss_res.end = __pa_symbol(__bss_stop) - 1;
148148
bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
149149

150-
mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt) * sizeof(*mem_res);
150+
/* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
151+
mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt + 1) * sizeof(*mem_res);
151152
mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
152153
if (!mem_res)
153154
panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);

0 commit comments

Comments
 (0)