]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Fix mem leak in ECDSA_sign().
authorslontis <shane.lontis@oracle.com>
Tue, 21 Mar 2023 06:06:06 +0000 (16:06 +1000)
committerTodd Short <todd.short@me.com>
Fri, 31 Mar 2023 19:03:16 +0000 (15:03 -0400)
Similiar to the issue found in PR #20553 for DSA_sign().
ECDSA_sign() leaked memory if the signature was NULL
when i2d_ECDSA_SIG was called.

Note that this does not affect the higher level EVP
functions as they correctly handle NULL.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from https://github.com/openssl/openssl/pull/20554)

(cherry picked from commit 4befe81a99b89c52b749a87eece82c1cba4fab12)

crypto/ec/ecdsa_ossl.c
crypto/sm2/sm2_sign.c
test/ecdsatest.c

index f90304a419b25cd838dc57db5b7afd40b633926b..0bf4635e2f9723a96fb6cb06b337dd8fa82df6a1 100644 (file)
@@ -75,7 +75,7 @@ int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
         *siglen = 0;
         return 0;
     }
-    *siglen = i2d_ECDSA_SIG(s, &sig);
+    *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
     ECDSA_SIG_free(s);
     return 1;
 }
index 88c67edfdb6d3b2deb2d3455a47636df21470728..67b1dbe8efe529c0fb6874a2bf3d039106eefb1c 100644 (file)
@@ -453,7 +453,7 @@ int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
         goto done;
     }
 
-    sigleni = i2d_ECDSA_SIG(s, &sig);
+    sigleni = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
     if (sigleni < 0) {
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
        goto done;
index 32e929ed3d32a4d2b5d2dd4d9ea545b8a9aba5e5..375bf2e9ead339f4dcf7b3070c85f8eac8520a12 100644 (file)
@@ -346,6 +346,22 @@ static int test_builtin_as_sm2(int n)
     return test_builtin(n, EVP_PKEY_SM2);
 }
 # endif
+
+static int test_ecdsa_sig_NULL(void)
+{
+    int ret;
+    unsigned int siglen;
+    unsigned char dgst[128] = { 0 };
+    EC_KEY *eckey = NULL;
+
+    ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))
+          && TEST_int_eq(EC_KEY_generate_key(eckey), 1)
+          && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen, eckey), 1)
+          && TEST_int_gt(siglen, 0);
+    EC_KEY_free(eckey);
+    return ret;
+}
+
 #endif /* OPENSSL_NO_EC */
 
 int setup_tests(void)
@@ -365,6 +381,7 @@ int setup_tests(void)
         return 0;
     }
     ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
+    ADD_TEST(test_ecdsa_sig_NULL);
 # ifndef OPENSSL_NO_SM2
     ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
 # endif