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: emu8000: pin the module while an emu8k PCM runtime exists
Date: Wed, 02 Dec 2026 02:57:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-244736@kernel.org> (raw)
In-Reply-To: <20261202021939.1-1-xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: emu8000: use-after-free of struct snd_pcm on open, close,
  and unbind
--

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

ALSA: emu8000: pin the module while an emu8k PCM runtime exists

This commit pins THIS_MODULE in emu8k_pcm_open() and drops the reference in
emu8k_pcm_close() to prevent snd-emu8000-synth from being unloaded while a
PCM substream is open.

> diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
> index d2ca70aad1621..22203b072b930 100644
> --- a/sound/isa/sb/emu8000_pcm.c
> +++ b/sound/isa/sb/emu8000_pcm.c
> @@ -232,6 +232,11 @@ static int emu8k_pcm_open(struct snd_pcm_substream *subs)
>  	spin_lock_init(&rec->timer_lock);
>  	timer_setup(&rec->timer, emu8k_pcm_timer_func, 0);
>  
> +	if (!try_module_get(THIS_MODULE)) {
> +		kfree(rec);
> +		return -ENODEV;
> +	}
> +
>  	runtime->hw = emu8k_pcm_hw;
>  	runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
>  	runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
> @@ -248,6 +253,7 @@ static int emu8k_pcm_close(struct snd_pcm_substream *subs)
>  	struct snd_emu8k_pcm *rec = subs->runtime->private_data;
>  	kfree(rec);
>  	subs->runtime->private_data = NULL;
> +	module_put(THIS_MODULE);
>  	return 0;
>  }

[Severity: High]
Can struct snd_pcm still be freed while snd_pcm_release() or snd_pcm_open()
is using it?

snd_pcm_release() calls substream->ops->close() while holding
pcm->open_mutex, and still accesses pcm after releasing that mutex:

sound/core/pcm_native.c:snd_pcm_release() {
	...
	scoped_guard(mutex, &pcm->open_mutex) {
		snd_pcm_release_substream(substream);
		kfree(pcm_file);
	}
	wake_up(&pcm->open_wait);
	module_put(pcm->card->module);
	snd_card_file_remove(pcm->card, file);
	return 0;
}

Dropping THIS_MODULE inside emu8k_pcm_close() lets a concurrent rmmod free
hw->pcm as soon as pcm->open_mutex is unlocked:

CPU0                                  CPU1
snd_pcm_release()
  mutex_lock(&pcm->open_mutex)
  snd_pcm_release_substream()
    emu8k_pcm_close()
      module_put(THIS_MODULE)
                                      delete_module()
                                        snd_emu8000_remove()
                                          snd_device_free(..., hw->pcm)
                                            snd_pcm_dev_disconnect()
                                              mutex_lock(&pcm->open_mutex)
  mutex_unlock(&pcm->open_mutex)
                                              mutex_unlock(&pcm->open_mutex)
                                            snd_pcm_dev_free()
                                              kfree(pcm)
  wake_up(&pcm->open_wait)  <-- use-after-free
  module_put(pcm->card->module)  <-- use-after-free

On the open side, snd_pcm_open() looks up pcm and only pins
pcm->card->module before acquiring pcm->open_mutex and calling
emu8k_pcm_open().  If try_module_get(THIS_MODULE) fails during module
unload, snd_pcm_open() unlocks pcm->open_mutex and then dereferences
pcm->card after snd_emu8000_remove() has freed pcm.

Unbinding the driver via /sys/bus/snd_seq/drivers/snd_emu8000_synth/unbind
also runs snd_emu8000_remove() regardless of the module reference count and
frees hw->pcm while a file descriptor is open.  Disconnecting hw->pcm with
snd_device_disconnect(dev->card, hw->pcm) and waiting for open files with
snd_card_file_sync(dev->card, hw->pcm) in snd_emu8000_remove() before
freeing hw->pcm (as hda_codec_driver_remove() does) would cover open, close
and unbind.

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

      reply	other threads:[~2026-12-02  2:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 18:19 [PATCH] ALSA: emu8000: pin the module while an emu8k PCM runtime exists Hanxiang Xu
2026-12-02  2:57 ` 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-244736@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