Skip to content

Commit c71d14e

Browse files
cgzonesjwcart2
authored andcommitted
newrole: ensure password memory erasure
Compiler can optimize calls to memset(3), due to the as-if rule, away if the object is not accessed later on. Use a wrapper using volatile pointers to ensure the memory is guaranteed to be erased. Also erase the encrypted password. Signed-off-by: Christian Göttsche <[email protected]>
1 parent 1af8089 commit c71d14e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

policycoreutils/newrole/newrole.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ static int read_pam_config(void)
333333

334334
#define PASSWORD_PROMPT _("Password:") /* prompt for getpass() */
335335

336+
static void memzero(void *ptr, size_t size)
337+
{
338+
volatile unsigned char * volatile p = ptr;
339+
while (size--) {
340+
*p++ = '\0';
341+
}
342+
}
343+
336344
/* authenticate_via_shadow_passwd()
337345
*
338346
* in: uname - the calling user's user name
@@ -351,6 +359,7 @@ static int authenticate_via_shadow_passwd(const char *uname)
351359
struct spwd *p_shadow_line;
352360
char *unencrypted_password_s;
353361
char *encrypted_password_s;
362+
int ret;
354363

355364
setspent();
356365
p_shadow_line = getspnam(uname);
@@ -371,12 +380,15 @@ static int authenticate_via_shadow_passwd(const char *uname)
371380
errno = 0;
372381
encrypted_password_s = crypt(unencrypted_password_s,
373382
p_shadow_line->sp_pwdp);
374-
memset(unencrypted_password_s, 0, strlen(unencrypted_password_s));
383+
memzero(unencrypted_password_s, strlen(unencrypted_password_s));
375384
if (errno || !encrypted_password_s) {
376385
fprintf(stderr, _("Cannot encrypt password.\n"));
377386
return 0;
378387
}
379-
return (!strcmp(encrypted_password_s, p_shadow_line->sp_pwdp));
388+
389+
ret = !strcmp(encrypted_password_s, p_shadow_line->sp_pwdp);
390+
memzero(encrypted_password_s, strlen(encrypted_password_s));
391+
return ret;
380392
}
381393
#endif /* if/else USE_PAM */
382394

0 commit comments

Comments
 (0)