Blame SOURCES/0002-pam_gdm-Use-the-last-cryptsetup-password-instead-of-.patch

eb421d
From 9f4b4ef1b5e1458ca67cff235b655060b27e357b Mon Sep 17 00:00:00 2001
eb421d
From: Graham Rogers <graham@rogers.me.uk>
eb421d
Date: Sun, 18 Apr 2021 12:22:14 +0100
eb421d
Subject: [PATCH 2/2] pam_gdm: Use the last cryptsetup password instead of the
eb421d
 first
eb421d
eb421d
---
eb421d
 pam_gdm/pam_gdm.c | 32 ++++++++++++++++++++++++++------
eb421d
 1 file changed, 26 insertions(+), 6 deletions(-)
eb421d
eb421d
diff --git a/pam_gdm/pam_gdm.c b/pam_gdm/pam_gdm.c
eb421d
index 767a6c8c..ef77f161 100644
eb421d
--- a/pam_gdm/pam_gdm.c
eb421d
+++ b/pam_gdm/pam_gdm.c
eb421d
@@ -11,75 +11,95 @@
eb421d
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
eb421d
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
eb421d
  * GNU General Public License for more details.
eb421d
  *
eb421d
  * You should have received a copy of the GNU General Public License
eb421d
  * along with this program; if not, write to the Free Software
eb421d
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
eb421d
  *
eb421d
  */
eb421d
 #include <config.h>
eb421d
 
eb421d
 #include <unistd.h>
eb421d
 
eb421d
 #include <security/_pam_macros.h>
eb421d
 #include <security/pam_ext.h>
eb421d
 #include <security/pam_misc.h>
eb421d
 #include <security/pam_modules.h>
eb421d
 #include <security/pam_modutil.h>
eb421d
 
eb421d
 #ifdef HAVE_KEYUTILS
eb421d
 #include <keyutils.h>
eb421d
 #endif
eb421d
 
eb421d
 int
eb421d
 pam_sm_authenticate (pam_handle_t  *pamh,
eb421d
                      int            flags,
eb421d
                      int            argc,
eb421d
                      const char   **argv)
eb421d
 {
eb421d
 #ifdef HAVE_KEYUTILS
eb421d
-        int r;
eb421d
-        void *cached_password = NULL;
eb421d
+        long r;
eb421d
+        size_t cached_passwords_length;
eb421d
+        char *cached_passwords = NULL;
eb421d
+        char *last_cached_password = NULL;
eb421d
         key_serial_t serial;
eb421d
+        size_t i;
eb421d
 
eb421d
         serial = find_key_by_type_and_desc ("user", "cryptsetup", 0);
eb421d
         if (serial == 0)
eb421d
                 return PAM_AUTHINFO_UNAVAIL;
eb421d
 
eb421d
-        r = keyctl_read_alloc (serial, &cached_password);
eb421d
-        if (r < 0 || r != strlen (cached_password))
eb421d
+        r = keyctl_read_alloc (serial, &cached_passwords);
eb421d
+        if (r < 0)
eb421d
                 return PAM_AUTHINFO_UNAVAIL;
eb421d
+        
eb421d
+        cached_passwords_length = r;
eb421d
+
eb421d
+        /*
eb421d
+            Find the last password in the NUL-separated list of passwords.
eb421d
+            Multiple passwords are returned either when the user enters an
eb421d
+            incorrect password or there are multiple encrypted drives.
eb421d
+            In the case of an incorrect password the last one is correct.
eb421d
+            In the case of multiple drives, choosing the last drive is as
eb421d
+            arbitrary a choice as any other, but choosing the last password at
eb421d
+            least supports multiple attempts on the last drive.
eb421d
+        */
eb421d
+        last_cached_password = cached_passwords;
eb421d
+        for (i = 0; i < cached_passwords_length; i++) {
eb421d
+                last_cached_password = cached_passwords + i;
eb421d
+                i += strlen (last_cached_password);
eb421d
+        }
eb421d
 
eb421d
-        r = pam_set_item (pamh, PAM_AUTHTOK, cached_password);
eb421d
+        r = pam_set_item (pamh, PAM_AUTHTOK, last_cached_password);
eb421d
 
eb421d
-        free (cached_password);
eb421d
+        free (cached_passwords);
eb421d
 
eb421d
         if (r < 0)
eb421d
                 return PAM_AUTH_ERR;
eb421d
         else
eb421d
                 return PAM_SUCCESS;
eb421d
 #endif
eb421d
 
eb421d
         return PAM_AUTHINFO_UNAVAIL;
eb421d
 }
eb421d
 
eb421d
 int
eb421d
 pam_sm_setcred (pam_handle_t *pamh,
eb421d
                 int           flags,
eb421d
                 int           argc,
eb421d
                 const char  **argv)
eb421d
 {
eb421d
         return PAM_SUCCESS;
eb421d
 }
eb421d
 
eb421d
 int
eb421d
 pam_sm_acct_mgmt (pam_handle_t  *pamh,
eb421d
                   int            flags,
eb421d
                   int            argc,
eb421d
                   const char   **argv)
eb421d
 {
eb421d
         return PAM_SUCCESS;
eb421d
 }
eb421d
 
eb421d
 int
eb421d
 pam_sm_chauthtok (pam_handle_t  *pamh,
eb421d
-- 
eb421d
2.35.1
eb421d