From: sashiko-bot@kernel.org
To: "Jaegyun Lee" <dreams200260@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset
Date: Sun, 29 Nov 2026 14:04:06 +0000 [thread overview]
Message-ID: <sashiko-outbox-239678@kernel.org> (raw)
In-Reply-To: <20261129134752.422736-1-dreams200260@gmail.com>
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
prev parent reply other threads:[~2026-11-29 14:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` sashiko-bot [this message]
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=sashiko-outbox-239678@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dreams200260@gmail.com \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox