Return-path: Received: from mga06.intel.com ([134.134.136.31]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fC8cg-00071I-Le for speck@linutronix.de; Fri, 27 Apr 2018 21:01:35 +0200 Subject: [MODERATED] Re: [PATCH v2] Linux Patch 1/1 References: <92c45587-5eb3-2421-6fc1-42e74a715e30@linux.intel.com> From: Tim Chen Message-ID: Date: Fri, 27 Apr 2018 12:01:31 -0700 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="4j3sxLEO2QWBf060KPsALMgDEStL5wlCc"; protected-headers="v1" To: speck@linutronix.de List-ID: This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156) --4j3sxLEO2QWBf060KPsALMgDEStL5wlCc Content-Type: multipart/mixed; boundary="U2qFrPwAS922ZKZons1Pt3Q6wuGXvW1xF" --U2qFrPwAS922ZKZons1Pt3Q6wuGXvW1xF Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: quoted-printable On 04/27/2018 09:08 AM, speck for Thomas Gleixner wrote: > On Wed, 25 Apr 2018, speck for Tim Chen wrote: >> +/* dynamic reduced data speculation in use */ >> +DEFINE_STATIC_KEY_FALSE(specctrl_rds); >> +EXPORT_SYMBOL(specctrl_rds); >=20 > 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. >=20 >> +/* current MSR_IA32_SPEC_CTRL state for ssb mitigation */ >> +DEFINE_PER_CPU(int, rds_msr_val); >> +EXPORT_SYMBOL(rds_msr_val); >=20 > Ditto Will remove. >=20 >> /* 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_PRCTL, >> }; >> =20 >> static const char *ssb_strings[] =3D { >> [SPEC_STORE_BYPASS_NONE] =3D "Vulnerable", >> - [SPEC_STORE_BYPASS_DISABLE] =3D "Mitigation: Speculative Store Bypas= s disabled" >> + [SPEC_STORE_BYPASS_DISABLE] =3D "Mitigation: Speculative Store Bypas= s disabled", >> + [SPEC_STORE_BYPASS_PRCTL] =3D "Mitigation: Speculative Store Bypass = disabled via prctl" >> }; >> =20 >> static const struct { >> @@ -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 St= ore Bypass */ >> + { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* disable Speculative Sto= re Bypass via prctl */ >> }; >> =20 >> +void enable_speculative_store_bypass_mitigation(void) >=20 > The declaration of those functions says: >=20 >> +/* >> + * speculation store bypass vulnerabilities mitigation functions. >> + * >> + * Should be called only from pre-emption off paths. >> + * >> + */ >=20 > Can you please explain how the PRCTL code fulfils the preempt off > prerequisite? Ok. Will add that in next version. >=20 > It does not, which clearly shows that you never tested that code with t= he > full set of debug stuff enabled, because otherwise the preempt check in= > __this_cpu_read/write would have triggered. >=20 >> +{ >> + if (static_branch_unlikely(&specctrl_rds) && >> + /* need to toggle msr? */ >> + __this_cpu_read(rds_msr_val) !=3D SPEC_CTRL_RDS) { >=20 > Bah. This is horrible to read, really. >=20 > if (static_branch_unlikely(&specctrl_rds)) { > if (__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); > } > } >=20 > But see below. >=20 >> +int arch_prctl_set_rds(struct task_struct *p, unsigned long val) >> +{ >> + if (val =3D=3D PR_RDS_MODE_ENABLE) { >> + set_tsk_thread_flag(p, TIF_RDS); >> + if (p =3D=3D current) >> + enable_speculative_store_bypass_mitigation(); >> + } else if (val =3D=3D PR_RDS_MODE_DISABLE) { >> + clear_tsk_thread_flag(p, TIF_RDS); >> + if (p =3D=3D current) >> + disable_speculative_store_bypass_mitigation(); >=20 > 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 a= gain? >=20 > And allowing to disable it once it is set is broken, really. >=20 Will change it if the consensus is not to revert it once it is set. >> + } else >> + return -EINVAL; >=20 > Missing parentheses. Dammit, why do I have say that over and over? Will fix >=20 >> + >> + 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) >=20 > What the heck has this to do with prctl? Its about TIF_RDS where ever t= hat > flag has been set from. >=20 >> +{ >> + 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 =3D=3D SPEC_STORE_BYPASS_CMD_AUTO)) >> return; >> =20 >> +again: >> switch (cmd) { >> case SPEC_STORE_BYPASS_CMD_AUTO: >> /* >> @@ -452,9 +519,17 @@ static void __init ssb_select_mitigation(void) >> */ >> if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD) >> break; >> + /* Choose prctl as the default mode */ >> + cmd =3D SPEC_STORE_BYPASS_CMD_PRCTL; >> + goto again; >> + break; > =20 > WTF? This is just crap. The goto is not required at all if done right. >=20 >> case SPEC_STORE_BYPASS_CMD_ON: >=20 >> @@ -529,6 +530,17 @@ __switch_to(struct task_struct *prev_p, struct ta= sk_struct *next_p) >> /* Load the Intel cache allocation PQR MSR. */ >> intel_rdt_sched_in(); >> =20 >> + 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(); >=20 > So this does an unconditional function call on every context switch > independent of whether the prctl mode is enabled or not. Crap, really. >=20 > What the heck is wrong with putting the TIF flag into _TIF_WORK_CTXSW a= nd > put this into __switch_to_extra() and do: >=20 > if ((tifp ^ tifn) & _TIF_RDS) > speculative_bypass_update(tifn); >=20 > and that would be: >=20 > static inline void speculative_bypass_update(unsigned long tifn) > { > u64 msr =3D x86_spec_ctrl_base | ((tifn & _TIF_RDS) << TIF_TO_RDS_SHIF= T); >=20 > wrmsrl(MSR_IA32_SPEC_CTRL, msr); > } >=20 > 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?=20 >=20 > int arch_prctl_set_rds(struct task_struct *p, unsigned long val) > { > if (!spec_store_bypass_prctl) > return -ENOTSUPP; > =09 > if (val !=3D PR_RDS_MODE_ENABLE) > return -EINVAL; >=20 > set_tsk_thread_flag(p, TIF_RDS); > if (p =3D=3D current) { > preempt_disable(); > speculative_bypass_update(task_thread_info(p)->flags); > preempt_enable(); > } > return 0; > }=09 >=20 > See? No static key in the context switch path and no unconditional func= tion > calls, a single conditional which is evaluated already in switch_to. >=20 > 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 th= e > extra hoop through __switch_to_extra(). It also spares the per cpu stat= e > variable for this RDS bit. >=20 > That would be too simple and too performant and would magically make th= is > work for 32bit as well, right? It even would make the seccomp magic sim= ple > because all it has to do is to set TIF_RDS on the thread once and not c= heck > for seccomp on every fricking context switch. >=20 > And most important it has absolutely ZERO impact if the prctl is not > enabled. >=20 > That said, the above might be acceptable when Linus does not veto the w= hole > thing in general. >=20 > Thanks, >=20 > tglx >=20 --U2qFrPwAS922ZKZons1Pt3Q6wuGXvW1xF-- --4j3sxLEO2QWBf060KPsALMgDEStL5wlCc--