← ALL ADVISORIES

CRITICAL BYTERAY-2026-0293Patched

wolfSSL — Heap Out-of-Bounds Write in TLS 1.3 FFDHE Key Share

On heap-math builds the TLS 1.3 pre-master secret buffer is 512 bytes (578 with PSK), but an FFDHE-6144 or FFDHE-8192 key share produces a 768 or 1024-byte shared secret. wolfSSL wrote the secret without checking the buffer size, so a peer that negotiates one of those groups causes a heap write of up to 512 bytes past the buffer before authentication.

Advisory
BYTERAY-2026-0293
CWE
CWE-787
CVSS
9.8 (CVSS:3.1/AV:N/AC:L/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, heap-math builds (--enable-heapmath) with TLS 1.3 and FFDHE-6144 or FFDHE-8192 compiled in
Fixed in
Master commit 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345, merged 2026-10-09). Not in a wolfSSL stable release as of 2026-12-03.
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

In TLS 1.3 wolfSSL computes the (EC)DHE shared secret straight into ssl->arrays->preMasterSecret. That buffer has a fixed size, ENCRYPT_LEN, which is derived from the big-integer configuration and not from the key exchange groups that are compiled in. On builds that use the heap big-integer library, ENCRYPT_LEN is 512 bytes, or 578 bytes when PSK support is compiled.

The finite-field path, TLSX_KeyShare_ProcessDh(), passes that buffer to the DH agreement without comparing its size to the prime of the negotiated group. For FFDHE-6144 and FFDHE-8192 the shared secret is as long as the prime, 768 or 1024 bytes. The write runs up to 512 bytes past the end of the heap buffer.

The key share is processed before either side is authenticated. A client can trigger the write in a server and a server can trigger it in a client. Default builds are not affected: they use SP math and compile only FFDHE-2048.

Affected Products and Versions

  • Product: wolfSSL, TLS 1.3 key share processing for finite-field groups (src/tls.c).
  • Required build: heap math (--enable-heapmath, USE_INTEGER_HEAP_MATH), TLS 1.3, DH, and HAVE_FFDHE_6144 or HAVE_FFDHE_8192.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The v5.9.2-stable and v5.9.4-stable tags contain the same code: the same fixed buffer and the same call with no size check.
  • Fixed on master by 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345, merged 2026-10-09). No release contains the fix as of 2026-12-03.
  • A default ./configure build is not affected. Heap math is off by default (configure.ac:1336) and only FFDHE-2048 is enabled (configure.ac:9418). The larger groups are added by defining the macros in CFLAGS or a user settings header.
  • Earlier releases were not checked.

ByteRay found the overflow only with heap math. With SP math and fastmath the buffer size follows the big-integer size, which must already be able to hold the largest prime in use.

Technical Details

The buffer size is set in wolfssl/internal.h. For heap math ENCRYPT_BASE_BITS is a constant:

    #else
        /* Integer/heap maths - support 4096-bit. */
        #define ENCRYPT_BASE_BITS  4096

ENCRYPT_LEN is ENCRYPT_BASE_BITS / 8, plus MAX_PSK_KEY_LEN + 2 when PSK is compiled (wolfssl/internal.h:1563-1567). TLS 1.3 builds allocate the buffer in ReinitSSL() (src/internal.c:8373-8374).

