Darwin: utun process UUID headers are parsed as IP payload
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- go
- Ambito
- networking
Direzione di ricerca
Read the Darwin native, gVisor, and GoStack read/write paths that handle utun framing, then inspect how each obtains socket options. Start by comparing four-byte and twenty-byte framing in the supplied regression evidence. Done means all three paths use the socket’s UTUN_OPT_FLAGS framing length, preserve the NativeTun four-byte caller contract, and reserve UUID bytes when injecting packets, with regression tests covering both IPv4 and IPv6.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
Darwin utun framing is currently treated as a fixed four-byte address-family prefix. XNU adds a 16-byte process UUID when UTUN_FLAGS_ENABLE_PROC_UUID (4) is enabled. The native, gVisor and current GoStack read/write paths therefore need to account for a 20-byte prefix for these sockets.
Reproduction and evidence
Environment: macOS 26.5, arm64. Investigated the SFM 1.14.3 dependency bca4bf029237e444104626bb25f947dda90b06b5 and current dev 9ffa5391a2adc292182ffbeac089e795b1a3190d.
A controlled kernel regression creates its own utun, sets and reads back the flag, sends one interface-bound UDP datagram to an isolated benchmark peer, and injects its reply:
- Flags 0: native and batch reads preserve the IP packet and the reply reaches its originating socket.
- Flags 4: the unpatched reader leaves the extra 16 bytes before the IPv4 header. An observed prefix is
00000002 00000000000000000000000000000000 45000030…. - After accounting for the header size, the kernel round-trip passes with both flags and both native/batch paths.
- Additional AF_UNIX datagram regressions reproduce the independent gVisor and current GoStack failure with nonzero metadata. Their four-byte controls pass; twenty-byte framing fails before the fix and passes after it, for IPv4 and IPv6.
XNU defines the optional UUID in if_utun.h and uses UTUN_HEADER_SIZE for both directions in if_utun.c.
An SFM per-app tunnel also logged ip: unknown version: 0, which is consistent with this failure. I have not read that running provider's FD flags directly or tested a rebuilt signed SFM, so this report does not claim that its end-to-end per-app issue is already resolved. SIP remains enabled.
Expected behavior
Read the socket's UTUN_OPT_FLAGS and use its actual framing length in native, gVisor and GoStack I/O. Preserve the NativeTun four-byte caller contract and reserve the optional UUID bytes when injecting packets.
A patch with regression tests has been prepared against dev in the fork branch d3pre5s/sing-tun:codex/darwin-process-uuid. No existing VPN or default route is changed by the reproducer below. It creates only its own utun and benchmark-range peer; run it on a test machine with administrator authorization.
Standalone kernel regression for current dev
Save the following as tun_darwin_proc_uuid_test.go in the current dev checkout, build with go test -c ., then run only -test.run ^TestDarwinNativeTunProcUUID$ -test.v with administrator authorization. It skips when not root. The full integration/stress suite is not needed for this reproducer.
Kernel regression source
//go:build darwin && !ios
package tun
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"os"
"os/exec"
"sync"
"testing"
"time"
"github.com/sagernet/sing/common/buf"
"golang.org/x/sys/unix"
)
// This integration test only uses a newly allocated utun and a /32 peer in the
// benchmark range. IP_BOUND_IF prevents a test datagram from using another link.
func TestDarwinNativeTunProcUUID(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("requires administrator authorization for an isolated kernel utun")
}
for _, flags := range []int{0, 4} {
for _, batch := range []bool{false, true} {
t.Run(fmt.Sprintf("flags=%d/batch=%v", flags, batch), func(t *testing.T) {
fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, 2)
if err != nil {
t.Fatal(err)
}
transferred := false
defer func() {
if !transferred {
_ = unix.Close(fd)
}
}()
var info unix.CtlInfo
copy(info.Name[:], utunControlName)
if err = unix.IoctlCtlInfo(fd, &info); err != nil {
t.Fatal(err)
}
interfaces, err := net.Interfaces()
if err != nil {
t.Fatal(err)
}
used := make(map[string]bool)
for _, iface := range interfaces {
used[iface.Name] = true
}
index := 200
for used[fmt.Sprintf("utun%d", index)] {
index++
}
address := &unix.SockaddrCtl{ID: info.Id, Unit: uint32(index + 1)}
if err = unix.Bind(fd, address); err != nil {
t.Fatal(err)
}
if err = unix.SetsockoptInt(fd, 2, 1, flags); err != nil {
t.Fatal(err)
}
if err = unix.Connect(fd, address); err != nil {
t.Fatal(err)
}
actualFlags, err := unix.GetsockoptInt(fd, 2, 1)
if err != nil || actualFlags != flags {
t.Fatalf("utun flags=%d: %v", actualFlags, err)
}
device, err := New(Options{FileDescriptor: fd, MTU: 1500, EXP_ExternalConfiguration: true})
if err != nil {
t.Fatal(err)
}
transferred = true
var closeOnce sync.Once
closeDevice := func() { closeOnce.Do(func() { _ = device.Close() }) }
t.Cleanup(closeDevice)
name, err := device.Name()
if err != nil {
t.Fatal(err)
}
// Different peers avoid reusing a route being removed by a prior subtest.
peerIndex := flags + 1
if batch {
peerIndex++
}
localIPText := fmt.Sprintf("198.19.%d.249", peerIndex)
peerIP := net.ParseIP(fmt.Sprintf("198.19.%d.250", peerIndex))
output, err := exec.Command("/sbin/ifconfig", name, "inet", localIPText, peerIP.String(), "netmask", "255.255.255.255", "up").CombinedOutput()
if err != nil {
t.Fatalf("configure own interface: %s: %v", output, err)
}
iface, err := net.InterfaceByName(name)
if err != nil {
t.Fatal(err)
}
conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.ParseIP(localIPText)})
if err != nil {
t.Fatal(err)
}
defer conn.Close()
raw, err := conn.SyscallConn()
if err != nil {
t.Fatal(err)
}
var bindError error
if err = raw.Control(func(socket uintptr) { bindError = unix.SetsockoptInt(int(socket), unix.IPPROTO_IP, 25, iface.Index) }); err != nil {
t.Fatal(err)
}
if bindError != nil {
t.Fatal(bindError)
}
payload := []byte("deus-utun-frame-test")
if _, err = conn.WriteToUDP(payload, &net.UDPAddr{IP: peerIP, Port: 45321}); err != nil {
t.Fatal(err)
}
guard := time.AfterFunc(5*time.Second, closeDevice)
defer guard.Stop()
var frame []byte
for !bytes.Contains(frame, payload) {
if batch {
packets, readErr := device.(DarwinTUN).BatchRead(0, 0)
if readErr != nil {
t.Fatal(readErr)
}
for _, packet := range packets {
if bytes.Contains(packet.Bytes(), payload) {
frame = append([]byte{0, 0, 0, unix.AF_INET}, packet.Bytes()...)
}
packet.Release()
}
} else {
frame = make([]byte, 1500+PacketOffset)
n, readErr := device.Read(frame)
if readErr != nil {
t.Fatal(readErr)
}
frame = frame[:n]
}
if !bytes.Contains(frame, payload) && len(frame) > 0 {
t.Logf("ignoring unrelated kernel packet: %x", frame[:min(len(frame), 32)])
}
}
guard.Stop()
if len(frame) < 32 || frame[4]>>4 != 4 {
t.Fatalf("flags=%d: expected IPv4 after canonical 4-byte header, got prefix %x", flags, frame[:min(len(frame), 24)])
}
packet := frame[4:]
if packet[9] != 17 || !bytes.Equal(packet[28:], payload) {
t.Fatal("UDP payload was not preserved")
}
// Construct the reply independently of the framing implementation.
localIP := append([]byte(nil), packet[12:16]...)
copy(packet[12:16], packet[16:20])
copy(packet[16:20], localIP)
sourcePort := binary.BigEndian.Uint16(packet[20:22])
copy(packet[20:22], packet[22:24])
binary.BigEndian.PutUint16(packet[22:24], sourcePort)
packet[26], packet[27], packet[10], packet[11] = 0, 0, 0, 0
var sum uint32
for i := 0; i < 20; i += 2 {
sum += uint32(binary.BigEndian.Uint16(packet[i : i+2]))
}
for sum>>16 != 0 {
sum = sum&0xffff + sum>>16
}
binary.BigEndian.PutUint16(packet[10:12], ^uint16(sum))
if batch {
buffer := buf.NewSize(len(packet))
_, _ = buffer.Write(packet)
writeErr := device.(DarwinTUN).BatchWrite([]*buf.Buffer{buffer})
buffer.Release()
if writeErr != nil {
t.Fatal(writeErr)
}
} else if n, writeErr := device.Write(frame); writeErr != nil || n != len(frame) {
t.Fatalf("write reply: n=%d: %v", n, writeErr)
}
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
response := make([]byte, 128)
n, _, err := conn.ReadFromUDP(response)
if err != nil || !bytes.Equal(response[:n], payload) {
t.Fatalf("UDP reply did not reach its socket: %v", err)
}
t.Logf("kernel flags=%d, %s: bidirectional UDP frame preserved", flags, name)
})
}
}
}
- Lingua principale
- Go
- Stelle
- 244
- Fork
- 260
- Merge medio
- 4g 21h
- PR unite (30g)
- 5
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di SagerNet/sing-tun
-
auto_redirect: output_udp_icmp routes replies to inbound UDP into the TUNForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
-
很强!!!Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
Tutte le issue di SagerNet/sing-tun
Issue simili
-
good first issue tools
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
Edwin-metcalf/MetCode#76 ·
I maintainer di solito rispondono entro 1 giorno
-
-diff's removed-name budget ignores the names the -ptr pass testedForse già presa @nova-loop l’ha presa oggi. Apertaarea: cli bug good first issue severity: low
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
TMHSDigital/subenum#495 ·
I maintainer di solito rispondono entro 1 giorno
-
bug effort:s impact:med raised-by:worker risk:low to:worker
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
medici-finance/assay#8120 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
factory:subtask
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
reportedBy/community
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openchoreo/openchoreo#5751 ·
I maintainer di solito rispondono entro 1 giorno