From 9f573456148ae3aaecc010bc9753b8f9559d81c8 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 5 Oct 2021 13:46:42 -0700 Subject: [PATCH] Fix for `Bad memory_mutex lock` on static memory cleanup (was free'ing mutex then trying to use it). --- src/internal.c | 51 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/internal.c b/src/internal.c index c033a494f..b533b8300 100644 --- a/src/internal.c +++ b/src/internal.c @@ -2391,24 +2391,33 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx) FreeDer(&ctx->staticKE.x25519Key); #endif #endif -#ifdef WOLFSSL_STATIC_MEMORY - if (ctx->heap != NULL) { -#ifdef WOLFSSL_HEAP_TEST - /* avoid dereferencing a test value */ - if (ctx->heap != (void*)WOLFSSL_HEAP_TEST) -#endif - { - WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)(ctx->heap); - wc_FreeMutex(&((WOLFSSL_HEAP*)(hint->memory))->memory_mutex); - } - } -#endif /* WOLFSSL_STATIC_MEMORY */ } +#ifdef WOLFSSL_STATIC_MEMORY +static void SSL_CtxResourceFreeStaticMem(void* heap) +{ + if (heap != NULL + #ifdef WOLFSSL_HEAP_TEST + /* avoid dereferencing a test value */ + && heap != (void*)WOLFSSL_HEAP_TEST + #endif + ) { + WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap; + WOLFSSL_HEAP* mem = hint->memory; + wc_FreeMutex(&mem->memory_mutex); + } +} +#endif /* WOLFSSL_STATIC_MEMORY */ void FreeSSL_Ctx(WOLFSSL_CTX* ctx) { int refCount; + void* heap = ctx->heap; +#ifdef WOLFSSL_STATIC_MEMORY + if (ctx->onHeap == 0) { + heap = NULL; + } +#endif /* decrement CTX reference count */ if ((refCount = SSL_CTX_RefCount(ctx, -1)) < 0) { @@ -2416,32 +2425,32 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx) * CTX was still malloc'd */ if (ctx->err == CTX_INIT_MUTEX_E) { 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; } if (refCount == 0) { - void* heap = ctx->heap; WOLFSSL_MSG("CTX ref count down to 0, doing full free"); + SSL_CtxResourceFree(ctx); #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \ !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) TicketEncCbCtx_Free(&ctx->ticketKeyCtx); #endif wc_FreeMutex(&ctx->countMutex); -#ifdef WOLFSSL_STATIC_MEMORY - if (ctx->onHeap == 0) { - heap = NULL; - } -#endif XFREE(ctx, heap, DYNAMIC_TYPE_CTX); - (void)heap; /* not used in some builds */ + #ifdef WOLFSSL_STATIC_MEMORY + SSL_CtxResourceFreeStaticMem(heap); + #endif } else { - (void)ctx; WOLFSSL_MSG("CTX ref count not 0 yet, no free"); } + (void)heap; /* not used in some builds */ }