|
| 1 | +From b1207905f9194d5bfa17393e605987f158e9448b Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark < [email protected]> |
| 3 | +Date: Thu, 11 Sep 2025 16:00:02 +0000 |
| 4 | +Subject: [PATCH] CVE-2025-8277: Avoid leaking ECDH keys and free previous |
| 5 | + DH/ECDH contexts; adjust packet filter for DH-GEX guess; free previously |
| 6 | + allocated pubkeys |
| 7 | + |
| 8 | +Signed-off-by: Azure Linux Security Servicing Account < [email protected]> |
| 9 | +Upstream-reference: AI Backport of https://git.libssh.org/projects/libssh.git/patch/?id=1c763e29d138db87665e98983f468d2dd0f286c1 https://git.libssh.org/projects/libssh.git/patch/?id=8e4d67aa9eda455bfad9ac610e54b7a548d0aa08 https://git.libssh.org/projects/libssh.git/patch/?id=266174a6d36687b65cf90174f06af90b8b27c65f https://git.libssh.org/projects/libssh.git/patch/?id=87db2659ec608a977a63eea529f17b9168388d73 |
| 10 | +--- |
| 11 | + src/dh_crypto.c | 5 +++++ |
| 12 | + src/dh_key.c | 5 +++++ |
| 13 | + src/ecdh_crypto.c | 11 +++++++++++ |
| 14 | + src/ecdh_gcrypt.c | 8 ++++++++ |
| 15 | + src/ecdh_mbedcrypto.c | 7 +++++++ |
| 16 | + src/packet.c | 2 ++ |
| 17 | + src/wrapper.c | 10 +++++++++- |
| 18 | + 7 files changed, 47 insertions(+), 1 deletion(-) |
| 19 | + |
| 20 | +diff --git a/src/dh_crypto.c b/src/dh_crypto.c |
| 21 | +index 9ff7ad3..325c568 100644 |
| 22 | +--- a/src/dh_crypto.c |
| 23 | ++++ b/src/dh_crypto.c |
| 24 | +@@ -407,6 +407,11 @@ int ssh_dh_init_common(struct ssh_crypto_struct *crypto) |
| 25 | + struct dh_ctx *ctx = NULL; |
| 26 | + int rc; |
| 27 | + |
| 28 | ++ /* Cleanup any previously allocated dh_ctx */ |
| 29 | ++ if (crypto->dh_ctx != NULL) { |
| 30 | ++ ssh_dh_cleanup(crypto); |
| 31 | ++ } |
| 32 | ++ |
| 33 | + ctx = calloc(1, sizeof(*ctx)); |
| 34 | + if (ctx == NULL) { |
| 35 | + return SSH_ERROR; |
| 36 | +diff --git a/src/dh_key.c b/src/dh_key.c |
| 37 | +index 20d24a3..d9743ce 100644 |
| 38 | +--- a/src/dh_key.c |
| 39 | ++++ b/src/dh_key.c |
| 40 | +@@ -237,6 +237,11 @@ int ssh_dh_init_common(struct ssh_crypto_struct *crypto) |
| 41 | + struct dh_ctx *ctx = NULL; |
| 42 | + int rc; |
| 43 | + |
| 44 | ++ /* Cleanup any previously allocated dh_ctx */ |
| 45 | ++ if (crypto->dh_ctx != NULL) { |
| 46 | ++ ssh_dh_cleanup(crypto); |
| 47 | ++ } |
| 48 | ++ |
| 49 | + ctx = calloc(1, sizeof(*ctx)); |
| 50 | + if (ctx == NULL) { |
| 51 | + return SSH_ERROR; |
| 52 | +diff --git a/src/ecdh_crypto.c b/src/ecdh_crypto.c |
| 53 | +index 069b137..8ad9bbf 100644 |
| 54 | +--- a/src/ecdh_crypto.c |
| 55 | ++++ b/src/ecdh_crypto.c |
| 56 | +@@ -219,7 +219,18 @@ int ssh_client_ecdh_init(ssh_session session){ |
| 57 | + return SSH_ERROR; |
| 58 | + } |
| 59 | + |
| 60 | ++ /* Free any previously allocated privkey #if OPENSSL_VERSION_NUMBER < 0x30000000L */ |
| 61 | ++ if (session->next_crypto->ecdh_privkey != NULL) { |
| 62 | ++#if 1 |
| 63 | ++ EC_KEY_free(session->next_crypto->ecdh_privkey); |
| 64 | ++#else |
| 65 | ++ EVP_PKEY_free(session->next_crypto->ecdh_privkey); |
| 66 | ++#endif |
| 67 | ++ session->next_crypto->ecdh_privkey = NULL; |
| 68 | ++ } |
| 69 | ++ |
| 70 | + session->next_crypto->ecdh_privkey = key; |
| 71 | ++ ssh_string_free(session->next_crypto->ecdh_client_pubkey); |
| 72 | + session->next_crypto->ecdh_client_pubkey = client_pubkey; |
| 73 | + |
| 74 | + /* register the packet callbacks */ |
| 75 | +diff --git a/src/ecdh_gcrypt.c b/src/ecdh_gcrypt.c |
| 76 | +index 3d9d426..918b0f5 100644 |
| 77 | +--- a/src/ecdh_gcrypt.c |
| 78 | ++++ b/src/ecdh_gcrypt.c |
| 79 | +@@ -101,8 +101,16 @@ int ssh_client_ecdh_init(ssh_session session) |
| 80 | + goto out; |
| 81 | + } |
| 82 | + |
| 83 | ++ /* Free any previously allocated privkey */ |
| 84 | ++ if (session->next_crypto->ecdh_privkey != NULL) { |
| 85 | ++ gcry_sexp_release(session->next_crypto->ecdh_privkey); |
| 86 | ++ session->next_crypto->ecdh_privkey = NULL; |
| 87 | ++ } |
| 88 | ++ |
| 89 | + session->next_crypto->ecdh_privkey = key; |
| 90 | + key = NULL; |
| 91 | ++ |
| 92 | ++ SSH_STRING_FREE(session->next_crypto->ecdh_client_pubkey); |
| 93 | + session->next_crypto->ecdh_client_pubkey = client_pubkey; |
| 94 | + client_pubkey = NULL; |
| 95 | + |
| 96 | +diff --git a/src/ecdh_mbedcrypto.c b/src/ecdh_mbedcrypto.c |
| 97 | +index dda7392..351aa65 100644 |
| 98 | +--- a/src/ecdh_mbedcrypto.c |
| 99 | ++++ b/src/ecdh_mbedcrypto.c |
| 100 | +@@ -70,6 +70,12 @@ int ssh_client_ecdh_init(ssh_session session) |
| 101 | + return SSH_ERROR; |
| 102 | + } |
| 103 | + |
| 104 | ++ /* Free any previously allocated privkey */ |
| 105 | ++ if (session->next_crypto->ecdh_privkey != NULL) { |
| 106 | ++ mbedtls_ecp_keypair_free(session->next_crypto->ecdh_privkey); |
| 107 | ++ SAFE_FREE(session->next_crypto->ecdh_privkey); |
| 108 | ++ } |
| 109 | ++ |
| 110 | + session->next_crypto->ecdh_privkey = malloc(sizeof(mbedtls_ecp_keypair)); |
| 111 | + if (session->next_crypto->ecdh_privkey == NULL) { |
| 112 | + return SSH_ERROR; |
| 113 | +@@ -110,6 +116,7 @@ int ssh_client_ecdh_init(ssh_session session) |
| 114 | + goto out; |
| 115 | + } |
| 116 | + |
| 117 | ++ SSH_STRING_FREE(session->next_crypto->ecdh_client_pubkey); |
| 118 | + session->next_crypto->ecdh_client_pubkey = client_pubkey; |
| 119 | + client_pubkey = NULL; |
| 120 | + |
| 121 | +diff --git a/src/packet.c b/src/packet.c |
| 122 | +index ea73f9a..dfb8b01 100644 |
| 123 | +--- a/src/packet.c |
| 124 | ++++ b/src/packet.c |
| 125 | +@@ -294,6 +294,7 @@ static enum ssh_packet_filter_result_e ssh_packet_incoming_filter(ssh_session se |
| 126 | + * or session_state == SSH_SESSION_STATE_INITIAL_KEX |
| 127 | + * - dh_handshake_state == DH_STATE_INIT |
| 128 | + * or dh_handshake_state == DH_STATE_INIT_SENT (re-exchange) |
| 129 | ++ * or dh_handshake_state == DH_STATE_REQUEST_SENT (dh-gex) |
| 130 | + * or dh_handshake_state == DH_STATE_FINISHED (re-exchange) |
| 131 | + * |
| 132 | + * Transitions: |
| 133 | +@@ -313,6 +314,7 @@ static enum ssh_packet_filter_result_e ssh_packet_incoming_filter(ssh_session se |
| 134 | + |
| 135 | + if ((session->dh_handshake_state != DH_STATE_INIT) && |
| 136 | + (session->dh_handshake_state != DH_STATE_INIT_SENT) && |
| 137 | ++ (session->dh_handshake_state != DH_STATE_REQUEST_SENT) && |
| 138 | + (session->dh_handshake_state != DH_STATE_FINISHED)) |
| 139 | + { |
| 140 | + rc = SSH_PACKET_DENIED; |
| 141 | +diff --git a/src/wrapper.c b/src/wrapper.c |
| 142 | +index d317dc4..1f8cf84 100644 |
| 143 | +--- a/src/wrapper.c |
| 144 | ++++ b/src/wrapper.c |
| 145 | +@@ -190,9 +190,17 @@ void crypto_free(struct ssh_crypto_struct *crypto) |
| 146 | + #endif /* OPENSSL_VERSION_NUMBER */ |
| 147 | + #elif defined HAVE_GCRYPT_ECC |
| 148 | + gcry_sexp_release(crypto->ecdh_privkey); |
| 149 | +-#endif |
| 150 | ++#elif defined HAVE_LIBMBEDCRYPTO |
| 151 | ++ mbedtls_ecp_keypair_free(crypto->ecdh_privkey); |
| 152 | ++ SAFE_FREE(crypto->ecdh_privkey); |
| 153 | ++#endif /* HAVE_LIBGCRYPT */ |
| 154 | + crypto->ecdh_privkey = NULL; |
| 155 | + } |
| 156 | ++#endif |
| 157 | ++#ifdef HAVE_LIBCRYPTO |
| 158 | ++ EVP_PKEY_free(crypto->curve25519_privkey); |
| 159 | ++#elif defined(HAVE_GCRYPT_CURVE25519) |
| 160 | ++ gcry_sexp_release(crypto->curve25519_privkey); |
| 161 | + #endif |
| 162 | + SAFE_FREE(crypto->dh_server_signature); |
| 163 | + if (crypto->session_id != NULL) { |
| 164 | +-- |
| 165 | +2.45.4 |
| 166 | + |
0 commit comments