From 102a0743326a03cd1a1202ceda21e175b7d3575c Mon Sep 17 00:00:00 2001 From: hc <hc@nodka.com> Date: Tue, 20 Feb 2024 01:20:52 +0000 Subject: [PATCH] add new system file --- kernel/include/linux/scs.h | 61 ++++++++++++++++++------------ 1 files changed, 36 insertions(+), 25 deletions(-) diff --git a/kernel/include/linux/scs.h b/kernel/include/linux/scs.h index c5572fd..18122d9 100644 --- a/kernel/include/linux/scs.h +++ b/kernel/include/linux/scs.h @@ -9,48 +9,59 @@ #define _LINUX_SCS_H #include <linux/gfp.h> +#include <linux/poison.h> #include <linux/sched.h> -#include <asm/page.h> +#include <linux/sizes.h> #ifdef CONFIG_SHADOW_CALL_STACK -/* - * In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit - * architecture) provided ~40% safety margin on stack usage while keeping - * memory allocation overhead reasonable. - */ -#define SCS_SIZE 1024UL -#define GFP_SCS (GFP_KERNEL | __GFP_ZERO) +#define SCS_ORDER 0 +#define SCS_SIZE (PAGE_SIZE << SCS_ORDER) +#define GFP_SCS (GFP_KERNEL | __GFP_ZERO) -/* - * A random number outside the kernel's virtual address space to mark the - * end of the shadow stack. - */ -#define SCS_END_MAGIC 0xaf0194819b1635f6UL +/* An illegal pointer value to mark the end of the shadow stack. */ +#define SCS_END_MAGIC (0x5f6UL + POISON_POINTER_DELTA) -#define task_scs(tsk) (task_thread_info(tsk)->shadow_call_stack) +#define task_scs(tsk) (task_thread_info(tsk)->scs_base) +#define task_scs_sp(tsk) (task_thread_info(tsk)->scs_sp) -static inline void task_set_scs(struct task_struct *tsk, void *s) +void *scs_alloc(int node); +void scs_free(void *s); +void scs_init(void); +int scs_prepare(struct task_struct *tsk, int node); +void scs_release(struct task_struct *tsk); + +static inline void scs_task_reset(struct task_struct *tsk) { - task_scs(tsk) = s; + /* + * Reset the shadow stack to the base address in case the task + * is reused. + */ + task_scs_sp(tsk) = task_scs(tsk); } -extern void scs_init(void); -extern void scs_task_reset(struct task_struct *tsk); -extern int scs_prepare(struct task_struct *tsk, int node); -extern bool scs_corrupted(struct task_struct *tsk); -extern void scs_release(struct task_struct *tsk); +static inline unsigned long *__scs_magic(void *s) +{ + return (unsigned long *)(s + SCS_SIZE) - 1; +} + +static inline bool task_scs_end_corrupted(struct task_struct *tsk) +{ + unsigned long *magic = __scs_magic(task_scs(tsk)); + unsigned long sz = task_scs_sp(tsk) - task_scs(tsk); + + return sz >= SCS_SIZE - 1 || READ_ONCE_NOCHECK(*magic) != SCS_END_MAGIC; +} #else /* CONFIG_SHADOW_CALL_STACK */ -#define task_scs(tsk) NULL - -static inline void task_set_scs(struct task_struct *tsk, void *s) {} +static inline void *scs_alloc(int node) { return NULL; } +static inline void scs_free(void *s) {} static inline void scs_init(void) {} static inline void scs_task_reset(struct task_struct *tsk) {} static inline int scs_prepare(struct task_struct *tsk, int node) { return 0; } -static inline bool scs_corrupted(struct task_struct *tsk) { return false; } static inline void scs_release(struct task_struct *tsk) {} +static inline bool task_scs_end_corrupted(struct task_struct *tsk) { return false; } #endif /* CONFIG_SHADOW_CALL_STACK */ -- Gitblit v1.6.2