Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR cleans up internal string/byte handling by aligning pointer types with how the data is used (primarily treating textual protocol fields as char * / const char *) and removing redundant casts, which reduces noise across the SSH session, key parsing, and SCP/userauth code paths.
Changes:
- Convert several session fields and local variables from
unsigned char *tochar *where they represent textual data (banners, kbd-int name/instruction, SCP command buffers, etc.). - Update parsing call sites to match these type changes (adding necessary casts when calling helpers like
ssh2_get_string()/ssh2_copy_string()). - Constify and adjust helper APIs used for kex algorithm negotiation (
ssh2_kex_agree_instrand related call sites).
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/userauth.c | Removes redundant casts when passing kbd-int name/instruction into the response callback. |
| src/userauth_kbd_packet.c | Adjusts ssh2_copy_string() calls to store into char * session fields. |
| src/session.c | Treats local/remote banners as char * and removes casts around strlen/strncmp. |
| src/scp.c | Treats SCP command buffers and quoting helpers as char * to reduce casting. |
| src/publickey.c | Aligns comment attribute handling with const char * usage. |
| src/pem.c | Treats OpenSSH key header strings as char * and updates parsing/strcmp call sites. |
| src/packet.c | Uses char * for disconnect/debug message strings and constifies strict-kex marker usage. |
| src/os400qc3.c | Removes redundant cast when passing decoded PEM data into key-loading proc. |
| src/openssl.c | Uses char * for parsed key type strings and removes redundant casts in comparisons/calls. |
| src/misc.c | Removes redundant casts in timing-safe compare implementation. |
| src/mbedtls.c | Uses char * for parsed curve name and removes redundant cast in curve lookup. |
| src/libssh2_priv.h | Updates session struct field types and constifies ssh2_kex_agree_instr signature. |
| src/kex.c | Propagates kex string type changes and constifies negotiation helpers and value-hash inputs. |
| src/hostkey.c | Uses char * for parsed ECDSA type/domain strings and removes redundant casts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| char *ciphername = NULL; | ||
| char *kdfname = NULL; | ||
| unsigned char *kdf = NULL; |
There was a problem hiding this comment.
Related:
via: #2420 (review)
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/openssl.c:3369
ssh2_get_string()returns a pointer/length slice that is not NUL-terminated (seessh2_get_string()insrc/misc.c). Herebufis treated as a C string viastrcmp()/strlen()(also indirectly inossl_ecdsa_curve_type_from_name()), which can read past the end of the SSH string and into subsequent decrypted data (OOB read) for malformed inputs. Capture the returned length and either compare using length-bounded logic (len + memcmp/strncmp) or copy the type into a bounded, NUL-terminated buffer before calling any C-string APIs.
This issue also appears on line 3466 of the same file.
rc = ssh2_get_string(decrypted, (unsigned char **)&buf, NULL);
if(rc || !buf) {
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"Public key type in decrypted key data not found");
goto cleanup;
}
src/openssl.c:3470
- Same issue as above:
bufcomes fromssh2_get_string()and is not NUL-terminated, but is compared withstrcmp(). For malformed inputs this can cause OOB reads. Capture the string length and use length-bounded comparisons, or create a bounded NUL-terminated copy before usingstrcmp()/strlen().
rc = ssh2_get_string(decrypted, (unsigned char **)&buf, NULL);
if(rc || !buf) {
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"Public key type in decrypted key data not found");
goto cleanup;
There was a problem hiding this comment.
Related:
via #2426 (review)
Pull request overview
This PR updates the WinCNG ECDSA private-key parsing path to use the newer internal API that returns char * buffers, removing the need for an (unsigned char **)&keytype pointer cast in wcng_ecdsa_new_private_parse().
Changes:
- Replace
ssh2_get_string(... (unsigned char **)&keytype ...)withssh2_get_chars(...)when reading the ECDSA key type.
Comments suppressed due to low confidence (1)
src/wincng.c:2370
ssh2_get_chars()(likessh2_get_string()) returns a length-delimited SSH “string” that is not NUL-terminated, butwcng_ecdsa_curve_type_from_name()usesstrcmp(). This can read past the end of the keytype field (UB / potential crash) when parsing OpenSSH private key blobs. Consider NUL-terminating a temporary copy before callingwcng_ecdsa_curve_type_from_name(), or changing the helper to accept a length and usememcmp().
result = ssh2_get_chars(&data_buffer, &keytype, &keytype_len);
if(result != LIBSSH2_ERROR_NONE)
goto cleanup;
result = wcng_ecdsa_curve_type_from_name(keytype, &curve_type);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
There was a problem hiding this comment.
mbedtls.c: if(ssh2_get_chars(decrypted, &name, NULL))
openssl.c: rc = ssh2_get_chars(decrypted, &buf, NULL);
openssl.c: rc = ssh2_get_chars(decrypted, &buf, NULL);
pem.c: if(ssh2_get_chars(&decoded, &ciphername, &tmp_len) ||
pem.c: if(ssh2_get_chars(&decoded, &kdfname, &tmp_len) ||
wincng.c: result = ssh2_get_chars(&data_buffer, &keytype, &keytype_len);
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.
Comments suppressed due to low confidence (4)
src/packet.c:740
- Casting
&message/&language(typechar **) tounsigned char **forssh2_get_string()is undefined behavior. Read into a temporaryunsigned char *and then assign to thechar *variables.
ssh2_get_string(&buf, (unsigned char **)&message,
&message_len);
ssh2_get_string(&buf, (unsigned char **)&language,
&language_len);
src/packet.c:795
- Casting
&message/&language(typechar **) tounsigned char **forssh2_get_string()is undefined behavior. Use a temporaryunsigned char *and then assign to thechar *variables.
ssh2_get_string(&buf, (unsigned char **)&message,
&message_len);
ssh2_get_string(&buf, (unsigned char **)&language,
&language_len);
src/openssl.c:3466
- Casting
&buf(achar **) tounsigned char **forssh2_get_string()is undefined behavior. Read into a temporaryunsigned char *and then assign tobuf.
rc = ssh2_get_string(decrypted, (unsigned char **)&buf, NULL);
src/hostkey.c:652
- Casting
&domain(achar **) tounsigned char **forssh2_get_string()is undefined behavior. Read into a temporaryunsigned char *and then assign todomain.
if(ssh2_get_string(&buf, (unsigned char **)&domain, &len) || len != 8)
return -1;
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/openssl.c:3369
ssh2_get_string()returns a pointer/length slice that is not NUL-terminated (seessh2_get_string()insrc/misc.c). Herebufis treated as a C string viastrcmp()/strlen()(also indirectly inossl_ecdsa_curve_type_from_name()), which can read past the end of the SSH string and into subsequent decrypted data (OOB read) for malformed inputs. Capture the returned length and either compare using length-bounded logic (len + memcmp/strncmp) or copy the type into a bounded, NUL-terminated buffer before calling any C-string APIs.
This issue also appears on line 3466 of the same file.
rc = ssh2_get_string(decrypted, (unsigned char **)&buf, NULL);
if(rc || !buf) {
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"Public key type in decrypted key data not found");
goto cleanup;
}
src/openssl.c:3470
- Same issue as above:
bufcomes fromssh2_get_string()and is not NUL-terminated, but is compared withstrcmp(). For malformed inputs this can cause OOB reads. Capture the string length and use length-bounded comparisons, or create a bounded NUL-terminated copy before usingstrcmp()/strlen().
rc = ssh2_get_string(decrypted, (unsigned char **)&buf, NULL);
if(rc || !buf) {
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"Public key type in decrypted key data not found");
goto cleanup;
Also use it from `ssh2_match_string()` to reduce casts. Apparently casting the return pointer of `ssh2_get_string()` buffer is frowned upon, though existing practice and not completelely clear if UB or not. Current practice (with two exceptions, now reduced to one in WinCNG code) is to declare the return buffer as `unsigned char *`, then cast that result everywhere on use. It results in excessive casts. The frowned-upon alternative to avoid this, is to declare as `char *` and pass it to `ssh2_get_string()` as `(unsigned char **)&data`. This new API avoid this, and allows to declare as `char *data`, retrieve it without cast, and use it without casts too. Ref: #2420 Ref: a7d05f9 #2422 Closes #2424
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (7)
src/pem.c:468
ssh2_get_chars()returns a length-delimited SSH string that is not NUL-terminated (seessh2_get_chars()insrc/misc.c). Usingstrcmp()onciphername/kdfnamecan read past the end of the field for malformed inputs. Use length-bounded comparisons based on the SSH string length.
if((!passphrase || strlen(passphrase) == 0) &&
strcmp(ciphername, "none")) {
/* passphrase required */
src/openssl.c:3381
ssh2_get_chars()returns a non-NUL-terminated SSH string, butbufis compared withstrcmp(). For malformed key blobs this can trigger out-of-bounds reads. Use length-bounded comparisons based on the SSH string length (the 4 bytes precedingbuf).
if(!strcmp("ssh-ed25519", buf) &&
(!want_method || !strcmp("ssh-ed25519", want_method)))
rc = ossl_ed25519_openssh_priv_to_pubkey(session, decrypted,
src/openssl.c:3397
bufreturned byssh2_get_chars()is a length-delimited field and not NUL-terminated, but this code usesstrcmp()for method dispatch. Replace with length-bounded comparisons to avoid OOB reads on malformed key blobs.
if(!strcmp("ssh-rsa", buf) &&
(!want_method || !strcmp("ssh-rsa", want_method)))
rc = ossl_rsa_openssh_priv_to_pubkey(session, decrypted,
src/openssl.c:3418
ossl_ecdsa_curve_type_from_name()usesstrlen()/strcmp()and therefore requires a NUL-terminated input, butbufcomes fromssh2_get_chars()and is not NUL-terminated. Dispatch on the curve name using the SSH string length andmemcmp()(and settypeaccordingly) to avoid out-of-bounds reads on malformed input.
else if(ossl_ecdsa_curve_type_from_name(buf, &type) == 0 &&
(!want_method || !strcmp("ssh-ecdsa", want_method)))
src/openssl.c:3488
- In
ssh2_sk_pubkey(),bufis returned byssh2_get_chars()and is not NUL-terminated, but is compared usingstrcmp(). Use the SSH string length andmemcmp()to avoid OOB reads when parsing malformed key blobs.
if(!strcmp("[email protected]", buf)) {
*algorithm = LIBSSH2_HOSTKEY_TYPE_ED25519;
rc = ossl_ed25519_sk_openssh_priv_to_pubkey(session, decrypted, method,
src/mbedtls.c:1038
ssh2_get_chars()returns a length-delimited string that is not NUL-terminated, butmbed_ecdsa_curve_type_from_name()usesstrlen()/strcmp(). This can read past the end of the field for malformed OpenSSH key blobs. Use the SSH string length andmemcmp()instead of passing the slice to string APIs.
if(ssh2_get_chars(decrypted, &name, NULL))
goto failed;
if(mbed_ecdsa_curve_type_from_name(name, &type))
goto failed;
src/pem.c:510
ciphernamecomes fromssh2_get_chars()and is not NUL-terminated, sostrcmp(ciphername, "none")and the method selection loop can read past the end of the field for malformed inputs. Compare using the SSH string length (and ensure the ciphername length matches the candidate method name length) before callingmemcmp().
if(ciphername && strcmp(ciphername, "none")) {
const struct crypt_method **all_methods, *cur_method;
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
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
…arsing Before this patch some checks were using `strcmp()` on received/read buffers 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's `ssh2_sk_pubkey()`, `pem_parse_data_openssh()`, `wcng_ecdsa_new_private_parse()`. Introduce `SSH2_IS_LITERAL()` macro to hide the delicate logic of comparing 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 Closes #2421
(mostly
unsigned char *tochar *)ssh2_get_string().void *arg inkex_value_hash()to avoid casts.sizeof()inssh2_packet_add(),also make the constant static.
Follow-up to 907fe84 #2424