mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests
@ 2026-08-15  5:29 Hyeontae Lee
  2026-10-29  9:44 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Hyeontae Lee @ 2026-08-15  5:29 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel, stable, Hyeontae Lee

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@vger.kernel.org
Assisted-by: Claude:claude-opus-4.8
Signed-off-by: Hyeontae Lee <wonju345@naver.com>
---
 drivers/usb/gadget/function/f_midi.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
index fba8cf787d6c1..9e67b3c74296f 100644
--- a/drivers/usb/gadget/function/f_midi.c
+++ b/drivers/usb/gadget/function/f_midi.c
@@ -420,6 +420,7 @@ static void f_midi_disable(struct usb_function *f)
 	struct f_midi *midi = func_to_midi(f);
 	struct usb_composite_dev *cdev = f->config->cdev;
 	struct usb_request *req = NULL;
+	unsigned long flags;
 
 	DBG(cdev, "disable\n");
 
@@ -431,8 +432,10 @@ static void f_midi_disable(struct usb_function *f)
 	usb_ep_disable(midi->out_ep);
 
 	/* release IN requests */
+	spin_lock_irqsave(&midi->transmit_lock, flags);
 	while (kfifo_get(&midi->in_req_fifo, &req))
 		free_ep_req(midi->in_ep, req);
+	spin_unlock_irqrestore(&midi->transmit_lock, flags);
 
 	f_midi_drop_out_substreams(midi);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests
  2026-08-15  5:29 [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests Hyeontae Lee
@ 2026-10-29  9:44 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-10-29  9:44 UTC (permalink / raw)
  To: Hyeontae Lee; +Cc: linux-usb, linux-kernel, stable

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@vger.kernel.org
> Assisted-by: Claude:claude-opus-4.8
> Signed-off-by: Hyeontae Lee <wonju345@naver.com>
> ---
>  drivers/usb/gadget/function/f_midi.c | 3 +++
>  1 file changed, 3 insertions(+)

This version:
	https://lore.kernel.org/r/20261028074821.579450-1-sonalipradhan@google.com
turns out to be a bit better, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-29  9:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-15  5:29 [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests Hyeontae Lee
2026-10-29  9:44 ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®