rand: fix coverity 1473636: data race condition
authorPauli <ppzgs1@gmail.com>
Tue, 23 Mar 2021 00:59:34 +0000 (10:59 +1000)
committerPauli <pauli@openssl.org>
Thu, 25 Mar 2021 22:41:32 +0000 (08:41 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14651)

crypto/rand/rand_lib.c

index 318540cff05a0138718d7b624b59c51ccb6eb11e..f6c5bc15eec7a64d4b197b3e8c1fcc3a14393a11 100644 (file)
@@ -158,7 +158,8 @@ int RAND_poll(void)
 }
 
 # ifndef OPENSSL_NO_DEPRECATED_3_0
-int RAND_set_rand_method(const RAND_METHOD *meth)
+static int rand_set_rand_method_internal(const RAND_METHOD *meth,
+                                         ossl_unused ENGINE *e)
 {
     if (!RUN_ONCE(&rand_init, do_rand_init))
         return 0;
@@ -167,13 +168,18 @@ int RAND_set_rand_method(const RAND_METHOD *meth)
         return 0;
 #  ifndef OPENSSL_NO_ENGINE
     ENGINE_finish(funct_ref);
-    funct_ref = NULL;
+    funct_ref = e;
 #  endif
     default_RAND_meth = meth;
     CRYPTO_THREAD_unlock(rand_meth_lock);
     return 1;
 }
 
+int RAND_set_rand_method(const RAND_METHOD *meth)
+{
+    return rand_set_rand_method_internal(meth, NULL);
+}
+
 const RAND_METHOD *RAND_get_rand_method(void)
 {
     const RAND_METHOD *tmp_meth = NULL;
@@ -228,8 +234,7 @@ int RAND_set_rand_engine(ENGINE *engine)
     }
 
     /* This function releases any prior ENGINE so call it first */
-    RAND_set_rand_method(tmp_meth);
-    funct_ref = engine;
+    rand_set_rand_method_internal(tmp_meth, engine);
     CRYPTO_THREAD_unlock(rand_engine_lock);
     return 1;
 }