Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read any pending data before shutting down stream #63

Closed
wants to merge 1 commit into from

Conversation

levkk
Copy link

@levkk levkk commented Apr 10, 2024

I ran into the issue where another Rustls client (launchbadge/sqlx) is sending CloseNotify (correctly I believe) before shutting down the stream. If the server doesn't wait for it when shutting down the socket, the client throws:

Transport endpoint is not connected (os error 107)

This PR ensures if there are any data still in the stream, it's read in and only then the stream is closed. All incoming data at this point is discarded, so this shouldn't be security issue I think, but I'm not an expert.

@levkk levkk changed the title Wait for client to send CloseNotify before closing socket Read any pending IO before shutting down stream Apr 10, 2024
@levkk levkk changed the title Read any pending IO before shutting down stream Read any pending data before shutting down stream Apr 10, 2024
@@ -339,6 +339,10 @@ where
ready!(self.write_io(cx))?;
}

while self.session.wants_read() {
ready!(self.read_io(cx))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should consider the case where return value is equal to 0?

@quininer
Copy link
Member

quininer commented Apr 11, 2024

Both parties need not wait to receive a "close_notify" alert before closing their read side of the connection, though doing so would introduce the possibility of truncation.

According to rfc https://www.rfc-editor.org/rfc/rfc8446#page-87 I don't think this is necessary. And waiting for packet before shutdown is rather a DoS risk, as the other party may send TLS packets slowly enough to prevent the TCP connection from being closed.

@quininer
Copy link
Member

I'm considering having the TlsStream shutdown not actually do a TCP shutdown, which has two benefits

  1. Users can decide for themselves when to do TCP shutdown.
  2. Users can reuse the TCP connection after the TLS connection has ended.

@levkk
Copy link
Author

levkk commented Apr 11, 2024

I think I agree. The bug seems to be more on the client side here. I've submitted a fix to SQLx which was my specific use case and that worked without this patch. Even if this patch is merged, I think there still will be a race condition between clients sending CloseNotify and us polling the socket for read data.

@quininer
Copy link
Member

I will close this PR and I will consider remove TCP shutdown in next major version.

@quininer quininer closed this Apr 12, 2024
@quininer quininer mentioned this pull request Apr 12, 2024
@levkk levkk deleted the levkk-fix-poll-shutdown branch April 12, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants