Skip to content

Commit 2191c00

Browse files
AlanSterngregkh
authored andcommitted
USB: gadget: Fix use-after-free Read in usb_udc_uevent()
The syzbot fuzzer found a race between uevent callbacks and gadget driver unregistration that can cause a use-after-free bug: --------------------------------------------------------------- BUG: KASAN: use-after-free in usb_udc_uevent+0x11f/0x130 drivers/usb/gadget/udc/core.c:1732 Read of size 8 at addr ffff888078ce2050 by task udevd/2968 CPU: 1 PID: 2968 Comm: udevd Not tainted 5.19.0-rc4-next-20220628-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 06/29/2022 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_address_description mm/kasan/report.c:317 [inline] print_report.cold+0x2ba/0x719 mm/kasan/report.c:433 kasan_report+0xbe/0x1f0 mm/kasan/report.c:495 usb_udc_uevent+0x11f/0x130 drivers/usb/gadget/udc/core.c:1732 dev_uevent+0x290/0x770 drivers/base/core.c:2424 --------------------------------------------------------------- The bug occurs because usb_udc_uevent() dereferences udc->driver but does so without acquiring the udc_lock mutex, which protects this field. If the gadget driver is unbound from the udc concurrently with uevent processing, the driver structure may be accessed after it has been deallocated. To prevent the race, we make sure that the routine holds the mutex around the racing accesses. Link: <https://lore.kernel.org/all/[email protected]> CC: [email protected] # fc274c1 Reported-and-tested-by: [email protected] Signed-off-by: Alan Stern <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 26c6c2f commit 2191c00

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

drivers/usb/gadget/udc/core.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,13 +1728,14 @@ static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
17281728
return ret;
17291729
}
17301730

1731-
if (udc->driver) {
1731+
mutex_lock(&udc_lock);
1732+
if (udc->driver)
17321733
ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
17331734
udc->driver->function);
1734-
if (ret) {
1735-
dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1736-
return ret;
1737-
}
1735+
mutex_unlock(&udc_lock);
1736+
if (ret) {
1737+
dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1738+
return ret;
17381739
}
17391740

17401741
return 0;

0 commit comments

Comments
 (0)