Repository navigation
Description
GitHub Issue — libssh2 Infinite Loop in PEM Parser (CWE-835)
Title: Infinite Loop in _libssh2_pem_parse_memory() When PEM End Marker Follows Begin Marker Immediately (CWE-835)
CWE
CWE-835: Loop with Unreachable Exit Condition (Infinite Loop)
Affected Versions
libssh2 <= 1.11.1 (all versions with _libssh2_pem_parse_memory() / libssh2_knownhost_readline())
Description
Summary
_libssh2_pem_parse_memory() in pem.c enters an infinite loop when parsing PEM data where the -----END ...----- marker directly follows the -----BEGIN ...----- marker (no base64 data between them). The function also loops infinitely when the PEM data does not contain the end marker at all and readline_memory() returns empty lines after data exhaustion.
Vulnerable Component
- File:
src/pem.c:244-268(_libssh2_pem_parse_memory) - Trigger path:
libssh2_userauth_publickey_frommemory()→_libssh2_pem_parse_memory() - Also affects: Any API that loads PEM private keys (RSA, DSA, EC, PKCS#8)
Root Cause
The function has two loops:
First loop (lines 184-193) — searches for headerbegin. Has if(!*line) break; at line 191, correctly exits on empty line.
Second loop (lines 244-268) — accumulates base64 and searches for headerend. Missing the !*line check. Between the two loops, at line 195, a readline_memory() call consumes the first line after headerbegin. If that line IS the headerend marker, it is consumed but never compared to headerend. The second loop then reads subsequent lines looking for headerend which was already consumed, creating an infinite loop.
// pem.c:195-268
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
return -1; // reads first line after BEGIN — could be the END marker!
}
// ... encrypted key handling skipped ...
*line = '\0';
do {
if(*line) {
// accumulate base64...
}
*line = '\0';
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
ret = -1;
goto out;
}
} while(strcmp(line, headerend) != 0); // BUG: no break on empty lineAdditionally, readline_memory() (line 71-94) always returns 0 (success) and always increments *filedata_offset by 1 (line 92), even after data exhaustion. This means after the input is consumed, every call returns an empty line with the offset incrementing forever. The second loop's strcmp("", headerend) never matches, and without a !*line check, the loop never exits.
Attack Vector
A remote attacker can trigger this by providing a malformed SSH private key file. Applications that load SSH keys from untrusted sources (e.g., SSH clients reading keys from a compromised filesystem, or server applications accepting key uploads) will hang indefinitely.
PoC — 2-byte input triggers infinite loop via public API:
// Craft a PEM file with no base64 between BEGIN and END
const char *pem = "-----BEGIN RSA PRIVATE KEY-----\n"
"-----END RSA PRIVATE KEY-----\n";
// Or simply any data without the END marker that starts with the BEGIN markerImpact
- Denial of Service: Thread/process hangs indefinitely consuming CPU
- No memory corruption: This is a logic bug, not a memory safety issue
- No information leak: No data is exposed
Steps to Reproduce
# Build libssh2 with ASAN
git clone https://github.com/libssh2/libssh2.git && cd libssh2
mkdir build && cd build
cmake .. -DCMAKE_C_COMPILER=clang \
-DCMAKE_C_FLAGS="-g -fsanitize=address -fno-omit-frame-pointer -O1" \
-DCMAKE_BUILD_TYPE=Debug
make -j$(nproc)
# Create PoC — PEM with empty body
printf '-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----\n' > /tmp/poc.pem
# Compile test program
cat > test.c << 'EOF'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libssh2.h>
int main(int argc, char **argv) {
FILE *f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET);
char *buf = malloc(sz);
fread(buf, 1, sz, f); fclose(f);
buf[sz-1] = '\0';
LIBSSH2_SESSION *session = libssh2_session_init();
// This will hang forever:
libssh2_userauth_publickey_frommemory(session, "user", 4,
buf, sz, buf, sz, "");
libssh2_session_free(session);
free(buf);
return 0;
}
EOF
clang -g -fsanitize=address -fno-omit-frame-pointer -O1 test.c \
-I../include src/libssh2.a -lssl -lcrypto -o test
timeout 5 ./test /tmp/poc.pem
# Result: timeout after 5 seconds — process is hung in infinite loopSuggested Fix
Add if(!*line) { ret = -1; goto out; } after the readline_memory call at line 264, matching the pattern in the first loop:
--- a/src/pem.c
+++ b/src/pem.c
@@ -262,6 +262,11 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session,
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
ret = -1;
goto out;
}
+ if(!*line) {
+ ret = -1;
+ goto out;
+ }
} while(strcmp(line, headerend) != 0);Additionally, readline_memory() should detect end-of-data and return a non-zero value instead of always returning 0, to prevent other callers from having the same issue.
Credit
Discovered through ASAN-guided fuzzing with the PathDiff differential fuzzing framework.
References
- CWE-835: https://cwe.mitre.org/data/definitions/835.html
- libssh2: https://github.com/libssh2/libssh2
- Related:
_libssh2_openssh_pem_parse_memory()in the same file has the same pattern
Activity
vszakats commentedon Jun 10, 2026
This looks to have been fixed earlier via:
141b035 #1746
It also doesn't reproduce with current master.
vszakats commentedon Jun 10, 2026
Thanks for reporting, closing this as already fixed. If it (or a variation) still happens
in current master, feel free to reopen or open a new issue.
afldl commentedon Jun 10, 2026
Thanks for the pointer to #1746. That PR did fix the first loop (the one searching for
headerbegin) by adding theif(!*line) breakguard.However, the second loop — the one that accumulates base64 and looks for
headerend— still has the same problem. On current master (src/pem.clines 243-267):No
!*linecheck here.readline_memory()still always returns 0 and incrementsoffunconditionally, so once the input is consumed, every call returns an empty string withoffticking up forever.strcmp("", headerend)never matches.The PoC from the report (
-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----\n) hits this path because the END marker gets consumed by thereadline_memorycall between the two loops (line 194), and the second loop never sees it.Could you re-check with that specific input on master?
vszakats commentedon Jun 11, 2026
It may be due to the report and repro code using OpenSSL, but
OpenSSL builds don't use this code path. Seems to need wincng.
I could replicate with an insolated example.
edit: I also wonder why though
readline_memory()result ischecked, it's always returning success.
vszakats commentedon Jun 11, 2026
@afldl Can you verify/retest with patch #2032?
afldl commentedon Jun 11, 2026
ok
pem: fix `readline_memory()` to detect end of input, not skip chars