chiark / gitweb /
treewide: replace homegrown memory_erase with explicit_bzero
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Feb 2017 01:05:27 +0000 (20:05 -0500)
committerSven Eden <yamakuzure@gmx.net>
Mon, 17 Jul 2017 15:58:36 +0000 (17:58 +0200)
explicit_bzero was added in glibc 2.25. Make use of it.

explicit_bzero is hardcoded to zero the memory, so string erase now
truncates the string, instead of overwriting it with 'x'. This causes
a visible difference only in the journalctl case.

src/basic/string-util.c
src/basic/string-util.h

index b906b581c9a2d228d531aa107304790498ba0fce..9b060a9a20dbb181f715bf348c741275dff0e209 100644 (file)
@@ -825,6 +825,7 @@ int free_and_strdup(char **p, const char *s) {
         return 1;
 }
 
         return 1;
 }
 
+#if !HAVE_DECL_EXPLICIT_BZERO
 /*
  * Pointer to memset is volatile so that compiler must de-reference
  * the pointer and can't assume that it points to any function in
 /*
  * Pointer to memset is volatile so that compiler must de-reference
  * the pointer and can't assume that it points to any function in
@@ -835,19 +836,19 @@ typedef void *(*memset_t)(void *,int,size_t);
 
 static volatile memset_t memset_func = memset;
 
 
 static volatile memset_t memset_func = memset;
 
-void* memory_erase(void *p, size_t l) {
-        return memset_func(p, 'x', l);
+void explicit_bzero(void *p, size_t l) {
+        memset_func(p, '\0', l);
 }
 }
+#endif
 
 char* string_erase(char *x) {
 
 char* string_erase(char *x) {
-
         if (!x)
                 return NULL;
 
         /* A delicious drop of snake-oil! To be called on memory where
          * we stored passphrases or so, after we used them. */
         if (!x)
                 return NULL;
 
         /* A delicious drop of snake-oil! To be called on memory where
          * we stored passphrases or so, after we used them. */
-
-        return memory_erase(x, strlen(x));
+        explicit_bzero(x, strlen(x));
+        return x;
 }
 
 char *string_free_erase(char *s) {
 }
 
 char *string_free_erase(char *s) {
index 668b63907566e1cfdf743a0f1915827d66d887fd..38c7c100971dc3ff2e165107e69d0aff6a35c2cc 100644 (file)
@@ -197,7 +197,10 @@ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const
         return memmem(haystack, haystacklen, needle, needlelen);
 }
 
         return memmem(haystack, haystacklen, needle, needlelen);
 }
 
-void* memory_erase(void *p, size_t l);
+#if !HAVE_DECL_EXPLICIT_BZERO
+void explicit_bzero(void *p, size_t l);
+#endif
+
 char *string_erase(char *x);
 
 char *string_free_erase(char *s);
 char *string_erase(char *x);
 
 char *string_free_erase(char *s);