Skip to content

Commit baeb66f

Browse files
Jimmy Hugregkh
authored andcommitted
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 399a45e ("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: 5702f75 ("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>
1 parent eb9ac77 commit baeb66f

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

drivers/usb/gadget/udc/core.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,13 @@ static void usb_gadget_state_work(struct work_struct *work)
11261126
void usb_gadget_set_state(struct usb_gadget *gadget,
11271127
enum usb_device_state state)
11281128
{
1129+
unsigned long flags;
1130+
1131+
spin_lock_irqsave(&gadget->state_lock, flags);
11291132
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);
11311136
trace_usb_gadget_set_state(gadget, 0);
11321137
}
11331138
EXPORT_SYMBOL_GPL(usb_gadget_set_state);
@@ -1361,6 +1366,8 @@ static void usb_udc_nop_release(struct device *dev)
13611366
void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
13621367
void (*release)(struct device *dev))
13631368
{
1369+
spin_lock_init(&gadget->state_lock);
1370+
gadget->teardown = false;
13641371
INIT_WORK(&gadget->work, usb_gadget_state_work);
13651372
gadget->dev.parent = parent;
13661373

@@ -1535,6 +1542,7 @@ EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
15351542
void usb_del_gadget(struct usb_gadget *gadget)
15361543
{
15371544
struct usb_udc *udc = gadget->udc;
1545+
unsigned long flags;
15381546

15391547
if (!udc)
15401548
return;
@@ -1548,6 +1556,13 @@ void usb_del_gadget(struct usb_gadget *gadget)
15481556
kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
15491557
sysfs_remove_link(&udc->dev.kobj, "gadget");
15501558
device_del(&gadget->dev);
1559+
/*
1560+
* Set the teardown flag before flushing the work to prevent new work
1561+
* from being scheduled while we are cleaning up.
1562+
*/
1563+
spin_lock_irqsave(&gadget->state_lock, flags);
1564+
gadget->teardown = true;
1565+
spin_unlock_irqrestore(&gadget->state_lock, flags);
15511566
flush_work(&gadget->work);
15521567
ida_free(&gadget_id_numbers, gadget->id_number);
15531568
cancel_work_sync(&udc->vbus_work);

include/linux/usb/gadget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ struct usb_gadget_ops {
376376
* can handle. The UDC must support this and all slower speeds and lower
377377
* number of lanes.
378378
* @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.
379382
* @name: Identifies the controller hardware type. Used in diagnostics
380383
* and sometimes configuration.
381384
* @dev: Driver model state for this abstract device.
@@ -451,6 +454,8 @@ struct usb_gadget {
451454
enum usb_ssp_rate max_ssp_rate;
452455

453456
enum usb_device_state state;
457+
spinlock_t state_lock;
458+
bool teardown;
454459
const char *name;
455460
struct device dev;
456461
unsigned isoch_delay;

0 commit comments

Comments
 (0)