diff options
author | Matt Caswell <matt@openssl.org> | 2023-05-10 14:44:17 +0100 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2023-05-30 17:19:11 +0100 |
commit | 7f2c22c1b9ec46070aa588d7f4a5ad5fe4a60bf4 (patch) | |
tree | 02394f3fbde4e0173099cfc83399fd7662a5c9f3 /crypto/rand | |
parent | 36424806d699233b9a90a3a97fff3011828e2548 (diff) |
Avoid taking a write lock in RAND_get_rand_method()
The function RAND_get_rand_method() is called every time RAND_bytes() or
RAND_priv_bytes() is called. We were obtaining a write lock in order to
find the default random method - even though we rarely write. We change
this to a read lock and only fallback to a write lock if we need to.
Partial fix for #20286
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20929)
Diffstat (limited to 'crypto/rand')
-rw-r--r-- | crypto/rand/rand_lib.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index 0cdb9caa6d..9b1b5999cf 100644 --- a/crypto/rand/rand_lib.c +++ b/crypto/rand/rand_lib.c @@ -189,6 +189,13 @@ const RAND_METHOD *RAND_get_rand_method(void) if (!RUN_ONCE(&rand_init, do_rand_init)) return NULL; + if (!CRYPTO_THREAD_read_lock(rand_meth_lock)) + return NULL; + tmp_meth = default_RAND_meth; + CRYPTO_THREAD_unlock(rand_meth_lock); + if (tmp_meth != NULL) + return tmp_meth; + if (!CRYPTO_THREAD_write_lock(rand_meth_lock)) return NULL; if (default_RAND_meth == NULL) { |