]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
auth-pam: Check the user didn't change during PAM transaction
authorMarco Trevisan (Treviño) <mail@3v1n0.net>
Mon, 30 Sep 2024 11:14:11 +0000 (13:14 +0200)
committerDamien Miller <djm@mindrot.org>
Sat, 24 May 2025 07:11:23 +0000 (17:11 +1000)
PAM modules can change the user during their execution, in such case ssh
would still use the user that has been provided giving potentially
access to another user with the credentials of another one.

So prevent this to happen, by ensuring that the final PAM user is
matching the one that initiated the transaction.

auth-pam.c

index 13c0a792e99e63246b9b82fc1ba325812039edb5..2481db45fd893425ad5fee5f7093734d9e2a614a 100644 (file)
@@ -467,6 +467,32 @@ 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->pw == NULL ||
+           authctxt->pw->pw_name == NULL)
+               fatal("%s: PAM authctxt user not initialized", __func__);
+
+       if ((sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
+           (sshpam_const void **) &pam_user)) != PAM_SUCCESS)
+               return sshpam_err;
+
+       if (pam_user == NULL) {
+               debug("PAM error: PAM_USER is NULL");
+               return PAM_USER_UNKNOWN;
+       }
+
+       if (strcmp(authctxt->pw->pw_name, pam_user) != 0) {
+               debug("PAM user \"%s\" does not match expected \"%s\"",
+                     pam_user, authctxt->pw->pw_name);
+               return PAM_USER_UNKNOWN;
+       }
+       return PAM_SUCCESS;
+}
+
 /*
  * Authentication thread.
  */
@@ -521,6 +547,8 @@ sshpam_thread(void *ctxtp)
                sshpam_set_maxtries_reached(1);
        if (sshpam_err != PAM_SUCCESS)
                goto auth_fail;
+       if ((sshpam_err = check_pam_user(sshpam_authctxt)) != PAM_SUCCESS)
+               goto auth_fail;
 
        if (!do_pam_account()) {
                sshpam_err = PAM_ACCT_EXPIRED;
@@ -686,8 +714,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)
@@ -706,12 +733,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)
-                       return (0);
-               pam_end(sshpam_handle, sshpam_err);
-               sshpam_handle = NULL;
+               if ((sshpam_err = check_pam_user(authctxt)) != PAM_SUCCESS)
+                       fatal("PAM user mismatch");
        }
        debug("PAM: initializing for \"%s\" with service \"%s\"", user,
            options.pam_service_name);
@@ -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) {