Skip to content

Commit 64e4796

Browse files
committed
wolfssl/wolfcrypt/wc_port.h: add definition for WOLFSSL_MUTEX_INITIALIZER, currently only #ifdef WOLFSSL_PTHREADS.
src/ssl.c: refactor dynamics of count_mutex, count_mutex_valid, and initRefCount, to be intrinsically race-free on pthreads builds, and to be always race-free for callers that call wolfSSL_Init() first, then wait for return before any other wolfSSL calls, and call wolfSSL_Cleanup() at most as many times as wolfSSL_Init(). also, in AddSessionToClientCache(), move final access to ClientCache inside the lock-protected span, to mollify Coverity.
1 parent 16c6bd6 commit 64e4796

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/ssl.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,12 @@ int wolfSSL_send_session(WOLFSSL* ssl)
12651265

12661266
/* prevent multiple mutex initializations */
12671267
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
12681271
static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex; /* init ref count mutex */
12691272
static WOLFSSL_GLOBAL int count_mutex_valid = 0;
1273+
#endif
12701274

12711275
/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
12721276
WOLFSSL_METHOD pointer passed in is given to ctx to manage.
@@ -6258,6 +6262,7 @@ int wolfSSL_Init(void)
62586262
}
62596263
#endif
62606264
#endif
6265+
#ifndef WOLFSSL_MUTEX_INITIALIZER
62616266
if (ret == WOLFSSL_SUCCESS) {
62626267
if (wc_InitMutex(&count_mutex) != 0) {
62636268
WOLFSSL_MSG("Bad Init Mutex count");
@@ -6267,6 +6272,7 @@ int wolfSSL_Init(void)
62676272
count_mutex_valid = 1;
62686273
}
62696274
}
6275+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
62706276
#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
62716277
/* OpenSSL registers cleanup using atexit */
62726278
if ((ret == WOLFSSL_SUCCESS) && (atexit(AtExitCleanup) != 0)) {
@@ -13378,21 +13384,30 @@ int wolfSSL_Cleanup(void)
1337813384

1337913385
WOLFSSL_ENTER("wolfSSL_Cleanup");
1338013386

13381-
if (initRefCount == 0)
13382-
return ret; /* possibly no init yet, but not failure either way */
13383-
13384-
if ((count_mutex_valid == 1) && (wc_LockMutex(&count_mutex) != 0)) {
13385-
WOLFSSL_MSG("Bad Lock Mutex count");
13386-
ret = BAD_MUTEX_E;
13387+
#ifndef WOLFSSL_MUTEX_INITIALIZER
13388+
if (count_mutex_valid == 1) {
13389+
#endif
13390+
if (wc_LockMutex(&count_mutex) != 0) {
13391+
WOLFSSL_MSG("Bad Lock Mutex count");
13392+
return BAD_MUTEX_E;
13393+
}
13394+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1338713395
}
13396+
#endif
1338813397

13389-
release = initRefCount-- == 1;
13390-
if (initRefCount < 0)
13391-
initRefCount = 0;
13398+
if (initRefCount > 0) {
13399+
--initRefCount;
13400+
if (initRefCount == 0)
13401+
release = 1;
13402+
}
1339213403

13404+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1339313405
if (count_mutex_valid == 1) {
13406+
#endif
1339413407
wc_UnLockMutex(&count_mutex);
13408+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1339513409
}
13410+
#endif
1339613411

1339713412
if (!release)
1339813413
return ret;
@@ -13442,11 +13457,13 @@ int wolfSSL_Cleanup(void)
1344213457
#endif
1344313458
#endif /* !NO_SESSION_CACHE */
1344413459

13460+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1344513461
if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex) != 0)) {
1344613462
if (ret == WOLFSSL_SUCCESS)
1344713463
ret = BAD_MUTEX_E;
1344813464
}
1344913465
count_mutex_valid = 0;
13466+
#endif
1345013467

1345113468
#ifdef OPENSSL_EXTRA
1345213469
wolfSSL_RAND_Cleanup();
@@ -14339,6 +14356,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1433914356
{
1434014357
int error = -1;
1434114358
word32 clientRow = 0, clientIdx = 0;
14359+
ClientSession* ret = NULL;
14360+
1434214361
(void)useTicket;
1434314362
if (side == WOLFSSL_CLIENT_END
1434414363
&& row != INVALID_SESSION_ROW
@@ -14392,6 +14411,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1439214411
ClientCache[clientRow].nextIdx %= CLIENT_SESSIONS_PER_ROW;
1439314412
}
1439414413

14414+
ret = &ClientCache[clientRow].Clients[clientIdx];
14415+
1439514416
wc_UnLockMutex(&clisession_mutex);
1439614417
}
1439714418
else {
@@ -14402,10 +14423,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1440214423
else {
1440314424
WOLFSSL_MSG("Skipping client cache");
1440414425
}
14405-
if (error == 0)
14406-
return &ClientCache[clientRow].Clients[clientIdx];
14407-
else
14408-
return NULL;
14426+
14427+
return ret;
1440914428
}
1441014429
#endif /* !NO_CLIENT_CACHE */
1441114430

wolfssl/wolfcrypt/wc_port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@
307307
#if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
308308
typedef wolfSSL_Mutex wolfSSL_RwLock;
309309
#endif
310+
#ifdef WOLFSSL_PTHREADS
311+
#define WOLFSSL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
312+
#endif
310313

311314
#ifndef WOLFSSL_NO_ATOMICS
312315
#ifdef HAVE_C___ATOMIC

0 commit comments

Comments
 (0)