From ec5d51098ab7e2fdbe295a86184dd714f188a951 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 25 Sep 2026 15:37:25 +0000 Subject: [PATCH] derp/derpserver: bound dup client sendHistory growth When two connections share a node key, they form a dup client set and noteClientActivity records each sending connection in the set's sendHistory. It appended on every frame whenever the sender was not the immediately previous one, and nothing trimmed the slice while both connections stayed alive. Two connections taking turns sending therefore grew sendHistory by one *sclient per frame without bound. Under the default lastWriterIsActive policy nothing ever stops that growth, so a malicious client (which controls its own node key) or a buggy one that keeps two connections alive and both sending could leak server memory, roughly 8 bytes per frame, for the life of the connection pair. Record the sender by moving it to the end of sendHistory and dropping any earlier occurrence, so each connection appears at most once and the slice stays bounded by the number of connections in the set. This preserves the existing behavior: the fighting check under disableFighters still runs before the move and still disables everyone on the first repeat, and removeClient still promotes the previous speaker from the slice tail. Fixes tailscale/corp#48884 Change-Id: I06198178e6ab0d7e2f04c1dc4c09eafcb16ace46 Signed-off-by: Brad Fitzpatrick --- derp/derpserver/derpserver.go | 21 ++++++--- derp/derpserver/derpserver_test.go | 72 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/derp/derpserver/derpserver.go b/derp/derpserver/derpserver.go index 4a922947b..343ecefd2 100644 --- a/derp/derpserver/derpserver.go +++ b/derp/derpserver/derpserver.go @@ -324,10 +324,14 @@ type dupClientSet struct { // data since. last *sclient - // sendHistory is a log of which members of set have sent - // frames to the derp server, with adjacent duplicates - // removed. When a member of set is removed, the same - // element(s) are removed from sendHistory. + // sendHistory records which members of set have sent frames to + // the DERP server, ordered from least to most recently active. + // Each member appears at most once: recording a member that is + // already present moves it to the end instead of appending a + // duplicate. That keeps the slice bounded by the size of set. + // Without the bound, two connections sharing a key and taking + // turns sending could grow it without limit. When a member of + // set is removed, it is also removed from sendHistory. sendHistory []*sclient } @@ -1879,7 +1883,14 @@ func (s *Server) noteClientActivity(c *sclient) { } } - // Append this client to the list of clients who spoke last. + // Record c as the most recent sender. If c is already in + // sendHistory, remove the earlier occurrence first so that each + // member appears at most once and the slice stays bounded by the + // number of connections in the set. The LastEqual check above + // already handled the case where c is the current tail. + if i := slices.Index(dup.sendHistory, c); i >= 0 { + dup.sendHistory = slices.Delete(dup.sendHistory, i, i+1) + } dup.sendHistory = append(dup.sendHistory, c) } diff --git a/derp/derpserver/derpserver_test.go b/derp/derpserver/derpserver_test.go index e1beadd8d..d96aa48a0 100644 --- a/derp/derpserver/derpserver_test.go +++ b/derp/derpserver/derpserver_test.go @@ -749,6 +749,78 @@ func TestServerDupClients(t *testing.T) { }) } +// TestServerDupClientSendHistoryBounded verifies that a dup client set's +// sendHistory does not grow without bound when two connections sharing a +// key take turns sending frames. See the sendHistory field comment. +func TestServerDupClientSendHistoryBounded(t *testing.T) { + clientPub := key.NewNode().Public() + + newClient := func(name string) *sclient { + return &sclient{key: clientPub, logf: logger.WithPrefix(t.Logf, name+": ")} + } + + t.Run("last_writer_active", func(t *testing.T) { + s := New(key.NewNode(), t.Logf) + s.dupPolicy = lastWriterIsActive + c1, c2 := newClient("c1"), newClient("c2") + s.registerClient(c1) + s.registerClient(c2) + + cs, _ := s.clients.Load(clientPub) + dup := cs.dup + if dup == nil { + t.Fatal("no dup set") + } + for range 5000 { + s.noteClientActivity(c1) + s.noteClientActivity(c2) + } + if got := len(dup.sendHistory); got > len(dup.set) { + t.Fatalf("sendHistory len = %d; want <= %d (set size)", got, len(dup.set)) + } + // The clients must remain active and undisabled, and the last + // one to send is the active one. + if c1.isDisabled.Load() || c2.isDisabled.Load() { + t.Fatal("client disabled under lastWriterIsActive; want none disabled") + } + if got := cs.activeClient.Load(); got != c2 { + t.Fatalf("active client = %p; want c2 %p", got, c2) + } + + // When the active connection drops, the surviving one must + // still be picked up from the recorded history. + s.unregisterClient(c2) + if got, _ := s.clients.Load(clientPub); got.activeClient.Load() != c1 { + t.Fatalf("after dropping c2, active = %p; want c1 %p", got.activeClient.Load(), c1) + } + }) + + t.Run("disable_fighters", func(t *testing.T) { + s := New(key.NewNode(), t.Logf) + s.dupPolicy = disableFighters + c1, c2 := newClient("c1"), newClient("c2") + s.registerClient(c1) + s.registerClient(c2) + + cs, _ := s.clients.Load(clientPub) + dup := cs.dup + if dup == nil { + t.Fatal("no dup set") + } + for range 5000 { + s.noteClientActivity(c1) + s.noteClientActivity(c2) + } + if got := len(dup.sendHistory); got > len(dup.set) { + t.Fatalf("sendHistory len = %d; want <= %d (set size)", got, len(dup.set)) + } + // Fighting connections must be disabled. + if !c1.isDisabled.Load() || !c2.isDisabled.Load() { + t.Fatal("fighting clients not disabled; want both disabled") + } + }) +} + func TestLimiter(t *testing.T) { rl := rate.NewLimiter(rate.Every(time.Minute), 100) for i := range 200 {