From a971f8787a144a94e5af0ec42daf5f28f15adeac Mon Sep 17 00:00:00 2001 From: Erik Karulf Date: Fri, 13 Nov 2026 01:14:15 -0600 Subject: [PATCH] Address issue #20 --- PROTOCOL.md | 9 +++++ internal/protocol/budget_test.go | 56 ++++++++++++++++++++++++++++++++ internal/protocol/handshake.go | 39 ++++++++-------------- 3 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 internal/protocol/budget_test.go diff --git a/PROTOCOL.md b/PROTOCOL.md index 20bf77a..7d4870a 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -273,3 +273,12 @@ There are no acknowledgements or retries. Missing fragments cause loss of that datagram, never delivery of a partial message. Complete duplicate datagrams may still be delivered; the application owns replay handling. This framing does not change the bootstrap or WireGuard protocol. + +Unauthenticated CPU work has a separate global token bucket: 128 tag-resolution +attempts per second (including an initial burst of 128), before source-table +eviction or peer enumeration. Signature verification retains its separate +64/second budget. Unknown finishes and server-directed message types perform +no table sweeps. Full expiry runs through periodic BootstrapCleanup; accesses +check record expiry explicitly. Memory caps remain enforced even between sweeps. +Valid finishes bypass the hello budgets; unauthenticated hello floods can still +consume shared hello capacity, so these bounds are not a fairness guarantee. diff --git a/internal/protocol/budget_test.go b/internal/protocol/budget_test.go new file mode 100644 index 0000000..75a4127 --- /dev/null +++ b/internal/protocol/budget_test.go @@ -0,0 +1,56 @@ +package protocol + +import ( + "context" + "testing" + "time" +) + +func TestRejectedPacketsDoNotSweepTables(t *testing.T) { + s, _, _, src, now := fixture(t) + s.pending = map[[hashSize]byte]*pending{{1}: {expires: now.Add(-time.Second)}} + s.replay = map[[hashSize]byte]time.Time{{1}: now.Add(-time.Second)} + s.limits = map[[32]byte]bucket{{1}: {start: now.Add(-time.Hour)}} + for _, kind := range []byte{ClientFinish, ServerAccepted} { + for n := 0; n < 100; n++ { + s.Handle(context.Background(), src, frame(kind, make([]byte, hashSize+proofSize)), now) + } + } + if len(s.pending) != 1 || len(s.replay) != 1 || len(s.limits) != 1 { + t.Fatal("rejected packet swept shared state") + } + s.Expire(now) + if len(s.pending)+len(s.replay)+len(s.limits) != 0 { + t.Fatal("periodic expiry failed") + } +} + +func TestRotatingSourcesCannotBypassResolutionBudget(t *testing.T) { + s, _, hello, _, now := fixture(t) + visited := 0 + s.Authorized = func(yield func(PeerID, []byte) bool) { visited++; yield(PeerID{1}, nil) } + for n := 0; n < 256; n++ { + src := [32]byte{byte(n), 1} + packet := append([]byte(nil), hello...) + copy(packet[8+32:8+64], src[:]) + s.Handle(context.Background(), src, packet, now) + } + if visited != 128 { + t.Fatalf("resolution scans=%d want 128", visited) + } + if len(s.limits) != 128 { + t.Fatalf("allocated source records=%d", len(s.limits)) + } +} + +func TestInvalidFinishDoesNotEnumerateAuthority(t *testing.T) { + s, c, hello, src, now := fixture(t) + reply, _ := s.Handle(context.Background(), src, hello, now) + _, finish, err := c.Complete(context.Background(), reply) + if err != nil { + t.Fatal(err) + } + finish[len(finish)-1] ^= 1 + s.Authorized = func(func(PeerID, []byte) bool) { t.Fatal("unauthenticated finish enumerated peers") } + s.Handle(context.Background(), src, finish, now) +} diff --git a/internal/protocol/handshake.go b/internal/protocol/handshake.go index cf31c51..7a0d591 100644 --- a/internal/protocol/handshake.go +++ b/internal/protocol/handshake.go @@ -169,6 +169,7 @@ type Server struct { limits map[[32]byte]bucket peerLimits map[[32]byte]bucket global bucket + resolution bucket } // Expire erases pending secrets even when the server receives no traffic. @@ -226,34 +227,15 @@ func allow(b bucket, now time.Time, max int) (bucket, bool) { // unknown identities, and encapsulation follows authentication and replay checks. func (s *Server) Handle(ctx context.Context, source [32]byte, packet []byte, now time.Time) ([]byte, *Session) { kind, b, err := Parse(packet) - if err != nil { + if err != nil || (kind != ClientHello && kind != ClientFinish) { return nil, nil } s.mu.Lock() defer s.mu.Unlock() - for h, p := range s.pending { - if !now.Before(p.expires) { - clear(p.hk[:]) - clear(p.session.PSK[:]) - delete(s.pending, h) - } - } - for h, t := range s.replay { - if !now.Before(t) { - delete(s.replay, h) - } - } - for _, limits := range []map[[32]byte]bucket{s.limits, s.peerLimits} { - for h, t := range limits { - if now.Sub(t.start) > time.Second { - delete(limits, h) - } - } - } if kind == ClientFinish { h := [hashSize]byte(b[:hashSize]) p := s.pending[h] - if p == nil || p.source != source || len(Lookup(s.Authorized, p.session.Peer)) == 0 || !hmac.Equal(proof(p.hk, "client-finished", h), b[hashSize:]) { + if p == nil || !now.Before(p.expires) || p.source != source || !hmac.Equal(proof(p.hk, "client-finished", h), b[hashSize:]) || len(Lookup(s.Authorized, p.session.Peer)) == 0 { return nil, nil } ack := frame(ServerAccepted, join(h[:], proof(p.hk, "server-accepted", h))) @@ -312,6 +294,12 @@ func (s *Server) Handle(ctx context.Context, source [32]byte, packet []byte, now return nil, nil } var ok bool + // A separate global budget precedes tag enumeration, table eviction and + // per-source allocation. Rotating transport keys cannot bypass this bound. + s.resolution, ok = allow(s.resolution, now, 128) + if !ok { + return nil, nil + } if s.limits == nil { s.limits = make(map[[32]byte]bucket) s.peerLimits = make(map[[32]byte]bucket) @@ -330,7 +318,8 @@ func (s *Server) Handle(ctx context.Context, source [32]byte, packet []byte, now // recover the real identity by recomputing each authorized peer's tag. One // HMAC per authorized peer is far cheaper than the ML-DSA verification it // gates, and the source bucket above bounds how often an unauthenticated - // sender can trigger the scan. Everything downstream uses the real PeerID. + // sender can trigger the scan; the global resolution budget also bounds + // rotating sources. Everything downstream uses the real PeerID. id, pub := s.resolve(b[:32], b[96:128]) if len(pub) == 0 || ID(pub) != id { return nil, nil @@ -340,10 +329,10 @@ func (s *Server) Handle(ctx context.Context, source [32]byte, packet []byte, now return nil, nil } replay := sha512.Sum384(join(id[:], b[96:128])) - if _, ok := s.replay[replay]; ok { + if expires, ok := s.replay[replay]; ok && now.Before(expires) { digest := sha512.Sum384(packet) for _, p := range s.pending { - if p.source == source && p.helloHash == digest { + if now.Before(p.expires) && p.source == source && p.helloHash == digest { return append([]byte(nil), p.response...), nil } } @@ -367,7 +356,7 @@ func (s *Server) Handle(ctx context.Context, source [32]byte, packet []byte, now } owned := 0 for _, p := range s.pending { - if p.session.Peer == id { + if now.Before(p.expires) && p.session.Peer == id { owned++ } }