Skip to content

Commit c2e4e5d

Browse files
committed
Change all our uses of CRYPTO_THREAD_run_once to use RUN_ONCE instead
That way, we have a way to check if the init function was successful or not. Reviewed-by: Kurt Roeckx <[email protected]>
1 parent 925d17f commit c2e4e5d

File tree

15 files changed

+178
-105
lines changed

15 files changed

+178
-105
lines changed

crypto/bio/b_addr.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef OPENSSL_NO_SOCK
1616
#include <openssl/err.h>
1717
#include <openssl/buffer.h>
18+
#include <internal/thread_once.h>
1819
#include <ctype.h>
1920

2021
CRYPTO_RWLOCK *bio_lookup_lock;
@@ -601,9 +602,10 @@ static int addrinfo_wrap(int family, int socktype,
601602
return 1;
602603
}
603604

604-
static void do_bio_lookup_init(void)
605+
DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init)
605606
{
606607
bio_lookup_lock = CRYPTO_THREAD_lock_new();
608+
return (bio_lookup_lock != NULL);
607609
}
608610

609611
/*-
@@ -727,7 +729,11 @@ int BIO_lookup(const char *host, const char *service,
727729
struct servent se_fallback = { NULL, NULL, 0, NULL };
728730
#endif
729731

730-
CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
732+
if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {
733+
BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
734+
ret = 0;
735+
goto err;
736+
}
731737

732738
CRYPTO_THREAD_write_lock(bio_lookup_lock);
733739
he_fallback_address = INADDR_ANY;

crypto/engine/eng_init.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ int ENGINE_init(ENGINE *e)
8080
ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
8181
return 0;
8282
}
83-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
83+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
84+
ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);
85+
return 0;
86+
}
8487
CRYPTO_THREAD_write_lock(global_engine_lock);
8588
ret = engine_unlocked_init(e);
8689
CRYPTO_THREAD_unlock(global_engine_lock);

crypto/engine/eng_int.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
# include "internal/cryptlib.h"
2020
# include <internal/engine.h>
21+
# include <internal/thread_once.h>
2122

2223
#ifdef __cplusplus
2324
extern "C" {
@@ -123,7 +124,7 @@ void engine_pkey_asn1_meths_free(ENGINE *e);
123124

124125
/* Once initialisation function */
125126
extern CRYPTO_ONCE engine_lock_init;
126-
void do_engine_lock_init(void);
127+
DECLARE_RUN_ONCE(do_engine_lock_init)
127128

128129
/*
129130
* This is a structure for storing implementations of various crypto

crypto/engine/eng_lib.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
1616

1717
/* The "new"/"free" stuff first */
1818

19-
void do_engine_lock_init(void)
19+
DEFINE_RUN_ONCE(do_engine_lock_init)
2020
{
2121
global_engine_lock = CRYPTO_THREAD_lock_new();
22+
return global_engine_lock != NULL;
2223
}
2324

2425
ENGINE *ENGINE_new(void)
2526
{
2627
ENGINE *ret;
2728

28-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
29-
30-
ret = OPENSSL_zalloc(sizeof(*ret));
31-
if (ret == NULL) {
29+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)
30+
|| (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
3231
ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
3332
return NULL;
3433
}

crypto/engine/eng_list.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ ENGINE *ENGINE_get_first(void)
136136
{
137137
ENGINE *ret;
138138

139-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
139+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
140+
ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
141+
return NULL;
142+
}
143+
140144
CRYPTO_THREAD_write_lock(global_engine_lock);
141145
ret = engine_list_head;
142146
if (ret) {
@@ -151,7 +155,11 @@ ENGINE *ENGINE_get_last(void)
151155
{
152156
ENGINE *ret;
153157

154-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
158+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
159+
ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
160+
return NULL;
161+
}
162+
155163
CRYPTO_THREAD_write_lock(global_engine_lock);
156164
ret = engine_list_tail;
157165
if (ret) {
@@ -279,7 +287,11 @@ ENGINE *ENGINE_by_id(const char *id)
279287
ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
280288
return NULL;
281289
}
282-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
290+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
291+
ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
292+
return NULL;
293+
}
294+
283295
CRYPTO_THREAD_write_lock(global_engine_lock);
284296
iterator = engine_list_head;
285297
while (iterator && (strcmp(id, iterator->id) != 0))

crypto/engine/tb_asnmth.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
189189
fstr.str = str;
190190
fstr.len = len;
191191

192-
CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
192+
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
193+
ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
194+
return NULL;
195+
}
196+
193197
CRYPTO_THREAD_write_lock(global_engine_lock);
194198
engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
195199
/* If found obtain a structural reference to engine */

crypto/err/err.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <openssl/buffer.h>
1919
#include <openssl/bio.h>
2020
#include <openssl/opensslconf.h>
21+
#include <internal/thread_once.h>
2122

