Skip to content

Commit 985ccea

Browse files
anhuJacobBarthelmeh
authored andcommitted
Fix session cache restore dangling pointer (ZD 21423)
Reinitialize pointer fields in WOLFSSL_SESSION after raw XMEMCPY or XFREAD in wolfSSL_memrestore_session_cache and wolfSSL_restore_session_cache. After restore, ticket is reset to staticTicket, ticketLenAlloc to 0, and peer to NULL.
1 parent c563f39 commit 985ccea

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/ssl_sess.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,22 @@ int wolfSSL_memrestore_session_cache(const void* mem, int sz)
522522
#endif
523523

524524
XMEMCPY(&SessionCache[i], row++, SIZEOF_SESSION_ROW);
525+
#ifndef SESSION_CACHE_DYNAMIC_MEM
526+
/* Reset pointers to safe values after raw copy */
527+
{
528+
int j;
529+
for (j = 0; j < SESSIONS_PER_ROW; j++) {
530+
WOLFSSL_SESSION* s = &SessionCache[i].Sessions[j];
531+
#ifdef HAVE_SESSION_TICKET
532+
s->ticket = s->staticTicket;
533+
s->ticketLenAlloc = 0;
534+
#endif
535+
#if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
536+
s->peer = NULL;
537+
#endif
538+
}
539+
}
540+
#endif
525541
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
526542
SESSION_ROW_UNLOCK(&SessionCache[i]);
527543
#endif
@@ -681,6 +697,22 @@ int wolfSSL_restore_session_cache(const char *fname)
681697
#endif
682698

683699
ret = (int)XFREAD(&SessionCache[i], SIZEOF_SESSION_ROW, 1, file);
700+
#ifndef SESSION_CACHE_DYNAMIC_MEM
701+
/* Reset pointers to safe values after raw copy */
702+
{
703+
int j;
704+
for (j = 0; j < SESSIONS_PER_ROW; j++) {
705+
WOLFSSL_SESSION* s = &SessionCache[i].Sessions[j];
706+
#ifdef HAVE_SESSION_TICKET
707+
s->ticket = s->staticTicket;
708+
s->ticketLenAlloc = 0;
709+
#endif
710+
#if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
711+
s->peer = NULL;
712+
#endif
713+
}
714+
}
715+
#endif
684716
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
685717
SESSION_ROW_UNLOCK(&SessionCache[i]);
686718
#endif

0 commit comments

Comments
 (0)