From e30db2d53fb36c7c75f52fdfd1a23e2988056482 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Sat, 9 Nov 2024 02:15:21 +0800 Subject: [PATCH] arch: riscv: reset global pointer on exception Reset the gp on exception entry from u-mode to protect the kernel against a possible rogue user thread. Signed-off-by: Yong Cong Sin Signed-off-by: Yong Cong Sin --- arch/riscv/core/isr.S | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/riscv/core/isr.S b/arch/riscv/core/isr.S index 65c40e63456..5ac71fe17b4 100644 --- a/arch/riscv/core/isr.S +++ b/arch/riscv/core/isr.S @@ -163,6 +163,14 @@ SECTION_FUNC(exception.entry, _isr_wrapper) lr t0, ___cpu_t_current_OFFSET(s0) lr tp, _thread_offset_to_tls(t0) + /* Make sure global pointer is sane */ +#ifdef CONFIG_RISCV_GP + .option push + .option norelax + la gp, __global_pointer$ + .option pop +#endif /* CONFIG_RISCV_GP */ + /* Clear our per-thread usermode flag */ lui t0, %tprel_hi(is_user_mode) add t0, t0, tp, %tprel_add(is_user_mode)