>Synopsis:      SIOCSETKALIVE arms both keepalive timeouts of a gre(4)
                interface even when it is not running, and
                gre_keepalive_send() re-arms itself.  gre_clone_destroy()
                only stops them through gre_down(), which it calls only
                for a running interface, so destroying a gre that has
                keepalives but is down frees the softc with sc_ka_send
                and sc_ka_hold still on the timeout wheel.  Once the
                memory is reused the timeout lists are corrupted and the
                machine panics in softclock() or hangs.
>Category:      kernel
>Severity:      serious
>Priority:      medium
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (source as of 2026-11-03)
>Environment:
        System      : OpenBSD 8.0-current
        Details     : OpenBSD 8.0-current (GENERIC.MP), WITNESS + DIAGNOSTIC
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
        Source      : sys/net/if_gre.c rev 1.192, openbsd/src as of
                      2026-11-03, git d5b06e73

>Description:

        The keepalive timeouts of a gre(4) interface are set up in
        gre_clone_create() (if_gre.c:667-668):

            timeout_set(&sc->sc_ka_send, gre_keepalive_send, sc);
            timeout_set_proc(&sc->sc_ka_hold, gre_keepalive_hold, sc);

        SIOCSETKALIVE ("ifconfig greN keepalive period count") arms both
        of them whether or not the interface is running
        (if_gre.c:2273-2275):

            timeout_add(&sc->sc_ka_send, 1);
            timeout_add_sec(&sc->sc_ka_hold,
                sc->sc_ka_timeo * sc->sc_ka_count);

        gre_keepalive_send() re-arms itself first thing, as long as
        sc_ka_timeo is set, and only then checks IFF_RUNNING
        (if_gre.c:3054-3061):

            if (sc->sc_ka_timeo)
                    timeout_add_sec(&sc->sc_ka_send, sc->sc_ka_timeo);

            if (!ISSET(sc->sc_if.if_flags, IFF_RUNNING) || ...)
                    return;

        so on a gre that is down sc_ka_send fires every period, forever.
        The only place that deletes the keepalive timeouts is gre_down()
        (if_gre.c:3002-3003), and gre_clone_destroy() calls it only for
        a running interface (if_gre.c:693-702):

            NET_LOCK();
            if (ISSET(ifp->if_flags, IFF_RUNNING))
                    gre_down(sc);
            TAILQ_REMOVE(&gre_list, sc, sc_entry);
            NET_UNLOCK();

            if_detach(ifp);

            free(sc, M_DEVBUF, sizeof(*sc));

        A gre with keepalives configured that was never brought up (or
        was taken down) is therefore freed with sc_ka_send, and until it
        expires sc_ka_hold, still queued.  By code reading the same can
        happen if gre_keepalive_send() is running and re-arms itself
        while gre_down() deletes sc_ka_send, and the interface is
        destroyed later (not tried separately).

        The freed timeouts are not noticed at first: free() only poisons
        the first 64 bytes of an item (POISON_SIZE in subr_poison.c), the
        timeouts are further into struct gre_softc, and the stale handler
        sees IFF_RUNNING clear and just re-arms.  When the memory is
        handed out again, for example by malloc(M_ZERO) for the next gre,
        the circq links of the still queued timeout are overwritten and
        the timeout wheel is corrupted.  In one run, on the next
        softclock that walked the bucket:

            uvm_fault(0xffffffff82bc3528, 0x8, 0, 2) -> e
            fatal page fault in supervisor mode
            trap type 6 code 2 rip ffffffff8129ba4f cs 8 rflags 10246 cr2 8
            panic: trap type 6, code=2, pc=ffffffff8129ba4f
            panic(ffffffff827027a5) at panic+0x170
            kerntrap(ffff800044b65bc0) 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(ffffffff82b10ff0) at sched_idle+0x244

        Depending on who gets the memory next, other runs ended in a
        panic far away from gre, for example (arguments left out)

            panic: kernel diagnostic assertion "kn->kn_kq == kq" failed:
                file "/usr/src-eoipA/sys/kern/kern_event.c", line 2154
            knote_remove() at knote_remove+0x2be
            kqueue_purge() at kqueue_purge+0x57
            kqpoll_exit() at kqpoll_exit+0x3b
            exit1() at exit1+0x256

        or the machine stopped without any message (no panic text in the
        message buffer after a reset), probably when the new owner
        queued its own timeout in the same place and the old and the new
        list ended up sharing an entry.

        A debug check in free() that panics when the freed range holds a
        queued timeout (not part of the fix) names the owner on the
        first destroy:

            TODBG: free(0xffff800000442800, 0x6e0) releases a queued
                timeout 0xffff800000442e68 (+0x668) on wheel flags 0x7
                time 2147398972 arg 0xffff800000442800
                fn 0xffffffff82408720 <gre_keepalive_hold>
            panic: TODBG: free of memory with queued timeout
                0xffff800000442e68
            ...
            free(ffff800000442800,2,6e0) at free+0x80
            gre_clone_destroy(ffff800000442850) at gre_clone_destroy+0xb4
            if_clone_destroy(ffff800044cefb40) at if_clone_destroy+0x13f
            ifioctl(ffff800000e2c050,80206979,ffff800044cefb40,
                ffff800044c1cfa0) at ifioctl+0x1f5
            sys_ioctl(ffff800044c1cfa0,ffff800044cefd00,
                ffff800044cefc80) at sys_ioctl+0x30e

        (lines wrapped; the check reports the first queued timeout it
        finds, here sc_ka_hold; "ifconfig gre0 create keepalive 1 2"
        followed by "ifconfig gre0 destroy", gre0 never up.)

