mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: liuc63 <liuc63@xiaopeng.com>
To: gregkh@linuxfoundation.org
Cc: cernekee@gmail.com, sebastian@breakpoint.cc, balbi@ti.com,
	liuwb@xiaopeng.com, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, Liu Chao <liuc63@xiaopeng.com>,
	stable@vger.kernel.org
Subject: [PATCH] usb: gadget: bcm63xx_udc: fix dequeue of unqueued request
Date: Fri, 23 Oct 2026 16:08:29 +0800	[thread overview]
Message-ID: <20261023080837.1680100-1-liuc63@xiaopeng.com> (raw)

From: Liu Chao <liuc63@xiaopeng.com>

bcm63xx_udc_dequeue() only checks whether bep->queue is empty; it never
verifies that the passed request is actually linked on that queue.  If
the request was already completed by the data ISR (which list_del's it)
or belongs to another endpoint, list_del() corrupts an unrelated list
node.  Worse, the out: path unconditionally sets req->status and calls
req->complete() even when rc == -EINVAL, so a request that was never
queued (or was already completed and possibly freed by the gadget
driver) gets completed a second time, leading to a double completion
and use-after-free.

Walk bep->queue to confirm the request is queued before touching it,
and only invoke the completion callback when the request was actually
removed from the queue.  The -EINVAL path now returns without
completing.

Fixes: 613065e53cb1 ("usb: gadget: bcm63xx UDC driver")
Cc: stable@vger.kernel.org
Reviewed-by: Weibin Liu <liuwb@xiaopeng.com>
Signed-off-by: Liu Chao <liuc63@xiaopeng.com>
---
 drivers/usb/gadget/udc/bcm63xx_udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/gadget/udc/bcm63xx_udc.c b/drivers/usb/gadget/udc/bcm63xx_udc.c
index 502612a56..a27dc7df7 100644
--- a/drivers/usb/gadget/udc/bcm63xx_udc.c
+++ b/drivers/usb/gadget/udc/bcm63xx_udc.c
@@ -1209,15 +1209,20 @@ static int bcm63xx_udc_dequeue(struct usb_ep *ep, struct usb_request *req)
 	int rc = 0;
 
 	spin_lock_irqsave(&udc->lock, flags);
-	if (list_empty(&bep->queue)) {
+
+	/* Make sure the request is actually queued on this endpoint */
+	list_for_each_entry(cur, &bep->queue, queue) {
+		if (cur == breq)
+			break;
+	}
+	if (&cur->queue == &bep->queue) {
 		rc = -EINVAL;
 		goto out;
 	}
 
-	cur = list_first_entry(&bep->queue, struct bcm63xx_req, queue);
 	usb_gadget_unmap_request(&udc->gadget, &breq->req, bep->iudma->is_tx);
 
-	if (breq == cur) {
+	if (breq == list_first_entry(&bep->queue, struct bcm63xx_req, queue)) {
 		iudma_reset_channel(udc, bep->iudma);
 		list_del(&breq->queue);
 
@@ -1235,8 +1240,11 @@ static int bcm63xx_udc_dequeue(struct usb_ep *ep, struct usb_request *req)
 out:
 	spin_unlock_irqrestore(&udc->lock, flags);
 
-	req->status = -ESHUTDOWN;
-	req->complete(ep, req);
+	/* Only complete the request if it was actually removed */
+	if (rc == 0) {
+		req->status = -ESHUTDOWN;
+		req->complete(ep, req);
+	}
 
 	return rc;
 }
--
2.50.1


                 reply	other threads:[~2026-10-23  8:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261023080837.1680100-1-liuc63@xiaopeng.com \
    --to=liuc63@xiaopeng.com \
    --cc=balbi@ti.com \
    --cc=cernekee@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=liuwb@xiaopeng.com \
    --cc=sebastian@breakpoint.cc \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®