From ad2375ffe9cfcf5a7bd50bd15deeb598b14f45aa Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sun, 4 Dec 2022 22:57:09 +0100 Subject: [PATCH] fix: potential endless loop under extremely high load If there are more connections waiting to be accepted than MAX_INCOMING_CONNECTIONS existing accepted connections are overwritten. --- other/bootstrap_daemon/docker/tox-bootstrapd.sha256 | 2 +- toxcore/TCP_server.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 4a40c693..217df156 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -5cf49ad258527f6a2734a9d1f863084f66189338f47ca9d0a49482b4217577fb /usr/local/bin/tox-bootstrapd +2f7cc0e1acb07697f55fce064328e8193e54bd20189370b0c56c3b2264bdfa24 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 6b813226..ecd557d1 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -1033,12 +1033,15 @@ TCP_Server *new_TCP_server(const Logger *logger, const Random *rng, const Networ non_null() static void do_TCP_accept_new(TCP_Server *tcp_server) { - for (uint32_t i = 0; i < tcp_server->num_listening_socks; ++i) { - Socket sock; + for (uint32_t sock_idx = 0; sock_idx < tcp_server->num_listening_socks; ++sock_idx) { - do { - sock = net_accept(tcp_server->ns, tcp_server->socks_listening[i]); - } while (accept_connection(tcp_server, sock) != -1); + for (uint32_t connection_idx = 0; connection_idx < MAX_INCOMING_CONNECTIONS; ++connection_idx) { + const Socket sock = net_accept(tcp_server->ns, tcp_server->socks_listening[sock_idx]); + + if (accept_connection(tcp_server, sock) == -1) { + break; + } + } } } #endif