wolfSSL — Use After Free in External Session Cache Lookup During Session ID Resumption
wolfSSL_GetSessionFromCache passed the external session cache get callback a copy flag that started at 0 and released the returned session whenever the flag was still 0. A callback that kept its session and did not write the flag had the session freed under it, and the next resumption with the same session ID read and released freed memory.
- Advisory
- BYTERAY-2026-0299
- 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, servers built with HAVE_EXT_CACHE (--enable-opensslextra) 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.
wolfSSL_GetSessionFromCache() set the flag to 0 before the call. OpenSSL, whose interface this is, sets it to 1, and its manual states that a callback which does not write the flag keeps its session. A callback written that way had its session released by wolfSSL after the lookup, while the application's store still pointed at it. Once the last reference was gone, the next ClientHello carrying the same session ID made the callback return a dangling pointer. wolfSSL read it, copied from it and released it again.
A client triggers the lookup by asking to resume a session, 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, external session cache lookup in
wolfSSL_GetSessionFromCache()(src/ssl_sess.c). - Required build:
HAVE_EXT_CACHE.OPENSSL_EXTRAdefines it (wolfssl/wolfcrypt/settings.h:4737-4738), so--enable-opensslextraand--enable-opensslallbuilds have it. The wpa_supplicant, memcached, nginx, HAProxy and lighttpd configurations define it as well (configure.ac:3436,configure.ac:10242,configure.ac:10251). - Required application code: a get callback that returns a session the application still holds and leaves
*copyunwritten. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same code: the flag starts at 0 and the session is released when it is still 0. - Fixed on master by
016e6ee311bfc653d097b44ec1a3606f88ec510c(PR 11460, merged 2026-10-13). No release contains the fix as of 2026-12-03. - A default
./configurebuild does not include this code. - Earlier releases were not checked.
Technical Details
The external lookup runs before the internal cache is consulted:
if (ssl->ctx->get_sess_cb != NULL) {
int copy = 0;
int found = 0;
WOLFSSL_SESSION* extSess;
/* Attempt to retrieve the session from the external cache. */
WOLFSSL_MSG("Calling external session cache");
extSess = ssl->ctx->get_sess_cb(ssl, (byte*)id, ID_LEN, ©);
On a match the session is duplicated into the output session. Then, whether or not it matched:
/* If copy not set then free immediately */
if (extSess != NULL && !copy)
wolfSSL_FreeSession(ssl->ctx, extSess);
No reference is taken between the call and the release. wolfSSL_FreeSession() decrements the reference count and frees the object when it reaches zero (src/ssl_sess.c:4296-4305).
The meaning of an explicit value is the same as in OpenSSL: 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 difference was the starting value. A callback that never writes the flag is treated by OpenSSL as keeping the session and was treated by wolfSSL as giving it away. The tested tree had no documentation entry for wolfSSL_CTX_sess_set_get_cb() that said so.
The path from the network is:
src/internal.c:42125—DoClientHello()setsresumingwhen the ClientHello carries a 32-byte session ID.src/internal.c:42490—DoClientHello()callsHandleTlsResumption().src/internal.c:41665—HandleTlsResumption()callswolfSSL_GetSession()unless the client resumed with a session ticket.src/ssl_sess.c:1532—wolfSSL_GetSession()callswolfSSL_GetSessionFromCache().src/ssl_sess.c:1302— the callback receives the session ID from the ClientHello and a flag that holds 0.src/ssl_sess.c:1309— a matching session is copied intossl->session.src/ssl_sess.c:1322-1323— the flag is still 0, so the callback's session loses one reference that the library never took.
When that was the last reference, the application's store now holds a pointer to freed memory. On the next lookup of the same ID the callback returns it. CheckSessionMatch() reads it (src/ssl_sess.c:1304), wolfSSL_DupSession() copies from it (src/ssl_sess.c:1309), and wolfSSL_FreeSession() is called on it a second time (src/ssl_sess.c:1323).
wolfSSL_GetSession() is also called from the old-format ClientHello path (src/internal.c:41536) and from ticket processing when WOLFSSL_TICKET_HAVE_ID is defined (src/internal.c:43892).
Reachability and Preconditions
- Build:
HAVE_EXT_CACHE, normally through--enable-opensslextra. - Runtime: the server registers a get callback with
wolfSSL_CTX_sess_set_get_cb()or the compatibility nameSSL_CTX_sess_set_get_cb(wolfssl/openssl/ssl.h:1322), and the session cache is not turned off for the connection (src/ssl_sess.c:1268-1269). - Application code: the callback returns a pointer that its store keeps, and does not write
*copy. - Not affected: a callback that sets
*copyto 1 and keeps its session, and a callback that sets*copyto 0 and returns a reference it no longer uses, such as an object deserialised for that lookup. - Outside this advisory: a callback that sets
*copyto 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. - Protocol: session ID resumption in TLS 1.2 and earlier. The lookup is skipped when the client resumed with a session ticket (
src/ssl_sess.c:1275-1276). - Attacker position: any client that can connect. It needs a session ID that the application's store holds, which it gets from its own earlier full handshake. No credentials are needed unless the server requires client certificates.
- The same sequence happens with ordinary clients. In an affected application every resumption drops a reference on the stored session.
- No user interaction.
Impact
From the source: 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 session ID reads the freed WOLFSSL_SESSION, copies its contents into the connection's session and releases it again.
Not verified: what a client can make of that beyond a crash. The outcome depends on the allocator and on what reuses the freed block. A second free of a block that is in use again, and a resumption built from stale or unrelated memory, are possible consequences that ByteRay did not demonstrate.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Use a master build that contains 016e6ee311bfc653d097b44ec1a3606f88ec510c (PR 11460). The commit starts the flag at 1 in wolfSSL_GetSessionFromCache() and in the two other functions that call the get callback. A callback that leaves the flag alone now keeps its session and wolfSSL does not release it. A callback that sets the flag to 0 still hands its reference to wolfSSL. The commit also adds a documentation entry for wolfSSL_CTX_sess_set_get_cb() that states both cases.
The pull request carries a porting note for the opposite case: a callback that returned a newly allocated session, did not write the flag and relied on wolfSSL to free it will leak after the change, and should set *copy = 0.
The fix is on master only. No release contains it as of 2026-12-03.
On 5.9.2 and 5.9.4 the workaround is in the application: write the flag in every get callback. Set it to 1 when the store keeps the session. 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(). This has no functional cost and behaves the same before and after the fix.
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
- wolfSSL_GetSessionFromCache, the external cache lookup at the tested commit
- wolfSSL_FreeSession, the reference drop
- PR 11460, session: initialize the external cache copy flag to 1
- Fix commit 016e6ee311
- CWE-416: Use After Free
- Related advisories for the other two call sites of the same callback: BYTERAY-2026-0300, BYTERAY-2026-0301