diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-04 00:27:57 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-08-10 11:52:15 +1000 |
commit | 39a4e527933a6b46234ff6f229469a68d9d455ac (patch) | |
tree | 0f9c90e274f48839a6b4f234007a4a5c5fc4c9d7 | |
parent | 5d6b15859e25da8271a3820662bb9d1f8a935107 (diff) | |
download | luaossl-39a4e527933a6b46234ff6f229469a68d9d455ac.tar.gz luaossl-39a4e527933a6b46234ff6f229469a68d9d455ac.tar.bz2 luaossl-39a4e527933a6b46234ff6f229469a68d9d455ac.zip |
Use a single lock across multiple init functions
-rw-r--r-- | src/openssl.c | 39 |
1 files changed, 13 insertions, 26 deletions
diff --git a/src/openssl.c b/src/openssl.c index a01fde5..df559d7 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
@@ -1781,14 +1781,11 @@ static int compat_X509_up_ref(X509 *crt) { | |||
1781 | */ | 1781 | */ |
1782 | #endif | 1782 | #endif |
1783 | 1783 | ||
1784 | /* compat_init must not be called from multiple threads at once */ | ||
1784 | static int compat_init(void) { | 1785 | static int compat_init(void) { |
1785 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
1786 | static int store_index = -1, ssl_ctx_index = -1, done; | 1786 | static int store_index = -1, ssl_ctx_index = -1, done; |
1787 | int error = 0; | 1787 | int error = 0; |
1788 | 1788 | ||
1789 | if ((error = pthread_mutex_lock(&mutex))) | ||
1790 | return error; | ||
1791 | |||
1792 | if (done) | 1789 | if (done) |
1793 | goto epilog; | 1790 | goto epilog; |
1794 | 1791 | ||
@@ -1859,8 +1856,6 @@ epilog: | |||
1859 | compat.tmp.store = NULL; | 1856 | compat.tmp.store = NULL; |
1860 | } | 1857 | } |
1861 | 1858 | ||
1862 | (void)pthread_mutex_unlock(&mutex); | ||
1863 | |||
1864 | return error; | 1859 | return error; |
1865 | sslerr: | 1860 | sslerr: |
1866 | error = auxL_EOPENSSL; | 1861 | error = auxL_EOPENSSL; |
@@ -1984,15 +1979,12 @@ static void ex_onfree(void *parent NOTUSED, void *_data, CRYPTO_EX_DATA *ad NOTU | |||
1984 | free(data); | 1979 | free(data); |
1985 | } /* ex_onfree() */ | 1980 | } /* ex_onfree() */ |
1986 | 1981 | ||
1982 | /* ex_init must not be called from multiple threads at once */ | ||
1987 | static int ex_init(void) { | 1983 | static int ex_init(void) { |
1988 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
1989 | static int done; | 1984 | static int done; |
1990 | struct ex_type *type; | 1985 | struct ex_type *type; |
1991 | int error = 0; | 1986 | int error = 0; |
1992 | 1987 | ||
1993 | if ((error = pthread_mutex_lock(&mutex))) | ||
1994 | return error; | ||
1995 | |||
1996 | if (done) | 1988 | if (done) |
1997 | goto epilog; | 1989 | goto epilog; |
1998 | 1990 | ||
@@ -2013,8 +2005,6 @@ static int ex_init(void) { | |||
2013 | 2005 | ||
2014 | done = 1; | 2006 | done = 1; |
2015 | epilog: | 2007 | epilog: |
2016 | (void)pthread_mutex_unlock(&mutex); | ||
2017 | |||
2018 | return error; | 2008 | return error; |
2019 | sslerr: | 2009 | sslerr: |
2020 | error = auxL_EOPENSSL; | 2010 | error = auxL_EOPENSSL; |
@@ -10058,14 +10048,11 @@ static unsigned long mt_gettid(void) { | |||
10058 | #endif | 10048 | #endif |
10059 | } /* mt_gettid() */ | 10049 | } /* mt_gettid() */ |
10060 | 10050 | ||
10051 | /* mt_init must not be called from multiple threads at once */ | ||
10061 | static int mt_init(void) { | 10052 | static int mt_init(void) { |
10062 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
10063 | static int done, bound; | 10053 | static int done, bound; |
10064 | int error = 0; | 10054 | int error = 0; |
10065 | 10055 | ||
10066 | if ((error = pthread_mutex_lock(&mutex))) | ||
10067 | return error; | ||
10068 | |||
10069 | if (done) | 10056 | if (done) |
10070 | goto epilog; | 10057 | goto epilog; |
10071 | 10058 | ||
@@ -10108,8 +10095,6 @@ static int mt_init(void) { | |||
10108 | 10095 | ||
10109 | done = 1; | 10096 | done = 1; |
10110 | epilog: | 10097 | epilog: |
10111 | pthread_mutex_unlock(&mutex); | ||
10112 | |||
10113 | return error; | 10098 | return error; |
10114 | } /* mt_init() */ | 10099 | } /* mt_init() */ |
10115 | 10100 | ||
@@ -10119,12 +10104,11 @@ static void initall(lua_State *L) { | |||
10119 | static int initssl; | 10104 | static int initssl; |
10120 | int error; | 10105 | int error; |
10121 | 10106 | ||
10122 | if ((error = mt_init())) | ||
10123 | auxL_error(L, error, "openssl.init"); | ||
10124 | |||
10125 | pthread_mutex_lock(&mutex); | 10107 | pthread_mutex_lock(&mutex); |
10126 | 10108 | ||
10127 | if (!initssl) { | 10109 | error = mt_init(); |
10110 | |||
10111 | if (!error && !initssl) { | ||
10128 | initssl = 1; | 10112 | initssl = 1; |
10129 | 10113 | ||
10130 | SSL_load_error_strings(); | 10114 | SSL_load_error_strings(); |
@@ -10138,12 +10122,15 @@ static void initall(lua_State *L) { | |||
10138 | OPENSSL_config(NULL); | 10122 | OPENSSL_config(NULL); |
10139 | } | 10123 | } |
10140 | 10124 | ||
10141 | pthread_mutex_unlock(&mutex); | 10125 | if (!error) |
10126 | error = compat_init(); | ||
10142 | 10127 | ||
10143 | if ((error = compat_init())) | 10128 | if (!error) |
10144 | auxL_error(L, error, "openssl.init"); | 10129 | error = ex_init(); |
10130 | |||
10131 | pthread_mutex_unlock(&mutex); | ||
10145 | 10132 | ||
10146 | if ((error = ex_init())) | 10133 | if (error) |
10147 | auxL_error(L, error, "openssl.init"); | 10134 | auxL_error(L, error, "openssl.init"); |
10148 | 10135 | ||
10149 | ex_newstate(L); | 10136 | ex_newstate(L); |