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

From: Hanxiang Xu

Date: Tue Dec 01 2026 - 21:44:01 EST


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@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---

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