Re: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures

From: Takashi Iwai

Date: Wed Dec 02 2026 - 04:04:19 EST


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

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