Skip to contentSkip to conversation

Support kdump with LUKS encryption by reusing LUKS volume keys

PATCH Superseded Coiby Xu 4 participants 34 messages May 23, 2024 – Oct 18, 2024

Subsystems (6)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

v4 cover Support kdump with LUKS encryption by reusing LUKS volume keys

LUKS is the standard for Linux disk encryption. Many users choose LUKS and in some use cases like Confidential VM it's mandated. With kdump enabled, when the 1st kernel crashes, the system could boot into the kdump/crash kernel and dump the memory image i.e. /proc/vmcore to a specified target. Currently, when dumping vmcore to a LUKS encrypted device, there are two problems,

- 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; For cloud confidential VMs, depending on the policy the kdump kernel may not be able to unseal the keys with TPM and the console virtual keyboard is untrusted.

- LUKS2 by default use the memory-hard Argon2 key derivation function which is quite memory-consuming compared to the limited memory reserved for kdump. Take Fedora example, by default, only 256M is reserved for systems having memory between 4G-64G. With LUKS enabled, ~1300M needs to be reserved for kdump. Note if the memory reserved for kdump can't be used by 1st kernel i.e. an user sees ~1300M memory missing in the 1st kernel.

Besides users (at least for Fedora) usually expect kdump to work out of the box i.e. no manual password input is needed. And it doesn't make sense to derivate the keys again in kdump kernel which seems to be redundant work.

This patch set addresses the above issues by make the LUKS volume keys persistent for kdump kernel with the help of cryptsetup's new APIs (--link-vk-to-keyring/--volume-key-keyring). Here is the life cycle of this kdump copy of LUKS volume keys,

1. After the 1st kernel loads the initramfs during boot, systemd use an user-input passphrase or TPM-sealed key to de-crypt the LUKS volume keys and then save the volume keys to specified keyring (using the --link-vk-to-keyring API) and the key will expire within specified time.

2. A user space tool (kdump initramfs builder) writes a key description to /sys/kernel/crash_dm_crypt_keys to inform the 1st kernel to record the key while building the kdump initramfs

3. The kexec_file_load syscall read the volume keys by recored key descriptions and then save them key to kdump reserved memory and wipe the copy.

4. When the 1st kernel crashes and the kdump initramfs is booted, the kdump initramfs asks the kdump kernel to create a user key using the key stored in kdump reserved memory by writing to to /sys/kernel/crash_dm_crypt_keys. Then the LUKS encrypted devide is unlocked with libcryptsetup's --volume-key-keyring API.

5. The system gets rebooted to the 1st kernel after dumping vmcore to the LUKS encrypted device is finished

After libcryptsetup saving the LUKS volume keys to specified keyring, whoever takes this should be responsible for the safety of these copies of keys. The keys will be saved in the memory area exclusively reserved for kdump where even the 1st kernel has no direct access. And further more, two additional protections are added,
- save the copy randomly in kdump reserved memory as suggested by Jan
- clear the _PAGE_PRESENT flag of the page that stores the copy as suggested by Pingfan

This patch set only supports x86. There will be patches to support other architectures once this patch set gets merged.

v4
- rebase onto latest Linus tree so Baoquan can apply the patches for code review
- fix kernel test robot warnings

v3
- Support CPU/memory hot-plugging [Baoquan]
- Don't save the keys temporarily to simplify the implementation [Baoquan]
- Support multiple LUKS encrypted volumes
- Read logon key instead of user key to improve security [Ondrej]
- A kernel config option CRASH_DM_CRYPT for this feature (disabled by default)
- Fix warnings found by kernel test robot
- Rebase the code onto 6.9.0-rc5+

v2
- work together with libscryptsetup's --link-vk-to-keyring/--volume-key-keyring APIs [Milan and Ondrej]
- add the case where console virtual keyboard is untrusted for confidential VM
- use dm_crypt_key instead of LUKS volume key [Milan and Eric]
- fix some code format issues
- don't move "struct kexec_segment" declaration
- Rebase the code onto latest Linus tree (6.7.0)

v1
- "Put the luks key handling related to crash_dump out into a separate file kernel/crash_dump_luks.c" [Baoquan]
- Put the generic luks handling code before the x86 specific code to make it easier for other arches to follow suit [Baoquan]
- Use phys_to_virt instead of "pfn -> page -> vaddr" [Dave Hansen]
- Drop the RFC prefix [Dave Young]
- Rebase the code onto latest Linus tree (6.4.0-rc4)

RFC v2
- libcryptsetup interacts with the kernel via sysfs instead of "hacking" dm-crypt
- to save a kdump copy of the LUKS volume key in 1st kernel
- to add a logon key using the copy for libcryptsetup in kdump kernel [Milan]
- to avoid the incorrect usage of LUKS master key in dm-crypt [Milan]
- save the kdump copy of LUKS volume key randomly [Jan]
- mark the kdump copy inaccessible [Pingfan]
- Miscellaneous
- explain when operations related to the LUKS volume key happen [Jan]
- s/master key/volume key/g
- use crash_ instead of kexec_ as function prefix
- fix commit subject prefixes e.g. "x86, kdump" to x86/crash

