Skip to content

fix: free BN_CTX and temp BIGNUMs on success path#112

Merged
atoomic merged 2 commits intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-new-key-params-success-leaks
Mar 19, 2026
Merged

fix: free BN_CTX and temp BIGNUMs on success path#112
atoomic merged 2 commits intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-new-key-params-success-leaks

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

What

Free BN_CTX, p_minus_1, and q_minus_1 on the success path in _new_key_from_parameters(), and add missing THROW on q = BN_new().

Why

These three allocations were only freed in the err: block, meaning every successful call to _new_key_from_parameters() with p/q provided leaked a BN_CTX and two BIGNUMs. This affects all OpenSSL versions.

The missing THROW(q = BN_new()) is inconsistent with the p case (which has THROW) and would cause a NULL dereference in BN_div if malloc failed.

Also fixes key_lifecycle.t test plan: skip() reports skipped tests which count toward the total, so the plan must always be 23 regardless of whether Bignum is available.

Testing

All 244 tests pass on macOS with OpenSSL 3.6.1.

🤖 Generated with Claude Code

@atoomic atoomic requested a review from timlegge March 17, 2026 22:32
@atoomic atoomic marked this pull request as ready for review March 17, 2026 22:32
Comment thread t/key_lifecycle.t Outdated
@atoomic
Copy link
Copy Markdown
Collaborator

atoomic commented Mar 17, 2026

@Koan-Bot rebase

@atoomic atoomic self-assigned this Mar 17, 2026
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.

I think we should free dmp1, dmq1 and iqmp

Comment thread RSA.xs
@@ -665,6 +665,12 @@ _new_key_from_parameters(proto, n, e, d, p, q)
#endif
#endif
dmp1 = dmq1 = iqmp = NULL;
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.

@Koan-Bot did we miss freeing dmp1, dmq1 and iqmp

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.

@Koan-Bot shouldn't dmp1, dmq1 and iqmp be freed on any openssl version?

…ers()

BN_CTX, p_minus_1, and q_minus_1 were only freed in the err: block,
leaking on every successful key construction from parameters when p/q
are provided. Also add missing THROW on q=BN_new() (potential NULL
deref if malloc fails) and fix key_lifecycle.t test plan mismatch.
@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-new-key-params-success-leaks branch from 12bab70 to 7a10583 Compare March 18, 2026 01:06
@Koan-Bot
Copy link
Copy Markdown
Contributor Author

Rebase: fix: free BN_CTX and temp BIGNUMs on success path

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

Diff: 1 file changed, 12 insertions(+), 1 deletion(-)

Review feedback was analyzed and applied.

Actions

  • Resolved merge conflicts (1 round(s))
  • Rebased koan.atoomic/fix-new-key-params-success-leaks onto upstream/main
  • Applied review feedback
  • Force-pushed koan.atoomic/fix-new-key-params-success-leaks to origin
  • CI passed

CI

CI passed.


Automated by Kōan

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.

@Koan-Bot review my comment on freeing three variables for all open SSL versions

Comment thread RSA.xs
@@ -665,6 +665,12 @@ _new_key_from_parameters(proto, n, e, d, p, q)
#endif
#endif
dmp1 = dmq1 = iqmp = NULL;
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.

@Koan-Bot shouldn't dmp1, dmq1 and iqmp be freed on any openssl version?

@atoomic atoomic requested a review from timlegge March 19, 2026 00:55
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.

tested the pre 3.0 code path and @Koan-Bot is right the free of dm1q and friends is only correct on openssl >- 0x300000000

@timlegge
Copy link
Copy Markdown
Member

@atoomic fine to merge

@atoomic atoomic merged commit 7821989 into cpan-authors:main Mar 19, 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