Skip to content

openssl: drop no-op code from ssh2_sk_pub_keyfilememory() - #2281

Closed
vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:ossl-sk-cleanup
Closed

vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:ossl-sk-cleanup

Conversation

@vszakats

@vszakats vszakats commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Prior to this patch it was loading a PEM-formatted private key via
OpenSSL. When successful, it did nothing, when failed, it fell back
trying to load an OpenSSH-formatted key. This was no-op in itself, but
SK keys don't support the PEM format AFAIU, so the function went to the
OpenSSH codepath when used as intended. In both cases, it leaked the
OpenSSL memory object.

Drop the superfluous intermediate step and map this function directly to
the internal worker ossl_key_sk_from_openssh_blob(), also fixing the
memory leak and unnecessary processing.

Also:

  • sync passphrase type with rest of internal APIs.

Memleak reported repeatedly by GitHub Code Quality
Follow-up to ed439a2 #698


"st is initialized to -1 at line 4242, and is only assigned in the
if(!pk) branch. If PEM_read_bio_PrivateKey succeeds (i.e., pk is
non-NULL), st is never updated and the function always returns -1. The
non-NULL pk case should be handled, including freeing pk before
returning."

"When PEM_read_bio_PrivateKey succeeds (pk != NULL), st remains -1
and the function immediately returns it without processing the key or
freeing pk. This causes a memory leak of the EVP_PKEY object and
always returns failure for PEM-format SK keys. The function needs to
handle the successful PEM parse case, and EVP_PKEY_free(pk) must be
called before returning."

Prior to this patch it was loading a PEM-formatted private key via
OpenSSL. When successful, it did nothing, when failed, it fell back
trying to load an OpenSSH-formatted key. This was no-op in itself, but
SK keys don't support the PEM format AFAIU, so the function went to the
OpenSSH codepath when used as intended. In both cases, it leaked the
OpenSSL memory object.

Drop the superfluous intermediate step and map this function directly to
the internal worker `ossl_key_sk_from_openssh_blob()`, also fixing the
memory leak and unnecessary processing.

Follow-up to ed439a2 libssh2#698
@vszakats

vszakats commented Jul 12, 2026 •

Copy link
Copy Markdown
Member Author

Still having a hard time understanding how is this supposed to work
and why the from-blob and from-file codepaths are different for SK
keys.

edit: even more, it's two in-memory functions, both handling sk keys.
See ossl_key_from_openssh_blob(). Both calling
ssh2_openssh_pem_parse_memory().

edit 2: maybe that should have been two tries with the two method
names from userauth, to a standard ossl_key_from_openssh_blob().

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 simplifies the OpenSSL backend’s ssh2_sk_pub_keyfilememory() implementation by removing a no-op PEM private-key parse attempt and mapping the function directly to the OpenSSH SK key parsing flow, addressing unnecessary work and the reported OpenSSL object leak in the prior logic.

Changes:

  • Removes the intermediate PEM_read_bio_PrivateKey() step from ssh2_sk_pub_keyfilememory() and routes directly through OpenSSH-format parsing.
  • Adjusts the passphrase handling to align with the OpenSSH PEM parser’s expected type.
  • Drops the now-unneeded internal SK worker wrapper and related plumbing.

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

Comment thread src/openssl.c Resolved
@vszakats vszakats closed this in 08bee36 Jul 12, 2026
@vszakats
vszakats deleted the ossl-sk-cleanup branch July 12, 2026 23:52
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