net: make address overlap impossible when relaying addresses #36954

Open
vasild wants to merge 2 commits from tor_i2p_addr_overlap into master AGit
vasild commented 2026-11-23 17:02:04 +00:00 (Migrated from github.com)

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.

`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 commented 2026-11-23 17:02:15 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36954 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36954.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36954#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [0xB10C](https://github.com/bitcoin/bitcoin/pull/36954#issuecomment-7328861275) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37005](https://github.com/bitcoin/bitcoin/pull/37005) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37005.svg"></sub> (http: Preserve socket errors before unlocking by natanleung) * [#35936](https://github.com/bitcoin/bitcoin/pull/35936) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35936.svg"></sub> (net: reject oversized locators before allocating by l0rinc) * [#35027](https://github.com/bitcoin/bitcoin/pull/35027) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35027.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.

🔵 Suggestion · Network-inclusive GetKey() serializes the port twice

The network-inclusive key carries two redundant port bytes into the relay filter, although uniqueness and legacy addrman placement remain correct.

Location: src/netaddress.cpp:907 (head).

🔵 Suggestion · The default GetKey() obscures the bucketing-key choice

Current bucketing is correct, but default GetKey() now selects the relay format even in addrman tests, obscuring which key each caller intends.

Location: src/netaddress.h:564 (head).


Reviewed: 6b6bf6fda777 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e1a29e5e677c47d383756f0fcf8f3760725c91d1104dd3ccb2f333b6217eba26:47880f5569fd2047d249266f337c4f2a509adf7d96c3ccb1c7644c2c2a650bff --> <!-- ralph-base:0bc1e88dd332476e143eaee3c74911f4ce91f0a1 --> <!-- ralph-head:6b6bf6fda7777ad430f73a9b07c9c58ebc366eea --> <!-- ralph-case:cc5356bbea6bedfd0302969d8831e2714e40e5e7735d55829344c1a15f7ba8b9 --> Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments. **🔵 Suggestion · Network-inclusive `GetKey()` serializes the port twice** The network-inclusive key carries two redundant port bytes into the relay filter, although uniqueness and legacy addrman placement remain correct. Location: [`src/netaddress.cpp:907`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/6b6bf6fda7777ad430f73a9b07c9c58ebc366eea/src/netaddress.cpp#L907>) (head). **🔵 Suggestion · The default `GetKey()` obscures the bucketing-key choice** Current bucketing is correct, but default `GetKey()` now selects the relay format even in addrman tests, obscuring which key each caller intends. Location: [`src/netaddress.h:564`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/6b6bf6fda7777ad430f73a9b07c9c58ebc366eea/src/netaddress.h#L564>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`6b6bf6fda777`](<https://git.fish.foo/bitcoin/bitcoin/commit/6b6bf6fda7777ad430f73a9b07c9c58ebc366eea>) · [Review report](<https://review.fish.foo/report/36954-6b6bf6fda7777ad430f73a9b07c9c58ebc366eea-3468-2.html>) · [Previous reports](<https://review.fish.foo/traces/36954/>) [permalink](<https://review.fish.foo/report/36954-6b6bf6fda7777ad430f73a9b07c9c58ebc366eea-3468-2.html>)
vasild commented 2026-11-24 05:03:13 +00:00 (Migrated from github.com)

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.

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.
l0rinc (Migrated from github.com) reviewed 2026-11-24 09:53:54 +00:00
l0rinc (Migrated from github.com) commented 2026-11-24 09:53:54 +00:00

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

shouldn't this rather be a `break` and an `AssertUnreachable()` after the switch for this to be guaranteed exhaustive?
vasild (Migrated from github.com) reviewed 2026-11-25 10:01:58 +00:00
vasild (Migrated from github.com) commented 2026-11-25 10:01:58 +00:00

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.:

github.com/bitcoin/bitcoin@edcac28380/src/netaddress.cpp (L487-L501)

and here just 1. is checked:

github.com/bitcoin/bitcoin@edcac28380/src/netaddress.cpp (L111-L136)

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.

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.: https://github.com/bitcoin/bitcoin/blob/edcac28380bbb8409988583b9f1ab32c41f52cac/src/netaddress.cpp#L487-L501 and here just 1. is checked: https://github.com/bitcoin/bitcoin/blob/edcac28380bbb8409988583b9f1ab32c41f52cac/src/netaddress.cpp#L111-L136 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.
maflcko (Migrated from github.com) reviewed 2026-11-25 11:52:53 +00:00
maflcko (Migrated from github.com) commented 2026-11-25 11:52:53 +00:00

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;
}
If you want to add the assert, it would be possible with a lambda, but up to you: ```cpp 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; } ```
vasild (Migrated from github.com) reviewed 2026-11-30 10:51:26 +00:00
vasild (Migrated from github.com) commented 2026-11-30 10:51:26 +00:00

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.

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 commented 2026-12-01 23:07:45 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
sipa (Migrated from github.com) reviewed 2026-12-02 15:42:19 +00:00
sipa (Migrated from github.com) commented 2026-12-02 15:42:19 +00:00

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.

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. ```c++ 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.
vasild (Migrated from github.com) reviewed 2026-12-03 08:08:23 +00:00
vasild (Migrated from github.com) commented 2026-12-03 08:08:23 +00:00

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

github.com/bitcoin/bitcoin@0bc1e88dd3/src/netaddress.h (L376-L390)

Simpler. Like. `NET_INTERNAL` works. No need to handle. https://github.com/bitcoin/bitcoin/blob/0bc1e88dd332476e143eaee3c74911f4ce91f0a1/src/netaddress.h#L376-L390
vasild commented 2026-12-03 09:55:35 +00:00 (Migrated from github.com)

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()).

`f5a425dee31eaa93aecbd8d5d7e459207f51937e...6b6bf6fda7777ad430f73a9b07c9c58ebc366eea`: rebase, take suggestion to [use bip155 serialization](https://github.com/bitcoin/bitcoin/pull/36954#discussion_r4772156192) 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()).
vasild (Migrated from github.com) reviewed 2026-12-03 09:56:07 +00:00
vasild (Migrated from github.com) commented 2026-12-03 09:56:07 +00:00

Done.

Done.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36954
No description provided.