Linux Patch 1/1
Subsystems (6)
PATCH v2 1/1 Linux Patch 1/1
From bf437945b231c9daaa3b927f50e57a868b7757d9 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Date: Mon, 23 Apr 2018 05:57:53 -0700
Subject: [PATCH v2] Linux Patch 1/1
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 wrappers, but it is not broadcast to all threads in the same process. It is generally safe to inherit because it doesn't allow any new attacks, but just prevents 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.
--- 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(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -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. Not specifying this option is equivalent to spec_store_bypass_disable=auto.
arch/x86/include/asm/nospec-branch.h +17 -0
@@ -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_PRCTL , };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/include/asm/thread_info.h +2 -0
@@ -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)
arch/x86/kernel/cpu/bugs.c +81 -5
150 lines not on this page · raw message
arch/x86/kernel/cpu/intel.c +3 -1
@@ -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 +12 -0
@@ -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,17 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)/* Load the Intel cache allocation PQR MSR. */ intel_rdt_sched_in (); +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; }
include/uapi/linux/prctl.h +12 -0
@@ -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 */ +/* +* Reduced Data Speculation controls +* +* Set this on a task so it will run with reduced level of data speculation +* to mitigate against speculative store bypass attack. +*/ +#define PR_SET_RDS_MODE 53/* set reduced data speculation mode of process */ +#define PR_RDS_MODE_DISABLE 0/* Disable reduced data speculation mode */ +#define PR_RDS_MODE_ENABLE 1/* Enable reduced data speculation mode */ + +#define PR_GET_RDS_MODE 54/* get reduced data speculation mode of process */ +#endif /* _LINUX_PRCTL_H */
kernel/fork.c +8 -0
@@ -1561,6 +1561,11 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +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_process(if (!p) goto fork_out; +/* 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
kernel/sys.c +16 -0
@@ -2242,6 +2242,16 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)return 1 ; } +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 =SVE_GET_VL ();break ; +case PR_SET_RDS_MODE : + error =arch_prctl_set_rds (me, arg2); +break ; +case PR_GET_RDS_MODE : + error =arch_prctl_get_rds (me); +break ;default : error = -EINVAL ;break ;
Signature · 1 line
2.9.4
Quoted 3 lines · speck for Tim Chen
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.
Let's not use the Intel name for this. It should be neutral since it will be used across architectures. PR_SET_SSB_MODE could be ok.
I personally favor a PR_SET_SPECULATION or similar first argument (seeking input from Linus here) that could be used to set flags for this and any future additional limitations that might need to be added. I can't see how this will be the first and only time. Another way around could be PR_SET_VULNERABILITY but "speculation" seems nicer to me.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 6 lines · speck for Jon Masters
Quoted 3 lines · speck for Tim Chen
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.
Quoted 9 lines
Let's not use the Intel name for this. It should be neutral since it will be used across architectures.
PR_SET_SSB_MODEcould be ok.I personally favor a
PR_SET_SPECULATIONor similar first argument (seeking input from Linus here) that could be used to set flags for this and any future additional limitations that might need to be added. I can't see how this will be the first and only time. Another way around could bePR_SET_VULNERABILITYbut "speculation" seems nicer to me.
How about PR_SET_SPECULATION_RESTRICTION? Linus, any suggestion?
Tim
One general question beyond the below: Should we define a new auxvec for processes to be told about speculation restrictions that they might be operating under, in case those are inherited, say across a fork?
Comments on the rest of the patch...
arch/x86/include/asm/nospec-branch.h extern void x86_restore_host_spec_ctrl(u64); · line 254
Quoted 2 lines · speck for Tim Chen
arch/x86/include/asm/nospec-branch.h +2
-0+/* cpu's MSR_IA32_SPEC_CTRL state for speculation store bypass mitigation */ +DECLARE_PER_CPU (int, rds_msr_val);
We do similar in RHEL today for IBRS, for similar performance reasons. Ultimately you /might/ want this to include additional bits for future mitigations that are toggled on top of the x86_spec_ctrl_base and thus call it something a bit more generic, but that's a nit for now.
Also, you're using an int here for the rds bit (which is fair enough) but the actual MSR is a u64. It might be worth a comment [ I've had to sanitize some of the previous IBRS assembly routines we've got in RHEL that were explicitly zeroing the high order bits during the MSR write to make sure we handle preserving those in case of future mitigations. ]
Quoted 1 line
+#define
TIF_RDS5 /* Reduced data speculation */
In the threadinfo for x86 call it whatever, but previous comment applies overall on naming.
Otherwise it looks ok. I prefer changing the name to something non-x86, and would like to know whether Linus would rather have a generic set of prctl knobs for speculation controls under one common name.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 16 lines · speck for Jon Masters
One general question beyond the below: Should we define a new auxvec for processes to be told about speculation restrictions that they might be operating under, in case those are inherited, say across a fork?
Comments on the rest of the patch...
arch/x86/include/asm/nospec-branch.h extern void x86_restore_host_spec_ctrl(u64); · line 254
Quoted 2 lines · speck for Tim Chen
arch/x86/include/asm/nospec-branch.h +2
-0+/* cpu's MSR_IA32_SPEC_CTRL state for speculation store bypass mitigation */ +DECLARE_PER_CPU (int, rds_msr_val);We do similar in RHEL today for IBRS, for similar performance reasons. Ultimately you /might/ want this to include additional bits for future mitigations that are toggled on top of the
x86_spec_ctrl_baseand thus call it something a bit more generic, but that's a nit for now.
Quoted 1 line
Also, you're using an int here for the rds bit (which is fair enough)
No. It's not fair enough.
Variables which store information which is read or written to registers should have an explicit data type which matches the register. Everything else with and without a comment is just sloppy hackery and utter crap.
It's fine to use u32 if you are sure that this is the lower part of the MSR, but then the variable name should reflect it, but int and a name which suggest that it's the actual MSR value is just not going to fly.
Thanks,
tglx
Quoted 22 lines · speck for Thomas Gleixner
Quoted 16 lines · speck for Jon Masters
One general question beyond the below: Should we define a new auxvec for processes to be told about speculation restrictions that they might be operating under, in case those are inherited, say across a fork?
Comments on the rest of the patch...
arch/x86/include/asm/nospec-branch.h extern void x86_restore_host_spec_ctrl(u64); · line 254
Quoted 2 lines · speck for Tim Chen
arch/x86/include/asm/nospec-branch.h +2
-0+/* cpu's MSR_IA32_SPEC_CTRL state for speculation store bypass mitigation */ +DECLARE_PER_CPU (int, rds_msr_val);We do similar in RHEL today for IBRS, for similar performance reasons. Ultimately you /might/ want this to include additional bits for future mitigations that are toggled on top of the
x86_spec_ctrl_baseand thus call it something a bit more generic, but that's a nit for now.Quoted 1 line
Also, you're using an int here for the rds bit (which is fair enough)
No. It's not fair enough.
Quoted 7 lines
Variables which store information which is read or written to registers should have an explicit data type which matches the register. Everything else with and without a comment is just sloppy hackery and utter crap.
It's fine to use u32 if you are sure that this is the lower part of the MSR, but then the variable name should reflect it, but int and a name which suggest that it's the actual MSR value is just not going to fly.
It is an oversight on my part. Will fix this in next version of patch.
Thanks.
Tim
Quoted 19 lines · speck for Tim Chen
arch/x86/kernel/process_64.c +4
-0@@ -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,17 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)/* Load the Intel cache allocation PQR MSR. */ intel_rdt_sched_in (); +if (test_tsk_thread_flag (next_p,TIF_RDS )) +/* +* Reduce data speculation for processes that
arch/x86/kernel/process_64.c __switch_to(struct task_struct *prev_p, struct task_struct *next_p) · line 545
Quoted 10 lines
arch/x86/kernel/process_64.c +8
-0+* 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; }
It's worth noting that if either thread sets RDS, effectively both my get it under the hood (depending upon implementation). This will have a subtle but minor performance impact.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 384
Quoted 3 lines · speck for Tim Chen
arch/x86/kernel/cpu/bugs.c +3
-0+/* dynamic reduced data speculation in use */ +DEFINE_STATIC_KEY_FALSE (specctrl_rds); +EXPORT_SYMBOL (specctrl_rds);
Why needs this to be exported?
arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 388
Quoted 3 lines
arch/x86/kernel/cpu/bugs.c +3
-0+/* current MSR_IA32_SPEC_CTRL state for ssb mitigation */ +DEFINE_PER_CPU (int, rds_msr_val); +EXPORT_SYMBOL (rds_msr_val);
Ditto
Quoted 17 lines
/* The kernel command line selection */
enumssb_mitigation_cmd{arch/x86/kernel/cpu/bugs.c +3
-1SPEC_STORE_BYPASS_CMD_NONE ,SPEC_STORE_BYPASS_CMD_AUTO ,SPEC_STORE_BYPASS_CMD_ON , + SPEC_STORE_BYPASS_CMD_PRCTL, };static const char *ssb_strings[] = { [SPEC_STORE_BYPASS_NONE ] ="Vulnerable" , - [SPEC_STORE_BYPASS_DISABLE ] ="Mitigation: Speculative Store Bypass disabled" + [SPEC_STORE_BYPASS_DISABLE ] ="Mitigation: Speculative Store Bypass disabled" , + [SPEC_STORE_BYPASS_PRCTL ] ="Mitigation: Speculative Store Bypass disabled via prctl" }; static const struct {
arch/x86/kernel/cpu/bugs.c static const struct { · line 414
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +2
-0@@ -395,8 +408,61 @@ static const struct { { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */ { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */ + { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* disable Speculative Store Bypass via prctl */ }; +void enable_speculative_store_bypass_mitigation (void )
The declaration of those functions says:
arch/x86/include/asm/nospec-branch.h do { \ · line 324
Quoted 6 lines
arch/x86/include/asm/nospec-branch.h +6
-0+/* +* speculation store bypass vulnerabilities mitigation functions. +* +* Should be called only from pre-emption off paths. +* +*/
Can you please explain how the PRCTL code fulfils the preempt off prerequisite?
It does not, which clearly shows that you never tested that code with the full set of debug stuff enabled, because otherwise the preempt check in __this_cpu_read/write would have triggered.
arch/x86/kernel/cpu/bugs.c static const struct { · line 418
Quoted 4 lines
arch/x86/kernel/cpu/bugs.c +4
-0+{ +if (static_branch_unlikely (&specctrl_rds) && +/* need to toggle msr? */ +__this_cpu_read (rds_msr_val) !=SPEC_CTRL_RDS ) {
Bah. This is horrible to read, really.
if (static_branch_unlikely(&specctrl_rds)) {
if (__this_cpu_read(rds_msr_val) != SPEC_CTRL_RDS) {
wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_RDS | x86_spec_ctrl_base);
__this_cpu_write(rds_msr_val, SPEC_CTRL_RDS);
}
}But see below.
arch/x86/kernel/cpu/bugs.c static const struct { · line 443
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +10
-0+int arch_prctl_set_rds (struct task_struct *p,unsigned long val) +{ +if (val ==PR_RDS_MODE_ENABLE ) { +set_tsk_thread_flag (p,TIF_RDS ); +if (p == current) +enable_speculative_store_bypass_mitigation (); + }else if (val ==PR_RDS_MODE_DISABLE ) { +clear_tsk_thread_flag (p,TIF_RDS ); +if (p == current) +disable_speculative_store_bypass_mitigation ();
In an earlier discussion it was said, that the prtcl - if at all - is a one way operation, i.e. set it end be done with it. Why is this different again?
And allowing to disable it once it is set is broken, really.
arch/x86/kernel/cpu/bugs.c static const struct { · line 445
Quoted 2 lines
arch/x86/kernel/cpu/bugs.c +2
-0+ }else +return -EINVAL ;
Missing parentheses. Dammit, why do I have say that over and over?
arch/x86/kernel/cpu/bugs.c static const struct { · line 458
Quoted 13 lines
arch/x86/kernel/cpu/bugs.c +13
-0+ +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_struct *child)
What the heck has this to do with prctl? Its about TIF_RDS where ever that flag has been set from.
Quoted 19 lines
arch/x86/kernel/cpu/bugs.c +8
-0+{ +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 == SPEC_STORE_BYPASS_CMD_AUTO))return ; +again:switch (cmd) {case SPEC_STORE_BYPASS_CMD_AUTO :/*
arch/x86/kernel/cpu/bugs.c static void __init ssb_select_mitigation(void) · line 525
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +4
-0@@ -452,9 +519,17 @@ static void __init ssb_select_mitigation(void)*/ if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD )break ; +/* Choose prctl as the default mode */ + cmd =SPEC_STORE_BYPASS_CMD_PRCTL ; + goto again; +break ;
WTF? This is just crap. The goto is not required at all if done right.
Quoted 6 lines
arch/x86/kernel/process_64.c +0
-0case SPEC_STORE_BYPASS_CMD_ON :@@ -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 ();
arch/x86/kernel/process_64.c __switch_to(struct task_struct *prev_p, struct task_struct *next_p) · line 542
Quoted 10 lines
arch/x86/kernel/process_64.c +10
-0+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 ();
So this does an unconditional function call on every context switch independent of whether the prctl mode is enabled or not. Crap, really.
What the heck is wrong with putting the TIF flag into _TIF_WORK_CTXSW and put this into __switch_to_extra() and do:
if ((tifp ^ tifn) & _TIF_RDS) speculative_bypass_update(tifn);
and that would be:
static inline void speculative_bypass_update(unsigned long tifn)
{
u64 msr = x86_spec_ctrl_base | ((tifn & _TIF_RDS) << TIF_TO_RDS_SHIFT);
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
}Which would get rid of the static key because the only place where the mitigation mode and therefore the PRCTL enablement needs to be checked is the prctl itself.
int arch_prctl_set_rds(struct task_struct *p, unsigned long val)
{
if (!spec_store_bypass_prctl)
return -ENOTSUPP;
if (val != PR_RDS_MODE_ENABLE)
return -EINVAL;
set_tsk_thread_flag(p, TIF_RDS);
if (p == current) {
preempt_disable();
speculative_bypass_update(task_thread_info(p)->flags);
preempt_enable();
}
return 0;
} See? No static key in the context switch path and no unconditional function calls, a single conditional which is evaluated already in switch_to.
If RDS is never set then speculative_bypass_update() is never ever invoked. If it needs to be toggled, then the MSR write is worse than the extra hoop through __switch_to_extra(). It also spares the per cpu state variable for this RDS bit.
That would be too simple and too performant and would magically make this work for 32bit as well, right? It even would make the seccomp magic simple because all it has to do is to set TIF_RDS on the thread once and not check for seccomp on every fricking context switch.
And most important it has absolutely ZERO impact if the prctl is not enabled.
That said, the above might be acceptable when Linus does not veto the whole thing in general.
Thanks,
tglx
Quoted 2 lines · speck for Thomas Gleixner
That said, the above might be acceptable when Linus does not veto the whole thing in general.
Also, in another discussion we had, if *not* the task itself would call prctl() but root or admin would be able to do set_tsk_thread_flag(p, TIF_RDS) once, I think we'll be better off wrt to breaking the task and tricking it into calling prctl() aspect.
I.e., do the marking from the outside and not have a prctl at all.
Regardless, the whole deal still feels like putting lipstick on a pig to me.
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Quoted 9 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Thomas Gleixner
That said, the above might be acceptable when Linus does not veto the whole thing in general.
Also, in another discussion we had, if *not* the task itself would call
prctl()but root or admin would be able to doset_tsk_thread_flag(p, TIF_RDS)once, I think we'll be better off wrt to breaking the task and tricking it into callingprctl()aspect.
Quoted 4 lines
I.e., do the marking from the outside and not have a prctl at all.
Regardless, the whole deal still feels like putting lipstick on a pig to me.
The problem is that the performance hit is up to 30% for worst case workloads. Typically it's a few percent, but clearly the silicon companies have a reasonable ask for us to preserve default leaving SSB enabled. The only way to do that is if we can easily disable it on a per-task basis for vulnerable code.
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling prctl() again to turn MD back on, the whole dance we're doing is useless.
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Quoted 7 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling
prctl()again to turn MD back on, the whole dance we're doing is useless.
That's not the worst problem. The worst thing is that it's inherited on fork and then the child can disable it again nilly-willy, which makes no sense whatsoever. If it's set only then the child CANNOT do that.
Thanks,
tglx
Quoted 6 lines · speck for Thomas Gleixner
Quoted 4 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
Quoted 7 lines
Quoted 3 lines
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling
prctl()again to turn MD back on, the whole dance we're doing is useless.That's not the worst problem. The worst thing is that it's inherited on fork and then the child can disable it again nilly-willy, which makes no sense whatsoever. If it's set only then the child CANNOT do that.
If the child can execute code it itself why shouldn't it be able to clear this bit? After all it can do everything else to itself too.
It would be unsafe if it could disable it on someone else, but of course it can't do that.
-Andi
Quoted 11 lines · speck for Andi Kleen
Quoted 10 lines · speck for Thomas Gleixner
Quoted 7 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling
prctl()again to turn MD back on, the whole dance we're doing is useless.
Quoted 6 lines
Quoted 3 lines
That's not the worst problem. The worst thing is that it's inherited on fork and then the child can disable it again nilly-willy, which makes no sense whatsoever. If it's set only then the child CANNOT do that.
If the child can execute code it itself why shouldn't it be able to clear this bit? After all it can do everything else to itself too.
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
Thanks,
tglx
Quoted 16 lines · speck for Thomas Gleixner
Quoted 15 lines · speck for Andi Kleen
Quoted 13 lines · speck for Thomas Gleixner
Quoted 7 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling
prctl()again to turn MD back on, the whole dance we're doing is useless.That's not the worst problem. The worst thing is that it's inherited on fork and then the child can disable it again nilly-willy, which makes no sense whatsoever. If it's set only then the child CANNOT do that.
Quoted 5 lines
Quoted 2 lines
If the child can execute code it itself why shouldn't it be able to clear this bit? After all it can do everything else to itself too.
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
And in fact you want it even inherit on exec because then you can start the JVM or whatever you want to protect with it disabled and never have to worry about it again.
Thanks,
tglx
Quoted 8 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Thomas Gleixner
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
And in fact you want it even inherit on exec because then you can start the JVM or whatever you want to protect with it disabled and never have to worry about it again.
I don't think that's the attack people are worried about.
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
It's almost purely a "I generated code assuming the architecture would actualy execute the code I wrote" issue.
That means that it's not like you're really executing "untrusted" code in general. Your jitted code isn't going to just run random sysctl's without any checking or anything like that. You trust your JVM to take care of *those* kinds of security issues.
So "user can turn it on and off as they please" is not really an issue. In fact, it could easily be seen as a feature. Making it expensive or hard to turn off the mitigation means that you can't necessarily just turn it on temporarily for the code you really care about.
Realistically, a JIT would probably prefer to only turn off store buffer bypass when jumping into JIT'ed code, and then turning it on when coming back. Exactly because it's really not about any "security", it's abnout "I translated untrusted code, and I want _exactly_ the semantics that I did the translations with - everything else I can and do check manually".
Linus
Quoted 29 lines · speck for Linus Torvalds
Quoted 8 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Thomas Gleixner
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
And in fact you want it even inherit on exec because then you can start the JVM or whatever you want to protect with it disabled and never have to worry about it again.
I don't think that's the attack people are worried about.
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
It's almost purely a "I generated code assuming the architecture would actualy execute the code I wrote" issue.
That means that it's not like you're really executing "untrusted" code in general. Your jitted code isn't going to just run random sysctl's without any checking or anything like that. You trust your JVM to take care of *those* kinds of security issues.
So "user can turn it on and off as they please" is not really an issue. In fact, it could easily be seen as a feature. Making it expensive or hard to turn off the mitigation means that you can't necessarily just turn it on temporarily for the code you really care about.
Quoted 5 lines
Realistically, a JIT would probably prefer to only turn off store buffer bypass when jumping into JIT'ed code, and then turning it on when coming back. Exactly because it's really not about any "security", it's abnout "I translated untrusted code, and I want _exactly_ the semantics that I did the translations with - everything else I can and do check manually".
Fair enough. I didn't look at it that way. Thanks for the clarification.
Thanks,
tglx
Quoted 7 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Linus Torvalds
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
Fair enough. I didn't look at it that way. Thanks for the clarification.
NOTE! My reading of the worry people have about the store buffer bypass attack may simply be wrong. So somebody should verify this part.
Because if the store buffer bypass is actually seen as a security issue on its own, then your argument that people shouldn't be able to turn the mitigation off is obviously very valid.
But I *think* that the store buffer bypass is a non-issue if you actually control the whole binary, because then you could just have written your code to not do the store that hid the old value in the first place.
So I think this one is fundamentally different from say Spectre/Meltdown, that actually worked across security domains.
That also argues that perhaps we could possible leave the store buffer bypass mitigation off by default, if we could just detect the "people are JIT'ing code" situation. Which we can't, afaik, but..
Anybody?
Linus
Quoted 5 lines
That also argues that perhaps we could possible leave the store buffer bypass mitigation off by default, if we could just detect the "people are JIT'ing code" situation. Which we can't, afaik, but..
Anybody?
That's what Tim's patches are doing essentially. Keep mitigation off by default but allows processes to enable using prctl.
Originally we tried to use seccomp for detect JIT, but it turned out that there can be other mitigations for seccomp too
(e.g. browsers doing full site process separation so they don't care about leaks anymore). So separate prctl makes more sense.
-andi
Quoted 6 lines · speck for Linus Torvalds
Quoted 1 line · speck for Thomas Gleixner
On Fri, 27 Apr 2018, speck for Linus Torvalds wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
Fair enough. I didn't look at it that way. Thanks for the clarification.
NOTE! My reading of the worry people have about the store buffer bypass attack may simply be wrong. So somebody should verify this part.
The concern we have is about managed code, yeah. I didn't want to fight too hard on the unset part if we get the set bit :) but there shouldn't be a security issue to having the prctl go both ways (and that would be ideal IMO). It's really about a sandboxed JavaScript or Java process (or somesuch similar like thing) in which that JIT is not expecting the potential for code to abuse this bypass to dump memory from the process containing it. e.g. in JavaScript you can make a memory dumping widget in about three lines of code that runs speculatively for that process.
Quoted 3 lines
Because if the store buffer bypass is actually seen as a security issue on its own, then your argument that people shouldn't be able to turn the mitigation off is obviously very valid.
It /might/ be an issue for the kernel stack. This is the fear many of us have. However, we do an lfence on entry to the kernel, so it would only be a problem there if we can find vulnerable syscall functions, and we haven't yet found any that contain suitable gadgets. That seems contrived, but it's the reason for the original kernel entry/exit frob.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 3 lines · speck for Jon Masters
It /might/ be an issue for the kernel stack. This is the fear many of us have.
Honestly, the kernel stack worry would be the *reverse* of the store buffer bypass.
The actual cache contents should be safe (meltdown), but a a user space load that hits in the store buffer of a kernel store might bypass the TLB checks _entirely_. So that would be a store buffer *HIT*, not a "speculatively do a load that later turns out _should_ have hit the store buffer".
That would be an architectural flaw, but one that should be fairly easy to mitigate ("just add a barrier before the return to user space, and curse the incompetence of hardware designers").
Quoted 1 line
However, we do an lfence on entry to the kernel
I wouldn't worry about kernel _entry_, since any data the kernel reads is by definition not a security issue. We have lots of cases where the kernel can get stale data because of speculation. That's not in the least new.
Examples of "kernel gets stale user space data" are things like takin ga fault on an instruction that has in the meantime been rewritten (due to incomplete I$ coherency). The kernel sees a fault from something that no longer even exists.
So it's kernel exit I'd worry about, where a kernel store that is (still) in the store buffers can be incorrectly seen by user space because the store buffer load forwarding doesn't do any permission checking at all.
I'm assuming nobody has _that_ bug. But it might be worth asking around.
Linus
PS. The real kernel worry wrt the store buffer bypass is obviously ebpf, not kernel entry/exit.
Quoted 9 lines · speck for Linus Torvalds
Quoted 3 lines · speck for Jon Masters
It /might/ be an issue for the kernel stack. This is the fear many of us have.
Honestly, the kernel stack worry would be the *reverse* of the store buffer bypass.
Kinda, but also on entry we save untrusted user-provided register values on the stack. If those are updated and then used (or also happens with uninitialized stack values), then it's possible that the older untrusted content is used. It's a very unlikely attack since we also do an lfence as part of the indirect retpolined function calls on entry, but once we dispatch to the syscall handler functions themselves, this very theoretical case is vaguely possible.
<snip>
Quoted 5 lines
So it's kernel exit I'd worry about, where a kernel store that is (still) in the store buffers can be incorrectly seen by user space because the store buffer load forwarding doesn't do any permission checking at all.
I'm assuming nobody has _that_ bug. But it might be worth asking around.
They do and this is still a problem. Not just on x86. POWER are adding a new "stf-flush" instruction to every entry/exit path for this reason. They're ready with those patches - obviously they're not on this list, but they know they can ping you with them.
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 10 lines · speck for Linus Torvalds
So it's kernel exit I'd worry about, where a kernel store that is (still) in the store buffers can be incorrectly seen by user space because the store buffer load forwarding doesn't do any permission checking at all.
I'm assuming nobody has _that_ bug. But it might be worth asking around.
Linus
PS. The real kernel worry wrt the store buffer bypass is obviously ebpf, not kernel entry/exit.
Well but in principle ebpf makes just the whole thing easier, as the attacker can freely construct fine-grained load+store gadget instead of having to find it in the code.
We've identified numberous spectre v1 gadgets already present in the kernel, I don't really see how this is different in *principle* ("only" you need load+store, instead of v1's load+load).
So JITed code is more of a concern because it allows for creating the gadget instead to having to actively find it in the existing code, but I fail to see how this is not an issue of userspace being able to read all of kernel's memory (either by using explicitly ebpf-instrumented gadget, or simply finding one in the existing code).
So I still believe we need to protect kernel as well, and prctl()-based aproach doesn't work for that.
So ... how does current patchset even deal with the ebpf scenario?
I'd be glad to be enlightened and proven wrong.
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 4 lines · speck for Jiri Kosina
We've identified numberous spectre v1 gadgets already present in the kernel, I don't really see how this is different in *principle* ("only" you need load+store, instead of v1's load+load).
No, afaik you need slow-address-store + load + load, where the first load bypasses the store buffer speculatively, and the second independent load then exposes the speculative wrong load data by perturing the cache.
Or am I missing some clever attack?
Honestly, the store buffer bypass is really really hard to use to attack the kernel exactly because you need that third memory access to perturb the side channel, and the first access has to be a store with the right setup so that the second access loads the wrong data, and the wrong data is the interesting one (not the right one from the store).
Linus
Quoted 5 lines · speck for Linus Torvalds
No, afaik you need slow-address-store + load + load, where the first load bypasses the store buffer speculatively, and the second independent load then exposes the speculative wrong load data by perturing the cache.
Or am I missing some clever attack?
Yeah, that's more or less the way I understand it -- when slow store is followed by load from the same address, the speculative execution observes the stale (previous) value if the store hasn't been retired yet.
If that depending speculatively executed load performs any action that creates a sidechannel (such as using that value as array index, thus causing observable/measurable cacheline effects), the attack is complete.
Basically the same we had with load+load for v1. And there are plenty of such constructs in the kernel. Why would we think that it's not the case with store+load?
It's just that noone really bothered too much, as it's all so much simpler with BPF.
Quoted 5 lines
Honestly, the store buffer bypass is really really hard to use to attack the kernel exactly because you need that third memory access to perturb the side channel, and the first access has to be a store with the right setup so that the second access loads the wrong data, and the wrong data is the interesting one (not the right one from the store).
So even if there really are no such gadgets / constructs anywhere in the kernel, they can be of course created through BPF. And therefore one of our primary concerns should be disabling MD around BPF program execution, shouldn't it?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 9 lines · speck for Jiri Kosina
Quoted 6 lines
Honestly, the store buffer bypass is really really hard to use to attack the kernel exactly because you need that third memory access to perturb the side channel, and the first access has to be a store with the right setup so that the second access loads the wrong data, and the wrong data is the interesting one (not the right one from the store).
So even if there really are no such gadgets / constructs anywhere in the kernel, they can be of course created through BPF.
No, not really. There is no "of course".
It's not clear that such a gadget can be constructed at all with bpf. We should *not* say "of course you can construct that" without having any kind of real data on that.
The thing is, to construct that, you really have to construct:
- a store to the same address that you do a subsequent load from
This part is probably easyish. BUT:
- you have to use a different base pointer for it, which suddenly means that it's not the obvious case of a stack spill "let's store to the stack and the load the value back", because that generally uses the same base pointer unless your code generation is complete garbage.
So now you have one big wrinkle already. But the *really* big wrinkle is that you also have to have interesting data that the store that was bypassed was actually hiding.
But let's assume you have that store-load pair, and let's assume you actually can load some interesting data that the first store overwrote. Now you need to do the second indirect load to act as the side channel for the cache.
So it's not trivial to generate in the first place, but then you need to *run* this gadget too. Which shouldn't be possible in the first place, since you now need bascially the Spectre v1 to execute that arbitrary code.
And the thing is, if you need spectre v1 to execute it, why go through all that complexity? Just do the original spectre thing to begin with.
No. To be practically exploitable, you need to be able to just generate the code and run it directly. But again, I'm assuming just the *regular* spectre is easier to just exploit if you can do that.
So what we should look at is "can you make ebpf easily generate that "store followed by load from same address with a different base register, and can you run it as non-root".
And I haven't seen anybody actually try to look into that. Have the ebpf people been involved on this issue at all?
Linus
Quoted 6 lines · speck for Linus Torvalds
This part is probably easyish. BUT:
- you have to use a different base pointer for it, which suddenly means that it's not the obvious case of a stack spill "let's store to the stack and the load the value back", because that generally uses the same base pointer unless your code generation is complete garbage.
Fair enough; if gcc is ever generating such code, it should probably be better to fix gcc directly anyway.
Quoted 3 lines
So what we should look at is "can you make ebpf easily generate that "store followed by load from same address with a different base register, and can you run it as non-root".
So that's, according to my understanding, the PoC that google folks have succesfully created (the only modification AFAIK they did to eBPF is allowing more easier cacheline flush from within eBPF), basically let otherwise sandboxed process to
- store unsingned long into memory
- store pointer into the same memory location
- load from that location and use it so that the side channel is there (array index), as due to MD the speculative execution observes the unsinged long, and not the pointer
And reportedly they succeeded (with the easiser cacheline flush modification).
Quoted 2 lines
And I haven't seen anybody actually try to look into that. Have the ebpf people been involved on this issue at all?
I am not sure if any of them is on this list. Jon, would it be possible to drag Daniel Borkmann here?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 11 lines · speck for Jiri Kosina
So that's, according to my understanding, the PoC that google folks have succesfully created (the only modification AFAIK they did to eBPF is allowing more easier cacheline flush from within eBPF), basically let otherwise sandboxed process to
- store unsingned long into memory
- store pointer into the same memory location
- load from that location and use it so that the side channel is there (array index), as due to MD the speculative execution observes the unsinged long, and not the pointer
Quoted 8 lines
And reportedly they succeeded (with the easiser cacheline flush modification).
Quoted 2 lines
And I haven't seen anybody actually try to look into that. Have the ebpf people been involved on this issue at all?
I am not sure if any of them is on this list. Jon, would it be possible to drag Daniel Borkmann here?
Ah, only now I noticed that Dave already did send a patchset to mitigate this in the interpreter by disabling MD while the interpreter is running; so probably no need to any more.
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Dave Hansen
maintainerPATCH v2 1/1 Linux Patch 1/1
In reply to Linus TorvaldsQuoted 3 lines · speck for Linus Torvalds
So now you have one big wrinkle already. But the *really* big wrinkle is that you also have to have interesting data that the store that was bypassed was actually hiding.
When I first heard about this being another "bounds bypass", I wasn't all that worried, certainly not for the kernel. We don't frequently shrink arrays or put secret data in array slots that were recently accessible as non-secrets.
But, there is a _partial_ PoC that demonstrates some of this stuff crossing the kernel boundary with eBPF. eBPF does a good job of tracking the architectural value of registers to make sure they, for instance, contain pointers to things that are supposed to be dereferenced. But, if you can cause some type confusion (with speculative store bypass) you can get speculative dereferences of eBPF registers that don't contain pointers at all.
I think the bar is still way higher than for Spectre V1, though.
Quoted 26 lines · speck for Linus Torvalds
Quoted 8 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Thomas Gleixner
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
And in fact you want it even inherit on exec because then you can start the JVM or whatever you want to protect with it disabled and never have to worry about it again.
I don't think that's the attack people are worried about.
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
It's almost purely a "I generated code assuming the architecture would actualy execute the code I wrote" issue.
That means that it's not like you're really executing "untrusted" code in general. Your jitted code isn't going to just run random sysctl's without any checking or anything like that. You trust your JVM to take care of *those* kinds of security issues.
Quoted 4 lines
So "user can turn it on and off as they please" is not really an issue. In fact, it could easily be seen as a feature. Making it expensive or hard to turn off the mitigation means that you can't necessarily just turn it on temporarily for the code you really care about.
I'll keep the option in prctl to turn the mitigation off then till we find a strong reason to do otherwise.
Tim
Quoted 27 lines · speck for Tim Chen
Quoted 26 lines · speck for Linus Torvalds
Quoted 8 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Thomas Gleixner
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
And in fact you want it even inherit on exec because then you can start the JVM or whatever you want to protect with it disabled and never have to worry about it again.
I don't think that's the attack people are worried about.
Basically, for the store buffer bypass, the *only* worry is JIT'ed code. I don't think people expect it to leak from supervisor to user mode, for example, so it's not primarily a protection domain issue.
It's almost purely a "I generated code assuming the architecture would actualy execute the code I wrote" issue.
That means that it's not like you're really executing "untrusted" code in general. Your jitted code isn't going to just run random sysctl's without any checking or anything like that. You trust your JVM to take care of *those* kinds of security issues.
Quoted 7 lines
Quoted 4 lines
So "user can turn it on and off as they please" is not really an issue. In fact, it could easily be seen as a feature. Making it expensive or hard to turn off the mitigation means that you can't necessarily just turn it on temporarily for the code you really care about.
I'll keep the option in prctl to turn the mitigation off then till we find a strong reason to do otherwise.
Konrad and I are sitting here chatting in my office. We debated the enumeration for this. Do we think simply calling and getting an EINVAL on older kernels is ok, or do we need a specific enumeration for the ability to control SSB from software? An enumeration would be nice. You might want to look to how the kernel handles MPX today to indicate whether the prctl implemented, and also whether runtime supported.
(he's not got his encrypted email in front of him but he says he'll work in your update to his patch series if you send it out tonight)
Jon.
Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
Quoted 16 lines · speck for Jon Masters
Quoted 7 lines · speck for Tim Chen
Quoted 3 lines · speck for Linus Torvalds
I'll keep the option in prctl to turn the mitigation off then till we find a strong reason to do otherwise.
Konrad and I are sitting here chatting in my office. We debated the enumeration for this. Do we think simply calling and getting an EINVAL on older kernels is ok, or do we need a specific enumeration for the ability to control SSB from software? An enumeration would be nice. You might want to look to how the kernel handles MPX today to indicate whether the prctl implemented, and also whether runtime supported.
Quoted 3 lines
(he's not got his encrypted email in front of him but he says he'll work in your update to his patch series if you send it out tonight)
I'll do my best to send an update tonight.
Tim
Quoted 17 lines · speck for Tim Chen
Quoted 16 lines · speck for Jon Masters
Quoted 7 lines · speck for Tim Chen
Quoted 3 lines · speck for Linus Torvalds
I'll keep the option in prctl to turn the mitigation off then till we find a strong reason to do otherwise.
Konrad and I are sitting here chatting in my office. We debated the enumeration for this. Do we think simply calling and getting an EINVAL on older kernels is ok, or do we need a specific enumeration for the ability to control SSB from software? An enumeration would be nice. You might want to look to how the kernel handles MPX today to indicate whether the prctl implemented, and also whether runtime supported.
Quoted 5 lines
Quoted 3 lines
(he's not got his encrypted email in front of him but he says he'll work in your update to his patch series if you send it out tonight)
I'll do my best to send an update tonight.
Please take your time. I've already picked up Konrads series for integration and will post tomorrow the git URL where it can be retrieved. That prctl stuff just goes incremental on top.
Thanks,
tglx
Quoted 6 lines · speck for Jon Masters
Konrad and I are sitting here chatting in my office. We debated the enumeration for this. Do we think simply calling and getting an EINVAL on older kernels is ok, or do we need a specific enumeration for the ability to control SSB from software? An enumeration would be nice. You might want to look to how the kernel handles MPX today to indicate whether the prctl implemented, and also whether runtime supported.
So if you looked at what I wrote earlier:
int arch_prctl_set_rds(struct task_struct *p, unsigned long val)
{
if (!spec_store_bypass_prctl)
return -ENOTSUPP;That's exactly the point. If the (older or not updated) kernel does not support it then you get -EINVAL because thats the default for not implemented prctls.
So if spec_store_bypass_prctl is not enabled you can return some other error code and we surely can make that error code depend on the mitigation state and return -ENOTVULNERABLE or whatever is suitable.
And no, you don't need the MPX way because that mitigation stuff is unconditional and cannot be compiled out.
Thanks,
tglx
Quoted 8 lines · speck for Thomas Gleixner
Quoted 6 lines · speck for Jon Masters
Konrad and I are sitting here chatting in my office. We debated the enumeration for this. Do we think simply calling and getting an EINVAL on older kernels is ok, or do we need a specific enumeration for the ability to control SSB from software? An enumeration would be nice. You might want to look to how the kernel handles MPX today to indicate whether the prctl implemented, and also whether runtime supported.
Quoted 7 lines
So if you looked at what I wrote earlier:
int
arch_prctl_set_rds(struct task_struct *p, unsigned long val){ if (!spec_store_bypass_prctl) return -ENOTSUPP;
The ENOTSUPP is in include/linux/errno.h and
not in userspace api, can we return that?
There is a comment in the include/linux/errno.h that says:
/*
* These should never be seen by user programs....
and then
/* Defined for the NFSv3 protocol */
Quoted 7 lines
That's exactly the point. If the (older or not updated) kernel does not support it then you get
-EINVALbecause thats the default for not implemented prctls.So if
spec_store_bypass_prctlis not enabled you can return some other error code and we surely can make that error code depend on the mitigation state and return-ENOTVULNERABLEor whatever is suitable.
You mean add a new ENOTVULNERABLE error number? I can't seem to find errno. If I need to add this, which file should it go to?
I'll keep the EINVAL temporarily in my patch till I can figure out the ENOTSUPP and ENOTVULNERABLE usage.
Tim
Quoted 8 lines
And no, you don't need the MPX way because that mitigation stuff is unconditional and cannot be compiled out.
Thanks,
tglx
Quoted 10 lines · speck for Tim Chen
Quoted 3 lines · speck for Thomas Gleixner
So if
spec_store_bypass_prctlis not enabled you can return some other error code and we surely can make that error code depend on the mitigation state and return-ENOTVULNERABLEor whatever is suitable.You mean add a new ENOTVULNERABLE error number? I can't seem to find errno. If I need to add this, which file should it go to?
I'll keep the EINVAL temporarily in my patch till I can figure out the ENOTSUPP and ENOTVULNERABLE usage.
I made that up and added: or whatever is suitable
In other words, find a suitable error code to express what you want to express.
Thanks,
tglx
Quoted 9 lines · speck for Thomas Gleixner
Quoted 8 lines · speck for Tim Chen
Quoted 3 lines · speck for Thomas Gleixner
So if
spec_store_bypass_prctlis not enabled you can return some other error code and we surely can make that error code depend on the mitigation state and return-ENOTVULNERABLEor whatever is suitable.You mean add a new ENOTVULNERABLE error number? I can't seem to find errno. If I need to add this, which file should it go to?
Quoted 7 lines
Quoted 2 lines
I'll keep the EINVAL temporarily in my patch till I can figure out the ENOTSUPP and ENOTVULNERABLE usage.
I made that up and added: or whatever is suitable
In other words, find a suitable error code to express what you want to express.
How about EOPNOTSUPP if we are are trying to set RDS and platform does not support it. And EALREADY to indicate that the platform is not vulnerable?
They are both defined in include/uapi/asm-generic/errno.h.
For older kernels, they will always return EINVAL.
Tim
Quoted 5 lines
Thanks,
tglx
Quoted 8 lines · speck for Tim Chen
Quoted 4 lines · speck for Linus Torvalds
So "user can turn it on and off as they please" is not really an issue. In fact, it could easily be seen as a feature. Making it expensive or hard to turn off the mitigation means that you can't necessarily just turn it on temporarily for the code you really care about.
I'll keep the option in prctl to turn the mitigation off then till we find a strong reason to do otherwise.
You better add a strong reason to the changelog WHY it is safe to have it both ways.
Thanks,
tglx
Quoted 2 lines
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
It's a TIF flag, not a mm flag.
It can only disable it for itself, not for other threads.
-Andi
Quoted 4 lines · speck for Andi Kleen
Quoted 2 lines
Sure. You set it on that sandbox thing and then the thread which is spawned of from there can disable it. Brilliant idea.
It's a TIF flag, not a mm flag.
I'm well aware of that.
Quoted 1 line
It can only disable it for itself, not for other threads.
I know.
Still the point is that if you spawn a thread and you know that the thread is going to run untrusted code, you want to make sure that MD is disabled for that thread, right? So if that thread can reenable it, then you lost control.
I might have completely misunderstood the whole thing, but with your terse explanations I'm not going to understand it better.
Thanks,
tglx
Quoted 4 lines
Still the point is that if you spawn a thread and you know that the thread is going to run untrusted code, you want to make sure that MD is disabled for that thread, right? So if that thread can reenable it, then you lost control.
There are multiple levels
1) JITed sandbox. cannot just do system calls, but can do PRIME+PROBE and may have call interfaces to other code
2) Native code process. No protection inside the process if you're at this level. Can also normally do system calls.
3) Kernel.
3) Between processes.
We try to protect against case (1) doing attacks here.
If the untrusted code can do random system calls you already lost control in a much worse way. So you already need to have system call protection in some way (using a JIT not allowing them or seccomp). Or rather if the process can subvert its environment somehow to do the prctl it can already execute arbitrary code, which is much worse the SSB.
Or to put it differently, the point of the prctl is to not allow JITed code to read data it shouldn't read from its JITed sandbox. If it already has escaped its sandbox then it can already read everything it wants in its address space, and do much worse thing.
On the other hand if you have the ability to switch it everywhere you want that gives you more flexibility to do the protection you need for your JIT.
-Andi
Quoted 22 lines · speck for Andi Kleen
Quoted 4 lines
Still the point is that if you spawn a thread and you know that the thread is going to run untrusted code, you want to make sure that MD is disabled for that thread, right? So if that thread can reenable it, then you lost control.
There are multiple levels
1) JITed sandbox. cannot just do system calls, but can do PRIME+PROBE and may have call interfaces to other code
2) Native code process. No protection inside the process if you're at this level. Can also normally do system calls.
3) Kernel.
3) Between processes.We try to protect against case (1) doing attacks here.
If the untrusted code can do random system calls you already lost control in a much worse way. So you already need to have system call protection in some way (using a JIT not allowing them or seccomp). Or rather if the process can subvert its environment somehow to do the prctl it can already execute arbitrary code, which is much worse the SSB.
Quoted 8 lines
Or to put it differently, the point of the prctl is to not allow JITed code to read data it shouldn't read from its JITed sandbox. If it already has escaped its sandbox then it can already read everything it wants in its address space, and do much worse thing.
On the other hand if you have the ability to switch it everywhere you want that gives you more flexibility to do the protection you need for your JIT.
That's a reasonable explanation. May I ask why this was not part of the changelog?
Thanks,
tglx
Quoted 7 lines · speck for Borislav Petkov
Quoted 2 lines · speck for Jon Masters
My opinion is this needs to be a prctl that a vulnerable application can set. It can be a one-way thing that's set/inherited by children, etc.
If the task does prctl, it *must* at least be a one-way thing because, as jikos said, if a malicious attacker can trick it into calling
prctl()again to turn MD back on, the whole dance we're doing is useless.
If you can trick someone to call prctl you can also trick them to execute other code. No need to use SSB and other complicated side channel methods then. The game is already over.
-Andi
Quoted 7 lines · speck for Thomas Gleixner
Review
Quoted 2 lines · speck for Tim Chen
(quoted diff) +2
-0+ +void arch_prctl_copy_rds_mode(struct task_struct *parent, struct task_struct *child)What the heck has this to do with prctl? Its about
TIF_RDSwhere ever that flag has been set from.
Quoted 6 lines
arch/x86/kernel/cpu/bugs.c static const struct { · line 464
Quoted 6 lines
arch/x86/kernel/cpu/bugs.c +6
-0+{ +if (test_tsk_thread_flag (parent,TIF_RDS )) +set_tsk_thread_flag (child,TIF_RDS ); +else +clear_tsk_thread_flag (child,TIF_RDS ); +}
And just for the record. Why do you need that function at all? What's wrong with using arch_dup_task_struct() for this? Right, again it would be too obvious to do that.
Oh well
Quoted 6 lines · speck for Thomas Gleixner
arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 384
Quoted 3 lines · speck for Tim Chen
arch/x86/kernel/cpu/bugs.c +3
-0+/* dynamic reduced data speculation in use */ +DEFINE_STATIC_KEY_FALSE (specctrl_rds); +EXPORT_SYMBOL (specctrl_rds);Why needs this to be exported?
Artifact from earlier version that uses this in a module that's no longer in use now. Will remove.
Quoted 6 lines
arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 388
Quoted 4 lines
arch/x86/kernel/cpu/bugs.c +3
-0+/* current MSR_IA32_SPEC_CTRL state for ssb mitigation */ +DEFINE_PER_CPU (int, rds_msr_val); +EXPORT_SYMBOL (rds_msr_val);Ditto
Will remove.
Quoted 35 lines
Quoted 18 lines
/* The kernel command line selection */
enumssb_mitigation_cmd{arch/x86/kernel/cpu/bugs.c +3
-1SPEC_STORE_BYPASS_CMD_NONE ,SPEC_STORE_BYPASS_CMD_AUTO ,SPEC_STORE_BYPASS_CMD_ON , + SPEC_STORE_BYPASS_CMD_PRCTL, };static const char *ssb_strings[] = { [SPEC_STORE_BYPASS_NONE ] ="Vulnerable" , - [SPEC_STORE_BYPASS_DISABLE ] ="Mitigation: Speculative Store Bypass disabled" + [SPEC_STORE_BYPASS_DISABLE ] ="Mitigation: Speculative Store Bypass disabled" , + [SPEC_STORE_BYPASS_PRCTL ] ="Mitigation: Speculative Store Bypass disabled via prctl" }; static const struct {arch/x86/kernel/cpu/bugs.c static const struct { · line 414
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +2
-0@@ -395,8 +408,61 @@ static const struct { { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */ { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */ { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */ + { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* disable Speculative Store Bypass via prctl */ }; +void enable_speculative_store_bypass_mitigation (void )The declaration of those functions says:
arch/x86/include/asm/nospec-branch.h do { \ · line 324
Quoted 6 lines
arch/x86/include/asm/nospec-branch.h +6
-0+/* +* speculation store bypass vulnerabilities mitigation functions. +* +* Should be called only from pre-emption off paths. +* +*/
Quoted 2 lines
Can you please explain how the PRCTL code fulfils the preempt off prerequisite?
Ok. Will add that in next version.
Quoted 35 lines
It does not, which clearly shows that you never tested that code with the full set of debug stuff enabled, because otherwise the preempt check in __this_cpu_read/write would have triggered.
arch/x86/kernel/cpu/bugs.c static const struct { · line 418
Quoted 4 lines
arch/x86/kernel/cpu/bugs.c +4
-0+{ +if (static_branch_unlikely (&specctrl_rds) && +/* need to toggle msr? */ +__this_cpu_read (rds_msr_val) !=SPEC_CTRL_RDS ) {Bah. This is horrible to read, really.
if (static_branch_unlikely(&specctrl_rds)) { if (__this_cpu_read(rds_msr_val) != SPEC_CTRL_RDS) { wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_RDS | x86_spec_ctrl_base); __this_cpu_write(rds_msr_val, SPEC_CTRL_RDS); } }But see below.
arch/x86/kernel/cpu/bugs.c static const struct { · line 443
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +10
-0+int arch_prctl_set_rds (struct task_struct *p,unsigned long val) +{ +if (val ==PR_RDS_MODE_ENABLE ) { +set_tsk_thread_flag (p,TIF_RDS ); +if (p == current) +enable_speculative_store_bypass_mitigation (); + }else if (val ==PR_RDS_MODE_DISABLE ) { +clear_tsk_thread_flag (p,TIF_RDS ); +if (p == current) +disable_speculative_store_bypass_mitigation ();In an earlier discussion it was said, that the prtcl - if at all - is a one way operation, i.e. set it end be done with it. Why is this different again?
Quoted 2 lines
And allowing to disable it once it is set is broken, really.
Will change it if the consensus is not to revert it once it is set.
Quoted 4 lines
arch/x86/kernel/cpu/bugs.c static const struct { · line 445
Quoted 2 lines
arch/x86/kernel/cpu/bugs.c +2
-0+ }else +return -EINVAL ;Missing parentheses. Dammit, why do I have say that over and over?
Will fix
Quoted 82 lines
Quoted 4 lines
arch/x86/kernel/cpu/bugs.c +3
-0+ +return 0 ; +}arch/x86/kernel/cpu/bugs.c static const struct { · line 458
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +10
-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_struct *child)What the heck has this to do with prctl? Its about
TIF_RDSwhere ever that flag has been set from.Quoted 19 lines
arch/x86/kernel/cpu/bugs.c +8
-0+{ +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 == SPEC_STORE_BYPASS_CMD_AUTO))return ; +again:switch (cmd) {case SPEC_STORE_BYPASS_CMD_AUTO :/* arch/x86/kernel/cpu/bugs.c static void __init ssb_select_mitigation(void) · line 525
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +4
-0@@ -452,9 +519,17 @@ static void __init ssb_select_mitigation(void)*/ if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD )break ; +/* Choose prctl as the default mode */ + cmd =SPEC_STORE_BYPASS_CMD_PRCTL ; + goto again; +break ;WTF? This is just crap. The goto is not required at all if done right.
Quoted 6 lines
arch/x86/kernel/process_64.c +0
-0case SPEC_STORE_BYPASS_CMD_ON :@@ -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 ();arch/x86/kernel/process_64.c __switch_to(struct task_struct *prev_p, struct task_struct *next_p) · line 542
Quoted 10 lines
arch/x86/kernel/process_64.c +10
-0+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 ();So this does an unconditional function call on every context switch independent of whether the prctl mode is enabled or not. Crap, really.
What the heck is wrong with putting the TIF flag into
_TIF_WORK_CTXSWand put this into__switch_to_extra()and do:if ((tifp ^ tifn) & _TIF_RDS) speculative_bypass_update(tifn);and that would be:
static inline void
speculative_bypass_update(unsigned long tifn){ u64 msr = x86_spec_ctrl_base | ((tifn & _TIF_RDS) << TIF_TO_RDS_SHIFT); wrmsrl(MSR_IA32_SPEC_CTRL, msr); }
Quoted 3 lines
Which would get rid of the static key because the only place where the mitigation mode and therefore the PRCTL enablement needs to be checked is the prctl itself.
Okay, I'll take a crack with this approach. You have any suggestions of dealing with AMD cpus which use a different msr (MSR_AMD64_LS_CFG) and differnet bit depending on cpu model?
I'm hoping I don't have to check for vendor in this code every time. I was earlier thinking of using static key to segregate the vendor code paths. So if I put in a static_cpu_has(X86_FEATURE_AMD_RDS) kind of check here, will it be acceptable? Or is there already a static key I can use to distinguish between intel and amd cpu?
Quoted 41 lines
int
arch_prctl_set_rds(struct task_struct *p, unsigned long val){ if (!spec_store_bypass_prctl) return -ENOTSUPP; if (val != PR_RDS_MODE_ENABLE) return -EINVAL; set_tsk_thread_flag(p, TIF_RDS); if (p == current) { preempt_disable(); speculative_bypass_update(task_thread_info(p)->flags); preempt_enable(); } return 0; }See? No static key in the context switch path and no unconditional function calls, a single conditional which is evaluated already in
switch_to.If RDS is never set then
speculative_bypass_update()is never ever invoked. If it needs to be toggled, then the MSR write is worse than the extra hoop through__switch_to_extra(). It also spares the per cpu state variable for this RDS bit.That would be too simple and too performant and would magically make this work for 32bit as well, right? It even would make the seccomp magic simple because all it has to do is to set
TIF_RDSon the thread once and not check for seccomp on every fricking context switch.And most important it has absolutely ZERO impact if the prctl is not enabled.
That said, the above might be acceptable when Linus does not veto the whole thing in general.
Thanks,
tglx
Quoted 10 lines · speck for Tim Chen
Quoted 3 lines · speck for Thomas Gleixner
Which would get rid of the static key because the only place where the mitigation mode and therefore the PRCTL enablement needs to be checked is the prctl itself.
Okay, I'll take a crack with this approach. You have any suggestions of dealing with AMD cpus which use a different msr (
MSR_AMD64_LS_CFG) and differnet bit depending on cpu model?I'm hoping I don't have to check for vendor in this code every time.
Now you care about performance? Excuse me, but that just makes me laugh.
The vendor check in the slow path is definitely way less horrible than a extra conditional _and_ a potentially pointless function call into a separate compilation unit in the hot path.
Quoted 4 lines
I was earlier thinking of using static key to segregate the vendor code paths. So if I put in a
static_cpu_has(X86_FEATURE_AMD_RDS)kind of check here, will it be acceptable? Or is there already a static key I can use to distinguish between intel and amd cpu?
Not that I know of. But adding one should be simple enough and I assume that we might have other places where this would be useful.
Thanks,
tglx