Skip to content

knownhost.c: Modify null-termination check for host key line - #2053

Merged
willco007 merged 1 commit into
masterfrom
willco007-patch-2012
Jun 15, 2026
Merged

willco007 merged 1 commit into
masterfrom
willco007-patch-2012

Conversation

@willco007

@willco007 willco007 commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Don't read in null terminator if the line ends on a single line instead of a new line character. fixes #2012

credit: afldl on GitHub

Don't read in null terminator if the file ends on a single line instead of a new line. fixes #2012

credit: afldl on GitHub
@willco007

Copy link
Copy Markdown
Member Author

Confirmed fix.

@willco007
willco007 merged commit bf33b94 into master Jun 15, 2026
94 checks passed
@willco007
willco007 deleted the willco007-patch-2012 branch June 15, 2026 16:03
vszakats added a commit that referenced this pull request Jul 17, 2026
…ne()`

It happens when the passed knownhosts line is missing a newline. Besides
overreading a byte in the line buffer, it may also miss to include the
last valid character of the key.

knownhosts line example:
```
line 1234<no newline here>
```

"At this point in the code, `len` has been decremented as `cp` advanced.
If the line ends without a newline and `len` reaches 0, `cp` may point
to a `'\0'` terminator outside the original `len` boundary.
Additionally, `keylen` is decremented unconditionally if `*cp` is
`'\0'`, but a null terminator may simply mean end-of-buffer rather than
a character to exclude from the key. This could lead to `keylen` being
off by one in certain cases. A more robust approach would be to
calculate `keylen` directly as `cp - keyp` after the loop, which
naturally excludes the newline."

Reported by GitHub Code Quality

Follow-up to bf33b94 #2053 #2012

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

Heap buffer over-read in knownhost_add() via malformed known_hosts line (CWE-125, CVSS 7.1)

1 participant