← ALL ADVISORIES

HIGH BYTERAY-2026-0300Patched

wolfSSL — Use After Free in External Session Cache Lookup During TLS 1.3 Stateful Ticket Resumption

GetSesionFromCacheOrExt passed the external session cache get callback a copy flag that started at 0, and wolfSSL released the returned session whenever the flag was still 0. A callback that kept its session and did not write the flag had it freed, and the next TLS 1.3 resumption with the same ticket identity read and released freed memory.

Advisory
BYTERAY-2026-0300
CWE
CWE-416
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 through 5.9.4, TLS 1.3 servers built with HAVE_EXT_CACHE (--enable-opensslextra) and session tickets whose external session cache get callback does not set its copy flag
Fixed in
Master commit 016e6ee311bfc653d097b44ec1a3606f88ec510c (PR 11460, merged 2026-10-13). Not in a wolfSSL stable release as of 2026-12-03.
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

With HAVE_EXT_CACHE a server application can keep TLS sessions in its own store and register a lookup callback with wolfSSL_CTX_sess_set_get_cb(). The callback returns a session and uses an int *copy argument to say who owns it. If the flag is 0 on return, the callback has handed its reference to the library and the library releases it. If the flag is non-zero, the callback keeps the session.

GetSesionFromCacheOrExt() serves the TLS 1.3 stateful ticket lookup. It set the flag to 0 before the call and took no reference of its own. A callback that returned a session from its store and did not write the flag had that session released by FreeSessionFromCacheOrExt() when the lookup was done, while the store still pointed at it. Once the last reference was gone, the next ClientHello with the same 32-byte ticket identity made the callback return a dangling pointer, which wolfSSL read and released again.

A client triggers the lookup by offering a pre-shared key identity, which needs no credentials. Whether the lookup is harmful is decided by the application's callback: one that writes the flag is not affected. Default builds do not compile the external cache.

Affected Products and Versions

  • Product: wolfSSL, GetSesionFromCacheOrExt() and FreeSessionFromCacheOrExt() in src/internal.c.
  • Required build: HAVE_EXT_CACHE, HAVE_SESSION_TICKET and WOLFSSL_TLS13. OPENSSL_EXTRA defines HAVE_EXT_CACHE (wolfssl/wolfcrypt/settings.h:4737-4738). Session tickets are off by default (configure.ac:9469-9473). --enable-opensslall turns them on (wolfssl/wolfcrypt/settings.h:4682-4683), as do the nginx, HAProxy, lighttpd and wpa_supplicant options (configure.ac:9475-9478). --enable-opensslextra alone needs --enable-session-ticket as well.
  • Required application code: a get callback that returns a session the application still holds and leaves *copy unwritten.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The v5.9.2-stable and v5.9.4-stable tags contain the same code.
  • Fixed on master by 016e6ee311bfc653d097b44ec1a3606f88ec510c (PR 11460, merged 2026-10-13). The v5.9.4-stable tag does not contain the fix.
  • A default ./configure build does not include this code. Earlier releases were not checked.

Technical Details

The lookup calls the external callback first (src/internal.c:44063-44072):

            int copy = 0;
            sess = ssl->ctx->get_sess_cb((WOLFSSL*)ssl,
                    id, ID_LEN, &copy);
            if (sess != NULL) {
                freeCtx->extCache = 1;
                /* If copy not set then free immediately */
                if (!copy)
                    freeCtx->freeSess = 1;
            }

FreeSessionFromCacheOrExt() later calls wolfSSL_FreeSession() on that session when freeSess is set (src/internal.c:44093-44097). No reference is taken in between. wolfSSL_FreeSession() decrements the reference count and frees the object at zero (src/ssl_sess.c:4296-4305).

The meaning of an explicit value is the same as in OpenSSL, whose interface this is: 0 gives the reference to the library, non-zero keeps it. wolfSSL's own test callback sets the flag to 1 to keep its object (tests/api/test_session.c:386-388). The defect is the starting value. OpenSSL starts the flag at 1, so a callback that never writes it keeps its session. wolfSSL started it at 0 and treated the same callback as giving the session away.

