Skip to content

System dialer: fail the dial when sockopt.interface cannot be applied - #7161

Closed
izislesar wants to merge 1 commit into
XTLS:mainfrom
izislesar:fix-7160-interface
Closed

izislesar wants to merge 1 commit into
XTLS:mainfrom
izislesar:fix-7160-interface

Conversation

@izislesar

Copy link
Copy Markdown
Contributor

Fixes #7160.

Problem

With sockopt.interface set and the interface missing at dial time, Xray logs the bind error at info level and dials anyway, unbound. The connection then follows the routing table — which is exactly what the binding exists to prevent. In the setup from #7160 the fallback loops the connection back into the TUN: about 8,000 looped connections, all 16,384 ephemeral ports in use on the host, EADDRNOTAVAIL for other processes, until a restart.

Change

The two Control closures in system_dialer.go (tcp dial and udp listen-packet) now propagate the error when sockopt.Interface is set, so the dial fails closed — the same way it already fails when the interface has no route. The dial failure is then reported by the usual path (freedom logs failed to open connection > failed to bind to interface <name> > no such device), visible at normal log levels instead of the info-only line it was.

Since RawConn.Control runs the option application inside a callback that cannot return an error itself, the error is captured and returned from the closure after Control completes.

Scope is deliberately limited to the interface option: other sockopt errors (TCP_FASTOPEN, SO_MARK, tcpCongestion) keep the current log-and-continue behavior, because support for those varies across kernels and systems, and configs in the wild rely on the setsockopt being ignored where the kernel refuses it. Unbound dialing is different though — what replaces the missing binding is never acceptable, it loops or leaks.

Also names the interface in the linux (BindToDevice) and windows (InterfaceByName) error wrappers, the way the darwin one already does, so the failing value shows up in the error:

failed to open connection to tcp:... > failed to dial to dest > dial tcp ...: failed to bind to interface nosuchif0 > no such device

Tests

  • TestDialInterfaceMissing in dialer_test.go: tcp and udp dials to a working listener with sockopt.interface: "nosuchif0", both must return an error naming the interface (before the change, both succeeded unbound; the reporter's reproduction is the same thing seen through a socks inbound);
  • full test suite of the package passes; also checked by hand with two xray binaries (vless + xhttp, socks inbound, freedom outbound bound to a nonexistent interface): the request now fails with the error above instead of going through.

Before, when sockopt.interface named an interface that does not exist,
the error was logged at info level and the dial went on unbound, so the
socket followed the routing table. In full-tunnel setups where outbounds
are bound to an interface precisely to avoid falling back into the TUN
(XTLS#7160), that silent fallback sends traffic into the tunnel again: the
connection loops until ephemeral ports are exhausted, or leaks past the
tunnel. Now the dial fails closed, the same way a bound socket fails when
its interface has no route.

Scope: only errors with sockopt.Interface set are propagated. Other sockopt
errors keep the current behavior, since kernel support for some of them
(TCP_FASTOPEN, SO_MARK, tcpCongestion) varies and configs in the wild
rely on those being ignored on systems that refuse the setsockopt.

Also name the interface in the linux and windows bind errors, the way the
darwin version already does, so the failure names the bad value.
@Fangliding

Copy link
Copy Markdown
Member

我也许需要处理一些其他东西

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sockopt.interface: when the interface does not exist, the dial silently goes unbound (can loop back into TUN)

2 participants