Repository navigation
WireGuard proxy: Fix potential startup races - #6852
Merged
Merged
Conversation
device.NewDevice may call Up -> BindUpdate -> bind.Open right away (e.g. on the TUN's initial EventUp), but resolveFunc, listenFunc, downFunc and reserved were assigned after NewDevice returned. Set them before creating the device; downFunc needs the device, so set and read it under bind.mu.
… is up CreateForwarder (SetTransportProtocolHandler) ran at the end of Start, after IpcSet had started the peer goroutines and dev.Up had opened the bind, so received packets could race with the handler setup. gVisor only supports setting these handlers during stack initialization, so do it in NewServer, before the device exists.
Contributor
|
出站修了个幻觉需要删掉,入站 CreateForwarder 移到和 tun 创建一起可以保留 |
Contributor
|
翻了一下 NewDevice,还真的可能自己 Up,https://github.com/WireGuard/wireguard-go/blob/ecfc5a8d54462e18e13c72173e2623d16d8e25a0/device/tun.go#L41-L44,之前以为只有外部 IpcSet/Up 才会动 bind 的 Open,那这修复没问题
|
LjhAUMEM
approved these changes
Sep 28, 2026
RPRX
pushed a commit
that referenced
this pull request
Sep 29, 2026
GFW-knocker
pushed a commit
to GFW-knocker/Xray-core
that referenced
this pull request
Oct 2, 2026
XTLS#6852 (comment) Fixes XTLS#6850 (cherry picked from commit 08cb6e6) [GFW-knocker: hand-merged. The fork's wnoise fields (Wnoise, Wheader and the count/delay/payload ranges) were also assigned after device.NewDevice; they are read by Get_extra_data for the first handshake, so they now go into the bind literal together with resolveFunc/listenFunc/reserved. Server side calls the fork's createForwarder (not yet renamed upstream's way).]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Both the bug and the following is written by Claude Opus 5.5, but I DID review everything and do think what it found is valid.
Fixed #6850.
Fix: WireGuard startup races
Branch:
fix/wireguard-init-races, based onorigin/maine5e85ca9. It is independent of the freedom fix: the two touch no files in common.Commits
5af3adacWireGuard outbound: Fix data race on bind fields during device creationFiles:
proxy/wireguard/client.go,proxy/wireguard/bind.goresolveFunc,listenFuncandreservedare set in thebind{}literal, beforedevice.NewDevice.downFuncneeds the device, so it is set afterwards through a newsetDownFunc()that holdsbind.mu. The receive path reads it under the same lock.Close():CloseclosescloseChbefore the socket, so the receive path's close branch returns without taking the lock.e3156c50WireGuard inbound: Install gVisor protocol handlers before the device is upFile:
proxy/wireguard/server.goCreateForwardermoved from the end ofStart()intoNewServer(), after theServerstruct is built and before any WireGuard device exists. The MASQUE inbound already does it this way.Tests
The tests are on branch
tests/udp-fullcone-wireguard-races(https://github.com/LagPixelLOL/Xray-core); nothing was added to the fix branches. To run them:Outbound race:
-raceon the WireGuard UDP tests, run as separate processes, because the race detector reports a given race only once per process.Inbound race:
TestWireGuardInboundHandlersInstalledBeforeDeviceUp, with a patched gVisor that pauses the handler install.Instance.Start, device upcore.New, no device yet-race, separate processesOther:
run.sh -r 5on a working tree): all 6 tests pass, and both race rows are 0/5.fix/freedom-udp-fullcone.testing/scenariosTestWireguard(a startup-only test).Not covered: WireGuard kernel-TUN mode (
noKernelTun: false), which needs/dev/net/tun.