>Synopsis: with ip6.forwarding=2 (IPsec-only forwarding) each non-IPsec IPv6 
>packet that would be forwarded leaks its mbuf chain; remotely triggerable 
>kernel memory exhaustion
>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/netinet6/ip6_forward.c rev 1.130, openbsd/src as of
2026-11-03, git d5b06e73
>Description:
net.inet6.ip6.forwarding=2 means "forward only packets that were
processed by IPsec".  The check lives in ip6_forward()
(sys/netinet6/ip6_forward.c), right before the packet is handed to
the interface:

#ifdef IPSEC
if (ISSET(flags, IPV6_FORWARDING) &&
   ISSET(flags, IPV6_FORWARDING_IPSEC) &&
   !ISSET(m->m_pkthdr.ph_tagsset, PACKET_TAG_IPSEC_IN_DONE)) {
error = EHOSTUNREACH;
goto senderr; <-- m still owned here
}
#endif
error = if_output_tso(ifp, &m, dst, rt, ifp->if_mtu);
...
senderr:
if (mcopy == NULL && icmp_len == 0)
goto done;
switch (error) { ... } <-- only builds the ICMP6 error

The senderr label is written for the state after if_output_tso(),
which consumes m.  Every other path that jumps to senderr/icmperror
with a live m calls m_freem(m) first (e.g. the route lookup failure
just above: "m_freem(m); goto icmperror;").  The IPsec-only refusal
does not, so the whole packet is leaked.

The IPv4 twin of this check is in ip_output() and uses "goto bad"
(-> m_freem), and ip6_output() also uses "goto bad", so only
ip6_forward() is affected.  pf_refragment6() and pf's route-to path
call ip6_forward() too, so pf-reassembled packets leak all their
fragments (a 8 KB ping6 leaked ~2 mbufs per fragment).

Impact: on a host acting as an IPsec-only IPv6 gateway
(ip6.forwarding=2 is the documented setting for exactly that) any
remote host on an attached network can send plain IPv6 packets with
a destination behind the gateway and permanently consume kernel
mbufs/clusters, 2 mbufs per small packet, until kern.maxclusters /
the mbuf memory limit is hit and networking on the box stalls.  Only
a reboot recovers the memory.

Found by a network-sysctl fuzzer (forwarding sysctls flipped while
traffic was forwarded across an rdomain router); after all traffic,
sockets and interfaces were gone, tens of thousands of MT_HEADER /
data mbufs stayed allocated.  pf fragment pools, ifq/ifiq lengths,
arp/nd6 hold queues and socket buffers were all empty.

>How-To-Repeat:
As root.  rdomain 101 is the router, rdomains 100 and 102 are the
hosts on either side:

 for p in 100/100/fd00:a:1::2 101/101/fd00:a:1::1 \
          102/101/fd00:a:2::1 103/102/fd00:a:2::2; do
     set -- $(echo $p | tr / " ")
     ifconfig pair$1 create; ifconfig pair$1 rdomain $2
     ifconfig pair$1 inet6 $3/64 up
 done
 ifconfig pair100 patch pair101; ifconfig pair102 patch pair103
 route -T100 add -inet6 default fd00:a:1::1
 route -T102 add -inet6 default fd00:a:2::1
 sleep 3
 for f in 1 2 1; do
     sysctl net.inet6.ip6.forwarding=$f
     netstat -m | head -1
     route -T100 exec ping6 -q -c 500 -i 0.01 fd00:a:2::2
     sleep 2; netstat -m | head -1
 done

With ip6.forwarding=1 the mbuf count does not change.  With
ip6.forwarding=2 it grows by about 2 mbufs per ping and never comes
back:

 ip6.forwarding=1: 500 x ping6 -s 56  mbufs +0
 ip6.forwarding=2: 500 x ping6 -s 56  mbufs +1003
 ip6.forwarding=1: 500 x ping6 -s 56  mbufs -3

Large datagrams (pf reassembles, then ip6_forward() refuses the
reassembled packet) leak more: the same loop with "-c 200 -s 8000"
leaks about 2800 mbufs with ip6.forwarding=2, and they are still in
use 65 seconds later.
manually verified by me (Tom) ...

obsd-ci-agent-148#   for p in 100/100/fd00:a:1::2 101/101/fd00:a:1::1 \
>            102/101/fd00:a:2::1 103/102/fd00:a:2::2; do
>       set -- $(echo $p | tr / " ")
>       ifconfig pair$1 create; ifconfig pair$1 rdomain $2
>       ifconfig pair$1 inet6 $3/64 up
>   done
01; ifconfig pair102 patch pair103                                            <
obsd-ci-agent-148#   route -T100 add -inet6 default fd00:a:1::1
add net default: gateway fd00:a:1::1
obsd-ci-agent-148#   route -T102 add -inet6 default fd00:a:2::1
add net default: gateway fd00:a:2::1
obsd-ci-agent-148#   sleep 3
          for f in 1 2 1; do
              sysctl net.inet6.ip6.forwarding=$f
              netstat -m | head -1
              route -T100 exec ping6 -q -c 500 -i 0.01 fd00:a:2::2
              sleep 2; netstat -m | head -1
          done
obsd-ci-agent-148#   for f in 1 2 1; do
>       sysctl net.inet6.ip6.forwarding=$f
>       netstat -m | head -1
>       route -T100 exec ping6 -q -c 500 -i 0.01 fd00:a:2::2
>       sleep 2; netstat -m | head -1
>   done
net.inet6.ip6.forwarding: 0 -> 1
53 mbufs in use:
PING fd00:a:2::2 (fd00:a:2::2): 56 data bytes



--- fd00:a:2::2 ping statistics ---
500 packets transmitted, 500 packets received, 0.0% packet loss
round-trip min/avg/max/std-dev = 0.078/0.225/2.910/0.209 ms
55 mbufs in use:
net.inet6.ip6.forwarding: 1 -> 2
55 mbufs in use:
PING fd00:a:2::2 (fd00:a:2::2): 56 data bytes



--- fd00:a:2::2 ping statistics ---
500 packets transmitted, 0 packets received, 100.0% packet loss
1053 mbufs in use:
net.inet6.ip6.forwarding: 2 -> 1
1053 mbufs in use:
PING fd00:a:2::2 (fd00:a:2::2): 56 data bytes


--- fd00:a:2::2 ping statistics ---
500 packets transmitted, 500 packets received, 0.0% packet loss
round-trip min/avg/max/std-dev = 0.079/0.214/4.885/0.304 ms
1053 mbufs in use:
obsd-ci-agent-148#


>Fix:
Free the packet before jumping to the error path:
please review with caution... as this is above My (toms) paygrade
Index: sys/netinet6/ip6_forward.c
===================================================================
--- sys/netinet6/ip6_forward.c
+++ sys/netinet6/ip6_forward.c
@@ -319,6 +319,7 @@ reroute:
     ISSET(flags, IPV6_FORWARDING_IPSEC) &&
     !ISSET(m->m_pkthdr.ph_tagsset, PACKET_TAG_IPSEC_IN_DONE)) {
  error = EHOSTUNREACH;
+ m_freem(m);
  goto senderr;
  }
 #endif

With m freed, senderr then sends ICMP6_DST_UNREACH/ADDR from the
saved icmp_buf/mcopy, which matches what IPv4 does for the same
refusal (ip_output() returns EHOSTUNREACH to ip_forward()).
The diff is against sys/netinet6/ip6_forward.c r1.130.

>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; "mbufs" is the change
in "mbufs in use" over 500 x ping6 -s 56 and 200 x ping6 -s 8000
with ip6.forwarding=2:

 unpatched: -s 56: +1003, +999, +1001; -s 8000: +2803, +2800, +2799
            (unchanged 65 s later).  ip6.forwarding=1 between -3
            and +2 in all runs.
 patched:   -s 56: +1, +0, +1; -s 8000: +1, +4, +1.
            ip6.forwarding=1 between -5 and +4 in all runs.

Reply via email to