← ALL ADVISORIES

LOW BYTERAY-2026-0473Disclosed

wolfSSL — Race Condition Between Session Cache Overwrite and Unlocked Session Accessors

Several public session accessors return pointers into a live session cache entry after releasing the cache locks. A handshake on another thread that stores a session with the same ID overwrites that entry in place, so the reader can see torn session data or, with session certificates enabled, a wrong or freed peer certificate pointer.

Advisory
BYTERAY-2026-0473
CWE
CWE-362
CVSS
3.7 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-28
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 (tested at master commit d95483f), multi-threaded applications using the OpenSSL-compatibility session accessors (OPENSSL_EXTRA and related builds); the peer-certificate case also needs SESSION_CERTS
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfSSL keeps resumable sessions in a shared session cache protected by row locks. ClientSessionToSession() in src/ssl_sess.c resolves an application-held client session handle to the entry inside the cache. It releases the row lock and the client-session mutex and then returns a pointer to that entry.

The library keeps writing to the entry. When a new session is stored with a session ID that is already cached, AddSessionToCache() overwrites the existing entry in place. A server that uses stateful resumption causes this on every reconnect.

If one thread reads a session through the public accessors while another completes a handshake that stores the same session ID, the two race. The reader can see half-overwritten session data, which usually makes resumption fail. In builds with SESSION_CERTS and OPENSSL_EXTRA, wolfSSL_SESSION_get0_peer() also writes to the shared entry without a lock, and a peer certificate pointer the application holds can be freed by the overwrite. The master secret is not reachable through this path. A default build does not compile these accessors.

Affected Products and Versions

  • Component: src/ssl_sess.c: ClientSessionToSession(), wolfSSL_SESSION_get0_peer(), wolfSSL_SESSION_get_peer_chain(), wolfSSL_SESSION_get_id(), wolfSSL_SESSION_get0_id_context().
  • Torn reads: any build that compiles the ID accessors, which is OPENSSL_ALL, OPENSSL_EXTRA, HAVE_STUNNEL, WOLFSSL_NGINX or WOLFSSL_HAPROXY.
  • Unlocked peer write and freed peer pointer: SESSION_CERTS together with OPENSSL_EXTRA.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • A default ./configure build defines none of these options.
  • Other releases were not checked.

Technical Details

ClientSessionToSession() drops both locks immediately before it returns the cache entry (src/ssl_sess.c:1866-1870):

            SESSION_ROW_UNLOCK(sessRow);
        }
        wc_UnLockMutex(&clisession_mutex);
        return (WOLFSSL_SESSION*)session;

The writer side is AddSessionToCache(). It takes the row write lock (src/ssl_sess.c:1973) and, when the session ID and side match an existing entry, marks it for overwrite with the message "Session already exists. Overwriting." (src/ssl_sess.c:1992-1998). The new content is then copied over the old entry.

The accessors that use the unlocked pointer:

  1. wolfSSL_SESSION_get0_peer() performs a lazy write on the shared entry. If session->peer is NULL it assigns it from the certificate chain, with no lock held (src/ssl_sess.c:2423-2425). Racing this write against an overwrite can store the old session's peer certificate in the freshly overwritten entry, which attributes the wrong server identity to a session.
  2. wolfSSL_SESSION_get_peer_chain() returns &session->chain (src/ssl_sess.c:2396-2399).
  3. wolfSSL_SESSION_get_id() returns a pointer to sessionID or altSessionID inside the entry (src/ssl_sess.c:4594-4600). wolfSSL_SESSION_get0_id_context() returns the same pointer (src/ssl_sess.c:4416-4420).
  4. During an overwrite, the old entry's peer is saved (src/ssl_sess.c:2078-2083) and freed after the row is unlocked (src/ssl_sess.c:2209-2212). A pointer obtained earlier through item 1 then refers to freed memory.

Library-internal use is protected. wolfSSL_SetSession() takes the row read lock again before it duplicates the session (src/ssl_sess.c:1558-1588). Only the application-facing accessors hand out the live entry.

Reachability and Preconditions

  • Build: one of the OpenSSL-compatibility options above. The peer-certificate cases need SESSION_CERTS and OPENSSL_EXTRA.
  • Runtime: a multi-threaded application. One thread calls a session accessor on a cache-backed client session. Another completes a handshake that stores a session with the same session ID and side.
  • Attacker position: a remote server that the client reconnects to can cause the overwrite by reusing a session ID. The timing window is narrow and the attacker does not control the reader thread.
  • No authentication beyond a normal handshake is involved.

Impact

In every affected build the reader can observe torn session metadata. The typical result is a failed resumption. With SESSION_CERTS and OPENSSL_EXTRA, the application can receive a peer certificate that belongs to a different session, or keep a peer pointer that the cache has freed, which is a use-after-free in the application. Both need the overwrite to interleave with the accessor. Keying material is not exposed.

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 for the application-facing accessors to work on a locked snapshot. The accessor path can return a duplicate made with wolfSSL_DupSession() while the row read lock is held, as wolfSSL_SetSession() already does. Where the live entry must be returned, the ID getters should copy into caller buffers under the row read lock. session->peer should be populated when the session is stored, or under the row write lock, and any peer or chain pointer handed to the application should be duplicated or reference-counted before the lock is dropped. Workaround: do not call these accessors on a cache-backed session while another thread may be handshaking with the same server. Taking an independent copy of the session for inspection, or serializing those calls in the application, avoids the race.

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