bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full #36675

Open
instagibbs wants to merge 1 commit from 2026-10-socks5-ipv6-expand into master AGit
instagibbs commented 2026-10-27 14:30:52 +00:00 (Migrated from github.com)

Found while auditing #36628

Already known upstream issue with C-tor: https://gitlab.torproject.org/tpo/core/tor/-/issues/32314

With -proxy pointed at Tor, outbound connections to IPv6 peers whose
address contains an inner :: always fail with "general failure".

Core sends every SOCKS5 destination as a DOMAINNAME, with IPv6 in its
compressed form. Tor copies the name into its relay BEGIN cell as
<name>:<port>, and the exit's tor_addr_port_split first tries to parse
the whole string as an IPv6 address. 2001:db8::1:8333 is a valid one,
because the :: absorbs the port as an eighth group. The exit then sees no
port and closes the stream. Addresses written with all eight groups, or
ending in ::, are unaffected: appending :port to them does not give a
valid address.

About 20% of the IPv6 addresses in a gossip-filled addrman, and 29% of
current IPv6 DNS seed answers, have an inner ::. The failure is closed;
there is no fallback to a direct connection.

Note that this also effects private broadcast by default.

This PR sends such addresses with all eight groups written out
(2001:db8:0:0:0:0:0:1). <name>:<port> then has nine groups, so the exit
splits at the last colon. Other forms:

Form Tor Tor, SafeSocks 1 getaddrinfo-based proxies
2001:db8::1 (today) fails fails works
[2001:db8::1] works works fails
ATYP IPv6 works, logs a DNS-leak warning refused works
eight groups (this PR) works works works

Tested against tor 0.4.9.13: on mainnet, master could not reach four
seed-provided peers with inner-:: addresses (30 "general failure"); with
this change all four connected and completed the version handshake.
feature_proxy.py and a new netbase_tests case check the bytes sent.

Found while auditing #36628 Already known upstream issue with C-tor: https://gitlab.torproject.org/tpo/core/tor/-/issues/32314 With `-proxy` pointed at Tor, outbound connections to IPv6 peers whose address contains an inner `::` always fail with "general failure". Core sends every SOCKS5 destination as a DOMAINNAME, with IPv6 in its compressed form. Tor copies the name into its relay BEGIN cell as `<name>:<port>`, and the exit's `tor_addr_port_split` first tries to parse the whole string as an IPv6 address. `2001:db8::1:8333` is a valid one, because the `::` absorbs the port as an eighth group. The exit then sees no port and closes the stream. Addresses written with all eight groups, or ending in `::`, are unaffected: appending `:port` to them does not give a valid address. About 20% of the IPv6 addresses in a gossip-filled addrman, and 29% of current IPv6 DNS seed answers, have an inner `::`. The failure is closed; there is no fallback to a direct connection. Note that this also effects private broadcast by default. This PR sends such addresses with all eight groups written out (`2001:db8:0:0:0:0:0:1`). `<name>:<port>` then has nine groups, so the exit splits at the last colon. Other forms: | Form | Tor | Tor, SafeSocks 1 | getaddrinfo-based proxies | |-----------------------|-------------|------------------|---------------------------| | `2001:db8::1` (today) | fails | fails | works | | `[2001:db8::1]` | works | works | fails | | ATYP IPv6 | works, logs a DNS-leak warning | refused | works | | eight groups (this PR)| works | works | works | Tested against tor 0.4.9.13: on mainnet, master could not reach four seed-provided peers with inner-`::` addresses (30 "general failure"); with this change all four connected and completed the version handshake. `feature_proxy.py` and a new `netbase_tests` case check the bytes sent.
DrahtBot commented 2026-10-27 14:31:03 +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
ACK andrewtoth, jpk68
Concept ACK darosior

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:

  • #36994 (net: reduce ASMap lookup and address grouping costs by l0rinc)

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/36675 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36675.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36675#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 | | ---- | --------- | | ACK | [andrewtoth](https://github.com/bitcoin/bitcoin/pull/36675#pullrequestreview-5804792947), [jpk68](https://github.com/bitcoin/bitcoin/pull/36675#issuecomment-6886890419) | | Concept ACK | [darosior](https://github.com/bitcoin/bitcoin/pull/36675#issuecomment-6445417783) | 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: * [#36994](https://github.com/bitcoin/bitcoin/pull/36994) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36994.svg"></sub> (net: reduce ASMap lookup and address grouping costs by l0rinc) 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-->
darosior (Migrated from github.com) reviewed 2026-10-27 14:46:30 +00:00
darosior (Migrated from github.com) left a comment

