Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth-pam: Check the user didn't change during PAM transaction #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions auth-pam.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,28 @@ sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
return (PAM_CONV_ERR);
}

static int
check_pam_user(Authctxt *authctxt)
{
const char *pam_user;

if (authctxt == NULL || authctxt->user == NULL)
fatal("%s: PAM authctxt user not initialized", __func__);

sshpam_err = pam_get_item(sshpam_handle,
PAM_USER, (sshpam_const void **) &pam_user);
if (sshpam_err != PAM_SUCCESS)
return sshpam_err;

if (pam_user == NULL || strcmp(authctxt->user, pam_user) != 0) {
debug("PAM: User '%s' does not match expected '%s'",
pam_user, authctxt->user);
return PAM_USER_UNKNOWN;
}

return PAM_SUCCESS;
}

/*
* Authentication thread.
*/
Expand Down Expand Up @@ -521,6 +543,9 @@ sshpam_thread(void *ctxtp)
sshpam_set_maxtries_reached(1);
if (sshpam_err != PAM_SUCCESS)
goto auth_fail;
sshpam_err = check_pam_user(sshpam_authctxt);
if (sshpam_err != PAM_SUCCESS)
goto auth_fail;

if (!do_pam_account()) {
sshpam_err = PAM_ACCT_EXPIRED;
Expand Down Expand Up @@ -686,8 +711,7 @@ sshpam_cleanup(void)
static int
sshpam_init(struct ssh *ssh, Authctxt *authctxt)
{
const char *pam_user, *user = authctxt->user;
const char **ptr_pam_user = &pam_user;
const char *user = authctxt->user;
int r;

if (options.pam_service_name == NULL)
Expand All @@ -706,9 +730,8 @@ sshpam_init(struct ssh *ssh, Authctxt *authctxt)
}
if (sshpam_handle != NULL) {
/* We already have a PAM context; check if the user matches */
sshpam_err = pam_get_item(sshpam_handle,
PAM_USER, (sshpam_const void **)ptr_pam_user);
if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
sshpam_err = check_pam_user(authctxt);
if (sshpam_err == PAM_SUCCESS)
return (0);
pam_end(sshpam_handle, sshpam_err);
sshpam_handle = NULL;
Expand Down Expand Up @@ -1378,6 +1401,8 @@ sshpam_auth_passwd(Authctxt *authctxt, const char *password)
sshpam_err = pam_authenticate(sshpam_handle, flags);
sshpam_password = NULL;
free(fake);
if (sshpam_err == PAM_SUCCESS)
sshpam_err = check_pam_user(authctxt);
if (sshpam_err == PAM_MAXTRIES)
sshpam_set_maxtries_reached(1);
if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Expand Down
Loading