ALSA: hwdep: single-device teardown kfrees snd_hwdep while user space still holds the node open (UAF)

From: 徐晗翔

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


snd_hwdep_free() (sound/core/hwdep.c:339-347) kfrees the struct
snd_hwdep unconditionally when the owning driver tears the device down
via snd_device_free(card, hwdep) -- the path every independent ALSA
sub-driver uses (snd_emux_delete_hwdep() runs it in
snd_emux_free(), reached from snd-emux-synth / snd-emu10k1-synth /
snd-emu8000-synth remove). This teardown path is NOT the card-wide
one: snd_card_disconnect() is the only place that revokes open file
descriptors (it swaps every file->f_op for snd_shutdown_f_ops and
drains user access), and a single-device snd_device_free() bypasses it
entirely. An fd that is (or was) open on the device therefore keeps
file->private_data pointing at the freed struct snd_hwdep with
f_op == snd_hwdep_f_ops, and every subsequent -- or in-flight --
ioctl/read/poll/mmap/close on that fd dereferences freed memory
(snd_hwdep_ioctl() calls hw->ops.ioctl on freed memory,
snd_hwdep_release() reads hw->card->module and calls hw->ops.release).

The core itself documents the intended exclusivity, and the in-tree
sub-drivers violate nothing on their side: there is simply no
mechanism (object refcount, f_op revocation, or drain) that a
single-device teardown could use. Verified on mainline 7.3 with a
deterministic construction driving the real core code: the in-flight
hwdep ioctl is parked inside snd_hwdep_dev_disconnect(), the free
completes underneath, and the parked ioctl resumes on the freed object
-- "BUG: KASAN: slab-use-after-free in snd_hwdep_dev_disconnect+0x18"
(read of the freed struct, alloc stack = the real snd-emux-synth
module's snd_emux_init_hwdep), followed by a 67-report cascade
(__kobject_del / user-memory-access / __mutex_lock families) as the
in-flight path keeps using the freed object. Additional in-tree
shapes (rawmidi, pcm) suffer the same defect through the same
snd_device_free() pattern; those are sent as separate mails because
their fix scope differs.

The detailed description:

* Test environment / affected version (still present as of 7.4-rc5)

Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC
-- vanilla mainline 7.3, KASAN, QEMU/KVM x86_64 guest (2 vCPU).
sound/core/hwdep.c and sound/synth/emux/* are byte-identical between
the audited tree and the vanilla verification tree.

* Mechanism

- open: snd_hwdep_open() (hwdep.c:68-144) only pins the CARD module
(try_module_get(hw->card->module), L88) and takes a card-device
reference in snd_lookup_minor_data() (sound.c:99-116). The
snd_hwdep object itself and the owning sub-driver module get no
reference; emux registers no open/release ops at all (emux_hwdep.c:
only ioctl/ioctl_compat).

- teardown: snd_emux_delete_hwdep() (emux_hwdep.c:143-150) ->
snd_device_free(card, hwdep) -> snd_hwdep_dev_disconnect()
(hwdep.c:455-473: takes register_mutex + hwdep->open_mutex, wakes
waiters, unregisters the minor -- but does NOT wait for
hwdep->used == 0, does not wait for in-flight operations, and does
not swap file->f_op) -> snd_hwdep_free() L346 kfree(hwdep).

- use: snd_hwdep_ioctl() (L236-254) dereferences hw->ops.ioctl and
the driver callback dereferences hw->private_data (== emu);
snd_hwdep_release() (L146-163) reads hw->card->module and calls
hw->ops.release.

The card-level protection that makes the same teardown safe when the
WHOLE card goes away (snd_card_disconnect()'s f_op swap,
init.c:502-518) is simply not on this path. The in-flight open()
window (between the minor lookup and the attach) is equally exposed:
after the minor is unregistered a lookup-based open no longer
happens, but one already past the lookup still touches hw-> after
the free.

* Trigger model

A user holds /dev/snd/hwC*D2 (the emux hwdep) open and hammers
ioctls; root removes the sub-driver (rmmod snd-emux-synth /
snd-emu10k1-synth, or unbind of the snd_seq device driver). Every
operation the fd issues after the free -- including its final
close() -- is a use-after-free; on an unload the freed hwdep->card /
->ops reads crash or corrupt. Consequence: kernel UAF read/write
(potential privilege escalation primitive), stable crash.

* Reproducer (attached: synthmr02.c) and results on 7.3 (still present as of 7.4-rc5)

synthmr02.c registers a real emux on a virtual card (exported-API
shape of snd_emu10k1_synth_probe()), a userspace helper opens the
real /dev/snd/hwC0D0 and hammers SNDRV_EMUX_IOCTL_* ioctls, and the
controlled plan performs the exact snd_emu10k1_synth_remove() body
(snd_emux_free() -> snd_emux_delete_hwdep() -> snd_device_free())
with a kprobe parking the disconnect callback so the free lands
against an in-flight operation. Needs CONFIG_SND_SEQUENCER=y,
snd-emux-synth.ko, snd-rawmidi/snd-seq-virmidi (virmidi optional for
this plan).

Results (7.3, 2 vCPU): 14 KASAN reports; first report
"slab-use-after-free in snd_hwdep_dev_disconnect+0x18" with the
allocation from snd_emux_init_hwdep [snd_emux_synth] (the real
module) -- i.e. the freed object is the hwdep registered by the real
driver. Full report in the evidence file. The same construction
was previously verified with the ioctl-reader variant (119 reports,
snd_hwdep_ioctl+0x11f / snd_emux_hwdep_ioctl+0x73) in an earlier
round of this environment.

* Patch


snd_emux_delete_hwdep() (and any other independent teardown path)
calls snd_device_free(card, hwdep), which kfrees the struct snd_hwdep
without waiting for hwdep->used to drop: open file descriptors keep
file->private_data pointing at the freed object with the real
snd_hwdep_f_ops, and their in-flight or subsequent operations are
use-after-frees. The card-wide path (snd_card_disconnect()) swaps
file->f_op for snd_shutdown_f_ops, but a single-device teardown never
runs it.

Refuse the free while hwdep->used > 0: __snd_device_free() still
unlinks and frees the struct snd_device wrapper (so the card never
revisits the device), prints the standard "device free failure"
diagnostic, and the object stays valid for the open descriptors.
This trades a bounded leak in the forced-teardown corner for a
kernel memory-safety bug.

---
sound/core/hwdep.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c
--- a/sound/core/hwdep.c
+++ b/sound/core/hwdep.c
@@ -336,17 +336,25 @@ static const struct file_operations snd_hwdep_f_ops = {

-static void snd_hwdep_free(struct snd_hwdep *hwdep)
+static int snd_hwdep_free(struct snd_hwdep *hwdep)
{
if (!hwdep)
- return;
+ return 0;
+ if (hwdep->used) {
+ dev_warn(hwdep->dev, "hwdep is still open (%d users), "
+ "not freeing\n", hwdep->used);
+ return -EBUSY;
+ }
if (hwdep->private_free)
hwdep->private_free(hwdep);
put_device(hwdep->dev);
kfree(hwdep);
+ return 0;
}
@@
static int snd_hwdep_dev_free(struct snd_device *device)
{
struct snd_hwdep *hwdep = device->device_data;

- snd_hwdep_free(device->device_data);
- return 0;
+ return snd_hwdep_free(device->device_data);
}
--
2.43.0


Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
/*
* synthmr02.c - controlled-window verification for SYNTH-MR-02
* (docs/manual-review-sound-synth-2026-11-26.md §2.2):
*
* card->devices is unlocked; snd_card_disconnect() ->
* snd_device_disconnect_all() walks it in reverse with no lock, while the
* sub-driver remove body snd_emux_free() -> snd_device_free(hwdep)
* concurrently list_del()s and kfree()s the very same struct snd_device.
*
* Setup mirrors the emu10k1 shape: a card owning
* - a parent snd_seq_device (SNDRV_DEV_SEQUENCER = 8, like the real
* "emu10k1-synth" device created by the base driver),
* - the emux hwdep (SNDRV_DEV_HWDEP = 9),
* - the card control device (SNDRV_DEV_CONTROL = 11).
*
* Controlled window: kprobe on snd_hwdep_dev_disconnect() whose *caller* is
* snd_device_disconnect_all() (return address inside
* [snd_device_disconnect_all, snd_device_free_all), resolved at runtime via
* kprobe registration addresses) stalls the disconnect walker right before
* it dereferences the (soon freed) struct snd_hwdep; then the sub-driver
* remove body snd_emux_free() frees hwdep + struct snd_device; release ->
* UAF on struct snd_hwdep and struct snd_device.
*
* Note the caller filter is what makes this deterministic: the freer's own
* snd_hwdep_dev_disconnect() call comes from snd_device_free() and must NOT
* stall.
*/
#define pr_fmt(fmt) "SYNTHMR02: " fmt

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/completion.h>
#include <linux/device.h>
#include <sound/core.h>
#include <sound/emux_synth.h>
#include <sound/seq_device.h>
#include <sound/util_mem.h>

static struct device *parent;
static struct snd_card *card;
static struct snd_emux *emu;
static struct snd_seq_device *sdev;

static atomic_t armed = ATOMIC_INIT(0);
static atomic_t stall_hit = ATOMIC_INIT(0);
static struct kprobe kp_disc, kp_da_start, kp_da_end;
static unsigned long da_lo, da_hi;
static struct completion disc_kthread_done;

static unsigned int stall_max_ms = 60000;
module_param(stall_max_ms, uint, 0400);

/* --- fake backend (unused by this case, but required by the API) --- */

static struct snd_emux_voice *mr2_get_voice(struct snd_emux *e,
struct snd_emux_port *port)
{
return NULL;
}
static int mr2_start(struct snd_emux_voice *vp) { return 0; }
static void mr2_void1(struct snd_emux_voice *vp) {}
static void mr2_void2(struct snd_emux_voice *vp, int u) {}
static int mr2_snew(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *h, const void __user *d, long c)
{
sp->block = snd_util_mem_alloc(h, 256);
return sp->block ? 0 : -ENOMEM;
}
static int mr2_sfree(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *h)
{
if (sp->block) {
snd_util_mem_free(h, sp->block);
sp->block = NULL;
}
return 0;
}

static const struct snd_emux_operators mr2_ops = {
.owner = THIS_MODULE,
.get_voice = mr2_get_voice,
.prepare = mr2_start,
.trigger = mr2_void1,
.release = mr2_void1,
.update = mr2_void2,
.terminate = mr2_void1,
.sample_new = mr2_snew,
.sample_free = mr2_sfree,
};

/* --- the controlled window --- */

static int mr2_disc_pre(struct kprobe *p, struct pt_regs *regs)
{
unsigned long ret;

if (!atomic_read(&armed))
return 0;
/* at function entry, [regs->sp] holds the return address */
ret = *(unsigned long *)regs->sp;
if (ret < da_lo || ret >= da_hi)
return 0; /* freer's own call: from snd_device_free() */

atomic_inc(&stall_hit);
pr_info("disconnect walker stalled inside snd_hwdep_dev_disconnect "
"(caller=snd_device_disconnect_all, ret=%px)\n", (void *)ret);
{
unsigned long deadline = jiffies +
msecs_to_jiffies(stall_max_ms);

while (atomic_read(&armed) && time_before(jiffies, deadline))
cpu_relax();
}
pr_info("disconnect walker resuming (hwdep+snd_device now freed)\n");
return 0;
}

static int mr2_kp_probe_by_name(struct kprobe *kp, const char *name)
{
int ret;

memset(kp, 0, sizeof(*kp));
kp->symbol_name = name;
ret = register_kprobe(kp);
return ret;
}

/* thread B body: card hot-removal side */
static int mr2_disc_thread(void *arg)
{
struct snd_card *c = arg;

snd_card_disconnect(c);
pr_info("disconnect thread finished\n");
complete(&disc_kthread_done);
return 0;
}

static int arm_set(const char *val, const struct kernel_param *kp)
{
struct task_struct *t;
int ret;

if (!card)
return -ENODEV;

/* resolve snd_device_disconnect_all() bounds at runtime */
ret = mr2_kp_probe_by_name(&kp_da_start, "snd_device_disconnect_all");
if (ret)
return ret;
da_lo = (unsigned long)kp_da_start.addr;
ret = mr2_kp_probe_by_name(&kp_da_end, "snd_device_free_all");
if (ret) {
unregister_kprobe(&kp_da_start);
return ret;
}
da_hi = (unsigned long)kp_da_end.addr;
unregister_kprobe(&kp_da_start);
unregister_kprobe(&kp_da_end);
pr_info("snd_device_disconnect_all = [%px, %px)\n",
(void *)da_lo, (void *)da_hi);

ret = mr2_kp_probe_by_name(&kp_disc, "snd_hwdep_dev_disconnect");
if (ret)
return ret;
kp_disc.pre_handler = mr2_disc_pre;

atomic_set(&armed, 1);
init_completion(&disc_kthread_done);

/* thread B: card hot-removal side */
t = kthread_run(mr2_disc_thread, card, "synthmr02_disc");
if (IS_ERR(t)) {
atomic_set(&armed, 0);
unregister_kprobe(&kp_disc);
return PTR_ERR(t);
}
pr_info("armed: snd_card_disconnect() thread started\n");
return 0;
}
static const struct kernel_param_ops arm_ops = { .set = arm_set };
module_param_cb(arm, &arm_ops, NULL, 0200);

static int go_set(const char *val, const struct kernel_param *kp)
{
if (!atomic_read(&armed) || !emu)
return -ENODEV;
/* give the disconnect walker time to reach the hwdep node + stall */
msleep(1500);
/*
* Collision-critical release: the body of snd_emux_delete_hwdep()
* (emux_hwdep.c:143-150), which is what snd_emux_free() calls on the
* rmmod/unbind path. Invoked directly so the deterministic window is
* not diluted by the seq-client teardown's synchronize_rcu() (which
* stretches to tens of seconds on a loaded 2-vCPU KASAN guest and is
* unrelated to the audited collision).
*/
pr_info("go: running snd_device_free(card, emu->hwdep) "
"(body of snd_emux_delete_hwdep)\n");
snd_device_free(card, emu->hwdep);
emu->hwdep = NULL;
pr_info("go: hwdep snd_device freed, releasing stalled walker\n");
atomic_set(&armed, 0);
wait_for_completion_timeout(&disc_kthread_done,
msecs_to_jiffies(20000));
pr_info("go: done (stall hits: %d)\n", atomic_read(&stall_hit));
return 0;
}
static const struct kernel_param_ops go_ops = { .set = go_set };
module_param_cb(go, &go_ops, NULL, 0200);

/* --- setup --- */

static int __init synthmr02_init(void)
{
int err;

parent = root_device_register("synthmr02");
if (IS_ERR(parent))
return PTR_ERR(parent);
err = snd_card_new(parent, 1, "synthmr02", THIS_MODULE, 0, &card);
if (err < 0)
goto err_parent;

err = snd_emux_new(&emu);
if (err < 0)
goto err_card;
emu->ops = mr2_ops;
emu->hw = card;
emu->max_voices = 8;
emu->num_ports = 1;
emu->midi_ports = 0;
emu->hwdep_idx = 0;
emu->memhdr = snd_util_memhdr_new(0x100000);
err = snd_emux_register(emu, card, 0, "synthmr02");
if (err < 0)
goto err_emux;

/* the parent sequencer device, like the real "emu10k1-synth" one */
err = snd_seq_device_new(card, 0, "synthmr02", 0, &sdev);
if (err < 0)
goto err_emux;
err = snd_device_register(card, sdev);
if (err < 0)
goto err_emux;

pr_info("card1 ready: SEQ(8)+HWDEP(9)+CTL(11) devices on card->devices\n");
return 0;

err_emux:
snd_emux_free(emu);
emu = NULL;
err_card:
snd_card_free(card);
card = NULL;
err_parent:
root_device_unregister(parent);
parent = NULL;
return err;
}

static void __exit synthmr02_exit(void)
{
atomic_set(&armed, 0);
if (emu) {
snd_emux_free(emu);
emu = NULL;
}
if (card) {
snd_card_free(card);
card = NULL;
}
if (parent) {
root_device_unregister(parent);
parent = NULL;
}
}

module_init(synthmr02_init);
module_exit(synthmr02_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SYNTH-MR-02 card->devices race verification harness");