Skip to content

src: fix potential OOB reads in EDCDA/ED25519 OpenSSH-formatted key parsing - #2421

Closed
vszakats wants to merge 18 commits into
libssh2:masterfrom
vszakats:base64dec-null-term
Closed

vszakats wants to merge 18 commits into
libssh2:masterfrom
vszakats:base64dec-null-term

Conversation

@vszakats

@vszakats vszakats commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

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


  • tackle WinCNG.
  • use memcmp().
  • de-dupe ECDSA curve functions found in mbedtls and openssl.

"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."

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);

Copilot AI review requested due to automatic review settings July 24, 2026 00:26

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 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' at data[len] after decoding.

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

Comment thread src/misc.c Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 00:48

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/misc.c:430

  • ssh2_base64_decode() allocates src_len bytes but now writes a NUL at data[len]. For malformed inputs where src_len can 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 allocate src_len + 1 up-front, or grow to len + 1 before writing).
    (*data)[len] = '\0';

@vszakats vszakats changed the title misc: null-terminate result buffer of ssh2_base64_decode() [WIP] misc: null-terminate result buffer of ssh2_base64_decode() Jul 24, 2026
@vszakats
vszakats force-pushed the base64dec-null-term branch from 0975153 to 6551363 Compare July 24, 2026 15:13
Copilot AI review requested due to automatic review settings July 24, 2026 15:13

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 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), so strcmp() is not a safe/effective way to compare the KDF name. Use a length-bounded compare (and avoid reading beyond the kdfname field).
    if((kdfname_len != sizeof("none") - 1 || strcmp(kdfname, "none")) &&
       (kdfname_len != sizeof("bcrypt") - 1 || strcmp(kdfname, "bcrypt"))) {

src/pem.c:482

  • This strcmp() relies on kdfname/ciphername being NUL-terminated, but ssh2_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

  • name comes from ssh2_get_chars() (length-delimited, not NUL-terminated). Even with name_len == 19, using strcmp() can read beyond the field into subsequent bytes. Use fixed-length memcmp() 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

  • buf is not guaranteed to be NUL-terminated (returned by ssh2_get_chars()), so strcmp() can read beyond the field. Prefer memcmp() 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

  • buf originates from ssh2_get_chars() (not NUL-terminated). Using strcmp() here can read past the field and into later decrypted bytes. Use memcmp() 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

  • buf comes from a length-delimited field (ssh2_get_chars()), so strcmp() is not safe unless the field is explicitly terminated. Use a bounded memcmp() instead.
    if(buf_len == sizeof("ssh-dss") - 1 &&
       !strcmp("ssh-dss", buf) &&
       (!want_method || !strcmp("ssh-dss", want_method)))

src/openssl.c:3417

  • buf is not guaranteed to be NUL-terminated after ssh2_get_chars(). Using strcmp() 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

  • buf is a non-NUL-terminated field from ssh2_get_chars(). Switch the key-type comparison to memcmp() to avoid reading beyond the field boundary.
    if(buf_len == sizeof("[email protected]") - 1 &&
       !strcmp("[email protected]", buf)) {

src/openssl.c:3507

  • buf is a length-delimited (not NUL-terminated) field from ssh2_get_chars(). Using strcmp() can read past the field; prefer memcmp() against the literal size.
    if(buf_len == sizeof("[email protected]") - 1 &&
       !strcmp("[email protected]", buf)) {

src/mbedtls.c:1008

  • name is obtained via ssh2_get_chars() and is not guaranteed to be NUL-terminated. Even with name_len == 19, strcmp() can read beyond the field; use fixed-length memcmp() 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"))

Comment thread src/pem.c Resolved
Comment thread src/pem.c Resolved
Comment thread src/openssl.c Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 15:19
@vszakats vszakats changed the title [WIP] misc: null-terminate result buffer of ssh2_base64_decode() src: harden string comparisons to avoid OOB read Jul 24, 2026

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 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)) {

Comment thread src/wincng.c Resolved
vszakats added 10 commits July 24, 2026 17:29
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
@vszakats
vszakats marked this pull request as draft July 24, 2026 15:32
@vszakats vszakats changed the title src: harden string comparisons to avoid OOB read src: fix potential OOB reads in EDCDA/ED25519 OpenSSH-formatted key parsing Jul 24, 2026
Copilot AI review requested due to automatic review settings July 24, 2026 16:20
@vszakats
vszakats requested a review from Copilot July 24, 2026 16:27
@vszakats
vszakats marked this pull request as ready for review July 24, 2026 16:27

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 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread src/libssh2_priv.h Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 16:34

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 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/kdfname are parsed via ssh2_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 using strcmp() (and memcmp() with strlen() 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(...) or ciphername_len == strlen(cur_method->name) && memcmp(...) == 0).
    if(ssh2_get_chars(&decoded, &ciphername, &ciphername_len) ||
       ciphername_len == 0) {

Comment thread src/libssh2_priv.h Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 16:45

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 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 avoid strcmp() on non-NUL-terminated ciphername/kdfname, but later in pem_parse_data_openssh() the same ciphername is still used with strcmp(ciphername, "none") and also compared against cur_method->name via memcmp(..., strlen(cur_method->name)) without checking ciphername_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

  • kdfname comes from ssh2_get_chars() and is not guaranteed to be NUL-terminated, but later in this function it is still checked with strcmp(kdfname, "bcrypt"). That can read past the end of the decoded key buffer if the crafted key data omits a \0 before 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;
    }

Copilot AI review requested due to automatic review settings July 24, 2026 16:53

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 5 out of 5 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 24, 2026 16:57

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 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread src/pem.c Resolved
Comment thread src/wincng.c Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 17:06

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 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread src/libssh2_priv.h Resolved
@vszakats vszakats closed this in 262c97f Jul 24, 2026
@vszakats
vszakats deleted the base64dec-null-term branch July 24, 2026 18:17
vszakats added a commit that referenced this pull request Jul 24, 2026
Also drop redundant sanity checks.

Follow-up to 262c97f #2421

Closes #2427
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