All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown
@ 2026-12-02  2:09 Hanxiang Xu
  2026-12-02  3:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:09 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

An independent teardown path (e.g. snd_emu8000_remove() freeing the
pcm it created at probe time) calls snd_device_free(card, pcm), which
runs snd_pcm_dev_disconnect() and then snd_pcm_free(): kfree(substream)
for every substream and kfree(pcm) -- none of which waits for the open
file descriptors on /dev/snd/pcmC*D*.  In-flight and later ioctl/read
operations and the final snd_pcm_release() dereference the freed
substream and pcm.

Refuse the free while either stream has opened substreams:
__snd_device_free() unlinks the snd_device wrapper but leaves the pcm
object tree alive for the open descriptors.  A driver that properly
drains its users before teardown is unaffected, since
substream_opened is zero at that point.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 sound/core/pcm.c | 3 +++
 1 file changed, 3 insertions(+), 0 deletions(-)

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 6c82744..d5617fe 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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:22 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.