@@ -40,6 +40,12 @@ static inline int setup_signal_shadow_stack(int proc32, void __user *restorer) {
static inline int restore_signal_shadow_stack(void) { return 0; }
#endif /* CONFIG_X86_SHADOW_STACK */
+#ifdef CONFIG_X86_SHADOW_STACK
+int prctl_elf_feature(int option, u64 arg2);
+#else
+static inline int prctl_elf_feature(int option, u64 arg2) { return -EINVAL; }
+#endif
+
#endif /* __ASSEMBLY__ */
#endif /* _ASM_X86_CET_H */
@@ -531,6 +531,7 @@ struct thread_struct {
#ifdef CONFIG_X86_SHADOW_STACK
struct thread_shstk shstk;
+ u64 feat_prctl_locked;
#endif
/* Floating point and extended processor state */
@@ -20,4 +20,14 @@
#define ARCH_MAP_VDSO_32 0x2002
#define ARCH_MAP_VDSO_64 0x2003
+#define ARCH_X86_FEATURE_STATUS 0x3001
+#define ARCH_X86_FEATURE_DISABLE 0x3002
+#define ARCH_X86_FEATURE_LOCK 0x3003
+#define ARCH_X86_FEATURE_ENABLE 0x3004
+
+/* x86 feature bits to be used with ARCH_X86_FEATURE arch_prctl()s */
+#define LINUX_X86_FEATURE_IBT 0x00000001
+#define LINUX_X86_FEATURE_SHSTK 0x00000002
+
+
#endif /* _ASM_X86_PRCTL_H */
@@ -153,7 +153,7 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev.o
obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o
-obj-$(CONFIG_X86_SHADOW_STACK) += shstk.o
+obj-$(CONFIG_X86_SHADOW_STACK) += shstk.o elf_feature_prctl.o
###
# 64 bit specific files
ifeq ($(CONFIG_X86_64),y)
new file mode 100644
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/errno.h>
+#include <linux/uaccess.h>
+#include <linux/prctl.h>
+#include <linux/compat.h>
+#include <linux/mman.h>
+#include <linux/elfcore.h>
+#include <linux/processor.h>
+#include <asm/prctl.h>
+#include <asm/cet.h>
+
+/* See Documentation/x86/intel_cet.rst. */
+
+static int elf_feat_copy_status_to_user(struct thread_shstk *shstk, u64 __user *ubuf)
+{
+ u64 buf[3] = {};
+
+ if (shstk->size) {
+ buf[0] = LINUX_X86_FEATURE_SHSTK;
+ buf[1] = shstk->base;
+ buf[2] = shstk->size;
+ }
+
+ return copy_to_user(ubuf, buf, sizeof(buf));
+}
+
+int prctl_elf_feature(int option, u64 arg2)
+{
+ struct thread_struct *thread = ¤t->thread;
+ u64 feat_succ = 0;
+
+ if (!cpu_feature_enabled(X86_FEATURE_SHSTK))
+ return -EOPNOTSUPP;
+
+ switch (option) {
+ case ARCH_X86_FEATURE_STATUS:
+ return elf_feat_copy_status_to_user(&thread->shstk, (u64 __user *)arg2);
+ case ARCH_X86_FEATURE_DISABLE:
+ if (arg2 & thread->feat_prctl_locked)
+ return -EPERM;
+
+ if (arg2 & LINUX_X86_FEATURE_SHSTK && !shstk_disable())
+ feat_succ |= LINUX_X86_FEATURE_SHSTK;
+
+ if (feat_succ != arg2)
+ return -ECANCELED;
+ return 0;
+ case ARCH_X86_FEATURE_ENABLE:
+ if (arg2 & thread->feat_prctl_locked)
+ return -EPERM;
+
+ if (arg2 & LINUX_X86_FEATURE_SHSTK && !shstk_setup())
+ feat_succ |= LINUX_X86_FEATURE_SHSTK;
+
+ if (feat_succ != arg2)
+ return -ECANCELED;
+ return 0;
+ case ARCH_X86_FEATURE_LOCK:
+ thread->feat_prctl_locked |= arg2;
+ return 0;
+
+ default:
+ return -EINVAL;
+ }
+}
@@ -1005,5 +1005,5 @@ long do_arch_prctl_common(struct task_struct *task, int option,
return fpu_xstate_prctl(task, option, arg2);
}
- return -EINVAL;
+ return prctl_elf_feature(option, arg2);
}
@@ -130,6 +130,7 @@ int shstk_setup(void)
void reset_thread_shstk(void)
{
+ current->thread.feat_prctl_locked = 0;
memset(¤t->thread.shstk, 0, sizeof(struct thread_shstk));
}