strv: Add _cleanup_strv_free_erase_ and _cleanup_string_free_erase_

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2015-10-15 10:02:35 -04:00 committed by Lennart Poettering
parent 1602b00853
commit ab84f5b95e
9 changed files with 43 additions and 48 deletions

View file

@ -144,7 +144,7 @@ static int parse_argv(int argc, char *argv[]) {
} }
int main(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; usec_t timeout;
char **p; char **p;
int r; int r;
@ -174,8 +174,6 @@ int main(int argc, char *argv[]) {
break; break;
} }
strv_erase(l);
finish: finish:
free(arg_message); free(arg_message);

View file

@ -86,6 +86,15 @@ char **strv_free(char **l) {
return NULL; 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 **strv_copy(char * const *l) {
char **r, **k; char **r, **k;

View file

@ -35,6 +35,10 @@ char **strv_free(char **l);
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free); DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
#define _cleanup_strv_free_ _cleanup_(strv_freep) #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); void strv_clear(char **l);
char **strv_copy(char * const *l); char **strv_copy(char * const *l);

View file

@ -6817,9 +6817,10 @@ void string_erase(char *x) {
memory_erase(x, strlen(x)); memory_erase(x, strlen(x));
} }
void strv_erase(char **l) { char *string_free_erase(char *s) {
char **i; if (!s)
return NULL;
STRV_FOREACH(i, l) string_erase(s);
string_erase(*i); return mfree(s);
} }

View file

@ -946,4 +946,7 @@ bool oom_score_adjust_is_valid(int oa);
#define memory_erase(p, l) memset((p), 'x', (l)) #define memory_erase(p, l) memset((p), 'x', (l))
void string_erase(char *x); 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)

View 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) { 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_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; const char *name = NULL;
char **p, *id; char **p, *id;
int r = 0; 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); 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) if (r < 0)
return log_error_errno(r, "Failed to query password: %m"); return log_error_errno(r, "Failed to query password: %m");
if (arg_verify) { if (arg_verify) {
_cleanup_strv_free_erase_ char **passwords2 = NULL;
assert(strv_length(passwords) == 1); assert(strv_length(passwords) == 1);
if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) { if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0)
r = log_oom(); return log_oom();
goto finish;
}
id = strjoina("cryptsetup-verification:", escaped_name); id = strjoina("cryptsetup-verification:", escaped_name);
r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2); r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2);
if (r < 0) { if (r < 0)
log_error_errno(r, "Failed to query verification password: %m"); return log_error_errno(r, "Failed to query verification password: %m");
goto finish;
}
assert(strv_length(passwords2) == 1); assert(strv_length(passwords2) == 1);
if (!streq(passwords[0], passwords2[0])) { if (!streq(passwords[0], passwords2[0])) {
log_warning("Passwords did not match, retrying."); log_warning("Passwords did not match, retrying.");
r = -EAGAIN; return -EAGAIN;
goto finish;
} }
} }
@ -400,10 +399,8 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
/* Pad password if necessary */ /* Pad password if necessary */
c = new(char, arg_key_size); c = new(char, arg_key_size);
if (!c) { if (!c)
r = -ENOMEM; return log_oom();
goto finish;
}
strncpy(c, *p, arg_key_size); strncpy(c, *p, arg_key_size);
free(*p); free(*p);
@ -413,13 +410,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
*ret = passwords; *ret = passwords;
passwords = NULL; passwords = NULL;
r = 0; return 0;
finish:
strv_erase(passwords);
strv_erase(passwords2);
return r;
} }
static int attach_tcrypt( static int attach_tcrypt(
@ -683,7 +674,7 @@ int main(int argc, char *argv[]) {
} }
for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) { 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) { if (!key_file) {
k = get_password(argv[2], argv[3], until, tries == 0 && !arg_verify, &passwords); 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, arg_header ? argv[3] : NULL,
passwords, passwords,
flags); flags);
strv_erase(passwords);
if (k >= 0) if (k >= 0)
break; break;
else if (k == -EAGAIN) { else if (k == -EAGAIN) {

View 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: "); msg2 = strjoina(draw_special_char(DRAW_TRIANGULAR_BULLET), " Please enter new root password again: ");
for (;;) { 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); r = ask_password_tty(msg1, NULL, 0, 0, NULL, &a);
if (r < 0) if (r < 0)
@ -467,19 +467,14 @@ static int prompt_root_password(void) {
} }
r = ask_password_tty(msg2, NULL, 0, 0, NULL, &b); r = ask_password_tty(msg2, NULL, 0, 0, NULL, &b);
if (r < 0) { if (r < 0)
string_erase(a);
return log_error_errno(r, "Failed to query root password: %m"); return log_error_errno(r, "Failed to query root password: %m");
}
if (!streq(a, b)) { if (!streq(a, b)) {
log_error("Entered passwords did not match, please try again."); log_error("Entered passwords did not match, please try again.");
string_erase(a);
string_erase(b);
continue; continue;
} }
string_erase(b);
arg_root_password = a; arg_root_password = a;
a = NULL; a = NULL;
break; break;

View 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) { 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; _cleanup_free_ char *p = NULL;
key_serial_t serial; key_serial_t serial;
size_t n; size_t n;
@ -119,7 +119,6 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
return r; return r;
r = strv_make_nulstr(l, &p, &n); r = strv_make_nulstr(l, &p, &n);
strv_erase(l);
if (r < 0) if (r < 0)
return r; return r;

View file

@ -307,7 +307,7 @@ static int parse_password(const char *filename, char **wall) {
} }
if (arg_plymouth) { 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); r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords);
if (r >= 0) { if (r >= 0) {
@ -330,10 +330,8 @@ static int parse_password(const char *filename, char **wall) {
} }
} }
strv_erase(passwords);
} else { } else {
_cleanup_free_ char *password = NULL; _cleanup_string_free_erase_ char *password = NULL;
int tty_fd = -1; int tty_fd = -1;
if (arg_console) { if (arg_console) {
@ -363,8 +361,6 @@ static int parse_password(const char *filename, char **wall) {
strcpy(packet + 1, password); strcpy(packet + 1, password);
} }
} }
string_erase(password);
} }
if (IN_SET(r, -ETIME, -ENOENT)) { if (IN_SET(r, -ETIME, -ENOENT)) {