>Synopsis:      "ifconfig loN rdomain N" (the destination rdomain equals the
                loopback's own unit) takes the EEXIST / ifp==loifp branch of
                if_createrdomain(), which sets loifp->if_rdomain directly; the
                following if_setrdomain() then sees rdomain == ifp->if_rdomain 
and
                skips its entire teardown (if_down, in_ifdetach, in6_ifdetach,
                route purge).  The loopback's connected/local routes are left in
                the OLD rdomain's routing table.  Destroying that old rdomain's
                loopback later calls rtable_l2set(old_rdomain, 0, 0), whose
                rtable_empty() check fails on the stranded route and panics:
                "rtable %d is not empty".
>Category:      kernel
>Severity:      serious
>Priority:      medium
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (source as of 2026-11-08, master afb08c4d5)
>Environment:
        System      : OpenBSD 8.0-current
        Details     : OpenBSD 8.0-current (GENERIC.MP: DIAGNOSTIC, POOL_DEBUG)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
        Source      : sys/net/if.c, sys/net/if_loop.c, sys/net/rtable.c,
                      openbsd/src master as of 2026-11-08, git afb08c4d5

>Description:

        if_createrdomain() (sys/net/if.c) creates rdomain N together with its
        loopback lo<N>:

            error = if_clone_create(loifname, 0);     /* "lo<N>" */
            if ((loifp = if_unit(loifname)) == NULL)
                    return (ENXIO);
            if (error && (ifp != loifp || error != EEXIST)) {
                    if_put(loifp);
                    return (error);
            }
            loifp->if_rdomain = rdomain;               /* moved, no teardown */
            rtable_l2set(rdomain, rdomain, loifp->if_index);

        When the interface being moved IS lo<N> (i.e. "ifconfig loN rdomain N",
        destination rdomain == the loopback's unit), if_clone_create("loN")
        returns EEXIST and loifp == ifp, so the guard is skipped and
        loifp->if_rdomain is set to N in place.  Back in if_setrdomain():

            if (rdomain != ifp->if_rdomain) {         /* now false -> skipped */
                    ... if_down, rti_delete, in_ifdetach, in6_ifdetach ...
            }

        rdomain now equals ifp->if_rdomain, so the whole teardown is skipped.
        Any address the loopback carried in its previous rdomain keeps its
        connected/local route in that previous rtable, while the loopback now
        lives in rdomain N.

        Destroying the previous rdomain's loopback then hits the assertion in
        rtable_l2set() (sys/net/rtable.c:364), reached from lo_clone_destroy()
        (sys/net/if_loop.c:221, "if (rdomain) rtable_l2set(rdomain, 0, 0)"):

            if (!rtable_empty(rtableid))
                    panic("rtable %d is not empty", rtableid);

            panic: rtable 204 is not empty
            panic(...) at panic+0x170
            rtable_l2set(cc,0,0) at rtable_l2set+0x236     /* 0xcc == 204 */
            if_clone_destroy(...) at if_clone_destroy+0x13f  /* ifconfig lo204 
destroy */

        Any user who can configure interfaces (root) panics the machine with
        shipped commands and no special hardware.

>How-To-Repeat:

        As root (lo204, lo206 unused; rdomains 204 and 206 free):

            ifconfig lo206 create            # rdomain 0
            ifconfig lo206 rdomain 204        # creates lo204, moves lo206 to 
204
            ifconfig lo206 inet 192.0.2.4/24
            ifconfig lo206 rdomain 206        # EEXIST/no-teardown: lo206 -> 
rdomain 206,
                                              # 192.0.2.4/24 route stranded in 
rtable 204
            route -T204 -n show -inet | grep 192.0.2.4   # route still in 204 
(FAIL)
            ifconfig lo204 destroy            # panic: rtable 204 is not empty

        The stranded route is also the known route-ifp-rdomain inconsistency
        (a route in rtable 204 whose ifa_ifp now lives in rdomain 206); a
        later "route -T204 add ... 192.0.2.4" reaches that assert by this new
        path as well.

>Fix:

        Refuse the self-move when the loopback already lives in a non-default
        rdomain, so it cannot be relocated in place while its old rdomain still
        holds its routes.  The normal case -- "ifconfig loN rdomain N" for a
        fresh loN in rdomain 0, which is how an rdomain gets its own loopback --
        is unchanged.  (A loopback in rdomain 0 that carries addresses can still
        strand routes in rtable 0; rtable 0 is never removed, so that does not
        reach this panic and is left alone here.)

Index: sys/net/if.c
===================================================================
--- sys/net/if.c
+++ sys/net/if.c
@@ -2043,6 +2043,19 @@
                return (error);
        }
 
+       /*
+        * Refuse to relocate an existing loopback that already lives in a
+        * non-default rdomain into another one: if_setrdomain() would then
+        * skip its teardown (rdomain == ifp->if_rdomain was just set below)
+        * and strand the old rdomain's connected routes, which later panics
+        * "rtable N is not empty" when that rdomain is removed.
+        */
+       if (ifp == loifp && loifp->if_rdomain != 0 &&
+           loifp->if_rdomain != rdomain) {
+               if_put(loifp);
+               return (EBUSY);
+       }
+
        loifp->if_rdomain = rdomain;
        rtable_l2set(rdomain, rdomain, loifp->if_index);
        if_put(loifp);

>Verification:
        Diff (results/risk-hunt/lo-rdomain-selfmove.diff, md5
        ae42e94ecc8929c0a7bc4d74631652cf) applied as shown to sys/net/if.c of
        openbsd/src master afb08c4d5, no fuzz or offset; GENERIC.MP, amd64,
        built without warnings (sha256 ad88b487...).  Unpatched = the same tree
        without the diff, same config (sha256 58a36473...).  Fresh boot before
        every run; script tests/risk/ab-lordomain.sh (How-To-Repeat, then
        "ifconfig lo206 destroy; ifconfig lo204 destroy", then normal use).

        Unpatched, 3/3 boots: "ifconfig lo206 rdomain 206" succeeds silently;
        "ifconfig lo204 destroy" panics "rtable 204 is not empty"
        (rtable_l2set(cc,0,0) <- if_clone_destroy).

        Patched, 3/3 boots: "ifconfig lo206 rdomain 206" reports
        "SIOCSIFRDOMAIN: Device busy" and lo206 stays in rdomain 204; both
        destroys complete; no panic; 8/8 checks.

        Normal use, every patched boot (on the unpatched kernel this part of the
        script is never reached, the box has already panicked): "ifconfig lo208 
create; ifconfig lo208
        rdomain 208" from rdomain 0 is accepted and lo208 becomes rdomain 208's
        loopback; an address on it answers ping -V208; it destroys cleanly.
        "ifconfig vether209 rdomain 209" plus an address answers ping -V209.

        Earlier release: d5b06e73 has the same code (if_createrdomain() sets
        loifp->if_rdomain on the ifp == loifp EEXIST path without teardown;
        arguments in the other order) -- affected by source inspection, not run.

Reply via email to