mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure
@ 2026-11-24  1:28 hanzhijian1991
  2026-11-24  2:38 ` Andrey Konovalov
  0 siblings, 1 reply; 4+ 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	[flat|nested] 4+ 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  2:38 ` Andrey Konovalov
  2026-12-01 23:03   ` [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind hanzhijian1991
  0 siblings, 1 reply; 4+ 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] 4+ messages in thread

* [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind
  2026-11-24  2:38 ` Andrey Konovalov
@ 2026-12-01 23:03   ` hanzhijian1991
  2026-12-03 19:08     ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: hanzhijian1991 @ 2026-12-01 23:03 UTC (permalink / raw)
  To: andreyknvl, gregkh
  Cc: Zhijian Han, linux-usb, linux-kernel, syzbot+d83b3c49738aea97c0e5

From: Zhijian Han <hanzhijian1991@gmail.com>

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>
---
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(+)

diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
index 4febf8dac..aedc7a305 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);
+	}
+
 	/* Matches kref_get() in gadget_bind(). */
 	kref_put(&dev->count, dev_free);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind
  2026-12-01 23:03   ` [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind hanzhijian1991
@ 2026-12-03 19:08     ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2026-12-03 19:08 UTC (permalink / raw)
  To: hanzhijian1991
  Cc: andreyknvl, linux-usb, linux-kernel, syzbot+d83b3c49738aea97c0e5

On Wed, Dec 02, 2026 at 07:03:02AM +0800, hanzhijian1991@gmail.com wrote:
> From: Zhijian Han <hanzhijian1991@gmail.com>
> 
> 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.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04  3:25 UTC | newest]

Thread overview: 4+ 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  2:38 ` Andrey Konovalov
2026-12-01 23:03   ` [PATCH v4] usb: gadget: raw_gadget: fix ep0 request leak on unbind hanzhijian1991
2026-12-03 19:08     ` Greg KH

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®