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

From: 徐晗翔

Date: Tue Dec 01 2026 - 06:57:28 EST


Update: this is still present on v7.4-rc5 -- the identical hwdep-fd hammer across snd_emux_free() yields 113 slab-use-after-free reports (snd_emux_hwdep_ioctl at five dereference points plus snd_hwdep_ioctl+0x11f reading hw->ops), so the open/release accounting + drain-wait patch still applies as-is.

Thanks.


> -----原始邮件-----
> 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> 发送时间:2026-12-01 11:23:55 (星期二)
> 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures(UAF)
>
> Opening /dev/snd/hwC*D* on an emux wavetable (emu10k1/emu8000 synth
> sub-driver) pins only the base-card module (try_module_get of
> hw->card->module in snd_hwdep_open()); the emux sub-driver module is
> not pinned, and removing the sub-driver (rmmod snd_emu10k1_synth, or
> sysfs unbind which bypasses module refcounts) runs snd_emux_free(),
> which unconditionally kfree()s the struct snd_hwdep, the whole soundfont
> list (snd_sf_free()) and the struct snd_emux itself -- while the fd
> stays open with file->private_data pointing at the freed hwdep and
> in-flight snd_emux_hwdep_ioctl() calls still executing (snd_hwdep_ioctl
> holds no open_mutex, and snd_hwdep_dev_disconnect() checks neither
> hwdep->used nor replaces f_op the way a whole-card disconnect does).
> Verified on mainline 7.3 with a KASAN kernel and the real in-tree
> snd-emux-synth module (harness attached): one hundred slab-use-after-free
> reports while a hwdep fd hammers VERSION/MEM_AVAIL/MISC_MODE ioctls
> across snd_emux_free() -- snd_emux_hwdep_ioctl() at five distinct
> dereference points (emu->sflist, emu->memhdr, emu->portptrs including a
> UAF write, hw->private_data) plus snd_hwdep_ioctl() reading hw->ops --
> and, with rmmod, the module text can additionally be unloaded while
> those callbacks are still running (snd_hwdep_f_ops.owner pins the
> snd-hwdep module, not the emux one). Needs the synth sub-driver
> probed; triggering the in-flight overlap needs only /dev/snd access.
> Consequences: multi-object UAF read/write (function pointer tables,
> mutex objects) and execution of unloaded module text -- crashes and a
> controlled indirect-call surface.
>
> The detailed description:
>
> * Test environment / affected version
>
> Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC
> -- vanilla mainline 7.3 tree, unmodified sources; KASAN enabled,
> QEMU/KVM x86_64 guest. sound/core/hwdep.c, sound/synth/emux/* and
> sound/core/rawmidi.c are byte-identical between the tested tree and
> the report baseline.
>
> * Mechanism
>
> - snd_hwdep_open() (hwdep.c:88) pins hw->card->module only; the emux
> hwdep registers no ops.open/ops.release hooks
> (emux_hwdep.c:126-131), so nothing increments snd_emux usage for
> hwdep fds (contrast: sequencer port subscribe and OSS-sequencer
> open both go through snd_emux_inc_count(), which also pins
> emu->ops.owner -- the sub-driver module).
> - snd_hwdep_ioctl() (hwdep.c:236-254) dispatches hw->ops.ioctl
> without holding hw->open_mutex; the emux ioctl touches
> hw->private_data, emu->sflist, emu->memhdr and emu->portptrs.
> - The sub-driver removal path snd_emux_free() (emux.c:124-143)
> performs snd_emux_delete_hwdep() -> snd_device_free() ->
> snd_hwdep_dev_disconnect() (which does not consult hwdep->used and
> does not revoke f_op for open fds) -> snd_hwdep_free() -> kfree
> (hwdep), then snd_sf_free() -> kfree(sflist), kfree(emu->voices),
> kfree(emu) -- none of it waits for hwdep->used to drop.
> - Whole-card snd_card_free() is NOT affected: snd_card_disconnect()
> replaces open fds' f_op with snd_shutdown_f_ops and the teardown
> waits for all files to close before the free sequence runs. The
> sub-driver independent removal bypasses that protocol entirely --
> that bypass is the defect.
>
> The same shape exists for the emux virtual rawmidi devices
> (snd_emux_delete_virmidi() -> snd_device_free() of RAWMIDI devices;
> snd_rawmidi_open() likewise only pins the card module and
> snd_rawmidi_free() kfrees unconditionally), and for the probe error
> path of snd_emux_register() (init_hwdep registers the device, then a
> later init_seq failure frees everything through snd_emux_free()).
> This patch fixes the hwdep fd face; the rawmidi face is mentioned
> for completeness and needs the equivalent treatment.
>
> * Trigger model
>
> emu10k1/emu8000 hardware with the synth sub-driver probed
> (hwC*D2 fixed for emu10k1). One process holds the exclusive hwdep
> fd open and issues ioctls; another (root, or hot-unplug hardware
> event) runs `rmmod snd_emu10k1_synth` (works: hwdep fds contribute
> no sub-driver module refcount) or writes the driver's unbind file.
> Consequence: UAF read/write on struct snd_hwdep (function pointer
> table hw->ops, hw->used decrement), struct snd_emux, struct
> snd_sf_list (presets_mutex operations on freed memory) and the
> portptrs targets, plus execution of already-unloaded module text in
> the rmmod case.
>
> * Reproducer and results on 7.3
>
> synthrace.c registers the real in-tree emux on a virtual card
> (snd_emux_new/snd_emux_register, backend ops mirroring emu10k1);
> synth_test holds /dev/snd/hwC0D0 open and loops the three ioctls;
> writing 1 to the module's "free" parameter runs the real
> snd_emux_free() (the sub-driver remove body) under the hammer.
>
> Results (7.3, KASAN, 2 vCPU): 100 slab-use-after-free reports:
> snd_emux_hwdep_ioctl+0x73/0x15d/0x1bd/0x2be/0x2d6 (emu/sflist/memhdr/
> portptrs accesses, including a UAF write) and
> snd_hwdep_ioctl+0x11f (hw->ops read). A harness run with the free
> disabled stays clean.
>
> * Fix
>
> Give hwdep fds the same user accounting the sequencer and OSS paths
> already have, and make the sub-driver removal wait for them:
>
> - register hw->ops.open / hw->ops.release calling
> snd_emux_inc_count() / snd_emux_dec_count(). inc_count already
> try_module_get()s emu->ops.owner (the sub-driver module), so an
> open hwdep fd now blocks rmmod exactly like a sequencer/OSS user;
> - count open fds additionally in a new atomic emu->hwdep_users and
> make snd_emux_free() wait for it to drain (at the top, before any
> sub-structure is torn down, so in-flight ioctls cannot observe
> partially freed state either).
>
> sysfs unbind bypasses module refcounts, which is why the drain wait
> is needed in addition to the refcount. This matches the semantics
> the whole-card path already provides (snd_card_disconnect() waits
> for open files before anything is freed).
>
> The patch is a suggested direction, written against 7.3 and reviewed
> by inspection; it has not been runtime-validated (the reported
> reproduction used the unpatched kernel). Happy to test any
> counter-proposal.
>
> * Patch
>
> [PATCH] ALSA: emux: drain open hwdep users before freeing emux
> structures
>
> snd_emux_free() (the sub-driver remove path, reachable via
> rmmod snd_emu10k1_synth or sysfs unbind) kfrees the struct snd_hwdep,
> the soundfont list and the struct snd_emux without waiting for open
> hwdep file descriptors: hwdep fds pin only the base-card module, the
> emux hwdep registers no open/release hooks, and an in-flight
> snd_emux_hwdep_ioctl() (which does not hold open_mutex) plus every
> later operation on the stale fd hits freed memory. KASAN on 7.3 shows
> 100 UAF reports in snd_emux_hwdep_ioctl/snd_hwdep_ioctl for this
> interleave.
>
> Register hwdep open/release hooks that take snd_emux_inc_count()
> (also pinning emu->ops.owner against rmmod, like the sequencer and
> OSS-sequencer paths already do), count them in a new emu->hwdep_users,
> and make snd_emux_free() wait for that count to drain before tearing
> anything down -- mirroring the drain that snd_card_disconnect()
> performs for whole-card removal. Note that sysfs unbind is not
> covered by module refcounts, which is why the drain wait is required
> in addition to the refcount.
>
> --- 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; /* open hwdep fds */
> + wait_queue_head_t free_wait; /* sub-driver removal waits for hwdep_users */
> int client; /* For the sequencer client */
> int ports[SNDRV_EMUX_MAX_PORTS]; /* The ports for this device */
> --- 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;
> @@ -126,6 +130,19 @@ int snd_emux_free(struct snd_emux *emu)
> if (! 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);
> --- a/sound/synth/emux/emux_hwdep.c
> +++ b/sound/synth/emux/emux_hwdep.c
> @@ (before snd_emux_hwdep_ioctl)
>
>
> +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
> */
> static int
> snd_emux_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
> @@ -120,6 +166,9 @@ int
> hw->iface = SNDRV_HWDEP_IFACE_EMUX_WAVETABLE;
> hw->ops.ioctl = snd_emux_hwdep_ioctl;
> /* 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;
> --
> 2.47.1
>
>
> Please keep me in Cc; I am not subscribed to the lists.
>
> Regards,
> Hanxiang Xu
> xuhx@xxxxxxxxxxx