Skip to content

Commit 95a6ccb

Browse files
pa1guptaKAGA-KOKO
authored andcommitted
x86/bhi: Mitigate KVM by default
BHI mitigation mode spectre_bhi=auto does not deploy the software mitigation by default. In a cloud environment, it is a likely scenario where userspace is trusted but the guests are not trusted. Deploying system wide mitigation in such cases is not desirable. Update the auto mode to unconditionally mitigate against malicious guests. Deploy the software sequence at VMexit in auto mode also, when hardware mitigation is not available. Unlike the force =on mode, software sequence is not deployed at syscalls in auto mode. Suggested-by: Alexandre Chartre <[email protected]> Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Daniel Sneddon <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]>
1 parent ec9404e commit 95a6ccb

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,12 @@ The possible values in this file are:
439439
- System is protected by retpoline
440440
* - BHI: BHI_DIS_S
441441
- System is protected by BHI_DIS_S
442-
* - BHI: SW loop
442+
* - BHI: SW loop; KVM SW loop
443443
- System is protected by software clearing sequence
444444
* - BHI: Syscall hardening
445445
- Syscalls are hardened against BHI
446+
* - BHI: Syscall hardening; KVM: SW loop
447+
- System is protected from userspace attacks by syscall hardening; KVM is protected by software clearing sequence
446448

447449
Full mitigation might require a microcode update from the CPU
448450
vendor. When the necessary microcode is not available, the kernel will
@@ -669,7 +671,8 @@ kernel command line.
669671
unconditionally disable.
670672
auto
671673
enable if hardware mitigation
672-
control(BHI_DIS_S) is available.
674+
control(BHI_DIS_S) is available, otherwise
675+
enable alternate mitigation in KVM.
673676

674677
For spectre_v2_user see Documentation/admin-guide/kernel-parameters.txt
675678

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6071,8 +6071,9 @@
60716071

60726072
on - unconditionally enable.
60736073
off - unconditionally disable.
6074-
auto - (default) enable only if hardware mitigation
6075-
control(BHI_DIS_S) is available.
6074+
auto - (default) enable hardware mitigation
6075+
(BHI_DIS_S) if available, otherwise enable
6076+
alternate mitigation in KVM.
60766077

60776078
spectre_v2= [X86,EARLY] Control mitigation of Spectre variant 2
60786079
(indirect branch speculation) vulnerability.

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@
469469
#define X86_FEATURE_CLEAR_BHB_LOOP (21*32+ 1) /* "" Clear branch history at syscall entry using SW loop */
470470
#define X86_FEATURE_BHI_CTRL (21*32+ 2) /* "" BHI_DIS_S HW control available */
471471
#define X86_FEATURE_CLEAR_BHB_HW (21*32+ 3) /* "" BHI_DIS_S HW control enabled */
472+
#define X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT (21*32+ 4) /* "" Clear branch history at vmexit using SW loop */
472473

473474
/*
474475
* BUG word(s)

arch/x86/include/asm/nospec-branch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,13 @@
330330
.macro CLEAR_BRANCH_HISTORY
331331
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP
332332
.endm
333+
334+
.macro CLEAR_BRANCH_HISTORY_VMEXIT
335+
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT
336+
.endm
333337
#else
334338
#define CLEAR_BRANCH_HISTORY
339+
#define CLEAR_BRANCH_HISTORY_VMEXIT
335340
#endif
336341

337342
#else /* __ASSEMBLY__ */

arch/x86/kernel/cpu/bugs.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,9 +1668,14 @@ static void __init bhi_select_mitigation(void)
16681668
if (!IS_ENABLED(CONFIG_X86_64))
16691669
return;
16701670

1671+
/* Mitigate KVM by default */
1672+
setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
1673+
pr_info("Spectre BHI mitigation: SW BHB clearing on vm exit\n");
1674+
16711675
if (bhi_mitigation == BHI_MITIGATION_AUTO)
16721676
return;
16731677

1678+
/* Mitigate syscalls when the mitigation is forced =on */
16741679
setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
16751680
pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
16761681
}
@@ -2811,10 +2816,12 @@ static const char * const spectre_bhi_state(void)
28112816
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
28122817
return "; BHI: BHI_DIS_S";
28132818
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
2814-
return "; BHI: SW loop";
2819+
return "; BHI: SW loop, KVM: SW loop";
28152820
else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
28162821
!(x86_read_arch_cap_msr() & ARCH_CAP_RRSBA))
28172822
return "; BHI: Retpoline";
2823+
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
2824+
return "; BHI: Syscall hardening, KVM: SW loop";
28182825

28192826
return "; BHI: Vulnerable (Syscall hardening enabled)";
28202827
}

arch/x86/kvm/vmx/vmenter.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ SYM_INNER_LABEL_ALIGN(vmx_vmexit, SYM_L_GLOBAL)
275275

276276
call vmx_spec_ctrl_restore_host
277277

278-
CLEAR_BRANCH_HISTORY
278+
CLEAR_BRANCH_HISTORY_VMEXIT
279279

280280
/* Put return value in AX */
281281
mov %_ASM_BX, %_ASM_AX

0 commit comments

Comments
 (0)