From: sashiko-bot@kernel.org
To: hanzhijian1991@gmail.com
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind
Date: Tue, 01 Dec 2026 23:12:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-244374@kernel.org> (raw)
In-Reply-To: <20261201173001.3141592-1-hanzhijian1991@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: gadget: raw_gadget: NULL deref and UAF on dev->req in
raw_process_ep0_io()
- [Low] usb: gadget: raw_gadget: reply notes in changelog and missing
Fixes trailer
--
commit 4ca9a08e6a4e33547f611152d2201e23e273cbdf
Author: Zhijian Han <hanzhijian1991@gmail.com>
> usb: gadget: raw_gadget: fix ep0 request leak on unbind
>
> You're right, thanks. I re-checked the syzbot console log: it never
> prints "failed to queue connect event", so the reproducer does not hit
> the raw_queue_event() failure path my previous patch targeted. That
> path is also effectively unreachable: USB_RAW_EVENT_CONNECT is the
> first event ever queued for a freshly bound device, so
> raw_event_queue_add() can only fail there under OOM.
>
> The actual leak: the reproducer unbinds and rebinds the dummy_udc
> driver through sysfs. When the UDC is bound again, gadget_bind() runs
> a second time and overwrites dev->req, leaking the request allocated
> by the first bind — gadget_unbind() never releases it.
>
> This patch releases dev->req in gadget_unbind(), while the gadget and
> its ep0 are still valid. (A similar fix was previously tested on
> syzbot for the same bug report; #syz test is currently disabled
> upstream, so I could not attach a fresh result.)
>
> gadget_bind() allocates an ep0 request and stores it in dev->req, but
> gadget_unbind() never releases it. The request is only freed by
> dev_free(), which runs when the last reference to the raw_gadget device
> is dropped. When the UDC is unbound and then bound again (e.g. via
> sysfs), gadget_bind() is called a second time and overwrites dev->req
> with a newly allocated request, leaking the old one.
>
> syzbot reproduces this by unbinding/rebinding the dummy_udc driver
> through sysfs while running a raw-gadget instance: each re-bind leaks
> a 128-byte dummy_hcd request.
>
> Free the request in gadget_unbind() instead, while the gadget and its
> ep0 are still valid. Dequeue the request first if an ep0 transfer is
> still in flight.
>
> Reported-by: syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=d83b3c49738aea97c0e5
> Signed-off-by: Zhijian Han <hanzhijian1991@gmail.com>
[Severity: Low]
The first three paragraphs of the commit message look like conversational
reply notes that belong below the "---" tearline rather than in the
changelog. Also, should this include a Fixes tag?
Fixes: f2c2e717642c ("usb: gadget: add raw-gadget interface")
> diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
> index 4febf8dac7ca5..aedc7a305cc41 100644
> --- a/drivers/usb/gadget/legacy/raw_gadget.c
> +++ b/drivers/usb/gadget/legacy/raw_gadget.c
> @@ -328,8 +328,30 @@ static int gadget_bind(struct usb_gadget *gadget,
> static void gadget_unbind(struct usb_gadget *gadget)
> {
> struct raw_dev *dev = get_gadget_data(gadget);
> + struct usb_request *req;
> + unsigned long flags;
> + bool queued;
>
> set_gadget_data(gadget, NULL);
> +
> + spin_lock_irqsave(&dev->lock, flags);
> + req = dev->req;
> + dev->req = NULL;
> + queued = dev->ep0_urb_queued;
> + spin_unlock_irqrestore(&dev->lock, flags);
> +
> + /*
> + * Free the ep0 request allocated in gadget_bind(). If the UDC is
> + * re-bound to this driver, a new gadget_bind() call would otherwise
> + * overwrite dev->req and leak the previous request. Do it here,
> + * while the gadget and its ep0 are still valid.
> + */
> + if (req) {
> + if (queued)
> + usb_ep_dequeue(gadget->ep0, req);
> + usb_ep_free_request(gadget->ep0, req);
> + }
[Severity: High]
Can clearing and freeing dev->req in gadget_unbind() cause a NULL pointer
dereference or use-after-free in raw_process_ep0_io()?
gadget_unbind() sets dev->req = NULL without clearing dev->gadget or
changing dev->state, and raw_process_ep0_io() checks !dev->gadget under
dev->lock without checking !dev->req:
drivers/usb/gadget/legacy/raw_gadget.c:raw_process_ep0_io() {
...
if (!dev->gadget) {
dev_dbg(dev->dev, "fail, gadget is not bound\n");
ret = -EBUSY;
goto out_unlock;
}
...
dev->req->buf = data;
dev->req->length = io->length;
dev->req->zero = usb_raw_io_flags_zero(io->flags);
dev->ep0_urb_queued = true;
spin_unlock_irqrestore(&dev->lock, flags);
ret = usb_ep_queue(dev->gadget->ep0, dev->req, GFP_KERNEL);
...
}
If a SETUP packet set ep0_in_pending or ep0_out_pending before unbind, a
subsequent USB_RAW_IOCTL_EP0_WRITE or USB_RAW_IOCTL_EP0_READ call passes
all checks in raw_process_ep0_io() and dereferences dev->req while it is
NULL.
In addition, raw_process_ep0_io() drops dev->lock with ep0_urb_queued =
true before calling usb_ep_queue() and later calls usb_ep_dequeue()
outside dev->lock when interrupted:
CPU0 CPU1
raw_process_ep0_io()
spin_lock_irqsave(&dev->lock, ...)
dev->ep0_urb_queued = true;
spin_unlock_irqrestore(&dev->lock, ...)
gadget_unbind()
spin_lock_irqsave(&dev->lock, ...)
req = dev->req;
dev->req = NULL;
queued = dev->ep0_urb_queued;
spin_unlock_irqrestore(&dev->lock, ...)
usb_ep_dequeue(gadget->ep0, req);
usb_ep_free_request(gadget->ep0, req);
usb_ep_queue(..., dev->req, ...) <-- NULL or freed request
Because req has not been queued to ep0 yet when gadget_unbind() runs,
usb_ep_dequeue() returns -EINVAL and usb_ep_free_request() frees req while
CPU0 is about to queue it (or while CPU0 is waiting in
wait_for_completion_interruptible() and then calls usb_ep_dequeue() with
dev->req).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201173001.3141592-1-hanzhijian1991@gmail.com?part=1
prev parent reply other threads:[~2026-12-01 23:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 1:28 [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure hanzhijian1991
2026-11-24 1:49 ` sashiko-bot
2026-11-24 2:38 ` Andrey Konovalov
2026-12-01 23:03 ` [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind hanzhijian1991
2026-12-01 23:12 ` 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-244374@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=hanzhijian1991@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