All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: emu8000: pin the module while an emu8k PCM runtime exists
@ 2026-12-01 18:19 Hanxiang Xu
  2026-12-02  2:57 ` sashiko-bot
  2026-12-02  8:48 ` Takashi Iwai
  0 siblings, 2 replies; 3+ messages in thread
From: Hanxiang Xu @ 2026-12-01 18:19 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

snd_emu8000_pcm_new() creates a PCM device on the host sound card
from the snd_emu8000_synth seq driver, and snd_emu8000_remove() frees
it again with snd_device_free(dev->card, hw->pcm) unconditionally.
An fd on /dev/snd/pcmC*D1p only pins the host card module
(try_module_get(pcm->card->module) in snd_pcm_open), so
`rmmod snd_emu8000_synth` runs snd_emu8000_remove() while the fd is
open: snd_pcm_dev_disconnect() stops the streams and unregisters the
device, snd_pcm_free() kfrees every substream and the snd_pcm itself,
and the fd's final close() then walks the freed pcm_file->substream,
substream->pcm and pcm->open_mutex (and calls substream->ops->close,
which points into the unloaded module).

Pin snd-emu8000-synth with try_module_get(THIS_MODULE) in
emu8k_pcm_open() and drop it in emu8k_pcm_close(), so `rmmod` returns
EBUSY instead of running snd_emu8000_remove() under an open fd.

Also reproduced on 7.4.0-rc5: 12 reports (slab-use-after-free in
snd_pcm_release, slab-out-of-bounds in snd_power_ref_and_wait) with a
KASAN kernel; orderly teardown without an open fd is clean.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---

 1 file changed, 6 insertions(+), 0 deletions(-)

diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
index d2ca70a..22203b0 100644
--- a/sound/isa/sb/emu8000_pcm.c
+++ b/sound/isa/sb/emu8000_pcm.c
@@ -232,6 +232,11 @@
 	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 @@
 	struct snd_emu8k_pcm *rec = subs->runtime->private_data;
 	kfree(rec);
 	subs->runtime->private_data = NULL;
+	module_put(THIS_MODULE);
 	return 0;
 }
 
-- 
2.43.0


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

* Re: [PATCH] ALSA: emu8000: pin the module while an emu8k PCM runtime exists
  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
  2026-12-02  8:48 ` Takashi Iwai
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02  2:57 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

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

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

* Re: [PATCH] ALSA: emu8000: pin the module while an emu8k PCM runtime  exists
  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
@ 2026-12-02  8:48 ` Takashi Iwai
  1 sibling, 0 replies; 3+ messages in thread
From: Takashi Iwai @ 2026-12-02  8:48 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel

On Tue, 01 Dec 2026 19:19:39 +0100,
Hanxiang Xu wrote:
> 
> snd_emu8000_pcm_new() creates a PCM device on the host sound card
> from the snd_emu8000_synth seq driver, and snd_emu8000_remove() frees
> it again with snd_device_free(dev->card, hw->pcm) unconditionally.
> An fd on /dev/snd/pcmC*D1p only pins the host card module
> (try_module_get(pcm->card->module) in snd_pcm_open), so
> `rmmod snd_emu8000_synth` runs snd_emu8000_remove() while the fd is
> open: snd_pcm_dev_disconnect() stops the streams and unregisters the
> device, snd_pcm_free() kfrees every substream and the snd_pcm itself,
> and the fd's final close() then walks the freed pcm_file->substream,
> substream->pcm and pcm->open_mutex (and calls substream->ops->close,
> which points into the unloaded module).
> 
> Pin snd-emu8000-synth with try_module_get(THIS_MODULE) in
> emu8k_pcm_open() and drop it in emu8k_pcm_close(), so `rmmod` returns
> EBUSY instead of running snd_emu8000_remove() under an open fd.
> 
> Also reproduced on 7.4.0-rc5: 12 reports (slab-use-after-free in
> snd_pcm_release, slab-out-of-bounds in snd_power_ref_and_wait) with a
> KASAN kernel; orderly teardown without an open fd is clean.
> 
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>

Although this doesn't cover fully, it's still a bit of improvement on
safety, so I applied to for-next branch now.


thanks,

Takashi

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-12-02  8:48 ` Takashi Iwai

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.