Re: usb/gadget: use-after-free in gadgetfs_setup

From: Andrey Konovalov
Date: Tue Dec 06 2016 - 11:02:39 EST


On Tue, Dec 6, 2016 at 1:28 PM, Andrey Konovalov <andreyknvl@xxxxxxxxxx> wrote:
> On Mon, Dec 5, 2016 at 8:31 PM, Alan Stern <stern@xxxxxxxxxxxxxxxxxxx> wrote:
>> On Mon, 5 Dec 2016, Andrey Konovalov wrote:
>>
>>> Hi!
>>>
>>> I've got the following error report while running the syzkaller fuzzer.
>>>
>>> On commit 3c49de52d5647cda8b42c4255cf8a29d1e22eff5 (Dec 2).
>>>
>>> BUG: KASAN: use-after-free in gadgetfs_setup+0x208a/0x20e0 at addr
>>> ffff88003dfe5bf2
>>> Read of size 2 by task syz-executor0/22994
>>> CPU: 3 PID: 22994 Comm: syz-executor0 Not tainted 4.9.0-rc7+ #16
>>> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
>>> ffff88006df06a18 ffffffff81f96aba ffffffffe0528500 1ffff1000dbe0cd6
>>> ffffed000dbe0cce ffff88006df068f0 0000000041b58ab3 ffffffff8598b4c8
>>> ffffffff81f96828 1ffff1000dbe0ccd ffff88006df06708 ffff88006df06748
>>> Call Trace:
>>> <IRQ> [ 201.343209] [< inline >] __dump_stack lib/dump_stack.c:15
>>> <IRQ> [ 201.343209] [<ffffffff81f96aba>] dump_stack+0x292/0x398
>>> lib/dump_stack.c:51
>>> [<ffffffff817e4dec>] kasan_object_err+0x1c/0x70 mm/kasan/report.c:159
>>> [< inline >] print_address_description mm/kasan/report.c:197
>>> [<ffffffff817e5080>] kasan_report_error+0x1f0/0x4e0 mm/kasan/report.c:286
>>> [< inline >] kasan_report mm/kasan/report.c:306
>>> [<ffffffff817e562a>] __asan_report_load_n_noabort+0x3a/0x40
>>> mm/kasan/report.c:337
>>> [< inline >] config_buf drivers/usb/gadget/legacy/inode.c:1298
>>> [<ffffffff8322c8fa>] gadgetfs_setup+0x208a/0x20e0
>>> drivers/usb/gadget/legacy/inode.c:1368
>>> [<ffffffff830fdcd0>] dummy_timer+0x11f0/0x36d0
>>> drivers/usb/gadget/udc/dummy_hcd.c:1858
>>> [<ffffffff814807c1>] call_timer_fn+0x241/0x800 kernel/time/timer.c:1308
>>> [< inline >] expire_timers kernel/time/timer.c:1348
>>> [<ffffffff81482de6>] __run_timers+0xa06/0xec0 kernel/time/timer.c:1641
>>> [<ffffffff814832c1>] run_timer_softirq+0x21/0x80 kernel/time/timer.c:1654
>>> [<ffffffff84f4af8b>] __do_softirq+0x2fb/0xb63 kernel/softirq.c:284
>>> [< inline >] invoke_softirq kernel/softirq.c:364
>>> [<ffffffff812d3b8e>] irq_exit+0x19e/0x1d0 kernel/softirq.c:405
>>> [< inline >] exiting_irq arch/x86/include/asm/apic.h:659
>>> [<ffffffff84f4a59b>] smp_apic_timer_interrupt+0x7b/0xa0
>>> arch/x86/kernel/apic/apic.c:960
>>> [<ffffffff84f4964c>] apic_timer_interrupt+0x8c/0xa0
>>> arch/x86/entry/entry_64.S:489
>>> <EOI> [ 201.343209] [<ffffffff84f472f9>] ?
>>> _raw_spin_unlock_irqrestore+0x119/0x1a0
>>> [<ffffffff8136f9e4>] try_to_wake_up+0x174/0x1160 kernel/sched/core.c:2099
>>> [< inline >] wake_up_process kernel/sched/core.c:2165
>>> [<ffffffff81370a7a>] wake_up_q+0x8a/0xe0 kernel/sched/core.c:469
>>> [<ffffffff814cc6de>] futex_wake+0x5be/0x6c0 kernel/futex.c:1453
>>> [<ffffffff814d41dd>] do_futex+0x11bd/0x1f00 kernel/futex.c:3219
>>> [< inline >] SYSC_futex kernel/futex.c:3275
>>> [<ffffffff814d521c>] SyS_futex+0x2fc/0x400 kernel/futex.c:3243
>>> [<ffffffff84f47ec1>] entry_SYSCALL_64_fastpath+0x1f/0xc2
>>
>> Can you test whether the patch below fixes this problem?
>
> Hi Alan,
>
> Yes, I believe it does.
> It also seems to fix the warnings in dummy_free_request() I've been getting.

It seems that I was wrong. Still see both use-after-free and warnings.

>
> Thanks!
>
>>
>> Alan Stern
>>
>>
>>
>> Index: usb-4.x/drivers/usb/gadget/legacy/inode.c
>> ===================================================================
>> --- usb-4.x.orig/drivers/usb/gadget/legacy/inode.c
>> +++ usb-4.x/drivers/usb/gadget/legacy/inode.c
>> @@ -1762,6 +1762,10 @@ dev_config (struct file *fd, const char
>> }
>> spin_unlock_irq(&dev->lock);
>>
>> + /* Registered but not yet bound to a UDC driver? */
>> + if (dev->gadget_registered)
>> + return -EIO;
>> +
>> if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
>> return -EINVAL;
>>
>>