Workaround seems fine to do. But it's surprising the Tor daemon does not follow its own spec and format the IPv6 with brackets.

Workaround seems fine to do. But it's surprising the Tor daemon does not follow [its own spec](https://torspec-b24541.pages.torproject.net/tor-spec/opening-streams.html) and format the IPv6 with brackets.
instagibbs commented 2026-10-27 14:58:18 +00:00 (Migrated from github.com)
@darosior https://gitlab.torproject.org/tpo/core/tor/-/issues/32314 already known and closed. "Just use Arti"
darosior commented 2026-10-27 15:01:35 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
First-time contributor

🔵 Suggestion · SOCKS5 address expansion depends on normalization and the DNS mock

The expansion inherits address normalization and consumes the connman fuzz harness's mocked DNS results instead of deterministically formatting the supplied IPv6 literal.

Location: src/netbase.cpp:406 (head).

🔵 Suggestion · Log the SOCKS5 destination name

netbase.cpp:421/424/441/506/540 still log strDest, but lines 482–483 send dest_name. For compressed IPv6, Core's debug logs therefore differ from the SOCKS5 DOMAINNAME on the wire, which makes them harder to match against proxy logs. Diagnostic impact only.

Location: src/netbase.cpp:421 (head).

Review coverage was incomplete.


