Skip to content

Commit f2c9699

Browse files
guoren83palmer-dabbelt
authored andcommitted
riscv: Add STACKPROTECTOR supported
The -fstack-protector & -fstack-protector-strong features are from gcc. The patch only add basic kernel support to stack-protector feature and some arch could have its own solution such as ARM64_PTR_AUTH. After enabling STACKPROTECTOR and STACKPROTECTOR_STRONG, the .text size is expanded from 0x7de066 to 0x81fb32 (only 5%) to add canary checking code. Signed-off-by: Guo Ren <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 08b5985 commit f2c9699

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ config RISCV
6666
select HAVE_PERF_EVENTS
6767
select HAVE_PERF_REGS
6868
select HAVE_PERF_USER_STACK_DUMP
69+
select HAVE_STACKPROTECTOR
6970
select HAVE_SYSCALL_TRACEPOINTS
7071
select IRQ_DOMAIN
7172
select MODULES_USE_ELF_RELA if MODULES
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef _ASM_RISCV_STACKPROTECTOR_H
4+
#define _ASM_RISCV_STACKPROTECTOR_H
5+
6+
#include <linux/random.h>
7+
#include <linux/version.h>
8+
#include <asm/timex.h>
9+
10+
extern unsigned long __stack_chk_guard;
11+
12+
/*
13+
* Initialize the stackprotector canary value.
14+
*
15+
* NOTE: this must only be called from functions that never return,
16+
* and it must always be inlined.
17+
*/
18+
static __always_inline void boot_init_stack_canary(void)
19+
{
20+
unsigned long canary;
21+
unsigned long tsc;
22+
23+
/* Try to get a semi random initial value. */
24+
get_random_bytes(&canary, sizeof(canary));
25+
tsc = get_cycles();
26+
canary += tsc + (tsc << BITS_PER_LONG/2);
27+
canary ^= LINUX_VERSION_CODE;
28+
canary &= CANARY_MASK;
29+
30+
current->stack_canary = canary;
31+
__stack_chk_guard = current->stack_canary;
32+
}
33+
#endif /* _ASM_RISCV_STACKPROTECTOR_H */

arch/riscv/kernel/process.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
register unsigned long gp_in_global __asm__("gp");
2626

27+
#ifdef CONFIG_STACKPROTECTOR
28+
#include <linux/stackprotector.h>
29+
unsigned long __stack_chk_guard __read_mostly;
30+
EXPORT_SYMBOL(__stack_chk_guard);
31+
#endif
32+
2733
extern asmlinkage void ret_from_fork(void);
2834
extern asmlinkage void ret_from_kernel_thread(void);
2935

0 commit comments

Comments
 (0)