Coiby Xu (7):
kexec_file: allow to place kexec_buf randomly
crash_dump: make dm crypt keys persist for the kdump kernel
crash_dump: store dm keys in kdump reserved memory
crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging
crash_dump: retrieve dm crypt keys in kdump kernel x86/crash: pass dm crypt keys to kdump kernel
x86/crash: make the page that stores the dm crypt keys inaccessible

 arch/x86/kernel/crash.c            |  15 +-
 arch/x86/kernel/kexec-bzimage64.c  |   7 +
 arch/x86/kernel/machine_kexec_64.c |  21 ++
 include/linux/crash_core.h         |   9 +-
 include/linux/crash_dump.h         |   2 +
 include/linux/kexec.h              |   6 +
 kernel/Kconfig.kexec               |   8 +
 kernel/Makefile                    |   1 +
 kernel/crash_dump_dm_crypt.c       | 319 +++++++++++++++++++++++++++++
 kernel/kexec_file.c                |  15 ++
 kernel/ksysfs.c                    |  22 ++
 11 files changed, 423 insertions(+), 2 deletions(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c

base-commit: de7e71ef8bed222dd144d8878091ecb6d5dfd208

Signature · 1 line
2.45.0
author
#1 lore raw

PATCH v4 1/7 kexec_file: allow to place kexec_buf randomly

Currently, kexec_buf is placed in order which means for the same machine, the info in the kexec_buf is always located at the same position each time the machine is booted. This may cause a risk for sensitive information like LUKS volume key. Now struct kexec_buf has a new field random which indicates it's supposed to be placed in a random position.

---
 include/linux/kexec.h |  2 ++
 kernel/kexec_file.c   | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/kexec.h +2 -0
index f0e9f8eda7a3..cc81b8a903ab 100644
@@ -171,6 +171,7 @@ int kexec_image_post_load_cleanup_default(struct kimage *image);
  * @buf_min:	The buffer can't be placed below this address.
  * @buf_max:	The buffer can't be placed above this address.
  * @top_down:	Allocate from top of memory.
+ * @random:	Place the buffer at a random position.
  */
 struct kexec_buf {
 	struct kimage *image;
@@ -182,6 +183,7 @@ struct kexec_buf {
 	unsigned long buf_min;
 	unsigned long buf_max;
 	bool top_down;
+	bool random;
 };
 
 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
kernel/kexec_file.c +15 -0
index 3d64290d24c9..06b77f9ac4cc 100644
@@ -25,6 +25,7 @@
 #include <linux/elfcore.h>
 #include <linux/kernel.h>
 #include <linux/kernel_read_file.h>
+#include <linux/prandom.h>
 #include <linux/syscalls.h>
 #include <linux/vmalloc.h>
 #include "kexec_internal.h"
@@ -437,6 +438,16 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
 	return ret;
 }
 
+static unsigned long kexec_random_start(unsigned long start, unsigned long end)
+{
+	unsigned long temp_start;
+	unsigned short i;
+
+	get_random_bytes(&i, sizeof(unsigned short));
+	temp_start = start + (end - start) / USHRT_MAX * i;
+	return temp_start;
+}
+
 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
 				    struct kexec_buf *kbuf)
 {
@@ -445,6 +456,8 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
 
 	temp_end = min(end, kbuf->buf_max);
 	temp_start = temp_end - kbuf->memsz + 1;
+	if (kbuf->random)
+		temp_start = kexec_random_start(temp_start, temp_end);
 
 	do {
 		/* align down start */
@@ -482,6 +495,8 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
 	unsigned long temp_start, temp_end;
 
 	temp_start = max(start, kbuf->buf_min);
+	if (kbuf->random)
+		temp_start = kexec_random_start(temp_start, end);
 
 	do {
 		temp_start = ALIGN(temp_start, kbuf->buf_align);
Signature · 1 line
2.45.0
#2 lore raw

Re: [PATCH v4 1/7] kexec_file: allow to place kexec_buf randomly

Quoted 64 lines · Coiby Xu

Currently, kexec_buf is placed in order which means for the same machine, the info in the kexec_buf is always located at the same position each time the machine is booted. This may cause a risk for sensitive information like LUKS volume key. Now struct kexec_buf has a new field random which indicates it's supposed to be placed in a random position.

---
 include/linux/kexec.h |  2 ++
 kernel/kexec_file.c   | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/kexec.h +2 -0
index f0e9f8eda7a3..cc81b8a903ab 100644
@@ -171,6 +171,7 @@ int kexec_image_post_load_cleanup_default(struct kimage *image);
  * @buf_min:	The buffer can't be placed below this address.
  * @buf_max:	The buffer can't be placed above this address.
  * @top_down:	Allocate from top of memory.
+ * @random:	Place the buffer at a random position.
  */
 struct kexec_buf {
 	struct kimage *image;
@@ -182,6 +183,7 @@ struct kexec_buf {
 	unsigned long buf_min;
 	unsigned long buf_max;
 	bool top_down;
+	bool random;
 };
 
 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
kernel/kexec_file.c +11 -0
index 3d64290d24c9..06b77f9ac4cc 100644
@@ -25,6 +25,7 @@
 #include <linux/elfcore.h>
 #include <linux/kernel.h>
 #include <linux/kernel_read_file.h>
+#include <linux/prandom.h>
 #include <linux/syscalls.h>
 #include <linux/vmalloc.h>
 #include "kexec_internal.h"
@@ -437,6 +438,16 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
 	return ret;
 }
 
+static unsigned long kexec_random_start(unsigned long start, unsigned long end)
+{
+	unsigned long temp_start;
+	unsigned short i;
+
+	get_random_bytes(&i, sizeof(unsigned short));
+	temp_start = start + (end - start) / USHRT_MAX * i;
+	return temp_start;
+}
+
 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
 				    struct kexec_buf *kbuf)
 {

kernel/kexec_file.c static int locate_mem_hole_top_down(unsigned long start, unsigned long end, · line 460

Quoted 5 lines
kernel/kexec_file.c +2 -0
@@ -445,6 +456,8 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
 
 	temp_end = min(end, kbuf->buf_max);
 	temp_start = temp_end - kbuf->memsz + 1;
+	if (kbuf->random)
+		temp_start = kexec_random_start(temp_start, temp_end);

As we discussed before, this need be limited in kdump scope, seems v4 doesn't include the change.

Quoted 15 lines
kernel/kexec_file.c +2 -0
 	do {
 		/* align down start */
@@ -482,6 +495,8 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
 	unsigned long temp_start, temp_end;
 
 	temp_start = max(start, kbuf->buf_min);
+	if (kbuf->random)
+		temp_start = kexec_random_start(temp_start, end);
 
 	do {
 		temp_start = ALIGN(temp_start, kbuf->buf_align);
Signature · 1 line
2.45.0
author
#3 lore raw

Re: [PATCH v4 1/7] kexec_file: allow to place kexec_buf randomly

Quoted 19 lines · Baoquan He
Quoted 18 lines · Coiby Xu

Currently, kexec_buf is placed in order which means for the same machine, the info in the kexec_buf is always located at the same position each time the machine is booted. This may cause a risk for sensitive information like LUKS volume key. Now struct kexec_buf has a new field random which indicates it's supposed to be placed in a random position.

---
 include/linux/kexec.h |  2 ++
 kernel/kexec_file.c   | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/kexec.h +0 -0
index f0e9f8eda7a3..cc81b8a903ab 100644

[...]

Quoted 9 lines

include/linux/kexec.h static int locate_mem_hole_top_down(unsigned long start, unsigned long end, · line 460

Quoted 6 lines
include/linux/kexec.h +2 -0
@@ -445,6 +456,8 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
 
 	temp_end = min(end, kbuf->buf_max);
 	temp_start = temp_end - kbuf->memsz + 1;
+	if (kbuf->random)
+		temp_start = kexec_random_start(temp_start, temp_end);

As we discussed before, this need be limited in kdump scope, seems v4 doesn't include the change.

Yes, v4 was sent mainly for you to review the patches by applying to latest Linus tree. v5 now include the change.

Signature · 2 lines
Best regards,
Coiby
author
#4 lore raw

PATCH v4 2/7 crash_dump: make dm crypt keys persist for the kdump kernel

In reply to Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

User space can also read this API to learn about current state.

---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +4 -1
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif
 
-
+#ifdef CONFIG_CRASH_DM_CRYPT
+int crash_sysfs_dm_crypt_keys_read(char *buf);
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+#endif
 
 #ifndef arch_crash_handle_hotplug_event
 static inline void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { }
kernel/Kconfig.kexec +8 -0
index 6c34e63c88ff..88525ad1c80a 100644
@@ -116,6 +116,14 @@ config CRASH_DUMP
 	  For s390, this option also enables zfcpdump.
 	  See also <file:Documentation/arch/s390/zfcpdump.rst>
 
+config CRASH_DM_CRYPT
+	bool "Support saving crash dump to dm-crypt encrypted volume"
+	depends on CRASH_DUMP
+	help
+	  With this option enabled, user space can intereact with
+	  /sys/kernel/crash_dm_crypt_keys to make the dm crypt keys
+	  persistent for the crash dump kernel.
+
 config CRASH_HOTPLUG
 	bool "Update the crash elfcorehdr on system configuration changes"
 	default y
kernel/Makefile +1 -0
index 3c13240dfc9f..f2e5b3e86d12 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o elfcorehdr.o
 obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_CRASH_DUMP) += crash_core.o
+obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
 obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/crash_dump_dm_crypt.c new file +113 -0

113 lines not on this page · raw message

kernel/ksysfs.c +22 -0
index 07fb5987b42b..2ba4dcbf5816 100644
@@ -167,6 +167,25 @@ static ssize_t vmcoreinfo_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(vmcoreinfo);
 
+#ifdef CONFIG_CRASH_DM_CRYPT
+static ssize_t crash_dm_crypt_keys_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)
+{
+	return crash_sysfs_dm_crypt_keys_read(buf);
+}
+
+static ssize_t crash_dm_crypt_keys_store(struct kobject *kobj,
+					 struct kobj_attribute *attr,
+					 const char *buf, size_t count)
+{
+	int ret;
+
+	ret = crash_sysfs_dm_crypt_keys_write(buf, count);
+	return ret < 0 ? ret : count;
+}
+KERNEL_ATTR_RW(crash_dm_crypt_keys);
+#endif /* CONFIG_CRASH_DM_CRYPT */
+
 #ifdef CONFIG_CRASH_HOTPLUG
 static ssize_t crash_elfcorehdr_size_show(struct kobject *kobj,
 			       struct kobj_attribute *attr, char *buf)
@@ -271,6 +290,9 @@ static struct attribute * kernel_attrs[] = {
 #endif
 #ifdef CONFIG_VMCORE_INFO
 	&vmcoreinfo_attr.attr,
+#ifdef CONFIG_CRASH_DM_CRYPT
+	&crash_dm_crypt_keys_attr.attr,
+#endif
 #ifdef CONFIG_CRASH_HOTPLUG
 	&crash_elfcorehdr_size_attr.attr,
 #endif
Signature · 1 line
2.45.0
maintainer
#5 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 7 lines · Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

"logon"? What is that?

Quoted 2 lines

User space can also read this API to learn about current state.

But you don't document it in Documentation/ABI/ so we don't know if this really is the case, and no one will know how to use it :(

Quoted 63 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +4 -1
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif
 
-
+#ifdef CONFIG_CRASH_DM_CRYPT
+int crash_sysfs_dm_crypt_keys_read(char *buf);
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+#endif
 
 #ifndef arch_crash_handle_hotplug_event
 static inline void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { }
kernel/Kconfig.kexec +8 -0
index 6c34e63c88ff..88525ad1c80a 100644
@@ -116,6 +116,14 @@ config CRASH_DUMP
 	  For s390, this option also enables zfcpdump.
 	  See also <file:Documentation/arch/s390/zfcpdump.rst>
 
+config CRASH_DM_CRYPT
+	bool "Support saving crash dump to dm-crypt encrypted volume"
+	depends on CRASH_DUMP
+	help
+	  With this option enabled, user space can intereact with
+	  /sys/kernel/crash_dm_crypt_keys to make the dm crypt keys
+	  persistent for the crash dump kernel.
+
 config CRASH_HOTPLUG
 	bool "Update the crash elfcorehdr on system configuration changes"
 	default y
kernel/Makefile +1 -0
index 3c13240dfc9f..f2e5b3e86d12 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o elfcorehdr.o
 obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_CRASH_DUMP) += crash_core.o
+obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
 obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/crash_dump_dm_crypt.c new file +0 -0
new file mode 100644
index 000000000000..78809189084a

kernel/crash_dump_dm_crypt.c line 6

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <keys/user-type.h>
+#include <linux/crash_dump.h>
+
+#define KEY_NUM_MAX 128
+#define KEY_SIZE_MAX 256

Why these values?

kernel/crash_dump_dm_crypt.c line 17

Quoted 11 lines
kernel/crash_dump_dm_crypt.c new file +11 -0
+
+// The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
+#define KEY_DESC_LEN 48
+
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static enum STATE_ENUM {
+	FRESH = 0,
+	INITIALIZED,
+	RECORDED,
+	LOADED,
+} state;

How are you going to keep these enums synced up with the string values?

Quoted 15 lines
kernel/crash_dump_dm_crypt.c new file +15 -0
+
+static unsigned int key_count;
+static size_t keys_header_size;
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;
+

kernel/crash_dump_dm_crypt.c line 42

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}
+
+static int init(const char *buf)
+{
+	unsigned int total_keys;
+	char dummy[5];

Why 5?

kernel/crash_dump_dm_crypt.c line 44

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+
+	if (sscanf(buf, "%4s %u", dummy, &total_keys) != 2)

Didn't you just overflow dummy now?

kernel/crash_dump_dm_crypt.c line 49

Quoted 5 lines
kernel/crash_dump_dm_crypt.c new file +5 -0
+		return -EINVAL;
+
+	if (key_count > KEY_NUM_MAX) {
+		pr_err("Exceed the maximum number of keys (KEY_NUM_MAX=%u)\n",
+		       KEY_NUM_MAX);

Do not let userspace spam the kernel log directly if it sends it invalid data.

Quoted 12 lines
kernel/crash_dump_dm_crypt.c new file +12 -0
+		return -EINVAL;
+	}
+
+	keys_header_size = get_keys_header_size(keys_header, total_keys);
+	key_count = 0;
+
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header)
+		return -ENOMEM;
+
+	keys_header->key_count = total_keys;
+	state = INITIALIZED;

kernel/crash_dump_dm_crypt.c line 71

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+	return 0;
+}
+
+static int record_key_desc(const char *buf, struct dm_crypt_key *dm_key)
+{
+	char key_desc[KEY_DESC_LEN];
+	char dummy[7];
+
+	if (state != INITIALIZED)
+		pr_err("Please send the cmd 'init <KEY_NUM>' first\n");

Again, don't let userspace spam the log.

Quoted 26 lines
kernel/crash_dump_dm_crypt.c new file +26 -0
+
+	if (sscanf(buf, "%6s %s", dummy, key_desc) != 2)
+		return -EINVAL;
+
+	if (key_count >= keys_header->key_count) {
+		pr_warn("Already have %u keys", key_count);
+		return -EINVAL;
+	}
+
+	strscpy(dm_key->key_desc, key_desc, KEY_DESC_LEN);
+	pr_debug("Key%d (%s) recorded\n", key_count, dm_key->key_desc);
+	key_count++;
+
+	if (key_count == keys_header->key_count)
+		state = RECORDED;
+
+	return 0;
+}
+
+static int process_cmd(const char *buf, size_t count)
+{
+	if (strncmp(buf, "init ", 5) == 0)
+		return init(buf);
+	else if (strncmp(buf, "record ", 7) == 0)
+		return record_key_desc(buf, &keys_header->keys[key_count]);
+

kernel/crash_dump_dm_crypt.c line 107

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+	return -EINVAL;
+}
+
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count)
+{
+	if (!is_kdump_kernel())
+		return process_cmd(buf, count);
+	return -EINVAL;
+}
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_write);

EXPORT_SYMBOL_GPL() as you are dealing with a sysfs api?

kernel/crash_dump_dm_crypt.c line 111

Quoted 4 lines
kernel/crash_dump_dm_crypt.c new file +4 -0
+
+int crash_sysfs_dm_crypt_keys_read(char *buf)
+{
+	return sprintf(buf, "%s\n", STATE_STR[state]);

sysfs_emit() please.

kernel/crash_dump_dm_crypt.c line 113

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+}
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_read);

Again, EXPORT_SYMBOL_GPL()?

Quoted 13 lines
kernel/ksysfs.c +5 -0
index 07fb5987b42b..2ba4dcbf5816 100644
@@ -167,6 +167,25 @@ static ssize_t vmcoreinfo_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(vmcoreinfo);
 
+#ifdef CONFIG_CRASH_DM_CRYPT
+static ssize_t crash_dm_crypt_keys_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)
+{
+	return crash_sysfs_dm_crypt_keys_read(buf);

kernel/ksysfs.c static ssize_t vmcoreinfo_show(struct kobject *kobj, · line 184

Quoted 10 lines
kernel/ksysfs.c +10 -0
+}
+
+static ssize_t crash_dm_crypt_keys_store(struct kobject *kobj,
+					 struct kobj_attribute *attr,
+					 const char *buf, size_t count)
+{
+	int ret;
+
+	ret = crash_sysfs_dm_crypt_keys_write(buf, count);
+	return ret < 0 ? ret : count;

Personally, I hate ? : lines, just write it out, the compiler is the same and this way it is much more readable:

	if (ret < 0)
		return ret;
	return count;

thanks,

greg k-h

author
#6 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Hi Greg,

Thanks for taking time to carefully review this patch!

Quoted 10 lines · Greg KH
Quoted 7 lines · Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

"logon"? What is that?

Thanks for raising this question. A logon key is similar to a user key but the payload can't be read by user space. I'll document this info and also refer users to security/keys/core.rst for details.

Quoted 6 lines
Quoted 3 lines

User space can also read this API to learn about current state.

But you don't document it in Documentation/ABI/ so we don't know if this really is the case, and no one will know how to use it :(

Thanks for bringing this to my attention! A new version will include Documentation/ABI/testing/crash_dm_crypt_keys to have all the details.

Quoted 11 lines
Quoted 11 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c

[...]

Quoted 6 lines
Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +0 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
Quoted 8 lines

kernel/crash_dump_dm_crypt.c line 6

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+// SPDX-License-Identifier: GPL-2.0-only
+#include <keys/user-type.h>
+#include <linux/crash_dump.h>
+
+#define KEY_NUM_MAX 128
+#define KEY_SIZE_MAX 256

Why these values?

For KEY_NUM_MAX, I assume the maximum number of LUKS encrypted volumes to unlock is 128.

For KEY_SIZE_MAX, according to /proc/crypto and "cryptsetup benchmark", the maximum key size is 64 bytes. So I assume 256 should be enough in near future.

Thanks for raising the question which makes realize it's better to include the assumptions in the commit message for domain experts to examine!

Quoted 14 lines

kernel/crash_dump_dm_crypt.c line 17

Quoted 12 lines
kernel/crash_dump_dm_crypt.c new file +11 -0
+
+// The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
+#define KEY_DESC_LEN 48
+
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static enum STATE_ENUM {
+	FRESH = 0,
+	INITIALIZED,
+	RECORDED,
+	LOADED,
+} state;

How are you going to keep these enums synced up with the string values?

Thanks to prompt me to come up with a more reliable way! I should have used the emums to index the array,

static const char *STATE_STR[] = {
[FRESH] = "fresh",
[INITIALIZED] = "initialized",
[RECORDED] = "recorded",
[LOADED] = "loaded"
};

Quoted 20 lines
Quoted 20 lines
kernel/crash_dump_dm_crypt.c new file +19 -0
+
+static unsigned int key_count;
+static size_t keys_header_size;
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
Quoted 8 lines

kernel/crash_dump_dm_crypt.c line 42

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+}
+
+static int init(const char *buf)
+{
+	unsigned int total_keys;
+	char dummy[5];

Why 5?

Oh, I had len("init")+1(NULL) in mind when wrote 5. In retrospect, 5 is not necessary. And in fact there is no need to define this dummy variable in the first place as explained in the next comment.

Quoted 5 lines

kernel/crash_dump_dm_crypt.c line 44

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+
+	if (sscanf(buf, "%4s %u", dummy, &total_keys) != 2)

Didn't you just overflow dummy now?

Correct me if I'm wrong, but using the width specifier i.e. "%4s" won't overflow dummy, right? Anyways, I think a better way is to simply use sscanf(buf, "init %u",..)
instead thus no need for this dummy variable.

Quoted 9 lines

kernel/crash_dump_dm_crypt.c line 49

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +5 -0
+		return -EINVAL;
+
+	if (key_count > KEY_NUM_MAX) {
+		pr_err("Exceed the maximum number of keys (KEY_NUM_MAX=%u)\n",
+		       KEY_NUM_MAX);

Do not let userspace spam the kernel log directly if it sends it invalid data.

Thanks for pointing out my oversight. I'll simply return -EINVAL in next version.

Quoted 17 lines
Quoted 17 lines
kernel/crash_dump_dm_crypt.c new file +16 -0
+		return -EINVAL;
+	}
+
+	keys_header_size = get_keys_header_size(keys_header, total_keys);
+	key_count = 0;
+
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header)
+		return -ENOMEM;
+
+	keys_header->key_count = total_keys;
+	state = INITIALIZED;
+	return 0;
+}
+
+static int record_key_desc(const char *buf, struct dm_crypt_key *dm_key)
Quoted 8 lines

kernel/crash_dump_dm_crypt.c line 71

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+{
+	char key_desc[KEY_DESC_LEN];
+	char dummy[7];
+
+	if (state != INITIALIZED)
+		pr_err("Please send the cmd 'init <KEY_NUM>' first\n");

Again, don't let userspace spam the log.

I'll fix this issue in next version. Thank you again!

Quoted 3 lines

kernel/crash_dump_dm_crypt.c line 73

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+
+	if (sscanf(buf, "%6s %s", dummy, key_desc) != 2)

Oh, key_desc could be overflowed here if there is a malicious user. I'll have a check on the length of buf in next version.

[...]

Quoted 10 lines

kernel/crash_dump_dm_crypt.c line 107

Quoted 1 line
kernel/crash_dump_dm_crypt.c new file +1 -0
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_write);

EXPORT_SYMBOL_GPL() as you are dealing with a sysfs api?

kernel/crash_dump_dm_crypt.c line 111

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +3 -0
+int crash_sysfs_dm_crypt_keys_read(char *buf)
+{
+	return sprintf(buf, "%s\n", STATE_STR[state]);

sysfs_emit() please.

This will be fixed, thanks!

Quoted 5 lines

kernel/crash_dump_dm_crypt.c line 79

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+}
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_read);

