diff options
author | Jakub Jelen <jjelen@redhat.com> | 2022-05-11 15:06:30 +0200 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2022-05-31 15:22:10 +0900 |
commit | ef2e1523c33c3143b4fee0c00f88a5a0842b337f (patch) | |
tree | b2caa75c882b2ba133ec9fc82f8bfe007bd0f2a9 | |
parent | ca2afc9fb64d9a9b2f8930ba505d9ab6c8a57667 (diff) | |
download | libgcrypt-ef2e1523c33c3143b4fee0c00f88a5a0842b337f.tar.gz |
Fix memory leaks in tests
* tests/aeswrap.c (check_one_with_padding): Free hd on error paths
* tests/basic.c (check_ccm_cipher): Free context on error paths
(check_ocb_cipher_checksum): Ditto.
(do_check_xts_cipher): Ditto.
(check_gost28147_cipher_basic): Ditto.
* tests/bench-slope.c (bench_ecc_init): Free memory on invalid input.
* tests/t-cv25519.c (test_it): Free memory on error path
* tests/t-dsa.c (hex2buffer): Free memory on error path
* tests/t-ecdsa.c (hex2buffer): Free memory on error path
(one_test_sexp): Cleanup memory on exit
* tests/t-mpi-point.c (check_ec_mul): Free memory on error
(check_ec_mul_reduction): Ditto
* tests/t-rsa-15.c (hex2buffer): Ditto
* tests/t-rsa-pss.c (hex2buffer): Ditto
* tests/t-x448.c (test_it): Free memory on error path
* tests/testdrv.c (my_spawn): Free memory on error paths
--
GnuPG-bug-id: 5973
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
-rw-r--r-- | tests/aeswrap.c | 5 | ||||
-rw-r--r-- | tests/basic.c | 11 | ||||
-rw-r--r-- | tests/bench-slope.c | 4 | ||||
-rw-r--r-- | tests/t-cv25519.c | 4 | ||||
-rw-r--r-- | tests/t-dsa.c | 5 | ||||
-rw-r--r-- | tests/t-ecdsa.c | 6 | ||||
-rw-r--r-- | tests/t-mpi-point.c | 12 | ||||
-rw-r--r-- | tests/t-rsa-15.c | 5 | ||||
-rw-r--r-- | tests/t-rsa-pss.c | 5 | ||||
-rw-r--r-- | tests/t-x448.c | 4 | ||||
-rw-r--r-- | tests/testdrv.c | 21 |
11 files changed, 63 insertions, 19 deletions
diff --git a/tests/aeswrap.c b/tests/aeswrap.c index ed4453bd..e5ecad75 100644 --- a/tests/aeswrap.c +++ b/tests/aeswrap.c @@ -219,6 +219,7 @@ check_one_with_padding (int algo, if (err) { fail ("gcry_cipher_setkey failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hd); return; } @@ -235,6 +236,7 @@ check_one_with_padding (int algo, if (err) { fail ("gcry_cipher_encrypt failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hd); return; } @@ -275,6 +277,7 @@ check_one_with_padding (int algo, if (err) { fail ("gcry_cipher_decrypt failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hd); return; } @@ -318,6 +321,7 @@ check_one_with_padding (int algo, if (err) { fail ("gcry_cipher_decrypt(2) failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hd); return; } @@ -347,6 +351,7 @@ check_one_with_padding (int algo, if (err) { fail ("gcry_cipher_decrypt(3) failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hd); return; } diff --git a/tests/basic.c b/tests/basic.c index f5513740..90bbf962 100644 --- a/tests/basic.c +++ b/tests/basic.c @@ -7339,6 +7339,8 @@ check_ccm_cipher (void) if (!keylen) { fail ("cipher-ccm, gcry_cipher_get_algo_keylen failed\n"); + gcry_cipher_close (hde); + gcry_cipher_close (hdd); return; } @@ -7358,6 +7360,8 @@ check_ccm_cipher (void) if (!blklen) { fail ("cipher-ccm, gcry_cipher_get_algo_blklen failed\n"); + gcry_cipher_close (hde); + gcry_cipher_close (hdd); return; } @@ -8431,7 +8435,7 @@ check_ocb_cipher_checksum (int algo, int keylen) const size_t buflen = 128 * 16; unsigned char *inbuf, *outbuf; gpg_error_t err = 0; - gcry_cipher_hd_t hde, hde2, hdd; + gcry_cipher_hd_t hde = NULL, hde2 = NULL, hdd = NULL; unsigned char tag[16]; unsigned char tag2[16]; unsigned char tag3[16]; @@ -8493,6 +8497,8 @@ check_ocb_cipher_checksum (int algo, int keylen) err = gcry_cipher_open (&hdd, algo, GCRY_CIPHER_MODE_OCB, 0); if (err) { + gcry_cipher_close (hde); + gcry_cipher_close (hde2); fail ("cipher-ocb, gcry_cipher_open failed (checksum, algo %d): %s\n", algo, gpg_strerror (err)); goto out_free; @@ -9126,6 +9132,7 @@ do_check_xts_cipher (int inplace) { fail ("cipher-xts, gcry_cipher_open failed (tv %d): %s\n", tidx, gpg_strerror (err)); + gcry_cipher_close (hde); return; } @@ -9350,6 +9357,8 @@ check_gost28147_cipher_basic (enum gcry_cipher_algos algo) if (err) { fail ("gost28147, gcry_cipher_open failed: %s\n", gpg_strerror (err)); + gcry_cipher_close (hde); + gcry_cipher_close (hdd); return; } diff --git a/tests/bench-slope.c b/tests/bench-slope.c index 5c49ac25..aaddaa85 100644 --- a/tests/bench-slope.c +++ b/tests/bench-slope.c @@ -2629,7 +2629,9 @@ bench_ecc_init (struct bench_obj *obj) switch (oper->algo) { default: - return -1; + gcry_mpi_release (x); + free (hd); + return -1; case ECC_ALGO_ED25519: err = gcry_sexp_build (&hd->key_spec, NULL, diff --git a/tests/t-cv25519.c b/tests/t-cv25519.c index ec1472a9..14a6719b 100644 --- a/tests/t-cv25519.c +++ b/tests/t-cv25519.c @@ -348,7 +348,7 @@ test_it (int testno, const char *k_str, int iter, const char *result_str) gcry_mpi_t mpi_k = NULL; gcry_mpi_t mpi_x = NULL; gcry_mpi_point_t P = NULL; - gcry_mpi_point_t Q; + gcry_mpi_point_t Q = NULL; int i; gcry_mpi_t mpi_kk = NULL; @@ -363,7 +363,7 @@ test_it (int testno, const char *k_str, int iter, const char *result_str) testno); if (verbose > 1) info ("not executed in FIPS mode\n"); - return; + goto leave; } Q = gcry_mpi_point_new (0); diff --git a/tests/t-dsa.c b/tests/t-dsa.c index 965b7cf2..8ed3b65b 100644 --- a/tests/t-dsa.c +++ b/tests/t-dsa.c @@ -168,7 +168,10 @@ hex2buffer (const char *string, size_t *r_length) for (s=string; *s; s +=2 ) { if (!hexdigitp (s) || !hexdigitp (s+1)) - return NULL; /* Invalid hex digits. */ + { + xfree (buffer); + return NULL; /* Invalid hex digits. */ + } ((unsigned char*)buffer)[length++] = xtoi_2 (s); } *r_length = length; diff --git a/tests/t-ecdsa.c b/tests/t-ecdsa.c index 9a0773b7..fa0a2ef9 100644 --- a/tests/t-ecdsa.c +++ b/tests/t-ecdsa.c @@ -195,7 +195,10 @@ hex2buffer (const char *string, size_t *r_length) for (; *s; s +=2 ) { if (!hexdigitp (s) || !hexdigitp (s+1)) - return NULL; /* Invalid hex digits. */ + { + xfree (buffer); + return NULL; /* Invalid hex digits. */ + } buffer[length++] = xtoi_2 (s); } *r_length = length; @@ -483,6 +486,7 @@ one_test_sexp (const char *curvename, const char *sha_alg, xfree (out_s); xfree (sig_r_string); xfree (sig_s_string); + xfree (pkbuffer); } diff --git a/tests/t-mpi-point.c b/tests/t-mpi-point.c index 72d7fa9b..0310fe11 100644 --- a/tests/t-mpi-point.c +++ b/tests/t-mpi-point.c @@ -3504,8 +3504,8 @@ check_ec_mul (void) }; gpg_error_t err; gcry_ctx_t ctx; - gcry_mpi_t k, x, y; - gcry_mpi_point_t G, Q; + gcry_mpi_t k = NULL, x = NULL, y = NULL; + gcry_mpi_point_t G = NULL, Q = NULL; int idx; for (idx = 0; tv[idx].curve; idx++) @@ -3544,7 +3544,7 @@ check_ec_mul (void) { fail ("tv[%d].'%s': error getting point parameter 'g'\n", idx, tv[idx].curve); - return; + goto err; } if (tv[idx].k_base10) @@ -3562,7 +3562,7 @@ check_ec_mul (void) { fail ("tv[%d].'%s': failed to get affine coordinates\n", idx, tv[idx].curve); - return; + goto err; } if (cmp_mpihex (x, tv[idx].qx) || cmp_mpihex (y, tv[idx].qy)) @@ -3576,6 +3576,7 @@ check_ec_mul (void) printf ("expected Qy: %s\n", tv[idx].qy); } +err: gcry_mpi_release (k); gcry_mpi_release (y); gcry_mpi_release (x); @@ -4368,7 +4369,7 @@ check_ec_mul_reduction (void) { fail ("tv[%d].'%s': failed to get affine coordinates\n", idx, tv[idx].curve); - return; + goto out; } if ((tv[idx].qx != NULL && tv[idx].qy != NULL) @@ -4383,6 +4384,7 @@ check_ec_mul_reduction (void) printf ("expected Qy: %s\n", tv[idx].qy); } +out: gcry_mpi_release (uy); gcry_mpi_release (ux); gcry_mpi_release (uz); diff --git a/tests/t-rsa-15.c b/tests/t-rsa-15.c index 67dbd2cc..65e74961 100644 --- a/tests/t-rsa-15.c +++ b/tests/t-rsa-15.c @@ -144,7 +144,10 @@ hex2buffer (const char *string, size_t *r_length) for (s=string; *s; s +=2 ) { if (!hexdigitp (s) || !hexdigitp (s+1)) - return NULL; /* Invalid hex digits. */ + { + xfree (buffer); + return NULL; /* Invalid hex digits. */ + } ((unsigned char*)buffer)[length++] = xtoi_2 (s); } *r_length = length; diff --git a/tests/t-rsa-pss.c b/tests/t-rsa-pss.c index fa8392e9..c5f90116 100644 --- a/tests/t-rsa-pss.c +++ b/tests/t-rsa-pss.c @@ -144,7 +144,10 @@ hex2buffer (const char *string, size_t *r_length) for (s=string; *s; s +=2 ) { if (!hexdigitp (s) || !hexdigitp (s+1)) - return NULL; /* Invalid hex digits. */ + { + xfree (buffer); + return NULL; /* Invalid hex digits. */ + } ((unsigned char*)buffer)[length++] = xtoi_2 (s); } *r_length = length; diff --git a/tests/t-x448.c b/tests/t-x448.c index f5f49e5e..bfc22fc0 100644 --- a/tests/t-x448.c +++ b/tests/t-x448.c @@ -324,7 +324,7 @@ test_it (int testno, const char *k_str, int iter, const char *result_str) gcry_mpi_t mpi_k = NULL; gcry_mpi_t mpi_x = NULL; gcry_mpi_point_t P = NULL; - gcry_mpi_point_t Q; + gcry_mpi_point_t Q = NULL; int i; gcry_mpi_t mpi_kk = NULL; @@ -339,7 +339,7 @@ test_it (int testno, const char *k_str, int iter, const char *result_str) testno); if (verbose > 1) info ("not executed in FIPS mode\n"); - return; + goto leave; } Q = gcry_mpi_point_new (0); diff --git a/tests/testdrv.c b/tests/testdrv.c index 816eae0a..0ccde326 100644 --- a/tests/testdrv.c +++ b/tests/testdrv.c @@ -532,6 +532,7 @@ my_spawn (const char *pgmname, char **argv, char **envp, MYPID_T *pid) *pid = fork (); if (*pid == MYINVALID_PID) { + xfree (arg_list); fail ("error forking process: %s\n", strerror (errno)); return -1; } @@ -546,17 +547,29 @@ my_spawn (const char *pgmname, char **argv, char **envp, MYPID_T *pid) /* Assign /dev/null to stdin. */ fd = open ("/dev/null", O_RDONLY); if (fd == -1) - die ("failed to open '%s': %s\n", "/dev/null", strerror (errno)); + { + xfree (arg_list); + die ("failed to open '%s': %s\n", "/dev/null", strerror (errno)); + } if (fd != 0 && dup2 (fd, 0) == -1) - die ("dup2(%d,0) failed: %s\n", fd, strerror (errno)); + { + xfree (arg_list); + die ("dup2(%d,0) failed: %s\n", fd, strerror (errno)); + } /* Assign /dev/null to stdout unless in verbose mode. */ if (!verbose) { fd = open ("/dev/null", O_RDONLY); if (fd == -1) - die ("failed to open '%s': %s\n", "/dev/null", strerror (errno)); + { + xfree (arg_list); + die ("failed to open '%s': %s\n", "/dev/null", strerror (errno)); + } if (fd != 1 && dup2 (fd, 1) == -1) - die ("dup2(%d,1) failed: %s\n", fd, strerror (errno)); + { + xfree (arg_list); + die ("dup2(%d,1) failed: %s\n", fd, strerror (errno)); + } } /* Exec the program. */ |