Skip to content

Commit da6393c

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: X86: Fix warning caused by stale emulation context
Reported by syzkaller: WARNING: CPU: 7 PID: 10526 at linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm] RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xae Commit 4a1e10d ("KVM: x86: handle hardware breakpoints during emulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have the EMULTYPE_NO_DECODE flag here and the emulation context will not be reused. Commit c8848ce ("KVM: x86: set ctxt->have_exception in x86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result in EMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization into init_emulate_ctxt() and before hardware breakpoints check. The ctxt->ud will be dropped by a follow-up patch. syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000 Reported-by: [email protected] Fixes: 4a1e10d (KVM: x86: handle hardware breakpoints during emulation) Signed-off-by: Wanpeng Li <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]>
1 parent e87e46d commit da6393c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

arch/x86/kvm/x86.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7228,6 +7228,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
72287228
BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
72297229
BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
72307230

7231+
ctxt->interruptibility = 0;
7232+
ctxt->have_exception = false;
7233+
ctxt->exception.vector = -1;
7234+
ctxt->perm_ok = false;
7235+
72317236
init_decode_cache(ctxt);
72327237
vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
72337238
}
@@ -7563,11 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
75637568
kvm_vcpu_check_breakpoint(vcpu, &r))
75647569
return r;
75657570

7566-
ctxt->interruptibility = 0;
7567-
ctxt->have_exception = false;
7568-
ctxt->exception.vector = -1;
7569-
ctxt->perm_ok = false;
7570-
75717571
ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
75727572

75737573
r = x86_decode_insn(ctxt, insn, insn_len);

0 commit comments

Comments
 (0)