net: stop sending every ADDR message once per address, and filter PushAddress on the right address

Two bugs from the general audit (origin/audit-fixes-20260901), both still on dev:

- SendMessages pushed the whole of vAddrToSend once for every address the peer did not know yet, so
  one getaddr reply of N addresses went out as N messages of N addresses each, all serialized and
  checksummed while cs_main is held. The locally built vAddr was never sent. Restore the upstream
  idiom: collect the unknown addresses, send them in MAX_ADDR_TO_SEND batches, then the rest once.
  The ADDR/ADDRV2 choice and the wire format are unchanged.
- CNode::PushAddress tested IsAddressKnown(addr) -- the CNode member holding this peer's own address
  -- instead of _addr, the address being queued. Once the peer's own address was in its addrKnown
  (routine: it advertises itself, or it comes back in a getaddr reply), every address queued for that
  peer was dropped until the daily addrKnown.reset(). Fixed together with the first bug, since it
  re-enables relay paths that would otherwise be amplified.

Regtest, two nodes over TLS, node A holding ~1000 addresses and B connecting and sending getaddr:
the old binary answered with 18 identical 235-byte addrv2 messages (5017 bytes to the peer); this
sends the same 235-byte message once (614 bytes). On mainnet a reply carries far more addresses,
and the waste grows with the square of that count. New gtest NetAddrRelay.PushAddressFiltersOnQueuedAddress
fails with the old filter.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-20 19:51:15 -06:00
co-authored by Claude Opus 5.5
parent 2ff5c5d541
commit dbe154c2dc
4 changed files with 56 additions and 17 deletions
+1
View File
@@ -20,6 +20,7 @@ hush_gtest_SOURCES = \
gtest/test_asyncrpc_cancel.cpp \
gtest/test_wallet_notedata.cpp \
gtest/test_net_closesocket.cpp \
gtest/test_net_addrrelay.cpp \
gtest/test_fd_defense.cpp \
gtest/test_wallet_dbenv.cpp \
gtest/test_wallet_notecache.cpp \
+33
View File
@@ -0,0 +1,33 @@
// Copyright (c) 2016-2024 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include <gtest/gtest.h>
#include "net.h"
#include "netbase.h"
// CNode::PushAddress must filter on the address being queued, not on the peer's own address (the
// CNode::addr member). The peer's own address routinely lands in its addrKnown (it advertises itself,
// or comes back in a getaddr reply); after that, filtering on the member dropped every address queued
// for that peer until the daily addrKnown.reset().
TEST(NetAddrRelay, PushAddressFiltersOnQueuedAddress) {
const CAddress peer(LookupNumeric("20.0.0.1", 21768));
const CAddress other(LookupNumeric("20.1.0.1", 21768));
const CAddress known(LookupNumeric("20.2.0.1", 21768));
ASSERT_TRUE(peer.IsValid() && other.IsValid() && known.IsValid());
CNode* node = new CNode(INVALID_SOCKET, peer, "", true); // inbound: sends no version message
ASSERT_TRUE(node->AddAddressIfNotAlreadyKnown(peer)); // the peer's own address is now known to it
node->PushAddress(other);
ASSERT_EQ(1u, node->vAddrToSend.size()) << "an unknown address was dropped because the peer's own address is known";
EXPECT_TRUE(node->vAddrToSend[0] == other);
// An address the peer already knows is still filtered out.
ASSERT_TRUE(node->AddAddressIfNotAlreadyKnown(known));
node->PushAddress(known);
EXPECT_EQ(1u, node->vAddrToSend.size()) << "an address the peer already knows was queued";
delete node;
}
+19 -16
View File
@@ -8412,6 +8412,20 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
// Message: addr
if (fSendTrickle)
{
// Collect the addresses the peer doesn't know yet into vAddr and send that, once
// (in MAX_ADDR_TO_SEND batches). This used to push the whole of vAddrToSend for every
// accepted address, so one getaddr reply went out as N messages of N addresses.
const char* msg_type;
int make_flags;
if (pto->m_wants_addrv2) {
msg_type = NetMsgType::ADDRV2;
make_flags = ADDRV2_FORMAT;
} else {
msg_type = NetMsgType::ADDR;
make_flags = 0;
}
const CNetMsgMaker msgMaker(std::min(pto->nVersion, PROTOCOL_VERSION));
vector<CAddress> vAddr;
vAddr.reserve(pto->vAddrToSend.size());
BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
@@ -8419,29 +8433,18 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
if (pto->AddAddressIfNotAlreadyKnown(addr))
{
vAddr.push_back(addr);
// receiver penalizes a message of more than MAX_ADDR_TO_SEND addresses
if (vAddr.size() >= MAX_ADDR_TO_SEND)
{
// Should be impossible since we always check size before adding to
// vAddrToSend. Recover by trimming the vector.
vAddr.resize(MAX_ADDR_TO_SEND);
pto->PushAddrMessage(msgMaker.Make(make_flags, msg_type, vAddr));
vAddr.clear();
}
const char* msg_type;
int make_flags;
if (pto->m_wants_addrv2) {
msg_type = NetMsgType::ADDRV2;
make_flags = ADDRV2_FORMAT;
} else {
msg_type = NetMsgType::ADDR;
make_flags = 0;
}
pto->PushAddrMessage(CNetMsgMaker(std::min(pto->nVersion, PROTOCOL_VERSION)).Make(make_flags, msg_type, pto->vAddrToSend));
}
}
pto->vAddrToSend.clear();
vAddr.clear();
if (!vAddr.empty())
pto->PushAddrMessage(msgMaker.Make(make_flags, msg_type, vAddr));
}
CNodeState &state = *State(pto->GetId());
+3 -1
View File
@@ -615,7 +615,9 @@ public:
// Known checking here is only to save space from duplicates.
// SendMessages will filter it again for knowns that were added
// after addresses were pushed.
if (_addr.IsValid() && !IsAddressKnown(addr) && addr_format_supported) {
// Test _addr, the address being queued: the member `addr` is this peer's own address, and
// once that was in addrKnown every address queued for the peer was dropped.
if (_addr.IsValid() && !IsAddressKnown(_addr) && addr_format_supported) {
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
vAddrToSend[insecure_rand() % vAddrToSend.size()] = _addr;