Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * GCC stack protector support.
0004  *
0005  * Stack protector works by putting predefined pattern at the start of
0006  * the stack frame and verifying that it hasn't been overwritten when
0007  * returning from the function.  The pattern is called stack canary
0008  * and gcc expects it to be defined by a global variable called
0009  * "__stack_chk_guard" on ARM.  This unfortunately means that on SMP
0010  * we cannot have a different canary value per task.
0011  */
0012 
0013 #ifndef __ASM_STACKPROTECTOR_H
0014 #define __ASM_STACKPROTECTOR_H
0015 
0016 #include <linux/random.h>
0017 #include <linux/version.h>
0018 #include <asm/pointer_auth.h>
0019 
0020 extern unsigned long __stack_chk_guard;
0021 
0022 /*
0023  * Initialize the stackprotector canary value.
0024  *
0025  * NOTE: this must only be called from functions that never return,
0026  * and it must always be inlined.
0027  */
0028 static __always_inline void boot_init_stack_canary(void)
0029 {
0030 #if defined(CONFIG_STACKPROTECTOR)
0031     unsigned long canary;
0032 
0033     /* Try to get a semi random initial value. */
0034     get_random_bytes(&canary, sizeof(canary));
0035     canary ^= LINUX_VERSION_CODE;
0036     canary &= CANARY_MASK;
0037 
0038     current->stack_canary = canary;
0039     if (!IS_ENABLED(CONFIG_STACKPROTECTOR_PER_TASK))
0040         __stack_chk_guard = current->stack_canary;
0041 #endif
0042     ptrauth_thread_init_kernel(current);
0043     ptrauth_thread_switch_kernel(current);
0044     ptrauth_enable();
0045 }
0046 
0047 #endif  /* _ASM_STACKPROTECTOR_H */