Again, EXPORT_SYMBOL_GPL()?

I'll replace two occurrences of EXPORT_SYMBOL with EXPORT_SYMBOL_GPL, thanks!

Quoted 27 lines
Quoted 14 lines
kernel/ksysfs.c +5 -0
index 07fb5987b42b..2ba4dcbf5816 100644
@@ -167,6 +167,25 @@ static ssize_t vmcoreinfo_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(vmcoreinfo);
 
+#ifdef CONFIG_CRASH_DM_CRYPT
+static ssize_t crash_dm_crypt_keys_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)
+{
+	return crash_sysfs_dm_crypt_keys_read(buf);

kernel/ksysfs.c static ssize_t vmcoreinfo_show(struct kobject *kobj, · line 184

Quoted 10 lines
kernel/ksysfs.c +10 -0
+}
+
+static ssize_t crash_dm_crypt_keys_store(struct kobject *kobj,
+					 struct kobj_attribute *attr,
+					 const char *buf, size_t count)
+{
+	int ret;
+
+	ret = crash_sysfs_dm_crypt_keys_write(buf, count);
+	return ret < 0 ? ret : count;

Personally, I hate ? : lines, just write it out, the compiler is the same and this way it is much more readable:

Quoted 3 lines
	if (ret < 0)
		return ret;
	return count;

Thanks for suggesting more readable code! I'll apply it to next version!

Quoted 10 lines

thanks,

greg k-h

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Signature · 2 lines
Best regards,
Coiby
#7 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

In reply to Coiby Xu

Hi Coiby,

Quoted 9 lines · Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

User space can also read this API to learn about current state.

From the subject, can I think the luks keys will persist forever? or only for a while? If need and can only keep it for a while, can you mention it and tell why and how it will be used. Because you add a lot of codes, but only simply mention the sysfs, that doesn't make sense.

Quoted 93 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +4 -1
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif
 
-
+#ifdef CONFIG_CRASH_DM_CRYPT
+int crash_sysfs_dm_crypt_keys_read(char *buf);
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+#endif
 
 #ifndef arch_crash_handle_hotplug_event
 static inline void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { }
kernel/Kconfig.kexec +8 -0
index 6c34e63c88ff..88525ad1c80a 100644
@@ -116,6 +116,14 @@ config CRASH_DUMP
 	  For s390, this option also enables zfcpdump.
 	  See also <file:Documentation/arch/s390/zfcpdump.rst>
 
+config CRASH_DM_CRYPT
+	bool "Support saving crash dump to dm-crypt encrypted volume"
+	depends on CRASH_DUMP
+	help
+	  With this option enabled, user space can intereact with
+	  /sys/kernel/crash_dm_crypt_keys to make the dm crypt keys
+	  persistent for the crash dump kernel.
+
 config CRASH_HOTPLUG
 	bool "Update the crash elfcorehdr on system configuration changes"
 	default y
kernel/Makefile +1 -0
index 3c13240dfc9f..f2e5b3e86d12 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o elfcorehdr.o
 obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_CRASH_DUMP) += crash_core.o
+obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
 obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/crash_dump_dm_crypt.c new file +29 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <keys/user-type.h>
+#include <linux/crash_dump.h>
+
+#define KEY_NUM_MAX 128
+#define KEY_SIZE_MAX 256
+
+// The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
+#define KEY_DESC_LEN 48
+
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static enum STATE_ENUM {
+	FRESH = 0,
+	INITIALIZED,
+	RECORDED,
+	LOADED,
+} state;
+
+static unsigned int key_count;
+static size_t keys_header_size;
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;

kernel/crash_dump_dm_crypt.c line 39

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}
+
+static int init(const char *buf)

~~~~ A more interesting name with more description?

Quoted 42 lines
kernel/crash_dump_dm_crypt.c new file +42 -0
+{
+	unsigned int total_keys;
+	char dummy[5];
+
+	if (sscanf(buf, "%4s %u", dummy, &total_keys) != 2)
+		return -EINVAL;
+
+	if (key_count > KEY_NUM_MAX) {
+		pr_err("Exceed the maximum number of keys (KEY_NUM_MAX=%u)\n",
+		       KEY_NUM_MAX);
+		return -EINVAL;
+	}
+
+	keys_header_size = get_keys_header_size(keys_header, total_keys);
+	key_count = 0;
+
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header)
+		return -ENOMEM;
+
+	keys_header->key_count = total_keys;
+	state = INITIALIZED;
+	return 0;
+}
+
+static int record_key_desc(const char *buf, struct dm_crypt_key *dm_key)
+{
+	char key_desc[KEY_DESC_LEN];
+	char dummy[7];
+
+	if (state != INITIALIZED)
+		pr_err("Please send the cmd 'init <KEY_NUM>' first\n");
+
+	if (sscanf(buf, "%6s %s", dummy, key_desc) != 2)
+		return -EINVAL;
+
+	if (key_count >= keys_header->key_count) {
+		pr_warn("Already have %u keys", key_count);
+		return -EINVAL;
+	}
+
+	strscpy(dm_key->key_desc, key_desc, KEY_DESC_LEN);

kernel/crash_dump_dm_crypt.c line 91

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+	pr_debug("Key%d (%s) recorded\n", key_count, dm_key->key_desc);
+	key_count++;
+
+	if (key_count == keys_header->key_count)
+		state = RECORDED;
+
+	return 0;
+}
+
+static int process_cmd(const char *buf, size_t count)
                                                  ~~~~

If nobody use the count, why do you add it?

Quoted 65 lines
kernel/crash_dump_dm_crypt.c new file +22 -0
+{
+	if (strncmp(buf, "init ", 5) == 0)
+		return init(buf);
+	else if (strncmp(buf, "record ", 7) == 0)
+		return record_key_desc(buf, &keys_header->keys[key_count]);
+
+	return -EINVAL;
+}
+
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count)
+{
+	if (!is_kdump_kernel())
+		return process_cmd(buf, count);
+	return -EINVAL;
+}
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_write);
+
+int crash_sysfs_dm_crypt_keys_read(char *buf)
+{
+	return sprintf(buf, "%s\n", STATE_STR[state]);
+}
+EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_read);
kernel/ksysfs.c +22 -0
index 07fb5987b42b..2ba4dcbf5816 100644
@@ -167,6 +167,25 @@ static ssize_t vmcoreinfo_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(vmcoreinfo);
 
+#ifdef CONFIG_CRASH_DM_CRYPT
+static ssize_t crash_dm_crypt_keys_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)
+{
+	return crash_sysfs_dm_crypt_keys_read(buf);
+}
+
+static ssize_t crash_dm_crypt_keys_store(struct kobject *kobj,
+					 struct kobj_attribute *attr,
+					 const char *buf, size_t count)
+{
+	int ret;
+
+	ret = crash_sysfs_dm_crypt_keys_write(buf, count);
+	return ret < 0 ? ret : count;
+}
+KERNEL_ATTR_RW(crash_dm_crypt_keys);
+#endif /* CONFIG_CRASH_DM_CRYPT */
+
 #ifdef CONFIG_CRASH_HOTPLUG
 static ssize_t crash_elfcorehdr_size_show(struct kobject *kobj,
 			       struct kobj_attribute *attr, char *buf)
@@ -271,6 +290,9 @@ static struct attribute * kernel_attrs[] = {
 #endif
 #ifdef CONFIG_VMCORE_INFO
 	&vmcoreinfo_attr.attr,
+#ifdef CONFIG_CRASH_DM_CRYPT
+	&crash_dm_crypt_keys_attr.attr,
+#endif
 #ifdef CONFIG_CRASH_HOTPLUG
 	&crash_elfcorehdr_size_attr.attr,
 #endif
Signature · 1 line
2.45.0
author
#8 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 1 line · Baoquan He

Hi Coiby,

Hi Baoquan,

Quoted 10 lines · Coiby Xu
Quoted 8 lines

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

Quoted 4 lines
Quoted 1 line

User space can also read this API to learn about current state.

From the subject, can I think the luks keys will persist forever? or only for a while?

Yes, you are right. The keys need to stay in kdump reserved memory.

Quoted 3 lines

If need and can only keep it for a while, can you
mention it and tell why and how it will be used. Because you add a lot of codes, but only simply mention the sysfs, that doesn't make sense.

Thanks for raising the concern! I've added Documentation/ABI/testing/crash_dm_crypt_keys and copy some text in the cover letter to this patch in v5.

Quoted 19 lines
Quoted 19 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +0 -0
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif

[...]

Quoted 2 lines
Quoted 1 line

+static int init(const char *buf)

~~~~ A more interesting name with more description?

Thanks for the suggestion! I've added some comments for this function in v5. But I can't come up with a better name after looking at current kernel code. You are welcome to suggest any better name:)

Quoted 3 lines
Quoted 1 line

+static int process_cmd(const char *buf, size_t count)

                                                 ~~~~

If nobody use the count, why do you add it?

Good catch! Yes, this is no need to use count in v4. But v5 now needs it to avoid buffer overflow.

Signature · 2 lines
Best regards,
Coiby
#9 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 15 lines · Coiby Xu
Quoted 1 line · Baoquan He

Hi Coiby,

Hi Baoquan,

Quoted 10 lines · Coiby Xu
Quoted 8 lines

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

User space can also read this API to learn about current state.

From the subject, can I think the luks keys will persist forever? or only for a while?

Yes, you are right. The keys need to stay in kdump reserved memory.

Hmm, there are two different concepts we may need differentiate. From security keys's point of view, the keys need be stored for a while so that kdump loading take action to get it, that's done through sysfs; Froom kdump's point of view, the keys need be stored forever till kdump kernel use it. I can't see what you are referring to from the subject, esepcially you stress the newly added sysfs /sys/kernel/crash_dm_crypt_keys.

Quoted 29 lines
Quoted 4 lines

