net: cap per-peer header flooding in and after IBD, by side-branch growth (F14)

AcceptBlockHeader runs CheckBlockHeader with fCheckPOW=0 (no RandomX/PoW check), so a peer can
cheaply mint valid-looking sibling headers of the tip, each stored as a permanent mapBlockIndex
entry. The existing per-peer cap ran only during IBD, leaving post-IBD growth unbounded -> a
memory-exhaustion DoS from one connected peer.

Two complementary per-peer caps, applied in and after IBD:
 (1) a TIGHT cap (2000) on newly-stored SIDE-BRANCH headers -- headers added to mapBlockIndex that
     did not become the best-known header. That is the actual growth vector; honest headers-first
     sync and new-block announcements are best-extending and never increment it, and duplicates
     (which do not grow mapBlockIndex) are excluded, so the cap can be small with no honest-peer
     risk. A sibling flood trips it after ~2000 entries instead of millions.
 (2) the prior loose cumulative cap (2*validated-height + 200000) as a backstop against a long
     FORWARD chain of correct-nBits headers (which do extend the best chain, so (1) never sees them).

Adversarial review drove the tightening: the earlier IBD-gate-removal-only version bounded memory
only at ~6.8M entries/peer. Counting just newly-stored non-best headers bounds it to a few thousand.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 22:28:06 -05:00
co-authored by Claude Opus 4.8
parent 661678dd84
commit 1c29fe86ff
+40 -15
View File
@@ -328,8 +328,12 @@ namespace {
bool fBulkHeaderSeen;
//! (server side) time (us) we last served a bulk stream to this peer, for flood throttling.
int64_t nLastBulkServeTime;
//! (#8 IBD header-flood cap) cumulative headers this peer made us process while in IBD.
//! (#8 header-flood cap) cumulative headers this peer made us process.
int64_t nHeadersProcessed;
//! (#8 header-flood cap) newly-stored headers from this peer that did NOT extend the best
//! header chain (side-branch/sibling): the real mapBlockIndex-growth vector. Honest sync and
//! new-block announcements are best-extending and never increment this.
int64_t nSideBranchHeaders;
CNodeState() {
fCurrentlyConnected = false;
@@ -351,6 +355,7 @@ namespace {
fBulkHeaderSeen = false;
nLastBulkServeTime = 0;
nHeadersProcessed = 0;
nSideBranchHeaders = 0;
}
};
@@ -7875,6 +7880,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
CBlockIndex *pindexLast = NULL;
int64_t nSideBranchThisMsg = 0; // newly-stored headers that did NOT extend the best header chain
BOOST_FOREACH(const CBlockHeader& header, headers) {
if(fDebug) {
printf("%s: size.%i, solution size.%i\n", __func__, (int)sizeof(header), (int)header.nSolution.size());
@@ -7887,6 +7893,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
return error("non-continuous headers sequence");
}
int32_t futureblock;
const bool fWasNew = (mapBlockIndex.count(header.GetHash()) == 0); // not already stored (duplicates don't grow mapBlockIndex)
if (!AcceptBlockHeader(&futureblock,header, state, &pindexLast)) {
int nDoS;
if (state.IsInvalid(nDoS) && futureblock == 0)
@@ -7896,29 +7903,47 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
return error("invalid header received");
}
}
else if (fWasNew && pindexLast != NULL && pindexLast != pindexBestHeader) {
// Newly added to mapBlockIndex but NOT the best-known header: a side-branch/sibling,
// which is exactly the flood vector. Honest headers-first sync and new-block
// announcements extend the best chain, so they are never counted here.
nSideBranchThisMsg++;
}
}
// SECURITY (#8: IBD header-flood cap): bound how many headers a single peer can make us
// store while in IBD. Honest headers-first sync needs at most ~chain-length headers from a
// peer; one that floods far past 2x the known chain length is only trying to bloat
// mapBlockIndex/leveldb (such headers are never selected -- nMinimumChainWork gates that --
// but they still cost memory/disk). Cap per-peer and drop the peer. IBD-only: post-IBD there is
// no header-accept PoW check (removed as bug-prone), so a post-IBD flood is bounded only by
// nMinimumChainWork gating selection -- memory/disk growth there is accepted as low-severity.
if (IsInitialBlockDownload()) {
// SECURITY (#8: header-flood cap): bound how many headers a single peer can make us store, in and
// after IBD. Header-accept does no PoW check (removed as bug-prone), so without a cap a connected
// peer could flood sibling headers of the tip and grow mapBlockIndex linearly (a memory-exhaustion
// DoS). Two complementary per-peer caps, both dropping the peer when exceeded:
{
CNodeState *hstate = State(pfrom->GetId());
if (hstate != NULL) {
hstate->nHeadersProcessed += (int64_t)nCount;
// Cap RELATIVE TO THE VALIDATED ACTIVE-CHAIN HEIGHT (attacker-hard -- advancing it requires
// connecting real PoW blocks), NOT pindexBestHeader: a forward-extending header flood advances
// pindexBestHeader in lockstep with the attacker, so a pindexBestHeader-relative cap never fires.
// The checkpoint height is a fixed floor so honest IBD (blocks still lagging headers) is never capped.
hstate->nHeadersProcessed += (int64_t)nCount;
hstate->nSideBranchHeaders += nSideBranchThisMsg;
// (1) Tight cap on newly-stored SIDE-BRANCH headers -- the actual mapBlockIndex-growth
// vector. A legitimate competing fork is bounded by the reorg window (MAX_REORG_LENGTH);
// this allows many times that for slack while bounding a flood to a few thousand entries
// per peer. Honest best-extending sync never increments the counter, so it is safe to
// keep small.
const int64_t kSideBranchCap = 2000;
if (hstate->nSideBranchHeaders > kSideBranchCap) {
Misbehaving(pfrom->GetId(), 100);
return error("%s: peer=%d flooded %lld side-branch headers (cap %lld)", __func__,
pfrom->id, (long long)hstate->nSideBranchHeaders, (long long)kSideBranchCap);
}
// (2) Loose cumulative backstop against an attacker who grinds a long FORWARD chain of
// correct-nBits headers (those do extend the best chain, so (1) never sees them). Cap
// RELATIVE TO THE VALIDATED ACTIVE-CHAIN HEIGHT (attacker-hard -- advancing it needs real
// PoW), NOT pindexBestHeader (a forward flood advances that in lockstep). The checkpoint
// height is a fixed floor so honest IBD (blocks lagging headers) is never capped.
int knownH = std::max((int)chainActive.Height(),
Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints()));
int64_t headerCap = 2 * (int64_t)knownH + 200000;
if (hstate->nHeadersProcessed > headerCap) {
Misbehaving(pfrom->GetId(), 100);
return error("%s: peer=%d flooded %lld headers during IBD (cap %lld)", __func__,
return error("%s: peer=%d flooded %lld headers (cap %lld)", __func__,
pfrom->id, (long long)hstate->nHeadersProcessed, (long long)headerCap);
}
}