From: Frederik Wedel-Heinen Date: Sun, 5 Jan 2025 19:42:51 +0000 (+0100) Subject: Return NULL from ossl_lib_ctx_get_concrete() when it is uninitialized X-Git-Tag: openssl-3.1.8~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61217d142ce7bd3d5ed089fd7610f77ac0867421;p=thirdparty%2Fopenssl.git Return NULL from ossl_lib_ctx_get_concrete() when it is uninitialized When default_context_inited is set to false we return NULL instead of the global default context. Fixes #25442 Reviewed-by: Tom Cosgrove Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/26319) (cherry picked from commit dfce0d7418d6d5b54d74fa80fc50392f00270c53) --- diff --git a/crypto/context.c b/crypto/context.c index 7a81db388b8..93b425e895e 100644 --- a/crypto/context.c +++ b/crypto/context.c @@ -48,17 +48,23 @@ struct ossl_lib_ctx_st { int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx) { - return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock); + if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) + return 0; + return CRYPTO_THREAD_write_lock(ctx->lock); } int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx) { - return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock); + if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) + return 0; + return CRYPTO_THREAD_read_lock(ctx->lock); } int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx) { - return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock); + if ((ctx = ossl_lib_ctx_get_concrete(ctx)) == NULL) + return 0; + return CRYPTO_THREAD_unlock(ctx->lock); } int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx) @@ -372,7 +378,7 @@ static OSSL_LIB_CTX *get_default_context(void) { OSSL_LIB_CTX *current_defctx = get_thread_default_context(); - if (current_defctx == NULL) + if (current_defctx == NULL && default_context_inited) current_defctx = &default_context_int; return current_defctx; } diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index 2be5652097a..e46ee631386 100644 --- a/crypto/rand/rand_lib.c +++ b/crypto/rand/rand_lib.c @@ -756,6 +756,9 @@ EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx) return NULL; ctx = ossl_lib_ctx_get_concrete(ctx); + + if (ctx == NULL) + return NULL; /* * If the private is also NULL then this is the first time we've * used this thread. @@ -789,6 +792,9 @@ EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx) return NULL; ctx = ossl_lib_ctx_get_concrete(ctx); + + if (ctx == NULL) + return NULL; /* * If the public is also NULL then this is the first time we've * used this thread.