SOCKS5 inbound: UDP ASSOCIATE relay socket accepts datagrams from any local sender and replies to the last one

Aperta
#4,716 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
34/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Start at common/bufio/bind.go (lines 132-171 in the reported commit) and read bufio.serverPacketConn.Read, which currently overwrites its reply target with every datagram's source. Then find where the SOCKS5 inbound handles UDP ASSOCIATE so the association's TCP peer address and the request's DST.ADDR/DST.PORT are available to the relay socket. The issue leaves the binding policy open (peer IP, first sender, or declared address/port), and that choice must be settled before coding. Done means datagrams from any other source are dropped and replies go only to the recorded address; verify with a loopback repro that injects a datagram from a second process and confirms the upstream never sees it.

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

Descrizione

Operating system

Linux

System version

1.14.2

Installation type

Others

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

No response

Version
sing-box 1.14.2 (uses `sing` at `87c33f17688fe3dd80eeabef7c3f7a69152ccd42`)
Description

A client authenticates on the SOCKS5 inbound and sends UDP ASSOCIATE. sing-box then opens a UDP
relay socket for the association. That socket accepts a datagram from any source address, not
only from the client that set up the association. Each sender it accepts becomes the reply
target, so replies go to whoever sent the most recent datagram.

RFC 1928, section 7, says: "A UDP association terminates when the TCP connection that the UDP
ASSOCIATE request arrived on terminates", and "the UDP relay server MUST acquire from the SOCKS
server the expected IP address of the client that will send datagrams to the BND.PORT given in
the reply to UDP ASSOCIATE. It MUST drop any datagrams arriving from any source IP address other
than the one recorded for the particular association." sing-box does not do this check. As a
result, the inbound's credentials protect only the TCP control connection. They do not protect
the UDP data path.

The behaviour looks like it comes from bufio.serverPacketConn in sing
(common/bufio/bind.go, lines 132-171 at the commit above). Its Read keeps the source of every
datagram it receives as the destination for later writes, and nothing ties that source to the
control connection's peer.

Observed behaviour

Setup: an application drives sing-box 1.14.2 through an authenticated SOCKS5 inbound on
127.0.0.1. The application authenticates, sends UDP ASSOCIATE, and exchanges datagrams with a
destination that its route rules allow. A second process then sends one SOCKS5 UDP datagram to
the association's relay port. This process runs under the same uid, has its own socket, and never
talks to the TCP control port or sends credentials. Results:

  1. sing-box forwards the injected datagram upstream.
  2. sing-box sends the reply to the injecting process, not to the original client.
  3. The original client does not receive the reply to the injected datagram. When the client
    sends its next datagram, the reply target switches back to the client. So the target is
    always the most recent sender.
  4. With a plain route configuration, the injected datagram goes to the destination in its own
    SOCKS header. That destination was not the association's destination, and no route rule
    allows it. The datagram still leaves through the outbound the association was routed to,
    including that outbound's upstream proxy credentials.
  5. With "udp_connect": true in a leading route-options rule, the injected datagram goes to
    the association's routed destination. The reply still goes to the injecting process.

Finding the relay port is easy. It is an unconnected UDP socket on loopback, and
/proc/net/udp shows it to every local user.

Why it matters

On a host with more than one user, any local process can do the following without the
credentials:

  • send traffic through someone else's association, under that user's routing and upstream
    proxy credentials
  • read the replies meant for that user

The original client's own traffic keeps working afterwards, so the client has no sign that
anything happened. We ran the test with a second process under the same uid. From the code, we
expect a different uid to behave the same way: loopback UDP has no per-uid check, and the
socket accepts any sender. We have not run the different-uid case.

Suggested fix

Bind each association to a single client address and drop datagrams from any other source.
There are two ways to choose that address:

  • the control connection's peer IP. This is what RFC 1928 section 7 describes, using the
    DST.ADDR/DST.PORT from the UDP ASSOCIATE request when the client fills them in.
  • the first sender. The source of the first datagram is recorded, and later datagrams from any
    other address/port are dropped.

Either way, replies go only to the recorded address. They must not follow the latest sender.
For a loopback inbound, the peer IP alone does not separate local processes. Binding to the
first sender's full address and port, or to the address/port the client declared in the
request, does separate them. An option to turn this on would also work if the default cannot
change.

Reproduction

We have an automated integration test for this. It is not public, so here is what it does:

  1. It starts sing-box 1.14.2 with an authenticated SOCKS5 inbound on loopback and a SOCKS5
    upstream outbound that records what it receives.
  2. A client opens UDP ASSOCIATE and does one datagram round trip to an allowed destination. The
    test identifies the association's relay port as the engine's new unconnected loopback UDP
    socket.
  3. A separate process sends one datagram to that port: SOCKS5 UDP header 00 00 00 01, an IPv4
    address and port, then a payload. The process reads whatever comes back within two seconds.
  4. The test checks the points under "Observed behaviour": what the upstream received, the reply
    at the second process, nothing reaching the original client, and the client still answered
    afterwards. The injected header names either the association's own destination or a
    different destination that no rule allows.

The test that expects the injected datagram to be dropped fails on 1.14.2. The tests that pin
the behaviour described above pass. Point 4 (no udp_connect) comes from our first run, which
used a plain route configuration. Point 5 is checked on every run.

Logs

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.