Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: gadget: udc: fix use-after-free in usb_gadget_state_work

A race condition during gadget teardown can lead to a use-after-free
in usb_gadget_state_work(), as reported by KASAN:

BUG: KASAN: invalid-access in sysfs_notify+0x2c/0xd0
Workqueue: events usb_gadget_state_work

The fundamental race occurs because a concurrent event (e.g., an
interrupt) can call usb_gadget_set_state() and schedule gadget->work
at any time during the cleanup process in usb_del_gadget().

Commit 399a45e5237c ("usb: gadget: core: flush gadget workqueue after
device removal") attempted to fix this by moving flush_work() to after
device_del(). However, this does not fully solve the race, as a new
work item can still be scheduled *after* flush_work() completes but
before the gadget's memory is freed, leading to the same use-after-free.

This patch fixes the race condition robustly by introducing a 'teardown'
flag and a 'state_lock' spinlock to the usb_gadget struct. The flag is
set during cleanup in usb_del_gadget() *before* calling flush_work() to
prevent any new work from being scheduled once cleanup has commenced.
The scheduling site, usb_gadget_set_state(), now checks this flag under
the lock before queueing the work, thus safely closing the race window.

Fixes: 5702f75375aa9 ("usb: gadget: udc-core: move sysfs_notify() to a workqueue")
Cc: stable <stable@kernel.org>
Signed-off-by: Jimmy Hu <hhhuuu@google.com>
Link: https://patch.msgid.link/20251023054945.233861-1-hhhuuu@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jimmy Hu and committed by
Greg Kroah-Hartman
baeb66fb eb9ac779

+21 -1
+16 -1
drivers/usb/gadget/udc/core.c
··· 1126 1126 void usb_gadget_set_state(struct usb_gadget *gadget, 1127 1127 enum usb_device_state state) 1128 1128 { 1129 + unsigned long flags; 1130 + 1131 + spin_lock_irqsave(&gadget->state_lock, flags); 1129 1132 gadget->state = state; 1130 - schedule_work(&gadget->work); 1133 + if (!gadget->teardown) 1134 + schedule_work(&gadget->work); 1135 + spin_unlock_irqrestore(&gadget->state_lock, flags); 1131 1136 trace_usb_gadget_set_state(gadget, 0); 1132 1137 } 1133 1138 EXPORT_SYMBOL_GPL(usb_gadget_set_state); ··· 1366 1361 void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, 1367 1362 void (*release)(struct device *dev)) 1368 1363 { 1364 + spin_lock_init(&gadget->state_lock); 1365 + gadget->teardown = false; 1369 1366 INIT_WORK(&gadget->work, usb_gadget_state_work); 1370 1367 gadget->dev.parent = parent; 1371 1368 ··· 1542 1535 void usb_del_gadget(struct usb_gadget *gadget) 1543 1536 { 1544 1537 struct usb_udc *udc = gadget->udc; 1538 + unsigned long flags; 1545 1539 1546 1540 if (!udc) 1547 1541 return; ··· 1556 1548 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); 1557 1549 sysfs_remove_link(&udc->dev.kobj, "gadget"); 1558 1550 device_del(&gadget->dev); 1551 + /* 1552 + * Set the teardown flag before flushing the work to prevent new work 1553 + * from being scheduled while we are cleaning up. 1554 + */ 1555 + spin_lock_irqsave(&gadget->state_lock, flags); 1556 + gadget->teardown = true; 1557 + spin_unlock_irqrestore(&gadget->state_lock, flags); 1559 1558 flush_work(&gadget->work); 1560 1559 ida_free(&gadget_id_numbers, gadget->id_number); 1561 1560 cancel_work_sync(&udc->vbus_work);
+5
include/linux/usb/gadget.h
··· 376 376 * can handle. The UDC must support this and all slower speeds and lower 377 377 * number of lanes. 378 378 * @state: the state we are now (attached, suspended, configured, etc) 379 + * @state_lock: Spinlock protecting the `state` and `teardown` members. 380 + * @teardown: True if the device is undergoing teardown, used to prevent 381 + * new work from being scheduled during cleanup. 379 382 * @name: Identifies the controller hardware type. Used in diagnostics 380 383 * and sometimes configuration. 381 384 * @dev: Driver model state for this abstract device. ··· 454 451 enum usb_ssp_rate max_ssp_rate; 455 452 456 453 enum usb_device_state state; 454 + spinlock_t state_lock; 455 + bool teardown; 457 456 const char *name; 458 457 struct device dev; 459 458 unsigned isoch_delay;