* [PATCH] ALSA: emu8000: pin the module while an emu8k PCM runtime exists
@ 2026-12-01 18:19 Hanxiang Xu
2026-12-02 8:48 ` Takashi Iwai
0 siblings, 1 reply; 2+ 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 [flat|nested] 2+ 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 8:48 ` Takashi Iwai
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-12-02 8:48 UTC | newest]
Thread overview: 2+ 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 8:48 ` Takashi Iwai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®