Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown
Date: Wed, 02 Dec 2026 03:23:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-244770@kernel.org> (raw)
In-Reply-To: <20261202100804.1-1-xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: rawmidi: permanent resource leak on -EBUSY in
  snd_rawmidi_free()
- [High] ALSA: rawmidi: UAF race in snd_rawmidi_free() against open and
  release
--

commit a8c690d1a9c9b070160648c8b1f5677136e99bce
Author: Hanxiang Xu <xuhx@hust.edu.cn>

ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown

This commit skips freeing open substreams in snd_rawmidi_free_substreams()
and returns -EBUSY from snd_rawmidi_free() when any substream is open.

> diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
> index 630d9fc8e9da0..851509e9d43ff 100644
> --- a/sound/core/rawmidi.c
> +++ b/sound/core/rawmidi.c
> @@ -1969,6 +1969,8 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
>  	while (!list_empty(&stream->substreams)) {
>  		substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
>  		list_del(&substream->list);
> +		if (substream->opened)
> +			continue;
>  		kfree(substream);
>  	}
>  }
> @@ -1978,6 +1980,9 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
>  {
>  	if (!rmidi)
>  		return 0;
> +	if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened ||
> +	    rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened)
> +		return -EBUSY;
>  
>  	snd_info_free_entry(rmidi->proc_entry);
>  	rmidi->proc_entry = NULL;

[Severity: High]
Does returning -EBUSY here permanently leak rmidi, its substreams, and its
subdevice resources?

__snd_device_free() unlinks the snd_device from card->devices and frees
it regardless of the error returned by dev_free():

sound/core/device.c:__snd_device_free() {
	/* unlink */
	list_del(&dev->list);

	__snd_device_disconnect(dev);
	if (dev->ops->dev_free) {
		if (dev->ops->dev_free(dev))
			dev_err(dev->card->dev, "device free failure\n");
	}
	kfree(dev);
}

When the open file descriptors on /dev/snd/midiC*D* are later closed,
snd_rawmidi_release() and close_substream() only decrement
substream_opened and free the runtime and snd_rawmidi_file; nothing ever
calls snd_rawmidi_free() again.  In addition to leaking rmidi, the
substreams, rmidi->private_data, and the rmidi->dev reference, bailing out
early leaves rmidi->proc_entry registered in procfs and skips
rmidi->ops->dev_unregister().  Likewise, snd_rawmidi_free_substreams()
calls list_del(&substream->list) before checking substream->opened, which
orphans and leaks any open substream.

[Severity: High]
Can checking substream_opened and substream->opened without holding
rmidi->open_mutex race with concurrent open and release calls and cause a
use-after-free?

open_substream() and close_substream() update substream->opened and
substream_opened under rmidi->open_mutex, but snd_rawmidi_free() runs
unlocked.  If the last open file descriptor is closing concurrently,
close_substream() decrements substream_opened to 0 before
rawmidi_release_priv() and snd_rawmidi_release() finish using rmidi:

CPU0                                  CPU1
snd_rawmidi_release()
  mutex_lock(&rmidi->open_mutex)
  rawmidi_release_priv()
    close_substream()
      ...
      stream->substream_opened--
                                      snd_device_free()
                                        ...
                                          snd_rawmidi_free()
                                            /* sees substream_opened == 0 */
                                            kfree(rmidi)
    wake_up(&rmidi->open_wait)  <-- use-after-free
  mutex_unlock(&rmidi->open_mutex)  <-- use-after-free
  module = rmidi->card->module  <-- use-after-free

A similar race exists against open_substream(): during single-device
teardown rmidi->card->shutdown is still 0, so a concurrent open can enter
open_substream() and run snd_rawmidi_runtime_create() and
substream->ops->open() before incrementing substream_opened, while
snd_rawmidi_free() sees substream_opened == 0 and frees the substreams,
rmidi->private_data, and rmidi underneath it.  If open_substream() sets
substream->opened = 1 just after snd_rawmidi_free() passes the
substream_opened check, snd_rawmidi_free_substreams() skips freeing that
substream while snd_rawmidi_free() still frees rmidi->private_data and
rmidi, leaving the open substream and file with dangling rmidi pointers.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100804.1-1-xuhx@hust.edu.cn?part=1

      reply	other threads:[~2026-12-02  3:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:08 [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown Hanxiang Xu
2026-12-02  3:23 ` 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-244770@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xuhx@hust.edu.cn \
    /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