Re: [PATCH v4 1/2] usb: gadget: gadgetfs: fix ep0 request lifetime on disconnect
From: Alan Stern
Date: Sun Nov 08 2026 - 21:52:18 EST
On Sat, Nov 07, 2026 at 09:46:13PM +0900, Jing Lin via B4 Relay wrote:
> From: Jing Lin <kayano050408@xxxxxxxxx>
>
> gadgetfs_unbind() frees the preallocated ep0 request after disconnecting
> the gadget, assuming that no I/O remains active.
>
> With dummy_hcd, disconnecting does not necessarily drain ep0, so a delayed
> userspace response can leave dev->req queued when it is freed. This
> triggers the warning in dummy_free_request() and a use-after-free when
> dummy_udc_stop() subsequently drains the endpoint queue.
>
> gadgetfs_setup() and ep0_write() can also race on dev->req and req->buf
> while a control transfer is in flight or while a completed delegated OUT
> transfer is waiting for ep0_read() to copy the payload to userspace.
> Overwriting req->buf with a descriptor pointer causes clean_req() to free
> the descriptor buffer instead of the request buffer.
>
> Copy completed control OUT payloads into a separate setup_buf in
> ep0_complete(), copy control IN payloads from userspace into a stack
> buffer before preparing dev->req in ep0_write(), and reset req->buf when
> control transfers finish. Track whether dev->req is queued, reject reuse
> while it is queued, and dequeue it without dev->lock before freeing it in
> gadgetfs_unbind().
>
> Fixes: 1da177e4c3f41 ("Linux-2.6.12-rc2")
> Reported-by: syzbot+c73e1942197af9f54ca3@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=c73e1942197af9f54ca3
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Jing Lin <kayano050408@xxxxxxxxx>
> ---
Quick question...
> @@ -1781,15 +1842,21 @@ gadgetfs_unbind (struct usb_gadget *gadget)
> usleep_range(1000, 2000);
> spin_lock_irq(&dev->lock);
> }
> + queued = dev->ep0_req_queued;
> spin_unlock_irq (&dev->lock);
>
> + if (dev->req) {
> + if (queued)
> + usb_ep_dequeue(gadget->ep0, dev->req);
Suppose another thread wants to submit dev->req, and has got as far as
setting dev->ep0_req_queued and releasing the spinlock, but hasn't
called usb_ep_queue() yet. What prevents it from making that call right
now? Then the request would be active -- same problem as before, right?
> + clean_req(gadget->ep0, dev->req);
> + usb_ep_free_request(gadget->ep0, dev->req);
> + dev->req = NULL;
> + }
Alan Stern