From cd37b1be3a19904060380596f1b87c9fb46e0fad Mon Sep 17 00:00:00 2001 From: DanS Date: Sun, 8 Nov 2026 11:52:44 -0600 Subject: [PATCH] fix(security): pick an unpredictable ephemeral port for migrate-to-seed (F13) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The migrate-to-seed isolated node chose its RPC port by scanning a fixed 21770-21899 range for LISTEN sockets. A local co-tenant could bind() a port in that range WITHOUT listen() — invisible to the LISTEN-state scan — get it picked, then start a listener and intercept the isolated node's plaintext RPC, up to returning a rogue z_getnewaddress that redirects the migration sweep. Add EmbeddedDaemon::pickEphemeralPort(): bind 127.0.0.1:0 and return the OS-assigned port via getsockname(). A real bind() proves the port free right now (a bound-not-listening squatter makes it fail), and the OS-chosen number is unpredictable, so a fixed range can't be pre-squatted. pickFreePort() uses it, keeping the old scan only as a fallback. The port reaches disk solely in the 0600 DRAGONX.conf. A tiny close-then-bind window remains but is no longer targetable in advance. Co-Authored-By: Claude Opus 4.8 --- src/daemon/embedded_daemon.cpp | 48 ++++++++++++++++++++++++++++++ src/daemon/embedded_daemon.h | 6 ++++ src/daemon/seed_wallet_creator.cpp | 17 +++++++---- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/daemon/embedded_daemon.cpp b/src/daemon/embedded_daemon.cpp index 1a3a917..8072ded 100644 --- a/src/daemon/embedded_daemon.cpp +++ b/src/daemon/embedded_daemon.cpp @@ -505,6 +505,54 @@ static bool isPortInUse(int port) #endif } +// Bind 127.0.0.1:0 and let the OS assign a free ephemeral port, returned via getsockname(); 0 on failure. +// We deliberately do NOT set SO_REUSEADDR, so the bind genuinely fails if anyone already holds the port — +// which, together with the OS-chosen (unpredictable) number, defeats a local co-tenant pre-squatting a +// fixed range: a bind()-without-listen() squatter is invisible to isPortInUse's LISTEN scan but still +// makes a real bind() to that port fail, and the caller never learns which port we chose from a guessable +// list. A tiny check-then-use window remains (we close before the child binds), but the port is now +// unpredictable and proven free, so it cannot be targeted in advance. +int EmbeddedDaemon::pickEphemeralPort() +{ +#ifdef _WIN32 + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return 0; + int port = 0; + SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock != INVALID_SOCKET) { + struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr.sin_port = 0; // OS picks a free ephemeral port + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0) { + struct sockaddr_in bound; memset(&bound, 0, sizeof(bound)); + int len = sizeof(bound); + if (getsockname(sock, (struct sockaddr*)&bound, &len) == 0) + port = ntohs(bound.sin_port); + } + closesocket(sock); + } + WSACleanup(); + return port; +#else + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) return 0; + int port = 0; + struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = 0; // OS picks a free ephemeral port + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0) { + struct sockaddr_in bound; memset(&bound, 0, sizeof(bound)); + socklen_t len = sizeof(bound); + if (getsockname(sock, (struct sockaddr*)&bound, &len) == 0) + port = ntohs(bound.sin_port); + } + close(sock); + return port; +#endif +} + bool EmbeddedDaemon::start(const std::string& binary_path, int lockWaitPolls) { std::lock_guard life(lifecycle_mutex_); diff --git a/src/daemon/embedded_daemon.h b/src/daemon/embedded_daemon.h index b5884ec..b4fcea5 100644 --- a/src/daemon/embedded_daemon.h +++ b/src/daemon/embedded_daemon.h @@ -471,6 +471,12 @@ public: /** @brief Is an arbitrary TCP port currently in use on localhost? (used to pick a free port) */ static bool tcpPortInUse(int port); + /** @brief Bind 127.0.0.1:0 and return the OS-assigned free ephemeral port (0 on failure). Unlike + * tcpPortInUse (a LISTEN-state scan that misses a socket bind()ed without listen()), a real bind() + * proves the port is free right now, and letting the OS choose makes it unpredictable — so a local + * co-tenant cannot pre-squat a fixed range and race a port the wallet is about to hand a child node. */ + static int pickEphemeralPort(); + /** Get number of consecutive daemon crashes (resets on successful start or manual reset) */ int getCrashCount() const { return crash_count_.load(); } /** Reset crash counter (call on successful connection or manual restart) */ diff --git a/src/daemon/seed_wallet_creator.cpp b/src/daemon/seed_wallet_creator.cpp index f80c85b..2b0b1c2 100644 --- a/src/daemon/seed_wallet_creator.cpp +++ b/src/daemon/seed_wallet_creator.cpp @@ -45,13 +45,20 @@ std::string randomToken(int n) return s; } -// A free localhost port for the isolated node — just above the default so it never collides with -// the main daemon (which keeps running on the default port throughout). +// A free localhost port for the isolated node. We ask the OS for an unpredictable ephemeral port via a +// real bind() (pickEphemeralPort) rather than scanning a fixed 21770-21899 range for LISTEN sockets: the +// old scan missed a local co-tenant that bind()ed a port without listen()ing (invisible to a LISTEN-state +// check), who could then race a listener onto the port we picked and intercept the isolated node's +// plaintext RPC — up to redirecting the migration sweep. An OS-assigned port is proven free by the bind +// and is not drawn from a guessable range, and it reaches disk only in the 0600 DRAGONX.conf. The fixed +// range remains a last-resort fallback if the OS bind path is unavailable. int pickFreePort() { - for (int p = 21770; p < 21900; ++p) - if (!EmbeddedDaemon::tcpPortInUse(p)) - return p; + const int p = EmbeddedDaemon::pickEphemeralPort(); + if (p > 0) return p; + for (int q = 21770; q < 21900; ++q) + if (!EmbeddedDaemon::tcpPortInUse(q)) + return q; return 0; }