2020-04-28 00:00:07 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
|
|
|
* Shadow Call Stack support.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Google LLC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_SCS_H
|
|
|
|
#define _LINUX_SCS_H
|
|
|
|
|
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <linux/poison.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_SHADOW_CALL_STACK
|
|
|
|
|
2020-12-01 07:34:41 +08:00
|
|
|
#define SCS_ORDER 0
|
|
|
|
#define SCS_SIZE (PAGE_SIZE << SCS_ORDER)
|
2020-04-28 00:00:07 +08:00
|
|
|
#define GFP_SCS (GFP_KERNEL | __GFP_ZERO)
|
|
|
|
|
|
|
|
/* 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)->scs_base)
|
2020-05-15 21:11:05 +08:00
|
|
|
#define task_scs_sp(tsk) (task_thread_info(tsk)->scs_sp)
|
2020-04-28 00:00:07 +08:00
|
|
|
|
2020-12-01 07:34:41 +08:00
|
|
|
void *scs_alloc(int node);
|
|
|
|
void scs_free(void *s);
|
2020-04-28 00:00:07 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Reset the shadow stack to the base address in case the task
|
|
|
|
* is reused.
|
|
|
|
*/
|
2020-05-15 21:11:05 +08:00
|
|
|
task_scs_sp(tsk) = task_scs(tsk);
|
2020-04-28 00:00:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long *__scs_magic(void *s)
|
|
|
|
{
|
|
|
|
return (unsigned long *)(s + SCS_SIZE) - 1;
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:56:05 +08:00
|
|
|
static inline bool task_scs_end_corrupted(struct task_struct *tsk)
|
2020-04-28 00:00:07 +08:00
|
|
|
{
|
|
|
|
unsigned long *magic = __scs_magic(task_scs(tsk));
|
2020-05-15 21:11:05 +08:00
|
|
|
unsigned long sz = task_scs_sp(tsk) - task_scs(tsk);
|
2020-04-28 00:00:07 +08:00
|
|
|
|
2020-05-15 21:11:05 +08:00
|
|
|
return sz >= SCS_SIZE - 1 || READ_ONCE_NOCHECK(*magic) != SCS_END_MAGIC;
|
2020-04-28 00:00:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_SHADOW_CALL_STACK */
|
|
|
|
|
2020-12-01 07:34:41 +08:00
|
|
|
static inline void *scs_alloc(int node) { return NULL; }
|
|
|
|
static inline void scs_free(void *s) {}
|
2020-04-28 00:00:07 +08:00
|
|
|
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 void scs_release(struct task_struct *tsk) {}
|
2020-05-15 21:56:05 +08:00
|
|
|
static inline bool task_scs_end_corrupted(struct task_struct *tsk) { return false; }
|
2020-04-28 00:00:07 +08:00
|
|
|
|
|
|
|
#endif /* CONFIG_SHADOW_CALL_STACK */
|
|
|
|
|
|
|
|
#endif /* _LINUX_SCS_H */
|