]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
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)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Feb 2017 02:07:55 +0000 (21:07 -0500)
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.

configure.ac
src/basic/string-util.c
src/basic/string-util.h
src/reply-password/reply-password.c
src/shared/ask-password-api.c
src/test/test-string-util.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index b9143d28ca65b44a21bdeb69e4d6f52247f32b75..ab1d17c53177e8a4fe884979f43bf2207a518c3d 100644 (file)
@@ -331,13 +331,15 @@ AC_CHECK_DECLS([
         kcmp,
         keyctl,
         LO_FLAGS_PARTSCAN,
-        copy_file_range],
+        copy_file_range,
+        explicit_bzero],
         [], [], [[
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/mount.h>
 #include <fcntl.h>
 #include <sched.h>
+#include <string.h>
 #include <linux/loop.h>
 #include <linux/random.h>
 ]])
index 2ba3604ba0bb5d0b5f37b3c0474253abe731f017..9d2f4bc8f9ef7e2ad4555a5322ff790e0fa524bc 100644 (file)
@@ -821,6 +821,7 @@ int free_and_strdup(char **p, const char *s) {
         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
@@ -831,19 +832,19 @@ typedef void *(*memset_t)(void *,int,size_t);
 
 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) {
-
         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) {
index e99f7964be5f38e314a8b5d62c46239c73824d9d..be44dedff438e2d48d9be8c6b37ebe141e533bde 100644 (file)
@@ -189,7 +189,10 @@ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const
         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);
index 17eab9772e25313fb8ed8f5a781b3a8aca0ad121..a17c8a62b82f891d62da24ce209dacfb19aea034 100644 (file)
@@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
         r = send_on_socket(fd, argv[2], packet, length);
 
 finish:
-        memory_erase(packet, sizeof(packet));
+        explicit_bzero(packet, sizeof(packet));
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
index 3e877920da96174402f2b25616ca3920c1790c26..e3b29e390cc19fefc9ac53223f03866206910365 100644 (file)
@@ -95,7 +95,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
                 if (n < m)
                         break;
 
-                memory_erase(p, n);
+                explicit_bzero(p, n);
                 free(p);
                 m *= 2;
         }
@@ -104,7 +104,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
         if (!l)
                 return -ENOMEM;
 
-        memory_erase(p, n);
+        explicit_bzero(p, n);
 
         *ret = l;
         return 0;
@@ -140,7 +140,7 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
                 return r;
 
         serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING);
-        memory_erase(p, n);
+        explicit_bzero(p, n);
         if (serial == -1)
                 return -errno;
 
@@ -390,7 +390,7 @@ int ask_password_tty(
         }
 
         x = strndup(passphrase, p);
-        memory_erase(passphrase, p);
+        explicit_bzero(passphrase, p);
         if (!x) {
                 r = -ENOMEM;
                 goto finish;
@@ -647,7 +647,7 @@ int ask_password_agent(
                                 l = strv_new("", NULL);
                         else
                                 l = strv_parse_nulstr(passphrase+1, n-1);
-                        memory_erase(passphrase, n);
+                        explicit_bzero(passphrase, n);
                         if (!l) {
                                 r = -ENOMEM;
                                 goto finish;
index e43373b0f5967c845fc9a698c16fdcbeb35d2c93..4b3e924cfb3cd1eb6c25d24f716b8c45e1db3d1e 100644 (file)
@@ -29,31 +29,20 @@ static void test_string_erase(void) {
         assert_se(streq(string_erase(x), ""));
 
         x = strdupa("1");
-        assert_se(streq(string_erase(x), "x"));
-
-        x = strdupa("12");
-        assert_se(streq(string_erase(x), "xx"));
-
-        x = strdupa("123");
-        assert_se(streq(string_erase(x), "xxx"));
-
-        x = strdupa("1234");
-        assert_se(streq(string_erase(x), "xxxx"));
-
-        x = strdupa("12345");
-        assert_se(streq(string_erase(x), "xxxxx"));
-
-        x = strdupa("123456");
-        assert_se(streq(string_erase(x), "xxxxxx"));
-
-        x = strdupa("1234567");
-        assert_se(streq(string_erase(x), "xxxxxxx"));
-
-        x = strdupa("12345678");
-        assert_se(streq(string_erase(x), "xxxxxxxx"));
+        assert_se(streq(string_erase(x), ""));
 
         x = strdupa("123456789");
-        assert_se(streq(string_erase(x), "xxxxxxxxx"));
+        assert_se(streq(string_erase(x), ""));
+
+        assert_se(x[1] == '\0');
+        assert_se(x[2] == '\0');
+        assert_se(x[3] == '\0');
+        assert_se(x[4] == '\0');
+        assert_se(x[5] == '\0');
+        assert_se(x[6] == '\0');
+        assert_se(x[7] == '\0');
+        assert_se(x[8] == '\0');
+        assert_se(x[9] == '\0');
 }
 
 static void test_ascii_strcasecmp_n(void) {
index b45490be1ac39868cb370f3034c670973e4d9e61..a17c006d57bd2b5f8fe04d5fa8ace844cf24a430 100644 (file)
@@ -243,7 +243,7 @@ static int ask_password_plymouth(
         r = 0;
 
 finish:
-        memory_erase(buffer, sizeof(buffer));
+        explicit_bzero(buffer, sizeof(buffer));
         return r;
 }
 
@@ -283,7 +283,7 @@ static int send_passwords(const char *socket_name, char **passwords) {
                 r = log_debug_errno(errno, "sendto(): %m");
 
 finish:
-        memory_erase(packet, packet_length);
+        explicit_bzero(packet, packet_length);
         return r;
 }