Repository navigation
Conversation
Address review feedback from vszakats on PR libssh2#1953: - CMakeLists.txt: refactor new targets into foreach loop, drop trailing blank line - ssh2_sftp_packet_fuzzer.cc: remove unused errno.h header - ssh2_knownhost_fuzzer.cc: remove unused stdlib.h and string.h headers Adds two new libFuzzer harnesses: - ssh2_sftp_packet_fuzzer: feeds valid SSH banner + fuzz payload to cover binary packet transport layer and SFTP packet dispatcher - ssh2_knownhost_fuzzer: exercises libssh2_knownhost_readline() parser
0555785 to
1691e8d
Compare
|
Thanks for the review @vszakats! I've addressed all the feedback:
The |
|
Fuzzer is pointing to an issue in knownhosts: @XananasX7 care to patch this issue? This report may be related: #2012 |
|
Thanks for tracking down and sharing the ASAN report @vszakats! I've pushed a fix for the heap-buffer-overflow in Root cause: The loop that seeks the trailing newline used while(len && *cp && (*cp != '\n')) { cp++; len--; }
if(*cp == '\n') keylen--;When Fix:
This is the same issue referenced in #2012. |
|
Thanks for the fix patch! Could you rebase on master to resolve the conflicts? There is also this patch in the works, addressing what looks like the same issue: #2053 |
Address review feedback from vszakats on PR libssh2#1953: - CMakeLists.txt: refactor new targets into foreach loop, drop trailing blank line - ssh2_sftp_packet_fuzzer.cc: remove unused errno.h header - ssh2_knownhost_fuzzer.cc: remove unused stdlib.h and string.h headers Adds two new libFuzzer harnesses: - ssh2_sftp_packet_fuzzer: feeds valid SSH banner + fuzz payload to cover binary packet transport layer and SFTP packet dispatcher - ssh2_knownhost_fuzzer: exercises libssh2_knownhost_readline() parser
1f671fd to
b1c35ac
Compare
| if(entry) { | ||
| if(entry->comment) | ||
| SSH2_FREE(session, entry->comment); | ||
| LIBSSH2_FREE(session, entry->comment); |
There was a problem hiding this comment.
Please rebase on the latest master. This is patch is currently undoing many changes done there in recent weeks.
|
Fixed the compile error — the ASAN-fix commit was using the old |
Address review feedback from vszakats on PR libssh2#1953: - CMakeLists.txt: refactor new targets into foreach loop, drop trailing blank line - ssh2_sftp_packet_fuzzer.cc: remove unused errno.h header - ssh2_knownhost_fuzzer.cc: remove unused stdlib.h and string.h headers Adds two new libFuzzer harnesses: - ssh2_sftp_packet_fuzzer: feeds valid SSH banner + fuzz payload to cover binary packet transport layer and SFTP packet dispatcher - ssh2_knownhost_fuzzer: exercises libssh2_knownhost_readline() parser
Delete redundant includes.
Delete redundant includes.
delete newline
When the input line is not NUL-terminated (as documented), the parse
loop that seeks the trailing newline used '*cp &&' as a stop condition:
while(len && *cp && *cp != '\n') { cp++; len--; }
if(*cp == '\n') keylen--;
Two problems:
1. '*cp &&' reads one byte past the end of the buffer when 'len' hits
zero, because the caller-supplied buffer has no NUL sentinel.
OSS-Fuzz detected this as a heap-buffer-overflow via the
ssh2_knownhost_fuzzer harness (AddressSanitizer).
2. 'if(*cp == '\n')' after the loop does not guard on 'len > 0'.
When the loop exits because len reached zero, cp is one byte past
the end of the buffer, so this is a second out-of-bounds read.
Fix both:
- Drop the redundant '*cp &&' guard; 'len' already stops the loop
before the buffer is overrun.
- Gate the newline-strip on 'if(len && ...)' so it only fires when
cp is still inside the buffer.
Related: libssh2#2012
Signed-off-by: El Mehdi Abenhazou <[email protected]>
a23d8f9 to
fe866c6
Compare
|
Fixed the build failures by rebasing onto upstream master and replacing the broken knownhost commits with a clean, minimal fix. Root cause: The earlier commits (b1c35ac + a23d8f9) used the old-style Fix applied: Rather than patching all the API renames (which were cosmetic churn on top of the actual fix), the branch has been rebased onto upstream master with a clean, minimal commit that applies only the actual security fix to the current upstream knownhost.c: - while(len && *cp && *cp != '\n') {
+ while(len && *cp != '\n') {
cp++;
len--;
}
- /* null-terminate where the newline is */
- if(*cp == '\n')
+ /* strip trailing newline, if any */
+ if(len && *cp == '\n')
keylen--; /* do not include this in the count */The fuzzer harness commits (tests/ossfuzz/ssh2_sftp_packet_fuzzer.cc + ssh2_knownhost_fuzzer.cc) are kept intact. The security fix content is fully preserved. CI should now pass. |
checksrc requires if(x)/if(!x) instead of if(x != 0)/if(x == 0). Fix all four violations across ssh2_sftp_packet_fuzzer.cc and ssh2_knownhost_fuzzer.cc.
|
Fixed the |
|
The fuzzer keeps failing in knownhosts/readline. Perhaps drop the local knownhost patch in favor of the fix merged to master?: bf33b94 |
## Summary Adds two new libFuzzer harnesses targeting previously uncovered parsing paths in libssh2. ### New harness: `tests/ossfuzz/ssh2_sftp_packet_fuzzer.cc` Complements the existing `ssh2_client_fuzzer` by prepending a valid `SSH-2.0` server banner so that `banner_receive()` in `session.c` always completes and the code enters the SSH binary-packet transport layer with fuzz-controlled payload bytes. **Coverage added:** - Server banner/version string parsing (`session.c:banner_receive`) - SSH binary packet transport reader (`transport.c:_libssh2_transport_read`) - SSH message type dispatch (`packet.c:_libssh2_packet_add`) including: - `SSH_MSG_DISCONNECT` string parsing - `SSH_MSG_DEBUG` and `SSH_MSG_IGNORE` handlers - `SSH_MSG_EXT_INFO` extension negotiation - `SSH_MSG_GLOBAL_REQUEST` parsing - `SSH_MSG_CHANNEL_*` family (DATA, EOF, CLOSE, REQUEST, OPEN, WINDOW_ADJUST) - `SSH_MSG_USERAUTH_BANNER` string extraction - `SSH_MSG_KEXINIT` algorithm name-list parser in `kex.c` ### New harness: `tests/ossfuzz/ssh2_knownhost_fuzzer.cc` Exercises `libssh2_knownhost_readline()` which parses lines from an OpenSSH `known_hosts` file. Uses only the public API; no network access required. **Coverage added:** - Plain and comma-separated hostname parsing (`knownhost.c:oldstyle_hostline`) - Hashed hostname parsing (`knownhost.c:hashed_hostline`, `|1|salt|hash` format) - Key-type token recognition and base64 public-key blob decoding (`misc.c:_libssh2_base64_decode`) - HMAC-SHA1 computation for hashed host verification - `libssh2_knownhost_writeline()` round-trip for successfully parsed entries ### Why this matters The existing `ssh2_client_fuzzer` exercises the client-side SSH handshake but: - Does not bypass banner parsing (banner data and binary-packet data are interleaved in the same fuzz corpus, making it hard for the fuzzer to reach post-banner code) - Does not cover the known-hosts parser (`knownhost_readline`, `hashed_hostline`, base64 decode) — reachable whenever an application calls `libssh2_knownhost_readfile()` Both paths process attacker-controlled data and are reachable in production deployments. ### Build Both harnesses integrate with the existing `tests/ossfuzz/ossfuzz.sh` build system and require no new dependencies. Verified to compile cleanly with OpenSSL + zlib on Debian Bookworm. Closes libssh2#1953
|
With two more fixes, I think knonwhosts tests should be passing the fuzzer/asan job now. Needs a rebase on master. |
Summary
Adds two new libFuzzer harnesses targeting previously uncovered parsing paths in libssh2.
New harness:
tests/ossfuzz/ssh2_sftp_packet_fuzzer.ccComplements the existing
ssh2_client_fuzzerby prepending a validSSH-2.0server banner so thatbanner_receive()insession.calways completes and the code enters the SSH binary-packet transport layer with fuzz-controlled payload bytes.Coverage added:
session.c:banner_receive)transport.c:_libssh2_transport_read)packet.c:_libssh2_packet_add) including:SSH_MSG_DISCONNECTstring parsingSSH_MSG_DEBUGandSSH_MSG_IGNOREhandlersSSH_MSG_EXT_INFOextension negotiationSSH_MSG_GLOBAL_REQUESTparsingSSH_MSG_CHANNEL_*family (DATA, EOF, CLOSE, REQUEST, OPEN, WINDOW_ADJUST)SSH_MSG_USERAUTH_BANNERstring extractionSSH_MSG_KEXINITalgorithm name-list parser inkex.cNew harness:
tests/ossfuzz/ssh2_knownhost_fuzzer.ccExercises
libssh2_knownhost_readline()which parses lines from an OpenSSHknown_hostsfile. Uses only the public API; no network access required.Coverage added:
knownhost.c:oldstyle_hostline)knownhost.c:hashed_hostline,|1|salt|hashformat)misc.c:_libssh2_base64_decode)libssh2_knownhost_writeline()round-trip for successfully parsed entriesWhy this matters
The existing
ssh2_client_fuzzerexercises the client-side SSH handshake but:knownhost_readline,hashed_hostline, base64 decode) — reachable whenever an application callslibssh2_knownhost_readfile()Both paths process attacker-controlled data and are reachable in production deployments.
Build
Both harnesses integrate with the existing
tests/ossfuzz/ossfuzz.shbuild system and require no new dependencies. Verified to compile cleanly with OpenSSL + zlib on Debian Bookworm.