← ALL ADVISORIES

MEDIUM BYTERAY-2026-0374Disclosed

wolfSSL — Mismatched Free of Session Ticket Buffer in Session Cache Insertion Error Path

When a session with an oversized ticket is added to the session cache, wolfSSL allocates the temporary ticket buffer with a NULL heap hint but one error path frees it with the session's static-pool hint. In static-memory builds the system-heap block is linked into the pool's free list.

Advisory
BYTERAY-2026-0374
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), clients built with WOLFSSL_STATIC_MEMORY, TLS 1.3 session tickets and WOLFSSL_TICKET_NONCE_MALLOC, with the session cache enabled
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

AddSessionToCache() in src/ssl_sess.c stores a session in the session cache. When the session ticket is larger than the fixed buffer inside the session record, the function copies the ticket into a temporary buffer, ticBuff. It allocates that buffer with a NULL heap hint. In a static-memory build that request goes to the system heap, not to the session's memory pool.

One error path does not keep the pairing. If the next allocation, the one for the TLS 1.3 ticket nonce, fails, the cleanup frees ticBuff with the session's heap hint. With a non-NULL hint the static-memory free routine treats the pointer as a pool block. It reads a block header from the bytes in front of the pointer, links the block into the pool's free list and adjusts the pool's usage counter.

A system-heap block then sits on the pool's free list, and the system heap's own bookkeeping for that block has been overwritten. A TLS 1.3 server chooses the ticket and nonce lengths that force both allocations. The failing branch also needs the client's pool to be exhausted at that moment. Builds without WOLFSSL_STATIC_MEMORY are not affected, and static memory is not part of a default build.

Affected Products and Versions

  • Component: src/ssl_sess.c, AddSessionToCache().
  • Required build: WOLFSSL_STATIC_MEMORY, WOLFSSL_TLS13, HAVE_SESSION_TICKET and WOLFSSL_TICKET_NONCE_MALLOC (non-FIPS, or FIPS 5.3 and later), with the internal session cache enabled. At run time the session carries a non-NULL heap hint, which is the case when the context was given a static pool.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • A default build does not enable static memory. With a NULL session heap hint the same line is a matched free. Other releases were not checked.

Technical Details

The allocation and the mismatched free are a few lines apart (src/ssl_sess.c:1934-1951):

        ticBuff = (byte*)XMALLOC((size_t)ticLen, NULL,
                DYNAMIC_TYPE_SESSION_TICK);
        ...
        preallocNonce = (byte*)XMALLOC(addSession->ticketNonce.len,
            addSession->heap, DYNAMIC_TYPE_SESSION_TICK);
        if (preallocNonce == NULL) {
            XFREE(ticBuff, addSession->heap, DYNAMIC_TYPE_SESSION_TICK);
            return MEMORY_E;
        }

Every other free of ticBuff in the function passes NULL (src/ssl_sess.c:1963, src/ssl_sess.c:1975, src/ssl_sess.c:2009, src/ssl_sess.c:2032, src/ssl_sess.c:2199). The free at src/ssl_sess.c:1949 is the only one that passes addSession->heap. It matches the free of preallocNonce next to it, which does come from the pool.

How the path is reached:

  1. A TLS 1.3 server sends a NewSessionTicket. The client reads the nonce length (src/tls13.c:14488) and the ticket length and stores the ticket on the session (src/tls13.c:14506, src/tls13.c:14515).
  2. On WOLFSSL_TICKET_NONCE_MALLOC builds the nonce length cap at src/tls13.c:14491-14495 does not apply. A nonce longer than the static buffer is allocated from the session's heap hint (src/tls13.c:14419-14420).
  3. AddSession() (src/tls13.c:14566) calls AddSessionToCache(). The ticket is longer than SESSION_TICKET_LEN, so ticBuff is allocated with a NULL hint (src/ssl_sess.c:1934-1935).
  4. The nonce is dynamic, so preallocNonce is requested from the pool (src/ssl_sess.c:1946-1947). The pool is exhausted and the request fails. The error branch frees ticBuff with the pool hint (src/ssl_sess.c:1949).
  5. With a non-NULL hint, wolfSSL_Free() takes the pool path (wolfcrypt/src/memory.c:1240). It derives a wc_Memory header from the bytes before the pointer (wolfcrypt/src/memory.c:1286), links the block into the pool's free list (wolfcrypt/src/memory.c:1311-1314) and subtracts the header's size field from the pool's usage count (wolfcrypt/src/memory.c:1330).

Reachability and Preconditions

  • Build flags as listed above. The session's heap hint is non-NULL.
  • The affected side is a wolfSSL client. The attacker controls, or is, the TLS 1.3 server the client connects to.
  • The server sends a ticket longer than SESSION_TICKET_LEN and a nonce longer than MAX_TICKET_NONCE_STATIC_SZ.
  • The client's static pool cannot satisfy the nonce allocation at that moment. A server can add pressure with connection churn but cannot make the failure deterministic. wolfSSL_DupSession() also reaches this function.

Impact

When the branch runs, a block owned by the system heap is placed on the static pool's free list, and pool bookkeeping is written into the area in front of that block. A later pool allocation can return memory the system heap owns. The realistic outcome is a crash or denial of service of the client, with memory corruption bounded to the affected allocators. The trigger depends on pool exhaustion at a specific point, so it is timing and environment dependent.

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 ticBuff at src/ssl_sess.c:1949 with a NULL hint, matching its allocation and the other frees of the same buffer. Keeping the allocation and every release of ticBuff in one helper would fix the hint in one place. Workarounds: build without WOLFSSL_TICKET_NONCE_MALLOC, which removes the failing nonce allocation, or disable the client session cache. Both give up functionality.

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