From c9ea89ac5049602fede4c5c09d3df5e5c82d4c65 Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 01:40:55 -0700 Subject: [PATCH] control-channel: detect a machine-id collision on client sockets only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QA finding 2. The id check ran for every connection that finished its handshake, so an inbound peer announcing our own id could force-close every socket and make the app draw a new machine id. Windows runs the id comparison and the keep-the-first-client rule as two branches of one block gated by `status == Connected && tcp.IsClient` (SocketStuff.cs:1980-1981): a server-accepted socket reaches neither. Move the collision check inside the `isOutbound` gate the dedup half already had, and say so in the README and docs/protocol.md §10.3 note 3. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/MWBNet/ControlChannel.swift | 28 ++++++++++++++++--------- docs/features/control-channel/README.md | 12 +++++++---- docs/protocol.md | 12 ++++++++--- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Sources/MWBNet/ControlChannel.swift b/Sources/MWBNet/ControlChannel.swift index 021f367..e05c8b5 100644 --- a/Sources/MWBNet/ControlChannel.swift +++ b/Sources/MWBNet/ControlChannel.swift @@ -364,17 +364,25 @@ public final class ControlChannel: @unchecked Sendable { /// The handshake completed. Two policies apply only here, because both are /// keyed by things a single connection cannot see (`SocketStuff.cs:1964-2004`). + /// + /// Both are client-only: `UpdateTcpSockets` runs the whole + /// collision-and-dedup block under `status == SocketStatus.Connected && + /// tcp.IsClient` (`:1980-1981`), so neither the id check (`:1984-1992`) nor + /// the keep-the-first-client rule (`:1993-1997`) ever sees a + /// server-accepted socket. private func accept(_ peer: PeerConnection, name: String, id: UInt32) { - if id == config.localID { // SocketStuff.cs:1984-1992 - sink.channelDetectedIDCollision(self) - closeAll(.idCollision) - return - } - // Keep the first client that connected; a later one is redundant - // (SocketStuff.cs:1421-1428, 1998). Inbound connections are always kept. - if peer.isOutbound, hasConnectedClient(to: name, excluding: peer) { - peer.close(.duplicateClient) - return + if peer.isOutbound { + if id == config.localID { // SocketStuff.cs:1984-1992 + sink.channelDetectedIDCollision(self) + closeAll(.idCollision) + return + } + // Keep the first client that connected; a later one is redundant + // (SocketStuff.cs:1421-1428, 1996-2004). + if hasConnectedClient(to: name, excluding: peer) { + peer.close(.duplicateClient) + return + } } dialers[name] = nil sink.channel(self, connected: name, peerID: id) diff --git a/docs/features/control-channel/README.md b/docs/features/control-channel/README.md index 51768c1..22fa4cb 100644 --- a/docs/features/control-channel/README.md +++ b/docs/features/control-channel/README.md @@ -66,10 +66,14 @@ No packet is interpreted here. and one inbound connection per peer, and forwards every app packet to both — the receiver's replay ring collapses the pair (contracts C1). - **Machine-id collision** (`SocketStuff.cs:1984-1992`): if a trusted - peer reports our own id, the channel reports `.idCollision` to the - sink; the app regenerates and calls `reopenAll()`. Windows force-closes - *every* socket in that branch, not just the offending one - (`docs/protocol.md` §10.3 note 3), and so do we. + peer reports our own id **over an outbound connection**, the channel + reports `.idCollision` to the sink; the app regenerates and calls + `reopenAll()`. Windows force-closes *every* socket in that branch, not + just the offending one (`docs/protocol.md` §10.3 note 3), and so do we. + Like the duplicate-client rule, this is client-only: `UpdateTcpSockets` + runs the whole block under `status == Connected && tcp.IsClient` + (`:1980-1981`), so an id reported over an inbound connection is + silently tolerated, here as there. - **Failure budget per endpoint** (`SocketStuff.cs:1482-1499`): more than 10 `.invalidKey` outcomes from the same remote address ⇒ report `.peerRejected(address)` and stop dialling it until `reopenAll()`. diff --git a/docs/protocol.md b/docs/protocol.md index 4d2d40b..654db23 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -633,13 +633,19 @@ sees them. One subsection per lane, appended, never rewritten. negative sentinels are separate paths: `REOPEN_WHEN_HOTKEY (-10055)` reopens everything, `REOPEN_WHEN_WSAECONNRESET (-10054)` only re-runs the client loop (`frmScreen.cs:452-461`, `InitAndCleanup.cs:28-29`). -3. **An id collision force-closes every socket, not just the offending one.** In - `UpdateTcpSockets`, `tobeRemovedSockets` starts as the *whole* socket list and +3. **An id collision force-closes every socket, not just the offending one — + and it is only ever detected on a client socket.** In `UpdateTcpSockets`, + `tobeRemovedSockets` starts as the *whole* socket list and is narrowed to "other clients with the same machine name" only in the `else` branch; the collision branch leaves it whole and sets `tcp = null` so the new socket's status is never written (`SocketStuff.cs:1978-1998`). A new `MachineId` is drawn and `PleaseReopenSocket = REOPEN_WHEN_HOTKEY` reopens - everything (`:1985-1990`). + everything (`:1985-1990`). The id comparison and the keep-the-first-client + rule are two branches of one block gated by `status == + SocketStatus.Connected && tcp.IsClient` (`:1980-1981`), so a server-accepted + socket reaches neither: a peer that announces our own `MachineId` over an + inbound connection is silently tolerated. Note 4's client-only observation + about the dedup half is the same gate. 4. **Duplicate clients are suppressed twice; duplicate servers never are.** The newcomer closes itself just before it would become trusted (`SocketStuff.cs:1421-1428`), and `UpdateTcpSockets` independently keeps the