[PATCH v9 3/3] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request
From: Yehyeong Lee
Date: Fri Nov 20 2026 - 12:33:16 EST
bot_send_bad_status() reuses fu->bot_req_in/bot_req_out to send the CSW or
drain the data phase, but a preceding sg command left req->sg and
req->num_sgs pointing at that command's t_data_sg - set in
bot_send_read_response() for a read, or usbg_prepare_w_request() for a
write. That command is freed before the status request runs, so on an
sg-capable UDC the status request is handed to the controller with a freed
scatter list: the controller reads freed memory out to the host on an IN
endpoint, or writes host data into it on an OUT endpoint.
Clear req->num_sgs and req->sg so the UDC uses the linear req->buf instead.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
v9: move the test note out of the changelog (Greg).
dummy_hcd does not do sg, so this was tested with a small module standing
in for the controller's queue path, dereferencing req->sg the way an
sg-capable UDC would. It reports a KASAN slab-use-after-free on the freed
entry for a read command followed by a rejected command that reuses
bot_req_in, 20 runs out of 20 without this patch and 0 out of 20 with it.
A normal sg read still queues its own scatter list.
drivers/usb/gadget/function/f_tcm.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index a8fdb24f9d94e5..b21db34fe2a7ac 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -140,6 +140,9 @@ static void bot_send_bad_status(struct usbg_cmd *cmd)
req->complete = bot_err_compl;
req->context = cmd;
req->buf = fu->cmd[0].buf;
+ /* the status request is reused; drop the previous command's sg list */
+ req->num_sgs = 0;
+ req->sg = NULL;
usb_ep_queue(ep, req, GFP_ATOMIC);
} else {
bot_enqueue_sense_code(fu, cmd);
--
2.43.0