wgengine/magicsock: reject stale peer relay challenges

Check the handshake generation before recording the challenge path.
Add regression coverage for a stale challenge arriving before the current one.
This commit is contained in:
James Tucker committed 2026-10-29 21:42:28 +00:00
1 parent f7c1b8e526
commit 85d10d3f03
2 files changed
+36

No files matched your search

+4
View File
@@ -614,6 +614,10 @@ func (r *relayManager) handleRxDiscoMsgRunLoop(event relayDiscoMsgEvent) {
// No outstanding work tied to this challenge, discard.
return
}
if msg.Generation != work.handshakeGen {
// Reject stale challenges before recording the handshake path.
return
}
_, ok = r.handshakeWorkAwaitingPong[work]
if ok {
// We've seen a challenge for this relay endpoint previously,
+32
View File
@@ -4,6 +4,7 @@
package magicsock
import (
"net/netip"
"testing"
"tailscale.com/disco"
@@ -46,6 +47,37 @@ func TestRelayManagerInitAndIdle(t *testing.T) {
<-rm.runLoopStoppedCh
}
func TestRelayManagerStaleHandshakeChallenge(t *testing.T) {
rm := &relayManager{}
rm.init()
<-rm.runLoopStoppedCh // drive runLoop handlers synchronously
server := key.NewDisco().Public()
work := &relayHandshakeWork{
se: udprelay.ServerEndpoint{ServerDisco: server, VNI: 1},
handshakeGen: 2,
rxDiscoMsgCh: make(chan relayDiscoMsgEvent, 1),
doneCh: make(chan relayEndpointHandshakeWorkDoneEvent, 1),
}
rm.handshakeWorkByServerDiscoVNI[serverDiscoVNI{server, 1}] = work
event := relayDiscoMsgEvent{
disco: server,
from: netip.MustParseAddrPort("127.0.0.1:12345"),
vni: 1,
msg: &disco.BindUDPRelayEndpointChallenge{
BindUDPRelayEndpointCommon: disco.BindUDPRelayEndpointCommon{VNI: 1, Generation: 1},
},
}
rm.handleRxDiscoMsgRunLoop(event)
if len(work.rxDiscoMsgCh) != 0 || len(rm.handshakeWorkAwaitingPong) != 0 || len(rm.addrPortVNIToHandshakeWork) != 0 {
t.Fatal("stale challenge claimed the new handshake's path")
}
event.msg.(*disco.BindUDPRelayEndpointChallenge).Generation = 2
rm.handleRxDiscoMsgRunLoop(event)
if len(work.rxDiscoMsgCh) != 1 || len(rm.handshakeWorkAwaitingPong) != 1 || len(rm.addrPortVNIToHandshakeWork) != 1 {
t.Fatal("current challenge was not accepted after a stale challenge")
}
}
// TestRelayManagerZeroServerDisco verifies that a [disco.CallMeMaybeVia] with
// a zero ServerDisco is dropped rather than crashing the process in
// [relayManager.ensureDiscoInfoFor]'s DiscoPrivate.Shared call, which rejects