linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* kvm: GPF in kvm_lapic_set_tpr
@ 2016-06-21 13:18 Dmitry Vyukov
  2016-06-21 14:12 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Vyukov @ 2016-06-21 13:18 UTC (permalink / raw)
  To: Paolo Bonzini, mtosatti, yoshikawa_takuya_b1, guangrong.xiao,
	Steve Rutherford, rkrcmar, KVM list, LKML
  Cc: syzkaller, Alexander Potapenko, Sasha Levin, Kostya Serebryany

Hello,

The following program triggers GPF in kvm_lapic_set_tpr:

kasan: CONFIG_KASAN_INLINE enabled
kasan: GPF could be caused by NULL-ptr deref or user memory access
general protection fault: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN
Modules linked in:
CPU: 0 PID: 7683 Comm: syz-executor Not tainted 4.7.0-rc4+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
task: ffff8800300b46c0 ti: ffff880030e78000 task.ti: ffff880030e78000
RIP: 0010:[<ffffffff811269be>]
  [<ffffffff811269be>] kvm_lapic_set_tpr+0x5e/0xe0 arch/x86/kvm/lapic.c:1648
RSP: 0018:ffff880030e7fa40  EFLAGS: 00010202
RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000
RDX: 0000000000000016 RSI: 0000000000000000 RDI: 00000000000000b0
RBP: ffff880030e7fa58 R08: ffffed0006142069 R09: ffff880030a102c0
R10: ffff880030a10347 R11: ffffed0006142069 R12: 0000000000000000
R13: ffff880030a10040 R14: 0000000000000000 R15: dffffc0000000000
FS:  00007fc65886c700(0000) GS:ffff88003ec00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fc65884b000 CR3: 00000000315cd000 CR4: 00000000000026f0
Stack:
 ffff880030a10040 0000000000000000 ffff880030a10040 ffff880030e7fa78
 ffffffff8109496c 0000000000000001 0000000000000000 ffff880030e7faa0
 ffffffff81094a22 ffff880030a10040 1ffff100061cff57 ffff880030e7fb58
Call Trace:
 [<ffffffff8109496c>] kvm_set_cr8.part.135+0x2c/0xb0 arch/x86/kvm/x86.c:809
 [<ffffffff81094a22>] kvm_set_cr8+0x32/0x50 arch/x86/kvm/x86.c:806
 [<ffffffff81178dd4>] vmx_vcpu_reset+0x114/0xb60 arch/x86/kvm/vmx.c:4996
 [<ffffffff810b91b0>] kvm_vcpu_reset+0x410/0x580 arch/x86/kvm/x86.c:7470
 [<ffffffff810b9355>] kvm_arch_vcpu_setup+0x35/0x60 arch/x86/kvm/x86.c:7393
 [<     inline     >] kvm_vm_ioctl_create_vcpu
arch/x86/kvm/../../../virt/kvm/kvm_main.c:2355
 [<ffffffff8106b9f2>] kvm_vm_ioctl+0x582/0x10d0
arch/x86/kvm/../../../virt/kvm/kvm_main.c:2839
 [<     inline     >] vfs_ioctl fs/ioctl.c:43
 [<ffffffff818510bc>] do_vfs_ioctl+0x18c/0xff0 fs/ioctl.c:674
 [<     inline     >] SYSC_ioctl fs/ioctl.c:689
 [<ffffffff81851faf>] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:680
 [<ffffffff86a96fc0>] entry_SYSCALL_64_fastpath+0x23/0xc1
arch/x86/entry/entry_64.S:207
Code: 92 00 00 00 4d 8b a4 24 60 03 00 00 48 b8 00 00 00 00 00 fc ff
df c1 e3 04 0f b6 db 49 8d bc 24 b0 00 00 00 48 89 fa 48 c1 ea 03 <80>
3c 02 00 75 5e 4d 8b ac 24 b0 00 00 00 48 b8 00 00 00 00 00
RIP  [<ffffffff811269be>] kvm_lapic_set_tpr+0x5e/0xe0 arch/x86/kvm/lapic.c:1648
 RSP <ffff880030e7fa40>
---[ end trace a57f94f7314716fc ]---


Run in a parallel loop:

// autogenerated by syzkaller (http://github.com/google/syzkaller)
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

long r[9];

void* thr(void* arg)
{
  switch ((long)arg) {
  case 0:
    r[0] = syscall(SYS_mmap, 0x20000000ul, 0x16000ul, 0x3ul, 0x32ul,
                   0xfffffffffffffffful, 0x0ul);
    break;
  case 1:
    r[2] = syscall(SYS_open, "/dev/kvm", 0x540ul, 0, 0, 0);
    break;
  case 2:
    r[3] = syscall(SYS_ioctl, r[2], 0xae01ul, 0x0ul, 0, 0, 0);
    break;
  case 3:
    r[4] = syscall(SYS_ioctl, r[3], 0xae41ul, 0x1ul, 0, 0, 0);
    break;
  case 4:
    r[5] = syscall(SYS_ioctl, r[4], 0xae80ul, 0, 0, 0, 0);
    break;
  case 5:
    r[6] = syscall(SYS_ioctl, r[4], 0xae80ul, 0, 0, 0, 0);
    break;
  case 6:
    r[7] = syscall(SYS_ioctl, r[4], 0xae80ul, 0, 0, 0, 0);
    break;
  case 7:
    r[8] =
        syscall(SYS_ioctl, r[4], 0x8040ae9ful, 0x20014fe4ul, 0, 0, 0);
    break;
  }
  return 0;
}

