[SSSD] [PATCH 2/4] Add function to safely wipe memory.

Simo Sorce simo at redhat.com
Thu Nov 1 05:24:54 UTC 2012


This is useful for wiping passwords, as it prevents the compiler from
optimizing out a memset to zero before a free()
---
 src/util/util.c |    9 +++++++++
 src/util/util.h |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index b812ef1b118803129caae25247efcbc1194b22f5..061291c04bb581082aa98d79b6b1ec1a1d1090ac 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -634,3 +634,12 @@ remove_ipv6_brackets(char *ipv6addr)
 
     return EOK;
 }
+
+void safezero(void *data, size_t size)
+{
+    volatile uint8_t *p = data;
+
+    while (size--) {
+        *p++ = 0;
+    }
+}
diff --git a/src/util/util.h b/src/util/util.h
index df57a3dec4b6d42bb1e4b5e70c109317a4aff31f..c308fc2fafb35e52a5b47be05fc8eb4c9d1c069e 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -533,6 +533,16 @@ sss_escape_ip_address(TALLOC_CTX *mem_ctx, int family, const char *addr);
 errno_t
 remove_ipv6_brackets(char *ipv6addr);
 
+
+/**
+ * @brief Safely zero a segment of memory,
+ *        prevents the compiler from optimizing out
+ *
+ * @param data   The address of buffer to wipe
+ * @param s      Size of the buffer
+ */
+void safezero(void *data, size_t size);
+
 /* from sss_tc_utf8.c */
 char *
 sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s);
-- 
1.7.1




More information about the sssd-devel mailing list