]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: Add _cleanup_strv_free_erase_ and _cleanup_string_free_erase_
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 15 Oct 2015 14:02:35 +0000 (10:02 -0400)
committerLennart Poettering <lennart@poettering.net>
Mon, 19 Oct 2015 21:13:07 +0000 (23:13 +0200)
src/ask-password/ask-password.c
src/basic/strv.c
src/basic/strv.h
src/basic/util.c
src/basic/util.h
src/cryptsetup/cryptsetup.c
src/firstboot/firstboot.c
src/shared/ask-password-api.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 89a49c2e86fd9c12075c0369e37dfe5987f9f388..a5448660003e23c3d2c83473ef5b94946ab6a534 100644 (file)
@@ -144,7 +144,7 @@ static int parse_argv(int argc, char *argv[]) {
 }
 
 int main(int argc, char *argv[]) {
-        _cleanup_strv_free_ char **l = NULL;
+        _cleanup_strv_free_erase_ char **l = NULL;
         usec_t timeout;
         char **p;
         int r;
@@ -174,8 +174,6 @@ int main(int argc, char *argv[]) {
                         break;
         }
 
-        strv_erase(l);
-
 finish:
         free(arg_message);
 
index b66c176487747d312ee7295215ba19d7e58be46d..501d022cb929ac935a0def731a9ee143fe8d72ae 100644 (file)
@@ -86,6 +86,15 @@ char **strv_free(char **l) {
         return NULL;
 }
 
+char **strv_free_erase(char **l) {
+        char **i;
+
+        STRV_FOREACH(i, l)
+                string_erase(*i);
+
+        return strv_free(l);
+}
+
 char **strv_copy(char * const *l) {
         char **r, **k;
 
index e49f443835797b458700dcf25b7801044c3defcd..a5dc696a87c9b784d3b1beff308a58aa83bbaa10 100644 (file)
@@ -35,6 +35,10 @@ char **strv_free(char **l);
 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
 #define _cleanup_strv_free_ _cleanup_(strv_freep)
 
+char **strv_free_erase(char **l);
+DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
+#define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
+
 void strv_clear(char **l);
 
 char **strv_copy(char * const *l);
index f24db9796e24a1a1383e45861cd58ec35fcb05cb..a14ed2e4cc431f0b60b20b804a2b4ca7b301999f 100644 (file)
@@ -6817,9 +6817,10 @@ void string_erase(char *x) {
         memory_erase(x, strlen(x));
 }
 
-void strv_erase(char **l) {
-        char **i;
+char *string_free_erase(char *s) {
+        if (!s)
+                return NULL;
 
-        STRV_FOREACH(i, l)
-                string_erase(*i);
+        string_erase(s);
+        return mfree(s);
 }
index b1c64675e030507abad3540abebcc6ac715f8fd4..4b1c5878c52f8d3ef519bb9f13419c8f1a2a634a 100644 (file)
@@ -946,4 +946,7 @@ bool oom_score_adjust_is_valid(int oa);
 
 #define memory_erase(p, l) memset((p), 'x', (l))
 void string_erase(char *x);
-void strv_erase(char **l);
+
+char *string_free_erase(char *s);
+DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
+#define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
index c9be17446bd1fccc185f880870717df636fe2cab..ecc1273eec3904a0994bde8d050e18ad6bd99d35 100644 (file)
@@ -314,7 +314,7 @@ static char *disk_mount_point(const char *label) {
 
 static int get_password(const char *vol, const char *src, usec_t until, bool accept_cached, char ***ret) {
         _cleanup_free_ char *description = NULL, *name_buffer = NULL, *mount_point = NULL, *maj_min = NULL, *text = NULL, *escaped_name = NULL;
-        _cleanup_strv_free_ char **passwords = NULL, **passwords2 = NULL;
+        _cleanup_strv_free_erase_ char **passwords = NULL;
         const char *name = NULL;
         char **p, *id;
         int r = 0;
@@ -361,32 +361,31 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
 
         id = strjoina("cryptsetup:", escaped_name);
 
-        r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE|(accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0), &passwords);
+        r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until,
+                              ASK_PASSWORD_PUSH_CACHE | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED),
+                              &passwords);
         if (r < 0)
                 return log_error_errno(r, "Failed to query password: %m");
 
         if (arg_verify) {
+                _cleanup_strv_free_erase_ char **passwords2 = NULL;
+
                 assert(strv_length(passwords) == 1);
 
-                if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) {
-                        r = log_oom();
-                        goto finish;
-                }
+                if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0)
+                        return log_oom();
 
                 id = strjoina("cryptsetup-verification:", escaped_name);
 
                 r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to query verification password: %m");
-                        goto finish;
-                }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to query verification password: %m");
 
                 assert(strv_length(passwords2) == 1);
 
                 if (!streq(passwords[0], passwords2[0])) {
                         log_warning("Passwords did not match, retrying.");
-                        r = -EAGAIN;
-                        goto finish;
+                        return -EAGAIN;
                 }
         }
 
