Blame SOURCES/cryptsetup-2.0.6-enable-all-supported-metadata-sizes-in-LUKS2-validat.patch

7cdc99
diff -rupN cryptsetup-2.0.3.old/lib/luks2/luks2_disk_metadata.c cryptsetup-2.0.3/lib/luks2/luks2_disk_metadata.c
7cdc99
--- cryptsetup-2.0.3.old/lib/luks2/luks2_disk_metadata.c	2019-03-27 15:48:28.316632526 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_disk_metadata.c	2019-03-27 15:48:48.093594565 +0100
7cdc99
@@ -387,11 +387,6 @@ int LUKS2_disk_hdr_write(struct crypt_de
7cdc99
 		return -EINVAL;
7cdc99
 	}
7cdc99
 
7cdc99
-	if (hdr->hdr_size != LUKS2_HDR_16K_LEN) {
7cdc99
-		log_dbg("Unsupported LUKS2 header size (%zu).", hdr->hdr_size);
7cdc99
-		return -EINVAL;
7cdc99
-	}
7cdc99
-
7cdc99
 	r = LUKS2_check_device_size(cd, crypt_metadata_device(cd), LUKS2_hdr_and_areas_size(hdr->jobj), 1);
7cdc99
 	if (r)
7cdc99
 		return r;
7cdc99
diff -rupN cryptsetup-2.0.3.old/lib/luks2/luks2.h cryptsetup-2.0.3/lib/luks2/luks2.h
7cdc99
--- cryptsetup-2.0.3.old/lib/luks2/luks2.h	2019-03-27 15:48:28.316632526 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2.h	2019-03-27 15:49:37.033500625 +0100
7cdc99
@@ -326,6 +326,9 @@ int LUKS2_generate_hdr(
7cdc99
 	unsigned int alignOffset,
7cdc99
 	int detached_metadata_device);
7cdc99
 
7cdc99
+int LUKS2_check_metadata_area_size(uint64_t metadata_size);
7cdc99
+int LUKS2_check_keyslots_area_size(uint64_t keyslots_size);
7cdc99
+
7cdc99
 uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr);
7cdc99
 int LUKS2_get_sector_size(struct luks2_hdr *hdr);
7cdc99
 const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment);
7cdc99
diff -rupN cryptsetup-2.0.3.old/lib/luks2/luks2_json_format.c cryptsetup-2.0.3/lib/luks2/luks2_json_format.c
7cdc99
--- cryptsetup-2.0.3.old/lib/luks2/luks2_json_format.c	2019-03-27 15:48:28.317632524 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_json_format.c	2019-03-27 15:48:48.094594563 +0100
7cdc99
@@ -114,6 +114,22 @@ int LUKS2_find_area_gap(struct crypt_dev
7cdc99
 	return 0;
7cdc99
 }
7cdc99
 
7cdc99
+int LUKS2_check_metadata_area_size(uint64_t metadata_size)
7cdc99
+{
7cdc99
+	/* see LUKS2_HDR2_OFFSETS */
7cdc99
+	return (metadata_size != 0x004000 &&
7cdc99
+		metadata_size != 0x008000 && metadata_size != 0x010000 &&
7cdc99
+		metadata_size != 0x020000 && metadata_size != 0x040000 &&
7cdc99
+		metadata_size != 0x080000 && metadata_size != 0x100000 &&
7cdc99
+		metadata_size != 0x200000 && metadata_size != 0x400000);
7cdc99
+}
7cdc99
+
7cdc99
+int LUKS2_check_keyslots_area_size(uint64_t keyslots_size)
7cdc99
+{
7cdc99
+	return (!keyslots_size || (keyslots_size % 4096) ||
7cdc99
+		keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE);
7cdc99
+}
7cdc99
+
7cdc99
 int LUKS2_generate_hdr(
7cdc99
 	struct crypt_device *cd,
7cdc99
 	struct luks2_hdr *hdr,
7cdc99
diff -rupN cryptsetup-2.0.3.old/lib/luks2/luks2_json_metadata.c cryptsetup-2.0.3/lib/luks2/luks2_json_metadata.c
7cdc99
--- cryptsetup-2.0.3.old/lib/luks2/luks2_json_metadata.c	2019-03-27 15:48:28.317632524 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_json_metadata.c	2019-03-27 15:57:44.322526763 +0100
7cdc99
@@ -701,30 +701,18 @@ static int hdr_validate_digests(json_obj
7cdc99
 }
7cdc99
 
7cdc99
 /* requires keyslots and segments sections being already validated */
7cdc99
-static int validate_keyslots_size(json_object *hdr_jobj, json_object *jobj_keyslots_size)
7cdc99
+static int validate_keyslots_size(json_object *hdr_jobj, uint64_t metadata_size, uint64_t keyslots_size)
7cdc99
 {
7cdc99
 	json_object *jobj_keyslots, *jobj, *jobj1;
7cdc99
-	uint64_t keyslots_size, segment_offset, keyslots_area_sum = 0;
7cdc99
-
7cdc99
-	if (!json_str_to_uint64(jobj_keyslots_size, &keyslots_size))
7cdc99
-		return 1;
7cdc99
-
7cdc99
-	if (keyslots_size % 4096) {
7cdc99
-		log_dbg("keyslots_size is not 4 KiB aligned");
7cdc99
-		return 1;
7cdc99
-	}
7cdc99
-
7cdc99
-	if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE) {
7cdc99
-		log_dbg("keyslots_size is too large. The cap is %" PRIu64 " bytes", (uint64_t) LUKS2_MAX_KEYSLOTS_SIZE);
7cdc99
-		return 1;
7cdc99
-	}
7cdc99
+	uint64_t segment_offset, keyslots_area_sum = 0;
7cdc99
 
7cdc99
 	json_object_object_get_ex(hdr_jobj, "segments", &jobj);
7cdc99
 	segment_offset = get_first_data_offset(jobj, "crypt");
7cdc99
 	if (segment_offset &&
7cdc99
 	    (segment_offset < keyslots_size ||
7cdc99
-	     (segment_offset - keyslots_size) < (2 * LUKS2_HDR_16K_LEN))) {
7cdc99
-		log_dbg("keyslots_size is too large %" PRIu64 " (bytes). Data offset: %" PRIu64 ", keyslots offset: %d", keyslots_size, segment_offset, 2 * LUKS2_HDR_16K_LEN);
7cdc99
+	     (segment_offset - keyslots_size) < (2 * metadata_size))) {
7cdc99
+		log_dbg("keyslots_size is too large %" PRIu64 " (bytes). Data offset: %" PRIu64
7cdc99
+			", keyslots offset: %" PRIu64, keyslots_size, segment_offset, 2 * metadata_size);
7cdc99
 		return 1;
7cdc99
 	}
