net: make address overlap impossible when relaying addresses #36954

pull vasild wants to merge 1 commits into bitcoin:master from vasild:tor_i2p_addr_overlap changing 4 files +51 −9
  1. vasild commented at 5:02 PM on November 23, 2026: contributor

    CService::GetKey() didn't include the network in the output. So in theory it would be possible for two IP2 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.

  2. net: make address overlap impossible when relaying addresses
    `CService::GetKey()` didn't include the network in the output. So in
    theory it would be possible for two IP2 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.
    f5a425dee3
  3. DrahtBot added the label P2P on Nov 23, 2026
  4. DrahtBot commented at 5:02 PM on November 23, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #35936 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35936.svg"></sub> (net: reject oversized locators before allocating by l0rinc)
    • #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-->

  5. vasild commented at 5:03 AM on November 24, 2026: contributor

    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.

  6. in src/netaddress.cpp:918 in f5a425dee3
     919 | +        case NET_I2P: key.push_back(4); break;
     920 | +        case NET_CJDNS: key.push_back(5); break;
     921 | +        case NET_INTERNAL: key.push_back(6); break;
     922 | +        case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     923 | +        case NET_MAX:        // m_net is never and should not be set to NET_MAX
     924 | +            AssertUnreachable();
    


    l0rinc commented at 9:53 AM on November 24, 2026:

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


    vasild commented at 10:01 AM on November 25, 2026:

    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 commented at 11:52 AM on November 25, 2026:

    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;
    }
    

    vasild commented at 10:51 AM on November 30, 2026:

    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.

Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 23:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me