Skip to content

Commit fc007ee

Browse files
rlubosnashif
authored andcommitted
net: sockets: tls: Prevent infinite block during handshake
In case peer goes down or we disconnect from the network during the TLS handshake, the TLS socket may block indefinitely during connect()/accept(), waiting for data from the peer. This should be avoided, hence use the preconfigured timeout for the TLS handshake, same as we use for TCP-level handshake. Signed-off-by: Robert Lubos <[email protected]>
1 parent 086e4f8 commit fc007ee

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

subsys/net/lib/sockets/sockets_tls.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,8 @@ int ztls_connect_ctx(struct tls_context *ctx, const struct sockaddr *addr,
21812181
/* TODO For simplicity, TLS handshake blocks the socket
21822182
* even for non-blocking socket.
21832183
*/
2184-
ret = tls_mbedtls_handshake(ctx, K_FOREVER);
2184+
ret = tls_mbedtls_handshake(
2185+
ctx, K_MSEC(CONFIG_NET_SOCKETS_CONNECT_TIMEOUT));
21852186
if (ret < 0) {
21862187
goto error;
21872188
}
@@ -2238,7 +2239,8 @@ int ztls_accept_ctx(struct tls_context *parent, struct sockaddr *addr,
22382239
/* TODO For simplicity, TLS handshake blocks the socket even for
22392240
* non-blocking socket.
22402241
*/
2241-
ret = tls_mbedtls_handshake(child, K_FOREVER);
2242+
ret = tls_mbedtls_handshake(
2243+
child, K_MSEC(CONFIG_NET_SOCKETS_CONNECT_TIMEOUT));
22422244
if (ret < 0) {
22432245
goto error;
22442246
}
@@ -2379,6 +2381,9 @@ static ssize_t sendto_dtls_client(struct tls_context *ctx, const void *buf,
23792381

23802382
/* TODO For simplicity, TLS handshake blocks the socket even for
23812383
* non-blocking socket.
2384+
* DTLS handshake timeout/retransmissions are limited by
2385+
* mbed TLS, so K_FOREVER is fine here, the function will not
2386+
* block indefinitely.
23822387
*/
23832388
ret = tls_mbedtls_handshake(ctx, K_FOREVER);
23842389
if (ret < 0) {

0 commit comments

Comments
 (0)