// SPDX-License-Identifier: GPL-2.0 /* * synthmr05.c - controlled-window verification for SYNTH-MR-05 * (docs/manual-review-sound-synth-2026-11-26.md ยง2.5): * * snd_emux_register() calls snd_emux_init_hwdep() FIRST, whose * snd_card_register() publishes /dev/snd/hwC*D* to userspace, and only then * snd_emux_init_seq() fills emu->portptrs[] (kzalloc'd NULL until then). * A user opening the hwdep node inside that window and issuing * SNDRV_EMUX_IOCTL_MISC_MODE hits snd_emux_hwdep_misc_mode() -> * emu->portptrs[i]->ctrls[..] with portptrs[i] == NULL -> NULL-pointer * WRITE -> oops. * * Controlled window: kprobe on snd_emux_init_seq() stalls the (single-caller) * probe thread at its entry - i.e., exactly after the hwdep node is exposed * and before portptrs[] is filled. Userspace polls for the node, opens it, * issues the ioctl -> deterministic NULL deref. After the stall expires the * registration completes. */ #define pr_fmt(fmt) "SYNTHMR05: " fmt #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 kprobe mr5_kp; static struct completion reg_done; static unsigned int stall_ms = 4000; module_param(stall_ms, uint, 0400); static int mr5_start(struct snd_emux_voice *vp) { return 0; } static void mr5_void1(struct snd_emux_voice *vp) {} static void mr5_void2(struct snd_emux_voice *vp, int u) {} static struct snd_emux_voice *mr5_get_voice(struct snd_emux *e, struct snd_emux_port *port) { return NULL; } static int mr5_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 mr5_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 mr5_ops = { .owner = THIS_MODULE, .get_voice = mr5_get_voice, .prepare = mr5_start, .trigger = mr5_void1, .release = mr5_void1, .update = mr5_void2, .terminate = mr5_void1, .sample_new = mr5_snew, .sample_free = mr5_sfree, }; static int mr5_reg_pre(struct kprobe *p, struct pt_regs *regs) { pr_info("probe thread stalled at snd_emux_init_seq() entry: " "hwC0D0 exposed, portptrs[] still NULL (window open)\n"); mdelay(stall_ms); pr_info("probe thread resuming snd_emux_init_seq()\n"); return 0; } static int mr5_reg_thread(void *arg) { int err; err = snd_emux_new(&emu); if (err < 0) { complete(®_done); return 0; } emu->ops = mr5_ops; emu->hw = card; emu->max_voices = 8; emu->num_ports = 1; /* > 0: drives the buggy loop bound */ emu->midi_ports = 0; emu->hwdep_idx = 0; /* /dev/snd/hwC0D0 */ emu->memhdr = snd_util_memhdr_new(0x100000); /* registers hwdep (exposes hwC0D0 via snd_card_register) and then * enters snd_emux_init_seq() -> stalled by the kprobe */ err = snd_emux_register(emu, card, 0, "synthmr05"); pr_info("snd_emux_register returned %d\n", err); if (err < 0) { snd_emux_free(emu); emu = NULL; } complete(®_done); return 0; } static int arm_set(const char *val, const struct kernel_param *kparam) { struct task_struct *t; int ret; if (!card) return -ENODEV; memset(&mr5_kp, 0, sizeof(mr5_kp)); mr5_kp.symbol_name = "snd_emux_init_seq"; mr5_kp.pre_handler = mr5_reg_pre; ret = register_kprobe(&mr5_kp); if (ret) { pr_err("register_kprobe(snd_emux_init_seq) failed: %d\n", ret); return ret; } init_completion(®_done); t = kthread_run(mr5_reg_thread, NULL, "synthmr05_reg"); if (IS_ERR(t)) { unregister_kprobe(&mr5_kp); return PTR_ERR(t); } pr_info("armed: probe thread started, window open for ~%ums\n", stall_ms); return 0; } static const struct kernel_param_ops arm_ops = { .set = arm_set }; module_param_cb(arm, &arm_ops, NULL, 0200); static int __init synthmr05_init(void) { int err; init_completion(®_done); parent = root_device_register("synthmr05"); if (IS_ERR(parent)) return PTR_ERR(parent); err = snd_card_new(parent, 0, "synthmr05", THIS_MODULE, 0, &card); if (err < 0) { root_device_unregister(parent); parent = NULL; return err; } return 0; } static void __exit synthmr05_exit(void) { wait_for_completion_timeout(®_done, msecs_to_jiffies(30000)); unregister_kprobe(&mr5_kp); 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(synthmr05_init); module_exit(synthmr05_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SYNTH-MR-05 probe-window race verification harness");