From 80c414ec8768cc4a15f0a8689e689af98d90ae21 Mon Sep 17 00:00:00 2001
From: ulfvonbelow <strilen@tilde.club>
Date: Sat, 28 Jan 2023 16:43:51 -0600
Subject: [PATCH] UTIL: fix memory leaks in several places.

Namely, in:
- GNUNET_CRYPTO_eddsa_private_key_derive
- GNUNET_CRYPTO_hash_file_cancel
- GNUNET_CRYPTO_hash_file
- checkvec and check_vectors in gnunet-crypto-tvg
---
 src/util/crypto_ecc_gnsrecord.c |  2 ++
 src/util/crypto_hash_file.c     |  2 ++
 src/util/gnunet-crypto-tvg.c    | 27 +++++++++++++++++----------
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/util/crypto_ecc_gnsrecord.c b/src/util/crypto_ecc_gnsrecord.c
index b902e0e0a..fc99bfc18 100644
--- a/src/util/crypto_ecc_gnsrecord.c
+++ b/src/util/crypto_ecc_gnsrecord.c
@@ -361,7 +361,9 @@ GNUNET_CRYPTO_eddsa_private_key_derive (
   gcry_mpi_release (h);
   gcry_mpi_release (x);
   gcry_mpi_release (n);
+  gcry_mpi_release (h_mod_n);
   gcry_mpi_release (a1);
+  gcry_mpi_release (eight);
   gcry_mpi_release (a2);
   gcry_ctx_release (ctx);
   GNUNET_CRYPTO_mpi_print_unsigned (dc, sizeof(dc), d);
diff --git a/src/util/crypto_hash_file.c b/src/util/crypto_hash_file.c
index 7300bab29..96d364d2b 100644
--- a/src/util/crypto_hash_file.c
+++ b/src/util/crypto_hash_file.c
@@ -184,6 +184,7 @@ GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
   if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
   {
     GNUNET_break (0);
+    GNUNET_free (fhc->filename);
     GNUNET_free (fhc);
     return NULL;
   }
@@ -227,6 +228,7 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
   GNUNET_free (fhc->filename);
   GNUNET_break (GNUNET_OK ==
                 GNUNET_DISK_file_close (fhc->fh));
+  gcry_md_close (fhc->md);
   GNUNET_free (fhc);
 }
 
diff --git a/src/util/gnunet-crypto-tvg.c b/src/util/gnunet-crypto-tvg.c
index 4655407f0..5a16bb8fc 100644
--- a/src/util/gnunet-crypto-tvg.c
+++ b/src/util/gnunet-crypto-tvg.c
@@ -637,11 +637,12 @@ checkvec (const char *operation,
                                                             blinded_len)) )
     {
       GNUNET_free (blinded_data);
+      GNUNET_free (blinded_data_comp);
       GNUNET_free (public_enc_data);
       GNUNET_free (secret_enc_data);
       GNUNET_free (sig_enc_data);
-      GNUNET_free (skey);
-      GNUNET_free (pkey);
+      GNUNET_CRYPTO_rsa_private_key_free (skey);
+      GNUNET_CRYPTO_rsa_public_key_free (pkey);
       GNUNET_break (0);
       return GNUNET_NO;
     }
@@ -650,6 +651,7 @@ checkvec (const char *operation,
     sig = GNUNET_CRYPTO_rsa_unblind (blinded_sig, &bks, pkey);
     GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_rsa_verify (&message_hash, sig,
                                                            pkey));
+    GNUNET_free(public_enc_data);
     public_enc_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey,
                                                           &public_enc_data);
     sig_enc_length_comp = GNUNET_CRYPTO_rsa_signature_encode (sig,
@@ -658,25 +660,29 @@ checkvec (const char *operation,
     if ( (sig_enc_length != sig_enc_length_comp) ||
          (0 != memcmp (sig_enc_data, sig_enc_data_comp, sig_enc_length) ))
     {
-      GNUNET_free (blinded_sig);
+      GNUNET_CRYPTO_rsa_signature_free (blinded_sig);
       GNUNET_free (blinded_data);
+      GNUNET_free (blinded_data_comp);
       GNUNET_free (public_enc_data);
       GNUNET_free (secret_enc_data);
       GNUNET_free (sig_enc_data);
-      GNUNET_free (skey);
-      GNUNET_free (sig);
-      GNUNET_free (pkey);
+      GNUNET_free (sig_enc_data_comp);
+      GNUNET_CRYPTO_rsa_private_key_free (skey);
+      GNUNET_CRYPTO_rsa_signature_free (sig);
+      GNUNET_CRYPTO_rsa_public_key_free (pkey);
       GNUNET_break (0);
       return GNUNET_NO;
     }
-    GNUNET_free (blinded_sig);
+    GNUNET_CRYPTO_rsa_signature_free (blinded_sig);
     GNUNET_free (blinded_data);
+    GNUNET_free (blinded_data_comp);
     GNUNET_free (public_enc_data);
     GNUNET_free (secret_enc_data);
     GNUNET_free (sig_enc_data);
-    GNUNET_free (sig);
-    GNUNET_free (pkey);
-    GNUNET_free (skey);
+    GNUNET_free (sig_enc_data_comp);
+    GNUNET_CRYPTO_rsa_signature_free (sig);
+    GNUNET_CRYPTO_rsa_public_key_free (pkey);
+    GNUNET_CRYPTO_rsa_private_key_free (skey);
   }
   else if (0 == strcmp (operation, "cs_blind_signing"))
   {
@@ -1009,6 +1015,7 @@ check_vectors ()
         break;
       }
     }
+    json_decref (vecfile);
     return (ret == GNUNET_OK) ? 0 : 1;
   }
 }
-- 
2.38.1

