Skip to content

TLS: Fix EVP_PKEY leak in SSLPrivateKeyHandler - #13464

Merged
moonchen merged 1 commit into
apache:masterfrom
moonchen:fix-ssl-private-key-leak
Jul 31, 2026
Merged

TLS: Fix EVP_PKEY leak in SSLPrivateKeyHandler#13464
moonchen merged 1 commit into
apache:masterfrom
moonchen:fix-ssl-private-key-leak

Conversation

@moonchen

@moonchen moonchen commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

SSL_CTX_use_PrivateKey() takes its own reference on the key, so the reference from PEM_read_bio_PrivateKey() belongs to the caller. SSLPrivateKeyHandler() released it only when attaching the key failed, so a successful load leaked one EVP_PKEY per certificate. This repeats at startup, on every config reload, and on every secret or certificate update.

Hold the key in a scoped_PKEY so every exit releases it.

SSL_CTX_use_PrivateKey() takes its own reference on the key, so the
reference from PEM_read_bio_PrivateKey() belongs to the caller.
SSLPrivateKeyHandler() released it only when attaching the key failed, so
a successful load leaked one EVP_PKEY per certificate.  This repeats at
startup, on every config reload, and on every secret or certificate
update.  Hold the key in a scoped_PKEY so every exit releases it.
Copilot AI review requested due to automatic review settings July 31, 2026 15:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an EVP_PKEY reference leak in the TLS private key loading path by ensuring the key returned from PEM_read_bio_PrivateKey() (or engine load) is always released, even on successful SSL_CTX_use_PrivateKey().

Changes:

  • Wrap the loaded private key in an RAII handle (scoped_PKEY) so all exit paths release the caller-owned reference.
  • Add an EVP_PKEY std::unique_ptr deleter + alias in P_SSLUtils.h and update SSLPrivateKeyHandler() to use .reset() / .get() accordingly.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/iocore/net/SSLUtils.cc Converts SSLPrivateKeyHandler() to manage the loaded EVP_PKEY via RAII and pass the raw pointer to OpenSSL when attaching the key.
src/iocore/net/P_SSLUtils.h Introduces PKEYDeleter and scoped_PKEY to standardize EVP_PKEY lifetime management alongside existing scoped OpenSSL handles.

@moonchen moonchen self-assigned this Jul 31, 2026

@JosiahWI JosiahWI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix. This is also fixed in #13458; I'll rebase that PR on this.

@JosiahWI JosiahWI added this to the 11.0.0 milestone Jul 31, 2026
@moonchen
moonchen merged commit 07aa06c into apache:master Jul 31, 2026
15 checks passed
@github-project-automation github-project-automation Bot moved this to For v10.2.0 in ATS v10.2.x Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: For v10.2.0

Development

Successfully merging this pull request may close these issues.

3 participants