@@ -14,11 +14,16 @@ struct thread_shstk {
#ifdef CONFIG_X86_SHADOW_STACK
int shstk_setup(void);
+int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags,
+ unsigned long stack_size);
void shstk_free(struct task_struct *p);
int shstk_disable(void);
void reset_thread_shstk(void);
#else
static inline void shstk_setup(void) {}
+static inline int shstk_alloc_thread_stack(struct task_struct *p,
+ unsigned long clone_flags,
+ unsigned long stack_size) { return 0; }
static inline void shstk_free(struct task_struct *p) {}
static inline void shstk_disable(void) {}
static inline void reset_thread_shstk(void) {}
@@ -146,6 +146,8 @@ do { \
#else
#define deactivate_mm(tsk, mm) \
do { \
+ if (!tsk->vfork_done) \
+ shstk_free(tsk); \
load_gs_index(0); \
loadsegment(fs, 0); \
} while (0)
@@ -46,6 +46,7 @@
#include <asm/proto.h>
#include <asm/frame.h>
#include <asm/unwind.h>
+#include <asm/cet.h>
#include "process.h"
@@ -117,6 +118,7 @@ void exit_thread(struct task_struct *tsk)
free_vm86(t);
+ shstk_free(tsk);
fpu__drop(fpu);
}
@@ -217,6 +219,10 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
if (clone_flags & CLONE_SETTLS)
ret = set_new_tls(p, tls);
+ /* Allocate a new shadow stack for pthread */
+ if (!ret)
+ ret = shstk_alloc_thread_stack(p, clone_flags, stack_size);
+
if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
io_bitmap_share(p);
@@ -106,6 +106,66 @@ void reset_thread_shstk(void)
memset(¤t->thread.shstk, 0, sizeof(struct thread_shstk));
}
+int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags,
+ unsigned long stack_size)
+{
+ struct thread_shstk *shstk = &tsk->thread.shstk;
+ unsigned long addr;
+ void *xstate;
+
+ /*
+ * If shadow stack is not enabled on the new thread, skip any
+ * switch to a new shadow stack.
+ */
+ if (!shstk->size)
+ return 0;
+
+ /*
+ * clone() does not pass stack_size, which was added to clone3().
+ * Use RLIMIT_STACK and cap to 4 GB.
+ */
+ if (!stack_size)
+ stack_size = min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G);
+
+ /*
+ * For CLONE_VM, except vfork, the child needs a separate shadow
+ * stack.
+ */
+ if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM)
+ return 0;
+
+
+ /*
+ * Compat-mode pthreads share a limited address space.
+ * If each function call takes an average of four slots
+ * stack space, allocate 1/4 of stack size for shadow stack.
+ */
+ if (in_compat_syscall())
+ stack_size /= 4;
+
+ /*
+ * 'tsk' is configured with a shadow stack and the fpu.state is
+ * up to date since it was just copied from the parent. There
+ * must be a valid non-init CET state location in the buffer.
+ */
+ xstate = get_xsave_buffer_unsafe(&tsk->thread.fpu, XFEATURE_CET_USER);
+ if (WARN_ON_ONCE(!xstate))
+ return -EINVAL;
+
+ stack_size = PAGE_ALIGN(stack_size);
+ addr = alloc_shstk(stack_size);
+ if (IS_ERR_VALUE(addr)) {
+ shstk->base = 0;
+ shstk->size = 0;
+ return PTR_ERR((void *)addr);
+ }
+
+ xsave_wrmsrl_unsafe(xstate, MSR_IA32_PL3_SSP, (u64)(addr + stack_size));
+ shstk->base = addr;
+ shstk->size = stack_size;
+ return 0;
+}
+
void shstk_free(struct task_struct *tsk)
{
struct thread_shstk *shstk = &tsk->thread.shstk;
@@ -115,7 +175,13 @@ void shstk_free(struct task_struct *tsk)
!shstk->base)
return;
- if (!tsk->mm)
+ /*
+ * When fork() with CLONE_VM fails, the child (tsk) already has a
+ * shadow stack allocated, and exit_thread() calls this function to
+ * free it. In this case the parent (current) and the child share
+ * the same mm struct.
+ */
+ if (!tsk->mm || tsk->mm != current->mm)
return;
unmap_shadow_stack(shstk->base, shstk->size);