Return-path: Received: from mga17.intel.com ([192.55.52.151]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fB7Go-0003JJ-F1 for speck@linutronix.de; Wed, 25 Apr 2018 01:22:46 +0200 Subject: [MODERATED] [PATCH 1/1] Patch 1 From: Tim Chen Message-ID: <2f9b0bb9-2fc0-888e-7090-78f4f8f50f93@linux.intel.com> Date: Tue, 24 Apr 2018 16:22:38 -0700 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mWEZ5pWPGl2teWwoIJ00Gd9UfMIXHlCJ6"; protected-headers="v1" To: speck@linutronix.de List-ID: This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156) --mWEZ5pWPGl2teWwoIJ00Gd9UfMIXHlCJ6 Content-Type: multipart/mixed; boundary="rNFFvqQ6XI5Sdp2fop9CPUGBA4lUEiKb7" --rNFFvqQ6XI5Sdp2fop9CPUGBA4lUEiKb7 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable x86/ssb: Enable option to mitigate SBB for seccomp processes SSB attacks are not expected to be feasible over ring transitions or kernel context switches. But it could be a valid attack inside processes that run untrusted user code (such as java script engines or other VMs), where the untrusted code attacks other code running in the same process. Many of these VMs already use sandboxes which rely on system call filtering using seccomp. This is true for the major web browsers, such as Chrome and Firefox. Automatically disable SSB for any process that uses seccomp. This allows protecting those VMs that already use a system call filter for a sandbox without changing their code. Other VMs that do not use syscall filter today can add a simple dummy filter to enable the mitigation. This patch is applied on top of Conrad's v4 patchset. Signed-off-by: Tim Chen --- Documentation/admin-guide/kernel-parameters.txt | 2 + arch/x86/include/asm/nospec-branch.h | 17 ++++++++ arch/x86/kernel/cpu/bugs.c | 53 +++++++++++++++++++= +++--- arch/x86/kernel/cpu/intel.c | 4 +- arch/x86/kernel/process_64.c | 11 +++++ 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentat= ion/admin-guide/kernel-parameters.txt index a2d337b..ff9a203 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -4053,6 +4053,8 @@ auto - kernel detects whether your CPU model contains a vulnerable implementation of Speculative Store Bypass and picks the most appropriate mitigation + seccomp - disable Speculative Store Bypass only for seccomp + processes. =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..692723f 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_SECCOMP, }; =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/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index f519214..0a1f319 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -133,7 +133,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 +148,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 +378,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_SECCOMP, }; =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_SECCOMP] =3D "Mitigation: Speculative Store Bypass d= isabled for seccomp processes" }; =20 static const struct { @@ -395,8 +407,29 @@ 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 */ + { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* disable Speculative St= ore Bypass for seccomp processes*/ }; =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); + } +} + static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void) { char arg[20]; @@ -445,6 +478,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 +486,17 @@ static void __init ssb_select_mitigation(void) */ if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD) break; + /* Choose SECCOMP as the default mode */ + cmd =3D SPEC_STORE_BYPASS_CMD_SECCOMP; + goto again; + break; case SPEC_STORE_BYPASS_CMD_ON: mode =3D SPEC_STORE_BYPASS_DISABLE; break; + case SPEC_STORE_BYPASS_CMD_SECCOMP: + mode =3D SPEC_STORE_BYPASS_SECCOMP; + static_branch_enable(&specctrl_rds); + break; case SPEC_STORE_BYPASS_CMD_NONE: break; } @@ -463,7 +505,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..9e1236d 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,16 @@ __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 (next_p->mm && test_tsk_thread_flag(next_p, TIF_SECCOMP)) + /* + * Reduce data speculation for sandboxed + * seccomp process for protection against speculation + * store bypass attack. + */ + enable_speculative_store_bypass_mitigation(); + else + disable_speculative_store_bypass_mitigation(); + return prev_p; } =20 --=20 2.9.4 --rNFFvqQ6XI5Sdp2fop9CPUGBA4lUEiKb7-- --mWEZ5pWPGl2teWwoIJ00Gd9UfMIXHlCJ6--