Skip to content

Commit c9ae63d

Browse files
bp3tk0vgregkh
authored andcommitted
x86/srso: Add IBPB on VMEXIT
Upstream commit: d893832 Add the option to flush IBPB only on VMEXIT in order to protect from malicious guests but one otherwise trusts the software that runs on the hypervisor. Signed-off-by: Borislav Petkov (AMD) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 79c8091 commit c9ae63d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

arch/x86/include/asm/cpufeatures.h

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

311311
#define X86_FEATURE_SRSO (11*32+24) /* "" AMD BTB untrain RETs */
312312
#define X86_FEATURE_SRSO_ALIAS (11*32+25) /* "" AMD BTB untrain RETs through aliasing */
313+
#define X86_FEATURE_IBPB_ON_VMEXIT (11*32+26) /* "" Issue an IBPB only on VMEXIT */
313314

314315
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
315316
#define X86_FEATURE_AVX_VNNI (12*32+ 4) /* AVX VNNI instructions */

arch/x86/kernel/cpu/bugs.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,20 +2316,23 @@ enum srso_mitigation {
23162316
SRSO_MITIGATION_MICROCODE,
23172317
SRSO_MITIGATION_SAFE_RET,
23182318
SRSO_MITIGATION_IBPB,
2319+
SRSO_MITIGATION_IBPB_ON_VMEXIT,
23192320
};
23202321

23212322
enum srso_mitigation_cmd {
23222323
SRSO_CMD_OFF,
23232324
SRSO_CMD_MICROCODE,
23242325
SRSO_CMD_SAFE_RET,
23252326
SRSO_CMD_IBPB,
2327+
SRSO_CMD_IBPB_ON_VMEXIT,
23262328
};
23272329

23282330
static const char * const srso_strings[] = {
23292331
[SRSO_MITIGATION_NONE] = "Vulnerable",
23302332
[SRSO_MITIGATION_MICROCODE] = "Mitigation: microcode",
23312333
[SRSO_MITIGATION_SAFE_RET] = "Mitigation: safe RET",
23322334
[SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
2335+
[SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
23332336
};
23342337

23352338
static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
@@ -2348,6 +2351,8 @@ static int __init srso_parse_cmdline(char *str)
23482351
srso_cmd = SRSO_CMD_SAFE_RET;
23492352
else if (!strcmp(str, "ibpb"))
23502353
srso_cmd = SRSO_CMD_IBPB;
2354+
else if (!strcmp(str, "ibpb-vmexit"))
2355+
srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
23512356
else
23522357
pr_err("Ignoring unknown SRSO option (%s).", str);
23532358

@@ -2431,6 +2436,20 @@ static void __init srso_select_mitigation(void)
24312436
pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
24322437
goto pred_cmd;
24332438
}
2439+
break;
2440+
2441+
case SRSO_CMD_IBPB_ON_VMEXIT:
2442+
if (IS_ENABLED(CONFIG_CPU_SRSO)) {
2443+
if (!boot_cpu_has(X86_FEATURE_ENTRY_IBPB) && has_microcode) {
2444+
setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2445+
srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
2446+
}
2447+
} else {
2448+
pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
2449+
goto pred_cmd;
2450+
}
2451+
break;
2452+
24342453
default:
24352454
break;
24362455
}

arch/x86/kvm/svm/svm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,9 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
14851485

14861486
if (sd->current_vmcb != svm->vmcb) {
14871487
sd->current_vmcb = svm->vmcb;
1488-
indirect_branch_prediction_barrier();
1488+
1489+
if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT))
1490+
indirect_branch_prediction_barrier();
14891491
}
14901492
if (kvm_vcpu_apicv_active(vcpu))
14911493
avic_vcpu_load(vcpu, cpu);

arch/x86/kvm/svm/vmenter.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ SYM_FUNC_START(__svm_vcpu_run)
223223
*/
224224
UNTRAIN_RET
225225

226+
/* SRSO */
227+
ALTERNATIVE "", "call entry_ibpb", X86_FEATURE_IBPB_ON_VMEXIT
228+
226229
/*
227230
* Clear all general purpose registers except RSP and RAX to prevent
228231
* speculative use of the guest's values, even those that are reloaded

0 commit comments

Comments
 (0)