int main()
{
  long i;
  pthread_t th[8];

  syscall(SYS_mmap, 0x20000000ul, 0x16000ul, 0x3ul, 0x32ul,
                   0xfffffffffffffffful, 0x0ul);

  srand(getpid());
  memset(r, -1, sizeof(r));
  for (i = 0; i < 8; i++) {
    pthread_create(&th[i], 0, thr, (void*)i);
  }
  for (i = 0; i < 8; i++) {
    pthread_join(th[i], 0);
  }
  for (i = 0; i < 8; i++) {
    pthread_create(&th[i], 0, thr, (void*)i);
    if ((rand() % 2) == 0)
      usleep(rand() % 10000);
  }
  for (i = 0; i < 8; i++) {
    pthread_join(th[i], 0);
  }
  return 0;
}


On commit 67016f6cdfd079e632bbc49e33178b2d558c120a (Jun 20).

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

* Re: kvm: GPF in kvm_lapic_set_tpr
  2016-06-21 13:18 kvm: GPF in kvm_lapic_set_tpr Dmitry Vyukov
@ 2016-06-21 14:12 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2016-06-21 14:12 UTC (permalink / raw)
  To: Dmitry Vyukov, mtosatti, yoshikawa_takuya_b1, guangrong.xiao,
	Steve Rutherford, rkrcmar, KVM list, LKML
  Cc: syzkaller, Alexander Potapenko, Sasha Levin, Kostya Serebryany,
	Peter Zijlstra



On 21/06/2016 15:18, Dmitry Vyukov wrote:
>  [<ffffffff811269be>] kvm_lapic_set_tpr+0x5e/0xe0 arch/x86/kvm/lapic.c:1648
>  [<ffffffff8109496c>] kvm_set_cr8.part.135+0x2c/0xb0 arch/x86/kvm/x86.c:809
>  [<ffffffff81094a22>] kvm_set_cr8+0x32/0x50 arch/x86/kvm/x86.c:806
>  [<ffffffff81178dd4>] vmx_vcpu_reset+0x114/0xb60 arch/x86/kvm/vmx.c:4996
>  [<ffffffff810b91b0>] kvm_vcpu_reset+0x410/0x580 arch/x86/kvm/x86.c:7470
>  [<ffffffff810b9355>] kvm_arch_vcpu_setup+0x35/0x60 arch/x86/kvm/x86.c:7393
>  [<     inline     >] kvm_vm_ioctl_create_vcpu virt/kvm/kvm_main.c:2355
>  [<ffffffff8106b9f2>] kvm_vm_ioctl+0x582/0x10d0 virt/kvm/kvm_main.c:2839
>  [<     inline     >] vfs_ioctl fs/ioctl.c:43
>  [<ffffffff818510bc>] do_vfs_ioctl+0x18c/0xff0 fs/ioctl.c:674
>  [<     inline     >] SYSC_ioctl fs/ioctl.c:689
>  [<ffffffff81851faf>] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:680
>  [<ffffffff86a96fc0>] entry_SYSCALL_64_fastpath+0x23/0xc1

The bug is not in KVM.  Modulo the shuffling of system calls from
threading, the program is equivalent to this:

long r[7];

int main(void)
{
        struct kvm_vcpu_events ve;
        r[0] = open("/dev/kvm", O_RDONLY);
        r[1] = ioctl(r[0], KVM_CREATE_VM, 0x0ul, 0, 0, 0);
        r[2] = ioctl(r[1], KVM_CREATE_VCPU, 0x1ul, 0, 0, 0);
        r[3] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0);
        r[4] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0);
        r[5] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0);
        r[6] = ioctl(r[2], KVM_GET_VCPU_EVENTS, &ve, 0, 0, 0);
        return 0;
}

However, it is easy to see that there is a clear critical path in the
bug, so that the threading must be completely irrelevant:

1) r[1] was valid in KVM_CREATE_VCPU, so KVM_CREATE_VCPU started after
the assignment of KVM_CREATE_VM

2) r[0] was valid in KVM_CREATE_VM, so KVM_CREATE_VM started after the
assignment of open("/dev/kvm")

3) the assignment of r[2] must not have happened because of the
backtrace, so KVM_CREATE_VCPU has not finished and the other ioctls will
not get to KVM.

The GP _could_ happen because of a bug, because lapic_in_kernel attempts
to skip a NULL check using a static key.  But the static key is
incremented before the point that is in the backtrace:

kvm_vm_ioctl_create_vcpu (virt/kvm/kvm_main.c)
 kvm_arch_vcpu_create (arch/x86/kvm/x86.c)
  vmx_create_vcpu (arch/x86/kvm/vmx.c)
   kvm_vcpu_init (virt/kvm/kvm_main.c)
    kvm_arch_vcpu_init (arch/x86/kvm/x86.c)
     static_key_slow_inc
    <-
   <-
  <-
 <-
 kvm_arch_vcpu_setup (arch/x86/kvm/x86.c)
  kvm_vcpu_reset (arch/x86/kvm/x86.c)
   vmx_vcpu_reset (arch/x86/kvm/vmx.c)
    kvm_set_cr8 (arch/x86/kvm/x86.c)

*mumble mumble*

Ok, the bug shows up when you do two concurrent calls to
static_key_slow_inc and is caused by commit 706249c22.  Patch on its way.

Paolo

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

end of thread, other threads:[~2016-06-21 14:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-21 13:18 kvm: GPF in kvm_lapic_set_tpr Dmitry Vyukov
2016-06-21 14:12 ` Paolo Bonzini

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).