Commit c12a0c3
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
2 files changed
+21
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1121 | 1121 | | |
1122 | 1122 | | |
1123 | 1123 | | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
1124 | 1127 | | |
1125 | | - | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
1126 | 1131 | | |
1127 | 1132 | | |
1128 | 1133 | | |
| |||
1356 | 1361 | | |
1357 | 1362 | | |
1358 | 1363 | | |
| 1364 | + | |
| 1365 | + | |
1359 | 1366 | | |
1360 | 1367 | | |
1361 | 1368 | | |
| |||
1530 | 1537 | | |
1531 | 1538 | | |
1532 | 1539 | | |
| 1540 | + | |
1533 | 1541 | | |
1534 | 1542 | | |
1535 | 1543 | | |
| |||
1543 | 1551 | | |
1544 | 1552 | | |
1545 | 1553 | | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
1546 | 1561 | | |
1547 | 1562 | | |
1548 | 1563 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
376 | 379 | | |
377 | 380 | | |
378 | 381 | | |
| |||
448 | 451 | | |
449 | 452 | | |
450 | 453 | | |
| 454 | + | |
| 455 | + | |
451 | 456 | | |
452 | 457 | | |
453 | 458 | | |
| |||
0 commit comments