linux/drivers/usb
Cen Zhang 90431d8523 usb: gadget: f_tcm: synchronize delayed set_alt with teardown
[ Upstream commit 79e2d75725 ]

The f_tcm set_alt() path defers endpoint setup to a work item and
completes the delayed status response from process context. The delayed
work uses f_tcm private state and may complete the setup request after
disconnect or function teardown has already moved on.

Cancel and drain the delayed set_alt work when the function is unbound or
freed. For disable paths, which are reached under the composite device
lock, use a small state machine and a non-sleeping cancellation path
instead of cancel_work_sync(). If the work is already running, mark it
cancelled and let the worker own the cleanup; otherwise tcm_disable() can
cancel the queued work and clean up immediately.

Also serialize the final delayed-status completion with the cancellation
check while holding the composite device lock. This prevents a disconnect
from clearing delayed_status while the worker is about to complete the
control request.

Validation reproduced this kernel report:
BUG: KASAN: slab-use-after-free in tcm_delayed_set_alt+0x6c/0xef0

Call Trace:
 <TASK>
 dump_stack_lvl+0x66/0xa0
 print_report+0xce/0x630
 ? tcm_delayed_set_alt+0x6c/0xef0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __virt_addr_valid+0x188/0x320
 ? tcm_delayed_set_alt+0x6c/0xef0
 kasan_report+0xe0/0x110
 ? tcm_delayed_set_alt+0x6c/0xef0
 tcm_delayed_set_alt+0x6c/0xef0
 ? __pfx_tcm_delayed_set_alt+0x10/0x10
 ? process_one_work+0x4cb/0xb90
 ? rcu_is_watching+0x20/0x50
 ? tcm_delayed_set_alt+0x9/0xef0
 process_one_work+0x4d7/0xb90
 ? __pfx_process_one_work+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __list_add_valid_or_report+0x37/0xf0
 ? __pfx_tcm_delayed_set_alt+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 worker_thread+0x2d8/0x570
 ? __pfx_worker_thread+0x10/0x10
 kthread+0x1ad/0x1f0
 ? __pfx_kthread+0x10/0x10
 ret_from_fork+0x3c9/0x540
 ? __pfx_ret_from_fork+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __switch_to+0x2e9/0x730
 ? __pfx_kthread+0x10/0x10
 ret_from_fork_asm+0x1a/0x30
 </TASK>

Allocated by task 544:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x14/0x30
 __kasan_kmalloc+0x8f/0xa0
 tcm_alloc+0x68/0x180
 usb_get_function+0x36/0x60
 config_usb_cfg_link+0x125/0x1b0
 configfs_symlink+0x322/0x890
 vfs_symlink+0xc2/0x270
 filename_symlinkat+0x295/0x2f0
 __x64_sys_symlinkat+0x62/0x90
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 661:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x14/0x30
 kasan_save_free_info+0x3b/0x60
 __kasan_slab_free+0x43/0x70
 kfree+0x2f9/0x530
 config_usb_cfg_unlink+0x173/0x1e0
 configfs_unlink+0x1fa/0x340
 vfs_unlink+0x15c/0x510
 filename_unlinkat+0x2ba/0x450
 __x64_sys_unlinkat+0x63/0x90
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fixes: c52661d60f ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable <stable@kernel.org>
Assisted-by: Codex:gpt-5.5
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
Link: https://patch.msgid.link/20260627104153.3822495-1-zzzccc427@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ adjusted context for 6.12's scalar `struct usbg_cdb cmd` and missing `stream_hash`, dropping the `hash_init()` context line ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2026-08-23 14:17:25 +02:00
..
atm usb: atm: cxacru: properly kill rcv_urb on error in cxacru_cm() 2026-08-19 17:14:15 +02:00
c67x00 usb/c67x00: Replace tasklet with work 2021-01-26 18:36:37 +01:00
cdns3 usb: cdnsp: fix incorrect endian conversions for APB timeout register 2026-08-19 17:14:15 +02:00
chipidea usb: chipidea: fix usage_count leak when autosuspend_delay is negative 2026-08-19 17:13:55 +02:00
class usb: cdc_acm: Add quirk for Uniden BC125AT scanner 2026-07-24 15:51:22 +02:00
common USB: ulpi: fix memory leak on registration failure 2026-07-24 15:51:23 +02:00
core USB: quirks: add NO_LPM for the Samsung T5 EVO Portable SSD 2026-07-24 15:51:23 +02:00
dwc2 usb: dwc2: Fix use after free in debug code 2026-06-19 13:33:27 +02:00
dwc3 usb: dwc3: meson-g12a: fix refcount leak in dwc3_meson_g12a_resume() 2026-07-24 15:51:22 +02:00
early usb: early: xhci-dbc: Fix early_ioremap leak 2025-08-28 16:24:06 +02:00
gadget usb: gadget: f_tcm: synchronize delayed set_alt with teardown 2026-08-23 14:17:25 +02:00
host usb: xhci-pci: Limit VIA VL805 DMA addressing to 36 bits 2026-08-19 17:13:56 +02:00
image usb: image: mdc800: kill download URB on timeout 2026-04-18 10:33:14 +02:00
isp1760 usb: isp1760: Fix out-of-bounds array access 2022-06-09 10:22:27 +02:00
misc usb: iowarrior: remove inherent race with minor number 2026-07-24 15:52:01 +02:00
mon usb: mon: Increase BUFF_MAX to 64 MiB to support multi-MB URBs 2025-12-07 06:09:22 +09:00
mtu3 usb: mtu3: unmap request DMA on queue failure 2026-07-24 15:51:23 +02:00
musb usb: musb: omap2430: fix device leak at unbind 2025-08-28 16:24:32 +02:00
phy usb: phy: isp1301: fix non-OF device reference imbalance 2026-01-19 13:09:50 +01:00
renesas_usbhs usb: renesas_usbhs: fix use-after-free in ISR during device removal 2026-04-18 10:33:14 +02:00
roles usb: roles: set switch registered flag early on 2025-03-13 12:50:37 +01:00
serial USB: serial: option: add TDTECH MT5710-CN 2026-08-19 17:13:55 +02:00
storage USB: storage: add NO_ATA_1X quirk for Longmai USB Key 2026-08-19 17:13:55 +02:00
typec usb: typec: tcpm: Fix VDM type for Enter Mode commands 2026-07-24 15:52:01 +02:00
usbip usbip: vudc: fix NULL deref in vep_dequeue() 2026-07-24 15:51:24 +02:00
Kconfig usb: remove reference to deleted config STB03xxx 2021-08-18 15:32:19 +02:00
Makefile usb: host: remove line for obsolete config USB_HWA_HCD 2021-08-18 15:32:19 +02:00
usb-skeleton.c USB: usb-skeleton: drop redundant in-urb check 2019-10-10 12:41:19 +02:00