wolfSSL — One-Byte Heap Out-of-Bounds Write in PEM Header Scanner
The PEM reader behind PEM_read_bio_X509_CRL and PEM_X509_INFO_read_bio reads one byte after the header delimiter without checking the buffer bound. When the delimiter ends on the last byte of the buffer, which a streaming BIO allows, that byte is written one position past the heap allocation.
- Advisory
- BYTERAY-2026-0311
- CWE
- CWE-787
- CVSS
- 8.1 (CVSS:3.1/AV:N/AC:H/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 (tested at master commit d95483f), builds with the OpenSSL compatibility layer (OPENSSL_ALL or OPENSSL_EXTRA) and WOLFSSL_PEM_TO_DER where PEM_read_bio_X509_CRL (with HAVE_CRL) or PEM_X509_INFO_read_bio reads from a streaming BIO
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio() is the internal reader behind the OpenSSL compatibility functions PEM_read_bio_X509_CRL and PEM_X509_INFO_read_bio. It allocates a buffer sized from the BIO's reported length and reads the PEM text into it one byte at a time while it looks for the header and footer lines.
The main read loop stops at the end of the buffer. One extra read inside the loop does not. After the closing dashes of the header line are found, the function reads the following newline into the next position without checking that the position is still inside the buffer. If the dashes end on the last byte of the buffer, the newline read stores one byte past the allocation.
This can happen when the BIO is a stream that keeps supplying data beyond the buffer size, such as a socket, an SSL BIO or a pipe. The byte written comes from the stream. Memory BIOs and regular files are not affected. A default ./configure build does not include the OpenSSL compatibility layer.
Affected Products and Versions
- Component:
src/x509.c,wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio(), reached fromwolfSSL_PEM_read_bio_X509_CRL()and from thePEM_X509_INFO_read_bioreader. - Required build: the OpenSSL compatibility layer (
OPENSSL_ALLorOPENSSL_EXTRA) andWOLFSSL_PEM_TO_DER, which is on by default. ThePEM_read_bio_X509_CRLentry point also needsHAVE_CRL(src/x509.c:13969). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not compile these readers. On builds that do, only streaming BIOs are affected; for memory BIOs and seekable files the length probe returns the exact size. - Other releases were not checked.
Technical Details
The function takes the length from wolfSSL_BIO_get_len() (src/x509.c:14233). A result of 0 is treated as a stream of unknown size and replaced by the fixed cap MAX_BIO_READ_BUFFER (src/x509.c:14240-14242). The buffer pem is then allocated with exactly l bytes (src/x509.c:14250).
The read loop is bounded by i < l (src/x509.c:14261). The newline read inside it is not (src/x509.c:14273):
while (i < l && wolfSSL_BIO_read(bio, &pem[i], 1) == 1) {
i++;
...
if (headerEnd) {
headerEnd += XSTR_SIZEOF("-----");
/* Read in the newline */
if (wolfSSL_BIO_read(bio, &pem[i], 1) != 1) {
The loop condition is checked before i is incremented. When the byte just read is the last one in the buffer, i becomes l. If that byte also completes the header's closing dashes, headerEnd is set and the newline read targets pem[l], one byte past the allocation. The header search accepts a match that ends exactly at the edge of the searched window (wolfcrypt/src/wc_port.c:5117).
How the data reaches the write:
- The application calls
PEM_read_bio_X509_CRLorPEM_X509_INFO_read_bioon a BIO. These reach the reader atsrc/x509.c:13984andsrc/x509.c:14460. - The length probe returns 0 for a socket BIO (
src/bio.c:1368-1369) and for a pipe or FIFO file BIO (src/ssl_misc.c:238-243,src/bio.c:1984-1989). For an SSL BIO it returns only the bytes buffered at that moment (src/bio.c:1348-1351). lis set from that result andpemis allocated withlbytes, while the stream can supply more.- The bounded loop fills the buffer. The header's closing dashes are resolved at
src/x509.c:14267-14271. - The unguarded read at
src/x509.c:14273stores the next stream byte atpem[i]. Withiequal tolthis is outside the buffer. - The function then fails with
ASN_NO_PEM_HEADER(src/x509.c:14311). The write has already happened.
loadX509orX509REQFromPemBio() (src/x509.c:13625) handles the same situation correctly. It grows the buffer for streaming sources and checks the bound on every indexed read.
Reachability and Preconditions
- Build:
OPENSSL_ALLorOPENSSL_EXTRA, withWOLFSSL_PEM_TO_DER;HAVE_CRLfor the CRL entry point. - Runtime: the application passes a streaming BIO (socket, SSL, pipe, FIFO or standard input) to
PEM_read_bio_X509_CRLorPEM_X509_INFO_read_bio. This is ordinary use of these functions in code ported from OpenSSL. - Attacker position: the party that supplies the PEM stream, for example a network peer that sends a CRL or a certificate bundle.
- Authentication state: none required by the reader itself.
- The header delimiter has to end exactly at the buffer edge. The sender therefore needs to know the effective buffer size: the fixed cap for sources that report length 0, or the buffered byte count for an SSL BIO.
Impact
A one-byte heap out-of-bounds write. The byte is the next one in the stream, so the sender chooses its value. It lands directly after the allocation, where the allocator may keep metadata for the neighbouring chunk.
A crash of the parsing process is the expected direct result. Further use of the corruption depends on the allocator and the heap layout. The write happens before the function returns its error, so checking the return value does not avoid it.
ByteRay reproduced this issue in an instrumented test build.
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 apply the loop's i < l bound to the newline read, and to treat a header delimiter that ends at the buffer edge as incomplete instead of reading another byte. Adopting the growing-buffer approach of loadX509orX509REQFromPemBio() for streaming sources addresses the underlying sizing problem. Other indexed writes into pem in the function should be checked for the same bound.
Workaround: read the PEM data into memory first and pass a memory BIO to these functions. The length probe is then exact and the extra read stays inside the buffer.
Timeline
- 2026-09-27 — Reported to wolfSSL.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.