@@ -400,10 +399,8 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
 
                 /* Pad password if necessary */
                 c = new(char, arg_key_size);
-                if (!c) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                if (!c)
+                        return log_oom();
 
                 strncpy(c, *p, arg_key_size);
                 free(*p);
@@ -413,13 +410,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
         *ret = passwords;
         passwords = NULL;
 
-        r = 0;
-
-finish:
-        strv_erase(passwords);
-        strv_erase(passwords2);
-
-        return r;
+        return 0;
 }
 
 static int attach_tcrypt(
@@ -683,7 +674,7 @@ int main(int argc, char *argv[]) {
                 }
 
                 for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
-                        _cleanup_strv_free_ char **passwords = NULL;
+                        _cleanup_strv_free_erase_ char **passwords = NULL;
 
                         if (!key_file) {
                                 k = get_password(argv[2], argv[3], until, tries == 0 && !arg_verify, &passwords);
@@ -702,7 +693,6 @@ int main(int argc, char *argv[]) {
                                                          arg_header ? argv[3] : NULL,
                                                          passwords,
                                                          flags);
-                        strv_erase(passwords);
                         if (k >= 0)
                                 break;
                         else if (k == -EAGAIN) {
index da247fbef85e56d66719414afdeca005d9e51c3d..82ebb9178861203d10ac18d3e1721bbe63501bdb 100644 (file)
@@ -455,7 +455,7 @@ static int prompt_root_password(void) {
         msg2 = strjoina(draw_special_char(DRAW_TRIANGULAR_BULLET), " Please enter new root password again: ");
 
         for (;;) {
-                _cleanup_free_ char *a = NULL, *b = NULL;
+                _cleanup_string_free_erase_ char *a = NULL, *b = NULL;
 
                 r = ask_password_tty(msg1, NULL, 0, 0, NULL, &a);
                 if (r < 0)
@@ -467,19 +467,14 @@ static int prompt_root_password(void) {
                 }
 
                 r = ask_password_tty(msg2, NULL, 0, 0, NULL, &b);
-                if (r < 0) {
-                        string_erase(a);
+                if (r < 0)
                         return log_error_errno(r, "Failed to query root password: %m");
-                }
 
                 if (!streq(a, b)) {
                         log_error("Entered passwords did not match, please try again.");
-                        string_erase(a);
-                        string_erase(b);
                         continue;
                 }
 
-                string_erase(b);
                 arg_root_password = a;
                 a = NULL;
                 break;
index e35594a5df9c1e5644931e3ea98d9dc236d3e011..ddf42f11e1f526ab3e97fb5e6e0e35ca3259a0bb 100644 (file)
@@ -94,7 +94,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
 }
 
 static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) {
-        _cleanup_strv_free_ char **l = NULL;
+        _cleanup_strv_free_erase_ char **l = NULL;
         _cleanup_free_ char *p = NULL;
         key_serial_t serial;
         size_t n;
@@ -119,7 +119,6 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
                 return r;
 
         r = strv_make_nulstr(l, &p, &n);
-        strv_erase(l);
         if (r < 0)
                 return r;
 
index 7a5ac9fa9c6ffb89c699c5b6db42bc507af83e8d..8423364046693164f02d0bc27fae78cc9065a282 100644 (file)
@@ -307,7 +307,7 @@ static int parse_password(const char *filename, char **wall) {
                 }
 
                 if (arg_plymouth) {
-                        _cleanup_strv_free_ char **passwords = NULL;
+                        _cleanup_strv_free_erase_ char **passwords = NULL;
 
                         r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords);
                         if (r >= 0) {
@@ -330,10 +330,8 @@ static int parse_password(const char *filename, char **wall) {
                                 }
                         }
 
-                        strv_erase(passwords);
-
                 } else {
-                        _cleanup_free_ char *password = NULL;
+                        _cleanup_string_free_erase_ char *password = NULL;
                         int tty_fd = -1;
 
                         if (arg_console) {
@@ -363,8 +361,6 @@ static int parse_password(const char *filename, char **wall) {
                                         strcpy(packet + 1, password);
                                 }
                         }
-
-                        string_erase(password);
                 }
 
                 if (IN_SET(r, -ETIME, -ENOENT)) {