// SPDX-License-Identifier: GPL-2.0+ /* * rtnlrace.c - dynamic verification for the audit-report Case 1: * * rtnl_link_unregister() (net/core/rtnetlink.c:701-729) is missing the * synchronize_rcu() that upstream "rtnetlink: wait for RCU readers in * rtnl_link_unregister()" (Fixes: 6b57ff21a310) added. * * Reader window under test (rtnetlink.c:595-606, rtnl_link_ops_get inlined): * rcu_read_lock(); // :595 * list_for_each_entry_rcu(...) // :597 found ops via RCU * strcmp(ops->kind, kind) // :598 matched * *srcu_index = srcu_read_lock(&ops->srcu); // :599 <-- handoff * rcu_read_unlock(); // :606 * * Controlled mode (stall_us > 0): * - registers a kzalloc()'d rtnl_link_ops with kind "rtnlrace" * (rtnl_link_register is EXPORT_SYMBOL_GPL), so a userspace RTM_GETLINK * dump with IFLA_INFO_KIND=rtnlrace walks exactly the audited path. * - a kprobe on __srcu_read_lock with return-address filter parks the dump * reader exactly at the :599 handoff (inside the RCU read-side critical * section, before any SRCU state is touched): the audited window, enlarged * for deterministic observation (same technique as kmod/r4_stall.c). * - the unregister kthread then performs what module_exit does: * rtnl_link_unregister(ops) -> list_del_rcu + synchronize_srcu (returns * immediately: the parked reader has NOT entered the SRCU region) + * cleanup_srcu_struct (frees srcu_usage and the percpu sda, sets * ssp->srcu_sup/sda to NULL, leaves srcu_ctrp dangling), then kfree(ops) * (models module_memfree of the unloaded module's static storage; KASAN * slab poisoning makes the post-free read observable). * - the parked reader resumes: READ_ONCE(ssp->srcu_ctrp) on freed memory * => KASAN use-after-free Read in __srcu_read_lock. * * Natural mode (stall_us == 0): no kprobe, no init registration. A kthread * hammers kzalloc/rtnl_link_register/rtnl_link_unregister/kfree cycles of a * kind-"rtnlrace" ops while userspace hammers dumps; the natural * strcmp->srcu_read_lock window is the only interleaving (rmmod shape). * * Offsets for build-kasan-7.3/vmlinux (rtnl_link_ops_get fully inlined): * rtnl_dump_ifinfo @ 0xffffffff826e8030 (size 0x925) * call __srcu_read_lock @ +0x36f -> return address +0x374 (kprobe base skips ENDBR, live filter +0x370) */ #define pr_fmt(fmt) "RTNLRACE: " fmt #include #include #include #include #include #include #include #include #include #include static int stall_ms = 1000; /* 0 = natural mode */ module_param(stall_ms, int, 0600); MODULE_PARM_DESC(stall_ms, "park the dump reader this long at the handoff (0=natural); implemented as udelay(10000) x N to dodge x86 __bad_udelay truncation for >20ms values"); static int unreg_delay_ms = 120; module_param(unreg_delay_ms, int, 0600); static int natural_secs = 90; module_param(natural_secs, int, 0600); static ulong ret_off = 0x370; module_param(ret_off, ulong, 0600); MODULE_PARM_DESC(ret_off, "return-address offset of the __srcu_read_lock call inside rtnl_dump_ifinfo"); static struct rtnl_link_ops *rops; /* controlled mode only */ static atomic_t hits = ATOMIC_INIT(0); static atomic_t parked = ATOMIC_INIT(0); static atomic_t freed = ATOMIC_INIT(0); static atomic_t rounds = ATOMIC_INIT(0); static unsigned long base_addr; static struct kprobe kp; static int probe_live; static struct task_struct *uthr; static int stall_pre(struct kprobe *p, struct pt_regs *regs) { unsigned long ra = *(unsigned long *)regs->sp; if (ra == base_addr + ret_off) { int h = atomic_inc_return(&hits); u64 t0 = ktime_to_ms(ktime_get()); int i; atomic_set(&parked, 1); if (h <= 3) pr_warn("stall #%d: parking dump reader %d ms at handoff (ra=%lx == rtnl_dump_ifinfo+0x%lx) t=%llu\n", h, stall_ms, ra, ret_off, t0); for (i = 0; i < stall_ms / 10; i++) udelay(10000); if (h <= 3) pr_warn("stall #%d: park END t=%llu (held %llu ms)\n", h, ktime_to_ms(ktime_get()), ktime_to_ms(ktime_get()) - t0); } else if (ra > base_addr && ra < base_addr + 0x2000) { static atomic_t dbg; int d = atomic_inc_return(&dbg); if (d <= 8) pr_warn("near-caller #%d ra-off=0x%lx\n", d, ra - base_addr); } return 0; } static unsigned long sym_addr(const char *name) { struct kprobe probe = { .symbol_name = name, }; unsigned long addr = 0; if (register_kprobe(&probe) == 0) { addr = (unsigned long)probe.addr; unregister_kprobe(&probe); } return addr; } static int unreg_thr(void *unused) { if (stall_ms > 0) { /* controlled: wait until the kprobe parked the dump reader */ while (!atomic_read(&parked) && !kthread_should_stop()) msleep(5); if (kthread_should_stop()) return 0; msleep(unreg_delay_ms); rtnl_link_unregister(rops); /* protocol evidence: what did cleanup_srcu_struct leave behind? * (rops itself is still allocated here, so these reads are safe) */ pr_warn("post-unregister t=%llu: ops=%px srcu.srcu_sup=%px srcu.sda=%px srcu.srcu_ctrp=%px\n", ktime_to_ms(ktime_get()), rops, rops->srcu.srcu_sup, rops->srcu.sda, rops->srcu.srcu_ctrp); kfree(rops); atomic_set(&freed, 1); pr_warn("ops kfree()d (models module_memfree of the unloaded module); parked reader will resume on freed memory\n"); } else { /* natural: hammer kzalloc/register/unregister/kfree (rmmod shape) */ u64 t0 = ktime_to_ms(ktime_get()); while (!kthread_should_stop() && ktime_to_ms(ktime_get()) - t0 < (u64)natural_secs * 1000) { struct rtnl_link_ops *o = kzalloc(sizeof(*o), GFP_KERNEL); if (!o) break; o->kind = "rtnlrace"; if (rtnl_link_register(o) == 0) { rtnl_link_unregister(o); kfree(o); } else { kfree(o); } atomic_inc(&rounds); if ((atomic_read(&rounds) & 0x3ff) == 0) pr_warn("natural round %d\n", atomic_read(&rounds)); } pr_warn("natural done: %d register/unregister/kfree rounds\n", atomic_read(&rounds)); } return 0; } static int __init rtnlrace_init(void) { int ret; if (stall_ms > 0) { rops = kzalloc(sizeof(*rops), GFP_KERNEL); if (!rops) return -ENOMEM; rops->kind = "rtnlrace"; ret = rtnl_link_register(rops); if (ret) { pr_err("rtnl_link_register failed: %d\n", ret); kfree(rops); rops = NULL; return ret; } pr_warn("registered rtnl_link_ops kind=rtnlrace (%px); start: /nlr rtnlrace\n", rops); base_addr = sym_addr("rtnl_dump_ifinfo"); if (!base_addr) { pr_err("cannot resolve rtnl_dump_ifinfo\n"); return -EINVAL; } kp.symbol_name = "__srcu_read_lock"; kp.pre_handler = stall_pre; ret = register_kprobe(&kp); if (ret) { pr_err("cannot register kprobe: %d\n", ret); return ret; } probe_live = 1; pr_warn("kprobe on __srcu_read_lock, park ra=rtnl_dump_ifinfo+0x%lx (%px)\n", ret_off, (void *)(base_addr + ret_off)); } uthr = kthread_run(unreg_thr, NULL, "rtnlrace_unreg"); if (IS_ERR(uthr)) return PTR_ERR(uthr); pr_warn("armed: stall_ms=%d unreg_delay_ms=%d mode=%s\n", stall_ms, unreg_delay_ms, stall_ms > 0 ? "controlled" : "natural"); return 0; } static void __exit rtnlrace_exit(void) { if (uthr) kthread_stop(uthr); if (probe_live) unregister_kprobe(&kp); if (stall_ms > 0 && atomic_read(&freed) == 0 && rops) { rtnl_link_unregister(rops); kfree(rops); } pr_warn("module exit (hits=%d rounds=%d)\n", atomic_read(&hits), atomic_read(&rounds)); } module_init(rtnlrace_init); module_exit(rtnlrace_exit); MODULE_LICENSE("GPL");