Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions rustls/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ impl<'a> io::Read for Reader<'a> {
/// connection, so the underlying TCP connection should be half-closed too.
///
/// If the peer closes the TLS session uncleanly (a TCP EOF without sending a
/// `close_notify` alert) this function returns `Err(ErrorKind::UnexpectedEof.into())`
/// once any pending data has been read.
/// `close_notify` alert) this function returns a `std::io::Error` of type
/// `ErrorKind::UnexpectedEof` once any pending data has been read.
///
/// Note that support for `close_notify` varies in peer TLS libraries: many do not
/// support it and uncleanly close the TCP connection (this might be
Expand All @@ -164,8 +164,13 @@ impl<'a> io::Read for Reader<'a> {
// cleanly closed; don't care about TCP EOF: express this as Ok(0)
(true, _) => {}
// unclean closure
(false, true) => return Err(io::ErrorKind::UnexpectedEof.into()),
// connection still going, but need more data: signal `WouldBlock` so that
(false, true) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
UNEXPECTED_EOF_MESSAGE,
))
}
// connection still going, but needs more data: signal `WouldBlock` so that
// the caller knows this
(false, false) => return Err(io::ErrorKind::WouldBlock.into()),
}
Expand All @@ -182,8 +187,8 @@ impl<'a> io::Read for Reader<'a> {
/// should be half-closed too.
///
/// If the peer closes the TLS session uncleanly (a TCP EOF without sending a
/// `close_notify` alert) this function returns `Err(ErrorKind::UnexpectedEof.into())`
/// once any pending data has been read.
/// `close_notify` alert) this function returns a `std::io::Error` of type
/// `ErrorKind::UnexpectedEof` once any pending data has been read.
///
/// Note that support for `close_notify` varies in peer TLS libraries: many do not
/// support it and uncleanly close the TCP connection (this might be
Expand All @@ -208,7 +213,12 @@ impl<'a> io::Read for Reader<'a> {
// cleanly closed; don't care about TCP EOF: express this as Ok(0)
(true, _) => {}
// unclean closure
(false, true) => return Err(io::ErrorKind::UnexpectedEof.into()),
(false, true) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
UNEXPECTED_EOF_MESSAGE,
));
}
// connection still going, but need more data: signal `WouldBlock` so that
// the caller knows this
(false, false) => return Err(io::ErrorKind::WouldBlock.into()),
Expand Down Expand Up @@ -759,3 +769,6 @@ impl<Data> ConnectionCore<Data> {

/// Data specific to the peer's side (client or server).
pub trait SideData: Debug {}

const UNEXPECTED_EOF_MESSAGE: &str = "peer closed connection without sending TLS close_notify: \
https://docs.rs/rustls/latest/rustls/manual/_03_howto/index.html#unexpected-eof";
17 changes: 17 additions & 0 deletions rustls/src/manual/howto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ Once you have these two pieces, configuring a server to use them involves, brief
[^1]: For PKCS#8 it does not support password encryption -- there's not a meaningful threat
model addressed by this, and the encryption supported is typically extremely poor.

# Unexpected EOF

TLS has a `close_notify` mechanism to prevent truncation attacks[^2].
According to the TLS RFCs, each party is required to send a `close_notify` message before
closing the write side of the connection. However, some implementations don't send it.
So long as the application layer protocol (for instance HTTP/2) has message length framing
and can reject truncated messages, this is not a security problem.

Rustls treats an EOF without `close_notify` as an error of type `std::io::Error` with
`ErrorKind::UnexpectedEof`. In some situations it's appropriate for the application to handle
this error the same way it would handle a normal EOF (a read returning `Ok(0)`). In particular
if `UnexpectedEof` occurs on an idle connection it is appropriate to treat it the same way as a
clean shutdown. And if an application always uses messages with length framing (in other words,
messages are never delimited by the close of the TCP connection), it can unconditionally
ignore `UnexpectedEof` errors from rustls.

[^2]: <https://datatracker.ietf.org/doc/html/rfc8446#section-6.1>
*/