Skip to contentSkip to conversation

arm64/kdump: pass dm-crypt keys to kdump kernel

PATCH In review Coiby Xu 2 participants 3 messages Jan 23 – Jan 29

Subsystems (4)
lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

PATCH v3 1/1 arm64/kdump: pass dm-crypt keys to kdump kernel

CONFIG_CRASH_DM_CRYPT has been introduced to support LUKS-encrypted device dump target by addressing two challenges [1],
- Kdump kernel may not be able to decrypt the LUKS partition. For some machines, a system administrator may not have a chance to enter the password to decrypt the device in kdump initramfs after the 1st kernel crashes

- LUKS2 by default use the memory-hard Argon2 key derivation function which is quite memory-consuming compared to the limited memory reserved for kdump.

To also enable this feature for ARM64, we only need to add device tree property dmcryptkeys [2] as similar to elfcorehdr to pass the memory address of the stored info of dm-crypt keys to the kdump kernel. Since this property is only needed by the kdump kenrel, it won't be exposed to user space.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/devicetree-org/dt-schema/pull/181

Cc Arnaud Lefebvre <[email protected]>
Cc Baoquan he <[email protected]>
Cc Dave Young <[email protected]>
Cc Kairui Song <[email protected]>
Cc Pingfan Liu <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Rob Herring <[email protected]>

---
v3
- Delete the property after reading it [Rob Herring]

v2
- Krzysztof
- Use imperative mood for commit message
- Add dt-schema ABI Documentation
https://github.com/devicetree-org/dt-schema/pull/181
- Don't print dm-crypt keys address via pr_debug

 arch/arm64/kernel/machine_kexec_file.c |  9 +++++++++
 drivers/of/fdt.c                       | 21 +++++++++++++++++++++
 drivers/of/kexec.c                     | 19 +++++++++++++++++++
 3 files changed, 49 insertions(+)
arch/arm64/kernel/machine_kexec_file.c +9 -0
index 410060ebd86d..5f3bad8ca96d 100644
@@ -134,6 +134,15 @@ int load_other_segments(struct kimage *image,
 
 		kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 			      image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
+
+		ret = crash_load_dm_crypt_keys(image);
+
+		if (ret == -ENOENT) {
+			kexec_dprintk("No dm crypt key to load\n");
+		} else if (ret) {
+			pr_err("Failed to load dm crypt keys\n");
+			goto out_err;
+		}
 	}
 #endif
 
drivers/of/fdt.c +21 -0
index 331646d667b9..2967e4aff807 100644
@@ -866,6 +866,26 @@ static void __init early_init_dt_check_for_elfcorehdr(unsigned long node)
 		 elfcorehdr_addr, elfcorehdr_size);
 }
 
+static void __init early_init_dt_check_for_dmcryptkeys(unsigned long node)
+{
+	const char *prop_name = "linux,dmcryptkeys";
+	const __be32 *prop;
+
+	if (!IS_ENABLED(CONFIG_CRASH_DM_CRYPT))
+		return;
+
+	pr_debug("Looking for dmcryptkeys property... ");
+
+	prop = of_get_flat_dt_prop(node, prop_name, NULL);
+	if (!prop)
+		return;
+
+	dm_crypt_keys_addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
+
+	/* Property only accessible to crash dump kernel */
+	fdt_delprop(initial_boot_params, node, prop_name);
+}
+
 static unsigned long chosen_node_offset = -FDT_ERR_NOTFOUND;
 
 /*
@@ -1097,6 +1117,7 @@ int __init early_init_dt_scan_chosen(char *cmdline)
 
 	early_init_dt_check_for_initrd(node);
 	early_init_dt_check_for_elfcorehdr(node);
+	early_init_dt_check_for_dmcryptkeys(node);
 
 	rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l);
 	if (rng_seed && l > 0) {
drivers/of/kexec.c +19 -0
index 1ee2d31816ae..4bfb1ea5744e 100644
@@ -432,6 +432,25 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 		if (ret)
 			goto out;
 
+		if (image->dm_crypt_keys_addr != 0) {
+			ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
+						       "linux,dmcryptkeys",
+						       image->dm_crypt_keys_addr,
+						       image->dm_crypt_keys_sz);
+
+			if (ret)
+				goto out;
+
+			/*
+			 * Avoid dmcryptkeys from being stomped on in kdump kernel by
+			 * setting up memory reserve map.
+			 */
+			ret = fdt_add_mem_rsv(fdt, image->dm_crypt_keys_addr,
+					      image->dm_crypt_keys_sz);
+			if (ret)
+				goto out;
+		}
+
 #ifdef CONFIG_CRASH_DUMP
 		/* add linux,usable-memory-range */
 		ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,

base-commit: c072629f05d7bca1148ab17690d7922a31423984

Signature · 1 line
2.52.0
maintainer
#1 lore raw
Quoted 53 lines · Coiby Xu

CONFIG_CRASH_DM_CRYPT has been introduced to support LUKS-encrypted device dump target by addressing two challenges [1],
- Kdump kernel may not be able to decrypt the LUKS partition. For some machines, a system administrator may not have a chance to enter the password to decrypt the device in kdump initramfs after the 1st kernel crashes

- LUKS2 by default use the memory-hard Argon2 key derivation function which is quite memory-consuming compared to the limited memory reserved for kdump.

