Re: [PATCH v8 2/4] usb: gadget: gadgetfs: fix ep0 setup abort and unbind state races
From: kayano050408
Date: Thu Nov 26 2026 - 01:40:21 EST
Hi Alan
Thank you for reviewing the whole series
Regarding how an event could be read after gadgetfs_unbind() has
finished, closing the ep0 file descriptor is indeed the normal path,
though gadgetfs_unbind() can also run while userspace still holds the
open ep0 descriptor if the UDC itself is unregistered via
usb_del_gadget_udc() or unbound through sysfs at
/sys/bus/gadget/drivers/gadgetfs/unbind
In v9 I dropped the STATE_DEV_UNBOUND changes to keep the series
focused on the main fixes, and if you think that external unbind path
is worth handling too I can send a separate small follow-up patch that
clears dev->ev_next, wakes dev->wait in gadgetfs_unbind(), and checks
STATE_DEV_UNBOUND before delivering events in ep0_read()
Thanks,
Jing Lin
On Wed, 25 Nov 2026 22:27:31 -0500, Alan Stern
<stern@xxxxxxxxxxxxxxxxxxx> wrote:
> On Tue, Nov 10, 2026 at 12:13:23PM +0800, Jing Lin via B4 Relay wrote:
> > From: Jing Lin <kayano050408@xxxxxxxxx>
> >
> > Several ep0 state transitions lose setup abort notifications or leave
> > the file descriptor in an inconsistent state when control requests,
> > disconnects, or unbinds race with userspace I/O:
> >
> > 1. ep0_write() drops dev->lock while copying an IN payload from
> > userspace. If a new SETUP packet arrives in that window, the stale
> > response must not be queued for the new transfer.
>
> The window can be reduced but not eliminated. After all, once dev->lock
> has been dropped there is no way to know if a new setup packet has just
> arrived.
>
> > 2. ep0_read() waiting for a delegated control OUT data stage only checks
> > dev->setup_out_ready, so it does not wake when a new SETUP packet,
> > disconnect, or unbind aborts the transfer, and can consume a newer
> > transfer's payload if dev->state re-enters STATE_DEV_SETUP. In
> > addition, gadgetfs_disconnect() overwrites STATE_DEV_UNCONNECTED
> > before next_event() checks for STATE_DEV_SETUP, skipping setup_abort.
>
> The problem will not exist once new setup packets are prevented from
> aborting older control-OUT tranfers.
>
> > 3. Early returns in gadgetfs_setup() on oversized OUT requests or a busy
> > ep0 request skip setting setup_abort when aborting STATE_DEV_SETUP,
> > while ep0_poll() clears setup_abort before ep0_read() or ep0_write()
> > can report -EIDRM.
>
> These should be relatively easy to fix.
>
> > 4. gadgetfs_unbind() leaves queued events readable in STATE_DEV_UNBOUND.
> > Delivering a queued GADGETFS_SETUP event after unbind puts dev->state
> > back into STATE_DEV_SETUP after dev->req and ep0->driver_data have
> > been cleared, causing a NULL pointer dereference on the next ep0 I/O.
>
> How can an event be delivered after gadgetfs_unbind() has finished? The
> unbind occurs when usb_gadget_unregister_driver() is called, which
> happens when the user program is closing its file descriptor for ep0,
> leaving no way for it to read any events.
>
> And as before, these issues are all totally separate so they belong in
> separate patches.
>
> Alan Stern