2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2009-02-09 22:17:40 +09:00
|
|
|
/*
|
|
|
|
|
* GCC stack protector support.
|
|
|
|
|
*
|
2025-01-23 14:07:39 -05:00
|
|
|
* Stack protector works by putting a predefined pattern at the start of
|
2009-02-09 22:17:40 +09:00
|
|
|
* the stack frame and verifying that it hasn't been overwritten when
|
2025-01-23 14:07:39 -05:00
|
|
|
* returning from the function. The pattern is called the stack canary
|
|
|
|
|
* and is a unique value for each task.
|
2009-02-09 22:17:40 +09:00
|
|
|
*/
|
|
|
|
|
|
2008-02-14 09:41:09 +01:00
|
|
|
#ifndef _ASM_STACKPROTECTOR_H
|
|
|
|
|
#define _ASM_STACKPROTECTOR_H 1
|
|
|
|
|
|
2018-06-14 12:21:18 +09:00
|
|
|
#ifdef CONFIG_STACKPROTECTOR
|
2009-02-09 22:17:39 +09:00
|
|
|
|
2008-02-14 09:56:04 +01:00
|
|
|
#include <asm/tsc.h>
|
2009-01-19 12:21:28 +09:00
|
|
|
#include <asm/processor.h>
|
2009-02-09 22:17:39 +09:00
|
|
|
#include <asm/percpu.h>
|
2009-02-09 22:17:40 +09:00
|
|
|
#include <asm/desc.h>
|
2015-04-26 16:56:05 +02:00
|
|
|
|
|
|
|
|
#include <linux/sched.h>
|
2008-02-14 09:56:04 +01:00
|
|
|
|
2025-03-03 11:52:45 -05:00
|
|
|
DECLARE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard);
|
2025-01-23 14:07:39 -05:00
|
|
|
|
2008-02-14 09:42:02 +01:00
|
|
|
/*
|
|
|
|
|
* Initialize the stackprotector canary value.
|
|
|
|
|
*
|
2020-04-22 18:11:30 +02:00
|
|
|
* NOTE: this must only be called from functions that never return
|
2008-02-14 09:42:02 +01:00
|
|
|
* and it must always be inlined.
|
2020-04-22 18:11:30 +02:00
|
|
|
*
|
|
|
|
|
* In addition, it should be called from a compilation unit for which
|
|
|
|
|
* stack protector is disabled. Alternatively, the caller should not end
|
|
|
|
|
* with a function call which gets tail-call optimized as that would
|
|
|
|
|
* lead to checking a modified canary value.
|
2008-02-14 09:42:02 +01:00
|
|
|
*/
|
|
|
|
|
static __always_inline void boot_init_stack_canary(void)
|
|
|
|
|
{
|
2022-10-23 22:14:23 +02:00
|
|
|
unsigned long canary = get_random_canary();
|
2008-02-14 09:56:04 +01:00
|
|
|
|
|
|
|
|
current->stack_canary = canary;
|
2021-02-13 11:19:44 -08:00
|
|
|
this_cpu_write(__stack_chk_guard, canary);
|
2009-02-09 22:17:40 +09:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 18:56:24 -04:00
|
|
|
static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
|
|
|
|
|
{
|
2021-02-13 11:19:44 -08:00
|
|
|
per_cpu(__stack_chk_guard, cpu) = idle->stack_canary;
|
2009-02-09 22:17:40 +09:00
|
|
|
}
|
|
|
|
|
|
2018-06-14 12:21:18 +09:00
|
|
|
#else /* STACKPROTECTOR */
|
2009-02-09 22:17:40 +09:00
|
|
|
|
|
|
|
|
/* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
|
|
|
|
|
|
2020-06-17 18:56:24 -04:00
|
|
|
static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
|
|
|
|
|
{ }
|
|
|
|
|
|
2018-06-14 12:21:18 +09:00
|
|
|
#endif /* STACKPROTECTOR */
|
2009-02-09 22:17:39 +09:00
|
|
|
#endif /* _ASM_STACKPROTECTOR_H */
|