Skip to content

Commit a0f7085

Browse files
Jinjie Ruanchenhuacai
authored andcommitted
LoongArch: Add RANDOMIZE_KSTACK_OFFSET support
Add support of kernel stack offset randomization while handling syscall, the offset is defaultly limited by KSTACK_OFFSET_MAX(). In order to avoid triggering stack canaries (due to __builtin_alloca()) and slowing down the entry path, use __no_stack_protector attribute to disable stack protector for do_syscall() at function level. With this patch, the REPORT_STACK test show that: `loongarch64 bits of stack entropy: 7` Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Jinjie Ruan <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 08f417d commit a0f7085

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ config LOONGARCH
106106
select HAVE_ARCH_KFENCE
107107
select HAVE_ARCH_KGDB if PERF_EVENTS
108108
select HAVE_ARCH_MMAP_RND_BITS if MMU
109+
select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
109110
select HAVE_ARCH_SECCOMP
110111
select HAVE_ARCH_SECCOMP_FILTER
111112
select HAVE_ARCH_TRACEHOOK

arch/loongarch/kernel/syscall.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
#include <linux/entry-common.h>
1010
#include <linux/errno.h>
1111
#include <linux/linkage.h>
12+
#include <linux/objtool.h>
13+
#include <linux/randomize_kstack.h>
1214
#include <linux/syscalls.h>
1315
#include <linux/unistd.h>
1416

1517
#include <asm/asm.h>
1618
#include <asm/exception.h>
19+
#include <asm/loongarch.h>
1720
#include <asm/signal.h>
1821
#include <asm/switch_to.h>
1922
#include <asm-generic/syscalls.h>
@@ -39,7 +42,7 @@ void *sys_call_table[__NR_syscalls] = {
3942
typedef long (*sys_call_fn)(unsigned long, unsigned long,
4043
unsigned long, unsigned long, unsigned long, unsigned long);
4144

42-
void noinstr do_syscall(struct pt_regs *regs)
45+
void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
4346
{
4447
unsigned long nr;
4548
sys_call_fn syscall_fn;
@@ -55,11 +58,28 @@ void noinstr do_syscall(struct pt_regs *regs)
5558

5659
nr = syscall_enter_from_user_mode(regs, nr);
5760

61+
add_random_kstack_offset();
62+
5863
if (nr < NR_syscalls) {
5964
syscall_fn = sys_call_table[nr];
6065
regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
6166
regs->regs[7], regs->regs[8], regs->regs[9]);
6267
}
6368

69+
/*
70+
* This value will get limited by KSTACK_OFFSET_MAX(), which is 10
71+
* bits. The actual entropy will be further reduced by the compiler
72+
* when applying stack alignment constraints: 16-bytes (i.e. 4-bits)
73+
* aligned, which will remove the 4 low bits from any entropy chosen
74+
* here.
75+
*
76+
* The resulting 6 bits of entropy is seen in SP[9:4].
77+
*/
78+
choose_random_kstack_offset(drdtime());
79+
6480
syscall_exit_to_user_mode(regs);
6581
}
82+
83+
#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
84+
STACK_FRAME_NON_STANDARD(do_syscall);
85+
#endif

0 commit comments

Comments
 (0)