summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorPetr Mikhalicin <mkh199740@mail.ru>2023-04-21 12:25:43 +0300
committerTomas Mraz <tomas@openssl.org>2023-04-24 11:31:57 +0200
commit31295ca02c0a2d7209a33047c7f6dd1dabc12c93 (patch)
treedd1ea7c9d3d95a2ccc0318f4e2934ea6e6c50069 /crypto
parentc04e78f0c69201226430fed14c291c281da47f2d (diff)
Fix calling pthread_key_delete on uninitialized data
default_context_do_init may be never called and CRYPTO_THREAD_init_local inside it may be never called too. But corresponding CRYPTO_THREAD_cleanup_local is always called at cleanup stage. This lead to undefined behavior. So, add flag to check that default_context_do_init will be called successfully or not. Fix: #20697 Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/20801)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/context.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/crypto/context.c b/crypto/context.c
index dcd9a1396b..b097b58cd5 100644
--- a/crypto/context.c
+++ b/crypto/context.c
@@ -348,17 +348,32 @@ static OSSL_LIB_CTX default_context_int;
static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
static CRYPTO_THREAD_LOCAL default_context_thread_local;
+static int default_context_inited = 0;
DEFINE_RUN_ONCE_STATIC(default_context_do_init)
{
- return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
- && context_init(&default_context_int);
+ if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL))
+ goto err;
+
+ if (!context_init(&default_context_int))
+ goto deinit_thread;
+
+ default_context_inited = 1;
+ return 1;
+
+deinit_thread:
+ CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
+err:
+ return 0;
}
void ossl_lib_ctx_default_deinit(void)
{
+ if (!default_context_inited)
+ return;
context_deinit(&default_context_int);
CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
+ default_context_inited = 0;
}
static OSSL_LIB_CTX *get_thread_default_context(void)