xref: /linux/arch/riscv/include/asm/stackprotector.h (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
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 */
34