>Synopsis:      nvgre_down() clears IFF_RUNNING, releases NET_LOCK for
                ifq_barrier()/taskq_del_barrier(), and only after re-taking it
                undoes the configuration-dependent state: the parent's detach 
and
                link-state hooks, the multicast group, the tunnel trees.  The
                nvgre reconfiguration ioctls are refused only while IFF_RUNNING
                is set, so during that window "-tunnel", "tunnel" (other address
                family), "parent"/"-parent" and "vnetid" can change the state
                nvgre_down() is about to use.  Seen: panic "unhandled af 0",
                the multicast membership freed by the other family's function
                ("free: size too large"/"too small ... type in_multi"), and a
                page fault in if_detachhook_del() on the wrong parent.
>Category:      kernel
>Severity:      serious
>Priority:      medium
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (source as of 2026-11-08)
>Environment:
        System      : OpenBSD 8.0-current
        Details     : OpenBSD 8.0-current (GENERIC.MP as shipped: DIAGNOSTIC,
                      POOL_DEBUG)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
        Source      : sys/net/if_gre.c rev 1.193, openbsd/src master as of
                      2026-11-08, git afb08c4d5 (the same code is in
                      d5b06e73, r1.192)

>Description:

        nvgre_down() (sys/net/if_gre.c):

            CLR(ifp->if_flags, IFF_RUNNING);

            NET_UNLOCK();
            ifq_barrier(&ifp->if_snd);
            taskq_del_barrier(softnet, &sc->sc_send_task);
            NET_LOCK();

            mq_purge(&sc->sc_send_list);

            ifp0 = if_get(sc->sc_ifp0);
            if (ifp0 != NULL) {
                    if_detachhook_del(ifp0, &sc->sc_dtask);
                    if_linkstatehook_del(ifp0, &sc->sc_ltask);
            }
            if_put(ifp0);

            switch (tunnel->t_af) {
            case AF_INET:   in_delmulti(sc->sc_inm); break;
            case AF_INET6:  in6_delmulti(sc->sc_inm); break;
            default:        unhandled_af(tunnel->t_af);
            }

            RBT_REMOVE(nvgre_ucast_tree, &nvgre_ucast_tree, sc);
            RBT_REMOVE(nvgre_mcast_tree, &nvgre_mcast_tree, sc);

        The reconfiguration ioctls in nvgre_ioctl() (SIOCSLIFPHYADDR,
        SIOCDIFPHYADDR, SIOCSIFPARENT, SIOCDIFPARENT, SIOCSVNETID, ...) return
        EBUSY only while IFF_RUNNING is set.  It is already clear while
        nvgre_down() sleeps in the barriers, so they run and change
        sc_ifp0, tunnel->t_af, the tunnel addresses or the vnetid.  After
        NET_LOCK is re-taken nvgre_down() then
          - panics in unhandled_af() if "-tunnel" set t_af to AF_UNSPEC:

            panic: unhandled af 0
            unhandled_af(0) at unhandled_af+0x1a
            nvgre_down() at nvgre_down+0x1a9
            nvgre_ioctl(..., SIOCSIFFLAGS, ...) at nvgre_ioctl+0x3c2

          - frees sc_inm with the wrong family's function if "tunnel"
            switched between IPv4 and IPv6 (in either direction):

            panic: free: size too large 80 > 64 (0xffff800000e04b00) type 
in_multi
            free() at free+0x3f2
            in6_delmulti() at in6_delmulti+0x16c
            nvgre_down() at nvgre_down+0x16a

          - removes the hooks from whatever sc_ifp0 now names, after
            "-parent"/"parent", and faults (seen with an earlier, partial
            fix that only snapshotted t_af):

            uvm_fault(..., 0x7, 0, 2) -> e
            if_detachhook_del() at if_detachhook_del+0x35
            nvgre_down() at nvgre_down+0x155

        and RBT_REMOVE() runs with keys that no longer match how the softc
        was inserted.

>How-To-Repeat:

        As root, pasted into one shell (vether200, nvgre200 and rdomain 200
        unused).  The four loops run in the background with their output
        discarded, for three minutes, then are stopped:

            ifconfig vether200 create rdomain 200 192.0.2.1/24 up
            ifconfig nvgre200 create rdomain 200 tunneldomain 200 \
                tunnel 192.0.2.1 239.0.2.1 parent vether200 vnetid 200
            (while :; do
                ifconfig nvgre200 -tunnel
                ifconfig nvgre200 tunnel 192.0.2.1 239.0.2.1
            done) >/dev/null 2>&1 &
            (while :; do
                ifconfig nvgre200 tunnel 2001:db8:200::1 ff12::200
                ifconfig nvgre200 tunnel 192.0.2.1 239.0.2.1
                ifconfig nvgre200 vnetid $(( (RANDOM % 1000) + 256 ))
                ifconfig nvgre200 -parent
                ifconfig nvgre200 parent vether200
            done) >/dev/null 2>&1 &
            (while :; do
                ifconfig nvgre200 up
                ifconfig nvgre200 down
            done) >/dev/null 2>&1 &
            (while :; do
                ifconfig nvgre200 down
                ifconfig nvgre200 up
            done) >/dev/null 2>&1 &
            sleep 180
            kill $(jobs -p)

        The machine panics in nvgre_down(), usually within a minute.

