Skip to content

Improve known_hosts parsing and harden _libssh2_base64_encode() - #1641

Merged
willco007 merged 3 commits into
masterfrom
willco007-base64
Aug 25, 2025
Merged

willco007 merged 3 commits into
masterfrom
willco007-base64

Conversation

@willco007

Copy link
Copy Markdown
Member

Notes:
Added additional base64 decode validation when parsing known_hosts and no longer assume what is going into _libssh2_base64_encode() is null terminated.

Reported by:
Dhiraj Mishra [email protected]

Credit:
Will Cosgrove

Reviewed by:
Michael Buckley

Return errors if base64 decode fails.
Must now include a insize parameter to encode the data.
@vszakats

Copy link
Copy Markdown
Member

One of libgcrypt fallouts was a flaky one, the other one is stable.
It appeared around 2 weeks ago for no apparent reason. Ref: #1642
for a bit more details.

I decided to delete the problematic i386 CI job. Rebasing this PR
should make it green, and it seems safe to merge either way.

@willco007

Copy link
Copy Markdown
Member Author

I'm just going to merge this to integrate the latest changes, thanks for fixing the build system yet again @vszakats!

@willco007
willco007 merged commit d1c0e14 into master Aug 25, 2025
@willco007
willco007 deleted the willco007-base64 branch August 25, 2025 16:39
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 20, 2026
…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
vszakats added a commit that referenced this pull request Jul 20, 2026
…ownhost_add*()`

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

Closes #2364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants