From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796495267; b=SrbjSSK9KADP1n7aOLSXSqKobeaYqqk/pU3u61wDx2NuoASxe28yP8cKfEXR9iXnWu2z sAdOodZOskfNsTrQNqX/dy5GTp9tpUzfnBNqowK7tp3+dlfhmu3gqz9HBR+P0nKbYdrHZ 5b9Anq7MWv/NsSMfK1JCftC96W4ExQr8/m9sSlxlS6wPMOhaUbpmmz1muHKmD83C+Y6Vr gradYEEWNMq5y8xMTdTdKfBbRZaFzoSuRa0ldBDiNKVhdFr/Cjyd5unr9xiQON8BUgCVu Gr+06SaGX94sitzOOoBXpfblkazCez2bUrtZ3i7h4T/i5QHSL9CRi0r1+DfVap5W74Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796495267; h=Received:DKIM-Signature:From:To:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=1ktPmz7h1VWwu1omtlEcyibKfHL5KcVWZNNrk54MkYs=; b=2JNuH+JLYfc4NaMF5HsXtPiR48mfi6Kkulh6BPwx6p35uB7GZJWjdn0Q4s/QyS20bSfh z+h8/oVGJ04XGjX51AbydKc8EXy5k916OeF3RDQ+/r0nLZXsPCz4F0o5RpjEjUZ19DwlM HACVwXxwfkMVmIVxDLaCt5YowVvzbieGq8hhO5EPdnQ8B3/oHlkQF5kmv1jqLt4JrSrXh K6LG3O8fsgDNY6O2mPCjlF+TQYOiPlJc45mU5ZK30lL9h9woNss9qKihzk8HcpHSeT3qx zxl1HYkFNzIdEJX60+xsgsF+UJdFjNlJc93EgmtPyXQIbIfsCfDY81AE7MFPU2pu8Lg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="jyWym8/l"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9979D606A31F for ; Sat, 5 Dec 2026 18:27:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jyWym8/l Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0582D1F0092B for ; Sat, 5 Dec 2026 18:27:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796495267; bh=1ktPmz7h1VWwu1omtlEcyibKfHL5KcVWZNNrk54MkYs=; h=From:To:Subject:Date:In-Reply-To:References; b=jyWym8/lk4Y9G56dO2T0t+FC9CIHFRTW3C+yF5ueFAljlt75eFFobekw29VhrsRWv +Wv1asoTX7FijM2MDLBzUybS+tMkdIUPGg0q9YKTkmx5/lDIBueYvZLaJ6O+xT86xb 1DGHBwJDP66zptz0kCTAvuhpC+6UiXM18zYq/cxLRAYOflYAj1rB7hJMh24753u6c0 lv91JP5bdlyS7zN0Vg4VMoqTNa1AFfACg4TMLoWwqo/RlgK2h4Jh5koqqWAGmxAmvv RiGjMBfnjHxwFzoa3wFMlC+vRQ1xgH5NK5hmCNHlfVlQcy1Amo30k67GFWGT9EFppA FDu2UvELRl62g== From: Chuck Lever To: Subject: [PATCH 4/8] tlshd: Limit the number of concurrent handshake processes Date: Sat, 5 Dec 2026 13:27:39 -0500 Message-ID: <20261205182743.79220-5-cel@kernel.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261205182743.79220-1-cel@kernel.org> References: <20261205182743.79220-1-cel@kernel.org> X-Mailing-List: kernel-tls-handshake@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit tlshd forks a root child for every handshake notification and does not count the children that are still running. Each connection to an in-kernel TLS server produces one notification, so a remote host that opens connections and then stalls accumulates children until their handshakes time out, consuming memory and PIDs. Run at most 256 children at a time. The kernel multicasts one notification for each request and does not repeat it, so tlshd cannot simply drop a notification at the limit. Count those notifications and fork for them as running children exit. In the meantime the requests wait on the kernel's queue, and the kernel refuses new requests once that queue is full. A peer that stalls 256 handshakes therefore delays all other handshakes until some of the stalled ones end. Replace the SIG_IGN disposition of SIGCHLD with a case in the signalfd loop that reaps children. Record the PIDs of the forked children, and lower the count only when a recorded PID is reaped. tlshd can also reap a child that it did not fork, such as one inherited across exec(2). Also log a failed fork(), which tlshd currently ignores, and keep the notification for a retry on a later event. Fixes: a4fd25b872bd ("tlshd: Introduce infrastructure for using netlink") Signed-off-by: Chuck Lever --- src/tlshd/netlink.c | 121 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 19 deletions(-) diff --git a/src/tlshd/netlink.c b/src/tlshd/netlink.c index a14fddc885ce..d7b8f9ba2162 100644 --- a/src/tlshd/netlink.c +++ b/src/tlshd/netlink.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -71,7 +72,7 @@ * The event loop in tlshd_genl_dispatch() waits on two file * descriptors: * - Netlink socket for handshake requests from the kernel - * - signalfd for SIGINT, SIGTERM, SIGHUP, and SIGUSR1 + * - signalfd for SIGINT, SIGTERM, SIGHUP, SIGUSR1, and SIGCHLD * * Signals are delivered synchronously through signalfd rather than * asynchronous signal handlers, ensuring that signal processing @@ -84,8 +85,9 @@ * * When a handshake request arrives via netlink, the parent process * forks a child to service that request. This occurs in - * tlshd_genl_accept_handler() at the call to fork(2). The child - * process: + * tlshd_start_children() at the call to fork(2). At most + * TLSHD_MAX_CHILDREN children run at once; further requests wait + * until a child exits. The child process: * - Closes the signalfd and unblocks signals * - Closes the netlink socket * - Calls tlshd_service_socket() to perform the TLS handshake @@ -93,7 +95,7 @@ * * The parent process: * - Continues running the event loop - * - Ignores SIGCHLD (children are reaped automatically) + * - Reaps children when SIGCHLD arrives * - Remains responsive to new handshake requests * - Handles configuration reload requests * @@ -446,6 +448,92 @@ static sigset_t tlshd_sig_poll_mask; */ static int tlshd_sig_poll_fd; +/** + * @def TLSHD_MAX_CHILDREN + * @brief Maximum number of concurrent handshake processes + */ +#define TLSHD_MAX_CHILDREN (256) + +/** + * @def TLSHD_MAX_PENDING + * @brief Maximum number of notifications remembered at the child limit + * + * The kernel queues at most 50 unaccepted requests. Notifications + * beyond that number belong to requests it has since cancelled. + */ +#define TLSHD_MAX_PENDING (256) + +/** + * @var unsigned int tlshd_nr_children + * Number of handshake processes not yet reaped + */ +static unsigned int tlshd_nr_children; + +/** + * @var pid_t tlshd_child_pids + * PIDs of unreaped handshake processes, in the first tlshd_nr_children + * elements + */ +static pid_t tlshd_child_pids[TLSHD_MAX_CHILDREN]; + +/** + * @var unsigned int tlshd_nr_pending + * Number of notifications for which no process has been forked + */ +static unsigned int tlshd_nr_pending; + +/** + * @brief Fork handshake processes for pending notifications + */ +static void tlshd_start_children(void) +{ + pid_t pid; + + while (tlshd_nr_pending && tlshd_nr_children < TLSHD_MAX_CHILDREN) { + pid = fork(); + if (pid < 0) { + tlshd_log_perror("fork"); + return; + } + if (!pid) { + /* child */ + close(tlshd_sig_poll_fd); + sigprocmask(SIG_UNBLOCK, &tlshd_sig_poll_mask, NULL); + + tlshd_genl_sock_close(tlshd_notification_nls); + + tlshd_service_socket(); + + tlshd_gnutls_priority_deinit(); + tlshd_config_shutdown(); + exit(EXIT_SUCCESS); + } + tlshd_nr_pending--; + tlshd_child_pids[tlshd_nr_children++] = pid; + } +} + +/** + * @brief Reap handshake processes that have exited + */ +static void tlshd_reap_children(void) +{ + unsigned int i; + pid_t pid; + + /* One SIGCHLD can report several exited children */ + while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { + /* A child inherited across exec(2) is not counted */ + for (i = 0; i < tlshd_nr_children; i++) { + if (tlshd_child_pids[i] == pid) { + tlshd_child_pids[i] = + tlshd_child_pids[--tlshd_nr_children]; + break; + } + } + } +} + /** * @brief Process one netlink notification event * @param[in] msg A netlink event to be handled @@ -472,20 +560,8 @@ static int tlshd_genl_event_handler(struct nl_msg *msg, HANDSHAKE_HANDLER_CLASS_TLSHD) return NL_SKIP; - if (!fork()) { - /* child */ - close(tlshd_sig_poll_fd); - sigprocmask(SIG_UNBLOCK, &tlshd_sig_poll_mask, NULL); - - nlmsg_free(msg); - tlshd_genl_sock_close(tlshd_notification_nls); - - tlshd_service_socket(); - - tlshd_gnutls_priority_deinit(); - tlshd_config_shutdown(); - exit(EXIT_SUCCESS); - } + if (tlshd_nr_pending < TLSHD_MAX_PENDING) + tlshd_nr_pending++; return NL_SKIP; } @@ -504,6 +580,7 @@ void tlshd_genl_dispatch(void) sigaddset(&tlshd_sig_poll_mask, SIGTERM); sigaddset(&tlshd_sig_poll_mask, SIGHUP); sigaddset(&tlshd_sig_poll_mask, SIGUSR1); + sigaddset(&tlshd_sig_poll_mask, SIGCHLD); err = tlshd_genl_sock_open(&tlshd_notification_nls); if (err) @@ -528,7 +605,8 @@ void tlshd_genl_dispatch(void) /* Detect which optional netlink attributes the kernel supports */ tlshd_detect_kernel_caps(); - if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { + /* An inherited SIG_IGN would reap children without notice */ + if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) { tlshd_log_perror("signal"); goto out_close; } @@ -580,6 +658,10 @@ void tlshd_genl_dispatch(void) case SIGUSR1: tlshd_config_reload(); continue; + case SIGCHLD: + tlshd_reap_children(); + tlshd_start_children(); + break; default: /* SIGINT or SIGTERM: exit */ goto out_sig; @@ -592,6 +674,7 @@ void tlshd_genl_dispatch(void) tlshd_log_nl_error("nl_recvmsgs", err); break; } + tlshd_start_children(); } } -- 2.56.0