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

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