// 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 #include #include #include #include #include #include #include #include #include #include #include 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");