snell v6: panic (buffer overflow in ExtendHeader) when detouring snell v6 over snell v6

Aperta
#4,732 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
networking

Direzione di ricerca

The crash is not in this repository: the failing frames are in snellv6/shaped.go in github.com/sagernet/sing-snell, at makeBufferRecord and the WriteVectorised call site that forwards split records to the detour writer. Start by reading makeBufferRecord, makeSliceRecord and the splitting in WriteVectorised to see where a start == 0 buffer reaches ExtendHeader, and confirm that path with the two snell v6 outbounds and the dd | curl upload from the report. Done means the chained-upload reproduction no longer panics and a regression test exercises two nested vectorised writers.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Operating system

macOS

System version

macOS 27.2.1 (Build 26B131)

Installation type

Original sing-box Command Line

If you are using a graphical client, please provide the version of the client.

No response

Version
sing-box version 1.14.4

Environment: go1.26.10 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: c005f35300ce76427dc285481ec5695f5b179cd2
CGO: enabled
Description

Chaining a snell v6 outbound via detour over another snell v6 outbound causes a deterministic panic: buffer overflow: capacity ..., start 0, need ... when uploading enough data to split records in snellv6.(*vectorisedShapedWriter).WriteVectorised.

Root cause in github.com/sagernet/sing-snell (v0.0.0-20260904135315-bc5a12ac736f in v1.14.4, and the same code remains in v0.0.0-20261110094602-04667b0a321c currently referenced by testing):

  1. When the outer snellv6.(*vectorisedShapedWriter).WriteVectorised receives a buffer larger than payloadLimit, it splits the payload via makeSliceRecord (snellv6/shaped.go:48), allocating new buffers with buf.NewSize(...) (start == 0, zero headroom).
  2. It passes those start == 0 buffers to w.upstream.WriteVectorised(flushRecords) (snellv6/shaped.go:292), which is the inner snellv6.(*vectorisedShapedWriter) from the detour outbound.
  3. For each buffer with dataLen <= payloadLimit, the inner WriteVectorised calls shapedWriter.makeBufferRecord(buffer) (snellv6/shaped.go:275 → shaped.go:89), which unconditionally calls buffer.ExtendHeader(frontLen) without checking buffer.Start() >= frontLen (or falling back to makeSliceRecord), causing buf.(*Buffer).ExtendHeader to panic.

Notes:

  • Reproduces both with reuse: true (via reuseVectorisedWriter.WriteVectorised, originally hit on iOS NetworkExtension 1.14.4) and with default reuse: false (via clientVectorisedWriter.WriteVectorised, shown below).
  • Related upstream issue: SagerNet/sing-snell#4.
Reproduction
  1. Save the following minimal server.json and client.json:

server.json:

{
  "log": {
    "level": "debug"
  },
  "inbounds": [
    {
      "type": "snell",
      "tag": "snell-in-1",
      "listen": "127.0.0.1",
      "listen_port": 18081,
      "version": 6,
      "psk": "test-snell-psk-123456"
    },
    {
      "type": "snell",
      "tag": "snell-in-2",
      "listen": "127.0.0.1",
      "listen_port": 18082,
      "version": 6,
      "psk": "test-snell-psk-123456"
    }
  ],
  "outbounds": [
    {
      "type": "direct"
    }
  ]
}

client.json:

{
  "log": {
    "level": "debug"
  },
  "inbounds": [
    {
      "type": "socks",
      "listen": "127.0.0.1",
      "listen_port": 18080
    }
  ],
  "outbounds": [
    {
      "type": "snell",
      "tag": "snell-hop1",
      "server": "127.0.0.1",
      "server_port": 18081,
      "version": 6,
      "psk": "test-snell-psk-123456"
    },
    {
      "type": "snell",
      "tag": "snell-hop2",
      "server": "127.0.0.1",
      "server_port": 18082,
      "version": 6,
      "psk": "test-snell-psk-123456",
      "detour": "snell-hop1"
    }
  ],
  "route": {
    "final": "snell-hop2"
  }
}
  1. Start a local HTTP POST sink on 127.0.0.1:18083, sing-box run -c server.json, and sing-box run -c client.json:
