Re: [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests
From: Greg Kroah-Hartman
Date: Thu Oct 29 2026 - 05:44:59 EST
On Sat, Aug 15, 2026 at 02:29:29PM +0900, Hyeontae Lee wrote:
> f_midi_disable() drains in_req_fifo and frees the requests without taking
> transmit_lock. f_midi_transmit() holds that lock across the whole of
> f_midi_do_transmit(), which peeks a request off the fifo without removing
> it and then keeps using it. kfifo_peek() does not advance the read index,
> so the drain frees exactly the request the transmit path is still using.
>
> No hostile host is needed: __composite_disconnect() calls f->disable() as
> well, so a bus reset or a cable pull while a MIDI client is writing is
> enough.
>
> BUG: KASAN: slab-use-after-free in f_midi_in_work+0x126c/0x18c0
> Read of size 4 at addr ffff888104818e20 by task kworker/2:2H/164
> Freed by task 158:
> kfree+0x121/0x380
> f_midi_disable+0x20e/0x430
> reset_config+0x9d/0x200
> __composite_disconnect+0xa7/0x140
>
> Take the lock around the drain, the way hidg_disable() does.
>
> Fixes: e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests")
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: Claude:claude-opus-4.8
> Signed-off-by: Hyeontae Lee <wonju345@xxxxxxxxx>
> ---
> drivers/usb/gadget/function/f_midi.c | 3 +++
> 1 file changed, 3 insertions(+)
This version:
https://lore.kernel.org/r/20261028074821.579450-1-sonalipradhan@xxxxxxxxxx
turns out to be a bit better, thanks.
greg k-h