mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hyeontae Lee <wonju345@naver.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, Hyeontae Lee <wonju345@naver.com>
Subject: [PATCH] usb: gadget: f_midi: hold transmit_lock while freeing IN requests
Date: Sat, 15 Aug 2026 14:29:29 +0900	[thread overview]
Message-ID: <20260815052929.146901-1-wonju345@naver.com> (raw)

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


             reply	other threads:[~2026-08-15  5:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-15  5:29 Hyeontae Lee [this message]
2026-10-29  9:44 ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260815052929.146901-1-wonju345@naver.com \
    --to=wonju345@naver.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®