/
githubmirror
/
bitcoin
Обзор
Документация
Войти
/
githubmirror
/
bitcoin
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/test/fuzz/rpc.cpp
420 строк
14 KB
MarcoFalke
fuzz: Rework rpc fuzz target
01 авг 2026, 17:40
01 авг 2026, 17:40
fa895bb
Код
Авторство
О чём код?
// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <base58.h> #include <key.h> #include <key_io.h> #include <primitives/block.h> #include <primitives/transaction.h> #include <psbt.h> #include <rpc/request.h> #include <rpc/server.h> #include <span.h> #include <streams.h> #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> #include <test/util/setup_common.h> #include <test/util/time.h> #include <tinyformat.h> #include <uint256.h> #include <univalue.h> #include <util/strencodings.h> #include <util/string.h> #include <util/time.h> #include <algorithm> #include <cassert> #include <cstdint> #include <cstdlib> #include <exception> #include <iostream> #include <memory> #include <optional> #include <stdexcept> #include <utility> #include <vector> enum class ChainType; namespace { struct RPCFuzzTestingSetup : public TestingSetup { RPCFuzzTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts} { } void CallRPC(const std::string& rpc_method, UniValue&& params) { JSONRPCRequest request; request.context = &m_node; request.strMethod = rpc_method; request.params = std::move(params); tableRPC.execute(request); } std::vector<std::string> GetRPCCommands() const { return tableRPC.listCommands(); } }; RPCFuzzTestingSetup* rpc_testing_setup = nullptr; std::string g_limit_to_rpc_command; // RPC commands which are not appropriate for fuzzing: such as RPC commands // reading or writing to a filename passed as an RPC parameter, RPC commands // resulting in network activity, etc. const std::vector<std::string> RPC_COMMANDS_NOT_SAFE_FOR_FUZZING{ "addconnection", // avoid DNS lookups "addnode", // avoid DNS lookups "addpeeraddress", // avoid DNS lookups "dumptxoutset", // avoid writing to disk "enumeratesigners", "echoipc", // avoid assertion failure (Assertion `"EnsureAnyNodeContext(request.context).init" && check' failed.) "exportasmap", // avoid writing to disk "generatetoaddress", // avoid prohibitively slow execution (when `num_blocks` is large) "generatetodescriptor", // avoid prohibitively slow execution (when `nblocks` is large) "gettxoutproof", // avoid prohibitively slow execution "importmempool", // avoid reading from disk "loadtxoutset", // avoid reading from disk "loadwallet", // avoid reading from disk "savemempool", // disabled as a precautionary measure: may take a file path argument in the future "setban", // avoid DNS lookups "stop", // avoid shutdown state }; // RPC commands which are safe for fuzzing. const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{ "abortprivatebroadcast", "analyzepsbt", "clearbanned", "combinepsbt", "combinerawtransaction", "converttopsbt", "createmultisig", "createpsbt", "createrawtransaction", "decodepsbt", "decoderawtransaction", "decodescript", "deriveaddresses", "descriptorprocesspsbt", "disconnectnode", "echo", "echojson", "estimaterawfee", "estimatesmartfee", "finalizepsbt", "generate", "generateblock", "getaddednodeinfo", "getaddrmaninfo", "getbestblockhash", "getblock", "getblockchaininfo", "getblockcount", "getblockfilter", "getblockfrompeer", // when no peers are connected, no p2p message is sent "getblockhash", "getblockheader", "getblockstats", "getblocktemplate", "getchaintips", "getchainstates", "getchaintxstats", "getconnectioncount", "getdeploymentinfo", "getdescriptoractivity", "getdescriptorinfo", "getdifficulty", "getindexinfo", "getmemoryinfo", "getmempoolancestors", "getmempooldescendants", "getmempoolentry", "getmempoolfeeratediagram", "getmempoolcluster", "getmempoolinfo", "getmininginfo", "getnettotals", "getnetworkhashps", "getnetworkinfo", "getnodeaddresses", "getopenrpcinfo", "getorphantxs", "getpeerinfo", "getprioritisedtransactions", "getprivatebroadcastinfo", "getrawaddrman", "getrawmempool", "getrawtransaction", "getrpcinfo", "gettxout", "gettxoutsetinfo", "gettxspendingprevout", "help", "invalidateblock", "joinpsbts", "listbanned", "logging", "mockscheduler", "ping", "preciousblock", "prioritisetransaction", "pruneblockchain", "reconsiderblock", "rpc.discover", "scanblocks", "scantxoutset", "sendmsgtopeer", // when no peers are connected, no p2p message is sent "sendrawtransaction", "setmocktime", "setnetworkactive", "signmessagewithprivkey", "signrawtransactionwithkey", "submitblock", "submitheader", "submitpackage", "syncwithvalidationinterfacequeue", "testmempoolaccept", "uptime", "utxoupdatepsbt", "validateaddress", "verifychain", "verifymessage", "verifytxoutproof", "waitforblock", "waitforblockheight", "waitfornewblock", }; UniValue ConsumeBasicRPCArgument(FuzzedDataProvider& fuzzed_data_provider, bool& good_data) { const size_t max_string_length = 4096; const size_t max_base58_bytes_length{64}; UniValue r{}; CallOneOf( fuzzed_data_provider, [&] { // arbitrary JSON argument if (!r.read(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))) { good_data = false; } }, [&] { // null argument r = UniValue{UniValue::VNULL}; }, [&] { // string argument r = UniValue{fuzzed_data_provider.ConsumeRandomLengthString(max_string_length)}; }, [&] { // base64 argument r = UniValue{EncodeBase64(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))}; }, [&] { // hex argument r = UniValue{HexStr(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))}; }, [&] { // bool argument r = UniValue{fuzzed_data_provider.ConsumeBool()}; }, [&] { // integral argument (int64_t) r = UniValue{fuzzed_data_provider.ConsumeIntegral<int64_t>()}; }, [&] { // integral argument (uint64_t) r = UniValue{fuzzed_data_provider.ConsumeIntegral<uint64_t>()}; }, [&] { // floating point argument r = UniValue{fuzzed_data_provider.ConsumeFloatingPoint<double>()}; }, [&] { // tx destination argument r = UniValue{EncodeDestination(ConsumeTxDestination(fuzzed_data_provider))}; }, [&] { // uint160 argument r = UniValue{ConsumeUInt160(fuzzed_data_provider).ToString()}; }, [&] { // uint256 argument r = UniValue{ConsumeUInt256(fuzzed_data_provider).ToString()}; }, [&] { // base32 argument r = UniValue{EncodeBase32(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))}; }, [&] { // base58 argument r = UniValue{EncodeBase58(MakeUCharSpan(fuzzed_data_provider.ConsumeRandomLengthString(max_base58_bytes_length)))}; }, [&] { // base58 argument with checksum r = UniValue{EncodeBase58Check(MakeUCharSpan(fuzzed_data_provider.ConsumeRandomLengthString(max_base58_bytes_length)))}; }, [&] { // hex encoded block std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS); if (!opt_block) { good_data = false; return; } DataStream data_stream{}; data_stream << TX_WITH_WITNESS(*opt_block); r = UniValue{HexStr(data_stream)}; }, [&] { // hex encoded block header std::optional<CBlockHeader> opt_block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider); if (!opt_block_header) { good_data = false; return; } DataStream data_stream{}; data_stream << *opt_block_header; r = UniValue{HexStr(data_stream)}; }, [&] { // hex encoded tx std::optional<CMutableTransaction> opt_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS); if (!opt_tx) { good_data = false; return; } DataStream data_stream; auto allow_witness = (fuzzed_data_provider.ConsumeBool() ? TX_WITH_WITNESS : TX_NO_WITNESS); data_stream << allow_witness(*opt_tx); r = UniValue{HexStr(data_stream)}; }, [&] { // base64 encoded psbt std::optional<PartiallySignedTransaction> opt_psbt = ConsumeDeserializableConstructor<PartiallySignedTransaction>(fuzzed_data_provider); if (!opt_psbt) { good_data = false; return; } DataStream data_stream{}; data_stream << *opt_psbt; r = UniValue{EncodeBase64(data_stream)}; }, [&] { // base58 encoded key CKey key = ConsumePrivateKey(fuzzed_data_provider); if (!key.IsValid()) { good_data = false; return; } r = UniValue{EncodeSecret(key)}; }, [&] { // hex encoded pubkey CKey key = ConsumePrivateKey(fuzzed_data_provider); if (!key.IsValid()) { good_data = false; return; } r = UniValue{HexStr(key.GetPubKey())}; }); return r; } constexpr int MAX_RPC_ARGUMENT_NESTING{9}; // NOLINTBEGIN(misc-no-recursion) UniValue ConsumeRPCArgument(FuzzedDataProvider& fuzzed_data_provider, bool& good_data, int nesting_depth) { if (nesting_depth == 0) { return ConsumeBasicRPCArgument(fuzzed_data_provider, good_data); } UniValue argument{}; std::vector<std::function<void()>> mks{ [&] { argument = ConsumeBasicRPCArgument(fuzzed_data_provider, good_data); }, [&] { argument = UniValue(UniValue::VARR); LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 100) { argument.push_back(ConsumeRPCArgument(fuzzed_data_provider, good_data, nesting_depth - 1)); } }, [&] { argument = UniValue(UniValue::VOBJ); LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 100) { argument.pushKV(fuzzed_data_provider.ConsumeRandomLengthString(128), ConsumeRPCArgument(fuzzed_data_provider, good_data, nesting_depth - 1)); } }, }; PickValue(fuzzed_data_provider, mks)(); return argument; } // NOLINTEND(misc-no-recursion) RPCFuzzTestingSetup* InitializeRPCFuzzTestingSetup() { static const auto setup = MakeNoLogFileContext<RPCFuzzTestingSetup>(); SetRPCWarmupFinished(); return setup.get(); } }; // namespace void initialize_rpc() { rpc_testing_setup = InitializeRPCFuzzTestingSetup(); const std::vector<std::string> supported_rpc_commands = rpc_testing_setup->GetRPCCommands(); for (const std::string& rpc_command : supported_rpc_commands) { const bool safe_for_fuzzing = std::find(RPC_COMMANDS_SAFE_FOR_FUZZING.begin(), RPC_COMMANDS_SAFE_FOR_FUZZING.end(), rpc_command) != RPC_COMMANDS_SAFE_FOR_FUZZING.end(); const bool not_safe_for_fuzzing = std::find(RPC_COMMANDS_NOT_SAFE_FOR_FUZZING.begin(), RPC_COMMANDS_NOT_SAFE_FOR_FUZZING.end(), rpc_command) != RPC_COMMANDS_NOT_SAFE_FOR_FUZZING.end(); if (!(safe_for_fuzzing || not_safe_for_fuzzing)) { std::cerr << "Error: RPC command \"" << rpc_command << "\" not found in RPC_COMMANDS_SAFE_FOR_FUZZING or RPC_COMMANDS_NOT_SAFE_FOR_FUZZING. Please update " << __FILE__ << ".\n"; std::terminate(); } if (safe_for_fuzzing && not_safe_for_fuzzing) { std::cerr << "Error: RPC command \"" << rpc_command << "\" found in *both* RPC_COMMANDS_SAFE_FOR_FUZZING and RPC_COMMANDS_NOT_SAFE_FOR_FUZZING. Please update " << __FILE__ << ".\n"; std::terminate(); } } const char* limit_to_rpc_command_env = std::getenv("LIMIT_TO_RPC_COMMAND"); if (limit_to_rpc_command_env != nullptr) { g_limit_to_rpc_command = std::string{limit_to_rpc_command_env}; } } FUZZ_TARGET(rpc, .init = initialize_rpc) { SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; bool good_data{true}; FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)}; const std::string rpc_command = fuzzed_data_provider.ConsumeRandomLengthString(64); if (!g_limit_to_rpc_command.empty() && rpc_command != g_limit_to_rpc_command) { return; } const bool safe_for_fuzzing = std::find(RPC_COMMANDS_SAFE_FOR_FUZZING.begin(), RPC_COMMANDS_SAFE_FOR_FUZZING.end(), rpc_command) != RPC_COMMANDS_SAFE_FOR_FUZZING.end(); if (!safe_for_fuzzing) { return; } UniValue arguments(UniValue::VARR); LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 100) { arguments.push_back(ConsumeRPCArgument(fuzzed_data_provider, good_data, MAX_RPC_ARGUMENT_NESTING)); } try { std::optional<test_only_CheckFailuresAreExceptionsNotAborts> maybe_mock{}; if (rpc_command == "echo") { // Avoid aborting fuzzing for this specific test-only RPC with an // intentional trigger_internal_bug maybe_mock.emplace(); } rpc_testing_setup->CallRPC(rpc_command, std::move(arguments)); } catch (const UniValue& json_rpc_error) { const std::string error_msg{json_rpc_error.find_value("message").get_str()}; if (error_msg.starts_with("Internal bug detected")) { // Only allow the intentional internal bug assert(error_msg.find("trigger_internal_bug") != std::string::npos); } } }