Skip to content

fix: double-free of BIGNUMs in _new_key_from_parameters()#140

Merged
atoomic merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-double-free-new-key-params
Apr 3, 2026
Merged

fix: double-free of BIGNUMs in _new_key_from_parameters()#140
atoomic merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-double-free-new-key-params

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

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

What

NULL BIGNUM pointers after freeing them on the success path of _new_key_from_parameters() to prevent double-free when make_rsa_obj() fails.

Why

On OpenSSL 3.x, both the if (p||q) and else branches free n, e, d (and p, q in the full-key branch) after EVP_PKEY_fromdata() succeeds, since OSSL_PARAM_BLD_push_BN() copies the values. But if make_rsa_obj() subsequently fails (e.g., memory allocation failure), execution falls through to the err: label which calls BN_clear_free() on the same pointers — a double-free.

How

Set each pointer to NULL immediately after BN_clear_free() on the success path, so the error handler's cleanup calls become no-ops. This follows the same NULL-after-free pattern already used for pctx, params_build, params, and ctx in this function.

Testing

Full test suite passes (507 tests). The double-free is only triggered under memory pressure (when New() inside make_rsa_obj fails), so it wouldn't surface in normal testing — it's a latent bug visible through code inspection.

🤖 Generated with Claude Code


Quality Report

Changes: 1 file changed, 2 insertions(+)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

…parameters()

Both the if (p||q) and else branches on OpenSSL 3.x free the BIGNUM
parameters (n, e, d, p, q) after EVP_PKEY_fromdata() succeeds, since
OSSL_PARAM_BLD_push_BN() copies the values. However, if make_rsa_obj()
subsequently fails, execution falls through to the err: label which
frees the same BIGNUMs again — a double-free.

Fix: set pointers to NULL immediately after freeing so the error
handler's BN_clear_free() calls become no-ops.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Good catch, Looks good to me.

@timlegge timlegge marked this pull request as ready for review March 29, 2026 00:55
@atoomic atoomic merged commit ed633e5 into cpan-authors:main Apr 3, 2026
28 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