Re: kvm: GPF in native_set_debugreg

From: Jeff Merkey
Date: Fri Jan 08 2016 - 16:15:52 EST


On 1/8/16, Jeff Merkey <linux.mdb@xxxxxxxxx> wrote:
> On 1/8/16, Dmitry Vyukov <dvyukov@xxxxxxxxxx> wrote:
>> Hello,
>>
>> The following program triggers GPF in native_set_debugreg if run in
>> a parallel loop:
>>
>> // autogenerated by syzkaller (http://github.com/google/syzkaller)
>> #include <unistd.h>
>> #include <sys/syscall.h>
>> #include <string.h>
>> #include <stdint.h>
>> #include <pthread.h>
>>
>> long r[8];
>>
>> void *thr(void *arg)
>> {
>> switch ((long)arg) {
>> case 0:
>> r[0] = syscall(SYS_mmap, 0x20000000ul, 0x7000ul,
>> 0x3ul, 0x32ul, 0xfffffffffffffffful, 0x0ul);
>> break;
>> case 1:
>> memcpy((void*)0x200008ab,
>> "\x2f\x64\x65\x76\x2f\x6b\x76\x6d", 8);
>> r[2] = syscall(SYS_open, 0x200008abul, 0x0ul, 0x0ul, 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, 0x7ul, 0, 0,
>> 0);
>> break;
>> case 4:
>> r[5] = syscall(SYS_ioctl, r[4], 0xae80ul, 0, 0, 0, 0);
>> break;
>> case 5:
>> memcpy((void*)0x20001000,
>> "\x5d\x6a\x6b\xe8\x57\x3b\x4b\x7e\xcf\x0d\xa1\x72\xa3\x4a\x29\x0c\xfc\x6d\x44\x00\xa7\x52\xc7\xd8\x00\xdb\x89\x9d\x78\xb5\x54\x6b\x6b\x13\x1c\xe9\x5e\xd3\x0e\x40\x6f\xb4\x66\xf7\x5b\xe3\x36\xcb",
>> 48);
>> r[7] = syscall(SYS_ioctl, r[4], 0x4080aea2ul,
>> 0x20001000ul, 0, 0, 0);
>> break;
>> }
>> return 0;
>> }
>>
>> int main()
>> {
>> long i;
>> pthread_t th[6];
>>
>> memset(r, -1, sizeof(r));
>> for (i = 0; i < 6; i++) {
>> pthread_create(&th[i], 0, thr, (void*)i);
>> usleep(10000);
>> }
>> for (i = 0; i < 6; i++) {
>> pthread_create(&th[i], 0, thr, (void*)i);
>> if (i%2==0)
>> usleep(10000);
>> }
>> usleep(100000);
>> return 0;
>> }
>>
>>
>> kasan: GPF could be caused by NULL-ptr deref or user memory
>> accessgeneral protection fault: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN
>> Modules linked in:
>> CPU: 0 PID: 8491 Comm: syz-executor Not tainted 4.4.0-rc8+ #217
>> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs
>> 01/01/2011
>> task: ffff8800631c8000 ti: ffff880065380000 task.ti: ffff880065380000
>> RIP: 0010:[<ffffffff8125fa5e>] [<ffffffff8125fa5e>]
>> native_set_debugreg+0x2e/0x40
>> RSP: 0018:ffff880065387a88 EFLAGS: 00000097
>> RAX: ffff8800631c8000 RBX: ffff880063423478 RCX: ffff8800631c87c8
>> RDX: 0000000000000000 RSI: 400ed35ee91c136b RDI: 0000000000000006
>> RBP: ffff880065387a88 R08: 0000000000000001 R09: 0000000000000002
>> R10: 0000000000000001 R11: 0000000000000001 R12: 1ffffffff0e09f6d
>> R13: ffff880063420030 R14: ffff880063420000 R15: ffff88006342002c
>> FS: 00007f8ed68a7700(0000) GS:ffff88003ec00000(0000)
>> knlGS:0000000000000000
>> CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
>> CR2: 0000000001710000 CR3: 000000006486d000 CR4: 00000000000026f0
>> DR0: 7e4b3b57e86b6a5d DR1: 0c294aa372a10dcf DR2: d8c752a700446dfc
>> DR3: 6b54b5789d89db00 DR6: 00000000ffff0ff0 DR7: 0000000000000400
>> Stack:
>> ffff880065387c10 ffffffff8106c3a6 ffffffff81067de4 ffff880065387b38
>> 0000000000000000 00000000002794c8 ffff880065380008 0000001b540f2184
>> 0000000000000000 1ffff1000ca70001 ffff880065380000 ffff880063421ac8
>> Call Trace:
>> [< inline >] set_debugreg ./arch/x86/include/asm/paravirt.h:45
>> [< inline >] vcpu_enter_guest arch/x86/kvm/x86.c:6532
>> [< inline >] vcpu_run arch/x86/kvm/x86.c:6660
>> [<ffffffff8106c3a6>] kvm_arch_vcpu_ioctl_run+0x47b6/0x5820
>> arch/x86/kvm/x86.c:6818
>> [<ffffffff8101cf61>] kvm_vcpu_ioctl+0x5f1/0xd00
>> arch/x86/kvm/../../../virt/kvm/kvm_main.c:2375
>> [< inline >] vfs_ioctl fs/ioctl.c:43
>> [<ffffffff817b36b1>] do_vfs_ioctl+0x681/0xe40 fs/ioctl.c:607
>> [< inline >] SYSC_ioctl fs/ioctl.c:622
>> [<ffffffff817b3eff>] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:613
>> [<ffffffff85e745b6>] entry_SYSCALL_64_fastpath+0x16/0x7a
>> arch/x86/entry/entry_64.S:185
>> Code: 07 48 89 e5 77 27 89 ff ff 24 fd c0 25 f3 85 0f 23 fe 5d c3 0f
>> 23 c6 5d c3 0f 23 ce 5d c3 0f 23 d6 5d c3 0f 23 de 5d c3 0f 23 f6 <5d>
>> c3 0f 0b 66 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 55 83 ff
>> RIP [<ffffffff8125fa5e>] native_set_debugreg+0x2e/0x40
>> ./arch/x86/include/asm/debugreg.h:65
>> RSP <ffff880065387a88>
>> ---[ end trace 94251fd7c32f126a ]---
>>
>>
>> On commit b06f3a168cdcd80026276898fd1fee443ef25743 (Jan 6).
>>
>
>
> The only place native_set_debugreg references memory is a jmpq
> instruction (6th instruction into the function) that handles the C
> switch statement then jumps to set the debugreg. RDI (edi) is set to
> 6, so someone was setting debugreg 6 (probably with garbage status).
> I guess this hardware GPF'd when someone tried to write to dr6.
>
> native_set_debugreg
> push rbp
> cmp 7,edi
> mov rsp,rbp
> ja +0x41
> move edi,edi
> jmpq ptr @(rdi,8)
>
> then several mov rsi, db#(0-7)
>
>
> Interesting it crashes when someone is writing to DR6. I have already
> reported this issue.
>
> Jeff
>


thread-debugreg6 is pointing off to invalid memory.


Jeff