Merge bitcoin/bitcoin#36305: net: Use exclusive P2P listen sockets on Windows
What changed, and why it matters
This change fixes a Windows-specific networking issue in Bitcoin Core. Previously, another program running on the same Windows computer under the same user account could hijack Bitcoin's peer-to-peer network port, potentially intercepting incoming connections meant for the Bitcoin node. The fix makes the listening socket exclusive on Windows, similar to a change already made for the RPC port. Other operating systems are unaffected because their behavior was already safe.
No immediate user action is required beyond upgrading to a release containing this fix. Windows node operators who rely on rapid restart during TIME_WAIT may notice slightly stricter port binding behavior, but this is the intended security improvement. Review whether any custom scripts or local services attempt to reuse bitcoind's P2P port and adjust them if necessary.
Security signals we found
Local port hijacking / connection interception on Windows
Incorrect socket option semantics on Windows (SO_REUSEADDR not exclusive)
Defense in depth for P2P listener ownership
Follow-up to similar RPC listener hardening (#36169)
Evidence from the diff
On Windows, SO_REUSEADDR does not prevent another process from binding to the same address and port if both processes run under the same user account. This allowed a local process to bind to bitcoind’s P2P listen port and intercept incoming connections. The patch changes CConnman::BindListenPort in src/net.cpp to use SO_EXCLUSIVEADDRUSE on Windows instead of SO_REUSEADDR. Non-Windows platforms keep SO_REUSEADDR to allow rapid restart while a socket is in TIME_WAIT. A functional test in test/functional/feature_port.py verifies that a competing socket cannot bind to bitcoind’s P2P port and that bitcoind fails to start if the port is already taken.
Changed components
src/net.cpp - CConnman::BindListenPorttest/functional/feature_port.pyWindows P2P listen socket binding onlyInspect captured patch +31 / −0
### src/net.cpp
@@ -3419,12 +3419,24 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
return false;
}
+#ifdef WIN32
+ // Prevent another process from binding to the same address and port,
+ // which could allow it to intercept incoming P2P connections. On Windows,
+ // SO_REUSEADDR is NOT exclusive, so another process running under the same
+ // user account could bind to the same port.
+ if (sock->SetSockOpt(SOL_SOCKET, SO_EXCLUSIVEADDRUSE, &nOne, sizeof(int)) == SOCKET_ERROR) {
+ strError = Untranslated(strprintf("Error setting SO_EXCLUSIVEADDRUSE on socket: %s", NetworkErrorString(WSAGetLastError())));
+ LogError("%s\n", strError.original);
+ return false;
+ }
+#else
// Allow binding if the port is still in TIME_WAIT state after
// the program was closed and restarted.
if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &nOne, sizeof(int)) == SOCKET_ERROR) {
strError = Untranslated(strprintf("Error setting SO_REUSEADDR on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
LogInfo("%s\n", strError.original);
}
+#endif
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
// and enable it by default or not. Try to enable it, if possible.
### test/functional/feature_port.py
@@ -7,11 +7,14 @@
-bind.
"""
+import socket
from test_framework.test_framework import (
BitcoinTestFramework,
)
+from test_framework.test_node import ErrorMatch
from test_framework.util import (
p2p_port,
+ assert_raises,
)
@@ -32,6 +35,22 @@ def run_test(self):
with node.assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port1}', f'Bound to 127.0.0.1:{port1 + 1}']):
self.restart_node(0, extra_args=["-listen", f"-port={port1}"])
+ self.log.info("Verifying that another process cannot bind the same P2P listen port as bitcoind")
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as competing_listener:
+ competing_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ # Misconfigured sockets allow port reuse unless the original
+ # listener requested for exclusive address use.
+ assert_raises(OSError, lambda: competing_listener.bind(("0.0.0.0", port1)))
+
+ self.log.info("Verifying that bitcoind fails during startup if another process is already bound to the same P2P listen port")
+ self.stop_node(0)
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as competing_listener:
+ competing_listener.bind(("0.0.0.0", port1))
+ node.assert_start_raises_init_error(
+ extra_args=["-listen", f"-port={port1}"],
+ expected_msg=fr"^Error\: Unable to bind to 0\.0\.0\.0\:{port1} on this computer\..*",
+ match=ErrorMatch.PARTIAL_REGEX)
+
self.log.info("When specifying -port multiple times, only the last one is taken")
with node.assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port2}', f'Bound to 127.0.0.1:{port2 + 1}'], unexpected_msgs=[f'Bound to 0.0.0.0:{port1}']):
self.restart_node(0, extra_args=["-listen", f"-port={port1}", f"-port={port2}"])Why this scored 62/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.