mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-23 21:23:05 +09:00
retire stale TSMP disco key after peer restart (#22232)
wgengine/magicsock: retire stale TSMP disco key after peer restart When a nonzero control key replaces an old control key equal to the active TSMP key, updateDiscoKey clears the TSMP-active flag and subsequent sends use the new control key, while the retired TSMP material stays stored. A zero control key falls back to the TSMP key as active, and a different TSMP key that is already active stays preferred. Each compare-and-swap attempt allocates a fresh disco snapshot before choosing the active key. Fixes #22201 Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
1 parent
e3ecbb9894
commit
ddb3fd6b43
3 files changed
+237
-29
No files matched your search
@@ -1544,18 +1544,23 @@ func (de *endpoint) setLastPing(ipp netip.AddrPort, now mono.Time) {
|
||||
}
|
||||
|
||||
// updateDiscoKey replaces the controlClient learned disco key for de.
|
||||
// Update only the control-provided key, leaving any existing TSMP key as-is.
|
||||
// If the new control key is zero, switch back to using the TSMP key if it
|
||||
// exists; otherwise mark the new control key as preferred.
|
||||
// Should both keys be zero, nil out the saved key.
|
||||
// The loop here ensures another update did not occur during the interval
|
||||
// between load and store (based on pointer identity).
|
||||
// It updates only the control-provided key and keeps any TSMP-provided key material.
|
||||
//
|
||||
// It reports whether the active disco key changed. If it reports true, the
|
||||
// caller must call [changeActiveDIscoLocked] to reset the state of the
|
||||
// disco connection.
|
||||
// Active-key precedence is recomputed from the snapshot loaded on each CAS
|
||||
// attempt:
|
||||
// - an unchanged nonzero control key, with control already active, is a no-op
|
||||
// - a zero control key falls back to the TSMP key as active
|
||||
// - a distinct TSMP key that is already active stays preferred
|
||||
// - a changed nonzero control key that replaces an old control key equal to
|
||||
// the active TSMP key becomes active (that advertisement is the restarted
|
||||
// peer's previous key)
|
||||
// - otherwise the new control key is preferred
|
||||
//
|
||||
// Should both keys be zero, nil out the saved key.
|
||||
//
|
||||
// It reports whether the stored disco state changed. If it reports true, the
|
||||
// caller must call [changedActiveDiscoLocked] to reset the disco connection.
|
||||
func (de *endpoint) updateDiscoKey(key key.DiscoPublic) bool {
|
||||
epDisco := &endpointDisco{}
|
||||
for {
|
||||
old := de.disco.Load()
|
||||
// Nothing to update
|
||||
@@ -1566,10 +1571,23 @@ func (de *endpoint) updateDiscoKey(key key.DiscoPublic) bool {
|
||||
if old.keyFromControl() == key && !old.tsmpIsActive() && !key.IsZero() {
|
||||
return false
|
||||
}
|
||||
// Fresh snapshot each attempt so a failed CAS cannot reuse a decision
|
||||
// computed from a replaced disco state.
|
||||
epDisco := &endpointDisco{}
|
||||
if old != nil {
|
||||
epDisco.tsmpKey = old.tsmpKey
|
||||
epDisco.tsmpShort = old.tsmpShort
|
||||
epDisco.tsmpActive = old.tsmpActive || key.IsZero()
|
||||
switch {
|
||||
case key.IsZero():
|
||||
// Control was removed; send with the TSMP key if we have one.
|
||||
epDisco.tsmpActive = true
|
||||
case old.tsmpActive && old.controlKey == old.tsmpKey && key != old.controlKey:
|
||||
// Peer restarted. The active TSMP key is the retired control
|
||||
// key, so elect the new control key as the active one.
|
||||
epDisco.tsmpActive = false
|
||||
default:
|
||||
epDisco.tsmpActive = old.tsmpActive
|
||||
}
|
||||
}
|
||||
if !key.IsZero() {
|
||||
epDisco.controlKey = key
|
||||
|
||||
@@ -999,11 +999,13 @@ func TestUpdateDiscoKey(t *testing.T) {
|
||||
zero := key.DiscoPublic{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(de *endpoint)
|
||||
newKey key.DiscoPublic
|
||||
wantChanged bool
|
||||
wantActiveKey key.DiscoPublic
|
||||
name string
|
||||
setup func(de *endpoint)
|
||||
newKey key.DiscoPublic
|
||||
wantChanged bool
|
||||
wantActiveKey key.DiscoPublic
|
||||
wantTsmpActive bool
|
||||
wantTSMPKey key.DiscoPublic
|
||||
}{
|
||||
{
|
||||
name: "first-key-set",
|
||||
@@ -1038,9 +1040,43 @@ func TestUpdateDiscoKey(t *testing.T) {
|
||||
tsmpActive: true,
|
||||
})
|
||||
},
|
||||
newKey: dk3,
|
||||
newKey: dk3,
|
||||
wantChanged: true,
|
||||
wantActiveKey: dk2,
|
||||
wantTsmpActive: true,
|
||||
wantTSMPKey: dk2,
|
||||
},
|
||||
{
|
||||
name: "control-key-matches-active-tsmp-key",
|
||||
setup: func(de *endpoint) {
|
||||
de.disco.Store(&endpointDisco{
|
||||
controlKey: dk1, controlShort: dk1.ShortString(),
|
||||
tsmpKey: dk2, tsmpShort: dk2.ShortString(),
|
||||
tsmpActive: true,
|
||||
})
|
||||
},
|
||||
newKey: dk2,
|
||||
wantChanged: true,
|
||||
wantActiveKey: dk2,
|
||||
wantTsmpActive: true,
|
||||
wantTSMPKey: dk2,
|
||||
},
|
||||
{
|
||||
// Control and the active TSMP key are the same key. A later
|
||||
// control rotation is a peer restart: prefer the new control
|
||||
// key and keep the retired TSMP material.
|
||||
name: "equal-control-and-tsmp-key-rotates",
|
||||
setup: func(de *endpoint) {
|
||||
de.disco.Store(&endpointDisco{
|
||||
controlKey: dk1, controlShort: dk1.ShortString(),
|
||||
tsmpKey: dk1, tsmpShort: dk1.ShortString(),
|
||||
tsmpActive: true,
|
||||
})
|
||||
},
|
||||
newKey: dk2,
|
||||
wantChanged: true,
|
||||
wantActiveKey: dk2,
|
||||
wantTSMPKey: dk1,
|
||||
},
|
||||
{
|
||||
name: "control-key-cleared-tsmp-present",
|
||||
@@ -1051,9 +1087,11 @@ func TestUpdateDiscoKey(t *testing.T) {
|
||||
tsmpActive: false,
|
||||
})
|
||||
},
|
||||
newKey: zero,
|
||||
wantChanged: true,
|
||||
wantActiveKey: dk2,
|
||||
newKey: zero,
|
||||
wantChanged: true,
|
||||
wantActiveKey: dk2,
|
||||
wantTsmpActive: true,
|
||||
wantTSMPKey: dk2,
|
||||
},
|
||||
{
|
||||
name: "control-key-cleared-no-tsmp",
|
||||
@@ -1076,9 +1114,16 @@ func TestUpdateDiscoKey(t *testing.T) {
|
||||
if got := de.updateDiscoKey(tt.newKey); got != tt.wantChanged {
|
||||
t.Errorf("expected de.updateDiscoKey()=%t, got %t", tt.wantChanged, got)
|
||||
}
|
||||
if got := de.disco.Load().key(); got != tt.wantActiveKey {
|
||||
gotDisco := de.disco.Load()
|
||||
if got := gotDisco.key(); got != tt.wantActiveKey {
|
||||
t.Errorf("active key: got %v, want %v", got, tt.wantActiveKey)
|
||||
}
|
||||
if got := gotDisco.tsmpIsActive(); got != tt.wantTsmpActive {
|
||||
t.Errorf("tsmpActive: got %t, want %t", got, tt.wantTsmpActive)
|
||||
}
|
||||
if got := gotDisco.keyFromTSMP(); got != tt.wantTSMPKey {
|
||||
t.Errorf("tsmp key: got %v, want %v", got, tt.wantTSMPKey)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1182,28 +1227,57 @@ func TestUpdateFromNodeChangedActiveDisco(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
initialControl key.DiscoPublic
|
||||
initial *endpointDisco
|
||||
netmapKey key.DiscoPublic
|
||||
wantCalled bool
|
||||
wantActiveKey key.DiscoPublic
|
||||
wantTsmpActive bool
|
||||
}{
|
||||
{
|
||||
name: "control-key-changes-fires",
|
||||
initialControl: dk1,
|
||||
netmapKey: dk2,
|
||||
wantCalled: true,
|
||||
name: "control-key-changes-fires",
|
||||
initial: &endpointDisco{controlKey: dk1, controlShort: dk1.ShortString()},
|
||||
netmapKey: dk2,
|
||||
wantCalled: true,
|
||||
wantActiveKey: dk2,
|
||||
},
|
||||
{
|
||||
name: "control-key-unchanged-no-fire",
|
||||
initialControl: dk1,
|
||||
name: "control-key-unchanged-no-fire",
|
||||
initial: &endpointDisco{controlKey: dk1, controlShort: dk1.ShortString()},
|
||||
netmapKey: dk1,
|
||||
wantCalled: false,
|
||||
wantActiveKey: dk1,
|
||||
},
|
||||
{
|
||||
// Control and the active TSMP key match, then control rotates.
|
||||
// updateFromNode must follow that new key and drop discovery trust.
|
||||
name: "equal-tsmp-active-rotates-fires",
|
||||
initial: &endpointDisco{
|
||||
controlKey: dk1, controlShort: dk1.ShortString(),
|
||||
tsmpKey: dk1, tsmpShort: dk1.ShortString(),
|
||||
tsmpActive: true,
|
||||
},
|
||||
netmapKey: dk2,
|
||||
wantCalled: true,
|
||||
wantActiveKey: dk2,
|
||||
},
|
||||
{
|
||||
name: "equal-tsmp-active-unchanged-no-fire",
|
||||
initial: &endpointDisco{
|
||||
controlKey: dk1, controlShort: dk1.ShortString(),
|
||||
tsmpKey: dk1, tsmpShort: dk1.ShortString(),
|
||||
tsmpActive: true,
|
||||
},
|
||||
netmapKey: dk1,
|
||||
wantCalled: false,
|
||||
wantActiveKey: dk1,
|
||||
wantTsmpActive: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
de := &endpoint{c: &Conn{logf: func(string, ...any) {}}}
|
||||
de.disco.Store(&endpointDisco{controlKey: tt.initialControl, controlShort: tt.initialControl.ShortString()})
|
||||
de.disco.Store(tt.initial)
|
||||
|
||||
// Set a future trustBestAddrUntil so we can detect if it was zeroed.
|
||||
de.trustBestAddrUntil = mono.Now().Add(time.Hour)
|
||||
@@ -1220,6 +1294,13 @@ func TestUpdateFromNodeChangedActiveDisco(t *testing.T) {
|
||||
if !tt.wantCalled && addrUntil != before {
|
||||
t.Errorf("trustBestAddrUntil unexpectedly changed from %v to %v", before, addrUntil)
|
||||
}
|
||||
gotDisco := de.disco.Load()
|
||||
if got := gotDisco.key(); got != tt.wantActiveKey {
|
||||
t.Errorf("active key: got %v, want %v", got, tt.wantActiveKey)
|
||||
}
|
||||
if got := gotDisco.tsmpIsActive(); got != tt.wantTsmpActive {
|
||||
t.Errorf("tsmpActive: got %t, want %t", got, tt.wantTsmpActive)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5020,6 +5020,115 @@ func TestUpsertPeerTSMPKeyPreservedOnControlUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpsertPeerControlRotationAfterTSMP verifies that a peer which advertised
|
||||
// its disco key over TSMP follows a later disco key learned via control. The
|
||||
// restarted peer's new key becomes active for that node without any inbound
|
||||
// disco or TSMP for the new key. A second peer that still uses the retired key
|
||||
// must not make that stale key look selected.
|
||||
func TestUpsertPeerControlRotationAfterTSMP(t *testing.T) {
|
||||
dkA := key.NewDisco().Public()
|
||||
dkB := key.NewDisco().Public()
|
||||
dkC := key.NewDisco().Public()
|
||||
|
||||
// withOtherPeer keeps dkA indexed by a second node so a global
|
||||
// knownPeerDiscoKey(dkA) hit cannot hide a stale active key on peer1.
|
||||
run := func(t *testing.T, withOtherPeer bool) {
|
||||
conn := newTestConn(t)
|
||||
|
||||
nk1 := key.NewNode().Public()
|
||||
peer1 := &tailcfg.Node{
|
||||
ID: 1,
|
||||
Key: nk1,
|
||||
DiscoKey: dkA,
|
||||
Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32")},
|
||||
}
|
||||
peers := []*tailcfg.Node{peer1}
|
||||
var nk2 key.NodePublic
|
||||
if withOtherPeer {
|
||||
nk2 = key.NewNode().Public()
|
||||
peers = append(peers, &tailcfg.Node{
|
||||
ID: 2,
|
||||
Key: nk2,
|
||||
DiscoKey: dkA,
|
||||
Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.2/32")},
|
||||
})
|
||||
}
|
||||
conn.SetNetworkMap(tailcfg.NodeView{}, nodeViews(peers))
|
||||
conn.HandleDiscoKeyAdvertisement(peer1.View(),
|
||||
packet.TSMPDiscoKeyAdvertisement{Key: dkA})
|
||||
|
||||
ep, ok := conn.peerMap.endpointForNodeKey(nk1)
|
||||
if !ok {
|
||||
t.Fatal("peer1 endpoint missing")
|
||||
}
|
||||
if got := ep.disco.Load(); got.key() != dkA || !got.tsmpIsActive() {
|
||||
t.Fatalf("after TSMP advertisement: key=%v tsmpActive=%v, want %v active", got.key(), got.tsmpIsActive(), dkA)
|
||||
}
|
||||
|
||||
check := func(want key.DiscoPublic) {
|
||||
t.Helper()
|
||||
ep, ok := conn.peerMap.endpointForNodeKey(nk1)
|
||||
if !ok {
|
||||
t.Fatal("peer1 endpoint missing")
|
||||
}
|
||||
got := ep.disco.Load()
|
||||
if got.key() != want || got.tsmpIsActive() {
|
||||
t.Errorf("active key=%v tsmpActive=%v, want %v tsmpActive=false", got.key(), got.tsmpIsActive(), want)
|
||||
}
|
||||
if got.keyFromTSMP() != dkA {
|
||||
t.Errorf("tsmp key=%v, want retired material %v", got.keyFromTSMP(), dkA)
|
||||
}
|
||||
if !conn.peerMap.knownPeerDiscoKey(got.key()) {
|
||||
t.Errorf("selected key %v failed knownPeerDiscoKey", got.key())
|
||||
}
|
||||
if _, ok := conn.peerMap.nodesOfDisco[got.key()][nk1]; !ok {
|
||||
t.Errorf("selected key %v does not map to peer1", got.key())
|
||||
}
|
||||
}
|
||||
|
||||
peer1.DiscoKey = dkB
|
||||
conn.UpsertPeer(peer1.View())
|
||||
check(dkB)
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkA][nk1]; ok {
|
||||
t.Error("retired key A should not map to peer1")
|
||||
}
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkB][nk1]; !ok {
|
||||
t.Error("key B should map to peer1")
|
||||
}
|
||||
if withOtherPeer {
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkA][nk2]; !ok {
|
||||
t.Error("key A should still map to the other peer")
|
||||
}
|
||||
if !conn.peerMap.knownPeerDiscoKey(dkA) {
|
||||
t.Error("key A should remain known via the other peer")
|
||||
}
|
||||
} else if conn.peerMap.knownPeerDiscoKey(dkA) {
|
||||
t.Error("retired key A should not pass knownPeerDiscoKey")
|
||||
}
|
||||
|
||||
peer1.DiscoKey = dkC
|
||||
conn.UpsertPeer(peer1.View())
|
||||
check(dkC)
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkB][nk1]; ok {
|
||||
t.Error("key B should not map to peer1 after rotation to C")
|
||||
}
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkC][nk1]; !ok {
|
||||
t.Error("key C should map to peer1")
|
||||
}
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkA][nk1]; ok {
|
||||
t.Error("key A should not map to peer1 after rotation to C")
|
||||
}
|
||||
if withOtherPeer {
|
||||
if _, ok := conn.peerMap.nodesOfDisco[dkA][nk2]; !ok {
|
||||
t.Error("key A should still map to the other peer after rotation to C")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("retired-key-unknown", func(t *testing.T) { run(t, false) })
|
||||
t.Run("other-peer-keeps-retired-key", func(t *testing.T) { run(t, true) })
|
||||
}
|
||||
|
||||
func TestUnambiguousNodeKeyOfPingLocked(t *testing.T) {
|
||||
nk1 := key.NewNode().Public()
|
||||
dk2 := key.NewDisco().Public()
|
||||
|
||||
Reference in new issue
Block a user