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");