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 5/8] tlshd: Enforce the QUIC handshake timeout in the event loop
Date: Sat,  5 Dec 2026 13:27:40 -0500	[thread overview]
Message-ID: <20261205182743.79220-6-cel@kernel.org> (raw)
In-Reply-To: <20261205182743.79220-1-cel@kernel.org>

The QUIC handshake timeout is a SIGEV_THREAD timer whose handler
writes conn->errcode from a separate thread.
tlshd_quic_conn_destroy() calls timer_delete() and then frees conn,
but timer_delete() does not wait for a handler that has already
started. If the timer expires as the handshake ends, the handler can
still be inside its log call while conn is freed, and the handler
then writes ETIMEDOUT into freed memory. A remote peer can aim for
that window by delaying its last message until the timeout is near.

Replace the timer with a CLOCK_MONOTONIC deadline that the handshake
loop checks, and shorten the select() timeout when less than a second
remains. A deadline that passes during the wait for a session ticket,
after the handshake has completed, no longer sets conn->errcode.

Fixes: 43a15fed2f33 ("tlshd: add support for quic handshake")
Signed-off-by: Chuck Lever <cel@kernel.org>
---
 src/tlshd/quic.c  | 84 ++++++++++++++++++++---------------------------
 src/tlshd/tlshd.h |  2 +-
 2 files changed, 36 insertions(+), 50 deletions(-)

diff --git a/src/tlshd/quic.c b/src/tlshd/quic.c
index f845b21540f6..801a7d777a94 100644
--- a/src/tlshd/quic.c
+++ b/src/tlshd/quic.c
@@ -36,54 +36,37 @@
 
 #ifdef HAVE_GNUTLS_QUIC
 /**
- * @brief Callback to handle a timer expiry
- * @param[in,out] arg  user data
- */
-static void quic_timer_handler(union sigval arg)
-{
-	struct tlshd_quic_conn *conn = arg.sival_ptr;
-
-	tlshd_log_error("conn timeout error %d", ETIMEDOUT);
-	conn->errcode = ETIMEDOUT;
-}
-
-/**
- * @brief Set a handshake timer
+ * @brief Set the handshake deadline
  * @param[in,out] conn  QUIC handshake context
  *
- * @retval 0   Timer configured successfully
- * @retval -1  Failed to configure timer
+ * A timeout of zero leaves the handshake without a deadline.
  */
-static int quic_conn_setup_timer(struct tlshd_quic_conn *conn)
+static void quic_conn_set_deadline(struct tlshd_quic_conn *conn)
 {
-	uint64_t msec = conn->parms->timeout_ms;
-	struct itimerspec its = {};
-	struct sigevent sev = {};
+	gint64 msec = conn->parms->timeout_ms;
 
-	sev.sigev_notify = SIGEV_THREAD;
-	sev.sigev_notify_function = quic_timer_handler;
-	sev.sigev_value.sival_ptr = conn;
-	if (timer_create(CLOCK_REALTIME, &sev, &conn->timer)) {
-		tlshd_log_error("timer creation error %d", errno);
-		return -1;
-	}
-
-	its.it_value.tv_sec  = msec / 1000;
-	its.it_value.tv_nsec = (msec % 1000) * 1000000;
-	if (timer_settime(conn->timer, 0, &its, NULL)) {
-		tlshd_log_error("timer setup error %d", errno);
-		return -1;
-	}
-	return 0;
+	if (msec)
+		conn->deadline = g_get_monotonic_time() + msec * 1000;
 }
 
 /**
- * @brief Delete a handshake timer
- * @param[in,out] conn  QUIC handshake context
+ * @brief Compute how long to wait for the next handshake message
+ * @param[in]     conn  QUIC handshake context
+ * @param[out]    tv    One second, or less if the deadline is nearer
  */
-static void quic_conn_delete_timer(struct tlshd_quic_conn *conn)
+static void quic_conn_wait_time(const struct tlshd_quic_conn *conn,
+				struct timeval *tv)
 {
-	timer_delete(conn->timer);
+	gint64 usec = G_USEC_PER_SEC;
+
+	if (conn->deadline) {
+		/* CLAMP() evaluates its arguments more than once */
+		gint64 left = conn->deadline - g_get_monotonic_time();
+
+		usec = CLAMP(left, 0, G_USEC_PER_SEC);
+	}
+	tv->tv_sec = usec / G_USEC_PER_SEC;
+	tv->tv_usec = usec % G_USEC_PER_SEC;
 }
 
 /**
@@ -537,13 +520,21 @@ static int quic_handshake_recvmsg(int sockfd, struct tlshd_quic_msg *msg)
 
 /**
  * @brief Predicate: Has the QUIC handshake completed
- * @param[in]     conn     QUIC handshake context
+ * @param[in,out] conn     QUIC handshake context
  *
- * @retval true   The handshake completed
- * @retval false  The handshake has not completed
+ * Sets conn->errcode to ETIMEDOUT if the deadline has passed on an
+ * unfinished handshake.
+ *
+ * @retval true   The handshake completed or failed
+ * @retval false  The handshake is still in progress
  */
-static bool quic_handshake_completed(const struct tlshd_quic_conn *conn)
+static bool quic_handshake_completed(struct tlshd_quic_conn *conn)
 {
+	if (!conn->completed && !conn->errcode && conn->deadline &&
+	    g_get_monotonic_time() >= conn->deadline) {
+		tlshd_log_error("conn timeout error %d", ETIMEDOUT);
+		conn->errcode = ETIMEDOUT;
+	}
 	return conn->completed || conn->errcode;
 }
 
@@ -613,10 +604,7 @@ int tlshd_quic_conn_create(struct tlshd_quic_conn **conn_p, struct tlshd_handsha
 		goto err;
 	}
 
-	if (quic_conn_setup_timer(conn)) {
-		ret = -errno;
-		goto err;
-	}
+	quic_conn_set_deadline(conn);
 
 	*conn_p = conn;
 	return 0;
@@ -639,7 +627,6 @@ void tlshd_quic_conn_destroy(struct tlshd_quic_conn *conn)
 		msg = conn->send_list;
 	}
 
-	quic_conn_delete_timer(conn);
 	gnutls_deinit(conn->session);
 	free(conn);
 }
@@ -775,8 +762,7 @@ void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn)
 		/* select() modifies readfds and tv on return */
 		FD_ZERO(&readfds);
 		FD_SET(sockfd, &readfds);
-		tv.tv_sec = 1;
-		tv.tv_usec = 0;
+		quic_conn_wait_time(conn, &tv);
 		ret = select(sockfd + 1, &readfds, NULL,  NULL, &tv);
 		if (ret < 0) {
 			conn->errcode = errno;
diff --git a/src/tlshd/tlshd.h b/src/tlshd/tlshd.h
index 1a8a4419d4c2..28c0c44f3bf1 100644
--- a/src/tlshd/tlshd.h
+++ b/src/tlshd/tlshd.h
@@ -231,7 +231,7 @@ struct tlshd_quic_conn {
 	uint8_t cert_req:2;
 	uint8_t is_serv:1;
 	uint32_t errcode;
-	timer_t timer;
+	gint64 deadline;	/* g_get_monotonic_time() units, 0 if none */
 
 	struct tlshd_quic_msg *send_list;
 	struct tlshd_quic_msg *send_last;
-- 
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 ` [PATCH 4/8] tlshd: Limit the number of concurrent handshake processes Chuck Lever
2026-12-05 18:27 ` Chuck Lever [this message]
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-6-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.