mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 09:54:25 +09:00
CVE-2025-8277: Fix memory leak of unused ephemeral key pair after client's wrong KEX guess
Signed-off-by: Francesco Rollo <eferollo@gmail.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Jakub Jelen
parent
4310a696f2
commit
ccff22d378
@@ -79,6 +79,12 @@ int ssh_curve25519_init(ssh_session session)
|
|||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free any previously allocated privkey */
|
||||||
|
if (session->next_crypto->curve25519_privkey != NULL) {
|
||||||
|
EVP_PKEY_free(session->next_crypto->curve25519_privkey);
|
||||||
|
session->next_crypto->curve25519_privkey = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
session->next_crypto->curve25519_privkey = pkey;
|
session->next_crypto->curve25519_privkey = pkey;
|
||||||
pkey = NULL;
|
pkey = NULL;
|
||||||
|
|
||||||
|
|||||||
@@ -89,6 +89,12 @@ int ssh_curve25519_init(ssh_session session)
|
|||||||
|
|
||||||
memcpy(*pubkey_loc, pubkey_data + 1, CURVE25519_PUBKEY_SIZE);
|
memcpy(*pubkey_loc, pubkey_data + 1, CURVE25519_PUBKEY_SIZE);
|
||||||
|
|
||||||
|
/* Free any previously allocated privkey */
|
||||||
|
if (session->next_crypto->curve25519_privkey != NULL) {
|
||||||
|
gcry_sexp_release(session->next_crypto->curve25519_privkey);
|
||||||
|
session->next_crypto->curve25519_privkey = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Store the private key */
|
/* Store the private key */
|
||||||
session->next_crypto->curve25519_privkey = keypair_sexp;
|
session->next_crypto->curve25519_privkey = keypair_sexp;
|
||||||
keypair_sexp = NULL;
|
keypair_sexp = NULL;
|
||||||
|
|||||||
@@ -407,6 +407,11 @@ int ssh_dh_init_common(struct ssh_crypto_struct *crypto)
|
|||||||
struct dh_ctx *ctx = NULL;
|
struct dh_ctx *ctx = NULL;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
/* Cleanup any previously allocated dh_ctx */
|
||||||
|
if (crypto->dh_ctx != NULL) {
|
||||||
|
ssh_dh_cleanup(crypto);
|
||||||
|
}
|
||||||
|
|
||||||
ctx = calloc(1, sizeof(*ctx));
|
ctx = calloc(1, sizeof(*ctx));
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
|
|||||||
@@ -237,6 +237,11 @@ int ssh_dh_init_common(struct ssh_crypto_struct *crypto)
|
|||||||
struct dh_ctx *ctx = NULL;
|
struct dh_ctx *ctx = NULL;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
/* Cleanup any previously allocated dh_ctx */
|
||||||
|
if (crypto->dh_ctx != NULL) {
|
||||||
|
ssh_dh_cleanup(crypto);
|
||||||
|
}
|
||||||
|
|
||||||
ctx = calloc(1, sizeof(*ctx));
|
ctx = calloc(1, sizeof(*ctx));
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
|
|||||||
@@ -191,6 +191,17 @@ static ssh_string ssh_ecdh_generate(ssh_session session)
|
|||||||
#endif /* OPENSSL_VERSION_NUMBER */
|
#endif /* OPENSSL_VERSION_NUMBER */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free any previously allocated privkey */
|
||||||
|
if (session->next_crypto->ecdh_privkey != NULL) {
|
||||||
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
||||||
|
EC_KEY_free(session->next_crypto->ecdh_privkey);
|
||||||
|
#else
|
||||||
|
EVP_PKEY_free(session->next_crypto->ecdh_privkey);
|
||||||
|
#endif
|
||||||
|
session->next_crypto->ecdh_privkey = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
session->next_crypto->ecdh_privkey = key;
|
session->next_crypto->ecdh_privkey = key;
|
||||||
return pubkey_string;
|
return pubkey_string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,12 @@ int ssh_client_ecdh_init(ssh_session session)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free any previously allocated privkey */
|
||||||
|
if (session->next_crypto->ecdh_privkey != NULL) {
|
||||||
|
gcry_sexp_release(session->next_crypto->ecdh_privkey);
|
||||||
|
session->next_crypto->ecdh_privkey = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
session->next_crypto->ecdh_privkey = key;
|
session->next_crypto->ecdh_privkey = key;
|
||||||
key = NULL;
|
key = NULL;
|
||||||
session->next_crypto->ecdh_client_pubkey = client_pubkey;
|
session->next_crypto->ecdh_client_pubkey = client_pubkey;
|
||||||
|
|||||||
@@ -70,6 +70,12 @@ int ssh_client_ecdh_init(ssh_session session)
|
|||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free any previously allocated privkey */
|
||||||
|
if (session->next_crypto->ecdh_privkey != NULL) {
|
||||||
|
mbedtls_ecp_keypair_free(session->next_crypto->ecdh_privkey);
|
||||||
|
SAFE_FREE(session->next_crypto->ecdh_privkey);
|
||||||
|
}
|
||||||
|
|
||||||
session->next_crypto->ecdh_privkey = malloc(sizeof(mbedtls_ecp_keypair));
|
session->next_crypto->ecdh_privkey = malloc(sizeof(mbedtls_ecp_keypair));
|
||||||
if (session->next_crypto->ecdh_privkey == NULL) {
|
if (session->next_crypto->ecdh_privkey == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
|
|||||||
Reference in New Issue
Block a user