Skip to content

Commit

Permalink
Merge pull request #9715 from ThePassionate/mbedtls-1022-2.8
Browse files Browse the repository at this point in the history
[Backport 2.28] net/mbedtls_net_connect: Preventing double close problem
  • Loading branch information
davidhorstmann-arm authored Oct 22, 2024
2 parents 17d2fb8 + aa6ae3f commit f87e855
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.d/replace-close-with-mbedtls_net_close.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bugfix
* Use 'mbedtls_net_close' instead of 'close' in 'mbedtls_net_bind'
and 'mbedtls_net_connect' to prevent possible double close fd
problems. Fixes #9711.
8 changes: 4 additions & 4 deletions library/net_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
break;
}

close(ctx->fd);
mbedtls_net_close(ctx);
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
}

Expand Down Expand Up @@ -242,21 +242,21 @@ int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *
n = 1;
if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &n, sizeof(n)) != 0) {
close(ctx->fd);
mbedtls_net_close(ctx);
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}

if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
close(ctx->fd);
mbedtls_net_close(ctx);
ret = MBEDTLS_ERR_NET_BIND_FAILED;
continue;
}

/* Listen only makes sense for TCP */
if (proto == MBEDTLS_NET_PROTO_TCP) {
if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
close(ctx->fd);
mbedtls_net_close(ctx);
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
continue;
}
Expand Down

0 comments on commit f87e855

Please sign in to comment.