>Synopsis:      "route -T N sourceaddr A" stores a bare pointer to the backing
                interface address (ifa->ifa_addr, no reference) in rtable N's
                per-af r_source.  rt_ifa_del() only clears the preferred source
                of the MAIN routing table of the interface's rdomain
                (rtable_clearsource(ifp->if_rdomain, ...)), so when A is deleted
                the pointer kept by any OTHER table of the same rdomain (e.g. a
                secondary rtable created with "route -T N") is left dangling.
                Afterwards "route -T N sourceaddr" reads freed memory, and the
                stale sockaddr is fed to ifa_ifwithaddr() from in_pcbselsrc(),
                in6_pcbselsrc() and the ICMP error paths: a use-after-free.
>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/route.c, sys/net/rtsock.c, sys/net/rtable.c,
                      openbsd/src master as of 2026-11-08, git afb08c4d5

>Description:

        A routing table can hold a "preferred source address" (RTM_SOURCE /
        "route sourceaddr").  rt_setsource() (sys/net/rtsock.c) resolves the
        address and stores it:

            ifa = ifa_ifwithaddr(src, rtableid);   /* looks up in 
rtable_l2(rtableid) */
            ...
            return rtable_setsource(rtableid, src->sa_family, ifa->ifa_addr);

        rtable_setsource() (sys/net/rtable.c) saves the bare pointer with no
        reference:

            ri->r_source = src;        /* == ifa->ifa_addr */

        Because the lookup uses rtable_l2(rtableid) (the rdomain), the source
        can be set on ANY routing table of an rdomain, including a secondary
        table created with "route -T N add ..." (its l2 is 0).

        When the backing address is removed, in_ifscrub()/in_control() ->
        rt_ifa_del() (sys/net/route.c) clears the source of ONLY the main
        table of the rdomain:

            rtable_clearsource(rdomain, ifa->ifa_addr);   /* rdomain = 
ifp->if_rdomain */

        Any secondary table of that rdomain keeps its r_source pointer.  Once
        the in_ifaddr is freed the pointer dangles.  After that:

          - "route -T N sourceaddr" (rtsock.c sysctl_source / RTM_SOURCE get)
            copies freed memory out to userland;
          - in_pcbselsrc() (sys/netinet/in_pcb.c) and in6_pcbselsrc()
            (sys/netinet6/in6_src.c) run ifa_ifwithaddr() against the freed
            sockaddr when a socket in table N selects a source address;
          - the ICMP error paths (ip_icmp.c, icmp6.c) do the same.

        It is observable without a debug kernel as a read-back that does not
        clear, and the value changes once the freed memory is reused:

            # vether201 (192.0.2.50/24) in rdomain 0; rtable 205 is secondary
            ifconfig vether201 create 192.0.2.50/24 up
            route -T205 add -blackhole 203.0.113.0/24 127.0.0.1   # creates 
table 205
            route -T205 sourceaddr 192.0.2.50
            route -T205 sourceaddr            # -> IPv4: 192.0.2.50
            ifconfig vether201 inet 192.0.2.50 delete
            route -T205 sourceaddr            # STILL 192.0.2.50 (should be 
none)
            # after reusing the freed ifa with fresh aliases, the read changes
            # (to another alias such as 192.0.2.52, or to "default", depending
            # on what reuses the memory) -- the stored pointer is into freed,
            # now-reused memory.

>How-To-Repeat:

        As root, on an interface and address not used for anything else:

            ifconfig vether201 create 192.0.2.50/24 up
            route -T205 add -blackhole 203.0.113.0/24 127.0.0.1
            route -T205 sourceaddr 192.0.2.50
            route -T205 sourceaddr                       # IPv4: 192.0.2.50
            ifconfig vether201 inet 192.0.2.50 delete
            route -T205 sourceaddr                        # FAIL: still 
192.0.2.50
            # reuse the freed memory, then read again:
            for i in 51 52 53 54 55 56 57 58 59 60; do
                ifconfig vether201 inet 192.0.2.$i/24 alias; done
            route -T205 sourceaddr   # changed: e.g. 192.0.2.52 or "default"

        The read-back not clearing (3/3) and then changing after reuse is the
        dangling pointer.  The same path is a use-after-free read when a
        socket in table 205 selects a source (in_pcbselsrc) or an ICMP error
        is built.

>Fix:

        Clear the preferred source in every routing table of the rdomain when
        the backing address is deleted, not just the main table.  rt_ifa_del()
        already runs once per rdomain the address lives in; walk that rdomain's
        tables.

        Only tables of the same rdomain are touched (rtable_l2(tid) ==
        rdomain): an rdomain's tables share its interfaces, and rt_setsource()
        only accepts an address of the table's own rdomain, so the same
        address configured in another rdomain, and that rdomain's preferred
        source, are left alone.


Index: sys/net/route.c
===================================================================
--- sys/net/route.c
+++ sys/net/route.c
@@ -1335,7 +1335,21 @@
        if (flags & RTF_CONNECTED)
                prio = ifp->if_priority + RTP_CONNECTED;
 
-       rtable_clearsource(rdomain, ifa->ifa_addr);
+       /*
+        * A preferred source address ("route sourceaddr") may have been
+        * set on ANY routing table of this rdomain, where it is kept as a
+        * bare pointer to ifa->ifa_addr with no reference.  Clear it in
+        * every table of the rdomain, not just the main one, so the
+        * pointer does not dangle once ifa is freed.
+        */
+       {
+               unsigned int tid;
+
+               for (tid = 0; tid <= RT_TABLEID_MAX; tid++) {
+                       if (rtable_exists(tid) && rtable_l2(tid) == rdomain)
+                               rtable_clearsource(tid, ifa->ifa_addr);
+               }
+       }
        KERNEL_LOCK();
        error = rtrequest_delete(&info, prio, ifp, &rt, rdomain);
        KERNEL_UNLOCK();

>Verification:
        Diff (results/risk-hunt/sourceaddr-dangling.diff, md5
        0f91f26c5a8905c02947eff16db92bff) applied as shown to sys/net/route.c of
        openbsd/src master afb08c4d5, no fuzz or offset; kernel GENERIC.MP,
        amd64, built without warnings (sha256 a717b880...).  Unpatched = the
        same tree without the diff, same config (bsd.master-pristine, sha256
        58a36473...).  Fresh boot before every run; test script
        tests/risk/ab-sourceaddr.sh (3 iterations per boot, inet and inet6).

        Unpatched, 3/3 boots: after "ifconfig ... inet 192.0.2.50 delete" and
        "inet6 2001:db8:201::50 delete", "route -T205 sourceaddr" still reports
        IPv4 192.0.2.50 / IPv6 2001:db8:201::50 in 9/9 iterations (18/24
        checks pass; all 6 failures per boot are the two "source cleared"
        checks).  After the freed in_ifaddr is reused by new aliases, the
        read-back changes to "default" without any command touching table
        205 (1/1), i.e. the stored pointer refers to freed memory.

        Patched, 3/3 boots: the read-back is "IPv4: default / IPv6: default"
        right after the address is deleted, 9/9 iterations (24/24 checks).

        Normal use, both kernels, every iteration: the preferred source can be
        set and read back on secondary table 205 (inet and inet6); deleting an
        unrelated address leaves another table-205 source in place; setting,
        pinging and clearing the source of an rdomain's own table (rdomain 208)
        works as before.  No panic or other dmesg output on either kernel.

        Same address in two rdomains, both kernels: 192.0.2.250/24 on a
        vether in rdomain 0 and on another in rdomain 211, table 205
        (rdomain 0) and table 211 both with sourceaddr 192.0.2.250.
        Deleting the address in rdomain 211 clears table 211's source and
        leaves table 205's (rdomain 0, address still configured there) in
        place on both kernels.  Deleting it in rdomain 0 then leaves table
        205 reporting the deleted address on the unpatched kernel and
        "default" on the patched one.  (The same address cannot be
        configured twice within one rdomain: SIOCAIFADDR "File exists".)

Reply via email to