>How-To-Repeat:

        As root:

        1. for i in $(jot 50); do ifconfig gre0 create keepalive 1 2; \
               ifconfig gre0 destroy; ifconfig gre0 create; sleep 2; \
               ifconfig gre0 destroy; done

        gre0 is never brought up.  The machine panics (in softclock() or
        wherever the reused memory is noticed) or stops responding before
        the loop ends, after 9 to 20 iterations in the runs below.

>Fix:

        Stop both keepalive timeouts in gre_clone_destroy() after
        if_detach(), whether or not the interface was running.  After
        if_detach() no ioctl can arm them again, and the softc was taken
        off gre_list under NET_LOCK, so gre_keepalive_recv() can not arm
        sc_ka_hold either.  gre_keepalive_send() re-arms itself while
        sc_ka_timeo is set, so clear it first, wait for a handler that
        may already be running (it may have read the old value and
        re-armed), then delete and wait again.  This is done without
        NET_LOCK; gre_keepalive_hold() takes NET_LOCK itself.

Index: sys/net/if_gre.c
===================================================================
RCS file: /cvs/src/sys/net/if_gre.c,v
diff -u -p -r1.192 if_gre.c
--- sys/net/if_gre.c    21 Nov 2025 04:44:26 -0000      1.192
+++ sys/net/if_gre.c    6 Nov 2026 07:45:00 -0000
@@ -699,6 +699,16 @@ gre_clone_destroy(struct ifnet *ifp)
 
        if_detach(ifp);
 
+       /*
+        * SIOCSETKALIVE arms the keepalive timeouts even if the
+        * interface is not running, and gre_keepalive_send() re-arms
+        * itself while sc_ka_timeo is set.  Stop both before the free.
+        */
+       sc->sc_ka_timeo = 0;
+       timeout_barrier(&sc->sc_ka_send);
+       timeout_del_barrier(&sc->sc_ka_send);
+       timeout_del_barrier(&sc->sc_ka_hold);
+
        free(sc, M_DEVBUF, sizeof(*sc));
 
        return (0);

>Verification:

        The diff above (md5 5caa226d6c7f7f0a690426569241119d, sha256
        243b5932c61189e92eb11408d80c0d9aa4a16b2e305765890272baf73f63bc36)
        was applied with patch -p0 -F0 to a fresh export of the
        2026-11-03 tree (git d5b06e73, sys/net/if_gre.c rev 1.192); the
        hunk applied at line 699 with no offset or fuzz.  Both the
        unpatched and the patched tree were checked file by file against
        the git tree (the patched one differs only in if_gre.c).
        Kernels: GENERIC.MP + "option WITNESS" (DIAGNOSTIC comes from
        GENERIC), amd64, built without warnings; 2-vcpu guest.  Each run
        below was on a freshly booted kernel.

        How-To-Repeat, step 1 exactly as listed:
          unpatched   3/3 runs died before the loop ended (after 9, 17
                      and 10 iterations): twice the kern_event.c panic
                      shown above, once no message at all; earlier runs
                      of the same loop also gave the softclock() page
                      fault shown above
          patched     3/3 runs completed all 50 iterations, no gre left,
                      no kernel messages

        With the debug check in free() described above (not part of the
        fix) added to both kernels:
          unpatched   panics on the first "ifconfig gre0 destroy" after
                      "ifconfig gre0 create keepalive 1 2" (output above)
          patched     3/3 runs without a report: a single create with
                      keepalive and destroy, the 50 iterations of step 1,
                      and 20 rounds of a running gre with a tunnel and
                      "keepalive 1 2" taken down, up and down again and
                      destroyed while down

        Normal use on the patched kernel (fresh boot each, 3/3 runs, 18
        checks each, all passed): a gre pair over a pair(4) underlay
        between two rdomains with "keepalive 1 3" comes up ("status:
        active") and passes pings; taking one side down makes the other
        side go to "keepalive down" and bringing it up makes both active
        again; "-keepalive" turns keepalives off and pings still work;
        "keepalive 1 3" brings it back to active; destroying one side
        makes the other go to "keepalive down"; both are destroyed;
        "keepalive 2 2" is accepted on a gre that is not up, and a gre
        re-created after destroying it has no keepalive; no process left
        waiting; no kernel messages.

Reply via email to