wolfSSL — Stack Out-of-Bounds Write in wolfSSL_iotsafe_on_ex from Unchecked id_size
wolfSSL_iotsafe_on_ex copies id_size bytes into each of four slot-id fields of a stack-local struct without comparing id_size to the field width, which is 1, 2 or 4 bytes depending on the build. A larger id_size from the calling application writes past the fields and the struct, and reads past the caller's source buffers.
- Advisory
- BYTERAY-2026-0498
- CWE
- CWE-787
- CVSS
- 3.6 (CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:N/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 the IoT-Safe port (WOLFSSL_IOTSAFE, --enable-iotsafe) and HAVE_PK_CALLBACKS
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
wolfSSL_iotsafe_on_ex() is the public function an application calls to tell the IoT-Safe port which secure-element slots to use for the private key, the ECDH key pair, the peer public key and the peer certificate. It takes a caller-supplied length, id_size, a 16-bit value.
The function copies exactly id_size bytes into each of four fields of a small struct on its stack. Those fields are 1, 2 or 4 bytes wide, selected at build time by IOTSAFE_ID_SIZE, which defaults to 1. Nothing compares id_size with the field width. A length above the build-time width runs past the intended field. A length above roughly 5 bytes runs past the struct and corrupts the stack frame. Each copy also reads the same number of bytes from the caller's source buffer.
Every caller in the library passes 1 byte, and no remote or network-influenced data reaches the parameter. The overflow happens only when the embedding application itself passes an oversized length. It is a missing check in a public API. Default builds do not compile the IoT-Safe port.
Affected Products and Versions
- Component:
wolfcrypt/src/port/iotsafe/iotsafe.c,wolfSSL_iotsafe_on_ex(). - Required build: the IoT-Safe port,
WOLFSSL_IOTSAFE(--enable-iotsafe, disabled by default), withHAVE_PK_CALLBACKS. WithoutHAVE_PK_CALLBACKSthe function returnsNOT_COMPILED_IN. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not include this code. Other releases were not checked.
Technical Details
The function is at wolfcrypt/src/port/iotsafe/iotsafe.c:1816-1843. Its parameter id_size is a word16 (wolfcrypt/src/port/iotsafe/iotsafe.c:1817). The body begins:
IOTSAFE iotsafe;
XMEMCPY(&iotsafe.privkey_id, privkey_id, id_size);
XMEMCPY(&iotsafe.ecdh_keypair_slot, ecdh_keypair_slot, id_size);
XMEMCPY(&iotsafe.peer_pubkey_slot, peer_pubkey_slot, id_size);
XMEMCPY(&iotsafe.peer_cert_slot, peer_cert_slot, id_size);
The four destination fields are byte, word16 or word32, chosen by IOTSAFE_ID_SIZE in wolfssl/wolfcrypt/port/iotsafe/iotsafe.h:79-104. The default is 1.
- The application calls
wolfSSL_iotsafe_on_ex()withid_sizeof up to 65535. - The function declares the
IOTSAFEstruct on the stack, about 12 bytes on the default build (wolfcrypt/src/port/iotsafe/iotsafe.c:1821). - Four copies of
id_sizebytes follow with no bound (wolfcrypt/src/port/iotsafe/iotsafe.c:1822-1825). The last copy starts at field offset 7, so anid_sizeof about 6 already goes past the default object. - Larger values overwrite the rest of the stack frame. The source side of each copy reads
id_sizebytes from a buffer the caller sized for a slot id.
The documented id_size range of 2 to 4 (doc/dox_comments/header_files/iotsafe.h:39) stays within sibling fields and padding on typical ABIs and does not leave the object. Only larger values do.
The stored length has no later use. The TLV builders always emit exactly IOTSAFE_ID_SIZE bytes (wolfcrypt/src/port/iotsafe/iotsafe.c:1336, wolfcrypt/src/port/iotsafe/iotsafe.c:1257). For comparison, iotsafe_getrandom() in the same file validates its size before use (wolfcrypt/src/port/iotsafe/iotsafe.c:613).
Reachability and Preconditions
- Build with the IoT-Safe port and
HAVE_PK_CALLBACKS. - The embedding application calls
wolfSSL_iotsafe_on_ex()withid_sizegreater thanIOTSAFE_ID_SIZE. - The only in-tree caller,
wolfSSL_iotsafe_on(), passes 1 (wolfcrypt/src/port/iotsafe/iotsafe.c:1848-1849). The shipped example passesIOTSAFE_ID_SIZE(IDE/iotsafe/memory-tls.c:334-335). Both are safe on every build configuration. - No attacker-reachable path exists in the code as tested. No remote input reaches the parameter.
Impact
An application that passes an oversized id_size corrupts its own stack frame and reads past its own buffers. The effect is on the integrity and availability of that application. Library-held confidential data is not exposed through this path. This is an API hardening defect: the length parameter is accepted without a check that the storage it is copied into can hold it.
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 validate id_size before the copies. The simplest form rejects any value other than IOTSAFE_ID_SIZE at function entry with BAD_FUNC_ARG. The alternative is to store the four ids as fixed byte arrays wide enough for the largest supported id and bound each copy by the size of its field. Until then, applications should pass IOTSAFE_ID_SIZE as id_size, as the in-tree example does.
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.