Re: [PATCH v8 2/4] usb: gadget: gadgetfs: fix ep0 setup abort and unbind state races
From: Alan Stern
Date: Wed Nov 25 2026 - 22:27:44 EST
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