fix: free BIGNUM parameters in _new_key_from_parameters() on 3.x#129
Merged
timlegge merged 1 commit intoMar 21, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()andRSA_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 tonew_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