Skip to content

Commit cb49631

Browse files
committed
KVM: SVM: Don't inject #UD if KVM attempts to skip SEV guest insn
Don't inject a #UD if KVM attempts to "emulate" to skip an instruction for an SEV guest, and instead resume the guest and hope that it can make forward progress. When commit 04c40f3 ("KVM: SVM: Inject #UD on attempted emulation for SEV guest w/o insn buffer") added the completely arbitrary #UD behavior, there were no known scenarios where a well-behaved guest would induce a VM-Exit that triggered emulation, i.e. it was thought that injecting #UD would be helpful. However, now that KVM (correctly) attempts to re-inject INT3/INTO, e.g. if a #NPF is encountered when attempting to deliver the INT3/INTO, an SEV guest can trigger emulation without a buffer, through no fault of its own. Resuming the guest and retrying the INT3/INTO is architecturally wrong, e.g. the vCPU will incorrectly re-hit code #DBs, but for SEV guests there is literally no other option that has a chance of making forward progress. Drop the #UD injection for all "skip" emulation, not just those related to INT3/INTO, even though that means that the guest will likely end up in an infinite loop instead of getting a #UD (the vCPU may also crash, e.g. if KVM emulated everything about an instruction except for advancing RIP). There's no evidence that suggests that an unexpected #UD is actually better than hanging the vCPU, e.g. a soft-hung vCPU can still respond to IRQs and NMIs to generate a backtrace. Reported-by: Wu Zongyo <wuzongyo@mail.ustc.edu.cn> Closes: https://lore.kernel.org/all/8eb933fd-2cf3-d7a9-32fe-2a1d82eac42a@mail.ustc.edu.cn Fixes: 6ef88d6 ("KVM: SVM: Re-inject INT3/INTO instead of retrying the instruction") Cc: stable@vger.kernel.org Cc: Tom Lendacky <thomas.lendacky@amd.com> Link: https://lore.kernel.org/r/20230825013621.2845700-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 1952e74 commit cb49631

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
365365
svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
366366

367367
}
368+
static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
369+
void *insn, int insn_len);
368370

369371
static int __svm_skip_emulated_instruction(struct kvm_vcpu *vcpu,
370372
bool commit_side_effects)
@@ -385,6 +387,14 @@ static int __svm_skip_emulated_instruction(struct kvm_vcpu *vcpu,
385387
}
386388

387389
if (!svm->next_rip) {
390+
/*
391+
* FIXME: Drop this when kvm_emulate_instruction() does the
392+
* right thing and treats "can't emulate" as outright failure
393+
* for EMULTYPE_SKIP.
394+
*/
395+
if (!svm_can_emulate_instruction(vcpu, EMULTYPE_SKIP, NULL, 0))
396+
return 0;
397+
388398
if (unlikely(!commit_side_effects))
389399
old_rflags = svm->vmcb->save.rflags;
390400

@@ -4677,16 +4687,25 @@ static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
46774687
* and cannot be decrypted by KVM, i.e. KVM would read cyphertext and
46784688
* decode garbage.
46794689
*
4680-
* Inject #UD if KVM reached this point without an instruction buffer.
4681-
* In practice, this path should never be hit by a well-behaved guest,
4682-
* e.g. KVM doesn't intercept #UD or #GP for SEV guests, but this path
4683-
* is still theoretically reachable, e.g. via unaccelerated fault-like
4684-
* AVIC access, and needs to be handled by KVM to avoid putting the
4685-
* guest into an infinite loop. Injecting #UD is somewhat arbitrary,
4686-
* but its the least awful option given lack of insight into the guest.
4690+
* If KVM is NOT trying to simply skip an instruction, inject #UD if
4691+
* KVM reached this point without an instruction buffer. In practice,
4692+
* this path should never be hit by a well-behaved guest, e.g. KVM
4693+
* doesn't intercept #UD or #GP for SEV guests, but this path is still
4694+
* theoretically reachable, e.g. via unaccelerated fault-like AVIC
4695+
* access, and needs to be handled by KVM to avoid putting the guest
4696+
* into an infinite loop. Injecting #UD is somewhat arbitrary, but
4697+
* its the least awful option given lack of insight into the guest.
4698+
*
4699+
* If KVM is trying to skip an instruction, simply resume the guest.
4700+
* If a #NPF occurs while the guest is vectoring an INT3/INTO, then KVM
4701+
* will attempt to re-inject the INT3/INTO and skip the instruction.
4702+
* In that scenario, retrying the INT3/INTO and hoping the guest will
4703+
* make forward progress is the only option that has a chance of
4704+
* success (and in practice it will work the vast majority of the time).
46874705
*/
46884706
if (unlikely(!insn)) {
4689-
kvm_queue_exception(vcpu, UD_VECTOR);
4707+
if (!(emul_type & EMULTYPE_SKIP))
4708+
kvm_queue_exception(vcpu, UD_VECTOR);
46904709
return false;
46914710
}
46924711

0 commit comments

Comments
 (0)