Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/knownhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,10 @@ static int knownhost_line_hashed(LIBSSH2_KNOWNHOSTS *hosts,
hostlen -= 3; /* deduct the marker */

/* this is where the salt starts, find the end of it */
for(p = salt; *p && *p != '|'; p++)
for(p = salt; (size_t)(p - salt) < hostlen && *p && *p != '|'; p++)
;

if(*p == '|') {
if((size_t)(p - salt) < hostlen && *p == '|') {
const char *hash = NULL;
size_t saltlen = p - salt;
if(saltlen >= (sizeof(saltbuf) - 1)) /* weird length */
Expand Down Expand Up @@ -863,6 +863,13 @@ static int knownhost_line(LIBSSH2_KNOWNHOSTS *hosts,
break;
}

/* The key is passed on as base64 with its length, and knownhost_add()
falls back to strlen() when the length is zero, so an empty key would
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.


/* Figure out host format */
if(hostlen < 3 || memcmp(host, "|1|", 3))
/* old style plain text: [name]([,][name])*
Expand Down
Loading