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