Skip to content

Commit

Permalink
net: sockets: tls: check return code from fcntl
Browse files Browse the repository at this point in the history
Not checking return code in fcntl can result in interpreting -1 as
flags, and cause unexpected behaviour.

Fixes #35541

Signed-off-by: Emil Lindqvist <emil@lindq.gr>
  • Loading branch information
emillindq authored and carlescufi committed May 27, 2021
1 parent d475f98 commit 91177ee
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions subsys/net/lib/sockets/sockets_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,8 @@ static int dtls_rx(void *ctx, unsigned char *buf, size_t len,
uint32_t dtls_timeout)
{
struct tls_context *tls_ctx = ctx;
bool is_block = !((tls_ctx->flags & ZSOCK_MSG_DONTWAIT) ||
(zsock_fcntl(tls_ctx->sock, F_GETFL, 0) &
O_NONBLOCK));
int sock_flags = zsock_fcntl(tls_ctx->sock, F_GETFL, 0);
bool is_block;
int timeout = (dtls_timeout == 0U) ? -1 : dtls_timeout;
uint32_t entry_time = k_uptime_get_32();
socklen_t addrlen = sizeof(struct sockaddr);
Expand All @@ -576,6 +575,13 @@ static int dtls_rx(void *ctx, unsigned char *buf, size_t len,
struct zsock_pollfd fds;
int flags = tls_ctx->flags & ~ZSOCK_MSG_TRUNC;

if (sock_flags == -1) {
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}

is_block = !((tls_ctx->flags & ZSOCK_MSG_DONTWAIT) ||
(sock_flags & O_NONBLOCK));

do {
retry = false;

Expand Down Expand Up @@ -1828,8 +1834,13 @@ static ssize_t recvfrom_dtls_server(struct tls_context *ctx, void *buf,
{
int ret;
bool repeat;
bool is_block = !((flags & ZSOCK_MSG_DONTWAIT) ||
(zsock_fcntl(ctx->sock, F_GETFL, 0) & O_NONBLOCK));
int sock_flags = zsock_fcntl(ctx->sock, F_GETFL, 0);
bool is_block;

if (sock_flags == -1) {
ret = -errno;
goto error;
}

if (!ctx->is_initialized) {
ret = tls_mbedtls_init(ctx, true);
Expand All @@ -1838,6 +1849,8 @@ static ssize_t recvfrom_dtls_server(struct tls_context *ctx, void *buf,
}
}

is_block = !((flags & ZSOCK_MSG_DONTWAIT) || (sock_flags & O_NONBLOCK));

/* Loop to enable DTLS reconnection for servers without closing
* a socket.
*/
Expand Down

0 comments on commit 91177ee

Please sign in to comment.