Skip to content

Commit 0ec827f

Browse files
KAGA-KOKOgregkh
authored andcommitted
x86/bugs: Rework spec_ctrl base and mask logic
commit be6fcb5 upstream x86_spec_ctrL_mask is intended to mask out bits from a MSR_SPEC_CTRL value which are not to be modified. However the implementation is not really used and the bitmask was inverted to make a check easier, which was removed in "x86/bugs: Remove x86_spec_ctrl_set()" Aside of that it is missing the STIBP bit if it is supported by the platform, so if the mask would be used in x86_virt_spec_ctrl() then it would prevent a guest from setting STIBP. Add the STIBP bit if supported and use the mask in x86_virt_spec_ctrl() to sanitize the value which is supplied by the guest. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Borislav Petkov <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ec90464 commit 0ec827f

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

arch/x86/kernel/cpu/bugs.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
4141
* The vendor and possibly platform specific bits which can be modified in
4242
* x86_spec_ctrl_base.
4343
*/
44-
static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
44+
static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
4545

4646
/*
4747
* AMD specific MSR info for Speculative Store Bypass control.
@@ -67,6 +67,10 @@ void __init check_bugs(void)
6767
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
6868
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
6969

70+
/* Allow STIBP in MSR_SPEC_CTRL if supported */
71+
if (boot_cpu_has(X86_FEATURE_STIBP))
72+
x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
73+
7074
/* Select the proper spectre mitigation before patching alternatives */
7175
spectre_v2_select_mitigation();
7276

@@ -135,18 +139,26 @@ static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
135139
void
136140
x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
137141
{
142+
u64 msrval, guestval, hostval = x86_spec_ctrl_base;
138143
struct thread_info *ti = current_thread_info();
139-
u64 msr, host = x86_spec_ctrl_base;
140144

141145
/* Is MSR_SPEC_CTRL implemented ? */
142146
if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
147+
/*
148+
* Restrict guest_spec_ctrl to supported values. Clear the
149+
* modifiable bits in the host base value and or the
150+
* modifiable bits from the guest value.
151+
*/
152+
guestval = hostval & ~x86_spec_ctrl_mask;
153+
guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
154+
143155
/* SSBD controlled in MSR_SPEC_CTRL */
144156
if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
145-
host |= ssbd_tif_to_spec_ctrl(ti->flags);
157+
hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
146158

147-
if (host != guest_spec_ctrl) {
148-
msr = setguest ? guest_spec_ctrl : host;
149-
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
159+
if (hostval != guestval) {
160+
msrval = setguest ? guestval : hostval;
161+
wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
150162
}
151163
}
152164
}
@@ -492,7 +504,7 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
492504
switch (boot_cpu_data.x86_vendor) {
493505
case X86_VENDOR_INTEL:
494506
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
495-
x86_spec_ctrl_mask &= ~SPEC_CTRL_SSBD;
507+
x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
496508
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
497509
break;
498510
case X86_VENDOR_AMD:

0 commit comments

Comments
 (0)