If need and can only keep it for a while, can you
mention it and tell why and how it will be used. Because you add a lot of codes, but only simply mention the sysfs, that doesn't make sense.

Thanks for raising the concern! I've added Documentation/ABI/testing/crash_dm_crypt_keys and copy some text in the cover letter to this patch in v5.

Quoted 19 lines
Quoted 19 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +0 -0
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif

[...]

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

+static int init(const char *buf)

~~~~ A more interesting name with more description?

Thanks for the suggestion! I've added some comments for this function in v5. But I can't come up with a better name after looking at current kernel code. You are welcome to suggest any better name:)

Usually init() is for the whole driver module. Your init() here only receive the passed total keys number, and allocate the key_header, how can you simply name it init()? If you call it init_keys_header(), I would think it's much more meaningful.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

+static int process_cmd(const char *buf, size_t count)

                                                 ~~~~

If nobody use the count, why do you add it?

Good catch! Yes, this is no need to use count in v4. But v5 now needs it to avoid buffer overflow.

OK, did you add code comment telling what 'count' stands for?

And the name 'process_cmd()' is also ambiguous. We may need avoid this kind of name, e.g process_cmd, do_things, handle_stuff. Can you add a more specific name?

author
#10 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 23 lines · Baoquan He
Quoted 15 lines · Coiby Xu
Quoted 1 line · Baoquan He

Hi Coiby,

Hi Baoquan,

Quoted 10 lines · Coiby Xu
Quoted 8 lines

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

User space can also read this API to learn about current state.

From the subject, can I think the luks keys will persist forever? or only for a while?

Yes, you are right. The keys need to stay in kdump reserved memory.

Quoted 7 lines

Hmm, there are two different concepts we may need differentiate. From security keys's point of view, the keys need be stored for a while so that kdump loading take action to get it, that's done through sysfs; Froom kdump's point of view, the keys need be stored forever till kdump kernel use it. I can't see what you are referring to from the subject, esepcially you stress the newly added sysfs /sys/kernel/crash_dm_crypt_keys.

Thanks for the suggestion! The patch set's goal is make the kdump kernel to be able to unlock encrypted volumes so the keys have always to be there to be ready for the kdump kernel. In fact, the 1st kernel also keeps the keys in runtime memory because I/O data could be written back to disk anytime and thus needs to be encrypted with the keys anytime. But keys are sensitive data so we try to make it as safe as possible and one measure for example to make it stay in the keyring for relatively short time. I'll add a note in next version's commit message. Together with the info of the life cycle of keys, hopefully it will bring some clarities

Quoted 33 lines
Quoted 33 lines
Quoted 5 lines

If need and can only keep it for a while, can you
mention it and tell why and how it will be used. Because you add a lot of codes, but only simply mention the sysfs, that doesn't make sense.

Thanks for raising the concern! I've added Documentation/ABI/testing/crash_dm_crypt_keys and copy some text in the cover letter to this patch in v5.

Quoted 19 lines
Quoted 19 lines
---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +0 -0
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif

[...]

Quoted 2 lines
Quoted 1 line

+static int init(const char *buf)

~~~~ A more interesting name with more description?

Quoted 8 lines
Quoted 3 lines

Thanks for the suggestion! I've added some comments for this function in v5. But I can't come up with a better name after looking at current kernel code. You are welcome to suggest any better name:)

Usually init() is for the whole driver module. Your init() here only receive the passed total keys number, and allocate the key_header, how can you simply name it init()? If you call it init_keys_header(), I would think it's much more meaningful.

Good suggestion! Yes, init_keys_header would be a much better choice! Unfortunately a new version will switch to configfs. So I will keep this lesson in mind and try to apply it to similar cases in the future.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

+static int process_cmd(const char *buf, size_t count)

                                                 ~~~~

If nobody use the count, why do you add it?

Quoted 8 lines
Quoted 2 lines

Good catch! Yes, this is no need to use count in v4. But v5 now needs it to avoid buffer overflow.

OK, did you add code comment telling what 'count' stands for?

And the name 'process_cmd()' is also ambiguous. We may need avoid this kind of name, e.g process_cmd, do_things, handle_stuff. Can you add a more specific name?

As new version will switch to configfs, process_cmd will no longer be needed. But I'll try coming up with better names in the future. Thanks!

Signature · 2 lines
Best regards,
Coiby
#11 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

In reply to Coiby Xu

On 05/23/24 at 01:04pm, Coiby Xu wrote:
.....

Quoted 16 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <keys/user-type.h>
+#include <linux/crash_dump.h>
+
+#define KEY_NUM_MAX 128
+#define KEY_SIZE_MAX 256
+
+// The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
+#define KEY_DESC_LEN 48
+

kernel/crash_dump_dm_crypt.c line 20

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static enum STATE_ENUM {
+	FRESH = 0,
+	INITIALIZED,
+	RECORDED,
+	LOADED,
+} state;
+
+static unsigned int key_count;
+static size_t keys_header_size;

These two global variables seems not so necessary. Please see comment at below.

kernel/crash_dump_dm_crypt.c line 29

Quoted 9 lines
kernel/crash_dump_dm_crypt.c new file +9 -0
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
                     ~~~~~~~~
                     This is the max number a system have from init();

You can add one field member to record how many key slots have been used.

kernel/crash_dump_dm_crypt.c line 31

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;

Maybe we can rearrange the keys_header like below, the name may not be very appropriate though.

static struct keys_header {
	unsigned int max_key_slots;
	unsigned int used_key_slots;
	struct dm_crypt_key keys[] __counted_by(key_count);
} *keys_header;

kernel/crash_dump_dm_crypt.c line 37

Quoted 8 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}

I personally don't think get_keys_header_size is so necessary. If we have to keep it, may be we can remove the global variable keys_header_size, we can call get_keys_header_size() and use local variable to record the value instead.

author
#12 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 9 lines · Baoquan He

On 05/23/24 at 01:04pm, Coiby Xu wrote:
.....

Quoted 7 lines
kernel/crash_dump_dm_crypt.c new file +1 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only

[...]

Quoted 6 lines

kernel/crash_dump_dm_crypt.c line 20

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +3 -0
+
+static unsigned int key_count;
+static size_t keys_header_size;

These two global variables seems not so necessary. Please see comment at below.

Thanks for the comment! But I think it's better to keep these two static variables for reasons as will be explained later.

Quoted 20 lines

kernel/crash_dump_dm_crypt.c line 29

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +9 -0
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
                    ~~~~~~~~
                    This is the max number a system have from init();

You can add one field member to record how many key slots have been used.

kernel/crash_dump_dm_crypt.c line 31

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;

Maybe we can rearrange the keys_header like below, the name may not be very appropriate though.

Quoted 5 lines
static struct keys_header {
	unsigned int max_key_slots;
	unsigned int used_key_slots;
	struct dm_crypt_key keys[] __counted_by(key_count);
} *keys_header;

Thanks for the suggestion! Since 1) KEY_NUM_MAX already defines the maximum number of dm crypt keys 2) we only need to let the kdump kernel now how many keys are saved, so I simply use total_keys instead of key_count in struct keys_header in v5,

static struct keys_header {
	unsigned int total_keys;
	struct dm_crypt_key keys[] __counted_by(total_keys);
} *keys_header;

Hopefully this renaming will improve code readability.

Quoted 14 lines

kernel/crash_dump_dm_crypt.c line 37

Quoted 9 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}

I personally don't think get_keys_header_size is so necessary. If we have to keep it, may be we can remove the global variable keys_header_size, we can call get_keys_header_size() and use local variable to record the value instead.

Thanks for the suggestion! But the kdump kernel also need to call get_keys_header_size in later patches.

Signature · 2 lines
Best regards,
Coiby
#13 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 52 lines · Coiby Xu
Quoted 9 lines · Baoquan He

On 05/23/24 at 01:04pm, Coiby Xu wrote:
.....

Quoted 7 lines
kernel/crash_dump_dm_crypt.c new file +1 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only

[...]

Quoted 6 lines

kernel/crash_dump_dm_crypt.c line 20

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +3 -0
+
+static unsigned int key_count;
+static size_t keys_header_size;

These two global variables seems not so necessary. Please see comment at below.

Thanks for the comment! But I think it's better to keep these two static variables for reasons as will be explained later.

Quoted 20 lines

kernel/crash_dump_dm_crypt.c line 29

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +9 -0
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
                    ~~~~~~~~
                    This is the max number a system have from init();

You can add one field member to record how many key slots have been used.

kernel/crash_dump_dm_crypt.c line 31

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;

Maybe we can rearrange the keys_header like below, the name may not be very appropriate though.

Quoted 5 lines
static struct keys_header {
	unsigned int max_key_slots;
	unsigned int used_key_slots;
	struct dm_crypt_key keys[] __counted_by(key_count);
} *keys_header;

Thanks for the suggestion! Since 1) KEY_NUM_MAX already defines the maximum number of dm crypt keys 2) we only need to let the kdump kernel now how many keys are saved, so I simply use total_keys instead of key_count in struct keys_header in v5,

Quoted 6 lines
static struct keys_header {
	unsigned int total_keys;
	struct dm_crypt_key keys[] __counted_by(total_keys);
} *keys_header;

Hopefully this renaming will improve code readability.

If you add key_count into keys_header, then kdump kernel will know how many keys are really saved and need be retrieved. What's your concern when you have to put key_count outside and take it as a global variable?

Quoted 16 lines
Quoted 4 lines
Quoted 3 lines
Quoted 11 lines

kernel/crash_dump_dm_crypt.c line 37

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}

I personally don't think get_keys_header_size is so necessary. If we have to keep it, may be we can remove the global variable keys_header_size, we can call get_keys_header_size() and use local variable to record the value instead.

Quoted 2 lines

Thanks for the suggestion! But the kdump kernel also need to call get_keys_header_size in later patches.

If so, you can remove keys_header_size now. You can define local variable to take the newly calculated value. keys_header_size seems not so necesary.

By the way, you don't need to rush to post v5. When people review patches, agreement need be reached after discussion. Then next version can be posted.

author
#14 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 58 lines · Baoquan He
Quoted 57 lines · Coiby Xu
Quoted 9 lines · Baoquan He

On 05/23/24 at 01:04pm, Coiby Xu wrote:
.....

Quoted 7 lines
kernel/crash_dump_dm_crypt.c new file +1 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only

[...]

Quoted 6 lines

kernel/crash_dump_dm_crypt.c line 20

Quoted 3 lines
kernel/crash_dump_dm_crypt.c new file +3 -0
+
+static unsigned int key_count;
+static size_t keys_header_size;

These two global variables seems not so necessary. Please see comment at below.

Thanks for the comment! But I think it's better to keep these two static variables for reasons as will be explained later.

Quoted 20 lines

kernel/crash_dump_dm_crypt.c line 29

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +9 -0
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
                    ~~~~~~~~
                    This is the max number a system have from init();

You can add one field member to record how many key slots have been used.

kernel/crash_dump_dm_crypt.c line 31

Quoted 2 lines
kernel/crash_dump_dm_crypt.c new file +2 -0
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;

Maybe we can rearrange the keys_header like below, the name may not be very appropriate though.

Quoted 5 lines
static struct keys_header {
	unsigned int max_key_slots;
	unsigned int used_key_slots;
	struct dm_crypt_key keys[] __counted_by(key_count);
} *keys_header;

Thanks for the suggestion! Since 1) KEY_NUM_MAX already defines the maximum number of dm crypt keys 2) we only need to let the kdump kernel now how many keys are saved, so I simply use total_keys instead of key_count in struct keys_header in v5,

static struct keys_header {
	unsigned int total_keys;
	struct dm_crypt_key keys[] __counted_by(total_keys);
} *keys_header;
Quoted 5 lines
Quoted 1 line

Hopefully this renaming will improve code readability.

If you add key_count into keys_header, then kdump kernel will know how many keys are really saved and need be retrieved. What's your concern when you have to put key_count outside and take it as a global variable?

Yes, the kdump kernel can know how many keys by reading keys_header. But 1st kernel needs to know how many keys are recorded so far as user space is expected to tell the kernel which keys are needed one by one and the key_count will be increased witch each new key.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

kernel/crash_dump_dm_crypt.c line 37

Quoted 11 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{
+	return struct_size(keys_header, keys, key_count);
+}

I personally don't think get_keys_header_size is so necessary. If we have to keep it, may be we can remove the global variable keys_header_size, we can call get_keys_header_size() and use local variable to record the value instead.

Quoted 6 lines
Quoted 2 lines

Thanks for the suggestion! But the kdump kernel also need to call get_keys_header_size in later patches.

If so, you can remove keys_header_size now. You can define local variable to take the newly calculated value. keys_header_size seems not so necesary.

