wgengine/magicsock: ignore netmap updates on a closed Conn

setNetworkMapInternal set relayClientEnabled from the new self node
even when updateNodes had ignored the update because the Conn was closed.
If no self node had been stored yet, a following SetFilter call passed
an invalid self node to updateRelayServersSet, which panicked.

Return before touching any state when the Conn is closed.

Fixes #22939

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-12-05 13:57:31 -08:00
1 parent 717ac8d76e
commit f1f5ef1189
2 files changed
+24 -4

No files matched your search

+6 -4
View File
@@ -3188,12 +3188,17 @@ func (c *Conn) setNetworkMapInternal(self tailcfg.NodeView, peers []tailcfg.Node
udpOffloadKnobsChanged := false
var curGRO, curGSO bool
c.mu.Lock()
if c.closed {
// Nothing to do here, the conn is closed and the update is no longer relevant.
// The updateNodes call above ignored the update too.
c.mu.Unlock()
return
}
relayClientChanged := c.relayClientEnabled != relayClientEnabled
c.relayClientEnabled = relayClientEnabled
filt := c.filt
selfView := c.self
peersSnap := c.peerSnapshotLocked()
isClosed := c.closed
c.usingCachedNetmap.Store(isCached)
if !self.Valid() {
c.initializedAt = mono.Now() // the netmap is being reset
@@ -3210,9 +3215,6 @@ func (c *Conn) setNetworkMapInternal(self tailcfg.NodeView, peers []tailcfg.Node
}
c.mu.Unlock() // release c.mu before potentially calling c.updateRelayServersSet which is O(m * n)
if isClosed {
return // nothing to do here, the conn is closed and the update is no longer relevant
}
if self.Valid() && !selfWasValid {
// Reaching here, we have our first "real" netmap (self is valid, and
// the previous netmap was unset or reset), so record the latency from reset.
+18
View File
@@ -37,6 +37,7 @@ import (
"github.com/tailscale/wireguard-go/device"
"github.com/tailscale/wireguard-go/tun/tuntest"
"go4.org/mem"
"go4.org/netipx"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
extwgconn "golang.zx2c4.com/wireguard/conn"
@@ -5447,3 +5448,20 @@ func TestUnambiguousNodeKeyOfPingLocked(t *testing.T) {
}
})
}
// Tests that a netmap and a filter set on a closed [Conn] are ignored
// rather than leaving it with the relay client enabled and no self node.
func TestSetNetworkMapAndFilterAfterClose(t *testing.T) {
c := newTestConn(t)
c.Close()
self := (&tailcfg.Node{ID: 1, Key: randNodeKey()}).View()
c.SetNetworkMap(self, nil)
c.SetFilter(filter.NewAllowNone(t.Logf, new(netipx.IPSet)))
c.mu.Lock()
defer c.mu.Unlock()
if c.relayClientEnabled {
t.Error("relay client enabled on a closed Conn")
}
}