wolfSSL — Two-Byte Heap Out-of-Bounds Write in TLS 1.2 DHE-PSK Pre-Master Secret Composition
The buffer that holds the TLS 1.2 DHE-PSK pre-master secret reserves space for one 2-byte length field, but the composition writes two. With a 4096-bit DH prime and a 63- or 64-byte PSK, MakePSKPreMasterSecret writes one or two bytes past the end of the heap allocation. No capacity check exists on the path.
- Advisory
- BYTERAY-2026-0394
- CWE
- CWE-787
- CVSS
- 5.6 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:L)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), builds with PSK support (--enable-psk) and the integer (heap) math backend, when a TLS 1.2 DHE-PSK suite is used with a 4096-bit DH prime and a 63- or 64-byte PSK
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
In a TLS 1.2 DHE-PSK handshake, wolfSSL builds the pre-master secret in one heap buffer: a 2-byte length, the Diffie-Hellman shared secret, a second 2-byte length, and the pre-shared key (RFC 4279). The buffer is ssl->arrays->preMasterSecret, and it is ENCRYPT_LEN bytes long.
ENCRYPT_LEN reserves room for one 2-byte length field, not two. With the integer math backend it is 578 bytes. A 4096-bit DH prime gives a shared secret of up to 512 bytes and a PSK can be 64 bytes, so the composition can reach 580 bytes. MakePSKPreMasterSecret() writes it without comparing the total to the buffer size. The write runs one or two bytes past the end of the allocation. Those bytes are the last bytes of the PSK.
All of these sizes are inside the library's default limits. On a client, the DH prime comes from the ServerKeyExchange message, which DHE-PSK does not sign, so the server or a party in the network path chooses it. PSK support is not part of a default build.
Affected Products and Versions
- Component:
src/internal.c,MakePSKPreMasterSecret(), used by the TLS 1.2 client and server DHE-PSK key exchange. - Required build: PSK support (
--enable-psk, off by default,configure.ac:6166-6168), DH, and a configuration whereENCRYPT_BASE_BITSis 4096. The analysis covers the integer (heap) math backend (wolfssl/internal.h:1380-1383), whereENCRYPT_LENis 578 bytes. Other math backends size the buffer from their own maximum integer width and were not assessed. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not enable PSK and does not use the integer math backend. Within an affected build, no DH or PSK size limit has to be raised from its default. - Other releases were not checked.
Technical Details
The size constant is ENCRYPT_LEN = (ENCRYPT_BASE_BITS / 8) + MAX_PSK_KEY_LEN + 2 (wolfssl/internal.h:1564). With ENCRYPT_BASE_BITS at 4096 and MAX_PSK_KEY_LEN at 64 (wolfssl/internal.h:1305-1308) that is 512 + 64 + 2 = 578. The trailing 2 covers one OPAQUE16_LEN prefix. The buffer is allocated with that size (src/internal.c:8372-8375).
MakePSKPreMasterSecret() writes two prefixes (src/internal.c:34686-34702). It stores the length of the DH secret with c16toa, advances past the secret, stores the PSK length with a second c16toa, and copies the PSK with XMEMCPY.
The footprint is 2 + sz + 2 + psk_keySz. Neither the function nor its callers compare that to ENCRYPT_LEN. On the client the data arrives as follows:
- The DHE-PSK ServerKeyExchange carries the DH prime, generator and public value, unsigned.
GetDhPublicKey()rejects a prime longer thanssl->options.maxDhKeySz(src/internal.c:36056-36065). The default maximum,MAX_DHKEY_SZ(wolfssl/internal.h:1298), is 512 bytes, so a 4096-bit prime passes. DhAgree()writes the shared secret atpreMasterSecret + OPAQUE16_LENand stores its length inpreMasterSz(src/internal.c:37770-37777). With a 4096-bit prime the secret is 511 or 512 bytes in roughly half of all handshakes.- The PSK length was limited to
MAX_PSK_KEY_LENearlier (src/internal.c:37613-37617). That check is correct for the PSK alone. SendClientKeyExchange()callsMakePSKPreMasterSecret(ssl->arrays, 0)(src/internal.c:37980), which writes the composition.
With a 512-byte secret and a 64-byte PSK the total is 580 and the copy ends two bytes past the buffer. With 512 and 63, or 511 and 64, it ends one byte past.
The server side is the same code: DoClientKeyExchange() calls DhAgree() (src/internal.c:45980-45988) and then MakePSKPreMasterSecret() (src/internal.c:46171). There the prime is the operator's own temporary DH parameter set.
Reachability and Preconditions
- Build with PSK and DH, and a 578-byte
ENCRYPT_LENas described above. - A TLS 1.2 DHE-PSK cipher suite is negotiated.
- The application's PSK is 63 or 64 bytes long.
- Client: the peer, or anyone who can alter the unsigned ServerKeyExchange in transit, supplies a 4096-bit prime. The write happens before the PSK is proven by either side.
- Server: the operator has configured temporary DH parameters of about 4096 bits. Any client that can start a DHE-PSK handshake then triggers the composition.
- The shared secret must come out at 511 or 512 bytes, which happens in about one handshake in two.
Impact
A heap out-of-bounds write of one or two bytes immediately after a buffer that holds secret material. The bytes written are the tail of the PSK, so the attacker does not choose them. The possible results are corruption of an adjacent heap object or of allocator metadata, and secret bytes left outside the buffer that is later wiped. The narrow size combination and the uncontrolled content make reliable exploitation difficult.
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 to size ENCRYPT_LEN for both length prefixes, and to add a capacity check in MakePSKPreMasterSecret() or at its two call sites that fails the handshake when 2 + preMasterSz + 2 + psk_keySz exceeds the buffer. Bounding the accepted DH secret size against the remaining capacity before DhAgree() gives a clean handshake error for an oversized prime.
Until a fix is applied, the sizes can be kept out of range: use PSKs of 62 bytes or fewer, or lower the maximum accepted DH key size below 4096 bits. Disabling the DHE-PSK cipher suites removes the path.
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.