xref: /openbmc/linux/arch/arm/include/asm/stackprotector.h (revision 189af4657186da08a2e79fb8e906cfd82b2ccddc)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c743f380SNicolas Pitre /*
3c743f380SNicolas Pitre  * GCC stack protector support.
4c743f380SNicolas Pitre  *
5c743f380SNicolas Pitre  * Stack protector works by putting predefined pattern at the start of
6c743f380SNicolas Pitre  * the stack frame and verifying that it hasn't been overwritten when
7c743f380SNicolas Pitre  * returning from the function.  The pattern is called stack canary
8c743f380SNicolas Pitre  * and gcc expects it to be defined by a global variable called
9*189af465SArd Biesheuvel  * "__stack_chk_guard" on ARM.  This prevents SMP systems from using a
10*189af465SArd Biesheuvel  * different value for each task unless we enable a GCC plugin that
11*189af465SArd Biesheuvel  * replaces these symbol references with references to each task's own
12*189af465SArd Biesheuvel  * value.
13c743f380SNicolas Pitre  */
14c743f380SNicolas Pitre 
15c743f380SNicolas Pitre #ifndef _ASM_STACKPROTECTOR_H
16c743f380SNicolas Pitre #define _ASM_STACKPROTECTOR_H 1
17c743f380SNicolas Pitre 
18c743f380SNicolas Pitre #include <linux/random.h>
19c743f380SNicolas Pitre #include <linux/version.h>
20c743f380SNicolas Pitre 
21*189af465SArd Biesheuvel #include <asm/thread_info.h>
22*189af465SArd Biesheuvel 
23c743f380SNicolas Pitre extern unsigned long __stack_chk_guard;
24c743f380SNicolas Pitre 
25c743f380SNicolas Pitre /*
26c743f380SNicolas Pitre  * Initialize the stackprotector canary value.
27c743f380SNicolas Pitre  *
28c743f380SNicolas Pitre  * NOTE: this must only be called from functions that never return,
29c743f380SNicolas Pitre  * and it must always be inlined.
30c743f380SNicolas Pitre  */
31c743f380SNicolas Pitre static __always_inline void boot_init_stack_canary(void)
32c743f380SNicolas Pitre {
33c743f380SNicolas Pitre 	unsigned long canary;
34c743f380SNicolas Pitre 
35c743f380SNicolas Pitre 	/* Try to get a semi random initial value. */
36c743f380SNicolas Pitre 	get_random_bytes(&canary, sizeof(canary));
37c743f380SNicolas Pitre 	canary ^= LINUX_VERSION_CODE;
38c743f380SNicolas Pitre 
39c743f380SNicolas Pitre 	current->stack_canary = canary;
40*189af465SArd Biesheuvel #ifndef CONFIG_STACKPROTECTOR_PER_TASK
41c743f380SNicolas Pitre 	__stack_chk_guard = current->stack_canary;
42*189af465SArd Biesheuvel #else
43*189af465SArd Biesheuvel 	current_thread_info()->stack_canary = current->stack_canary;
44*189af465SArd Biesheuvel #endif
45c743f380SNicolas Pitre }
46c743f380SNicolas Pitre 
47c743f380SNicolas Pitre #endif	/* _ASM_STACKPROTECTOR_H */
48