2016-12-05 13:10:56

by Andrey Konovalov

[permalink] [raw]
Subject: usb/gadget: use-after-free in gadgetfs_setup

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
Object at ffff88003dfe5bf0, in cache kmalloc-32 size: 32
Allocated:
PID = 21625
[ 201.343209] [<ffffffff81203526>] save_stack_trace+0x16/0x20
arch/x86/kernel/stacktrace.c:57
[ 201.343209] [<ffffffff817e40d3>] save_stack+0x43/0xd0 mm/kasan/kasan.c:495
[ 201.343209] [< inline >] set_track mm/kasan/kasan.c:507
[ 201.343209] [<ffffffff817e435d>] kasan_kmalloc+0xad/0xe0
mm/kasan/kasan.c:598
[ 201.343209] [<ffffffff817e4932>] kasan_slab_alloc+0x12/0x20
mm/kasan/kasan.c:537
[ 201.343209] [< inline >] slab_post_alloc_hook mm/slab.h:417
[ 201.343209] [< inline >] slab_alloc_node mm/slub.c:2708
[ 201.343209] [< inline >] slab_alloc mm/slub.c:2716
[ 201.343209] [<ffffffff817e3465>]
__kmalloc_track_caller+0x195/0x290 mm/slub.c:4240
[ 201.343209] [<ffffffff8171bb1c>] memdup_user+0x2c/0xa0 mm/util.c:137
[ 201.343209] [<ffffffff8322872b>] dev_config+0x2eb/0x1190
drivers/usb/gadget/legacy/inode.c:1778
[ 201.343209] [<ffffffff817fdd55>] __vfs_write+0x5d5/0x760 fs/read_write.c:510
[ 201.343209] [<ffffffff817ff650>] vfs_write+0x170/0x4e0 fs/read_write.c:560
[ 201.343209] [< inline >] SYSC_write fs/read_write.c:607
[ 201.343209] [<ffffffff81803a5b>] SyS_write+0xfb/0x230 fs/read_write.c:599
[ 201.343209] [<ffffffff84f47ec1>] entry_SYSCALL_64_fastpath+0x1f/0xc2
Freed:
PID = 21625
[ 201.343209] [<ffffffff81203526>] save_stack_trace+0x16/0x20
arch/x86/kernel/stacktrace.c:57
[ 201.343209] [<ffffffff817e40d3>] save_stack+0x43/0xd0 mm/kasan/kasan.c:495
[ 201.343209] [< inline >] set_track mm/kasan/kasan.c:507
[ 201.343209] [<ffffffff817e49b3>] kasan_slab_free+0x73/0xc0
mm/kasan/kasan.c:571
[ 201.343209] [< inline >] slab_free_hook mm/slub.c:1352
[ 201.343209] [< inline >] slab_free_freelist_hook mm/slub.c:1374
[ 201.343209] [< inline >] slab_free mm/slub.c:2951
[ 201.343209] [<ffffffff817e0e17>] kfree+0xe7/0x2b0 mm/slub.c:3871
[ 201.343209] [<ffffffff8322904b>] dev_config+0xc0b/0x1190
drivers/usb/gadget/legacy/inode.c:1848
[ 201.343209] [<ffffffff817fdd55>] __vfs_write+0x5d5/0x760 fs/read_write.c:510
[ 201.343209] [<ffffffff817ff650>] vfs_write+0x170/0x4e0 fs/read_write.c:560
[ 201.343209] [< inline >] SYSC_write fs/read_write.c:607
[ 201.343209] [<ffffffff81803a5b>] SyS_write+0xfb/0x230 fs/read_write.c:599
[ 201.343209] [<ffffffff84f47ec1>] entry_SYSCALL_64_fastpath+0x1f/0xc2
Memory state around the buggy address:
ffff88003dfe5a80: fb fb fb fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88003dfe5b00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
>ffff88003dfe5b80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fb fb
^
ffff88003dfe5c00: fb fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88003dfe5c80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================


2016-12-05 19:32:03

by Alan Stern

[permalink] [raw]
Subject: Re: usb/gadget: use-after-free in gadgetfs_setup

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?

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;


2016-12-06 12:28:49

by Andrey Konovalov

[permalink] [raw]
Subject: Re: usb/gadget: use-after-free in gadgetfs_setup

On Mon, Dec 5, 2016 at 8:31 PM, Alan Stern <[email protected]> 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.

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;
>
>

2016-12-06 16:02:37

by Andrey Konovalov

[permalink] [raw]
Subject: Re: usb/gadget: use-after-free in gadgetfs_setup

On Tue, Dec 6, 2016 at 1:28 PM, Andrey Konovalov <[email protected]> wrote:
> On Mon, Dec 5, 2016 at 8:31 PM, Alan Stern <[email protected]> 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;
>>
>>