Skip to content

Commit 1756049

Browse files
lib/agetpass.*: Use alloca(3) to minimize visibility of passwords
The stack should have enough space for PASS_MAX+2 allocations. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 213c6e4 commit 1756049

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

lib/agetpass.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,23 @@
88

99
#include <config.h>
1010

11+
#include <alloca.h>
1112
#include <errno.h>
1213
#include <limits.h>
1314
#include <readpassphrase.h>
1415
#include <stddef.h>
15-
#include <stdlib.h>
1616
#include <string.h>
1717

1818
#include "alloc/malloc.h"
1919
#include "attr.h"
20-
21-
#if WITH_LIBBSD == 0
22-
#include "freezero.h"
23-
#endif /* WITH_LIBBSD */
20+
#include "string/memset/memzero.h"
2421

2522

2623
// Similar to getpass(3), but free of its problems.
2724
#define agetpass(prompt) agetpass_(prompt, RPP_REQUIRE_TTY)
2825
#define agetpass_stdin() agetpass_(NULL, RPP_STDIN)
2926

30-
#define agetpass_(...) getpass_(MALLOC(PASS_MAX + 2, char), __VA_ARGS__)
27+
#define agetpass_(...) getpass_(alloca(PASS_MAX + 2), __VA_ARGS__)
3128

3229

3330
inline void erase_pass(char *pass);
@@ -40,9 +37,6 @@ getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags)
4037
{
4138
size_t len;
4239

43-
if (pass == NULL)
44-
return NULL;
45-
4640
/*
4741
* Since we want to support passwords upto PASS_MAX, we need
4842
* PASS_MAX bytes for the password itself, and one more byte for
@@ -63,15 +57,15 @@ getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags)
6357
return pass;
6458

6559
fail:
66-
freezero(pass, PASS_MAX + 2);
60+
memzero(pass, PASS_MAX + 2);
6761
return NULL;
6862
}
6963

7064

7165
inline void
7266
erase_pass(char *pass)
7367
{
74-
freezero(pass, PASS_MAX + 2);
68+
memzero(pass, PASS_MAX + 2);
7569
}
7670

7771

0 commit comments

Comments
 (0)