linux/drivers/usb/dwc3
Pei Xiao 9c85583279 usb: dwc3: gadget: Fix use-after-free in dwc3_gadget_free_endpoints due to race condition
In dwc3_gadget_init_endpoint, &dep->nostream_work is bound with
dwc3_nostream_work, and dwc3_gadget_endpoint_stream_event can queue
this delayed work on system_percpu_wq when a DEPEVT_STREAM_NOSTREAM
event is received.

If we remove the gadget, dwc3_gadget_free_endpoints makes cleanup and
the memory allocated for dep with kzalloc() is released by kfree(dep),
while the delayed work mentioned above may still be pending or
running. The sequence of operations that may lead to a UAF bug is as
follows:

CPU0                                      CPU1

                                          | dwc3_thread_interrupt
                                          | dwc3_endpoint_interrupt
                                          | dwc3_gadget_endpoint_stream_event
                                          | queue_delayed_work(system_percpu_wq,
                                          |                    &dep->nostream_work)
dwc3_gadget_free_endpoints                |
dwc3_free_trb_pool(dep)                   |
list_del(&dep->endpoint.ep_list)          |
dwc3_debugfs_remove_endpoint_dir(dep)     |
kfree(dep)                                |
// dep is freed                           |
                                          | dwc3_nostream_work
                                          | // use dep (use-after-free)

Fix it by canceling the delayed work before kfree(dep) in
dwc3_gadget_free_endpoints.

Fixes: dcfe437492 ("usb: dwc3: gadget: Reinitiate stream for all host NoStream behavior")
Assisted-by: Codex:deepseek-v4-flash
Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Cc: stable@vger.kernel.org
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
Link: https://patch.msgid.link/331d1d5133496d2b4184e05f8848adb06930a138.1785893865.git.xiaopei01@kylinos.cn
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2026-08-14 10:49:25 +09:00
..
core.c Merge 7.2-rc3 into usb-next 2026-07-13 07:09:28 +02:00
core.h usb: dwc3: fix kernel-doc string in struct dwc3_ep 2026-07-10 08:50:36 +02:00
debug.h usb: dwc3: Add trace event for dwc3_set_prtcap 2025-09-06 15:21:59 +02:00
debugfs.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
drd.c usb: dwc3: Add dwc pointer to dwc3_readl/writel 2026-01-16 13:47:07 +01:00
dwc3-am62.c usb: dwc3: am62: Propagate USB2 refclk enable failures 2026-07-10 08:50:36 +02:00
dwc3-apple.c usb: dwc3: apple: Ignore USB role switches to the active role 2026-01-14 16:02:54 +01:00
dwc3-exynos.c usb: dwc3-exynos: add support for ExynosAutov920 2025-05-21 13:12:43 +02:00
dwc3-generic-plat.c usb: dwc3: starfive: Add JHB100 USB 2.0 DRD controller 2026-04-11 12:02:43 +02:00
dwc3-google.c usb: dwc3: google: Use FIELD_MODIFY() 2026-05-21 16:43:04 +02:00
dwc3-haps.c
dwc3-imx.c usb: dwc3: imx: avoid calling imx suspend/resume callbacks twice 2026-03-18 16:22:18 +01:00
dwc3-imx8mp.c usb: dwc3: imx8mp: fix memory leak on probe failure path 2026-04-02 09:37:45 +02:00
dwc3-keystone.c
dwc3-meson-g12a.c usb: dwc3: meson-g12a: fix refcount leak in dwc3_meson_g12a_resume() 2026-06-25 16:04:11 +01:00
dwc3-octeon.c usb: dwc3: dwc3-octeon: Use FIELD_MODIFY() 2026-05-21 16:43:04 +02:00
dwc3-of-simple.c usb: dwc3: of-simple: fix clock resource leak in dwc3_of_simple_probe 2025-12-17 14:54:38 +01:00
dwc3-omap.c usb: dwc3: omap: Fix devm_regulator_get_optional() error handling 2025-01-11 17:02:30 +01:00
dwc3-pci.c usb: dwc3: pci: add support for the Intel Nova Lake -H 2026-03-11 16:19:41 +01:00
dwc3-qcom-legacy.c usb: dwc3: qcom: Snapshot driver for backwards compatibilty 2025-04-15 14:29:19 +02:00
dwc3-qcom.c usb: dwc3: qcom: make dwc3_qcom_glue_ops static 2026-07-10 08:50:36 +02:00
dwc3-rtk.c
dwc3-st.c usb: dwc3: Don't use %pK through printk 2025-03-14 09:17:30 +01:00
dwc3-xilinx.c usb: dwc3: xilinx: use reset_control_reset() in versal init 2026-07-08 17:06:43 +02:00
ep0.c usb: dwc3: Log dwc3 address in traces 2026-01-27 15:52:45 +01:00
gadget.c usb: dwc3: gadget: Fix use-after-free in dwc3_gadget_free_endpoints due to race condition 2026-08-14 10:49:25 +09:00
gadget.h usb: dwc3: Add dwc pointer to dwc3_readl/writel 2026-01-16 13:47:07 +01:00
glue.h usb: dwc3: add needs_full_reinit flag 2026-02-25 10:35:58 -08:00
host.c usb: xhci: standardize multi bit-field macros 2026-08-14 10:26:41 +09:00
io.h usb: dwc3: Log dwc3 address in traces 2026-01-27 15:52:45 +01:00
Kconfig usb: dwc3: introduce flatten model driver of i.MX Soc 2026-02-25 10:35:58 -08:00
Makefile usb: dwc3: introduce flatten model driver of i.MX Soc 2026-02-25 10:35:58 -08:00
trace.c
trace.h usb: dwc3: Log dwc3 address in traces 2026-01-27 15:52:45 +01:00
ulpi.c usb: dwc3: Support USB3340x ULPI PHY high-speed negotiation. 2026-03-11 15:00:28 +01:00