From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796286858; b=2rFrFtlEL1jsGetckxiUrbeiqKRY5O6M0Qk+8qeG0BpuRyiM6POIK7wuszLyvGJV7lG2 TsBUA75o3J3C1LreM0HSCwhhQ+xrnKBK2EJ7Ekr7FrwnHUAt4pv6uGqWFIw69NpA2SEZ0 AAHJ9XryXtMSEnoDWSl86NHLAiUkGzXTegmCPcnRrhza5Kod3amlNMf2fdjWZ6Jfl7gnL NcvClappAXJsU/lF3Q3vi5HLKBXepNijDVYnqDyWfiNPAmBGWswWnz5kq0eHUj0zxgOas NbagXmy1dpv7VVQjzQcdwDBExAcXoBiZbFlkGOyTm8Zm7VtvabaBT1B9NFk589D2PVg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796286858; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=S+2zPzi0GnkO4qIoQBVZ5Rlm2sJTAzoz52dwg3k/8RM=; b=SruXdiMzy6bjqRx8PyIHkkSTgTZ0joikCoIIyTqx+ToKi2VzynLXWj+OK8g3oWcsj0LP 9y1EIlKCTQidRvPW/REkm99WOog+dY+Ety/MjggV7BPlZWkIbMxeVczkkxS20iM8UwSlN EtRSxDTqy+I6+ApS23p9O3Rc08cWxlo1R/F8mp6Cn8JOduha2MEvw1q97946a95i+tAQq +h5WhyQryHArFeVzYtx2594RMIcGvZMMoKD6bG97dY8REnd7WcDODDLmuLrEKxgMmmTpZ H5o3PYWdYcvBrDWKqTvRbN5FoOm19lSQvWU2HpyNH+DhJO6Qszc+eYPNx5rXS9m4Mlw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="decko5/9"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D677A606A322; Thu, 3 Dec 2026 08:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=decko5/9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97B551F000C1; Thu, 3 Dec 2026 08:34:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796286858; bh=S+2zPzi0GnkO4qIoQBVZ5Rlm2sJTAzoz52dwg3k/8RM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=decko5/9ijnspl8GbApMTo1FgxFXqzm+5bY2prlRxnlZxcE5N8Pa7/vp/Q9XhRauq zfEbzL7TypnnkcG/i0A8PRZs28d/ht6qlwb02eWz/c1HUoHKWiSCMTSj6e/rI/Kh7l vNrliQxlMOy0SPAZTrKr3X338kY993r57SgEXIEwRlqMYMByZIoW+CgMK/y3XCaWJ6 xcXfGVs82GbSdUMniz0AnEH6E6CTM4u/RwcRtXDlGvJyn/T5JQv27LQNt9H46hp2ge s8t33h+9HvCOiS7XtRoho502LwFzqAPcWUWJXXoF2Yf7t2AydsqyOH3FAnDxZpW+cT 1ZkoHfolgQGLA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] usb: gadget: midi2: serialize the IN endpoint transmit path Reply-To: sashiko-reviews@lists.linux.dev To: "Jaegyun Lee" Cc: linux-usb@vger.kernel.org, linux-sound@vger.kernel.org In-Reply-To: <20261203080717.2643096-1-dreams200260@gmail.com> References: <20261203080717.2643096-1-dreams200260@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 08:34:18 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] usb: gadget: midi2: f_midi2_unbind() cancels tx_work too early, causing UAF - [High] usb: gadget: midi2: reply_ep_in() races with f_midi2_stop_eps() without tx_lock - [High] usb: gadget: midi2: unbounded IRQ-off tx_lock loop in f_midi2_tx_work() -- commit 666ce18825ae6a9ca3ce1c318e637235dc810a4c Author: Jaegyun Lee usb: gadget: midi2: serialize the IN endpoint transmit path This commit moves the USB MIDI 2.0 gadget IN transmit path into a per-endpoint work item and adds a per-endpoint tx_lock spinlock to serialize it with IN request allocation and teardown. > diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/f= unction/f_midi2.c > index 75036d4d60b35..8dfd68b03cd76 100644 > --- a/drivers/usb/gadget/function/f_midi2.c > +++ b/drivers/usb/gadget/function/f_midi2.c [ ... ] > @@ -1052,6 +1054,23 @@ static void process_midi1_transmit(struct f_midi2 = *midi2) > } > } > =20 > +static void f_midi2_tx_work(struct work_struct *work) > +{ > + struct f_midi2_usb_ep *usb_ep =3D > + container_of(work, struct f_midi2_usb_ep, tx_work); > + unsigned long flags; > + > + if (!usb_ep->usb_ep) > + return; > + > + spin_lock_irqsave(&usb_ep->tx_lock, flags); > + if (usb_ep->ep) > + process_ump_transmit(usb_ep->ep); > + else > + process_midi1_transmit(usb_ep->card); > + spin_unlock_irqrestore(&usb_ep->tx_lock, flags); > +} [Severity: High] Can holding usb_ep->tx_lock with IRQs disabled across the unbounded transmit loops stall the CPU indefinitely? In altset 0, process_midi1_transmit() loops reading 4 bytes at a time via snd_ump_transmit(). When userspace on another CPU writes UMP packets that do not convert to MIDI 1.0 bytes (such as Utility or Stream messages) or target an unmapped group, the loop discards them and continues without queueing a USB request. Because userspace refills the ring buffer in bulk faster than 4-byte reads drain it, snd_ump_transmit() keeps returning data and f_midi2_tx_work() holds tx_lock with local IRQs disabled indefinitely, leading to RCU stalls or hard lockups while f_midi2_set_alt() -> f_midi2_stop_eps() busy-spins on tx_lock. Even during normal streaming, f_midi2_ep_in_complete() and f_midi2_midi1_ep_in_complete() replenish usb_ep->free_reqs via put_empty_request() without taking tx_lock, so completions on another CPU can keep both transmit loops spinning with IRQs off. Bounding the work done per tx_lock hold and re-queueing tx_work when more data remains would avoid this. [ ... ] > @@ -1264,9 +1287,13 @@ static void f_midi2_queue_out_reqs(struct f_midi2_= usb_ep *usb_ep) > static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in, > struct f_midi2_usb_ep *ep_out) > { > + unsigned long flags; > + > + spin_lock_irqsave(&ep_in->tx_lock, flags); > f_midi2_drop_reqs(ep_in); > - f_midi2_drop_reqs(ep_out); > f_midi2_free_ep_reqs(ep_in); > + spin_unlock_irqrestore(&ep_in->tx_lock, flags); > + f_midi2_drop_reqs(ep_out); > f_midi2_free_ep_reqs(ep_out); > } [Severity: High] Can freeing ep_in requests here race with reply_ep_in() and reply_ump_stream_string() and cause a use-after-free on req? With process_ump enabled by default, f_midi2_ep_out_complete() -> process_ump() -> process_ump_stream_msg() sends replies on ep->ep_in through reply_ep_in() and reply_ump_stream_string() without taking ep_in->tx_lock. Reordering f_midi2_free_ep_reqs(ep_in) ahead of f_midi2_drop_reqs(ep_out) leaves ep_out completing while ep_in requests are being freed: CPU0 CPU1 f_midi2_set_alt() f_midi2_ep_out_complete() f_midi2_stop_eps() ... -> reply_ep_in() req =3D get_empty_request(ep_in) spin_lock_irqsave(tx_lock) f_midi2_drop_reqs(ep_in) f_midi2_free_ep_reqs(ep_in) spin_unlock_irqrestore(...) memcpy(req->buf, ...) <-- use-after-free queue_request_ep_in(req) Acquiring ep_in->tx_lock (and checking usb_ep->enabled) in reply_ep_in() and reply_ump_stream_string() would serialize these reply paths with ep_in request teardown. [ ... ] > @@ -2079,11 +2110,21 @@ static int f_midi2_bind(struct usb_configuration = *c, struct usb_function *f) > } > =20 > /* gadget function unbind callback */ > +static void f_midi2_cancel_tx_work(struct f_midi2 *midi2) > +{ > + int i; > + > + cancel_work_sync(&midi2->midi1_ep_in.tx_work); > + for (i =3D 0; i < midi2->num_eps; i++) > + cancel_work_sync(&midi2->midi2_eps[i].ep_in.tx_work); > +} > + > static void f_midi2_unbind(struct usb_configuration *c, struct usb_funct= ion *f) > { > struct f_midi2 *midi2 =3D func_to_midi2(f); > int i; > =20 > + f_midi2_cancel_tx_work(midi2); > f_midi2_free_card(midi2); > =20 > f_midi2_free_ep(&midi2->midi1_ep_in); [Severity: High] Can calling f_midi2_cancel_tx_work() before f_midi2_free_card() and f_midi2_free_ep() cause a use-after-free on ep->ump? At this point in f_midi2_unbind(), neither producer of tx_work has been stopped. f_midi2_disable() does not disable endpoints or dequeue in-flight IN requests, so f_midi2_ep_in_complete() and f_midi2_midi1_ep_in_complete() can still re-queue tx_work until f_midi2_free_ep() runs, and f_midi2_ump_trigger() can queue it until f_midi2_free_card() disconnects the card: CPU0 CPU1 f_midi2_unbind() f_midi2_cancel_tx_work() f_midi2_ep_in_complete() queue_work(..., &ep->ep_in.tx_work) f_midi2_free_card() snd_card_free_when_closed() put_device() // frees ep->ump f_midi2_tx_work() process_ump_transmit() snd_ump_transmit(ep->ump, ...) <-- use-after-free f_midi2_free_ep() When no userspace file descriptors are open, snd_card_free_when_closed() frees the card and ep->ump synchronously while usb_ep->usb_ep->enabled is still true and usb_ep->reqs is still populated, so the re-queued f_midi2_tx_work() passes a freed ep->ump to snd_ump_transmit(). Because configfs unbind (purge_configs_funcs()) calls f_midi2_unbind() without calling f_midi2_free(), a re-queued tx_work can also outlive f_midi2_unbind() and race with a later f_midi2_bind(). Stopping and freeing the endpoints before disconnecting the card and cancelling tx_work afterward would prevent tx_work from being re-queued. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203080717.2643= 096-1-dreams200260@gmail.com?part=3D1