1 From bbeac2830f4de270bb48141681cb730aadf8dce1 Mon Sep 17 00:00:00 2001
2 From: Wanpeng Li <kernellwp@gmail.com>
3 Date: Wed, 9 Aug 2017 22:33:12 -0700
4 Subject: KVM: X86: Fix residual mmio emulation request to userspace
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 From: Wanpeng Li <kernellwp@gmail.com>
11 commit bbeac2830f4de270bb48141681cb730aadf8dce1 upstream.
13 Reported by syzkaller:
15 The kvm-intel.unrestricted_guest=0
17 WARNING: CPU: 5 PID: 1014 at /home/kernel/data/kvm/arch/x86/kvm//x86.c:7227 kvm_arch_vcpu_ioctl_run+0x38b/0x1be0 [kvm]
18 CPU: 5 PID: 1014 Comm: warn_test Tainted: G W OE 4.13.0-rc3+ #8
19 RIP: 0010:kvm_arch_vcpu_ioctl_run+0x38b/0x1be0 [kvm]
22 ? rcu_read_lock_sched_held+0x79/0x80
23 ? kmem_cache_free+0x2f2/0x350
24 kvm_vcpu_ioctl+0x340/0x700 [kvm]
25 ? kvm_vcpu_ioctl+0x340/0x700 [kvm]
27 do_vfs_ioctl+0xa4/0x6a0
30 entry_SYSCALL_64_fastpath+0x23/0xc2
31 ? __this_cpu_preempt_check+0x13/0x20
33 The syszkaller folks reported a residual mmio emulation request to userspace
34 due to vm86 fails to emulate inject real mode interrupt(fails to read CS) and
35 incurs a triple fault. The vCPU returns to userspace with vcpu->mmio_needed == true
36 and KVM_EXIT_SHUTDOWN exit reason. However, the syszkaller testcase constructs
37 several threads to launch the same vCPU, the thread which lauch this vCPU after
38 the thread whichs get the vcpu->mmio_needed == true and KVM_EXIT_SHUTDOWN will
47 #include <sys/types.h>
52 #include <linux/kvm.h>
61 res = ioctl(kvmcpu, KVM_RUN, 0);
62 printf("ret1=%d exit_reason=%d suberror=%d\n",
63 res, run->exit_reason, run->internal.suberror);
72 kvm = open("/dev/kvm", O_RDWR);
73 kvmvm = ioctl(kvm, KVM_CREATE_VM, 0);
74 kvmcpu = ioctl(kvmvm, KVM_CREATE_VCPU, 0);
75 run = (struct kvm_run*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, kvmcpu, 0);
77 for (i = 0; i < 4; i++) {
78 pthread_create(&th[i], 0, thr, 0);
79 usleep(rand() % 10000);
81 for (i = 0; i < 4; i++)
82 pthread_join(th[i], 0);
96 while (waitpid(pid, &status, __WALL) != pid) {}
101 This patch fixes it by resetting the vcpu->mmio_needed once we receive
102 the triple fault to avoid the residue.
104 Reported-by: Dmitry Vyukov <dvyukov@google.com>
105 Tested-by: Dmitry Vyukov <dvyukov@google.com>
106 Cc: Paolo Bonzini <pbonzini@redhat.com>
107 Cc: Radim Krčmář <rkrcmar@redhat.com>
108 Cc: Dmitry Vyukov <dvyukov@google.com>
109 Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
110 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
111 Cc: Zubin Mithra <zsm@chromium.org>
112 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
115 arch/x86/kvm/vmx.c | 1 +
116 arch/x86/kvm/x86.c | 1 +
117 2 files changed, 2 insertions(+)
119 --- a/arch/x86/kvm/vmx.c
120 +++ b/arch/x86/kvm/vmx.c
121 @@ -5574,6 +5574,7 @@ static int handle_external_interrupt(str
122 static int handle_triple_fault(struct kvm_vcpu *vcpu)
124 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
125 + vcpu->mmio_needed = 0;
129 --- a/arch/x86/kvm/x86.c
130 +++ b/arch/x86/kvm/x86.c
131 @@ -6478,6 +6478,7 @@ static int vcpu_enter_guest(struct kvm_v
133 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
134 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
135 + vcpu->mmio_needed = 0;