wolfSSL — Use After Free from an Uninitialized Copy Flag in the DTLS External Session Cache Lookup
TlsSessionIdIsValid looks up the session ID of a DTLS ClientHello through the external session cache get callback with a copy flag that has no initial value, and releases the returned session when the flag reads zero without holding a reference. A session the application still keeps can be freed, and a repeated ClientHello then reads and frees it again.
- Advisory
- BYTERAY-2026-0301
- 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 (tested at master commit d95483f), DTLS servers built with HAVE_EXT_CACHE (--enable-opensslextra) and WOLFSSL_DTLS_NO_HVR_ON_RESUME that register an external session cache get callback
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
With HAVE_EXT_CACHE a server application can keep 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. A DTLS server calls TlsSessionIdIsValid() while it handles a ClientHello statelessly, to decide whether the session ID in it can be resumed. That function calls the callback with the session ID taken from the datagram.
The function declares its copy flag without an initial value and takes no reference on the session the callback returns. When the flag reads zero after the call, it releases the session with wolfSSL_FreeSession(). For a callback that does not write the flag, the decision is made by whatever the stack slot held. If the session is one the application's store still keeps, the store is left with a dangling pointer. A second ClientHello with the same session ID makes the callback return it, and wolfSSL reads it and frees it again.
The lookup runs on an unauthenticated ClientHello, before the handshake proper. Whether it is harmful depends on the application's callback. Default builds do not compile the external cache or this DTLS path.
Affected Products and Versions
- Component: wolfSSL,
TlsSessionIdIsValid()insrc/dtls.c, the stateless DTLS resumption check. - Required build:
HAVE_EXT_CACHE, whichOPENSSL_EXTRAdefines (wolfssl/wolfcrypt/settings.h:4737-4738), and DTLS withWOLFSSL_DTLS_NO_HVR_ON_RESUME. That is--enable-dtlsand--enable-opensslextrawith-DWOLFSSL_DTLS_NO_HVR_ON_RESUME. The function is also compiled when onlyWOLFSSL_DTLS13is defined (src/dtls.c:536), but its one caller is underWOLFSSL_DTLS_NO_HVR_ON_RESUME(src/dtls.c:1080-1083). - Required application code: a registered get callback that returns a session its store keeps.
- Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not include this code. - Other releases were not checked.
Technical Details
The flag is declared at src/dtls.c:470 as int copy;, with no initial value. The callback is called with its address, and the result is used and released (src/dtls.c:482-493):
WOLFSSL_SESSION* extSess =
ssl->ctx->get_sess_cb((WOLFSSL*)ssl, sessionID.elements, ID_LEN,
©);
if (extSess != NULL) {
/* ... */
if (!IsAtLeastTLSv1_3(extSess->version))
*resume = TRUE;
if (!copy)
wolfSSL_FreeSession(ssl->ctx, extSess);
Two things are missing. The flag has no defined value unless the callback writes it; the two other functions that call this callback give it one (src/internal.c:44064, src/ssl_sess.c:1297). And no reference is taken between the call and the release, so wolfSSL_FreeSession() drops a reference the library never held. It decrements the count and frees the object at zero (src/ssl_sess.c:4296-4305).
An explicit 0 from the callback means that it hands its reference to the library, as in the two sibling functions; BYTERAY-2026-0299 describes that contract. A callback that keeps its session and writes 0 has given away a reference it still needs. A callback that keeps its session and writes nothing depends here on uninitialized stack memory.
The path from the network is:
- A DTLS ClientHello for DTLS 1.2 or earlier arrives with a 32-byte session ID. DTLS 1.3 ClientHellos do not take this path.
DoClientHello()hands a ClientHello on a connection that is not yet stateful toDoClientHelloStateless()(src/internal.c:41841).DoClientHelloStateless()callsTlsResumptionIsValid()(src/dtls.c:1083), which callsTlsSessionIdIsValid()(src/dtls.c:533).- The callback receives the session ID from the datagram (
src/dtls.c:482-483). - The returned session's
versionfield is read (src/dtls.c:489). - The flag reads zero and the session is released (
src/dtls.c:492-493).
When step 6 removes the last reference, the store's entry is dangling. The next ClientHello with the same session ID repeats steps 4 to 6 on freed memory: a read at src/dtls.c:489 and a second free at src/dtls.c:493.
Reachability and Preconditions
- Build:
HAVE_EXT_CACHEand DTLS withWOLFSSL_DTLS_NO_HVR_ON_RESUME. - Runtime: the server registers a get callback, and the session cache is not turned off for the connection.
- Application code: the callback returns a pointer its store keeps, and the flag reads zero afterwards, because the callback left it unwritten and the stack slot held zero, or because it wrote 0.
- 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. - Attacker position: anyone who can send UDP datagrams to the server. The ClientHello needs no cookie and no credentials. It must carry a session ID that the application's store holds, which a client gets from its own earlier handshake.
- The attacker sends the same session ID at least twice.
Impact
A remote sender can make the server free a session that the application's store still references, then read the freed WOLFSSL_SESSION and free it a second time. This is memory corruption in the server process at a time the sender chooses. With a callback that does not write the flag, whether the first free happens depends on stack contents and can differ between builds and calls. What can be made of it beyond a crash depends on the allocator and was not assessed.
ByteRay ran a wolfSSL DTLS server built with WOLFSSL_DTLS_NO_HVR_ON_RESUME under AddressSanitizer and sent it ClientHello datagrams without a cookie. The second datagram with a session ID from the application's store produced a heap use-after-free read in TlsSessionIdIsValid() at src/dtls.c:489, on a session the same function had freed at src/dtls.c:493 while handling the first.
The test callback wrote 0 to the flag and kept its pointer, which takes the release branch on every call. ClientHellos with an unknown session ID or an empty one ran without a report.
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. BYTERAY-2026-0299 and BYTERAY-2026-0300 cover the other two call sites of the same callback and give their status.
The fix direction is to give the flag a defined starting value in TlsSessionIdIsValid(), to take a reference with wolfSSL_SESSION_up_ref() before using a session the callback keeps and release only what the lookup holds, and to state the meaning of the flag at the public declaration of wolfSSL_CTX_sess_set_get_cb(). The three call sites should behave the same way.
Workaround 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(). Builds without WOLFSSL_DTLS_NO_HVR_ON_RESUME do not call this function.
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
- TlsSessionIdIsValid at the tested commit
- The call from DoClientHelloStateless
- Related advisories for the other two call sites of the same callback: BYTERAY-2026-0299, BYTERAY-2026-0300