From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795961047; b=DyqZ3XQikajCtU4LcCJv+60eVn1nVxvhvdaCaqBwU+i652MfPpFliU75HqQRQ/EkIQ7+ 9SIkTXxx/LQ+M1nA+/m9ABtgv6MXDOuXNowOXSTXTs63AHayT4WHp53xtoQS4/T1lTQDB 9xNmvhVM8cjsT+PhSO2MtD2FV3cxVAZj0jILVtF0k2HkP3P3V6ugKJeBZzDLOgCGo/Tob gKAC+UaknB0GQaTjHcmFkeYXBjIN36YAxD/vj2OcOS/Q+hEuKIPdhRM5+sU1bXKxfVC+x K/dBoePBPCR1KxnaU3OD4dBOx0hAneUoBq+VTNkySwm4eRI4lZ5MqYZsN3CjqZ8oDIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795961047; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=F2Cs9J7eztYocINrvENGeffFaLPX7fV54VAFMcHhWKc=; b=YIpXWEz2D8tiDBHdKE2VH+Q4YCBHwFur4joBBsxu4+BfBEskQtGVfS0AEM66W5wVGKUc qkDm+ihG0k8QFXFmYQ/dkNBKGd8VGAktM8ROJiyeZXHdG3ZvSC9cKq+ESNRvTGgvtKF9L BJrGihJcVhEHMJuY/+Dh1RSKHHFD9Z/wP3qcUV5us7M+arCeE6kbqVeOLM04gFkwru4ER vl5owZa+B+VmkismVUYX7/c3tRrPkJrwbyGzcypRtHgdCSwL/NpBsQlQB3N7uiDTvPSaB ioZIO+rouHvLk4ddyjWV9VA9AuBnSGmLmdTAt+3nyo4W1tlFG8d0nKK+I56BZOwwngg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=njNRs67q; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 121DD606A323 for ; Sun, 29 Nov 2026 14:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=njNRs67q Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C5451F000C1; Sun, 29 Nov 2026 14:04:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795961046; bh=F2Cs9J7eztYocINrvENGeffFaLPX7fV54VAFMcHhWKc=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=njNRs67q3dq7wkm4pCrmC11DFGkdqX2vujwW3tMCDh1Hb2kGc6o3kQUXAnfv2wE0x AY2AGJZRjJ3hdDO+yp0UjuRIAonTaAwSvRGVU8gntHoowB2bLdwZwKeoJj0fjstnBX wfPBucstCKrpDpkTU+ahyN233qSUN434C60gULsQCYbDteweF9ZM/h0zcZr6lp7me2 +blOpyU1gB84/35FSBZw8zRy6U9E7hDGVt2dh3UHghkAbbBuv9RS4RT89gMOwzLw85 fcVqTspQzy9+oe6JT0J2EZqQGhgV4CuuRBtkFfsPTLqyJsET5EN7rCUOfwRSFATTbg xaKPndVkafsUw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 1/2] usb: gadget: f_tcm: dequeue data requests on Bulk-Only Reset Reply-To: sashiko-reviews@lists.linux.dev To: "Jaegyun Lee" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261129134752.422736-1-dreams200260@gmail.com> References: <20261129134752.422736-1-dreams200260@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Nov 2026 14:04:06 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/fun= ction/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); > } > =20 > +static void bot_dequeue_req(struct usb_ep *ep, struct usb_request *req) > +{ > + if (req && req->status =3D=3D -EINPROGRESS) > + usb_ep_dequeue(ep, req); > +} > + > static void bot_send_bad_status(struct usbg_cmd *cmd) > { > struct f_uas *fu =3D 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); > =20 > 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 &=3D ~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 =3D=3D -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 =3D ... usb_ep_queue(ep_in, bot_req_in) bot_send_read_response(cmd2) fu->bot_req_in->buf =3D ... fu->bot_req_in->length =3D ... <-- 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) > =20 > cmd->state =3D UASP_QUEUE_COMMAND; > =20 > - if (req->status =3D=3D -ESHUTDOWN) { > + if (req->status =3D=3D -ESHUTDOWN || > + (req->status =3D=3D -ECONNRESET && (cmd->fu->flags & USBG_IS_BOT)))= { > struct uas_stream *stream =3D &cmd->fu->stream[se_cmd->map_tag]; > =20 > 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 =3D=3D -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 --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129134752.4227= 36-1-dreams200260@gmail.com?part=3D1