>Synopsis:      RTM_ADD/RTM_CHANGE with -ifp (or a link-level gateway) may
                pick an interface that is not in the rdomain of the
                routing table; such a route is never purged, keeps an
                address whose ifa_ifp is NULL after the interface is
                destroyed, and the next lookup through it panics
>Category:      kernel
>Severity:      non-critical
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (src as of 2026-11-03)
>Environment:
        System      : OpenBSD 8.0-current, GENERIC.MP as shipped (DIAGNOSTIC,
                      POOL_DEBUG)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest), 4 CPUs
        Source      : sys/net/rtsock.c rev 1.391, sys/net/route.c rev 1.451,
                      sys/netinet/in.c rev 1.196 as of 2026-11-03 (line
                      numbers below are from these)

>Description:

        rtm_getifa() (rtsock.c:1260) chooses the interface address for
        RTM_ADD and RTM_CHANGE.  If the message has an RTAX_IFP sockaddr it
        takes the interface by index, if_get(sdl->sdl_index) (:1278), and
        then ifaof_ifpforaddr(sa, ifp) (:1304); ifa_ifwithroute() does the
        same for a link-level gateway (:1235).  Neither checks that the
        interface is in the rdomain of the routing table, while the other
        lookups (ifa_ifwithaddr(), ifa_ifwithdstaddr(), rtalloc()) are all
        limited to rtable_l2(rtid).  So, as root,

            route -T 111 add 198.51.100.0/24 -iface -ifp vether110 192.0.2.1

        succeeds although vether110 is in rdomain 110, and rtable 111 now
        has a route whose rt_ifa/rt_ifidx belong to rdomain 110.

        Everything that removes routes when an address or an interface goes
        away only walks the rtables of the interface's own rdomain:
        rt_ifa_purge() (route.c:1469) and rt_if_track() (:1891).  When the
        interface is destroyed, the route in rtable 111 stays (moving the
        interface to another rdomain or deleting its address should do the
        same; not tested).  in_purgeaddr() has
        meanwhile set ia_ifp = NULL (in.c:762; in6.c does the same), and
        the route still holds a reference to that address.  The route
        shows "(null)" as interface and cannot be deleted any more:
        RTM_DELETE fails with ESRCH because if_get(rt->rt_ifidx) is NULL
        (rtsock.c:981).

        The next RTM_ADD whose gateway is reached through this route gets
        the dead address from ifa_ifwithroute() (rtalloc() of the gateway)
        and rtrequest() panics:

        panic: kernel diagnostic assertion "info->rti_ifa->ifa_ifp != NULL"
            failed: file "sys/net/route.c", line 993
        rtrequest() at rtrequest+0x7df
        rtm_output() at rtm_output+0x5a7
        route_output() at route_output+0x4ea
        route_send() at route_send+0x64
        sosend() at sosend+0x432
        dofilewritev() at dofilewritev+0x18f
        sys_write() at sys_write+0x55
        syscall() at syscall+0x60d
        Xsyscall() at Xsyscall+0x128

        If the stale route is a cloning route, any packet sent through it
        panics the same way from rt_clone() (rtrequest RTM_RESOLVE <-
        rt_clone <- route_mpath <- in_pcbselsrc <- rip_output, e.g. a
        ping -V 111).  Without DIAGNOSTIC rtrequest() would go on with
        ifp = NULL (not tested).  Only root can add routes.

>How-To-Repeat:

        As root (vether110, vether111 and rdomains 110 and 111 unused):

        1.  ifconfig vether110 create
        2.  ifconfig vether110 rdomain 110
        3.  ifconfig vether110 inet 192.0.2.1/24 up
        4.  ifconfig vether111 create
        5.  ifconfig vether111 rdomain 111
        6.  ifconfig vether111 inet 203.0.113.1/24 up
        7.  route -T 111 add 198.51.100.0/24 -iface -ifp vether110 192.0.2.1
        8.  route -n -T 111 show -inet
        9.  ifconfig vether110 destroy
        10. route -n -T 111 show -inet
        11. route -T 111 delete 198.51.100.0/24
        12. route -T 111 add 10.0.0.0/8 198.51.100.9

        Step 7 adds a route to rtable 111 (rdomain 111) through vether110,
        which is in rdomain 110; step 8 lists it with interface vether110.
        After step 9 the route is still listed by step 10, now with
        interface "(null)", and step 11 cannot delete it ("not in table").
        Step 12 panics the unpatched kernel with the assertion above, every
        time.  With the diff below, step 7 fails with "Network is
        unreachable", steps 10 and 11 find no such route, step 12 fails
        with "Network is unreachable" too and nothing panics; clean up with
        "ifconfig vether111 destroy".

