Welcome! Log In Create A New Profile

Advanced

Re: [PATCH 2 of 3] Tests: handling of EAGAIN from sysread() with IO::Socket::SSL

Sergey Kandaurov
March 20, 2023 11:36AM
> On 11 Mar 2023, at 13:30, Maxim Dounin <mdounin@mdounin.ru> wrote:
>
> Hello!
>
> On Fri, Mar 10, 2023 at 08:00:05AM +0300, Maxim Dounin wrote:
>
>> # HG changeset patch
>> # User Maxim Dounin <mdounin@mdounin.ru>
>> # Date 1678424071 -10800
>> # Fri Mar 10 07:54:31 2023 +0300
>> # Node ID 49d12f8c4cf69e1cbe7feccae3b0ea1ac2ca8c2f
>> # Parent fdebeebd07b160f1d30e18d56e64dfb08570f8b1
>> Tests: handling of EAGAIN from sysread() with IO::Socket::SSL.
>>
>> With IO::Socket::SSL, when select() reports that the socket is readable,
>> reading from it might still fail with EAGAIN, since no application data is
>> available in the socket. In particular, this might happen with TLSv1.3
>> when a session ticket is received after the handshake. Fix is to explicitly
>> check for EAGAIN errors.
>
> Err, IO::Socket::SSL actually generates EWOULDBLOCK rather than
> EAGAIN, and this is important on some systems (notably Windows).
>
> s/EAGAIN/EWOULDBLOCK/g;
>
> # HG changeset patch
> # User Maxim Dounin <mdounin@mdounin.ru>
> # Date 1678522238 -10800
> # Sat Mar 11 11:10:38 2023 +0300
> # Node ID 0fefa04c5be1e8095072d176cdf847c7c3766fbf
> # Parent fdebeebd07b160f1d30e18d56e64dfb08570f8b1
> Tests: handling of EWOULDBLOCK from sysread() with IO::Socket::SSL.
>
> With IO::Socket::SSL, when select() reports that the socket is readable,
> reading from it might still fail with EWOULDBLOCK, since no application
> data is available in the socket. In particular, this might happen with
> TLSv1.3 when a session ticket is received after the handshake. Fix is
> to explicitly check for EWOULDBLOCK errors.
>
> diff --git a/lib/Test/Nginx/IMAP.pm b/lib/Test/Nginx/IMAP.pm
> --- a/lib/Test/Nginx/IMAP.pm
> +++ b/lib/Test/Nginx/IMAP.pm
> @@ -68,7 +68,9 @@ sub getline {
> while (IO::Select->new($socket)->can_read(8)) {
> $socket->blocking(0);
> my $n = $socket->sysread(my $buf, 1024);
> + my $again = !defined $n && $!{EWOULDBLOCK};
> $socket->blocking(1);
> + next if $again;
> last unless $n;
>
> $self->{_read_buffer} .= $buf;
> diff --git a/lib/Test/Nginx/POP3.pm b/lib/Test/Nginx/POP3.pm
> --- a/lib/Test/Nginx/POP3.pm
> +++ b/lib/Test/Nginx/POP3.pm
> @@ -68,7 +68,9 @@ sub getline {
> while (IO::Select->new($socket)->can_read(8)) {
> $socket->blocking(0);
> my $n = $socket->sysread(my $buf, 1024);
> + my $again = !defined $n && $!{EWOULDBLOCK};
> $socket->blocking(1);
> + next if $again;
> last unless $n;
>
> $self->{_read_buffer} .= $buf;
> diff --git a/lib/Test/Nginx/SMTP.pm b/lib/Test/Nginx/SMTP.pm
> --- a/lib/Test/Nginx/SMTP.pm
> +++ b/lib/Test/Nginx/SMTP.pm
> @@ -68,7 +68,9 @@ sub getline {
> while (IO::Select->new($socket)->can_read(8)) {
> $socket->blocking(0);
> my $n = $socket->sysread(my $buf, 1024);
> + my $again = !defined $n && $!{EWOULDBLOCK};
> $socket->blocking(1);
> + next if $again;
> last unless $n;
>
> $self->{_read_buffer} .= $buf;
> diff --git a/lib/Test/Nginx/Stream.pm b/lib/Test/Nginx/Stream.pm
> --- a/lib/Test/Nginx/Stream.pm
> +++ b/lib/Test/Nginx/Stream.pm
> @@ -84,8 +84,10 @@ sub read {
> $s = $self->{_socket};
>
> $s->blocking(0);
> - if (IO::Select->new($s)->can_read($extra{read_timeout} || 8)) {
> - $s->sysread($buf, 1024);
> + while (IO::Select->new($s)->can_read($extra{read_timeout} || 8)) {
> + my $n = $s->sysread($buf, 1024);
> + next if !defined $n && $!{EWOULDBLOCK};
> + last;
> }
>
> log_in($buf);
>

Looks good.

Note that this occurs with non-blocking sockets, as seen with diff.

From POD:
Using Non-Blocking Sockets
If you have a non-blocking socket, the expected behavior on read,
write, accept or connect is to set $! to EWOULDBLOCK if the operation
cannot be completed immediately. Note that EWOULDBLOCK is the same as
EAGAIN on UNIX systems, but is different on Windows.

This reminds me how OpenSSL has changed the SSL_MODE_AUTO_RETRY default,
then IO::Socket::SSL disabled it again on non-blocking sockets.

--
Sergey Kandaurov
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[PATCH 1 of 3] Tests: style

Maxim Dounin 504 March 10, 2023 12:02AM

[PATCH 2 of 3] Tests: handling of EAGAIN from sysread() with IO::Socket::SSL

Maxim Dounin 115 March 10, 2023 12:02AM

Re: [PATCH 2 of 3] Tests: handling of EAGAIN from sysread() with IO::Socket::SSL

Maxim Dounin 111 March 11, 2023 04:32AM

Re: [PATCH 2 of 3] Tests: handling of EAGAIN from sysread() with IO::Socket::SSL

Sergey Kandaurov 105 March 20, 2023 11:36AM

[PATCH 3 of 3] Tests: adapted session reuse tests to work with TLSv1.3

Maxim Dounin 116 March 10, 2023 12:02AM

Re: [PATCH 3 of 3] Tests: adapted session reuse tests to work with TLSv1.3

Sergey Kandaurov 96 March 20, 2023 11:50AM

Re: [PATCH 3 of 3] Tests: adapted session reuse tests to work with TLSv1.3

Maxim Dounin 147 March 20, 2023 08:00PM

Re: [PATCH 1 of 3] Tests: style

Sergey Kandaurov 102 March 20, 2023 11:36AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 308
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready