The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: syzbot <syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com>
To: hanzhijian1991@gmail.com
Cc: hanzhijian1991@gmail.com, linux-kernel@vger.kernel.org,
	 syzkaller-bugs@googlegroups.com
Subject: Re: [PATCH] usb: gadget: raw_gadget: fix ep0 request leak on unbind
Date: Tue, 24 Nov 2026 01:18:30 -0800	[thread overview]
Message-ID: <6b055666.71deaa98.2984e6.0016.GAE@google.com> (raw)
In-Reply-To: <20261124091825.2275478-1-hanzhijian1991@gmail.com>

> From: Zhijian Han <hanzhijian1991@gmail.com>
>
> #syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

Patch testing via #syz test is temporarily disabled.
See https://github.com/google/syzkaller/issues/8122 for details.

>
> From 6d5331190d1c541e112aa26ea25f0744eecaaadf Mon Sep 17 00:00:00 2001
> From: Zhijian Han <hanzhijian1991@gmail.com>
> Date: Tue, 24 Nov 2026 17:18:01 +0800
> Subject: [PATCH] usb: gadget: raw_gadget: fix ep0 request leak on unbind
>
> 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>
> ---
>  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
>

       reply	other threads:[~2026-11-24  9:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261124091825.2275478-1-hanzhijian1991@gmail.com>
2026-11-24  9:18 ` syzbot [this message]
     [not found] <20261124091833.2275524-1-hanzhijian1991@gmail.com>
2026-11-24  9:18 ` [PATCH] usb: gadget: raw_gadget: fix ep0 request leak on unbind syzbot
     [not found] <20261124091819.2275412-1-hanzhijian1991@gmail.com>
2026-11-24  9:18 ` syzbot

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=6b055666.71deaa98.2984e6.0016.GAE@google.com \
    --to=syzbot+d83b3c49738aea97c0e5@syzkaller.appspotmail.com \
    --cc=hanzhijian1991@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzkaller-bugs@googlegroups.com \
    /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