main: refuse an oversized reorg instead of shutting the node down

ActivateBestChainStep's MAX_REORG_LENGTH guard called StartShutdown() when a
peer-delivered fork would roll back more than -maxreorg (default 99) blocks.
That is a per-reachability remote DoS: the victim re-pulls the attacker's fork
from addrman after every restart and re-kills itself as long as the attacker
is reachable (reproduced on an isolated regtest net; the node exited 2-4s
after pulling a 135-block fork, and again on restart while the attacker was up).

Refuse the fork and keep running instead, mirroring the past-notarized-height
branch just above (InvalidateBlock + DoS(100)). Invalidate the base of the
attacker branch (the block at forkHeight+1), not just its tip, so
FindMostWorkChain's fFailedChain sweep removes the whole branch from the
candidate set in one pass and AcceptBlockHeader rejects any future extension
(pprev becomes BLOCK_FAILED_CHILD) -- the fork is never re-selected or
re-pulled. The base is strictly on the fork side, so no honest block is
disconnected. DoS(100) penalizes the peer; under the default banscore (101) the
ban lands on the second such trip, but the durable protection is the chainstate
poisoning, not the ban. cs_main is already held.

Because the refusal is persisted (BLOCK_FAILED on the base), raising -maxreorg
alone does not make the node follow this fork after a trip: recovery for a
genuine deep reorg is an explicit `reconsiderblock <base>` (the log prints the
hash), optionally with a raised -maxreorg so it is not re-refused. The refusal
is also published via strMiscWarning so it shows in getinfo/getnetworkinfo
rather than leaving a refused node silently stranded. Reorgs within the limit
and the notarized-height branch are unchanged; the startup-only RewindBlockIndex
guard is left as-is.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 17:32:08 -05:00
co-authored by Claude Opus 4.8
parent 6b67fc0e8e
commit da7acda6d8
+43 -14
View File
@@ -4315,23 +4315,52 @@ static bool ActivateBestChainStep(bool fSkipdpow, CValidationState &state, CBloc
assert(MAX_REORG_LENGTH > 0);//, "We must be able to reorg some distance");
if ( reorgLength > MAX_REORG_LENGTH)
{
// A peer-delivered fork that would roll back more than MAX_REORG_LENGTH
// (-maxreorg, default 99) blocks. This used to StartShutdown() the whole
// process, which is a per-reachability remote DoS: the attacker's fork is
// re-pulled from addrman after every restart and re-kills the node. Instead
// refuse the fork, poison the attacker branch so it leaves
// setBlockIndexCandidates and is never re-selected or re-pulled, stay up on
// the current chain, and penalize the delivering peer (DoS(100); the default
// banscore 101 bans on the second such trip). This mirrors the
// past-notarized-height branch above, which already refuses a fork with
// InvalidateBlock + DoS(100) and keeps running.
//
// Invalidate the BASE of the attacker branch (the first block above the fork
// point), not just its tip: FindMostWorkChain's fFailedChain sweep then
// erases the whole branch from the candidate set in one pass, and
// AcceptBlockHeader rejects any future extension (pprev is BLOCK_FAILED_CHILD).
// The base is strictly on the fork side (height forkHeight+1), so it is never
// in chainActive and no honest block is disconnected or poisoned.
CBlockIndex *pindexBadBase = (pindexFork != 0) ? pindexMostWork->GetAncestor(pindexFork->GetHeight() + 1) : pindexMostWork;
if (pindexBadBase == 0)
pindexBadBase = pindexMostWork;
// Because the refusal is PERSISTED (BLOCK_FAILED on the base), raising
// -maxreorg alone does NOT make the node follow this fork after a trip --
// recovery for a genuine deep reorg is an explicit `reconsiderblock <base>`
// (optionally with a raised -maxreorg so it isn't re-refused). Say so, and
// surface it in getinfo "errors" so a refused node is never silently stranded.
auto msg = strprintf(_(
"A block chain reorganization has been detected that would roll back %d blocks!!! "
"This is larger than the maximum of %d blocks, and so the node is shutting down for your safety."
), reorgLength, MAX_REORG_LENGTH) + "\n\n" +
"A block chain reorganization that would roll back %d blocks (more than the maximum of %d) "
"was refused; staying on the current chain."
), reorgLength, MAX_REORG_LENGTH) + "\n" +
_("Reorganization details") + ":\n" +
"- " + strprintf(_("Current tip: %s, height %d, work %s\n"),
pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" +
"- " + strprintf(_("New tip: %s, height %d, work %s\n"),
"- " + strprintf(_("Current tip: %s, height %d, work %s"),
pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" +
"- " + strprintf(_("Rejected tip: %s, height %d, work %s"),
pindexMostWork->phashBlock->GetHex(), pindexMostWork->GetHeight(), pindexMostWork->chainPower.chainWork.GetHex()) + "\n" +
"- " + strprintf(_("Fork point: %s %s, height %d"),
SMART_CHAIN_SYMBOL,pindexFork->phashBlock->GetHex(), pindexFork->GetHeight()) + "\n\n" +
_("Please help me, wise human!");
LogPrintf("*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg,reorgLength+10);
fprintf(stderr,"*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg.c_str(),reorgLength+10);
uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR);
StartShutdown();
return false;
"- " + strprintf(_("Fork point: %s %s, height %d"),
SMART_CHAIN_SYMBOL, pindexFork ? pindexFork->phashBlock->GetHex() : "(none)", pindexFork ? pindexFork->GetHeight() : -1) + "\n" +
strprintf(_("If this is a legitimate deep reorg, recover with: reconsiderblock %s (raise -maxreorg first if needed)"),
pindexBadBase->phashBlock->GetHex());
LogPrintf("*** %s\n", msg);
fprintf(stderr,"*** %s\n", msg.c_str());
strMiscWarning = msg; // surfaces in getinfo/getnetworkinfo (GetWarnings), not fatal
CValidationState tmpstate;
InvalidateBlock(tmpstate, pindexBadBase);
return state.DoS(100, error("ActivateBestChainStep(): reorg of %d blocks exceeds maximum of %d; refusing fork and staying on current chain",
reorgLength, MAX_REORG_LENGTH), REJECT_INVALID, "oversized-reorg");
}
// Disconnect active blocks which are no longer in the best chain.