Skip to content

wincng: fix leaking objects on ECDSA context OOM - #2306

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:wcngleakleak
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:wcngleakleak

Conversation

@vszakats

Copy link
Copy Markdown
Member

Ref: #2300 (comment)
Ref: #2301 (comment)

Follow-up to 3e72343 #1315

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 adjusts the WinCNG ECDSA code paths to avoid leaking resources when allocating the ECDSA context fails (OOM), by allocating context objects earlier and ensuring error cleanups null out freed output pointers.

Changes:

  • Allocate ssh2_ecdsa_ctx / wcng_ecdsa_ctx earlier in ssh2_ecdsa_create_key(), ssh2_ecdsa_curve_name_with_octal_new(), and wcng_parse_ecdsa_privatekey() to prevent downstream resource leaks on OOM.
  • Improve error cleanup to free allocated contexts and set output pointers to NULL on failure.
Comments suppressed due to low confidence (1)

src/wincng.c:2168

  • ssh2_ecdsa_curve_name_with_octal_new() infers the curve from the encoded point length (wcng_ecdsa_decode_uncompressed_point() sets publickey.curve), but it never verifies that this matches the caller-provided curve argument. This can accept malformed/contradictory host keys and then store a potentially wrong ec_ctx->curve, which later drives hash/signature sizing in ssh2_ecdsa_verify() via ssh2_ecdsa_get_curve_type(ec_ctx). Add an explicit check to reject mismatches before importing the key.
    result = wcng_ecdsa_decode_uncompressed_point(
        publickey_encoded,
        publickey_encoded_len,
        &publickey);
    if(result != LIBSSH2_ERROR_NONE)

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

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