string-util: introduce explicit_zero_safe()
The only real difference is that this wrapper can deal with NULL pointer arguments, but only if the length is also zero. CID 1396277
This commit is contained in:
parent
7388cea30d
commit
87f5446311
|
@ -1059,8 +1059,11 @@ typedef void *(*memset_t)(void *,int,size_t);
|
||||||
|
|
||||||
static volatile memset_t memset_func = memset;
|
static volatile memset_t memset_func = memset;
|
||||||
|
|
||||||
void explicit_bzero(void *p, size_t l) {
|
void* explicit_bzero_safe(void *p, size_t l) {
|
||||||
memset_func(p, '\0', l);
|
if (l > 0)
|
||||||
|
memset_func(p, '\0', l);
|
||||||
|
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1070,7 +1073,7 @@ char* string_erase(char *x) {
|
||||||
|
|
||||||
/* A delicious drop of snake-oil! To be called on memory where
|
/* A delicious drop of snake-oil! To be called on memory where
|
||||||
* we stored passphrases or so, after we used them. */
|
* we stored passphrases or so, after we used them. */
|
||||||
explicit_bzero(x, strlen(x));
|
explicit_bzero_safe(x, strlen(x));
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,8 +198,15 @@ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const
|
||||||
return memmem(haystack, haystacklen, needle, needlelen);
|
return memmem(haystack, haystacklen, needle, needlelen);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !HAVE_EXPLICIT_BZERO
|
#if HAVE_EXPLICIT_BZERO
|
||||||
void explicit_bzero(void *p, size_t l);
|
static inline void* explicit_bzero_safe(void *p, size_t l) {
|
||||||
|
if (l > 0)
|
||||||
|
explicit_bzero(p, l);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
void explicit_bzero_safe(void *p, size_t l);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *string_erase(char *x);
|
char *string_erase(char *x);
|
||||||
|
|
|
@ -95,7 +95,7 @@ int main(int argc, char *argv[]) {
|
||||||
r = send_on_socket(fd, argv[2], packet, length);
|
r = send_on_socket(fd, argv[2], packet, length);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
explicit_bzero(packet, length);
|
explicit_bzero_safe(packet, length);
|
||||||
|
|
||||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
|
||||||
if (n < m)
|
if (n < m)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
explicit_bzero(p, n);
|
explicit_bzero_safe(p, n);
|
||||||
free(p);
|
free(p);
|
||||||
m *= 2;
|
m *= 2;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
|
||||||
if (!l)
|
if (!l)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
explicit_bzero(p, n);
|
explicit_bzero_safe(p, n);
|
||||||
|
|
||||||
*ret = l;
|
*ret = l;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -124,7 +124,7 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING);
|
serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING);
|
||||||
explicit_bzero(p, n);
|
explicit_bzero_safe(p, n);
|
||||||
if (serial == -1)
|
if (serial == -1)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
|
@ -349,7 +349,7 @@ int ask_password_tty(
|
||||||
if (!(flags & ASK_PASSWORD_SILENT))
|
if (!(flags & ASK_PASSWORD_SILENT))
|
||||||
backspace_string(ttyfd, passphrase);
|
backspace_string(ttyfd, passphrase);
|
||||||
|
|
||||||
explicit_bzero(passphrase, sizeof(passphrase));
|
explicit_bzero_safe(passphrase, sizeof(passphrase));
|
||||||
p = codepoint = 0;
|
p = codepoint = 0;
|
||||||
|
|
||||||
} else if (IN_SET(c, '\b', 127)) {
|
} else if (IN_SET(c, '\b', 127)) {
|
||||||
|
@ -379,7 +379,7 @@ int ask_password_tty(
|
||||||
}
|
}
|
||||||
|
|
||||||
p = codepoint = q == (size_t) -1 ? p - 1 : q;
|
p = codepoint = q == (size_t) -1 ? p - 1 : q;
|
||||||
explicit_bzero(passphrase + p, sizeof(passphrase) - p);
|
explicit_bzero_safe(passphrase + p, sizeof(passphrase) - p);
|
||||||
|
|
||||||
} else if (!dirty && !(flags & ASK_PASSWORD_SILENT)) {
|
} else if (!dirty && !(flags & ASK_PASSWORD_SILENT)) {
|
||||||
|
|
||||||
|
@ -430,7 +430,7 @@ int ask_password_tty(
|
||||||
}
|
}
|
||||||
|
|
||||||
x = strndup(passphrase, p);
|
x = strndup(passphrase, p);
|
||||||
explicit_bzero(passphrase, sizeof(passphrase));
|
explicit_bzero_safe(passphrase, sizeof(passphrase));
|
||||||
if (!x) {
|
if (!x) {
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
|
@ -681,7 +681,7 @@ int ask_password_agent(
|
||||||
l = strv_new("", NULL);
|
l = strv_new("", NULL);
|
||||||
else
|
else
|
||||||
l = strv_parse_nulstr(passphrase+1, n-1);
|
l = strv_parse_nulstr(passphrase+1, n-1);
|
||||||
explicit_bzero(passphrase, n);
|
explicit_bzero_safe(passphrase, n);
|
||||||
if (!l) {
|
if (!l) {
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
|
@ -228,7 +228,7 @@ static int ask_password_plymouth(
|
||||||
r = 0;
|
r = 0;
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
explicit_bzero(buffer, sizeof(buffer));
|
explicit_bzero_safe(buffer, sizeof(buffer));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,7 +275,7 @@ static int send_passwords(const char *socket_name, char **passwords) {
|
||||||
r = (int) n;
|
r = (int) n;
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
explicit_bzero(packet, packet_length);
|
explicit_bzero_safe(packet, packet_length);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue