← ALL ADVISORIES

MEDIUM BYTERAY-2026-0372Disclosed

wolfSSL — Double Free in i2d_DSAparams on DER Encoding Failure

When wolfSSL_i2d_DSAparams allocates the output buffer for the caller and the DER encoding step then fails, it frees the buffer but leaves the caller's pointer set to the freed address. A caller that frees its output pointer on the error path, the usual OpenSSL pattern, frees the same memory twice.

Advisory
BYTERAY-2026-0372
CWE
CWE-415
CVSS
5.9 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:H)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-28
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 (tested at master commit d95483f), builds with OPENSSL_EXTRA and DSA whose application calls wolfSSL_i2d_DSAparams with a NULL output pointer and frees the output after a failure
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfSSL_i2d_DSAparams() is the OpenSSL compatibility function that encodes DSA parameters as DER. When the caller passes a pointer whose value is NULL, the function allocates the output buffer itself and stores its address through that pointer.

The address is stored before the encoding runs. The encoding can still fail, for example when an internal allocation fails. On that path the function frees the buffer it allocated and returns an error. It does not reset the caller's pointer, which now refers to freed memory.

OpenSSL's contract for this family of functions is that the output pointer is untouched or NULL after a failure. A caller that relies on it and frees the pointer regardless of the return value frees the buffer a second time. The result is heap corruption and, typically, a crash. The trigger is a failure of the encoding step, not the content of any input. DSA and the OpenSSL compatibility layer are not part of a default build.

Affected Products and Versions

  • Component: src/pk.c, wolfSSL_i2d_DSAparams(), exported to applications as i2d_DSAparams (wolfssl/openssl/dsa.h:151).
  • Required build: OPENSSL_EXTRA with DSA enabled. Required use: the caller passes *out == NULL and frees *out after an error return.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • A default build is not affected. DSA is disabled by default (configure.ac:5833-5835), as is the OpenSSL compatibility layer (configure.ac:3315). Other releases were not checked.

Technical Details

The relevant part of the function (src/pk.c:1864-1883):

        *out = (unsigned char*)XMALLOC(derLen, key->heap, DYNAMIC_TYPE_OPENSSL);
    ...
        ret = wc_DsaKeyToParamsDer_ex(key, *out, &derLen);
    ...
    if (ret < 0 && preAllocated == 0) {
        XFREE(*out, key ? key->heap : NULL, DYNAMIC_TYPE_OPENSSL);
    }

The sequence:

  1. A first call to wc_DsaKeyToParamsDer_ex() with a NULL buffer returns the encoded size in derLen (src/pk.c:1859).
  2. The function allocates derLen bytes and writes the address into the caller's *out (src/pk.c:1869).
  3. A second call to wc_DsaKeyToParamsDer_ex() does the encoding (src/pk.c:1875). It can fail even though the first call succeeded. Its internal working allocation can fail and return MEMORY_E (wolfcrypt/src/asn.c:12805). Its size check returns BAD_FUNC_ARG when the encoded size no longer fits (wolfcrypt/src/asn.c:12834-12836), which can happen if the key's integers are changed by another thread between the two calls.
  4. On failure the function frees *out (src/pk.c:1881-1883) and returns. *out still holds the freed address.
  5. The caller frees *out.

The DSA signature encoder in the same file, wolfSSL_i2d_DSA_SIG(), allocates into a local variable and assigns to *out only once the allocation has succeeded and no failing step remains (src/pk.c:1475-1482). wolfSSL's own test for i2d_DSAparams() follows the pattern that goes wrong: it starts with a NULL output pointer (tests/api.c:25319), calls the function (tests/api.c:25326) and frees the pointer unconditionally (tests/api.c:25331).

One caller inside the library, wolfSSL_X509_PUBKEY_set(), passes the data pointer of an ASN.1 string as the output (src/x509.c:11602-11603). After a failure that pointer is also left dangling. A second free is avoided there because the string's free routine releases the data only when the length is positive and the string is marked dynamic (src/ssl_asn1.c:2673), and neither holds on the failure path.

Reachability and Preconditions

  • Build with OPENSSL_EXTRA and DSA. The application calls i2d_DSAparams() with a pointer whose value is NULL and frees that pointer after an error.
  • The encoding step fails after the sizing step succeeded. The two triggers are an allocation failure, which a remote party can provoke in a network-facing process by putting it under memory pressure, and a concurrent change to a DSA key shared between threads without locking.
  • No network path inside wolfSSL reaches this function directly. Exposure depends on how the embedding application uses it.

Impact

The same heap block is released twice. That corrupts allocator state and normally ends in a crash of the process that embeds wolfSSL, a denial of service. The condition is deterministic once the encoding step fails, but an attacker has to cause that failure indirectly.

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 set *out to NULL after the free, or better, to allocate into a local variable, run the encoding, and assign to *out only after every step that can fail has succeeded. That matches the contract callers expect from OpenSSL. Until then, an application can pass a temporary pointer, check the return value, and free or adopt the buffer only on success. An application that must keep the usual pattern can set its own pointer to NULL after an error return without freeing it.

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.

References