seccomp: Enable speculation flaw mitigations
authorKees Cook <keescook@chromium.org>
Tue, 1 May 2018 22:07:31 +0000 (15:07 -0700)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 3 May 2018 11:55:52 +0000 (13:55 +0200)
When speculation flaw mitigations are opt-in (via prctl), using seccomp
will automatically opt-in to these protections, since using seccomp
indicates at least some level of sandboxing is desired.

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
kernel/seccomp.c

index dc77548167ef0993487a61bcf6cbbabb2f6f2434..9f34533046aa9cc80305ed62a4f9c7f6d4ac93c5 100644 (file)
@@ -19,6 +19,8 @@
 #include <linux/compat.h>
 #include <linux/coredump.h>
 #include <linux/kmemleak.h>
+#include <linux/nospec.h>
+#include <linux/prctl.h>
 #include <linux/sched.h>
 #include <linux/sched/task_stack.h>
 #include <linux/seccomp.h>
@@ -227,6 +229,19 @@ static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
        return true;
 }
 
+/*
+ * If a given speculation mitigation is opt-in (prctl()-controlled),
+ * select it, by disabling speculation (enabling mitigation).
+ */
+static inline void spec_mitigate(struct task_struct *task,
+                                unsigned long which)
+{
+       int state = arch_prctl_spec_ctrl_get(task, which);
+
+       if (state > 0 && (state & PR_SPEC_PRCTL))
+               arch_prctl_spec_ctrl_set(task, which, PR_SPEC_DISABLE);
+}
+
 static inline void seccomp_assign_mode(struct task_struct *task,
                                       unsigned long seccomp_mode)
 {
@@ -238,6 +253,8 @@ static inline void seccomp_assign_mode(struct task_struct *task,
         * filter) is set.
         */
        smp_mb__before_atomic();
+       /* Assume seccomp processes want speculation flaw mitigation. */
+       spec_mitigate(task, PR_SPEC_STORE_BYPASS);
        set_tsk_thread_flag(task, TIF_SECCOMP);
 }