From f1f5ef1189d21d99ede305c9246ef4342fccc076 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sat, 5 Dec 2026 13:07:45 -0800 Subject: [PATCH] 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 --- wgengine/magicsock/magicsock.go | 10 ++++++---- wgengine/magicsock/magicsock_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 21406ccd9..68eb1c3fd 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -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. diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index 1cba49cd6..677e1da07 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -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") + } +}