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

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