diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2018-11-28 15:51:33 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2018-11-28 15:51:33 +0000 |
commit | 65b8edd794c381efd1b0e98e7f00fb680aab20b5 (patch) | |
tree | 11a504a4692f49a43d18b93b0baf31d3941ac5bb | |
parent | 715795ebcd348ae1d63f63e5769d425ec812babc (diff) |
Correct lock initialisation for libcrypto.
The current crypto_lock_init() function is not called early enough, meaning
that locks are already in use before it gets called. Worse, locks could be
in use when they are then initialised. Furthermore, since functions like
CRYPTO_lock() are public API, these could be called directly bypassing
initialisation.
Avoid these issues by using static initialisers.
ok bcook@
-rw-r--r-- | lib/libcrypto/crypto_init.c | 5 | ||||
-rw-r--r-- | lib/libcrypto/crypto_lock.c | 66 |
2 files changed, 55 insertions, 16 deletions
diff --git a/lib/libcrypto/crypto_init.c b/lib/libcrypto/crypto_init.c index 3745e2e7186..67e79208904 100644 --- a/lib/libcrypto/crypto_init.c +++ b/lib/libcrypto/crypto_init.c @@ -23,6 +23,7 @@ #include <openssl/conf.h> #include <openssl/evp.h> #include <openssl/err.h> + #include "cryptlib.h" int OpenSSL_config(const char *); @@ -30,17 +31,15 @@ int OpenSSL_no_config(void); static pthread_t crypto_init_thread; -void crypto_init_locks(void); - static void OPENSSL_init_crypto_internal(void) { crypto_init_thread = pthread_self(); + OPENSSL_cpuid_setup(); ERR_load_crypto_strings(); OpenSSL_add_all_ciphers(); OpenSSL_add_all_digests(); - crypto_init_locks(); } int diff --git a/lib/libcrypto/crypto_lock.c b/lib/libcrypto/crypto_lock.c index 3d615cf485b..5d317a81c00 100644 --- a/lib/libcrypto/crypto_lock.c +++ b/lib/libcrypto/crypto_lock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crypto_lock.c,v 1.1 2018/11/11 06:41:28 bcook Exp $ */ +/* $OpenBSD: crypto_lock.c,v 1.2 2018/11/28 15:51:32 jsing Exp $ */ /* * Copyright (c) 2018 Brent Cook <bcook@openbsd.org> * @@ -19,16 +19,54 @@ #include <openssl/crypto.h> -static pthread_mutex_t locks[CRYPTO_NUM_LOCKS]; +static pthread_mutex_t locks[] = { + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, +}; -void -crypto_init_locks(void) -{ - int i; +#define CTASSERT(x) extern char _ctassert[(x) ? 1 : -1 ] \ + __attribute__((__unused__)) - for (i = 0; i < CRYPTO_NUM_LOCKS; i++) - pthread_mutex_init(&locks[i], NULL); -} +CTASSERT((sizeof(locks) / sizeof(*locks)) == CRYPTO_NUM_LOCKS); void CRYPTO_lock(int mode, int type, const char *file, int line) @@ -37,19 +75,21 @@ CRYPTO_lock(int mode, int type, const char *file, int line) return; if (mode & CRYPTO_LOCK) - pthread_mutex_lock(&locks[type]); - else - pthread_mutex_unlock(&locks[type]); + (void) pthread_mutex_lock(&locks[type]); + else if (mode & CRYPTO_UNLOCK) + (void) pthread_mutex_unlock(&locks[type]); } int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, int line) { - int ret = 0; + int ret; + CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, file, line); ret = *pointer + amount; *pointer = ret; CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, file, line); + return (ret); } |