>Synopsis:      setting net.inet.ip.mtudisc to 0 frees PMTU rttimers without 
>timeout_del(); softclock() later walks/calls the freed timeout (GPF in 
>CIRCQ_REMOVE, call through NULL to_func)
>Category:      kernel
>Environment:
        System      : OpenBSD 8.0
        Details     : OpenBSD 8.0-current (GENERIC.MP), src as of 2026-11-03
                      (git mirror d5b06e73), sys/net/route.c 1.451
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM, 2 vCPU, 4 GB), ddb.panic=0

>Description:
        rt_timer_queue_flush() in sys/net/route.c is called by ip_sysctl()
        (sys/netinet/ip_input.c, IPCTL_MTUDISC) when net.inet.ip.mtudisc is
        changed to 0.  It moves every rttimer of ip_mtudisc_timeout_q to a
        local list, runs the callout and pool_put()s it -- but it never calls
        timeout_del(&r->rtt_timeout).  Each rttimer's timeout was armed by
        rt_timer_add() (timeout_set_proc + timeout_add_sec(rtq_timeout)), so
        after the flush a TIMEOUT_PROC timeout is still linked on the timeout
        wheel inside freed rttmr pool memory:

          - with pool poisoning the item's to_list.next is a poison pattern;
            when softclock() reaches the stale entry it faults in
            CIRCQ_REMOVE() ("fatal protection fault ... softclock+0xcf",
            the instruction is "to->to_list.next->prev = ...");
          - if the item has been reused and zeroed, to_kclock == 0 and
            to_func == NULL, so softclock() goes through
            softclock_process_kclock_timeout() and calls address 0
            ("attempt to execute user address 0x0 ... softclock+0xb8");
          - removing a neighbouring timeout from the same wheel bucket can
            write into the freed item (free list corruption).

        The other rttimer users get this right: rt_timer_unlink() deletes the
        timeout and, if timeout_del() fails because the timeout already
        fired, leaves the entry on the queue for rt_timer_timer() to clean up.
        The flush ignores that case too: an entry whose timeout has fired and
        whose rt_timer_timer() is waiting for NET_LOCK (which the flush
        holds) is freed under it, and an entry already unlinked by
        rt_timer_unlink() (rtt_rt == NULL, rtt_link invalidated) gets a second
        LIST_REMOVE() and a callout with a NULL route.

        PMTU rttimers are created by icmp_mtudisc_clone(): on ICMP
        need-fragmentation for a TCP connection (tcp_ctlinput ->
        icmp_mtudisc), and by the TCP retransmit timer when it disables path
        MTU discovery after TCP_MAXRXTSHIFT/6 retransmits.  So any box doing
        TCP over a path that drops packets or has a smaller MTU, where an
        administrator sets net.inet.ip.mtudisc=0, can hit it; the panic comes
        net.inet.ip.mtudisctimeout seconds later (600 by default), at
        softclock time, with an unrelated thread interrupted.

        Found by a network sysctl fuzzer (random writes to all writable net.*
        sysctls plus interface churn and forwarded TCP/UDP traffic), which
        toggles net.inet.ip.forwarding (TCP retransmits -> PMTU host routes),
        net.inet.ip.mtudisctimeout and net.inet.ip.mtudisc.  Two of the
        panics it produced:

        fatal protection fault in supervisor mode
        trap type 4 code 0 rip ffffffff8192dc8f cs 8 rflags 10246 cr2 
ae6788cd1c8 cpl d rsp ffff80002a65a910
        panic: trap type 4, code=0, pc=ffffffff8192dc8f
        panic() at panic+0x170
        kerntrap() at kerntrap+0x15d
        alltraps_kern_meltdown() at alltraps_kern_meltdown+0x7b
        softclock(0) at softclock+0xcf
        softintr_dispatch(0) at softintr_dispatch+0x105
        dosoftint(0) at dosoftint+0x3f
        Xsoftclock() at Xsoftclock+0x27
        pf_setup_pdesc(...) at pf_setup_pdesc+0x7b7
        pf_test(18,2,...) at pf_test+0x12e
        ip6_output(...) at ip6_output+0xaf1
        udp6_output(...) at udp6_output+0x2ba
        sosend(...) at sosend+0x432

        attempt to execute user address 0x0 in supervisor mode
        fatal page fault in supervisor mode
        trap type 6 code 10 rip 0 cs 8 rflags 10246 cr2 0 cpl 1 rsp 
