Skip to content

Commit fd1dae1

Browse files
tests: Add fuzzing harness for ProcessMessage(...)
1 parent 5518eee commit fd1dae1

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ FUZZ_TARGETS = \
5353
test/fuzz/partial_merkle_tree_deserialize \
5454
test/fuzz/partially_signed_transaction_deserialize \
5555
test/fuzz/prefilled_transaction_deserialize \
56+
test/fuzz/process_message \
5657
test/fuzz/psbt \
5758
test/fuzz/psbt_input_deserialize \
5859
test/fuzz/psbt_output_deserialize \
@@ -542,6 +543,12 @@ test_fuzz_prefilled_transaction_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
542543
test_fuzz_prefilled_transaction_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
543544
test_fuzz_prefilled_transaction_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
544545

546+
test_fuzz_process_message_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
547+
test_fuzz_process_message_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
548+
test_fuzz_process_message_LDADD = $(FUZZ_SUITE_LD_COMMON)
549+
test_fuzz_process_message_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
550+
test_fuzz_process_message_SOURCES = $(FUZZ_SUITE) test/fuzz/process_message.cpp
551+
545552
test_fuzz_psbt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
546553
test_fuzz_psbt_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
547554
test_fuzz_psbt_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set<uint256>& orphan_work_se
19081908
}
19091909
}
19101910

1911-
bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, BanMan* banman, const std::atomic<bool>& interruptMsgProc)
1911+
bool ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, BanMan* banman, const std::atomic<bool>& interruptMsgProc)
19121912
{
19131913
LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
19141914
if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)

src/test/fuzz/process_message.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <banman.h>
6+
#include <chainparams.h>
7+
#include <consensus/consensus.h>
8+
#include <net.h>
9+
#include <net_processing.h>
10+
#include <protocol.h>
11+
#include <scheduler.h>
12+
#include <script/script.h>
13+
#include <streams.h>
14+
#include <test/fuzz/FuzzedDataProvider.h>
15+
#include <test/fuzz/fuzz.h>
16+
#include <test/util/mining.h>
17+
#include <test/util/setup_common.h>
18+
#include <util/memory.h>
19+
#include <validationinterface.h>
20+
#include <version.h>
21+
22+
#include <algorithm>
23+
#include <atomic>
24+
#include <cassert>
25+
#include <chrono>
26+
#include <cstdint>
27+
#include <iosfwd>
28+
#include <iostream>
29+
#include <map>
30+
#include <memory>
31+
#include <set>
32+
#include <string>
33+
#include <vector>
34+
35+
bool ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, BanMan* banman, const std::atomic<bool>& interruptMsgProc);
36+
37+
namespace {
38+
39+
#ifdef MESSAGE_TYPE
40+
#define TO_STRING_(s) #s
41+
#define TO_STRING(s) TO_STRING_(s)
42+
const std::string LIMIT_TO_MESSAGE_TYPE{TO_STRING(MESSAGE_TYPE)};
43+
#else
44+
const std::string LIMIT_TO_MESSAGE_TYPE;
45+
#endif
46+
47+
const std::map<std::string, std::set<std::string>> EXPECTED_DESERIALIZATION_EXCEPTIONS = {
48+
{"CDataStream::read(): end of data: iostream error", {"addr", "block", "blocktxn", "cmpctblock", "feefilter", "filteradd", "filterload", "getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", "notfound", "ping", "sendcmpct", "tx"}},
49+
{"CompactSize exceeds limit of type: iostream error", {"cmpctblock"}},
50+
{"differential value overflow: iostream error", {"getblocktxn"}},
51+
{"index overflowed 16 bits: iostream error", {"getblocktxn"}},
52+
{"index overflowed 16-bits: iostream error", {"cmpctblock"}},
53+
{"indexes overflowed 16 bits: iostream error", {"getblocktxn"}},
54+
{"non-canonical ReadCompactSize(): iostream error", {"addr", "block", "blocktxn", "cmpctblock", "filteradd", "filterload", "getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", "notfound", "tx"}},
55+
{"ReadCompactSize(): size too large: iostream error", {"addr", "block", "blocktxn", "cmpctblock", "filteradd", "filterload", "getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", "notfound", "tx"}},
56+
{"Superfluous witness record: iostream error", {"block", "blocktxn", "cmpctblock", "tx"}},
57+
{"Unknown transaction optional data: iostream error", {"block", "blocktxn", "cmpctblock", "tx"}},
58+
};
59+
60+
const RegTestingSetup* g_setup;
61+
} // namespace
62+
63+
void initialize()
64+
{
65+
static RegTestingSetup setup{};
66+
g_setup = &setup;
67+
68+
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
69+
MineBlock(g_setup->m_node, CScript() << OP_TRUE);
70+
}
71+
SyncWithValidationInterfaceQueue();
72+
}
73+
74+
void test_one_input(const std::vector<uint8_t>& buffer)
75+
{
76+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
77+
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
78+
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
79+
return;
80+
}
81+
CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
82+
CNode p2p_node{0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, false};
83+
p2p_node.fSuccessfullyConnected = true;
84+
p2p_node.nVersion = PROTOCOL_VERSION;
85+
p2p_node.SetSendVersion(PROTOCOL_VERSION);
86+
g_setup->m_node.peer_logic->InitializeNode(&p2p_node);
87+
try {
88+
(void)ProcessMessage(&p2p_node, random_message_type, random_bytes_data_stream, GetTimeMillis(), Params(), g_setup->m_node.connman.get(), g_setup->m_node.banman.get(), std::atomic<bool>{false});
89+
} catch (const std::ios_base::failure& e) {
90+
const std::string exception_message{e.what()};
91+
const auto p = EXPECTED_DESERIALIZATION_EXCEPTIONS.find(exception_message);
92+
if (p == EXPECTED_DESERIALIZATION_EXCEPTIONS.cend() || p->second.count(random_message_type) == 0) {
93+
std::cout << "Unexpected exception when processing message type \"" << random_message_type << "\": " << exception_message << std::endl;
94+
assert(false);
95+
}
96+
}
97+
SyncWithValidationInterfaceQueue();
98+
}

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <init.h>
1414
#include <miner.h>
1515
#include <net.h>
16+
#include <net_processing.h>
1617
#include <noui.h>
1718
#include <pow.h>
1819
#include <rpc/blockchain.h>
@@ -62,7 +63,7 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
6263
}
6364

6465
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
65-
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
66+
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()}
6667
{
6768
fs::create_directories(m_path_root);
6869
gArgs.ForceSetArg("-datadir", m_path_root.string());
@@ -136,6 +137,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
136137
m_node.mempool->setSanityCheck(1.0);
137138
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
138139
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
140+
m_node.peer_logic = MakeUnique<PeerLogicValidation>(m_node.connman.get(), m_node.banman.get(), *m_node.scheduler);
139141
}
140142

141143
TestingSetup::~TestingSetup()

0 commit comments

Comments
 (0)