Skip to content

Commit 8442df2

Browse files
committed
x86/bugs: KVM: Add support for SRSO_MSR_FIX
Add support for CPUID Fn8000_0021_EAX[31] (SRSO_MSR_FIX). If this bit is 1, it indicates that software may use MSR BP_CFG[BpSpecReduce] to mitigate SRSO. Enable BpSpecReduce to mitigate SRSO across guest/host boundaries. Switch back to enabling the bit when virtualization is enabled and to clear the bit when virtualization is disabled because using a MSR slot would clear the bit when the guest is exited and any training the guest has done, would potentially influence the host kernel when execution enters the kernel and hasn't VMRUN the guest yet. More detail on the public thread in Link below. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20241202120416.6054-1-bp@kernel.org
1 parent d082ecb commit 8442df2

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

Documentation/admin-guide/hw-vuln/srso.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,20 @@ The possible values in this file are:
104104

105105
(spec_rstack_overflow=ibpb-vmexit)
106106

107+
* 'Mitigation: Reduced Speculation':
107108

109+
This mitigation gets automatically enabled when the above one "IBPB on
110+
VMEXIT" has been selected and the CPU supports the BpSpecReduce bit.
111+
112+
It gets automatically enabled on machines which have the
113+
SRSO_USER_KERNEL_NO=1 CPUID bit. In that case, the code logic is to switch
114+
to the above =ibpb-vmexit mitigation because the user/kernel boundary is
115+
not affected anymore and thus "safe RET" is not needed.
116+
117+
After enabling the IBPB on VMEXIT mitigation option, the BpSpecReduce bit
118+
is detected (functionality present on all such machines) and that
119+
practically overrides IBPB on VMEXIT as it has a lot less performance
120+
impact and takes care of the guest->host attack vector too.
108121

109122
In order to exploit vulnerability, an attacker needs to:
110123

arch/x86/include/asm/cpufeatures.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@
468468
#define X86_FEATURE_IBPB_BRTYPE (20*32+28) /* MSR_PRED_CMD[IBPB] flushes all branch type predictions */
469469
#define X86_FEATURE_SRSO_NO (20*32+29) /* CPU is not affected by SRSO */
470470
#define X86_FEATURE_SRSO_USER_KERNEL_NO (20*32+30) /* CPU is not affected by SRSO across user/kernel boundaries */
471+
#define X86_FEATURE_SRSO_BP_SPEC_REDUCE (20*32+31) /*
472+
* BP_CFG[BpSpecReduce] can be used to mitigate SRSO for VMs.
473+
* (SRSO_MSR_FIX in the official doc).
474+
*/
471475

472476
/*
473477
* Extended auxiliary flags: Linux defined - for features scattered in various

arch/x86/include/asm/msr-index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@
721721

722722
/* Zen4 */
723723
#define MSR_ZEN4_BP_CFG 0xc001102e
724+
#define MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT 4
724725
#define MSR_ZEN4_BP_CFG_SHARED_BTB_FIX_BIT 5
725726

726727
/* Fam 19h MSRs */

arch/x86/kernel/cpu/bugs.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,7 @@ enum srso_mitigation {
25222522
SRSO_MITIGATION_SAFE_RET,
25232523
SRSO_MITIGATION_IBPB,
25242524
SRSO_MITIGATION_IBPB_ON_VMEXIT,
2525+
SRSO_MITIGATION_BP_SPEC_REDUCE,
25252526
};
25262527

25272528
enum srso_mitigation_cmd {
@@ -2539,7 +2540,8 @@ static const char * const srso_strings[] = {
25392540
[SRSO_MITIGATION_MICROCODE] = "Vulnerable: Microcode, no safe RET",
25402541
[SRSO_MITIGATION_SAFE_RET] = "Mitigation: Safe RET",
25412542
[SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
2542-
[SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
2543+
[SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only",
2544+
[SRSO_MITIGATION_BP_SPEC_REDUCE] = "Mitigation: Reduced Speculation"
25432545
};
25442546

25452547
static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
@@ -2578,7 +2580,7 @@ static void __init srso_select_mitigation(void)
25782580
srso_cmd == SRSO_CMD_OFF) {
25792581
if (boot_cpu_has(X86_FEATURE_SBPB))
25802582
x86_pred_cmd = PRED_CMD_SBPB;
2581-
return;
2583+
goto out;
25822584
}
25832585

25842586
if (has_microcode) {
@@ -2590,7 +2592,7 @@ static void __init srso_select_mitigation(void)
25902592
*/
25912593
if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
25922594
setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
2593-
return;
2595+
goto out;
25942596
}
25952597

25962598
if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
@@ -2670,6 +2672,12 @@ static void __init srso_select_mitigation(void)
26702672

26712673
ibpb_on_vmexit:
26722674
case SRSO_CMD_IBPB_ON_VMEXIT:
2675+
if (boot_cpu_has(X86_FEATURE_SRSO_BP_SPEC_REDUCE)) {
2676+
pr_notice("Reducing speculation to address VM/HV SRSO attack vector.\n");
2677+
srso_mitigation = SRSO_MITIGATION_BP_SPEC_REDUCE;
2678+
break;
2679+
}
2680+
26732681
if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
26742682
if (has_microcode) {
26752683
setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
@@ -2691,7 +2699,15 @@ static void __init srso_select_mitigation(void)
26912699
}
26922700

26932701
out:
2694-
pr_info("%s\n", srso_strings[srso_mitigation]);
2702+
/*
2703+
* Clear the feature flag if this mitigation is not selected as that
2704+
* feature flag controls the BpSpecReduce MSR bit toggling in KVM.
2705+
*/
2706+
if (srso_mitigation != SRSO_MITIGATION_BP_SPEC_REDUCE)
2707+
setup_clear_cpu_cap(X86_FEATURE_SRSO_BP_SPEC_REDUCE);
2708+
2709+
if (srso_mitigation != SRSO_MITIGATION_NONE)
2710+
pr_info("%s\n", srso_strings[srso_mitigation]);
26952711
}
26962712

26972713
#undef pr_fmt

arch/x86/kvm/svm/svm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ static void svm_disable_virtualization_cpu(void)
607607
kvm_cpu_svm_disable();
608608

609609
amd_pmu_disable_virt();
610+
611+
if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
612+
msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
610613
}
611614

612615
static int svm_enable_virtualization_cpu(void)
@@ -684,6 +687,9 @@ static int svm_enable_virtualization_cpu(void)
684687
rdmsr(MSR_TSC_AUX, sev_es_host_save_area(sd)->tsc_aux, msr_hi);
685688
}
686689

690+
if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
691+
msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
692+
687693
return 0;
688694
}
689695

arch/x86/lib/msr.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int msr_set_bit(u32 msr, u8 bit)
103103
{
104104
return __flip_bit(msr, bit, true);
105105
}
106+
EXPORT_SYMBOL_GPL(msr_set_bit);
106107

107108
/**
108109
* msr_clear_bit - Clear @bit in a MSR @msr.
@@ -118,6 +119,7 @@ int msr_clear_bit(u32 msr, u8 bit)
118119
{
119120
return __flip_bit(msr, bit, false);
120121
}
122+
EXPORT_SYMBOL_GPL(msr_clear_bit);
121123

122124
#ifdef CONFIG_TRACEPOINTS
123125
void do_trace_write_msr(unsigned int msr, u64 val, int failed)

0 commit comments

Comments
 (0)