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

+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`).