Skip to content

Commit 98cfa46

Browse files
rpedgecohansendc
authored andcommitted
x86: Introduce userspace API for shadow stack
Add three new arch_prctl() handles: - ARCH_SHSTK_ENABLE/DISABLE enables or disables the specified feature. Returns 0 on success or a negative value on error. - ARCH_SHSTK_LOCK prevents future disabling or enabling of the specified feature. Returns 0 on success or a negative value on error. The features are handled per-thread and inherited over fork(2)/clone(2), but reset on exec(). Co-developed-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Rick Edgecombe <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Mike Rapoport (IBM) <[email protected]> Tested-by: Pengfei Xu <[email protected]> Tested-by: John Allen <[email protected]> Tested-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/all/20230613001108.3040476-27-rick.p.edgecombe%40intel.com
1 parent 6ee8366 commit 98cfa46

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

arch/x86/include/asm/processor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct vm86;
2828
#include <asm/unwind_hints.h>
2929
#include <asm/vmxfeatures.h>
3030
#include <asm/vdso/processor.h>
31+
#include <asm/shstk.h>
3132

3233
#include <linux/personality.h>
3334
#include <linux/cache.h>
@@ -475,6 +476,11 @@ struct thread_struct {
475476
*/
476477
u32 pkru;
477478

479+
#ifdef CONFIG_X86_USER_SHADOW_STACK
480+
unsigned long features;
481+
unsigned long features_locked;
482+
#endif
483+
478484
/* Floating point and extended processor state */
479485
struct fpu fpu;
480486
/*

arch/x86/include/asm/shstk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_X86_SHSTK_H
3+
#define _ASM_X86_SHSTK_H
4+
5+
#ifndef __ASSEMBLY__
6+
#include <linux/types.h>
7+
8+
struct task_struct;
9+
10+
#ifdef CONFIG_X86_USER_SHADOW_STACK
11+
long shstk_prctl(struct task_struct *task, int option, unsigned long features);
12+
void reset_thread_features(void);
13+
#else
14+
static inline long shstk_prctl(struct task_struct *task, int option,
15+
unsigned long arg2) { return -EINVAL; }
16+
static inline void reset_thread_features(void) {}
17+
#endif /* CONFIG_X86_USER_SHADOW_STACK */
18+
19+
#endif /* __ASSEMBLY__ */
20+
21+
#endif /* _ASM_X86_SHSTK_H */

arch/x86/include/uapi/asm/prctl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#define ARCH_MAP_VDSO_32 0x2002
2424
#define ARCH_MAP_VDSO_64 0x2003
2525

26+
/* Don't use 0x3001-0x3004 because of old glibcs */
27+
2628
#define ARCH_GET_UNTAG_MASK 0x4001
2729
#define ARCH_ENABLE_TAGGED_ADDR 0x4002
2830
#define ARCH_GET_MAX_TAG_BITS 0x4003
2931
#define ARCH_FORCE_TAGGED_SVA 0x4004
3032

33+
#define ARCH_SHSTK_ENABLE 0x5001
34+
#define ARCH_SHSTK_DISABLE 0x5002
35+
#define ARCH_SHSTK_LOCK 0x5003
36+
3137
#endif /* _ASM_X86_PRCTL_H */

arch/x86/kernel/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ obj-$(CONFIG_CALL_THUNKS) += callthunks.o
147147

148148
obj-$(CONFIG_X86_CET) += cet.o
149149

150+
obj-$(CONFIG_X86_USER_SHADOW_STACK) += shstk.o
151+
150152
###
151153
# 64 bit specific files
152154
ifeq ($(CONFIG_X86_64),y)

arch/x86/kernel/process_64.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip,
515515
load_gs_index(__USER_DS);
516516
}
517517

518+
reset_thread_features();
519+
518520
loadsegment(fs, 0);
519521
loadsegment(es, _ds);
520522
loadsegment(ds, _ds);
@@ -894,6 +896,10 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
894896
else
895897
return put_user(LAM_U57_BITS, (unsigned long __user *)arg2);
896898
#endif
899+
case ARCH_SHSTK_ENABLE:
900+
case ARCH_SHSTK_DISABLE:
901+
case ARCH_SHSTK_LOCK:
902+
return shstk_prctl(task, option, arg2);
897903
default:
898904
ret = -EINVAL;
899905
break;

arch/x86/kernel/shstk.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* shstk.c - Intel shadow stack support
4+
*
5+
* Copyright (c) 2021, Intel Corporation.
6+
* Yu-cheng Yu <[email protected]>
7+
*/
8+
9+
#include <linux/sched.h>
10+
#include <linux/bitops.h>
11+
#include <asm/prctl.h>
12+
13+
void reset_thread_features(void)
14+
{
15+
current->thread.features = 0;
16+
current->thread.features_locked = 0;
17+
}
18+
19+
long shstk_prctl(struct task_struct *task, int option, unsigned long features)
20+
{
21+
if (option == ARCH_SHSTK_LOCK) {
22+
task->thread.features_locked |= features;
23+
return 0;
24+
}
25+
26+
/* Don't allow via ptrace */
27+
if (task != current)
28+
return -EINVAL;
29+
30+
/* Do not allow to change locked features */
31+
if (features & task->thread.features_locked)
32+
return -EPERM;
33+
34+
/* Only support enabling/disabling one feature at a time. */
35+
if (hweight_long(features) > 1)
36+
return -EINVAL;
37+
38+
if (option == ARCH_SHSTK_DISABLE) {
39+
return -EINVAL;
40+
}
41+
42+
/* Handle ARCH_SHSTK_ENABLE */
43+
return -EINVAL;
44+
}

0 commit comments

Comments
 (0)