wolfSSL — Mismatched Free of Session Object on Mutex Initialization Failure in wolfSSL_NewSession
wolfSSL_NewSession allocates a session from the caller's heap hint, zeroes it, and on a reference-count mutex failure frees it using the zeroed heap field. In static-memory builds the pool block is passed to the system free(), or to a different pool, instead of the pool it came from.
- Advisory
- BYTERAY-2026-0376
- CWE
- CWE-762
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), multithreaded builds with WOLFSSL_STATIC_MEMORY and no atomic reference counting (WOLFSSL_REFCNT_ERROR_RETURN defined)
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
wolfSSL_NewSession() in src/ssl_sess.c allocates a WOLFSSL_SESSION with the heap hint its caller passes. In a static-memory build with a pool configured, that is a block inside the application's pool. The function then zeroes the whole structure, which also clears the field that records the heap.
If initialization of the session's reference-count mutex fails, the error path frees the block with ret->heap. That field is still NULL at that point. The hint the function was called with is stored only later.
The free therefore goes to a different allocator from the allocation. Without a global heap hint the pool block is handed to the system free(). With a global hint it is inserted into that pool's free list. The block is lost from its own pool either way. The trigger is an operating-system failure to create a mutex. It is not driven by peer input. Builds without WOLFSSL_STATIC_MEMORY allocate and free through the same allocator. Single-threaded builds and builds with atomic reference counting do not compile the error branch. Static memory is not part of a default build.
Affected Products and Versions
- Component:
src/ssl_sess.c,wolfSSL_NewSession(). - Required build:
WOLFSSL_STATIC_MEMORY, multithreaded, without atomic operations, so thatWOLFSSL_REFCNT_ERROR_RETURNis defined (wolfssl/wolfcrypt/wc_port.h:967). At run time the context has a static pool, so the session is created with a non-NULL heap hint. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default build does not enable static memory. When the heap hint is NULL, allocation and free both use the system heap and match. Other releases were not checked.
Technical Details
The session is allocated with the caller's heap argument (src/ssl_sess.c:3892-3893). The order of what follows is the defect (src/ssl_sess.c:3896-3903):
XMEMSET(ret, 0, sizeof(WOLFSSL_SESSION));
wolfSSL_RefInit(&ret->ref, &err);
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
if (err != 0) {
WOLFSSL_MSG("Error setting up session reference mutex");
XFREE(ret, ret->heap, DYNAMIC_TYPE_SESSION);
return NULL;
}
ret->heap = heap; follows at src/ssl_sess.c:3911, after the error branch. On builds where WOLFSSL_REFCNT_ERROR_RETURN is not defined the branch is not compiled (src/ssl_sess.c:3904-3905). The path, step by step:
InitSSL_StaticMemory()gives the connection a pool hint (src/internal.c:8447-8456).InitSSL()then callswolfSSL_NewSession(ssl->heap)(src/internal.c:9034).XMALLOCwith that hint returns a block inside the pool (src/ssl_sess.c:3892-3893). Static-memory allocation with a hint has no fallback to the system heap (wolfcrypt/src/memory.c:1086-1156).XMEMSETclears the structure, soret->heapis NULL (src/ssl_sess.c:3896).wolfSSL_RefInit()creates a mutex on these builds.wc_InitMutex()fails (wolfcrypt/src/wc_port.c:2539-2548).- The error branch calls
XFREE(ret, ret->heap, ...)with a NULL hint (src/ssl_sess.c:3901). wolfSSL_Free()selects the allocator from the hint. With a NULL hint and no global hint it calls the systemfree()(wolfcrypt/src/memory.c:1240-1261). With a global hint set it inserts the block into that pool's free list (wolfcrypt/src/memory.c:1273-1327).
Reachability and Preconditions
- Build flags as listed above, and a context configured with a static pool through
wolfSSL_CTX_SetStaticMemory. wc_InitMutex()fails during session creation. On an RTOS this happens when the system runs out of mutex objects or memory.- Callers that reach the function:
wolfSSL_new()throughInitSSL(),wolfSSL_clear()(src/ssl.c:5785),wolfSSL_DupSession()(src/ssl_sess.c:4277) andwolfSSL_new_session()(src/ssl_sess.c:3939). - No attacker-supplied data takes part. A remote peer can at most add general resource pressure by keeping connections open.
Impact
Each time the branch runs, one fixed-size block is lost from a bounded pool, and a pointer the system heap never issued is passed to free(). That corrupts system-heap metadata or aborts the process. With a global heap hint configured, the block lands on the wrong pool's free list instead. The practical result is a crash or denial of service of the device's TLS stack, under resource exhaustion that already exists.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
ByteRay has not confirmed a fixed wolfSSL release for this issue as of 2026-12-03, and has not checked it against wolfSSL changes made after the tested commit.
The fix direction is to free with the same hint as the allocation: pass the function's heap parameter in the error branch, or store ret->heap = heap directly after the XMEMSET, before any step that can fail. Workaround: a build that provides atomic operations for reference counting does not compile the failing branch.
Timeline
- 2026-09-28 — Reported to wolfSSL.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.
References
wolfSSL_NewSessionat the tested commit- Related heap-hint mismatches in the session code: BYTERAY-2026-0374 (ticket buffer), BYTERAY-2026-0375 (cached ticket nonce)