Add CRYPTO_thread_glock_new
authorRich Salz <rsalz@openssl.org>
Tue, 29 Aug 2017 19:24:17 +0000 (15:24 -0400)
committerRich Salz <rsalz@openssl.org>
Thu, 31 Aug 2017 23:42:03 +0000 (19:42 -0400)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/4294)

14 files changed:
crypto/bio/b_addr.c
crypto/bio/bio_meth.c
crypto/engine/eng_lib.c
crypto/err/err.c
crypto/ex_data.c
crypto/init.c
crypto/mem_dbg.c
crypto/mem_sec.c
crypto/objects/o_names.c
crypto/rand/drbg_lib.c
crypto/rand/rand_lib.c
crypto/store/store_register.c
include/openssl/crypto.h
util/libcrypto.num

index ba26c9144af271feeea6438d233d744d2915e1bc..47e4fc20fb93ceb105a787439031322e7b084c1e 100644 (file)
@@ -603,7 +603,7 @@ static int addrinfo_wrap(int family, int socktype,
 DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init)
 {
     OPENSSL_init_crypto(0, NULL);
-    bio_lookup_lock = CRYPTO_THREAD_lock_new();
+    bio_lookup_lock = CRYPTO_THREAD_glock_new("bio_lookup");
     return bio_lookup_lock != NULL;
 }
 
index be6420c028f86ba6cf28876bf460b23618b29360..99e81c7cadd6f28a528b78151bec13d23718cbb9 100644 (file)
@@ -15,7 +15,7 @@ static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
 
 DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
 {
-    bio_type_lock = CRYPTO_THREAD_lock_new();
+    bio_type_lock = CRYPTO_THREAD_glock_new("bio_type");
     return bio_type_lock != NULL;
 }
 
index c060a5d6ac9723c9067b93d392a51fe6d022640a..8e2ff9da16060bfee5c3af5edb8469b818f7bccc 100644 (file)
@@ -21,7 +21,7 @@ CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
 DEFINE_RUN_ONCE(do_engine_lock_init)
 {
     OPENSSL_init_crypto(0, NULL);
-    global_engine_lock = CRYPTO_THREAD_lock_new();
+    global_engine_lock = CRYPTO_THREAD_glock_new("global_engine");
     return global_engine_lock != NULL;
 }
 
index ab08435af1e2618a6c89f8f5ef46dfcfac5b3051..5e3474fffff007ae03f486d472dbad3f97c287b7 100644 (file)
@@ -267,7 +267,7 @@ static void ERR_STATE_free(ERR_STATE *s)
 DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
 {
     OPENSSL_init_crypto(0, NULL);
-    err_string_lock = CRYPTO_THREAD_lock_new();
+    err_string_lock = CRYPTO_THREAD_glock_new("err_string");
     int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
                                             err_string_data_cmp);
     return err_string_lock != NULL && int_error_hash != NULL;
index 22c4d3d9b904d73c5db4117faf59467e272de597..30b16f945728a6c4de1601782987606ac77c7653 100644 (file)
@@ -39,7 +39,7 @@ static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
 DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
 {
     OPENSSL_init_crypto(0, NULL);
-    ex_data_lock = CRYPTO_THREAD_lock_new();
+    ex_data_lock = CRYPTO_THREAD_glock_new("ex_data");
     return ex_data_lock != NULL;
 }
 
index 074e683cc01156b46c1517fcaec14ca9cf7310af..458520a3e2125b697be64ad6d81bcdf1111a655d 100644 (file)
 #include "internal/dso.h"
 #include "internal/store.h"
 
+
+typedef struct global_lock_st {
+    CRYPTO_RWLOCK *lock;
+    const char *name;
+    struct global_lock_st *next;
+} GLOBAL_LOCK;
+
+static GLOBAL_LOCK *global_locks;
+
 static int stopped = 0;
 
 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
@@ -65,6 +74,7 @@ struct ossl_init_stop_st {
 
 static OPENSSL_INIT_STOP *stop_handlers = NULL;
 static CRYPTO_RWLOCK *init_lock = NULL;
+static CRYPTO_RWLOCK *glock_lock = NULL;
 
 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
 static int base_inited = 0;
@@ -84,7 +94,8 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_base)
 #ifndef OPENSSL_SYS_UEFI
     atexit(OPENSSL_cleanup);
 #endif
-    if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
+    if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL
+            || (glock_lock = CRYPTO_THREAD_lock_new()) == NULL)
         return 0;
     OPENSSL_cpuid_setup();
 
@@ -405,6 +416,14 @@ void OPENSSL_cleanup(void)
         return;
     stopped = 1;
 
