From 1c763e29d138db87665e98983f468d2dd0f286c1 Mon Sep 17 00:00:00 2001
From: Jakub Jelen <jjelen@redhat.com>
Date: Wed, 6 Aug 2025 15:32:56 +0200
Subject: [PATCH] CVE-2025-8277: mbedtls: Avoid leaking ecdh keys

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit ffed80f8c078122990a4eba2b275facd56dd43e0)

Upstream-Status: Backport [https://git.libssh.org/projects/libssh.git/commit/?h=stable-0.11&id=1c763e29d138db87665e98983f468d2dd0f286c1]
CVE: CVE-2025-8277
Signed-off-by: Vijay Anusuri <vanusuri@mvista.com>
---
 src/ecdh_mbedcrypto.c | 1 +
 src/wrapper.c         | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/ecdh_mbedcrypto.c b/src/ecdh_mbedcrypto.c
index 6074b93d..351aa655 100644
--- a/src/ecdh_mbedcrypto.c
+++ b/src/ecdh_mbedcrypto.c
@@ -116,6 +116,7 @@ int ssh_client_ecdh_init(ssh_session session)
         goto out;
     }
 
+    SSH_STRING_FREE(session->next_crypto->ecdh_client_pubkey);
     session->next_crypto->ecdh_client_pubkey = client_pubkey;
     client_pubkey = NULL;
 
diff --git a/src/wrapper.c b/src/wrapper.c
index 43bf2137..0397f96d 100644
--- a/src/wrapper.c
+++ b/src/wrapper.c
@@ -193,7 +193,10 @@ void crypto_free(struct ssh_crypto_struct *crypto)
 #endif
         crypto->ecdh_privkey = NULL;
     }
-#endif
+#elif defined HAVE_LIBMBEDCRYPTO
+    mbedtls_ecp_keypair_free(crypto->ecdh_privkey);
+    SAFE_FREE(crypto->ecdh_privkey);
+#endif /* HAVE_LIBGCRYPT */
     SAFE_FREE(crypto->dh_server_signature);
     if (crypto->session_id != NULL) {
         explicit_bzero(crypto->session_id, crypto->session_id_len);
-- 
2.25.1

