mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-09 19:14:02 +00:00
net/mbedtls_net_connect: Preventing double close problem
In the test examples and real usage scenarios, 'mbedtls_net_free' is called after 'mbedtls_net_connect' fails, which will cause the problem of double close the same fd. It is possible to close this closed fd which has been applied by other link. Signed-off-by: makejian <makejian@xiaomi.com>
This commit is contained in:
parent
2ca6c285a0
commit
66b39d4cf1
4
ChangeLog.d/replace-close-with-mbedtls_net_close.txt
Normal file
4
ChangeLog.d/replace-close-with-mbedtls_net_close.txt
Normal file
@ -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.
|
@ -190,7 +190,7 @@ int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(ctx->fd);
|
mbedtls_net_close(ctx);
|
||||||
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
|
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;
|
n = 1;
|
||||||
if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
|
if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
|
||||||
(const char *) &n, sizeof(n)) != 0) {
|
(const char *) &n, sizeof(n)) != 0) {
|
||||||
close(ctx->fd);
|
mbedtls_net_close(ctx);
|
||||||
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
|
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;
|
ret = MBEDTLS_ERR_NET_BIND_FAILED;
|
||||||
continue;
|
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 */
|
/* Listen only makes sense for TCP */
|
||||||
if (proto == MBEDTLS_NET_PROTO_TCP) {
|
if (proto == MBEDTLS_NET_PROTO_TCP) {
|
||||||
if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
|
if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
|
||||||
close(ctx->fd);
|
mbedtls_net_close(ctx);
|
||||||
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
|
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user