diff --git a/ChangeLog.d/replace-close-with-mbedtls_net_close.txt b/ChangeLog.d/replace-close-with-mbedtls_net_close.txt new file mode 100644 index 000000000000..80cea6772bd7 --- /dev/null +++ b/ChangeLog.d/replace-close-with-mbedtls_net_close.txt @@ -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. \ No newline at end of file diff --git a/library/net_sockets.c b/library/net_sockets.c index edec5876ad8a..e466a69b710c 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -190,7 +190,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; } @@ -237,13 +237,13 @@ 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; } @@ -251,7 +251,7 @@ int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char * /* 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; }