Re: [PATCH v8 1/4] usb: gadget: gadgetfs: fix ep0 request lifetime and buffer races

From: kayano050408

Date: Wed Nov 25 2026 - 22:21:46 EST


Hi Alan,

Thank you for the detailed review. I have received your comments on 1/4.

I will wait until you have gone through 2/4 to 4/4 and shared the
remaining comments, and then start reworking the series based on all
of your feedback.

Thanks,
Jing Lin

On Wed, 25 Nov 2026 22:15:43 -0500, Alan Stern
<stern@xxxxxxxxxxxxxxxxxxx> wrote:
> At last, I finally got some time to go over this material.
>
> On Tue, Nov 10, 2026 at 12:13:22PM +0800, 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 controllers such as 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.
>
> This is a genuine problem.
>
> > gadgetfs_setup() and the userspace copy helpers also race on dev->req
> > and req->buf:
> >
> > 1. ep0_read() versus gadgetfs_setup(). A delegated control OUT request
> > is queued by gadgetfs_setup() and completed by ep0_complete(), which
> > leaves the payload in dev->req->buf and sets setup_out_ready.
> > ep0_read() copies that payload with copy_to_user() after dropping
> > dev->lock, so the copy can fault and sleep. If a new control
> > transfer arrives during that window, gadgetfs_setup() reuses dev->req
> > for it; for example, GET_DESCRIPTOR(config) points req->buf at
> > dev->config. When ep0_read() resumes, clean_req() frees whatever
> > req->buf points to, which is now dev->config.
>
> But this isn't. Or at least, your analysis is incomplete.
>
> The real problem here is that gadgetfs does not handle "aborted"
> control-OUT transfers correctly. The request for the OUT data stage is
> submitted when the setup packet arrives, and the host is told that the
> transfer was successful when the request completes correctly, but the
> user program doesn't receive the data until some time later. And if
> another control transfer arrives in the meantime, "aborting" the earlier
> transfer, the user program doesn't ever receive the data. It's not good
> for the host and the peripheral to disagree over whether a transfer was
> successful!
>
> The start of a new transfer should not be allowed to erase all knowledge
> of a properly completed control-OUT transfer until the data has been
> read. Instead, the gadget should NAK the new transfer until then (i.e.,
> not submit any request in response to the new setup).
>
> Fixing that will be a fairly substantial change. Furthermore, this
> whole issue is so entirely different from the race involving
> gadgetfs_unbind() above that it belongs in its own patch.
>
> > 2. ep0_write() versus gadgetfs_setup(). For a delegated control IN
> > request, ep0_write() prepares dev->req and copies the response from
> > userspace with copy_from_user() after dropping dev->lock. A control
> > request that arrives during that copy can point req->buf at dev->dev
> > or dev->config, so the userspace copy overwrites a descriptor buffer,
> > or reset req->buf to dev->rbuf so completion calls clean_req() on the
> > wrong object.
>
> It should be possible to handle this problem without copying the buffer.
> For instance, the user data could be put in its own kmalloc-ed buffer
> instead of in dev->rbuf.
>
> > Copy completed control OUT payloads into a private setup_buf in
> > ep0_complete(), copy control IN payloads into a stack buffer before
> > preparing dev->req and leaving STATE_DEV_SETUP in ep0_write(), and track
> > buffers allocated by setup_req() separately in dev->ep0_req_buf so
> > clean_req() never frees descriptor storage. Ignore zero-length reads
> > while a non-zero control OUT data stage is pending so they cannot queue
> > a status request early and clobber setup_out_len.
> >
> > Submit ep0 requests through a shared queue_ep0_req() helper that tracks
> > dev->ep0_req_queued and wakes dev->wait whenever the flag clears. In
> > gadgetfs_unbind(), tear down non-ep0 endpoints first, then dequeue any
> > active ep0 request and wait under dev->lock with wait_event_lock_irq()
> > for completion to finish. If the controller rejects ep0 dequeue, keep a
> > reference on dev and let the completion handler free the request when
> > udc_stop() drains ep0.
>
> These descriptions are a little confusing to read, because they mix
> together your solutions to three separate problems. Each solution
> really should be in its own patch.
>
> Also, don't worry about a rejected dequeue call. In practice, the only
> reason such a call can fail is because the request to be dequeued has
> already completed (or was never successfully submitted in the first
> place).
>
> Alan Stern