>Fix:

        Do not use an interface taken by index from the message unless it
        is in the rdomain of the routing table, as the other lookups
        already do: rtm_getifa() refuses an RTAX_IFP of another rdomain
        with ENETUNREACH, and ifa_ifwithroute() finds no address for a
        link-level gateway of another rdomain (so rtm_getifa() returns
        ENETUNREACH too).  Both paths are used by RTM_ADD and RTM_CHANGE.

Index: sys/net/rtsock.c
===================================================================
RCS file: /cvs/src/sys/net/rtsock.c,v
diff -u -p -r1.391 rtsock.c
--- sys/net/rtsock.c    17 Apr 2026 18:30:45 -0000      1.391
+++ sys/net/rtsock.c    6 Nov 2026 06:00:00 -0000
@@ -1233,7 +1233,8 @@ ifa_ifwithroute(int flags, const struct
 
                        sdl = satosdl_const(gateway);
                        ifp = if_get(sdl->sdl_index);
-                       if (ifp != NULL)
+                       if (ifp != NULL &&
+                           ifp->if_rdomain == rtable_l2(rtableid))
                                ifa = ifaof_ifpforaddr(dst, ifp);
                        if_put(ifp);
                } else {
@@ -1276,6 +1277,11 @@ rtm_getifa(struct rt_addrinfo *info, uns
 
                sdl = satosdl_const(info->rti_info[RTAX_IFP]);
                ifp = if_get(sdl->sdl_index);
+               /* the interface must be in the rdomain of the table */
+               if (ifp != NULL && ifp->if_rdomain != rtable_l2(rtid)) {
+                       if_put(ifp);
+                       return (ENETUNREACH);
+               }
        }
 
 #ifdef IPSEC

>Verification:

        amd64 KVM guest, 4 CPUs, 4 GB.  Pristine -current src (2026-11-03,
        rtsock.c 1.391), GENERIC.MP as shipped (DIAGNOSTIC + POOL_DEBUG),
        and the same with WITNESS.  The patched kernel is exactly the diff
        above applied with patch -p0 -F0 to a fresh export of the same
        tree (0 build warnings; the patched tree was checked file by file
        against the pristine one: only rtsock.c differs).

        The How-To-Repeat steps exactly as shown above:

          unpatched   3 of 3 runs panicked at step 12 with the assertion
                      and trace above (and 1 of 1 with WITNESS); steps 8
                      and 10 listed the route, step 11 failed.
          patched     3 of 3 runs: steps 7 and 12 failed with "Network is
                      unreachable", no route via vether110 in rtable 111,
                      no panic or fault in dmesg.

        The cloning variant (route add ... -cloning ..., destroy, then
        ping -V 111 198.51.100.7) panicked the unpatched kernel the same
        way from rt_clone() (1 of 1, and 1 of 1 with WITNESS).

        Also refused on the patched kernel: a link-level gateway of another
        rdomain (route add -host ... -link -iface vether110) and
        route change ... -ifp vether110; the same commands with an
        interface of the table's own rdomain still work.

        Normal use on the patched kernel, rtables 130 and 131: inet add /
        get / change / delete via a gateway, -ifp and -ifa of the same
        rdomain, -link -iface, -mpath, inet6 add / change / delete with
        -ifp, ARP and ND entries (arp -s / ndp -s, -d), a cloned ARP
        entry overwritten by a static host route of the same priority,
        route sourceaddr, ping inside the rdomain, destroy an interface
        that has routes, move it to another rdomain, flush, route monitor:
        20 of 20 checks passed, no stale or "(null)" routes, no panic or
        fault in dmesg.  A route with -ifp of another rdomain was accepted
        by the unpatched kernel and refused by the patched one.

Reply via email to