Skip to content

Commit 19b33bf

Browse files
committed
x86/bugs: KVM: Add support for SRSO_MSR_FIX
JIRA: https://issues.redhat.com/browse/RHEL-88224 commit 8442df2 Author: Borislav Petkov <bp@alien8.de> Date: Tue, 18 Feb 2025 12:13:33 +0100 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 Signed-off-by: Waiman Long <longman@redhat.com>
1 parent 1af036e commit 19b33bf

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
@@ -466,6 +466,10 @@
466466
#define X86_FEATURE_IBPB_BRTYPE (20*32+28) /* MSR_PRED_CMD[IBPB] flushes all branch type predictions */
467467
#define X86_FEATURE_SRSO_NO (20*32+29) /* CPU is not affected by SRSO */
468468
#define X86_FEATURE_SRSO_USER_KERNEL_NO (20*32+30) /* CPU is not affected by SRSO across user/kernel boundaries */
469+
#define X86_FEATURE_SRSO_BP_SPEC_REDUCE (20*32+31) /*
470+
* BP_CFG[BpSpecReduce] can be used to mitigate SRSO for VMs.
471+
* (SRSO_MSR_FIX in the official doc).
472+
*/
469473

470474
/*
471475
* 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
@@ -685,6 +685,7 @@
685685

686686
/* Zen4 */
687687
#define MSR_ZEN4_BP_CFG 0xc001102e
688+
#define MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT 4
688689
#define MSR_ZEN4_BP_CFG_SHARED_BTB_FIX_BIT 5
689690

690691
/* Fam 19h MSRs */

arch/x86/kernel/cpu/bugs.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,7 @@ enum srso_mitigation {
26072607
SRSO_MITIGATION_SAFE_RET,
26082608
SRSO_MITIGATION_IBPB,
26092609
SRSO_MITIGATION_IBPB_ON_VMEXIT,
2610+
SRSO_MITIGATION_BP_SPEC_REDUCE,
26102611
};
26112612

26122613
enum srso_mitigation_cmd {
@@ -2624,7 +2625,8 @@ static const char * const srso_strings[] = {
26242625
[SRSO_MITIGATION_MICROCODE] = "Vulnerable: Microcode, no safe RET",
26252626
[SRSO_MITIGATION_SAFE_RET] = "Mitigation: Safe RET",
26262627
[SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
2627-
[SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
2628+
[SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only",
2629+
[SRSO_MITIGATION_BP_SPEC_REDUCE] = "Mitigation: Reduced Speculation"
26282630
};
26292631

26302632
static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
@@ -2663,7 +2665,7 @@ static void __init srso_select_mitigation(void)
26632665
srso_cmd == SRSO_CMD_OFF) {
26642666
if (boot_cpu_has(X86_FEATURE_SBPB))
26652667
x86_pred_cmd = PRED_CMD_SBPB;
2666-
return;
2668+
goto out;
26672669
}
26682670

26692671
if (has_microcode) {
@@ -2675,7 +2677,7 @@ static void __init srso_select_mitigation(void)
26752677
*/
26762678
if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
26772679
setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
2678-
return;
2680+
goto out;
26792681
}
26802682

26812683
if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
@@ -2755,6 +2757,12 @@ static void __init srso_select_mitigation(void)
27552757

27562758
ibpb_on_vmexit:
27572759
case SRSO_CMD_IBPB_ON_VMEXIT:
2760+
if (boot_cpu_has(X86_FEATURE_SRSO_BP_SPEC_REDUCE)) {
2761+
pr_notice("Reducing speculation to address VM/HV SRSO attack vector.\n");
2762+
srso_mitigation = SRSO_MITIGATION_BP_SPEC_REDUCE;
2763+
break;
2764+
}
2765+
27582766
if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
27592767
if (has_microcode) {
27602768
setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
@@ -2776,7 +2784,15 @@ static void __init srso_select_mitigation(void)
27762784
}
27772785

27782786
out:
2779-
pr_info("%s\n", srso_strings[srso_mitigation]);
2787+
/*
2788+
* Clear the feature flag if this mitigation is not selected as that
2789+
* feature flag controls the BpSpecReduce MSR bit toggling in KVM.
2790+
*/
2791+
if (srso_mitigation != SRSO_MITIGATION_BP_SPEC_REDUCE)
2792+
setup_clear_cpu_cap(X86_FEATURE_SRSO_BP_SPEC_REDUCE);
2793+
2794+
if (srso_mitigation != SRSO_MITIGATION_NONE)
2795+
pr_info("%s\n", srso_strings[srso_mitigation]);
27802796
}
27812797

27822798
#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)