From: Richard Levitte Date: Tue, 19 Jul 2016 17:42:11 +0000 (+0200) Subject: Change all our uses of CRYPTO_THREAD_run_once to use RUN_ONCE instead X-Git-Tag: OpenSSL_1_1_0-pre6~152 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c2e4e5d248567d4eea5bf6d525bdbcc09099ba6e;p=oweals%2Fopenssl.git Change all our uses of CRYPTO_THREAD_run_once to use RUN_ONCE instead That way, we have a way to check if the init function was successful or not. Reviewed-by: Kurt Roeckx --- diff --git a/crypto/bio/b_addr.c b/crypto/bio/b_addr.c index b77fd4b1a0..4e8785fdcd 100644 --- a/crypto/bio/b_addr.c +++ b/crypto/bio/b_addr.c @@ -15,6 +15,7 @@ #ifndef OPENSSL_NO_SOCK #include #include +#include #include CRYPTO_RWLOCK *bio_lookup_lock; @@ -601,9 +602,10 @@ static int addrinfo_wrap(int family, int socktype, return 1; } -static void do_bio_lookup_init(void) +DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) { bio_lookup_lock = CRYPTO_THREAD_lock_new(); + return (bio_lookup_lock != NULL); } /*- @@ -727,7 +729,11 @@ int BIO_lookup(const char *host, const char *service, struct servent se_fallback = { NULL, NULL, 0, NULL }; #endif - CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init); + if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) { + BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE); + ret = 0; + goto err; + } CRYPTO_THREAD_write_lock(bio_lookup_lock); he_fallback_address = INADDR_ANY; diff --git a/crypto/engine/eng_init.c b/crypto/engine/eng_init.c index c51a38dac0..8be7c6fc86 100644 --- a/crypto/engine/eng_init.c +++ b/crypto/engine/eng_init.c @@ -80,7 +80,10 @@ int ENGINE_init(ENGINE *e) ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER); return 0; } - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { + ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE); + return 0; + } CRYPTO_THREAD_write_lock(global_engine_lock); ret = engine_unlocked_init(e); CRYPTO_THREAD_unlock(global_engine_lock); diff --git a/crypto/engine/eng_int.h b/crypto/engine/eng_int.h index 6b6ff9fa4c..c604faddd7 100644 --- a/crypto/engine/eng_int.h +++ b/crypto/engine/eng_int.h @@ -18,6 +18,7 @@ # include "internal/cryptlib.h" # include +# include #ifdef __cplusplus extern "C" { @@ -123,7 +124,7 @@ void engine_pkey_asn1_meths_free(ENGINE *e); /* Once initialisation function */ extern CRYPTO_ONCE engine_lock_init; -void do_engine_lock_init(void); +DECLARE_RUN_ONCE(do_engine_lock_init) /* * This is a structure for storing implementations of various crypto diff --git a/crypto/engine/eng_lib.c b/crypto/engine/eng_lib.c index f7d0216efb..67d755a2aa 100644 --- a/crypto/engine/eng_lib.c +++ b/crypto/engine/eng_lib.c @@ -16,19 +16,18 @@ CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT; /* The "new"/"free" stuff first */ -void do_engine_lock_init(void) +DEFINE_RUN_ONCE(do_engine_lock_init) { global_engine_lock = CRYPTO_THREAD_lock_new(); + return global_engine_lock != NULL; } ENGINE *ENGINE_new(void) { ENGINE *ret; - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); - - ret = OPENSSL_zalloc(sizeof(*ret)); - if (ret == NULL) { + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init) + || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); return NULL; } diff --git a/crypto/engine/eng_list.c b/crypto/engine/eng_list.c index a7252e930b..934389f74e 100644 --- a/crypto/engine/eng_list.c +++ b/crypto/engine/eng_list.c @@ -136,7 +136,11 @@ ENGINE *ENGINE_get_first(void) { ENGINE *ret; - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { + ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE); + return NULL; + } + CRYPTO_THREAD_write_lock(global_engine_lock); ret = engine_list_head; if (ret) { @@ -151,7 +155,11 @@ ENGINE *ENGINE_get_last(void) { ENGINE *ret; - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { + ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE); + return NULL; + } + CRYPTO_THREAD_write_lock(global_engine_lock); ret = engine_list_tail; if (ret) { @@ -279,7 +287,11 @@ ENGINE *ENGINE_by_id(const char *id) ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER); return NULL; } - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { + ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE); + return NULL; + } + CRYPTO_THREAD_write_lock(global_engine_lock); iterator = engine_list_head; while (iterator && (strcmp(id, iterator->id) != 0)) diff --git a/crypto/engine/tb_asnmth.c b/crypto/engine/tb_asnmth.c index 2a6a4ae95e..480267daab 100644 --- a/crypto/engine/tb_asnmth.c +++ b/crypto/engine/tb_asnmth.c @@ -189,7 +189,11 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, fstr.str = str; fstr.len = len; - CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init); + if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { + ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE); + return NULL; + } + CRYPTO_THREAD_write_lock(global_engine_lock); engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); /* If found obtain a structural reference to engine */ diff --git a/crypto/err/err.c b/crypto/err/err.c index 836fac4260..ad1ccd10d0 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -18,6 +18,7 @@ #include #include #include +#include static void err_load_strings(int lib, ERR_STRING_DATA *str); @@ -270,9 +271,10 @@ static void ERR_STATE_free(ERR_STATE *s) OPENSSL_free(s); } -static void do_err_strings_init(void) +DEFINE_RUN_ONCE_STATIC(do_err_strings_init) { err_string_lock = CRYPTO_THREAD_lock_new(); + return err_string_lock != NULL; } void err_cleanup(void) @@ -284,7 +286,7 @@ void err_cleanup(void) void ERR_load_ERR_strings(void) { #ifndef OPENSSL_NO_ERR - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + RUN_ONCE(&err_string_init, do_err_strings_init); err_load_strings(0, ERR_str_libraries); err_load_strings(0, ERR_str_reasons); @@ -316,11 +318,12 @@ void ERR_load_strings(int lib, ERR_STRING_DATA *str) err_load_strings(lib, str); } -void ERR_unload_strings(int lib, ERR_STRING_DATA *str) +int ERR_unload_strings(int lib, ERR_STRING_DATA *str) { LHASH_OF(ERR_STRING_DATA) *hash; - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) + return 0; CRYPTO_THREAD_write_lock(err_string_lock); hash = get_hash(0, 0); @@ -332,11 +335,14 @@ void ERR_unload_strings(int lib, ERR_STRING_DATA *str) } } CRYPTO_THREAD_unlock(err_string_lock); + + return 1; } void err_free_strings_int(void) { - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) + return; CRYPTO_THREAD_write_lock(err_string_lock); lh_ERR_STRING_DATA_free(int_error_hash); @@ -582,7 +588,9 @@ const char *ERR_lib_error_string(unsigned long e) ERR_STRING_DATA d, *p; unsigned long l; - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { + return NULL; + } l = ERR_GET_LIB(e); d.error = ERR_PACK(l, 0, 0); @@ -595,7 +603,9 @@ const char *ERR_func_error_string(unsigned long e) ERR_STRING_DATA d, *p; unsigned long l, f; - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { + return NULL; + } l = ERR_GET_LIB(e); f = ERR_GET_FUNC(e); @@ -609,7 +619,9 @@ const char *ERR_reason_error_string(unsigned long e) ERR_STRING_DATA d, *p = NULL; unsigned long l, r; - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { + return NULL; + } l = ERR_GET_LIB(e); r = ERR_GET_REASON(e); @@ -644,16 +656,17 @@ void ERR_remove_state(unsigned long pid) } #endif -static void err_do_init(void) +DEFINE_RUN_ONCE_STATIC(err_do_init) { - CRYPTO_THREAD_init_local(&err_thread_local, NULL); + return CRYPTO_THREAD_init_local(&err_thread_local, NULL); } ERR_STATE *ERR_get_state(void) { ERR_STATE *state = NULL; - CRYPTO_THREAD_run_once(&err_init, err_do_init); + if (!RUN_ONCE(&err_init, err_do_init)) + return NULL; state = CRYPTO_THREAD_get_local(&err_thread_local); @@ -679,7 +692,9 @@ int ERR_get_next_error_library(void) { int ret; - CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init); + if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { + return 0; + } CRYPTO_THREAD_write_lock(err_string_lock); ret = int_err_library_number++; diff --git a/crypto/ex_data.c b/crypto/ex_data.c index 0666393900..d9807d5859 100644 --- a/crypto/ex_data.c +++ b/crypto/ex_data.c @@ -8,6 +8,7 @@ */ #include "internal/cryptlib_int.h" +#include "internal/thread_once.h" #include /* @@ -35,9 +36,10 @@ static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT]; static CRYPTO_RWLOCK *ex_data_lock = NULL; static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT; -static void do_ex_data_init(void) +DEFINE_RUN_ONCE_STATIC(do_ex_data_init) { ex_data_lock = CRYPTO_THREAD_lock_new(); + return ex_data_lock != NULL; } /* @@ -53,7 +55,10 @@ static EX_CALLBACKS *get_and_lock(int class_index) return NULL; } - CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init); + if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) { + CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE); + return NULL; + } if (ex_data_lock == NULL) { /* diff --git a/crypto/init.c b/crypto/init.c index f38129fa20..ace11da610 100644 --- a/crypto/init.c +++ b/crypto/init.c @@ -22,6 +22,7 @@ #include #include #include +#include static int stopped = 0; @@ -61,7 +62,7 @@ static CRYPTO_RWLOCK *init_lock = NULL; static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; static int base_inited = 0; -static void ossl_init_base(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_base) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n"); @@ -74,20 +75,22 @@ static void ossl_init_base(void) #ifndef OPENSSL_SYS_UEFI atexit(OPENSSL_cleanup); #endif - init_lock = CRYPTO_THREAD_lock_new(); + if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL) + return 0; OPENSSL_cpuid_setup(); base_inited = 1; + return 1; } static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; static int load_crypto_strings_inited = 0; -static void ossl_init_no_load_crypto_strings(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings) { /* Do nothing in this case */ - return; + return 1; } -static void ossl_init_load_crypto_strings(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings) { /* * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time @@ -101,10 +104,11 @@ static void ossl_init_load_crypto_strings(void) err_load_crypto_strings_int(); #endif load_crypto_strings_inited = 1; + return 1; } static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_add_all_ciphers(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers) { /* * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time @@ -117,10 +121,11 @@ static void ossl_init_add_all_ciphers(void) # endif openssl_add_all_ciphers_int(); #endif + return 1; } static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_add_all_digests(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests) { /* * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time @@ -133,18 +138,19 @@ static void ossl_init_add_all_digests(void) # endif openssl_add_all_digests_int(); #endif + return 1; } -static void ossl_init_no_add_algs(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs) { /* Do nothing */ - return; + return 1; } static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; static int config_inited = 0; static const char *appname; -static void ossl_init_config(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_config) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, @@ -153,8 +159,9 @@ static void ossl_init_config(void) #endif openssl_config_int(appname); config_inited = 1; + return 1; } -static void ossl_init_no_config(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_no_config) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, @@ -162,103 +169,114 @@ static void ossl_init_no_config(void) #endif openssl_no_config_int(); config_inited = 1; + return 1; } static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; static int async_inited = 0; -static void ossl_init_async(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_async) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n"); #endif - async_init(); + if (!async_init()) + return 0; async_inited = 1; + return 1; } #ifndef OPENSSL_NO_ENGINE static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_openssl(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: " "engine_load_openssl_int()\n"); # endif engine_load_openssl_int(); + return 1; } # if !defined(OPENSSL_NO_HW) && \ (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_cryptodev(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: " "engine_load_cryptodev_int()\n"); # endif engine_load_cryptodev_int(); + return 1; } # endif # ifndef OPENSSL_NO_RDRAND static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_rdrand(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: " "engine_load_rdrand_int()\n"); # endif engine_load_rdrand_int(); + return 1; } # endif static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_dynamic(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: " "engine_load_dynamic_int()\n"); # endif engine_load_dynamic_int(); + return 1; } # ifndef OPENSSL_NO_STATIC_ENGINE # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_padlock(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: " "engine_load_padlock_int()\n"); # endif engine_load_padlock_int(); + return 1; } # endif # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_capi(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: " "engine_load_capi_int()\n"); # endif engine_load_capi_int(); + return 1; } # endif static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_dasync(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dasync) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: " "engine_load_dasync_int()\n"); # endif engine_load_dasync_int(); + return 1; } # if !defined(OPENSSL_NO_AFALGENG) static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; -static void ossl_init_engine_afalg(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg) { # ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: " "engine_load_afalg_int()\n"); # endif engine_load_afalg_int(); + return 1; } # endif # endif @@ -268,10 +286,11 @@ static void ossl_init_engine_afalg(void) static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; static int zlib_inited = 0; -static void ossl_init_zlib(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_zlib) { /* Do nothing - we need to know about this for the later cleanup */ zlib_inited = 1; + return 1; } #endif @@ -464,94 +483,87 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) return 0; } - if (!CRYPTO_THREAD_run_once(&base, ossl_init_base)) + if (!RUN_ONCE(&base, ossl_init_base)) return 0; if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) - && !CRYPTO_THREAD_run_once(&load_crypto_strings, - ossl_init_no_load_crypto_strings)) + && !RUN_ONCE(&load_crypto_strings, + ossl_init_no_load_crypto_strings)) return 0; if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS) - && !CRYPTO_THREAD_run_once(&load_crypto_strings, - ossl_init_load_crypto_strings)) + && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings)) return 0; if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) - && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs)) + && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs)) return 0; if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS) - && !CRYPTO_THREAD_run_once(&add_all_ciphers, - ossl_init_add_all_ciphers)) + && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers)) return 0; if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS) - && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs)) + && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs)) return 0; if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS) - && !CRYPTO_THREAD_run_once(&add_all_digests, - ossl_init_add_all_digests)) + && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests)) return 0; if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) - && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config)) + && !RUN_ONCE(&config, ossl_init_no_config)) return 0; if (opts & OPENSSL_INIT_LOAD_CONFIG) { int ret; CRYPTO_THREAD_write_lock(init_lock); appname = (settings == NULL) ? NULL : settings->appname; - ret = CRYPTO_THREAD_run_once(&config, ossl_init_config); + ret = RUN_ONCE(&config, ossl_init_config); CRYPTO_THREAD_unlock(init_lock); if (!ret) return 0; } if ((opts & OPENSSL_INIT_ASYNC) - && !CRYPTO_THREAD_run_once(&async, ossl_init_async)) + && !RUN_ONCE(&async, ossl_init_async)) return 0; #ifndef OPENSSL_NO_ENGINE if ((opts & OPENSSL_INIT_ENGINE_OPENSSL) - && !CRYPTO_THREAD_run_once(&engine_openssl, - ossl_init_engine_openssl)) + && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl)) return 0; # if !defined(OPENSSL_NO_HW) && \ (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV) - && !CRYPTO_THREAD_run_once(&engine_cryptodev, - ossl_init_engine_cryptodev)) + && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev)) return 0; # endif # ifndef OPENSSL_NO_RDRAND if ((opts & OPENSSL_INIT_ENGINE_RDRAND) - && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand)) + && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand)) return 0; # endif if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC) - && !CRYPTO_THREAD_run_once(&engine_dynamic, - ossl_init_engine_dynamic)) + && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic)) return 0; # ifndef OPENSSL_NO_STATIC_ENGINE # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) if ((opts & OPENSSL_INIT_ENGINE_PADLOCK) - && !CRYPTO_THREAD_run_once(&engine_padlock, - ossl_init_engine_padlock)) + && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock)) return 0; # endif # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) if ((opts & OPENSSL_INIT_ENGINE_CAPI) - && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi)) + && !RUN_ONCE(&engine_capi, ossl_init_engine_capi)) return 0; # endif if ((opts & OPENSSL_INIT_ENGINE_DASYNC) - && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync)) + && !RUN_ONCE(&engine_dasync, ossl_init_engine_dasync)) return 0; # if !defined(OPENSSL_NO_AFALGENG) if ((opts & OPENSSL_INIT_ENGINE_AFALG) - && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg)) + && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg)) return 0; # endif # endif @@ -564,7 +576,7 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) #ifndef OPENSSL_NO_COMP if ((opts & OPENSSL_INIT_ZLIB) - && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib)) + && !RUN_ONCE(&zlib, ossl_init_zlib)) return 0; #endif diff --git a/crypto/mem_dbg.c b/crypto/mem_dbg.c index 0b48708232..0307c02517 100644 --- a/crypto/mem_dbg.c +++ b/crypto/mem_dbg.c @@ -112,7 +112,8 @@ int CRYPTO_mem_ctrl(int mode) #else int ret = mh_mode; - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); + if (!RUN_ONCE(&memdbg_init, do_memdbg_init)) + return -1; CRYPTO_THREAD_write_lock(malloc_lock); switch (mode) { @@ -185,7 +186,8 @@ static int mem_check_on(void) CRYPTO_THREAD_ID cur; if (mh_mode & CRYPTO_MEM_CHECK_ON) { - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); + if (!RUN_ONCE(&memdbg_init, do_memdbg_init)) + return 0; cur = CRYPTO_THREAD_get_current_id(); CRYPTO_THREAD_read_lock(malloc_lock); @@ -228,7 +230,9 @@ static int pop_info(void) { APP_INFO *current = NULL; - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); + if (!RUN_ONCE(&memdbg_init, do_memdbg_init)) + return 0; + current = (APP_INFO *)CRYPTO_THREAD_get_local(&appinfokey); if (current != NULL) { APP_INFO *next = current->next; @@ -258,9 +262,8 @@ int CRYPTO_mem_debug_push(const char *info, const char *file, int line) if (mem_check_on()) { CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); - - if ((ami = OPENSSL_malloc(sizeof(*ami))) == NULL) + if (!RUN_ONCE(&memdbg_init, do_memdbg_init) + || (ami = OPENSSL_malloc(sizeof(*ami))) == NULL) goto err; ami->threadid = CRYPTO_THREAD_get_current_id(); @@ -313,9 +316,8 @@ void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p, if (mem_check_on()) { CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); - - if ((m = OPENSSL_malloc(sizeof(*m))) == NULL) { + if (!RUN_ONCE(&memdbg_init, do_memdbg_init) + || (m = OPENSSL_malloc(sizeof(*m))) == NULL) { OPENSSL_free(addr); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); return; @@ -543,7 +545,8 @@ int CRYPTO_mem_leaks(BIO *b) /* Ensure all resources are released */ OPENSSL_cleanup(); - CRYPTO_THREAD_run_once(&memdbg_init, do_memdbg_init); + if (!RUN_ONCE(&memdbg_init, do_memdbg_init)) + return -1; CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); diff --git a/crypto/rand/md_rand.c b/crypto/rand/md_rand.c index 9d39831cf2..7924478e9e 100644 --- a/crypto/rand/md_rand.c +++ b/crypto/rand/md_rand.c @@ -27,6 +27,8 @@ #include +#include + #ifdef OPENSSL_FIPS # include #endif @@ -85,10 +87,11 @@ static RAND_METHOD rand_meth = { rand_status }; -static void do_rand_lock_init(void) +DEFINE_RUN_ONCE_STATIC(do_rand_lock_init) { rand_lock = CRYPTO_THREAD_lock_new(); rand_tmp_lock = CRYPTO_THREAD_lock_new(); + return rand_lock != NULL && rand_tmp_lock != NULL; } RAND_METHOD *RAND_OpenSSL(void) @@ -141,7 +144,8 @@ static int rand_add(const void *buf, int num, double add) if (m == NULL) goto err; - CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init); + if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init)) + goto err; /* check if we already have the lock */ if (crypto_lock_rand) { @@ -339,7 +343,9 @@ static int rand_bytes(unsigned char *buf, int num, int pseudo) * global 'md'. */ - CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init); + if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init)) + goto err_mem; + CRYPTO_THREAD_write_lock(rand_lock); /* * We could end up in an async engine while holding this lock so ensure @@ -534,7 +540,9 @@ static int rand_status(void) int ret; int do_not_lock; - CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init); + if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init)) + return 0; + cur = CRYPTO_THREAD_get_current_id(); /* * check if we already have the lock (could happen if a RAND_poll() diff --git a/include/openssl/err.h b/include/openssl/err.h index 9dcd27e9be..60ffc49edc 100644 --- a/include/openssl/err.h +++ b/include/openssl/err.h @@ -232,7 +232,7 @@ void ERR_print_errors(BIO *bp); void ERR_add_error_data(int num, ...); void ERR_add_error_vdata(int num, va_list args); void ERR_load_strings(int lib, ERR_STRING_DATA str[]); -void ERR_unload_strings(int lib, ERR_STRING_DATA str[]); +int ERR_unload_strings(int lib, ERR_STRING_DATA str[]); void ERR_load_ERR_strings(void); #if OPENSSL_API_COMPAT < 0x10100000L diff --git a/ssl/ssl_cert.c b/ssl/ssl_cert.c index 2a07ee6910..c155e787d5 100644 --- a/ssl/ssl_cert.c +++ b/ssl/ssl_cert.c @@ -29,6 +29,7 @@ #include #include #include "ssl_locl.h" +#include "internal/thread_once.h" static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid, void *other, @@ -37,17 +38,19 @@ static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int o static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT; static volatile int ssl_x509_store_ctx_idx = -1; -static void ssl_x509_store_ctx_init(void) +DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init) { ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0, "SSL for verify callback", NULL, NULL, NULL); + return ssl_x509_store_ctx_idx >= 0; } int SSL_get_ex_data_X509_STORE_CTX_idx(void) { - CRYPTO_THREAD_run_once(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init); + if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init)) + return -1; return ssl_x509_store_ctx_idx; } diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c index 2fc4309a91..2a54f9d44d 100644 --- a/ssl/ssl_ciph.c +++ b/ssl/ssl_ciph.c @@ -46,6 +46,7 @@ #include #include #include "ssl_locl.h" +#include "internal/thread_once.h" #define SSL_ENC_DES_IDX 0 #define SSL_ENC_3DES_IDX 1 @@ -479,7 +480,7 @@ static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b) return ((*a)->id - (*b)->id); } -static void do_load_builtin_compressions(void) +DEFINE_RUN_ONCE_STATIC(do_load_builtin_compressions) { SSL_COMP *comp = NULL; COMP_METHOD *method = COMP_zlib(); @@ -498,12 +499,12 @@ static void do_load_builtin_compressions(void) } } CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); + return 1; } static void load_builtin_compressions(void) { - CRYPTO_THREAD_run_once(&ssl_load_builtin_comp_once, - do_load_builtin_compressions); + RUN_ONCE(&ssl_load_builtin_comp_once, do_load_builtin_compressions); } #endif diff --git a/ssl/ssl_init.c b/ssl/ssl_init.c index 66525de6c5..543da13339 100644 --- a/ssl/ssl_init.c +++ b/ssl/ssl_init.c @@ -14,6 +14,7 @@ #include #include #include "ssl_locl.h" +#include "internal/thread_once.h" static int stopped; @@ -21,7 +22,7 @@ static void ssl_library_stop(void); static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT; static int ssl_base_inited = 0; -static void ossl_init_ssl_base(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: " @@ -108,11 +109,12 @@ static void ossl_init_ssl_base(void) */ OPENSSL_atexit(ssl_library_stop); ssl_base_inited = 1; + return 1; } static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT; static int ssl_strings_inited = 0; -static void ossl_init_load_ssl_strings(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings) { /* * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time @@ -126,12 +128,13 @@ static void ossl_init_load_ssl_strings(void) ERR_load_SSL_strings(); #endif ssl_strings_inited = 1; + return 1; } -static void ossl_init_no_load_ssl_strings(void) +DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_ssl_strings) { /* Do nothing in this case */ - return; + return 1; } static void ssl_library_stop(void) @@ -193,17 +196,15 @@ int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) | OPENSSL_INIT_ADD_ALL_DIGESTS, settings)) return 0; - if (!CRYPTO_THREAD_run_once(&ssl_base, ossl_init_ssl_base)) + if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base)) return 0; if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS) - && !CRYPTO_THREAD_run_once(&ssl_strings, - ossl_init_no_load_ssl_strings)) + && !RUN_ONCE(&ssl_strings, ossl_init_no_load_ssl_strings)) return 0; if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS) - && !CRYPTO_THREAD_run_once(&ssl_strings, - ossl_init_load_ssl_strings)) + && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings)) return 0; return 1;