Re: [PATCH RESEND] usb: gadget: f_midi: fix UAF race during IN request handling
From: Takashi Iwai
Date: Tue Oct 20 2026 - 05:11:56 EST
On Mon, 19 Oct 2026 15:57:01 +0200,
Sonali Pradhan wrote:
>
> f_midi_disable() frees pre-allocated IN requests from in_req_fifo
> without holding midi->transmit_lock. Concurrent f_midi_transmit() or
> f_midi_complete() calls can then access these request buffers after they
> are freed, leading to a use-after-free.
>
> Fix this by acquiring midi->transmit_lock in f_midi_disable() while
> releasing IN requests, and checking ep->enabled under midi->transmit_lock
> in f_midi_transmit() and f_midi_complete() before modifying requests.
>
> Fixes: e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Sonali Pradhan <sonalipradhan@xxxxxxxxxx>
> ---
> Resending and rebasing on latest linux-next as I haven't received
> feedback on my initial submission:
> https://lore.kernel.org/all/20260819102709.212646-1-sonalipradhan@xxxxxxxxxx/
>
> drivers/usb/gadget/function/f_midi.c | 19 ++++++++++++++++---
> 1 file changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
> index 63fb6ee70a3d..c26d04eb9209 100644
> --- a/drivers/usb/gadget/function/f_midi.c
> +++ b/drivers/usb/gadget/function/f_midi.c
> @@ -280,9 +280,18 @@ f_midi_complete(struct usb_ep *ep, struct usb_request *req)
> /* We received stuff. req is queued again, below */
> f_midi_handle_out_data(ep, req);
> } else if (ep == midi->in_ep) {
> + unsigned long flags;
> +
> /* Our transmit completed. See if there's more to go.
> * f_midi_transmit eats req, don't queue it again. */
> + spin_lock_irqsave(&midi->transmit_lock, flags);
> + if (!ep->enabled) {
> + spin_unlock_irqrestore(&midi->transmit_lock, flags);
> + return;
> + }
> req->length = 0;
> + spin_unlock_irqrestore(&midi->transmit_lock, flags);
> +
Does this transmit_lock help here? As far as I see, the ep
enable/disable itself is done outside the lock.
> @@ -678,10 +690,11 @@ static void f_midi_transmit(struct f_midi *midi)
> unsigned long flags;
>
> /* We only care about USB requests if IN endpoint is enabled */
> - if (!ep || !ep->enabled)
> - goto drop_out;
> -
> spin_lock_irqsave(&midi->transmit_lock, flags);
> + if (!ep || !ep->enabled) {
> + spin_unlock_irqrestore(&midi->transmit_lock, flags);
> + goto drop_out;
> + }
Ditto.
thanks,
Takashi