Return-path: Received: from mta.outflux.net ([2001:19d0:2:6:c0de:0:736d:7471] helo=smtp.outflux.net) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fDerd-0007tw-M0 for speck@linutronix.de; Wed, 02 May 2018 01:39:18 +0200 Received: from www.outflux.net (serenity.outflux.net [10.2.0.2]) by vinyl.outflux.net (8.15.2/8.15.2/Debian-3) with ESMTP id w41NdAlr024184 for ; Tue, 1 May 2018 16:39:10 -0700 Date: Tue, 1 May 2018 16:39:10 -0700 From: Kees Cook Subject: [MODERATED] [PATCH SSBv9 1/3] seccomp Message-ID: <20180501233910.GB32640@outflux.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline To: speck@linutronix.de List-ID: From: Kees Cook Subject: [PATCH SSBv9 seccomp 1/3] nospec: Allow getting/setting on non-current task This adjusts arch_prctl_get/set_spec_ctrl() to operate on tasks other than current. This is needed both for /proc/$pid/status queries and for seccomp (since thread-syncing can trigger seccomp in non-current threads). Signed-off-by: Kees Cook --- arch/x86/kernel/cpu/bugs.c | 27 ++++++++++++++++----------- include/linux/nospec.h | 7 +++++-- kernel/sys.c | 10 ++++++---- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index c28856e475c8..6da9aeae990f 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -526,31 +526,35 @@ static void ssb_select_mitigation() #undef pr_fmt -static int ssb_prctl_set(unsigned long ctrl) +static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl) { - bool rds = !!test_tsk_thread_flag(current, TIF_RDS); + bool rds = !!test_tsk_thread_flag(task, TIF_RDS); if (ssb_mode != SPEC_STORE_BYPASS_PRCTL) return -ENXIO; if (ctrl == PR_SPEC_ENABLE) - clear_tsk_thread_flag(current, TIF_RDS); + clear_tsk_thread_flag(task, TIF_RDS); else - set_tsk_thread_flag(current, TIF_RDS); + set_tsk_thread_flag(task, TIF_RDS); - if (rds != !!test_tsk_thread_flag(current, TIF_RDS)) + /* + * If being set on non-current task, delay setting the CPU + * mitigation until it is next scheduled. + */ + if (task == current && rds != !!test_tsk_thread_flag(task, TIF_RDS)) speculative_store_bypass_update(); return 0; } -static int ssb_prctl_get(void) +static int ssb_prctl_get(struct task_struct *task) { switch (ssb_mode) { case SPEC_STORE_BYPASS_DISABLE: return PR_SPEC_DISABLE; case SPEC_STORE_BYPASS_PRCTL: - if (test_tsk_thread_flag(current, TIF_RDS)) + if (test_tsk_thread_flag(task, TIF_RDS)) return PR_SPEC_PRCTL | PR_SPEC_DISABLE; return PR_SPEC_PRCTL | PR_SPEC_ENABLE; default: @@ -560,24 +564,25 @@ static int ssb_prctl_get(void) } } -int arch_prctl_set_spec_ctrl(unsigned long which, unsigned long ctrl) +int arch_prctl_set_spec_ctrl(struct task_struct *task, unsigned long which, + unsigned long ctrl) { if (ctrl != PR_SPEC_ENABLE && ctrl != PR_SPEC_DISABLE) return -ERANGE; switch (which) { case PR_SPEC_STORE_BYPASS: - return ssb_prctl_set(ctrl); + return ssb_prctl_set(task, ctrl); default: return -ENODEV; } } -int arch_prctl_get_spec_ctrl(unsigned long which) +int arch_prctl_get_spec_ctrl(struct task_struct *task, unsigned long which) { switch (which) { case PR_SPEC_STORE_BYPASS: - return ssb_prctl_get(); + return ssb_prctl_get(task); default: return -ENODEV; } diff --git a/include/linux/nospec.h b/include/linux/nospec.h index 1e63a0a90e96..ba6e346c0f49 100644 --- a/include/linux/nospec.h +++ b/include/linux/nospec.h @@ -7,6 +7,8 @@ #define _LINUX_NOSPEC_H #include +struct task_struct; + /** * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise * @index: array element index @@ -57,7 +59,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, }) /* Speculation control prctl */ -int arch_prctl_set_spec_ctrl(unsigned long which, unsigned long ctrl); -int arch_prctl_get_spec_ctrl(unsigned long which); +int arch_prctl_set_spec_ctrl(struct task_struct *task, unsigned long which, + unsigned long ctrl); +int arch_prctl_get_spec_ctrl(struct task_struct *task, unsigned long which); #endif /* _LINUX_NOSPEC_H */ diff --git a/kernel/sys.c b/kernel/sys.c index d7afe29319f1..38a843a5b58d 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2244,12 +2244,14 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data) return 1; } -int __weak arch_prctl_set_spec_ctrl(unsigned long which, unsigned long ctrl) +int __weak arch_prctl_set_spec_ctrl(struct task_struct *task, + unsigned long which, unsigned long ctrl) { return -EINVAL; } -int __weak arch_prctl_get_spec_ctrl(unsigned long which) +int __weak arch_prctl_get_spec_ctrl(struct task_struct *task, + unsigned long which) { return -EINVAL; } @@ -2465,12 +2467,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_SET_SPECULATION_CTRL: if (arg4 || arg5) return -EINVAL; - error = arch_prctl_set_spec_ctrl(arg2, arg3); + error = arch_prctl_set_spec_ctrl(me, arg2, arg3); break; case PR_GET_SPECULATION_CTRL: if (arg3 || arg4 || arg5) return -EINVAL; - error = arch_prctl_get_spec_ctrl(arg2); + error = arch_prctl_get_spec_ctrl(me, arg2); break; default: error = -EINVAL; -- 2.7.4 -- Kees Cook @outflux.net