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:
1 parent
08b243a29a
commit
0e6ee77dc7
3 files changed
+73
-15
No files matched your search
@@ -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
|
||||
|
||||
@@ -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`).
|
||||
Reference in new issue
Block a user