mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures
@ 2026-12-02  2:12 Hanxiang Xu
  2026-12-02  9:04 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:12 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

Opening a hwdep device of the emux wavetable pins only the base-card
module; the emux hwdep registers no open/release hooks, so an open fd
keeps file->private_data pointing at the struct snd_hwdep and in-flight
ioctls keep running against the emux structures while the sub-driver
removal (rmmod snd_emu10k1_synth, or unbind which bypasses module
refcounts) kfrees the hwdep, the soundfont list and the struct snd_emux
itself -- snd_hwdep_ioctl() holds no open_mutex and
snd_hwdep_dev_disconnect() neither checks hwdep->used nor revokes f_op
(dynamically confirmed on v7.4-rc5, 113 UAF reports).

Register hwdep open/release hooks that account for the fd with
snd_emux_inc_count()/snd_emux_dec_count() -- which also pins the
sub-driver module like the sequencer and OSS-sequencer paths -- and
make snd_emux_free() wait until all hwdep users are gone before
tearing anything down, mirroring the drain snd_card_disconnect()
performs for whole-card removal.

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

 include/sound/emux_synth.h    |  2 ++
 sound/synth/emux/emux.c       |  3 +++
 sound/synth/emux/emux_hwdep.c | 22 ++++++++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/include/sound/emux_synth.h b/include/sound/emux_synth.h
index 2c0a976..46fe2bb 100644
--- a/include/sound/emux_synth.h
+++ b/include/sound/emux_synth.h
@@ -99,6 +99,8 @@ struct snd_emux {
 	int use_time;	/* allocation counter */
 	spinlock_t voice_lock;	/* Lock for voice access */
 	struct mutex register_mutex;
+	atomic_t hwdep_users;
+	wait_queue_head_t free_wait;
 	int client;		/* For the sequencer client */
 	int ports[SNDRV_EMUX_MAX_PORTS];	/* The ports for this device */
 	struct snd_emux_port *portptrs[SNDRV_EMUX_MAX_PORTS];
diff --git a/sound/synth/emux/emux.c b/sound/synth/emux/emux.c
index 2a42d04..4352c57 100644
--- a/sound/synth/emux/emux.c
+++ b/sound/synth/emux/emux.c
@@ -32,6 +32,8 @@ int snd_emux_new(struct snd_emux **remu)
 
 	spin_lock_init(&emu->voice_lock);
 	mutex_init(&emu->register_mutex);
+	atomic_set(&emu->hwdep_users, 0);
+	init_waitqueue_head(&emu->free_wait);
 
 	emu->client = -1;
 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
@@ -131,6 +133,7 @@ int snd_emux_free(struct snd_emux *emu)
 		return -EINVAL;
 
 	timer_shutdown_sync(&emu->tlist);
+	wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users));
 
 	snd_emux_proc_free(emu);
 	snd_emux_delete_virmidi(emu);
diff --git a/sound/synth/emux/emux_hwdep.c b/sound/synth/emux/emux_hwdep.c
index 14b990c..3240cbd 100644
--- a/sound/synth/emux/emux_hwdep.c
+++ b/sound/synth/emux/emux_hwdep.c
@@ -73,6 +73,26 @@ snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __user *arg)
 }
 
 
+static int snd_emux_hwdep_open(struct snd_hwdep *hw, struct file *file)
+{
+	struct snd_emux *emu = hw->private_data;
+
+	if (!snd_emux_inc_count(emu))
+		return -EFAULT;
+	atomic_inc(&emu->hwdep_users);
+	return 0;
+}
+
+static int snd_emux_hwdep_release(struct snd_hwdep *hw, struct file *file)
+{
+	struct snd_emux *emu = hw->private_data;
+
+	atomic_dec(&emu->hwdep_users);
+	wake_up(&emu->free_wait);
+	snd_emux_dec_count(emu);
+	return 0;
+}
+
 /*
  * ioctl
  */
@@ -127,6 +147,8 @@ snd_emux_init_hwdep(struct snd_emux *emu)
 	/* The ioctl parameter types are compatible between 32- and
 	 * 64-bit architectures, so use the same function. */
 	hw->ops.ioctl_compat = snd_emux_hwdep_ioctl;
+	hw->ops.open = snd_emux_hwdep_open;
+	hw->ops.release = snd_emux_hwdep_release;
 	hw->exclusive = 1;
 	hw->private_data = emu;
 	err = snd_card_register(emu->card);
-- 
2.43.0


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

* Re: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures
  2026-12-02  2:12 [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures Hanxiang Xu
@ 2026-12-02  9:04 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2026-12-02  9:04 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel

On Wed, 02 Dec 2026 03:12:31 +0100,
Hanxiang Xu wrote:
> 
> Opening a hwdep device of the emux wavetable pins only the base-card
> module; the emux hwdep registers no open/release hooks, so an open fd
> keeps file->private_data pointing at the struct snd_hwdep and in-flight
> ioctls keep running against the emux structures while the sub-driver
> removal (rmmod snd_emu10k1_synth, or unbind which bypasses module
> refcounts) kfrees the hwdep, the soundfont list and the struct snd_emux
> itself -- snd_hwdep_ioctl() holds no open_mutex and
> snd_hwdep_dev_disconnect() neither checks hwdep->used nor revokes f_op
> (dynamically confirmed on v7.4-rc5, 113 UAF reports).
> 
> Register hwdep open/release hooks that account for the fd with
> snd_emux_inc_count()/snd_emux_dec_count() -- which also pins the
> sub-driver module like the sequencer and OSS-sequencer paths -- and
> make snd_emux_free() wait until all hwdep users are gone before
> tearing anything down, mirroring the drain snd_card_disconnect()
> performs for whole-card removal.
> 
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>

This could be replaced simply with a call of snd_card_file_sync(card,
hwdep), instead?   Also it can be covered similarly for vmidi rawmidi
in emux code, too.  Just add those before snd_device_free() calls.


Takashi

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  2:12 [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures Hanxiang Xu
2026-12-02  9:04 ` 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®