ffff80002a65d7e8
        panic: trap type 6, code=10, pc=0
        panic() at panic+0x170
        kerntrap() at kerntrap+0x15d
        alltraps_kern_meltdown() at alltraps_kern_meltdown+0x7b
        
0(fffff98015f80d68,0,27157a14b90ed176,ffff80002a57b070,0,ffff80002a4060b8) at 0
        softclock(0) at softclock+0xb8
        softintr_dispatch(0) at softintr_dispatch+0x105
        dosoftint(0) at dosoftint+0x3f
        Xsoftclock() at Xsoftclock+0x27
        _kernel_lock() at _kernel_lock+0xb2
        sysctl_vslock(70312e6ee990,10) at sysctl_vslock+0x4a
        sys_sysctl(...) at sys_sysctl+0x187

        In the first case the panicking CPU held NET_LOCK, so reboot hung in
        "syncing disks".  In the fuzzer op logs, both crashes follow a
        sequence of net.inet.ip.mtudisc=0 writes after mtudisctimeout had
        been set to a small value (31 s and 15 s).

        The owner was identified with a debug-only check (not part of the
        diff below) in free(9)/pool_put(9) that panics when the memory being
        freed contains a timeout that is still on a timeout queue.  It fires
        on the first flush:

        TODBG: rttmr(0xfffff9815a461e78, 136) frees a LINKED timeout 
0xfffff9815a461e98 (+0x20) flags 0x7 kclock -1 arg 0xfffff9815a461e78 next 
0xffffffff82b0e540 prev 0xffffffff82c36070
        TODBG:   to_func 0xffffffff81acf510 <rt_timer_timer>
        panic: TODBG: free of pending timeout 0xfffff9815a461e98 (rttmr)
        panic(ffffffff82751955) at panic+0x170
        todbg_free(fffff9815a461e78,88,ffffffff8266e7c4) at todbg_free+0x207
        pool_put(ffffffff82c09748,fffff9815a461e78) at pool_put+0x58
        rt_timer_queue_flush(ffffffff82be8178) at rt_timer_queue_flush+0x167
        ip_sysctl(...) at ip_sysctl+0x3b5
        net_sysctl(...) at net_sysctl+0x27b
        sys_sysctl(...) at sys_sysctl+0x1c5
        syscall(...) at syscall+0x60d

>How-To-Repeat:
        
        manually verified in 8.0 current (nov2 )   by me (tom ) 
        As root on a scratch machine (creates pair210-213 in rdomains
        110-112, sets net.inet.ip.forwarding=1 and changes mtudisc and
        mtudisctimeout without restoring them):

        #!/bin/ksh
        for p in 210/110/10.110.1.2 211/111/10.110.1.1 \
            212/111/10.110.2.1 213/112/10.110.2.2; do
                set -- $(echo $p | tr / ' ')
                ifconfig pair$1 create; ifconfig pair$1 rdomain $2
                ifconfig pair$1 inet $3/24 up
        done
        ifconfig pair210 patch pair211; ifconfig pair212 patch pair213
        ifconfig pair212 mtu 1280       # router egress only
        route -qn -T110 add default 10.110.1.1
        route -qn -T112 add default 10.110.2.1
        sysctl net.inet.ip.forwarding=1
        route -T112 exec tcpbench -s -p 12410 >/dev/null 2>&1 &
        sleep 1
        while :; do
                sysctl net.inet.ip.mtudisctimeout=10 net.inet.ip.mtudisc=1
                # DF TCP through the 1280 hop -> ICMP need-frag -> PMTU
                # host route 10.110.2.2 (flags D) with an rttimer
                route -T110 exec timeout 5 tcpbench -t 2 -p 12410 10.110.2.2
                route -nT110 show -inet | grep '10.110.2.2 .*D'
                sysctl net.inet.ip.mtudisc=0    # frees the pending rttimer
                sleep 12                        # stale timeout expires
        done

        Every iteration prints the D host route, i.e. an rttimer is pending
        when mtudisc is set to 0.  Without the diff below the machine panics
        in the first minute, e.g. on GENERIC.MP built from the tree above:

        fatal protection fault in supervisor mode
        trap type 4 code 0 rip ffffffff81473304 cs 8 rflags 10246 cr2 
