← ALL ADVISORIES

CRITICAL BYTERAY-2026-0297Disclosed

wolfSSL — Heap Out-of-Bounds Write in Sniffer Static DH Key Agreement

The wolfSSL sniffer decrypts TLS 1.3 sessions that use a static DH key by computing the shared secret into a 512-byte buffer (heap-math builds) with no size check. With a 6144 or 8192-bit static key, any captured handshake that uses a finite-field group makes the sniffer write up to 512 bytes past the buffer.

Advisory
BYTERAY-2026-0297
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, builds with --enable-sniffer and --enable-heapmath, sniffer loaded with a static DH key whose prime is larger than 4096 bits
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

The wolfSSL sniffer decrypts captured TLS traffic for servers whose private keys the operator has loaded. For TLS 1.3 sessions that use a static Diffie-Hellman key, it recomputes the shared secret from that key and the client's key share, and writes the result into the preMasterSecret buffer of an internal session object. The buffer has ENCRYPT_LEN bytes. On builds that use the heap big-integer library that is 512 bytes, or 578 with PSK compiled.

The sniffer calls wc_DhAgree() on that buffer without comparing its size to the prime of the static key. With a 6144-bit or 8192-bit key the secret is 768 or 1024 bytes and the write runs past the end of the heap buffer.

The sniffer processes whatever handshakes it captures for a watched server, so anyone who can open a TLS 1.3 connection to that server on the monitored path can trigger the write. Default builds are not affected. The sniffer and heap math are both opt-in.

Affected Products and Versions

  • Product: wolfSSL sniffer, SetupKeys() in src/sniffer.c, static DH branch.
  • Required build: --enable-sniffer (which adds WOLFSSL_STATIC_EPHEMERAL and, with DH, WOLFSSL_DH_EXTRA), TLS 1.3, and heap math (--enable-heapmath).
  • Required configuration: a static DH key with a prime above 4096 bits loaded into the sniffer, for example the key of a server that uses FFDHE-6144 or 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.
  • Partly fixed on master. Commit 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345, merged 2026-10-09) enlarges the buffer so that keys for every compiled FFDHE group fit. The agreement call itself is still unchecked. No release contains the commit as of 2026-12-03.
  • A default ./configure build does not include the sniffer (configure.ac:3161) and does not use heap math (configure.ac:1336).
  • Earlier releases were not checked.

Technical Details

For heap math ENCRYPT_BASE_BITS is the constant 4096 (wolfssl/internal.h:1380-1382), and ENCRYPT_LEN is that value in bytes, plus 66 when PSK is compiled (wolfssl/internal.h:1563-1567). Each sniffer session owns a WOLFSSL object created with wolfSSL_new() (src/sniffer.c:5661), and its pre-master buffer is allocated with that size (src/internal.c:8373-8374).

The path through the sniffer is:

  1. src/sniffer.c:3541 — ProcessKeyShare() reads the named group of a key share from the captured hello. A compiled FFDHE group sets dh_key_bits.
  2. src/sniffer.c:4287 — for a TLS 1.3 session, ProcessServerHello() calls SetupKeys() with the client's key share as the peer public value.
  3. src/sniffer.c:2820 — the static DH branch is entered. The key comes from the static ephemeral key the operator loaded (src/sniffer.c:2827-2829).
  4. src/sniffer.c:2924 — wc_DhKeyDecode() loads the static key into the DH object. This replaces the named-group parameters set just before with the prime and generator from the key itself.
  5. src/sniffer.c:3268 — the secret is computed into the fixed buffer:
        if (args->key->type == WC_PK_TYPE_DH) {
            /* Derive secret from private key and peer's public key */
            ret = wc_DhAgree(&args->key->priv.dh.key,
                session->sslServer->arrays->preMasterSecret,
                &session->sslServer->arrays->preMasterSz,
                args->key->priv.dh.privKey, args->key->priv.dh.privKeySz,
                input, *sslBytes);
  1. wolfcrypt/src/dh.c:2348 — wc_DhAgree_Sync() writes the result with mp_to_unsigned_bin(z, agree) and stores the length in *agreeSz afterwards. It does not read *agreeSz as a capacity.

The size of the write is the size of the static key's prime. The group named in the handshake decides only whether the branch is entered. The RSA branch a few lines earlier passes a capacity to its decrypt call (src/sniffer.c:3256); the DH branch has no equivalent.

After the agreement the sniffer copies the secret, with its new length, into the buffer of the client-side session object, which has the same fixed size (src/sniffer.c:3374-3376).

Reachability and Preconditions

  • Build: sniffer and heap math. SP math and fastmath derive the buffer size from the big-integer size and were not found affected.
  • Operator configuration: a static DH key with a prime above 4096 bits, loaded with ssl_SetEphemeralKey() or ssl_SetEphemeralKeyBuffer(). This is the operator's choice. The attacker does not supply the key.
  • Trigger: a TLS 1.3 handshake for the watched address and port in which the server selects an FFDHE group that the sniffer build supports. TLS 1.2 handshakes do not take this branch.
  • Attacker position: anyone whose handshake the sniffer captures. A client that can connect to the monitored server is enough. No credentials are needed and the sniffer performs no authentication of its own.
  • No user interaction.

Impact

A heap out-of-bounds write in the process that runs the sniffer, typically a monitoring or inspection service that holds the servers' private keys.

ByteRay reproduced it by feeding the sniffer a captured, genuine TLS 1.3 handshake that used FFDHE-8192 and FFDHE-6144, with the matching static key loaded, on a heap-math sniffer build with those 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, wc_DhAgree, SetupKeys and ProcessServerHello. The same test with FFDHE-4096 ran with no report.

Confirmed: memory corruption and a crash of the sniffer process. Not verified: code execution. The bytes written are the DH shared secret, which the client can compute and vary but not choose freely. The second copy into the client-side buffer is from source review.

Remediation and Mitigation

Partly fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03).

Commit 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345) sizes the pre-master buffer from the compiled key exchanges. The DH part is the larger of the compiled maximum DH size and the largest compiled FFDHE group. On a master build, a static key for any group the sniffer supports therefore fits, and the case ByteRay reproduced no longer overflows. The commit is on master only. No release contains it as of 2026-12-03.

The sniffer's DH branch is otherwise unchanged on master. It still calls wc_DhAgree() directly on the fixed buffer, and the static key's size is not checked when it is loaded. PR 11930 added a size check to the TLS-layer DhAgree() wrapper, but the sniffer does not use that wrapper. From source review, a static key whose prime is larger than the buffer, that is, larger than both the compiled maximum DH size and every compiled FFDHE group, would still be written past the end. ByteRay did not test that case.

The remaining fix is a comparison in SetupKeys(): reject the session, or the key at load time, when the prime of the static key is longer than the pre-master buffer.

Until then, on heap-math builds do not load static DH keys above 4096 bits into the sniffer, or build the sniffer with SP math. On master, compile every FFDHE group for which a static key is loaded.

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-12-03 — Public disclosure.

Credits

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

References