Re: ALSA: hwdep: struct snd_hwdep is kfree()d while open files, an in-flight ioctl and delayed close() still use it (UAF)

From: 徐晗翔

Date: Tue Dec 01 2026 - 07:01:01 EST


Update: still present in 7.4-rc5 -- snd_hwdep_free() still kfree()s
the struct with open fds holding it, and the same reproducer hits
immediately (28 reports in the deterministic hold variant and 109 in
the in-flight variant, same snd_hwdep_release/snd_hwdep_ioctl call
sites as originally reported).

Thanks.


> -----原始邮件-----
> 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> 发送时间:2026-12-01 12:31:40 (星期二)
> 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: ALSA: hwdep: struct snd_hwdep is kfree()d while open files, an in-flight ioctl and delayed close() still use it (UAF)
>
> An ALSA sub-driver that registers devices on a live card -- here the
> emux wavetable drivers (snd_emu10k1_synth / snd_emu8000_synth) behind
> the sequencer device framework -- removes its hwdep device with
> snd_device_free() on its own remove path. That synchronously runs
> snd_hwdep_dev_disconnect() -- which only briefly takes register_mutex
> and hwdep->open_mutex, wakes the open waiters, unregisters the minor
> and drops off the registry, without waiting for hw->used to drop and
> without replacing any file->f_op -- and then snd_hwdep_free() kfree()s
> struct snd_hwdep. Every open /dev/snd/hwC*D* fd keeps
> file->private_data pointing at the freed object: in-flight ioctls
> dereference it (hw->ops, hwdep.c:251-252), a close() runs
> snd_hwdep_release() on it (hw->card->module read, mutex_lock on the
> freed open_mutex, hw->used--), and a future open() fails with -ENODEV
> while the dangling fd stays armed. We reproduced both shapes on
> vanilla 7.3: a deterministic variant (hold an fd across the sub-driver
> free, then ioctl+close) hit six distinct use-after-free reports inside
> snd_hwdep_release() (+0x2b the hw->card->module read, +0x4e/+0x5a the
> freed open_mutex, +0x80 used--, +0x9b/+0xb3 wake_up), and an in-flight
> variant (ioctl hammer racing the free) produced 119 reports
> (snd_hwdep_ioctl+0x11f reading hw->ops, snd_emux_hwdep_ioctl+0x73
> walking hw->private_data into the freed emux). The sequencer device
> framework holds no module reference for bound devices and the hwdep
> f_ops owner is the ALSA core module, so `rmmod snd_emu10k1_synth` is
> not blocked by the open fd. Removing the sub-driver requires root;
> holding and using the fd needs no privilege.
>
> 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 release tarball, KASAN enabled, QEMU/KVM
> x86_64 guest. The emux/sequencer/virmidi code is the byte-identical
> in-tree modules (built from an unmodified tree with a config variant
> that only adds SND_EMU10K1/SND_SYNTH_EMUX/SND_SEQ_* as modules);
> sound/core/hwdep.c and sound/synth/emux/* are byte-identical to
> mainline 7.3; line numbers below are from 7.3.
>
> * Mechanism
>
> - Open side holds no lifetime guarantee for the object:
> snd_hwdep_open() pins the card (sound.c:111) and the card module
> (hwdep.c:88) only; the emux hwdep registers no ops.open/release
> (emux_hwdep.c:126-129), so opening pins no sub-driver module at
> all; success stores file->private_data = hw (hwdep.c:132) and the
> f_ops owner is the ALSA core module, so the fd outlives any
> module unload.
>
> - Remove side is synchronous and does not drain:
> snd_emux_delete_hwdep() (emux_hwdep.c:147) -> snd_device_free()
> -> __snd_device_free() -> snd_hwdep_dev_disconnect()
> (hwdep.c:455-473: register_mutex + open_mutex held only briefly,
> wake_up(open_wait), minor unregistered, list_del_init) and then
> snd_hwdep_free() -> kfree(hwdep) (hwdep.c:346). There is no wait
> for hw->used == 0, no f_op swap, no cancelation point.
>
> - Whole-card removal triggers the same free chain nested inside
> snd_device_disconnect_all() (the seq device's device_del()
> synchronously unbinds the sub-driver, whose remove calls the same
> snd_device_free()); there snd_card_disconnect() has already
> swapped file->f_op to the dummy shutdown ops, so only syscalls
> that are already in flight (past the f_op dispatch) can hit the
> freed object -- a narrower but real window, and it happens before
> the "wait until all files are closed" barrier runs.
>
> - An open() parked inside its retry loop (hwdep.c:116-118: unlock ->
> schedule -> relock) is woken by the disconnect and then takes the
> mutex / reads hw->card->shutdown on the freed object; with
> exclusive==1 but used==0 (the emux hwdep registers no ops.open)
> the loop can even complete successfully on the disconnected,
> about-to-be-freed device.
>
> * Trigger model
>
> Two parts, both reachable on a soundfont-capable card
> (emu10k1/emu8000 with the snd-emux-synth sub-driver loaded):
>
> - the unprivileged part: open /dev/snd/hwC*D2 and use it (the
> exclusive flag limits concurrent openers, not lifetime);
>
> - the privileged part: `rmmod snd_emu10k1_synth` (or sysfs
> unbind) while that fd is open or an ioctl is in flight.
>
> Consequence: use-after-free reads/writes on struct snd_hwdep
> (including mutex_lock() on the freed open_mutex and an
> hw->ops.ioctl function-pointer load), plus a second-order UAF into
> the freed emux private data.
>
> * Reproducer (attached: synthrace.c, fwmr_test.c) and results on 7.3
>
> synthrace.c registers a REAL emux on a virtual card through the
> exported emux API with exactly the snd_emu10k1_synth_probe/remove()
> shape (snd_emux_new -> ops -> snd_emux_register creating the real
> /dev/snd/hwC0D0; snd_emux_free() is the sub-driver remove body).
> Zero kernel source modification. fwmr_test.c is the userspace
> side.
>
> # controlled/deterministic (fd held across the free):
> insmod synthrace.ko
> ./fwmr_test mr02hold 4 & # open, probe, hold, then ioctl+close
> sleep 2
> echo 1 > /sys/module/synthrace/parameters/free # snd_emux_free()
> # in-flight variant:
> ./fwmr_test mr02hammer 20000 & # ioctl hammer
> sleep 2
> echo 1 > /sys/module/synthrace/parameters/free
>
> Results (7.3, 2 vCPU, KASAN):
>
> - deterministic hold: first close after the free reports
> slab-use-after-free at snd_hwdep_release+0x2b/0x4e/0x5a/0x80/
> 0x9b/0xb3 (six distinct accesses on one close);
>
> - in-flight hammer: 119 reports -- snd_hwdep_ioctl+0x11f
> (hw->ops read) x60 and snd_emux_hwdep_ioctl+0x73 x59; reopen
> after the free returns -ENODEV as expected.
>
> * Suggested patch
>
> Anchor struct snd_hwdep's lifetime on its embedded device: a custom
> release callback performs the kfree(), every open file takes
> get_device(hw->dev) and every release path puts it back, and
> snd_hwdep_free() only drops the card/registry reference (the
> private_free ordering is deliberately unchanged). The complete
> patch against 7.3 follows (also kept as patch/0001-fw-mr02.patch);
> happy to rework per review.
>
> ---- patch begins ----
> ---
> --- a/sound/core/hwdep.c
> +++ b/sound/core/hwdep.c
> @@ -84,8 +84,10 @@
> return -ENXIO;
> if (hw == NULL)
> return -ENODEV;
> + get_device(hw->dev);
>
> if (!try_module_get(hw->card->module)) {
> + put_device(hw->dev);
> snd_card_unref(hw->card);
> return -ENODEV;
> }
> @@ -137,8 +139,10 @@
> }
> }
> mutex_unlock(&hw->open_mutex);
> - if (err < 0)
> + if (err < 0) {
> module_put(hw->card->module);
> + put_device(hw->dev);
> + }
> snd_card_unref(hw->card);
> return err;
> }
> @@ -159,6 +163,7 @@
>
> snd_card_file_remove(hw->card, file);
> module_put(mod);
> + put_device(hw->dev);
> return err;
> }
>
> @@ -336,6 +341,18 @@
> .mmap = snd_hwdep_mmap,
> };
>
> +/*
> + * The struct snd_hwdep memory itself is freed by the embedded device's
> + * release callback once the last reference (an open file) is dropped;
> + * snd_hwdep_free() only drops the card/registry reference.
> + */
> +static void snd_hwdep_dev_release(struct device *dev)
> +{
> + struct snd_hwdep *hwdep = container_of(dev, struct snd_hwdep, dev);
> +
> + kfree(hwdep);
> +}
> +
> static void snd_hwdep_free(struct snd_hwdep *hwdep)
> {
> if (!hwdep)
> @@ -343,7 +360,6 @@
> if (hwdep->private_free)
> hwdep->private_free(hwdep);
> put_device(hwdep->dev);
> - kfree(hwdep);
> }
>
> /**
> @@ -390,6 +406,7 @@
> snd_hwdep_free(hwdep);
> return err;
> }
> + hwdep->dev->release = snd_hwdep_dev_release;
>
> dev_set_name(hwdep->dev, "hwC%iD%i", card->number, device);
> #ifdef CONFIG_SND_OSSEMUL
> ---- patch ends ----
>
>
>
> Please keep me in Cc; I am not subscribed to the lists.
>
> Regards,
> Hanxiang Xu
> xuhx@xxxxxxxxxxx