Skip to content

Commit dfbde45

Browse files
committed
global refactor of static mutex initialization to use WOLFSSL_MUTEX_INITIALIZER, and adjustment of WOLFSSL_MUTEX_INITIALIZER macro to take an argument, for Linux kernel compatibility.
1 parent 6500444 commit dfbde45

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 */
@@ -13603,9 +13611,9 @@ int wolfSSL_Cleanup(void)
1360313611
WOLFSSL_ENTER("wolfSSL_Cleanup");
1360413612

1360513613
#ifndef WOLFSSL_MUTEX_INITIALIZER
13606-
if (count_mutex_valid == 1) {
13614+
if (inits_count_mutex_valid == 1) {
1360713615
#endif
13608-
if (wc_LockMutex(&count_mutex) != 0) {
13616+
if (wc_LockMutex(&inits_count_mutex) != 0) {
1360913617
WOLFSSL_MSG("Bad Lock Mutex count");
1361013618
return BAD_MUTEX_E;
1361113619
}
@@ -13620,9 +13628,9 @@ int wolfSSL_Cleanup(void)
1362013628
}
1362113629

1362213630
#ifndef WOLFSSL_MUTEX_INITIALIZER
13623-
if (count_mutex_valid == 1) {
13631+
if (inits_count_mutex_valid == 1) {
1362413632
#endif
13625-
wc_UnLockMutex(&count_mutex);
13633+
wc_UnLockMutex(&inits_count_mutex);
1362613634
#ifndef WOLFSSL_MUTEX_INITIALIZER
1362713635
}
1362813636
#endif
@@ -13666,21 +13674,23 @@ int wolfSSL_Cleanup(void)
1366613674
}
1366713675
}
1366813676
#ifndef NO_CLIENT_CACHE
13677+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1366913678
if ((clisession_mutex_valid == 1) &&
1367013679
(wc_FreeMutex(&clisession_mutex) != 0)) {
1367113680
if (ret == WOLFSSL_SUCCESS)
1367213681
ret = BAD_MUTEX_E;
1367313682
}
1367413683
clisession_mutex_valid = 0;
1367513684
#endif
13685+
#endif
1367613686
#endif /* !NO_SESSION_CACHE */
1367713687

1367813688
#ifndef WOLFSSL_MUTEX_INITIALIZER
13679-
if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex) != 0)) {
13689+
if ((inits_count_mutex_valid == 1) && (wc_FreeMutex(&inits_count_mutex) != 0)) {
1368013690
if (ret == WOLFSSL_SUCCESS)
1368113691
ret = BAD_MUTEX_E;
1368213692
}
13683-
count_mutex_valid = 0;
13693+
inits_count_mutex_valid = 0;
1368413694
#endif
1368513695

1368613696
#ifdef OPENSSL_EXTRA
@@ -13701,11 +13711,13 @@ int wolfSSL_Cleanup(void)
1370113711
#endif
1370213712

1370313713
#ifdef HAVE_GLOBAL_RNG
13714+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1370413715
if ((globalRNGMutex_valid == 1) && (wc_FreeMutex(&globalRNGMutex) != 0)) {
1370513716
if (ret == WOLFSSL_SUCCESS)
1370613717
ret = BAD_MUTEX_E;
1370713718
}
1370813719
globalRNGMutex_valid = 0;
13720+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
1370913721

1371013722
#if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
1371113723
wolfSSL_FIPS_drbg_free(gDrbgDefCtx);
@@ -32932,13 +32944,15 @@ void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
3293232944
#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
3293332945
static int wolfSSL_RAND_InitMutex(void)
3293432946
{
32947+
#ifndef WOLFSSL_MUTEX_INITIALIZER
3293532948
if (gRandMethodsInit == 0) {
3293632949
if (wc_InitMutex(&gRandMethodMutex) != 0) {
3293732950
WOLFSSL_MSG("Bad Init Mutex rand methods");
3293832951
return BAD_MUTEX_E;
3293932952
}
3294032953
gRandMethodsInit = 1;
3294132954
}
32955+
#endif
3294232956
return 0;
3294332957
}
3294432958
#endif
@@ -33310,8 +33324,10 @@ void wolfSSL_RAND_Cleanup(void)
3331033324
wc_UnLockMutex(&gRandMethodMutex);
3331133325
}
3331233326

33327+
#ifndef WOLFSSL_MUTEX_INITIALIZER
3331333328
if (wc_FreeMutex(&gRandMethodMutex) == 0)
3331433329
gRandMethodsInit = 0;
33330+
#endif
3331533331
#endif
3331633332
#ifdef HAVE_GLOBAL_RNG
3331733333
if (wc_LockMutex(&globalRNGMutex) == 0) {

0 commit comments

Comments
 (0)