denialofservice_tests.cpp
1 // Copyright (c) 2011-2022 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 // Unit tests for denial-of-service detection/prevention code 6 7 #include <banman.h> 8 #include <chainparams.h> 9 #include <common/args.h> 10 #include <net.h> 11 #include <net_processing.h> 12 #include <pubkey.h> 13 #include <script/sign.h> 14 #include <script/signingprovider.h> 15 #include <serialize.h> 16 #include <test/util/net.h> 17 #include <test/util/random.h> 18 #include <test/util/setup_common.h> 19 #include <timedata.h> 20 #include <util/string.h> 21 #include <util/time.h> 22 #include <validation.h> 23 24 #include <array> 25 #include <stdint.h> 26 27 #include <boost/test/unit_test.hpp> 28 29 static CService ip(uint32_t i) 30 { 31 struct in_addr s; 32 s.s_addr = i; 33 return CService(CNetAddr(s), Params().GetDefaultPort()); 34 } 35 36 BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup) 37 38 // Test eviction of an outbound peer whose chain never advances 39 // Mock a node connection, and use mocktime to simulate a peer 40 // which never sends any headers messages. PeerLogic should 41 // decide to evict that outbound peer, after the appropriate timeouts. 42 // Note that we protect 4 outbound nodes from being subject to 43 // this logic; this test takes advantage of that protection only 44 // being applied to nodes which send headers with sufficient 45 // work. 46 BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) 47 { 48 LOCK(NetEventsInterface::g_msgproc_mutex); 49 50 ConnmanTestMsg& connman = static_cast<ConnmanTestMsg&>(*m_node.connman); 51 // Disable inactivity checks for this test to avoid interference 52 connman.SetPeerConnectTimeout(99999s); 53 PeerManager& peerman = *m_node.peerman; 54 55 // Mock an outbound peer 56 CAddress addr1(ip(0xa0b0c001), NODE_NONE); 57 NodeId id{0}; 58 CNode dummyNode1{id++, 59 /*sock=*/nullptr, 60 addr1, 61 /*nKeyedNetGroupIn=*/0, 62 /*nLocalHostNonceIn=*/0, 63 CAddress(), 64 /*addrNameIn=*/"", 65 ConnectionType::OUTBOUND_FULL_RELAY, 66 /*inbound_onion=*/false}; 67 68 connman.Handshake( 69 /*node=*/dummyNode1, 70 /*successfully_connected=*/true, 71 /*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS), 72 /*local_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS), 73 /*version=*/PROTOCOL_VERSION, 74 /*relay_txs=*/true); 75 TestOnlyResetTimeData(); 76 77 // This test requires that we have a chain with non-zero work. 78 { 79 LOCK(cs_main); 80 BOOST_CHECK(m_node.chainman->ActiveChain().Tip() != nullptr); 81 BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->nChainWork > 0); 82 } 83 84 // Test starts here 85 BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders 86 87 { 88 LOCK(dummyNode1.cs_vSend); 89 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false); 90 BOOST_CHECK(!to_send.empty()); 91 } 92 connman.FlushSendBuffer(dummyNode1); 93 94 int64_t nStartTime = GetTime(); 95 // Wait 21 minutes 96 SetMockTime(nStartTime+21*60); 97 BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders 98 { 99 LOCK(dummyNode1.cs_vSend); 100 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false); 101 BOOST_CHECK(!to_send.empty()); 102 } 103 // Wait 3 more minutes 104 SetMockTime(nStartTime+24*60); 105 BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in disconnect 106 BOOST_CHECK(dummyNode1.fDisconnect == true); 107 108 peerman.FinalizeNode(dummyNode1); 109 } 110 111 static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType, bool onion_peer = false) 112 { 113 CAddress addr; 114 115 if (onion_peer) { 116 auto tor_addr{g_insecure_rand_ctx.randbytes(ADDR_TORV3_SIZE)}; 117 BOOST_REQUIRE(addr.SetSpecial(OnionToString(tor_addr))); 118 } 119 120 while (!addr.IsRoutable()) { 121 addr = CAddress(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE); 122 } 123 124 vNodes.emplace_back(new CNode{id++, 125 /*sock=*/nullptr, 126 addr, 127 /*nKeyedNetGroupIn=*/0, 128 /*nLocalHostNonceIn=*/0, 129 CAddress(), 130 /*addrNameIn=*/"", 131 connType, 132 /*inbound_onion=*/false}); 133 CNode &node = *vNodes.back(); 134 node.SetCommonVersion(PROTOCOL_VERSION); 135 136 peerLogic.InitializeNode(node, ServiceFlags(NODE_NETWORK | NODE_WITNESS)); 137 node.fSuccessfullyConnected = true; 138 139 connman.AddTestNode(node); 140 } 141 142 BOOST_AUTO_TEST_CASE(stale_tip_peer_management) 143 { 144 NodeId id{0}; 145 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params()); 146 auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, {}); 147 148 constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS; 149 CConnman::Options options; 150 options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS; 151 152 const auto time_init{GetTime<std::chrono::seconds>()}; 153 SetMockTime(time_init); 154 const auto time_later{time_init + 3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s}; 155 connman->Init(options); 156 std::vector<CNode *> vNodes; 157 158 // Mock some outbound peers 159 for (int i = 0; i < max_outbound_full_relay; ++i) { 160 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY); 161 } 162 163 peerLogic->CheckForStaleTipAndEvictPeers(); 164 165 // No nodes should be marked for disconnection while we have no extra peers 166 for (const CNode *node : vNodes) { 167 BOOST_CHECK(node->fDisconnect == false); 168 } 169 170 SetMockTime(time_later); 171 172 // Now tip should definitely be stale, and we should look for an extra 173 // outbound peer 174 peerLogic->CheckForStaleTipAndEvictPeers(); 175 BOOST_CHECK(connman->GetTryNewOutboundPeer()); 176 177 // Still no peers should be marked for disconnection 178 for (const CNode *node : vNodes) { 179 BOOST_CHECK(node->fDisconnect == false); 180 } 181 182 // If we add one more peer, something should get marked for eviction 183 // on the next check (since we're mocking the time to be in the future, the 184 // required time connected check should be satisfied). 185 SetMockTime(time_init); 186 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY); 187 SetMockTime(time_later); 188 189 peerLogic->CheckForStaleTipAndEvictPeers(); 190 for (int i = 0; i < max_outbound_full_relay; ++i) { 191 BOOST_CHECK(vNodes[i]->fDisconnect == false); 192 } 193 // Last added node should get marked for eviction 194 BOOST_CHECK(vNodes.back()->fDisconnect == true); 195 196 vNodes.back()->fDisconnect = false; 197 198 // Update the last announced block time for the last 199 // peer, and check that the next newest node gets evicted. 200 peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime()); 201 202 peerLogic->CheckForStaleTipAndEvictPeers(); 203 for (int i = 0; i < max_outbound_full_relay - 1; ++i) { 204 BOOST_CHECK(vNodes[i]->fDisconnect == false); 205 } 206 BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect == true); 207 BOOST_CHECK(vNodes.back()->fDisconnect == false); 208 209 vNodes[max_outbound_full_relay - 1]->fDisconnect = false; 210 211 // Add an onion peer, that will be protected because it is the only one for 212 // its network, so another peer gets disconnected instead. 213 SetMockTime(time_init); 214 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY, /*onion_peer=*/true); 215 SetMockTime(time_later); 216 peerLogic->CheckForStaleTipAndEvictPeers(); 217 218 for (int i = 0; i < max_outbound_full_relay - 2; ++i) { 219 BOOST_CHECK(vNodes[i]->fDisconnect == false); 220 } 221 BOOST_CHECK(vNodes[max_outbound_full_relay - 2]->fDisconnect == false); 222 BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect == true); 223 BOOST_CHECK(vNodes[max_outbound_full_relay]->fDisconnect == false); 224 225 // Add a second onion peer which won't be protected 226 SetMockTime(time_init); 227 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY, /*onion_peer=*/true); 228 SetMockTime(time_later); 229 peerLogic->CheckForStaleTipAndEvictPeers(); 230 231 BOOST_CHECK(vNodes.back()->fDisconnect == true); 232 233 for (const CNode *node : vNodes) { 234 peerLogic->FinalizeNode(*node); 235 } 236 237 connman->ClearTestNodes(); 238 } 239 240 BOOST_AUTO_TEST_CASE(block_relay_only_eviction) 241 { 242 NodeId id{0}; 243 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params()); 244 auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, {}); 245 246 constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS}; 247 constexpr int64_t MINIMUM_CONNECT_TIME{30}; 248 CConnman::Options options; 249 options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS; 250 251 connman->Init(options); 252 std::vector<CNode*> vNodes; 253 254 // Add block-relay-only peers up to the limit 255 for (int i = 0; i < max_outbound_block_relay; ++i) { 256 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY); 257 } 258 peerLogic->CheckForStaleTipAndEvictPeers(); 259 260 for (int i = 0; i < max_outbound_block_relay; ++i) { 261 BOOST_CHECK(vNodes[i]->fDisconnect == false); 262 } 263 264 // Add an extra block-relay-only peer breaking the limit (mocks logic in ThreadOpenConnections) 265 AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY); 266 peerLogic->CheckForStaleTipAndEvictPeers(); 267 268 // The extra peer should only get marked for eviction after MINIMUM_CONNECT_TIME 269 for (int i = 0; i < max_outbound_block_relay; ++i) { 270 BOOST_CHECK(vNodes[i]->fDisconnect == false); 271 } 272 BOOST_CHECK(vNodes.back()->fDisconnect == false); 273 274 SetMockTime(GetTime() + MINIMUM_CONNECT_TIME + 1); 275 peerLogic->CheckForStaleTipAndEvictPeers(); 276 for (int i = 0; i < max_outbound_block_relay; ++i) { 277 BOOST_CHECK(vNodes[i]->fDisconnect == false); 278 } 279 BOOST_CHECK(vNodes.back()->fDisconnect == true); 280 281 // Update the last block time for the extra peer, 282 // and check that the next youngest peer gets evicted. 283 vNodes.back()->fDisconnect = false; 284 vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>(); 285 286 peerLogic->CheckForStaleTipAndEvictPeers(); 287 for (int i = 0; i < max_outbound_block_relay - 1; ++i) { 288 BOOST_CHECK(vNodes[i]->fDisconnect == false); 289 } 290 BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect == true); 291 BOOST_CHECK(vNodes.back()->fDisconnect == false); 292 293 for (const CNode* node : vNodes) { 294 peerLogic->FinalizeNode(*node); 295 } 296 connman->ClearTestNodes(); 297 } 298 299 BOOST_AUTO_TEST_CASE(peer_discouragement) 300 { 301 LOCK(NetEventsInterface::g_msgproc_mutex); 302 303 auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); 304 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params()); 305 auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, {}); 306 307 CNetAddr tor_netaddr; 308 BOOST_REQUIRE( 309 tor_netaddr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")); 310 const CService tor_service{tor_netaddr, Params().GetDefaultPort()}; 311 312 const std::array<CAddress, 3> addr{CAddress{ip(0xa0b0c001), NODE_NONE}, 313 CAddress{ip(0xa0b0c002), NODE_NONE}, 314 CAddress{tor_service, NODE_NONE}}; 315 316 const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[]. 317 318 std::array<CNode*, 3> nodes; 319 320 banman->ClearBanned(); 321 NodeId id{0}; 322 nodes[0] = new CNode{id++, 323 /*sock=*/nullptr, 324 addr[0], 325 /*nKeyedNetGroupIn=*/0, 326 /*nLocalHostNonceIn=*/0, 327 CAddress(), 328 /*addrNameIn=*/"", 329 ConnectionType::INBOUND, 330 /*inbound_onion=*/false}; 331 nodes[0]->SetCommonVersion(PROTOCOL_VERSION); 332 peerLogic->InitializeNode(*nodes[0], NODE_NETWORK); 333 nodes[0]->fSuccessfullyConnected = true; 334 connman->AddTestNode(*nodes[0]); 335 peerLogic->UnitTestMisbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD); // Should be discouraged 336 BOOST_CHECK(peerLogic->SendMessages(nodes[0])); 337 338 BOOST_CHECK(banman->IsDiscouraged(addr[0])); 339 BOOST_CHECK(nodes[0]->fDisconnect); 340 BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged 341 342 nodes[1] = new CNode{id++, 343 /*sock=*/nullptr, 344 addr[1], 345 /*nKeyedNetGroupIn=*/1, 346 /*nLocalHostNonceIn=*/1, 347 CAddress(), 348 /*addrNameIn=*/"", 349 ConnectionType::INBOUND, 350 /*inbound_onion=*/false}; 351 nodes[1]->SetCommonVersion(PROTOCOL_VERSION); 352 peerLogic->InitializeNode(*nodes[1], NODE_NETWORK); 353 nodes[1]->fSuccessfullyConnected = true; 354 connman->AddTestNode(*nodes[1]); 355 peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1); 356 BOOST_CHECK(peerLogic->SendMessages(nodes[1])); 357 // [0] is still discouraged/disconnected. 358 BOOST_CHECK(banman->IsDiscouraged(addr[0])); 359 BOOST_CHECK(nodes[0]->fDisconnect); 360 // [1] is not discouraged/disconnected yet. 361 BOOST_CHECK(!banman->IsDiscouraged(addr[1])); 362 BOOST_CHECK(!nodes[1]->fDisconnect); 363 peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), 1); // [1] reaches discouragement threshold 364 BOOST_CHECK(peerLogic->SendMessages(nodes[1])); 365 // Expect both [0] and [1] to be discouraged/disconnected now. 366 BOOST_CHECK(banman->IsDiscouraged(addr[0])); 367 BOOST_CHECK(nodes[0]->fDisconnect); 368 BOOST_CHECK(banman->IsDiscouraged(addr[1])); 369 BOOST_CHECK(nodes[1]->fDisconnect); 370 371 // Make sure non-IP peers are discouraged and disconnected properly. 372 373 nodes[2] = new CNode{id++, 374 /*sock=*/nullptr, 375 addr[2], 376 /*nKeyedNetGroupIn=*/1, 377 /*nLocalHostNonceIn=*/1, 378 CAddress(), 379 /*addrNameIn=*/"", 380 ConnectionType::OUTBOUND_FULL_RELAY, 381 /*inbound_onion=*/false}; 382 nodes[2]->SetCommonVersion(PROTOCOL_VERSION); 383 peerLogic->InitializeNode(*nodes[2], NODE_NETWORK); 384 nodes[2]->fSuccessfullyConnected = true; 385 connman->AddTestNode(*nodes[2]); 386 peerLogic->UnitTestMisbehaving(nodes[2]->GetId(), DISCOURAGEMENT_THRESHOLD); 387 BOOST_CHECK(peerLogic->SendMessages(nodes[2])); 388 BOOST_CHECK(banman->IsDiscouraged(addr[0])); 389 BOOST_CHECK(banman->IsDiscouraged(addr[1])); 390 BOOST_CHECK(banman->IsDiscouraged(addr[2])); 391 BOOST_CHECK(nodes[0]->fDisconnect); 392 BOOST_CHECK(nodes[1]->fDisconnect); 393 BOOST_CHECK(nodes[2]->fDisconnect); 394 395 for (CNode* node : nodes) { 396 peerLogic->FinalizeNode(*node); 397 } 398 connman->ClearTestNodes(); 399 } 400 401 BOOST_AUTO_TEST_CASE(DoS_bantime) 402 { 403 LOCK(NetEventsInterface::g_msgproc_mutex); 404 405 auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); 406 auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params()); 407 auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, {}); 408 409 banman->ClearBanned(); 410 int64_t nStartTime = GetTime(); 411 SetMockTime(nStartTime); // Overrides future calls to GetTime() 412 413 CAddress addr(ip(0xa0b0c001), NODE_NONE); 414 NodeId id{0}; 415 CNode dummyNode{id++, 416 /*sock=*/nullptr, 417 addr, 418 /*nKeyedNetGroupIn=*/4, 419 /*nLocalHostNonceIn=*/4, 420 CAddress(), 421 /*addrNameIn=*/"", 422 ConnectionType::INBOUND, 423 /*inbound_onion=*/false}; 424 dummyNode.SetCommonVersion(PROTOCOL_VERSION); 425 peerLogic->InitializeNode(dummyNode, NODE_NETWORK); 426 dummyNode.fSuccessfullyConnected = true; 427 428 peerLogic->UnitTestMisbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD); 429 BOOST_CHECK(peerLogic->SendMessages(&dummyNode)); 430 BOOST_CHECK(banman->IsDiscouraged(addr)); 431 432 peerLogic->FinalizeNode(dummyNode); 433 } 434 435 BOOST_AUTO_TEST_SUITE_END()