Skip to contentSkip to conversation

Patch 1

PATCH Superseded Tim Chen 1 participant 1 message Apr 24, 2018

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tim Chen

author
#0 lore raw

PATCH 1/1 Patch 1

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.

---
 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(-)
Documentation/admin-guide/kernel-parameters.txt +2 -0
index a2d337b..ff9a203 100644
@@ -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.
 
 			Not specifying this option is equivalent to
 			spec_store_bypass_disable=auto.
arch/x86/include/asm/nospec-branch.h +17 -0
index 6f3d224..692723f 100644
@@ -3,10 +3,12 @@
 #ifndef _ASM_X86_NOSPEC_BRANCH_H_
 #define _ASM_X86_NOSPEC_BRANCH_H_
 
+#include <linux/jump_label.h>
 #include <asm/alternative.h>
 #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,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,
 };
 
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
+/* cpu's MSR_IA32_SPEC_CTRL state for speculation store bypass mitigation */
+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)
 
+/*
+ * 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__ */
 
 /*
arch/x86/kernel/cpu/bugs.c +48 -5

111 lines not on this page · raw message

arch/x86/kernel/cpu/intel.c +3 -1
index e0f0596..98ca193 100644
@@ -772,7 +772,9 @@ static void init_intel(struct cpuinfo_x86 *c)
 
 	init_intel_misc_features(c);
 
-	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);
 	}
 }
arch/x86/kernel/process_64.c +11 -0
index 4b100fe..9e1236d 100644
@@ -54,6 +54,7 @@
 #include <asm/vdso.h>
 #include <asm/intel_rdt_sched.h>
 #include <asm/unistd.h>
+#include <asm/nospec-branch.h>
 #ifdef CONFIG_IA32_EMULATION
 /* Not included via unistd.h */
 #include <asm/unistd_32_ia32.h>
@@ -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();
 
+	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;
 }
 
Signature · 1 line
2.9.4