Skip to content

Commit fee060c

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: avoid calling x86 emulator without a decoded instruction
Whenever x86_decode_emulated_instruction() detects a breakpoint, it returns the value that kvm_vcpu_check_breakpoint() writes into its pass-by-reference second argument. Unfortunately this is completely bogus because the expected outcome of x86_decode_emulated_instruction is an EMULATION_* value. Then, if kvm_vcpu_check_breakpoint() does "*r = 0" (corresponding to a KVM_EXIT_DEBUG userspace exit), it is misunderstood as EMULATION_OK and x86_emulate_instruction() is called without having decoded the instruction. This causes various havoc from running with a stale emulation context. The fix is to move the call to kvm_vcpu_check_breakpoint() where it was before commit 4aa2691 ("KVM: x86: Factor out x86 instruction emulation with decoding") introduced x86_decode_emulated_instruction(). The other caller of the function does not need breakpoint checks, because it is invoked as part of a vmexit and the processor has already checked those before executing the instruction that #GP'd. This fixes CVE-2022-1852. Reported-by: Qiuhao Li <[email protected]> Reported-by: Gaoning Pan <[email protected]> Reported-by: Yongkang Jia <[email protected]> Fixes: 4aa2691 ("KVM: x86: Factor out x86 instruction emulation with decoding") Cc: [email protected] Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> [Rewrote commit message according to Qiuhao's report, since a patch already existed to fix the bug. - Paolo] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d22d247 commit fee060c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

arch/x86/kvm/x86.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8296,7 +8296,7 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
82968296
}
82978297
EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
82988298

8299-
static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
8299+
static bool kvm_vcpu_check_code_breakpoint(struct kvm_vcpu *vcpu, int *r)
83008300
{
83018301
if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
83028302
(vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
@@ -8365,25 +8365,23 @@ static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
83658365
}
83668366

83678367
/*
8368-
* Decode to be emulated instruction. Return EMULATION_OK if success.
8368+
* Decode an instruction for emulation. The caller is responsible for handling
8369+
* code breakpoints. Note, manually detecting code breakpoints is unnecessary
8370+
* (and wrong) when emulating on an intercepted fault-like exception[*], as
8371+
* code breakpoints have higher priority and thus have already been done by
8372+
* hardware.
8373+
*
8374+
* [*] Except #MC, which is higher priority, but KVM should never emulate in
8375+
* response to a machine check.
83698376
*/
83708377
int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
83718378
void *insn, int insn_len)
83728379
{
8373-
int r = EMULATION_OK;
83748380
struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
8381+
int r;
83758382

83768383
init_emulate_ctxt(vcpu);
83778384

8378-
/*
8379-
* We will reenter on the same instruction since we do not set
8380-
* complete_userspace_io. This does not handle watchpoints yet,
8381-
* those would be handled in the emulate_ops.
8382-
*/
8383-
if (!(emulation_type & EMULTYPE_SKIP) &&
8384-
kvm_vcpu_check_breakpoint(vcpu, &r))
8385-
return r;
8386-
83878385
r = x86_decode_insn(ctxt, insn, insn_len, emulation_type);
83888386

83898387
trace_kvm_emulate_insn_start(vcpu);
@@ -8416,6 +8414,15 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
84168414
if (!(emulation_type & EMULTYPE_NO_DECODE)) {
84178415
kvm_clear_exception_queue(vcpu);
84188416

8417+
/*
8418+
* Return immediately if RIP hits a code breakpoint, such #DBs
8419+
* are fault-like and are higher priority than any faults on
8420+
* the code fetch itself.
8421+
*/
8422+
if (!(emulation_type & EMULTYPE_SKIP) &&
8423+
kvm_vcpu_check_code_breakpoint(vcpu, &r))
8424+
return r;
8425+
84198426
r = x86_decode_emulated_instruction(vcpu, emulation_type,
84208427
insn, insn_len);
84218428
if (r != EMULATION_OK) {

0 commit comments

Comments
 (0)