* [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset
@ 2026-11-29 13:47 Jaegyun Lee
2026-11-29 13:47 ` [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage Jaegyun Lee
2026-11-29 14:04 ` [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset sashiko-bot
0 siblings, 2 replies; 4+ messages in thread
From: Jaegyun Lee @ 2026-11-29 13:47 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Jaegyun Lee, Thinh Nguyen, linux-usb, linux-kernel, stable
usbg_bot_setup() handles a Bulk-Only Mass Storage Reset by only
re-arming the CBW request:
case US_BULK_RESET_REQUEST:
/* XXX maybe we should remove previous requests for IN + OUT */
...
bot_enqueue_cmd_cbw(fu);
return 0;
A data-IN request that is still in flight stays queued on ep_in. The
next READ goes through bot_send_read_response(), which reuses the same
fu->bot_req_in and rewrites its buf and length while it is still
queued; the usb_ep_queue() that follows fails
("bot_send_read_response(244)"). The UDC then continues the old
request with the new buffer while ->actual still holds the progress of
the first command. In dummy_hcd, transfer() computes
req.length - req.actual (unsigned) and copies from buf + actual into
the host's URB, i.e. from past the end of the new 512-byte buffer.
Reproduced with dummy_hcd and a tcm BOT
function: the host sends CBW READ(10) of 128 blocks, reads 4096 bytes
of the data stage, sends a Bulk-Only Reset, then CBW READ(10) of 1
block and reads 4096 bytes:
[ 46.452690][ C1] ==================================================================
[ 46.452709][ C1] BUG: KASAN: slab-use-after-free in dummy_timer+0x1ac2/0x35c0
[ 46.452743][ C1] Read of size 4096 at addr ffff888029c5a000 by task swapper/1/0
[ 46.452761][ C1]
[ 46.452771][ C1] CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full)
[ 46.452798][ C1] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 46.452811][ C1] Call Trace:
[ 46.452819][ C1] <IRQ>
[ 46.452827][ C1] dump_stack_lvl+0x116/0x1b0
[ 46.452852][ C1] print_report+0xf1/0x5c0
[ 46.452873][ C1] ? __virt_addr_valid+0x238/0x430
[ 46.452906][ C1] ? dummy_timer+0x1ac2/0x35c0
[ 46.452929][ C1] kasan_report+0xca/0x100
[ 46.452950][ C1] ? dummy_timer+0x1ac2/0x35c0
[ 46.452975][ C1] kasan_check_range+0x39/0x1c0
[ 46.453000][ C1] __asan_memcpy+0x24/0x60
[ 46.453029][ C1] dummy_timer+0x1ac2/0x35c0
[ 46.453055][ C1] ? debug_object_deactivate+0x1ec/0x3a0
[ 46.453083][ C1] ? __hrtimer_run_queues+0x3f8/0xa00
[ 46.453116][ C1] ? __pfx_dummy_timer+0x10/0x10
[ 46.453140][ C1] ? rcu_is_watching+0x13/0xc0
[ 46.453204][ C1] __hrtimer_run_queues+0x17f/0xa00
[ 46.453236][ C1] ? __pfx_dummy_timer+0x10/0x10
[ 46.453262][ C1] hrtimer_run_softirq+0x1c9/0x360
[ 46.453297][ C1] handle_softirqs+0x20e/0x9e0
[ 46.453327][ C1] __irq_exit_rcu+0x184/0x240
[ 46.453354][ C1] irq_exit_rcu+0x9/0x30
[ 46.453380][ C1] sysvec_apic_timer_interrupt+0xa8/0xc0
[ 46.453412][ C1] </IRQ>
[ 46.453419][ C1] <TASK>
[ 46.453426][ C1] asm_sysvec_apic_timer_interrupt+0x1a/0x20
[ 46.453450][ C1] RIP: 0010:pv_native_safe_halt+0x1e/0x30
[ 46.453483][ C1] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 eb 0c 0f 1f 44 00 00 0f 00 2d 29 f8 15 00 0f 1f 44 00 00 fb f4 <c3> cc cc cc cc 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 90 90 90 90
[ 46.453505][ C1] RSP: 0018:ffffc9000047fdf8 EFLAGS: 00000246
[ 46.453525][ C1] RAX: 000000000000e4b4 RBX: ffff8880404b0000 RCX: ffffffff8bd63249
[ 46.453540][ C1] RDX: ffffed100fdc6826 RSI: 0000000000000001 RDI: ffffffff8196c771
[ 46.453555][ C1] RBP: ffffed1008096000 R08: 0000000000000000 R09: ffffed100fdc6825
[ 46.453569][ C1] R10: ffff88807ee3412b R11: ffffffff818de5c7 R12: dffffc0000000000
[ 46.453583][ C1] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[ 46.453598][ C1] ? finish_task_switch.isra.0+0x147/0x1100
[ 46.453633][ C1] ? ct_kernel_exit+0x1d9/0x240
[ 46.453652][ C1] ? do_idle+0x3c1/0x5c0
[ 46.453676][ C1] default_idle+0xe/0x20
[ 46.453696][ C1] default_idle_call+0x6c/0xb0
[ 46.453717][ C1] do_idle+0x3c1/0x5c0
[ 46.453739][ C1] ? __pfx_do_idle+0x10/0x10
[ 46.453760][ C1] ? _raw_spin_unlock_irq_enable+0x120/0x160
[ 46.453791][ C1] ? lockdep_hardirqs_on+0x7d/0x110
[ 46.453824][ C1] cpu_startup_entry+0x4f/0x60
[ 46.453847][ C1] start_secondary+0x21d/0x2d0
[ 46.453877][ C1] ? __pfx_start_secondary+0x10/0x10
[ 46.453908][ C1] common_startup_64+0x13e/0x158
[ 46.453935][ C1] </TASK>
[ 46.453942][ C1]
[ 46.453946][ C1] Allocated by task 9488:
[ 46.453955][ C1] kasan_save_stack+0x24/0x50
[ 46.453985][ C1] kasan_save_track+0x14/0x30
[ 46.454014][ C1] __kasan_kmalloc+0xaa/0xb0
[ 46.454043][ C1] __kmalloc_cache_noprof+0x2da/0x6e0
[ 46.454066][ C1] kernfs_fop_open+0x34a/0x1170
[ 46.454088][ C1] do_dentry_open+0x68e/0x14f0
[ 46.454107][ C1] vfs_open_consume+0xcc/0x460
[ 46.454132][ C1] path_openat+0x1c16/0x2240
[ 46.454154][ C1] do_file_open+0x216/0x470
[ 46.454185][ C1] do_sys_openat2+0x139/0x1d0
[ 46.454210][ C1] __x64_sys_openat+0x13f/0x1f0
[ 46.454236][ C1] do_syscall_64+0x132/0x7c0
[ 46.454266][ C1] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 46.454285][ C1]
[ 46.454289][ C1] Freed by task 9488:
[ 46.454298][ C1] kasan_save_stack+0x24/0x50
[ 46.454327][ C1] kasan_save_track+0x14/0x30
[ 46.454355][ C1] kasan_save_free_info+0x3b/0x60
[ 46.454379][ C1] __kasan_slab_free+0x61/0x80
[ 46.454396][ C1] kfree+0x2ca/0x6e0
[ 46.454416][ C1] kernfs_fop_release+0x129/0x1f0
[ 46.454437][ C1] __fput+0x3fe/0xb50
[ 46.454460][ C1] fput_close_sync+0x10f/0x210
[ 46.454484][ C1] filp_close_sync+0x76/0xa0
[ 46.454511][ C1] __x64_sys_close+0x84/0x110
[ 46.454537][ C1] do_syscall_64+0x132/0x7c0
[ 46.454566][ C1] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 46.454586][ C1]
[ 46.454590][ C1] The buggy address belongs to the object at ffff888029c5a000
[ 46.454590][ C1] which belongs to the cache kmalloc-512 of size 512
[ 46.454607][ C1] The buggy address is located 0 bytes inside of
[ 46.454607][ C1] freed 512-byte region [ffff888029c5a000, ffff888029c5a200)
[ 46.454628][ C1]
[ 46.454632][ C1] The buggy address belongs to the physical page:
[ 46.454641][ C1] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff888029c58c00 pfn:0x29c58
[ 46.454661][ C1] head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
[ 46.454679][ C1] flags: 0xfff00000000240(workingset|head|node=0|zone=1|lastcpupid=0x7ff)
[ 46.454700][ C1] page_type: f5(slab)
[ 46.454719][ C1] raw: 00fff00000000240 ffff88801d041c80 ffffea000077f610 ffffea0000854e10
[ 46.454738][ C1] raw: ffff888029c58c00 000000000010000f 00000000f5000000 0000000000000000
[ 46.454758][ C1] head: 00fff00000000240 ffff88801d041c80 ffffea000077f610 ffffea0000854e10
[ 46.454778][ C1] head: ffff888029c58c00 000000000010000f 00000000f5000000 0000000000000000
[ 46.454798][ C1] head: 00fff00000000002 ffffffffffffff01 00000000ffffffff 00000000ffffffff
[ 46.454817][ C1] head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000004
[ 46.454829][ C1] page dumped because: kasan: bad access detected
[ 46.454839][ C1] page_owner tracks the page as allocated
[ 46.454845][ C1] page last allocated via order 2, migratetype Unmovable, gfp_mask 0x2d2800(GFP_NOWAIT|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC|__GFP_THISNODE), pid 5042, tgid 5042 (systemd-udevd), ts 21056793217
[ 46.454880][ C1] post_alloc_hook+0xff/0x130
[ 46.454901][ C1] get_page_from_freelist+0xdf0/0x2a10
[ 46.454924][ C1] __alloc_frozen_pages_noprof+0x28b/0x2dd0
[ 46.454948][ C1] new_slab+0xb9/0x6b0
[ 46.454971][ C1] ___slab_alloc+0x2db/0x830
[ 46.454994][ C1] __kmalloc_flags_noprof+0x463/0x8a0
[ 46.455016][ C1] alloc_slab_obj_exts+0x69/0x180
[ 46.455040][ C1] new_slab+0x515/0x6b0
[ 46.455062][ C1] refill_objects+0x10e/0x3d0
[ 46.455085][ C1] __pcs_replace_empty_main+0x35a/0x680
[ 46.455108][ C1] kmem_cache_alloc_noprof+0x562/0x6c0
[ 46.455135][ C1] anon_vma_clone+0x4dd/0x2040
[ 46.455171][ C1] anon_vma_fork+0x231/0xc00
[ 46.455188][ C1] dup_mmap+0x1252/0x2290
[ 46.455215][ C1] copy_process+0x756c/0x8680
[ 46.455241][ C1] kernel_clone+0x1f6/0x910
[ 46.455267][ C1] page last free pid 24 tgid 24 ts 17342984235 stack trace:
[ 46.455282][ C1] __free_frozen_pages+0x75e/0x1190
[ 46.455300][ C1] rcu_core+0x5f0/0x1260
[ 46.455324][ C1] handle_softirqs+0x20e/0x9e0
[ 46.455349][ C1] run_ksoftirqd+0x3a/0x60
[ 46.455374][ C1] smpboot_thread_fn+0x2bf/0xb40
[ 46.455394][ C1] kthread+0x390/0x4a0
[ 46.455414][ C1] ret_from_fork+0xb0c/0xdb0
[ 46.455442][ C1] ret_from_fork_asm+0x1a/0x30
[ 46.455468][ C1]
[ 46.455472][ C1] Memory state around the buggy address:
[ 46.455481][ C1] ffff888029c59f00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[ 46.455496][ C1] ffff888029c59f80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[ 46.455511][ C1] >ffff888029c5a000: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 46.455522][ C1] ^
[ 46.455532][ C1] ffff888029c5a080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 46.455547][ C1] ffff888029c5a100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 46.455558][ C1] ==================================================================
On reset, dequeue the data and status requests before re-arming the
CBW, and free the command on -ECONNRESET/-ESHUTDOWN in the BOT
completions.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
drivers/usb/gadget/function/f_tcm.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -63,7 +63,7 @@ static void bot_status_complete(struct u
struct f_uas *fu = cmd->fu;
transport_generic_free_cmd(&cmd->se_cmd, 0);
- if (req->status == -ESHUTDOWN)
+ if (req->status == -ESHUTDOWN || req->status == -ECONNRESET)
return;
if (req->status < 0)
@@ -93,6 +93,11 @@ static void bot_err_compl(struct usb_ep
struct usbg_cmd *cmd = req->context;
struct f_uas *fu = cmd->fu;
+ if (req->status == -ESHUTDOWN || req->status == -ECONNRESET) {
+ transport_generic_free_cmd(&cmd->se_cmd, 0);
+ return;
+ }
+
if (req->status < 0)
pr_err("ERR %s(%d)\n", __func__, __LINE__);
@@ -111,6 +116,12 @@ static void bot_err_compl(struct usb_ep
bot_enqueue_sense_code(fu, cmd);
}
+static void bot_dequeue_req(struct usb_ep *ep, struct usb_request *req)
+{
+ if (req && req->status == -EINPROGRESS)
+ usb_ep_dequeue(ep, req);
+}
+
static void bot_send_bad_status(struct usbg_cmd *cmd)
{
struct f_uas *fu = cmd->fu;
@@ -198,7 +209,7 @@ static void bot_read_compl(struct usb_ep
if (req->status < 0)
pr_err("ERR %s(%d)\n", __func__, __LINE__);
- if (req->status == -ESHUTDOWN) {
+ if (req->status == -ESHUTDOWN || req->status == -ECONNRESET) {
transport_generic_free_cmd(&cmd->se_cmd, 0);
return;
}
@@ -455,7 +466,9 @@ static int usbg_bot_setup(struct usb_fun
return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
case US_BULK_RESET_REQUEST:
- /* XXX maybe we should remove previous requests for IN + OUT */
+ bot_dequeue_req(fu->ep_in, fu->bot_req_in);
+ bot_dequeue_req(fu->ep_out, fu->bot_req_out);
+ bot_dequeue_req(fu->ep_in, fu->bot_status.req);
if (fu->flags & USBG_BOT_WEDGED) {
fu->flags &= ~USBG_BOT_WEDGED;
usb_ep_clear_halt(fu->ep_in);
@@ -1081,7 +1094,8 @@ static void usbg_data_write_cmpl(struct
cmd->state = UASP_QUEUE_COMMAND;
- if (req->status == -ESHUTDOWN) {
+ if (req->status == -ESHUTDOWN ||
+ (req->status == -ECONNRESET && (cmd->fu->flags & USBG_IS_BOT))) {
struct uas_stream *stream = &cmd->fu->stream[se_cmd->map_tag];
hash_del(&stream->node);
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage
2026-11-29 13:47 [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset Jaegyun Lee
@ 2026-11-29 13:47 ` Jaegyun Lee
2026-11-29 13:56 ` sashiko-bot
2026-11-29 14:04 ` [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Jaegyun Lee @ 2026-11-29 13:47 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Jaegyun Lee, Thinh Nguyen, linux-usb, linux-kernel, stable
The US_BULK_RESET_REQUEST case of usbg_bot_setup() ends with
bot_enqueue_cmd_cbw(fu);
return 0;
and never queues anything on ep0. composite_setup() leaves the status
stage of a class request to the function's ->setup(), as the
US_BULK_GET_MAX_LUN case right above does. The host never sees the
reset complete and the control transfer times out. The request type,
wValue and wLength of the reset request are not checked either.
Reproduced: a Bulk-Only Mass Storage Reset
(bmRequestType 0x21, bRequest 0xff, wLength 0) to a tcm BOT function
fails with ETIMEDOUT:
[ 46.051694][ T9631] usb 1-1: usbfs: USBDEVFS_CONTROL failed cmd tcm_bot_reset rqt 33 rq 255 len 0 ret -110
Check the request format and queue the zero-length status stage.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
drivers/usb/gadget/function/f_tcm.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index f7dc09f4021de..c46d48cc348ce 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -466,6 +466,10 @@ static int usbg_bot_setup(struct usb_function *f,
return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
case US_BULK_RESET_REQUEST:
+ if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_CLASS |
+ USB_RECIP_INTERFACE) || w_value || w_length)
+ return -EINVAL;
+
bot_dequeue_req(fu->ep_in, fu->bot_req_in);
bot_dequeue_req(fu->ep_out, fu->bot_req_out);
bot_dequeue_req(fu->ep_in, fu->bot_status.req);
@@ -475,7 +479,9 @@ static int usbg_bot_setup(struct usb_function *f,
}
bot_enqueue_cmd_cbw(fu);
- return 0;
+
+ cdev->req->length = 0;
+ return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
}
return -ENOTSUPP;
}
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage
2026-11-29 13:47 ` [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage Jaegyun Lee
@ 2026-11-29 13:56 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-29 13:56 UTC (permalink / raw)
To: Jaegyun Lee; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] usb: gadget: f_tcm: data race in usbg_bot_setup() and
bot_cmd_complete() [1]
--
commit 8c8c2b026d0d7c87ed616a18a1d98e7e4805cec5
Author: Jaegyun Lee <dreams200260@gmail.com>
usb: gadget: f_tcm: complete the Bulk-Only Reset status stage
This commit validates the USB control request fields for
US_BULK_RESET_REQUEST in usbg_bot_setup() and queues a zero-length status
stage response on ep0.
> diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
> index 2d74ca7c6d126..b213f57cb8296 100644
> --- a/drivers/usb/gadget/function/f_tcm.c
> +++ b/drivers/usb/gadget/function/f_tcm.c
> @@ -466,6 +466,10 @@ static int usbg_bot_setup(struct usb_function *f,
> return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
>
> case US_BULK_RESET_REQUEST:
> + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_CLASS |
> + USB_RECIP_INTERFACE) || w_value || w_length)
> + return -EINVAL;
> +
> bot_dequeue_req(fu->ep_in, fu->bot_req_in);
> bot_dequeue_req(fu->ep_out, fu->bot_req_out);
> bot_dequeue_req(fu->ep_in, fu->bot_status.req);
> @@ -475,7 +479,9 @@ static int usbg_bot_setup(struct usb_function *f,
> }
>
> bot_enqueue_cmd_cbw(fu);
[Severity: High]
This problem wasn't introduced by this patch, but can usbg_bot_setup() and
bot_cmd_complete() race on fu->flags [1]?
The USB gadget framework does not serialize ep0 setup callbacks against bulk
endpoint completion callbacks across CPUs. Both usbg_bot_setup() (and its
call to bot_enqueue_cmd_cbw()) and bot_cmd_complete() perform unlocked
read-modify-write updates on fu->flags:
drivers/usb/gadget/function/f_tcm.c:bot_cmd_complete() {
...
fu->flags &= ~USBG_BOT_CMD_PEND;
...
ret = bot_submit_command(fu, req->buf, req->actual);
if (ret) {
...
fu->flags |= USBG_BOT_WEDGED;
bot_enqueue_cmd_cbw(fu);
} else if (fu->flags & USBG_BOT_WEDGED) {
fu->flags &= ~USBG_BOT_WEDGED;
usb_ep_clear_halt(fu->ep_in);
}
}
If an ep0 reset arrives while an ep_out CBW completion is running on another
CPU, one update can clobber the other:
CPU0 CPU1
usbg_bot_setup() bot_cmd_complete()
[read fu->flags]
[read fu->flags]
fu->flags &= ~USBG_BOT_WEDGED; fu->flags &= ~USBG_BOT_CMD_PEND;
[write fu->flags]
[write fu->flags] <-- lost update
Losing an update to USBG_BOT_CMD_PEND or USBG_BOT_WEDGED can either
double-queue the CBW request or leave USBG_BOT_CMD_PEND stuck set so that
bot_enqueue_cmd_cbw() never queues another CBW.
[1] https://sashiko.dev/#/bug/linux-7f1632bf-7fa1-4d1c-9f74-d3b84235cdd5
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129134752.422736-1-dreams200260@gmail.com?part=2
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset
2026-11-29 13:47 [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset Jaegyun Lee
2026-11-29 13:47 ` [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage Jaegyun Lee
@ 2026-11-29 14:04 ` sashiko-bot
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-29 14:04 UTC (permalink / raw)
To: Jaegyun Lee; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] usb: gadget: f_tcm: unsynchronized BOT reset in
usbg_bot_setup()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] usb: gadget: f_tcm: use-after-free in
usbg_data_write_cmpl() [1]
--
commit 9b10055f5070b58b60638f512671af7cfafe6872
Author: Jaegyun Lee <dreams200260@gmail.com>
usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset
This commit dequeues active BOT data and status requests on a Bulk-Only
Mass Storage Reset and frees the associated target command when BOT request
completions observe -ECONNRESET.
> diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
> index 3af28419a8bca..2d74ca7c6d126 100644
> --- a/drivers/usb/gadget/function/f_tcm.c
> +++ b/drivers/usb/gadget/function/f_tcm.c
[ ... ]
> @@ -111,6 +116,12 @@ static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
> bot_enqueue_sense_code(fu, cmd);
> }
>
> +static void bot_dequeue_req(struct usb_ep *ep, struct usb_request *req)
> +{
> + if (req && req->status == -EINPROGRESS)
> + usb_ep_dequeue(ep, req);
> +}
> +
> static void bot_send_bad_status(struct usbg_cmd *cmd)
> {
> struct f_uas *fu = cmd->fu;
[ ... ]
> @@ -455,7 +466,9 @@ static int usbg_bot_setup(struct usb_function *f,
> return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
>
> case US_BULK_RESET_REQUEST:
> - /* XXX maybe we should remove previous requests for IN + OUT */
> + bot_dequeue_req(fu->ep_in, fu->bot_req_in);
> + bot_dequeue_req(fu->ep_out, fu->bot_req_out);
> + bot_dequeue_req(fu->ep_in, fu->bot_status.req);
> if (fu->flags & USBG_BOT_WEDGED) {
> fu->flags &= ~USBG_BOT_WEDGED;
> usb_ep_clear_halt(fu->ep_in);
[Severity: High]
Can a pre-reset command that has not yet called usb_ep_queue() race with
US_BULK_RESET_REQUEST and corrupt the shared fu->bot_req_in,
fu->bot_req_out, or fu->bot_status.req?
When a CBW arrives, bot_submit_command() clears USBG_BOT_CMD_PEND and hands
the command to tpg->workqueue and the target core before any data or status
request is queued on the endpoint. If US_BULK_RESET_REQUEST arrives while
that command is still executing in bot_cmd_work() or the target backend,
none of the three BOT requests has status == -EINPROGRESS, so
bot_dequeue_req() is a no-op and usbg_bot_setup() immediately re-arms CBW
reception via bot_enqueue_cmd_cbw():
CPU0 CPU1
bot_cmd_complete()
bot_submit_command()
queue_work(..., &cmd1->work)
usbg_bot_setup(US_BULK_RESET_REQUEST)
bot_dequeue_req(...) // not queued yet
bot_enqueue_cmd_cbw(fu)
...
// host sends second CBW (cmd2)
bot_send_read_response(cmd1)
fu->bot_req_in->buf = ...
usb_ep_queue(ep_in, bot_req_in)
bot_send_read_response(cmd2)
fu->bot_req_in->buf = ...
fu->bot_req_in->length = ...
<-- overwrites queued request
The second command rewrites fu->bot_req_in while it is queued for the first
command (and leaks cmd1 when ->context is overwritten), hitting the same
use-after-free/out-of-bounds access in the UDC that this patch aims to fix.
The same overlap happens on UDCs where usb_ep_dequeue() completes
asynchronously after usbg_bot_setup() has already re-armed the CBW.
> @@ -1081,7 +1094,8 @@ static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
>
> cmd->state = UASP_QUEUE_COMMAND;
>
> - if (req->status == -ESHUTDOWN) {
> + if (req->status == -ESHUTDOWN ||
> + (req->status == -ECONNRESET && (cmd->fu->flags & USBG_IS_BOT))) {
> struct uas_stream *stream = &cmd->fu->stream[se_cmd->map_tag];
>
> hash_del(&stream->node);
[Severity: Critical]
This problem wasn't introduced by this patch, but in UASP mode an aborted
write request also completes in usbg_data_write_cmpl() with -ECONNRESET
after se_cmd has already been freed [1]:
CPU0 CPU1
target_handle_abort()
usbg_aborted_task()
usb_ep_dequeue() // async
transport_cmd_check_stop_to_fabric()
usbg_check_stop_free()
target_put_sess_cmd(se_cmd) // freed
usbg_data_write_cmpl()
// req->status == -ECONNRESET
if (req->status)
goto cleanup;
...
cleanup:
target_put_sess_cmd(se_cmd)
<-- use-after-free
Because the new -ECONNRESET check is gated on USBG_IS_BOT, UASP completions
still fall through to cleanup and dereference the freed se_cmd in
target_put_sess_cmd() and transport_send_check_condition_and_sense().
[1] https://sashiko.dev/#/bug/linux-4c6d71b2-1641-4aa6-b90a-4e859651f9b4
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129134752.422736-1-dreams200260@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-29 14:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 13:47 [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset Jaegyun Lee
2026-11-29 13:47 ` [PATCH 2/2] usb: gadget: f_tcm: complete the Bulk-Only Reset status stage Jaegyun Lee
2026-11-29 13:56 ` sashiko-bot
2026-11-29 14:04 ` [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox