xref: /openbmc/linux/arch/arm/include/asm/stackprotector.h (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
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
9189af465SArd Biesheuvel  * "__stack_chk_guard" on ARM.  This prevents SMP systems from using a
10189af465SArd Biesheuvel  * different value for each task unless we enable a GCC plugin that
11189af465SArd Biesheuvel  * replaces these symbol references with references to each task's own
12189af465SArd Biesheuvel  * value.
13c743f380SNicolas Pitre  */
14c743f380SNicolas Pitre 
15c743f380SNicolas Pitre #ifndef _ASM_STACKPROTECTOR_H
16c743f380SNicolas Pitre #define _ASM_STACKPROTECTOR_H 1
17c743f380SNicolas Pitre 
18189af465SArd Biesheuvel #include <asm/thread_info.h>
19189af465SArd Biesheuvel 
20c743f380SNicolas Pitre extern unsigned long __stack_chk_guard;
21c743f380SNicolas Pitre 
22c743f380SNicolas Pitre /*
23c743f380SNicolas Pitre  * Initialize the stackprotector canary value.
24c743f380SNicolas Pitre  *
25c743f380SNicolas Pitre  * NOTE: this must only be called from functions that never return,
26c743f380SNicolas Pitre  * and it must always be inlined.
27c743f380SNicolas Pitre  */
boot_init_stack_canary(void)28c743f380SNicolas Pitre static __always_inline void boot_init_stack_canary(void)
29c743f380SNicolas Pitre {
30*622754e8SJason A. Donenfeld 	unsigned long canary = get_random_canary();
31c743f380SNicolas Pitre 
32c743f380SNicolas Pitre 	current->stack_canary = canary;
33189af465SArd Biesheuvel #ifndef CONFIG_STACKPROTECTOR_PER_TASK
34c743f380SNicolas Pitre 	__stack_chk_guard = current->stack_canary;
35189af465SArd Biesheuvel #endif
36c743f380SNicolas Pitre }
37c743f380SNicolas Pitre 
38c743f380SNicolas Pitre #endif	/* _ASM_STACKPROTECTOR_H */
39