← ALL ADVISORIES

HIGH BYTERAY-2026-0303Disclosed

wolfSSL — Mismatched Heap Free of Static-Memory Connection State After SNI Context Swap

In static-memory builds, wolfSSL frees a connection's own heap blocks with the heap of the WOLFSSL_CTX attached at teardown, not the heap that allocated them. When an SNI callback moves the connection to a CTX with a different heap through wolfSSL_set_SSL_CTX, static-pool blocks are handed to the wrong allocator.

Advisory
BYTERAY-2026-0303
CWE
CWE-771
CVSS
8.1 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-27
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 (tested at master commit d95483f), builds with WOLFSSL_STATIC_MEMORY (--enable-staticmemory) where an SNI callback moves a connection to a WOLFSSL_CTX that uses a different heap
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

With WOLFSSL_STATIC_MEMORY, a WOLFSSL_CTX can be created over a fixed memory pool. Every connection made from that context takes its WOLFSSL object and a per-connection heap-hint block from the pool. The connection keeps a pointer to that hint in ssl->heap.

wolfSSL_set_SSL_CTX() lets a server change the context of a connection during the handshake, normally from an SNI callback that selects a virtual host. It replaces ssl->ctx and leaves ssl->heap as it was. The teardown code does not use ssl->heap to decide where memory goes back. It uses the heap of whichever context is attached at that moment.

If the two contexts do not share a heap, teardown frees pool memory through the wrong allocator. A context without a pool sends pool-interior addresses to the C library free(). A context with a second pool has the blocks linked into that pool's free list. The client chooses the SNI name, so the client decides whether a connection takes this path. Default builds do not enable static memory and are not affected.

Affected Products and Versions

  • Component: src/internal.c, FreeSSL_StaticMemory(), together with wolfSSL_free() and FreeSSL().
  • Required build: WOLFSSL_STATIC_MEMORY (--enable-staticmemory), with SNI support and wolfSSL_set_SSL_CTX() compiled in.
  • Required configuration: the listening context is created over a static pool, and an SNI callback swaps connections to a context whose heap differs. That is either a context with no heap hint, which is how a context is created by default, or a context over a separate pool.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • A default ./configure build does not include this code.
  • Other releases were not checked.

Technical Details

FreeSSL_StaticMemory() picks the heap for its frees from the current context:

        void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;

It then releases the statistics block and the hint block itself with that value (src/internal.c:9872, src/internal.c:9875):

            XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL);
        ...
        XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL);

In the same function the pool bookkeeping goes through ssl_hint->memory, which is the pool the connection was created from (src/internal.c:9848). The I/O and handshake counters are decremented there (src/internal.c:9854, src/internal.c:9864). The counters and the frees can therefore refer to two different heaps.

The sequence is:

  1. The server creates the connection. The WOLFSSL object comes from the listening context's pool (src/ssl.c:766), and the hint block is set up from the same pool (src/internal.c:8447-8456).
  2. The client sends a ClientHello with a server_name extension. The application's SNI callback runs.
  3. The callback calls wolfSSL_set_SSL_CTX() (src/ssl.c:9029). The function sets ssl->ctx to the new context (src/ssl.c:9079). Its own comment says that everything else in the WOLFSSL object stays untouched (src/ssl.c:9046), and ssl->heap still points into the first pool.
  4. At the end of the connection wolfSSL_free() calls FreeSSL(ssl, ssl->ctx->heap) (src/ssl.c:803). FreeSSL() frees the WOLFSSL object with that argument (src/internal.c:10497).
  5. FreeSSL_StaticMemory() takes its heap from the current context (src/internal.c:9843) and frees the statistics block and the hint block with it.

When the new context has no heap hint, the allocator wrapper falls through to the C library and is given addresses that lie inside the static pool. When the new context has its own pool, the blocks are added to that pool's free list, while the usage counters were decremented on the first pool.

Reachability and Preconditions

  • Build: WOLFSSL_STATIC_MEMORY. Not part of a default build.
  • Runtime: the listening context uses a static pool. The application registers an SNI callback that calls wolfSSL_set_SSL_CTX(). This is the use the function is documented for (src/ssl.c:9037-9039).
  • At least one context that the callback can select has a different heap from the listening context. Nothing in wolfSSL_set_SSL_CTX() rejects or reports such a swap.
  • Attacker position: a remote TLS client. No authentication is needed. The client supplies the server_name value that selects the context.
  • The mismatched free happens when the connection is torn down, once per connection that took the swap.
  • A deployment in which all contexts share one heap configuration is not affected.

Impact

Memory that belongs to a static pool is released through an allocator that does not own it. With a pool-less target context the C library receives pointers it never returned, which corrupts its metadata or aborts the process, and the blocks are lost to the bounded pool. With a second pool as the target, blocks from the first pool enter the second pool's free list, so later allocations from the two pools can overlap. The report rates the main consequence as loss of availability, with corruption of allocator state as a route to further memory corruption.

ByteRay reproduced the pool-less case on a build with --enable-staticmemory --enable-opensslextra under AddressSanitizer. A TLS client that sent a server name selecting the second context caused the server to abort at connection teardown with a bad-free report in FreeSSL_StaticMemory, and the freed address was inside the static pool. A connection without the server name was torn down cleanly.

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 bind every teardown free to the heap the memory came from. ssl->heap already survives the context swap, so wolfSSL_free(), FreeSSL() and FreeSSL_StaticMemory() can use it instead of ssl->ctx->heap. The counter updates and the frees then address the same pool. As a second layer, wolfSSL_set_SSL_CTX() can refuse a swap between contexts whose heap hints differ, and the documentation for SNI context switching can state that all contexts involved must share a heap configuration.

Workaround: create every context that an SNI callback can select over the same static pool as the listening context, so that the heap hint is identical before and after the swap.

Timeline

  • 2026-09-27 — Reported to wolfSSL.
  • 2026-12-03 — Public disclosure.

Credits

Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.

References