Sure, new version will have the global variable keys_header_size removed which can make the code a bit easier to reason about, thanks!

Quoted 4 lines

By the way, you don't need to rush to post v5. When people review patches, agreement need be reached after discussion. Then next version can be posted.

Thanks for the suggestion! I'll wait for the consensus to be truly reached next time.

And sorry, it takes longer time than I have expected to evaluate all the feedback. In retrospective, maybe it's better to respond to each feedback in a parallel manner than pondering over all the feedback first.

Signature · 2 lines
Best regards,
Coiby
#15 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

In reply to Coiby Xu
Quoted 7 lines · Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

This patch highly lack document to describe what it's doing. For example, how you will manipulate the /sys/kernel/crash_dm_crypt_keys to initialize, record the keys, can you give examples how you opeate on them?

Quoted 34 lines

User space can also read this API to learn about current state.

---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +4 -1
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif
 
-
+#ifdef CONFIG_CRASH_DM_CRYPT
+int crash_sysfs_dm_crypt_keys_read(char *buf);
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+#endif
 
 #ifndef arch_crash_handle_hotplug_event
 static inline void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { }
kernel/Kconfig.kexec +0 -0
index 6c34e63c88ff..88525ad1c80a 100644

kernel/Kconfig.kexec config CRASH_DUMP · line 121

Quoted 6 lines
kernel/Kconfig.kexec +3 -0
@@ -116,6 +116,14 @@ config CRASH_DUMP
 	  For s390, this option also enables zfcpdump.
 	  See also <file:Documentation/arch/s390/zfcpdump.rst>
 
+config CRASH_DM_CRYPT
+	bool "Support saving crash dump to dm-crypt encrypted volume"
+	depends on CRASH_DUMP

Do we need add dependency on some security features, e.g KEYS? The current code will enable the CRASH_DM_CRYPT regardless of the existence of LUKS disk at all.

Quoted 61 lines
kernel/Kconfig.kexec +5 -0
+	help
+	  With this option enabled, user space can intereact with
+	  /sys/kernel/crash_dm_crypt_keys to make the dm crypt keys
+	  persistent for the crash dump kernel.
+
 config CRASH_HOTPLUG
 	bool "Update the crash elfcorehdr on system configuration changes"
 	default y
kernel/Makefile +1 -0
index 3c13240dfc9f..f2e5b3e86d12 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o elfcorehdr.o
 obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_CRASH_DUMP) += crash_core.o
+obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
 obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/crash_dump_dm_crypt.c new file +35 -0
new file mode 100644
index 000000000000..78809189084a
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <keys/user-type.h>
+#include <linux/crash_dump.h>
+
+#define KEY_NUM_MAX 128
+#define KEY_SIZE_MAX 256
+
+// The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
+#define KEY_DESC_LEN 48
+
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static enum STATE_ENUM {
+	FRESH = 0,
+	INITIALIZED,
+	RECORDED,
+	LOADED,
+} state;
+
+static unsigned int key_count;
+static size_t keys_header_size;
+
+struct dm_crypt_key {
+	unsigned int key_size;
+	char key_desc[KEY_DESC_LEN];
+	u8 data[KEY_SIZE_MAX];
+};
+
+static struct keys_header {
+	unsigned int key_count;
+	struct dm_crypt_key keys[] __counted_by(key_count);
+} *keys_header;
+
+static size_t get_keys_header_size(struct keys_header *keys_header,
+				   size_t key_count)
+{

kernel/crash_dump_dm_crypt.c line 45

Quoted 10 lines
kernel/crash_dump_dm_crypt.c new file +10 -0
+	return struct_size(keys_header, keys, key_count);
+}
+
+static int init(const char *buf)
+{
+	unsigned int total_keys;
+	char dummy[5];
+
+	if (sscanf(buf, "%4s %u", dummy, &total_keys) != 2)
+		return -EINVAL;

This is what I wondered and tried to find a document to get why. Can we search in the current system and deduce how many keys we can could use for kdump kernel? Or we have to retrieve and pass it from user space?

kernel/crash_dump_dm_crypt.c line 51

Quoted 6 lines
kernel/crash_dump_dm_crypt.c new file +6 -0
+
+	if (key_count > KEY_NUM_MAX) {
+		pr_err("Exceed the maximum number of keys (KEY_NUM_MAX=%u)\n",
+		       KEY_NUM_MAX);
+		return -EINVAL;
+	}

Why chekcing key_count in init()? Don't you need to check total_keys instead? Clearly you don't do a boundary test for total_keys, otherwise it will trigger issue.

kernel/crash_dump_dm_crypt.c line 63

Quoted 12 lines
kernel/crash_dump_dm_crypt.c new file +12 -0
+
+	keys_header_size = get_keys_header_size(keys_header, total_keys);
+	key_count = 0;
+
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header)
+		return -ENOMEM;
+
+	keys_header->key_count = total_keys;
+	state = INITIALIZED;
+	return 0;
+}

Please add more code comments, kernel-doc for your code, we can't assume people reading these codes know the entire matter.

author
#16 lore raw

Re: [PATCH v4 2/7] crash_dump: make dm crypt keys persist for the kdump kernel

Quoted 9 lines · Baoquan He
Quoted 7 lines · Coiby Xu

A sysfs /sys/kernel/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. User space can send the following commands,
- "init KEY_NUM"
Initialize needed structures
- "record KEY_DESC"
Record a key description. The key must be a logon key.

Quoted 4 lines

This patch highly lack document to describe what it's doing. For example, how you will manipulate the /sys/kernel/crash_dm_crypt_keys to initialize, record the keys, can you give examples how you opeate on them?

Thanks for the suggestion! v5 now includes
Documentation/ABI/testing/crash_dm_crypt_keys.

Quoted 38 lines
Quoted 38 lines

User space can also read this API to learn about current state.

---
 include/linux/crash_core.h   |   5 +-
 kernel/Kconfig.kexec         |   8 +++
 kernel/Makefile              |   1 +
 kernel/crash_dump_dm_crypt.c | 113 +++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c              |  22 +++++++
 5 files changed, 148 insertions(+), 1 deletion(-)
 create mode 100644 kernel/crash_dump_dm_crypt.c
include/linux/crash_core.h +4 -1
index 44305336314e..6bff1c24efa3 100644
@@ -34,7 +34,10 @@ static inline void arch_kexec_protect_crashkres(void) { }
 static inline void arch_kexec_unprotect_crashkres(void) { }
 #endif
 
-
+#ifdef CONFIG_CRASH_DM_CRYPT
+int crash_sysfs_dm_crypt_keys_read(char *buf);
+int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+#endif
 
 #ifndef arch_crash_handle_hotplug_event
 static inline void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { }
kernel/Kconfig.kexec +0 -0
index 6c34e63c88ff..88525ad1c80a 100644
@@ -116,6 +116,14 @@ config CRASH_DUMP
 	  For s390, this option also enables zfcpdump.
 	  See also <file:Documentation/arch/s390/zfcpdump.rst>
 
Quoted 7 lines

kernel/Kconfig.kexec config CRASH_DUMP · line 121

Quoted 3 lines
kernel/Kconfig.kexec +3 -0
+config CRASH_DM_CRYPT
+	bool "Support saving crash dump to dm-crypt encrypted volume"
+	depends on CRASH_DUMP

Do we need add dependency on some security features, e.g KEYS? The current code will enable the CRASH_DM_CRYPT regardless of the existence of LUKS disk at all.

Good suggestion! I make CRASH_DM_CRYPT depend on DM_CRYPT in v5.

Quoted 12 lines

[...]

kernel/Kconfig.kexec

Quoted 7 lines
kernel/Kconfig.kexec +7 -0
+static int init(const char *buf)
+{
+	unsigned int total_keys;
+	char dummy[5];
+
+	if (sscanf(buf, "%4s %u", dummy, &total_keys) != 2)
+		return -EINVAL;

This is what I wondered and tried to find a document to get why. Can we search in the current system and deduce how many keys we can could use for kdump kernel? Or we have to retrieve and pass it from user space?

I don't think we can get this info from the kernel space directly because the kernel don't know the kdump target. So we have to pass this info from user space.

Quoted 11 lines

kernel/Kconfig.kexec

Quoted 7 lines
kernel/Kconfig.kexec +6 -0
+
+	if (key_count > KEY_NUM_MAX) {
+		pr_err("Exceed the maximum number of keys (KEY_NUM_MAX=%u)\n",
+		       KEY_NUM_MAX);
+		return -EINVAL;
+	}

Why chekcing key_count in init()? Don't you need to check total_keys instead? Clearly you don't do a boundary test for total_keys, otherwise it will trigger issue.

Good catch! Yes, I should check total_keys instead.

Quoted 16 lines

kernel/Kconfig.kexec

Quoted 13 lines
kernel/Kconfig.kexec +12 -0
+
+	keys_header_size = get_keys_header_size(keys_header, total_keys);
+	key_count = 0;
+
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header)
+		return -ENOMEM;
+
+	keys_header->key_count = total_keys;
+	state = INITIALIZED;
+	return 0;
+}

Please add more code comments, kernel-doc for your code, we can't assume people reading these codes know the entire matter.

Thanks for the suggestion! I've added some comments and documentation in v5. Please let me know if there is anything more to improve.

Signature · 2 lines
Best regards,
Coiby
author
#17 lore raw

PATCH v4 3/7 crash_dump: store dm keys in kdump reserved memory

In reply to Coiby Xu

When the kdump kernel image and initrd are loaded, the dm crypts keys will be read from keyring and then stored in kdump reserved memory.

---
 include/linux/crash_core.h   |  3 ++
 include/linux/crash_dump.h   |  2 +
 include/linux/kexec.h        |  4 ++
 kernel/crash_dump_dm_crypt.c | 87 ++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
include/linux/crash_core.h +3 -0
index 6bff1c24efa3..ab20829d0bc9 100644
@@ -37,6 +37,9 @@ static inline void arch_kexec_unprotect_crashkres(void) { }
 #ifdef CONFIG_CRASH_DM_CRYPT
 int crash_sysfs_dm_crypt_keys_read(char *buf);
 int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
+int crash_load_dm_crypt_keys(struct kimage *image);
+#else
+static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; }
 #endif
 
 #ifndef arch_crash_handle_hotplug_event
include/linux/crash_dump.h +2 -0
index acc55626afdc..dfd8e4fe6129 100644
@@ -15,6 +15,8 @@
 extern unsigned long long elfcorehdr_addr;
 extern unsigned long long elfcorehdr_size;
 
+extern unsigned long long dm_crypt_keys_addr;
+
 #ifdef CONFIG_CRASH_DUMP
 extern int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size);
 extern void elfcorehdr_free(unsigned long long addr);
include/linux/kexec.h +4 -0
index cc81b8a903ab..bd40f4208e1f 100644
@@ -370,6 +370,10 @@ struct kimage {
 	void *elf_headers;
 	unsigned long elf_headers_sz;
 	unsigned long elf_load_addr;
+
+	/* dm crypt keys buffer */
+	unsigned long dm_crypt_keys_addr;
+	unsigned long dm_crypt_keys_sz;
 };
 
 /* kexec interface functions */
kernel/crash_dump_dm_crypt.c +87 -0

94 lines not on this page · raw message

Signature · 1 line
2.45.0
#18 lore raw

Re: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory

kernel test robot noticed the following build errors:

Hi Coiby,

kernel test robot noticed the following build errors:

[auto build test ERROR on de7e71ef8bed222dd144d8878091ecb6d5dfd208]

url:    https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/kexec_file-allow-to-place-kexec_buf-randomly/20240523-130727
base:   de7e71ef8bed222dd144d8878091ecb6d5dfd208
patch link:    https://lore.kernel.org/r/20240523050451.788754-4-coxu%40redhat.com
patch subject: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory
config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20240524/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240524/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/crash_dump_dm_crypt.c: In function 'crash_load_dm_crypt_keys':

>> kernel/crash_dump_dm_crypt.c:158:16: error: variable 'kbuf' has initializer but incomplete type
     158 |         struct kexec_buf kbuf = {
         |                ^~~~~~~~~
>> kernel/crash_dump_dm_crypt.c:159:18: error: 'struct kexec_buf' has no member named 'image'
     159 |                 .image = image,
         |                  ^~~~~
   kernel/crash_dump_dm_crypt.c:159:26: warning: excess elements in struct initializer
     159 |                 .image = image,
         |                          ^~~~~
   kernel/crash_dump_dm_crypt.c:159:26: note: (near initialization for 'kbuf')
>> kernel/crash_dump_dm_crypt.c:160:18: error: 'struct kexec_buf' has no member named 'buf_min'
     160 |                 .buf_min = 0,
         |                  ^~~~~~~
   kernel/crash_dump_dm_crypt.c:160:28: warning: excess elements in struct initializer
     160 |                 .buf_min = 0,
         |                            ^
   kernel/crash_dump_dm_crypt.c:160:28: note: (near initialization for 'kbuf')
>> kernel/crash_dump_dm_crypt.c:161:18: error: 'struct kexec_buf' has no member named 'buf_max'
     161 |                 .buf_max = ULONG_MAX,
         |                  ^~~~~~~
   In file included from include/linux/limits.h:7,
                    from include/linux/thread_info.h:12,
                    from include/asm-generic/preempt.h:5,
                    from ./arch/arm/include/generated/asm/preempt.h:1,
                    from include/linux/preempt.h:79,
                    from include/linux/rcupdate.h:27,
                    from include/linux/rbtree.h:24,
                    from include/linux/key.h:15,
                    from kernel/crash_dump_dm_crypt.c:2:
   include/vdso/limits.h:13:25: warning: excess elements in struct initializer
      13 | #define ULONG_MAX       (~0UL)
         |                         ^
   kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
     161 |                 .buf_max = ULONG_MAX,
         |                            ^~~~~~~~~
   include/vdso/limits.h:13:25: note: (near initialization for 'kbuf')
      13 | #define ULONG_MAX       (~0UL)
         |                         ^
   kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
     161 |                 .buf_max = ULONG_MAX,
         |                            ^~~~~~~~~
>> kernel/crash_dump_dm_crypt.c:162:18: error: 'struct kexec_buf' has no member named 'top_down'
     162 |                 .top_down = false,
         |                  ^~~~~~~~
   kernel/crash_dump_dm_crypt.c:162:29: warning: excess elements in struct initializer
     162 |                 .top_down = false,
         |                             ^~~~~
   kernel/crash_dump_dm_crypt.c:162:29: note: (near initialization for 'kbuf')
>> kernel/crash_dump_dm_crypt.c:163:18: error: 'struct kexec_buf' has no member named 'random'
     163 |                 .random = true,
         |                  ^~~~~~
   kernel/crash_dump_dm_crypt.c:163:27: warning: excess elements in struct initializer
     163 |                 .random = true,
         |                           ^~~~
   kernel/crash_dump_dm_crypt.c:163:27: note: (near initialization for 'kbuf')
>> kernel/crash_dump_dm_crypt.c:158:26: error: storage size of 'kbuf' isn't known
     158 |         struct kexec_buf kbuf = {
         |                          ^~~~
>> kernel/crash_dump_dm_crypt.c:187:20: error: 'KEXEC_BUF_MEM_UNKNOWN' undeclared (first use in this function)
     187 |         kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
         |                    ^~~~~~~~~~~~~~~~~~~~~
   kernel/crash_dump_dm_crypt.c:187:20: note: each undeclared identifier is reported only once for each function it appears in
>> kernel/crash_dump_dm_crypt.c:188:13: error: implicit declaration of function 'kexec_add_buffer' [-Werror=implicit-function-declaration]
     188 |         r = kexec_add_buffer(&kbuf);
         |             ^~~~~~~~~~~~~~~~
   kernel/crash_dump_dm_crypt.c:158:26: warning: unused variable 'kbuf' [-Wunused-variable]
     158 |         struct kexec_buf kbuf = {
         |                          ^~~~

cc1: some warnings being treated as errors

vim +/kbuf +158 kernel/crash_dump_dm_crypt.c

155
156 int crash_load_dm_crypt_keys(struct kimage *image)
157 {

 > 158		struct kexec_buf kbuf = {
 > 159			.image = image,
 > 160			.buf_min = 0,
 > 161			.buf_max = ULONG_MAX,
 > 162			.top_down = false,
 > 163			.random = true,
   164		};
   165	
   166		int r;
   167	
   168		if (state == FRESH)
   169			return 0;
   170	
   171		if (key_count != keys_header->key_count) {
   172			pr_err("Only record %u keys (%u in total)\n", key_count,
   173			       keys_header->key_count);
   174			return -EINVAL;
   175		}
   176	
   177		image->dm_crypt_keys_addr = 0;
   178		r = build_keys_header();
   179		if (r)
   180			return r;
   181	
   182		kbuf.buffer = keys_header;
   183		kbuf.bufsz = keys_header_size;
   184	
   185		kbuf.memsz = kbuf.bufsz;
   186		kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
 > 187		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 > 188		r = kexec_add_buffer(&kbuf);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#19 lore raw

Re: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory

Hi Coiby,

Quoted 14 lines · kernel test robot

Hi Coiby,

kernel test robot noticed the following build errors:

[auto build test ERROR on de7e71ef8bed222dd144d8878091ecb6d5dfd208]

url:    https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/kexec_file-allow-to-place-kexec_buf-randomly/20240523-130727
base:   de7e71ef8bed222dd144d8878091ecb6d5dfd208
patch link:    https://lore.kernel.org/r/20240523050451.788754-4-coxu%40redhat.com
patch subject: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory
config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20240524/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240524/[email protected]/reproduce)
Quoted 6 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Please respond to lkp report sooner, otherwise the reproducer link could be unavailable.

Quoted 114 lines

kernel/crash_dump_dm_crypt.c: In function 'crash_load_dm_crypt_keys':

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:158:16: error: variable 'kbuf' has initializer but incomplete type

     158 |         struct kexec_buf kbuf = {
         |                ^~~~~~~~~
Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:159:18: error: 'struct kexec_buf' has no member named 'image'

159 | .image = image,
| ^~~~~
kernel/crash_dump_dm_crypt.c:159:26: warning: excess elements in struct initializer
159 | .image = image,
| ^~~~~
kernel/crash_dump_dm_crypt.c:159:26: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:160:18: error: 'struct kexec_buf' has no member named 'buf_min'

     160 |                 .buf_min = 0,
         |                  ^~~~~~~
   kernel/crash_dump_dm_crypt.c:160:28: warning: excess elements in struct initializer
     160 |                 .buf_min = 0,
         |                            ^

kernel/crash_dump_dm_crypt.c:160:28: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:161:18: error: 'struct kexec_buf' has no member named 'buf_max'

161 | .buf_max = ULONG_MAX,
| ^~~~~~~
In file included from include/linux/limits.h:7,
from include/linux/thread_info.h:12,
from include/asm-generic/preempt.h:5,
from ./arch/arm/include/generated/asm/preempt.h:1, from include/linux/preempt.h:79,
from include/linux/rcupdate.h:27,
from include/linux/rbtree.h:24,
from include/linux/key.h:15,
from kernel/crash_dump_dm_crypt.c:2:
include/vdso/limits.h:13:25: warning: excess elements in struct initializer

      13 | #define ULONG_MAX       (~0UL)
         |                         ^
   kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
     161 |                 .buf_max = ULONG_MAX,
         |                            ^~~~~~~~~
   include/vdso/limits.h:13:25: note: (near initialization for 'kbuf')
      13 | #define ULONG_MAX       (~0UL)
         |                         ^

kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
161 | .buf_max = ULONG_MAX,
| ^~~~~~~~~

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:162:18: error: 'struct kexec_buf' has no member named 'top_down'

162 | .top_down = false,
| ^~~~~~~~
kernel/crash_dump_dm_crypt.c:162:29: warning: excess elements in struct initializer
162 | .top_down = false,
| ^~~~~
kernel/crash_dump_dm_crypt.c:162:29: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:163:18: error: 'struct kexec_buf' has no member named 'random'

163 | .random = true,
| ^~~~~~
kernel/crash_dump_dm_crypt.c:163:27: warning: excess elements in struct initializer
163 | .random = true,
| ^~~~
kernel/crash_dump_dm_crypt.c:163:27: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:158:26: error: storage size of 'kbuf' isn't known

     158 |         struct kexec_buf kbuf = {
         |                          ^~~~
Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:187:20: error: 'KEXEC_BUF_MEM_UNKNOWN' undeclared (first use in this function)

     187 |         kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
         |                    ^~~~~~~~~~~~~~~~~~~~~

kernel/crash_dump_dm_crypt.c:187:20: note: each undeclared identifier is reported only once for each function it appears in

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:188:13: error: implicit declaration of function 'kexec_add_buffer' [-Werror=implicit-function-declaration]

     188 |         r = kexec_add_buffer(&kbuf);
         |             ^~~~~~~~~~~~~~~~
   kernel/crash_dump_dm_crypt.c:158:26: warning: unused variable 'kbuf' [-Wunused-variable]
     158 |         struct kexec_buf kbuf = {
         |                          ^~~~

cc1: some warnings being treated as errors

vim +/kbuf +158 kernel/crash_dump_dm_crypt.c

   155	
   156	int crash_load_dm_crypt_keys(struct kimage *image)
   157	{
Quoted 6 lines

158 struct kexec_buf kbuf = {
159 .image = image,
160 .buf_min = 0,
161 .buf_max = ULONG_MAX,
162 .top_down = false,
163 .random = true,

   164		};
   165	
   166		int r;
   167	
   168		if (state == FRESH)
   169			return 0;
   170	
   171		if (key_count != keys_header->key_count) {
   172			pr_err("Only record %u keys (%u in total)\n", key_count,
   173			       keys_header->key_count);
   174			return -EINVAL;
   175		}
   176	
   177		image->dm_crypt_keys_addr = 0;
   178		r = build_keys_header();
   179		if (r)
   180			return r;
   181	
   182		kbuf.buffer = keys_header;
   183		kbuf.bufsz = keys_header_size;
   184	
   185		kbuf.memsz = kbuf.bufsz;
   186		kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
Quoted 2 lines

187 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
188 r = kexec_add_buffer(&kbuf);

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#20 lore raw

Re: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory

Quoted 1 line · Baoquan He

Hi Coiby,

Hi Baoquan,

Quoted 21 lines · kernel test robot
Quoted 19 lines

Hi Coiby,

kernel test robot noticed the following build errors:

[auto build test ERROR on de7e71ef8bed222dd144d8878091ecb6d5dfd208]

url:    https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/kexec_file-allow-to-place-kexec_buf-randomly/20240523-130727
base:   de7e71ef8bed222dd144d8878091ecb6d5dfd208
patch link:    https://lore.kernel.org/r/20240523050451.788754-4-coxu%40redhat.com
patch subject: [PATCH v4 3/7] crash_dump: store dm keys in kdump reserved memory
config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20240524/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240524/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 4 lines
Quoted 1 line

All errors (new ones prefixed by >>):

Please respond to lkp report sooner, otherwise the reproducer link could be unavailable.

Thanks for the reminder! I believe these errors are false positives
because 1) include/linux/kexec.h is included after "#include <linux/crash_dump.h>"
and 2) similar errors are seen for kernel/kexec_file.c by "make ARCH=arm CROSS_COMPILE=arm-linux-gnu- kernel/kexec_file.o"

Quoted 116 lines
Quoted 115 lines

kernel/crash_dump_dm_crypt.c: In function 'crash_load_dm_crypt_keys':

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:158:16: error: variable 'kbuf' has initializer but incomplete type

     158 |         struct kexec_buf kbuf = {
         |                ^~~~~~~~~
Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:159:18: error: 'struct kexec_buf' has no member named 'image'

159 | .image = image,
| ^~~~~
kernel/crash_dump_dm_crypt.c:159:26: warning: excess elements in struct initializer
159 | .image = image,
| ^~~~~
kernel/crash_dump_dm_crypt.c:159:26: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:160:18: error: 'struct kexec_buf' has no member named 'buf_min'

     160 |                 .buf_min = 0,
         |                  ^~~~~~~
   kernel/crash_dump_dm_crypt.c:160:28: warning: excess elements in struct initializer
     160 |                 .buf_min = 0,
         |                            ^

kernel/crash_dump_dm_crypt.c:160:28: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:161:18: error: 'struct kexec_buf' has no member named 'buf_max'

161 | .buf_max = ULONG_MAX,
| ^~~~~~~
In file included from include/linux/limits.h:7,
from include/linux/thread_info.h:12,
from include/asm-generic/preempt.h:5,
from ./arch/arm/include/generated/asm/preempt.h:1, from include/linux/preempt.h:79,
from include/linux/rcupdate.h:27,
from include/linux/rbtree.h:24,
from include/linux/key.h:15,
from kernel/crash_dump_dm_crypt.c:2:
include/vdso/limits.h:13:25: warning: excess elements in struct initializer

      13 | #define ULONG_MAX       (~0UL)
         |                         ^
   kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
     161 |                 .buf_max = ULONG_MAX,
         |                            ^~~~~~~~~
   include/vdso/limits.h:13:25: note: (near initialization for 'kbuf')
      13 | #define ULONG_MAX       (~0UL)
         |                         ^

kernel/crash_dump_dm_crypt.c:161:28: note: in expansion of macro 'ULONG_MAX'
161 | .buf_max = ULONG_MAX,
| ^~~~~~~~~

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:162:18: error: 'struct kexec_buf' has no member named 'top_down'

162 | .top_down = false,
| ^~~~~~~~
kernel/crash_dump_dm_crypt.c:162:29: warning: excess elements in struct initializer
162 | .top_down = false,
| ^~~~~
kernel/crash_dump_dm_crypt.c:162:29: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:163:18: error: 'struct kexec_buf' has no member named 'random'

163 | .random = true,
| ^~~~~~
kernel/crash_dump_dm_crypt.c:163:27: warning: excess elements in struct initializer
163 | .random = true,
| ^~~~
kernel/crash_dump_dm_crypt.c:163:27: note: (near initialization for 'kbuf')

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:158:26: error: storage size of 'kbuf' isn't known

     158 |         struct kexec_buf kbuf = {
         |                          ^~~~
Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:187:20: error: 'KEXEC_BUF_MEM_UNKNOWN' undeclared (first use in this function)

     187 |         kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
         |                    ^~~~~~~~~~~~~~~~~~~~~

kernel/crash_dump_dm_crypt.c:187:20: note: each undeclared identifier is reported only once for each function it appears in

Quoted 1 line
Quoted 1 line

kernel/crash_dump_dm_crypt.c:188:13: error: implicit declaration of function 'kexec_add_buffer' [-Werror=implicit-function-declaration]

     188 |         r = kexec_add_buffer(&kbuf);
         |             ^~~~~~~~~~~~~~~~
   kernel/crash_dump_dm_crypt.c:158:26: warning: unused variable 'kbuf' [-Wunused-variable]
     158 |         struct kexec_buf kbuf = {
         |                          ^~~~

cc1: some warnings being treated as errors

vim +/kbuf +158 kernel/crash_dump_dm_crypt.c

   155	
   156	int crash_load_dm_crypt_keys(struct kimage *image)
   157	{
Quoted 6 lines

158 struct kexec_buf kbuf = {
159 .image = image,
160 .buf_min = 0,
161 .buf_max = ULONG_MAX,
162 .top_down = false,
163 .random = true,

   164		};
   165	
   166		int r;
   167	
   168		if (state == FRESH)
   169			return 0;
   170	
   171		if (key_count != keys_header->key_count) {
   172			pr_err("Only record %u keys (%u in total)\n", key_count,
   173			       keys_header->key_count);
   174			return -EINVAL;
   175		}
   176	
   177		image->dm_crypt_keys_addr = 0;
   178		r = build_keys_header();
   179		if (r)
   180			return r;
   181	
   182		kbuf.buffer = keys_header;
   183		kbuf.bufsz = keys_header_size;
   184	
   185		kbuf.memsz = kbuf.bufsz;
   186		kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
Quoted 2 lines

187 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
188 r = kexec_add_buffer(&kbuf);

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

Signature · 2 lines
Best regards,
Coiby
author
#21 lore raw

PATCH v4 4/7 crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging

In reply to Coiby Xu

When there is CPU/memory hot-plugging, the kdump kernel image and initrd will be reloaded. The user space can write the "reuse" command to /sys/kernel/crash_dm_crypt_key so the stored keys can be re-saved again.

Note currently only x86 (commit ea53ad9cf73b ("x86/crash: add x86 crash hotplug support")) and ppc (WIP) supports the new infrastructure (commit 247262756121 ("crash: add generic infrastructure for crash hotplug support")). If the new infrastructure get extended to all arches, this patch can be dropped.

---
 kernel/crash_dump_dm_crypt.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
kernel/crash_dump_dm_crypt.c +26 -4
index 89fec768fba8..b4dc881cc867 100644
@@ -10,12 +10,13 @@
 // The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
 #define KEY_DESC_LEN 48
 
-static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded", "reuse"};
 static enum STATE_ENUM {
 	FRESH = 0,
 	INITIALIZED,
 	RECORDED,
 	LOADED,
+	REUSE,
 } state;
 
 static unsigned int key_count;
@@ -90,12 +91,31 @@ static int record_key_desc(const char *buf, struct dm_crypt_key *dm_key)
 	return 0;
 }
 
+static void get_keys_from_kdump_reserved_memory(void)
+{
+	struct keys_header *keys_header_loaded;
+
+	arch_kexec_unprotect_crashkres();
+
+	keys_header_loaded = kmap_local_page(pfn_to_page(
+		kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT));
+
+	memcpy(keys_header, keys_header_loaded, keys_header_size);
+	kunmap_local(keys_header_loaded);
+	state = RECORDED;
+}
+
 static int process_cmd(const char *buf, size_t count)
 {
 	if (strncmp(buf, "init ", 5) == 0)
 		return init(buf);
 	else if (strncmp(buf, "record ", 7) == 0)
 		return record_key_desc(buf, &keys_header->keys[key_count]);
+	else if (!strcmp(buf, "reuse")) {
+		state = REUSE;
+		get_keys_from_kdump_reserved_memory();
+		return 0;
+	}
 
 	return -EINVAL;
 }
@@ -175,9 +195,11 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	}
 
 	image->dm_crypt_keys_addr = 0;
-	r = build_keys_header();
-	if (r)
-		return r;
+	if (state != REUSE) {
+		r = build_keys_header();
+		if (r)
+			return r;
+	}
 
 	kbuf.buffer = keys_header;
 	kbuf.bufsz = keys_header_size;
Signature · 1 line
2.45.0
#22 lore raw

Re: [PATCH v4 4/7] crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging

Quoted 9 lines · Coiby Xu

When there is CPU/memory hot-plugging, the kdump kernel image and initrd will be reloaded. The user space can write the "reuse" command to /sys/kernel/crash_dm_crypt_key so the stored keys can be re-saved again.

Note currently only x86 (commit ea53ad9cf73b ("x86/crash: add x86 crash hotplug support")) and ppc (WIP) supports the new infrastructure (commit 247262756121 ("crash: add generic infrastructure for crash hotplug support")). If the new infrastructure get extended to all arches, this patch can be dropped.

I am confused, what is the new infrastructure? And why this patch can be dropped if 'the new infrastructure' is extended to all arches.

Quoted 58 lines
---
 kernel/crash_dump_dm_crypt.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
kernel/crash_dump_dm_crypt.c +21 -1
index 89fec768fba8..b4dc881cc867 100644
@@ -10,12 +10,13 @@
 // The key scription has the format: cryptsetup:UUID 11+36+1(NULL)=48
 #define KEY_DESC_LEN 48
 
-static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded"};
+static char *STATE_STR[] = {"fresh", "initialized", "recorded", "loaded", "reuse"};
 static enum STATE_ENUM {
 	FRESH = 0,
 	INITIALIZED,
 	RECORDED,
 	LOADED,
+	REUSE,
 } state;
 
 static unsigned int key_count;
@@ -90,12 +91,31 @@ static int record_key_desc(const char *buf, struct dm_crypt_key *dm_key)
 	return 0;
 }
 
+static void get_keys_from_kdump_reserved_memory(void)
+{
+	struct keys_header *keys_header_loaded;
+
+	arch_kexec_unprotect_crashkres();
+
+	keys_header_loaded = kmap_local_page(pfn_to_page(
+		kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT));
+
+	memcpy(keys_header, keys_header_loaded, keys_header_size);
+	kunmap_local(keys_header_loaded);
+	state = RECORDED;
+}
+
 static int process_cmd(const char *buf, size_t count)
 {
 	if (strncmp(buf, "init ", 5) == 0)
 		return init(buf);
 	else if (strncmp(buf, "record ", 7) == 0)
 		return record_key_desc(buf, &keys_header->keys[key_count]);
+	else if (!strcmp(buf, "reuse")) {
+		state = REUSE;
+		get_keys_from_kdump_reserved_memory();
+		return 0;
+	}
 
 	return -EINVAL;
 }

kernel/crash_dump_dm_crypt.c int crash_load_dm_crypt_keys(struct kimage *image) · line 201

Quoted 10 lines
kernel/crash_dump_dm_crypt.c +4 -3
@@ -175,9 +195,11 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	}
 
 	image->dm_crypt_keys_addr = 0;
-	r = build_keys_header();
-	if (r)
-		return r;
+	if (state != REUSE) {
+		r = build_keys_header();
+		if (r)
+			return r;

Is the logic here wrong? Isn't it we return when it's REUSE. If not REUSE, we need build_keys_header(), then add buffer?

Quoted 7 lines
kernel/crash_dump_dm_crypt.c +1 -0
+	}
 
 	kbuf.buffer = keys_header;
 	kbuf.bufsz = keys_header_size;
Signature · 1 line
2.45.0
author
#23 lore raw

PATCH v4 5/7 crash_dump: retrieve dm crypt keys in kdump kernel

In reply to Coiby Xu

Crash kernel will retrieve the dm crypt keys based on the dmcryptkeys command line parameter. When user space writes the key description to /sys/kernel/crash_dm_crypt_key, the crash kernel will save the encryption keys to the user keyring. Then user space e.g. cryptsetup's --volume-key-keyring API can use it to unlock the encrypted device.

---
 include/linux/crash_core.h   |  1 +
 kernel/crash_dump_dm_crypt.c | 99 +++++++++++++++++++++++++++++++++++-
 2 files changed, 99 insertions(+), 1 deletion(-)
include/linux/crash_core.h +1 -0
index ab20829d0bc9..d7308b6e83f4 100644
@@ -38,6 +38,7 @@ static inline void arch_kexec_unprotect_crashkres(void) { }
 int crash_sysfs_dm_crypt_keys_read(char *buf);
 int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
 int crash_load_dm_crypt_keys(struct kimage *image);
+ssize_t dm_crypt_keys_read(char *buf, size_t count, u64 *ppos);
 #else
 static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; }
 #endif
kernel/crash_dump_dm_crypt.c +98 -1

121 lines not on this page · raw message

Signature · 1 line
2.45.0
#24 lore raw

Re: [PATCH v4 5/7] crash_dump: retrieve dm crypt keys in kdump kernel

On 05/23/24 at 01:04pm, Coiby Xu wrote:
......

kernel/crash_dump_dm_crypt.c static struct keys_header { · line 70

Quoted 7 lines
kernel/crash_dump_dm_crypt.c +7 -0
+ssize_t __weak dm_crypt_keys_read(char *buf, size_t count, u64 *ppos)
+{
+	struct kvec kvec = { .iov_base = buf, .iov_len = count };
+	struct iov_iter iter;
+
+	iov_iter_kvec(&iter, READ, &kvec, 1, count);
+	return read_from_oldmem(&iter, count, ppos, false);

Do we need create a x86 specific version to cope with the confidential computing thing, e.g sme/tdx?

Quoted 2 lines
(quoted diff) +2 -0
+}
+
author
#25 lore raw

Re: [PATCH v4 5/7] crash_dump: retrieve dm crypt keys in kdump kernel

Quoted 11 lines · Baoquan He

On 05/23/24 at 01:04pm, Coiby Xu wrote:
......

kernel/crash_dump_dm_crypt.c static struct keys_header { · line 70

Quoted 7 lines
kernel/crash_dump_dm_crypt.c +7 -0
+ssize_t __weak dm_crypt_keys_read(char *buf, size_t count, u64 *ppos)
+{
+	struct kvec kvec = { .iov_base = buf, .iov_len = count };
+	struct iov_iter iter;
+
+	iov_iter_kvec(&iter, READ, &kvec, 1, count);
+	return read_from_oldmem(&iter, count, ppos, false);

Do we need create a x86 specific version to cope with the confidential computing thing, e.g sme/tdx?

Thanks for raising the concern! I'll test sme/tdx and will fix it in v6 if any issue is found.

Signature · 2 lines
Best regards,
Coiby
author
#26 lore raw

PATCH v4 6/7 x86/crash: pass dm crypt keys to kdump kernel

In reply to Coiby Xu

1st kernel will build up the kernel command parameter dmcryptkeys as similar to elfcorehdr to pass the memory address of the stored info of dm crypt key to kdump kernel.

---
 arch/x86/kernel/crash.c           | 15 ++++++++++++++-
 arch/x86/kernel/kexec-bzimage64.c |  7 +++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
arch/x86/kernel/crash.c +14 -1
index f06501445cd9..74b3844ae53c 100644
@@ -266,6 +266,7 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 				 unsigned long long mend)
 {
 	unsigned long start, end;
+	int r;
 
 	cmem->ranges[0].start = mstart;
 	cmem->ranges[0].end = mend;
@@ -274,7 +275,19 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 	/* Exclude elf header region */
 	start = image->elf_load_addr;
 	end = start + image->elf_headers_sz - 1;
-	return crash_exclude_mem_range(cmem, start, end);
+	r = crash_exclude_mem_range(cmem, start, end);
+
+	if (r)
+		return r;
+
+	/* Exclude dm crypt keys region */
+	if (image->dm_crypt_keys_addr) {
+		start = image->dm_crypt_keys_addr;
+		end = start + image->dm_crypt_keys_sz - 1;
+		return crash_exclude_mem_range(cmem, start, end);
+	}
+
+	return r;
 }
 
 /* Prepare memory map for crash dump kernel */
arch/x86/kernel/kexec-bzimage64.c +7 -0
index 68530fad05f7..9c94428927bd 100644
@@ -76,6 +76,10 @@ static int setup_cmdline(struct kimage *image, struct boot_params *params,
 	if (image->type == KEXEC_TYPE_CRASH) {
 		len = sprintf(cmdline_ptr,
 			"elfcorehdr=0x%lx ", image->elf_load_addr);
+
+		if (image->dm_crypt_keys_addr != 0)
+			len += sprintf(cmdline_ptr + len,
+					"dmcryptkeys=0x%lx ", image->dm_crypt_keys_addr);
 	}
 	memcpy(cmdline_ptr + len, cmdline, cmdline_len);
 	cmdline_len += len;
@@ -441,6 +445,9 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		ret = crash_load_segments(image);
 		if (ret)
 			return ERR_PTR(ret);
+		ret = crash_load_dm_crypt_keys(image);
+		if (ret)
+			pr_debug("Either no dm crypt key or error to retrieve the dm crypt key\n");
 	}
 #endif
 
Signature · 1 line
2.45.0
#27 lore raw

Re: [PATCH v4 6/7] x86/crash: pass dm crypt keys to kdump kernel

Quoted 15 lines · Coiby Xu

1st kernel will build up the kernel command parameter dmcryptkeys as similar to elfcorehdr to pass the memory address of the stored info of dm crypt key to kdump kernel.

---
 arch/x86/kernel/crash.c           | 15 ++++++++++++++-
 arch/x86/kernel/kexec-bzimage64.c |  7 +++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
arch/x86/kernel/crash.c +0 -0
index f06501445cd9..74b3844ae53c 100644

arch/x86/kernel/crash.c static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, · line 269

Quoted 4 lines
arch/x86/kernel/crash.c +1 -0
@@ -266,6 +266,7 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 				 unsigned long long mend)
 {
 	unsigned long start, end;
+	int r;
            ~?

r is only to contain the returned value? Then you can call it ret as many do in kernel code.

Quoted 9 lines
arch/x86/kernel/crash.c +1 -1
 	cmem->ranges[0].start = mstart;
 	cmem->ranges[0].end = mend;
@@ -274,7 +275,19 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 	/* Exclude elf header region */
 	start = image->elf_load_addr;
 	end = start + image->elf_headers_sz - 1;
-	return crash_exclude_mem_range(cmem, start, end);
+	r = crash_exclude_mem_range(cmem, start, end);

arch/x86/kernel/crash.c static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, · line 288

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+
+	if (r)
+		return r;
+
+	/* Exclude dm crypt keys region */
+	if (image->dm_crypt_keys_addr) {
+		start = image->dm_crypt_keys_addr;
+		end = start + image->dm_crypt_keys_sz - 1;
+		return crash_exclude_mem_range(cmem, start, end);
+	}

You need adjust the array length of cmem->ranges[], I believe you will cause the array overflow because the keys are randomly set and mostly will be in the middle of crashkernel region.

Quoted 5 lines
arch/x86/kernel/crash.c +2 -0
+
+	return r;
 }
 
 /* Prepare memory map for crash dump kernel */
author Applied
#28 lore raw

Re: [PATCH v4 6/7] x86/crash: pass dm crypt keys to kdump kernel

Quoted 22 lines · Baoquan He
Quoted 15 lines · Coiby Xu

1st kernel will build up the kernel command parameter dmcryptkeys as similar to elfcorehdr to pass the memory address of the stored info of dm crypt key to kdump kernel.

---
 arch/x86/kernel/crash.c           | 15 ++++++++++++++-
 arch/x86/kernel/kexec-bzimage64.c |  7 +++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
arch/x86/kernel/crash.c +0 -0
index f06501445cd9..74b3844ae53c 100644

arch/x86/kernel/crash.c static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, · line 269

Quoted 4 lines
arch/x86/kernel/crash.c +1 -0
@@ -266,6 +266,7 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 				 unsigned long long mend)
 {
 	unsigned long start, end;
+	int r;
           ~?
Quoted 2 lines

r is only to contain the returned value? Then you can call it ret as many do in kernel code.

Applied to v6, thanks for the suggestion!

Quoted 16 lines
Quoted 16 lines
arch/x86/kernel/crash.c +7 -1
 	cmem->ranges[0].start = mstart;
 	cmem->ranges[0].end = mend;
@@ -274,7 +275,19 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
 	/* Exclude elf header region */
 	start = image->elf_load_addr;
 	end = start + image->elf_headers_sz - 1;
-	return crash_exclude_mem_range(cmem, start, end);
+	r = crash_exclude_mem_range(cmem, start, end);
+
+	if (r)
+		return r;
+
+	/* Exclude dm crypt keys region */
+	if (image->dm_crypt_keys_addr) {
Quoted 8 lines

arch/x86/kernel/crash.c static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, · line 288

Quoted 4 lines
arch/x86/kernel/crash.c +4 -0
+		start = image->dm_crypt_keys_addr;
+		end = start + image->dm_crypt_keys_sz - 1;
+		return crash_exclude_mem_range(cmem, start, end);
+	}

You need adjust the array length of cmem->ranges[], I believe you will cause the array overflow because the keys are randomly set and mostly will be in the middle of crashkernel region.

Yes, you are absolutely right. I've fixed in v6.

Signature · 2 lines
Best regards,
Coiby
author
#29 lore raw

PATCH v4 7/7 x86/crash: make the page that stores the dm crypt keys inaccessible

In reply to Coiby Xu

This adds an addition layer of protection for the saved copy of dm crypt key. Trying to access the saved copy will cause page fault.

---
 arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/machine_kexec_64.c +21 -0
index b180d8e497c3..fc0a80f4254e 100644
@@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
 	kexec_mark_range(control, crashk_res.end, protect);
 }
 
+static void kexec_mark_dm_crypt_keys(bool protect)
+{
+	unsigned long start_paddr, end_paddr;
+	unsigned int nr_pages;
+
+	if (kexec_crash_image->dm_crypt_keys_addr) {
+		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
+		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
+		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
+		if (protect)
+			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
+		else
+			__set_memory_prot(
+				(unsigned long)phys_to_virt(start_paddr),
+				nr_pages,
+				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
+	}
+}
+
 void arch_kexec_protect_crashkres(void)
 {
 	kexec_mark_crashkres(true);
+	kexec_mark_dm_crypt_keys(true);
 }
 
 void arch_kexec_unprotect_crashkres(void)
 {
+	kexec_mark_dm_crypt_keys(false);
 	kexec_mark_crashkres(false);
 }
 #endif
Signature · 1 line
2.45.0
#30 lore raw

Re: [PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible

Quoted 30 lines · Coiby Xu

This adds an addition layer of protection for the saved copy of dm crypt key. Trying to access the saved copy will cause page fault.

---
 arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/machine_kexec_64.c +13 -0
index b180d8e497c3..fc0a80f4254e 100644
@@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
 	kexec_mark_range(control, crashk_res.end, protect);
 }
 
+static void kexec_mark_dm_crypt_keys(bool protect)
+{
+	unsigned long start_paddr, end_paddr;
+	unsigned int nr_pages;
+
+	if (kexec_crash_image->dm_crypt_keys_addr) {
+		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
+		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
+		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
+		if (protect)
+			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
+		else
+			__set_memory_prot(

arch/x86/kernel/machine_kexec_64.c static void kexec_mark_crashkres(bool protect) · line 570

Quoted 10 lines
arch/x86/kernel/machine_kexec_64.c +7 -0
+				(unsigned long)phys_to_virt(start_paddr),
+				nr_pages,
+				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
+	}
+}
+
 void arch_kexec_protect_crashkres(void)
 {
 	kexec_mark_crashkres(true);
+	kexec_mark_dm_crypt_keys(true);

Isn't crashkernel region covering crypt keys' storing region? Do we need mark it again specifically? Not sure if I miss anything.

Quoted 11 lines
arch/x86/kernel/machine_kexec_64.c +1 -0
 }
 
 void arch_kexec_unprotect_crashkres(void)
 {
+	kexec_mark_dm_crypt_keys(false);
 	kexec_mark_crashkres(false);
 }
 #endif
Signature · 1 line
2.45.0
author
#31 lore raw

Re: [PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible

Quoted 36 lines · Baoquan He
Quoted 35 lines · Coiby Xu

This adds an addition layer of protection for the saved copy of dm crypt key. Trying to access the saved copy will cause page fault.

---
 arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/machine_kexec_64.c +18 -0
index b180d8e497c3..fc0a80f4254e 100644
@@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
 	kexec_mark_range(control, crashk_res.end, protect);
 }
 
+static void kexec_mark_dm_crypt_keys(bool protect)
+{
+	unsigned long start_paddr, end_paddr;
+	unsigned int nr_pages;
+
+	if (kexec_crash_image->dm_crypt_keys_addr) {
+		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
+		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
+		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
+		if (protect)
+			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
+		else
+			__set_memory_prot(
+				(unsigned long)phys_to_virt(start_paddr),
+				nr_pages,
+				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
+	}
+}
Quoted 8 lines

arch/x86/kernel/machine_kexec_64.c static void kexec_mark_crashkres(bool protect) · line 570

Quoted 5 lines
arch/x86/kernel/machine_kexec_64.c +2 -0
+
 void arch_kexec_protect_crashkres(void)
 {
 	kexec_mark_crashkres(true);
+	kexec_mark_dm_crypt_keys(true);

Isn't crashkernel region covering crypt keys' storing region? Do we need mark it again specifically? Not sure if I miss anything.

kexec_mark_crashkres only makes the page read-only whereas kexec_mark_dm_crypt_keys makes the memory inaccessible. I've added a comment for this function in v5 and hopefully no one will be confused by it.

Signature · 2 lines
Best regards,
Coiby
#32 lore raw
In reply to Coiby Xu

Hi Coiby,

Quoted 6 lines · Coiby Xu

LUKS is the standard for Linux disk encryption. Many users choose LUKS and in some use cases like Confidential VM it's mandated. With kdump enabled, when the 1st kernel crashes, the system could boot into the kdump/crash kernel and dump the memory image i.e. /proc/vmcore to a specified target. Currently, when dumping vmcore to a LUKS encrypted device, there are two problems,

I am done with this round of reviewing. The overall approach looks good to me, while there are places to improve or fix. I have added comment on all things I am concerned about, please check. Thanks for the effort.

By the way, do you get confirmation on the solution from encryption/keys developer of redhat internally or upstream? With my understanding, it looks good. It may need their confirmation or approval in some ways.

Thanks
Baoquan

author
#33 lore raw

Hi Baoquan,

Quoted 10 lines · Baoquan He

Hi Coiby,

Quoted 6 lines · Coiby Xu

LUKS is the standard for Linux disk encryption. Many users choose LUKS and in some use cases like Confidential VM it's mandated. With kdump enabled, when the 1st kernel crashes, the system could boot into the kdump/crash kernel and dump the memory image i.e. /proc/vmcore to a specified target. Currently, when dumping vmcore to a LUKS encrypted device, there are two problems,

Quoted 3 lines

I am done with this round of reviewing. The overall approach looks good to me, while there are places to improve or fix. I have added comment on all things I am concerned about, please check. Thanks for the effort.

Thanks for taking time on thoroughly reviewing the patches and suggesting various improvements! I believe all comments have been addressed now except for testing sme/tdx:)

Quoted 4 lines

By the way, do you get confirmation on the solution from encryption/keys developer of redhat internally or upstream? With my understanding, it looks good. It may need their confirmation or approval in some ways.

Yes, actually cryptsetup's maintainer Milan pointed me to libcryptsetup's new API and suggested me to reach out to Ondrej for help. And Ondrej has already finished the major work in upstream cryptsetup and systemd. But it will be good for them to have further confirmation and I'll ask for their help. Thanks for the reminder!

Quoted 4 lines

Thanks
Baoquan

Signature · 2 lines
Best regards,
Coiby