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