passwd: initialize pointers correctly

Fix for running passwd as root (or sudo passwd $USER).
Crashed on call to free(orig) during cleanup.

Fix regression from commit 17058a06c4
Root user never changes the orig pointer, so when cleaning up, passwd tried to
free orig=(char*)""
Example: sudo passwd $USER
Changing password for xxx
New password:
Bad password: too short
Retype password:
Passwords don't match
free(): invalid pointer
Aborted

function                                             old     new   delta
passwd_main                                          958     961      +3

Signed-off-by: Einar Jón <tolvupostur@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Einar Jón 2019-01-08 16:31:37 +01:00 committed by Denys Vlasenko
parent 959b04bc0e
commit ce51140664

View File

@ -43,7 +43,7 @@
static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo) static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo)
{ {
char salt[MAX_PW_SALT_LEN]; char salt[MAX_PW_SALT_LEN];
char *orig = (char*)""; char *orig = NULL;
char *newp = NULL; char *newp = NULL;
char *cp = NULL; char *cp = NULL;
char *ret = NULL; /* failure so far */ char *ret = NULL; /* failure so far */
@ -51,7 +51,7 @@ static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo
if (myuid != 0 && pw->pw_passwd[0]) { if (myuid != 0 && pw->pw_passwd[0]) {
char *encrypted; char *encrypted;
orig = bb_ask_noecho_stdin("Old password: "); /* returns ptr to static */ orig = bb_ask_noecho_stdin("Old password: "); /* returns malloced str */
if (!orig) if (!orig)
goto err_ret; goto err_ret;
encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */ encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
@ -64,11 +64,11 @@ static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(encrypted); free(encrypted);
} }
newp = bb_ask_noecho_stdin("New password: "); /* returns ptr to static */ newp = bb_ask_noecho_stdin("New password: "); /* returns malloced str */
if (!newp) if (!newp)
goto err_ret; goto err_ret;
if (ENABLE_FEATURE_PASSWD_WEAK_CHECK if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
&& obscure(orig, newp, pw) && obscure(orig, newp, pw) /* NB: passing NULL orig is ok */
&& myuid != 0 && myuid != 0
) { ) {
goto err_ret; /* non-root is not allowed to have weak passwd */ goto err_ret; /* non-root is not allowed to have weak passwd */