mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wanpeng Li <kernellwp@gmail.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	"Wanpeng Li" <wanpeng.li@hotmail.com>,
	"Nadav Amit" <nadav.amit@gmail.com>,
	"Dmitry Vyukov" <dvyukov@google.com>
Subject: [PATCH v2] KVM: VMX: Fix rflags cache during vCPU reset
Date: Mon, 20 Nov 2017 14:52:21 -0800	[thread overview]
Message-ID: <1511218341-6221-1-git-send-email-wanpeng.li@hotmail.com> (raw)

From: Wanpeng Li <wanpeng.li@hotmail.com>

Reported by syzkaller:

   *** Guest State ***
   CR0: actual=0x0000000080010031, shadow=0x0000000060000010, gh_mask=fffffffffffffff7
   CR4: actual=0x0000000000002061, shadow=0x0000000000000000, gh_mask=ffffffffffffe8f1
   CR3 = 0x000000002081e000
   RSP = 0x000000000000fffa  RIP = 0x0000000000000000
   RFLAGS=0x00023000         DR7 = 0x00000000000000
          ^^^^^^^^^^         
   ------------[ cut here ]------------
   WARNING: CPU: 6 PID: 24431 at /home/kernel/linux/arch/x86/kvm//x86.c:7302 kvm_arch_vcpu_ioctl_run+0x651/0x2ea0 [kvm]
   CPU: 6 PID: 24431 Comm: reprotest Tainted: G        W  OE   4.14.0+ #26
   RIP: 0010:kvm_arch_vcpu_ioctl_run+0x651/0x2ea0 [kvm]
   RSP: 0018:ffff880291d179e0 EFLAGS: 00010202
   Call Trace:
    kvm_vcpu_ioctl+0x479/0x880 [kvm]
    do_vfs_ioctl+0x142/0x9a0
    SyS_ioctl+0x74/0x80
    entry_SYSCALL_64_fastpath+0x23/0x9a

The BUG is triggered while doing:

*(uint32_t*)0x20afd000 = (uint32_t)0xf0403;
*(uint32_t*)0x20afd004 = (uint32_t)0x0;
*(uint64_t*)0x20afd008 = (uint64_t)0x65;
*(uint64_t*)0x20afd010 = (uint64_t)0x5;
*(uint64_t*)0x20afd018 = (uint64_t)0xfffffffffffffffe;
*(uint64_t*)0x20afd020 = (uint64_t)0xfff;
*(uint64_t*)0x20afd038 = (uint64_t)0x2;
*(uint64_t*)0x20afd040 = (uint64_t)0x2;
r[15] = syscall(__NR_ioctl, r[4], 0x4048ae9bul, 0x20afd000ul);

i.e. KVM_SET_GUEST_DEBUG ioctl with

struct kvm_guest_debug {
    .control = 0xf0403,
    .pad = 0,
    .arch = {
        .debugreg[0] = 0x65,
        .debugreg[1] = 0x5,
        .debugreg[2] = 0xfffffffffffffffe,
        .debugreg[3] = 0xfff,
        .debugreg[6] = 0x2,
        .debugreg[7] = 0x2 
    }

The syzkaller testcase tries to setup the processor specific debug registers 
and configure vCPU for handling guest debug events through KVM_SET_GUEST_DEBUG.
The KVM_SET_GUEST_DEBUG ioctl will get and set rflags in order to set TF bit 
if single step is needed. All regs' caches are reset to avail and GUEST_RFLAGS 
vmcs field is reset to 0x2 during vCPU reset. However, the cache of rflags is 
not reset during vCPU reset. The function vmx_get_rflags() returns an unreset 
rflags cache value since the cache is marked avail, it is 0 after boot. Vmentry 
fails if the rflags reserved bit 1 is 0.

This patch fixes it by resetting both the GUEST_RFLAGS vmcs field and its cache 
to 0x2 during vCPU reset.

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Tested-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Nadav Amit <nadav.amit@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
 arch/x86/kvm/vmx.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index b348920..131fa1c 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -5590,6 +5590,7 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
 		vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
 	}
 
+	kvm_set_rflags(vcpu, 2);
 	vmcs_writel(GUEST_RFLAGS, 0x02);
 	kvm_rip_write(vcpu, 0xfff0);
 
-- 
2.7.4

             reply	other threads:[~2017-11-20 22:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-20 22:52 Wanpeng Li [this message]
2017-11-20 23:09 ` Paolo Bonzini
2017-11-21  0:34   ` Wanpeng Li
2017-12-05  0:28     ` Jim Mattson
2017-12-05  0:53       ` Wanpeng Li
2017-12-05 13:54         ` Paolo Bonzini
2017-12-05 17:53           ` Jim Mattson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1511218341-6221-1-git-send-email-wanpeng.li@hotmail.com \
    --to=kernellwp@gmail.com \
    --cc=dvyukov@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nadav.amit@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=wanpeng.li@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®