* [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure
@ 2026-11-24 1:28 hanzhijian1991
2026-11-24 1:49 ` sashiko-bot
2026-11-24 2:38 ` Andrey Konovalov
0 siblings, 2 replies; 3+ messages in thread
From: hanzhijian1991 @ 2026-11-24 1:28 UTC (permalink / raw)
To: linux-usb; +Cc: andreyknvl, gregkh, linux-kernel, syzbot+d83b3c49738aea97c0e5
From: Zhijian Han <hanzhijian1991@gmail.com>
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.
Reported-by: syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?extid=d83b3c49738aea97c0e5
Signed-off-by: Zhijian Han <hanzhijian1991@gmail.com>
---
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(+)
diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
index 4febf8dac7ca..33d70b552b6d 100644
--- a/drivers/usb/gadget/legacy/raw_gadget.c
+++ b/drivers/usb/gadget/legacy/raw_gadget.c
@@ -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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure
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
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-24 1:49 UTC (permalink / raw)
To: hanzhijian1991, linux-usb
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 <hanzhijian1991@gmail.com>
> usb: gadget: raw_gadget: fix memory leak on connect event failure
>
> 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.
>
> Reported-by: syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com
> Link: https://syzkaller.appspot.com/bug?extid=d83b3c49738aea97c0e5
> Signed-off-by: Zhijian Han <hanzhijian1991@gmail.com>
[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")?
> diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
> index 4febf8dac7ca5..33d70b552b6d9 100644
> --- a/drivers/usb/gadget/legacy/raw_gadget.c
> +++ b/drivers/usb/gadget/legacy/raw_gadget.c
> @@ -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 CPU1
gadget_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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124012837.2071714-1-hanzhijian1991@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure
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
1 sibling, 0 replies; 3+ messages in thread
From: Andrey Konovalov @ 2026-11-24 2:38 UTC (permalink / raw)
To: hanzhijian1991
Cc: linux-usb, gregkh, linux-kernel, syzbot+d83b3c49738aea97c0e5
On Tue, Nov 24, 2026 at 2:28 AM <hanzhijian1991@gmail.com> wrote:
>
> From: Zhijian Han <hanzhijian1991@gmail.com>
>
> 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.
>
> Reported-by: syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com
> Link: https://syzkaller.appspot.com/bug?extid=d83b3c49738aea97c0e5
Why does this change fix the linked issue? The syzbot reproducer does
a bind-unbind-bind sequence without raw_queue_event() failing AFAICT.
> Signed-off-by: Zhijian Han <hanzhijian1991@gmail.com>
> ---
> 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(+)
>
> diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
> index 4febf8dac7ca..33d70b552b6d 100644
> --- a/drivers/usb/gadget/legacy/raw_gadget.c
> +++ b/drivers/usb/gadget/legacy/raw_gadget.c
> @@ -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
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 2:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox