UDP traffic to the exact TUN self-address can loop through the direct outbound on macOS
Environment
- OS: macOS 27.0 (26A5388g), Darwin 27.0.0, arm64
- Installation: original sing-box command-line program
- TUN stack:
system auto_route: truestrict_route: trueauto_redirect: not enabled
sing-box version 1.14.0-beta.3
Environment: go1.26.5 darwin/arm64
Tags: with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,with_ocm,with_cloudflared,with_naive_outbound,with_usbip,with_openvpn,with_openconnect,badlinkname,tfogo_checklinkname0
Revision: 1569c52957beaea308497860380d99b841c282b8
CGO: enabled
Summary
I observed a high-rate UDP self-route loop involving a destination equal to the TUN interface's own IPv4 address.
In a read-only StartedService/SubscribeConnections snapshot:
- 4094 of 4096 active UDP/IPv4 connections had the form
172.19.0.1:<ephemeral> -> 172.19.0.1:59871. - Every matching connection entered through the TUN inbound, matched
ip_is_private, used thedirectoutbound, and was attributed to the sing-box process. - The matching connections used different ephemeral source ports.
- During one occurrence, sing-box used approximately 560%-600% CPU and TUN output reached approximately 182k packets/s, while physical-interface output was approximately 259 packets/s.
This evidence is consistent with direct output being recaptured by the same TUN and creating a new UDP session with a new source port on each pass. A static connection snapshot does not independently prove that per-packet reinjection mechanism.
Port 59871 was held by a separate local wildcard UDP listener. I did not establish which process emitted the initial packet.
Relevant configuration
This is a reduction of the relevant routing path, not a claim that it is a deterministic standalone reproducer:
{
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"interface_name": "utun9",
"address": ["172.19.0.1/30"],
"auto_route": true,
"strict_route": true,
"stack": "system"
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"auto_detect_interface": true,
"rules": [
{
"ip_is_private": true,
"action": "route",
"outbound": "direct"
}
]
}
}
Exact-address guard behavior
The direct outbound's TUN loop guard currently checks:
if prefix.Addr() != address && prefix.Contains(address) {
return true
}
For an interface prefix represented as 172.19.0.1/30 and a destination of exactly 172.19.0.1, prefix.Contains(address) is true, but prefix.Addr() != address is false. The exact assigned TUN address is therefore excluded from this guard.
The predicate was added in 761b7f4e and is still present at 4f7f89463ccf.
The behavior can be characterized deterministically without creating a TUN device. At revision 1569c52957beaea308497860380d99b841c282b8, add protocol/direct/outbound_exact_tun_test.go:
package direct
import (
"net/netip"
"testing"
)
func TestLoopGuardRejectsExactTUNAddress(t *testing.T) {
outbound := &Outbound{}
outbound.myAddresses.Store([]netip.Prefix{
netip.MustParsePrefix("172.19.0.1/30"),
})
if !outbound.isMyLoopbackAddress(netip.MustParseAddr("172.19.0.1")) {
t.Fatal("exact TUN interface address is not rejected")
}
}
Run:
go test ./protocol/direct -run '^TestLoopGuardRejectsExactTUNAddress$' -count=1 -v
Complete output:
=== RUN TestLoopGuardRejectsExactTUNAddress
outbound_exact_tun_test.go:15: exact TUN interface address is not rejected
--- FAIL: TestLoopGuardRejectsExactTUNAddress (0.00s)
FAIL
FAIL github.com/sagernet/sing-box/protocol/direct
FAIL
This test characterizes the guard predicate only. It does not by itself reproduce the macOS packet reinjection or the UDP session amplification.
Workaround and observation
I placed the following rule before all direct and mode-routing rules, then restarted the same beta.3 binary:
{
"inbound": "tun-in",
"network": "udp",
"ip_cidr": ["172.19.0.1/32"],
"action": "reject",
"method": "drop"
}
With this rule active:
- an initial four-minute observation showed ordinary, non-amplifying TUN packet rates;
- a follow-up 2h35m after the same restart still showed normal CPU, memory usage, and TUN packet rates;
- sing-box was at approximately 2.9% CPU and 60 MiB RSS at that follow-up;
- the TUN had no interface errors or drops, and the service had not exited or restarted.
I did not take another connection-API snapshot after applying the workaround, so I am not claiming a measured zero matching-session count.
Expected behavior
The direct outbound should not feed a packet indefinitely back into its originating TUN when the destination is the exact assigned address of that TUN.
The connection should either be rejected by the loop guard or delivered through a host-local path that cannot re-enter the same TUN. If the equality exemption is intentional for reaching host-local services, could it be restricted to safe local delivery?
Kernel panic context
The host panicked twice in separate boot sessions with the same assertion:
dlil_if_ref: wraparound refcnt @dlil_subr.c:410
In both panic reports, sing-box was the panicked task and the normalized high-level path originated from UDP sendto. The first panic occurred before I began runtime loop sampling. The loop was directly observed after the first reboot before the second panic, and was observed again after the second reboot.
This is included only as safety-impact context. It does not establish that sing-box created or leaked the XNU interface reference, that the loop was active at either exact panic instant, or that the loop was the root cause of the kernel failure.
Related reports
- #4178 reports a TUN/direct self-route loop on Windows with IPv6 and public destinations rather than the exact assigned IPv4 TUN address.
- #4309 reports a similar random-source-port/UDPNAT amplification pattern, but on Linux/OpenWrt with IPv6,
auto_redirect, and a public destination. - #4086 reports a TUN/direct routing loop on Linux involving a different routing path.
This report is specifically about macOS, IPv4 UDP, no auto_redirect, and a destination exactly equal to the assigned TUN address.
Could you check if this has been correctly fixed in the latest stable version?
I encountered the same issue on macOS with v1.13.19. In my case, a Wi-Fi roam triggered the UDP self-route loop to the TUN address within seconds.
v1.13.19 includes the exact-address check, but direct does not refresh myAddresses after interface changes. The corresponding refresh logic is present in v1.14.0-rc.1: direct implements InterfaceUpdateListener and refreshes the address cache in InterfaceUpdated().
https://github.com/SagerNet/sing-box/blob/8dd67a1e49711ce8a9a884bef60a2139ef36446f/protocol/direct/outbound.go#L36-L129
Perhaps this change could also be backported to the stable branch.
@nekohasekai
TL;DR: On Windows, I reproduced a UDP TUN → direct → TUN loop to 172.19.0.2:5351 with Throne 1.2.4 (sing-box 1.13.16, Throne fork). A single UDP packet triggered a surge in sessions. Rejecting traffic to the TUN subnet prevented it.
After upgrading to Throne 1.3.0-beta.1 (sing-box 1.14.0-rc.5, Throne fork), the same test no longer triggers the surge, even with the reject rule disabled. This is related to #4367, but tests the Windows TUN-peer case.
I can share a positive test result for a related UDP TUN/direct loop on Windows. My case involved the TUN peer address 172.19.0.2:5351, rather than the exact TUN self-address 172.19.0.1 reported here, so it is closer to #4392.
Versions tested:
-
Throne 1.2.4: sing-box 1.13.16, using Throne’s fork at
fd4e19523a14. Dependency reference -
Throne 1.3.0-beta.1: sing-box 1.14.0-rc.5, using Throne’s fork at
990fb42efe38. Dependency reference The relevant settings were: -
Windows, TUN
systemstack. -
TUN address:
172.19.0.1/24. -
auto_route: true,strict_route: true. -
route.auto_detect_interface: true. -
DNS hijacking followed by an
ip_is_privaterule routing private destinations through thedirectoutbound.
Initially, Acronis updater.exe was sending PCP requests to 172.19.0.2:5351. This triggered a rapid increase in UDP sessions and elevated CPU usage in ThroneCore.exe. The connection list filled with approximately 1024 UDP sessions of the form:
172.19.0.1:<changing ephemeral port> → 172.19.0.2:5351
Most subsequent sessions were attributed to ThroneCore.exe itself and matched ip_is_private → direct. This was consistent with direct outbound packets being recaptured by the TUN.
My temporary workaround was to reject traffic destined for the TUN subnet, placing this rule after DNS hijacking and before the private-IP/direct rule:
{
"inbound": ["tun-in"],
"ip_cidr": ["172.19.0.0/24"],
"action": "reject"
}
For the before/after comparison, I used a small Python script that sends exactly one UDP datagram, containing a NAT-PMP external-address query (00 00), to 172.19.0.2:5351. It does not retransmit. The script also supports observing the matching UDP sessions through the Clash API.
The packet-sending part is equivalent to:
import socket
import time
# On an affected core, one packet can start a persistent loop.
# Stopping this script may not stop it; disable TUN/restart the core.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.connect(("172.19.0.2", 5351))
sock.send(b"\x00\x00")
time.sleep(10)
With the workaround disabled, this test reproduced the UDP-session spike before the upgrade. After upgrading to Throne 1.3.0-beta.1, I disabled the workaround and repeated the test: the UDP-session spike no longer occurs.
This confirms that my Windows/TUN-peer reproducer no longer reproduces the problem with the newer Throne build. I have not tested the macOS exact-self-address case, the Wi-Fi-roaming scenario, or the latest upstream stable binary, so I cannot confirm those separately.
I checked the upstream v1.14.0 tag specifically, as requested above.
At source level, the original macOS exact-self-address case appears to be fixed in stable v1.14.0.
The original guard effectively allowed the exact assigned interface address:
if prefix.Addr() != address && prefix.Contains(address) {
return true
}
Commit:
7da573f1873ec9a4b13a31cd04e7c0f940d309de
Fix TUN loopback under Darwin systems
changed the logic to:
for _, prefix := range myAddresses {
if !C.IsDarwin && prefix.Addr() == address {
continue
}
if prefix.Contains(address) {
return true
}
}
and that exact logic is present in the upstream v1.14.0 tag.
So for the reported macOS case:
TUN prefix: 172.19.0.1/30
destination: 172.19.0.1
platform: Darwin
the equality exception is no longer taken, prefix.Contains(address) returns true, and both:
DialContext(...)
ListenPacket(...)
return:
loopback connection to TUN range
before opening the direct socket.
Importantly, this is a targeted Darwin fix rather than a global semantic change: non-Darwin platforms still retain the historical exact-address exemption.
I also checked the interface-change case mentioned above. v1.14.0 already has:
_ adapter.InterfaceUpdateListener = (*Outbound)(nil)
and:
func (h *Outbound) InterfaceUpdated(ctx context.Context) {
h.fetchMyAddresses()
...
}
so the cached interface-address set is refreshed after network/interface updates as well.
Therefore the two relevant failure modes discussed in this thread are both covered in upstream stable v1.14.0 source:
- exact TUN self-address on Darwin is rejected;
- the direct outbound refreshes
myAddressesafter interface changes.
I have not reproduced the original packet storm on a macOS host, so this is source-level verification rather than a runtime reproduction.
One remaining gap I can see is regression coverage: I could not find a dedicated test for isMyLoopbackAddress.
A focused test matrix would be useful:
Darwin + exact prefix.Addr() -> reject
Darwin + another address in prefix -> reject
non-Darwin + exact prefix.Addr() -> preserve current allow semantics
unrelated destination -> allow
and ideally exercise both TCP DialContext and UDP ListenPacket, since the reported failure was the UDP recapture loop.
Based on the stable source, I think #4367 itself can be considered fixed in v1.14.0.