Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Shadow Call Stack support.
0004  *
0005  * Copyright (C) 2019 Google LLC
0006  */
0007 
0008 #ifndef _LINUX_SCS_H
0009 #define _LINUX_SCS_H
0010 
0011 #include <linux/gfp.h>
0012 #include <linux/poison.h>
0013 #include <linux/sched.h>
0014 #include <linux/sizes.h>
0015 
0016 #ifdef CONFIG_SHADOW_CALL_STACK
0017 
0018 #define SCS_ORDER       0
0019 #define SCS_SIZE        (PAGE_SIZE << SCS_ORDER)
0020 #define GFP_SCS         (GFP_KERNEL | __GFP_ZERO)
0021 
0022 /* An illegal pointer value to mark the end of the shadow stack. */
0023 #define SCS_END_MAGIC       (0x5f6UL + POISON_POINTER_DELTA)
0024 
0025 #define task_scs(tsk)       (task_thread_info(tsk)->scs_base)
0026 #define task_scs_sp(tsk)    (task_thread_info(tsk)->scs_sp)
0027 
0028 void *scs_alloc(int node);
0029 void scs_free(void *s);
0030 void scs_init(void);
0031 int scs_prepare(struct task_struct *tsk, int node);
0032 void scs_release(struct task_struct *tsk);
0033 
0034 static inline void scs_task_reset(struct task_struct *tsk)
0035 {
0036     /*
0037      * Reset the shadow stack to the base address in case the task
0038      * is reused.
0039      */
0040     task_scs_sp(tsk) = task_scs(tsk);
0041 }
0042 
0043 static inline unsigned long *__scs_magic(void *s)
0044 {
0045     return (unsigned long *)(s + SCS_SIZE) - 1;
0046 }
0047 
0048 static inline bool task_scs_end_corrupted(struct task_struct *tsk)
0049 {
0050     unsigned long *magic = __scs_magic(task_scs(tsk));
0051     unsigned long sz = task_scs_sp(tsk) - task_scs(tsk);
0052 
0053     return sz >= SCS_SIZE - 1 || READ_ONCE_NOCHECK(*magic) != SCS_END_MAGIC;
0054 }
0055 
0056 #else /* CONFIG_SHADOW_CALL_STACK */
0057 
0058 static inline void *scs_alloc(int node) { return NULL; }
0059 static inline void scs_free(void *s) {}
0060 static inline void scs_init(void) {}
0061 static inline void scs_task_reset(struct task_struct *tsk) {}
0062 static inline int scs_prepare(struct task_struct *tsk, int node) { return 0; }
0063 static inline void scs_release(struct task_struct *tsk) {}
0064 static inline bool task_scs_end_corrupted(struct task_struct *tsk) { return false; }
0065 
0066 #endif /* CONFIG_SHADOW_CALL_STACK */
0067 
0068 #endif /* _LINUX_SCS_H */