wolfSSL — Race Conditions and Unchecked Buffer Growth in the Threaded Record Encryption Job Lifecycle
The WOLFSSL_THREADED_CRYPT job slots are shared between the connection thread and worker threads through plain integer flags with no synchronisation. A slot is reported ready before its record is staged, connection teardown frees cipher state and job buffers while workers may still use them, and the send path ignores a failed output buffer allocation before copying a full record.
- Advisory
- BYTERAY-2026-0341
- CWE
- CWE-362
- CVSS
- 6.9 (CVSS:3.1/AV:L/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:L)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 through 5.9.4, builds with WOLFSSL_THREADED_CRYPT
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
With WOLFSSL_THREADED_CRYPT, wolfSSL_write() parks each outgoing record in one of 16 job slots. Application worker threads poll a slot with wolfSSL_AsyncEncryptReady(), encrypt it with wolfSSL_AsyncEncrypt(), and the connection thread later sends it. The slot state is a set of plain int fields that both sides read and write without a lock, an atomic type or a memory barrier.
Three defects follow from how that state is handled. The connection thread marks a slot in the state that workers treat as ready before it has allocated the buffer or built the record. wolfSSL_free() sets a stop flag and frees the slot's cipher object and buffer in the same step, with no way to wait for a worker, and the stop flag itself is inside the object being freed. When a finished job is sent, the result of growing the output buffer is ignored, so after a failed allocation a whole record is copied into a buffer that by default holds five bytes.
None of these is driven by bytes from the network. They depend on thread timing, teardown order and memory exhaustion. Default builds are not affected: no configure option sets WOLFSSL_THREADED_CRYPT.
Affected Products and Versions
- Component: TLS record sending and connection teardown in
src/internal.c(SendData(),SendAsyncData(),wolfSSL_ResourceFree()), and the worker API insrc/ssl.c. - Required build:
WOLFSSL_THREADED_CRYPT, defined by the integrator. The macro is not set byconfigure.acor the CMake build. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same code. - Not fixed on master.
- A default
./configurebuild does not compile this code. Earlier releases were not checked.
Technical Details
The job state is ThreadCrypt (wolfssl/internal.h:5278-5291). Its init, offset, cryptLen, done, avail and stop members are plain int. Sixteen of them are embedded in the WOLFSSL object (wolfssl/internal.h:5318-5320).
Premature readiness. A worker asks whether a slot has work with (src/ssl.c:10433-10434):
encrypt = &ssl->buffers.encrypt[idx];
return (encrypt->avail == 0) && (encrypt->done == 0);
SendData() puts a slot into exactly that state as soon as it picks it (src/internal.c:29736-29737), and only afterwards allocates or reallocates the job buffer (src/internal.c:29738), builds the record (src/internal.c:29773-29779), creates the slot's cipher (src/internal.c:29794-29799) and fills in the length, offset and additional data (src/internal.c:29800-29828). A worker that polls in that window encrypts a buffer that is being replaced or a record that is half built. SendAsyncData() opens the same window on a job it has just sent, because it clears done before it sets avail (src/internal.c:29347-29348).
Teardown. wolfSSL_ResourceFree(), called from FreeSSL() (src/internal.c:10493), does this for each slot (src/internal.c:9996-10003):
for (i = 0; i < WOLFSSL_THREADED_CRYPT_CNT; i++) {
bufferStatic* buff = &ssl->buffers.encrypt[i].buffer;
ssl->buffers.encrypt[i].stop = 1;
FreeCiphersSide(&ssl->buffers.encrypt[i].encrypt, ssl->heap);
if (buff->dynamicFlag) {
XFREE(buff->buffer - buff->offset, ssl->heap,
DYNAMIC_TYPE_OUT_BUFFER);
The stop flag is set and the cipher object and job buffer are freed immediately. A worker that is inside wolfSSL_AsyncEncrypt() at that moment keeps using both. A worker that checks the flag afterwards with wolfSSL_AsyncEncryptStop() reads it from the WOLFSSL object, which wolfSSL_free() releases next. The library offers no call that waits for workers to leave a slot.
Unchecked growth. SendAsyncData() moves a finished job to the output buffer (src/internal.c:29342-29344):
GrowOutputBuffer(ssl, encrypt->buffer.length);
XMEMCPY(ssl->buffers.outputBuffer.buffer, encrypt->buffer.buffer,
encrypt->buffer.length);
The sequence that leads to the overflow is:
- After each complete send the output buffer is shrunk back to its static storage (
src/internal.c:12631-12632), which isRECORD_HEADER_SZ, five bytes, unless the build overrides it (wolfssl/internal.h:2484-2492). GrowOutputBuffer()returnsMEMORY_Eand leaves the buffer unchanged when its allocation fails (src/internal.c:12744-12749).SendAsyncData()discards that result and copiesencrypt->buffer.lengthbytes, a full record, into the five-byte buffer inside theWOLFSSLobject.
Reachability and Preconditions
- The library is built with
WOLFSSL_THREADED_CRYPTand the application runs worker threads against the job slots. - Premature readiness needs a worker to poll a slot while the connection thread is between selecting it and finishing the staging. Workers that act only on the signal callback, which fires after staging, avoid the first window but not the one in
SendAsyncData(). - The teardown defect needs
wolfSSL_free()to run while a worker is in, or later enters, the worker API for that connection. - The overflow needs a memory allocation of record size to fail on the send path.
- A network peer does not control any of these directly. A peer can influence timing and load, for example by closing a connection during a transfer.
Impact
Confirmed from the source: a worker can encrypt and mark done a job whose buffer is being reallocated or whose contents are incomplete; teardown frees memory that worker threads are entitled, by the API, to be using; and an allocation failure on the send path becomes a heap overflow of up to a record in length inside the WOLFSSL object, with the record bytes as content.
Plausible but not demonstrated: use of the freed cipher object or buffer by a worker leading to disclosure of other heap data in a transmitted record, or to memory corruption. ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Not fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03). A fix needs the slot state to be synchronised, the ready state to be published only after staging is complete, a teardown that stops workers and waits for them before freeing slot resources, and a check of the buffer growth result before the copy.
Applications that use this feature today should stop and join their worker threads before calling wolfSSL_free(), and should have workers act on the signal callback rather than polling. These steps do not remove the unchecked growth.
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
- Teardown of threaded encryption slots at the tested commit
- SendAsyncData at the tested commit
- ThreadCrypt job state at the tested commit
- BYTERAY-2026-0340: TLS 1.3 records in threaded record encryption
- BYTERAY-2026-0342: worker API index and failure handling
- CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization