162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * GCC stack protector support.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Stack protector works by putting predefined pattern at the start of
662306a36Sopenharmony_ci * the stack frame and verifying that it hasn't been overwritten when
762306a36Sopenharmony_ci * returning from the function.  The pattern is called stack canary
862306a36Sopenharmony_ci * and gcc expects it to be defined by a global variable called
962306a36Sopenharmony_ci * "__stack_chk_guard" on ARM.  This prevents SMP systems from using a
1062306a36Sopenharmony_ci * different value for each task unless we enable a GCC plugin that
1162306a36Sopenharmony_ci * replaces these symbol references with references to each task's own
1262306a36Sopenharmony_ci * value.
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifndef _ASM_STACKPROTECTOR_H
1662306a36Sopenharmony_ci#define _ASM_STACKPROTECTOR_H 1
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include <asm/thread_info.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciextern unsigned long __stack_chk_guard;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/*
2362306a36Sopenharmony_ci * Initialize the stackprotector canary value.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * NOTE: this must only be called from functions that never return,
2662306a36Sopenharmony_ci * and it must always be inlined.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistatic __always_inline void boot_init_stack_canary(void)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	unsigned long canary = get_random_canary();
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	current->stack_canary = canary;
3362306a36Sopenharmony_ci#ifndef CONFIG_STACKPROTECTOR_PER_TASK
3462306a36Sopenharmony_ci	__stack_chk_guard = current->stack_canary;
3562306a36Sopenharmony_ci#endif
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#endif	/* _ASM_STACKPROTECTOR_H */
39