Skip to content

potentially questionable overloading of ErrorKind::WouldBlock #777

Closed

Description

@hawkw

PR #629 changed the behavior of the std::io::Read implementation for reading plaintext bytes from a TLS connection. Ok(0) (EOF) is only returned when a CloseNotify was received from the remote peer. When no bytes are available to read but the remote peer has not sent a CloseNotify message, an Error with ErrorKind::WouldBlock is returned instead.

What I'm wondering here is whether WouldBlock is, in fact, the correct ErrorKind to return in this case. Per the standard library documentation, WouldBlock indicates that

The operation needs to block to complete, but the blocking operation was requested to not occur.

Generally, this is used when an IO operation is performed asynchronously: for example, on Linux, when a socket is opened with the O_NONBLOCK flag and a read or write call returned EWOULDBLOCK. Async IO libraries such as tokio and mio assign special meaning to this ErrorKind, as it is assumed to indicate that the operating system will somehow notify the program when that socket is once again readable (e.g. via an epoll event).

Therefore, I wonder if it's really correct to overload the meaning of WouldBlock here. My understanding is that rustls will return it when the connection was closed abruptly by the peer (without sending a CloseNotify), or when the connection has not been closed, but additional TLS bytes must be read from IO before more plaintext bytes are available to read. Returning WouldBlock in both cases seems potentially confusing.

I wonder if it would be more correct to return an error with ErrorKind::UnexpectedEof in the case where an EOF was encountered without receiving a CloseNotify?

Activity

  1. djc commented on Jul 8, 2021

    @djc
    Member

    To be sure, we're talking about Connection::read() (on current main), right?

        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            let len = self.received_plaintext.read(buf)?;
    
            if len == 0 && !buf.is_empty() {
                // no bytes available:
                // - if we received a close_notify, this is a genuine permanent EOF
                // - otherwise say EWOULDBLOCK
                if !self.connection_at_eof() {
                    return Err(io::ErrorKind::WouldBlock.into());
                }
            }
    
            Ok(len)
        }

    The way I understand it is that self.received_plaintext contains application data bytes that are buffered in the Connection, after having been decrypted from the bytes read from the io::Read passed to read_tls(). As long as we haven't determined that the connection is at EOF (by way of a CloseNotify alert from the peer), we could at any time get new application data traffic being passed into the connection.

    As such, I think the situation here is analogous to the async I/O sockets at the OS level or in tokio, in that rustls is indicating that the caller should notify the Connection when new TLS data is available, by calling read_tls().

  2. hawkw commented on Jul 8, 2021

    @hawkw
    ContributorAuthor

    To be sure, we're talking about Connection::read() (on current main), right?

    Yes, that's correct.

    It's possible I've misinterpreted the behavior here --- in which case, thanks for clearing this up!

    I agree that returning WouldBlock is correct when additional read_tls() calls are necessary before more plaintext bytes can be read. The case I was thinking of, specifically, is one where the reader passed to read_tls returns Ok(0), indicating that the IO stream EOFed without a CloseNotify. Does Connection::read return WouldBlock in that case? If it doesn't, I may have misunderstood something.

  3. djc commented on Jul 8, 2021

    @djc
    Member

    It looks like Connection does not keep track of whether the Read passed to read_tls() has returned Ok(0), so in that case it seems like read() would still end up yielding WouldBlock because received_plaintext would run empty and we would not keep track of the fact that we'd received a non-CloseNotify EOF. That seems like something we ought to fix?

  4. hawkw commented on Jul 8, 2021

    @hawkw
    ContributorAuthor

    @djc Yeah...i ended up just having tokio-rustls track this itself so that it can handle this correctly. It would be nice if rustlsdid that instead!

  5. djc commented on Jul 8, 2021

    @djc
    Member

    If you want to review #779, that would be great!

  6. hawkw commented on Jul 9, 2021

    @hawkw
    ContributorAuthor

    If you want to review #779, that would be great!

    yup, i'll take a look!

  7. djc commented on Jul 27, 2021

    @djc
    Member

    This should be fixed by b84721e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions