Jimmy Hu
1001431019
usb: gadget: udc: fix use-after-free in usb_gadget_state_work
...
[ Upstream commit baeb66fbd4 ]
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 399a45e523 ("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: 5702f75375 ("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 >
Signed-off-by: Sasha Levin <sashal@kernel.org >
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org >
2025-12-07 06:25:02 +09:00
..
2025-06-27 11:11:19 +01:00
2025-05-29 11:03:15 +02:00
2024-12-19 18:13:14 +01:00
2025-10-02 13:44:07 +02:00
2025-11-02 22:15:22 +09:00
2024-12-14 20:03:12 +01:00
2025-12-07 06:24:57 +09:00
2024-12-14 20:03:26 +01:00
2025-12-07 06:24:56 +09:00
2025-05-29 11:02:43 +02:00
2025-05-29 11:02:49 +02:00
2025-09-25 11:13:44 +02:00
2025-06-27 11:11:26 +01:00
2025-02-17 10:04:53 +01:00
2025-02-27 04:30:17 -08:00
2025-04-10 14:39:28 +02:00
2025-12-07 06:24:57 +09:00
2025-10-19 16:34:03 +02:00
2025-12-07 06:25:02 +09:00
2025-08-20 18:30:58 +02:00
2024-12-27 14:02:13 +01:00
2024-12-27 14:01:57 +01:00
2025-06-19 15:31:38 +02:00
2025-12-01 11:43:21 +01:00
2025-09-04 15:31:47 +02:00
2025-11-02 22:15:20 +09:00
2025-04-25 10:47:49 +02:00
2025-02-17 10:04:43 +01:00
2025-06-19 15:32:33 +02:00
2025-11-02 22:15:22 +09:00
2025-11-02 22:15:22 +09:00
2025-11-13 15:33:59 -05:00
2025-05-02 07:58:54 +02:00
2025-09-09 18:58:24 +02:00
2025-11-13 15:33:57 -05:00
2025-09-09 18:58:01 +02:00
2025-10-15 12:00:04 +02:00
2025-10-15 12:00:03 +02:00
2025-05-29 11:02:00 +02:00
2025-06-19 15:32:33 +02:00
2025-08-28 16:31:06 +02:00
2025-04-20 10:15:04 +02:00
2025-11-13 15:34:06 -05:00
2024-12-14 20:03:38 +01:00
2024-12-14 20:04:16 +01:00
2025-03-13 13:01:58 +01:00
2024-12-05 14:02:45 +01:00
2025-11-24 10:35:46 +01:00
2025-09-19 16:35:45 +02:00
2025-03-07 18:25:35 +01:00
2025-08-28 16:31:10 +02:00
2025-04-10 14:39:28 +02:00
2025-06-04 14:43:52 +02:00
2025-06-19 15:32:12 +02:00
2025-09-11 17:21:46 +02:00
2025-10-19 16:34:02 +02:00
2025-07-10 16:05:08 +02:00
2025-10-12 12:57:19 +02:00
2025-09-04 15:31:45 +02:00
2025-05-29 11:01:59 +02:00
2024-12-14 20:04:09 +01:00
2025-02-21 14:01:34 +01:00
2025-05-02 07:58:52 +02:00
2025-05-29 11:03:25 +02:00
2025-03-13 13:02:07 +01:00
2024-12-14 20:03:35 +01:00
2025-05-18 08:24:59 +02:00
2025-07-10 16:05:08 +02:00
2025-11-13 15:34:14 -05:00
2024-12-14 20:03:48 +01:00
2025-11-13 15:33:55 -05:00
2025-11-24 10:35:55 +01:00
2025-08-15 12:13:49 +02:00
2025-08-15 12:13:31 +02:00
2025-09-19 16:35:42 +02:00
2025-04-10 14:39:17 +02:00
2025-09-09 18:58:04 +02:00
2025-05-29 11:03:22 +02:00
2025-02-17 10:05:31 +01:00
2025-01-23 17:23:03 +01:00
2025-11-24 10:36:07 +01:00
2025-06-27 11:11:40 +01:00
2025-05-22 14:29:48 +02:00
2025-08-20 18:30:14 +02:00
2025-07-17 18:37:16 +02:00
2025-08-20 18:30:37 +02:00
2024-12-05 14:01:35 +01:00
2025-04-10 14:39:12 +02:00
2025-09-25 11:13:48 +02:00
2024-12-27 14:02:17 +01:00
2025-05-29 11:03:25 +02:00
2025-01-17 13:40:34 +01:00
2025-05-09 09:50:34 +02:00
2025-08-15 12:14:05 +02:00
2025-08-28 16:31:12 +02:00
2025-08-28 16:31:05 +02:00
2025-05-29 11:02:23 +02:00
2025-08-01 09:48:42 +01:00
2024-12-05 14:01:22 +01:00
2025-02-08 09:57:58 +01:00
2024-12-09 10:41:08 +01:00
2025-08-28 16:31:09 +02:00
2025-03-28 22:03:30 +01:00
2024-12-05 14:03:03 +01:00
2025-10-19 16:34:01 +02:00
2025-11-24 10:36:05 +01:00
2024-12-14 20:04:01 +01:00
2025-08-20 18:30:57 +02:00
2024-12-05 14:02:46 +01:00
2025-05-29 11:02:25 +02:00
2025-11-24 10:35:59 +01:00
2025-07-17 18:37:24 +02:00
2025-06-19 15:31:54 +02:00
2025-01-09 13:33:54 +01:00
2025-08-20 18:30:16 +02:00
2025-09-25 11:13:51 +02:00
2025-01-09 13:33:54 +01:00
2025-10-23 16:20:47 +02:00
2025-05-29 11:03:23 +02:00
2024-12-05 14:03:08 +01:00
2025-05-18 08:24:59 +02:00
2025-08-15 12:13:59 +02:00
2025-01-17 13:40:50 +01:00
2025-02-08 09:57:23 +01:00
2025-05-29 11:02:41 +02:00
2024-12-19 18:13:19 +01:00
2025-02-27 04:30:18 -08:00
2025-08-28 16:31:04 +02:00
2025-11-24 10:36:06 +01:00
2025-02-08 09:57:59 +01:00
2025-06-04 14:43:53 +02:00
2025-08-28 16:31:05 +02:00
2025-11-24 10:35:47 +01:00
2025-04-25 10:47:50 +02:00
2024-12-05 14:02:27 +01:00
2025-04-10 14:39:11 +02:00
2025-03-13 13:02:03 +01:00
2025-06-19 15:32:18 +02:00
2024-12-05 14:01:32 +01:00
2024-12-05 14:03:08 +01:00
2025-10-15 12:00:03 +02:00
2025-06-19 15:32:37 +02:00
2025-08-20 18:30:35 +02:00
2025-05-29 11:03:22 +02:00
2025-04-20 10:15:25 +02:00
2025-05-29 11:02:04 +02:00
2025-06-19 15:32:11 +02:00
2025-11-13 15:34:12 -05:00
2025-05-29 11:03:02 +02:00
2025-09-19 16:35:42 +02:00
2025-09-19 16:35:42 +02:00
2025-06-19 15:31:54 +02:00
2025-03-13 13:02:07 +01:00
2025-06-19 15:32:22 +02:00
2025-10-23 16:20:39 +02:00
2025-05-29 11:02:20 +02:00
2025-06-19 15:31:42 +02:00
2025-01-23 17:22:57 +01:00
2025-08-15 12:13:35 +02:00
2025-04-20 10:15:12 +02:00
2025-08-15 12:13:53 +02:00
2025-01-23 17:22:53 +01:00
2025-08-15 12:13:42 +02:00
2025-07-17 18:37:08 +02:00
2025-02-08 09:57:07 +01:00
2025-05-29 11:03:02 +02:00
2025-03-07 18:25:43 +01:00
2025-05-29 11:03:02 +02:00
2025-08-15 12:13:46 +02:00
2025-10-19 16:33:34 +02:00
2024-12-05 14:01:23 +01:00
2025-08-20 18:30:49 +02:00
2024-12-14 20:03:18 +01:00
2025-03-13 13:01:58 +01:00
2025-02-01 18:39:35 +01:00
2024-12-05 14:01:23 +01:00
2025-02-27 04:30:14 -08:00
2025-11-13 15:34:18 -05:00
2025-02-08 09:57:07 +01:00
2025-09-09 18:58:10 +02:00
2025-02-27 04:30:19 -08:00
2025-03-07 18:25:32 +01:00
2024-12-05 14:01:23 +01:00
2025-07-10 16:04:54 +02:00
2025-08-01 09:48:43 +01:00
2024-12-14 20:03:32 +01:00
2024-12-19 18:13:24 +01:00
2025-10-02 13:44:11 +02:00
2025-06-27 11:11:30 +01:00
2025-04-10 14:39:26 +02:00
2024-12-14 20:04:16 +01:00
2025-11-13 15:34:02 -05:00
2025-05-22 14:29:50 +02:00
2025-04-10 14:39:35 +02:00
2025-05-29 11:02:04 +02:00
2025-05-29 11:02:04 +02:00
2025-05-18 08:24:56 +02:00
2025-04-10 14:39:37 +02:00
2025-07-10 16:05:11 +02:00
2025-01-23 17:23:02 +01:00
2024-12-09 10:41:06 +01:00
2025-08-20 18:30:40 +02:00
2025-05-22 14:29:38 +02:00
2025-09-09 18:58:15 +02:00
2025-01-02 10:34:10 +01:00
2025-08-15 12:14:04 +02:00
2024-12-27 14:02:20 +01:00
2025-01-17 13:40:33 +01:00