Skip to content

fix: free OpenSSL 3.x resources in _new_key_from_parameters()#90

Merged
atoomic merged 1 commit into
cpan-authors:mainfrom
atoomic:koan.atoomic/fix-new-key-params-error-leaks
Mar 16, 2026
Merged

fix: free OpenSSL 3.x resources in _new_key_from_parameters()#90
atoomic merged 1 commit into
cpan-authors:mainfrom
atoomic:koan.atoomic/fix-new-key-params-error-leaks

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented Mar 13, 2026

What

Free pctx, params_build, and params that were leaked in _new_key_from_parameters() on OpenSSL 3.x.

Why

Three OpenSSL 3.x resources allocated during key construction were never fully cleaned up:

  • pctx (EVP_PKEY_CTX): leaked on every call — neither happy path nor error path freed it
  • params_build (OSSL_PARAM_BLD): freed in the p/q branch but leaked in the else (public-key only) branch and on all THROW error paths
  • params (OSSL_PARAM): same as params_build

How

  • Free all three immediately after EVP_PKEY_fromdata() succeeds, in both the p/q and else branches
  • NULL pointers after freeing to prevent double-free
  • Add fallback cleanup in the err: block (inside #if OPENSSL_VERSION_NUMBER >= 0x30000000L) for THROW error paths that jump before inline cleanup

All OpenSSL *_free() functions accept NULL, so the err: block is safe even when inline cleanup already ran.

Testing

  • make test passes (281 tests) on Perl 5.42.0 / OpenSSL 3.6.1 (macOS)

🤖 Generated with Claude Code


Quality Report

Changes: 1 file changed, 15 insertions(+)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Comment thread RSA.xs Outdated
if (iqmp) BN_clear_free(iqmp);
if (ctx) BN_CTX_free(ctx);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_PKEY_CTX_free(pctx);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we check whether pctx, params_build and params != NULL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, explicit NULL checks before freeing are not needed here. All three OpenSSL free functions handle NULL gracefully (they're no-ops on NULL input):

  • EVP_PKEY_CTX_free(NULL) — safe
  • OSSL_PARAM_BLD_free(NULL) — safe
  • OSSL_PARAM_free(NULL) — safe

The allocation points are already guarded by CHECK_OPEN_SSL() which croaks immediately on NULL, so we can't reach the usage code with a NULL pointer. And OSSL_PARAM_BLD_to_param() failure is caught by THROW(params != NULL), which jumps to the err: label where the PR adds cleanup.

The NULL-after-free assignments (pctx = NULL; etc.) are there for defensive double-free protection — if both the inline success-path cleanup and the err: label fire, they won't double-free.

@atoomic
Copy link
Copy Markdown
Collaborator

atoomic commented Mar 15, 2026

@Koan-Bot rebase

@atoomic atoomic marked this pull request as ready for review March 15, 2026 22:06
@Koan-Bot
Copy link
Copy Markdown
Contributor Author

Rebase: fix: free OpenSSL 3.x resources in _new_key_from_parameters()

Branch koan.atoomic/fix-new-key-params-error-leaks rebased onto main and force-pushed.

Diff: 1 file changed, 10 insertions(+)

Review feedback was analyzed and applied.

Actions

  • Resolved merge conflicts (1 round(s))
  • Rebased koan.atoomic/fix-new-key-params-error-leaks onto upstream/main
  • Force-pushed koan.atoomic/fix-new-key-params-error-leaks to origin

Automated by Kōan

@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-new-key-params-error-leaks branch from 9f64a40 to 6ad6229 Compare March 15, 2026 22:59
@atoomic
Copy link
Copy Markdown
Collaborator

atoomic commented Mar 15, 2026

Something is wrong with that rebase. WTH so many commits were added !!
sorry for the noise

1 similar comment
@atoomic
Copy link
Copy Markdown
Collaborator

atoomic commented Mar 15, 2026

Something is wrong with that rebase. WTH so many commits were added !!
sorry for the noise

@atoomic
Copy link
Copy Markdown
Collaborator

atoomic commented Mar 16, 2026

@Koan-Bot rebase

On OpenSSL 3.x, _new_key_from_parameters() allocates three resources
that were never fully cleaned up:

- pctx (EVP_PKEY_CTX): leaked on every call (both happy and error paths)
- params_build (OSSL_PARAM_BLD): leaked in the else branch (public-key
  only path) and on any THROW error path
- params (OSSL_PARAM): same as params_build

Fix: free all three immediately after EVP_PKEY_fromdata() succeeds in
both the p/q branch and the else branch, NULL the pointers, and add
fallback cleanup in the err: block for THROW error paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Koan-Bot
Copy link
Copy Markdown
Contributor Author

Rebase: fix: free OpenSSL 3.x resources in _new_key_from_parameters()

Branch koan.atoomic/fix-new-key-params-error-leaks rebased onto main and force-pushed.

Diff: 1 file changed, 10 insertions(+)

Review feedback was analyzed and applied.

Actions

  • Rebased koan.atoomic/fix-new-key-params-error-leaks onto upstream/main
  • Force-pushed koan.atoomic/fix-new-key-params-error-leaks to origin

Automated by Kōan

@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-new-key-params-error-leaks branch from 6ad6229 to b788b3f Compare March 16, 2026 03:09
@atoomic atoomic merged commit 49fb186 into cpan-authors:main Mar 16, 2026
27 checks passed
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.

3 participants