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