Reviewed: e8469d76b46b · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:fef06b8697f1090f7a0c02c848d504f4672d1e2e --> <!-- ralph-head:e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1 --> <!-- ralph-case:f492b770a498e98ce27f91745a583f37b31859224d9297eeb0ed16fd7ba080c8 --> **🔵 Suggestion · SOCKS5 address expansion depends on normalization and the DNS mock** The expansion inherits address normalization and consumes the connman fuzz harness's mocked DNS results instead of deterministically formatting the supplied IPv6 literal. Location: [`src/netbase.cpp:406`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1/src/netbase.cpp#L406>) (head). **🔵 Suggestion · Log the SOCKS5 destination name** netbase.cpp:421/424/441/506/540 still log `strDest`, but lines 482–483 send `dest_name`. For compressed IPv6, Core's debug logs therefore differ from the SOCKS5 DOMAINNAME on the wire, which makes them harder to match against proxy logs. Diagnostic impact only. Location: [`src/netbase.cpp:421`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1/src/netbase.cpp#L421>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`e8469d76b46b`](<https://git.fish.foo/bitcoin/bitcoin/commit/e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1>) · [Full review report](<https://review.fish.foo/traces/36675-e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1-2139-5.html>) · [Previous reports](<https://review.fish.foo/traces/36675/>) [permalink](<https://review.fish.foo/traces/36675-e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1-2139-5.html>)
andrewtoth (Migrated from github.com) approved these changes 2026-11-04 00:18:51 +00:00
andrewtoth (Migrated from github.com) left a comment

ACK 721e2f5051

Nice find. Tested with a small patch to only select IPV6 peers via private broadcast. Many SOCKS connection failures to addresses with :: on master, successfully connected to an address with :: with this patch.

This will also cause issues with anyone using the -proxy= with tor feature.

I see from a cursory analysis in bitnod.es that this would fail to connect to ~50% of known reachable ipv6 peers.

ACK 721e2f5051142c2194838dd06fe0a86a07621c14 Nice find. Tested with a small patch to only select IPV6 peers via private broadcast. Many SOCKS connection failures to addresses with `::` on master, successfully connected to an address with `::` with this patch. This will also cause issues with anyone using the `-proxy=` with tor feature. I see from a cursory analysis in bitnod.es that this would fail to connect to ~50% of known reachable ipv6 peers.
andrewtoth (Migrated from github.com) commented 2026-11-04 00:06:02 +00:00

nit

        name += strprintf("%x", ReadBE16(&bytes[i]));
nit ```suggestion name += strprintf("%x", ReadBE16(&bytes[i])); ```
andrewtoth (Migrated from github.com) commented 2026-11-04 00:09:13 +00:00

We could dedupe this with the above test.

diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py
index 67274f9648..719737b340 100755
--- a/test/functional/feature_proxy.py
+++ b/test/functional/feature_proxy.py
@@ -164,15 +164,18 @@ class ProxyTest(BitcoinTestFramework):
         rv.append(cmd)
         self.network_test(node, addr, network=NET_IPV4)
 
-        if self.have_ipv6:
-            addr = "[1233:3432:2434:2343:3234:2345:6546:4534]:5443"
+        for host, expected in [
+            ("1233:3432:2434:2343:3234:2345:6546:4534", b"1233:3432:2434:2343:3234:2345:6546:4534"),
+            ("1233:3432::6546:4534", b"1233:3432:0:0:0:0:6546:4534"),
+        ]:
+            addr = f"[{host}]:5443"
             self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for address {addr}")
             node.addnode(addr, "onetry", v2transport=False)
             cmd = proxies[1].queue.get()
             assert isinstance(cmd, Socks5Command)
             # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
             assert_equal(cmd.atyp, AddressType.DOMAINNAME)
-            assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534")
+            assert_equal(cmd.addr, expected)
             assert_equal(cmd.port, 5443)
             if not auth:
                 assert_equal(cmd.username, None)
@@ -180,18 +183,6 @@ class ProxyTest(BitcoinTestFramework):
             rv.append(cmd)
             self.network_test(node, addr, network=NET_IPV6)
 
-            addr = "[1233:3432::6546:4534]:5443"
-            self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for shortened address {addr}")
-            node.addnode(addr, "onetry", v2transport=False)
-            cmd = proxies[1].queue.get()
-            assert isinstance(cmd, Socks5Command)
-            # A shortened IPv6 address is sent with all eight groups, so that Tor does not read "<addr>:<port>" as one address
-            assert_equal(cmd.atyp, AddressType.DOMAINNAME)
-            assert_equal(cmd.addr, b"1233:3432:0:0:0:0:6546:4534")
-            assert_equal(cmd.port, 5443)
-            rv.append(cmd)
-            self.network_test(node, addr, network=NET_IPV6)
-
         if test_onion:
             addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:8333"
             self.log.debug(f"Test: outgoing onion connection through node {node.index} for address {addr}")

We could dedupe this with the above test. ```diff diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index 67274f9648..719737b340 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -164,15 +164,18 @@ class ProxyTest(BitcoinTestFramework): rv.append(cmd) self.network_test(node, addr, network=NET_IPV4) - if self.have_ipv6: - addr = "[1233:3432:2434:2343:3234:2345:6546:4534]:5443" + for host, expected in [ + ("1233:3432:2434:2343:3234:2345:6546:4534", b"1233:3432:2434:2343:3234:2345:6546:4534"), + ("1233:3432::6546:4534", b"1233:3432:0:0:0:0:6546:4534"), + ]: + addr = f"[{host}]:5443" self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for address {addr}") node.addnode(addr, "onetry", v2transport=False) cmd = proxies[1].queue.get() assert isinstance(cmd, Socks5Command) # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6 assert_equal(cmd.atyp, AddressType.DOMAINNAME) - assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534") + assert_equal(cmd.addr, expected) assert_equal(cmd.port, 5443) if not auth: assert_equal(cmd.username, None) @@ -180,18 +183,6 @@ class ProxyTest(BitcoinTestFramework): rv.append(cmd) self.network_test(node, addr, network=NET_IPV6) - addr = "[1233:3432::6546:4534]:5443" - self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for shortened address {addr}") - node.addnode(addr, "onetry", v2transport=False) - cmd = proxies[1].queue.get() - assert isinstance(cmd, Socks5Command) - # A shortened IPv6 address is sent with all eight groups, so that Tor does not read "<addr>:<port>" as one address - assert_equal(cmd.atyp, AddressType.DOMAINNAME) - assert_equal(cmd.addr, b"1233:3432:0:0:0:0:6546:4534") - assert_equal(cmd.port, 5443) - rv.append(cmd) - self.network_test(node, addr, network=NET_IPV6) - if test_onion: addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:8333" self.log.debug(f"Test: outgoing onion connection through node {node.index} for address {addr}") ```
instagibbs (Migrated from github.com) reviewed 2026-11-04 16:24:28 +00:00
instagibbs (Migrated from github.com) commented 2026-11-04 16:24:28 +00:00

taken

taken
instagibbs (Migrated from github.com) reviewed 2026-11-04 16:25:12 +00:00
instagibbs (Migrated from github.com) commented 2026-11-04 16:25:12 +00:00

applied but kept the have_ipv6 guard

applied but kept the have_ipv6 guard
andrewtoth (Migrated from github.com) approved these changes 2026-11-06 18:09:21 +00:00
andrewtoth (Migrated from github.com) left a comment

reACK e8469d76b4

reACK e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1
jpk68 commented 2026-11-15 00:52:04 +00:00 (Migrated from github.com)
ACK e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1
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!36675
No description provided.