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
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user