mirror of
https://github.com/amnezia-vpn/amneziawg-go.git
synced 2026-12-03 18:42:09 +07:00
[GH-ISSUE #168] device: first transport packet can use stale S4 after UAPI configuration #83
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
amnezia-vpn/amneziawg-go#83
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @coremraz on GitHub (Jul 31, 2026).
Original GitHub issue: https://github.com/amnezia-vpn/amneziawg-go/issues/168
Suggested title
device: first transport packet can use stale S4 after UAPI configurationAffected upstream source
https://github.com/amnezia-vpn/amneziawg-gov3.0.3cf9d2dd202821301f7039093b0a1b3d4b574c47cmasterwas identical to that revision.golang:1.25.12, Docker withCAP_NET_ADMINand/dev/net/tun.Safe reproduction
This reproducer uses only upstream tests, localhost UDP and freshly generated
test keys. It contains no gateway address, production key, client config or
credential.
Observed on the pinned revision: the AWG test fails after a successful
handshake with a ping/pong transit timeout. A profile bisect showed that
S4=25is sufficient: seven of ten isolated runs failed; H1–H4 alone andS1–S3 alone passed.
Causal evidence
Device.NewDevicestartsRoutineReadFromTUNbefore the caller has appliedUAPI configuration. In
device/send.go, that routine loadsdevice.paddings.transport, calculatesoffset, and then blocks intun.device.Read. If UAPI commitsS4=25while that read is waiting, thefirst returned packet is still associated with the stale local
S4=0.The receiver has already applied S4 and therefore calls
DeterminePacketTypeAndPaddingexpecting the transport header at offset 25.It rejects the packet before key lookup or AEAD decrypt.
Temporary instrumentation in a disposable source copy correlated one failing
run as follows:
S4=0;S4=25on both devices;S4=0snapshot;s4=25, expected H4/type4, observed type4at bytezero and random bytes at byte 25, then logged
Received message with unknown type.This is a stale snapshot across a blocking syscall, not a key exchange,
header-protection-key, routing or NAT failure.
Candidate direction for maintainer review
The following was validated only in an ephemeral Docker copy; it is not a
proposed final patch. Immediately after
Read, re-load the current transportpadding. If it changed, move every just-read payload from the original offset
to the current offset using overlap-safe
copy, then use that same currentpadding for the outgoing element:
With that source-copy change:
-race=false;TestAWGDevicePingpassed 10/10 with-race=false;TestAWGDevicePingpassed 10/10 with-race.Please advise whether re-basing the packet buffer after
Readis the intendedconfiguration-update behavior, or whether initial UAPI configuration should
instead gate the first TUN read. A final upstream patch should include a
deterministic regression test for this startup ordering.
Downstream safety posture
No production data plane has been enabled from this finding. The downstream
canary remains zero-peer with UDP closed and forwarding/NAT disabled. No local
fork or S4 workaround will be deployed before a reviewed upstream or otherwise
immutable-pinned fix, exact-artifact verification, and closed synthetic-peer
staging.
@hoaxisr commented on GitHub (Aug 13, 2026):
Still present on 3.1. Confirming on a newer revision than the one in the report,
with failure rates and a small matrix that narrows what triggers it.
Revisions tested
08271d00(master, tagv3.1.20260813) - reproduces08d68cd(the commit right before the 3.1 feature merge) -reproduces at the same rates1..s4only, on08271d00(3.1)s1..s4only, on08d68cd(pre-3.1)jc/jmin/jmax+s1..s4s1..s4+random_trailersjc/jmin/jmax+random_trailers, no Sxrandom_trailersonlyThe last two rows are the useful part: the 3.1 features on their own are clean,
and junk packets on their own are clean.
Sxis the single factor which linesup with the stale-
offset-across-Readexplanation in the report rather thanwith anything the 3.1 merge introduced.
Test used for the first two rows:
Independent confirmation of the mechanism. Two temporary probes on a clean
checkout, caught on both sides of the same failing run:
The sender probe sits at the end of
RoutineEncryptionand shows the outboundelement carrying
elem.padding == 0whiledevice.paddings.transportalreadyreads
25. The receiver probe sits right afterDeterminePacketTypeAndPaddingand shows the resulting 64-byte datagram classified as
MessageUnknownType.Same picture as the report, on the 3.1 revision.
@StarProxima commented on GitHub (Oct 9, 2026):
Still present on v3.1.20260828 (
masterb5928ef). Adding what we saw in an integration and a test that may help to pick one of the open PRs.Where it shows. We use amneziawg-go in the WireGuard proxy of Xray-core, outbound and inbound, with a gVisor netstack TUN, in an app for iOS, Android and macOS. An idle outbound there closes its device and creates a new one on the next connection, so the bug hits every wake rather than once per start: the first TCP SYN arrives a second late after its retransmission, and the first UDP datagram, often a DNS query, is lost. On the inbound side it is the first answer to the first client. Our builds for devices carry a workaround: a TUN wrapper holds the first
ReaduntilIpcSetandUpare done, then returns zero packets once, so thatRoutineReadFromTUNloads S4 again before it reads the first real packet.The fix the open PRs share makes that workaround unnecessary. We turned the hold off and ran two tests of the integration: the first UDP answer of an inbound (S4=25, header protection, random trailers) and one UDP datagram after each of 5 wakes of an outbound (S4=25). UDP is not retried, so a lost packet fails the test.
master+ #184master+ the same approach, our local branch(macOS 26 arm64, Go 1.26.4,
go test -count=5per row.)A test that also covers a live change of S4. It configures each device only once its reader waits in
Read, as #184 does, then changes S4 on the running devices while both readers wait, to a larger and then to a smaller value:macOS 26 arm64, Go 1.26.4:
TestTransportPaddingChangesWhileTUNReadWaitsTestAWGDevicePingmaster(b5928ef)startup,s4=40,s4=0-racemasterwith the same approach (load S4 again afterRead, move the packets or drop what no longer fits)-race-raceOne difference between the open PRs. A packet read behind the old offset can be as long as the buffer minus that offset. If S4 grew while
Readwaited, moving the packet behind the new offset needs a bounds check, or slicing the buffer panics. #169 and #184 drop such a packet; #160 and #170 do not check. The smallest buffer is on iOS,MaxSegmentSize = 1700bytes (queueconstants_ios.go): there a packet longer than 1700 - 16 - S4 bytes would reach the check. With the usual TUN MTU of 1280-1420 it does not, so this is about a panic that should stay impossible rather than one we have seen.@roman-png commented on GitHub (Oct 14, 2026):
Still present on v3.1.20260828 (
b5928ef). Adding a deterministic regression test, since the existing repros fail only 4–11 of 20 runs; it may help to pick one of the open PRs (#169 / #170 / #184 / #160).The test parks both TUN readers in
ReadwithS4=0beforeIpcSet, soRoutineReadFromTUNis guaranteed to hold the stale padding when the first packet arrives:Results (Go 1.26, linux/amd64 and windows/amd64):
TestTransportPaddingSetAfterReaderStartedTestAWGDevicePingRead(same approach as the open PRs; drop a packet that no longer fits)We carry the fix as a patch in https://github.com/roman-png/vpnpool-engine (
patches/amneziawg-go/0004-…) until one of the PRs lands. Happy to adapt the test into whichever PR you prefer.