xref: /openbmc/linux/arch/arm64/include/asm/stackprotector.h (revision 28321582334c261c13b20d7efe634e610b4c100b)
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>
18*28321582SAmit Daniel Kachhap #include <asm/pointer_auth.h>
19c0c264aeSLaura Abbott 
20c0c264aeSLaura Abbott extern unsigned long __stack_chk_guard;
21c0c264aeSLaura Abbott 
22c0c264aeSLaura Abbott /*
23c0c264aeSLaura Abbott  * Initialize the stackprotector canary value.
24c0c264aeSLaura Abbott  *
25c0c264aeSLaura Abbott  * NOTE: this must only be called from functions that never return,
26c0c264aeSLaura Abbott  * and it must always be inlined.
27c0c264aeSLaura Abbott  */
28c0c264aeSLaura Abbott static __always_inline void boot_init_stack_canary(void)
29c0c264aeSLaura Abbott {
30*28321582SAmit Daniel Kachhap #if defined(CONFIG_STACKPROTECTOR)
31c0c264aeSLaura Abbott 	unsigned long canary;
32c0c264aeSLaura Abbott 
33c0c264aeSLaura Abbott 	/* Try to get a semi random initial value. */
34c0c264aeSLaura Abbott 	get_random_bytes(&canary, sizeof(canary));
35c0c264aeSLaura Abbott 	canary ^= LINUX_VERSION_CODE;
36d21f5498SRik van Riel 	canary &= CANARY_MASK;
37c0c264aeSLaura Abbott 
38c0c264aeSLaura Abbott 	current->stack_canary = canary;
390a1213faSArd Biesheuvel 	if (!IS_ENABLED(CONFIG_STACKPROTECTOR_PER_TASK))
40c0c264aeSLaura Abbott 		__stack_chk_guard = current->stack_canary;
41*28321582SAmit Daniel Kachhap #endif
42*28321582SAmit Daniel Kachhap 	ptrauth_thread_init_kernel(current);
43*28321582SAmit Daniel Kachhap 	ptrauth_thread_switch_kernel(current);
44c0c264aeSLaura Abbott }
45c0c264aeSLaura Abbott 
46c0c264aeSLaura Abbott #endif	/* _ASM_STACKPROTECTOR_H */
47