Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens ssh2_base64_decode() by ensuring the decoded output buffer is NUL-terminated, preventing out-of-bounds reads when downstream code treats decoded fields as C strings (e.g., strcmp() in OpenSSH key parsing).
Changes:
- Allocate one extra byte for the decoded buffer (
src_len + 1). - Write a terminating
'\0'atdata[len]after decoding.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
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/misc.c:430
ssh2_base64_decode()allocatessrc_lenbytes but now writes a NUL atdata[len]. For malformed inputs wheresrc_lencan be 0 (e.g., empty base64 field), this can become an out-of-bounds write if the allocator returns a non-NULL pointer for size 0. Ensure the buffer has room for the terminator (either allocatesrc_len + 1up-front, or grow tolen + 1before writing).
(*data)[len] = '\0';
ssh2_base64_decode()ssh2_base64_decode()
0975153 to
6551363
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (10)
src/pem.c:475
ssh2_get_chars()does not NUL-terminate the returned field (src/misc.c:929-944), sostrcmp()is not a safe/effective way to compare the KDF name. Use a length-bounded compare (and avoid reading beyond thekdfnamefield).
if((kdfname_len != sizeof("none") - 1 || strcmp(kdfname, "none")) &&
(kdfname_len != sizeof("bcrypt") - 1 || strcmp(kdfname, "bcrypt"))) {
src/pem.c:482
- This
strcmp()relies onkdfname/ciphernamebeing NUL-terminated, butssh2_get_chars()returns non-terminated data (src/misc.c:929-944). Please compare using the recorded lengths instead of walking into subsequent fields.
if(kdfname_len == sizeof("none") - 1 && !strcmp(kdfname, "none") &&
(ciphername_len != sizeof("none") - 1 || strcmp(ciphername, "none"))) {
src/openssl.c:694
namecomes fromssh2_get_chars()(length-delimited, not NUL-terminated). Even withname_len == 19, usingstrcmp()can read beyond the field into subsequent bytes. Use fixed-lengthmemcmp()comparisons instead.
if(!strcmp(name, "ecdsa-sha2-nistp256"))
type = SSH2_EC_CURVE_NISTP256;
else if(!strcmp(name, "ecdsa-sha2-nistp384"))
type = SSH2_EC_CURVE_NISTP384;
else if(!strcmp(name, "ecdsa-sha2-nistp521"))
src/openssl.c:3390
bufis not guaranteed to be NUL-terminated (returned byssh2_get_chars()), sostrcmp()can read beyond the field. Prefermemcmp()with the known literal size.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf) &&
(!want_method || !strcmp("[email protected]", want_method)))
src/openssl.c:3400
buforiginates fromssh2_get_chars()(not NUL-terminated). Usingstrcmp()here can read past the field and into later decrypted bytes. Usememcmp()with the known length.
if(buf_len == sizeof("ssh-rsa") - 1 &&
!strcmp("ssh-rsa", buf) &&
(!want_method || !strcmp("ssh-rsa", want_method)))
src/openssl.c:3409
bufcomes from a length-delimited field (ssh2_get_chars()), sostrcmp()is not safe unless the field is explicitly terminated. Use a boundedmemcmp()instead.
if(buf_len == sizeof("ssh-dss") - 1 &&
!strcmp("ssh-dss", buf) &&
(!want_method || !strcmp("ssh-dss", want_method)))
src/openssl.c:3417
bufis not guaranteed to be NUL-terminated afterssh2_get_chars(). Usingstrcmp()here can read beyond the key-type field; use a bounded compare.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf))
src/openssl.c:3494
bufis a non-NUL-terminated field fromssh2_get_chars(). Switch the key-type comparison tomemcmp()to avoid reading beyond the field boundary.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf)) {
src/openssl.c:3507
bufis a length-delimited (not NUL-terminated) field fromssh2_get_chars(). Usingstrcmp()can read past the field; prefermemcmp()against the literal size.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf)) {
src/mbedtls.c:1008
nameis obtained viassh2_get_chars()and is not guaranteed to be NUL-terminated. Even withname_len == 19,strcmp()can read beyond the field; use fixed-lengthmemcmp()comparisons.
if(!strcmp(name, "ecdsa-sha2-nistp256"))
type = SSH2_EC_CURVE_NISTP256;
else if(!strcmp(name, "ecdsa-sha2-nistp384"))
type = SSH2_EC_CURVE_NISTP384;
else if(!strcmp(name, "ecdsa-sha2-nistp521"))
ssh2_base64_decode()There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (12)
src/pem.c:469
- The comparisons against ciphername/kdfname still use strcmp() on data returned by ssh2_get_chars(). ssh2_get_chars() does not NUL-terminate (it returns a pointer into the parsed buffer), so strcmp() will read past the parsed string boundary into subsequent fields and can still read past the allocation unless the base64-decoded blob is explicitly NUL-terminated. Use a length-bounded comparison (memcmp/strncmp) instead of strcmp().
if((!passphrase || strlen(passphrase) == 0) &&
(ciphername_len != sizeof("none") - 1 || strcmp(ciphername, "none"))) {
/* passphrase required */
src/pem.c:476
- These KDF algorithm checks use strcmp() on length-prefixed data (ssh2_get_chars()). Even with the added length checks, strcmp() can read beyond the parsed field boundary, and correctness depends on an incidental NUL byte in subsequent data. Prefer length-bounded comparisons (memcmp/strncmp) when validating SSH "string" fields.
if((kdfname_len != sizeof("none") - 1 || strcmp(kdfname, "none")) &&
(kdfname_len != sizeof("bcrypt") - 1 || strcmp(kdfname, "bcrypt"))) {
ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
src/pem.c:483
- This validation mixes a length check with strcmp() on kdfname/ciphername extracted via ssh2_get_chars(), which does not NUL-terminate the parsed field. Replace strcmp() with a bounded compare to avoid reading beyond the field boundary and to keep the check independent of NUL termination of the underlying blob.
if(kdfname_len == sizeof("none") - 1 && !strcmp(kdfname, "none") &&
(ciphername_len != sizeof("none") - 1 || strcmp(ciphername, "none"))) {
ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "invalid format");
src/openssl.c:695
- ossl_ecdsa_curve_type_from_name() now receives a length, but it still uses strcmp() on the non-NUL-terminated SSH "string" returned by ssh2_get_chars(). strcmp() can read past the parsed field boundary; use a bounded comparison (memcmp/strncmp) keyed off name_len instead.
if(!name || name_len != 19)
return -1;
if(!strcmp(name, "ecdsa-sha2-nistp256"))
type = SSH2_EC_CURVE_NISTP256;
else if(!strcmp(name, "ecdsa-sha2-nistp384"))
type = SSH2_EC_CURVE_NISTP384;
else if(!strcmp(name, "ecdsa-sha2-nistp521"))
type = SSH2_EC_CURVE_NISTP521;
src/openssl.c:3382
- These key-type checks compare buf (from ssh2_get_chars()) using strcmp(). ssh2_get_chars() does not NUL-terminate, so strcmp() can read beyond buf_len into subsequent parsed fields (and potentially past the allocation if the underlying buffer isn’t NUL-terminated). Since you already have buf_len, compare with memcmp/strncmp instead.
#if LIBSSH2_ED25519
if(buf_len == sizeof("ssh-ed25519") - 1 &&
!strcmp("ssh-ed25519", buf) &&
(!want_method || !strcmp("ssh-ed25519", want_method)))
src/openssl.c:3390
- strcmp() against buf here has the same issue as other OpenSSH key-type comparisons: ssh2_get_chars() returns a non-NUL-terminated pointer into a binary blob. Use a bounded compare keyed off buf_len.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf) &&
(!want_method || !strcmp("[email protected]", want_method)))
src/openssl.c:3400
- Using strcmp() on buf (ssh2_get_chars output) can read past buf_len into the remainder of the decrypted blob. Prefer memcmp/strncmp with the known literal length.
if(buf_len == sizeof("ssh-rsa") - 1 &&
!strcmp("ssh-rsa", buf) &&
(!want_method || !strcmp("ssh-rsa", want_method)))
src/openssl.c:3409
- strcmp() on buf is unsafe for the same reason as the other key-type checks (ssh2_get_chars returns a length-prefixed, non-NUL-terminated field). Use a bounded compare.
if(buf_len == sizeof("ssh-dss") - 1 &&
!strcmp("ssh-dss", buf) &&
(!want_method || !strcmp("ssh-dss", want_method)))
src/openssl.c:3417
- strcmp() on buf (from ssh2_get_chars) can read beyond the parsed field boundary. Since buf_len is already checked, compare the bytes directly (memcmp) rather than relying on NUL termination.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf))
src/mbedtls.c:1011
- mbed_ecdsa_curve_type_from_name() uses strcmp() even though the input comes from ssh2_get_chars() (non-NUL-terminated SSH "string"). This can read beyond the parsed field boundary; switch to memcmp/strncmp bounded by the known literal length.
if(!name || name_len != 19)
return -1;
if(!strcmp(name, "ecdsa-sha2-nistp256"))
type = SSH2_EC_CURVE_NISTP256;
else if(!strcmp(name, "ecdsa-sha2-nistp384"))
type = SSH2_EC_CURVE_NISTP384;
else if(!strcmp(name, "ecdsa-sha2-nistp521"))
type = SSH2_EC_CURVE_NISTP521;
else
return -1;
src/openssl.c:3494
- strcmp() on buf is unsafe here as well: buf is returned by ssh2_get_chars() and is not NUL-terminated. Use a bounded compare keyed off buf_len (which is already available).
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf)) {
src/openssl.c:3507
- strcmp() on buf can read past buf_len because buf is a length-prefixed SSH "string" (ssh2_get_chars). Use memcmp/strncmp with the known literal length.
if(buf_len == sizeof("[email protected]") - 1 &&
!strcmp("[email protected]", buf)) {
To make sure readers expecting a null-terminated string do not read past the decoded buffer. "`ciphername`/`kdfname` (in `pem_parse_data_openssh()`) are obtained via `ssh2_get_string()` from the base64-decoded key blob and later compared with `strcmp()`. The decoded buffer from `ssh2_base64_decode()` is not NUL-terminated, so these `strcmp()` calls can read past the end of the allocation if an attacker crafts lengths such that no `\0` occurs before the buffer end. NUL-terminate the decoded buffer immediately after decoding to make later string comparisons bounded by the allocation." Reported by Copilot Bug: libssh2#2420 (comment) Ref: 0fcb662 libssh2#2403
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/pem.c:446
ciphername/kdfnameare parsed viassh2_get_chars(), which returns a non-NUL-terminated buffer slice (length is provided via the outlen parameter). Later in this function, these values are still compared usingstrcmp()(andmemcmp()withstrlen()but without a length check), which can still read past the end of the underlying allocation when processing attacker-controlled OpenSSH key blobs. Please update the remaining comparisons to be length-bounded (e.g.,SSH2_IS_LITERAL(...)orciphername_len == strlen(cur_method->name) && memcmp(...) == 0).
if(ssh2_get_chars(&decoded, &ciphername, &ciphername_len) ||
ciphername_len == 0) {
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/pem.c:472
- These early
SSH2_IS_LITERAL()checks correctly avoidstrcmp()on non-NUL-terminatedciphername/kdfname, but later inpem_parse_data_openssh()the sameciphernameis still used withstrcmp(ciphername, "none")and also compared againstcur_method->nameviamemcmp(..., strlen(cur_method->name))without checkingciphername_len. Both can still read past the end of the decoded buffer on crafted inputs.
Please update the later cipher selection block to use ciphername_len (e.g., !SSH2_IS_LITERAL(ciphername, ciphername_len, "none") and require ciphername_len == strlen(cur_method->name) before memcmp).
if((!passphrase || strlen(passphrase) == 0) &&
!SSH2_IS_LITERAL(ciphername, ciphername_len, "none")) {
/* passphrase required */
ret = LIBSSH2_ERROR_KEYFILE_AUTH_FAILED;
goto out;
}
src/pem.c:479
kdfnamecomes fromssh2_get_chars()and is not guaranteed to be NUL-terminated, but later in this function it is still checked withstrcmp(kdfname, "bcrypt"). That can read past the end of the decoded key buffer if the crafted key data omits a\0before the allocation end.
Please switch that later check to SSH2_IS_LITERAL(kdfname, kdfname_len, "bcrypt") (or otherwise use kdfname_len with memcmp).
if(!SSH2_IS_LITERAL(kdfname, kdfname_len, "none") &&
!SSH2_IS_LITERAL(kdfname, kdfname_len, "bcrypt")) {
ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"unrecognized KDF algorithm");
goto out;
}
Before this patch some checks were using
strcmp()on received/readbuffers that may not necessarily have a null-terminator. Fix by always
using the buffer length returned by the buffer reader, compare this
length with the expected one, and use
memcmp()to verify the content.When reading ECDSA/ED25519 OpenSSH-formatted keys in
mbed_parse_openssh_key(),ossl_key_from_openssh(), OpenSSL'sssh2_sk_pubkey(),pem_parse_data_openssh(),wcng_ecdsa_new_private_parse().Introduce
SSH2_IS_LITERAL()macro to hide the delicate logic ofcomparing a non-null-terminated memory buffer with a string literal.
Reported by Copilot
Bug: #2420 (comment)
Bug: #2420 (review)
Bug: #2426 (review)
Bug: #2420 (comment)
Bug: #2420 (comment)
Ref: 0fcb662 #2403
memcmp()."
ciphername/kdfname(inpem_parse_data_openssh()) are obtained viassh2_get_string()from the base64-decoded key blob and later comparedwith
strcmp(). The decoded buffer fromssh2_base64_decode()is notNUL-terminated, so these
strcmp()calls can read past the end of theallocation if an attacker crafts lengths such that no
\0occurs beforethe buffer end. NUL-terminate the decoded buffer immediately after
decoding to make later string comparisons bounded by the allocation."