Commit a32df28664 for openssl.org

commit a32df286644d478e08707c13aa2aa5632f596510
Author: Richard Levitte <levitte@openssl.foundation>
Date:   Thu Aug 27 10:22:20 2026 +0200

    Fix potential double-free in dh_finish()

    dh_finish() (the default DH method's finish slot) frees the cached
    Montgomery context, dh->method_mont_p, without NULLing the pointer.
    Once the cache has been warmed, a subsequent finish call — e.g. via
    DH_free() after DH_set_method() — frees it again.

    NULL the pointer after freeing.

    Fixes: https://github.com/openssl/openssl/issues/32541
    Assisted-by: Pi:moonshotai/kimi-k3
    Signed-off-by: Richard Levitte <levitte@openssl.foundation>
    Reviewed-by: Jakub Zelenka <jakub.zelenka@openssl.foundation>
    Reviewed-by: Matt Caswell <matt@openssl.foundation>
    Merge-date: Sun Aug 30 14:54:59 2026
    Merged-from: https://github.com/openssl/openssl/pull/32543

diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
index 3f45a8d8f0..6edf33d15a 100644
--- a/crypto/dh/dh_key.c
+++ b/crypto/dh/dh_key.c
@@ -207,6 +207,7 @@ static int dh_init(DH *dh)
 static int dh_finish(DH *dh)
 {
     BN_MONT_CTX_free(dh->method_mont_p);
+    dh->method_mont_p = NULL;
     return 1;
 }