2022-12-24 23:49:50 +00:00
// Copyright (c) 2021-2022 The Bitcoin Core developers
2021-04-28 06:35:46 +00:00
// 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/client.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 <tinyformat.h>
2023-11-07 17:46:41 +01:00
# include <uint256.h>
2021-04-28 06:35:46 +00:00
# include <univalue.h>
# include <util/strencodings.h>
# include <util/string.h>
# include <util/time.h>
2023-11-07 17:46:41 +01:00
# include <algorithm>
# include <cassert>
2021-04-28 06:35:46 +00:00
# include <cstdint>
2023-11-07 17:46:41 +01:00
# include <cstdlib>
# include <exception>
2021-04-28 06:35:46 +00:00
# include <iostream>
# include <memory>
# include <optional>
# include <stdexcept>
# include <vector>
2023-11-07 17:46:41 +01:00
enum class ChainType ;
2021-04-28 06:35:46 +00:00
namespace {
struct RPCFuzzTestingSetup : public TestingSetup {
2023-04-17 22:20:59 +02:00
RPCFuzzTestingSetup ( const ChainType chain_type , const std : : vector < const char * > & extra_args ) : TestingSetup { chain_type , extra_args }
2021-04-28 06:35:46 +00:00
{
}
2021-12-02 19:43:52 +01:00
void CallRPC ( const std : : string & rpc_method , const std : : vector < std : : string > & arguments )
2021-04-28 06:35:46 +00:00
{
JSONRPCRequest request ;
request . context = & m_node ;
request . strMethod = rpc_method ;
2021-12-02 19:43:52 +01:00
try {
request . params = RPCConvertValues ( rpc_method , arguments ) ;
} catch ( const std : : runtime_error & ) {
return ;
}
tableRPC . execute ( request ) ;
2021-04-28 06:35:46 +00:00
}
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
" dumpwallet " , // avoid writing to disk
2023-06-22 11:49:28 +02:00
" enumeratesigners " ,
2021-04-28 09:29:35 +00:00
" echoipc " , // avoid assertion failure (Assertion `"EnsureAnyNodeContext(request.context).init" && check' failed.)
" generatetoaddress " , // avoid prohibitively slow execution (when `num_blocks` is large)
" generatetodescriptor " , // avoid prohibitively slow execution (when `nblocks` is large)
" gettxoutproof " , // avoid prohibitively slow execution
2023-04-13 13:13:18 +02:00
" importmempool " , // avoid reading from disk
2021-04-28 06:35:46 +00:00
" importwallet " , // avoid reading from disk
2019-03-29 15:31:54 -04:00
" loadtxoutset " , // avoid reading from disk
2021-04-28 06:35:46 +00:00
" loadwallet " , // avoid reading from disk
2021-04-28 09:29:35 +00:00
" savemempool " , // disabled as a precautionary measure: may take a file path argument in the future
2021-04-28 06:35:46 +00:00
" 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 {
2023-04-13 20:36:25 +02:00
" analyzepsbt " ,
2021-04-28 06:35:46 +00:00
" clearbanned " ,
" combinepsbt " ,
" combinerawtransaction " ,
" converttopsbt " ,
" createmultisig " ,
" createpsbt " ,
" createrawtransaction " ,
" decodepsbt " ,
" decoderawtransaction " ,
" decodescript " ,
" deriveaddresses " ,
2022-08-03 19:32:59 -04:00
" descriptorprocesspsbt " ,
2021-04-28 06:35:46 +00:00
" disconnectnode " ,
" echo " ,
" echojson " ,
" estimaterawfee " ,
" estimatesmartfee " ,
" finalizepsbt " ,
" generate " ,
" generateblock " ,
" getaddednodeinfo " ,
2023-01-29 18:59:00 +05:30
" getaddrmaninfo " ,
2021-04-28 06:35:46 +00:00
" getbestblockhash " ,
" getblock " ,
" getblockchaininfo " ,
" getblockcount " ,
" getblockfilter " ,
2023-04-13 20:36:25 +02:00
" getblockfrompeer " , // when no peers are connected, no p2p message is sent
2021-04-28 06:35:46 +00:00
" getblockhash " ,
" getblockheader " ,
" getblockstats " ,
" getblocktemplate " ,
" getchaintips " ,
2019-03-29 17:55:08 -04:00
" getchainstates " ,
2021-04-28 06:35:46 +00:00
" getchaintxstats " ,
" getconnectioncount " ,
2021-12-01 17:46:21 +10:00
" getdeploymentinfo " ,
2021-04-28 06:35:46 +00:00
" getdescriptorinfo " ,
" getdifficulty " ,
" getindexinfo " ,
" getmemoryinfo " ,
" getmempoolancestors " ,
" getmempooldescendants " ,
" getmempoolentry " ,
" getmempoolinfo " ,
" getmininginfo " ,
" getnettotals " ,
" getnetworkhashps " ,
" getnetworkinfo " ,
" getnodeaddresses " ,
" getpeerinfo " ,
2023-04-16 12:23:05 +01:00
" getprioritisedtransactions " ,
rpc: getrawaddrman for addrman entries
Exposing address manager table entries in a hidden RPC allows to introspect
addrman tables in tests and during development.
As response JSON object the following FORMAT1 is choosen:
{
"table": {
"<bucket>/<position>": { "address": "..", "port": .., ... },
"<bucket>/<position>": { "address": "..", "port": .., ... },
"<bucket>/<position>": { "address": "..", "port": .., ... },
...
}
}
An alternative would be FORMAT2
{
"table": {
"bucket": {
"position": { "address": "..", "port": .., ... },
"position": { "address": "..", "port": .., ... },
..
},
"bucket": {
"position": { "address": "..", "port": .., ... },
..
},
}
}
FORMAT1 and FORMAT2 have different encodings for the location of the
address in the address manager. While FORMAT2 might be easier to process
for downstream tools, it also mimics internal addrman mappings, which
might change at some point. Users not interested in the address location
can ignore the location key. They don't have to adapt to a new RPC
response format, when the internal addrman layout changes. Additionally,
FORMAT1 is also slightly easier to to iterate in downstream tools. The
RPC response-building implemenation complexcity is lower with FORMAT1
as we can more easily build a "<bucket>/<position>" key than a multiple
"bucket" objects with multiple "position" objects (FORMAT2).
2023-09-20 19:37:45 +02:00
" getrawaddrman " ,
2021-04-28 06:35:46 +00:00
" getrawmempool " ,
" getrawtransaction " ,
" getrpcinfo " ,
" gettxout " ,
" gettxoutsetinfo " ,
2023-04-13 20:36:25 +02:00
" gettxspendingprevout " ,
2021-04-28 06:35:46 +00:00
" help " ,
" invalidateblock " ,
" joinpsbts " ,
" listbanned " ,
" logging " ,
2021-04-28 09:29:35 +00:00
" mockscheduler " ,
2021-04-28 06:35:46 +00:00
" ping " ,
" preciousblock " ,
2023-04-13 20:36:25 +02:00
" prioritisetransaction " ,
2021-04-28 06:35:46 +00:00
" pruneblockchain " ,
" reconsiderblock " ,
2021-11-19 13:22:29 -05:00
" scanblocks " ,
2021-04-28 06:35:46 +00:00
" scantxoutset " ,
2023-08-15 12:02:14 -04:00
" sendmsgtopeer " , // when no peers are connected, no p2p message is sent
2021-04-28 06:35:46 +00:00
" sendrawtransaction " ,
" setmocktime " ,
" setnetworkactive " ,
" signmessagewithprivkey " ,
" signrawtransactionwithkey " ,
" submitblock " ,
" submitheader " ,
2021-07-20 11:45:52 +01:00
" submitpackage " ,
2021-04-28 06:35:46 +00:00
" syncwithvalidationinterfacequeue " ,
" testmempoolaccept " ,
" uptime " ,
" utxoupdatepsbt " ,
" validateaddress " ,
" verifychain " ,
" verifymessage " ,
" verifytxoutproof " ,
" waitforblock " ,
" waitforblockheight " ,
" waitfornewblock " ,
} ;
2023-11-07 17:46:41 +01:00
std : : string ConsumeScalarRPCArgument ( FuzzedDataProvider & fuzzed_data_provider , bool & good_data )
2021-04-28 06:35:46 +00:00
{
const size_t max_string_length = 4096 ;
2021-05-11 21:27:37 +02:00
const size_t max_base58_bytes_length { 64 } ;
2021-04-28 06:35:46 +00:00
std : : string r ;
CallOneOf (
fuzzed_data_provider ,
[ & ] {
// string argument
r = fuzzed_data_provider . ConsumeRandomLengthString ( max_string_length ) ;
} ,
[ & ] {
// base64 argument
r = EncodeBase64 ( fuzzed_data_provider . ConsumeRandomLengthString ( max_string_length ) ) ;
} ,
[ & ] {
// hex argument
r = HexStr ( fuzzed_data_provider . ConsumeRandomLengthString ( max_string_length ) ) ;
} ,
[ & ] {
// bool argument
r = fuzzed_data_provider . ConsumeBool ( ) ? " true " : " false " ;
} ,
[ & ] {
// range argument
r = " [ " + ToString ( fuzzed_data_provider . ConsumeIntegral < int64_t > ( ) ) + " , " + ToString ( fuzzed_data_provider . ConsumeIntegral < int64_t > ( ) ) + " ] " ;
} ,
[ & ] {
// integral argument (int64_t)
r = ToString ( fuzzed_data_provider . ConsumeIntegral < int64_t > ( ) ) ;
} ,
[ & ] {
// integral argument (uint64_t)
r = ToString ( fuzzed_data_provider . ConsumeIntegral < uint64_t > ( ) ) ;
} ,
[ & ] {
// floating point argument
r = strprintf ( " %f " , fuzzed_data_provider . ConsumeFloatingPoint < double > ( ) ) ;
} ,
[ & ] {
// tx destination argument
r = EncodeDestination ( ConsumeTxDestination ( fuzzed_data_provider ) ) ;
} ,
[ & ] {
// uint160 argument
r = ConsumeUInt160 ( fuzzed_data_provider ) . ToString ( ) ;
} ,
[ & ] {
// uint256 argument
r = ConsumeUInt256 ( fuzzed_data_provider ) . ToString ( ) ;
} ,
[ & ] {
// base32 argument
r = EncodeBase32 ( fuzzed_data_provider . ConsumeRandomLengthString ( max_string_length ) ) ;
} ,
[ & ] {
// base58 argument
2021-05-11 21:27:37 +02:00
r = EncodeBase58 ( MakeUCharSpan ( fuzzed_data_provider . ConsumeRandomLengthString ( max_base58_bytes_length ) ) ) ;
2021-04-28 06:35:46 +00:00
} ,
[ & ] {
// base58 argument with checksum
2021-05-11 21:27:37 +02:00
r = EncodeBase58Check ( MakeUCharSpan ( fuzzed_data_provider . ConsumeRandomLengthString ( max_base58_bytes_length ) ) ) ;
2021-04-28 06:35:46 +00:00
} ,
[ & ] {
// hex encoded block
2023-09-07 19:16:57 +10:00
std : : optional < CBlock > opt_block = ConsumeDeserializable < CBlock > ( fuzzed_data_provider , TX_WITH_WITNESS ) ;
2021-04-28 06:35:46 +00:00
if ( ! opt_block ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
2023-09-11 18:06:51 +02:00
DataStream data_stream { } ;
2023-09-07 19:16:57 +10:00
data_stream < < TX_WITH_WITNESS ( * opt_block ) ;
2021-04-28 06:35:46 +00:00
r = HexStr ( data_stream ) ;
} ,
[ & ] {
// hex encoded block header
std : : optional < CBlockHeader > opt_block_header = ConsumeDeserializable < CBlockHeader > ( fuzzed_data_provider ) ;
if ( ! opt_block_header ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
2023-01-03 13:21:44 +01:00
DataStream data_stream { } ;
2021-04-28 06:35:46 +00:00
data_stream < < * opt_block_header ;
r = HexStr ( data_stream ) ;
} ,
[ & ] {
// hex encoded tx
2023-09-07 19:16:57 +10:00
std : : optional < CMutableTransaction > opt_tx = ConsumeDeserializable < CMutableTransaction > ( fuzzed_data_provider , TX_WITH_WITNESS ) ;
2021-04-28 06:35:46 +00:00
if ( ! opt_tx ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
2023-09-07 19:16:57 +10:00
DataStream data_stream ;
auto allow_witness = ( fuzzed_data_provider . ConsumeBool ( ) ? TX_WITH_WITNESS : TX_NO_WITNESS ) ;
data_stream < < allow_witness ( * opt_tx ) ;
2021-04-28 06:35:46 +00:00
r = HexStr ( data_stream ) ;
} ,
[ & ] {
// base64 encoded psbt
std : : optional < PartiallySignedTransaction > opt_psbt = ConsumeDeserializable < PartiallySignedTransaction > ( fuzzed_data_provider ) ;
if ( ! opt_psbt ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
2023-09-11 18:06:51 +02:00
DataStream data_stream { } ;
2021-04-28 06:35:46 +00:00
data_stream < < * opt_psbt ;
2022-01-02 11:31:25 +01:00
r = EncodeBase64 ( data_stream ) ;
2021-04-28 06:35:46 +00:00
} ,
[ & ] {
// base58 encoded key
2023-09-05 22:38:45 +02:00
CKey key = ConsumePrivateKey ( fuzzed_data_provider ) ;
2021-04-28 06:35:46 +00:00
if ( ! key . IsValid ( ) ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
r = EncodeSecret ( key ) ;
} ,
[ & ] {
// hex encoded pubkey
2023-09-05 22:38:45 +02:00
CKey key = ConsumePrivateKey ( fuzzed_data_provider ) ;
2021-04-28 06:35:46 +00:00
if ( ! key . IsValid ( ) ) {
2023-11-07 17:46:41 +01:00
good_data = false ;
2021-04-28 06:35:46 +00:00
return ;
}
r = HexStr ( key . GetPubKey ( ) ) ;
} ) ;
return r ;
}
2023-11-07 17:46:41 +01:00
std : : string ConsumeArrayRPCArgument ( FuzzedDataProvider & fuzzed_data_provider , bool & good_data )
2021-04-28 06:35:46 +00:00
{
std : : vector < std : : string > scalar_arguments ;
2023-11-07 17:46:41 +01:00
LIMITED_WHILE ( good_data & & fuzzed_data_provider . ConsumeBool ( ) , 100 )
{
scalar_arguments . push_back ( ConsumeScalarRPCArgument ( fuzzed_data_provider , good_data ) ) ;
2021-04-28 06:35:46 +00:00
}
return " [ \" " + Join ( scalar_arguments , " \" , \" " ) + " \" ] " ;
}
2023-11-07 17:46:41 +01:00
std : : string ConsumeRPCArgument ( FuzzedDataProvider & fuzzed_data_provider , bool & good_data )
2021-04-28 06:35:46 +00:00
{
2023-11-07 17:46:41 +01:00
return fuzzed_data_provider . ConsumeBool ( ) ? ConsumeScalarRPCArgument ( fuzzed_data_provider , good_data ) : ConsumeArrayRPCArgument ( fuzzed_data_provider , good_data ) ;
2021-04-28 06:35:46 +00:00
}
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 } ;
}
}
2023-07-11 14:33:31 +02:00
FUZZ_TARGET ( rpc , . init = initialize_rpc )
2021-04-28 06:35:46 +00:00
{
FuzzedDataProvider fuzzed_data_provider { buffer . data ( ) , buffer . size ( ) } ;
2023-11-07 17:46:41 +01:00
bool good_data { true } ;
2021-04-28 06:35:46 +00:00
SetMockTime ( 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 ;
}
std : : vector < std : : string > arguments ;
2023-11-07 17:46:41 +01:00
LIMITED_WHILE ( good_data & & fuzzed_data_provider . ConsumeBool ( ) , 100 )
{
arguments . push_back ( ConsumeRPCArgument ( fuzzed_data_provider , good_data ) ) ;
2021-04-28 06:35:46 +00:00
}
try {
rpc_testing_setup - > CallRPC ( rpc_command , arguments ) ;
2021-12-02 19:43:52 +01:00
} catch ( const UniValue & json_rpc_error ) {
2023-05-09 09:25:50 +02:00
const std : : string error_msg { json_rpc_error . find_value ( " message " ) . get_str ( ) } ;
2023-10-03 16:26:30 +02:00
if ( error_msg . starts_with ( " Internal bug detected " ) ) {
2021-12-02 19:43:52 +01:00
// Only allow the intentional internal bug
assert ( error_msg . find ( " trigger_internal_bug " ) ! = std : : string : : npos ) ;
}
2021-04-28 06:35:46 +00:00
}
}