In init_ssl, open the correct CRL path pre-chroot
A message that is not in the archive (sent off-list or not yet received): [email protected]...
A message that is not in the archive (sent off-list or not yet received): [email protected]...
Hi,
Thanks a lot for reworking your contribution!
Quoted 23 lines · Max Fillinger
When using the chroot option, the
init_sslfunction 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 <[email protected]...>--- 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(-)src/openvpn/init.c +0
-0
src/openvpn/init.c do_init_crypto_tls_c1(struct context *c) · line 2706
Quoted 6 lines
src/openvpn/init.c +2
-1@@ -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);
Our codestyle (that is not yet 100% consistent all over the place...) prefers checks for pointers to simply be "if (ptr)" or "if (!ptr)". Explicit comparison with NULL is not required.
This said, I would simplify these two lines above as:
2706 init_ssl(options, &(c->c1.ks.ssl_ctx),
2707 c->c0 && c->c0->uid_gid_chroot_set);
(I removed the local variable, but that's more a matter of taste)
Quoted 8 lines
src/openvpn/init.c +0
-0if (!tls_ctx_initialised (&c->c1.ks.ssl_ctx)) { switch (auth_retry_get())src/openvpn/misc.c +0
-0
src/openvpn/misc.c get_num_elements(const char *string, char delimiter) · line 773
Quoted 7 lines
src/openvpn/misc.c +4
-0@@ -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) +{
maybe we should just call this prepend_dir(dir, path, gc) ? After all this is just doing a dir + '/' + filename; the fact that the dir is the chroot-dir is due to the usecase, not about what the function does.
Quoted 57 lines
src/openvpn/misc.c +7
-0+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; +}src/openvpn/misc.h +7
-0@@ -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 */ src/openvpn/options.c +1
-7@@ -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); }src/openvpn/ssl.c +1
-1@@ -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);
src/openvpn/ssl.c init_ssl(const struct options *options, struct tls_root_ctx *new_ctx) · line 707
Quoted 7 lines
src/openvpn/ssl.c +3
-1@@ -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" not going to chroot ?
src/openvpn/ssl.c init_ssl(const struct options *options, struct tls_root_ctx *new_ctx) · line 710
Quoted 3 lines
src/openvpn/ssl.c +3
-0+* +* If we're going to chroot later, we need to prefix the path of the +* chroot directory to crl_file. */
multiline comments should always end with */ on the last line
src/openvpn/ssl.c init_ssl(const struct options *options, struct tls_root_ctx *new_ctx) · line 721
Quoted 11 lines
src/openvpn/ssl.c +11
-0+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); + }
Arne: any opinion on using c->c2.gc here? init_ssl() is invoked once per instance and re-invoked upon SIGUSR1. I believe it is fine to use the c2's gc?
Assuming the answer to the above is true, I would suggest the following simplification (compile tested only!):
704 if (options->crl_file && !(options->ssl_flags &SSLF_CRL_VERIFY_DIR))
705 {
706 const char *crl_data = options->crl_file;
707
708 /* If we're running with the chroot option, we may runinit_ssl() before
709 * and after chroot-ing. We can use the crl_file path as-is
if we're
710 * not going to chroot, or if we already are inside the chroot. 711 * 712 * If we're going to chroot later, we need to prefix the
path of the
713 * chroot directory to crl_file.
714 */
715 if (!in_chroot && !options->crl_file_inline &&options->chroot_dir)
716 {
717 struct buffer crl_file_buf =
prepend_chroot(options->chroot_dir,
718options->crl_file, gc);
719 crl_data = BSTR(&crl_file_buf);
720 }
721
722 tls_ctx_reload_crl(new_ctx, crl_data,options->crl_file_inline);
723 }
what do you think?
Quoted 9 lines
src/openvpn/ssl.c +0
-0}/* Once keys and cert are loaded, load ECDH parameters */ src/openvpn/ssl.h +0
-0
src/openvpn/ssl.h void free_ssl_lib(void); · line 165
Quoted 8 lines
src/openvpn/ssl.h +1
-1@@ -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 * @{ */
Regards,
Signature · 1 line
Antonio Quartulli
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +11
-0+ 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); + }
Quoted 4 lines
Arne: any opinion on using
c->c2.gchere?init_ssl()is invoked once per instance and re-invoked upon SIGUSR1. I believe it is fine to use the c2's gc?
I would prefer a local gc here. It is easier to see that the memory can be freed right now and we don't add this unecessary depenendency on c2. Also c2 is not clear on renegotiations, so we will collect one string per renegotiation here.
Arne
Quoted 19 lines · Arne Schwabe
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +11
-0+ 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); + }Quoted 4 lines
Arne: any opinion on using
c->c2.gchere?init_ssl()is invoked once per instance and re-invoked upon SIGUSR1. I believe it is fine to use the c2's gc?
Quoted 4 lines
I would prefer a local gc here. It is easier to see that the memory can be freed right now and we don't add this unecessary depenendency on c2. Also c2 is not clear on renegotiations, so we will collect one string per renegotiation here.
This routine is invoked upon SIGUSR1, not upon renegotiation. And I believe c2 is cleaned upon SIGUSR1. SO I would consider this memory allocation the same as other options we have.
However, I do agree with your point that limiting the scope is better.
Regards,
Signature · 1 line
Antonio Quartulli
PATCH v3 1/2 In init_ssl, open the correct CRL path pre-chroot
In reply to Antonio QuartulliWhen 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 situation.
This commit does not address key and certificate files. For these, the --persist-key option should be used.
--- src/openvpn/init.c | 2 +- src/openvpn/misc.c | 11 +++++++++++ src/openvpn/misc.h | 6 ++++++ src/openvpn/options.c | 8 +------- src/openvpn/ssl.c | 21 +++++++++++++++++++-- src/openvpn/ssl.h | 2 +- 6 files changed, 39 insertions(+), 11 deletions(-)
src/openvpn/init.c +1 -1
@@ -2702,7 +2702,7 @@ do_init_crypto_tls_c1(struct context *c)* Initialize the OpenSSL library's global * SSL context. */ -init_ssl (options, &(c->c1.ks.ssl_ctx)); +init_ssl (options, &(c->c1.ks.ssl_ctx), c->c0 && c->c0->uid_gid_chroot_set);if (!tls_ctx_initialised (&c->c1.ks.ssl_ctx)) { switch (auth_retry_get())
src/openvpn/misc.c +11 -0
@@ -767,3 +767,14 @@ get_num_elements(const char *string, char delimiter)return element_count; } + +struct buffer +prepend_dir (const char *dir,const char *path,struct gc_arena *gc) +{ +size_t len =strlen (dir) +strlen (PATH_SEPARATOR_STR ) +strlen (path) +1 ; +struct buffer combined_path =alloc_buf_gc (len, gc); +buf_printf (&combined_path,"%s%s%s" , dir,PATH_SEPARATOR_STR , path); +ASSERT (combined_path.len >0 ); + +return combined_path; +}
src/openvpn/misc.h +6 -0
@@ -190,4 +190,10 @@ void output_peer_info_env(struct env_set *es, const char *peer_info);int get_num_elements (const char *string,char delimiter); +/** +* Prepend a directory to a path. +*/ +struct buffer +prepend_dir (const char *dir,const char *path,struct gc_arena *gc); +#endif /* ifndef MISC_H */
src/openvpn/options.c +1 -7
@@ -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_dir (chroot, file, &gc); ret =check_file_access (type,BSTR (&chroot_file), mode, opt);gc_free (&gc); }
src/openvpn/ssl.c +19 -2
@@ -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,24 @@ 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 || 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_dir (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 */
src/openvpn/ssl.h +1 -1
@@ -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 * @{ */
Signature · 1 line
2.11.0
Hi,
Quoted 9 lines · Max Fillinger
When using the chroot option, the
init_sslfunction 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 situation.This commit does not address key and certificate files. For these, the --persist-key option should be used.
Signed-off-by Max Fillinger <[email protected]...>
Compile tested against my zoo of SSL libraries and got no complaint. GitLab CI did not complain either.
I reproduced the bug by having the file reachable by the option parser (chroot+crl_path), but unreachable by the first run of init_ssl() (no chroot included in the path here).
I could see that this patch addresses this issue and prevent the first init_ssl() from failing.
Subsequent CRL reloads also work as expected.
Being this a bugfix for chroot, it should be merged to 2.5 too, if possible.
Regards,
Signature · 1 line
Antonio Quartulli
Looking at the code change looks reasonable.
I have not tested this beyond a basic "it compiles with 2.5 as well" test.
Your patch has been applied to the master and release/2.5 branch (bugfix).
commit 21a0b2494e7f4f1c6325b2972743158acad4f394 (master) commit d91c14e815820d04c022677722f6f096b88770d6 (HEAD -> release/2.5)
Author: Max Fillinger Date: Thu Apr 15 11:12:48 2021 +0200
In init_ssl, open the correct CRL path pre-chroot
Signed-off-by: Max Fillinger <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22117.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
A message that is not in the archive (sent off-list or not yet received): [email protected]...
Re: [Openvpn-devel] [PATCH v2 2/2] Abort if CRL file can't be stat-ed in init_ssl
Quoted 38 lines · Max Fillinger
Now that the path for the CRL file is handled correctly when using chroot, there's no good reason for the file to be inaccessible during
init_ssl().This commit ensures that the CRL file is accessed successfully at least once, which fixes a bug where the mbedtls version of OpenVPN wouldn't use a reloaded CRL if it initially failed to access the file.
In
tls_process(), we stick with the previous behavior of logging a warning and keeping the old CRL to ensure that the CRL file can be updated on-the-fly.Signed-off-by Max Fillinger <[email protected]...>--- src/openvpn/ssl.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)src/openvpn/ssl.c +5
-1@@ -539,10 +539,14 @@ tls_version_parse(const char *vstr, const char *extra)* @param crl_file The file name to load the CRL from, or * "[[INLINE]]" in the case of inline files. * @param crl_inline A string containing the CRL +* @param abort_on_err If this is true, OpenVPN exits if it can't stat +* the CRL file. If it is false, it will instead +* log a warning and continue using the previously +* loaded CRL. */ static void tls_ctx_reload_crl (struct tls_root_ctx *ssl_ctx,const char *crl_file, -bool crl_file_inline) +bool crl_file_inline,bool abort_on_err) {/* if something goes wrong with stat(), we'll store 0 as mtime */ platform_stat_t crl_stat = {0 };
src/openvpn/ssl.c tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, · line 566
Quoted 5 lines
src/openvpn/ssl.c +1
-1@@ -559,7 +563,14 @@ tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, }else if (platform_stat (crl_file, &crl_stat) <0 ) { -msg (M_WARN ,"WARNING: Failed to stat CRL file, not (re)loading CRL." ); +if (abort_on_err)
Do you think it would make sense to simply check:
if (!ssl_ctx->crl_last_mtime)
abort
else
warn
?
the last_mtime field should tell us if this is the first time we try initializing the CRL or not.
And we want to abort only if the failure happens during the first initialization.
This way we avoid adding yet another state flag that has to be conveyed from the caller.
Regards,
Quoted 36 lines
src/openvpn/ssl.c +10
-3+ { +msg (M_FATAL ,"ERROR: Failed to stat CRL file during init_ssl, exiting." ); + } + else + { +msg (M_WARN ,"WARNING: Failed to stat CRL file, not (re)loading CRL." ); + }return ; }@@ -710,13 +721,13 @@ init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_ch* 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); +tls_ctx_reload_crl (new_ctx, options->crl_file, options->crl_file_inline, true); }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); +tls_ctx_reload_crl (new_ctx,BSTR (&crl_file_buf), options->crl_file_inline, true);gc_free (&gc); } }@@ -2752,7 +2763,7 @@ tls_process(struct tls_multi *multi, && !(session->opt->ssl_flags & SSLF_CRL_VERIFY_DIR)) {tls_ctx_reload_crl (&session->opt->ssl_ctx, - session->opt->crl_file, session->opt->crl_file_inline); + session->opt->crl_file, session->opt->crl_file_inline, false); }/* New connection, remove any old X509 env variables */
Signature · 1 line
Antonio Quartulli
PATCH v3 2/2 Abort if CRL file can't be stat-ed in ssl_init
Now that the path for the CRL file is handled correctly when using chroot, there's no good reason for the file to be inaccessible during ssl_init().
This commit ensures that the CRL file is accessed successfully at least once, which fixes a bug where the mbedtls version of OpenVPN wouldn't use a reloaded CRL if it initially failed to access the file.
--- src/openvpn/ssl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
src/openvpn/ssl.c +9 -1
@@ -559,7 +559,15 @@ tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, }else if (platform_stat (crl_file, &crl_stat) <0 ) { -msg (M_WARN ,"WARNING: Failed to stat CRL file, not (re)loading CRL." ); +/* If crl_last_mtime is zero, the CRL file has not been read before. */ +if (ssl_ctx->crl_last_mtime ==0 ) + { +msg (M_FATAL ,"ERROR: Failed to stat CRL file during initialization, exiting." ); + } +else + { +msg (M_WARN ,"WARNING: Failed to stat CRL file, not reloading CRL." ); + }return ; }
Signature · 1 line
2.11.0
Re: [Openvpn-devel] [PATCH v3 2/2] Abort if CRL file can't be stat-ed in ssl_init
Hi,
Quoted 9 lines · Max Fillinger
Now that the path for the CRL file is handled correctly when using chroot, there's no good reason for the file to be inaccessible during
ssl_init().This commit ensures that the CRL file is accessed successfully at least once, which fixes a bug where the mbedtls version of OpenVPN wouldn't use a reloaded CRL if it initially failed to access the file.
Signed-off-by Max Fillinger <[email protected]...>
It simply does what it says: when calling init_ssl() upon instance initialization (i.e. upon openvpn startup, and upon USR1 on clients) if the CRL is not accessible, then openvpn will abort.
This behaviour is what we need because "starting an instance without having the CRL where it is expected" is equivalent to a configuration error.
Being unable to re-load the CRL at runtime is instead acceptable, because some subsystem might be just recreating the CRL, therefore openvpn will skip the reload and continue using what is currently in memory. The CRL will be reloaded at the next occasion.
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Abort if CRL file can't be stat-ed in ssl_init
In reply to Max FillingerThe actual code change looks very magical to me, and shrunk dramatically between v1 and v3 of that code :-) - but if Antonio has tested this and ACKs it, who am I to complain. I have compile-tested it, at least.
Your patch has been applied to the master and release/2.5 branch (bugfix).
commit 940619c88067d95a1c9865795624bc3822a89bd7 (master) commit 8a06459d0c969b083377b63be16e5da1245ce0ee (release/2.5)
Author: Max Fillinger Date: Thu Apr 15 11:34:54 2021 +0200
Abort if CRL file can't be stat-ed in ssl_init
Signed-off-by: Max Fillinger <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22118.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering