[tor-commits] [tor/release-0.4.4] Add a tor_str_wipe_and_free() function.

asn at torproject.org asn at torproject.org
Tue Aug 11 11:54:57 UTC 2020


commit 5b9508c9a5d757223c62749c51eaf7453ff26691
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon Aug 3 10:11:03 2020 -0400

    Add a tor_str_wipe_and_free() function.
    
    Frequently we want to do
    
        if (s) {
          memwipe(s, 0, sizeof(s));
          tor_free(s);
        }
    
    and it's good to have a way to do this concisely.
---
 src/lib/crypt_ops/crypto_util.c | 14 ++++++++++++++
 src/lib/crypt_ops/crypto_util.h | 10 ++++++++++
 2 files changed, 24 insertions(+)

diff --git a/src/lib/crypt_ops/crypto_util.c b/src/lib/crypt_ops/crypto_util.c
index 67a1a9eb92..2f821fa024 100644
--- a/src/lib/crypt_ops/crypto_util.c
+++ b/src/lib/crypt_ops/crypto_util.c
@@ -109,3 +109,17 @@ memwipe(void *mem, uint8_t byte, size_t sz)
    **/
   memset(mem, byte, sz);
 }
+
+/**
+ * Securely all memory in <b>str</b>, then free it.
+ *
+ * As tor_free(), tolerates null pointers.
+ **/
+void
+tor_str_wipe_and_free_(char *str)
+{
+  if (!str)
+    return;
+  memwipe(str, 0, strlen(str));
+  tor_free_(str);
+}
diff --git a/src/lib/crypt_ops/crypto_util.h b/src/lib/crypt_ops/crypto_util.h
index 613a1bd0dd..b5d7f62521 100644
--- a/src/lib/crypt_ops/crypto_util.h
+++ b/src/lib/crypt_ops/crypto_util.h
@@ -14,8 +14,18 @@
 #define TOR_CRYPTO_UTIL_H
 
 #include "lib/cc/torint.h"
+#include "lib/malloc/malloc.h"
 
 /** OpenSSL-based utility functions. */
 void memwipe(void *mem, uint8_t byte, size_t sz);
 
+void tor_str_wipe_and_free_(char *str);
+/**
+ * Securely all memory in <b>str</b>, then free it.
+ *
+ * As tor_free(), tolerates null pointers, and sets <b>str</b> to NULL.
+ **/
+#define tor_str_wipe_and_free(str)                      \
+  FREE_AND_NULL(char, tor_str_wipe_and_free_, (str))
+
 #endif /* !defined(TOR_CRYPTO_UTIL_H) */





More information about the tor-commits mailing list