net: fix rare race in Tor stream isolation credentials

bitcoin/bitcoin: PR #37043

1 suggestion

Optional improvements

Link

[suggestion] · Credential uniqueness tests miss concurrent Generate() calls

The existing uniqueness assertion in feature_proxy.py checks sequential connections, so it does not exercise the concurrent credential-generation invariant fixed here.

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

Details and suggested fix

node_test() waits for the proxy command after each addnode before requesting the next connection. Those checks would still pass with the former separate atomic read and increment, although overlapping calls from the addcon and opencon threads could receive identical credentials and allow Tor circuit sharing. A data-race sanitizer need not detect that logical race because both operations are individually atomic.

Expose a small test seam for TorStreamIsolationCredentialsGenerator, which is currently defined only in netbase.cpp and instantiated as a function-local static in ConnectThroughProxy(). Add a unit test that repeatedly calls Generate() from several threads sharing one instance, collects results in per-thread containers, and checks their combined uniqueness after joining. This would exercise the missing invariant without changing production behavior. Such a stress test improves the chance of catching a regression but cannot reliably reproduce the bad interleaving.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The historical requirement was isolation per outgoing connection, not a credential identity retained for a peer. In the 2015 stream-isolation proposal, Wladimir van der Laan specified that the default-enabled -proxyrandomize option would randomize credentials 'for every outgoing connection,' thereby creating a new circuit 'for every peer connection.' The stated privacy rationale was that distinct SOCKS credentials cause Tor to separate circuits, making connections harder to correlate.

This establishes the connection-level granularity behind generating credentials during each isolation-enabled SOCKS negotiation. Reusing credentials merely because a connection targets the same peer or belongs to the same group is not the requirement expressed by the original proposal.

Source 1

The session-prefix-plus-counter scheme was intended to extend uniqueness across application launches as well as within a launch. PR #32176 explicitly replaced a simple atomic integer counter with a random session prefix and an atomic counter so that different launches would not reuse credentials and therefore circuits. The accompanying code comment explicitly includes launches occurring successively or in parallel.

The prefix is not a peer or connection-group identifier. It separates application sessions, while the changing counter distinguishes generated credentials within a session. The affected implementation retains this two-level isolation scope.

Source 1 · Source 2

Ralph's inferences

In the pinned implementation, the historical per-connection requirement is realized by one shared generator that is invoked whenever ConnectThroughProxy() reaches an isolation-enabled SOCKS negotiation. There is no credential cache keyed by peer, destination, or connection group. Thus a repeated attempt reaching negotiation requests another credential pair, rather than inheriting the previous attempt's pair.

This is the precise mapping from the historical outgoing-connection requirement to the affected function. It is not literally a requirement to generate credentials on every function entry: failure to connect to the proxy returns before generation, and connections with stream isolation disabled do not generate credentials.

Source 1

Cross-launch uniqueness is probabilistic rather than a coordinated global namespace: each generator selects an eight-byte random prefix, then appends its counter. The intended scope is distinct per-connection values within a launch and avoidance of accidental credential collisions between launches, not a mathematically collision-free identity across all applications.

This distinguishes the counter's within-session role from the random prefix's cross-session role without interpreting 'unique' as an unlimited global guarantee.

Source 1