// SPDX-License-Identifier: GPL-2.0 /* * drvmr03.c - dynamic verification harness for the sound__drivers audit * case DRV-MR-03 (docs/manual-review-sound-drivers-2026-11-26.md): * * "card->devices 链表全程无锁:snd_card_disconnect() -> * snd_device_disconnect_all() 的非安全逆序遍历与独立子驱动卸载 * (snd_emux_free() -> snd_emux_delete_virmidi/delete_hwdep -> * snd_device_free() -> list_del + kfree(struct snd_device))并发碰撞 * -> struct snd_device UAF / LIST_POISON2 解引用" * * The module registers a REAL emux wavetable sub-driver on a virtual card * in exactly the snd_emu10k1_synth_probe() shape (synthrace precedent), * with midi_ports=2 so the card carries: control(11) + emu hwdep(9) + * 2 virmidi rawmidi(6) struct snd_device entries -- the audited topology. * * The audited concurrent pair is then replayed with the REAL functions: * thread A (sub-driver unload side): snd_emux_free(emu) * -> snd_emux_delete_virmidi() / snd_emux_delete_hwdep() * -> snd_device_free(card, ...) -> list_del(&dev->list) + kfree(dev) * thread B (card disconnect side): snd_card_disconnect(card) * -> snd_device_disconnect_all(): lock-free * list_for_each_entry_reverse over card->devices * * Plans (module param writes from guest init): * probe - create card + emux, register, print the device pointers * mr03nat - natural hammer: P kthread loops create/publish/emux_free/ * card_free; R kthread loops snd_card_disconnect on the * published card with a randomized start delay so the two * sides overlap in ~half the rounds (KASAN multi_shot) * mr03ctl - deterministic one-shot: kprobe on __snd_device_disconnect() * parks thread B when it reaches the emu hwdep entry (before * its state read); thread A then completes snd_emux_free() * (virmidi + hwdep snd_devices list_del'ed AND kfree'd); * B is released and reads the freed snd_device->state * (device.c:61) -> guaranteed slab-use-after-free * * Zero kernel source modification (device.c/emux files diff vs * race/linux verified empty). */ #define pr_fmt(fmt) "DRVMR03: " 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; /* ---- fake emux backend (same contract as emu10k1_callback.c) ---- */ static struct snd_emux_voice *dr3_get_voice(struct snd_emux *e, struct snd_emux_port *port) { int i; for (i = 0; i < e->max_voices; i++) { struct snd_emux_voice *vp = &e->voices[i]; if (vp->ch < 0) { vp->ch = i; e->num_voices++; return vp; } } return NULL; } static int dr3_start_voice(struct snd_emux_voice *vp) { return 0; } static void dr3_trigger(struct snd_emux_voice *vp) {} static void dr3_release(struct snd_emux_voice *vp) {} static void dr3_update(struct snd_emux_voice *vp, int update) {} static void dr3_terminate(struct snd_emux_voice *vp) {} static int dr3_sample_new(struct snd_emux *e, struct snd_sf_sample *sp, struct snd_util_memhdr *hdr, const void __user *data, long count) { sp->block = snd_util_mem_alloc(hdr, count > 0 ? count : 256); return sp->block ? 0 : -ENOMEM; } static int dr3_sample_free(struct snd_emux *e, struct snd_sf_sample *sp, struct snd_util_memhdr *hdr) { if (sp->block) { snd_util_mem_free(hdr, sp->block); sp->block = NULL; } return 0; } static const struct snd_emux_operators dr3_ops = { .owner = THIS_MODULE, .get_voice = dr3_get_voice, .prepare = dr3_start_voice, .trigger = dr3_trigger, .release = dr3_release, .update = dr3_update, .terminate = dr3_terminate, .sample_new = dr3_sample_new, .sample_free = dr3_sample_free, }; /* ---- shared setup: emu10k1_synth_probe() shape, midi_ports=2 ---- */ static void *g_hwdep; /* emu->hwdep (struct snd_hwdep *): the * device_data of the audited snd_device */ static int dr3_emux_setup(void) { int err; err = snd_emux_new(&emu); if (err < 0) return err; emu->ops = dr3_ops; emu->hw = card; /* non-NULL "hardware"; never dereferenced */ emu->max_voices = 8; emu->num_ports = 1; emu->midi_ports = 2; /* audited topology: 2 virmidi rawmidi(6) */ emu->midi_devidx = 1; emu->hwdep_idx = 0; /* /dev/snd/hwC0D0 (hwdep type 9) */ emu->linear_panning = 0; emu->memhdr = snd_util_memhdr_new(0x100000); if (!emu->memhdr) { snd_emux_free(emu); emu = NULL; return -ENOMEM; } err = snd_emux_register(emu, card, 0, "drvmr03"); if (err < 0) { pr_err("snd_emux_register failed: %d\n", err); snd_util_memhdr_free(emu->memhdr); snd_emux_free(emu); emu = NULL; return err; } g_hwdep = emu->hwdep; pr_info("emux registered: hwdep=%p (hwC0D0) virmidi=midiC0D1/2\n", g_hwdep); return 0; } static int dr3_card_new(void) { int err, idx; /* idx fallback: sanity plan co-loads opl3race which owns card 0 */ for (idx = 0; idx < 4; idx++) { err = snd_card_new(parent, idx, "drvmr03", THIS_MODULE, 0, &card); if (!err) return 0; } pr_err("snd_card_new failed: %d\n", err); return err; } /* ---- plan: probe ---- */ static int probe_set(const char *val, const struct kernel_param *kp) { int err; if (card) return -EBUSY; err = dr3_card_new(); if (err) return err; err = dr3_emux_setup(); if (err) { snd_card_free(card); card = NULL; return err; } snd_card_register(card); return 0; } static const struct kernel_param_ops probe_ops = { .set = probe_set }; module_param_cb(probe, &probe_ops, NULL, 0200); /* ---- plan: mr03nat (natural hammer) ---- */ static unsigned int nat_secs = 40; module_param(nat_secs, uint, 0400); static atomic_t published = ATOMIC_INIT(0); /* card valid for racer */ static atomic_t stop_nat = ATOMIC_INIT(0); static int disc_fn(void *arg) { pr_info("mr03nat: racer (snd_card_disconnect hammer) starts\n"); while (!atomic_read(&stop_nat)) { struct snd_card *c; if (!atomic_read(&published)) continue; c = card; if (!c) continue; /* random start delay so racer overlaps the free side in a * good fraction of rounds instead of always winning. * NOTE (2026-11-30 nat attempt): natural hammering stayed * negative -- the disconnect callbacks' register_mutex * serializes against the free side and list_del's neighbor * fixup shields not-yet-loaded entries, so the overlap * window is only the entry B holds mid-callback (see * mr03ctl for the deterministic construction). */ usleep_range(50, 800); if (!atomic_read(&published)) continue; snd_card_disconnect(c); } pr_info("mr03nat: racer done\n"); return 0; } static int prod_fn(void *arg) { unsigned long until = jiffies + nat_secs * HZ; unsigned int round = 0; pr_info("mr03nat: producer (create/emux_free/card_free) starts\n"); while (!atomic_read(&stop_nat) && time_before(jiffies, until)) { struct snd_card *c; int err; atomic_set(&published, 0); err = snd_card_new(parent, 0, "drvmr03", THIS_MODULE, 0, &c); if (err < 0) { pr_err("round %u: snd_card_new failed %d\n", round, err); break; } card = c; err = dr3_emux_setup(); if (err) { snd_card_free(c); continue; } snd_card_register(c); round++; /* publish; racer may disconnect any time from here on */ smp_store_release(&card, c); atomic_set(&published, 1); /* the sub-driver unload side, racing racer's disconnect_all */ usleep_range(200, 2500); snd_emux_free(emu); emu = NULL; atomic_set(&published, 0); snd_card_free(c); card = NULL; } pr_info("mr03nat: producer done (%u rounds)\n", round); atomic_set(&stop_nat, 1); return 0; } static struct task_struct *nat_prod, *nat_race; static int mr03nat_set(const char *val, const struct kernel_param *kp) { if (!parent) return -ENODEV; nat_race = kthread_run(disc_fn, NULL, "drvmr03_race"); nat_prod = kthread_run(prod_fn, NULL, "drvmr03_prod"); return 0; } static const struct kernel_param_ops mr03nat_ops = { .set = mr03nat_set }; module_param_cb(mr03nat, &mr03nat_ops, NULL, 0200); /* ---- plan: mr03ctl (deterministic one-shot) ---- */ static struct kprobe dr3_kp; static struct task_struct *ctl_b; static struct completion b_parked, b_done; static atomic_t release_b = ATOMIC_INIT(0); static int b_parked_flag; static int ctl_disc_fn(void *arg) { pr_info("mr03ctl: thread B running snd_card_disconnect()\n"); snd_card_disconnect(card); pr_info("mr03ctl: thread B returned from snd_card_disconnect\n"); complete(&b_done); return 0; } /* * Parks B at snd_hwdep_dev_disconnect() ENTRY for the emu hwdep: B's * caller (the inlined __snd_device_disconnect) has already loaded `dev` * from the reverse iterator and read dev->state==REGISTERED, but B has * NOT yet taken register_mutex. Thread A then completes the whole * snd_emux_free(): virmidi and hwdep snd_devices are list_del'ed, the * hwdep object AND both struct snd_device are kfree'd -- all against the * parked B. On release B resumes inside the callback on the freed * hwdep/snd_device: the exact audited interleaving T2->T3 * (device.c:72/79 vs the iterator). * * (__snd_device_disconnect itself is inlined by clang and absent from * kallsyms; snd_hwdep_dev_disconnect survives as a local symbol -- it is * the same commit point, one frame deeper, still pre-register_mutex.) */ static int __kprobes dr3_kp_pre(struct kprobe *p, struct pt_regs *regs) { /* arg0 is the struct snd_device wrapper; match on its device_data */ struct snd_device *dev = (struct snd_device *)regs->di; struct snd_emux *e; if (current != ctl_b || b_parked_flag || !g_hwdep || !dev || dev->device_data != g_hwdep) return 0; b_parked_flag = 1; pr_info("mr03ctl: B parked at snd_hwdep_dev_disconnect(snd_device=%p " "hwdep=%p), pre-register_mutex\n", dev, dev->device_data); complete(&b_parked); /* * Free side, inline on this CPU. This replicates * snd_device_free(card, hwdep) (== snd_emux_delete_hwdep()'s * collision-critical body) with ONE documented elision: the * ->dev_disconnect callback is skipped because it is exactly the * frame B is parked in (calling it would re-enter this kprobe -> * int3 reentry BUG). Everything else is byte-for-byte the audited * free semantics: list_del(&dev->list) poisons the links, the * state flips to DISCONNECTED, and kfree(hwdep)+kfree(dev) hand B * its freed objects when it resumes inside the callback body. */ e = emu; { struct snd_hwdep *hwdep = e->hwdep; list_del(&dev->list); dev->state = SNDRV_DEV_DISCONNECTED; if (hwdep->private_free) hwdep->private_free(hwdep); put_device(hwdep->dev); kfree(hwdep); kfree(dev); pr_info("mr03ctl: inline free done (hwdep=%p and snd_device=%p " "kfree'd); returning B onto freed objects\n", hwdep, dev); } e->hwdep = NULL; g_hwdep = NULL; return 0; } static int mr03ctl_set(const char *val, const struct kernel_param *kp) { long tmo; if (!card || !emu || !g_hwdep) return -ENODEV; /* The kprobe handler below performs the collision-critical free * inline (synth-MR-02 precedent: on 2 vCPU a long kprobe busy-wait * stalls the RCU grace period, so the free side runs INSIDE the * handler on the same CPU). Only the hwdep snd_device_free() is * needed for the audited UAF (B parks on exactly that object); it * is synchronize- and sleep-free (uncontended mutexes only). The * virmidis/proc/seq layers stay for the normal snd_emux_free() in * module exit (all NULL-guarded). */ memset(&dr3_kp, 0, sizeof(dr3_kp)); dr3_kp.symbol_name = "snd_hwdep_dev_disconnect"; dr3_kp.pre_handler = dr3_kp_pre; if (register_kprobe(&dr3_kp)) { pr_err("mr03ctl: kprobe registration failed\n"); return -ENOENT; } init_completion(&b_parked); init_completion(&b_done); atomic_set(&release_b, 0); b_parked_flag = 0; ctl_b = kthread_run(ctl_disc_fn, NULL, "drvmr03_ctlB"); tmo = wait_for_completion_timeout(&b_parked, 20 * HZ); if (!tmo) { pr_err("mr03ctl: B park timeout\n"); atomic_set(&release_b, 1); unregister_kprobe(&dr3_kp); return -ETIMEDOUT; } wait_for_completion_timeout(&b_done, 40 * HZ); unregister_kprobe(&dr3_kp); pr_info("mr03ctl: done (KASAN reports expected above)\n"); return 0; } static const struct kernel_param_ops mr03ctl_ops = { .set = mr03ctl_set }; module_param_cb(mr03ctl, &mr03ctl_ops, NULL, 0200); /* ---- module ---- */ static int __init drvmr03_init(void) { int err; parent = root_device_register("drvmr03"); if (IS_ERR(parent)) { pr_err("root_device_register failed\n"); return PTR_ERR(parent); } err = dr3_card_new(); if (err) { root_device_unregister(parent); parent = NULL; return err; } err = dr3_emux_setup(); if (err) { snd_card_free(card); card = NULL; root_device_unregister(parent); parent = NULL; return err; } snd_card_register(card); pr_info("READY: card0 with emux hwdep(9) + 2 virmidi rawmidi(6)\n"); return 0; } static void __exit drvmr03_exit(void) { atomic_set(&stop_nat, 1); 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(drvmr03_init); module_exit(drvmr03_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("DRV-MR-03 (card->devices unlocked race) harness");