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

Simo Sorce simo at redhat.com
Sat Nov 24 04:36:17 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 |    9 +++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index ab980775a1e4c87b16d32220bccda6cb644e0756..f268fbcd564cd93a2e63097c595cf19b65eb6800 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -700,3 +700,12 @@ bool string_in_list(const char *string, char **list, bool case_sensitive)
 
     return false;
 }
+
+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 2d63e733f133f107d1ff44a049db1eb6eff09daa..72b8a23b8668dce6dcd7d5b2b8001f64eb76859d 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -541,6 +541,15 @@ errno_t add_string_to_list(TALLOC_CTX *mem_ctx, const char *string,
 
 bool string_in_list(const char *string, char **list, bool case_sensitive);
 
+/**
+ * @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