Skip to content

knownhost: fix potential off-by-one read in libssh2_knownhost_readline() - #2335

Closed
vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:kh-off-by-one
Closed

vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:kh-off-by-one

Conversation

@vszakats

@vszakats vszakats commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

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

…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

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 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 cp after the parsing loop when len may be 0 (end-of-buffer), preventing a potential 1-byte over-read.
  • Compute keylen as cp - 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.

@vszakats vszakats closed this in b758dc2 Jul 17, 2026
@vszakats
vszakats deleted the kh-off-by-one branch July 17, 2026 00:05
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.

2 participants