Skip to content

Commit c12a0c3

Browse files
Jimmy Hugregkh
authored andcommitted
usb: gadget: udc: fix use-after-free in usb_gadget_state_work
[ Upstream commit baeb66f ] 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 <[email protected]> Signed-off-by: Jimmy Hu <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b991645 commit c12a0c3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

drivers/usb/gadget/udc/core.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,8 +1121,13 @@ static void usb_gadget_state_work(struct work_struct *work)
11211121
void usb_gadget_set_state(struct usb_gadget *gadget,
11221122
enum usb_device_state state)
11231123
{
1124+
unsigned long flags;
1125+
1126+
spin_lock_irqsave(&gadget->state_lock, flags);
11241127
gadget->state = state;
1125-
schedule_work(&gadget->work);
1128+
if (!gadget->teardown)
1129+
schedule_work(&gadget->work);
1130+
spin_unlock_irqrestore(&gadget->state_lock, flags);
11261131
trace_usb_gadget_set_state(gadget, 0);
11271132
}
11281133
EXPORT_SYMBOL_GPL(usb_gadget_set_state);
@@ -1356,6 +1361,8 @@ static void usb_udc_nop_release(struct device *dev)
13561361
void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
13571362
void (*release)(struct device *dev))
13581363
{
1364+
spin_lock_init(&gadget->state_lock);
1365+
gadget->teardown = false;
13591366
INIT_WORK(&gadget->work, usb_gadget_state_work);
13601367
gadget->dev.parent = parent;
13611368

@@ -1530,6 +1537,7 @@ EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
15301537
void usb_del_gadget(struct usb_gadget *gadget)
15311538
{
15321539
struct usb_udc *udc = gadget->udc;
1540+
unsigned long flags;
15331541

15341542
if (!udc)
15351543
return;
@@ -1543,6 +1551,13 @@ void usb_del_gadget(struct usb_gadget *gadget)
15431551
kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
15441552
sysfs_remove_link(&udc->dev.kobj, "gadget");
15451553
device_del(&gadget->dev);
1554+
/*
1555+
* Set the teardown flag before flushing the work to prevent new work
1556+
* from being scheduled while we are cleaning up.
1557+
*/
1558+
spin_lock_irqsave(&gadget->state_lock, flags);
1559+
gadget->teardown = true;
1560+
spin_unlock_irqrestore(&gadget->state_lock, flags);
15461561
flush_work(&gadget->work);
15471562
ida_free(&gadget_id_numbers, gadget->id_number);
15481563
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
@@ -373,6 +373,9 @@ struct usb_gadget_ops {
373373
* can handle. The UDC must support this and all slower speeds and lower
374374
* number of lanes.
375375
* @state: the state we are now (attached, suspended, configured, etc)
376+
* @state_lock: Spinlock protecting the `state` and `teardown` members.
377+
* @teardown: True if the device is undergoing teardown, used to prevent
378+
* new work from being scheduled during cleanup.
376379
* @name: Identifies the controller hardware type. Used in diagnostics
377380
* and sometimes configuration.
378381
* @dev: Driver model state for this abstract device.
@@ -448,6 +451,8 @@ struct usb_gadget {
448451
enum usb_ssp_rate max_ssp_rate;
449452

450453
enum usb_device_state state;
454+
spinlock_t state_lock;
455+
bool teardown;
451456
const char *name;
452457
struct device dev;
453458
unsigned isoch_delay;

0 commit comments

Comments
 (0)