wolfSSL — Mismatched Heap Hint on Free of Cached TLS 1.3 Ticket Nonce in Session Cache Overwrite
When a session cache entry is overwritten, wolfSSL frees the old entry's dynamically allocated TLS 1.3 ticket nonce with the heap hint of the connection doing the overwrite, not the one that allocated it. In static-memory builds that mix heap hints over the shared cache, the buffer goes to the wrong allocator.
- Advisory
- BYTERAY-2026-0375
- CWE
- CWE-771
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), builds with WOLFSSL_STATIC_MEMORY, TLS 1.3 session tickets and WOLFSSL_TICKET_NONCE_MALLOC where contexts with different heap hints share the session cache
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
AddSessionToCache() in src/ssl_sess.c supports a dynamically allocated TLS 1.3 ticket nonce. The nonce buffer is allocated with the heap hint of the connection that adds the session, and the pointer is then handed to the cache entry. The cache entry does not keep the hint: its heap field is set to NULL when the session is stored.
When a later connection overwrites that entry, the function takes the stored nonce pointer and frees it with the heap hint of the connection doing the overwrite. A comment in the code says the two heaps should be the same. Nothing enforces it, and the session cache is process-global.
In a static-memory build the hint selects the allocator. If the two connections belong to contexts with different heap hints, the buffer is freed into a different pool's free list, or passed to the system free() although it lies inside a pool. The result is allocator-state corruption. Deployments with a single pool, or with no static pool at all, free with the same hint and are not affected. Static memory is not part of a default build.
Affected Products and Versions
- Component:
src/ssl_sess.c,AddSessionToCache(), the cache overwrite path. - Required build:
WOLFSSL_STATIC_MEMORY,WOLFSSL_TLS13,HAVE_SESSION_TICKETandWOLFSSL_TICKET_NONCE_MALLOC(non-FIPS, or FIPS 5.3 and later), with the internal session cache enabled. - Required deployment: connections whose contexts use different heap hints share the session cache. Two contexts with separate static pools, or one pooled context and one context with a NULL hint.
- Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default build does not enable static memory. Other releases were not checked.
Technical Details
The nonce is allocated with the adding connection's hint (src/ssl_sess.c:1946-1947). The comment above that line states the assumption (src/ssl_sess.c:1943-1945):
/* use the AddSession->heap even if the buffer maybe saved in
* CachedSession objects. CachedSession heap and AddSession heap should
* be the same */
The old nonce is released at the end of the function with the current connection's hint, XFREE(toFree, addSession->heap, DYNAMIC_TYPE_SESSION_TICK) (src/ssl_sess.c:2204). The sequence across two connections:
- Connection A, using pool P1, receives a TLS 1.3 NewSessionTicket with a nonce too long for the static buffer.
AddSessionToCache()allocatespreallocNoncefrom P1 (src/ssl_sess.c:1946-1947). - The cache entry's hint is cleared (
src/ssl_sess.c:2125) and the buffer is handed to the entry throughwolfSSL_DupSessionEx()(src/ssl_sess.c:2130,src/ssl_sess.c:2167-2172). - The entry stays in the process-global session cache (
src/ssl_sess.c:33). - Connection B, using pool P2 or a NULL hint, adds a session with the same ID on the same side and takes the overwrite path (
src/ssl_sess.c:1992-1994). - The stored nonce pointer is moved into
toFree(src/ssl_sess.c:2102-2107). toFreeis freed with B's hint (src/ssl_sess.c:2204). A second free site for the stored copy is atsrc/ssl_sess.c:101.- With a NULL hint,
wolfSSL_Free()calls the systemfree()on a pool-interior block (wolfcrypt/src/memory.c:1240-1261). With P2's hint, it links the block into P2's free list and lowers P2's usage count (wolfcrypt/src/memory.c:1268-1327,wolfcrypt/src/memory.c:1330).
The preallocNonce buffer itself is freed consistently with its own hint, and ticBuff is freed with NULL at the same sites. Only the nonce taken over from the cache entry is freed with a hint that did not allocate it.
Reachability and Preconditions
- Build flags as listed above.
- The session cache is shared by connections with different heap hints. This is deployment configuration, not attacker input.
- A TLS 1.3 peer issues a session ticket with a nonce longer than the static nonce buffer, which forces the dynamic allocation. No authentication beyond the handshake is involved.
- A later connection with a different hint caches a session whose ID collides with the stored entry on the same side.
Impact
The nonce buffer is returned to an allocator that did not issue it. With a NULL hint this corrupts system-heap metadata and removes the block from its bounded pool for good. With another pool's hint it poisons that pool's free list, and later allocations from the two pools can alias. The practical result is a crash or denial of service, with memory aliasing between unrelated allocations once pool memory is reused. The mismatch only occurs in mixed-hint deployments. The collision step makes it conditional rather than deterministic.
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 make the allocating hint travel with the buffer: keep the hint in the cache entry, or in a field next to the nonce pointer, and use it at both free sites. An alternative is to allocate cached nonces with one fixed hint, as the code already does for ticBuff. Workarounds: use one heap hint for every context that shares the session cache, or build without WOLFSSL_TICKET_NONCE_MALLOC.
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
- Nonce allocation in
AddSessionToCacheat the tested commit - Free of the taken-over nonce at the tested commit
- Related heap-hint mismatches in the session code: BYTERAY-2026-0374 (ticket buffer), BYTERAY-2026-0376 (session object)