diff --git a/lib/luks2/luks2_json_metadata.c b/lib/luks2/luks2_json_metadata.c index 6ec0c1f1..29c03615 100644 --- a/lib/luks2/luks2_json_metadata.c +++ b/lib/luks2/luks2_json_metadata.c @@ -970,13 +970,16 @@ int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair) return r; } -int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr) +static int hdr_cleanup_and_validate(struct crypt_device *cd, struct luks2_hdr *hdr) { - /* NOTE: is called before LUKS2 validation routines */ - /* erase unused digests (no assigned keyslot or segment) */ LUKS2_digests_erase_unused(cd, hdr); - if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN)) + return LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN); +} + +int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr) +{ + if (hdr_cleanup_and_validate(cd, hdr)) return -EINVAL; return LUKS2_disk_hdr_write(cd, hdr, crypt_metadata_device(cd), false); @@ -984,11 +987,7 @@ int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr) int LUKS2_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr) { - /* NOTE: is called before LUKS2 validation routines */ - /* erase unused digests (no assigned keyslot or segment) */ - LUKS2_digests_erase_unused(cd, hdr); - - if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN)) + if (hdr_cleanup_and_validate(cd, hdr)) return -EINVAL; return LUKS2_disk_hdr_write(cd, hdr, crypt_metadata_device(cd), true);