wolfSSL — Missing Upper Bound on Port in wolfSSL_BIO_set_conn_port
wolfSSL_BIO_set_conn_port rejects a port of zero or below but has no upper bound. A value above 65535 is truncated to 16 bits when stored, so "70000" becomes 4464 and "65536" becomes 0, and the function returns success. A later connect uses the truncated port.
- Advisory
- BYTERAY-2026-0500
- CWE
- CWE-20
- 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; the connect path also needs HAVE_HTTP_CLIENT
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
wolfSSL_BIO_set_conn_port() is an OpenSSL compatibility helper. It converts a port string from the application into a number and stores it in the BIO for the later connect.
The function checks that the number is not zero and not negative. It does not check that the number is at most 65535. The port field is 16 bits wide, so a larger value keeps only its low 16 bits: "65536" is stored as 0 and "70000" as 4464. The function reports success in both cases.
The application is told the requested port was set when a different one was stored. On connect it either reaches a different service or fails without an obvious cause. The port string comes from the embedding application. No path inside the library feeds remote data into it. The code is part of the OpenSSL compatibility layer, which a default build does not enable.
Affected Products and Versions
- Component:
src/bio.c,wolfSSL_BIO_set_conn_port(), with the sink inwolfSSL_BIO_do_connect()andwolfIO_TcpConnect(). - Required build:
OPENSSL_EXTRA(the block atsrc/bio.c:2404-3481). The connect atsrc/bio.c:2703also requiresHAVE_HTTP_CLIENT. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not defineOPENSSL_EXTRAand does not include this code. Other releases were not checked.
Technical Details
The parse, the guard and the store are at src/bio.c:2661-2676:
p = XATOI(port);
if (!p || p < 0) {
WOLFSSL_MSG("Port parsing error");
return WOLFSSL_FAILURE;
}
...
b->port = (word16)p;
return WOLFSSL_SUCCESS;
XATOI is atoi() (wolfssl/wolfcrypt/types.h:1307). The guard is one-sided. It has no p > 65535 term.
- The application passes a port string to
wolfSSL_BIO_set_conn_port()(src/bio.c:2651). NULL arguments are rejected (src/bio.c:2656). atoi()parses the string (src/bio.c:2661).- The guard at
src/bio.c:2662returnsWOLFSSL_FAILUREfor"0"and for negative values. A value above 65535 passes. - The cast at
src/bio.c:2675keeps the low 16 bits and the function returnsWOLFSSL_SUCCESS. wolfSSL_BIO_do_connect()passesb->porttowolfIO_TcpConnect()(src/bio.c:2703), whose port parameter is aword16(src/wolfio.c:1434). The truncation cannot be detected there.
The outcomes by input: "0" fails and leaves the port unchanged. "65536" succeeds with port 0, and the connect fails later. "70000" succeeds with port 4464, and the connect goes to that port. wolfSSL_BIO_new_connect() in the same file checks both bounds before the same cast (src/bio.c:2584-2587). The difference between the two functions is an omission here.
Reachability and Preconditions
- Build with
OPENSSL_EXTRA, andHAVE_HTTP_CLIENTfor the connect. - The application passes a port string above 65535 to
wolfSSL_BIO_set_conn_port(), directly or forwarded from configuration or user input without validation, and later callswolfSSL_BIO_do_connect()on the BIO. - The library itself has no production caller. The references are the public prototype (
wolfssl/ssl.h:2269), theBIO_set_conn_portmacro (wolfssl/openssl/ssl.h:960) and the test suite. - If the application takes the port from untrusted configuration or user input, that source decides which port is contacted.
Impact
The application connects to a port it did not ask for while being told the requested port was honoured. The result is interaction with the wrong service and misleading logs, or a late connect failure. It becomes a way to redirect a connection only when the port string originates outside the application. There is no memory-safety impact.
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 complete the range check before the cast, so that any value outside 1 to 65535 returns WOLFSSL_FAILURE. That matches wolfSSL_BIO_new_connect(). A range-checked parse in place of atoi() would reject oversized input at parse time. Until then, applications should validate the port string themselves before calling wolfSSL_BIO_set_conn_port().
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.