18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * GCC stack protector support.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Stack protector works by putting predefined pattern at the start of
68c2ecf20Sopenharmony_ci * the stack frame and verifying that it hasn't been overwritten when
78c2ecf20Sopenharmony_ci * returning from the function.  The pattern is called stack canary
88c2ecf20Sopenharmony_ci * and gcc expects it to be defined by a global variable called
98c2ecf20Sopenharmony_ci * "__stack_chk_guard" on ARM.  This prevents SMP systems from using a
108c2ecf20Sopenharmony_ci * different value for each task unless we enable a GCC plugin that
118c2ecf20Sopenharmony_ci * replaces these symbol references with references to each task's own
128c2ecf20Sopenharmony_ci * value.
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_ci#include <asm/thread_info.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciextern unsigned long __stack_chk_guard;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * Initialize the stackprotector canary value.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * NOTE: this must only be called from functions that never return,
298c2ecf20Sopenharmony_ci * and it must always be inlined.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cistatic __always_inline void boot_init_stack_canary(void)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	unsigned long canary;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/* Try to get a semi random initial value. */
368c2ecf20Sopenharmony_ci	get_random_bytes(&canary, sizeof(canary));
378c2ecf20Sopenharmony_ci	canary ^= LINUX_VERSION_CODE;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	current->stack_canary = canary;
408c2ecf20Sopenharmony_ci#ifndef CONFIG_STACKPROTECTOR_PER_TASK
418c2ecf20Sopenharmony_ci	__stack_chk_guard = current->stack_canary;
428c2ecf20Sopenharmony_ci#else
438c2ecf20Sopenharmony_ci	current_thread_info()->stack_canary = current->stack_canary;
448c2ecf20Sopenharmony_ci#endif
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#endif	/* _ASM_STACKPROTECTOR_H */
48