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=1796495268; b=t6YaxE4BKDCl0g3b7+mOmfE3BpmoHkrHuPT10d+EXE7pjBmss3pDcY7HahbFlQuJ96On BvH/zWpoaRcBVxWJH+PWkOHaJRctzI51u8CNu2OmbhFPvG8oti21FmJhHIBHL5yyV7XsP 69PJ/RasXsL525kbBDjOwJQOb/S0oFQJgtbK9NxCsN77iOwZfcP8wlFpVZnY5LVN5ecBt jnxzxKBRL3O5nIW4KB/Lu7D/WORafkd3cOGr9Sg2Hl05KIQ9S1zMC7QVoPj9ZTpvXxL0p zCRzT/sbJkYVWNh5006yg4Pc2GGtbYu2UfURBz7QZPa/ixOxqKD7kWUwoFc50IjR8aQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796495268; h=Received:DKIM-Signature:From:To:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=5RwmQOiKi57EEZuCeDayn2wNbrvE0shSiT5AmJ9kaU0=; b=nUwpLshDs+jFJvP104Q1nX4DSriPxZuAn9/72wKrm/Lm2wKHKqGroHKRSHefJ6gAM2Jn 6f8ZJL75vePxw2yXA/Bu219ET5Cd3eJdIXk5TKweyMy5i90tlQjhHHUuFd25DKnm+TaiK axWz/IQuMwIBxVzOvUgubv7zhIS95sQPaCFSlvpMIgGCc0dMA2yzVj9b/uWaRbcNNEeWU DgsM08OoNDQwEHwZ+AO0qv/tR0zPnPoWnGajKypXFzO330P5lphwZ6bGmfzzpFi3mLS72 DErRK5fot3Vud9KnreWoJ4r+Nw9WZJNg233c+FdX2r3mYMLPU/CkqRYV4AgwZn7EJvA== 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=YOGNOuxr; 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 E674A606A31F 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=YOGNOuxr Received: by smtp.kernel.org (Postfix) with ESMTPSA id 827901F00936 for ; Sat, 5 Dec 2026 18:27:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796495267; bh=5RwmQOiKi57EEZuCeDayn2wNbrvE0shSiT5AmJ9kaU0=; h=From:To:Subject:Date:In-Reply-To:References; b=YOGNOuxrI53vA5RGrykgAGH3A6JVcvJZUGlGz3/MXZgIxnA6aSsznhDoz0ybNXVPT PCtV5IYYKKfjrNZcgmMM5zi6rI97IMao3Wq5N9h9Qc27xR2hiEQeJQsW3wvFNZJ+yl h0rgSBSGRYQoZG/cL0jUdM0APOAI4QV6dRU3IwauPqUlI/0Htt1NluF8YN49QIhmlY h/mFj+S4BV4CC5WwLlGFZ9uLkBDtSC4XTihMwQvTFCjamGhwJAHoB+Mok7IWstAJsB W3LFhJQHtDhEbSRudlLSgS86PHWX9PDnQo8p18FwiZhLLd3GutUaxZgMw5SwZwAGjB son9d0ZHlFVog== From: Chuck Lever To: Subject: [PATCH 5/8] tlshd: Enforce the QUIC handshake timeout in the event loop Date: Sat, 5 Dec 2026 13:27:40 -0500 Message-ID: <20261205182743.79220-6-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 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 --- 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