Skip to content

Commit 7e18643

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: fix race when vmap stack overflow
Currently, when detecting vmap stack overflow, riscv firstly switches to the so called shadow stack, then use this shadow stack to call the get_overflow_stack() to get the overflow stack. However, there's a race here if two or more harts use the same shadow stack at the same time. To solve this race, we introduce spin_shadow_stack atomic var, which will be swap between its own address and 0 in atomic way, when the var is set, it means the shadow_stack is being used; when the var is cleared, it means the shadow_stack isn't being used. Fixes: 31da94c ("riscv: add VMAP_STACK overflow detection") Signed-off-by: Jisheng Zhang <[email protected]> Suggested-by: Guo Ren <[email protected]> Reviewed-by: Guo Ren <[email protected]> Link: https://lore.kernel.org/r/[email protected] [Palmer: Add AQ to the swap, and also some comments.] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 31da94c commit 7e18643

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

arch/riscv/include/asm/asm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define REG_L __REG_SEL(ld, lw)
2424
#define REG_S __REG_SEL(sd, sw)
2525
#define REG_SC __REG_SEL(sc.d, sc.w)
26+
#define REG_AMOSWAP_AQ __REG_SEL(amoswap.d.aq, amoswap.w.aq)
2627
#define REG_ASM __REG_SEL(.dword, .word)
2728
#define SZREG __REG_SEL(8, 4)
2829
#define LGREG __REG_SEL(3, 2)

arch/riscv/kernel/entry.S

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ handle_syscall_trace_exit:
387387

388388
#ifdef CONFIG_VMAP_STACK
389389
handle_kernel_stack_overflow:
390+
/*
391+
* Takes the psuedo-spinlock for the shadow stack, in case multiple
392+
* harts are concurrently overflowing their kernel stacks. We could
393+
* store any value here, but since we're overflowing the kernel stack
394+
* already we only have SP to use as a scratch register. So we just
395+
* swap in the address of the spinlock, as that's definately non-zero.
396+
*
397+
* Pairs with a store_release in handle_bad_stack().
398+
*/
399+
1: la sp, spin_shadow_stack
400+
REG_AMOSWAP_AQ sp, sp, (sp)
401+
bnez sp, 1b
402+
390403
la sp, shadow_stack
391404
addi sp, sp, SHADOW_OVERFLOW_STACK_SIZE
392405

arch/riscv/kernel/traps.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,29 @@ asmlinkage unsigned long get_overflow_stack(void)
213213
OVERFLOW_STACK_SIZE;
214214
}
215215

216+
/*
217+
* A pseudo spinlock to protect the shadow stack from being used by multiple
218+
* harts concurrently. This isn't a real spinlock because the lock side must
219+
* be taken without a valid stack and only a single register, it's only taken
220+
* while in the process of panicing anyway so the performance and error
221+
* checking a proper spinlock gives us doesn't matter.
222+
*/
223+
unsigned long spin_shadow_stack;
224+
216225
asmlinkage void handle_bad_stack(struct pt_regs *regs)
217226
{
218227
unsigned long tsk_stk = (unsigned long)current->stack;
219228
unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
220229

230+
/*
231+
* We're done with the shadow stack by this point, as we're on the
232+
* overflow stack. Tell any other concurrent overflowing harts that
233+
* they can proceed with panicing by releasing the pseudo-spinlock.
234+
*
235+
* This pairs with an amoswap.aq in handle_kernel_stack_overflow.
236+
*/
237+
smp_store_release(&spin_shadow_stack, 0);
238+
221239
console_verbose();
222240

223241
pr_emerg("Insufficient stack space to handle exception!\n");

0 commit comments

Comments
 (0)