From patchwork Mon Apr 12 06:45:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maximilian Fillinger X-Patchwork-Id: 1733 X-Patchwork-Delegate: a@unstable.cc Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director13.mail.ord1d.rsapps.net ([172.27.255.55]) by backend30.mail.ord1d.rsapps.net with LMTP id +DSTA6h5dGALNAAAIUCqbw (envelope-from ) for ; Mon, 12 Apr 2021 12:47:36 -0400 Received: from proxy15.mail.iad3a.rsapps.net ([172.27.255.55]) by director13.mail.ord1d.rsapps.net with LMTP id MMNeA6h5dGAiJgAA91zNiA (envelope-from ) for ; Mon, 12 Apr 2021 12:47:36 -0400 Received: from smtp7.gate.iad3a ([172.27.255.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy15.mail.iad3a.rsapps.net with LMTPS id IIQYOad5dGAVEQAAHi9b9g (envelope-from ) for ; Mon, 12 Apr 2021 12:47:35 -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: smtp7.gate.iad3a.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; dkim=fail (key not found in DNS) header.d=foxcrypto.com; dmarc=fail (p=none; dis=none) header.from=foxcrypto.com X-Suspicious-Flag: YES X-Classification-ID: c7eb2db0-9bae-11eb-8d4b-525400bbebb8-1-1 Received: from [216.105.38.7] ([216.105.38.7:47682] helo=lists.sourceforge.net) by smtp7.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 4F/D2-07439-7A974706; Mon, 12 Apr 2021 12:47:35 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.92.3) (envelope-from ) id 1lVzi7-00011Y-2c; Mon, 12 Apr 2021 16:46:51 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1lVzi5-00011Q-6u for openvpn-devel@lists.sourceforge.net; Mon, 12 Apr 2021 16:46:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:MIME-Version:References:In-Reply-To: Date:Subject:CC:To:From:Sender:Reply-To:Message-ID:Content-Transfer-Encoding: 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=c13pvFwSi7+r3xOX013wqYYSdnTRFGPaL2WTpfy8tzQ=; b=BaHsPQxqljHLW4h7Fr65l/KHNo Ws+Nnx9tpwrbeMWaZHgPL8BKaSXxpOTMosh89NWJMxmW/fWZgCjzh96DK23m92nvpZg64AZTGhIAE NOHvyXSmNNadORsKEyMppg+xpAns3ga7zvNW41bt5usOYBwI9Ut5NYScdzVdh2bpwX8Y=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:MIME-Version:References:In-Reply-To:Date:Subject:CC:To:From: Sender:Reply-To:Message-ID:Content-Transfer-Encoding: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=c13pvFwSi7+r3xOX013wqYYSdnTRFGPaL2WTpfy8tzQ=; b=C2RpaIbEkL+Fwf7ZH9CunLDiia uE6udeeSTMFd+JaHico569si31N+7a3I+sZdHi/wC2hZjtl24CyL5cliNVllc6p9zwZqkY4YH6h62 5Xi3Jn3Gix66lrcdZ4gD8h7qr9H3GMYOUGEpz1fCzSOQYjZY2HASPaKmG4oq2P79XRnI=; Received: from nl-dft-mx-01.fox-it.com ([178.250.144.135]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1lVzi0-0008K6-3t for openvpn-devel@lists.sourceforge.net; Mon, 12 Apr 2021 16:46:48 +0000 From: Max Fillinger To: Date: Mon, 12 Apr 2021 18:45:44 +0200 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210412164545.11151-1-maximilian.fillinger@foxcrypto.com> References: <20210412164545.11151-1-maximilian.fillinger@foxcrypto.com> MIME-Version: 1.0 X-ClientProxiedBy: FOXDFT1EX01.FOX.local (10.0.0.129) To FOXDFT1EX01.FOX.local (10.0.0.129) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; d=foxcrypto.com; s=NL-DFT-MX-01; c=relaxed/relaxed; h=from:to:cc:subject:date:references:mime-version:content-type; bh=c13pvFwSi7+r3xOX013wqYYSdnTRFGPaL2WTpfy8tzQ=; b=e2dUffFBabvlpagFWjTc1yzWeqJym+IPKWBOIoytCFrvdaEImM7K1n4I0R1L3nzh0UmUx7dpMD+g fj5MsWoJl5mjE07JlqYKEloi4uDTOJd3SHCK5mLvwVqIkUlXwTtJIZA10qtLj3416r92LgZ/Wpy3 NEluX9LqmVJDTPeTbsh2gAmZXpWndvZaXUfaLLT2lpXwYqZysrOZXJMemkLasQ624STTUQXr19Ao SnXWwgk12mZ73do+U0UXyO7zUuX13mp6V/w19Ac+Opt4cbPyssi5SGSdm5YKUSwQlXiBP9rmsq0I wvDGjhFOv8sSwyDtsySwdtRfAjNIwdPB345VPQ== X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid 1.0 MISSING_MID Missing Message-Id: header 0.1 DKIM_INVALID DKIM or DK signature exists, but is not valid X-Headers-End: 1lVzi0-0008K6-3t Subject: [Openvpn-devel] [PATCH v2 1/2] In init_ssl, open the correct CRL path pre-chroot 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 Message-Id: X-getmail-retrieved-from-mailbox: Inbox When using the chroot option, the init_ssl function can be called before entering the chroot or, when OpenVPN receives a SIGHUP, afterwards. This commit ensures that OpenVPN tries to open the correct path for the CRL file in either situations. This commit does not address key and certificate files. For these, the --persist-key option should be used. Signed-off-by: Max Fillinger --- src/openvpn/init.c | 3 ++- src/openvpn/misc.c | 11 +++++++++++ src/openvpn/misc.h | 7 +++++++ src/openvpn/options.c | 8 +------- src/openvpn/ssl.c | 20 ++++++++++++++++++-- src/openvpn/ssl.h | 2 +- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 28d183aa..ba38e548 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2702,7 +2702,8 @@ do_init_crypto_tls_c1(struct context *c) * Initialize the OpenSSL library's global * SSL context. */ - init_ssl(options, &(c->c1.ks.ssl_ctx)); + bool in_chroot = (c->c0 != NULL) ? c->c0->uid_gid_chroot_set : false; + init_ssl(options, &(c->c1.ks.ssl_ctx), in_chroot); if (!tls_ctx_initialised(&c->c1.ks.ssl_ctx)) { switch (auth_retry_get()) diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c index feaefb3b..a64803a7 100644 --- a/src/openvpn/misc.c +++ b/src/openvpn/misc.c @@ -767,3 +767,14 @@ get_num_elements(const char *string, char delimiter) return element_count; } + +struct buffer +prepend_chroot(const char *chroot_dir, const char *path, struct gc_arena *gc) +{ + size_t len = strlen(chroot_dir) + strlen(PATH_SEPARATOR_STR) + strlen(path) + 1; + struct buffer combined_path = alloc_buf_gc(len, gc); + buf_printf(&combined_path, "%s%s%s", chroot_dir, PATH_SEPARATOR_STR, path); + ASSERT(combined_path.len > 0); + + return combined_path; +} diff --git a/src/openvpn/misc.h b/src/openvpn/misc.h index 9b018eb5..9f352754 100644 --- a/src/openvpn/misc.h +++ b/src/openvpn/misc.h @@ -190,4 +190,11 @@ void output_peer_info_env(struct env_set *es, const char *peer_info); int get_num_elements(const char *string, char delimiter); +/** + * Prepend the chroot directory to a path. Used when we need to access + * a file in the chroot directory before we have chroot-ed. + */ +struct buffer +prepend_chroot(const char *chroot_dir, const char *path, struct gc_arena *gc); + #endif /* ifndef MISC_H */ diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 9e61b1e0..d6ba2938 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -3317,14 +3317,8 @@ check_file_access_chroot(const char *chroot, const int type, const char *file, c { struct gc_arena gc = gc_new(); struct buffer chroot_file; - int len = 0; - - /* Build up a new full path including chroot directory */ - len = strlen(chroot) + strlen(PATH_SEPARATOR_STR) + strlen(file) + 1; - chroot_file = alloc_buf_gc(len, &gc); - buf_printf(&chroot_file, "%s%s%s", chroot, PATH_SEPARATOR_STR, file); - ASSERT(chroot_file.len > 0); + chroot_file = prepend_chroot(chroot, file, &gc); ret = check_file_access(type, BSTR(&chroot_file), mode, opt); gc_free(&gc); } diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index d8662d00..8782b140 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -584,7 +584,7 @@ tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, * All files are in PEM format. */ void -init_ssl(const struct options *options, struct tls_root_ctx *new_ctx) +init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_chroot) { ASSERT(NULL != new_ctx); @@ -702,7 +702,23 @@ init_ssl(const struct options *options, struct tls_root_ctx *new_ctx) /* Read CRL */ if (options->crl_file && !(options->ssl_flags & SSLF_CRL_VERIFY_DIR)) { - tls_ctx_reload_crl(new_ctx, options->crl_file, options->crl_file_inline); + /* If we're running with the chroot option, we may run init_ssl() before + * and after chroot-ing. We can use the crl_file path as-is if we're + * not going to chroot, or if we already are inside the chroot. + * + * If we're going to chroot later, we need to prefix the path of the + * chroot directory to crl_file. */ + if (options->chroot_dir == NULL || in_chroot || options->crl_file_inline) + { + tls_ctx_reload_crl(new_ctx, options->crl_file, options->crl_file_inline); + } + else + { + struct gc_arena gc = gc_new(); + struct buffer crl_file_buf = prepend_chroot(options->chroot_dir, options->crl_file, &gc); + tls_ctx_reload_crl(new_ctx, BSTR(&crl_file_buf), options->crl_file_inline); + gc_free(&gc); + } } /* Once keys and cert are loaded, load ECDH parameters */ diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 300a70d3..45ebe720 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -159,7 +159,7 @@ void free_ssl_lib(void); * Build master SSL context object that serves for the whole of OpenVPN * instantiation */ -void init_ssl(const struct options *options, struct tls_root_ctx *ctx); +void init_ssl(const struct options *options, struct tls_root_ctx *ctx, bool in_chroot); /** @addtogroup control_processor * @{ */