Skip to content

net: Use exclusive P2P listen sockets on Windows - #3

Closed
winterrdog wants to merge 2 commits into
masterfrom
fix/p2p-exclusive-bind-win32
Closed

winterrdog wants to merge 2 commits into
masterfrom
fix/p2p-exclusive-bind-win32

Conversation

@winterrdog

@winterrdog winterrdog commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner

on Windows, SO_REUSEADDR does not carry over the same semantics as on
POSIX systems. a second socket can set SO_REUSEADDR and bind to a
port that is already in use by another process running under
the same user account. this means another local process could bind to
the P2P listen port and receive incoming connections intended for
bitcoind.

this matters because the P2P listener is expected to have exclusive
ownership of its address and port. allowing another process to bind to
it could let that process intercept incoming P2P connections. this is
in a similar spirit to bitcoin#36169, which made the RPC listener exclusive on
Windows.

to fix this, we use SO_EXCLUSIVEADDRUSE for the P2P listen socket on
Windows instead of SO_REUSEADDR, while retaining SO_REUSEADDR on
other platforms. the added test verifies the expected behaviour by
attempting to bind a second socket to the same port. on Windows, this
fails with WSAEACCES (10013), which Python exposes as
PermissionError (an OSError subclass).

references:

Check that another socket cannot bind to a P2P port while `bitcoind` is
already listening on it. This makes sure the listen port cannot be
claimed by another process.
On Windows, `SO_REUSEADDR` does not give sockets exclusive use of an
address and port, so another process running under the same user
account can bind to the same port and intercept incoming P2P
connections.

Make the P2P listen port exclusive on Windows by using
`SO_EXCLUSIVEADDRUSE` so as to prevent this.
@winterrdog

winterrdog commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner Author

todos

  • run tests on windows (it was a hustle setting up MSVC; i am too rusty for win32)
  • execute push to upstream

@winterrdog

winterrdog commented Sep 20, 2026 •

Copy link
Copy Markdown
Owner Author

indeed, this difference in SO_REUSEADDR behaviour across operating systems is
somewhat surprising, and has led to similar discussions in Tornado,
CPython, and Trio.

discussions about this similar issue elsewhere:

@winterrdog winterrdog closed this Sep 20, 2026
winterrdog pushed a commit that referenced this pull request Nov 6, 2026
…:__pad_and_output

fa3821e test: Add missing suppression for race:std::__1::__pad_and_output (MarcoFalke)

Pull request description:

  From https://github.com/bitcoin/bitcoin/actions/runs/41684419453/job/126086262840?pr=35990#step:11:8669:

  ```
  WARNING: ThreadSanitizer: data race (pid=17903)
    Write of size 8 at 0x7ff5c1990c20 by thread T12 (mutexes: write M0):
      #0 width /llvm-project/libcxx/include/ios:498:18 (libc++.so.1+0xd3816) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #1 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>> std::__1::__pad_and_output[abi:dee230102]<char, std::__1::char_traits<char>>(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, char const*, char const*, char const*, std::__1::ios_base&, char) /llvm-project/libcxx/include/__locale_dir/pad_and_output.h:67:9 (libc++.so.1+0xd3816)
      #2 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>> std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>>::__do_put_integral[abi:dee230102]<long long>(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, std::__1::ios_base&, char, long long) const /llvm-project/libcxx/include/__locale_dir/num.h:907:10 (libc++.so.1+0xbcfa0) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #3 std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>>::do_put(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, std::__1::ios_base&, char, long long) const /llvm-project/libcxx/include/__locale_dir/num.h:919:16 (libc++.so.1+0xbcb79) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #4 put /llvm-project/libcxx/include/__locale_dir/num.h:780:12 (libc++.so.1+0x9b40c) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      bitcoin#5 std::__1::basic_ostream<char, std::__1::char_traits<char>>& std::__1::basic_ostream<char, std::__1::char_traits<char>>::__put_num[abi:dee230102]<long long>(long long) /llvm-project/libcxx/include/__ostream/basic_ostream.h:104:21 (libc++.so.1+0x9b40c)
      bitcoin#6 std::__1::basic_ostream<char, std::__1::char_traits<char>>::operator<<(long long) /llvm-project/libcxx/include/__ostream/basic_ostream.h:316:10 (libc++.so.1+0x9b2a0) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      bitcoin#7 KernelLog::LogMessage(btck::LogEntry const&) /home/runner/work/_temp/src/bitcoin-chainstate.cpp:49:33 (bitcoin-chainstate+0xf2667) (BuildId: cff46198c8fd29997404a749cd55fdd6fcb3e865)

ACKs for top commit:
  fanquake:
    utACK fa3821e

Tree-SHA512: 383705fe33e429d40baaf7811a3edd6ce5164c423a1450c7a7a5d749d1ba44b8dac7a1270cbb593fcd0888dd9a2efbe7507650adf69b2f46ac09db4488cb9aec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant