X-Git-Url: https://git.openssl.org/?p=openssl.git;a=blobdiff_plain;f=crypto%2Fex_data.c;h=22c4d3d9b904d73c5db4117faf59467e272de597;hp=0666393900b4852012a76ac26722c594fd0ec5e9;hb=43a0449fe6ce18b750803be8a115a412a7235496;hpb=aa6bb1352b1026b20a23b49da4efdcf171926eb0 diff --git a/crypto/ex_data.c b/crypto/ex_data.c index 0666393900..22c4d3d9b9 100644 --- a/crypto/ex_data.c +++ b/crypto/ex_data.c @@ -8,6 +8,7 @@ */ #include "internal/cryptlib_int.h" +#include "internal/thread_once.h" #include /* @@ -35,9 +36,11 @@ static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT]; static CRYPTO_RWLOCK *ex_data_lock = NULL; static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT; -static void do_ex_data_init(void) +DEFINE_RUN_ONCE_STATIC(do_ex_data_init) { + OPENSSL_init_crypto(0, NULL); ex_data_lock = CRYPTO_THREAD_lock_new(); + return ex_data_lock != NULL; } /* @@ -53,7 +56,10 @@ static EX_CALLBACKS *get_and_lock(int class_index) return NULL; } - CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init); + if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) { + CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE); + return NULL; + } if (ex_data_lock == NULL) { /* @@ -114,11 +120,11 @@ static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, { } -static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, +static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, void *from_d, int idx, long argl, void *argp) { - return 0; + return 1; } int CRYPTO_free_ex_index(int class_index, int idx) @@ -245,13 +251,14 @@ int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) * for each index in the class used by this variable */ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, - CRYPTO_EX_DATA *from) + const CRYPTO_EX_DATA *from) { int mx, j, i; - char *ptr; + void *ptr; EX_CALLBACK *stack[10]; EX_CALLBACK **storage = NULL; EX_CALLBACKS *ip; + int toret = 0; if (from->sk == NULL) /* Nothing to copy over */ @@ -274,21 +281,35 @@ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, } CRYPTO_THREAD_unlock(ex_data_lock); - if (mx > 0 && storage == NULL) { + if (mx == 0) + return 1; + if (storage == NULL) { CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE); return 0; } + /* + * Make sure the ex_data stack is at least |mx| elements long to avoid + * issues in the for loop that follows; so go get the |mx|'th element + * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign + * to itself. This is normally a no-op; but ensures the stack is the + * proper size + */ + if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1))) + goto err; for (i = 0; i < mx; i++) { ptr = CRYPTO_get_ex_data(from, i); if (storage[i] && storage[i]->dup_func) - storage[i]->dup_func(to, from, &ptr, i, - storage[i]->argl, storage[i]->argp); + if (!storage[i]->dup_func(to, from, &ptr, i, + storage[i]->argl, storage[i]->argp)) + goto err; CRYPTO_set_ex_data(to, i, ptr); } + toret = 1; + err: if (storage != stack) OPENSSL_free(storage); - return 1; + return toret; } @@ -301,11 +322,12 @@ void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) int mx, i; EX_CALLBACKS *ip; void *ptr; + EX_CALLBACK *f; EX_CALLBACK *stack[10]; EX_CALLBACK **storage = NULL; if ((ip = get_and_lock(class_index)) == NULL) - return; + goto err; mx = sk_EX_CALLBACK_num(ip->meth); if (mx > 0) { @@ -319,20 +341,23 @@ void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) } CRYPTO_THREAD_unlock(ex_data_lock); - if (mx > 0 && storage == NULL) { - CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE); - return; - } for (i = 0; i < mx; i++) { - if (storage[i] && storage[i]->free_func) { + if (storage != NULL) + f = storage[i]; + else { + CRYPTO_THREAD_write_lock(ex_data_lock); + f = sk_EX_CALLBACK_value(ip->meth, i); + CRYPTO_THREAD_unlock(ex_data_lock); + } + if (f != NULL && f->free_func != NULL) { ptr = CRYPTO_get_ex_data(ad, i); - storage[i]->free_func(obj, ptr, ad, i, - storage[i]->argl, storage[i]->argp); + f->free_func(obj, ptr, ad, i, f->argl, f->argp); } } if (storage != stack) OPENSSL_free(storage); + err: sk_void_free(ad->sk); ad->sk = NULL; }