python3 -c '
import http.server, socketserver
class H(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        n = int(self.headers.get("Content-Length", 0))
        while n > 0:
            chunk = self.rfile.read(min(n, 65536))
            if not chunk: break
            n -= len(chunk)
        self.send_response(200); self.end_headers(); self.wfile.write(b"ok\n")
    def log_message(self, *a): pass
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("127.0.0.1", 18083), H) as s: s.handle_request()
' &
sing-box run -c server.json &
sing-box run -c client.json &
  1. Upload 4 MiB through the local SOCKS5 inbound:
dd if=/dev/zero bs=1048576 count=4 2>/dev/null | curl -sS -x socks5h://127.0.0.1:18080 --data-binary @- http://127.0.0.1:18083/

The client sing-box process immediately crashes with panic: buffer overflow: capacity 1329,start 0, need 118.

Logs
# Client log (complete):
INFO[0000] inbound/socks[0]: tcp server started at 127.0.0.1:18080
INFO[0000] network: updated default interface en0, index 14
INFO[0000] network: updated network environment: gateway [10.13.128.1 fe80::62db:15ff:fe7c:2], gateway_mac [60:db:15:7c:00:02 60:db:15:7c:00:02]
INFO[0000] sing-box started (0.00s)
INFO[0000] [3563243450 0ms] inbound/socks[0]: inbound connection from 127.0.0.1:61463
INFO[0000] [3563243450 0ms] inbound/socks[0]: inbound connection to 127.0.0.1:18083
INFO[0000] [3563243450 0ms] outbound/snell[snell-hop2]: outbound connection to 127.0.0.1:18083
INFO[0000] [3563243450 0ms] outbound/snell[snell-hop1]: outbound connection to 127.0.0.1:18082
panic: buffer overflow: capacity 1329,start 0, need 118

goroutine 76 [running]:
github.com/sagernet/sing/common/buf.(*Buffer).ExtendHeader(0x2bd33b56c040, 0x76)
	github.com/sagernet/[email protected]/common/buf/buffer.go:116 +0x13c
github.com/sagernet/sing-snell/snellv6.(*shapedWriter).makeBufferRecord(0x2bd33b3eae80, 0x2bd33b56c040)
	github.com/sagernet/[email protected]/snellv6/shaped.go:89 +0xcc
github.com/sagernet/sing-snell/snellv6.(*vectorisedShapedWriter).WriteVectorised(0x2bd33b662048, {0x2bd33b496280, 0xb, 0x33eb?})
	github.com/sagernet/[email protected]/snellv6/shaped.go:275 +0x32c
github.com/sagernet/sing-snell/snellv6.(*clientVectorisedWriter).WriteVectorised(0x2bd33b3eae00?, {0x2bd33b496280, 0xb, 0x10})
	github.com/sagernet/[email protected]/snellv6/client.go:231 +0x188
github.com/sagernet/sing-snell/snellv6.(*vectorisedShapedWriter).WriteVectorised(0x2bd33b662030, {0x2bd33b52dec0, 0x8, 0x106cbf980?})
	github.com/sagernet/[email protected]/snellv6/shaped.go:292 +0x1d0
github.com/sagernet/sing-snell/snellv6.(*clientVectorisedWriter).WriteVectorised(0x2bd33b4e6090?, {0x2bd33b52dec0, 0x8, 0x8})
	github.com/sagernet/[email protected]/snellv6/client.go:231 +0x188
github.com/sagernet/sing/common/bufio.copyWaitVectorisedWithPool(0x2bd33b333ca0, {0x106d42020, 0x2bd33b662018}, {0x106d4b390, 0x2bd33b4e6090}, 0x2bd33b702001?)
	github.com/sagernet/[email protected]/common/bufio/copy_direct.go:87 +0xa0
