[PATCH] ALSA: core: protect card->devices against concurrent disconnect/free -- snd_device UAF with split sub-driver removal (UAF)
From: 徐晗翔
Date: Mon Nov 30 2026 - 22:27:14 EST
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
// 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");