From 51859aaff259bdc6a5ea66c9e56fee62e3b53034 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sun, 20 Jun 2021 22:01:36 +0200 Subject: [PATCH] Fix fd range for select on Windows Fix mbedtls_net_poll() and mbedtls_net_recv_timeout() often failing with MBEDTLS_ERR_NET_POLL_FAILED on Windows: they were testing that the file descriptor is in range for fd_set, but on Windows socket descriptors are not limited to a small range. Fixes #4465. Signed-off-by: Gilles Peskine --- ChangeLog.d/winsock.txt | 4 ++++ library/net_sockets.c | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 ChangeLog.d/winsock.txt diff --git a/ChangeLog.d/winsock.txt b/ChangeLog.d/winsock.txt new file mode 100644 index 0000000000..0b42e691c2 --- /dev/null +++ b/ChangeLog.d/winsock.txt @@ -0,0 +1,4 @@ +Bugfix + * Fix mbedtls_net_poll() and mbedtls_net_recv_timeout() often failing with + MBEDTLS_ERR_NET_POLL_FAILED on Windows. Fixes #4465. + diff --git a/library/net_sockets.c b/library/net_sockets.c index 746ed2a730..5fbe1f764a 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -145,12 +145,17 @@ static int check_fd( int fd, int for_select ) if( fd < 0 ) return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); +#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ + !defined(EFI32) + (void) for_select; +#else /* A limitation of select() is that it only works with file descriptors * that are strictly less than FD_SETSIZE. This is a limitation of the * fd_set type. Error out early, because attempting to call FD_SET on a * large file descriptor is a buffer overflow on typical platforms. */ if( for_select && fd >= FD_SETSIZE ) return( MBEDTLS_ERR_NET_POLL_FAILED ); +#endif return( 0 ); }