Skip to content

net: make address overlap impossible when relaying addresses - #36954

Open
vasild wants to merge 2 commits into
bitcoin:masterfrom
vasild:tor_i2p_addr_overlap
Open

vasild wants to merge 2 commits into
bitcoin:masterfrom
vasild:tor_i2p_addr_overlap

Conversation

@vasild

@vasild vasild commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

CService::GetKey() didn't include the network in the output. So in theory it would be possible for two I2P and Tor addresses to have the same output from CService::GetKey().

This would have unexpected results when used in the per-peer bloom filter of known addresses - we may wrongfully assume that a peer knows foo.onion when we have sent to them foo.i2p.

To make this impossible - include the network in CService::GetKey().

Addrman already includes the network separately so it does not need this change. The code would be simplest if addrman also uses GetKey() with the network but that would mess the positions of addresses in existent databases (peers.dat). So, keep a variant of network-less GetKey() for addrman.

Thanks, l0rinc for pointing me into this. I think it is minor but also the change is minor, so worth fixing IMO.

@DrahtBot DrahtBot added the P2P label Nov 23, 2026
@DrahtBot

DrahtBot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK 0xB10C

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37005 (http: Preserve socket errors before unlocking by natanleung)
  • #35936 (net: reject oversized locators before allocating by l0rinc)
  • #35027 (net: add -outboundbind option for outgoing source address by 8144225309)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@vasild

vasild commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Both IPv6 and CJDNS use 16 bytes, so there could be an overlap like with Tor and I2P. The first of those 16 bytes identifies the address as CJDNS. While such an address is also a valid IPv6 address, it is globally unroutable on the public internet.

Comment thread src/netaddress.cpp Outdated
case NET_INTERNAL: key.push_back(6); break;
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
case NET_MAX: // m_net is never and should not be set to NET_MAX
AssertUnreachable();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this rather be a break and an AssertUnreachable() after the switch for this to be guaranteed exhaustive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two things here: 1. values for m_net that should never be set and 2. ensure that the switch handles all enum values.

For example here there are two asserts to handle each one of 1. and 2.:

bitcoin/src/netaddress.cpp

Lines 487 to 501 in edcac28

switch (m_net) {
case NET_IPV4:
case NET_IPV6:
case NET_INTERNAL:
return true;
case NET_ONION:
case NET_I2P:
case NET_CJDNS:
return false;
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
case NET_MAX: // m_net is never and should not be set to NET_MAX
AssertUnreachable();
} // no default case, so the compiler can warn about missing cases
AssertUnreachable();

and here just 1. is checked:

bitcoin/src/netaddress.cpp

Lines 111 to 136 in edcac28

switch (ipIn.m_net) {
case NET_IPV4:
assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
break;
case NET_IPV6:
assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
break;
case NET_ONION:
assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
break;
case NET_I2P:
assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
break;
case NET_CJDNS:
assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
break;
case NET_INTERNAL:
assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
break;
case NET_UNROUTABLE:
case NET_MAX:
AssertUnreachable();
} // no default case, so the compiler can warn about missing cases
m_net = ipIn.m_net;
m_addr = ipIn.m_addr;

The new code I added is like in the second example, handling only 1. To handle 2. one must conclude every case with a return so that the assert after the switch becomes unreachable. This is omitted in places where some work has to be done after the switch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add the assert, it would be possible with a lambda, but up to you:

enum class Tabs {
    info,
    console,
};

int GetInt(Tabs tab)
{
    int ret = [&]() {
        switch (tab) {
        case Tabs::info: return 0;
        case Tabs::console: return 1;
        } // no default case, so the compiler can warn about missing cases
        AssertUnreachable(); 
    }();
    LogInfo("Tab %s", ret);
    return ret;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool pattern, I wasn't aware of it. I like it. The surrounding code does not use it though, so for now I will leave it as it is for now.

@0xB10C

0xB10C commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

Comment thread src/netaddress.cpp
{
std::vector<unsigned char> key;

if (include_network) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be simpler to reuse the BIP155 serialization here, if there is no requirement for compatibility with the old non-include_network format.

if (include_network) {
    VectorWriter writer(key, 0);
    writer << CNetAddr::V2(*this);
    return key;
}

This will assert(false); if used on a NET_INTERNAL address, so that may still need to be handled separately if that's a possibility.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler. Like. NET_INTERNAL works. No need to handle.

bitcoin/src/netaddress.h

Lines 376 to 390 in 0bc1e88

template <typename Stream>
void SerializeV2Stream(Stream& s) const
{
if (IsInternal()) {
// Serialize NET_INTERNAL as embedded in IPv6. We need to
// serialize such addresses from addrman.
s << static_cast<uint8_t>(BIP155Network::IPV6);
s << COMPACTSIZE(ADDR_IPV6_SIZE);
SerializeV1Stream(s);
return;
}
s << static_cast<uint8_t>(GetBIP155Network());
s << m_addr;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

The current version of `CService::GetKey()` is used for addrman
bucketing. While on-disk format of `peers.dat` does not depend on it,
loading the same `peers.dat` with different bucketing rules will
cause many (if not all) addresses to end up in a different bucket and/or
position in memory and likely colliding with another address and thus
being lost.

In some circumstances it may be ok to do that, but it must be justified
and definitely must not happen unintentionally. Thus, add tests that
would need to be adjusted if this happens.
`CService::GetKey()` didn't include the network in the output. So in
theory it would be possible for two I2P and Tor addresses to have the
same output from `CService::GetKey()`.

This would have unexpected results when used in the per-peer bloom
filter of known addresses - we may wrongfully assume that a peer knows
`foo.onion` when we have sent to them `foo.i2p`.

To make this impossible - include the network in `CService::GetKey()`.

Addrman already includes the network separately so it does not need this
change. The code would be simplest if addrman also uses `GetKey()` with
the network but that would change its in-memory bucketing, resuling in
some addresses being collided away on the first load with the new
bucketing scheme. So, keep a variant of network-less `GetKey()` for
addrman.
@vasild
vasild force-pushed the tor_i2p_addr_overlap branch from f5a425d to 6b6bf6f Compare December 3, 2026 09:47
@vasild

vasild commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

f5a425dee31eaa93aecbd8d5d7e459207f51937e...6b6bf6fda7777ad430f73a9b07c9c58ebc366eea: rebase, take suggestion to use bip155 serialization and add tests to ensure addrman bucketing rules do not change unintended due to GetKey() changes.

I considered including the network unconditionally in GetKey() to make the code simpler and accept addrman rebucketing. To assess the scale of the rebucketing I took a full addrman's peers.dat:

new table: 65512 addresses
tried table: 15259 addresses

after loading that file with unconditional-with-network-GetKey() the collisions resulted in:

new table: 42905 addresses
tried table: 9431 addresses

I assume that this is going to be more or less the same for every full addrman and better with addrmans that have less addresses. I think that is too much, so I leave the option to preserve the bucketing (have network-less-GetKey()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants