xref: /openbmc/linux/arch/arm64/include/asm/stackprotector.h (revision c0c264ae5112d1cdb7d37d4e208b7a7e766a7418)
1*c0c264aeSLaura Abbott /*
2*c0c264aeSLaura Abbott  * GCC stack protector support.
3*c0c264aeSLaura Abbott  *
4*c0c264aeSLaura Abbott  * Stack protector works by putting predefined pattern at the start of
5*c0c264aeSLaura Abbott  * the stack frame and verifying that it hasn't been overwritten when
6*c0c264aeSLaura Abbott  * returning from the function.  The pattern is called stack canary
7*c0c264aeSLaura Abbott  * and gcc expects it to be defined by a global variable called
8*c0c264aeSLaura Abbott  * "__stack_chk_guard" on ARM.  This unfortunately means that on SMP
9*c0c264aeSLaura Abbott  * we cannot have a different canary value per task.
10*c0c264aeSLaura Abbott  */
11*c0c264aeSLaura Abbott 
12*c0c264aeSLaura Abbott #ifndef __ASM_STACKPROTECTOR_H
13*c0c264aeSLaura Abbott #define __ASM_STACKPROTECTOR_H
14*c0c264aeSLaura Abbott 
15*c0c264aeSLaura Abbott #include <linux/random.h>
16*c0c264aeSLaura Abbott #include <linux/version.h>
17*c0c264aeSLaura Abbott 
18*c0c264aeSLaura Abbott extern unsigned long __stack_chk_guard;
19*c0c264aeSLaura Abbott 
20*c0c264aeSLaura Abbott /*
21*c0c264aeSLaura Abbott  * Initialize the stackprotector canary value.
22*c0c264aeSLaura Abbott  *
23*c0c264aeSLaura Abbott  * NOTE: this must only be called from functions that never return,
24*c0c264aeSLaura Abbott  * and it must always be inlined.
25*c0c264aeSLaura Abbott  */
26*c0c264aeSLaura Abbott static __always_inline void boot_init_stack_canary(void)
27*c0c264aeSLaura Abbott {
28*c0c264aeSLaura Abbott 	unsigned long canary;
29*c0c264aeSLaura Abbott 
30*c0c264aeSLaura Abbott 	/* Try to get a semi random initial value. */
31*c0c264aeSLaura Abbott 	get_random_bytes(&canary, sizeof(canary));
32*c0c264aeSLaura Abbott 	canary ^= LINUX_VERSION_CODE;
33*c0c264aeSLaura Abbott 
34*c0c264aeSLaura Abbott 	current->stack_canary = canary;
35*c0c264aeSLaura Abbott 	__stack_chk_guard = current->stack_canary;
36*c0c264aeSLaura Abbott }
37*c0c264aeSLaura Abbott 
38*c0c264aeSLaura Abbott #endif	/* _ASM_STACKPROTECTOR_H */
39