session-matrix: a failed read spends the untrusted budget, as on Windows

QA finding 1. TcpReceiveData does not signal a bad read out of band: it
rewrites the buffer's type byte to PackageType.Error (0 bytes) or
PackageType.Invalid (short read) (SocketStuff.cs:574-578, 588-593) and
MainTCPRoutine runs that package through the whole loop body — the
private errCount for the Error case (:1332-1364), then the same untrusted
counting as any garbage packet (:1370-1529). So a run of malformed reads
ends in .invalidKey, not .error, and the trusted branch's
`receivedCount > 0` guard (:1531) keeps a 0-byte read out of the receiver.

Session.noteReceiveError now takes the byte count and feeds a synthetic
.error/.invalid packet through the new receive(_:bytesReceived:), which
is the loop body; feed() goes through it too. Written up as protocol
§10.2 note 10 and in the README's Assumptions.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 00:58:29 -07:00
1 parent 08b243a29a
commit 0e6ee77dc7
3 files changed
+73 -15

No files matched your search

+40 -14
View File
@@ -184,18 +184,24 @@ public final class Session {
// `package.Bytes = buf` (SocketStuff.cs:585-598).
let packet = size == packageSize ? head : Framing.open(inbox.prefix(size), magic: config.magic)
inbox.removeFirst(size)
receiveErrors = 0 // SocketStuff.cs:1364
handle(packet)
receive(packet, bytesReceived: size)
}
}
/// A read that returned nothing or a short packet — `PackageType.Error` on
/// Windows. Six in a row close the connection (`SocketStuff.cs:1326-1361`).
public func noteReceiveError() {
receiveErrors += 1
guard receiveErrors > 5 else { return }
report(.error)
close()
/// A read that did not produce a whole packet, with the byte count it
/// returned.
///
/// Windows turns the read itself into a package — `PackageType.Error` for a
/// 0-byte read, `PackageType.Invalid` for a short one
/// (`SocketStuff.cs:574-578, 588-593`) — and then runs it through the *same*
/// loop body as any packet off the wire. So a malformed read spends the
/// untrusted budget exactly like a garbage packet does
/// (`:1330-1338` falls through to `:1379-1521`), and a run of short reads
/// during the handshake ends in `.invalidKey`, not `.error`. Only the 0-byte
/// kind feeds the separate six-in-a-row counter (`:1344-1364`).
public func noteReceiveError(bytesReceived: Int) {
receive(
Packet(type: bytesReceived == 0 ? .error : .invalid), bytesReceived: bytesReceived)
}
/// Reports a status the network layer knows and the session cannot see, such
@@ -264,9 +270,26 @@ public final class Session {
// MARK: - Receiving
private func handle(_ packet: Packet) {
/// One turn of `MainTCPRoutine`'s receive loop (`SocketStuff.cs:1326-1547`):
/// the read-error counter, then the handshake reply, then the trust gate.
/// `bytesReceived` is what the read that produced `packet` returned.
private func receive(_ packet: Packet, bytesReceived: Int) {
if packet.type == .error {
receiveErrors += 1 // SocketStuff.cs:1334
guard receiveErrors <= 5 else { // SocketStuff.cs:1344-1364
report(.error)
close()
return
}
} else {
receiveErrors = 0 // SocketStuff.cs:1366-1368
}
handle(packet, bytesReceived: bytesReceived)
}
private func handle(_ packet: Packet, bytesReceived: Int) {
// A Handshake is answered whatever the trust state, and never counts
// against the untrusted budget (SocketStuff.cs:1367-1384).
// against the untrusted budget (SocketStuff.cs:1370-1384).
if packet.type == .handshake {
var ack = packet
ack.type = .handshakeAck
@@ -279,9 +302,12 @@ public final class Session {
}
guard untrustedCount >= 0 else {
// Trusted. Duplicate acks from the peer's ten handshakes are
// ignored rather than processed (SocketStuff.cs:1524-1527).
if packet.type != .handshakeAck { deliver(packet) }
// Trusted. A read that returned nothing never reaches the receiver:
// the whole branch is guarded by `receivedCount > 0`
// (SocketStuff.cs:1531). Duplicate acks from the peer's ten
// handshakes are ignored rather than processed (:1540-1543).
guard bytesReceived > 0, packet.type != .handshakeAck else { return }
deliver(packet)
return
}
countUntrusted(packet)
+9 -1
View File
@@ -38,6 +38,14 @@ the Windows source again.
10 the session reports `.invalidKey` and asks to close
(`SocketStuff.cs:1387-1406`). Application packets are surfaced to the
caller only from a trusted session (CLAUDE.md invariant 2).
- **A failed read is a packet.** Windows builds a `PackageType.Error`
(0 bytes read) or `PackageType.Invalid` (short read) package out of the
read itself (`SocketStuff.cs:574-578, 588-593`) and runs it through the
whole loop body, so a malformed read spends the same untrusted budget
as a garbage packet; only the 0-byte kind also feeds the
six-in-a-row close (`:1330-1364`). The caller therefore passes the byte
count — `noteReceiveError(bytesReceived:)` — and must not translate a
short read into a 0-byte one (docs/protocol.md §10.2 note 10).
- **Sequence numbers and the replay ring are process-wide**, as on
Windows (`Common.cs:906`, `Receiver.cs:40-82`): a `PacketSequencer`
and a `ReplayFilter` are created once by the app and shared by every
@@ -149,7 +157,7 @@ public final class Session { // not Sendable: one per connecti
public init(config:sequencer:replay:isClient:)
public func start(now: TimeInterval) // queues 10 Handshake packets
public func feed(_ plaintext: Data) // 32-then-32 reassembly inside
public func noteReceiveError() // a 0-byte / short read
public func noteReceiveError(bytesReceived: Int) // a 0-byte / short read
public func report(_ new: SocketStatus) // .resolving, .sendError, …
public func tick(now: TimeInterval, userActive: Bool)
public func send(_ packet: Packet) // stamps Id + Src, frames, queues
+24
View File
@@ -568,3 +568,27 @@ sees them. One subsection per lane, appended, never rewritten.
`now - HEARTBEAT_TIMEOUT + 10 000 ms` (`MachinePool.cs:111-122`), so the
machine keeps its pool slot and its matrix position and comes back to life on
the next heartbeat without any renegotiation.
10. **A failed read is a packet, and it spends the untrusted budget.**
`TcpReceiveData` does not signal a bad read out of band: it overwrites the
type byte of the buffer it was filling with `PackageType.Error` when the
read returned 0 bytes and `PackageType.Invalid` when it returned a short
count, for both the 32-byte and the 64-byte halves
(`SocketStuff.cs:574-578, 588-593`). `MainTCPRoutine` then increments a
private `errCount` for the `Error` case only, closing with
`SocketStatus.Error` on the sixth in a row (`:1332-1364`), resets `errCount`
on **any** other type including `Invalid` (`:1366-1368`) — and then lets the
package fall through the `Type == Handshake` test into the ordinary
untrusted branch (`:1370-1529`). Consequences worth stating, because they
are not obvious from the code's shape:
- A short read during the handshake is counted as one of the ten untrusted
packets. Five of them raise `SocketStatus.InvalidKey` (`:1519-1521`) and
ten send the ten `Invalid` packets and close (`:1389-1400`) — so a run of
malformed reads ends in `InvalidKey`, not `Error`.
- A 0-byte read spends the untrusted budget **and** an `errCount`, so
whichever limit is reached first decides how the connection dies: six in
a row close with `Error` before the tenth untrusted packet, but errors
interleaved with any other traffic reset `errCount` while `packageCount`
keeps climbing.
- The trusted branch is guarded by `receivedCount > 0` (`:1531`), so a
0-byte read never reaches `Receiver.ProcessPackage`; a short read does,
and `PreProcess` drops it for being `Invalid` (`Receiver.cs:42-50`).