Repository navigation
knownhost.c: Modify null-termination check for host key line - #2053
Merged
Merged
Conversation
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
Member
Author
|
Confirmed fix. |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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