rand: Add missing check for rand_get_global
authorJiasheng Jiang <jiasheng@iscas.ac.cn>
Tue, 15 Feb 2022 09:45:04 +0000 (17:45 +0800)
committerDr. Matthias St. Pierre <matthias.st.pierre@ncp-e.com>
Sun, 20 Feb 2022 12:09:27 +0000 (13:09 +0100)
As the potential failure of the rand_get_global(),
for example fail to get lock, 'dgbl' could be NULL
pointer and be dereferenced later.
Therefore, it should be better to check it and return
error if fails, like RAND_get0_primary() and other callers.

Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/17690)

crypto/rand/rand_lib.c

index 4f36e84bd45173fc55006320865d11d1132822a6..c772bcc79cd7b7a47d5ade54474c11ed2e3f1dc9 100644 (file)
@@ -529,6 +529,8 @@ static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
     EVP_RAND_CTX *ctx;
     char *name;
 
+    if (dgbl == NULL)
+        return NULL;
     name = dgbl->seed_name != NULL ? dgbl->seed_name : "SEED-SRC";
     rand = EVP_RAND_fetch(libctx, name, dgbl->seed_propq);
     if (rand == NULL) {
@@ -561,6 +563,8 @@ static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
     const OSSL_PARAM *settables;
     char *name, *cipher;
 
+    if (dgbl == NULL)
+        return NULL;
     name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
     rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
     if (rand == NULL) {
@@ -763,6 +767,9 @@ static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
         return 0;
     }
 
+    if (dgbl == NULL)
+        return 0;
+
     for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
         cval = sk_CONF_VALUE_value(elist, i);
         if (strcasecmp(cval->name, "random") == 0) {