Check UUID of active device to match header when initializing context.
authorMilan Broz <gmazyland@gmail.com>
Mon, 15 Aug 2011 16:32:44 +0000 (16:32 +0000)
committerMilan Broz <gmazyland@gmail.com>
Mon, 15 Aug 2011 16:32:44 +0000 (16:32 +0000)
git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@597 36d66b0a-2a48-0410-832c-cd162a569da5

lib/setup.c

index c61d066..e3586b1 100644 (file)
@@ -219,6 +219,36 @@ static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
        return 0;
 }
 
+/*
+ * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
+ */
+static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
+{
+       int i, j;
+       char *str;
+
+       if (!dm_uuid || !hdr_uuid)
+               return -EINVAL;
+
+       str = strchr(dm_uuid, '-');
+       if (!str)
+               return -EINVAL;
+
+       for (i = 0, j = 1; hdr_uuid[i]; i++) {
+               if (hdr_uuid[i] == '-')
+                       continue;
+
+               if (!str[j] || str[j] == '-')
+                       return -EINVAL;
+
+               if (str[j] != hdr_uuid[i])
+                       return -EINVAL;
+               j++;
+       }
+
+       return 0;
+}
+
 int PLAIN_activate(struct crypt_device *cd,
                     const char *name,
                     struct volume_key *vk,
@@ -642,6 +672,16 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
                                r = 0;
                                goto out;
                        }
+                       /* checks whether UUIDs match each other */
+                       r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
+                       if (r < 0) {
+                               log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
+                                       (*cd)->hdr.uuid, dmd.uuid);
+                               free((*cd)->type);
+                               (*cd)->type = NULL;
+                               r = 0;
+                               goto out;
+                       }
                }
        }