From 6b67fc0e8e99d5a96a5c9dac9660cc9a194d44a7 Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 17:05:03 -0500 Subject: [PATCH 1/3] hush_checkPOW: verify RandomX at the recovered height on the peer-accept path A block arriving over P2P reaches ProcessNewBlock(0,0,...) (the net BLOCK handler), and both CheckBlock() calls on that accept path -- in ProcessNewBlock and in AcceptBlock -- pass fCheckPOW=0, so CheckBlockHeader's RandomX check never runs there. The only real-height PoW gate before the block becomes a setBlockIndexCandidate is hush_checkPOW(0,pblock,height), and for a peer block height is 0: CheckRandomXSolution ran at height 0 (where RandomXValidationRequired is false, so it is skipped), the real height was then recovered from hashPrevBlock, but RandomX was never re-checked. So a block with a forged RandomX solution (only a grindable SHA256 target) could become a candidate with its RandomX never verified at its real height, and trip the MAX_REORG_LENGTH reorg guard before the connect-time check would reject it -- the cheap half of the reorg-shutdown DoS. Re-run CheckRandomXSolution at the recovered height before returning. It is a no-op wherever the first call was (algo gate, activation height -- regtest is far below it -- HUSH_LOADINGBLOCKS during IBD/reindex, the last in-index checkpoint, and the thread-local miner self-check skip), so it only adds one light-mode verification on the height==0 peer-accept path, which previously did no real-height RandomX work at all. Co-Authored-By: Claude Opus 4.8 --- src/hush_bitcoind.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index e29b4f5bd..3d09cdac5 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -1713,6 +1713,22 @@ int32_t hush_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) height = pprev->GetHeight() + 1; if ( height == 0 ) return(0); + // The CheckRandomXSolution() above ran with height==0, where RandomXValidationRequired(0) + // is false, so RandomX was NOT verified. Re-verify now that the real height is recovered + // from hashPrevBlock. Without this, a peer block entering via ProcessNewBlock(0,0,...) (the + // net BLOCK handler, main.cpp) becomes a setBlockIndexCandidate with its PoW never checked + // at its real height: both CheckBlock() calls on that accept path pass fCheckPOW=0, so + // CheckBlockHeader's RandomX check never runs there -- this hush_checkPOW(0,...) is the ONLY + // real-height PoW gate before candidacy. A forged-RandomX fork could then trip the reorg + // guard before the connect-time check rejects it. This is a no-op wherever the first call + // was already a no-op: RandomXValidationRequired() gates on algo, the activation height + // (regtest heights are far below it), HUSH_LOADINGBLOCKS (IBD/reindex), and the last + // in-index checkpoint, and the thread-local fSkipRandomXValidation still skips the miner self-check. + if ( !CheckRandomXSolution(pblock, height) ) + { + fprintf(stderr,"hush_checkPOW slowflag.%d ht.%d CheckRandomXSolution failed (post height-recovery)\n",slowflag,height); + return(-1); + } } if ( (SMART_CHAIN_SYMBOL[0] != 0) && bhash > bnTarget ) { From da7acda6d898743c9884f56018265888ad558d43 Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 17:05:03 -0500 Subject: [PATCH 2/3] 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 ` (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 --- src/main.cpp | 57 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index db0af063b..1b03f613f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 ` + // (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. From ae7e085144d238c0860d35001488bb0fe6e539ca Mon Sep 17 00:00:00 2001 From: DanS Date: Thu, 29 Oct 2026 01:25:37 -0500 Subject: [PATCH 3/3] gtest: regression guard for the hush_checkPOW height-0 RandomX re-check A forged-RandomX block reaches hush_checkPOW(0,pblock,0) over the P2P accept path; the first CheckRandomXSolution runs at height 0 (skipped), the height is recovered from hashPrevBlock, and fix (a) re-runs CheckRandomXSolution at the real height. The test drives the same forged block through hush_checkPOW twice: with the parent unknown the height stays 0 and the block is not rejected here (the pre-fix outcome), and with the parent known at height 9 the recovered height makes the fix reject it. The A/B contrast fails if the re-check is reverted. Also asserts CheckRandomXSolution is skipped at height 0 and rejects the forgery at the real height. Co-Authored-By: Claude Opus 4.8 --- src/Makefile.gtest.include | 1 + src/gtest/test_randomx_heightrecover.cpp | 116 +++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/gtest/test_randomx_heightrecover.cpp diff --git a/src/Makefile.gtest.include b/src/Makefile.gtest.include index eb985f328..6fc713999 100644 --- a/src/Makefile.gtest.include +++ b/src/Makefile.gtest.include @@ -12,6 +12,7 @@ hush_gtest_SOURCES = \ gtest/main.cpp \ gtest/utils.cpp \ gtest/test_randomx_preverify.cpp \ + gtest/test_randomx_heightrecover.cpp \ gtest/test_hdtransparent.cpp \ gtest/test_mnemonic_compat.cpp \ gtest/test_stratum_jobid.cpp \ diff --git a/src/gtest/test_randomx_heightrecover.cpp b/src/gtest/test_randomx_heightrecover.cpp new file mode 100644 index 000000000..03e3a7fb5 --- /dev/null +++ b/src/gtest/test_randomx_heightrecover.cpp @@ -0,0 +1,116 @@ +// Copyright (c) 2024-2026 The DragonX developers +// Distributed under the GPLv3 software license, see the accompanying +// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html +// +// Regression guard for the hush_checkPOW height-0 RandomX fix. A block arriving over P2P reaches +// hush_checkPOW with height==0; the first CheckRandomXSolution runs at height 0 (where +// RandomXValidationRequired is false, so it is skipped), the real height is then recovered from +// hashPrevBlock, and the fix re-runs CheckRandomXSolution at that height. Without the fix a block +// with a forged RandomX solution would be accepted on that path. We prove: (1) CheckRandomXSolution +// is skipped at height 0 but rejects the forged solution at the real height; (2) hush_checkPOW lets +// the forged block through only while the parent is unknown (height cannot be recovered), and +// rejects it once the parent is known and the height recovers -- which is exactly what the fix does. + +#include +#include +#include + +#include "arith_uint256.h" +#include "chain.h" +#include "chainparams.h" +#include "chainparamsbase.h" +#include "pow.h" +#include "primitives/block.h" +#include "primitives/transaction.h" +#include "RandomX/src/randomx.h" +#include "hush_defs.h" +#include "main.h" +#include "sync.h" +#include "uint256.h" + +extern int32_t HUSH_LOADINGBLOCKS; +extern bool fCheckpointsEnabled; +int32_t hush_checkPOW(int32_t slowflag, CBlock *pblock, int32_t height); + +TEST(RandomXHeightRecover, ForgedSolutionRejectedOnceHeightIsRecovered) +{ + // hush_checkPOW calls Params(); give it a selected chain. Done before the ASSETCHAINS_* globals + // below so params init cannot clobber them. RandomX is enforced via ASSETCHAINS_RANDOMX_VALIDATION, + // not the chain's activation height, so MAIN's high activation height is irrelevant here. + SelectParams(CBaseChainParams::MAIN); + + // Save the globals this test flips. + uint32_t sAlgo = ASSETCHAINS_ALGO, sRx = ASSETCHAINS_RANDOMX; + int32_t sVal = ASSETCHAINS_RANDOMX_VALIDATION, sLoad = HUSH_LOADINGBLOCKS; + bool sCkpt = fCheckpointsEnabled; + char sSym[sizeof(SMART_CHAIN_SYMBOL)]; + std::memcpy(sSym, SMART_CHAIN_SYMBOL, sizeof sSym); + + ASSETCHAINS_RANDOMX = 2; // a distinct nonzero algo id + ASSETCHAINS_ALGO = ASSETCHAINS_RANDOMX; + ASSETCHAINS_RANDOMX_VALIDATION = 1; // enforce from height 1 + HUSH_LOADINGBLOCKS = 0; // not initial-load (else RandomX skipped) + fCheckpointsEnabled = false; // avoid the below-checkpoint skip + std::strcpy(SMART_CHAIN_SYMBOL, "DRGXTEST"); // non-empty: not the default regtest chain + + // A block with a FORGED RandomX solution (all zero bytes -- overwhelmingly not the real hash). + CBlock block; + block.nVersion = 4; + block.hashPrevBlock = uint256S("0x0000000000000000000000000000000000000000000000000000000000000011"); + block.hashMerkleRoot = uint256S("0x0000000000000000000000000000000000000000000000000000000000000022"); + block.hashFinalSaplingRoot = uint256S("0x0000000000000000000000000000000000000000000000000000000000000033"); + block.nTime = 1700000000; + block.nBits = 0x200f0f0f; + block.nNonce = uint256S("0x0000000000000000000000000000000000000000000000000000000000000001"); + block.nSolution.assign(RANDOMX_HASH_SIZE, 0); + + // hush_block2pubkey33 reads vtx[0]; give the block a minimal coinbase so it is never a null deref. + CMutableTransaction cb; + cb.vin.resize(1); + cb.vin[0].prevout.SetNull(); + cb.vout.resize(1); + block.vtx.push_back(CTransaction(cb)); + + // Grind the nonce so the block's SHA target check passes. Then the ONLY thing that can reject it + // in hush_checkPOW at the recovered height is the RandomX re-check, so the test isolates the fix + // (without the fix the block would fall through the target check and be accepted). + arith_uint256 bnTarget; + bool neg, over; + bnTarget.SetCompact(block.nBits, &neg, &over); + for (uint32_t n = 1; n < 100000; n++) { + block.nNonce = ArithToUint256(arith_uint256(n)); + if (UintToArith256(block.GetHash()) <= bnTarget) break; + } + ASSERT_LE(UintToArith256(block.GetHash()), bnTarget) << "could not grind a passing SHA target"; + + // (1) The primitive the fix relies on: skipped at height 0, rejects the forgery at the real height. + EXPECT_TRUE(CheckRandomXSolution(&block, 0)) << "RandomX must be skipped at height 0"; + EXPECT_FALSE(CheckRandomXSolution(&block, 10)) << "a forged solution must be rejected at its real height"; + + // (2a) Parent unknown: hush_checkPOW cannot recover the height, so it stays 0 and the block is NOT + // rejected here -- the pre-fix behavior for an unrecoverable height. + EXPECT_GE(hush_checkPOW(0, &block, 0), 0) + << "with no recoverable height the forged block is not rejected on this path"; + + // (2b) Parent known at height 9: the height recovers to 10 and the fix re-checks RandomX -> rejected. + CBlockIndex prev; + prev.SetHeight(9); + uint256 prevHash = block.hashPrevBlock; + prev.phashBlock = &prevHash; + { + LOCK(cs_main); + mapBlockIndex[prevHash] = &prev; + } + EXPECT_LT(hush_checkPOW(0, &block, 0), 0) + << "once the height recovers, the fix must reject the forged RandomX solution"; + { + LOCK(cs_main); + mapBlockIndex.erase(prevHash); + } + + // Restore. + ASSETCHAINS_ALGO = sAlgo; ASSETCHAINS_RANDOMX = sRx; + ASSETCHAINS_RANDOMX_VALIDATION = sVal; HUSH_LOADINGBLOCKS = sLoad; + fCheckpointsEnabled = sCkpt; + std::memcpy(SMART_CHAIN_SYMBOL, sSym, sizeof sSym); +}