Blame SOURCES/cryptsetup-2.6.0-Fix-internal-crypt-segment-compare-routine.patch

37ff65
From 3e4c69a01709d35322ffa17c5360608907a207d7 Mon Sep 17 00:00:00 2001
37ff65
From: Ondrej Kozina <okozina@redhat.com>
37ff65
Date: Tue, 11 Oct 2022 11:48:13 +0200
37ff65
Subject: [PATCH 5/5] Fix internal crypt segment compare routine.
37ff65
37ff65
The function is supposed to check if manipulated
37ff65
active dm-crypt device matches the on-disk metadata.
37ff65
Unfortunately it did not take into account differences
37ff65
between normal cipher specification (aes-xts-plain64)
37ff65
and capi format specification (capi:xts(aes)-plain64).
37ff65
The internal query function always converted capi format
37ff65
in normal format and therefor failed if capi format was
37ff65
used in metadata.
37ff65
37ff65
Fixes: #759.
37ff65
---
37ff65
 lib/setup.c        | 36 ++++++++++++++++++++++++++----------
37ff65
 tests/api-test-2.c | 14 ++++++++++++--
37ff65
 2 files changed, 38 insertions(+), 12 deletions(-)
37ff65
37ff65
diff --git a/lib/setup.c b/lib/setup.c
37ff65
index 6d7411b5..809049b9 100644
37ff65
--- a/lib/setup.c
37ff65
+++ b/lib/setup.c
37ff65
@@ -2458,6 +2458,9 @@ static int _compare_crypt_devices(struct crypt_device *cd,
37ff65
 			       const struct dm_target *src,
37ff65
 			       const struct dm_target *tgt)
37ff65
 {
37ff65
+	char *src_cipher = NULL, *src_integrity = NULL;
37ff65
+	int r = -EINVAL;
37ff65
+
37ff65
 	/* for crypt devices keys are mandatory */
37ff65
 	if (!src->u.crypt.vk || !tgt->u.crypt.vk)
37ff65
 		return -EINVAL;
37ff65
@@ -2465,21 +2468,30 @@ static int _compare_crypt_devices(struct crypt_device *cd,
37ff65
 	/* CIPHER checks */
37ff65
 	if (!src->u.crypt.cipher || !tgt->u.crypt.cipher)
37ff65
 		return -EINVAL;
37ff65
-	if (strcmp(src->u.crypt.cipher, tgt->u.crypt.cipher)) {
37ff65
-		log_dbg(cd, "Cipher specs do not match.");
37ff65
+
37ff65
+	/*
37ff65
+	 * dm_query_target converts capi cipher specification to dm-crypt format.
37ff65
+	 * We need to do same for cipher specification requested in source
37ff65
+	 * device.
37ff65
+	 */
37ff65
+	if (crypt_capi_to_cipher(&src_cipher, &src_integrity, src->u.crypt.cipher, src->u.crypt.integrity))
37ff65
 		return -EINVAL;
37ff65
+
37ff65
+	if (strcmp(src_cipher, tgt->u.crypt.cipher)) {
37ff65
+		log_dbg(cd, "Cipher specs do not match.");
37ff65
+		goto out;
37ff65
 	}
37ff65
 
37ff65
 	if (tgt->u.crypt.vk->keylength == 0 && crypt_is_cipher_null(tgt->u.crypt.cipher))
37ff65
 		log_dbg(cd, "Existing device uses cipher null. Skipping key comparison.");
37ff65
 	else if (_compare_volume_keys(src->u.crypt.vk, 0, tgt->u.crypt.vk, tgt->u.crypt.vk->key_description != NULL)) {
37ff65
 		log_dbg(cd, "Keys in context and target device do not match.");
37ff65
-		return -EINVAL;
37ff65
+		goto out;
37ff65
 	}
37ff65
 
37ff65
-	if (crypt_strcmp(src->u.crypt.integrity, tgt->u.crypt.integrity)) {
37ff65
+	if (crypt_strcmp(src_integrity, tgt->u.crypt.integrity)) {
37ff65
 		log_dbg(cd, "Integrity parameters do not match.");
37ff65
-		return -EINVAL;
37ff65
+		goto out;
37ff65
 	}
37ff65
 
37ff65
 	if (src->u.crypt.offset      != tgt->u.crypt.offset ||
37ff65
@@ -2487,15 +2499,19 @@ static int _compare_crypt_devices(struct crypt_device *cd,
37ff65
 	    src->u.crypt.iv_offset   != tgt->u.crypt.iv_offset ||
37ff65
 	    src->u.crypt.tag_size    != tgt->u.crypt.tag_size) {
37ff65
 		log_dbg(cd, "Integer parameters do not match.");
37ff65
-		return -EINVAL;
37ff65
+		goto out;
37ff65
 	}
37ff65
 
37ff65
-	if (device_is_identical(src->data_device, tgt->data_device) <= 0) {
37ff65
+	if (device_is_identical(src->data_device, tgt->data_device) <= 0)
37ff65
 		log_dbg(cd, "Data devices do not match.");
37ff65
-		return -EINVAL;
37ff65
-	}
37ff65
+	else
37ff65
+		r = 0;
37ff65
 
37ff65
-	return 0;
37ff65
+out:
37ff65
+	free(src_cipher);
37ff65
+	free(src_integrity);
37ff65
+
37ff65
+	return r;
37ff65
 }
37ff65
 
37ff65
 static int _compare_integrity_devices(struct crypt_device *cd,
37ff65
diff --git a/tests/api-test-2.c b/tests/api-test-2.c
37ff65
index 0534677a..34002d1a 100644
37ff65
--- a/tests/api-test-2.c
37ff65
+++ b/tests/api-test-2.c
37ff65
@@ -1585,8 +1585,8 @@ static void ResizeDeviceLuks2(void)
37ff65
 
37ff65
 	const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
37ff65
 	size_t key_size = strlen(mk_hex) / 2;
37ff65
-	const char *cipher = "aes";
37ff65
-	const char *cipher_mode = "cbc-essiv:sha256";
37ff65
+	const char *cipher = "aes", *capi_cipher = "capi:cbc(aes)";
37ff65
+	const char *cipher_mode = "cbc-essiv:sha256", *capi_cipher_mode = "essiv:sha256";
37ff65
 	uint64_t r_payload_offset, r_header_size, r_size;
37ff65
 
37ff65
 	/* Cannot use Argon2 in FIPS */
37ff65
@@ -1728,6 +1728,16 @@ static void ResizeDeviceLuks2(void)
37ff65
 	OK_(crypt_deactivate(cd, CDEVICE_1));
37ff65
 	CRYPT_FREE(cd);
37ff65
 
37ff65
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
37ff65
+	OK_(crypt_set_pbkdf_type(cd, &pbkdf));
37ff65
+	OK_(crypt_format(cd, CRYPT_LUKS2, capi_cipher, capi_cipher_mode, NULL, key, key_size, NULL));
37ff65
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
37ff65
+	OK_(crypt_resize(cd, CDEVICE_1, 8));
37ff65
+	if (!t_device_size(DMDIR CDEVICE_1, &r_size))
37ff65
+		EQ_(8, r_size >> SECTOR_SHIFT);
37ff65
+	OK_(crypt_deactivate(cd, CDEVICE_1));
37ff65
+	CRYPT_FREE(cd);
37ff65
+
37ff65
 	_cleanup_dmdevices();
37ff65
 }
37ff65
 
37ff65
-- 
37ff65
2.38.1
37ff65