Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jaegyun Lee" <dreams200260@gmail.com>
Cc: linux-usb@vger.kernel.org, linux-sound@vger.kernel.org
Subject: Re: [PATCH v2] usb: gadget: midi2: serialize the IN endpoint transmit path
Date: Thu, 03 Dec 2026 08:34:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-247360@kernel.org> (raw)
In-Reply-To: <20261203080717.2643096-1-dreams200260@gmail.com>

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 <dreams200260@gmail.com>

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/function/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)
>  	}
>  }
>  
> +static void f_midi2_tx_work(struct work_struct *work)
> +{
> +	struct f_midi2_usb_ep *usb_ep =
> +		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 = 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)
>  }
>  
>  /* 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 = 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_function *f)
>  {
>  	struct f_midi2 *midi2 = func_to_midi2(f);
>  	int i;
>  
> +	f_midi2_cancel_tx_work(midi2);
>  	f_midi2_free_card(midi2);
>  
>  	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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203080717.2643096-1-dreams200260@gmail.com?part=1

      reply	other threads:[~2026-12-03  8:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:07 [PATCH v2] usb: gadget: midi2: serialize the IN endpoint transmit path Jaegyun Lee
2026-12-03  8:34 ` sashiko-bot [this message]

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=sashiko-outbox-247360@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dreams200260@gmail.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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