The path from the network is:

  1. src/tls13.c:6801 — DoPreSharedKeys() calls DoClientTicket_ex() for a pre-shared key identity from the ClientHello.
  2. src/internal.c:44116-44121 — a 32-byte identity on a TLS 1.3 connection is treated as a stateful ticket and passed to GetSesionFromCacheOrExt().
  3. src/internal.c:44065-44066 — the callback receives the identity and a flag that holds 0.
  4. src/internal.c:44070-44071 — the flag is still 0, so freeSess is set.
  5. src/internal.c:44147 — PopulateInternalTicketFromSession() reads the session.
  6. src/tls13.c:6885-6887 or src/internal.c:44155 — FreeSessionFromCacheOrExt() drops a reference that the library never took.

When that was the last reference, the application's store holds a pointer to freed memory. On the next lookup of the same identity the callback returns it, step 5 reads it (src/internal.c:43989) and step 6 releases it a second time. DoClientTicket() (src/internal.c:44242) and the DTLS 1.3 stateless path (src/dtls.c:590) reach the same lookup.

Reachability and Preconditions

  • Build: HAVE_EXT_CACHE, session tickets and TLS 1.3, as listed above.
  • Runtime: the server registers a get callback with wolfSSL_CTX_sess_set_get_cb() or the compatibility name SSL_CTX_sess_set_get_cb, and issues stateful TLS 1.3 tickets, whose identity is a 32-byte session ID.
  • Application code: the callback returns a pointer that its store keeps, and does not write *copy.
  • Not affected: a callback that sets *copy to 1 and keeps its session, and a callback that sets *copy to 0 and returns a reference it no longer uses.
  • Outside this advisory: a callback that sets *copy to 0 and goes on using the pointer. That gives away a reference the application still needs. It has the same result with OpenSSL, and the upstream fix does not change it.
  • Attacker position: any client that can connect. It needs a ticket identity that the application's store holds, which it gets from its own earlier full handshake. No user interaction. Ordinary clients cause the same sequence: in an affected application every stateful ticket resumption drops a reference on the stored session.

Impact

In an affected application, resumption lookups release references that belong to the application's store. After the last one the store holds a dangling pointer, and the next lookup of that identity reads the freed WOLFSSL_SESSION, builds the connection's ticket state from it and releases it again. What a client can make of that beyond a crash depends on the allocator and on what reuses the freed block, and was not demonstrated.

ByteRay ran a wolfSSL TLS 1.3 server with an external cache callback and stateful tickets under AddressSanitizer, with a wolfSSL client that completed a full handshake and then presented the same ticket identity again. The sanitizer reported a heap use-after-free read in PopulateInternalTicketFromSession(), called from DoClientTicket_ex(), on a session that FreeSessionFromCacheOrExt() had freed during an earlier resumption. The test callback wrote 0 to the flag and kept its pointer. At the tested commit a callback that leaves the flag unwritten takes the same branch, because the flag starts at 0. The same test with a callback that set the flag to 1 ran without a report.

Remediation and Mitigation

Use a master build that contains 016e6ee311bfc653d097b44ec1a3606f88ec510c (PR 11460). The commit starts the flag at 1 in GetSesionFromCacheOrExt(). When the callback leaves the flag set, wolfSSL takes its own reference with wolfSSL_SESSION_up_ref(), and a session it cannot reference is treated as a miss. FreeSessionFromCacheOrExt() then releases exactly the reference the lookup holds: its own, or the one the callback handed over by setting the flag to 0. The fix is on master. The v5.9.4-stable tag does not contain it.

On 5.9.2 and 5.9.4 the workaround is in the application: write the flag in every get callback. Set it to 0 only when the callback returns a reference of its own, for example a fresh copy or one taken with wolfSSL_SESSION_up_ref(). Set it to 1 when the store keeps the session.

Timeline

  • 2026-09-15 — wolfSSL opened PR 11460, "session: initialize the external cache copy flag to 1". This was before ByteRay's report.
  • 2026-09-27 — Reported to wolfSSL.
  • 2026-10-13 — PR 11460 merged to master.
  • 2026-12-03 — Public disclosure.

Credits

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

wolfSSL had opened the pull request that fixes this before the report was sent. That pull request does not reference ByteRay.

References