Skip to content

fix: free BIGNUM parameters in _new_key_from_parameters() on 3.x#129

Merged
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-new-key-params-bn-leak
Mar 21, 2026
Merged

fix: free BIGNUM parameters in _new_key_from_parameters() on 3.x#129
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-new-key-params-bn-leak

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

@toddr-bot toddr-bot commented Mar 20, 2026

What

Free leaked BIGNUM parameters (n, e, d, p, q) in _new_key_from_parameters() on OpenSSL 3.x.

Why

On pre-3.x, RSA_set0_key() and RSA_set0_factors() take ownership of BIGNUMs — the caller must not free them. On 3.x, OSSL_PARAM_BLD_push_BN() copies the values, leaving the originals orphaned. Every call to new_key_from_parameters() on 3.x leaked 3-5 BIGNUMs.

How

Added BN_clear_free() calls for n, e, d, p, q on both the success and error paths, guarded by #if OPENSSL_VERSION_NUMBER >= 0x30000000L. The existing dmp1/dmq1/iqmp frees (already 3.x-guarded) are correct and unchanged.

Also removed the pre-existing commented-out //if (p) BN_clear_free(p) dead code in the error block, replacing it with the properly guarded 3.x frees.

Testing

All 497 tests pass. The leak itself is only observable under Valgrind (#123) or similar tooling — functional behavior is unchanged.

🤖 Generated with Claude Code


Quality Report

Changes: 1 file changed, 21 insertions(+), 3 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

On pre-3.x, RSA_set0_key() and RSA_set0_factors() take ownership of
the BIGNUM parameters (n, e, d, p, q), so the caller must not free
them.  On 3.x, OSSL_PARAM_BLD_push_BN() copies the values internally,
leaving the original BIGNUMs orphaned.

Every call to new_key_from_parameters() on OpenSSL 3.x leaked 3-5
BIGNUMs (n, e, d, and optionally p, q).  Add BN_clear_free() calls
on both the success and error paths, guarded by the 3.x version check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr toddr marked this pull request as ready for review March 21, 2026 00:17
Copy link
Copy Markdown
Member

@timlegge timlegge left a comment

Choose a reason for hiding this comment

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

Approved

@timlegge timlegge merged commit 9ca9899 into cpan-authors:main Mar 21, 2026
81 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.

2 participants