Skip to content

knownhost: fix out-of-bounds read in knownhost_line_hashed() - #2363

Closed
abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:knownhost-parse-bounds
Closed

abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:knownhost-parse-bounds

Conversation

@abdul-khaliq-khalid

@abdul-khaliq-khalid abdul-khaliq-khalid commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

The salt separator scan in knownhost_line_hashed() tested only *p and never consulted hostlen, so a |1| hashed-host line with no closing | walked off the end of a non-NUL-terminated span handed in by libssh2_knownhost_readline(). With an exact-length, unterminated buffer, "|1|abc ssh-rsa AAAAB3NzaC1yc2E" gives a heap-buffer-overflow read at knownhost.c:725. Bounding the scan by hostlen keeps it inside the buffer; valid plain and hashed lines still parse unchanged.

Narrowed to just this fix. The zero-keylen / strlen() fallback over-read that was originally in here is a separate issue, left for #2364 (or a standalone patch) to handle at the root.

Comment thread src/knownhost.c
make it scan past the caller-supplied buffer. */
if(!keylen)
return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Failed to parse known_hosts line (no key)");

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.

Proposing to fix the root cause of this: #2364

@vszakats vszakats Jul 20, 2026 •

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.

I suspect this may fix this address sanitizer report seen a few days ago:

==52==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7bd9a7be4047 at pc 0x55e309b9a76b bp 0x7ffec35dc810 sp 0x7ffec35dbfd8
READ of size 1 at 0x7bd9a7be4047 thread T0
SCARINESS: 12 (1-byte-read-heap-buffer-overflow)
    #0 0x55e309b9a76a in strlen /src/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:425:5
    #1 0x55e309c686ae in knownhost_add /src/libssh2/src/knownhost.c:143:18
    #2 0x55e309c705ff in knownhost_line_legacy /src/libssh2/src/knownhost.c:689:18
    #3 0x55e309c6d8b6 in knownhost_line /src/libssh2/src/knownhost.c:870:16
    #4 0x55e309c6cbf4 in libssh2_knownhost_readline /src/libssh2/src/knownhost.c:969:10

https://github.com/libssh2/libssh2/actions/runs/29666842385/job/88138609820?pr=2350#step:5:729

Since this looks like a separate issue, I'd test and merge this change in
a separate PR, if you don't mind?

