Skip to content

Commit e473198

Browse files
committed
KVM: x86: do not report a vCPU as preempted outside instruction boundaries
jira VULN-8766 cve CVE-2022-39189 commit-author Paolo Bonzini <[email protected]> commit 6cd8824 If a vCPU is outside guest mode and is scheduled out, it might be in the process of making a memory access. A problem occurs if another vCPU uses the PV TLB flush feature during the period when the vCPU is scheduled out, and a virtual address has already been translated but has not yet been accessed, because this is equivalent to using a stale TLB entry. To avoid this, only report a vCPU as preempted if sure that the guest is at an instruction boundary. A rescheduling request will be delivered to the host physical CPU as an external interrupt, so for simplicity consider any vmexit *not* instruction boundary except for external interrupts. It would in principle be okay to report the vCPU as preempted also if it is sleeping in kvm_vcpu_block(): a TLB flush IPI will incur the vmentry/vmexit overhead unnecessarily, and optimistic spinning is also unlikely to succeed. However, leave it for later because right now kvm_vcpu_check_block() is doing memory accesses. Even though the TLB flush issue only applies to virtual memory address, it's very much preferrable to be conservative. Reported-by: Jann Horn <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> (cherry picked from commit 6cd8824) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent fa3eafa commit e473198

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ struct kvm_vcpu_arch {
644644
u64 ia32_misc_enable_msr;
645645
u64 smbase;
646646
u64 smi_count;
647+
bool at_instruction_boundary;
647648
bool tpr_access_reporting;
648649
bool xsaves_enabled;
649650
bool xfd_no_write_intercept;
@@ -1282,6 +1283,8 @@ struct kvm_vcpu_stat {
12821283
u64 nested_run;
12831284
u64 directed_yield_attempted;
12841285
u64 directed_yield_successful;
1286+
u64 preemption_reported;
1287+
u64 preemption_other;
12851288
u64 guest_mode;
12861289
};
12871290

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4176,6 +4176,8 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
41764176

41774177
static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
41784178
{
4179+
if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
4180+
vcpu->arch.at_instruction_boundary = true;
41794181
}
41804182

41814183
static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6631,6 +6631,7 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
66316631
return;
66326632

66336633
handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc));
6634+
vcpu->arch.at_instruction_boundary = true;
66346635
}
66356636

66366637
static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)

arch/x86/kvm/x86.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
289289
STATS_DESC_COUNTER(VCPU, nested_run),
290290
STATS_DESC_COUNTER(VCPU, directed_yield_attempted),
291291
STATS_DESC_COUNTER(VCPU, directed_yield_successful),
292+
STATS_DESC_COUNTER(VCPU, preemption_reported),
293+
STATS_DESC_COUNTER(VCPU, preemption_other),
292294
STATS_DESC_ICOUNTER(VCPU, guest_mode)
293295
};
294296

@@ -4607,6 +4609,19 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
46074609
static const u8 preempted = KVM_VCPU_PREEMPTED;
46084610
gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS;
46094611

4612+
/*
4613+
* The vCPU can be marked preempted if and only if the VM-Exit was on
4614+
* an instruction boundary and will not trigger guest emulation of any
4615+
* kind (see vcpu_run). Vendor specific code controls (conservatively)
4616+
* when this is true, for example allowing the vCPU to be marked
4617+
* preempted if and only if the VM-Exit was due to a host interrupt.
4618+
*/
4619+
if (!vcpu->arch.at_instruction_boundary) {
4620+
vcpu->stat.preemption_other++;
4621+
return;
4622+
}
4623+
4624+
vcpu->stat.preemption_reported++;
46104625
if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
46114626
return;
46124627

@@ -10367,6 +10382,13 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
1036710382
vcpu->arch.l1tf_flush_l1d = true;
1036810383

1036910384
for (;;) {
10385+
/*
10386+
* If another guest vCPU requests a PV TLB flush in the middle
10387+
* of instruction emulation, the rest of the emulation could
10388+
* use a stale page translation. Assume that any code after
10389+
* this point can start executing an instruction.
10390+
*/
10391+
vcpu->arch.at_instruction_boundary = false;
1037010392
if (kvm_vcpu_running(vcpu)) {
1037110393
r = vcpu_enter_guest(vcpu);
1037210394
} else {

0 commit comments

Comments
 (0)