2019-01-21 17:23:35

by Alexander Popov

[permalink] [raw]
Subject: [PATCH v2] KVM: x86: Fix single-step debugging

The single-step debugging of KVM guests on x86 is broken: if we run
gdb 'stepi' command at the breakpoint when the guest interrupts are
enabled, RIP always jumps to native_apic_mem_write(). Then other
nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
be called without X86_EFLAGS_TF set.

Let's fix it. Please consider that for -stable.

Signed-off-by: Alexander Popov <[email protected]>
---
arch/x86/kvm/x86.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index f049ecf..9686068 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
toggle_interruptibility(vcpu, ctxt->interruptibility);
vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
kvm_rip_write(vcpu, ctxt->eip);
- if (r == EMULATE_DONE &&
- (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
+ if (r == EMULATE_DONE && ctxt->tf)
kvm_vcpu_do_singlestep(vcpu, &r);
if (!ctxt->have_exception ||
exception_type(ctxt->exception.vector) == EXCPT_TRAP)
--
2.7.4



2019-01-21 14:25:48

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v2] KVM: x86: Fix single-step debugging

On Mon, Jan 21, 2019 at 03:48:40PM +0300, Alexander Popov wrote:
> The single-step debugging of KVM guests on x86 is broken: if we run
> gdb 'stepi' command at the breakpoint when the guest interrupts are
> enabled, RIP always jumps to native_apic_mem_write(). Then other
> nasty effects follow.
>
> Long investigation showed that on Jun 7, 2017 the
> commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
> introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
> be called without X86_EFLAGS_TF set.
>
> Let's fix it. Please consider that for -stable.
>
> Signed-off-by: Alexander Popov <[email protected]>
> ---
> arch/x86/kvm/x86.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)

<formletter>

This is not the correct way to submit patches for inclusion in the
stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

2019-01-24 08:43:22

by Alexander Popov

[permalink] [raw]
Subject: Re: [PATCH v2] KVM: x86: Fix single-step debugging

On 21.01.2019 15:48, Alexander Popov wrote:
> The single-step debugging of KVM guests on x86 is broken: if we run
> gdb 'stepi' command at the breakpoint when the guest interrupts are
> enabled, RIP always jumps to native_apic_mem_write(). Then other
> nasty effects follow.
>
> Long investigation showed that on Jun 7, 2017 the
> commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
> introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
> be called without X86_EFLAGS_TF set.
>
> Let's fix it. Please consider that for -stable.

Hello everyone!

Just a friendly ping about this fix.
Looking forward to the feedback.

Best regards,
Alexander

2019-01-25 17:41:04

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH v2] KVM: x86: Fix single-step debugging

On 21/01/19 15:24, Greg KH wrote:
> On Mon, Jan 21, 2019 at 03:48:40PM +0300, Alexander Popov wrote:
>> The single-step debugging of KVM guests on x86 is broken: if we run
>> gdb 'stepi' command at the breakpoint when the guest interrupts are
>> enabled, RIP always jumps to native_apic_mem_write(). Then other
>> nasty effects follow.
>>
>> Long investigation showed that on Jun 7, 2017 the
>> commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
>> introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
>> be called without X86_EFLAGS_TF set.
>>
>> Let's fix it. Please consider that for -stable.
>>
>> Signed-off-by: Alexander Popov <[email protected]>
>> ---
>> arch/x86/kvm/x86.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> <formletter>
>
> This is not the correct way to submit patches for inclusion in the
> stable kernel tree. Please read:
> https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
> for how to do this properly.
>
> </formletter>
>

Yup, will take care of doing this properly. :)

Paolo

2019-01-25 17:54:36

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH v2] KVM: x86: Fix single-step debugging

On 21/01/19 13:48, Alexander Popov wrote:
> The single-step debugging of KVM guests on x86 is broken: if we run
> gdb 'stepi' command at the breakpoint when the guest interrupts are
> enabled, RIP always jumps to native_apic_mem_write(). Then other
> nasty effects follow.
>
> Long investigation showed that on Jun 7, 2017 the
> commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
> introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
> be called without X86_EFLAGS_TF set.
>
> Let's fix it. Please consider that for -stable.
>
> Signed-off-by: Alexander Popov <[email protected]>
> ---
> arch/x86/kvm/x86.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index f049ecf..9686068 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
> toggle_interruptibility(vcpu, ctxt->interruptibility);
> vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
> kvm_rip_write(vcpu, ctxt->eip);
> - if (r == EMULATE_DONE &&
> - (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
> + if (r == EMULATE_DONE && ctxt->tf)
> kvm_vcpu_do_singlestep(vcpu, &r);
> if (!ctxt->have_exception ||
> exception_type(ctxt->exception.vector) == EXCPT_TRAP)
>

Queued, with Cc to stable and the right "Fixes" trailer.

Paolo

2019-02-05 23:35:57

by Jim Mattson

[permalink] [raw]
Subject: Re: [PATCH v2] KVM: x86: Fix single-step debugging

On Fri, Jan 25, 2019 at 9:52 AM Paolo Bonzini <[email protected]> wrote:
>
> On 21/01/19 13:48, Alexander Popov wrote:
> > The single-step debugging of KVM guests on x86 is broken: if we run
> > gdb 'stepi' command at the breakpoint when the guest interrupts are
> > enabled, RIP always jumps to native_apic_mem_write(). Then other
> > nasty effects follow.
> >
> > Long investigation showed that on Jun 7, 2017 the
> > commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall")
> > introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can
> > be called without X86_EFLAGS_TF set.
> >
> > Let's fix it. Please consider that for -stable.
> >
> > Signed-off-by: Alexander Popov <[email protected]>
> > ---
> > arch/x86/kvm/x86.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index f049ecf..9686068 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
> > toggle_interruptibility(vcpu, ctxt->interruptibility);
> > vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
> > kvm_rip_write(vcpu, ctxt->eip);
> > - if (r == EMULATE_DONE &&
> > - (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
> > + if (r == EMULATE_DONE && ctxt->tf)
> > kvm_vcpu_do_singlestep(vcpu, &r);
> > if (!ctxt->have_exception ||
> > exception_type(ctxt->exception.vector) == EXCPT_TRAP)
> >
>
> Queued, with Cc to stable and the right "Fixes" trailer.
>
> Paolo

This patch seems to imply that userspace is expected to set the
guest's RFLAGS.TF when requesting KVM_GUEST_SINGLESTEP, but the
documentation for KVM_GUEST_SINGLESTEP is not clear about this. Can
someone clarify how KVM_GUEST_SINGLESTEP is supposed to work?