+    /* Free list of global locks. */
+    while (global_locks != NULL) {
+        GLOBAL_LOCK *next = global_locks->next;
+
+        free(global_locks);
+        global_locks = next;
+    }
+
     /*
      * Thread stop may not get automatically called by the thread library for
      * the very last thread in some situations, so call it directly.
@@ -421,6 +440,9 @@ void OPENSSL_cleanup(void)
     stop_handlers = NULL;
 
     CRYPTO_THREAD_lock_free(init_lock);
+    init_lock = NULL;
+    CRYPTO_THREAD_lock_free(glock_lock);
+    glock_lock = NULL;
 
     /*
      * We assume we are single-threaded for this function, i.e. no race
@@ -684,7 +706,47 @@ int OPENSSL_atexit(void (*handler)(void))
     return 1;
 }
 
-#ifdef OPENSSL_SYS_UNIX
+#ifndef OPENSSL_SYS_UNIX
+CRYPTO_RWLOCK *CRYPTO_THREAD_glock_new(const char *name)
+{
+    return CRYPTO_THREAD_lock_new();
+}
+#else
+
+
+/*
+ * Create a new global lock, return NULL on error.
+ */
+CRYPTO_RWLOCK *CRYPTO_THREAD_glock_new(const char *name)
+{
+    GLOBAL_LOCK *newlock;
+
+    if (name == NULL
+            || glock_lock == NULL
+            || (newlock = malloc(sizeof(*newlock))) == NULL)
+        return CRYPTO_THREAD_lock_new();
+    CRYPTO_THREAD_write_lock(glock_lock);
+    newlock->name = name;
+    newlock->lock = CRYPTO_THREAD_lock_new();
+    newlock->next = global_locks;
+    global_locks = newlock->next;
+    CRYPTO_THREAD_unlock(glock_lock);
+    return newlock->lock;
+}
+
+/*
+ * Unlock all global locks.
+ */
+static void unlock_all(void)
+{
+    GLOBAL_LOCK *lp;
+
+    CRYPTO_THREAD_write_lock(init_lock);
+    for (lp = global_locks; lp != NULL; lp = lp->next)
+        CRYPTO_THREAD_unlock(lp->lock);
+    CRYPTO_THREAD_unlock(init_lock);
+}
+
 /*
  * The following three functions are for OpenSSL developers.  This is
  * where we set/reset state across fork (called via pthread_atfork when
@@ -698,14 +760,22 @@ int OPENSSL_atexit(void (*handler)(void))
 
 void OPENSSL_fork_prepare(void)
 {
+    GLOBAL_LOCK *lp;
+
+    CRYPTO_THREAD_write_lock(init_lock);
+    for (lp = global_locks; lp != NULL; lp = lp->next)
+        CRYPTO_THREAD_write_lock(lp->lock);
+    CRYPTO_THREAD_unlock(init_lock);
 }
 
 void OPENSSL_fork_parent(void)
 {
+    unlock_all();
 }
 
 void OPENSSL_fork_child(void)
 {
+    unlock_all();
     rand_fork();
 }
 #endif
index 70b5e62ab55fc6d77b409daf2dd7ccad1aba6e0c..9228dcef13240b02908a5f47566e97a47be0254d 100644 (file)
@@ -90,8 +90,8 @@ static CRYPTO_THREAD_ID disabling_threadid;
 
 DEFINE_RUN_ONCE_STATIC(do_memdbg_init)
 {
-    malloc_lock = CRYPTO_THREAD_lock_new();
-    long_malloc_lock = CRYPTO_THREAD_lock_new();
+    malloc_lock = CRYPTO_THREAD_glock_new("malloc");
+    long_malloc_lock = CRYPTO_THREAD_glock_new("long_malloc");
     if (malloc_lock == NULL || long_malloc_lock == NULL
         || !CRYPTO_THREAD_init_local(&appinfokey, NULL)) {
         CRYPTO_THREAD_lock_free(malloc_lock);
index f8470249c39648f3eaeefb89b3b34f1345282641..ff6e657c62dcb5137d61faeb10d212b0cb085578 100644 (file)
@@ -66,7 +66,7 @@ int CRYPTO_secure_malloc_init(size_t size, int minsize)
     int ret = 0;
 
     if (!secure_mem_initialized) {
-        sec_malloc_lock = CRYPTO_THREAD_lock_new();
+        sec_malloc_lock = CRYPTO_THREAD_glock_new("sec_malloc");
         if (sec_malloc_lock == NULL)
             return 0;
         if ((ret = sh_init(size, minsize)) != 0) {
index 73676445c1e43b2559bea6fc9610819675258385..d0e8e05fe8789af373f42af955a66ca9d89de5bd 100644 (file)
@@ -69,7 +69,7 @@ DEFINE_RUN_ONCE_STATIC(o_names_init)
 {
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
     names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
-    obj_lock = CRYPTO_THREAD_lock_new();
+    obj_lock = CRYPTO_THREAD_glock_new("obj");
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
     return names_lh != NULL && obj_lock != NULL;
 }
index 83ddc27ee5a9f8225a350accb1a828045269c3b1..b7f7e4c3412e1cc3989227e4c24fc6bf30eda79d 100644 (file)
@@ -337,11 +337,11 @@ void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
  * Creates a global DRBG with default settings.
  * Returns 1 on success, 0 on failure
  */
