>Synopsis:      rip_output() returns in_pcbselsrc() error without m_freem(m); 
>permanent mbuf leak, reachable by any user via setuid ping(8)
>Category:      kernel
>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)
        Source      : sys/netinet/raw_ip.c rev 1.168, openbsd/src as of 
2026-11-03, git d5b06e73
>Description:
        In sys/netinet/raw_ip.c, rip_output() selects a source address when
        the header has none (always true without IP_HDRINCL, and with
        IP_HDRINCL when ip_src is 0.0.0.0):

                if (ip->ip_src.s_addr == INADDR_ANY) {
                        error = in_pcbselsrc(&ip->ip_src, dst, inp);
                        if (error != 0)
                                return (error);         <-- m is not freed
                }

        Every other error exit of rip_output() frees the chain (EMSGSIZE
        paths call m_freem(), rip_chkhdr() frees on failure) and the caller
        rip_send() sets m = NULL after calling rip_output(), so nobody frees
        it.  in_pcbselsrc() fails (EADDRNOTAVAIL) whenever the socket's
        routing table has no route to the destination, so each such send
        leaks the whole packet: 2 mbufs for a 64 byte ICMP echo (payload +
        the M_PREPEND'ed IP header mbuf), mbufs plus clusters for large
        payloads.  The memory is never returned (only reboot helps).

        The IPv6 counterpart rip6_output() does "goto bad" -> m_freem() on
        in6_pcbselsrc() failure, and udp_output() does "goto release", so
        this is specific to raw IPv4.

        Reachability: ping(8) is setuid root and opens a raw ICMP socket,
        and "ping -V <rtable>" is not restricted to root, so a local user
        can leak kernel mbufs at will by pinging an unroutable destination in
        any routing table without a matching route (or in rtable 0 on a host
        without a default route), e.g. many parallel
                ping -V 101 -c 100000 -s 9000 198.51.100.7
        each failed send leaks ~9 KB of clusters.  Eventually kern.maxclusters
        is reached and networking stalls for the whole box.

        Found by a network-sysctl fuzzer (interface churn pinging in empty
        rdomains); the residue showed up as mbufs that never came back after
        all sockets/interfaces were gone.
>How-To-Repeat:
        As root (rdomain 101 with only its loopback, no route to the dst):

                # ifconfig vether150 create
                # ifconfig vether150 rdomain 101 && ifconfig vether150 destroy
                # netstat -m | head -1
                # route -T101 exec ping -c1 -w1 198.51.100.7
                ping: sendmsg: Can't assign requested address
                # netstat -m | head -1

        Each failed send leaves 1-2 more mbufs in use that are never freed.
        (The unprivileged "ping -V" path described above was not run for
        this report.)  It was run manually and shown 
        obsd-ci-agent-148# su tom
        ksh: Cannot determine current working directory
        obsd-ci-agent-148$ cd /home/tom/                                        
       
        obsd-ci-agent-148$ exit
        obsd-ci-agent-148# ifconfig vether150 create
         && ifconfig vether150 destroy                                          
      <
        obsd-ci-agent-148# netstat -m | head -1
        45 mbufs in use:
        obsd-ci-agent-148# route -T101 exec ping -c1 -w1 198.51.100.7
        PING 198.51.100.7 (198.51.100.7): 56 data bytes
        ping: sendmsg: Can't assign requested address
        ping: wrote 198.51.100.7 64 chars, ret=-1
        netstat -m | head -1

        --- 198.51.100.7 ping statistics ---
        1 packets transmitted, 0 packets received, 100.0% packet loss
        obsd-ci-agent-148# netstat -m | head -1
        47 mbufs in use:
        obsd-ci-agent-148# netstat -m | head -1 
        47 mbufs in use:
        obsd-ci-agent-148# route -T101 exec ping -c1 -w1 198.51.100.7 
        PING 198.51.100.7 (198.51.100.7): 56 data bytes
        ping: sendmsg: Can't assign requested address
        ping: wrote 198.51.100.7 64 chars, ret=-1

        --- 198.51.100.7 ping statistics ---
        1 packets transmitted, 0 packets received, 100.0% packet loss
        obsd-ci-agent-148# netstat -m | head -1                       
        49 mbufs in use:
        obsd-ci-agent-148# route -T101 exec ping -c1 -w1 198.51.100.7 
        PING 198.51.100.7 (198.51.100.7): 56 data bytes
        ping: sendmsg: Can't assign requested address
        ping: wrote 198.51.100.7 64 chars, ret=-1

        --- 198.51.100.7 ping statistics ---    
        1 packets transmitted, 0 packets received, 100.0% packet loss
        obsd-ci-agent-148# netstat -m | head -1                       
        51 mbufs in use:
        obsd-ci-agent-148# su tom                                               
       
        ksh: Cannot determine current working directory
        obsd-ci-agent-148$ cd /home/tom
        obsd-ci-agent-148$ ping -V 
        ping: option requires an argument -- V
        usage: ping [-DdEefgHLnqRv] [-c count] [-I sourceaddr] [-i interval]
        [-l preload] [-p pattern] [-s packetsize] [-T toskeyword]
        [-t ttl] [-V rtable] [-w maxwait] host
        obsd-ci-agent-148$ ping -V 101 -c1 -w1 198.51.100.8
        PING 198.51.100.8 (198.51.100.8): 56 data bytes
        ping: sendmsg: Can't assign requested address
        ping: wrote 198.51.100.8 64 chars, ret=-1

        --- 198.51.100.8 ping statistics ---
        1 packets transmitted, 0 packets received, 100.0% packet loss
        obsd-ci-agent-148$ 
        obsd-ci-agent-148$ exit                             
        obsd-ci-agent-148# netstat -m | head -1  
        53 mbufs in use:
        obsd-ci-agent-148# 

        A small program makes it easy to count: a raw ICMP socket with
        SO_RTABLE 101 that sendto()s a 64 byte ICMP echo to 198.51.100.7
        1000 times (every send fails with EADDRNOTAVAIL), printing the
        first line of "netstat -m" before, after, and after close(2):

        # cat rawleak.c
        /*
         * rawip-selsrc-leak.c -- deterministic repro: rip_output() leaks the 
packet
         * mbuf chain when in_pcbselsrc() fails (no route to destination in the
         * socket's routing table -> EADDRNOTAVAIL/EHOSTUNREACH).
         *
         *   cc -o rawleak rawip-selsrc-leak.c
         *   ./rawleak RTABLE COUNT [DST]     # RTABLE must have no route to DST
         *
         * Prints "mbufs in use" (netstat -m) before/after; each failed sendto()
         * leaks 2 mbufs (payload + the M_PREPEND'ed IP header mbuf).
         */
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <netinet/ip.h>
        #include <netinet/ip_icmp.h>
        #include <arpa/inet.h>
        #include <err.h>
        #include <errno.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        static void
        mbufs(const char *tag)
        {
                char line[256];
                FILE *f = popen("netstat -m", "r");
        
                if (f && fgets(line, sizeof(line), f))
                        printf("%s: %s", tag, line);
                if (f)
                        pclose(f);
        }
        
        int
        main(int argc, char *argv[])
        {
                struct sockaddr_in sin;
                struct icmp icp;
                int s, rt, i, n, fails = 0, lasterr = 0;
        
                if (argc < 3)
                        errx(2, "usage: %s RTABLE COUNT [DST]", argv[0]);
                rt = atoi(argv[1]);
                n = atoi(argv[2]);
                memset(&sin, 0, sizeof(sin));
                sin.sin_len = sizeof(sin);
                sin.sin_family = AF_INET;
                inet_pton(AF_INET, argc > 3 ? argv[3] : "198.51.100.7", 
&sin.sin_addr);
        
                if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
                        err(1, "socket");
                if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rt, sizeof(rt)) == -1)
                        err(1, "SO_RTABLE %d", rt);
                memset(&icp, 0, sizeof(icp));
                icp.icmp_type = ICMP_ECHO;
        
                mbufs("before");
                for (i = 0; i < n; i++)
                        if (sendto(s, &icp, 64, 0, (struct sockaddr *)&sin,
                            sizeof(sin)) == -1) {
                                fails++;
                                lasterr = errno;
                        }
                printf("sendto: %d/%d failed, last errno %d (%s)\n", fails, n,
                    lasterr, strerror(lasterr));
                mbufs("after ");
                close(s);
                mbufs("closed");
                return 0;
        }

                # cc -o rawleak rawleak.c
                # ./rawleak 101 1000
                before: 11448 mbufs in use:
                sendto: 1000/1000 failed, last errno 49 (Can't assign requested 
address)
                after : 13448 mbufs in use:
                closed: 13448 mbufs in use:
                # ./rawleak 0 1000 127.0.0.1

        The same program in rtable 0 to 127.0.0.1 (sends succeed) leaves the
        mbuf count unchanged.

>Fix:
        Free the chain on the error path:

Index: sys/netinet/raw_ip.c
===================================================================
--- sys/netinet/raw_ip.c
+++ sys/netinet/raw_ip.c
@@ -310,8 +310,10 @@ rip_output(struct mbuf *m, struct socket *so, struct s
 
        if (ip->ip_src.s_addr == INADDR_ANY) {
                error = in_pcbselsrc(&ip->ip_src, dst, inp);
-               if (error != 0)
+               if (error != 0) {
+                       m_freem(m);
                        return (error);
+               }
        }
 
 #ifdef INET6

        The diff is against sys/netinet/raw_ip.c r1.168.

>Verification:
        Diff applied as shown with patch -p0 to the revisions listed under
        Environment (openbsd/src as of 2026-11-03), no fuzz or offset;
        kernel GENERIC.MP + WITNESS + DIAGNOSTIC, amd64, built without
        warnings.  Unpatched = the same tree without the diff, same config.
        How-To-Repeat run three times on each kernel; counts are "mbufs in
        use" before the 1000 failing sendto() calls and after close(2):

          unpatched: 3/3 +2000 (11448 -> 13448, 13450 -> 15450,
                     15452 -> 17452), not released on close; the single
                     failing ping left +1 or +2.  Control (rtable 0 to
                     127.0.0.1, sends succeed): +0.
          patched:   3/3 +0 (45 -> 45, 45 -> 45, 46 -> 46); control +0.

        100 x "ping -V 101 -c1 -w1 198.51.100.7" as user nobody: unpatched
        +201, +201, +200 mbufs (3 runs); patched -4, -3, +1.

Reply via email to