[PR #208] fix: make I1-I5 safe to change while handshakes are sent #206

Open
opened 2026-11-07 18:00:31 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/amnezia-vpn/amneziawg-go/pull/208
Author: @Dv2Corn
Created: 11/5/2026
Status: 🔄 Open

Base: master ← Head: fix/ipackets-race


📝 Commits (1)

  • 6e83005 fix: make I1-I5 safe to change while handshakes are sent

📊 Changes

4 files changed (+56 additions, -10 deletions)

View changed files

📝 device/device.go (+1 -1)
📝 device/device_test.go (+46 -0)
📝 device/send.go (+2 -2)
📝 device/uapi.go (+7 -7)

📄 Description

  • Device.ipackets is a plain [5]*obfChain (device/device.go#L113). IpcSet assigns it on every i1=..i5= line (device/uapi.go#L438-L471) holding only ipcMutex, while every handshake initiation reads it with no lock at all (device/send.go#L139), from the TUN reader (device/send.go#L439) and the handshake timers (device/timers.go#L108, L126). Any IpcSet on a running device that carries an iN= line, including a tool re-sending its full config to add or remove a peer, is therefore a data race. The race detector reports it as a write in handleDeviceLine against a read in SendHandshakeInitiation. Every other device-level value IpcSet writes is already atomic or under a lock (junk, S1-S4, H1-H4, timings, header protection key).
  • The array is now [5]atomic.Pointer[obfChain]: IpcSet stores, SendHandshakeInitiation and IpcGet load. An obfChain is never modified after newObfChain returns, so publishing the pointer atomically is enough. A handshake that races with an update uses either the old or the new chain for each slot.
  • Added TestSignaturePacketsChangeDuringHandshake to device/device_test.go: one goroutine keeps sending handshake initiations while I1 is set, changed and cleared through IpcSet, and the test checks that the cleared value is gone from IpcGet. With -race it fails on master (WARNING: DATA RACE, write at uapi.go:443, read at send.go:139) and passes 200 of 200 runs with this change.

Tested with Go 1.27.3: go test -race -count=200 -run TestSignaturePacketsChangeDuringHandshake ./device and go test ./.... TestAWGDevicePing fails the same way with and without this change in my environment, and Test_outlineIntegration needs network access, which my test environment did not have.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/amnezia-vpn/amneziawg-go/pull/208 **Author:** [@Dv2Corn](https://github.com/Dv2Corn) **Created:** 11/5/2026 **Status:** 🔄 Open **Base:** `master` ← **Head:** `fix/ipackets-race` --- ### 📝 Commits (1) - [`6e83005`](https://github.com/amnezia-vpn/amneziawg-go/commit/6e83005c913446384406b152067d32d95f53bc8a) fix: make I1-I5 safe to change while handshakes are sent ### 📊 Changes **4 files changed** (+56 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `device/device.go` (+1 -1) 📝 `device/device_test.go` (+46 -0) 📝 `device/send.go` (+2 -2) 📝 `device/uapi.go` (+7 -7) </details> ### 📄 Description * `Device.ipackets` is a plain `[5]*obfChain` ([device/device.go#L113](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/device.go#L113)). `IpcSet` assigns it on every `i1=`..`i5=` line ([device/uapi.go#L438-L471](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/uapi.go#L438-L471)) holding only `ipcMutex`, while every handshake initiation reads it with no lock at all ([device/send.go#L139](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/send.go#L139)), from the TUN reader ([device/send.go#L439](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/send.go#L439)) and the handshake timers ([device/timers.go#L108](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/timers.go#L108), [L126](https://github.com/amnezia-vpn/amneziawg-go/blob/b5928efb6ca19f0153958460c3d141f04abc5c2e/device/timers.go#L126)). Any `IpcSet` on a running device that carries an `iN=` line, including a tool re-sending its full config to add or remove a peer, is therefore a data race. The race detector reports it as a write in `handleDeviceLine` against a read in `SendHandshakeInitiation`. Every other device-level value `IpcSet` writes is already atomic or under a lock (junk, S1-S4, H1-H4, timings, header protection key). * The array is now `[5]atomic.Pointer[obfChain]`: `IpcSet` stores, `SendHandshakeInitiation` and `IpcGet` load. An `obfChain` is never modified after `newObfChain` returns, so publishing the pointer atomically is enough. A handshake that races with an update uses either the old or the new chain for each slot. * Added `TestSignaturePacketsChangeDuringHandshake` to `device/device_test.go`: one goroutine keeps sending handshake initiations while I1 is set, changed and cleared through `IpcSet`, and the test checks that the cleared value is gone from `IpcGet`. With `-race` it fails on master (`WARNING: DATA RACE`, write at `uapi.go:443`, read at `send.go:139`) and passes 200 of 200 runs with this change. Tested with Go 1.27.3: `go test -race -count=200 -run TestSignaturePacketsChangeDuringHandshake ./device` and `go test ./...`. `TestAWGDevicePing` fails the same way with and without this change in my environment, and `Test_outlineIntegration` needs network access, which my test environment did not have. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
amnezia-vpn/amneziawg-go#206
No description provided.