usb: gadget: raw_gadget: fix memory leak on connect event failure
Subsystems (2)
PATCH v3 1/1 usb: gadget: raw_gadget: fix memory leak on connect event failure
From: Zhijian Han <[email protected]>
gadget_bind() allocates an ep0 request and stores it in dev->req. If raw_queue_event() fails to queue the connect event, the function returns without releasing the request, leaking it.
Release the request before returning in that case.
---
Changes in v3:
- Move the changelog below the --- separator and drop the duplicated entry, as requested by Greg KH.
Changes in v2:
- Fix author name and add missing Signed-off-by (full name), as requested by Greg KH.
--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/gadget/legacy/raw_gadget.c +2 -0
@@ -316,6 +316,8 @@ static int gadget_bind(struct usb_gadget *gadget, ret =raw_queue_event (dev,USB_RAW_EVENT_CONNECT ,0 ,NULL );if (ret <0 ) {dev_err (&gadget->dev,"failed to queue connect event\n" ); +usb_ep_free_request (gadget->ep0, dev->req); + dev->req =NULL ;set_gadget_data (gadget,NULL );return ret; }
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] usb: gadget: raw_gadget: unlocked dev->req free in gadget_bind() error path - [Low] usb: gadget…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] usb: gadget: raw_gadget: unlocked dev->req free in gadget_bind() error path
- [Low] usb: gadget: raw_gadget: missing Fixes trailer for gadget_bind() leak fix
--
commit 02db8ff1f01d6f46fdd516cc5c3f8446a7fb6921
Author: Zhijian Han <[email protected]>
Quoted 11 lines
usb: gadget:
raw_gadget: fix memory leak on connect event failure
gadget_bind()allocates an ep0 request and stores it indev->req. Ifraw_queue_event()fails to queue the connect event, the function returns without releasing the request, leaking it.Release the request before returning in that case.
Reported-by [email protected]Signed-off-by Zhijian Han <[email protected]>
[Severity: Low]
Should this bug fix include a Fixes: tag for 83e30f2bf86e ("USB: gadget: Fix the memory leak in raw_gadget driver") or f2c2e717642c ("usb: gadget: add raw-gadget interface")?
drivers/usb/gadget/legacy/raw_gadget.c static int gadget_bind(struct usb_gadget *gadget, · line 323
Quoted 13 lines
drivers/usb/gadget/legacy/raw_gadget.c +2
-0@@ -316,6 +316,8 @@ static int gadget_bind(struct usb_gadget *gadget, ret =raw_queue_event (dev,USB_RAW_EVENT_CONNECT ,0 ,NULL );if (ret <0 ) {dev_err (&gadget->dev,"failed to queue connect event\n" ); +usb_ep_free_request (gadget->ep0, dev->req); + dev->req =NULL ;set_gadget_data (gadget,NULL );return ret; }
[Severity: Medium]
Can freeing and clearing dev->req without holding dev->lock race with raw_process_ep0_io()?
A running raw-gadget driver can be unbound and rebound via sysfs while dev->state remains STATE_DEV_RUNNING. Because gadget_bind() publishes dev->req and dev->gadget under dev->lock and drops the lock before calling raw_queue_event(), a concurrent raw_process_ep0_io() can observe STATE_DEV_RUNNING before raw_queue_event() fails and sets STATE_DEV_FAILED:
CPU0 CPU1gadget_bind()
spin_lock_irqsave(&dev->lock, ...)
dev->req = req;
dev->gadget = gadget;
spin_unlock_irqrestore(&dev->lock, ...)
raw_process_ep0_io()
spin_lock_irqsave(&dev->lock, ...)
/* dev->state == STATE_DEV_RUNNING */
dev->req->buf = dev->ep0_urb->buf;
dev->ep0_urb_queued = true;
spin_unlock_irqrestore(&dev->lock, ...)
raw_queue_event() -> fails
dev->state = STATE_DEV_FAILED; usb_ep_free_request(..., dev->req)
dev->req = NULL;
usb_ep_queue(..., dev->req, ...)
<-- NULL deref or use-after-free
Even if raw_process_ep0_io() calls usb_ep_queue() just before usb_ep_free_request() runs, freeing an in-flight request or passing a NULL dev->req to usb_ep_dequeue() on interrupt will corrupt endpoint state.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Andrey Konovalov
reviewerQuoted 11 lines · On Tue, Nov 24, 2026 at 2:28 AM <[email protected]> wrote:
From: Zhijian Han <[email protected]>
gadget_bind()allocates an ep0 request and stores it indev->req. Ifraw_queue_event()fails to queue the connect event, the function returns without releasing the request, leaking it.Release the request before returning in that case.
Why does this change fix the linked issue? The syzbot reproducer does a bind-unbind-bind sequence without raw_queue_event() failing AFAICT.
Quoted 30 lines
Signed-off-by Zhijian Han <[email protected]>---
Changes in v3:
- Move the changelog below the --- separator and drop the duplicated entry, as requested by Greg KH.Changes in v2:
- Fix author name and add missing Signed-off-by (full name), as requested by Greg KH.--- drivers/usb/gadget/legacy/raw_gadget.c | 2 ++ 1 file changed, 2 insertions(+)drivers/usb/gadget/legacy/raw_gadget.c +2
-1@@ -316,6 +316,8 @@ static int gadget_bind(struct usb_gadget *gadget, ret =raw_queue_event (dev,USB_RAW_EVENT_CONNECT ,0 ,NULL );if (ret <0 ) {dev_err (&gadget->dev,"failed to queue connect event\n" ); +usb_ep_free_request (gadget->ep0, dev->req); + dev->req =NULL ;set_gadget_data (gadget,NULL );return ret; } --2.43.0
PATCH v4 1/1 usb: gadget: raw_gadget: fix ep0 request leak on unbind
From: Zhijian Han <[email protected]>
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.
---
Changes in v4:
- Drop the raw_queue_event() failure-path fix (that path is not reachable for the CONNECT event); release dev->req in gadget_unbind() instead, which fixes the actual double-bind leak.
drivers/usb/gadget/legacy/raw_gadget.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
drivers/usb/gadget/legacy/raw_gadget.c +22 -0
@@ -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); + } +/* Matches kref_get() in gadget_bind(). */ kref_put (&dev->count, dev_free); }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind
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…
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 <[email protected]>
Quoted 30 lines
usb: gadget:
raw_gadget: fix ep0 request leak on unbindYou'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_CONNECTis the first event ever queued for a freshly bound device, soraw_event_queue_add()can only fail there under OOM.The actual leak: the reproducer unbinds and rebinds the
dummy_udcdriver through sysfs. When the UDC is bound again,gadget_bind()runs a second time and overwritesdev->req, leaking the request allocated by the first bind —gadget_unbind()never releases it.This patch releases
dev->reqingadget_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 indev->req, butgadget_unbind()never releases it. The request is only freed bydev_free(), which runs when the last reference to theraw_gadgetdevice is dropped. When the UDC is unbound and then bound again (e.g. via sysfs),gadget_bind()is called a second time and overwritesdev->reqwith a newly allocated request, leaking the old one.syzbot reproduces this by unbinding/rebinding the
dummy_udcdriver through sysfs while running a raw-gadget instance: each re-bind leaks a 128-bytedummy_hcdrequest.
Quoted 7 lines
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 [email protected]Signed-off-by Zhijian Han <[email protected]>
[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?
Quoted 21 lines
drivers/usb/gadget/legacy/raw_gadget.c +11
-0@@ -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); + +/*
drivers/usb/gadget/legacy/raw_gadget.c static int gadget_bind(struct usb_gadget *gadget, · line 353
Quoted 10 lines
drivers/usb/gadget/legacy/raw_gadget.c +10
-0+* 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 CPU1raw_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).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg KH
maintainerRe: [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind
In reply to [email protected]Quoted 8 lines · On Wed, Dec 02, 2026 at 07:03:02AM +0800, [email protected] wrote:
From: Zhijian Han <[email protected]>
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_CONNECTis the first event ever queued for a freshly bound device, soraw_event_queue_add()can only fail there under OOM.
What is this in the changelog for?
Delete this whole thing and write it on your own, no LLM at all.
thanks,
greg k-h