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

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