Skip to content

Commit 7a69ade

Browse files
committed
util: fuse flushes from connections with errors
1 parent 493d089 commit 7a69ade

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

‎rustls/src/conn.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ https://docs.rs/rustls/latest/rustls/manual/_03_howto/index.html#unexpected-eof"
339339
self.sink.write_vectored(bufs)
340340
}
341341

342+
/// This does not do any I/O, but returns the fatal error the connection previously
343+
/// encountered, if any.
342344
fn flush(&mut self) -> io::Result<()> {
343345
self.sink.flush()
344346
}
@@ -400,7 +402,7 @@ https://docs.rs/rustls/latest/rustls/manual/_03_howto/index.html#unexpected-eof"
400402
}
401403

402404
fn flush(&mut self) -> io::Result<()> {
403-
Ok(())
405+
self.check_error()
404406
}
405407
}
406408
}

‎rustls/src/stream.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ where
148148
fn flush(&mut self) -> Result<()> {
149149
self.complete_prior_io()?;
150150

151-
self.conn.writer().flush()?;
152-
153151
// `complete_prior_io()` may return after making only partial progress, so
154152
// keep writing until all TLS output has been handed to the transport.
155153
while self.conn.wants_write() {
@@ -163,7 +161,11 @@ where
163161

164162
// Always flush the transport, even if we had no TLS output: an earlier
165163
// flush (for example, from within `write()`) may have failed.
166-
self.sock.flush()
164+
self.sock.flush()?;
165+
166+
// Report any fatal error the connection encountered after an earlier `write()`
167+
// had already accepted its plaintext (for example, while processing received data).
168+
self.conn.writer().flush()
167169
}
168170
}
169171

‎rustls/tests/api.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,48 @@ impl Write for FailsFirstFlush {
35073507
}
35083508
}
35093509

3510+
/// See <https://github.com/rustls/rustls/issues/3306>.
3511+
#[test]
3512+
fn stream_reports_error_after_write_accepted_plaintext() {
3513+
let (mut client, mut server) = make_pair(KeyType::Rsa2048, &provider::default_provider());
3514+
do_handshake(&mut client, &mut server);
3515+
3516+
server
3517+
.writer()
3518+
.write_all(b"hello")
3519+
.unwrap();
3520+
let mut server_output = Vec::new();
3521+
server
3522+
.write_tls(&mut server_output)
3523+
.unwrap();
3524+
*server_output.last_mut().unwrap() ^= 1;
3525+
3526+
// the transport blocks writes, so the first `write()` goes on to read the corrupt record
3527+
let mut pipe = TestNonBlockIo {
3528+
writes: vec![],
3529+
reads: vec![server_output],
3530+
};
3531+
let mut client_stream = Stream::new(&mut client, &mut pipe);
3532+
3533+
assert_eq!(client_stream.write(b"before").unwrap(), 6);
3534+
3535+
// one write for each of the pending records: our data and the fatal alert
3536+
client_stream
3537+
.sock
3538+
.writes
3539+
.extend([usize::MAX, usize::MAX]);
3540+
let err = client_stream
3541+
.write(b"after")
3542+
.unwrap_err();
3543+
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
3544+
assert_eq!(err.to_string(), Error::DecryptError.to_string());
3545+
assert!(!client_stream.conn.wants_write());
3546+
3547+
let err = client_stream.flush().unwrap_err();
3548+
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
3549+
assert_eq!(err.to_string(), Error::DecryptError.to_string());
3550+
}
3551+
35103552
fn make_disjoint_suite_configs() -> (ClientConfig, ServerConfig) {
35113553
let kt = KeyType::Rsa2048;
35123554
let client_provider = CryptoProvider {

0 commit comments

Comments
 (0)