* [PATCH v8 1/4] usb: gadget: f_tcm: queue the BOT status requests with GFP_ATOMIC
2026-11-18 12:40 [PATCH v8 0/4] usb: gadget: f_tcm: fix infoleaks, sleep, and UAF in the BOT path Yehyeong Lee
@ 2026-11-18 12:40 ` Yehyeong Lee
2026-11-18 12:43 ` sashiko-bot
2026-11-18 12:40 ` [PATCH v8 2/4] usb: gadget: f_tcm: zero the CBW buffer the status path sends Yehyeong Lee
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Yehyeong Lee @ 2026-11-18 12:40 UTC (permalink / raw)
To: gregkh, nkapron, linux-usb
Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel, benquike
bot_send_bad_status() and bot_send_status() run from endpoint completion
callbacks - bot_send_bad_status() via usbg_data_write_cmpl()'s cleanup
path, and both of them via bot_read_compl() after a BOT read. Those
callbacks are called with interrupts disabled and must not sleep, but both
functions queue the status or drain request with GFP_KERNEL, which can.
Queue with GFP_ATOMIC, like the other usb_ep_queue() calls reached from the
same callbacks (bot_enqueue_sense_code() and bot_err_compl()).
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
drivers/usb/gadget/function/f_tcm.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 9e6d4f39900a27..12e2b20e33b921 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -139,7 +139,7 @@ static void bot_send_bad_status(struct usbg_cmd *cmd)
req->complete = bot_err_compl;
req->context = cmd;
req->buf = fu->cmd[0].buf;
- usb_ep_queue(ep, req, GFP_KERNEL);
+ usb_ep_queue(ep, req, GFP_ATOMIC);
} else {
bot_enqueue_sense_code(fu, cmd);
}
@@ -167,7 +167,7 @@ static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
csw->Status = US_BULK_STAT_OK;
fu->bot_status.req->context = cmd;
- ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
+ ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
if (ret)
pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
} else {
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v8 2/4] usb: gadget: f_tcm: zero the CBW buffer the status path sends
2026-11-18 12:40 [PATCH v8 0/4] usb: gadget: f_tcm: fix infoleaks, sleep, and UAF in the BOT path Yehyeong Lee
2026-11-18 12:40 ` [PATCH v8 1/4] usb: gadget: f_tcm: queue the BOT status requests with GFP_ATOMIC Yehyeong Lee
@ 2026-11-18 12:40 ` Yehyeong Lee
2026-11-18 12:45 ` sashiko-bot
2026-11-18 12:40 ` [PATCH v8 3/4] usb: gadget: f_tcm: reject a short write data-out transfer Yehyeong Lee
2026-11-18 12:40 ` [PATCH v8 4/4] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request Yehyeong Lee
3 siblings, 1 reply; 9+ messages in thread
From: Yehyeong Lee @ 2026-11-18 12:40 UTC (permalink / raw)
To: gregkh, nkapron, linux-usb
Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel, benquike
bot_prepare_reqs() allocates fu->cmd[0].buf with kmalloc(), one endpoint
maxpacket in size, and the only thing ever written into it is the 31-byte
CBW the host sends. The rest is never initialised.
When a command is rejected before its data stage, bot_send_bad_status()
points the status request at that same buffer and queues it to the IN
endpoint for a read, and bot_err_compl() re-queues it until the length the
host declared in the CBW is satisfied. So the host gets the buffer's
contents back, a maxpacket at a time, for as long a transfer as it asked
for, and whatever lies past the CBW it never wrote.
Allocate it with kzalloc().
On a dummy_hcd gadget with a 512-byte bulk maxpacket, a rejected IN
command declaring 4096 bytes returns 4096 bytes to the host, of which 72
are non-zero and begin with the previous CBW's "USBC" signature. The
part no CBW ever writes read back as zero there, both before and after
this patch, so what that buffer held beforehand was not observed; what
the test does show is that the host is handed a buffer it only ever
wrote 31 bytes of.
Hui Peng's multi-driver fix of 2026-09-19 zeroed this buffer too; the
reviewer asked for it to be split per driver and it was not applied.
Link: https://lore.kernel.org/all/20260919223521.3890508-1-benquike@gmail.com/
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
drivers/usb/gadget/function/f_tcm.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 12e2b20e33b921..f2e7a9ad77809a 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -330,7 +330,7 @@ static int bot_prepare_reqs(struct f_uas *fu)
fu->bot_status.req->complete = bot_status_complete;
fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
- fu->cmd[0].buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
+ fu->cmd[0].buf = kzalloc(fu->ep_out->maxpacket, GFP_KERNEL);
if (!fu->cmd[0].buf)
goto err_buf;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v8 2/4] usb: gadget: f_tcm: zero the CBW buffer the status path sends
2026-11-18 12:40 ` [PATCH v8 2/4] usb: gadget: f_tcm: zero the CBW buffer the status path sends Yehyeong Lee
@ 2026-11-18 12:45 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 12:45 UTC (permalink / raw)
To: Yehyeong Lee, linux-usb
> bot_prepare_reqs() allocates fu->cmd[0].buf with kmalloc(), one endpoint
> maxpacket in size, and the only thing ever written into it is the 31-byte
> CBW the host sends. The rest is never initialised.
>
> When a command is rejected before its data stage, bot_send_bad_status()
> points the status request at that same buffer and queues it to the IN
> endpoint for a read, and bot_err_compl() re-queues it until the length the
> host declared in the CBW is satisfied. So the host gets the buffer's
> contents back, a maxpacket at a time, for as long a transfer as it asked
> for, and whatever lies past the CBW it never wrote.
>
> Allocate it with kzalloc().
>
> On a dummy_hcd gadget with a 512-byte bulk maxpacket, a rejected IN
> command declaring 4096 bytes returns 4096 bytes to the host, of which 72
> are non-zero and begin with the previous CBW's "USBC" signature. The
> part no CBW ever writes read back as zero there, both before and after
> this patch, so what that buffer held beforehand was not observed; what
> the test does show is that the host is handed a buffer it only ever
> wrote 31 bytes of.
>
> Hui Peng's multi-driver fix of 2026-09-19 zeroed this buffer too; the
> reviewer asked for it to be split per driver and it was not applied.
>
> Link: https://lore.kernel.org/all/20260919223521.3890508-1-benquike@gmail.com/
> Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118124005.2428068-1-yhlee@isslab.korea.ac.kr?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v8 3/4] usb: gadget: f_tcm: reject a short write data-out transfer
2026-11-18 12:40 [PATCH v8 0/4] usb: gadget: f_tcm: fix infoleaks, sleep, and UAF in the BOT path Yehyeong Lee
2026-11-18 12:40 ` [PATCH v8 1/4] usb: gadget: f_tcm: queue the BOT status requests with GFP_ATOMIC Yehyeong Lee
2026-11-18 12:40 ` [PATCH v8 2/4] usb: gadget: f_tcm: zero the CBW buffer the status path sends Yehyeong Lee
@ 2026-11-18 12:40 ` Yehyeong Lee
2026-11-18 12:59 ` sashiko-bot
2026-11-18 12:40 ` [PATCH v8 4/4] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request Yehyeong Lee
3 siblings, 1 reply; 9+ messages in thread
From: Yehyeong Lee @ 2026-11-18 12:40 UTC (permalink / raw)
To: gregkh, nkapron, linux-usb
Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel, benquike
usbg_data_write_cmpl() copies se_cmd->data_length bytes out of the bounce
buffer into the command's scatter-gather list without checking how much the
host actually sent. A BOT host can declare a larger OUT transfer than it
delivers and end the data stage with a short packet: req->actual is then
shorter than req->length, but the completion handler still commits the full
declared length. The tail past req->actual is whatever was already in the
kmalloc'd bounce buffer, which is not zeroed, so bytes the host never sent
are written to the backing store and can be read back by the host.
Fail the command when req->actual does not match req->length instead of
committing that tail. By the time usbg_data_write_cmpl() runs the OUT
data request has completed, so mark the command and skip the ep_out drain
in bot_send_bad_status() - there is nothing left on the wire, and draining
it re-arms ep_out for data the host already finished sending and wedges the
bulk pipe. This mirrors how f_mass_storage stops draining once the host
ends the OUT stage. A command rejected before its data stage still drains,
since the host has yet to send it.
On a dummy_hcd gadget, a WRITE(10) declaring 8192 bytes whose data stage
delivers 4000 and ends on a short packet is answered with a CSW status of
0, and reading the range back shows the 4000 bytes the host sent followed
by 4192 bytes it did not. With this patch the command fails, the CSW
status is 1, and the range reads back unchanged.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
drivers/usb/gadget/function/f_tcm.c | 14 +++++++++++---
drivers/usb/gadget/function/tcm.h | 1 +
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index f2e7a9ad77809a..970dba09d75e4a 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -120,7 +120,8 @@ static void bot_send_bad_status(struct usbg_cmd *cmd)
csw->Residue = cpu_to_le32(cmd->data_len);
- if (cmd->data_len) {
+ /* nothing to drain once the OUT data phase has completed */
+ if (cmd->data_len && !(cmd->flags & USBG_CMD_OUT_DATA_DONE)) {
if (cmd->is_read) {
ep = fu->ep_in;
req = fu->bot_req_in;
@@ -1090,8 +1091,15 @@ static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
return;
}
- if (req->status) {
- pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
+ /* the OUT data request completed; nothing is left to drain */
+ cmd->flags |= USBG_CMD_OUT_DATA_DONE;
+
+ /* a short data-out left the tail unwritten; don't commit stale data */
+ if (req->status || req->actual != req->length) {
+ struct usb_gadget *gadget = fuas_to_gadget(cmd->fu);
+
+ dev_err_ratelimited(&gadget->dev, "%s() state %d transfer failed\n",
+ __func__, cmd->state);
goto cleanup;
}
diff --git a/drivers/usb/gadget/function/tcm.h b/drivers/usb/gadget/function/tcm.h
index e1d5a939161273..50764a33cdf366 100644
--- a/drivers/usb/gadget/function/tcm.h
+++ b/drivers/usb/gadget/function/tcm.h
@@ -88,6 +88,7 @@ struct usbg_cmd {
u32 flags;
#define USBG_CMD_PENDING_DATA_WRITE BIT(0)
+#define USBG_CMD_OUT_DATA_DONE BIT(1)
/* UAS only */
u16 tag;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v8 3/4] usb: gadget: f_tcm: reject a short write data-out transfer
2026-11-18 12:40 ` [PATCH v8 3/4] usb: gadget: f_tcm: reject a short write data-out transfer Yehyeong Lee
@ 2026-11-18 12:59 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 12:59 UTC (permalink / raw)
To: Yehyeong Lee, linux-usb
> usbg_data_write_cmpl() copies se_cmd->data_length bytes out of the bounce
> buffer into the command's scatter-gather list without checking how much the
> host actually sent. A BOT host can declare a larger OUT transfer than it
> delivers and end the data stage with a short packet: req->actual is then
> shorter than req->length, but the completion handler still commits the full
> declared length. The tail past req->actual is whatever was already in the
> kmalloc'd bounce buffer, which is not zeroed, so bytes the host never sent
> are written to the backing store and can be read back by the host.
>
> Fail the command when req->actual does not match req->length instead of
> committing that tail. By the time usbg_data_write_cmpl() runs the OUT
> data request has completed, so mark the command and skip the ep_out drain
> in bot_send_bad_status() - there is nothing left on the wire, and draining
> it re-arms ep_out for data the host already finished sending and wedges the
> bulk pipe. This mirrors how f_mass_storage stops draining once the host
> ends the OUT stage. A command rejected before its data stage still drains,
> since the host has yet to send it.
>
> On a dummy_hcd gadget, a WRITE(10) declaring 8192 bytes whose data stage
> delivers 4000 and ends on a short packet is answered with a CSW status of
> 0, and reading the range back shows the 4000 bytes the host sent followed
> by 4192 bytes it did not. With this patch the command fails, the CSW
> status is 1, and the range reads back unchanged.
>
> Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118124005.2428068-1-yhlee@isslab.korea.ac.kr?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v8 4/4] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request
2026-11-18 12:40 [PATCH v8 0/4] usb: gadget: f_tcm: fix infoleaks, sleep, and UAF in the BOT path Yehyeong Lee
` (2 preceding siblings ...)
2026-11-18 12:40 ` [PATCH v8 3/4] usb: gadget: f_tcm: reject a short write data-out transfer Yehyeong Lee
@ 2026-11-18 12:40 ` Yehyeong Lee
2026-11-18 12:46 ` sashiko-bot
3 siblings, 1 reply; 9+ messages in thread
From: Yehyeong Lee @ 2026-11-18 12:40 UTC (permalink / raw)
To: gregkh, nkapron, linux-usb
Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel, benquike
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.
A test module that stands in for the controller's queue path, so that it
dereferences req->sg the way an sg-capable UDC would, 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.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
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 970dba09d75e4a..e2ecd5922ee77c 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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v8 4/4] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request
2026-11-18 12:40 ` [PATCH v8 4/4] usb: gadget: f_tcm: clear the scatter list when reusing the BOT status request Yehyeong Lee
@ 2026-11-18 12:46 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 12:46 UTC (permalink / raw)
To: Yehyeong Lee, linux-usb
> 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.
>
> A test module that stands in for the controller's queue path, so that it
> dereferences req->sg the way an sg-capable UDC would, 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.
>
> Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118124005.2428068-1-yhlee@isslab.korea.ac.kr?part=4
^ permalink raw reply [flat|nested] 9+ messages in thread