diff options
author | Petr Mikhalicin <mkh199740@mail.ru> | 2023-04-21 12:25:43 +0300 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2023-04-24 11:32:29 +0200 |
commit | f7c5fdec7248ec0805429f115b2140fff54faad0 (patch) | |
tree | 033a7810680e1979ee1a9910bc9e79bf7a6df302 | |
parent | b236fd27caa54cbd1d04204a1fbdb6689c51dbc0 (diff) | |
download | openssl-new-f7c5fdec7248ec0805429f115b2140fff54faad0.tar.gz |
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)
(cherry picked from commit 31295ca02c0a2d7209a33047c7f6dd1dabc12c93)
-rw-r--r-- | crypto/context.c | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/crypto/context.c b/crypto/context.c index c6358afc81..edfaa822b6 100644 --- a/crypto/context.c +++ b/crypto/context.c @@ -332,17 +332,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) |