All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jaegyun Lee <dreams200260@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>
Cc: Jaegyun Lee <dreams200260@gmail.com>,
	Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset
Date: Sun, 29 Nov 2026 22:47:51 +0900	[thread overview]
Message-ID: <20261129134752.422736-1-dreams200260@gmail.com> (raw)

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);

             reply	other threads:[~2026-11-29 13:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:47 Jaegyun Lee [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129134752.422736-1-dreams200260@gmail.com \
    --to=dreams200260@gmail.com \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.