2223
static void err_load_strings(int lib, ERR_STRING_DATA *str);
2324

@@ -270,9 +271,10 @@ static void ERR_STATE_free(ERR_STATE *s)
270271
OPENSSL_free(s);
271272
}
272273

273-
static void do_err_strings_init(void)
274+
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
274275
{
275276
err_string_lock = CRYPTO_THREAD_lock_new();
277+
return err_string_lock != NULL;
276278
}
277279

278280
void err_cleanup(void)
@@ -284,7 +286,7 @@ void err_cleanup(void)
284286
void ERR_load_ERR_strings(void)
285287
{
286288
#ifndef OPENSSL_NO_ERR
287-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
289+
RUN_ONCE(&err_string_init, do_err_strings_init);
288290

289291
err_load_strings(0, ERR_str_libraries);
290292
err_load_strings(0, ERR_str_reasons);
@@ -316,11 +318,12 @@ void ERR_load_strings(int lib, ERR_STRING_DATA *str)
316318
err_load_strings(lib, str);
317319
}
318320

319-
void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
321+
int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
320322
{
321323
LHASH_OF(ERR_STRING_DATA) *hash;
322324

323-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
325+
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
326+
return 0;
324327

325328
CRYPTO_THREAD_write_lock(err_string_lock);
326329
hash = get_hash(0, 0);
@@ -332,11 +335,14 @@ void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
332335
}
333336
}
334337
CRYPTO_THREAD_unlock(err_string_lock);
338+
339+
return 1;
335340
}
336341

337342
void err_free_strings_int(void)
338343
{
339-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
344+
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
345+
return;
340346

341347
CRYPTO_THREAD_write_lock(err_string_lock);
342348
lh_ERR_STRING_DATA_free(int_error_hash);
@@ -582,7 +588,9 @@ const char *ERR_lib_error_string(unsigned long e)
582588
ERR_STRING_DATA d, *p;
583589
unsigned long l;
584590

585-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
591+
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
592+
return NULL;
593+
}
586594

587595
l = ERR_GET_LIB(e);
588596
d.error = ERR_PACK(l, 0, 0);
@@ -595,7 +603,9 @@ const char *ERR_func_error_string(unsigned long e)
595603
ERR_STRING_DATA d, *p;
596604
unsigned long l, f;
597605

598-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
606+
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
607+
return NULL;
608+
}
599609

600610
l = ERR_GET_LIB(e);
601611
f = ERR_GET_FUNC(e);
@@ -609,7 +619,9 @@ const char *ERR_reason_error_string(unsigned long e)
609619
ERR_STRING_DATA d, *p = NULL;
610620
unsigned long l, r;
611621

612-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
622+
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
623+
return NULL;
624+
}
613625

614626
l = ERR_GET_LIB(e);
615627
r = ERR_GET_REASON(e);
@@ -644,16 +656,17 @@ void ERR_remove_state(unsigned long pid)
644656
}
645657
#endif
646658

647-
static void err_do_init(void)
659+
DEFINE_RUN_ONCE_STATIC(err_do_init)
648660
{
649-
CRYPTO_THREAD_init_local(&err_thread_local, NULL);
661+
return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
650662
}
651663

652664
ERR_STATE *ERR_get_state(void)
653665
{
654666
ERR_STATE *state = NULL;
655667

656-
CRYPTO_THREAD_run_once(&err_init, err_do_init);
668+
if (!RUN_ONCE(&err_init, err_do_init))
669+
return NULL;
657670

658671
state = CRYPTO_THREAD_get_local(&err_thread_local);
659672

@@ -679,7 +692,9 @@ int ERR_get_next_error_library(void)
679692
{
680693
int ret;
681694

682-
CRYPTO_THREAD_run_once(&err_string_init, do_err_strings_init);
695+
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
696+
return 0;
697+
}
683698

684699
CRYPTO_THREAD_write_lock(err_string_lock);
685700
ret = int_err_library_number++;

crypto/ex_data.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include "internal/cryptlib_int.h"
11+
#include "internal/thread_once.h"
1112
#include <openssl/lhash.h>
1213

1314
/*
@@ -35,9 +36,10 @@ static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
3536
static CRYPTO_RWLOCK *ex_data_lock = NULL;
3637
static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
3738

38-
static void do_ex_data_init(void)
39+
DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
3940
{
4041
ex_data_lock = CRYPTO_THREAD_lock_new();
42+
return ex_data_lock != NULL;
4143
}
4244

4345
/*
@@ -53,7 +55,10 @@ static EX_CALLBACKS *get_and_lock(int class_index)
5355
return NULL;
5456
}
5557

56-
CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);
58+
if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
59+
CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
60+
return NULL;
61+
}
5762

5863
if (ex_data_lock == NULL) {
5964
/*

0 commit comments

Comments
 (0)