7cdc99
 
7cdc99
@@ -738,7 +726,8 @@ static int validate_keyslots_size(json_o
7cdc99
 	}
7cdc99
 
7cdc99
 	if (keyslots_area_sum > keyslots_size) {
7cdc99
-		log_dbg("Sum of all keyslot area sizes (%" PRIu64 ") is greater than value in config section %" PRIu64, keyslots_area_sum, keyslots_size);
7cdc99
+		log_dbg("Sum of all keyslot area sizes (%" PRIu64 ") is greater than value in config section %"
7cdc99
+			PRIu64, keyslots_area_sum, keyslots_size);
7cdc99
 		return 1;
7cdc99
 	}
7cdc99
 
7cdc99
@@ -749,7 +738,7 @@ static int hdr_validate_config(json_obje
7cdc99
 {
7cdc99
 	json_object *jobj_config, *jobj, *jobj1;
7cdc99
 	int i;
7cdc99
-	uint64_t json_size;
7cdc99
+	uint64_t json_size, keyslots_size;
7cdc99
 
7cdc99
 	if (!json_object_object_get_ex(hdr_jobj, "config", &jobj_config)) {
7cdc99
 		log_dbg("Missing config section.");
7cdc99
@@ -760,21 +749,21 @@ static int hdr_validate_config(json_obje
7cdc99
 	    !json_str_to_uint64(jobj, &json_size))
7cdc99
 		return 1;
7cdc99
 
7cdc99
-	/* currently it's hardcoded */
7cdc99
-	if (json_size != (LUKS2_HDR_16K_LEN - LUKS2_HDR_BIN_LEN)) {
7cdc99
-		log_dbg("Invalid json_size %" PRIu64, json_size);
7cdc99
+	if (!(jobj = json_contains(jobj_config, "section", "Config", "keyslots_size", json_type_string)) ||
7cdc99
+	    !json_str_to_uint64(jobj, &keyslots_size))
7cdc99
 		return 1;
7cdc99
-	}
7cdc99
 
7cdc99
-	if (json_size % 4096) {
7cdc99
-		log_dbg("Json area is not properly aligned to 4 KiB.");
7cdc99
+	if (LUKS2_check_metadata_area_size(json_size + LUKS2_HDR_BIN_LEN)) {
7cdc99
+		log_dbg("Unsupported LUKS2 header size (%" PRIu64 ").", json_size + LUKS2_HDR_BIN_LEN);
7cdc99
 		return 1;
7cdc99
 	}
7cdc99
 
7cdc99
-	if (!(jobj = json_contains(jobj_config, "section", "Config", "keyslots_size", json_type_string)))
7cdc99
+	if (LUKS2_check_keyslots_area_size(keyslots_size)) {
7cdc99
+		log_dbg("Unsupported LUKS2 keyslots size (%" PRIu64 ").", keyslots_size);
7cdc99
 		return 1;
7cdc99
+	}
7cdc99
 
7cdc99
-	if (validate_keyslots_size(hdr_jobj, jobj))
7cdc99
+	if (validate_keyslots_size(hdr_jobj, json_size + LUKS2_HDR_BIN_LEN, keyslots_size))
7cdc99
 		return 1;
7cdc99
 
7cdc99
 	/* Flags array is optional */