Skip to content

Commit cf733c3

Browse files
authored
Merge pull request #7284 from douzzer/20240229-mutex-initializer-global-refactor
20240229-mutex-initializer-global-refactor
2 parents 4a167e9 + dfbde45 commit cf733c3

18 files changed

Lines changed: 392 additions & 116 deletions

File tree

linuxkm/linuxkm_wc_port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@
800800
*/
801801
#include <linux/mutex.h>
802802
typedef struct mutex wolfSSL_Mutex;
803+
#define WOLFSSL_MUTEX_INITIALIZER(lockname) __MUTEX_INITIALIZER(lockname)
803804

804805
/* prevent gcc's mm_malloc.h from being included, since it unconditionally
805806
* includes stdlib.h, which is kernel-incompatible.

src/sniffer.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ typedef struct SnifferServer {
447447
struct SnifferServer* next; /* for list */
448448
} SnifferServer;
449449

450-
451450
/* Session Flags */
452451
typedef struct Flags {
453452
byte side; /* which end is current packet headed */
@@ -569,13 +568,13 @@ typedef struct SnifferSession {
569568
/* Sniffer Server List and mutex */
570569
static THREAD_LS_T WOLFSSL_GLOBAL SnifferServer* ServerList = NULL;
571570
#ifndef HAVE_C___ATOMIC
572-
static WOLFSSL_GLOBAL wolfSSL_Mutex ServerListMutex;
571+
static WOLFSSL_GLOBAL wolfSSL_Mutex ServerListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ServerListMutex);
573572
#endif
574573

575574
/* Session Hash Table, mutex, and count */
576575
static THREAD_LS_T WOLFSSL_GLOBAL SnifferSession* SessionTable[HASH_SIZE];
577576
#ifndef HAVE_C___ATOMIC
578-
static WOLFSSL_GLOBAL wolfSSL_Mutex SessionMutex;
577+
static WOLFSSL_GLOBAL wolfSSL_Mutex SessionMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(SessionMutex);
579578
#endif
580579
static THREAD_LS_T WOLFSSL_GLOBAL int SessionCount = 0;
581580

@@ -584,7 +583,7 @@ static WOLFSSL_GLOBAL int MaxRecoveryMemory = -1;
584583
/* per session max recovery memory */
585584
#ifndef WOLFSSL_SNIFFER_NO_RECOVERY
586585
/* Recovery of missed data switches and stats */
587-
static WOLFSSL_GLOBAL wolfSSL_Mutex RecoveryMutex; /* for stats */
586+
static WOLFSSL_GLOBAL wolfSSL_Mutex RecoveryMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(RecoveryMutex); /* for stats */
588587
/* # of sessions with missed data */
589588
static WOLFSSL_GLOBAL word32 MissedDataSessions = 0;
590589
#endif
@@ -596,7 +595,7 @@ static WOLFSSL_GLOBAL void* ConnectionCbCtx = NULL;
596595
#ifdef WOLFSSL_SNIFFER_STATS
597596
/* Sessions Statistics */
598597
static WOLFSSL_GLOBAL SSLStats SnifferStats;
599-
static WOLFSSL_GLOBAL wolfSSL_Mutex StatsMutex;
598+
static WOLFSSL_GLOBAL wolfSSL_Mutex StatsMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(StatsMutex);
600599
#endif
601600

602601
#ifdef WOLFSSL_SNIFFER_KEY_CALLBACK
@@ -683,6 +682,7 @@ static int addKeyLogSnifferServerHelper(const char* address,
683682
void ssl_InitSniffer_ex(int devId)
684683
{
685684
wolfSSL_Init();
685+
#ifndef WOLFSSL_MUTEX_INITIALIZER
686686
#ifndef HAVE_C___ATOMIC
687687
wc_InitMutex(&ServerListMutex);
688688
wc_InitMutex(&SessionMutex);
@@ -694,6 +694,11 @@ void ssl_InitSniffer_ex(int devId)
694694
XMEMSET(&SnifferStats, 0, sizeof(SSLStats));
695695
wc_InitMutex(&StatsMutex);
696696
#endif
697+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
698+
699+
#ifdef WOLFSSL_SNIFFER_STATS
700+
XMEMSET(&SnifferStats, 0, sizeof(SSLStats));
701+
#endif
697702
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_ASYNC_CRYPT)
698703
CryptoDeviceId = devId;
699704
#endif
@@ -903,13 +908,15 @@ void ssl_FreeSniffer(void)
903908
#endif /* WOLFSSL_SNIFFER_KEYLOGFILE */
904909

905910

911+
#ifndef WOLFSSL_MUTEX_INITIALIZER
906912
#ifndef WOLFSSL_SNIFFER_NO_RECOVERY
907913
wc_FreeMutex(&RecoveryMutex);
908914
#endif
909915
#ifndef HAVE_C___ATOMIC
910916
wc_FreeMutex(&SessionMutex);
911917
wc_FreeMutex(&ServerListMutex);
912918
#endif
919+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
913920

914921
#ifdef WOLF_CRYPTO_CB
915922
#ifdef HAVE_INTEL_QA_SYNC
@@ -7235,7 +7242,7 @@ static THREAD_LS_T WOLFSSL_GLOBAL
72357242
SecretNode*
72367243
secretHashTable[WOLFSSL_SNIFFER_KEYLOGFILE_HASH_TABLE_SIZE] = {NULL};
72377244
#ifndef HAVE_C___ATOMIC
7238-
static WOLFSSL_GLOBAL wolfSSL_Mutex secretListMutex;
7245+
static WOLFSSL_GLOBAL wolfSSL_Mutex secretListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(secretListMutex);
72397246
#endif
72407247

72417248
static unsigned int secretHashFunction(unsigned char* clientRandom);

src/ssl.c

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ int wc_OBJ_sn2nid(const char *sn)
309309
static WC_RNG globalRNG;
310310
static int initGlobalRNG = 0;
311311

312-
static wolfSSL_Mutex globalRNGMutex;
312+
static WC_MAYBE_UNUSED wolfSSL_Mutex globalRNGMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(globalRNGMutex);
313+
#ifndef WOLFSSL_MUTEX_INITIALIZER
313314
static int globalRNGMutex_valid = 0;
315+
#endif
314316

315317
#if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
316318
static WOLFSSL_DRBG_CTX* gDrbgDefCtx = NULL;
@@ -406,8 +408,10 @@ WC_RNG* wolfssl_make_rng(WC_RNG* rng, int* local)
406408
* OPENSSL_EXTRA where RAND callbacks are not used */
407409
#ifndef WOLFSSL_NO_OPENSSL_RAND_CB
408410
static const WOLFSSL_RAND_METHOD* gRandMethods = NULL;
411+
static wolfSSL_Mutex gRandMethodMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(gRandMethodMutex);
412+
#ifndef WOLFSSL_MUTEX_INITIALIZER
409413
static int gRandMethodsInit = 0;
410-
static wolfSSL_Mutex gRandMethodMutex;
414+
#endif
411415
#endif /* !WOLFSSL_NO_OPENSSL_RAND_CB */
412416
#endif /* OPENSSL_EXTRA */
413417

@@ -1265,11 +1269,9 @@ int wolfSSL_send_session(WOLFSSL* ssl)
12651269

12661270
/* prevent multiple mutex initializations */
12671271
static volatile WOLFSSL_GLOBAL int initRefCount = 0;
1268-
#ifdef WOLFSSL_MUTEX_INITIALIZER
1269-
static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex = WOLFSSL_MUTEX_INITIALIZER;
1270-
#else
1271-
static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex; /* init ref count mutex */
1272-
static WOLFSSL_GLOBAL int count_mutex_valid = 0;
1272+
static WOLFSSL_GLOBAL wolfSSL_Mutex inits_count_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(inits_count_mutex); /* init ref count mutex */
1273+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1274+
static WOLFSSL_GLOBAL int inits_count_mutex_valid = 0;
12731275
#endif
12741276

12751277
/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
@@ -6164,8 +6166,10 @@ int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
61646166
/* Client Cache */
61656167
/* uses session mutex */
61666168

6167-
static WOLFSSL_GLOBAL wolfSSL_Mutex clisession_mutex; /* ClientCache mutex */
6169+
static WOLFSSL_GLOBAL wolfSSL_Mutex clisession_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(clisession_mutex); /* ClientCache mutex */
6170+
#ifndef WOLFSSL_MUTEX_INITIALIZER
61686171
static WOLFSSL_GLOBAL int clisession_mutex_valid = 0;
6172+
#endif
61696173
#endif /* !NO_CLIENT_CACHE */
61706174

61716175
void EvictSessionFromCache(WOLFSSL_SESSION* session)
@@ -6223,22 +6227,40 @@ int wolfSSL_Init(void)
62236227

62246228
WOLFSSL_ENTER("wolfSSL_Init");
62256229

6230+
#ifndef WOLFSSL_MUTEX_INITIALIZER
6231+
if (inits_count_mutex_valid == 0) {
6232+
if (wc_InitMutex(&inits_count_mutex) != 0) {
6233+
WOLFSSL_MSG("Bad Init Mutex count");
6234+
return BAD_MUTEX_E;
6235+
}
6236+
else {
6237+
inits_count_mutex_valid = 1;
6238+
}
6239+
}
6240+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
6241+
6242+
if (wc_LockMutex(&inits_count_mutex) != 0) {
6243+
WOLFSSL_MSG("Bad Lock Mutex count");
6244+
return BAD_MUTEX_E;
6245+
}
6246+
6247+
if ((ret == WOLFSSL_SUCCESS) && (initRefCount == 0)) {
6248+
/* Initialize crypto for use with TLS connection */
6249+
62266250
#if FIPS_VERSION_GE(5,1)
62276251
ret = wolfCrypt_SetPrivateKeyReadEnable_fips(1, WC_KEYTYPE_ALL);
6228-
if (ret != 0)
6229-
return ret;
6230-
else
6252+
if (ret == 0)
62316253
ret = WOLFSSL_SUCCESS;
62326254
#endif
62336255

6234-
if (initRefCount == 0) {
6235-
/* Initialize crypto for use with TLS connection */
6236-
if (wolfCrypt_Init() != 0) {
6237-
WOLFSSL_MSG("Bad wolfCrypt Init");
6238-
ret = WC_INIT_E;
6256+
if (ret == WOLFSSL_SUCCESS) {
6257+
if (wolfCrypt_Init() != 0) {
6258+
WOLFSSL_MSG("Bad wolfCrypt Init");
6259+
ret = WC_INIT_E;
6260+
}
62396261
}
62406262

6241-
#ifdef HAVE_GLOBAL_RNG
6263+
#if defined(HAVE_GLOBAL_RNG) && !defined(WOLFSSL_MUTEX_INITIALIZER)
62426264
if (ret == WOLFSSL_SUCCESS) {
62436265
if (wc_InitMutex(&globalRNGMutex) != 0) {
62446266
WOLFSSL_MSG("Bad Init Mutex rng");
@@ -6293,6 +6315,7 @@ int wolfSSL_Init(void)
62936315
}
62946316
#endif
62956317
#ifndef NO_CLIENT_CACHE
6318+
#ifndef WOLFSSL_MUTEX_INITIALIZER
62966319
if (ret == WOLFSSL_SUCCESS) {
62976320
if (wc_InitMutex(&clisession_mutex) != 0) {
62986321
WOLFSSL_MSG("Bad Init Mutex session");
@@ -6302,19 +6325,9 @@ int wolfSSL_Init(void)
63026325
clisession_mutex_valid = 1;
63036326
}
63046327
}
6328+
#endif
63056329
#endif
63066330
#endif
6307-
#ifndef WOLFSSL_MUTEX_INITIALIZER
6308-
if (ret == WOLFSSL_SUCCESS) {
6309-
if (wc_InitMutex(&count_mutex) != 0) {
6310-
WOLFSSL_MSG("Bad Init Mutex count");
6311-
ret = BAD_MUTEX_E;
6312-
}
6313-
else {
6314-
count_mutex_valid = 1;
6315-
}
6316-
}
6317-
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
63186331
#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
63196332
/* OpenSSL registers cleanup using atexit */
63206333
if ((ret == WOLFSSL_SUCCESS) && (atexit(AtExitCleanup) != 0)) {
@@ -6325,16 +6338,11 @@ int wolfSSL_Init(void)
63256338
}
63266339

63276340
if (ret == WOLFSSL_SUCCESS) {
6328-
if (wc_LockMutex(&count_mutex) != 0) {
6329-
WOLFSSL_MSG("Bad Lock Mutex count");
6330-
ret = BAD_MUTEX_E;
6331-
}
6332-
else {
6333-
initRefCount++;
6334-
wc_UnLockMutex(&count_mutex);
6335-
}
6341+
initRefCount++;
63366342
}
63376343

6344+
wc_UnLockMutex(&inits_count_mutex);
6345+
63386346
if (ret != WOLFSSL_SUCCESS) {
63396347
initRefCount = 1; /* Force cleanup */
63406348
(void)wolfSSL_Cleanup(); /* Ignore any error from cleanup */
@@ -13620,9 +13628,9 @@ int wolfSSL_Cleanup(void)
1362013628
WOLFSSL_ENTER("wolfSSL_Cleanup");
1362113629

1362213630
#ifndef WOLFSSL_MUTEX_INITIALIZER
13623-
if (count_mutex_valid == 1) {
13631+
if (inits_count_mutex_valid == 1) {
1362413632
#endif
13625-
if (wc_LockMutex(&count_mutex) != 0) {
13633+
if (wc_LockMutex(&inits_count_mutex) != 0) {
1362613634
WOLFSSL_MSG("Bad Lock Mutex count");
1362713635
return BAD_MUTEX_E;
1362813636
}
@@ -13637,9 +13645,9 @@ int wolfSSL_Cleanup(void)
1363713645
}
1363813646

1363913647
#ifndef WOLFSSL_MUTEX_INITIALIZER
13640-
if (count_mutex_valid == 1) {
13648+
if (inits_count_mutex_valid == 1) {
1364113649
#endif
13642-
wc_UnLockMutex(&count_mutex);
13650+
wc_UnLockMutex(&inits_count_mutex);
1364313651
#ifndef WOLFSSL_MUTEX_INITIALIZER
1364413652
}
1364513653
#endif
@@ -13683,21 +13691,23 @@ int wolfSSL_Cleanup(void)
1368313691
}
1368413692
}
1368513693
#ifndef NO_CLIENT_CACHE
13694+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1368613695
if ((clisession_mutex_valid == 1) &&
1368713696
(wc_FreeMutex(&clisession_mutex) != 0)) {
1368813697
if (ret == WOLFSSL_SUCCESS)
1368913698
ret = BAD_MUTEX_E;
1369013699
}
1369113700
clisession_mutex_valid = 0;
1369213701
#endif
13702+
#endif
1369313703
#endif /* !NO_SESSION_CACHE */
1369413704

1369513705
#ifndef WOLFSSL_MUTEX_INITIALIZER
13696-
if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex) != 0)) {
13706+
if ((inits_count_mutex_valid == 1) && (wc_FreeMutex(&inits_count_mutex) != 0)) {
1369713707
if (ret == WOLFSSL_SUCCESS)
1369813708
ret = BAD_MUTEX_E;
1369913709
}
13700-
count_mutex_valid = 0;
13710+
inits_count_mutex_valid = 0;
1370113711
#endif
1370213712

1370313713
#ifdef OPENSSL_EXTRA
@@ -13718,11 +13728,13 @@ int wolfSSL_Cleanup(void)
1371813728
#endif
1371913729

1372013730
#ifdef HAVE_GLOBAL_RNG
13731+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1372113732
if ((globalRNGMutex_valid == 1) && (wc_FreeMutex(&globalRNGMutex) != 0)) {
1372213733
if (ret == WOLFSSL_SUCCESS)
1372313734
ret = BAD_MUTEX_E;
1372413735
}
1372513736
globalRNGMutex_valid = 0;
13737+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
1372613738

1372713739
#if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
1372813740
wolfSSL_FIPS_drbg_free(gDrbgDefCtx);
@@ -32949,13 +32961,15 @@ void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
3294932961
#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
3295032962
static int wolfSSL_RAND_InitMutex(void)
3295132963
{
32964+
#ifndef WOLFSSL_MUTEX_INITIALIZER
3295232965
if (gRandMethodsInit == 0) {
3295332966
if (wc_InitMutex(&gRandMethodMutex) != 0) {
3295432967
WOLFSSL_MSG("Bad Init Mutex rand methods");
3295532968
return BAD_MUTEX_E;
3295632969
}
3295732970
gRandMethodsInit = 1;
3295832971
}
32972+
#endif
3295932973
return 0;
3296032974
}
3296132975
#endif
@@ -33327,8 +33341,10 @@ void wolfSSL_RAND_Cleanup(void)
3332733341
wc_UnLockMutex(&gRandMethodMutex);
3332833342
}
3332933343

33344+
#ifndef WOLFSSL_MUTEX_INITIALIZER
3333033345
if (wc_FreeMutex(&gRandMethodMutex) == 0)
3333133346
gRandMethodsInit = 0;
33347+
#endif
3333233348
#endif
3333333349
#ifdef HAVE_GLOBAL_RNG
3333433350
if (wc_LockMutex(&globalRNGMutex) == 0) {

0 commit comments

Comments
 (0)