Re: kvm: use-after-free in irq_bypass_register_consumer
From: Dmitry Vyukov
Date: Fri Mar 24 2017 - 09:59:00 EST
On Sat, Mar 4, 2017 at 5:24 PM, Dmitry Vyukov <dvyukov@xxxxxxxxxx> wrote:
> On Fri, Jan 27, 2017 at 10:26 PM, Dmitry Vyukov <dvyukov@xxxxxxxxxx> wrote:
>> Hello,
>>
>> The following program triggers use-after-free in irq_bypass_register_consumer:
>> https://bit.googleplex.com/raw/5875575541465088/5629499534213120/
>>
>> BUG: KASAN: use-after-free in irq_bypass_register_consumer+0x48c/0x4c0
>> virt/lib/irqbypass.c:198 at addr ffff88006b74b438
>> Read of size 8 by task a.out/24723
>> CPU: 1 PID: 24723 Comm: a.out Not tainted 4.10.0-rc5+ #201
>> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
>> Call Trace:
>> __dump_stack lib/dump_stack.c:15 [inline]
>> dump_stack+0x2ee/0x3ef lib/dump_stack.c:51
>> kasan_object_err+0x1c/0x70 mm/kasan/report.c:165
>> print_address_description mm/kasan/report.c:203 [inline]
>> kasan_report_error+0x17b/0x430 mm/kasan/report.c:287
>> kasan_report mm/kasan/report.c:307 [inline]
>> __asan_report_load8_noabort+0x3e/0x40 mm/kasan/report.c:328
>> irq_bypass_register_consumer+0x48c/0x4c0 virt/lib/irqbypass.c:198
>> kvm_irqfd_assign arch/x86/kvm/../../../virt/kvm/eventfd.c:417 [inline]
>> kvm_irqfd+0x130e/0x1e30 arch/x86/kvm/../../../virt/kvm/eventfd.c:578
>> kvm_vm_ioctl+0x107a/0x1d10 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3007
>> vfs_ioctl fs/ioctl.c:43 [inline]
>> do_vfs_ioctl+0x1bf/0x1790 fs/ioctl.c:683
>> SYSC_ioctl fs/ioctl.c:698 [inline]
>> SyS_ioctl+0x8f/0xc0 fs/ioctl.c:689
>> entry_SYSCALL_64_fastpath+0x1f/0xc2
>> RIP: 0033:0x4402b9
>> RSP: 002b:00007f7f07514d98 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
>> RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00000000004402b9
>> RDX: 0000000020ae3000 RSI: 000000004020ae76 RDI: 0000000000000007
>> RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
>> R10: 0000000000000000 R11: 0000000000000297 R12: 0000000000000000
>> R13: 0000000000000000 R14: 00007f7f075159c0 R15: 00007f7f07515700
>> Object at ffff88006b74b2c0, in cache kmalloc-512 size: 512
>> Allocated:
>> PID = 24723
>> [<ffffffff812b1506>] save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:57
>> [<ffffffff81a0cdd3>] save_stack+0x43/0xd0 mm/kasan/kasan.c:502
>> [<ffffffff81a0d09a>] set_track mm/kasan/kasan.c:514 [inline]
>> [<ffffffff81a0d09a>] kasan_kmalloc+0xaa/0xd0 mm/kasan/kasan.c:605
>> [<ffffffff81a098eb>] kmem_cache_alloc_trace+0x10b/0x670 mm/slab.c:3629
>> [<ffffffff8108b95f>] kmalloc include/linux/slab.h:490 [inline]
>> [<ffffffff8108b95f>] kzalloc include/linux/slab.h:636 [inline]
>> [<ffffffff8108b95f>] kvm_irqfd_assign
>> arch/x86/kvm/../../../virt/kvm/eventfd.c:296 [inline]
>> [<ffffffff8108b95f>] kvm_irqfd+0x16f/0x1e30
>> arch/x86/kvm/../../../virt/kvm/eventfd.c:578
>> [<ffffffff81081b4a>] kvm_vm_ioctl+0x107a/0x1d10
>> arch/x86/kvm/../../../virt/kvm/kvm_main.c:3007
>> [<ffffffff81aa41cf>] vfs_ioctl fs/ioctl.c:43 [inline]
>> [<ffffffff81aa41cf>] do_vfs_ioctl+0x1bf/0x1790 fs/ioctl.c:683
>> [<ffffffff81aa582f>] SYSC_ioctl fs/ioctl.c:698 [inline]
>> [<ffffffff81aa582f>] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:689
>> [<ffffffff841c9c81>] entry_SYSCALL_64_fastpath+0x1f/0xc2
>> Freed:
>> PID = 1556
>> [<ffffffff812b1506>] save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:57
>> [<ffffffff81a0cdd3>] save_stack+0x43/0xd0 mm/kasan/kasan.c:502
>> [<ffffffff81a0d70f>] set_track mm/kasan/kasan.c:514 [inline]
>> [<ffffffff81a0d70f>] kasan_slab_free+0x6f/0xb0 mm/kasan/kasan.c:578
>> [<ffffffff81a0b5c3>] __cache_free mm/slab.c:3505 [inline]
>> [<ffffffff81a0b5c3>] kfree+0xd3/0x250 mm/slab.c:3822
>> [<ffffffff8108b4f0>] irqfd_shutdown+0x150/0x1c0
>> arch/x86/kvm/../../../virt/kvm/eventfd.c:148
>> [<ffffffff814b4920>] process_one_work+0xbd0/0x1c10 kernel/workqueue.c:2098
>> [<ffffffff814b5b83>] worker_thread+0x223/0x1990 kernel/workqueue.c:2232
>> [<ffffffff814ce546>] kthread+0x326/0x3f0 kernel/kthread.c:227
>> [<ffffffff841c9ef1>] ret_from_fork+0x31/0x40 arch/x86/entry/entry_64.S:430
>>
>> On commit fd694aaa46c7ed811b72eb47d5eb11ce7ab3f7f1.
>
>
> Still happens on commit 0710f3ff91ecc4a715db6e4d0690472b13c4dac6:
>
> BUG: KASAN: use-after-free in irq_bypass_register_consumer+0x4cb/0x4e0
> virt/lib/irqbypass.c:198 at addr ffff88006777ddf8
> Read of size 8 by task a.out/15766
> CPU: 2 PID: 15766 Comm: a.out Not tainted 4.10.0+ #285
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Call Trace:
> __dump_stack lib/dump_stack.c:16 [inline]
> dump_stack+0x2fb/0x3fd lib/dump_stack.c:52
> kasan_object_err+0x1c/0x70 mm/kasan/report.c:166
> print_address_description mm/kasan/report.c:204 [inline]
> kasan_report_error mm/kasan/report.c:288 [inline]
> kasan_report.part.2+0x1b0/0x460 mm/kasan/report.c:310
> kasan_report mm/kasan/report.c:331 [inline]
> __asan_report_load8_noabort+0x29/0x30 mm/kasan/report.c:331
> irq_bypass_register_consumer+0x4cb/0x4e0 virt/lib/irqbypass.c:198
> kvm_irqfd_assign arch/x86/kvm/../../../virt/kvm/eventfd.c:417 [inline]
> kvm_irqfd+0x137d/0x1e20 arch/x86/kvm/../../../virt/kvm/eventfd.c:578
> kvm_vm_ioctl+0x108c/0x1d70 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3011
> vfs_ioctl fs/ioctl.c:45 [inline]
> do_vfs_ioctl+0x1bf/0x1790 fs/ioctl.c:685
> SYSC_ioctl fs/ioctl.c:700 [inline]
> SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
> entry_SYSCALL_64_fastpath+0x1f/0xc2
> RIP: 0033:0x4402b9
> RSP: 002b:00007fb73c4f9d98 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
> RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00000000004402b9
> RDX: 0000000020ae3000 RSI: 000000004020ae76 RDI: 0000000000000008
> RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
> R10: 0000000000000000 R11: 0000000000000297 R12: 0000000000000000
> R13: 0000000000000000 R14: 00007fb73c4fa9c0 R15: 00007fb73c4fa700
> Object at ffff88006777dc80, in cache kmalloc-512 size: 512
> Allocated:
> PID = 15766
> save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_kmalloc+0xaa/0xd0 mm/kasan/kasan.c:616
> kmem_cache_alloc_trace+0x10b/0x6e0 mm/slab.c:3638
> kmalloc include/linux/slab.h:490 [inline]
> kzalloc include/linux/slab.h:663 [inline]
> kvm_irqfd_assign arch/x86/kvm/../../../virt/kvm/eventfd.c:296 [inline]
> kvm_irqfd+0x16f/0x1e20 arch/x86/kvm/../../../virt/kvm/eventfd.c:578
> kvm_vm_ioctl+0x108c/0x1d70 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3011
> vfs_ioctl fs/ioctl.c:45 [inline]
> do_vfs_ioctl+0x1bf/0x1790 fs/ioctl.c:685
> SYSC_ioctl fs/ioctl.c:700 [inline]
> SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
> entry_SYSCALL_64_fastpath+0x1f/0xc2
> Freed:
> PID = 670
> save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_slab_free+0x6f/0xb0 mm/kasan/kasan.c:589
> __cache_free mm/slab.c:3514 [inline]
> kfree+0xd3/0x250 mm/slab.c:3831
> irqfd_shutdown+0x150/0x1c0 arch/x86/kvm/../../../virt/kvm/eventfd.c:148
> process_one_work+0xc06/0x1c40 kernel/workqueue.c:2096
> worker_thread+0x223/0x19f0 kernel/workqueue.c:2230
> kthread+0x334/0x400 kernel/kthread.c:229
> ret_from_fork+0x31/0x40 arch/x86/entry/entry_64.S:430
> Memory state around the buggy address:
> ffff88006777dc80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88006777dd00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>>ffff88006777dd80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ^
> ffff88006777de00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88006777de80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> ==================================================================
>
>
> Here is correct link for reproducer program:
> https://gist.githubusercontent.com/dvyukov/34114444518fa22baff19ae204cc46a6/raw/7826cbcd1cbc472dfa4972fe56371df3c94b70c7/gistfile1.txt
Still happens regularly.
On 093b995e3b55a0ae0670226ddfcb05bfbf0099ae (Mar 20):
==================================================================
BUG: KASAN: use-after-free in irq_bypass_register_consumer+0x4f0/0x500
virt/lib/irqbypass.c:198 at addr ffff88006648ec38
Read of size 8 by task a.out/6857
CPU: 3 PID: 6857 Comm: a.out Not tainted 4.11.0-rc3+ #366
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:16 [inline]
dump_stack+0x1b8/0x28d lib/dump_stack.c:52
kasan_object_err+0x1c/0x70 mm/kasan/report.c:166
print_address_description mm/kasan/report.c:210 [inline]
kasan_report_error mm/kasan/report.c:294 [inline]
kasan_report.part.2+0x1be/0x480 mm/kasan/report.c:316
kasan_report mm/kasan/report.c:337 [inline]
__asan_report_load8_noabort+0x29/0x30 mm/kasan/report.c:337
irq_bypass_register_consumer+0x4f0/0x500 virt/lib/irqbypass.c:198
kvm_irqfd_assign arch/x86/kvm/../../../virt/kvm/eventfd.c:417 [inline]
kvm_irqfd+0x1391/0x1d70 arch/x86/kvm/../../../virt/kvm/eventfd.c:572
kvm_vm_ioctl+0xe9c/0x1c60 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3012
vfs_ioctl fs/ioctl.c:45 [inline]
do_vfs_ioctl+0x1af/0x16d0 fs/ioctl.c:685
SYSC_ioctl fs/ioctl.c:700 [inline]
SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
entry_SYSCALL_64_fastpath+0x1f/0xc2
RIP: 0033:0x4402b9
RSP: 002b:00007f0564cc2d98 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00000000004402b9
RDX: 0000000020ae3000 RSI: 000000004020ae76 RDI: 0000000000000007
RBP: 0000000000000086 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000297 R12: 0000000000000000
R13: 0000000000000000 R14: 00007f0564cc39c0 R15: 00007f0564cc3700
Object at ffff88006648eac0, in cache kmalloc-512 size: 512
Allocated:
PID = 6857
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:517
set_track mm/kasan/kasan.c:529 [inline]
kasan_kmalloc+0xbc/0xf0 mm/kasan/kasan.c:620
kmem_cache_alloc_trace+0x11a/0x720 mm/slab.c:3638
kmalloc include/linux/slab.h:490 [inline]
kzalloc include/linux/slab.h:663 [inline]
kvm_irqfd_assign arch/x86/kvm/../../../virt/kvm/eventfd.c:296 [inline]
kvm_irqfd+0x16c/0x1d70 arch/x86/kvm/../../../virt/kvm/eventfd.c:572
kvm_vm_ioctl+0xe9c/0x1c60 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3012
vfs_ioctl fs/ioctl.c:45 [inline]
do_vfs_ioctl+0x1af/0x16d0 fs/ioctl.c:685
SYSC_ioctl fs/ioctl.c:700 [inline]
SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
entry_SYSCALL_64_fastpath+0x1f/0xc2
Freed:
PID = 1252
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:517
set_track mm/kasan/kasan.c:529 [inline]
kasan_slab_free+0x81/0xc0 mm/kasan/kasan.c:593
__cache_free mm/slab.c:3514 [inline]
kfree+0xd7/0x250 mm/slab.c:3831
irqfd_shutdown+0x13c/0x1a0 arch/x86/kvm/../../../virt/kvm/eventfd.c:148
process_one_work+0xb20/0x1b40 kernel/workqueue.c:2097
worker_thread+0x1b4/0x1340 kernel/workqueue.c:2231
kthread+0x359/0x420 kernel/kthread.c:229
ret_from_fork+0x31/0x40 arch/x86/entry/entry_64.S:430
Memory state around the buggy address:
ffff88006648eb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff88006648eb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88006648ec00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff88006648ec80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
ffff88006648ed00: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb
==================================================================