linux/include/linux/usb
Jimmy Hu eee16f3ff0 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>
[ Adjust context ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2026-01-19 13:10:05 +01:00
..
audio-v2.h usb: audio-v2: add ability to define feature unit descriptor 2021-07-21 10:10:11 +02:00
audio-v3.h USB: Replace zero-length array with flexible-array member 2020-02-23 19:24:51 +01:00
audio.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
c67x00.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
ccid.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
cdc-wdm.h usb: class: cdc-wdm: WWAN framework integration 2021-05-11 16:17:56 -07:00
cdc.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
cdc_ncm.h net: usb: cdc_ncm: use new API for bh tasklet 2021-02-03 14:22:21 -08:00
ch9.h usb: common: move function's kerneldoc next to its definition 2021-03-10 09:37:17 +01:00
chipidea.h usb: chipidea: tegra: Support host mode 2021-01-13 11:26:34 +01:00
composite.h usb: gadget: Add function wakeup support 2024-11-01 01:52:35 +01:00
ehci-dbgp.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
ehci_def.h USB: EHCI: Add alias for Broadcom INSNREG 2021-08-18 22:28:28 +02:00
ehci_pdriver.h usb: ehci: add spurious flag to disable overcurrent checking 2021-03-23 12:36:17 +01:00
ezusb.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
functionfs.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
g_hid.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
gadget.h usb: gadget: udc: fix use-after-free in usb_gadget_state_work 2026-01-19 13:10:05 +01:00
gadget_configfs.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
hcd.h usb: xhci: Add timeout argument in address_device USB HCD callback 2025-03-13 12:50:21 +01:00
input.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
iowarrior.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
irda.h USB: serial: ir-usb: fix link-speed handling 2020-01-23 09:11:16 +01:00
isp116x.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
isp1301.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
isp1362.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
m66592.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
musb-ux500.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
musb.h usb: musb: remove unused members in struct musb_hdrc_config 2018-05-22 12:03:25 +02:00
net2280.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
of.h USB: of: clean up device-node helper 2017-11-28 15:12:38 +01:00
ohci_pdriver.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
otg-fsm.h usb: otg-fsm: Fix hrtimer list corruption 2021-07-27 16:31:31 +02:00
otg.h usb: common: add helper to get role-switch-default-mode 2021-07-27 15:58:37 +02:00
pd.h usb: pd: Set PD_T_SINK_WAIT_CAP to 310ms 2021-06-02 16:59:29 +02:00
pd_ado.h typec: tcpm: Add ADO header for Alert message handling 2018-03-09 09:21:40 -08:00
pd_bdo.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
pd_ext_sdb.h usb: typec: tcpm: Properly handle Alert and Status Messages 2021-06-04 13:24:16 +02:00
pd_vdo.h usb: pd: Make SVDM Version configurable in VDM header 2021-02-09 11:48:54 +01:00
phy.h USB: phy: drop legacy board-file support 2018-04-22 15:58:24 +02:00
phy_companion.h USB: Replace HTTP links with HTTPS ones 2020-07-21 13:41:57 +02:00
quirks.h usb: fix spelling mistakes in header files 2021-05-21 20:05:31 +02:00
r8a66597.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
r8152.h r8152: add vendor/device ID pair for Dell Alienware AW1022z 2025-06-04 14:38:03 +02:00
renesas_usbhs.h usb: renesas_usbhs: Switch to GPIO descriptor 2019-12-30 20:37:42 +01:00
rndis_host.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
role.h Revert "usb: common: usb-conn-gpio: Set last role to unknown before initial detection" 2023-07-23 13:47:25 +02:00
serial.h USB-serial updates for 5.14-rc1 2021-06-24 12:54:28 +02:00
sl811.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
storage.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00
tcpci.h usb: typec: tcpci: clear the fault status bit 2023-09-06 21:28:40 +01:00
tcpm.h usb: typec: tcpci: Prevent Sink disconnection before vPpsShutdown in SPR PPS 2025-03-13 12:50:16 +01:00
tegra_usb_phy.h usb: phy: tegra: Support waking up from a low power mode 2021-01-13 11:26:34 +01:00
typec.h usb: typec: Factor out non-PD fwnode properties 2022-12-31 13:14:28 +01:00
typec_altmode.h usb: typec: bus: verify partner exists in typec_altmode_attention 2023-09-19 12:22:53 +02:00
typec_dp.h usb: typec: altmodes/displayport: do not index invalid pin_assignments 2025-07-10 15:57:43 +02:00
typec_mux.h usb: typec: mux: Allow the mux handles to be requested with fwnode 2020-03-04 11:12:49 +01:00
typec_tbt.h usb: typec: Remove one bit support for the Thunderbolt rounded/non-rounded cable 2020-11-18 12:53:45 +01:00
uas.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
ulpi.h usb: ulpi: Add resource-managed variant of otg_ulpi_create() 2020-01-09 16:21:57 +01:00
usb338x.h usb: gadget: net2280: Move all "ll" registers in one structure 2019-08-30 09:14:38 +03:00
usb_phy_generic.h usb: phy-generic: Delete unused platform data 2020-01-15 10:39:20 +01:00
usbnet.h net: usbnet: Avoid potential RCU stall on LINK_CHANGE event 2025-08-28 16:24:14 +02:00
xhci-dbgp.h USB: add SPDX identifiers to all remaining files in drivers/usb/ 2017-11-04 11:48:02 +01:00