Skip to content

knownhost: stop determining keylen if passed as zero in libssh2_knownhost_add*() - #2364

Closed
vszakats wants to merge 8 commits into
libssh2:masterfrom
vszakats:kh-no-autostrlen
Closed

vszakats wants to merge 8 commits into
libssh2:masterfrom
vszakats:kh-no-autostrlen

Conversation

@vszakats

@vszakats vszakats commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

To complement/replace similar fixes made earlier to
ssh2_base64_encode() and knownhost_line(), respectively.

Until libssh2 1.11.1, libssh2_knownhost_add*() public APIs had a
special behavior when they received keylen == 0 argument. They
automatically determined the length of the key buffer via strlen().
When passing zero length either on purpose or by accident, it could
result in an OOB read if the buffer's actual length was zero. (Such case
could also happen within libssh2's own code.)

This behavior was not documented on the man page, but the public
libssh2.h header mentioned it in comments.

Earlier patch #1641 dropped this behavior for one codepath, which was a
breaking change, arguably for the better. This patch fixes the remaining
codepath and removes this behavior from these functions completely. They
now return LIBSSH2_ERROR_BAD_USE when called with a zero keylen
argument, with an explanatory error string.

Recommended fix is passing key buffer length, or strlen() it at the
call site if the buffer is null-terminated, and making sure the buffer
is non-empty.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 #2363
Follow-up to d1c0e14 #1641
Follow-up to 4b991b2


  • Turns out this behavior was documented in libssh2.h. Reconsider?

https://codesearch.debian.net/search?q=libssh2_knownhost_add&literal=1
https://github.com/search?q=libssh2_knownhost_add+language%3AC&type=code

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 hardens the knownhost add APIs by rejecting keylen == 0 instead of falling back to computing the key length (previously via strlen() in some cases), aligning behavior with safer length-explicit handling higher up the call chain.

Changes:

  • Reject zero keylen in the internal knownhost_add() helper by returning LIBSSH2_ERROR_BAD_USE.
  • Remove the implicit “determine key length” fallback behavior when keylen is not provided.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/knownhost.c Resolved
