Linux Patch 1/1
Subsystems (3)
PATCH v4 1/1 Linux Patch 1/1
From 78fbf3237435a14cdebeb354e890029f7243aa58 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Date: Fri, 27 Apr 2018 03:33:04 -0700
Subject: [PATCH] x86/ssb: Enable option to mitigate SSB by explicit prctl
Processes that are unable to use other software mitigations and execute untrusted code may want to disable speculative store bypass. This patch adds a new prctl : PR_SET_SPEC_CTRL_MODE.
The prctl allows the control of speculation mode
to mitigate speculative store bypass vulnerability of the current process. When PR_SPEC_CTRL_MODE_RDS is set, the reduced data speculation will be set for the process to provide mitigation.
The prctl mode is inherited to children to handle threads and allow wrappers, but it is not broadcast to all threads in the same process. It is generally safe to inherit because it doesn't allow any new attacks, but just prevents them.
It also adds a matching prctl PR_GET_SPEC_CTRL_MODE to retrieve that state.
We allow using PR_SPEC_CTRL_MODE_NONE to clear the mode after PR_SPEC_CTRL_MODE_RDS is set. We expect that the attack will be from code within a JITed sandbox. We want to prevent the JITed code in sandbox from reading data it shouldn't read from its JITed sandbox. A JIT can turn on PR_SPEC_CTRL_MODE_RDS mode before running code in the JIT sandbox, and turn it off when returning. JIT sandboxed code should be unable to do system calls so there is no danger of it turning off PR_SPEC_CTRL_MODE_RDS. So allowing the prctl to disable the mode later should be safe. If the JIT'ed code can do system call, we have greater security issue on our hand.
We are not trying to protect native code processes in the same address space, or threads within the same process. They already can access data of each other.
To Do:
1. For older kernel that does not support this prctl interface, we'll get EINVAL returned. We will like to return some a more informative value if we issue this prctl on a kernel that support this interface, but lack support on the platform.
v4:
1. Fix incorrect condition for applying RDS in x86_restore_host_spec_ctrl and x86_set_guest_spec_ctrl.
2. Fix up a few unnecessary vendor checks after checking specctrl_dynamic_rds, which is set only for Intel cpu.
v3:
1. Get rid of static key usage and code clean up from patch review.
v2:
1. Switch from using RDS on seccomp processes to using an explicit prctl to enable RDS.
This patch is applied on top of v6 of Konrad's SSB patchset.
--- Documentation/admin-guide/kernel-parameters.txt | 4 + arch/x86/include/asm/msr-index.h | 3 +- arch/x86/include/asm/nospec-branch.h | 3 + arch/x86/include/asm/thread_info.h | 4 +- arch/x86/kernel/cpu/bugs.c | 111 ++++++++++++++++++++++-- arch/x86/kernel/cpu/intel.c | 2 +- arch/x86/kernel/process.c | 24 +++++ include/uapi/linux/prctl.h | 13 +++ kernel/sys.c | 16 ++++ 9 files changed, 172 insertions(+), 8 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -4053,6 +4053,10 @@ auto - kernel detects whether your CPU model contains a vulnerable implementation of Speculative Store Bypass and picks the most appropriate mitigation + prctl - disable Speculative Store Bypass for processes + via prctl. Processes run normally with + Seculative Store bypass by default unless + told otherwise by prctl. Not specifying this option is equivalent to spec_store_bypass_disable=auto.
arch/x86/include/asm/msr-index.h +2 -1
@@ -42,7 +42,8 @@#define MSR_IA32_SPEC_CTRL 0x00000048/* Speculation Control */ #define SPEC_CTRL_IBRS (1 << 0)/* Indirect Branch Restricted Speculation */ #define SPEC_CTRL_STIBP (1 << 1)/* Single Thread Indirect Branch Predictors */ -#define SPEC_CTRL_RDS (1 << 2)/* Reduced Data Speculation */ +#define SPEC_CTRL_RDS_SHIFT 2/* Reduced Data Speculation bit */ +#define SPEC_CTRL_RDS (1 << SPEC_CTRL_RDS_SHIFT)/* Reduced Data Speculation */ #define MSR_IA32_PRED_CMD 0x00000049/* Prediction Command */ #define PRED_CMD_IBPB (1 << 0)/* Indirect Branch Prediction Barrier */
arch/x86/include/asm/nospec-branch.h +3 -0
@@ -7,6 +7,7 @@#include <asm/alternative-asm.h> #include <asm/cpufeatures.h> #include <asm/msr-index.h> +#include <asm/percpu.h> /* * Fill the CPU return stack buffer.
@@ -242,6 +243,7 @@ extern void x86_restore_host_spec_ctrl(u64);enum ssb_mitigation {SPEC_STORE_BYPASS_NONE ,SPEC_STORE_BYPASS_DISABLE , +SPEC_STORE_BYPASS_PRCTL , };extern char __indirect_thunk_start[];
@@ -310,6 +312,7 @@ do { \preempt_enable (); \ } while (0) +extern bool specctrl_dynamic_rds;#endif /* __ASSEMBLY__ */ /*
arch/x86/include/asm/thread_info.h +3 -1
@@ -79,6 +79,7 @@ struct thread_info {#define TIF_SIGPENDING 2/* signal pending */ #define TIF_NEED_RESCHED 3/* rescheduling necessary */ #define TIF_SINGLESTEP 4/* reenable singlestep on user return*/ +#define TIF_RDS 5/* Reduced data speculation */ #define TIF_SYSCALL_EMU 6/* syscall emulation active */ #define TIF_SYSCALL_AUDIT 7/* syscall auditing active */ #define TIF_SECCOMP 8/* secure computing */
@@ -105,6 +106,7 @@ struct thread_info {#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP) +#define _TIF_RDS (1 << TIF_RDS)#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)#define _TIF_SECCOMP (1 << TIF_SECCOMP)
@@ -144,7 +146,7 @@ struct thread_info {/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW \ - (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP) + (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_RDS)#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
arch/x86/kernel/cpu/bugs.c +106 -5
173 lines not on this page · raw message
arch/x86/kernel/cpu/intel.c +1 -1
@@ -773,7 +773,7 @@ static void init_intel(struct cpuinfo_x86 *c)init_intel_misc_features (c);if (cpu_has (c,X86_FEATURE_SPEC_STORE_BYPASS_DISABLE )) -x86_set_spec_ctrl (SPEC_CTRL_RDS ); +x86_set_spec_ctrl (x86_get_default_spec_ctrl ()); }
arch/x86/kernel/process.c +24 -0
@@ -38,6 +38,7 @@#include <asm/switch_to.h> #include <asm/desc.h> #include <asm/prctl.h> +#include <asm/nospec-branch.h> /* * per-CPU TSS segments. Threads are completely 'soft' on Linux,
@@ -278,6 +279,26 @@ static inline void switch_to_bitmap(struct tss_struct *tss, } } +static inline void speculative_bypass_update (unsigned long tifn) +{ +u64 msr; + +if (!specctrl_dynamic_rds) +return ; + +/* +* Note: TIF_RDS bit position (5) is greater than +* SPEC_CTRL_RDS bit position (2). The shift value +* (TIF_RDS - SPEC_CTRL_RDS_SHIFT) used to convert from TIF_RDS +* bit position to SPEC_CTRL_RDS bit position +* below is positive and valid. +*/ + msr =x86_get_default_spec_ctrl () | + ((tifn & _TIF_RDS) >> (TIF_RDS -SPEC_CTRL_RDS_SHIFT )); + +wrmsrl (MSR_IA32_SPEC_CTRL , msr); +} +void __switch_to_xtra (struct task_struct *prev_p,struct task_struct *next_p,struct tss_struct *tss) {
@@ -309,6 +330,9 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,if ((tifp ^ tifn) & _TIF_NOCPUID)set_cpuid_faulting (!!(tifn & _TIF_NOCPUID)); + +if ((tifp ^ tifn) & _TIF_RDS) +speculative_bypass_update (tifn); }/*
include/uapi/linux/prctl.h +13 -0
@@ -207,4 +207,17 @@ struct prctl_mm_map {# define PR_SVE_VL_LEN_MASK 0xffff# define PR_SVE_VL_INHERIT (1 << 17)/* inherit across exec */ +/* +* Speculation Control Mode knobs +* +* Set this on a task to control speculation mode. +* Initial use is to mitigate against speculative store bypass attack using +* reduced data speculation mode. +*/ +#define PR_SET_SPEC_CTRL_MODE 52/* set speculation control mode of process */ +#define PR_SPEC_CTRL_MODE_NONE 0/* no speculation control mode in use */ +#define PR_SPEC_CTRL_MODE_RDS (1 << 0)/* reduced data speculation mode */ + +#define PR_GET_SPEC_CTRL_MODE 53/* get speculation control mode of process */ +#endif /* _LINUX_PRCTL_H */
kernel/sys.c +16 -0
@@ -2242,6 +2242,16 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)return 1 ; } +int __weak arch_prctl_set_spec_ctrl (struct task_struct *p,unsigned long arg) +{ +return 0 ; +} + +int __weak arch_prctl_get_spec_ctrl (struct task_struct *p) +{ +return 0 ; +} + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) {
@@ -2450,6 +2460,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,case PR_SVE_GET_VL : error =SVE_GET_VL ();break ; +case PR_SET_SPEC_CTRL_MODE : + error =arch_prctl_set_spec_ctrl (me, arg2); +break ; +case PR_GET_SPEC_CTRL_MODE : + error =arch_prctl_get_spec_ctrl (me); +break ;default : error = -EINVAL ;break ;
Signature · 1 line
2.9.4
From 78fbf3237435a14cdebeb354e890029f7243aa58 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Date: Fri, 27 Apr 2018 03:33:04 -0700
Subject: [PATCH] x86/ssb: Enable option to mitigate SSB by explicit prctl
Processes that are unable to use other software mitigations and execute untrusted code may want to disable speculative store bypass. This patch adds a new prctl : PR_SET_SPEC_CTRL_MODE.
The prctl allows the control of speculation mode
to mitigate speculative store bypass vulnerability of the current process. When PR_SPEC_CTRL_MODE_RDS is set, the reduced data speculation will be set for the process to provide mitigation.
The prctl mode is inherited to children to handle threads and allow wrappers, but it is not broadcast to all threads in the same process. It is generally safe to inherit because it doesn't allow any new attacks, but just prevents them.
It also adds a matching prctl PR_GET_SPEC_CTRL_MODE to retrieve that state.
We allow using PR_SPEC_CTRL_MODE_NONE to clear the mode after PR_SPEC_CTRL_MODE_RDS is set. We expect that the attack will be from code within a JITed sandbox. We want to prevent the JITed code in sandbox from reading data it shouldn't read from its JITed sandbox. A JIT can turn on PR_SPEC_CTRL_MODE_RDS mode before running code in the JIT sandbox, and turn it off when returning. JIT sandboxed code should be unable to do system calls so there is no danger of it turning off PR_SPEC_CTRL_MODE_RDS. So allowing the prctl to disable the mode later should be safe. If the JIT'ed code can do system call, we have greater security issue on our hand.
We are not trying to protect native code processes in the same address space, or threads within the same process. They already can access data of each other.
To Do:
1. For older kernel that does not support this prctl interface, we'll get EINVAL returned. We will like to return some a more informative value if we issue this prctl on a kernel that support this interface, but lack support on the platform.
v4:
1. Fix incorrect condition for applying RDS in x86_restore_host_spec_ctrl and x86_set_guest_spec_ctrl.
2. Fix up a few unnecessary vendor checks after checking specctrl_dynamic_rds, which is set only for Intel cpu.
v3:
1. Get rid of static key usage and code clean up from patch review.
v2:
1. Switch from using RDS on seccomp processes to using an explicit prctl to enable RDS.
This patch is applied on top of v6 of Konrad's SSB patchset.
--- Documentation/admin-guide/kernel-parameters.txt | 4 + arch/x86/include/asm/msr-index.h | 3 +- arch/x86/include/asm/nospec-branch.h | 3 + arch/x86/include/asm/thread_info.h | 4 +- arch/x86/kernel/cpu/bugs.c | 111 ++++++++++++++++++++++-- arch/x86/kernel/cpu/intel.c | 2 +- arch/x86/kernel/process.c | 24 +++++ include/uapi/linux/prctl.h | 13 +++ kernel/sys.c | 16 ++++ 9 files changed, 172 insertions(+), 8 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -4053,6 +4053,10 @@ auto - kernel detects whether your CPU model contains a vulnerable implementation of Speculative Store Bypass and picks the most appropriate mitigation + prctl - disable Speculative Store Bypass for processes + via prctl. Processes run normally with + Seculative Store bypass by default unless + told otherwise by prctl. Not specifying this option is equivalent to spec_store_bypass_disable=auto.
arch/x86/include/asm/msr-index.h +2 -1
@@ -42,7 +42,8 @@#define MSR_IA32_SPEC_CTRL 0x00000048/* Speculation Control */ #define SPEC_CTRL_IBRS (1 << 0)/* Indirect Branch Restricted Speculation */ #define SPEC_CTRL_STIBP (1 << 1)/* Single Thread Indirect Branch Predictors */ -#define SPEC_CTRL_RDS (1 << 2)/* Reduced Data Speculation */ +#define SPEC_CTRL_RDS_SHIFT 2/* Reduced Data Speculation bit */ +#define SPEC_CTRL_RDS (1 << SPEC_CTRL_RDS_SHIFT)/* Reduced Data Speculation */ #define MSR_IA32_PRED_CMD 0x00000049/* Prediction Command */ #define PRED_CMD_IBPB (1 << 0)/* Indirect Branch Prediction Barrier */
arch/x86/include/asm/nospec-branch.h +3 -0
@@ -7,6 +7,7 @@#include <asm/alternative-asm.h> #include <asm/cpufeatures.h> #include <asm/msr-index.h> +#include <asm/percpu.h> /* * Fill the CPU return stack buffer.
@@ -242,6 +243,7 @@ extern void x86_restore_host_spec_ctrl(u64);enum ssb_mitigation {SPEC_STORE_BYPASS_NONE ,SPEC_STORE_BYPASS_DISABLE , +SPEC_STORE_BYPASS_PRCTL , };extern char __indirect_thunk_start[];
@@ -310,6 +312,7 @@ do { \preempt_enable (); \ } while (0) +extern bool specctrl_dynamic_rds;#endif /* __ASSEMBLY__ */ /*
arch/x86/include/asm/thread_info.h +3 -1
@@ -79,6 +79,7 @@ struct thread_info {#define TIF_SIGPENDING 2/* signal pending */ #define TIF_NEED_RESCHED 3/* rescheduling necessary */ #define TIF_SINGLESTEP 4/* reenable singlestep on user return*/ +#define TIF_RDS 5/* Reduced data speculation */ #define TIF_SYSCALL_EMU 6/* syscall emulation active */ #define TIF_SYSCALL_AUDIT 7/* syscall auditing active */ #define TIF_SECCOMP 8/* secure computing */
@@ -105,6 +106,7 @@ struct thread_info {#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP) +#define _TIF_RDS (1 << TIF_RDS)#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)#define _TIF_SECCOMP (1 << TIF_SECCOMP)
@@ -144,7 +146,7 @@ struct thread_info {/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW \ - (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP) + (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_RDS)#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
arch/x86/kernel/cpu/bugs.c +106 -5
173 lines not on this page · raw message
arch/x86/kernel/cpu/intel.c +1 -1
@@ -773,7 +773,7 @@ static void init_intel(struct cpuinfo_x86 *c)init_intel_misc_features (c);if (cpu_has (c,X86_FEATURE_SPEC_STORE_BYPASS_DISABLE )) -x86_set_spec_ctrl (SPEC_CTRL_RDS ); +x86_set_spec_ctrl (x86_get_default_spec_ctrl ()); }
arch/x86/kernel/process.c +24 -0
@@ -38,6 +38,7 @@#include <asm/switch_to.h> #include <asm/desc.h> #include <asm/prctl.h> +#include <asm/nospec-branch.h> /* * per-CPU TSS segments. Threads are completely 'soft' on Linux,
@@ -278,6 +279,26 @@ static inline void switch_to_bitmap(struct tss_struct *tss, } } +static inline void speculative_bypass_update (unsigned long tifn) +{ +u64 msr; + +if (!specctrl_dynamic_rds) +return ; + +/* +* Note: TIF_RDS bit position (5) is greater than +* SPEC_CTRL_RDS bit position (2). The shift value +* (TIF_RDS - SPEC_CTRL_RDS_SHIFT) used to convert from TIF_RDS +* bit position to SPEC_CTRL_RDS bit position +* below is positive and valid. +*/ + msr =x86_get_default_spec_ctrl () | + ((tifn & _TIF_RDS) >> (TIF_RDS -SPEC_CTRL_RDS_SHIFT )); + +wrmsrl (MSR_IA32_SPEC_CTRL , msr); +} +void __switch_to_xtra (struct task_struct *prev_p,struct task_struct *next_p,struct tss_struct *tss) {
@@ -309,6 +330,9 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,if ((tifp ^ tifn) & _TIF_NOCPUID)set_cpuid_faulting (!!(tifn & _TIF_NOCPUID)); + +if ((tifp ^ tifn) & _TIF_RDS) +speculative_bypass_update (tifn); }/*
include/uapi/linux/prctl.h +13 -0
@@ -207,4 +207,17 @@ struct prctl_mm_map {# define PR_SVE_VL_LEN_MASK 0xffff# define PR_SVE_VL_INHERIT (1 << 17)/* inherit across exec */ +/* +* Speculation Control Mode knobs +* +* Set this on a task to control speculation mode. +* Initial use is to mitigate against speculative store bypass attack using +* reduced data speculation mode. +*/ +#define PR_SET_SPEC_CTRL_MODE 52/* set speculation control mode of process */ +#define PR_SPEC_CTRL_MODE_NONE 0/* no speculation control mode in use */ +#define PR_SPEC_CTRL_MODE_RDS (1 << 0)/* reduced data speculation mode */ + +#define PR_GET_SPEC_CTRL_MODE 53/* get speculation control mode of process */ +#endif /* _LINUX_PRCTL_H */
kernel/sys.c +16 -0
@@ -2242,6 +2242,16 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)return 1 ; } +int __weak arch_prctl_set_spec_ctrl (struct task_struct *p,unsigned long arg) +{ +return 0 ; +} + +int __weak arch_prctl_get_spec_ctrl (struct task_struct *p) +{ +return 0 ; +} + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) {
@@ -2450,6 +2460,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,case PR_SVE_GET_VL : error =SVE_GET_VL ();break ; +case PR_SET_SPEC_CTRL_MODE : + error =arch_prctl_set_spec_ctrl (me, arg2); +break ; +case PR_GET_SPEC_CTRL_MODE : + error =arch_prctl_get_spec_ctrl (me); +break ;default : error = -EINVAL ;break ;
Signature · 1 line
2.9.4