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

Simo Sorce simo at redhat.com
Tue Oct 23 13:53:59 UTC 2012


This is useful for wiping passwords, as it prevents the compiler from
optimizing out a memset to zero before a free()

Signed-off-by: Simo Sorce <simo at redhat.com>
---
 src/util/util.c |  9 +++++++++
 src/util/util.h | 10 ++++++++++
 2 files changed, 19 insertions(+)

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 a96f519ad959b8706f31c5cf7e4204c77e39b737..db501c912a151613f8dc4c31f1899fd3a3568b02 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -528,6 +528,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.11.4




More information about the sssd-devel mailing list