-static int setup_drbg(RAND_DRBG *drbg)
+static int setup_drbg(RAND_DRBG *drbg, const char *name)
 {
     int ret = 1;
 
-    drbg->lock = CRYPTO_THREAD_lock_new();
+    drbg->lock = CRYPTO_THREAD_glock_new(name);
     ret &= drbg->lock != NULL;
     drbg->size = RANDOMNESS_NEEDED;
     drbg->secure = CRYPTO_secure_malloc_initialized();
@@ -362,8 +362,8 @@ DEFINE_RUN_ONCE_STATIC(do_rand_init_drbg)
 {
     int ret = 1;
 
-    ret &= setup_drbg(&rand_drbg);
-    ret &= setup_drbg(&priv_drbg);
+    ret &= setup_drbg(&rand_drbg, "rand_drbg");
+    ret &= setup_drbg(&priv_drbg, "priv_drbg");
 
     return ret;
 }
index a27281c6e1a4e560c91d816ad5be82c21e39a504..2f2ab6a86dc63934b6bb918e11d9dc43248347d9 100644 (file)
@@ -182,13 +182,13 @@ DEFINE_RUN_ONCE_STATIC(do_rand_init)
     int ret = 1;
 
 #ifndef OPENSSL_NO_ENGINE
-    rand_engine_lock = CRYPTO_THREAD_lock_new();
+    rand_engine_lock = CRYPTO_THREAD_glock_new("rand_engine");
     ret &= rand_engine_lock != NULL;
 #endif
-    rand_meth_lock = CRYPTO_THREAD_lock_new();
+    rand_meth_lock = CRYPTO_THREAD_glock_new("rand_meth");
     ret &= rand_meth_lock != NULL;
 
-    rand_bytes.lock = CRYPTO_THREAD_lock_new();
+    rand_bytes.lock = CRYPTO_THREAD_glock_new("rand_bytes");
     ret &= rand_bytes.lock != NULL;
     rand_bytes.curr = 0;
     rand_bytes.size = MAX_RANDOMNESS_HELD;
index 85c38b84dd127e55e081ee6b880c45e530d530f4..987ca42f8cfbbb94e7fec6f5236851d5f0543ee4 100644 (file)
@@ -20,7 +20,7 @@ static CRYPTO_ONCE registry_init = CRYPTO_ONCE_STATIC_INIT;
 
 DEFINE_RUN_ONCE_STATIC(do_registry_init)
 {
-    registry_lock = CRYPTO_THREAD_lock_new();
+    registry_lock = CRYPTO_THREAD_glock_new("registry");
     return registry_lock != NULL;
 }
 
index cebde9734b8bba89ab0cb0cba54b10dc952c526f..c4a36afb7ed7ce9a6fe0e99045950ca856092659 100644 (file)
@@ -68,6 +68,7 @@ typedef struct {
 typedef void CRYPTO_RWLOCK;
 
 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void);
+CRYPTO_RWLOCK *CRYPTO_THREAD_glock_new(const char *name);
 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock);
 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock);
 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock);
index d955f9166b71bd968991eb7128af011e27aecbf0..8a276b96cdc2e2713e23e932bdca53c9481b4be5 100644 (file)
@@ -4389,3 +4389,4 @@ EVP_aria_256_ccm                        4332      1_1_1   EXIST::FUNCTION:ARIA
 EVP_aria_128_gcm                        4333   1_1_1   EXIST::FUNCTION:ARIA
 EVP_aria_128_ccm                        4334   1_1_1   EXIST::FUNCTION:ARIA
 EVP_aria_192_gcm                        4335   1_1_1   EXIST::FUNCTION:ARIA
+CRYPTO_THREAD_glock_new                 4336   1_1_1   EXIST::FUNCTION: