kcheckpass: actually set and restore effective user ID if required

it did not had to be SUID to begin with because neither setuid() nor
seteuid() were used previously in any of the backends, fixes password check
with shadow backend

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-02-22 06:38:54 +02:00
parent 951ca60c44
commit 9b8fd856be

View file

@ -35,6 +35,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <pwd.h> #include <pwd.h>
#include <shadow.h> #include <shadow.h>
#include <unistd.h>
#include <sys/types.h>
AuthReturn Authenticate(const char *method, AuthReturn Authenticate(const char *method,
const char *login, char *(*conv) (ConvRequest, const char *)) const char *login, char *(*conv) (ConvRequest, const char *))
@ -51,14 +53,22 @@ AuthReturn Authenticate(const char *method,
if (!(pw = getpwnam(login))) if (!(pw = getpwnam(login)))
return AuthAbort; return AuthAbort;
uid_t eid = geteuid();
if (eid != 0 && seteuid(0) != 0)
return AuthAbort;
spw = getspnam(login); spw = getspnam(login);
password = spw ? spw->sp_pwdp : pw->pw_passwd; password = spw ? spw->sp_pwdp : pw->pw_passwd;
if (!*password) if (!*password) {
seteuid(eid);
return AuthOk; return AuthOk;
}
if (!(typed_in_password = conv(ConvGetHidden, 0))) if (!(typed_in_password = conv(ConvGetHidden, 0))) {
seteuid(eid);
return AuthAbort; return AuthAbort;
}
#if defined( __linux__ ) && defined( HAVE_PW_ENCRYPT ) #if defined( __linux__ ) && defined( HAVE_PW_ENCRYPT )
crpt_passwd = pw_encrypt(typed_in_password, password); /* (1) */ crpt_passwd = pw_encrypt(typed_in_password, password); /* (1) */
@ -67,10 +77,12 @@ AuthReturn Authenticate(const char *method,
#endif #endif
if (crpt_passwd && !strcmp(password, crpt_passwd )) { if (crpt_passwd && !strcmp(password, crpt_passwd )) {
seteuid(eid);
dispose(typed_in_password); dispose(typed_in_password);
return AuthOk; /* Success */ return AuthOk; /* Success */
} }
dispose(typed_in_password); dispose(typed_in_password);
seteuid(eid);
return AuthBad; /* Password wrong or account locked */ return AuthBad; /* Password wrong or account locked */
} }