Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 2bf604d

Browse files
pa1guptagregkh
authored andcommitted
x86/bhi: Mitigate KVM by default
commit 95a6ccb upstream. 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 <alexandre.chartre@oracle.com> Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> Signed-off-by: Daniel Sneddon <daniel.sneddon@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Reviewed-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Daniel Sneddon <daniel.sneddon@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a39bfa5 commit 2bf604d

6 files changed

Lines changed: 23 additions & 6 deletions

File tree

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
@@ -6039,8 +6039,9 @@
60396039

60406040
on - unconditionally enable.
60416041
off - unconditionally disable.
6042-
auto - (default) enable only if hardware mitigation
6043-
control(BHI_DIS_S) is available.
6042+
auto - (default) enable hardware mitigation
6043+
(BHI_DIS_S) if available, otherwise enable
6044+
alternate mitigation in KVM.
60446045

60456046
spectre_v2= [X86] Control mitigation of Spectre variant 2
60466047
(indirect branch speculation) vulnerability.

arch/x86/include/asm/cpufeatures.h

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

469470
/*
470471
* BUG word(s)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,13 @@
339339
.macro CLEAR_BRANCH_HISTORY
340340
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP
341341
.endm
342+
343+
.macro CLEAR_BRANCH_HISTORY_VMEXIT
344+
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT
345+
.endm
342346
#else
343347
#define CLEAR_BRANCH_HISTORY
348+
#define CLEAR_BRANCH_HISTORY_VMEXIT
344349
#endif
345350

346351
#else /* __ASSEMBLY__ */

arch/x86/kernel/cpu/bugs.c

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

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

1677+
/* Mitigate syscalls when the mitigation is forced =on */
16731678
setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
16741679
pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
16751680
}
@@ -2810,10 +2815,12 @@ static const char * const spectre_bhi_state(void)
28102815
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
28112816
return "; BHI: BHI_DIS_S";
28122817
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
2813-
return "; BHI: SW loop";
2818+
return "; BHI: SW loop, KVM: SW loop";
28142819
else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
28152820
!(x86_read_arch_cap_msr() & ARCH_CAP_RRSBA))
28162821
return "; BHI: Retpoline";
2822+
else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
2823+
return "; BHI: Syscall hardening, KVM: SW loop";
28172824

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

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)