Return-path: Received: from mga01.intel.com ([192.55.52.88]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fBUW0-0001tY-E8 for speck@linutronix.de; Thu, 26 Apr 2018 02:12:01 +0200 Subject: [MODERATED] [PATCH v2] Linux Patch 1/1 From: Tim Chen Message-ID: <92c45587-5eb3-2421-6fc1-42e74a715e30@linux.intel.com> Date: Wed, 25 Apr 2018 17:11:56 -0700 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="wRMMWQwiNpKIb2LXUZHV9bBthgdFPRk0z"; protected-headers="v1" To: speck@linutronix.de List-ID: This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156) --wRMMWQwiNpKIb2LXUZHV9bBthgdFPRk0z Content-Type: multipart/mixed; boundary="bZxtC0gbvIyDyDpSukNoaS2xHQir7ip4Z" --bZxtC0gbvIyDyDpSukNoaS2xHQir7ip4Z Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable =46rom bf437945b231c9daaa3b927f50e57a868b7757d9 Mon Sep 17 00:00:00 2001 From: Tim Chen Date: Mon, 23 Apr 2018 05:57:53 -0700 Subject: [PATCH v2] Linux Patch 1/1=20 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_RDS_MODE. The prctl allows the enable/disable of reduced data speculation mode to mitigate speculative store bypass vulnerability of the current process. The prctl mode is inherited to children to handle threads and allow wrapp= ers, but it is not broadcast to all threads in the same process. It is general= ly safe to inherit because it doesn't allow any new attacks, but just preven= ts them. It also adds a matching prctl PR_GET_RDS_MODE to retrieve that state. This patch is applied on top of v4 of Konrad's SSB patchset. v2: Switch from using RDS on seccomp processes to using an explicit prctl to enable RDS. Signed-off-by: Tim Chen --- Documentation/admin-guide/kernel-parameters.txt | 4 ++ arch/x86/include/asm/nospec-branch.h | 17 +++++ arch/x86/include/asm/thread_info.h | 2 + arch/x86/kernel/cpu/bugs.c | 86 +++++++++++++++++++= ++++-- arch/x86/kernel/cpu/intel.c | 4 +- arch/x86/kernel/process_64.c | 12 ++++ include/uapi/linux/prctl.h | 12 ++++ kernel/fork.c | 8 +++ kernel/sys.c | 16 +++++ 9 files changed, 155 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentat= ion/admin-guide/kernel-parameters.txt index a2d337b..1f95161 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -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 + Speculative Store bypass by default unless + told otherwise by prctl. =20 Not specifying this option is equivalent to spec_store_bypass_disable=3Dauto. diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/= nospec-branch.h index 6f3d224..a6bb9da 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -3,10 +3,12 @@ #ifndef _ASM_X86_NOSPEC_BRANCH_H_ #define _ASM_X86_NOSPEC_BRANCH_H_ =20 +#include #include #include #include #include +#include =20 /* * Fill the CPU return stack buffer. @@ -242,11 +244,15 @@ extern void x86_restore_host_spec_ctrl(u64); enum ssb_mitigation { SPEC_STORE_BYPASS_NONE, SPEC_STORE_BYPASS_DISABLE, + SPEC_STORE_BYPASS_PRCTL, }; =20 extern char __indirect_thunk_start[]; extern char __indirect_thunk_end[]; =20 +/* cpu's MSR_IA32_SPEC_CTRL state for speculation store bypass mitigatio= n */ +DECLARE_PER_CPU(int, rds_msr_val); + /* * On VMEXIT we must ensure that no RSB predictions learned in the guest= * can be followed in the host, by overwriting the RSB completely. Both @@ -310,6 +316,17 @@ do { \ preempt_enable(); \ } while (0) =20 +/* + * speculation store bypass vulnerabilities mitigation functions. + * + * Should be called only from pre-emption off paths. + * + */ + +void enable_speculative_store_bypass_mitigation(void); +void disable_speculative_store_bypass_mitigation(void); + +DECLARE_STATIC_KEY_FALSE(specctrl_rds); #endif /* __ASSEMBLY__ */ =20 /* diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/th= read_info.h index a5d9521..f401321 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -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) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index f519214..10819f7 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include #include @@ -133,7 +134,7 @@ EXPORT_SYMBOL_GPL(x86_set_spec_ctrl); =20 u64 x86_get_spec_ctrl(void) { - return x86_spec_ctrl_base; + return x86_spec_ctrl_base | __this_cpu_read(rds_msr_val); } EXPORT_SYMBOL_GPL(x86_get_spec_ctrl); =20 @@ -148,10 +149,12 @@ EXPORT_SYMBOL_GPL(x86_set_guest_spec_ctrl); =20 void x86_restore_host_spec_ctrl(u64 guest_spec_ctrl) { - if (x86_get_spec_ctrl() =3D=3D guest_spec_ctrl) + u64 host_val =3D x86_get_spec_ctrl(); + + if (host_val =3D=3D guest_spec_ctrl) return; else - wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + wrmsrl(MSR_IA32_SPEC_CTRL, host_val); } EXPORT_SYMBOL_GPL(x86_restore_host_spec_ctrl); =20 @@ -376,16 +379,26 @@ static void __init spectre_v2_select_mitigation(voi= d) =20 static enum ssb_mitigation ssb_mode =3D SPEC_STORE_BYPASS_NONE; =20 +/* dynamic reduced data speculation in use */ +DEFINE_STATIC_KEY_FALSE(specctrl_rds); +EXPORT_SYMBOL(specctrl_rds); + +/* current MSR_IA32_SPEC_CTRL state for ssb mitigation */ +DEFINE_PER_CPU(int, rds_msr_val); +EXPORT_SYMBOL(rds_msr_val); + /* The kernel command line selection */ enum ssb_mitigation_cmd { SPEC_STORE_BYPASS_CMD_NONE, SPEC_STORE_BYPASS_CMD_AUTO, SPEC_STORE_BYPASS_CMD_ON, + SPEC_STORE_BYPASS_CMD_PRCTL, }; =20 static const char *ssb_strings[] =3D { [SPEC_STORE_BYPASS_NONE] =3D "Vulnerable", - [SPEC_STORE_BYPASS_DISABLE] =3D "Mitigation: Speculative Store Bypass d= isabled" + [SPEC_STORE_BYPASS_DISABLE] =3D "Mitigation: Speculative Store Bypass d= isabled", + [SPEC_STORE_BYPASS_PRCTL] =3D "Mitigation: Speculative Store Bypass dis= abled via prctl" }; =20 static const struct { @@ -395,8 +408,61 @@ static const struct { { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Byp= ass */ { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store= Bypass */ + { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* disable Speculative Store = Bypass via prctl */ }; =20 +void enable_speculative_store_bypass_mitigation(void) +{ + if (static_branch_unlikely(&specctrl_rds) && + /* need to toggle msr? */ + __this_cpu_read(rds_msr_val) !=3D SPEC_CTRL_RDS) { + wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_RDS | x86_spec_ctrl_= base); + __this_cpu_write(rds_msr_val, SPEC_CTRL_RDS); + } +} + +void disable_speculative_store_bypass_mitigation(void) +{ + if (static_branch_unlikely(&specctrl_rds) && + /* need to toggle msr? */ + __this_cpu_read(rds_msr_val) !=3D 0) { + wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + __this_cpu_write(rds_msr_val, 0); + } +} + +int arch_prctl_set_rds(struct task_struct *p, unsigned long val) +{ + if (val =3D=3D PR_RDS_MODE_ENABLE) { + set_tsk_thread_flag(p, TIF_RDS); + if (p =3D=3D current) + enable_speculative_store_bypass_mitigation(); + } else if (val =3D=3D PR_RDS_MODE_DISABLE) { + clear_tsk_thread_flag(p, TIF_RDS); + if (p =3D=3D current) + disable_speculative_store_bypass_mitigation(); + } else + return -EINVAL; + + return 0; +} + +int arch_prctl_get_rds(struct task_struct *p) +{ + if (test_tsk_thread_flag(p, TIF_RDS)) + return PR_RDS_MODE_ENABLE; + + return PR_RDS_MODE_DISABLE; +} + +void arch_prctl_copy_rds_mode(struct task_struct *parent, struct task_st= ruct *child) +{ + if (test_tsk_thread_flag(parent, TIF_RDS)) + set_tsk_thread_flag(child, TIF_RDS); + else + clear_tsk_thread_flag(child, TIF_RDS); +} + static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void) { char arg[20]; @@ -445,6 +511,7 @@ static void __init ssb_select_mitigation(void) cmd =3D=3D SPEC_STORE_BYPASS_CMD_AUTO)) return; =20 +again: switch (cmd) { case SPEC_STORE_BYPASS_CMD_AUTO: /* @@ -452,9 +519,17 @@ static void __init ssb_select_mitigation(void) */ if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD) break; + /* Choose prctl as the default mode */ + cmd =3D SPEC_STORE_BYPASS_CMD_PRCTL; + goto again; + break; case SPEC_STORE_BYPASS_CMD_ON: mode =3D SPEC_STORE_BYPASS_DISABLE; break; + case SPEC_STORE_BYPASS_CMD_PRCTL: + mode =3D SPEC_STORE_BYPASS_PRCTL; + static_branch_enable(&specctrl_rds); + break; case SPEC_STORE_BYPASS_CMD_NONE: break; } @@ -463,7 +538,8 @@ static void __init ssb_select_mitigation(void) pr_info("%s\n", ssb_strings[mode]); =20 if (mode !=3D SPEC_STORE_BYPASS_NONE) { - if (boot_cpu_has(X86_FEATURE_RDS)) + if (boot_cpu_has(X86_FEATURE_RDS) && + !static_branch_unlikely(&specctrl_rds)) x86_spec_ctrl_base |=3D SPEC_CTRL_RDS; =20 setup_force_cpu_cap(X86_FEATURE_DISABLE_SSB); diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index e0f0596..98ca193 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -772,7 +772,9 @@ static void init_intel(struct cpuinfo_x86 *c) =20 init_intel_misc_features(c); =20 - if (cpu_has(c, X86_FEATURE_DISABLE_SSB) && cpu_has(c, X86_FEATURE_RDS))= { + if (cpu_has(c, X86_FEATURE_DISABLE_SSB) && + cpu_has(c, X86_FEATURE_RDS) && + !static_branch_unlikely(&specctrl_rds)) { x86_set_spec_ctrl(SPEC_CTRL_RDS); } } diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 4b100fe..c8c5cc4 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -54,6 +54,7 @@ #include #include #include +#include #ifdef CONFIG_IA32_EMULATION /* Not included via unistd.h */ #include @@ -529,6 +530,17 @@ __switch_to(struct task_struct *prev_p, struct task_= struct *next_p) /* Load the Intel cache allocation PQR MSR. */ intel_rdt_sched_in(); =20 + if (test_tsk_thread_flag(next_p, TIF_RDS)) + /* + * Reduce data speculation for processes that + * have reduced data speculation option turned + * on via prctl. This provides protection against speculation + * store bypass attack for untrusted code within the process. + */ + enable_speculative_store_bypass_mitigation(); + else + disable_speculative_store_bypass_mitigation(); + return prev_p; } =20 diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index af5f8c2..7635b9a 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -207,4 +207,16 @@ struct prctl_mm_map { # define PR_SVE_VL_LEN_MASK 0xffff # define PR_SVE_VL_INHERIT (1 << 17) /* inherit across exec */ =20 +/* + * Reduced Data Speculation controls + * + * Set this on a task so it will run with reduced level of data speculat= ion + * to mitigate against speculative store bypass attack. + */ +#define PR_SET_RDS_MODE 53 /* set reduced data speculation mode of proc= ess */ +#define PR_RDS_MODE_DISABLE 0 /* Disable reduced data speculation = mode */ +#define PR_RDS_MODE_ENABLE 1 /* Enable reduced data speculation mo= de */ + +#define PR_GET_RDS_MODE 54 /* get reduced data speculation mode of proc= ess */ + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/fork.c b/kernel/fork.c index a5d21c4..8fe9c80 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1561,6 +1561,11 @@ static inline void rcu_copy_process(struct task_st= ruct *p) #endif /* #ifdef CONFIG_TASKS_RCU */ } =20 +void __weak arch_prctl_copy_rds_mode(struct task_struct *parent, struct = task_struct *child) +{ + return; +} + /* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -1633,6 +1638,9 @@ static __latent_entropy struct task_struct *copy_pr= ocess( if (!p) goto fork_out; =20 + /* Copy the reduced data speculation mode from parent to child */ + arch_prctl_copy_rds_mode(current, p); + /* * This _must_ happen before we call free_task(), i.e. before we jump * to any of the bad_fork_* labels. This is to avoid freeing diff --git a/kernel/sys.c b/kernel/sys.c index ad69218..8fbb698 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2242,6 +2242,16 @@ static int propagate_has_child_subreaper(struct ta= sk_struct *p, void *data) return 1; } =20 +int __weak arch_prctl_set_rds(struct task_struct *p, unsigned long arg) +{ + return 0; +} + +int __weak arch_prctl_get_rds(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 =3D SVE_GET_VL(); break; + case PR_SET_RDS_MODE: + error =3D arch_prctl_set_rds(me, arg2); + break; + case PR_GET_RDS_MODE: + error =3D arch_prctl_get_rds(me); + break; default: error =3D -EINVAL; break; --=20 2.9.4 --bZxtC0gbvIyDyDpSukNoaS2xHQir7ip4Z-- --wRMMWQwiNpKIb2LXUZHV9bBthgdFPRk0z--