[PATCH] ALSA: emux: expose the hwdep device only after ports are initialized

From: Hanxiang Xu

Date: Tue Dec 01 2026 - 22:15:31 EST


snd_emux_register() creates and registers the hwdep device
(snd_emux_init_hwdep() -> snd_card_register()) before
snd_emux_init_seq() fills emu->portptrs[], and ignores the init_seq()
return value. A SNDRV_EMUX_IOCTL_MISC_MODE ioctl arriving inside that
window walks emu->portptrs[i]->ctrls[] with portptrs[i] == NULL and
causes a near-NULL kernel write (dynamically confirmed on v7.4-rc5);
a failed sequencer setup additionally leaves the already-registered
hwdep behind for the probe error path to free with possibly-open fds.

Initialize the ports (and OSS/virmidi) first and create the hwdep
device last, so the device only becomes visible to userspace after
everything its ioctls touch is initialized, and propagate the
snd_emux_init_seq() error.

Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---

sound/synth/emux/emux.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/sound/synth/emux/emux.c b/sound/synth/emux/emux.c
index 4352c57..518019e 100644
--- a/sound/synth/emux/emux.c
+++ b/sound/synth/emux/emux.c
@@ -107,18 +107,19 @@ int snd_emux_register(struct snd_emux *emu, struct snd_card *card, int index, ch
if (emu->sflist == NULL)
return -ENOMEM;

- err = snd_emux_init_hwdep(emu);
- if (err < 0)
- return err;
-
snd_emux_init_voices(emu);

- snd_emux_init_seq(emu, card, index);
+ if (snd_emux_init_seq(emu, card, index) < 0)
+ return -ENODEV;
#if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
snd_emux_init_seq_oss(emu);
#endif
snd_emux_init_virmidi(emu, card);

+ err = snd_emux_init_hwdep(emu);
+ if (err < 0)
+ return err;
+
snd_emux_proc_init(emu, card, index);
return 0;
}
--
2.43.0