From patchwork Mon May 10 03:13:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1799 X-Patchwork-Delegate: a@unstable.cc Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.28.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id +EW2FdsxmWCJQAAAIUCqbw (envelope-from ) for ; Mon, 10 May 2021 09:15:07 -0400 Received: from proxy8.mail.ord1c.rsapps.net ([172.28.255.1]) by director7.mail.ord1d.rsapps.net with LMTP id qDGEFdsxmWCJMQAAovjBpQ (envelope-from ) for ; Mon, 10 May 2021 09:15:07 -0400 Received: from smtp8.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy8.mail.ord1c.rsapps.net with LMTPS id kGdSFdsxmWBDDgAAHz/atg (envelope-from ) for ; Mon, 10 May 2021 09:15:07 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp8.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: bc1c70f4-b191-11eb-acf7-782bcb03304b-1-1 Received: from [216.105.38.7] ([216.105.38.7:36836] helo=lists.sourceforge.net) by smtp8.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 80/E6-04109-9D139906; Mon, 10 May 2021 09:15:05 -0400 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1lg5jl-0000r2-8e; Mon, 10 May 2021 13:14:17 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg5ji-0000qo-Ul for openvpn-devel@lists.sourceforge.net; Mon, 10 May 2021 13:14:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=e7X7xo1dtEA4apb9ZPrC5Rc7ZqMukE6g8VOCvs2vswo=; b=Rgs8M3dXtotq2lqRsRkOPK7fE7 zjcSmiKqNQez/5zDlR8F3OjmSG3DadH/ofRYY9mQEoCDpHY1VhMIcnPDQCxlacy77M7M1RrMWld5b 1I5ReFIOUM/y95O+uLfeeFqOMaYoD/ibA+Z/5craU1lPDgC8XPPCwZ9FMEefLYktgcwE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=e7X7xo1dtEA4apb9ZPrC5Rc7ZqMukE6g8VOCvs2vswo=; b=hvvr5jDDzslgna0j7IQF138ZFZ 4uknimbE/l0j4nr0LQCHWevRMWaQn3tuahMORbCZLmH5faWlJl1qMBYjrArE9hclc8UGNS6mUHCww /oatpQQ/dLvVFZxi1FS3Zzq+1owuA1houAR4KbYM4BReTqVVw/FcJqLXfmRMvkjMWLLU=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1lg5jf-0006LL-1R for openvpn-devel@lists.sourceforge.net; Mon, 10 May 2021 13:14:15 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.94.2 (FreeBSD)) (envelope-from ) id 1lg5jQ-000P7D-Bl for openvpn-devel@lists.sourceforge.net; Mon, 10 May 2021 15:13:56 +0200 Received: (nullmailer pid 969011 invoked by uid 10006); Mon, 10 May 2021 13:13:56 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Mon, 10 May 2021 15:13:56 +0200 Message-Id: <20210510131356.968965-1-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210422151724.2132573-1-arne@rfc2549.org> References: <20210422151724.2132573-1-arne@rfc2549.org> MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1lg5jf-0006LL-1R Subject: [Openvpn-devel] [PATCH] Use exponential backoff for caching in tls_authentication_status X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox The caching in tls_authentication_status broke the quick reaction to authentication status in the code paths that did not do caching like PUSH_REQUEST reply code path. This patch introduces exponential backoff for the caching so we still retain the quick reaction while still keeping the benefit of caching. Signed-off-by: Arne Schwabe Acked-by: Antonio Quartulli --- src/openvpn/multi.c | 3 +-- src/openvpn/push.c | 2 +- src/openvpn/ssl.c | 2 +- src/openvpn/ssl.h | 3 --- src/openvpn/ssl_common.h | 5 ++++- src/openvpn/ssl_verify.c | 25 +++++++++++++++++++++++-- src/openvpn/ssl_verify.h | 8 +++----- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index ab2270a58..3f9710134 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -2596,8 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = { static void multi_connection_established(struct multi_context *m, struct multi_instance *mi) { - if (tls_authentication_status(mi->context.c2.tls_multi, TLS_MULTI_AUTH_STATUS_INTERVAL) - != TLS_AUTHENTICATION_SUCCEEDED) + if (tls_authentication_status(mi->context.c2.tls_multi) != TLS_AUTHENTICATION_SUCCEEDED) { return; } diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 671220c3f..5c6528b05 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -864,7 +864,7 @@ process_incoming_push_request(struct context *c) int ret = PUSH_MSG_ERROR; - if (tls_authentication_status(c->c2.tls_multi, TLS_MULTI_AUTH_STATUS_INTERVAL) == TLS_AUTHENTICATION_FAILED + if (tls_authentication_status(c->c2.tls_multi) == TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state == CAS_FAILED) { const char *client_reason = tls_client_reason(c->c2.tls_multi); diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 7d66cf565..4ddb02cb0 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -3133,7 +3133,7 @@ tls_multi_process(struct tls_multi *multi, update_time(); - enum tls_auth_status tas = tls_authentication_status(multi, TLS_MULTI_AUTH_STATUS_INTERVAL); + enum tls_auth_status tas = tls_authentication_status(multi); /* * If lame duck session expires, kill it. diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 2791143f6..81cc22131 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -91,9 +91,6 @@ #define TLS_MULTI_HORIZON 2 /* call tls_multi_process frequently for n seconds after * every packet sent/received action */ -/* Interval that tls_multi_process should call tls_authentication_status */ -#define TLS_MULTI_AUTH_STATUS_INTERVAL 10 - /* * Buffer sizes (also see mtu.h). */ diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 52488a163..93b01a587 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -570,10 +570,13 @@ struct tls_multi char *locked_username; struct cert_hash_set *locked_cert_hash_set; - /* Time of last when we updated the cached state of + /** Time of last when we updated the cached state of * tls_authentication_status deferred files */ time_t tas_cache_last_update; + /** The number of times we updated the cache */ + unsigned int tas_cache_num_updates; + /* * An error message to send to client on AUTH_FAILED */ diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index b1b01f777..5473ce6a4 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -1073,8 +1073,28 @@ key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached) return ACF_DISABLED; } +/** + * The minimum times to have passed to update the cache. Older versions + * of OpenVPN had code path that did not do any caching, so we start + * with no caching (0) here as well to have the same super quick initial + * reaction. + */ +static time_t cache_intervals[] = {0, 0, 0, 0, 0, 1, 1, 2, 2, 4, 8}; + +/** + * uses cache_intervals times to determine if we should update the + * cache. + */ +static bool +tls_authentication_status_use_cache(struct tls_multi *multi) +{ + unsigned int idx = min_uint(multi->tas_cache_num_updates, SIZE(cache_intervals) - 1); + time_t latency = cache_intervals[idx]; + return multi->tas_cache_last_update + latency >= now; +} + enum tls_auth_status -tls_authentication_status(struct tls_multi *multi, const int latency) +tls_authentication_status(struct tls_multi *multi) { bool deferred = false; @@ -1087,7 +1107,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency) /* at least one key already failed authentication */ bool failed_auth = false; - bool cached = multi->tas_cache_last_update + latency >= now; + bool cached = tls_authentication_status_use_cache(multi); for (int i = 0; i < KEY_SCAN_SIZE; ++i) { @@ -1143,6 +1163,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency) if (!cached) { multi->tas_cache_last_update = now; + multi->tas_cache_num_updates++; } #if 0 diff --git a/src/openvpn/ssl_verify.h b/src/openvpn/ssl_verify.h index 06b88b568..4f18660fd 100644 --- a/src/openvpn/ssl_verify.h +++ b/src/openvpn/ssl_verify.h @@ -75,7 +75,7 @@ enum tls_auth_status /** * Return current session authentication state of the tls_multi structure * This will return TLS_AUTHENTICATION_SUCCEEDED only if the session is - * fully authenicated, i.e. VPN traffic is allowed over it. + * fully authenticated, i.e. VPN traffic is allowed over it. * * Checks the status of all active keys and checks if the deferred * authentication has succeeded. @@ -84,15 +84,13 @@ enum tls_auth_status * from KS_AUTH_DEFERRED to KS_AUTH_FALSE/KS_AUTH_TRUE if the deferred * authentication has succeeded after last call. * - * @param latency if not null, return TLS_AUTHENTICATION_UNDEFINED if - * the last call for this multi struct has been less - * than latency seconds ago + * * @param multi the tls_multi struct to operate on * * @return Current authentication status of the tls_multi */ enum tls_auth_status -tls_authentication_status(struct tls_multi *multi, const int latency); +tls_authentication_status(struct tls_multi *multi); /** Check whether the \a ks \c key_state has finished the key exchange part * of the OpenVPN hand shake. This is that the key_method_2read/write