Repository navigation
knownhost: fix out-of-bounds read in knownhost_line_hashed() - #2363
abdul-khaliq-khalid wants to merge 1 commit into
Conversation
| make it scan past the caller-supplied buffer. */ | ||
| if(!keylen) | ||
| return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, | ||
| "Failed to parse known_hosts line (no key)"); |
There was a problem hiding this comment.
I suspect this may fix this address sanitizer report seen a few days ago:
==52==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7bd9a7be4047 at pc 0x55e309b9a76b bp 0x7ffec35dc810 sp 0x7ffec35dbfd8
READ of size 1 at 0x7bd9a7be4047 thread T0
SCARINESS: 12 (1-byte-read-heap-buffer-overflow)
#0 0x55e309b9a76a in strlen /src/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:425:5
#1 0x55e309c686ae in knownhost_add /src/libssh2/src/knownhost.c:143:18
#2 0x55e309c705ff in knownhost_line_legacy /src/libssh2/src/knownhost.c:689:18
#3 0x55e309c6d8b6 in knownhost_line /src/libssh2/src/knownhost.c:870:16
#4 0x55e309c6cbf4 in libssh2_knownhost_readline /src/libssh2/src/knownhost.c:969:10
Since this looks like a separate issue, I'd test and merge this change in
a separate PR, if you don't mind?
(till we decide on #2364, which is breaking and needs a decision.)
There was a problem hiding this comment.
Merging it as-os, to keep it simple and keep the PR message.
There was a problem hiding this comment.
Not at all, that split makes sense to me. I've dropped the keylen hunk here, so this PR is now just the hostlen-bounded salt scan, and the empty-key path is yours to land wherever it fits best.
Agreed that #2364 is the better place for it. Fixing it at the parse layer only papers over the strlen() fallback, and the fallback is the actual hazard since any caller passing an unterminated key with zero keylen hits the same thing. The one thing worth weighing is that the behavior is documented in libssh2.h, so returning LIBSSH2_ERROR_BAD_USE will break callers that rely on it. If that turns out to be too disruptive, the parse-layer check is still available as the narrow fix.
Rebuilt with asan after the trim: the hashed case is silent and valid plain plus hashed lines still return rc=0. checksrc and spacecheck are clean.
There was a problem hiding this comment.
Pull request overview
This PR hardens known_hosts parsing to prevent out-of-bounds reads when the input line buffer is not NUL-terminated, addressing two specific ASAN-detected over-read paths in knownhost_line_hashed() and knownhost_line().
Changes:
- Bound the hashed-host salt separator scan by
hostlento avoid reading past the host span when no|is found before the buffer end. - Reject parsed lines that result in an empty
keylen, preventing a downstreamstrlen()fallback from scanning beyond the caller-provided buffer.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
|
Issue tested and reported by address sanitizer: |
|
Also confirmed (using PR #1953) these two patches fix the two asan issues: |
|
Thanks, merged now. |
knownhost_line_hashed()
…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
…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
The salt separator scan in
knownhost_line_hashed()tested only*pand never consultedhostlen, so a|1|hashed-host line with no closing|walked off the end of a non-NUL-terminated span handed in bylibssh2_knownhost_readline(). With an exact-length, unterminated buffer,"|1|abc ssh-rsa AAAAB3NzaC1yc2E"gives a heap-buffer-overflow read at knownhost.c:725. Bounding the scan byhostlenkeeps it inside the buffer; valid plain and hashed lines still parse unchanged.Narrowed to just this fix. The zero-
keylen/strlen()fallback over-read that was originally in here is a separate issue, left for #2364 (or a standalone patch) to handle at the root.