github.com/sagernet/sing/common/bufio.copyWaitWithPool(0x2bd33b333ca0, {0x114a23200, 0x2bd33b564600}, {0x106d48a40, 0x2bd33b4ce000}, {0x106d4b3b8, 0x2bd33b4600c0}, {0xc7e, 0x20, 0x2edd, ...})
	github.com/sagernet/[email protected]/common/bufio/copy_direct.go:65 +0x290
github.com/sagernet/sing/common/bufio.copyExtended(0x2bd33b333ca0, {0x114a23200, 0x2bd33b564600}, {0x106d48a40, 0x2bd33b4ce000})
	github.com/sagernet/[email protected]/common/bufio/copy.go:128 +0xd4
github.com/sagernet/sing/common/bufio.CopyWithCounters({0x11477edd0, 0x2bd33b564600}, {0x106d39f60?, 0x2bd33b702038?}, {0x11477ee10, 0x2bd33b218318}, {0x0, 0x0, 0x0}, {0x0, ...}, ...)
	github.com/sagernet/[email protected]/common/bufio/copy.go:99 +0x530
github.com/sagernet/sing/common/bufio.CopyWithIncreateBuffer({0x11477edd0?, 0x2bd33b564600?}, {0x11477ee10, 0x2bd33b218318}, 0x7d000, 0x8)
	github.com/sagernet/[email protected]/common/bufio/copy.go:58 +0x36c
github.com/sagernet/sing-box/route.(*ConnectionManager).connectionCopy(0x2bd33b6377c0, {0x106d647f8, 0x2bd33b6f4270}, {0x106d82a38, 0x2bd33b218318}, {0x106d83068, 0x2bd33b564600}, 0x0, 0x2bd33b21a6b0, 0x0)
	github.com/sagernet/sing-box/route/conn.go:294 +0xa8
created by github.com/sagernet/sing-box/route.(*ConnectionManager).NewConnection in goroutine 51
	github.com/sagernet/sing-box/route/conn.go:160 +0x528

# Server log (complete):
INFO[0000] inbound/snell[snell-in-1]: tcp server started at 127.0.0.1:18081
INFO[0000] inbound/snell[snell-in-2]: tcp server started at 127.0.0.1:18082
INFO[0000] network: updated default interface en0, index 14
INFO[0000] network: updated network environment: gateway [10.13.128.1 fe80::62db:15ff:fe7c:2], gateway_mac [60:db:15:7c:00:02 60:db:15:7c:00:02]
INFO[0000] sing-box started (0.00s)
INFO[0000] [3157553626 0ms] inbound/snell[snell-in-1]: inbound connection from 127.0.0.1:61464
INFO[0000] [3157553626 0ms] inbound/snell[snell-in-1]: inbound connection to 127.0.0.1:18082
INFO[0000] [3157553626 0ms] outbound/direct[0]: outbound connection to 127.0.0.1:18082
INFO[0000] [3176775297 0ms] inbound/snell[snell-in-2]: inbound connection from 127.0.0.1:61465
INFO[0000] [3176775297 0ms] inbound/snell[snell-in-2]: inbound connection to 127.0.0.1:18083
INFO[0000] [3176775297 0ms] outbound/direct[0]: outbound connection to 127.0.0.1:18083
DEBUG[0002] [3157553626 1.2s] connection: connection upload finished
DEBUG[0002] [3176775297 1.2s] connection: connection upload finished
DEBUG[0002] [3176775297 1.2s] connection: connection download finished
DEBUG[0003] [3157553626 2.3s] inbound/snell[snell-in-1]: connection closed: snell: serve 127.0.0.1:61464: context canceled
Supporter
Integrity requirements
  • I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • I confirm that I have provided the server and client configuration files and process that can be reproduced locally, instead of a complicated client configuration file that has been stripped of sensitive data.
  • I confirm that I have provided the simplest configuration that can be used to reproduce the error I reported, instead of depending on remote servers, TUN, graphical interface clients, or other closed-source software.
  • I confirm that I have provided the complete configuration files and logs, rather than just providing parts I think are useful out of confidence in my own intelligence.
Lingua principale
Go
Stelle
40.4k
Fork
5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SagerNet/sing-box

Tutte le issue di SagerNet/sing-box

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.