All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chuck Lever <cel@kernel.org>
To: <kernel-tls-handshake@lists.linux.dev>
Subject: [PATCH 4/8] tlshd: Limit the number of concurrent handshake processes
Date: Sat,  5 Dec 2026 13:27:39 -0500	[thread overview]
Message-ID: <20261205182743.79220-5-cel@kernel.org> (raw)
In-Reply-To: <20261205182743.79220-1-cel@kernel.org>

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 <cel@kernel.org>
---
 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 <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/signalfd.h>
+#include <sys/wait.h>
 #include <netinet/in.h>
 
 #include <stdbool.h>
@@ -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


  parent reply	other threads:[~2026-12-05 18:27 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 18:27 [PATCH 0/8] More security-related fixes Chuck Lever
2026-12-05 18:27 ` [PATCH 1/8] tlshd: Unlink keyrings that a reload removes from the configuration Chuck Lever
2026-12-05 18:27 ` [PATCH 2/8] tlshd: Escape client PSK identities before logging them Chuck Lever
2026-12-05 18:27 ` [PATCH 3/8] tlshd: Escape the client certificate description before logging it Chuck Lever
2026-12-05 18:27 ` Chuck Lever [this message]
2026-12-05 18:27 ` [PATCH 5/8] tlshd: Enforce the QUIC handshake timeout in the event loop Chuck Lever
2026-12-05 18:27 ` [PATCH 6/8] tlshd: Strip whitespace from keyring names Chuck Lever
2026-12-05 18:27 ` [PATCH 7/8] tlshd: Escape certificate names before logging tag filter results Chuck Lever
2026-12-05 18:27 ` [PATCH 8/8] tlshd: Skip the session ticket after a failed QUIC handshake Chuck Lever

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205182743.79220-5-cel@kernel.org \
    --to=cel@kernel.org \
    --cc=kernel-tls-handshake@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.