wolfSSL — Partial and Removable Memory Wipe in Heap Math mp_clear on Non-FIPS Builds
In the heap math backend, mp_clear on non-FIPS builds zeroes only the digits currently in use, with plain stores and no barrier, and then frees the buffer. A compiler may remove the stores, and digits beyond the in-use count are never cleared. The main key-free paths wipe their secrets separately.
- Advisory
- BYTERAY-2026-0525
- CWE
- CWE-733
- CVSS
- 2.5 (CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:L/I:N/A:N)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), non-FIPS builds that use the heap math big-integer backend in integer.c (--enable-heapmath, with SP math and fastmath disabled)
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
wolfSSL's heap math backend (wolfcrypt/src/integer.c) releases every big integer through mp_clear(), which is meant to erase the heap digit buffer before freeing it. In a FIPS build it calls the hardened mp_forcezero(). In other builds it runs its own loop: it writes zero to the digits marked in use, with ordinary stores, and frees the buffer immediately.
Two things can go wrong. The stores are never read and nothing stops their removal, so an optimizing compiler may delete them. And only used of the alloc digits are cleared, so if the integer once held a larger value, the trailing digits keep their old contents even when the stores run.
The practical exposure is limited. This backend is built only when both the default SP math backend and fastmath are disabled. The RSA, ECC and DH code wipes its most sensitive values with the hardened routine before freeing them. Reading leftover bytes needs a separate memory disclosure.
Affected Products and Versions
- Component:
wolfcrypt/src/integer.c,mp_clear(). The partial wipe also applies to this file'smp_forcezero(). - Required build: the heap math backend without
HAVE_FIPS. It is used only when SP math and fastmath are both disabled; the defaults are SP math on and fastmath off (configure.ac:474-477). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not compile this backend. - Other releases were not checked.
Technical Details
The non-FIPS branch of mp_clear() (wolfcrypt/src/integer.c:151-182) is:
/* first zero the digits */
for (i = 0; i < a->used; i++) {
a->dp[i] = 0;
}
/* free ram */
mp_free(a);
mp_free() passes the digit buffer to XFREE (wolfcrypt/src/integer.c:184-196). Under HAVE_FIPS the function calls mp_forcezero(a) instead (wolfcrypt/src/integer.c:153-154).
- No barrier. The loop (
wolfcrypt/src/integer.c:170-172) uses no volatile access, noWC_BARRIER_DATAand no assembly construct. Its stores are the last writes to the buffer before it is freed, so a compiler may remove them and the wipe does nothing. - Partial wipe. The loop stops at
a->used. Digits fromusedup toallocare not touched. If the integer earlier held a value with more digits, for example a longer private scalar, those digits stay in the freed chunk. - The second point also applies to the FIPS branch of this backend:
mp_forcezero()in the same file wipesa->used * sizeof(mp_digit)bytes (wolfcrypt/src/integer.c:198-210).
The exposure is bounded by what callers do first. The DH agreement code force-zeroes the shared secret and the private input (wolfcrypt/src/dh.c:2354-2356). The ECDH code force-zeroes the shared point (wolfcrypt/src/ecc.c:5125-5126). Key frees force-zero the RSA private components (wolfcrypt/src/rsa.c:689-695), the DH private value (wolfcrypt/src/dh.c:1043) and the ECC private scalar (wolfcrypt/src/ecc.c:8660-8669). For those values mp_clear() is not the only wipe. Because mp_forcezero() in this backend also stops at used, the residue case in point 2 can still apply to them.
Reachability and Preconditions
- Non-FIPS build with the heap math backend.
- Either a compiler that removes the zeroing stores, or an integer whose in-use digit count shrank below its allocation before it was cleared.
- Sensitive data in the affected digits that was not wiped by an earlier hardened call.
- A separate way for an attacker to read freed heap memory, such as a heap over-read, a core dump or physical memory capture.
Impact
Bytes of big integers can remain in freed heap memory after mp_clear() returns. These integers carry private keys, shared secrets and intermediate values. For the values the library's own teardown paths handle, the hardened wipe runs first, so the residual exposure is to values freed without it and to digits above the in-use count. Recovery needs an independent memory-disclosure issue.
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 remove the HAVE_FIPS condition in mp_clear() and always wipe through ForceZero(), and to wipe the full allocation, a->alloc digits, in both mp_clear() and this backend's mp_forcezero(). If a hand-written loop is kept, it needs a volatile-qualified pointer or a data barrier after the stores. Builds on the default SP math backend, or on fastmath, do not compile this function. Related issues in those backends and in the wipe helper are described in BYTERAY-2026-0526, BYTERAY-2026-0524 and BYTERAY-2026-0523.
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.