Blame SOURCES/cryptsetup-2.0.4-make-LUKS2-auto-recovery-aware-of-device-signatures.patch

ac737d
From 078ed81d14904f48a6237646050ba5eb74d702b7 Mon Sep 17 00:00:00 2001
ac737d
From: Ondrej Kozina <okozina@redhat.com>
ac737d
Date: Wed, 4 Jul 2018 15:58:09 +0200
ac737d
Subject: [PATCH 2/6] Make LUKS2 auto-recovery aware of device signatures.
ac737d
ac737d
auto-recovery triggers any time when only single correct LUKS2
ac737d
header instance was found. That may be dangerous.
ac737d
ac737d
We should suppress auto-recovery in case blkid decided the
ac737d
device is no longer LUKS device. For example if secondary (intact)
ac737d
LUKS2 header was left behind and blkid declares the device is LVM2
ac737d
member.
ac737d
ac737d
Moreover if at least one header instance is corrupted and blkid
ac737d
declares device non-empty and non-LUKS in the same time, header load
ac737d
operation will be aborted with error.
ac737d
---
ac737d
 lib/internal.h                  |  1 +
ac737d
 lib/luks2/luks2_disk_metadata.c | 61 ++++++++++++++++++++++++++++++++++++++++-
ac737d
 lib/luks2/luks2_internal.h      |  2 +-
ac737d
 lib/luks2/luks2_json_metadata.c |  4 +--
ac737d
 4 files changed, 64 insertions(+), 4 deletions(-)
ac737d
ac737d
diff --git a/lib/internal.h b/lib/internal.h
ac737d
index 07a1a08..e6d2323 100644
ac737d
--- a/lib/internal.h
ac737d
+++ b/lib/internal.h
ac737d
@@ -32,6 +32,7 @@
ac737d
 
ac737d
 #include "nls.h"
ac737d
 #include "bitops.h"
ac737d
+#include "utils_blkid.h"
ac737d
 #include "utils_crypt.h"
ac737d
 #include "utils_loop.h"
ac737d
 #include "utils_dm.h"
ac737d
diff --git a/lib/luks2/luks2_disk_metadata.c b/lib/luks2/luks2_disk_metadata.c
ac737d
index 4d9bce2..6ca9d5e 100644
ac737d
--- a/lib/luks2/luks2_disk_metadata.c
ac737d
+++ b/lib/luks2/luks2_disk_metadata.c
ac737d
@@ -531,12 +531,59 @@ static json_object *parse_and_validate_json(const char *json_area, int length)
ac737d
 	return jobj;
ac737d
 }
ac737d
 
ac737d
+static int detect_device_signatures(const char *path)
ac737d
+{
ac737d
+	blk_probe_status prb_state;
ac737d
+	int r;
ac737d
+	struct blkid_handle *h;
ac737d
+
ac737d
+	if (!blk_supported()) {
ac737d
+		log_dbg("Blkid probing of device signatures disabled.");
ac737d
+		return 0;
ac737d
+	}
ac737d
+
ac737d
+	if ((r = blk_init_by_path(&h, path))) {
ac737d
+		log_dbg("Failed to initialize blkid_handle by path.");
ac737d
+		return -EINVAL;
ac737d
+	}
ac737d
+
ac737d
+	/* We don't care about details. Be fast. */
ac737d
+	blk_set_chains_for_fast_detection(h);
ac737d
+
ac737d
+	/* Filter out crypto_LUKS. we don't care now */
ac737d
+	blk_superblocks_filter_luks(h);
ac737d
+
ac737d
+	prb_state = blk_safeprobe(h);
ac737d
+
ac737d
+	switch (prb_state) {
ac737d
+	case PRB_AMBIGUOUS:
ac737d
+		log_dbg("Blkid probe couldn't decide device type unambiguously.");
ac737d
+		/* fall through */
ac737d
+	case PRB_FAIL:
ac737d
+		log_dbg("Blkid probe failed.");
ac737d
+		r = -EINVAL;
ac737d
+		break;
ac737d
+	case PRB_OK: /* crypto_LUKS type is filtered out */
ac737d
+		r = -EINVAL;
ac737d
+
ac737d
+		if (blk_is_partition(h))
ac737d
+			log_dbg("Blkid probe detected partition type '%s'", blk_get_partition_type(h));
ac737d
+		else if (blk_is_superblock(h))
ac737d
+			log_dbg("blkid probe detected superblock type '%s'", blk_get_superblock_type(h));
ac737d
+		break;
ac737d
+	case PRB_EMPTY:
ac737d
+		log_dbg("Blkid probe detected no foreign device signature.");
ac737d
+	}
ac737d
+	blk_free(h);
ac737d
+	return r;
ac737d
+}
ac737d
+
ac737d
 /*
ac737d
  * Read and convert on-disk LUKS2 header to in-memory representation..
ac737d
  * Try to do recovery if on-disk state is not consistent.
ac737d
  */
ac737d
 int LUKS2_disk_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr,
ac737d
-			struct device *device, int do_recovery)
ac737d
+			struct device *device, int do_recovery, int do_blkprobe)
ac737d
 {
ac737d
 	enum { HDR_OK, HDR_OBSOLETE, HDR_FAIL, HDR_FAIL_IO } state_hdr1, state_hdr2;
ac737d
 	struct luks2_hdr_disk hdr_disk1, hdr_disk2;
ac737d
@@ -616,6 +663,12 @@ int LUKS2_disk_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr,
ac737d
 	if (state_hdr1 == HDR_OK && state_hdr2 != HDR_OK) {
ac737d
 		log_dbg("Secondary LUKS2 header requires recovery.");
ac737d
 
ac737d
+		if (do_blkprobe && (r = detect_device_signatures(device_path(device)))) {
ac737d
+			log_err(cd, _("Device contains ambiguous signatures, cannot auto-recover LUKS2.\n"
ac737d
+				      "Please run \"cryptsetup repair\" for recovery."));
ac737d
+			goto err;
ac737d
+		}
ac737d
+
ac737d
 		if (do_recovery) {
ac737d
 			memcpy(&hdr_disk2, &hdr_disk1, LUKS2_HDR_BIN_LEN);
ac737d
 			r = crypt_random_get(NULL, (char*)hdr_disk2.salt, sizeof(hdr_disk2.salt), CRYPT_RND_SALT);
ac737d
@@ -631,6 +684,12 @@ int LUKS2_disk_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr,
ac737d
 	} else if (state_hdr1 != HDR_OK && state_hdr2 == HDR_OK) {
ac737d
 		log_dbg("Primary LUKS2 header requires recovery.");
ac737d
 
ac737d
+		if (do_blkprobe && (r = detect_device_signatures(device_path(device)))) {
ac737d
+			log_err(cd, _("Device contains ambiguous signatures, cannot auto-recover LUKS2.\n"
ac737d
+				      "Please run \"cryptsetup repair\" for recovery."));
ac737d
+			goto err;
ac737d
+		}
ac737d
+
ac737d
 		if (do_recovery) {
ac737d
 			memcpy(&hdr_disk1, &hdr_disk2, LUKS2_HDR_BIN_LEN);
ac737d
 			r = crypt_random_get(NULL, (char*)hdr_disk1.salt, sizeof(hdr_disk1.salt), CRYPT_RND_SALT);
ac737d
diff --git a/lib/luks2/luks2_internal.h b/lib/luks2/luks2_internal.h
ac737d
index e9beab8..dcabed7 100644
ac737d
--- a/lib/luks2/luks2_internal.h
ac737d
+++ b/lib/luks2/luks2_internal.h
ac737d
@@ -42,7 +42,7 @@
ac737d
  * On-disk access function prototypes
ac737d
  */
ac737d
 int LUKS2_disk_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr,
ac737d
-			struct device *device, int do_recovery);
ac737d
+			struct device *device, int do_recovery, int do_blkprobe);
ac737d
 int LUKS2_disk_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr,
ac737d
 			 struct device *device);
ac737d
 
ac737d
diff --git a/lib/luks2/luks2_json_metadata.c b/lib/luks2/luks2_json_metadata.c
ac737d
index 362388e..125cad9 100644
ac737d
--- a/lib/luks2/luks2_json_metadata.c
ac737d
+++ b/lib/luks2/luks2_json_metadata.c
ac737d
@@ -853,7 +853,7 @@ int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr)
ac737d
 		return r;
ac737d
 	}
ac737d
 
ac737d
-	r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1);
ac737d
+	r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1, 1);
ac737d
 	if (r == -EAGAIN) {
ac737d
 		/* unlikely: auto-recovery is required and failed due to read lock being held */
ac737d
 		device_read_unlock(crypt_metadata_device(cd));
ac737d
@@ -865,7 +865,7 @@ int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr)
ac737d
 			return r;
ac737d
 		}
ac737d
 
ac737d
-		r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1);
ac737d
+		r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1, 1);
ac737d
 
ac737d
 		device_write_unlock(crypt_metadata_device(cd));
ac737d
 	} else
ac737d
-- 
ac737d
1.8.3.1
ac737d