src/ssl.c: fix races in wolfSSL_Init() and wolfSSL_RAND_bytes().

pull/7473/head
Daniel Pouzzner 2024-04-25 15:49:27 -05:00
parent 460991a6f0
commit 8e8e9bd0eb
1 changed files with 11 additions and 6 deletions

View File

@ -5757,12 +5757,13 @@ int wolfSSL_Init(void)
if (ret == WOLFSSL_SUCCESS) { if (ret == WOLFSSL_SUCCESS) {
initRefCount++; initRefCount++;
} else {
initRefCount = 1; /* Force cleanup */
} }
wc_UnLockMutex(&inits_count_mutex); wc_UnLockMutex(&inits_count_mutex);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
initRefCount = 1; /* Force cleanup */
(void)wolfSSL_Cleanup(); /* Ignore any error from cleanup */ (void)wolfSSL_Cleanup(); /* Ignore any error from cleanup */
} }
@ -23923,18 +23924,22 @@ int wolfSSL_RAND_bytes(unsigned char* buf, int num)
} }
#endif #endif
#ifdef HAVE_GLOBAL_RNG #ifdef HAVE_GLOBAL_RNG
if (initGlobalRNG) { if (wc_LockMutex(&globalRNGMutex) != 0) {
if (wc_LockMutex(&globalRNGMutex) != 0) { WOLFSSL_MSG("Bad Lock Mutex rng");
WOLFSSL_MSG("Bad Lock Mutex rng"); return ret;
return ret; }
}
if (initGlobalRNG) {
rng = &globalRNG; rng = &globalRNG;
used_global = 1; used_global = 1;
} }
else else
#endif #endif
{ {
#ifdef HAVE_GLOBAL_RNG
wc_UnLockMutex(&globalRNGMutex);
#endif
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG); tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
if (tmpRNG == NULL) if (tmpRNG == NULL)