|
| 1 | +From a9d8a3d44829cf9182b252bc951f35fb0d573972 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Jakub Jelen < [email protected]> |
| 3 | +Date: Wed, 14 May 2025 14:07:58 +0200 |
| 4 | +Subject: CVE-2025-5372 libgcrypto: Simplify error checking and handling of |
| 5 | + return codes in ssh_kdf() |
| 6 | + |
| 7 | +Signed-off-by: Jakub Jelen < [email protected]> |
| 8 | +Reviewed-by: Andreas Schneider < [email protected]> |
| 9 | + |
| 10 | +Upstream Patch Reference: https://git.libssh.org/projects/libssh.git/patch/?id=a9d8a3d44829cf9182b252bc951f35fb0d573972 |
| 11 | +--- |
| 12 | + src/libcrypto.c | 63 ++++++++++++++++++++++--------------------------- |
| 13 | + 1 file changed, 28 insertions(+), 35 deletions(-) |
| 14 | + |
| 15 | +diff --git a/src/libcrypto.c b/src/libcrypto.c |
| 16 | +index 911b363..aa48c67 100644 |
| 17 | +--- a/src/libcrypto.c |
| 18 | ++++ b/src/libcrypto.c |
| 19 | +@@ -163,7 +163,7 @@ int ssh_kdf(struct ssh_crypto_struct *crypto, |
| 20 | + uint8_t key_type, unsigned char *output, |
| 21 | + size_t requested_len) |
| 22 | + { |
| 23 | +- int rc = -1; |
| 24 | ++ int ret = SSH_ERROR, rv; |
| 25 | + #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 26 | + EVP_KDF_CTX *ctx = EVP_KDF_CTX_new_id(EVP_KDF_SSHKDF); |
| 27 | + #else |
| 28 | +@@ -185,90 +185,83 @@ int ssh_kdf(struct ssh_crypto_struct *crypto, |
| 29 | + } |
| 30 | + |
| 31 | + #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 32 | +- rc = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_MD, |
| 33 | ++ rv = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_MD, |
| 34 | + sshkdf_digest_to_md(crypto->digest_type)); |
| 35 | +- if (rc != 1) { |
| 36 | ++ if (rv != 1) { |
| 37 | + goto out; |
| 38 | + } |
| 39 | +- rc = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_KEY, key, key_len); |
| 40 | +- if (rc != 1) { |
| 41 | ++ rv = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_KEY, key, key_len); |
| 42 | ++ if (rv != 1) { |
| 43 | + goto out; |
| 44 | + } |
| 45 | +- rc = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_XCGHASH, |
| 46 | ++ rv = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_XCGHASH, |
| 47 | + crypto->secret_hash, crypto->digest_len); |
| 48 | +- if (rc != 1) { |
| 49 | ++ if (rv != 1) { |
| 50 | + goto out; |
| 51 | + } |
| 52 | +- rc = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_TYPE, key_type); |
| 53 | +- if (rc != 1) { |
| 54 | ++ rv = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_TYPE, key_type); |
| 55 | ++ if (rv != 1) { |
| 56 | + goto out; |
| 57 | + } |
| 58 | +- rc = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID, |
| 59 | ++ rv = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID, |
| 60 | + crypto->session_id, crypto->session_id_len); |
| 61 | +- if (rc != 1) { |
| 62 | ++ if (rv != 1) { |
| 63 | + goto out; |
| 64 | + } |
| 65 | +- rc = EVP_KDF_derive(ctx, output, requested_len); |
| 66 | +- if (rc != 1) { |
| 67 | ++ rv = EVP_KDF_derive(ctx, output, requested_len); |
| 68 | ++ if (rv != 1) { |
| 69 | + goto out; |
| 70 | + } |
| 71 | + #else |
| 72 | +- rc = OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_KDF_PARAM_DIGEST, |
| 73 | ++ rv = OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_KDF_PARAM_DIGEST, |
| 74 | + md, strlen(md)); |
| 75 | +- if (rc != 1) { |
| 76 | +- rc = -1; |
| 77 | ++ if (rv != 1) { |
| 78 | + goto out; |
| 79 | + } |
| 80 | +- rc = OSSL_PARAM_BLD_push_octet_string(param_bld, OSSL_KDF_PARAM_KEY, |
| 81 | ++ rv = OSSL_PARAM_BLD_push_octet_string(param_bld, OSSL_KDF_PARAM_KEY, |
| 82 | + key, key_len); |
| 83 | +- if (rc != 1) { |
| 84 | +- rc = -1; |
| 85 | ++ if (rv != 1) { |
| 86 | + goto out; |
| 87 | + } |
| 88 | +- rc = OSSL_PARAM_BLD_push_octet_string(param_bld, |
| 89 | ++ rv = OSSL_PARAM_BLD_push_octet_string(param_bld, |
| 90 | + OSSL_KDF_PARAM_SSHKDF_XCGHASH, |
| 91 | + crypto->secret_hash, |
| 92 | + crypto->digest_len); |
| 93 | +- if (rc != 1) { |
| 94 | +- rc = -1; |
| 95 | ++ if (rv != 1) { |
| 96 | + goto out; |
| 97 | + } |
| 98 | +- rc = OSSL_PARAM_BLD_push_octet_string(param_bld, |
| 99 | ++ rv = OSSL_PARAM_BLD_push_octet_string(param_bld, |
| 100 | + OSSL_KDF_PARAM_SSHKDF_SESSION_ID, |
| 101 | + crypto->session_id, |
| 102 | + crypto->session_id_len); |
| 103 | +- if (rc != 1) { |
| 104 | +- rc = -1; |
| 105 | ++ if (rv != 1) { |
| 106 | + goto out; |
| 107 | + } |
| 108 | +- rc = OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_KDF_PARAM_SSHKDF_TYPE, |
| 109 | ++ rv = OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_KDF_PARAM_SSHKDF_TYPE, |
| 110 | + (const char*)&key_type, 1); |
| 111 | +- if (rc != 1) { |
| 112 | +- rc = -1; |
| 113 | ++ if (rv != 1) { |
| 114 | + goto out; |
| 115 | + } |
| 116 | + |
| 117 | + params = OSSL_PARAM_BLD_to_param(param_bld); |
| 118 | + if (params == NULL) { |
| 119 | +- rc = -1; |
| 120 | + goto out; |
| 121 | + } |
| 122 | + |
| 123 | +- rc = EVP_KDF_derive(ctx, output, requested_len, params); |
| 124 | +- if (rc != 1) { |
| 125 | +- rc = -1; |
| 126 | ++ rv = EVP_KDF_derive(ctx, output, requested_len, params); |
| 127 | ++ if (rv != 1) { |
| 128 | + goto out; |
| 129 | + } |
| 130 | + #endif /* OPENSSL_VERSION_NUMBER */ |
| 131 | +- |
| 132 | ++ ret = SSH_OK; |
| 133 | + out: |
| 134 | + #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 135 | + OSSL_PARAM_BLD_free(param_bld); |
| 136 | + OSSL_PARAM_free(params); |
| 137 | + #endif |
| 138 | + EVP_KDF_CTX_free(ctx); |
| 139 | +- if (rc < 0) { |
| 140 | +- return rc; |
| 141 | ++ if (ret < 0) { |
| 142 | ++ return ret; |
| 143 | + } |
| 144 | + return 0; |
| 145 | + } |
| 146 | +-- |
| 147 | +2.34.1 |
| 148 | + |
0 commit comments