8e6a8a3010 cpl 1 rsp ffff800045426f70
        panic: trap type 4, code=0, pc=ffffffff81473304
        panic(ffffffff826f767b) at panic+0x170
        kerntrap(ffff800045426ec0) at kerntrap+0x15d
        alltraps_kern_meltdown() at alltraps_kern_meltdown+0x7b
        __x86_indirect_thunk_r11() at __x86_indirect_thunk_r11+0x14
        softclock_thread(ffff800045416f90) at softclock_thread+0xb1

        (the TIMEOUT_PROC rttimer reached the softclock thread with a
        poisoned to_func), or:

        fatal protection fault in supervisor mode
        trap type 4 code 0 rip ffffffff81ff50ff cs 8 rflags 10287 cr2 
fbd57540028 cpl d rsp ffff800045421bf0
        panic: trap type 4, code=0, pc=ffffffff81ff50ff
        panic(ffffffff826f767b) at panic+0x170
        kerntrap(ffff800045421b40) at kerntrap+0x15d
        alltraps_kern_meltdown() at alltraps_kern_meltdown+0x7b
        softclock(0) at softclock+0xcf
        softintr_dispatch(0) at softintr_dispatch+0x105
        dosoftint(0) at dosoftint+0x3f
        Xsoftclock() at Xsoftclock+0x27
        acpicpu_idle() at acpicpu_idle+0x2b3
        sched_idle(ffffffff82ad5ff0) at sched_idle+0x244

>Fix:
        Delete each timeout before taking the rttimer off the queue, and
        leave entries whose timeout already fired to rt_timer_timer(), the
        same way rt_timer_unlink() does.  If timeout_del() succeeds the
        callback can no longer run and the flush owns the entry.  If it fails,
        rt_timer_timer() is already running: it is waiting for NET_LOCK,
        which the flush holds, and will unlink, call out and free the entry
        itself.  This also covers entries already unlinked by
        rt_timer_unlink() (rtt_rt == NULL), whose timeout has necessarily
        fired.

Index: sys/net/route.c
===================================================================
RCS file: /cvs/src/sys/net/route.c,v
diff -u -p -r1.451 route.c
--- sys/net/route.c     22 Apr 2026 15:17:43 -0000      1.451
+++ sys/net/route.c     5 Nov 2026 07:30:00 -0000
@@ -1565,14 +1565,21 @@ rt_timer_queue_change(struct rttimer_que
 void
 rt_timer_queue_flush(struct rttimer_queue *rtq)
 {
-       struct rttimer          *r;
+       struct rttimer          *r, *nr;
        TAILQ_HEAD(, rttimer)    rttlist;
 
        NET_ASSERT_LOCKED();
 
        TAILQ_INIT(&rttlist);
        mtx_enter(&rttimer_mtx);
-       while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
+       TAILQ_FOREACH_SAFE(r, &rtq->rtq_head, rtt_next, nr) {
+               /*
+                * The timer must not stay on the timeout wheel after
+                * the rttimer is freed.  If the timeout already fired,
+                * rt_timer_timer() owns the entry and does the cleanup.
+                */
+               if (timeout_del(&r->rtt_timeout) == 0)
+                       continue;
                LIST_REMOVE(r, rtt_link);
                TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
                TAILQ_INSERT_TAIL(&rttlist, r, rtt_next);

        Tested on amd64 (2 vCPU, 4 GB) with GENERIC.MP built from the tree
        named above, without and with exactly this diff (only route.c and
        vers.c recompiled), running the How-To-Repeat loop (mtudisctimeout 10)
        for 5 x 60 s and 1 x 300 s per kernel:

          without the diff: 6 runs, 6 panics (5 in softclock_thread on a
                            poisoned to_func, 1 GPF at softclock+0xcf), each
                            in the first minute
          with the diff:    6 runs, 0 panics, 40 flushes of a pending PMTU
                            rttimer (4 per 60 s run, 20 in the 300 s run),
                            no panic/fault/freelist/witness lines in dmesg

Reply via email to