mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-04-28 14:27:27 +02:00
Refactor file descriptor checks into a common function
This will make it easier to change the behavior uniformly. No behavior change. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
@@ -166,6 +166,26 @@ static int net_prepare( void )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Return 0 if the file descriptor is valid, an error otherwise.
|
||||
* If for_select != 0, check whether the file descriptor is within the range
|
||||
* allowed for fd_set used for the FD_xxx macros and the select() function.
|
||||
*/
|
||||
static int check_fd( int fd, int for_select )
|
||||
{
|
||||
if( fd < 0 )
|
||||
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
|
||||
|
||||
/* 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 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a context
|
||||
*/
|
||||
@@ -497,15 +517,9 @@ int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
|
||||
|
||||
int fd = ctx->fd;
|
||||
|
||||
if( fd < 0 )
|
||||
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
|
||||
|
||||
/* 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( fd >= FD_SETSIZE )
|
||||
return( MBEDTLS_ERR_NET_POLL_FAILED );
|
||||
ret = check_fd( fd, 1 );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer)
|
||||
@@ -584,8 +598,9 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
|
||||
int ret;
|
||||
int fd = ((mbedtls_net_context *) ctx)->fd;
|
||||
|
||||
if( fd < 0 )
|
||||
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
|
||||
ret = check_fd( fd, 0 );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
||||
ret = (int) read( fd, buf, len );
|
||||
|
||||
@@ -623,15 +638,9 @@ int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
|
||||
fd_set read_fds;
|
||||
int fd = ((mbedtls_net_context *) ctx)->fd;
|
||||
|
||||
if( fd < 0 )
|
||||
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
|
||||
|
||||
/* 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( fd >= FD_SETSIZE )
|
||||
return( MBEDTLS_ERR_NET_POLL_FAILED );
|
||||
ret = check_fd( fd, 1 );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
||||
FD_ZERO( &read_fds );
|
||||
FD_SET( fd, &read_fds );
|
||||
@@ -671,8 +680,9 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
|
||||
int ret;
|
||||
int fd = ((mbedtls_net_context *) ctx)->fd;
|
||||
|
||||
if( fd < 0 )
|
||||
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
|
||||
ret = check_fd( fd, 0 );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
||||
ret = (int) write( fd, buf, len );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user