Fix for Bad memory_mutex lock on static memory cleanup (was free'ing mutex then trying to use it).

This commit is contained in:
David Garske
2021-10-05 13:46:42 -07:00
parent 0bee624ee5
commit 9f57345614

View File

@ -2391,24 +2391,33 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
FreeDer(&ctx->staticKE.x25519Key); FreeDer(&ctx->staticKE.x25519Key);
#endif #endif
#endif #endif
}
#ifdef WOLFSSL_STATIC_MEMORY #ifdef WOLFSSL_STATIC_MEMORY
if (ctx->heap != NULL) { static void SSL_CtxResourceFreeStaticMem(void* heap)
{
if (heap != NULL
#ifdef WOLFSSL_HEAP_TEST #ifdef WOLFSSL_HEAP_TEST
/* avoid dereferencing a test value */ /* avoid dereferencing a test value */
if (ctx->heap != (void*)WOLFSSL_HEAP_TEST) && heap != (void*)WOLFSSL_HEAP_TEST
#endif #endif
{ ) {
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)(ctx->heap); WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wc_FreeMutex(&((WOLFSSL_HEAP*)(hint->memory))->memory_mutex); WOLFSSL_HEAP* mem = hint->memory;
wc_FreeMutex(&mem->memory_mutex);
} }
} }
#endif /* WOLFSSL_STATIC_MEMORY */ #endif /* WOLFSSL_STATIC_MEMORY */
}
void FreeSSL_Ctx(WOLFSSL_CTX* ctx) void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
{ {
int refCount; int refCount;
void* heap = ctx->heap;
#ifdef WOLFSSL_STATIC_MEMORY
if (ctx->onHeap == 0) {
heap = NULL;
}
#endif
/* decrement CTX reference count */ /* decrement CTX reference count */
if ((refCount = SSL_CTX_RefCount(ctx, -1)) < 0) { if ((refCount = SSL_CTX_RefCount(ctx, -1)) < 0) {
@ -2416,32 +2425,32 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
* CTX was still malloc'd */ * CTX was still malloc'd */
if (ctx->err == CTX_INIT_MUTEX_E) { if (ctx->err == CTX_INIT_MUTEX_E) {
SSL_CtxResourceFree(ctx); SSL_CtxResourceFree(ctx);
XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX); XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
#ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
} }
return; return;
} }
if (refCount == 0) { if (refCount == 0) {
void* heap = ctx->heap;
WOLFSSL_MSG("CTX ref count down to 0, doing full free"); WOLFSSL_MSG("CTX ref count down to 0, doing full free");
SSL_CtxResourceFree(ctx); SSL_CtxResourceFree(ctx);
#if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \ #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
!defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
TicketEncCbCtx_Free(&ctx->ticketKeyCtx); TicketEncCbCtx_Free(&ctx->ticketKeyCtx);
#endif #endif
wc_FreeMutex(&ctx->countMutex); wc_FreeMutex(&ctx->countMutex);
#ifdef WOLFSSL_STATIC_MEMORY
if (ctx->onHeap == 0) {
heap = NULL;
}
#endif
XFREE(ctx, heap, DYNAMIC_TYPE_CTX); XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
(void)heap; /* not used in some builds */ #ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
} }
else { else {
(void)ctx;
WOLFSSL_MSG("CTX ref count not 0 yet, no free"); WOLFSSL_MSG("CTX ref count not 0 yet, no free");
} }
(void)heap; /* not used in some builds */
} }