Skip to content

Commit 85b178b

Browse files
Rely on WC_RNG lock for global RNG and make RAND_cleanup a no-op (#12251)
* Make RAND_cleanup a no-op, free the global RNG in wolfSSL_Cleanup OpenSSL made RAND_cleanup() a no-op in 1.1.0 ("it does nothing and should not be called"), so code written for it may call it at any time. Do the same: the teardown moves to wolfssl_rand_cleanup(), which wolfSSL_Cleanup() calls on the last reference. It runs the RAND method cleanup and frees the global RNG. This lets the global RNG be used without globalRNGMutex: nothing can free it under a running generate. The global RNG now stays allocated until wolfSSL_Cleanup(). A program that uses the RAND API without wolfSSL_Init() and wolfSSL_Cleanup() no longer has a call that frees it. The tests that need a fresh global RNG call wolfssl_rand_cleanup(). * Global RNG: rely on the WC_RNG lock Since 8eacfc6 a WC_RNG has its own lock, held by wc_RNG_GenerateBlock() and the reseed and stir calls. Stop taking globalRNGMutex around those uses of the global RNG. It is still taken to init and free the RNG and to guard gDrbgDefCtx. Builds where the global RNG has no lock of its own (no WC_RNG_HAVE_AUTO_LOCK, e.g. FIPS before v7, or WC_RNG_AUTO_LOCK_DEFAULT_OFF) still need the mutex around each use. So do WOLF_CRYPTO_CB_FIND builds, as the RNG callback runs outside the RNG's own lock, and builds without atomics. That now lives in three helpers, so the callers make one call and hold no lock: - wolfssl_global_rng_generate() fills a buffer, - wolfssl_global_rng_mix() stirs in caller material, - wolfssl_global_rng_reseed() reseeds from the system. The generate and mix helpers init the global RNG when needed, before taking the lock, as wolfSSL_RAND_Init() takes globalRNGMutex. Reseed does not init: RAND_poll fails when the RNG is not initialized. All three check the init flag again under the lock. initGlobalRNG is now atomic so a thread that sees it set also sees the initialized RNG. The pid check that old FIPS and selftest builds do in wolfSSL_RAND_bytes() moves to its own function; those builds always have the mutex. Add a test that uses RAND_bytes, BN_rand, RAND_status and RAND_poll from several threads.
1 parent bd431d2 commit 85b178b

8 files changed

Lines changed: 347 additions & 285 deletions

File tree

‎src/ssl.c‎

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static struct SystemCryptoPolicy crypto_policy;
293293

294294
#define HAVE_GLOBAL_RNG /* consolidate flags for using globalRNG */
295295
static WC_RNG globalRNG;
296-
static volatile int initGlobalRNG = 0;
296+
static wolfSSL_Atomic_Int initGlobalRNG = WOLFSSL_ATOMIC_INITIALIZER(0);
297297

298298
#if defined(OPENSSL_EXTRA) || !defined(WOLFSSL_MUTEX_INITIALIZER)
299299
static WC_MAYBE_UNUSED wolfSSL_Mutex globalRNGMutex
@@ -303,6 +303,149 @@ static WC_MAYBE_UNUSED wolfSSL_Mutex globalRNGMutex
303303
static int globalRNGMutex_valid = 0;
304304
#endif
305305

306+
#ifdef OPENSSL_EXTRA
307+
/* Serialise use of globalRNG. The mutex is not needed when the RNG locks
308+
* itself. A crypto callback found by WOLF_CRYPTO_CB_FIND runs outside the RNG
309+
* lock, so keep the mutex then. globalRNGMutex still guards init and free.
310+
* Only for the helpers below. */
311+
#if defined(WC_RNG_HAVE_AUTO_LOCK) && \
312+
!defined(WC_RNG_AUTO_LOCK_DEFAULT_OFF) && \
313+
!defined(WOLF_CRYPTO_CB_FIND) && !defined(WOLFSSL_NO_ATOMICS)
314+
static int wolfssl_global_rng_lock(void)
315+
{
316+
return 0;
317+
}
318+
319+
static void wolfssl_global_rng_unlock(void)
320+
{
321+
}
322+
#else
323+
static int wolfssl_global_rng_lock(void)
324+
{
325+
return (wc_LockMutex(&globalRNGMutex) == 0) ? 0 : BAD_MUTEX_E;
326+
}
327+
328+
static void wolfssl_global_rng_unlock(void)
329+
{
330+
wc_UnLockMutex(&globalRNGMutex);
331+
}
332+
#endif
333+
334+
/* Init the global RNG if needed. Call without the lock held:
335+
* wolfSSL_RAND_Init() takes globalRNGMutex. Returns 0 on success. */
336+
WC_MAYBE_UNUSED static int wolfssl_global_rng_init(void)
337+
{
338+
if (WOLFSSL_ATOMIC_LOAD(initGlobalRNG) != 0)
339+
return 0;
340+
return (wolfSSL_RAND_Init() == WOLFSSL_SUCCESS) ? 0 : BAD_STATE_E;
341+
}
342+
343+
/* Fill out with sz bytes from the global RNG. Inits it if needed.
344+
* Returns 0 on success. */
345+
WC_MAYBE_UNUSED static int wolfssl_global_rng_generate(byte* out, word32 sz)
346+
{
347+
int ret = wolfssl_global_rng_init();
348+
349+
if (ret == 0)
350+
ret = wolfssl_global_rng_lock();
351+
if (ret != 0)
352+
return ret;
353+
/* Pairs with the store in wolfSSL_RAND_Init(). */
354+
if (WOLFSSL_ATOMIC_LOAD(initGlobalRNG) == 0) {
355+
wolfssl_global_rng_unlock();
356+
return BAD_STATE_E;
357+
}
358+
while ((ret == 0) && (sz > 0)) {
359+
word32 len = min(sz, (word32)RNG_MAX_BLOCK_LEN);
360+
361+
ret = wc_RNG_GenerateBlock(&globalRNG, out, len);
362+
out += len;
363+
sz -= len;
364+
}
365+
wolfssl_global_rng_unlock();
366+
367+
return ret;
368+
}
369+
370+
#ifdef HAVE_HASHDRBG
371+
/* Mix caller material into the global RNG. It gets no entropy credit.
372+
* Inits the RNG if needed. Returns 0 on success. */
373+
WC_MAYBE_UNUSED static int wolfssl_global_rng_mix(const byte* in, word32 sz)
374+
{
375+
int ret = wolfssl_global_rng_init();
376+
377+
if (ret == 0)
378+
ret = wolfssl_global_rng_lock();
379+
if (ret != 0)
380+
return ret;
381+
/* Pairs with the store in wolfSSL_RAND_Init(). */
382+
if (WOLFSSL_ATOMIC_LOAD(initGlobalRNG) == 0) {
383+
wolfssl_global_rng_unlock();
384+
return BAD_STATE_E;
385+
}
386+
#ifdef WC_RNG_HAVE_STIR
387+
/* An uncredited stir: credited user-class reseeds of conformant
388+
* instances are refused with WC_RNG_RBGC_STRATUM_IMMUTABLE. A standing
389+
* reseed or recovery obligation (NEEDS_RECOVERY_E: credited reseed due,
390+
* invalidation latched, or epoch stale; NOT_READY_E: the reseed interval
391+
* tripped within the call) outranks advisory material and is not a
392+
* failure: the next generate performs the credited reseed. */
393+
ret = wc_RNG_DRBG_Stir(&globalRNG, in, sz);
394+
if ((ret == WC_NO_ERR_TRACE(NOT_READY_E)) ||
395+
(ret == WC_NO_ERR_TRACE(NEEDS_RECOVERY_E))) {
396+
ret = 0;
397+
}
398+
#else
399+
ret = wc_RNG_DRBG_Reseed(&globalRNG, in, sz);
400+
#endif
401+
wolfssl_global_rng_unlock();
402+
403+
return ret;
404+
}
405+
406+
/* Reseed the global RNG from the system. Returns 0 on success. */
407+
WC_MAYBE_UNUSED static int wolfssl_global_rng_reseed(void)
408+
{
409+
/* The lock also covers wc_GenerateSeed(), which writes globalRNG.seed. */
410+
int ret = wolfssl_global_rng_lock();
411+
412+
if (ret != 0)
413+
return ret;
414+
/* Pairs with the store in wolfSSL_RAND_Init(). */
415+
if (WOLFSSL_ATOMIC_LOAD(initGlobalRNG) == 0) {
416+
wolfssl_global_rng_unlock();
417+
return BAD_STATE_E;
418+
}
419+
#ifdef WC_RNG_HAVE_RESEED_NOW
420+
ret = wc_RNG_DRBG_Reseed_Now(&globalRNG, NULL, 0);
421+
if (ret != 0) {
422+
WOLFSSL_MSG("Bad wc_RNG_DRBG_Reseed_Now");
423+
}
424+
#else /* random.h predates wc_RNG_DRBG_Reseed_Now() */
425+
{
426+
byte entropy[16];
427+
word32 entropy_sz = 16;
428+
429+
ret = wc_GenerateSeed(&globalRNG.seed, entropy, entropy_sz);
430+
if (ret != 0) {
431+
WOLFSSL_MSG("Bad wc_GenerateSeed");
432+
}
433+
else {
434+
ret = wc_RNG_DRBG_Reseed(&globalRNG, entropy, entropy_sz);
435+
if (ret != 0) {
436+
WOLFSSL_MSG("Error reseeding DRBG");
437+
}
438+
}
439+
ForceZero(entropy, sizeof(entropy));
440+
}
441+
#endif /* WC_RNG_HAVE_RESEED_NOW */
442+
wolfssl_global_rng_unlock();
443+
444+
return ret;
445+
}
446+
#endif /* HAVE_HASHDRBG */
447+
#endif /* OPENSSL_EXTRA */
448+
306449
#if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
307450
static WOLFSSL_DRBG_CTX* gDrbgDefCtx = NULL;
308451
#endif
@@ -311,7 +454,7 @@ WC_RNG* wolfssl_get_global_rng(void)
311454
{
312455
WC_RNG* ret = NULL;
313456

314-
if (initGlobalRNG == 0)
457+
if (WOLFSSL_ATOMIC_LOAD(initGlobalRNG) == 0)
315458
WOLFSSL_MSG("Global RNG no Init");
316459
else
317460
ret = &globalRNG;
@@ -3834,7 +3977,7 @@ int wolfSSL_Cleanup(void)
38343977
#endif
38353978

38363979
#ifdef OPENSSL_EXTRA
3837-
wolfSSL_RAND_Cleanup();
3980+
wolfssl_rand_cleanup();
38383981
#endif
38393982

38403983
if (wolfCrypt_Cleanup() != 0) {

‎src/ssl_bn.c‎

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,6 @@ int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
21822182
{
21832183
int ret = 1;
21842184
word32 len = (word32)((bits + 7) / 8);
2185-
WC_RNG* rng;
21862185

21872186
WOLFSSL_ENTER("wolfSSL_BN_rand");
21882187

@@ -2212,31 +2211,17 @@ int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
22122211
else if (ret == 1) {
22132212
byte* buff = NULL;
22142213

2215-
/* Get random to global random to generate bits. */
2216-
if ((rng = wolfssl_make_global_rng()) == NULL) {
2217-
WOLFSSL_MSG("Failed to use global RNG.");
2218-
ret = 0;
2219-
}
2220-
22212214
/* Allocate buffer to hold generated bits. */
2222-
if ((ret == 1) && ((buff = (byte*)XMALLOC(len, NULL,
2223-
DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
2215+
buff = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2216+
if (buff == NULL) {
22242217
WOLFSSL_MSG("Failed to allocate buffer.");
22252218
ret = 0;
22262219
}
2227-
/* Global RNG is shared, lock it while generating. */
2228-
if ((ret == 1) && (wc_LockMutex(&globalRNGMutex) != 0)) {
2229-
WOLFSSL_MSG("Bad Lock Mutex rng");
2230-
ret = 0;
2231-
}
2232-
22332220
/* Generate bytes to cover bits. */
2234-
if (ret == 1) {
2235-
if (wc_RNG_GenerateBlock(rng, buff, len) != 0) {
2236-
WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
2237-
ret = 0;
2238-
}
2239-
wc_UnLockMutex(&globalRNGMutex);
2221+
if ((ret == 1) &&
2222+
(wolfssl_global_rng_generate(buff, (word32)len) != 0)) {
2223+
WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
2224+
ret = 0;
22402225
}
22412226
/* Read bytes in to big number. */
22422227
if ((ret == 1) && mp_read_unsigned_bin((mp_int*)bn->internal, buff, len)

0 commit comments

Comments
 (0)