Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: hanzhijian1991@gmail.com, linux-usb@vger.kernel.org
Subject: Re: [PATCH v3] usb: gadget: raw_gadget: fix memory leak on connect event failure
Date: Tue, 24 Nov 2026 01:49:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-230357@kernel.org> (raw)
In-Reply-To: <20261124012837.2071714-1-hanzhijian1991@gmail.com>

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

  reply	other threads:[~2026-11-24  1:49 UTC|newest]

Thread overview: 3+ 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 [this message]
2026-11-24  2:38 ` Andrey Konovalov

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-230357@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