Skip to content

ossfuzz: add ssh2_knownhost_fuzzer for known_hosts parser - #2036

Closed
XananasX7 wants to merge 1 commit into
libssh2:masterfrom
XananasX7:ossfuzz/ssh2-knownhost-fuzzer
Closed

XananasX7 wants to merge 1 commit into
libssh2:masterfrom
XananasX7:ossfuzz/ssh2-knownhost-fuzzer

Conversation

@XananasX7

Copy link
Copy Markdown

Summary

The existing ssh2_client_fuzzer covers only the SSH session handshake. The known_hosts parser (src/knownhost.c) has no OSS-Fuzz coverage.

src/knownhost.c handles: base64 decoding of host keys, SHA1 HMAC hostname hashing/matching, OpenSSH known_hosts line parsing (including |1| salt prefix), and binary-search of the host store.

New harness: tests/ossfuzz/ssh2_knownhost_fuzzer.cc

Three operation modes (selected by first fuzz byte):

op Coverage
0 libssh2_knownhost_readline() – parse one known_hosts line
1 Multi-line readline loop + libssh2_knownhost_checkp() hostname lookup
2 Parse + libssh2_knownhost_writefile() write round-trip

@vszakats vszakats left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This needs formatting, dropping the unused/redundant headers,
and adding to the build system. What about mergin the new file into the
existing #1953?

Adds a fuzz harness for libssh2_knownhost_readline() covering the
OpenSSH known_hosts line parser in src/knownhost.c:

- Plain and comma-separated hostname parsing (oldstyle_hostline)
- Hashed hostname parsing (hashed_hostline, |1|salt|hash format)
- Key-type token recognition and base64 public-key blob decoding
- HMAC-SHA1 computation for hashed host verification

The harness uses only the public API, requires no network access,
and is wired into CMakeLists.txt, Makefile.am, and ossfuzz.sh.
@XananasX7
XananasX7 force-pushed the ossfuzz/ssh2-knownhost-fuzzer branch from 09f460c to ceb2ba2 Compare June 13, 2026 16:59
@XananasX7

Copy link
Copy Markdown
Author

Thanks for the feedback @vszakats! Addressed all three points:

  1. Formatting — Rewrote the harness to match the project's coding style (matching ssh2_client_fuzzer.cc): BSD-3 SPDX header, no unused includes, consistent brace style, no extra blank lines.
  2. Dropped redundant headers — Removed <stddef.h>, <stdint.h>, <stdlib.h>, <stdio.h>, <string.h> which are not needed since we only call libssh2_knownhost_* public API. Only "libssh2.h" remains.
  3. Build system wiring — Added ssh2_knownhost_fuzzer to CMakeLists.txt, Makefile.am, and ossfuzz.sh.

Regarding merging into PR #1953: that PR already covers both sftp and knownhost fuzzers. I'm happy to close this one if #1953 is the preferred path, or keep this as a standalone PR for just the knownhost fuzzer — whichever you prefer.

@vszakats

Copy link
Copy Markdown
Member

Thanks for the feedback @vszakats! Addressed all three points:

  1. Formatting — Rewrote the harness to match the project's coding style (matching ssh2_client_fuzzer.cc): BSD-3 SPDX header, no unused includes, consistent brace style, no extra blank lines.
  2. Dropped redundant headers — Removed <stddef.h>, <stdint.h>, <stdlib.h>, <stdio.h>, <string.h> which are not needed since we only call libssh2_knownhost_* public API. Only "libssh2.h" remains.
  3. Build system wiring — Added ssh2_knownhost_fuzzer to CMakeLists.txt, Makefile.am, and ossfuzz.sh.

Regarding merging into PR #1953: that PR already covers both sftp and knownhost fuzzers. I'm happy to close this one if #1953 is the preferred path, or keep this as a standalone PR for just the knownhost fuzzer — whichever you prefer.

Since merging both this and #1953 will conflict due to doing the same/similar
build modifications in parallel, I'd propose moving this file into #1953.

@XananasX7

Copy link
Copy Markdown
Author

Closing as requested by @vszakats — the ssh2_knownhost_fuzzer.cc file and all build system wiring has been moved into PR #1953 (ossfuzz: add ssh2_sftp_packet_fuzzer and ssh2_knownhost_fuzzer), which already touches the same CMakeLists.txt, Makefile.am, and ossfuzz.sh files. Please review the combined harnesses there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants