mirror-linux/include/linux/usb
Jimmy Hu baeb66fbd4 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 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>
2025-10-28 15:46:02 +01:00
..
audio-v2.h
audio-v3.h
audio.h
c67x00.h
ccid.h
cdc-wdm.h
cdc.h
cdc_ncm.h net: usb: cdc-ncm: check for filtering capability 2025-07-21 16:59:26 -07:00
ch9.h
chipidea.h usb: chipidea: udc: add CI_HDRC_CONTROLLER_PULLUP_EVENT event 2025-06-19 12:28:25 +02:00
composite.h usb: gadget: Remove unused usb_remove_config 2025-06-19 12:26:48 +02:00
ehci-dbgp.h
ehci_def.h
ehci_pdriver.h
ezusb.h
func_utils.h
functionfs.h
g_hid.h
gadget.h usb: gadget: udc: fix use-after-free in usb_gadget_state_work 2025-10-28 15:46:02 +01:00
gadget_configfs.h
hcd.h
input.h
iowarrior.h
irda.h
isp116x.h
isp1301.h
isp1362.h
ljca.h
m66592.h
mctp-usb.h usb: Add base USB MCTP definitions 2025-02-21 16:45:21 -08:00
midi-v2.h
musb-ux500.h
musb.h usb: musb: Constify struct musb_fifo_cfg 2025-02-03 16:29:53 +01:00
net2280.h
of.h
ohci_pdriver.h
onboard_dev.h
otg-fsm.h
otg.h
pd.h usb: typec: tcpm: Add new AMS for Get_Revision response 2024-12-24 08:56:04 +01:00
pd_ado.h
pd_bdo.h
pd_ext_sdb.h
pd_vdo.h
phy.h usb: phy: Remove API devm_usb_put_phy() 2025-01-13 06:12:09 +01:00
phy_companion.h
quirks.h
r8a66597.h
r8152.h r8152: add vendor/device ID pair for Dell Alienware AW1022z 2025-02-10 17:57:35 -08:00
renesas_usbhs.h
rndis_host.h
role.h
rzv2m_usb3drd.h
serial.h
sl811.h
storage.h
tcpci.h
tcpm.h usb: typec: tcpci: Prevent Sink disconnection before vPpsShutdown in SPR PPS 2025-01-17 12:40:20 +01:00
tegra_usb_phy.h usb: phy: tegra: Remove unused functions 2025-06-19 12:26:32 +02:00
typec.h usb: typec: Make active on port altmode writable 2024-12-24 08:56:05 +01:00
typec_altmode.h
typec_dp.h usb: typec: altmodes/displayport: do not index invalid pin_assignments 2025-06-24 15:42:55 +01:00
typec_mux.h usb: typec: Stub out typec_switch APIs when CONFIG_TYPEC=n 2025-09-17 12:42:30 +02:00
typec_retimer.h
typec_tbt.h usb: typec: Add driver for Thunderbolt 3 Alternate Mode 2024-12-24 08:56:05 +01:00
uas.h
ulpi.h usb: ulpi: Remove unused otg_ulpi_create 2025-03-03 10:23:35 +01:00
usb338x.h
usb_phy_generic.h
usbio.h usb: misc: Add Intel USBIO bridge driver 2025-09-12 14:05:09 +02:00
usbnet.h Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2025-07-26 11:49:45 -07:00
uvc.h media: uvcvideo: Support UVC_CROSXU_CONTROL_IQ_PROFILE 2025-09-13 18:35:02 +02:00
webusb.h
xhci-dbgp.h
xhci-sideband.h xhci: sideband: add api to trace sideband usage 2025-09-12 14:08:02 +02:00