(till we decide on #2364, which is breaking and needs a decision.)

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.

Merging it as-os, to keep it simple and keep the PR message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not at all, that split makes sense to me. I've dropped the keylen hunk here, so this PR is now just the hostlen-bounded salt scan, and the empty-key path is yours to land wherever it fits best.

Agreed that #2364 is the better place for it. Fixing it at the parse layer only papers over the strlen() fallback, and the fallback is the actual hazard since any caller passing an unterminated key with zero keylen hits the same thing. The one thing worth weighing is that the behavior is documented in libssh2.h, so returning LIBSSH2_ERROR_BAD_USE will break callers that rely on it. If that turns out to be too disruptive, the parse-layer check is still available as the narrow fix.

Rebuilt with asan after the trim: the hashed case is silent and valid plain plus hashed lines still return rc=0. checksrc and spacecheck are clean.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens known_hosts parsing to prevent out-of-bounds reads when the input line buffer is not NUL-terminated, addressing two specific ASAN-detected over-read paths in knownhost_line_hashed() and knownhost_line().

Changes:

  • Bound the hashed-host salt separator scan by hostlen to avoid reading past the host span when no | is found before the buffer end.
  • Reject parsed lines that result in an empty keylen, preventing a downstream strlen() fallback from scanning beyond the caller-provided buffer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

vszakats pushed a commit to vszakats/libssh2 that referenced this pull request Jul 20, 2026
libssh2_knownhost_readline() hands the parser a span that is not NUL-terminated, but knownhost_line_hashed() looks for the salt separator with a loop that only tests *p and never consults hostlen, and knownhost_line() can drive keylen to zero on a line whose key field is a single token with no space or newline, at which point knownhost_add() falls back to strlen(key) and copies what it finds into entry->key. Either path walks off the end of the caller buffer: with an exact-length, unterminated buffer ASAN reports a heap-buffer-overflow read at knownhost.c:725 for "|1|abc ssh-rsa AAAAB3NzaC1yc2E" and at knownhost.c:143 for "h AAAAAAAAAAAAAAAAAAAAAA". Bounding the salt scan by hostlen and rejecting an empty key in the parser keeps both reads inside the buffer without touching the public knownhost_add() contract, where a zero keylen legitimately means "NUL-terminated string"; valid plain and hashed lines still parse unchanged.

I went looking for an existing patch first: these are distinct from the readline() off-by-one fixed in fed2d6e and from the comment over-read fixed by libssh2#2053, and no open PR currently touches knownhost.c. The salt loop at line 725 is the only remaining scan in the file that is not length-bounded.

Closes libssh2#2363
@vszakats

Copy link
Copy Markdown
Member

Issue tested and reported by address sanitizer:

==52==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c55dfbe19f4 at pc 0x562f17a35b1b bp 0x7ffc3d389470 sp 0x7ffc3d389468
READ of size 1 at 0x7c55dfbe19f4 thread T0
SCARINESS: 12 (1-byte-read-heap-buffer-overflow)
    #0 0x562f17a35b1a in knownhost_line_hashed /src/libssh2/src/knownhost.c:725:19
    #1 0x562f17a3297d in knownhost_line /src/libssh2/src/knownhost.c:882:16
    #2 0x562f17a31be4 in libssh2_knownhost_readline /src/libssh2/src/knownhost.c:976:10

https://github.com/libssh2/libssh2/actions/runs/29745783525/job/88363260911?pr=2365#step:5:833

@vszakats

vszakats commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

Also confirmed (using PR #1953) these two patches fix the two asan issues:
https://github.com/libssh2/libssh2/actions/runs/29746738006/job/88366489751?pr=2365
(no idea why the fuzz process fails like it does anyway, it seems unrelated.)

@vszakats vszakats closed this in 102e444 Jul 20, 2026
@vszakats

Copy link
Copy Markdown
Member

Thanks, merged now.

@abdul-khaliq-khalid abdul-khaliq-khalid changed the title knownhost: fix two out-of-bounds reads in known_hosts parsing knownhost: fix out-of-bounds read in knownhost_line_hashed() Jul 20, 2026
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 20, 2026
…ownhost_add*()`

To apply the same fix made earlier to `ssh2_base64_encode()`, but this
one level up in the call chain.

This is stricly speaking an incompatible change, but because the
automatic-strlen behavior was never documented for these functions, it
required a valid `keylen` value be passed, and because the fallback
feature is fragile, potentially unsafe, I propose this.

Replace by returning `LIBSSH2_ERROR_BAD_USE` if zero `keylen` is passed.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 libssh2#2363
Follow-up to d1c0e14 libssh2#1641
vszakats added a commit that referenced this pull request Jul 20, 2026
…ownhost_add*()`

To complement/replace similar fixes made earlier to
`ssh2_base64_encode()` and `knownhost_line()`, respectively.

Until libssh2 1.11.1, `libssh2_knownhost_add*()` public APIs had a
special behavior when they received `keylen == 0` argument. They
automatically determined the length of the `key` buffer via `strlen()`.
When passing zero length either on purpose or by accident, it could
result in an OOB read if the buffer's actual length was zero. (Such case
could also happen within libssh2's own code.)

This behavior was not documented on the man page, but the public
`libssh2.h` header mentioned it in comments.

Earlier patch #1641 dropped this behavior for one codepath, which was a
breaking change, arguably for the better. This patch fixes the remaining
codepath and removes this behavior from these functions completely. They
now return `LIBSSH2_ERROR_BAD_USE` when called with a zero `keylen`
argument, with an explanatory error string.

Recommended fix is passing `key` buffer length, or `strlen()` it at the
call site if the buffer is null-terminated, and making sure the buffer
is non-empty.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 #2363
Follow-up to d1c0e14 #1641
Follow-up to 4b991b2

Closes #2364
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.

3 participants