Branch data Line data Source code
1 : : // Copyright (c) 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 <net.h>
7 : : #include <net_processing.h>
8 : : #include <protocol.h>
9 : : #include <sync.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <test/fuzz/fuzz.h>
12 : : #include <test/fuzz/util.h>
13 : : #include <test/fuzz/util/net.h>
14 : : #include <test/util/net.h>
15 : : #include <test/util/setup_common.h>
16 : : #include <test/util/time.h>
17 : : #include <test/util/validation.h>
18 : : #include <util/check.h>
19 : : #include <util/time.h>
20 : : #include <validationinterface.h>
21 : :
22 : : #include <array>
23 : : #include <ios>
24 : : #include <memory>
25 : : #include <vector>
26 : :
27 : : namespace {
28 : : TestingSetup* g_setup;
29 : :
30 : 1 : void initialize()
31 : : {
32 : 1 : static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
33 [ + - + - : 1 : /*chain_type=*/ChainType::REGTEST);
+ - ]
34 : 1 : g_setup = testing_setup.get();
35 : 1 : }
36 : :
37 : : // Inbound message types with private broadcast specific handling.
38 : : // Used as the guided path in the CallOneOf() below.
39 : : constexpr std::array INBOUND_MSG_TYPES{
40 : : NetMsgType::VERSION,
41 : : NetMsgType::VERACK,
42 : : NetMsgType::GETDATA,
43 : : NetMsgType::PONG,
44 : : };
45 : : } // namespace
46 : :
47 [ + - ]: 3035 : FUZZ_TARGET(p2p_private_broadcast, .init = ::initialize)
48 : : {
49 : 2559 : SeedRandomStateForTest(SeedRand::ZEROS);
50 : 2559 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
51 : :
52 : 2559 : auto& node{g_setup->m_node};
53 : 2559 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
54 : 2559 : connman.Reset();
55 : 2559 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
56 : :
57 : 2559 : FakeNodeClock clock_ctx{1610000000s}; // 2021-01-07, arbitrary
58 [ + - ]: 2559 : FakeSteadyClock steady_clock;
59 [ + - ]: 2559 : chainman.ResetIbd();
60 : : // Sometimes leave IBD: incoming TX processing (the broadcast-abort path)
61 : : // returns early during IBD.
62 [ + + + - ]: 2559 : if (fuzzed_data_provider.ConsumeBool()) chainman.JumpOutOfIbd();
63 : :
64 : : // Reset, so that dangling pointers can be detected by sanitizers.
65 [ + + ]: 2559 : node.banman.reset();
66 [ + - ]: 2559 : node.addrman.reset();
67 [ + - ]: 2559 : node.peerman.reset();
68 : 5118 : node.addrman = std::make_unique<AddrMan>(
69 [ + - ]: 2559 : *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
70 : 5118 : node.peerman = PeerManager::make(connman, *node.addrman,
71 : : /*banman=*/nullptr, chainman,
72 [ + - ]: 2559 : *node.mempool, *node.warnings,
73 : : PeerManager::Options{
74 : : .reconcile_txs = true,
75 : : .deterministic_rng = true,
76 : 2559 : });
77 [ + - ]: 2559 : connman.SetMsgProc(node.peerman.get());
78 : 2559 : connman.SetAddrman(*node.addrman);
79 : :
80 : : // Seed with 0-3 transactions to test multiple pending broadcasts; zero exercises
81 : : // the connected-in-vain disconnect in PushPrivateBroadcastTx().
82 : 2559 : const int num_txs{fuzzed_data_provider.ConsumeIntegralInRange(0, 3)};
83 : 2559 : std::vector<CTransactionRef> seeded_txs;
84 [ + + ]: 5377 : for (int i = 0; i < num_txs; ++i) {
85 [ + - ]: 8454 : auto tx{MakeTransactionRef(ConsumeTransaction(fuzzed_data_provider, /*prevout_txids=*/std::nullopt))};
86 [ + - ]: 2818 : (void)node.peerman->InitiateTxBroadcastPrivate(tx);
87 [ + - ]: 2818 : seeded_txs.push_back(tx);
88 : 2818 : }
89 : :
90 [ + - ]: 2559 : LOCK(NetEventsInterface::g_msgproc_mutex);
91 : :
92 : 2559 : static NodeId node_id{0};
93 : : // Create at least one PRIVATE_BROADCAST peer, optionally add others of random types.
94 : 2559 : std::vector<CNode*> peers;
95 : :
96 : 2559 : CNode* pb_node = new CNode(
97 : : /*id=*/node_id++,
98 [ + - - + ]: 5118 : /*sock=*/std::make_shared<FuzzedSock>(fuzzed_data_provider, steady_clock),
99 [ + - ]: 5118 : /*addrIn=*/ConsumeAddress(fuzzed_data_provider),
100 : : /*nKeyedNetGroupIn=*/0,
101 : : /*nLocalHostNonceIn=*/0,
102 [ + - ]: 5118 : /*addrBindIn=*/CService{},
103 : 0 : /*addrNameIn=*/"",
104 : : /*conn_type_in=*/ConnectionType::PRIVATE_BROADCAST,
105 : : /*inbound_onion=*/false,
106 [ + - + - : 7677 : /*network_key=*/0);
+ - + - ]
107 : :
108 [ + - ]: 2559 : peers.push_back(pb_node);
109 [ + - ]: 2559 : connman.AddTestNode(*pb_node);
110 : : // Capture outbound messages to verify if well formed (and to learn the PING
111 : : // nonce), before SocketSendData drains vSendMsg.
112 [ + - ]: 2559 : connman.SetCaptureMessages(true);
113 [ + - ]: 2559 : const auto CaptureMessageOrig = CaptureMessage;
114 : 2559 : const CAddress pb_addr = pb_node->addr;
115 : 2559 : std::optional<uint64_t> pb_ping_nonce;
116 : 33638 : CaptureMessage = [&](const CAddress& addr, const std::string& msg_type,
117 : : std::span<const unsigned char> data, bool is_incoming) {
118 [ + - + + ]: 31079 : if (is_incoming || addr != pb_addr) return;
119 [ + + ]: 6986 : if (msg_type == NetMsgType::PING) {
120 : 485 : Assert(data.size() == sizeof(uint64_t));
121 : 485 : uint64_t nonce;
122 : 485 : SpanReader{data} >> nonce;
123 : 485 : pb_ping_nonce = nonce;
124 : 485 : return;
125 : : }
126 [ + + ]: 6501 : if (msg_type == NetMsgType::VERSION) {
127 : 2559 : SpanReader ds{data};
128 : 2559 : int32_t version;
129 : 2559 : uint64_t my_services, your_services, my_services_dup, nonce;
130 : 2559 : int64_t my_time;
131 [ + - ]: 2559 : CService your_addr, my_addr;
132 [ + - ]: 2559 : std::string user_agent;
133 : 2559 : int32_t height;
134 : 2559 : bool relay;
135 [ + - + - : 2559 : ds >> version >> my_services >> my_time >>
+ - + - ]
136 [ + - + - ]: 2559 : your_services >> CNetAddr::V1(your_addr) >>
137 [ + - + - ]: 2559 : my_services_dup >> CNetAddr::V1(my_addr) >>
138 [ + - + - : 2559 : nonce >> user_agent >> height >> relay;
+ - ]
139 [ + - ]: 2559 : Assert(version == WTXID_RELAY_VERSION);
140 [ + - - + : 2559 : Assert(my_services == NODE_NONE && my_services_dup == NODE_NONE);
+ - ]
141 [ + - ]: 2559 : Assert(my_time == 0);
142 [ + - ]: 2559 : Assert(your_services == NODE_NONE);
143 [ + - + - : 2559 : Assert(your_addr == CService{});
+ - ]
144 [ + - ]: 2559 : Assert(user_agent == "/pynode:0.0.1/");
145 [ + - ]: 2559 : Assert(height == 0);
146 [ + - ]: 2559 : Assert(!relay);
147 : 2559 : return;
148 : 2559 : }
149 [ + + ]: 3942 : if (msg_type != NetMsgType::INV) return;
150 : 1298 : SpanReader ds{data};
151 : 1298 : std::vector<CInv> invs;
152 [ + - ]: 1298 : ds >> invs;
153 [ - + + - ]: 1298 : Assert(invs.size() == 1);
154 [ + - ]: 1298 : Assert(invs[0].IsMsgTx());
155 : 3857 : };
156 : :
157 : : // Complete handshake so PushPrivateBroadcastTx runs.
158 [ + - ]: 2559 : connman.Handshake(
159 : : /*node=*/*pb_node,
160 : : /*successfully_connected=*/true,
161 : : /*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
162 : : /*local_services=*/NODE_NONE,
163 : : /*version=*/PROTOCOL_VERSION,
164 : : /*relay_txs=*/true);
165 : :
166 : : // Optionally add extra peers of random connection types.
167 : 2559 : const int extra_peers{fuzzed_data_provider.ConsumeIntegralInRange(0, 2)};
168 [ + + ]: 5010 : for (int i = 0; i < extra_peers; ++i) {
169 : 4901 : auto extra_peer{ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, node_id++)};
170 : : // An address collision would match the capture hook's filter and fail
171 : : // its assertions on this peer's (legitimate) other-typed messages.
172 [ + - + + ]: 2451 : if (extra_peer->addr == pb_addr) continue;
173 [ + - ]: 2450 : peers.push_back(extra_peer.release());
174 [ + - ]: 2450 : connman.AddTestNode(*peers.back());
175 : 2450 : node.peerman->InitializeNode(
176 [ + - ]: 2450 : *peers.back(),
177 : 2450 : static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
178 : 2451 : }
179 : :
180 [ + + + + ]: 48718 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
181 : : {
182 : : // Pick any random peer to test interleaved message handling.
183 : 46159 : CNode& p2p_node = *PickValue(fuzzed_data_provider, peers);
184 [ + + ]: 46159 : if (p2p_node.fDisconnect) continue;
185 : :
186 [ + - ]: 41235 : clock_ctx += ConsumeDuration<std::chrono::seconds>(fuzzed_data_provider, 0s, 600s);
187 : :
188 : 41235 : std::optional<CSerializedNetMsg> net_msg;
189 [ + - ]: 41235 : CallOneOf(
190 : : fuzzed_data_provider,
191 : 4796 : [&] {
192 : 4796 : net_msg.emplace();
193 : 4796 : net_msg->m_type = std::string{PickValue(fuzzed_data_provider, INBOUND_MSG_TYPES)};
194 : 4796 : },
195 : 20274 : [&] {
196 : 20274 : net_msg.emplace();
197 : 20274 : net_msg->m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
198 : 20274 : },
199 : 738 : [&] {
200 : 738 : (void)node.peerman->InitiateTxBroadcastPrivate(
201 [ + - + - ]: 2952 : MakeTransactionRef(ConsumeTransaction(fuzzed_data_provider, /*prevout_txids=*/std::nullopt)));
202 : 738 : },
203 : 5216 : [&] {
204 : : // Construct a valid GETDATA for a seeded tx to exercise the TX send path.
205 [ + + + + ]: 5216 : if (p2p_node.IsPrivateBroadcastConn() &&
206 [ + + + + ]: 5216 : p2p_node.fSuccessfullyConnected &&
207 [ + - ]: 613 : !seeded_txs.empty()) {
208 : 613 : const auto& tx{PickValue(fuzzed_data_provider, seeded_txs)};
209 [ + - ]: 1839 : net_msg.emplace(NetMsg::Make(
210 [ + - ]: 1226 : NetMsgType::GETDATA,
211 : 1226 : std::vector<CInv>{{MSG_TX, tx->GetHash().ToUint256()}}));
212 : : }
213 : 5216 : },
214 : 517 : [&] {
215 : : // Confirm reception of the pushed TX with a PONG matching the captured PING nonce.
216 [ + + + + ]: 517 : if (&p2p_node == pb_node && pb_ping_nonce) {
217 [ + - ]: 12 : net_msg.emplace(NetMsg::Make(NetMsgType::PONG, *pb_ping_nonce));
218 : : }
219 : 517 : },
220 : 9694 : [&] {
221 : : // Echo a seeded tx back from a non-private-broadcast peer to exercise
222 : : // the received-from-network broadcast-abort path.
223 [ + + + + ]: 9694 : if (!p2p_node.IsPrivateBroadcastConn() &&
224 [ + + + + ]: 9694 : p2p_node.fSuccessfullyConnected &&
225 [ + + ]: 4743 : !seeded_txs.empty()) {
226 : 4227 : const auto& tx{PickValue(fuzzed_data_provider, seeded_txs)};
227 [ + - ]: 8454 : net_msg.emplace(NetMsg::Make(NetMsgType::TX, TX_WITH_WITNESS(*tx)));
228 : : }
229 : 9694 : });
230 : :
231 [ + + ]: 41235 : if (net_msg) {
232 [ + + ]: 29916 : if (net_msg->data.empty()) {
233 : 25070 : net_msg->data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
234 : : }
235 [ + - ]: 29916 : connman.FlushSendBuffer(p2p_node);
236 : :
237 : : // ConsumeTransaction() can produce messages larger than the
238 : : // maximum payload accepted by the P2P transport.
239 [ - + - + : 29916 : if (net_msg->data.size() > MAX_PROTOCOL_MESSAGE_LENGTH) continue;
- - ]
240 : :
241 [ + - ]: 29916 : (void)connman.ReceiveMsgFrom(p2p_node, std::move(*net_msg));
242 : :
243 : : bool more_work{true};
244 [ + + ]: 62319 : while (more_work) {
245 [ + - ]: 32403 : p2p_node.fPauseSend = false;
246 : 32403 : try {
247 [ + - ]: 32403 : more_work = connman.ProcessMessagesOnce(p2p_node);
248 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
249 : 0 : }
250 [ + - ]: 32403 : node.peerman->SendMessages(p2p_node);
251 : : }
252 : : }
253 : 41235 : }
254 : :
255 [ + - ]: 2559 : CaptureMessage = CaptureMessageOrig;
256 [ + - ]: 2559 : connman.SetCaptureMessages(false);
257 : :
258 [ + - ]: 2559 : node.connman->StopNodes();
259 [ + - ]: 5118 : }
|