Avoid a crash in mbed TLS 2.25 with --verb < 8
PATCH 1/1 Avoid a crash in mbed TLS 2.25 with --verb < 8
mbed TLS 2.25 has a nasty bug that the print function for Montgomery style EC curves (Curve25519 and Curve448) does segfault. See also the issue reported here: https://github.com/ARMmbed/mbedtls/issues/4208
We request always debug level 3 from mbed TLS but filter out any debug output of level 3 unless verb 8 or higher is set. This commeit sets the debug level to 2 to avoid this problem by makeing mbed TLS not generatin the problematic debug output.
For the affected version to still use --verb 8 with mbed TLS 2.25 is to restrict the EC groups to ones that do not crash the print function like with '--tls-groups secp521r1:secp384r1:secp256r1'.
This patch has no patch on user-visible behaviour on unaffected mbed TLS versions.
--- src/openvpn/options.c | 6 ++++++ src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_mbedtls.c | 11 ++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-)
src/openvpn/options.c +6 -0
@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >7 ) + { +/* We pass this flag to the SSL library to avoid a bug +* in mbed TLS 2.5.0 with high log level */ + options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )
src/openvpn/ssl_common.h +1 -0
@@ -350,6 +350,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT
src/openvpn/ssl_mbedtls.c +10 -1
@@ -1058,7 +1058,16 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* This works around a crash in mbed TLS 2.25 if Curve25591 is selected +* for DH (https://github.com/ARMmbed/mbedtls/issues/4208)*/ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED ) + { +mbedtls_debug_set_threshold (3 ); + } +else + { +mbedtls_debug_set_threshold (2 ); + }#endif mbedtls_ssl_conf_dbg (ks_ssl->ssl_config, my_debug,NULL ); mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,
Signature · 1 line
2.30.1
Hi,
On 08/03/2021 15:21, Arne Schwabe wrote:
[cut]
Quoted 5 lines
src/openvpn/options.c +0
-0
src/openvpn/options.c add_option(struct options *options, · line 5891
Quoted 9 lines
src/openvpn/options.c +6
-0@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >7 ) + { +/* We pass this flag to the SSL library to avoid a bug +* in mbed TLS 2.5.0 with high log level */ + options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }
(2.5.0 is probably a typ0?)
If we know that a certain combination of options + the right mbedTLS version will lead to a crash, shouldn't we catch it here (or somewhere else) and error out right away?
Quoted 20 lines
src/openvpn/options.c +0
-0#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )src/openvpn/ssl_common.h +1
-0@@ -350,6 +350,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT src/openvpn/ssl_mbedtls.c +0
-0
src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl, · line 1065
Quoted 9 lines
src/openvpn/ssl_mbedtls.c +5
-1@@ -1058,7 +1058,16 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* This works around a crash in mbed TLS 2.25 if Curve25591 is selected +* for DH (https://github.com/ARMmbed/mbedtls/issues/4208)*/ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED ) + { +mbedtls_debug_set_threshold (3 );
If a user is on mbedTLS 2.25 and specifies verb = 8 we will then crash (assuming the right combination of options was chosen).
Isn't it better to just always use 2 as threshold when the mbedTLS version is known to be buggy (regardless of verb)?
An ifdef around this invocation would do it, without the need of introducing another SSL flag.
src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl,
Quoted 8 lines
src/openvpn/ssl_mbedtls.c +5
-0+ } +else + { +mbedtls_debug_set_threshold (2 ); + }> #endifmbedtls_ssl_conf_dbg (ks_ssl->ssl_config, my_debug,NULL ); mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,
my 2 cents
Cheers,
Signature · 1 line
Antonio Quartulli
Am 09.03.21 um 10:09 schrieb Antonio Quartulli:
Quoted 5 lines
Hi,
On 08/03/2021 15:21, Arne Schwabe wrote:
[cut]
Quoted 16 lines
Quoted 5 lines
src/openvpn/options.c +0
-0src/openvpn/options.c add_option(struct options *options, · line 5891
Quoted 9 lines
src/openvpn/options.c +6
-0@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >7 ) + { +/* We pass this flag to the SSL library to avoid a bug +* in mbed TLS 2.5.0 with high log level */ + options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }(2.5.0 is probably a typ0?)
Yes.
Quoted 4 lines
If we know that a certain combination of options + the right mbedTLS version will lead to a crash, shouldn't we catch it here (or somewhere else) and error out right away?
This one that we know of. But if we do a warning I would rather do a generic one. The debug logging of mbed TLS seems to be not very well tested. normally we don't explciitly try to detect and warn about every obscure bug that we know of. But we could do for this bug:
#if mbed version >= 2.25.0
msg(M_INFO, "Note: mbed TLS 2.25.0 is known to crash on trying too output x25519 and x448 parameters in debug. If you see mbed TLS crash after printing out ECDH curve: x25519 you can workaround this bug by using '--tls-groups secp521r1:secp384r1:secp256r1'"); #endif
Quoted 33 lines
Quoted 20 lines
src/openvpn/options.c +0
-0#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )src/openvpn/ssl_common.h +1
-0@@ -350,6 +350,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT src/openvpn/ssl_mbedtls.c +0
-0src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl, · line 1065
Quoted 9 lines
src/openvpn/ssl_mbedtls.c +5
-1@@ -1058,7 +1058,16 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* This works around a crash in mbed TLS 2.25 if Curve25591 is selected +* for DH (https://github.com/ARMmbed/mbedtls/issues/4208)*/ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED ) + { +mbedtls_debug_set_threshold (3 );If a user is on mbedTLS 2.25 and specifies verb = 8 we will then crash (assuming the right combination of options was chosen).
Quoted 4 lines
Isn't it better to just always use 2 as threshold when the mbedTLS version is known to be buggy (regardless of verb)?
An ifdef around this invocation would do it, without the need of introducing another SSL flag.
I am strongly against removing the possibility to debug mbed TLS completely just because it *might* crash on a setting that is not used outside of debugging (verb >= 8).
Also seeing that this bug, it seems that requesting level 3 logging from mbed TLS is not a good idea in general as the code seems not to be well tested and there might other high debug print crashes lurking. Also even though it does not seem to affect performance, currently we generate a number of debug prints for each packet by requesting level logging from mbed TLS (crypto operation debug prints).
Arne
Hi,
Quoted 8 lines · Arne Schwabe
mbed TLS 2.25 has a nasty bug that the print function for Montgomery style EC curves (Curve25519 and Curve448) does segfault. See also the issue reported here: https://github.com/ARMmbed/mbedtls/issues/4208
We request always debug level 3 from mbed TLS but filter out any debug output of level 3 unless verb 8 or higher is set. This commeit sets the debug level to 2 to avoid this problem by makeing mbed TLS not generatin the problematic debug output.
Only setting level 3 when we actually need it is a better approach anyway. So +1 that, even without this bug. Somewhat surprisingly, a quick test shows that this does not matter for performance (perhaps that is why I didn't do it like that back then, can't remember).
Quoted 19 lines
For the affected version to still use --verb 8 with mbed TLS 2.25 is to restrict the EC groups to ones that do not crash the print function like with '--tls-groups secp521r1:secp384r1:secp256r1'.
This patch has no patch on user-visible behaviour on unaffected mbed TLS versions.
Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/options.c | 6 ++++++ src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_mbedtls.c | 11 ++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-)src/openvpn/options.c +0
-0
src/openvpn/options.c add_option(struct options *options, · line 5886
Quoted 4 lines
src/openvpn/options.c +1
-0@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >7 )
Instead of the magic 7, can we use "D_TLS_DEBUG_MED & M_DEBUG_LEVEL"?
src/openvpn/options.c add_option(struct options *options, · line 5889
Quoted 3 lines
src/openvpn/options.c +3
-0+ { +/* We pass this flag to the SSL library to avoid a bug +* in mbed TLS 2.5.0 with high log level */
(Repeating Antonio:) Should be 2.25.
Quoted 30 lines
src/openvpn/options.c +2
-0+ options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )src/openvpn/ssl_common.h +1
-0@@ -350,6 +350,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT src/openvpn/ssl_mbedtls.c +3
-1@@ -1058,7 +1058,16 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* This works around a crash in mbed TLS 2.25 if Curve25591 is selected +* for DH (https://github.com/ARMmbed/mbedtls/issues/4208)*/ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED )
src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl, · line 1073
Quoted 10 lines
src/openvpn/ssl_mbedtls.c +7
-0+ { +mbedtls_debug_set_threshold (3 ); + } + else + { +mbedtls_debug_set_threshold (2 ); + }#endif mbedtls_ssl_conf_dbg (ks_ssl->ssl_config, my_debug,NULL ); mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,
Otherwise, this looks good to me. Just stare-at-code, no real testing done.
-Steffan
PATCH v2 1/1 Avoid generating unecessary mbed debug messages
In reply to Arne SchwabeThe main motivation to make this change is to avoid a crash in mbed TLS 2.25 with --verb < 8.
mbed TLS 2.25 has a nasty bug that the print function for Montgomery style EC curves (Curve25519 and Curve448) does segfault. See also the issue reported here: https://github.com/ARMmbed/mbedtls/issues/4208
We request always debug level 3 from mbed TLS but filter out any debug output of level 3 unless verb 8 or higher is set. This commeit sets the debug level to 2 to avoid this problem by makeing mbed TLS not generatin the problematic debug output.
For the affected version to still use --verb 8 with mbed TLS 2.25 is to restrict the EC groups to ones that do not crash the print function like with '--tls-groups secp521r1:secp384r1:secp256r1'.
This patch has no patch on user-visible behaviour on unaffected mbed TLS versions.
Patch V2: Replace magic constant with proper define. Highlight more this
avoding generating unessary debug output than crash workaround.
--- src/openvpn/options.c | 6 ++++++ src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_mbedtls.c | 13 ++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-)
src/openvpn/options.c +6 -0
@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >= (D_TLS_DEBUG_MED &M_DEBUG_LEVEL )) + { +/* We pass this flag to the SSL library to avoid a +* mbed TLS always generating debug level logging */ + options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )
src/openvpn/ssl_common.h +1 -0
@@ -352,6 +352,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT
src/openvpn/ssl_mbedtls.c +12 -1
@@ -1058,7 +1058,18 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* We only want to have mbed TLS generate debug level logging when we would +* also display it. +* In fact mbed TLS 2.25.0 crashes generating debug log if Curve25591 is +* selected for DH (https://github.com/ARMmbed/mbedtls/issues/4208) */ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED ) + { +mbedtls_debug_set_threshold (3 ); + } +else + { +mbedtls_debug_set_threshold (2 ); + }#endif mbedtls_ssl_conf_dbg (ks_ssl->ssl_config, my_debug,NULL ); mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,
Signature · 1 line
2.30.1
Re: [Openvpn-devel] [PATCH v2] Avoid generating unecessary mbed debug messages
Hi,
Quoted 34 lines · Arne Schwabe
The main motivation to make this change is to avoid a crash in mbed TLS 2.25 with --verb < 8.
mbed TLS 2.25 has a nasty bug that the print function for Montgomery style EC curves (Curve25519 and Curve448) does segfault. See also the issue reported here: https://github.com/ARMmbed/mbedtls/issues/4208
We request always debug level 3 from mbed TLS but filter out any debug output of level 3 unless verb 8 or higher is set. This commeit sets the debug level to 2 to avoid this problem by makeing mbed TLS not generatin the problematic debug output.
For the affected version to still use --verb 8 with mbed TLS 2.25 is to restrict the EC groups to ones that do not crash the print function like with '--tls-groups secp521r1:secp384r1:secp256r1'.
This patch has no patch on user-visible behaviour on unaffected mbed TLS versions.
Signed-off-by Arne Schwabe <[email protected]...>Patch V2: Replace magic constant with proper define. Highlight more this
avoding generating unessary debug output than crash workaround.--- src/openvpn/options.c | 6 ++++++ src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_mbedtls.c | 13 ++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-)src/openvpn/options.c +0
-0
src/openvpn/options.c add_option(struct options *options, · line 5889
Quoted 7 lines
src/openvpn/options.c +4
-0@@ -5883,6 +5883,12 @@ add_option(struct options *options, {VERIFY_PERMISSION (OPT_P_MESSAGES ); options->verbosity =positive_atoi (p[1 ]); +if (options->verbosity >= (D_TLS_DEBUG_MED &M_DEBUG_LEVEL )) + { +/* We pass this flag to the SSL library to avoid a +* mbed TLS always generating debug level logging */
Typo: "a mbed TLS" -> "mbed TLS".
Quoted 32 lines
src/openvpn/options.c +2
-0+ options->ssl_flags |=SSLF_TLS_DEBUG_ENABLED ; + }#if !defined(ENABLE_DEBUG ) && !defined(ENABLE_SMALL )/* Warn when a debug verbosity is supplied when built without debug support */ if (options->verbosity >=7 )src/openvpn/ssl_common.h +1
-0@@ -352,6 +352,7 @@ struct tls_options#define SSLF_TLS_VERSION_MIN_MASK 0xF/* (uses bit positions 6 to 9) */ #define SSLF_TLS_VERSION_MAX_SHIFT 10#define SSLF_TLS_VERSION_MAX_MASK 0xF/* (uses bit positions 10 to 13) */ +#define SSLF_TLS_DEBUG_ENABLED (1<<14)unsigned int ssl_flags;#ifdef ENABLE_MANAGEMENT src/openvpn/ssl_mbedtls.c +5
-1@@ -1058,7 +1058,18 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,mbedtls_ssl_config_defaults (ks_ssl->ssl_config, ssl_ctx->endpoint,MBEDTLS_SSL_TRANSPORT_STREAM ,MBEDTLS_SSL_PRESET_DEFAULT );#ifdef MBEDTLS_DEBUG_C -mbedtls_debug_set_threshold (3 ); +/* We only want to have mbed TLS generate debug level logging when we would +* also display it. +* In fact mbed TLS 2.25.0 crashes generating debug log if Curve25591 is +* selected for DH (https://github.com/ARMmbed/mbedtls/issues/4208) */ +if (session->opt->ssl_flags &SSLF_TLS_DEBUG_ENABLED )
src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl, · line 1075
Quoted 10 lines
src/openvpn/ssl_mbedtls.c +7
-0+ { +mbedtls_debug_set_threshold (3 ); + } + else + { +mbedtls_debug_set_threshold (2 ); + }#endif mbedtls_ssl_conf_dbg (ks_ssl->ssl_config, my_debug,NULL ); mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,
Otherwise, this now looks good me.
Thanks,
-Steffan
[Openvpn-devel] [PATCH applied] Re: Avoid generating unecessary mbed debug messages
In reply to Arne SchwabeTested with exactly that testbed that initially discovered the SIGSEGV, and as expected, it no longer crashes. Verified that it still crashes with "--verb 8" (it does).
Code also looks secure and does what it says on the lid.
That said, ACKed and merged, I'm still not totally happy with the approach chosen - this is quite some new lines of code to change one "3" into a "2", depending on the value of one other variable... but we discussed this before, and Arne did not want to have the mbedTLS code "peek" into get_debug_level() to get at the --verb setting. Since Steffan is fine with this approach, I'm clearly the minority here...
.. while writing this, the ACK from Steffan came in... so, recording that as well... MULTI-ACK! :-) - and fixing the comment as requested.
Your patch has been applied to the master and release/2.5 branch (stability / long-term compat thing).
commit 4524feb2bbbb6d1bd463a0c5c2d53aae5bdf360a (master) commit 475d17a53eba85591f270008f8b583383a5b9afa (release/2.5)
Author: Arne Schwabe Date: Tue Mar 16 13:44:21 2021 +0100
Avoid generating unecessary mbed debug messages
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg21667.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering