← ALL ADVISORIES

HIGH BYTERAY-2026-0316Disclosed

wolfSSL — Use-After-Free Read of Pending Application Data After TLS Input Buffer Growth

wolfSSL returns decrypted application data through a pointer into its receive buffer. When more input is already buffered and the next record needs a larger buffer, GetInputData_ex() reallocates the buffer without checking for pending plaintext, and the next wolfSSL_read() copies from the freed block.

Advisory
BYTERAY-2026-0316
CWE
CWE-416
CVSS
8.2 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:L/A:N)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-27
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 (tested at master commit d95483f), TLS (not DTLS) builds without WOLFSSL_TLS_READ_AHEAD, which includes the default build
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfSSL does not copy decrypted application data out of its receive buffer. DoApplicationData() stores a pointer into the buffer, with a length, in ssl->buffers.clearOutputBuffer. wolfSSL_read() and wolfSSL_peek() later copy from that pointer. The pointer is valid only as long as the receive buffer is neither moved nor freed.

Other parts of the library respect this. wolfSSL_inject(), the shutdown path and ShrinkInputBuffer() all refuse to touch the buffer while plaintext is pending. The path that grows the buffer for the next record does not. When the receive buffer still holds bytes after an application-data record, the record loop goes on to the next record. If that record is larger than the buffer, GetInputData_ex() allocates a new block and frees the old one, with the pending pointer still aimed at it.

If the body of that record has not arrived, the call returns WANT_READ. The next read finds pending data and copies it from freed memory into the application's buffer. The code is in the default build. Reaching it requires the receive buffer to hold part of a following record, and the Reachability section describes how far ByteRay confirmed that.

Affected Products and Versions

  • Component: src/internal.c, GetInputData_ex(), with the pointer set in DoApplicationData() and read in ReceiveData().
  • Required build: TLS 1.2 or TLS 1.3 over a stream transport. DTLS returns from the record loop after application data and does not take this path. Builds with WOLFSSL_TLS_READ_AHEAD also return early; read-ahead is off by default (configure.ac:2675-2683).
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The report states that the path is reachable in a default ./configure build with no extra flags.
  • Other releases were not checked.

Technical Details

DoApplicationData() computes rawData = input + idx (src/internal.c:24001) and stores it as the pending plaintext (src/internal.c:24164-24165):

        ssl->buffers.clearOutputBuffer.buffer = rawData;
        ssl->buffers.clearOutputBuffer.length = (unsigned int)dataSz;

GetInputData_ex() grows the receive buffer when the requested record does not fit (src/internal.c:24697-24701):

    if (inSz > maxLength) {
        if (GrowInputBuffer(ssl,
                (int)(size + (word32)dtlsExtra + (word32)extra), usedLength) < 0)
            return MEMORY_E;
    }

There is no test of clearOutputBuffer.length before the call. GrowInputBuffer() uses ResizeInputBufferStorage(), which allocates a new block (src/internal.c:12800), moves the unread bytes and frees the old block (src/internal.c:12825). The guards that exist elsewhere are at src/ssl_api_rw.c:443-448, src/ssl_api_rw.c:999 and src/internal.c:12477.

The sequence:

  1. wolfSSL_read() runs the record loop. An application-data record is decrypted, and clearOutputBuffer is pointed at its plaintext inside the receive buffer.
  2. The receive buffer is not exhausted, so the loop continues with the next record instead of returning (src/internal.c:26449-26484). The DTLS and read-ahead conditions that would return here are compiled out.
  3. The header of the next record is parsed. The loop asks for the record body with GetInputData(ssl, ssl->curSize) (src/internal.c:25846). The size comes from the peer's record header.
  4. The body does not fit. GetInputData_ex() grows the buffer, and the old block, which the pending pointer refers to, is freed.
  5. The body has not arrived. The receive call returns WANT_READ (src/internal.c:24719-24720), and so does wolfSSL_read(). The pending pointer and length are unchanged.
  6. The application calls wolfSSL_read() or wolfSSL_peek() again. Pending length is non-zero, so no record processing takes place. ReceiveData() copies from the stale pointer (src/internal.c:30040) and advances it (src/internal.c:30043-30044).

When the next record does fit and no reallocation is needed, the same function still moves the unread bytes to the start of the buffer (src/internal.c:24703-24707). That move can overwrite the pending plaintext in place.

Reachability and Preconditions

  • Build: default. TLS, not DTLS, and no WOLFSSL_TLS_READ_AHEAD.
  • The peer has completed the handshake. It can be a client or a server. It sends a valid application-data record, then the header of a second record that announces a larger size, and withholds the body.
  • The receive buffer must contain bytes of the second record at the moment the first one has been decrypted. The report describes this as the peer sending both in one TCP segment.
  • In ByteRay's test the default socket receive path did not produce that state. It asks the transport for exactly the bytes of the current record, so nothing of the next record is buffered when the first is complete. ByteRay produced the state by passing the received segment to wolfSSL_inject(), the public function for applications that read the transport themselves. That usage is the confirmed exposure. The trigger was not confirmed with the stock socket callbacks.
  • The application calls wolfSSL_read() or wolfSSL_peek() again after WANT_READ, as event-driven code does.

Impact

The application receives, as TLS application data, the contents of a heap block that wolfSSL has freed. The block is zeroed before release when encryption is active, so what the read returns is whatever the allocator has since placed there. In a process that serves several connections that can be data from another session. The amount is bounded by the pending record length, at most one record. A read from a block that has been returned to the system can also crash the process. In the variant without reallocation, the application receives bytes of the following record in place of the plaintext it was owed.

ByteRay reproduced both variants on a TLS 1.2 session under AddressSanitizer, with the received segment supplied through wolfSSL_inject(). With a record header that forced the buffer to grow, the second wolfSSL_read() produced a heap-use-after-free read of 32 bytes in ReceiveData, on memory freed in ResizeInputBufferStorage. With part of the second record's body present, the pending plaintext was replaced by the second record's wire bytes and no sanitizer report was raised. A control run with benign input produced no 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.

The fix direction is to apply the existing rule on the grow path. Before GetInputData_ex() grows or compacts the buffer it should check for pending plaintext. It can then either copy the pending bytes to stable storage and repoint clearOutputBuffer, or defer the operation until the application has read them, as wolfSSL_inject() and ShrinkInputBuffer() do. The pending pointer should also be made safe on the MEMORY_E and WANT_READ exits of this sequence. A regression test through wolfSSL_read() and wolfSSL_peek() would cover the case.

For applications that use wolfSSL_inject(): pass no more than one complete record per call, and read all pending application data before injecting more. ByteRay has not tested this as a complete workaround.

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