Skip to content

Commit 033016e

Browse files
committed
Wipe session ticket and nonce copies before freeing
Several paths released session ticket and ticket nonce buffers without wiping them: GetSessionFromCache, the AddSessionToCache overwrite path, wolfSSL_DupSessionEx, wolfSSL_d2i_SSL_SESSION, SessionTicketNoncePopulate and the TLS ticket create and decrypt paths. Wipe each buffer before it is freed or replaced. In wolfSSL_DupSessionEx the old ticket is wiped before XREALLOC, since its contents are overwritten after the call.
1 parent d47321b commit 033016e

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/internal.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46280,6 +46280,10 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
4628046280
* async re-entry the size is the same and the buffer is kept. */
4628146281
if (ticketSz <= sizeof(ssl->session->staticTicket)) {
4628246282
if (ssl->session->ticket != ssl->session->staticTicket) {
46283+
if (ssl->session->ticket != NULL) {
46284+
ForceZero(ssl->session->ticket,
46285+
ssl->session->ticketLenAlloc);
46286+
}
4628346287
XFREE(ssl->session->ticket, ssl->session->heap,
4628446288
DYNAMIC_TYPE_SESSION_TICK);
4628546289
ssl->session->ticket = ssl->session->staticTicket;
@@ -46294,6 +46298,7 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
4629446298
return MEMORY_E;
4629546299
}
4629646300
if (ssl->session->ticketLenAlloc > 0) {
46301+
ForceZero(ssl->session->ticket, ssl->session->ticketLenAlloc);
4629746302
XFREE(ssl->session->ticket, ssl->session->heap,
4629846303
DYNAMIC_TYPE_SESSION_TICK);
4629946304
}
@@ -46899,6 +46904,10 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
4689946904
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
4690046905
if (ssl->session->ticketNonce.data
4690146906
!= ssl->session->ticketNonce.dataStatic) {
46907+
if (ssl->session->ticketNonce.data != NULL) {
46908+
ForceZero(ssl->session->ticketNonce.data,
46909+
ssl->session->ticketNonce.len);
46910+
}
4690246911
XFREE(ssl->session->ticketNonce.data, ssl->session->heap,
4690346912
DYNAMIC_TYPE_SESSION_TICK);
4690446913
ssl->session->ticketNonce.data =

‎src/ssl_sess.c‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,8 +1399,10 @@ static int GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output,
13991399
return WOLFSSL_FAILURE;
14001400
}
14011401
#endif
1402-
if (output->ticketLenAlloc)
1402+
if (output->ticketLenAlloc) {
1403+
ForceZero(output->ticket, output->ticketLenAlloc);
14031404
XFREE(output->ticket, output->heap, DYNAMIC_TYPE_SESSION_TICK);
1405+
}
14041406
/* cppcheck-suppress autoVariables */
14051407
output->ticket = tmpTicket;
14061408
output->ticketLenAlloc = PREALLOC_SESSION_TICKET_LEN;
@@ -1420,6 +1422,9 @@ static int GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output,
14201422
defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
14211423
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
14221424
if (output->ticketNonce.data != output->ticketNonce.dataStatic) {
1425+
if (output->ticketNonce.data != NULL) {
1426+
ForceZero(output->ticketNonce.data, output->ticketNonce.len);
1427+
}
14231428
XFREE(output->ticketNonce.data, output->heap,
14241429
DYNAMIC_TYPE_SESSION_TICK);
14251430
output->ticketNonce.data = output->ticketNonce.dataStatic;
@@ -1533,6 +1538,9 @@ static int GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output,
15331538
XMEMCPY(output->ticket, tmpTicket, output->ticketLen);
15341539
}
15351540
}
1541+
if (tmpBufSet && WC_VAR_OK(tmpTicket)) {
1542+
ForceZero(tmpTicket, PREALLOC_SESSION_TICKET_LEN);
1543+
}
15361544
WC_FREE_VAR_EX(tmpTicket, output->heap, DYNAMIC_TYPE_TMP_BUFFER);
15371545

15381546
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
@@ -1546,6 +1554,7 @@ static int GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output,
15461554
if (output->ticketNonce.data != NULL)
15471555
XMEMCPY(output->ticketNonce.data, preallocNonce,
15481556
preallocNonceLen);
1557+
ForceZero(preallocNonce, preallocNonceLen);
15491558
XFREE(preallocNonce, output->heap, DYNAMIC_TYPE_SESSION_TICK);
15501559
preallocNonce = NULL;
15511560
#else
@@ -1569,6 +1578,9 @@ static int GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output,
15691578
preallocNonce = NULL;
15701579
}
15711580
}
1581+
if (preallocNonce != NULL) {
1582+
ForceZero(preallocNonce, preallocNonceLen);
1583+
}
15721584
XFREE(preallocNonce, output->heap, DYNAMIC_TYPE_SESSION_TICK);
15731585
#endif /* WOLFSSL_TLS13 && WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3)*/
15741586

@@ -2180,8 +2192,12 @@ int AddSessionToCache(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* addSession,
21802192
* OS will most likely not even be allocated to the process. */
21812193
if (ticBuff != NULL && cacheSession->ticketLenAlloc < ticLen) {
21822194
/* Save pointer only if separately allocated */
2183-
if (cacheSession->ticket != cacheSession->staticTicket)
2195+
if (cacheSession->ticket != cacheSession->staticTicket) {
2196+
if (cacheSession->ticket != NULL) {
2197+
ForceZero(cacheSession->ticket, cacheSession->ticketLenAlloc);
2198+
}
21842199
cacheTicBuff = cacheSession->ticket;
2200+
}
21852201
ticBuffUsed = 1;
21862202
cacheSession->ticket = ticBuff;
21872203
cacheSession->ticketLenAlloc = (word16) ticLen;
@@ -2194,6 +2210,8 @@ int AddSessionToCache(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* addSession,
21942210

21952211
if (cacheSession->ticketNonce.data !=
21962212
cacheSession->ticketNonce.dataStatic) {
2213+
ForceZero(cacheSession->ticketNonce.data,
2214+
cacheSession->ticketNonce.len);
21972215
toFree = cacheSession->ticketNonce.data;
21982216
cacheSession->ticketNonce.data = cacheSession->ticketNonce.dataStatic;
21992217
cacheSession->ticketNonce.len = 0;
@@ -2293,6 +2311,9 @@ int AddSessionToCache(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* addSession,
22932311
XFREE(cacheTicBuff, NULL, DYNAMIC_TYPE_SESSION_TICK);
22942312
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
22952313
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
2314+
if (preallocNonce != NULL) {
2315+
ForceZero(preallocNonce, preallocNonceLen);
2316+
}
22962317
XFREE(preallocNonce, NULL, DYNAMIC_TYPE_SESSION_TICK);
22972318
XFREE(toFree, NULL, DYNAMIC_TYPE_SESSION_TICK);
22982319
#endif /* WOLFSSL_TLS13 && WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3)*/
@@ -3383,6 +3404,7 @@ WOLFSSL_SESSION* wolfSSL_d2i_SSL_SESSION(WOLFSSL_SESSION** sess,
33833404

33843405
/* Dispose of ol dynamic ticket and ensure space for new ticket. */
33853406
if (s->ticketLenAlloc > 0) {
3407+
ForceZero(s->ticket, s->ticketLenAlloc);
33863408
XFREE(s->ticket, NULL, DYNAMIC_TYPE_SESSION_TICK);
33873409
}
33883410
if (s->ticketLen <= sizeof(s->staticTicket))
@@ -4294,6 +4316,9 @@ static int wolfSSL_DupSessionEx(const WOLFSSL_SESSION* input,
42944316
return WOLFSSL_FAILURE;
42954317
}
42964318

4319+
if (output->ticketNonce.data != NULL) {
4320+
ForceZero(output->ticketNonce.data, output->ticketNonce.len);
4321+
}
42974322
XFREE(output->ticketNonce.data,
42984323
output->heap, DYNAMIC_TYPE_SESSION_TICK);
42994324
output->ticketNonce.data = output->ticketNonce.dataStatic;
@@ -4349,6 +4374,10 @@ static int wolfSSL_DupSessionEx(const WOLFSSL_SESSION* input,
43494374
ret = WOLFSSL_FAILURE;
43504375
}
43514376
else {
4377+
/* Old ticket is replaced below, wipe it before realloc. */
4378+
if (ticBuff != NULL) {
4379+
ForceZero(ticBuff, ticLenAlloc);
4380+
}
43524381
#ifdef WOLFSSL_NO_REALLOC
43534382
tmp = (byte*)XMALLOC(input->ticketLen,
43544383
output->heap, DYNAMIC_TYPE_SESSION_TICK);
@@ -4406,6 +4435,9 @@ static int wolfSSL_DupSessionEx(const WOLFSSL_SESSION* input,
44064435
}
44074436
}
44084437
else {
4438+
if (ticBuff != NULL) {
4439+
ForceZero(ticBuff, ticLenAlloc);
4440+
}
44094441
XFREE(ticBuff, output->heap, DYNAMIC_TYPE_SESSION_TICK);
44104442
output->ticket = output->staticTicket;
44114443
output->ticketLenAlloc = 0;

‎src/tls13.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14849,6 +14849,9 @@ int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce,
1484914849
{
1485014850
if (session->ticketNonce.data
1485114851
!= session->ticketNonce.dataStatic) {
14852+
if (session->ticketNonce.data != NULL) {
14853+
ForceZero(session->ticketNonce.data, session->ticketNonce.len);
14854+
}
1485214855
XFREE(session->ticketNonce.data, session->heap,
1485314856
DYNAMIC_TYPE_SESSION_TICK);
1485414857
session->ticketNonce.data = session->ticketNonce.dataStatic;

0 commit comments

Comments
 (0)