KVM: X86: Fix warning caused by stale emulation context
PATCH 1/1 KVM: X86: Fix warning caused by stale emulation context
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/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 4a1e10d5b5d8c (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 EMULTYPE_NO_DECODE flag here and the emulation context will not be reused. Commit c8848cee74ff (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 before hardware breakpoints check.
syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
--- arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +7 -7
@@ -7552,6 +7552,13 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,init_emulate_ctxt (vcpu); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; + + ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; +/* * We will reenter on the same instruction since we do not set * complete_userspace_io. This does not handle watchpoints yet,
@@ -7561,13 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; - r =x86_decode_insn (ctxt, insn, insn_len);trace_kvm_emulate_insn_start (vcpu);
Signature · 1 line
2.7.4
Sean Christopherson
maintainerQuoted 39 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/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/0xaeCommit
4a1e10d5b5d8c(KVM: x86: handle hardware breakpoints duringemulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't haveEMULTYPE_NO_DECODEflag here and the emulation context will not be reused. Commitc8848cee74ff(KVM: x86:
setctxt->have_exceptioninx86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result inEMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization before hardware breakpoints check.syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
Reported-by [email protected]Fixes 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 7558
Quoted 7 lines
arch/x86/kvm/x86.c +4
-0@@ -7552,6 +7552,13 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,init_emulate_ctxt (vcpu); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false;
What about moving this block all the way into init_emulate_ctxt()?
arch/x86/kvm/x86.c int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 7559
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+ ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;
This can be left where it is since ctxt->ud is consumed only by x86_decode_insn(). I don't have a strong preference as it really only matters for the backport. For upstream, we can kill it off in a follow-up patch by passing emulation_type to x86_decode_insn() and dropping ctxt->ud altogether. Tracking that info in ctxt for literally one call is silly.
arch/x86/kvm/emulate.c +2 -1
@@ -5322,7 +5322,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->execute = opcode.u.execute; -if (unlikely (ctxt->ud) &&likely (!(ctxt->d & EmulateOnUD))) +if (unlikely (emulation_type &EMULTYPE_TRAP_UD ) && +likely (!(ctxt->d & EmulateOnUD)))return EMULATION_FAILED ; if (unlikely(ctxt->d &
arch/x86/kvm/kvm_emulate.h +0 -1
@@ -314,7 +314,6 @@ struct x86_emulate_ctxt {int interruptibility;bool perm_ok;/* do not check permissions if true */ -bool ud;/* inject an #UD if host doesn't support insn */ bool tf;/* TF value before instruction (after for syscall/sysret) */ bool have_exception;
arch/x86/kvm/x86.c +1 -3
@@ -7552,8 +7552,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,init_emulate_ctxt (vcpu); - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; -/* * We will reenter on the same instruction since we do not set * complete_userspace_io. This does not handle watchpoints yet,
@@ -7563,7 +7561,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - r =x86_decode_insn (ctxt, insn, insn_len); + r =x86_decode_insn (ctxt, insn, insn_len, emulation_type);trace_kvm_emulate_insn_start (vcpu); ++vcpu->stat.insn_emulation;
Quoted 21 lines
arch/x86/kvm/x86.c +1
-7+/* * We will reenter on the same instruction since we do not set * complete_userspace_io. This does not handle watchpoints yet, @@ -7561,13 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; - r =x86_decode_insn (ctxt, insn, insn_len);trace_kvm_emulate_insn_start (vcpu);Signature · 1 line
2.7.4
Quoted 51 lines · Sean Christopherson
Quoted 39 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/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/0xaeCommit
4a1e10d5b5d8c(KVM: x86: handle hardware breakpoints duringemulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't haveEMULTYPE_NO_DECODEflag here and the emulation context will not be reused. Commitc8848cee74ff(KVM: x86:
setctxt->have_exceptioninx86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result inEMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization before hardware breakpoints check.syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
Reported-by [email protected]Fixes 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 7558
Quoted 7 lines
arch/x86/kvm/x86.c +4
-0@@ -7552,6 +7552,13 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,init_emulate_ctxt (vcpu); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false;What about moving this block all the way into
init_emulate_ctxt()?
Quoted 7 lines
arch/x86/kvm/x86.c int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 7559
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+ ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;This can be left where it is since
ctxt->udis consumed only byx86_decode_insn(). I don't have a strong preference as it really only matters for the backport. For upstream, we can kill it off in a follow-up patch by passingemulation_typetox86_decode_insn()and droppingctxt->udaltogether. Tracking that info in ctxt for literally one call is silly.
Good suggestion, will do in v2.
Wanpeng