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

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