Blame SOURCES/cryptsetup-2.0.6-check-json-size-matches-value-from-binary-LUKS2-head.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 21:06:52.048172644 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_disk_metadata.c	2019-03-27 21:07:12.068978543 +0100
7cdc99
@@ -204,6 +204,12 @@ static int hdr_disk_sanity_check_pre(str
7cdc99
 		return -EINVAL;
7cdc99
 	}
7cdc99
 
7cdc99
+	if (secondary && (offset != be64_to_cpu(hdr->hdr_size))) {
7cdc99
+		log_dbg("LUKS2 offset 0x%04x in secondary header doesn't match size 0x%04x.",
7cdc99
+			(unsigned)offset, (unsigned)be64_to_cpu(hdr->hdr_size));
7cdc99
+		return -EINVAL;
7cdc99
+	}
7cdc99
+
7cdc99
 	/* FIXME: sanity check checksum alg. */
7cdc99
 
7cdc99
 	log_dbg("LUKS2 header version %u of size %u bytes, checksum %s.",
7cdc99
@@ -476,7 +482,7 @@ static int validate_json_area(const char
7cdc99
 	return 0;
7cdc99
 }
7cdc99
 
7cdc99
-static int validate_luks2_json_object(json_object *jobj_hdr)
7cdc99
+static int validate_luks2_json_object(json_object *jobj_hdr, uint64_t length)
7cdc99
 {
7cdc99
 	int r;
7cdc99
 
7cdc99
@@ -487,14 +493,14 @@ static int validate_luks2_json_object(js
7cdc99
 		return r;
7cdc99
 	}
7cdc99
 
7cdc99
-	r = LUKS2_hdr_validate(jobj_hdr);
7cdc99
+	r = LUKS2_hdr_validate(jobj_hdr, length);
7cdc99
 	if (r) {
7cdc99
 		log_dbg("Repairing JSON metadata.");
7cdc99
 		/* try to correct known glitches */
7cdc99
 		LUKS2_hdr_repair(jobj_hdr);
7cdc99
 
7cdc99
 		/* run validation again */
7cdc99
-		r = LUKS2_hdr_validate(jobj_hdr);
7cdc99
+		r = LUKS2_hdr_validate(jobj_hdr, length);
7cdc99
 	}
7cdc99
 
7cdc99
 	if (r)
7cdc99
@@ -516,7 +522,7 @@ static json_object *parse_and_validate_j
7cdc99
 
7cdc99
 	r = validate_json_area(json_area, offset, length);
7cdc99
 	if (!r)
7cdc99
-		r = validate_luks2_json_object(jobj);
7cdc99
+		r = validate_luks2_json_object(jobj, length);
7cdc99
 
7cdc99
 	if (r) {
7cdc99
 		json_object_put(jobj);
7cdc99
diff -rupN cryptsetup-2.0.3.old/lib/luks2/luks2_internal.h cryptsetup-2.0.3/lib/luks2/luks2_internal.h
7cdc99
--- cryptsetup-2.0.3.old/lib/luks2/luks2_internal.h	2019-03-27 21:06:52.048172644 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_internal.h	2019-03-27 21:07:12.070978524 +0100
7cdc99
@@ -73,7 +73,7 @@ void JSON_DBG(json_object *jobj, const c
7cdc99
 json_object *json_contains(json_object *jobj, const char *name, const char *section,
7cdc99
 		      const char *key, json_type type);
7cdc99
 
7cdc99
-int LUKS2_hdr_validate(json_object *hdr_jobj);
7cdc99
+int LUKS2_hdr_validate(json_object *hdr_jobj, uint64_t length);
7cdc99
 int LUKS2_keyslot_validate(json_object *hdr_jobj, json_object *hdr_keyslot, const char *key);
7cdc99
 int LUKS2_check_json_size(const struct luks2_hdr *hdr);
7cdc99
 int LUKS2_token_validate(json_object *hdr_jobj, json_object *jobj_token, const char *key);
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 21:06:52.049172634 +0100
7cdc99
+++ cryptsetup-2.0.3/lib/luks2/luks2_json_metadata.c	2019-03-27 21:07:44.937659885 +0100
7cdc99
@@ -446,7 +446,7 @@ int LUKS2_token_validate(json_object *hd
7cdc99
 	return 0;
7cdc99
 }
7cdc99
 
7cdc99
-static int hdr_validate_json_size(json_object *hdr_jobj)
7cdc99
+static int hdr_validate_json_size(json_object *hdr_jobj, uint64_t hdr_json_size)
7cdc99
 {
7cdc99
 	json_object *jobj, *jobj1;
7cdc99
 	const char *json;
7cdc99
@@ -460,12 +460,22 @@ static int hdr_validate_json_size(json_o
7cdc99
 	json_area_size = json_object_get_uint64(jobj1);
7cdc99
 	json_size = (uint64_t)strlen(json);
7cdc99
 
7cdc99
-	return json_size > json_area_size ? 1 : 0;
7cdc99
+	if (hdr_json_size != json_area_size) {
7cdc99
+		log_dbg("JSON area size doesn't match value in binary header.");
7cdc99
+		return 1;
7cdc99
+	}
7cdc99
+
7cdc99
+	if (json_size > json_area_size) {
7cdc99
+		log_dbg("JSON doesn't fit in the designated area.");
7cdc99
+		return 1;
7cdc99
+	}
7cdc99
+
7cdc99
+	return 0;
7cdc99
 }
7cdc99
 
7cdc99
 int LUKS2_check_json_size(const struct luks2_hdr *hdr)
7cdc99
 {
7cdc99
-	return hdr_validate_json_size(hdr->jobj);
7cdc99
+	return hdr_validate_json_size(hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN);
7cdc99
 }
7cdc99
 
7cdc99
 static int hdr_validate_keyslots(json_object *hdr_jobj)
7cdc99
@@ -830,7 +840,7 @@ static int hdr_validate_config(json_obje
7cdc99
 	return 0;
7cdc99
 }
7cdc99
 
7cdc99
-int LUKS2_hdr_validate(json_object *hdr_jobj)
7cdc99
+int LUKS2_hdr_validate(json_object *hdr_jobj, uint64_t json_size)
7cdc99
 {
7cdc99
 	struct {
7cdc99
 		int (*validate)(json_object *);
7cdc99
@@ -852,10 +862,8 @@ int LUKS2_hdr_validate(json_object *hdr_
7cdc99
 		if (checks[i].validate && checks[i].validate(hdr_jobj))
7cdc99
 			return 1;
7cdc99
 
7cdc99
-	if (hdr_validate_json_size(hdr_jobj)) {
7cdc99
-		log_dbg("Json header is too large.");
7cdc99
+	if (hdr_validate_json_size(hdr_jobj, json_size))
7cdc99
 		return 1;
7cdc99
-	}
7cdc99
 
7cdc99
 	/* validate keyslot implementations */
7cdc99
 	if (LUKS2_keyslots_validate(hdr_jobj))
7cdc99
@@ -903,7 +911,7 @@ int LUKS2_hdr_write(struct crypt_device
7cdc99
 	/* erase unused digests (no assigned keyslot or segment) */
7cdc99
 	LUKS2_digests_erase_unused(cd, hdr);
7cdc99
 
7cdc99
-	if (LUKS2_hdr_validate(hdr->jobj))
7cdc99
+	if (LUKS2_hdr_validate(hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN))
7cdc99
 		return -EINVAL;
7cdc99
 
7cdc99
 	return LUKS2_disk_hdr_write(cd, hdr, crypt_metadata_device(cd));
7cdc99
@@ -1650,7 +1658,7 @@ const char *LUKS2_get_cipher(struct luks
7cdc99
 		return NULL;
7cdc99
 
7cdc99
 	if (!json_object_object_get_ex(jobj2, "encryption", &jobj3))
7cdc99
-		return NULL;
7cdc99
+		return "null";
7cdc99
 
7cdc99
 	return json_object_get_string(jobj3);
7cdc99
 }