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: pcm: do not free a pcm with opened substreams on single-device teardown
Date: Wed, 02 Dec 2026 03:22:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-244765@kernel.org> (raw)
In-Reply-To: <20261202100905.1-1-xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: pcm: permanent leak of struct snd_pcm on -EBUSY in
  snd_pcm_free()
- [High] ALSA: pcm: driver state UAF on close after snd_pcm_free()
  returns -EBUSY
- [Medium] ALSA: pcm: race and UAF in open/release when substream_opened
  is zero
--

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

ALSA: pcm: do not free a pcm with opened substreams on single-device teardown

This commit returns -EBUSY from snd_pcm_free() when either stream still
has opened substreams so single-device teardown does not free an active
struct snd_pcm.

> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
> index da285358eb655..4e012707d8806 100644
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -861,6 +861,9 @@ static int snd_pcm_free(struct snd_pcm *pcm)
>  {
>  	if (!pcm)
>  		return 0;
> +	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_opened ||
> +	    pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_opened)
> +		return -EBUSY;
>  	if (!pcm->internal) {
>  		scoped_guard(mutex, &register_mutex)
>  			pcm_call_notify(pcm, n_unregister);

[Severity: High]
Does returning -EBUSY here permanently leak struct snd_pcm and all of its
associated resources?

__snd_device_free() unconditionally unlinks the struct snd_device from
card->devices and frees it even when dev->ops->dev_free(dev) fails:

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);
}

Neither snd_pcm_release() nor snd_card_do_free() has any remaining path
to call snd_pcm_free(pcm) after the open descriptors close, so the PCM
streams, preallocated DMA buffers, per-substream timer devices, driver
private_free cleanup, and the snd-pcm-oss module reference released by
pcm_call_notify(pcm, n_unregister) -> snd_pcm_oss_unregister_minor() are
never freed.

[Severity: High]
Can leaving the open substream alive while snd_device_free() returns
immediately cause a use-after-free and NULL pointer dereference in the
driver when the descriptor is later closed?

In the motivating snd_emu8000_remove() path, the driver frees and zeroes
its state right after snd_device_free() returns:

sound/isa/sb/emu8000_synth.c:snd_emu8000_remove() {
    ...
	if (hw->pcm)
		snd_device_free(dev->card, hw->pcm);
	snd_emux_free(hw->emu);
	snd_util_memhdr_free(hw->memhdr);
	hw->emu = NULL;
	hw->memhdr = NULL;
    ...
}

When the surviving file descriptor is subsequently closed,
snd_pcm_release_substream() calls do_hw_free() -> emu8k_pcm_hw_free() and
substream->ops->close(), which dereference the already-freed and NULLed
rec->emu->emu, rec->emu->memhdr, and rec->block.

[Severity: Medium]
Can an in-flight open or release still suffer a use-after-free on struct
snd_pcm while substream_opened is 0?

substream_opened is updated under pcm->open_mutex inside
snd_pcm_attach_substream() and snd_pcm_detach_substream(), and
snd_pcm_release() continues accessing pcm after substream_opened drops to
0:

CPU0                              CPU1
snd_pcm_release()
  lock(&pcm->open_mutex)
  snd_pcm_release_substream()
    snd_pcm_detach_substream()
      pstr->substream_opened--
                                  snd_device_free(card, pcm)
                                    ...
                                      snd_pcm_free(pcm)
                                        substream_opened == 0
                                        kfree(pcm)
  unlock(&pcm->open_mutex)
  wake_up(&pcm->open_wait)
  module_put(pcm->card->module)  <-- use-after-free

The same window exists in snd_pcm_open() and snd_pcm_oss_open() after
snd_lookup_minor_data_file() returns pcm and before
snd_pcm_attach_substream() increments substream_opened.

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

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:09 [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown Hanxiang Xu
2026-12-02  3:22 ` 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-244765@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