Skip to content

Commit c7cdd96

Browse files
greentimepalmer-dabbelt
authored andcommitted
riscv: prevent stack corruption by reserving task_pt_regs(p) early
Early function calls, such as setup_vm(), relocate_enable_mmu(), soc_early_init() etc, are free to operate on stack. However, PT_SIZE_ON_STACK bytes at the head of the kernel stack are purposedly reserved for the placement of per-task register context pointed by task_pt_regs(p). Those functions may corrupt task_pt_regs if we overlap the $sp with it. In fact, we had accidentally corrupted sstatus.VS in some tests, treating the kernel to save V context before V was actually allocated, resulting in a kernel panic. Thus, we should skip PT_SIZE_ON_STACK for $sp before making C function calls from the top-level assembly. Co-developed-by: ShihPo Hung <[email protected]> Signed-off-by: ShihPo Hung <[email protected]> Co-developed-by: Vincent Chen <[email protected]> Signed-off-by: Vincent Chen <[email protected]> Signed-off-by: Greentime Hu <[email protected]> Signed-off-by: Andy Chiu <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Reviewed-by: Heiko Stuebner <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 76e22fd commit c7cdd96

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

arch/riscv/kernel/head.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ clear_bss_done:
301301
la tp, init_task
302302
la sp, init_thread_union + THREAD_SIZE
303303
XIP_FIXUP_OFFSET sp
304+
addi sp, sp, -PT_SIZE_ON_STACK
304305
#ifdef CONFIG_BUILTIN_DTB
305306
la a0, __dtb_start
306307
XIP_FIXUP_OFFSET a0
@@ -318,6 +319,7 @@ clear_bss_done:
318319
/* Restore C environment */
319320
la tp, init_task
320321
la sp, init_thread_union + THREAD_SIZE
322+
addi sp, sp, -PT_SIZE_ON_STACK
321323

322324
#ifdef CONFIG_KASAN
323325
call kasan_early_init

0 commit comments

Comments
 (0)