TLSX_KeyShare_ProcessDh() looks up the prime size of the group and applies one check, that the peer's public value has that length (src/tls.c:9655). It then calls the agreement:

        ret = DhAgree(ssl, dhKey,
            (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
            keyShareEntry->ke, keyShareEntry->keLen,                    /* peer's public key */
            ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,    /* secret */
            NULL, 0
        );

preMasterSz holds ENCRYPT_LEN at this point (src/tls.c:10666-10667), but nothing downstream treats it as a capacity. The data flow is:

  1. src/tls.c:9643 — pSz is the prime length of the named group, 768 for FFDHE-6144 and 1024 for FFDHE-8192. Builds without HAVE_PUBLIC_FFDHE get the same value at src/tls.c:9645.
  2. src/tls.c:9721 — DhAgree() receives the fixed buffer.
  3. src/internal.c:7386 — DhAgree() forwards the buffer and the size pointer to wc_DhAgree() without a comparison.
  4. wolfcrypt/src/dh.c:2465 — wc_DhAgree() calls wc_DhAgree_Sync() in its non-constant-time mode.
  5. wolfcrypt/src/dh.c:2348 — the result is written with mp_to_unsigned_bin(z, agree). The length is stored in *agreeSz only afterwards.
        else {
            ret = mp_to_unsigned_bin(z, agree);
            if (ret == MP_OKAY)
                *agreeSz = (word32)mp_unsigned_bin_size(z);
        }

The secret is a value below the prime, so it is almost always as long as the prime. With a 512-byte buffer the write passes the end by up to 256 bytes for FFDHE-6144 and up to 512 bytes for FFDHE-8192. With PSK compiled the buffer is 578 bytes and the figures are 190 and 446.

Reachability and Preconditions

  • Build: heap math, TLS 1.3, and FFDHE-6144 or FFDHE-8192 compiled in. All three are opt-in.
  • Server side: the client offers one of those groups in supported_groups and sends a key share for it. TLSX_KeyShare_Choose() (src/tls.c:12065) accepts a client share when the group is compiled in and ranked in the server's group list. The built-in list includes both large groups when they are compiled (src/tls.c:11655-11659). The source shows no DH size policy check on this path, so a server needs no special runtime setting. The write happens in TLSX_KeyShare_DeriveSecret(), called from src/tls13.c:18076.
  • Client side: the server answers with a key share for the large group. A client accepts only a group it offered (src/tls.c:10904). By default a heap-math client does not offer groups above 4096 bits (src/tls.c:16468-16469), so the application must have raised the maximum DH size or set an explicit group list.
  • Attacker position: any TLS 1.3 peer. No credentials are needed and the handshake is not yet authenticated on either side.
  • No user interaction.

Impact

A heap out-of-bounds write of up to 512 bytes in a server or client, reachable by an unauthenticated peer during the handshake.

ByteRay reproduced it with wolfSSL on both ends of a TLS 1.3 handshake using FFDHE-8192, on a heap-math build with the large groups compiled. AddressSanitizer reported a heap-buffer-overflow write that started 0 bytes past the 512-byte pre-master buffer, in mp_to_unsigned_bin called from wc_DhAgree_Sync, DhAgree and TLSX_KeyShare_ProcessDh. It was reached both in the server's accept path and in the client's ServerHello processing. The same handshake with FFDHE-4096 completed with no report. In that test the maximum DH size was raised on both endpoints. That a server needs no such setting is from source review.

Confirmed: memory corruption and a crash. Not verified: code execution. The bytes written are the DH shared secret. The peer knows them, because it can compute the same secret, and it can vary them through its own key share, but it does not choose them freely. Whether that is enough for more than a crash depends on the allocator and on what follows the buffer.

Remediation and Mitigation

Use a master build that contains 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345). The commit sizes the pre-master buffer from the compiled key exchanges, so it is at least as large as the largest compiled FFDHE prime, and it makes TLSX_KeyShare_ProcessDh() reject a group whose prime is larger than the buffer. It also caps the value accepted by wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() at the compiled maximum.

PR 11930 (commit c411cc71b7e6c8b8050ff98c6ffdb6c91632cc6e, merged 2026-11-03) adds a second check: the TLS-layer DhAgree() now returns BUFFER_E when the output buffer is smaller than the prime. wc_DhAgree() itself is unchanged and still expects the caller to supply a prime-sized buffer.

Both changes are on master only. No release contains them as of 2026-12-03.

Until an update is possible, build without HAVE_FFDHE_6144 and HAVE_FFDHE_8192, or do not use heap math. Either removes the condition. The cost is losing those two groups, or changing the math backend.

Timeline

  • 2026-09-27 — Reported to wolfSSL.
  • 2026-10-09 — PR 11345 merged to master. It had been opened on 2026-09-01, before this report.
  • 2026-10-29 — PR 11930 opened.
  • 2026-11-03 — PR 11930 merged to master.
  • 2026-12-03 — Public disclosure.

Credits

Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.

References