1*b2441318SGreg 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 9c743f380SNicolas Pitre * "__stack_chk_guard" on ARM. This unfortunately means that on SMP 10c743f380SNicolas Pitre * we cannot have a different canary value per task. 11c743f380SNicolas Pitre */ 12c743f380SNicolas Pitre 13c743f380SNicolas Pitre #ifndef _ASM_STACKPROTECTOR_H 14c743f380SNicolas Pitre #define _ASM_STACKPROTECTOR_H 1 15c743f380SNicolas Pitre 16c743f380SNicolas Pitre #include <linux/random.h> 17c743f380SNicolas Pitre #include <linux/version.h> 18c743f380SNicolas Pitre 19c743f380SNicolas Pitre extern unsigned long __stack_chk_guard; 20c743f380SNicolas Pitre 21c743f380SNicolas Pitre /* 22c743f380SNicolas Pitre * Initialize the stackprotector canary value. 23c743f380SNicolas Pitre * 24c743f380SNicolas Pitre * NOTE: this must only be called from functions that never return, 25c743f380SNicolas Pitre * and it must always be inlined. 26c743f380SNicolas Pitre */ 27c743f380SNicolas Pitre static __always_inline void boot_init_stack_canary(void) 28c743f380SNicolas Pitre { 29c743f380SNicolas Pitre unsigned long canary; 30c743f380SNicolas Pitre 31c743f380SNicolas Pitre /* Try to get a semi random initial value. */ 32c743f380SNicolas Pitre get_random_bytes(&canary, sizeof(canary)); 33c743f380SNicolas Pitre canary ^= LINUX_VERSION_CODE; 34c743f380SNicolas Pitre 35c743f380SNicolas Pitre current->stack_canary = canary; 36c743f380SNicolas Pitre __stack_chk_guard = current->stack_canary; 37c743f380SNicolas Pitre } 38c743f380SNicolas Pitre 39c743f380SNicolas Pitre #endif /* _ASM_STACKPROTECTOR_H */ 40