Skip to content

mbedtls: fix double-free in mbed_pub_priv_key() error path - #2502

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mbed-dub-free
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mbed-dub-free

Conversation

@vszakats

@vszakats vszakats commented Aug 3, 2026

Copy link
Copy Markdown
Member

"mbed_pub_priv_key() frees pkey on the unsupported-key path, but the
caller (ssh2_pub_privkey()) unconditionally calls
mbedtls_pk_free(&pkey) after this helper returns. That makes this a
double-free on the error path (memory corruption). The helper should not
free pkey; leave ownership/cleanup to the caller."

Reported by Copilot
Bug: #2501 (comment)
Bug: #2501 (comment)
Follow-up to 186f1a2 #132

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 fixes a double-free in the mbedTLS backend by ensuring mbed_pub_priv_key() does not free an mbedtls_pk_context that is owned and always freed by its caller (ssh2_pub_privkey()).

Changes:

  • Remove mbedtls_pk_free(pkey) from the unsupported-key-type error path in mbed_pub_priv_key().
  • Keep mbedtls_pk_free(&pkey) centralized in ssh2_pub_privkey() for consistent ownership and cleanup.

💡 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