From d3fa51155b744888f3a7766f87e3d232ab44e8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Mon, 16 Mar 2026 05:41:52 -0600 Subject: [PATCH 1/2] fix: free BN_CTX and temp BIGNUMs on success in _new_key_from_parameters() 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. --- RSA.xs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/RSA.xs b/RSA.xs index af12460..df5f328 100644 --- a/RSA.xs +++ b/RSA.xs @@ -623,7 +623,7 @@ _new_key_from_parameters(proto, n, e, d, p, q) } else if (!q) { - q = BN_new(); + THROW(q = BN_new()); THROW(BN_div(q, NULL, n, p, ctx)); } #if OLD_CRUFTY_SSL_VERSION @@ -686,6 +686,12 @@ _new_key_from_parameters(proto, n, e, d, p, q) #endif #endif dmp1 = dmq1 = iqmp = NULL; + BN_CTX_free(ctx); + ctx = NULL; + BN_clear_free(p_minus_1); + p_minus_1 = NULL; + BN_clear_free(q_minus_1); + q_minus_1 = NULL; #if OPENSSL_VERSION_NUMBER >= 0x30000000L OSSL_PARAM_BLD_free(params_build); params_build = NULL; From 7a10583080eb3c57ad02368c9594476980c7cac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Tue, 17 Mar 2026 19:06:29 -0600 Subject: [PATCH 2/2] rebase: apply review feedback on #112 --- RSA.xs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/RSA.xs b/RSA.xs index df5f328..6ee1c96 100644 --- a/RSA.xs +++ b/RSA.xs @@ -684,6 +684,11 @@ _new_key_from_parameters(proto, n, e, d, p, q) #else THROW(RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)); #endif +#endif +#if OPENSSL_VERSION_NUMBER >= 0x30000000L + BN_clear_free(dmp1); + BN_clear_free(dmq1); + BN_clear_free(iqmp); #endif dmp1 = dmq1 = iqmp = NULL; BN_CTX_free(ctx);