Repository navigation
Conversation
…ne()` It happens when the last line of a knownhosts file 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 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
There was a problem hiding this comment.
Pull request overview
This PR fixes an out-of-bounds read risk in libssh2_knownhost_readline() when parsing an in-memory known_hosts line that does not end with a newline (and where len may reach 0 exactly as the parser advances).
Changes:
- Avoid dereferencing
cpafter the parsing loop whenlenmay be 0 (end-of-buffer), preventing a potential 1-byte over-read. - Compute
keylenascp - keyp, which correctly captures the parsed key span and naturally excludes a trailing newline when present.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
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:
"At this point in the code,
lenhas been decremented ascpadvanced.If the line ends without a newline and
lenreaches 0,cpmay pointto a
'\0'terminator outside the originallenboundary.Additionally,
keylenis decremented unconditionally if*cpis'\0', but a null terminator may simply mean end-of-buffer rather thana character to exclude from the key. This could lead to
keylenbeingoff by one in certain cases. A more robust approach would be to
calculate
keylendirectly ascp - keypafter the loop, whichnaturally excludes the newline."
Reported by GitHub Code Quality
Follow-up to bf33b94 #2053 #2012