From 353753190114399b9edf0e355a5b55cff31e25fc Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 20 Nov 2026 19:51:18 -0600 Subject: [PATCH] rpc: cap HTTP request headers at 8 KiB, and keep getblocktemplate from aborting on an error From the general audit, both still on dev: - libevent's max_headers_size defaults to unlimited, and the request line and headers are buffered before the -rpcallowip check or authentication runs, so one connection could grow the node's memory with every byte it sent. Capped at 8 KiB. Regtest: a 9 KiB header and a 10 KiB request line now get 400 (the old binary accepted the first and processed the second); normal calls are unaffected. qa/rpc-tests/httpbasics.py gains a request-line case. - getblocktemplate left cs_main with LEAVE_CRITICAL_SECTION around CreateNewBlockWithKey. If that threw (a wallet or BDB fault), the enclosing scoped lock's destructor unlocked an already unlocked mutex while unwinding, which aborts the node with nothing logged. It now re-enters cs_main first, so the error reaches the caller. Regtest: getblocktemplate returns templates as before. Co-Authored-By: Claude Opus 5.5 (1M context) --- qa/rpc-tests/httpbasics.py | 13 +++++++++++++ src/httpserver.cpp | 5 +++++ src/rpc/mining.cpp | 33 ++++++++++++++++++++++++++------- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/qa/rpc-tests/httpbasics.py b/qa/rpc-tests/httpbasics.py index f5a92efe9..8af8301f9 100755 --- a/qa/rpc-tests/httpbasics.py +++ b/qa/rpc-tests/httpbasics.py @@ -97,5 +97,18 @@ class HTTPBasicsTest (BitcoinTestFramework): assert_equal('"error":null' in out1, True) assert_equal(conn.sock!=None, True) # connection must be closed because hushd should use keep-alive by default + # Check excessive request size: request line + headers are capped at 8 KiB + conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port) + conn.connect() + conn.request('GET', '/' + ('x'*1000), '', headers) + out1 = conn.getresponse() + assert_equal(out1.status, httplib.NOT_FOUND) + + conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port) + conn.connect() + conn.request('GET', '/' + ('x'*10000), '', headers) + out1 = conn.getresponse() + assert_equal(out1.status, httplib.BAD_REQUEST) + if __name__ == '__main__': HTTPBasicsTest().main() diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 521b7b377..50a132897 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -420,6 +420,10 @@ static void libevent_log_cb(int severity, const char *msg) LogPrint("libevent", "libevent: %s\n", msg); } +/** Maximum size of an HTTP request line + headers. libevent's default is unbounded and it + * buffers all of it before the -rpcallowip ACL and auth checks run. Same value as upstream. */ +static const size_t MAX_HEADERS_SIZE = 8192; + bool InitHTTPServer() { struct evhttp* http = 0; @@ -466,6 +470,7 @@ bool InitHTTPServer() } evhttp_set_timeout(http, GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)); + evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); evhttp_set_max_body_size(http, MAX_SIZE); evhttp_set_gencb(http, http_request_cb, NULL); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 0d9f15674..75ffb4cc5 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -71,6 +71,22 @@ uint64_t the_commission(const CBlock *pblock,int32_t height); int32_t hush_blockload(CBlock& block,CBlockIndex *pindex); arith_uint256 hush_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc); +/** + * Releases cs_main for its scope and re-acquires it on exit, including when unwinding. + * For code already holding cs_main through a scoped LOCK(cs_main): if an exception escaped + * while cs_main was released by hand, that LOCK's destructor would unlock a mutex this thread + * no longer owns (BOOST_VERIFY -> abort). + */ +class CMainUnlockScope +{ +public: + CMainUnlockScope() { LEAVE_CRITICAL_SECTION(cs_main); } + ~CMainUnlockScope() { ENTER_CRITICAL_SECTION(cs_main); } +private: + CMainUnlockScope(const CMainUnlockScope&); + CMainUnlockScope& operator=(const CMainUnlockScope&); +}; + /** * Return average network hashes per second based on the last 'lookup' blocks, * or over the difficulty averaging window if 'lookup' is nonpositive. @@ -712,8 +728,8 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp } // Release the wallet and main lock while waiting - LEAVE_CRITICAL_SECTION(cs_main); { + CMainUnlockScope unlockMain; checktxtime = boost::get_system_time() + boost::posix_time::minutes(1); boost::unique_lock lock(csBestBlock); @@ -728,7 +744,6 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp } } } - ENTER_CRITICAL_SECTION(cs_main); if (!IsRPCRunning()) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down"); @@ -758,12 +773,16 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp } #ifdef ENABLE_WALLET CReserveKey reservekey(pwalletMain); - LEAVE_CRITICAL_SECTION(cs_main); - pblocktemplate = CreateNewBlockWithKey(reservekey,pindexPrevNew->GetHeight()+1,HUSH_MAXGPUCOUNT,false); -#else - pblocktemplate = CreateNewBlockWithKey(); #endif - ENTER_CRITICAL_SECTION(cs_main); + { + // CreateNewBlockWithKey can throw (keypool/wallet DB errors); cs_main must be held again by then. + CMainUnlockScope unlockMain; +#ifdef ENABLE_WALLET + pblocktemplate = CreateNewBlockWithKey(reservekey,pindexPrevNew->GetHeight()+1,HUSH_MAXGPUCOUNT,false); +#else + pblocktemplate = CreateNewBlockWithKey(); +#endif + } if (!pblocktemplate) throw std::runtime_error("CreateNewBlock(): create block failed"); //throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory or no available utxo for staking");