// SPDX-License-Identifier: GPL-2.0 /* * synthrace.c - dynamic verification harness for the emux wavetable audit * cases SYNTH-MR-01 / SYNTH-MR-03 / SYNTH-MR-04 * (docs/manual-review-sound-synth-2026-11-26.md). * * It drives the REAL in-tree emux code (snd-emux-synth.ko: emux.c, * emux_synth.c, soundfont.c, emux_hwdep.c, emux_proc.c, emux_seq.c) through * its exported API with a fake backend ("hw"), replicating exactly the * snd_emu10k1_synth_probe()/snd_emu10k1_synth_remove() usage shape: * * snd_emux_new() -> ops_setup -> fields -> snd_emux_register() * snd_emux_free() (sub-driver remove body) * * No kernel source is modified; the audited code paths are the byte-identical * in-tree modules (diff vs race/linux verified zero). * * Plans (module params, write "1" to trigger): * mr01nat - SYNTH-MR-01 natural hammering: kernel kthread loops * snd_emux_note_on() on the real emux port while another kernel * kthread loops the REAL snd_soundfont_remove_unlocked() / * snd_soundfont_remove_samples() (the hwdep ioctl bodies). * The soundfont itself is loaded from userspace through the real * hwdep SNDRV_EMUX_IOCTL_LOAD_PATCH path (synth_test load-loop). * mr01ctl - SYNTH-MR-01 controlled window: kprobe post_handler on * snd_soundfont_search_zone stalls the note_on thread right at * the start of its unprotected table[] window; one * snd_soundfont_remove_unlocked() runs and frees zone/sample/ * block underneath; release -> cascaded UAF. * free - SYNTH-MR-03/MR-04 trigger: snd_emux_free() with an open, * hammering hwdep fd (mr03) or proc reader (mr04) on the other * side. This is the exact snd_emu10k1_synth_remove() body. */ #define pr_fmt(fmt) "SYNTHRACE: " 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 backend ops (same contract as emu10k1_callback.c) ---- */ static atomic_t mr01_armed = ATOMIC_INIT(0); static atomic_t mr01_stalled = ATOMIC_INIT(0); static struct snd_emux_voice *sr_get_voice(struct snd_emux *e, struct snd_emux_port *port) { int i; /* * mr01ctl controlled window: parked mid note_on, AFTER table[] was * filled by snd_soundfont_search_zone() and INSIDE the voice_lock * configuration section, i.e. exactly inside the audited * "search returned -> last table[]/vp->zone/vp->block use" span * (emux_synth.c:63-114). Removing zone/sample/block underneath and * then resuming replays the SYNTH-MR-01 interleaving end-to-end. */ if (atomic_read(&mr01_armed) && !atomic_xchg(&mr01_stalled, 1)) { unsigned long deadline = jiffies + msecs_to_jiffies(20000); pr_info("mr01ctl: note_on parked mid-configuration " "(table[] window open)\n"); while (atomic_read(&mr01_armed) && time_before(jiffies, deadline)) cpu_relax(); pr_info("mr01ctl: note_on resuming\n"); } 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; } } /* recycle voice 0 so the hammering never starves */ if (e->max_voices > 0) { e->voices[0].ch = 0; return &e->voices[0]; } return NULL; } /* * MR-01 UAF write point analog: emu10k1 start_voice() does * emem->map_locked++ on the block handed down through vp->block; we touch * the block the same way (read+write through the freed snd_util_memblk). */ static int mr01_note = 60; static int mr01_vel = 100; static atomic_t sr_start_calls = ATOMIC_INIT(0); static int sr_start_voice(struct snd_emux_voice *vp) { struct snd_util_memblk *b = vp->block; atomic_inc(&sr_start_calls); if (b) b->offset++; /* UAF write on freed memblk */ return 0; } static void sr_trigger(struct snd_emux_voice *vp) {} static void sr_release(struct snd_emux_voice *vp) {} static void sr_update(struct snd_emux_voice *vp, int update) {} /* emu10k1 terminate_voice() reads vp->block (emem->map_locked--): analog */ static void sr_terminate(struct snd_emux_voice *vp) { struct snd_util_memblk *b = vp->block; if (b) { unsigned int t = b->size; /* UAF read on freed memblk */ (void)t; } } static int sr_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); if (!sp->block) return -ENOMEM; return 0; } static int sr_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 void sr_sample_reset(struct snd_emux *e) { snd_emux_terminate_all(e); /* same as snd_emu8000_sample_reset() */ } /* * SYNTH-MR-01's practically exploitable variant is the EMU10K1 backend * (emu10k1_ops has NO .sample_reset, emu10k1_callback.c:51-63): removals * then never terminate playing voices, so snd_emux_voice::block/::zone stay * dangling after snd_emu10k1_synth_free()/kfree(sp) and every later * terminate (e.g. voice recycle in note_on) dereferences the freed block. * Default to that audited shape; sample_reset_en=1 switches to the EMU8000 * shape (snd_emu8000_patch.c registers snd_emu8000_sample_reset). */ static int sample_reset_en; module_param(sample_reset_en, int, 0400); static const struct snd_emux_operators sr_ops = { .owner = THIS_MODULE, .get_voice = sr_get_voice, .prepare = sr_start_voice, .trigger = sr_trigger, .release = sr_release, .update = sr_update, .terminate = sr_terminate, .sample_new = sr_sample_new, .sample_free = sr_sample_free, }; /* ---- shared setup/teardown ---- */ /* * snd_emux_note_on() and the snd_soundfont_remove_*() bodies are real, * non-exported global symbols of snd-emux-synth.ko. Resolve them at runtime * via kprobe registration (kprobe-addr method, same technique as * kmod/nmi/nmi_race.c) and call them through their exact prototypes. */ typedef void (*sr_note_on_t)(void *port, int note, int vel, struct snd_midi_channel *chan); typedef int (*sr_sf_op_t)(struct snd_sf_list *sflist); static sr_note_on_t sr_note_on; static sr_sf_op_t sr_remove_unlocked; static sr_sf_op_t sr_remove_samples; static void *sr_kallsym(const char *name) { struct kprobe kp; void *addr; memset(&kp, 0, sizeof(kp)); kp.symbol_name = name; if (register_kprobe(&kp) < 0) { pr_err("kallsym(%s) failed\n", name); return NULL; } addr = kp.addr; unregister_kprobe(&kp); return addr; } static int sr_resolve_syms(void) { sr_note_on = (sr_note_on_t)sr_kallsym("snd_emux_note_on"); sr_remove_unlocked = (sr_sf_op_t)sr_kallsym("snd_soundfont_remove_unlocked"); sr_remove_samples = (sr_sf_op_t)sr_kallsym("snd_soundfont_remove_samples"); return sr_note_on && sr_remove_unlocked && sr_remove_samples ? 0 : -ENOENT; } static int sr_emux_setup(void) { int err; err = snd_emux_new(&emu); if (err < 0) return err; emu->ops = sr_ops; if (sample_reset_en) emu->ops.sample_reset = sr_sample_reset; emu->hw = card; /* non-NULL "hardware"; never dereferenced */ emu->max_voices = 8; emu->num_ports = 1; emu->midi_ports = 0; /* skip virmidi: not needed for these cases */ emu->hwdep_idx = 0; /* /dev/snd/hwC0D0 */ 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, "synthrace"); 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; } /* * The real emu10k1 flow nests snd_emux_register() inside the outer * snd_card_register() (seq-device probe), which then registers the * emux proc entry created above. Replicate that final registration. */ snd_card_register(card); pr_info("emux registered: hwdep=hwC0D0 sflist=%p proc ready\n", emu->sflist); return 0; } static void sr_card_new(void) { int err; parent = root_device_register("synthrace"); if (IS_ERR(parent)) { parent = NULL; return; } err = snd_card_new(parent, 0, "synthrace", THIS_MODULE, 0, &card); if (err < 0) { pr_err("snd_card_new failed: %d\n", err); root_device_unregister(parent); parent = NULL; card = NULL; } } /* ---- MR-01 natural hammering ---- */ static unsigned int nat_secs = 25; module_param(nat_secs, uint, 0400); static struct task_struct *noteon_thr; static struct task_struct *remove_thr; static int noteon_fn(void *arg) { struct snd_emux_port *port; struct snd_midi_channel *chan; unsigned long until = jiffies + nat_secs * HZ; port = emu->portptrs[0]; if (!port) { pr_err("mr01nat: no port\n"); return 0; } chan = &port->chset.channels[0]; pr_info("mr01nat: note_on hammer starts\n"); while (!kthread_should_stop() && time_before(jiffies, until)) { sr_note_on(port, 60, 100, chan); cond_resched(); } pr_info("mr01nat: note_on hammer done\n"); return 0; } static int remove_fn(void *arg) { unsigned long until = jiffies + nat_secs * HZ; int n = 0; pr_info("mr01nat: remove hammer starts\n"); while (!kthread_should_stop() && time_before(jiffies, until)) { /* these are the exact bodies of the hwdep ioctls * SNDRV_EMUX_IOCTL_REMOVE_LAST_SAMPLES / RESET_SAMPLES * * Throttled to ~2.5k/s: at unthrottled rates the removal * outpaces the userspace loader and the soundfont spends its * life empty, starving note_on of zones (see first mr01nat * run: 5.5M removes, 0 collisions). The audit race needs the * font alive while note_on is inside its table[] window. */ /* REMOVE_LAST_SAMPLES body only: unlike remove_samples() * (snd_sf_clear -> snd_sf_init) it keeps currsf/open_client * intact so the userspace loader can keep re-arming the font, * while still freeing the very zone/sample/block trio of the * audited SYNTH-MR-01 chain */ sr_remove_unlocked(emu->sflist); n++; usleep_range(200, 500); } pr_info("mr01nat: remove hammer done (%d cycles)\n", n); return 0; } static int mr01nat_set(const char *val, const struct kernel_param *kp) { if (!emu || !emu->portptrs[0]) return -ENODEV; noteon_thr = kthread_run(noteon_fn, NULL, "synthrace_noteon"); remove_thr = kthread_run(remove_fn, NULL, "synthrace_remove"); return 0; } static const struct kernel_param_ops mr01nat_ops = { .set = mr01nat_set }; module_param_cb(mr01nat, &mr01nat_ops, NULL, 0200); /* ---- MR-01 controlled window (park inside sr_get_voice, see above) ---- */ static struct completion mr01_done; static int mr01_noteon_once(void *arg) { struct snd_emux_port *port; struct snd_midi_channel *chan; port = emu->portptrs[0]; chan = &port->chset.channels[0]; atomic_set(&sr_start_calls, 0); sr_note_on(port, mr01_note, mr01_vel, chan); pr_info("mr01ctl: note_on returned, start_voice calls=%d\n", atomic_read(&sr_start_calls)); complete(&mr01_done); return 0; } static int mr01ctl_set(const char *val, const struct kernel_param *kp) { struct task_struct *t; int rc; if (!emu || !emu->portptrs[0]) return -ENODEV; if (!sr_note_on || !sr_remove_unlocked || !sr_remove_samples) return -ENOENT; init_completion(&mr01_done); atomic_set(&mr01_stalled, 0); atomic_set(&mr01_armed, 1); t = kthread_run(mr01_noteon_once, NULL, "synthrace_ctl"); if (IS_ERR(t)) { atomic_set(&mr01_armed, 0); return PTR_ERR(t); } /* wait until the note_on thread is actually parked mid-window */ { unsigned long deadline = jiffies + msecs_to_jiffies(8000); while (!atomic_read(&mr01_stalled) && time_before(jiffies, deadline)) msleep(1); } if (!atomic_read(&mr01_stalled)) { atomic_set(&mr01_armed, 0); wait_for_completion_timeout(&mr01_done, msecs_to_jiffies(2000)); pr_err("mr01ctl: note_on never parked; aborted\n"); return -EIO; } /* free zone/sample/block underneath the parked note_on */ pr_info("mr01ctl: running snd_soundfont_remove_unlocked()\n"); rc = sr_remove_unlocked(emu->sflist); pr_info("mr01ctl: remove_unlocked returned %d\n", rc); msleep(50); atomic_set(&mr01_armed, 0); /* release: cascaded UAF now */ wait_for_completion_timeout(&mr01_done, msecs_to_jiffies(10000)); pr_info("mr01ctl: done\n"); return 0; } static const struct kernel_param_ops mr01ctl_ops = { .set = mr01ctl_set }; module_param_cb(mr01ctl, &mr01ctl_ops, NULL, 0200); static int probe_set(const char *val, const struct kernel_param *kp) { struct snd_emux_port *port; struct snd_midi_channel *chan; if (!emu || !emu->portptrs[0]) return -ENODEV; port = emu->portptrs[0]; chan = &port->chset.channels[0]; atomic_set(&sr_start_calls, 0); pr_info("probe: pre note_on presets_locked=%d fonts=%d zone_ctr=%d\n", emu->sflist->presets_locked, emu->sflist->fonts_size, emu->sflist->zone_counter); sr_note_on(port, 60, 100, chan); pr_info("probe: note_on done, start_voice calls=%d\n", atomic_read(&sr_start_calls)); return 0; } static const struct kernel_param_ops probe_ops = { .set = probe_set }; module_param_cb(probe, &probe_ops, NULL, 0200); /* ---- MR-03/MR-04 trigger: sub-driver remove body ---- */ static int free_set(const char *val, const struct kernel_param *kp) { if (!emu) return -ENODEV; pr_info("free: calling snd_emux_free() (sub-driver remove body)\n"); snd_emux_free(emu); emu = NULL; pr_info("free: done\n"); return 0; } static const struct kernel_param_ops free_ops = { .set = free_set }; module_param_cb(free, &free_ops, NULL, 0200); /* ---- module plumbing ---- */ static int __init synthrace_init(void) { int ret; ret = sr_resolve_syms(); if (ret < 0) return ret; sr_card_new(); if (!card) return -ENODEV; return sr_emux_setup(); } static void __exit synthrace_exit(void) { if (noteon_thr) kthread_stop(noteon_thr); if (remove_thr) kthread_stop(remove_thr); 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(synthrace_init); module_exit(synthrace_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("emux SYNTH-MR-01/03/04 race verification harness");