>Fix:

        Please consider this diff for discussion.  It undoes everything
        that depends on the configuration before NET_LOCK is released,
        while the ioctls still cannot change it; only the queue barriers
        and the purge of the send list remain after the sleep.  A
        concurrent "up" during the sleep sets everything up again from
        scratch through nvgre_up(), which is consistent.

        Locking: this takes no new lock and does not make nvgre_down()
        hold NET_LOCK any longer.  The moved operations (two hook
        deletions, the multicast leave, two RBT_REMOVEs) already ran
        under NET_LOCK, after the barriers; they now run before the
        unlock instead.  The barriers still run without NET_LOCK.  A
        larger alternative that keeps the current order -- a per-softc
        lock serialising up/down plus EBUSY on reconfiguration while
        down is in progress, like vxlan(4) -- was also built and passed
        the same A/B (3/3 crash -> 3/3 survive, also with WITNESS, no
        lock order reversals); available on request.

Index: sys/net/if_gre.c
===================================================================
--- sys/net/if_gre.c
+++ sys/net/if_gre.c
@@ -3564,13 +3564,12 @@ nvgre_down(struct nvgre_softc *sc)
 
        CLR(ifp->if_flags, IFF_RUNNING);
 
-       NET_UNLOCK();
-       ifq_barrier(&ifp->if_snd);
-       taskq_del_barrier(softnet, &sc->sc_send_task);
-       NET_LOCK();
-
-       mq_purge(&sc->sc_send_list);
-
+       /*
+        * Undo everything that depends on the configuration (parent, tunnel
+        * address family, multicast group) before NET_LOCK is released below:
+        * the reconfiguration ioctls only check IFF_RUNNING, which is now
+        * clear, so they can change the configuration while we sleep.
+        */
        ifp0 = if_get(sc->sc_ifp0);
        if (ifp0 != NULL) {
                if_detachhook_del(ifp0, &sc->sc_dtask);
@@ -3595,6 +3594,13 @@ nvgre_down(struct nvgre_softc *sc)
        RBT_REMOVE(nvgre_ucast_tree, &nvgre_ucast_tree, sc);
        RBT_REMOVE(nvgre_mcast_tree, &nvgre_mcast_tree, sc);
 
+       NET_UNLOCK();
+       ifq_barrier(&ifp->if_snd);
+       taskq_del_barrier(softnet, &sc->sc_send_task);
+       NET_LOCK();
+
+       mq_purge(&sc->sc_send_list);
+
        return (0);
 }
 

>Verification:

        The diff above (md5 eed528bfd6698ab73142953af21a784f) applies with
        patch -p0 -F0 to sys/net/if_gre.c r1.193 (master afb08c4d5) without
        offset, and to r1.192 (d5b06e73) with offset 3, no fuzz.  Kernels:
        master afb08c4d5 GENERIC.MP as shipped, amd64, no warnings; patched
        = the same tree + the diff.  Every run on a freshly booted kernel
        (2-vcpu guest), the four loops above run for 180 s by a script
        (errors discarded):

          unpatched  6/6 crashed: "unhandled af 0" in nvgre_down() (1),
                     "free: size too large 80 > 64 ... in_multi" and
                     "free: size too small 64 <= 128 / 2 ... in_multi"
                     (in6_delmulti() on an IPv4 membership and in_delmulti()
                     on an IPv6 one, from nvgre_down()) (2), and a stop in ddb
                     with an unusable trace ("SPL NOT LOWERED ON TRAP EXIT",
                     stopped in proc_trampoline / savectx) (3)
          patched    3/3 survived the full 180 s, no kernel messages

        The How-To-Repeat exactly as printed (one shell, 180 s), on master
        afb08c4d5 GENERIC.MP (unpatched md5 acbccac0, patched md5 debcea40),
        fresh boot per run, VM 142:
          unpatched  3/3 panic: "free: size too large 80 > 64 ... in_multi",
                     "free: size too small 64 <= 128 / 2 ... in_multi",
                     "unhandled af 0"
          patched    3/3 survived
        The same with the loops' output going to the console instead of
        /dev/null: unpatched 1/1 panic (in_multi free), so the output does
        not hide the race.  (The block above is laid out one command per line 
for
        mail; the tested script had the same commands in the same loops.
        An earlier four-shell version of the loops:
        unpatched 1/1 panic, patched 1/1 survived.)

        (An earlier, partial fix that only snapshotted t_af and moved the
        RBT_REMOVEs before the sleep still crashed 3/3, in
        if_detachhook_del() as shown above, which is why the whole
        configuration-dependent teardown is moved.)

        Normal use on the patched kernel, 3/3, 7 checks each, no kernel
        messages (net.inet.gre.allow=1): two nvgre(4) over a pair(4) link
        between rdomains, ping across, down/up and ping again, "-tunnel" and
        "tunnel" while down then up and ping again, destroy both.  The same
        test passes on the unpatched kernel.

Reply via email to