18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * GCC stack protector support. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * (This is directly adopted from the ARM implementation) 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Stack protector works by putting predefined pattern at the start of 88c2ecf20Sopenharmony_ci * the stack frame and verifying that it hasn't been overwritten when 98c2ecf20Sopenharmony_ci * returning from the function. The pattern is called stack canary 108c2ecf20Sopenharmony_ci * and gcc expects it to be defined by a global variable called 118c2ecf20Sopenharmony_ci * "__stack_chk_guard" on MIPS. This unfortunately means that on SMP 128c2ecf20Sopenharmony_ci * we cannot have a different canary value per task. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#ifndef _ASM_STACKPROTECTOR_H 168c2ecf20Sopenharmony_ci#define _ASM_STACKPROTECTOR_H 1 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/random.h> 198c2ecf20Sopenharmony_ci#include <linux/version.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciextern unsigned long __stack_chk_guard; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * Initialize the stackprotector canary value. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * NOTE: this must only be called from functions that never return, 278c2ecf20Sopenharmony_ci * and it must always be inlined. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistatic __always_inline void boot_init_stack_canary(void) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci unsigned long canary; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci /* Try to get a semi random initial value. */ 348c2ecf20Sopenharmony_ci get_random_bytes(&canary, sizeof(canary)); 358c2ecf20Sopenharmony_ci canary ^= LINUX_VERSION_CODE; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci current->stack_canary = canary; 388c2ecf20Sopenharmony_ci __stack_chk_guard = current->stack_canary; 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#endif /* _ASM_STACKPROTECTOR_H */ 42