…ownhost_add*()`

To apply the same fix made earlier to `ssh2_base64_encode()`, but this
one level up in the call chain.

This is stricly speaking an incompatible change, but because the
automatic-strlen behavior was never documented for these functions, it
required a valid `keylen` value be passed, and because the fallback
feature is fragile, potentially unsafe, I propose this.

Replace by returning `LIBSSH2_ERROR_BAD_USE` if zero `keylen` is passed.

Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617

Follow-up to 102e444 libssh2#2363
Follow-up to d1c0e14 libssh2#1641

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/knownhost.c:137

  • This change makes keylen == 0 an unconditional error, but the public API documentation still says it may be omitted when the key is a NUL-terminated base64 string (e.g., include/libssh2.h around libssh2_knownhost_add*() and the comment blocks above libssh2_knownhost_add*() in this file). The docs should be updated in the same PR to avoid a documented/implemented mismatch (and because this is an intentional breaking change, it should also be called out in user-facing docs/release notes).
    if(!hosts || !host || !key || !keylen)

Comment thread src/knownhost.c Resolved
Comment thread src/knownhost.c Resolved
Copilot AI review requested due to automatic review settings July 20, 2026 19:05

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/knownhost.c:143

  • This change intentionally makes keylen==0 an error, but the public API contract in include/libssh2.h still documents that keylen “may be omitted (zero)” for null-terminated base64 keys (see include/libssh2.h:1097-1099 and :1159-1160). Please update the public header docs (and any other API docs/release notes as appropriate) in the same PR to avoid a documented/implemented mismatch for libssh2_knownhost_add*().
    /* Set an error message because this was a breaking change. */
    if(!keylen)
        return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
                        "Known-host key length required");

src/knownhost.c:868

  • Removing the explicit keylen==0 check here means a malformed known_hosts line with a long comment but an empty key can now fail later with LIBSSH2_ERROR_BAD_USE / "Known-host key length required" (from knownhost_add()), which is a misleading error for a parse failure. Add a dedicated parse error when the parsed key span is empty.

    /* Figure out host format */

Comment thread src/knownhost.c Resolved
Copilot AI review requested due to automatic review settings July 20, 2026 19:12

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/knownhost.c:144

  • This change makes keylen == 0 an error, but the public API is still documented as allowing keylen to be omitted (0) for NUL-terminated base64 keys (e.g., include/libssh2.h and the comment blocks above libssh2_knownhost_add*() in this file). That creates a documented/implemented mismatch, and it also contradicts the PR description’s claim that the docs “always required a valid keylen”. Please update the public header (and any in-tree API docs/release notes as appropriate) to reflect the new required keylen contract, or adjust the code/PR description if the behavior should remain supported.
    /* keylen == 0 previously fell back to strlen(key); require explicit
       length. */
    if(!keylen)
        return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
                        "Known-host key length required");

Comment thread src/knownhost.c Resolved
Copilot AI review requested due to automatic review settings July 20, 2026 19:58

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/knownhost.c:144

  • This change intentionally makes keylen == 0 a hard error, but the public API contract is still documented as allowing omitted/zero keylen for NUL-terminated base64 keys (see the comment blocks above libssh2_knownhost_add()/libssh2_knownhost_addc() in this file and in include/libssh2.h). As-is, the implementation and documented contract diverge, which will be confusing for callers and downstream packagers.

To keep the API behavior change safe and explicit, please update the public docs in the same PR (at minimum: include/libssh2.h and the header comments in this file) to remove the “keylen may be omitted (zero)” language and document the new LIBSSH2_ERROR_BAD_USE behavior.

    /* keylen == 0 previously fell back to strlen(key) for base64-encoded keys.
       Require explicit length now. */
    if(!keylen)
        return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
                        "Known-host key length required");

Copilot AI review requested due to automatic review settings July 20, 2026 21:00

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

src/knownhost.c:142

  • The new historical note is a bit misleading: previously keylen == 0 only fell back to strlen(key) when the key was base64-encoded (i.e., when LIBSSH2_KNOWNHOST_KEYENC_BASE64 was set). Tightening the wording avoids future confusion about what behavior actually changed.
    /* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
       Require explicit length now. */
    if(!keylen)

src/knownhost.c:144

  • Now that keylen == 0 is rejected, the public API documentation is out of sync: both include/libssh2.h and the API doc blocks in this file still say that keylen may be omitted (zero) for NUL-terminated base64 keys (e.g., include/libssh2.h around the libssh2_knownhost_add*() docs, and src/knownhost.c around the libssh2_knownhost_addc() docs). Those comments should be updated in the same PR to avoid a documented/implemented mismatch for API consumers.
    if(!keylen)
        return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
                        "Known-host key length required");

Copilot AI review requested due to automatic review settings July 20, 2026 21:09

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/knownhost.c:141

  • The new comment says keylen == 0 fell back to strlen(key) unconditionally, but the removed code shows that fallback only applied when LIBSSH2_KNOWNHOST_KEYENC_BASE64 was set. Tightening the wording avoids misleading future maintenance about the actual historical behavior.
    /* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
       Require explicit length now. */

Comment thread include/libssh2.h Resolved
Copilot AI review requested due to automatic review settings July 20, 2026 21:19

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/knownhost.c:141

  • The new comment implies keylen == 0 always fell back to strlen(key) historically, but the removed codepath shows the fallback only applied when typemask included LIBSSH2_KNOWNHOST_KEYENC_BASE64. Tightening the comment to match the prior behavior will avoid misleading future maintenance.
    /* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
       Require explicit length now. */

@vszakats vszakats closed this in 7b18532 Jul 20, 2026
@vszakats
vszakats deleted the kh-no-autostrlen branch July 20, 2026 21:38
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