linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context
@ 2021-05-25  3:02 Wanpeng Li
  2021-05-25  3:02 ` [PATCH v2 2/2] KVM: X86: Kill off ctxt->ud Wanpeng Li
  2021-05-26 17:52 ` [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Sean Christopherson
  0 siblings, 2 replies; 4+ messages in thread
From: Wanpeng Li @ 2021-05-25  3:02 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

From: Wanpeng Li <wanpengli@tencent.com>

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 the 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 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: syzbot+71271244f206d17f6441@syzkaller.appspotmail.com
Fixes: 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
v1 -> v2:
 * move the second part emulation context initialization into init_emulate_ctxt()

 arch/x86/kvm/x86.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index bed7b53..3c109d3 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7228,6 +7228,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
 	BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
 	BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
 
+	ctxt->interruptibility = 0;
+	ctxt->have_exception = false;
+	ctxt->exception.vector = -1;
+	ctxt->perm_ok = false;
+
 	init_decode_cache(ctxt);
 	vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
 }
@@ -7554,6 +7559,8 @@ 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,13 +7570,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);
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] KVM: X86: Kill off ctxt->ud
  2021-05-25  3:02 [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Wanpeng Li
@ 2021-05-25  3:02 ` Wanpeng Li
  2021-05-26 17:52 ` [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Sean Christopherson
  1 sibling, 0 replies; 4+ messages in thread
From: Wanpeng Li @ 2021-05-25  3:02 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

From: Wanpeng Li <wanpengli@tencent.com>

ctxt->ud is consumed only by x86_decode_insn(), we can kill it off by passing 
emulation_type to x86_decode_insn() and dropping ctxt->ud altogether. Tracking 
that info in ctxt for literally one call is silly.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/emulate.c     | 5 +++--
 arch/x86/kvm/kvm_emulate.h | 3 +--
 arch/x86/kvm/x86.c         | 4 +---
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 8a0ccdb..5e5de05 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -5111,7 +5111,7 @@ static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
 	return rc;
 }
 
-int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
+int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
 {
 	int rc = X86EMUL_CONTINUE;
 	int mode = ctxt->mode;
@@ -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 &
diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
index f016838..3e870bf 100644
--- a/arch/x86/kvm/kvm_emulate.h
+++ b/arch/x86/kvm/kvm_emulate.h
@@ -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;
@@ -491,7 +490,7 @@ enum x86_intercept {
 #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
 #endif
 
-int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
+int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type);
 bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);
 #define EMULATION_FAILED -1
 #define EMULATION_OK 0
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3c109d3..7ea9424 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7559,8 +7559,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,
@@ -7570,7 +7568,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;
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context
  2021-05-25  3:02 [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Wanpeng Li
  2021-05-25  3:02 ` [PATCH v2 2/2] KVM: X86: Kill off ctxt->ud Wanpeng Li
@ 2021-05-26 17:52 ` Sean Christopherson
  2021-05-27  5:01   ` Wanpeng Li
  1 sibling, 1 reply; 4+ messages in thread
From: Sean Christopherson @ 2021-05-26 17:52 UTC (permalink / raw)
  To: Wanpeng Li
  Cc: linux-kernel, kvm, Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

On Mon, May 24, 2021, Wanpeng Li wrote:
> From: Wanpeng Li <wanpengli@tencent.com>
> 
> 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 the 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 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: syzbot+71271244f206d17f6441@syzkaller.appspotmail.com
> Fixes: 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)
> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> ---
> v1 -> v2:
>  * move the second part emulation context initialization into init_emulate_ctxt()
> 
>  arch/x86/kvm/x86.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index bed7b53..3c109d3 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7228,6 +7228,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
>  	BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
>  	BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
>  
> +	ctxt->interruptibility = 0;
> +	ctxt->have_exception = false;
> +	ctxt->exception.vector = -1;
> +	ctxt->perm_ok = false;
> +
>  	init_decode_cache(ctxt);
>  	vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
>  }
> @@ -7554,6 +7559,8 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
>  
>  	init_emulate_ctxt(vcpu);
>  
> +	ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;

Heh, you sent the delta relative to v1.  To avoid confusion, can you post v3
with this squashed in?

Thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context
  2021-05-26 17:52 ` [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Sean Christopherson
@ 2021-05-27  5:01   ` Wanpeng Li
  0 siblings, 0 replies; 4+ messages in thread
From: Wanpeng Li @ 2021-05-27  5:01 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: LKML, kvm, Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

On Thu, 27 May 2021 at 01:52, Sean Christopherson <seanjc@google.com> wrote:
>
> On Mon, May 24, 2021, Wanpeng Li wrote:
> > From: Wanpeng Li <wanpengli@tencent.com>
> >
> > 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 the 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 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: syzbot+71271244f206d17f6441@syzkaller.appspotmail.com
> > Fixes: 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)
> > Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> > ---
> > v1 -> v2:
> >  * move the second part emulation context initialization into init_emulate_ctxt()
> >
> >  arch/x86/kvm/x86.c | 14 +++++++-------
> >  1 file changed, 7 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index bed7b53..3c109d3 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -7228,6 +7228,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
> >       BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
> >       BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
> >
> > +     ctxt->interruptibility = 0;
> > +     ctxt->have_exception = false;
> > +     ctxt->exception.vector = -1;
> > +     ctxt->perm_ok = false;
> > +
> >       init_decode_cache(ctxt);
> >       vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
> >  }
> > @@ -7554,6 +7559,8 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
> >
> >       init_emulate_ctxt(vcpu);
> >
> > +     ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
>
> Heh, you sent the delta relative to v1.  To avoid confusion, can you post v3
> with this squashed in?

Do it in v3.

    Wanpeng

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-27  5:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-25  3:02 [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Wanpeng Li
2021-05-25  3:02 ` [PATCH v2 2/2] KVM: X86: Kill off ctxt->ud Wanpeng Li
2021-05-26 17:52 ` [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Sean Christopherson
2021-05-27  5:01   ` Wanpeng Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).