Skip to content

Commit 41213bf

Browse files
deepak0414Paul Walmsley
authored andcommitted
riscv: enable kernel access to shadow stack memory via the FWFT SBI call
The kernel has to perform shadow stack operations on the user shadow stack. During signal delivery and sigreturn, the shadow stack token must be created and validated respectively. Thus shadow stack access for the kernel must be enabled. In the future, when kernel shadow stacks are enabled, they must be enabled as early as possible for better coverage and to prevent any imbalance between the regular stack and the shadow stack. After 'relocate_enable_mmu' has completed, this is the earliest that it can be enabled. Reviewed-by: Zong Li <zong.li@sifive.com> Signed-off-by: Deepak Gupta <debug@rivosinc.com> Tested-by: Andreas Korb <andreas.korb@aisec.fraunhofer.de> # QEMU, custom CVA6 Tested-by: Valentin Haudiquet <valentin.haudiquet@canonical.com> Link: https://patch.msgid.link/20251112-v5_user_cfi_series-v23-22-b55691eacf4f@rivosinc.com [pjw@kernel.org: updated to apply; cleaned up commit message] Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent c9b859c commit 41213bf

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

arch/riscv/kernel/asm-offsets.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,10 @@ void asm_offsets(void)
533533
DEFINE(FREGS_A6, offsetof(struct __arch_ftrace_regs, a6));
534534
DEFINE(FREGS_A7, offsetof(struct __arch_ftrace_regs, a7));
535535
#endif
536+
#ifdef CONFIG_RISCV_SBI
537+
DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT);
538+
DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET);
539+
DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK);
540+
DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK);
541+
#endif
536542
}

arch/riscv/kernel/head.S

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <asm/image.h>
1616
#include <asm/scs.h>
1717
#include <asm/xip_fixup.h>
18+
#include <asm/usercfi.h>
1819
#include "efi-header.S"
1920

2021
__HEAD
@@ -170,6 +171,19 @@ secondary_start_sbi:
170171
call relocate_enable_mmu
171172
#endif
172173
call .Lsetup_trap_vector
174+
#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI)
175+
li a7, SBI_EXT_FWFT
176+
li a6, SBI_EXT_FWFT_SET
177+
li a0, SBI_FWFT_SHADOW_STACK
178+
li a1, 1 /* enable supervisor to access shadow stack access */
179+
li a2, SBI_FWFT_SET_FLAG_LOCK
180+
ecall
181+
beqz a0, 1f
182+
la a1, riscv_nousercfi
183+
li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI
184+
REG_S a0, (a1)
185+
1:
186+
#endif
173187
scs_load_current
174188
call smp_callin
175189
#endif /* CONFIG_SMP */
@@ -330,6 +344,19 @@ SYM_CODE_START(_start_kernel)
330344
la tp, init_task
331345
la sp, init_thread_union + THREAD_SIZE
332346
addi sp, sp, -PT_SIZE_ON_STACK
347+
#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI)
348+
li a7, SBI_EXT_FWFT
349+
li a6, SBI_EXT_FWFT_SET
350+
li a0, SBI_FWFT_SHADOW_STACK
351+
li a1, 1 /* enable supervisor to access shadow stack access */
352+
li a2, SBI_FWFT_SET_FLAG_LOCK
353+
ecall
354+
beqz a0, 1f
355+
la a1, riscv_nousercfi
356+
li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI
357+
REG_S a0, (a1)
358+
1:
359+
#endif
333360
scs_load_current
334361

335362
#ifdef CONFIG_KASAN

0 commit comments

Comments
 (0)