To also enable this feature for ARM64, we only need to add device tree property dmcryptkeys [2] as similar to elfcorehdr to pass the memory address of the stored info of dm-crypt keys to the kdump kernel. Since this property is only needed by the kdump kenrel, it won't be exposed to user space.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/devicetree-org/dt-schema/pull/181

Cc Arnaud Lefebvre <[email protected]>
Cc Baoquan he <[email protected]>
Cc Dave Young <[email protected]>
Cc Kairui Song <[email protected]>
Cc Pingfan Liu <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Rob Herring <[email protected]>

---
v3
- Delete the property after reading it [Rob Herring]

v2
- Krzysztof
- Use imperative mood for commit message
- Add dt-schema ABI Documentation
https://github.com/devicetree-org/dt-schema/pull/181
- Don't print dm-crypt keys address via pr_debug

 arch/arm64/kernel/machine_kexec_file.c |  9 +++++++++
 drivers/of/fdt.c                       | 21 +++++++++++++++++++++
 drivers/of/kexec.c                     | 19 +++++++++++++++++++
 3 files changed, 49 insertions(+)
arch/arm64/kernel/machine_kexec_file.c +0 -0
index 410060ebd86d..5f3bad8ca96d 100644
@@ -134,6 +134,15 @@ int load_other_segments(struct kimage *image,
 
 		kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",

arch/arm64/kernel/machine_kexec_file.c int load_other_segments(struct kimage *image, · line 145

Quoted 10 lines
arch/arm64/kernel/machine_kexec_file.c +9 -0
 			      image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
+
+		ret = crash_load_dm_crypt_keys(image);
+
+		if (ret == -ENOENT) {
+			kexec_dprintk("No dm crypt key to load\n");
+		} else if (ret) {
+			pr_err("Failed to load dm crypt keys\n");
+			goto out_err;
+		}

This looks like an unusual mixture of kexec_dprintk() and pr_err().

Stepping back a second, why do we need to print anything from the arch code at all? It looks like crash_load_dm_crypt_keys() already prints for the -ENOENT case so I'd be inclined just to do:

	ret = crash_load_dm_crypt_keys(image);
	if (ret)
		goto out_err;

Will

author
#2 lore raw
Quoted 71 lines · Will Deacon
Quoted 53 lines · Coiby Xu

CONFIG_CRASH_DM_CRYPT has been introduced to support LUKS-encrypted device dump target by addressing two challenges [1],
- Kdump kernel may not be able to decrypt the LUKS partition. For some machines, a system administrator may not have a chance to enter the password to decrypt the device in kdump initramfs after the 1st kernel crashes

- LUKS2 by default use the memory-hard Argon2 key derivation function which is quite memory-consuming compared to the limited memory reserved for kdump.

To also enable this feature for ARM64, we only need to add device tree property dmcryptkeys [2] as similar to elfcorehdr to pass the memory address of the stored info of dm-crypt keys to the kdump kernel. Since this property is only needed by the kdump kenrel, it won't be exposed to user space.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/devicetree-org/dt-schema/pull/181

Cc Arnaud Lefebvre <[email protected]>
Cc Baoquan he <[email protected]>
Cc Dave Young <[email protected]>
Cc Kairui Song <[email protected]>
Cc Pingfan Liu <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Rob Herring <[email protected]>

---
v3
- Delete the property after reading it [Rob Herring]

v2
- Krzysztof
- Use imperative mood for commit message
- Add dt-schema ABI Documentation
https://github.com/devicetree-org/dt-schema/pull/181
- Don't print dm-crypt keys address via pr_debug

 arch/arm64/kernel/machine_kexec_file.c |  9 +++++++++
 drivers/of/fdt.c                       | 21 +++++++++++++++++++++
 drivers/of/kexec.c                     | 19 +++++++++++++++++++
 3 files changed, 49 insertions(+)
arch/arm64/kernel/machine_kexec_file.c +0 -0
index 410060ebd86d..5f3bad8ca96d 100644
@@ -134,6 +134,15 @@ int load_other_segments(struct kimage *image,
 
 		kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",

arch/arm64/kernel/machine_kexec_file.c int load_other_segments(struct kimage *image, · line 145

Quoted 10 lines
arch/arm64/kernel/machine_kexec_file.c +9 -0
 			      image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
+
+		ret = crash_load_dm_crypt_keys(image);
+
+		if (ret == -ENOENT) {
+			kexec_dprintk("No dm crypt key to load\n");
+		} else if (ret) {
+			pr_err("Failed to load dm crypt keys\n");
+			goto out_err;
+		}

This looks like an unusual mixture of kexec_dprintk() and pr_err().

Stepping back a second, why do we need to print anything from the arch code at all? It looks like crash_load_dm_crypt_keys() already prints for the -ENOENT case so I'd be inclined just to do:

Quoted 6 lines
	ret = crash_load_dm_crypt_keys(image);
	if (ret)
		goto out_err;

Will

Hi Will,

Thanks for carefully reviewing the patch! Yeah, crash_load_dm_crypt_keys already prints for the -ENOTENT case, good catch! And it's also a good idea to not let the arch code print anything since crash_load_dm_crypt_keys is a better place. I'll make crash_load_dm_crypt_keys print more logs and also return 0 for the case of -ENOENT. This can make the arch code more succinct.

Signature · 2 lines
Best regards,
Coiby