Skip to contentSkip to conversation

Bug fixes and enhancements for kdump LUKS support

PATCH Superseded Coiby Xu 2 participants 29 messages Jul 29 – Aug 19

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

v3 cover Bug fixes and enhancements for kdump LUKS support

Hi all,

This patch set brings some stability fixes and improvements to the recently introduced CONFIG_CRASH_DM_CRYPT feature [1][2] (which is to support LUKS-encrypted device dump target),
1. address several memory management issues
2. fix race conditions like configfs writing happens concurrently with kexec_file_load syscall
3. improve configfs handling
4. update documentation

To help kernel bots test the patch set, a dependent patch [3] already applied to the kexec-fixes branch of liveupdate/linux.git tree [4] is also included.

v3
- Changes suggested by Sourabh
- more robust check on crash hotplug support
- use configfs mutex when writing to crash_dm_crypt_keys/restore
- warn the user when keys have been restored
- Drop patch "crash_dump: Release reference to a keyring at correct time" since Guangshuo's version [3] is a more complete fix
- Adjust layout of documentation for crash hotplug support
- Check return codes of the function that restore_dm_crypt_keys_to_thread_keyring calls

v2
- A different way to address potential double and UAF issues
- Address several issues [5] scrutinized by Sourabh based on Sashiko's review feedback [6] on v1 patch
- Other improvements like logging fix and doc updates

[1] http://lists.infradead.org/pipermail/kexec/2025-February/031850.html
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]
[4] https://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git/log/?h=kexec-fixes
[5] https://lore.kernel.org/all/[email protected]/
[6] https://sashiko.dev/#/patchset/20260403100126.1468200-1-coxu%40redhat.com

Coiby Xu (9):
crash_dump: Fix potential double free and UAF of keys_header
crash_dump: Disallow writing to dm-crypt configfs during
kexec_file_load syscall
crash_dump: Read the number of dm-crypt keys from reserved memory
crash_dump: Free temporary dm-crypt keys_header buffer in kdump kernel
crash_dump: Only use kexec_dprintk during the kexec_file_load syscall
crash_dump: Improve readability of config_keys_restore_store
crash_dump: Check the function return codes in
restore_dm_crypt_keys_to_thread_keyring
crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash
hotplug supported
Documentation: kdump: Add arm64 and ppc64le to encrypted dump target
support list

Guangshuo Li (1):
crash_dump: release keyring reference at the correct time

 Documentation/admin-guide/kdump/kdump.rst |  20 ++-
 include/linux/kexec.h                     |   6 +
 kernel/crash_dump_dm_crypt.c              | 189 ++++++++++++++++------
 kernel/kexec_file.c                       |   2 +
 4 files changed, 163 insertions(+), 54 deletions(-)

base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v3 1/10 crash_dump: release keyring reference at the correct time

From: Guangshuo Li <[email protected]>

restore_dm_crypt_keys_to_thread_keyring() gets a reference to the user keyring before restoring the saved dm-crypt keys.

The same keyring reference is then passed to add_key_to_keyring() for each saved key, but add_key_to_keyring() drops that reference on every call. This is only balanced when exactly one key is restored. With multiple keys, the keyring reference is dropped too many times and may trigger a refcount underflow or use-after-free.

When more than five keys are restored, a refcount underflow/use-after-free warning can be triggered.

The early error paths after lookup_user_key() also return without dropping the keyring reference.

Keep ownership of the keyring reference in restore_dm_crypt_keys_to_thread_keyring(), drop it once on all exit paths, and make add_key_to_keyring() only use the reference without consuming it.

---
 kernel/crash_dump_dm_crypt.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
kernel/crash_dump_dm_crypt.c +10 -5
index cb875ddb6ba6..c685497cd470 100644
@@ -81,7 +81,6 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
 		kexec_dprintk("Error when adding key");
 	}
 
-	key_ref_put(keyring_ref);
 	return r;
 }
 
@@ -104,6 +103,7 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 	struct dm_crypt_key *key;
 	size_t keys_header_size;
 	key_ref_t keyring_ref;
+	int ret = 0;
 	u64 addr;
 
 	/* find the target keyring (which must be writable) */
@@ -118,7 +118,8 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 	dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
 	if (key_count > KEY_NUM_MAX) {
 		kexec_dprintk("Failed to read the number of dm-crypt keys\n");
-		return -1;
+		ret = -1;
+		goto out;
 	}
 
 	kexec_dprintk("There are %u keys\n", key_count);
@@ -126,8 +127,10 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 
 	keys_header_size = get_keys_header_size(key_count);
 	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
-	if (!keys_header)
-		return -ENOMEM;
+	if (!keys_header) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
 
@@ -137,7 +140,9 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 		add_key_to_keyring(key, keyring_ref);
 	}
 
-	return 0;
+out:
+	key_ref_put(keyring_ref);
+	return ret;
 }
 
 static int read_key_from_user_keyring(struct dm_crypt_key *dm_key)
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH v3 2/10 crash_dump: Fix potential double free and UAF of keys_header

In reply to Coiby Xu

If kexec_add_buffer somehow fails, keys_header will be freed. Depending on /sys/kernel/config/crash_dm_crypt_key/reuse, it will lead to the following two problems if the kexec_file_load syscall is called again,
1. Double free of keys_header if reuse=false
2. UAF of keys_header if reuse=true

To address these problems and also make it easier to reason about the code, keep two invariants,
1. keys_header will always be freed at the end of kexec_file_load syscall except during kdump image unloading for CPU/memory hot-plugging support
2. There will always be valid keys_header if reuse=true

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")
Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
---
 include/linux/kexec.h        |  6 ++++
 kernel/crash_dump_dm_crypt.c | 66 ++++++++++++++++++++++++++----------
 kernel/kexec_file.c          |  2 ++
 3 files changed, 56 insertions(+), 18 deletions(-)
include/linux/kexec.h +6 -0
index 8a22bc9b8c6c..91256d7ff434 100644
@@ -552,6 +552,12 @@ void set_kexec_sig_enforced(void);
 static inline void set_kexec_sig_enforced(void) {}
 #endif
 
+#ifdef CONFIG_CRASH_DM_CRYPT
+void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image);
+#else
+static inline void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image) {}
+#endif
+
 #endif /* !defined(__ASSEBMLY__) */
 
 #endif /* LINUX_KEXEC_H */
kernel/crash_dump_dm_crypt.c +48 -18

132 lines not on this page · raw message

kernel/kexec_file.c +2 -0
index 2bfbb2d144e6..0421f1e89791 100644
@@ -139,6 +139,8 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 	kfree(image->image_loader_data);
 	image->image_loader_data = NULL;
 
+	kexec_file_post_load_cleanup_dm_crypt(image);
+
 	kexec_file_dbg_print = false;
 }
 
Signature · 1 line
2.55.0
author
#3 lore raw

PATCH v3 3/10 crash_dump: Disallow writing to dm-crypt configfs during kexec_file_load syscall

In reply to Coiby Xu

If writing to the configfs group happens concurrently during kexec_file_load syscall, it may lead to the following issues,
- buffer overflow if dm-crypt keys are added after allocation
- stale total_keys if dm-crypt keys are removed during iteration
- keys_header will not be freed if config/crash_dm_crypt_key/reuse is set true

So hold config_keys_subsys.su_mutex for the entire sequence during the kexec_file_load syscall to ensure a consistent snapshot.

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")
---
 kernel/crash_dump_dm_crypt.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/crash_dump_dm_crypt.c +21 -2
index 4335b6cb1fc4..d2e66c6fe6f3 100644
@@ -293,6 +293,7 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
 static ssize_t config_keys_reuse_store(struct config_item *item,
 					   const char *page, size_t count)
 {
+	struct mutex *lock;
 	bool val;
 	int r;
 
@@ -302,8 +303,12 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 		return -EINVAL;
 	}
 
+	lock = &to_config_group(item)->cg_subsys->su_mutex;
+	mutex_lock(lock);
+
+	r = -EINVAL;
 	if (kstrtobool(page, &val) || !val)
-		return -EINVAL;
+		goto unlock;
 
 	if (is_dm_key_reused) {
 		pr_info("Already got dm-crypt keys, please continue with kexec_file_load syscall\n");
@@ -311,11 +316,15 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 		r = get_keys_from_kdump_reserved_memory();
 		if (r) {
 			pr_warn("Failed to get dm-crypt keys from reserved memory\n");
-			return r;
+			goto unlock;
 		}
 		is_dm_key_reused = true;
 	}
 
+	r = count;
+
+unlock:
+	mutex_unlock(lock);
 	return count;
 }
 
@@ -421,6 +430,8 @@ static int build_keys_header(void)
 	return 0;
 }
 
+static bool mutex_acquired;
+
 int crash_load_dm_crypt_keys(struct kimage *image)
 {
 	struct kexec_buf kbuf = {
@@ -432,6 +443,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	};
 	int r = 0;
 
+	mutex_lock(&config_keys_subsys.su_mutex);
+	mutex_acquired = true;
+
 	if (key_count <= 0) {
 		kexec_dprintk("No dm-crypt keys\n");
 		return 0;
@@ -481,6 +495,11 @@ void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
 		kfree_sensitive(keys_header);
 		keys_header = NULL;
 	}
+
+	if (mutex_acquired) {
+		mutex_unlock(&config_keys_subsys.su_mutex);
+		mutex_acquired = false;
+	}
 }
 
 static int __init configfs_dmcrypt_keys_init(void)
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v3 03/10] crash_dump: Disallow writing to dm-crypt configfs during kexec_file_load syscall

Quoted 30 lines · Coiby Xu

If writing to the configfs group happens concurrently during kexec_file_load syscall, it may lead to the following issues,
- buffer overflow if dm-crypt keys are added after allocation
- stale total_keys if dm-crypt keys are removed during iteration
- keys_header will not be freed if config/crash_dm_crypt_key/reuse is set true

So hold config_keys_subsys.su_mutex for the entire sequence during the kexec_file_load syscall to ensure a consistent snapshot.

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")
---
  kernel/crash_dump_dm_crypt.c | 23 +++++++++++++++++++++--
  1 file changed, 21 insertions(+), 2 deletions(-)
kernel/crash_dump_dm_crypt.c +1 -0
index 4335b6cb1fc4..d2e66c6fe6f3 100644
@@ -293,6 +293,7 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
  static ssize_t config_keys_reuse_store(struct config_item *item,
  					   const char *page, size_t count)
  {
+	struct mutex *lock;
  	bool val;
  	int r;
 

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct config_item *item, · line 307

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +2 -0
@@ -302,8 +303,12 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
  		return -EINVAL;
  	}
 
+	lock = &to_config_group(item)->cg_subsys->su_mutex;
+	mutex_lock(lock);

Is this lock only protecting against races between key reuse and kexec_file_load(), or does it also handle the case where a new key is added during key reuse or kexec_file_load() is running?

If it is only intended to protect key reuse versus kexec_file_load(), why can't we use the kexec lock instead?

The reason I'm asking is that, in upcoming patches, the key reuse path accesses kexec_crash_image properties and the crash reserved region directly. Doing so without taking the kexec lock (using kexec_trylock()) could lead to race conditions.

- Sourabh Jain

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct config_item *item, · line 311

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +3 -1
+
+	r = -EINVAL;
  	if (kstrtobool(page, &val) || !val)
-		return -EINVAL;
+		goto unlock;

The jump above skips setting count, causing the function to return count instead of -EINVAL. Is this really intended?

Quoted 51 lines
kernel/crash_dump_dm_crypt.c +15 -1
  	if (is_dm_key_reused) {
  		pr_info("Already got dm-crypt keys, please continue with kexec_file_load syscall\n");
@@ -311,11 +316,15 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
  		r = get_keys_from_kdump_reserved_memory();
  		if (r) {
  			pr_warn("Failed to get dm-crypt keys from reserved memory\n");
-			return r;
+			goto unlock;
  		}
  		is_dm_key_reused = true;
  	}
 
+	r = count;
+
+unlock:
+	mutex_unlock(lock);
  	return count;
  }
 
@@ -421,6 +430,8 @@ static int build_keys_header(void)
  	return 0;
  }
 
+static bool mutex_acquired;
+
  int crash_load_dm_crypt_keys(struct kimage *image)
  {
  	struct kexec_buf kbuf = {
@@ -432,6 +443,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
  	};
  	int r = 0;
 
+	mutex_lock(&config_keys_subsys.su_mutex);
+	mutex_acquired = true;
+
  	if (key_count <= 0) {
  		kexec_dprintk("No dm-crypt keys\n");
  		return 0;
@@ -481,6 +495,11 @@ void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
  		kfree_sensitive(keys_header);
  		keys_header = NULL;
  	}
+
+	if (mutex_acquired) {
+		mutex_unlock(&config_keys_subsys.su_mutex);
+		mutex_acquired = false;
+	}
  }
 
  static int __init configfs_dmcrypt_keys_init(void)
author
#5 lore raw

Re: [PATCH v3 03/10] crash_dump: Disallow writing to dm-crypt configfs during kexec_file_load syscall

Quoted 30 lines · Sourabh Jain
Quoted 27 lines · Coiby Xu

If writing to the configfs group happens concurrently during kexec_file_load syscall, it may lead to the following issues,
- buffer overflow if dm-crypt keys are added after allocation
- stale total_keys if dm-crypt keys are removed during iteration
- keys_header will not be freed if config/crash_dm_crypt_key/reuse is set true

So hold config_keys_subsys.su_mutex for the entire sequence during the kexec_file_load syscall to ensure a consistent snapshot.

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")
---
 kernel/crash_dump_dm_crypt.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/crash_dump_dm_crypt.c +1 -0
index 4335b6cb1fc4..d2e66c6fe6f3 100644
@@ -293,6 +293,7 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
 static ssize_t config_keys_reuse_store(struct config_item *item,
 					   const char *page, size_t count)
 {
+	struct mutex *lock;
 	bool val;
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct config_item *item, · line 306

Quoted 6 lines
kernel/crash_dump_dm_crypt.c +2 -0
 	int r;
@@ -302,8 +303,12 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 		return -EINVAL;
 	}
+	lock = &to_config_group(item)->cg_subsys->su_mutex;
+	mutex_lock(lock);

Is this lock only protecting against races between key reuse and kexec_file_load(),

The lock here is to protect against races between key reuse and kexec_file_load.

Quoted 2 lines

or does it also handle the case where a new key is added during key reuse or kexec_file_load() is running?

For the cases where a key is added/deleted, configfs will automatically take care of them because it will acquire mutex lock automatically.

Quoted 7 lines

If it is only intended to protect key reuse versus kexec_file_load(), why can't we use the kexec lock instead?

The reason I'm asking is that, in upcoming patches, the key reuse path accesses kexec_crash_image properties and the crash reserved region directly. Doing so without taking the kexec lock (using kexec_trylock()) could lead to race conditions.

After comparing the kexec lock approach with the configfs mutex lock approach, I think the latter is a simpler solution because
1. the kexec lock is non-blocking and we have to repeatedly try until the lock get acquired. So it means user space has to make changes as well.

2. configfs already acquires the mutex lock automatically for creating/deleting configfs items. So if we use configfs mutex lock, it means one less place to use the lock.

In config_keys_reuse_store, kexec_crash_image will be checked before accessing its properties and the crash reserved region. Can you elaborate on what the race conditions are? Will acquiring the lock before accessing kexec_crash_image properties and the crash reserved region help protect against these races?

In theory, the kexec lock can be a more robust approach. But considering only root can write to the crash dm-crypt keys configfs and load kdump image, I'm not sure it's necessary to adopt a bit more complex solution.

Quoted 10 lines

- Sourabh Jain

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct config_item *item, · line 311

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +3 -1
+
+	r = -EINVAL;
 	if (kstrtobool(page, &val) || !val)
-		return -EINVAL;
+		goto unlock;

The jump above skips setting count, causing the function to return count instead of -EINVAL. Is this really intended?

Thanks for catching this issue! In the end of the function, r instead of count should be returned.

Quoted 47 lines
Quoted 46 lines
kernel/crash_dump_dm_crypt.c +15 -1
 	if (is_dm_key_reused) {
 		pr_info("Already got dm-crypt keys, please continue with kexec_file_load syscall\n");
@@ -311,11 +316,15 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 		r = get_keys_from_kdump_reserved_memory();
 		if (r) {
 			pr_warn("Failed to get dm-crypt keys from reserved memory\n");
-			return r;
+			goto unlock;
 		}
 		is_dm_key_reused = true;
 	}
+	r = count;
+
+unlock:
+	mutex_unlock(lock);
 	return count;
 }
@@ -421,6 +430,8 @@ static int build_keys_header(void)
 	return 0;
 }
+static bool mutex_acquired;
+
 int crash_load_dm_crypt_keys(struct kimage *image)
 {
 	struct kexec_buf kbuf = {
@@ -432,6 +443,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	};
 	int r = 0;
+	mutex_lock(&config_keys_subsys.su_mutex);
+	mutex_acquired = true;
+
 	if (key_count <= 0) {
 		kexec_dprintk("No dm-crypt keys\n");
 		return 0;
@@ -481,6 +495,11 @@ void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
 		kfree_sensitive(keys_header);
 		keys_header = NULL;
 	}
+
+	if (mutex_acquired) {
+		mutex_unlock(&config_keys_subsys.su_mutex);
+		mutex_acquired = false;
+	}
 }
 static int __init configfs_dmcrypt_keys_init(void)
Signature · 2 lines
Best regards,
Coiby
#6 lore raw

Re: [PATCH v3 03/10] crash_dump: Disallow writing to dm-crypt configfs during kexec_file_load syscall

Hello Coiby,

Quoted 75 lines · Coiby Xu
Quoted 32 lines · Sourabh Jain
Quoted 29 lines · Coiby Xu

If writing to the configfs group happens concurrently during kexec_file_load syscall, it may lead to the following issues,
  - buffer overflow if dm-crypt keys are added after allocation
  - stale total_keys if dm-crypt keys are removed during iteration
  - keys_header will not be freed if config/crash_dm_crypt_key/reuse is set true

So hold config_keys_subsys.su_mutex for the entire sequence during the kexec_file_load syscall to ensure a consistent snapshot.

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")

---
 kernel/crash_dump_dm_crypt.c | 23 +++++++++++++++++++++--

 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/crash_dump_dm_crypt.c +1 -0
index 4335b6cb1fc4..d2e66c6fe6f3 100644
@@ -293,6 +293,7 @@ static ssize_t config_keys_reuse_show(struct 
 config_item *item, char *page)
  static ssize_t config_keys_reuse_store(struct config_item *item,
                         const char *page, size_t count)
  {
+    struct mutex *lock;
      bool val;
      int r;
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct · line 307

Quoted 6 lines
kernel/crash_dump_dm_crypt.c +2 -0
@@ -302,8 +303,12 @@ static ssize_t config_keys_reuse_store(struct 
 config_item *item,
          return -EINVAL;
      }
+    lock = &to_config_group(item)->cg_subsys->su_mutex;
+    mutex_lock(lock);

Is this lock only protecting against races between key reuse and kexec_file_load(),

The lock here is to protect against races between key reuse and kexec_file_load.

Quoted 2 lines

or does it also handle the case where a new key is added during key reuse or kexec_file_load() is running?

For the cases where a key is added/deleted, configfs will automatically take care of them because it will acquire mutex lock automatically.

Quoted 7 lines

If it is only intended to protect key reuse versus kexec_file_load(), why can't we use the kexec lock instead?

The reason I'm asking is that, in upcoming patches, the key reuse path accesses kexec_crash_image properties and the crash reserved region directly. Doing so without taking the kexec lock (using kexec_trylock()) could lead to race conditions.

After comparing the kexec lock approach with the configfs mutex lock approach, I think the latter is a simpler solution because
1. the kexec lock is non-blocking and we have to repeatedly try until the lock get acquired. So it means user space has to make changes as well.

2. configfs already acquires the mutex lock automatically for creating/deleting configfs items. So if we use configfs mutex lock, it means one less place to use the lock.

In config_keys_reuse_store, kexec_crash_image will be checked before accessing its properties and the crash reserved region. Can you elaborate on what the race conditions are? Will acquiring the lock before accessing kexec_crash_image properties and the crash reserved region help protect against these races?

Quoted 3 lines

In theory, the kexec lock can be a more robust approach. But considering only root can write to the crash dm-crypt keys configfs and load kdump image, I'm not sure it's necessary to adopt a bit more complex solution.

The reuse function accesses the kexec crash image properties and crashkernel memory without taking the kexec lock. This could lead to race conditions or other problems.

Since we need to take the kexec lock anyway, my suggestion is: can we use the kexec lock instead of cg_subsys->su_mutex if the purpose of the mutex is only to synchronize reuse with kexec_file_load?

As we know, kexec_file_load already runs under the kexec lock. So using the same lock should provide the required synchronization.

- Sourabh Jain

Quoted 66 lines
Quoted 4 lines

- Sourabh Jain

Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_store(struct config_item *item, · line 311

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +3 -1
+
+    r = -EINVAL;
      if (kstrtobool(page, &val) || !val)
-        return -EINVAL;
+        goto unlock;

The jump above skips setting count, causing the function to return count instead of -EINVAL. Is this really intended?

Thanks for catching this issue! In the end of the function, r instead of count should be returned.

Quoted 49 lines
Quoted 48 lines
kernel/crash_dump_dm_crypt.c +15 -1
      if (is_dm_key_reused) {
          pr_info("Already got dm-crypt keys, please continue with kexec_file_load syscall\n");
@@ -311,11 +316,15 @@ static ssize_t config_keys_reuse_store(struct 
 config_item *item,
          r = get_keys_from_kdump_reserved_memory();
          if (r) {
              pr_warn("Failed to get dm-crypt keys from reserved memory\n");
-            return r;
+            goto unlock;
          }
          is_dm_key_reused = true;
      }
+    r = count;
+
+unlock:
+    mutex_unlock(lock);
      return count;
  }
@@ -421,6 +430,8 @@ static int build_keys_header(void)
      return 0;
  }
+static bool mutex_acquired;
+
  int crash_load_dm_crypt_keys(struct kimage *image)
  {
      struct kexec_buf kbuf = {
@@ -432,6 +443,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
      };
      int r = 0;
+    mutex_lock(&config_keys_subsys.su_mutex);
+    mutex_acquired = true;
+
      if (key_count <= 0) {
          kexec_dprintk("No dm-crypt keys\n");
          return 0;
@@ -481,6 +495,11 @@ void 
 kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
          kfree_sensitive(keys_header);
          keys_header = NULL;
      }
+
+    if (mutex_acquired) {
+        mutex_unlock(&config_keys_subsys.su_mutex);
+        mutex_acquired = false;
+    }
  }
  static int __init configfs_dmcrypt_keys_init(void)
author
#7 lore raw

Re: [PATCH v3 03/10] crash_dump: Disallow writing to dm-crypt configfs during kexec_file_load syscall

Quoted 86 lines · Sourabh Jain

Hello Coiby,

Quoted 75 lines · Coiby Xu
Quoted 32 lines · Sourabh Jain
Quoted 29 lines · Coiby Xu

If writing to the configfs group happens concurrently during kexec_file_load syscall, it may lead to the following issues,
  - buffer overflow if dm-crypt keys are added after allocation
  - stale total_keys if dm-crypt keys are removed during iteration
  - keys_header will not be freed if config/crash_dm_crypt_key/reuse is set true

So hold config_keys_subsys.su_mutex for the entire sequence during the kexec_file_load syscall to ensure a consistent snapshot.

Fixes 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")

---
 kernel/crash_dump_dm_crypt.c | 23 +++++++++++++++++++++--

 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/crash_dump_dm_crypt.c +1 -0
index 4335b6cb1fc4..d2e66c6fe6f3 100644
@@ -293,6 +293,7 @@ static ssize_t config_keys_reuse_show(struct 
 config_item *item, char *page)
  static ssize_t config_keys_reuse_store(struct config_item *item,
                         const char *page, size_t count)
  {
+    struct mutex *lock;
      bool val;
      int r;
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t · line 307

Quoted 6 lines
kernel/crash_dump_dm_crypt.c +2 -0
@@ -302,8 +303,12 @@ static ssize_t 
 config_keys_reuse_store(struct config_item *item,
          return -EINVAL;
      }
+    lock = &to_config_group(item)->cg_subsys->su_mutex;
+    mutex_lock(lock);

Is this lock only protecting against races between key reuse and kexec_file_load(),

The lock here is to protect against races between key reuse and kexec_file_load.

Quoted 2 lines

or does it also handle the case where a new key is added during key reuse or kexec_file_load() is running?

For the cases where a key is added/deleted, configfs will automatically take care of them because it will acquire mutex lock automatically.

Quoted 7 lines

If it is only intended to protect key reuse versus kexec_file_load(), why can't we use the kexec lock instead?

The reason I'm asking is that, in upcoming patches, the key reuse path accesses kexec_crash_image properties and the crash reserved region directly. Doing so without taking the kexec lock (using kexec_trylock()) could lead to race conditions.

After comparing the kexec lock approach with the configfs mutex lock approach, I think the latter is a simpler solution because
1. the kexec lock is non-blocking and we have to repeatedly try until the lock get acquired. So it means user space has to make changes as well.

2. configfs already acquires the mutex lock automatically for creating/deleting configfs items. So if we use configfs mutex lock, it means one less place to use the lock.

In config_keys_reuse_store, kexec_crash_image will be checked before accessing its properties and the crash reserved region. Can you elaborate on what the race conditions are? Will acquiring the lock before accessing kexec_crash_image properties and the crash reserved region help protect against these races?

Quoted 3 lines

In theory, the kexec lock can be a more robust approach. But considering only root can write to the crash dm-crypt keys configfs and load kdump image, I'm not sure it's necessary to adopt a bit more complex solution.

The reuse function accesses the kexec crash image properties and crashkernel memory without taking the kexec lock. This could lead to race conditions or other problems.

Quoted 8 lines

Since we need to take the kexec lock anyway, my suggestion is: can we use the kexec lock instead of cg_subsys->su_mutex if the purpose of the mutex is only to synchronize reuse with kexec_file_load?

As we know, kexec_file_load already runs under the kexec lock. So using the same lock should provide the required synchronization.

- Sourabh Jain

After 1) digging into the git history to learn more about what problems may happen without ensuring serial access to the kexec crash image properties and 2) noticing kexec-tools won't retry when kexec_file_load syscall failed with -EBUSY due to kexec lock acquisition failure which implies it's very unlikely to fail to get the kexec lock, I'm convinced it's better to switch to kexec lock for the reuse function. Thanks for the suggestion!

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

PATCH v3 4/10 crash_dump: Read the number of dm-crypt keys from reserved memory

In reply to Coiby Xu

In case user adds/deletes the keys by mistake, it's safer to read the number of keys from reserved memory.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
Reported-and-Suggested-by Sourabh Jain <[email protected]>
---
 kernel/crash_dump_dm_crypt.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
kernel/crash_dump_dm_crypt.c +23 -13
index d2e66c6fe6f3..a3996208738b 100644
@@ -88,21 +88,31 @@ static int get_keys_from_kdump_reserved_memory(void)
 {
 	struct keys_header *keys_header_loaded;
 	size_t keys_header_size;
-
-	keys_header_size = get_keys_header_size(key_count);
-	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
-	if (!keys_header)
-		return -ENOMEM;
+	int r = 0;
 
 	arch_kexec_unprotect_crashkres();
 	keys_header_loaded = kmap_local_page(pfn_to_page(
 		kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT));
 
+	if (keys_header_loaded->total_keys <= 0 ||
+	    keys_header_loaded->total_keys > KEY_NUM_MAX) {
+		pr_warn("keys_header saved to reserved memory may be corrupt\n");
+		r = -EINVAL;
+		goto kunmap;
+	}
+
+	keys_header_size = get_keys_header_size(keys_header_loaded->total_keys);
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header) {
+		r = -ENOMEM;
+		goto kunmap;
+	}
+
 	memcpy(keys_header, keys_header_loaded, keys_header_size);
+kunmap:
 	kunmap_local(keys_header_loaded);
 	arch_kexec_protect_crashkres();
-
-	return 0;
+	return r;
 }
 
 static int restore_dm_crypt_keys_to_thread_keyring(void)
@@ -446,12 +456,12 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	mutex_lock(&config_keys_subsys.su_mutex);
 	mutex_acquired = true;
 
-	if (key_count <= 0) {
-		kexec_dprintk("No dm-crypt keys\n");
-		return 0;
-	}
-
 	if (!is_dm_key_reused) {
+		if (key_count <= 0) {
+			kexec_dprintk("No dm-crypt keys\n");
+			return 0;
+		}
+
 		r = build_keys_header();
 		if (r)
 			goto out;
@@ -462,7 +472,7 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 	 * cleaned up at the end of kexec_file_load syscall
 	 */
 	kbuf.buffer = keys_header;
-	kbuf.bufsz = get_keys_header_size(key_count);
+	kbuf.bufsz = get_keys_header_size(keys_header->total_keys);
 
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v3 04/10] crash_dump: Read the number of dm-crypt keys from reserved memory

Hi Coiby,

Quoted 2 lines · Coiby Xu

In case user adds/deletes the keys by mistake, it's safer to read the number of keys from reserved memory.

I am not sure how we differentiate between a key being deleted accidentally and a user intentionally deleting it. However, I have a question about how the kernel handles key addition and removal.

How does the kernel handle key add/remove operations to keep the kexec segment corresponding to the key header up to date?

The reason I am asking is to understand what happens when a user deletes a key. Does the kexec segment corresponding to that key header still retain information about the deleted key?

If it does, could you explain why? If it does not, could you explain how the kexec segment gets updated?

Also, for my understanding, could you please point me to what exactly is stored in the key header's kexec segment? During restore, kernel access the old kernel memory using the information stored in that kexec segment, so I would like to better understand what data it contains.

Thanks,
Sourabh Jain

Quoted 78 lines
Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
Reported-and-Suggested-by Sourabh Jain <[email protected]>
---
  kernel/crash_dump_dm_crypt.c | 36 +++++++++++++++++++++++-------------
  1 file changed, 23 insertions(+), 13 deletions(-)
kernel/crash_dump_dm_crypt.c +23 -13
index d2e66c6fe6f3..a3996208738b 100644
@@ -88,21 +88,31 @@ static int get_keys_from_kdump_reserved_memory(void)
  {
  	struct keys_header *keys_header_loaded;
  	size_t keys_header_size;
-
-	keys_header_size = get_keys_header_size(key_count);
-	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
-	if (!keys_header)
-		return -ENOMEM;
+	int r = 0;
 
  	arch_kexec_unprotect_crashkres();
  	keys_header_loaded = kmap_local_page(pfn_to_page(
  		kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT));
 
+	if (keys_header_loaded->total_keys <= 0 ||
+	    keys_header_loaded->total_keys > KEY_NUM_MAX) {
+		pr_warn("keys_header saved to reserved memory may be corrupt\n");
+		r = -EINVAL;
+		goto kunmap;
+	}
+
+	keys_header_size = get_keys_header_size(keys_header_loaded->total_keys);
+	keys_header = kzalloc(keys_header_size, GFP_KERNEL);
+	if (!keys_header) {
+		r = -ENOMEM;
+		goto kunmap;
+	}
+
  	memcpy(keys_header, keys_header_loaded, keys_header_size);
+kunmap:
  	kunmap_local(keys_header_loaded);
  	arch_kexec_protect_crashkres();
-
-	return 0;
+	return r;
  }
 
  static int restore_dm_crypt_keys_to_thread_keyring(void)
@@ -446,12 +456,12 @@ int crash_load_dm_crypt_keys(struct kimage *image)
  	mutex_lock(&config_keys_subsys.su_mutex);
  	mutex_acquired = true;
 
-	if (key_count <= 0) {
-		kexec_dprintk("No dm-crypt keys\n");
-		return 0;
-	}
-
  	if (!is_dm_key_reused) {
+		if (key_count <= 0) {
+			kexec_dprintk("No dm-crypt keys\n");
+			return 0;
+		}
+
  		r = build_keys_header();
  		if (r)
  			goto out;
@@ -462,7 +472,7 @@ int crash_load_dm_crypt_keys(struct kimage *image)
  	 * cleaned up at the end of kexec_file_load syscall
  	 */
  	kbuf.buffer = keys_header;
-	kbuf.bufsz = get_keys_header_size(key_count);
+	kbuf.bufsz = get_keys_header_size(keys_header->total_keys);
 
  	kbuf.memsz = kbuf.bufsz;
  	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
author
#10 lore raw

Re: [PATCH v3 04/10] crash_dump: Read the number of dm-crypt keys from reserved memory

Quoted 1 line · Sourabh Jain

Hi Coiby,

Hi Sourabh,

Quoted 9 lines · Coiby Xu
Quoted 2 lines

In case user adds/deletes the keys by mistake, it's safer to read the number of keys from reserved memory.

I am not sure how we differentiate between a key being deleted accidentally and a user intentionally deleting it. However, I have a question about how the kernel handles key addition and removal.

Quoted 7 lines

How does the kernel handle key add/remove operations to keep the kexec segment corresponding to the key header up to date?

The reason I am asking is to understand what happens when a user deletes a key. Does the kexec segment corresponding to that key header still retain information about the deleted key?

If it does, could you explain why? If it does not, could you explain how the kexec segment gets updated?

Thanks for the questions! When a user delete a key by removing a configfs item, the kexec segment still retain information about the deleted key. The kexec segment will only be updated when the user tries to reload the kdump kernel. Because I think introducing a sync mechanism to automatically update the kexec segment when there is a change to configfs is unnecessary.

Quoted 4 lines

Also, for my understanding, could you please point me to what exactly is stored in the key header's kexec segment? During restore, kernel access the old kernel memory using the information stored in that kexec segment, so I would like to better understand what data it contains.

What is stored can be known from struct keys_header and dm_crypt_key,

struct dm_crypt_key {
	unsigned int key_size;
	char key_desc[KEY_DESC_MAX_LEN];
	u8 data[KEY_SIZE_MAX];
};
struct keys_header {
	unsigned int total_keys;
	struct dm_crypt_key keys[] __counted_by(total_keys);
} *keys_header;

[...]

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

Re: [PATCH v3 04/10] crash_dump: Read the number of dm-crypt keys from reserved memory

Quoted 20 lines · Coiby Xu
Quoted 1 line · Sourabh Jain

Hi Coiby,

Hi Sourabh,

Quoted 15 lines · Coiby Xu
Quoted 2 lines

In case user adds/deletes the keys by mistake, it's safer to read the number of keys from reserved memory.

I am not sure how we differentiate between a key being deleted accidentally and a user intentionally deleting it. However, I have a question about how the kernel handles key addition and removal.

How does the kernel handle key add/remove operations to keep the kexec segment corresponding to the key header up to date?

The reason I am asking is to understand what happens when a user deletes a key. Does the kexec segment corresponding to that key header still retain information about the deleted key?

Quoted 8 lines
Quoted 1 line

If it does, could you explain why? If it does not, could you explain how the kexec segment gets updated?

Thanks for the questions! When a user delete a key by removing a configfs item, the kexec segment still retain information about the deleted key. The kexec segment will only be updated when the user tries to reload the kdump kernel. Because I think introducing a sync mechanism to automatically update the kexec segment when there is a change to configfs is unnecessary.

Thanks for the clarification. So, the kexec segment retains the deleted key information until the kdump kernel is reloaded.

Quoted 13 lines
Quoted 4 lines

Also, for my understanding, could you please point me to what exactly is stored in the key header's kexec segment? During restore, kernel access the old kernel memory using the information stored in that kexec segment, so I would like to better understand what data it contains.

What is stored can be known from struct keys_header and dm_crypt_key,

struct dm_crypt_key {
    unsigned int key_size;
    char key_desc[KEY_DESC_MAX_LEN];
    u8 data[KEY_SIZE_MAX];
};

Quoted 6 lines

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

[...]

Thanks for the pointer I will explore this.

- Sourabh Jain

author
#12 lore raw

PATCH v3 5/10 crash_dump: Free temporary dm-crypt keys_header buffer in kdump kernel

In reply to Coiby Xu

Although we expect the system to reboot immediately after vmcore dumping is finished, it's still good to free the temporary keys_header buffer.

Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
 kernel/crash_dump_dm_crypt.c | 1 +
 1 file changed, 1 insertion(+)
kernel/crash_dump_dm_crypt.c +1 -0
index a3996208738b..9a64e2d6841b 100644
@@ -117,6 +117,7 @@ static int get_keys_from_kdump_reserved_memory(void)
 
 static int restore_dm_crypt_keys_to_thread_keyring(void)
 {
+	struct keys_header *keys_header __free(kfree_sensitive) = NULL;
 	struct dm_crypt_key *key;
 	size_t keys_header_size;
 	key_ref_t keyring_ref;
Signature · 1 line
2.55.0
Reviewed-by on v3 5/10
#13 lore raw

Re: [PATCH v3 05/10] crash_dump: Free temporary dm-crypt keys_header buffer in kdump kernel

Quoted 15 lines · Coiby Xu

Although we expect the system to reboot immediately after vmcore dumping is finished, it's still good to free the temporary keys_header buffer.

Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
  kernel/crash_dump_dm_crypt.c | 1 +
  1 file changed, 1 insertion(+)
kernel/crash_dump_dm_crypt.c +0 -0
index a3996208738b..9a64e2d6841b 100644

kernel/crash_dump_dm_crypt.c static int get_keys_from_kdump_reserved_memory(void) · line 123

Quoted 7 lines
kernel/crash_dump_dm_crypt.c +1 -0
@@ -117,6 +117,7 @@ static int get_keys_from_kdump_reserved_memory(void)
 
  static int restore_dm_crypt_keys_to_thread_keyring(void)
  {
+	struct keys_header *keys_header __free(kfree_sensitive) = NULL;
  	struct dm_crypt_key *key;
  	size_t keys_header_size;
  	key_ref_t keyring_ref;

Yeah, it's good to release the memory regardless.

Feel free to add:

author
#14 lore raw

PATCH v3 6/10 crash_dump: Only use kexec_dprintk during the kexec_file_load syscall

In reply to Coiby Xu

kexec_dprintk will only be activated by "kexec -d" during kexec_file_load syscall. So use pr_* outside of this syscall.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
 kernel/crash_dump_dm_crypt.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
kernel/crash_dump_dm_crypt.c +7 -8
index 9a64e2d6841b..3ee01996778f 100644
@@ -75,10 +75,10 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
 	if (!IS_ERR(key_ref)) {
 		r = key_ref_to_ptr(key_ref)->serial;
 		key_ref_put(key_ref);
-		kexec_dprintk("Success adding key %s", dm_key->key_desc);
+		pr_debug("Success adding key %s", dm_key->key_desc);
 	} else {
 		r = PTR_ERR(key_ref);
-		kexec_dprintk("Error when adding key");
+		pr_warn("Error when adding key");
 	}
 
 	return r;
@@ -128,19 +128,19 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 	keyring_ref =
 		lookup_user_key(KEY_SPEC_USER_KEYRING, 0x01, KEY_NEED_WRITE);
 	if (IS_ERR(keyring_ref)) {
-		kexec_dprintk("Failed to get the user keyring\n");
+		pr_warn("Failed to get the user keyring\n");
 		return PTR_ERR(keyring_ref);
 	}
 
 	addr = dm_crypt_keys_addr;
 	dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
 	if (key_count > KEY_NUM_MAX) {
-		kexec_dprintk("Failed to read the number of dm-crypt keys\n");
+		pr_warn("Failed to read the number of dm-crypt keys\n");
 		ret = -1;
 		goto out;
 	}
 
-	kexec_dprintk("There are %u keys\n", key_count);
+	pr_debug("There are %u keys\n", key_count);
 	addr = dm_crypt_keys_addr;
 
 	keys_header_size = get_keys_header_size(key_count);
@@ -154,7 +154,7 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 
 	for (int i = 0; i < keys_header->total_keys; i++) {
 		key = &keys_header->keys[i];
-		kexec_dprintk("Get key (size=%u)\n", key->key_size);
+		pr_debug("Get key (size=%u)\n", key->key_size);
 		add_key_to_keyring(key, keyring_ref);
 	}
 
@@ -309,8 +309,7 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 	int r;
 
 	if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) {
-		kexec_dprintk(
-			"dm-crypt keys haven't be saved to crash-reserved memory\n");
+		pr_debug("dm-crypt keys haven't be saved to crash-reserved memory\n");
 		return -EINVAL;
 	}
 
Signature · 1 line
2.55.0
Reviewed-by on v3 6/10
#15 lore raw

Re: [PATCH v3 06/10] crash_dump: Only use kexec_dprintk during the kexec_file_load syscall

Quoted 15 lines · Coiby Xu

kexec_dprintk will only be activated by "kexec -d" during kexec_file_load syscall. So use pr_* outside of this syscall.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
  kernel/crash_dump_dm_crypt.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
kernel/crash_dump_dm_crypt.c +0 -0
index 9a64e2d6841b..3ee01996778f 100644

kernel/crash_dump_dm_crypt.c static int add_key_to_keyring(struct dm_crypt_key *dm_key, · line 78

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +1 -1
@@ -75,10 +75,10 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
  	if (!IS_ERR(key_ref)) {
  		r = key_ref_to_ptr(key_ref)->serial;
  		key_ref_put(key_ref);
-		kexec_dprintk("Success adding key %s", dm_key->key_desc);
+		pr_debug("Success adding key %s", dm_key->key_desc);

Missing a newline in the above debug message.

kernel/crash_dump_dm_crypt.c static int add_key_to_keyring(struct dm_crypt_key *dm_key, · line 82

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +1 -1
  	} else {
  		r = PTR_ERR(key_ref);
-		kexec_dprintk("Error when adding key");
+		pr_warn("Error when adding key");
  	}

Missing a newline. Also, I think printing the error code (r) would be helpful.

Apart from these two minor nits, the changes look good to me.

Quoted 44 lines
kernel/crash_dump_dm_crypt.c +5 -6
  	return r;
@@ -128,19 +128,19 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
  	keyring_ref =
  		lookup_user_key(KEY_SPEC_USER_KEYRING, 0x01, KEY_NEED_WRITE);
  	if (IS_ERR(keyring_ref)) {
-		kexec_dprintk("Failed to get the user keyring\n");
+		pr_warn("Failed to get the user keyring\n");
  		return PTR_ERR(keyring_ref);
  	}
 
  	addr = dm_crypt_keys_addr;
  	dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
  	if (key_count > KEY_NUM_MAX) {
-		kexec_dprintk("Failed to read the number of dm-crypt keys\n");
+		pr_warn("Failed to read the number of dm-crypt keys\n");
  		ret = -1;
  		goto out;
  	}
 
-	kexec_dprintk("There are %u keys\n", key_count);
+	pr_debug("There are %u keys\n", key_count);
  	addr = dm_crypt_keys_addr;
 
  	keys_header_size = get_keys_header_size(key_count);
@@ -154,7 +154,7 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 
  	for (int i = 0; i < keys_header->total_keys; i++) {
  		key = &keys_header->keys[i];
-		kexec_dprintk("Get key (size=%u)\n", key->key_size);
+		pr_debug("Get key (size=%u)\n", key->key_size);
  		add_key_to_keyring(key, keyring_ref);
  	}
 
@@ -309,8 +309,7 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
  	int r;
 
  	if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) {
-		kexec_dprintk(
-			"dm-crypt keys haven't be saved to crash-reserved memory\n");
+		pr_debug("dm-crypt keys haven't be saved to crash-reserved memory\n");
  		return -EINVAL;
  	}
 
author
#16 lore raw

Re: [PATCH v3 06/10] crash_dump: Only use kexec_dprintk during the kexec_file_load syscall

Quoted 26 lines · Sourabh Jain
Quoted 15 lines · Coiby Xu

kexec_dprintk will only be activated by "kexec -d" during kexec_file_load syscall. So use pr_* outside of this syscall.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
 kernel/crash_dump_dm_crypt.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
kernel/crash_dump_dm_crypt.c +0 -0
index 9a64e2d6841b..3ee01996778f 100644

kernel/crash_dump_dm_crypt.c static int add_key_to_keyring(struct dm_crypt_key *dm_key, · line 78

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +1 -1
@@ -75,10 +75,10 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
 	if (!IS_ERR(key_ref)) {
 		r = key_ref_to_ptr(key_ref)->serial;
 		key_ref_put(key_ref);
-		kexec_dprintk("Success adding key %s", dm_key->key_desc);
+		pr_debug("Success adding key %s", dm_key->key_desc);

Missing a newline in the above debug message.

Quoted 7 lines

kernel/crash_dump_dm_crypt.c static int add_key_to_keyring(struct dm_crypt_key *dm_key, · line 82

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +1 -1
 	} else {
 		r = PTR_ERR(key_ref);
-		kexec_dprintk("Error when adding key");
+		pr_warn("Error when adding key");
 	}

Missing a newline. Also, I think printing the error code (r) would be helpful.

Good catches! And I'll apply your suggestion!

Quoted 4 lines

Apart from these two minor nits, the changes look good to me.

Thanks for reviewing the patch!

[...]

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

PATCH v3 7/10 crash_dump: Improve readability of config_keys_restore_store

In reply to Coiby Xu

config_keys_restore_store currently doesn't validate the user input before restoring dm-crypt keys. Although it's not necessary for the case of vmcore dumping, it's better to do it for the sake of consistency and code readability. Also check the return code of restore_dm_crypt_keys_to_thread_keyring.

Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
 kernel/crash_dump_dm_crypt.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
kernel/crash_dump_dm_crypt.c +10 -3
index 3ee01996778f..1f4549956824 100644
@@ -370,12 +370,19 @@ static ssize_t config_keys_restore_show(struct config_item *item, char *page)
 static ssize_t config_keys_restore_store(struct config_item *item,
 					  const char *page, size_t count)
 {
-	if (!restore)
-		restore_dm_crypt_keys_to_thread_keyring();
+	bool val;
 
-	if (kstrtobool(page, &restore))
+	if (kstrtobool(page, &val))
 		return -EINVAL;
 
+	if (val) {
+		if (restore) {
+			pr_warn("dm-crypt keys already restored!\n");
+			return count;
+		} else if (!restore_dm_crypt_keys_to_thread_keyring())
+			restore = true;
+	}
+
 	return count;
 }
 
Signature · 1 line
2.55.0
Reviewed-by on v3 7/10
#18 lore raw

Re: [PATCH v3 07/10] crash_dump: Improve readability of config_keys_restore_store

Quoted 27 lines · Coiby Xu

config_keys_restore_store currently doesn't validate the user input before restoring dm-crypt keys. Although it's not necessary for the case of vmcore dumping, it's better to do it for the sake of consistency and code readability. Also check the return code of restore_dm_crypt_keys_to_thread_keyring.

Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
  kernel/crash_dump_dm_crypt.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
kernel/crash_dump_dm_crypt.c +2 -3
index 3ee01996778f..1f4549956824 100644
@@ -370,12 +370,19 @@ static ssize_t config_keys_restore_show(struct config_item *item, char *page)
  static ssize_t config_keys_restore_store(struct config_item *item,
  					  const char *page, size_t count)
  {
-	if (!restore)
-		restore_dm_crypt_keys_to_thread_keyring();
+	bool val;
 
-	if (kstrtobool(page, &restore))
+	if (kstrtobool(page, &val))
  		return -EINVAL;

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_restore_show(struct config_item *item, char *page) · line 386

Quoted 10 lines
kernel/crash_dump_dm_crypt.c +8 -0
 
+	if (val) {
+		if (restore) {
+			pr_warn("dm-crypt keys already restored!\n");
+			return count;
+		} else if (!restore_dm_crypt_keys_to_thread_keyring())
+			restore = true;
+	}
+
  	return count;

Nit: returns count even when restore fails.

Rest of changes looks good to me.

Quoted 2 lines
kernel/crash_dump_dm_crypt.c +0 -0
  }
 
author
#19 lore raw

Re: [PATCH v3 07/10] crash_dump: Improve readability of config_keys_restore_store

Quoted 32 lines · Sourabh Jain
Quoted 29 lines · Coiby Xu

config_keys_restore_store currently doesn't validate the user input before restoring dm-crypt keys. Although it's not necessary for the case of vmcore dumping, it's better to do it for the sake of consistency and code readability. Also check the return code of restore_dm_crypt_keys_to_thread_keyring.

Fixes 62f17d9df692 ("crash_dump: retrieve dm crypt keys in kdump kernel")
---
 kernel/crash_dump_dm_crypt.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
kernel/crash_dump_dm_crypt.c +5 -3
index 3ee01996778f..1f4549956824 100644
@@ -370,12 +370,19 @@ static ssize_t config_keys_restore_show(struct config_item *item, char *page)
 static ssize_t config_keys_restore_store(struct config_item *item,
 					  const char *page, size_t count)
 {
-	if (!restore)
-		restore_dm_crypt_keys_to_thread_keyring();
+	bool val;
-	if (kstrtobool(page, &restore))
+	if (kstrtobool(page, &val))
 		return -EINVAL;
+	if (val) {
+		if (restore) {
+			pr_warn("dm-crypt keys already restored!\n");
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_restore_show(struct config_item *item, char *page) · line 386

Quoted 6 lines
kernel/crash_dump_dm_crypt.c +5 -0
+			return count;
+		} else if (!restore_dm_crypt_keys_to_thread_keyring())
+			restore = true;
+	}
+
 	return count;

Nit: returns count even when restore fails.

I'll change it in next version, thanks!

Quoted 3 lines

Rest of changes looks good to me.

And thanks for reviewing and ack'ing the patch!

Quoted 3 lines
Quoted 2 lines
kernel/crash_dump_dm_crypt.c +0 -0
 }
Signature · 2 lines
Best regards,
Coiby
author
#20 lore raw

PATCH v3 8/10 crash_dump: Check the function return codes in restore_dm_crypt_keys_to_thread_keyring

In reply to Coiby Xu

We should check the return codes so we can abort if keyring allocation or reading old memory fails.

Note there is no need to refer a key add_key_to_keyring, so delete related code.

---
 kernel/crash_dump_dm_crypt.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
kernel/crash_dump_dm_crypt.c +16 -7
index 1f4549956824..b5b78656cf06 100644
@@ -65,7 +65,7 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
 			      key_ref_t keyring_ref)
 {
 	key_ref_t key_ref;
-	int r;
+	int r = 0;
 
 	/* create or update the requested key and add it to the target keyring */
 	key_ref = key_create_or_update(keyring_ref, "user", dm_key->key_desc,
@@ -73,8 +73,6 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
 				       KEY_USR_ALL, KEY_ALLOC_IN_QUOTA);
 
 	if (!IS_ERR(key_ref)) {
-		r = key_ref_to_ptr(key_ref)->serial;
-		key_ref_put(key_ref);
 		pr_debug("Success adding key %s", dm_key->key_desc);
 	} else {
 		r = PTR_ERR(key_ref);
@@ -133,9 +131,14 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 	}
 
 	addr = dm_crypt_keys_addr;
-	dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
+	ret = dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
+	if (ret < 0) {
+		pr_err("Failed to read the number of dm-crypt keys\n");
+		goto out;
+	}
+
 	if (key_count > KEY_NUM_MAX) {
-		pr_warn("Failed to read the number of dm-crypt keys\n");
+		pr_warn("Read %u dm-crypt keys (max=%u)\n", key_count, KEY_NUM_MAX);
 		ret = -1;
 		goto out;
 	}
@@ -150,12 +153,18 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
 		goto out;
 	}
 
-	dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
+	ret = dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
+	if (ret < 0) {
+		pr_err("Failed to read dm-crypt keys\n");
+		goto out;
+	}
 
 	for (int i = 0; i < keys_header->total_keys; i++) {
 		key = &keys_header->keys[i];
 		pr_debug("Get key (size=%u)\n", key->key_size);
-		add_key_to_keyring(key, keyring_ref);
+		ret = add_key_to_keyring(key, keyring_ref);
+		if (ret)
+			break;
 	}
 
 out:
Signature · 1 line
2.55.0
Reviewed-by on v3 8/10
#21 lore raw

Re: [PATCH v3 08/10] crash_dump: Check the function return codes in restore_dm_crypt_keys_to_thread_keyring

Quoted 61 lines · Coiby Xu

We should check the return codes so we can abort if keyring allocation or reading old memory fails.

Note there is no need to refer a key add_key_to_keyring, so delete related code.

---
  kernel/crash_dump_dm_crypt.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
kernel/crash_dump_dm_crypt.c +13 -6
index 1f4549956824..b5b78656cf06 100644
@@ -65,7 +65,7 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
  			      key_ref_t keyring_ref)
  {
  	key_ref_t key_ref;
-	int r;
+	int r = 0;
 
  	/* create or update the requested key and add it to the target keyring */
  	key_ref = key_create_or_update(keyring_ref, "user", dm_key->key_desc,
@@ -73,8 +73,6 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key,
  				       KEY_USR_ALL, KEY_ALLOC_IN_QUOTA);
 
  	if (!IS_ERR(key_ref)) {
-		r = key_ref_to_ptr(key_ref)->serial;
-		key_ref_put(key_ref);
  		pr_debug("Success adding key %s", dm_key->key_desc);
  	} else {
  		r = PTR_ERR(key_ref);
@@ -133,9 +131,14 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
  	}
 
  	addr = dm_crypt_keys_addr;
-	dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
+	ret = dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
+	if (ret < 0) {
+		pr_err("Failed to read the number of dm-crypt keys\n");
+		goto out;
+	}
+
  	if (key_count > KEY_NUM_MAX) {
-		pr_warn("Failed to read the number of dm-crypt keys\n");
+		pr_warn("Read %u dm-crypt keys (max=%u)\n", key_count, KEY_NUM_MAX);
  		ret = -1;
  		goto out;
  	}
@@ -150,12 +153,18 @@ static int restore_dm_crypt_keys_to_thread_keyring(void)
  		goto out;
  	}
 
-	dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
+	ret = dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
+	if (ret < 0) {
+		pr_err("Failed to read dm-crypt keys\n");
+		goto out;
+	}
 

kernel/crash_dump_dm_crypt.c static int restore_dm_crypt_keys_to_thread_keyring(void) · line 170

Quoted 10 lines
kernel/crash_dump_dm_crypt.c +3 -1
  	for (int i = 0; i < keys_header->total_keys; i++) {
  		key = &keys_header->keys[i];
  		pr_debug("Get key (size=%u)\n", key->key_size);
-		add_key_to_keyring(key, keyring_ref);
+		ret = add_key_to_keyring(key, keyring_ref);
+		if (ret)
+			break;
  	}
 
  out:

Changes looks good to me. Feel free to add.

author
#22 lore raw

PATCH v3 9/10 crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash hotplug supported

In reply to Coiby Xu

If crash hotplug is supported, dm-crypt keys saved to reserved memory will be taken care of automatically. Thus it doesn't make sense to use configfs/crash_dm_crypt_key/reuse. Reserving image->dm_crypt_keys_addr is also unnecessary. Currently x86_64 and ppc64le have implemented crash hotplug feature.

Also update the doc accordingly. Note two doc issues are fixed as well.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
---
 Documentation/admin-guide/kdump/kdump.rst | 16 ++++++++++------
 kernel/crash_dump_dm_crypt.c              | 23 ++++++++++++++++++++---
 2 files changed, 30 insertions(+), 9 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +10 -6
index 7587caadbae1..0bf2eb100a05 100644
@@ -577,9 +577,10 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
 
 1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
     # Add key #1
-    mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7426b0996720
+    VOL1_UUID=7d26b7b4-e342-4d2d-b660-7426b0996720
+    mkdir /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID
     # Add key #1's description
-    echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/config/crash_dm_crypt_keys/description
+    echo cryptsetup:$VOL1_UUID > /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID/description
 
     # how many keys do we have now?
     cat /sys/kernel/config/crash_dm_crypt_keys/count
@@ -591,15 +592,18 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
     cat /sys/kernel/config/crash_dm_crypt_keys/count
     2
 
-    # To support CPU/memory hot-plugging, reuse keys already saved to reserved
-    # memory
-    echo true > /sys/kernel/config/crash_dm_crypt_key/reuse
-
 2. Load the dump-capture kernel
 
 3. After the dump-capture kerne get booted, restore the keys to user keyring
    echo yes > /sys/kernel/crash_dm_crypt_keys/restore
 
+For CPU/memory hot-plugging, you can reuse keys already saved to reserved
+memory before reloading the kdump image,
+    echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse
+
+Note if crash hotplug is supported, this API is totally unnecessary thus will
+be disabled automatically.
+
 Contact
 =======
 
kernel/crash_dump_dm_crypt.c +20 -3
index b5b78656cf06..46d6b31c3ca4 100644
@@ -310,6 +310,15 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
 	return sysfs_emit(page, "%d\n", is_dm_key_reused);
 }
 
+static bool crash_hotplug_support(struct kimage *image)
+{
+#ifdef CONFIG_CRASH_HOTPLUG
+	return image->hotplug_support;
+#else
+	return false;
+#endif
+}
+
 static ssize_t config_keys_reuse_store(struct config_item *item,
 					   const char *page, size_t count)
 {
@@ -317,6 +326,11 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
 	bool val;
 	int r;
 
+	if (kexec_crash_image && crash_hotplug_support(kexec_crash_image)) {
+		pr_debug("Crash hotplug supported\n");
+		return -EINVAL;
+	}
+
 	if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) {
 		pr_debug("dm-crypt keys haven't be saved to crash-reserved memory\n");
 		return -EINVAL;
@@ -513,9 +527,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
 void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
 {
 	/*
-	 * For CPU/memory hot-plugging, the kdump image will be reloaded. Prevent
-	 * keys_header from being cleaned up during unloading when
-	 * is_dm_key_reused=true
+	 * For CPU/memory hot-plugging without CONFIG_CRASH_HOTPLUG, the whole kdump
+	 * image will be reloaded. Prevent keys_header from being cleaned up during
+	 * unloading when is_dm_key_reused=true
 	 */
 	if (!is_dm_key_reused) {
 		kfree_sensitive(keys_header);
@@ -526,6 +540,9 @@ void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
 		mutex_unlock(&config_keys_subsys.su_mutex);
 		mutex_acquired = false;
 	}
+
+	if (crash_hotplug_support(image))
+		image->dm_crypt_keys_addr = 0;
 }
 
 static int __init configfs_dmcrypt_keys_init(void)
Signature · 1 line
2.55.0
#23 lore raw

Re: [PATCH v3 09/10] crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash hotplug supported

Quoted 60 lines · Coiby Xu

If crash hotplug is supported, dm-crypt keys saved to reserved memory will be taken care of automatically. Thus it doesn't make sense to use configfs/crash_dm_crypt_key/reuse. Reserving image->dm_crypt_keys_addr is also unnecessary. Currently x86_64 and ppc64le have implemented crash hotplug feature.

Also update the doc accordingly. Note two doc issues are fixed as well.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
---
  Documentation/admin-guide/kdump/kdump.rst | 16 ++++++++++------
  kernel/crash_dump_dm_crypt.c              | 23 ++++++++++++++++++++---
  2 files changed, 30 insertions(+), 9 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +10 -6
index 7587caadbae1..0bf2eb100a05 100644
@@ -577,9 +577,10 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
 
  1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
      # Add key #1
-    mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7426b0996720
+    VOL1_UUID=7d26b7b4-e342-4d2d-b660-7426b0996720
+    mkdir /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID
      # Add key #1's description
-    echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/config/crash_dm_crypt_keys/description
+    echo cryptsetup:$VOL1_UUID > /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID/description
 
      # how many keys do we have now?
      cat /sys/kernel/config/crash_dm_crypt_keys/count
@@ -591,15 +592,18 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
      cat /sys/kernel/config/crash_dm_crypt_keys/count
      2
 
-    # To support CPU/memory hot-plugging, reuse keys already saved to reserved
-    # memory
-    echo true > /sys/kernel/config/crash_dm_crypt_key/reuse
-
  2. Load the dump-capture kernel
 
  3. After the dump-capture kerne get booted, restore the keys to user keyring
     echo yes > /sys/kernel/crash_dm_crypt_keys/restore
 
+For CPU/memory hot-plugging, you can reuse keys already saved to reserved
+memory before reloading the kdump image,
+    echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse
+
+Note if crash hotplug is supported, this API is totally unnecessary thus will
+be disabled automatically.
+
  Contact
  =======
 
kernel/crash_dump_dm_crypt.c +0 -0
index b5b78656cf06..46d6b31c3ca4 100644

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_show(struct config_item *item, char *page) · line 316

Quoted 7 lines
kernel/crash_dump_dm_crypt.c +4 -0
@@ -310,6 +310,15 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
  	return sysfs_emit(page, "%d\n", is_dm_key_reused);
  }
 
+static bool crash_hotplug_support(struct kimage *image)
+{
+#ifdef CONFIG_CRASH_HOTPLUG
+	return image->hotplug_support;

I don't think it is good idea to access kexec_crash_image properties without holding the kexec lock.

Also, would it make sense to move this API somewhere else?

- Sourabh Jain

Quoted 43 lines
kernel/crash_dump_dm_crypt.c +16 -3
+#else
+	return false;
+#endif
+}
+
  static ssize_t config_keys_reuse_store(struct config_item *item,
  					   const char *page, size_t count)
  {
@@ -317,6 +326,11 @@ static ssize_t config_keys_reuse_store(struct config_item *item,
  	bool val;
  	int r;
 
+	if (kexec_crash_image && crash_hotplug_support(kexec_crash_image)) {
+		pr_debug("Crash hotplug supported\n");
+		return -EINVAL;
+	}
+
  	if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) {
  		pr_debug("dm-crypt keys haven't be saved to crash-reserved memory\n");
  		return -EINVAL;
@@ -513,9 +527,9 @@ int crash_load_dm_crypt_keys(struct kimage *image)
  void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
  {
  	/*
-	 * For CPU/memory hot-plugging, the kdump image will be reloaded. Prevent
-	 * keys_header from being cleaned up during unloading when
-	 * is_dm_key_reused=true
+	 * For CPU/memory hot-plugging without CONFIG_CRASH_HOTPLUG, the whole kdump
+	 * image will be reloaded. Prevent keys_header from being cleaned up during
+	 * unloading when is_dm_key_reused=true
  	 */
  	if (!is_dm_key_reused) {
  		kfree_sensitive(keys_header);
@@ -526,6 +540,9 @@ void kexec_file_post_load_cleanup_dm_crypt(struct kimage *image)
  		mutex_unlock(&config_keys_subsys.su_mutex);
  		mutex_acquired = false;
  	}
+
+	if (crash_hotplug_support(image))
+		image->dm_crypt_keys_addr = 0;
  }
 
  static int __init configfs_dmcrypt_keys_init(void)
author
#24 lore raw

Re: [PATCH v3 09/10] crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash hotplug supported

Quoted 58 lines · Sourabh Jain
Quoted 55 lines · Coiby Xu

If crash hotplug is supported, dm-crypt keys saved to reserved memory will be taken care of automatically. Thus it doesn't make sense to use configfs/crash_dm_crypt_key/reuse. Reserving image->dm_crypt_keys_addr is also unnecessary. Currently x86_64 and ppc64le have implemented crash hotplug feature.

Also update the doc accordingly. Note two doc issues are fixed as well.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")
---
 Documentation/admin-guide/kdump/kdump.rst | 16 ++++++++++------
 kernel/crash_dump_dm_crypt.c              | 23 ++++++++++++++++++++---
 2 files changed, 30 insertions(+), 9 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +10 -6
index 7587caadbae1..0bf2eb100a05 100644
@@ -577,9 +577,10 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
 1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
     # Add key #1
-    mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7426b0996720
+    VOL1_UUID=7d26b7b4-e342-4d2d-b660-7426b0996720
+    mkdir /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID
     # Add key #1's description
-    echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/config/crash_dm_crypt_keys/description
+    echo cryptsetup:$VOL1_UUID > /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID/description
     # how many keys do we have now?
     cat /sys/kernel/config/crash_dm_crypt_keys/count
@@ -591,15 +592,18 @@ with /sys/kernel/config/crash_dm_crypt_keys for setup,
     cat /sys/kernel/config/crash_dm_crypt_keys/count
     2
-    # To support CPU/memory hot-plugging, reuse keys already saved to reserved
-    # memory
-    echo true > /sys/kernel/config/crash_dm_crypt_key/reuse
-
 2. Load the dump-capture kernel
 3. After the dump-capture kerne get booted, restore the keys to user keyring
    echo yes > /sys/kernel/crash_dm_crypt_keys/restore
+For CPU/memory hot-plugging, you can reuse keys already saved to reserved
+memory before reloading the kdump image,
+    echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse
+
+Note if crash hotplug is supported, this API is totally unnecessary thus will
+be disabled automatically.
+
 Contact
 =======
kernel/crash_dump_dm_crypt.c +0 -0
index b5b78656cf06..46d6b31c3ca4 100644
@@ -310,6 +310,15 @@ static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
 	return sysfs_emit(page, "%d\n", is_dm_key_reused);
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_show(struct config_item *item, char *page) · line 316

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +4 -0
 }
+static bool crash_hotplug_support(struct kimage *image)
+{
+#ifdef CONFIG_CRASH_HOTPLUG
+	return image->hotplug_support;

I don't think it is good idea to access kexec_crash_image properties without holding the kexec lock.

Thanks for raising the concern! Let's discuss this issue in [PATCH v3 03/10].

Quoted 2 lines

Also, would it make sense to move this API somewhere else?

Can you help me understand why to move this API? Is it because using ifdef in .c file is discouraged?

Quoted 2 lines

- Sourabh Jain

[...]

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

Re: [PATCH v3 09/10] crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash hotplug supported

Quoted 77 lines · Coiby Xu
Quoted 67 lines · Sourabh Jain
Quoted 64 lines · Coiby Xu

If crash hotplug is supported, dm-crypt keys saved to reserved memory will be taken care of automatically. Thus it doesn't make sense to use configfs/crash_dm_crypt_key/reuse. Reserving image->dm_crypt_keys_addr is also unnecessary. Currently x86_64 and ppc64le have implemented crash hotplug feature.

Also update the doc accordingly. Note two doc issues are fixed as well.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")

---
 Documentation/admin-guide/kdump/kdump.rst | 16 ++++++++++------ kernel/crash_dump_dm_crypt.c              | 23 ++++++++++++++++++++---

 2 files changed, 30 insertions(+), 9 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +0 -1
index 7587caadbae1..0bf2eb100a05 100644
@@ -577,9 +577,10 @@ with /sys/kernel/config/crash_dm_crypt_keys for 
 setup,
  1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
      # Add key #1
-    mkdir 

/sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7426b0996720 +    VOL1_UUID=7d26b7b4-e342-4d2d-b660-7426b0996720
+    mkdir /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID
     # Add key #1's description
-    echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/config/crash_dm_crypt_keys/description
+    echo cryptsetup:$VOL1_UUID > /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID/description

     # how many keys do we have now?
     cat /sys/kernel/config/crash_dm_crypt_keys/count
Documentation/admin-guide/kdump/kdump.rst +0 -0
@@ -591,15 +592,18 @@ with /sys/kernel/config/crash_dm_crypt_keys 
 for setup,

     cat /sys/kernel/config/crash_dm_crypt_keys/count
     2
-    # To support CPU/memory hot-plugging, reuse keys already saved to reserved
-    # memory
-    echo true > /sys/kernel/config/crash_dm_crypt_key/reuse
-
 2. Load the dump-capture kernel
 3. After the dump-capture kerne get booted, restore the keys to user keyring echo yes > /sys/kernel/crash_dm_crypt_keys/restore
+For CPU/memory hot-plugging, you can reuse keys already saved to reserved

Documentation/admin-guide/kdump/kdump.rst +4 -0
+memory before reloading the kdump image,
+    echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse
+
+Note if crash hotplug is supported, this API is totally unnecessary 

thus will

Documentation/admin-guide/kdump/kdump.rst +2 -0
+be disabled automatically.
+

 Contact
 =======

kernel/crash_dump_dm_crypt.c +0 -0
index b5b78656cf06..46d6b31c3ca4 100644
@@ -310,6 +310,15 @@ static ssize_t config_keys_reuse_show(struct 
 config_item *item, char *page)
      return sysfs_emit(page, "%d\n", is_dm_key_reused);
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_show(struct config_item *item, char *page) · line 316

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +4 -0
  }
+static bool crash_hotplug_support(struct kimage *image)
+{
+#ifdef CONFIG_CRASH_HOTPLUG
+    return image->hotplug_support;

I don't think it is good idea to access kexec_crash_image properties without holding the kexec lock.

Quoted 7 lines

Thanks for raising the concern! Let's discuss this issue in [PATCH v3 03/10].

Quoted 2 lines

Also, would it make sense to move this API somewhere else?

Can you help me understand why to move this API? Is it because using ifdef in .c file is discouraged?

This API is used to verify crash hotplug support, which is not directly related to crash_dm_crypt_keys. It also seems to be generally useful.

- Sourabh Jain

Quoted 5 lines
Quoted 3 lines

- Sourabh Jain

[...]

author
#26 lore raw

Re: [PATCH v3 09/10] crash_dump: Disallow configfs/crash_dm_crypt_key/reuse if crash hotplug supported

Quoted 84 lines · Sourabh Jain
Quoted 81 lines · Coiby Xu
Quoted 66 lines · Sourabh Jain
Quoted 63 lines · Coiby Xu

If crash hotplug is supported, dm-crypt keys saved to reserved memory will be taken care of automatically. Thus it doesn't make sense to use configfs/crash_dm_crypt_key/reuse. Reserving image->dm_crypt_keys_addr is also unnecessary. Currently x86_64 and ppc64le have implemented crash hotplug feature.

Also update the doc accordingly. Note two doc issues are fixed as well.

Fixes 9ebfa8dcaea7 ("crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging")

---
 Documentation/admin-guide/kdump/kdump.rst | 16 ++++++++++------ kernel/crash_dump_dm_crypt.c              | 23 ++++++++++++++++++++---

 2 files changed, 30 insertions(+), 9 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +2 -2
index 7587caadbae1..0bf2eb100a05 100644
@@ -577,9 +577,10 @@ with /sys/kernel/config/crash_dm_crypt_keys 
 for setup,
  1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
      # Add key #1
-    mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7426b0996720
+    VOL1_UUID=7d26b7b4-e342-4d2d-b660-7426b0996720
+    mkdir /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID
      # Add key #1's description
-    echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > 

/sys/kernel/config/crash_dm_crypt_keys/description +    echo cryptsetup:$VOL1_UUID > /sys/kernel/config/crash_dm_crypt_keys/$VOL1_UUID/description

     # how many keys do we have now?
     cat /sys/kernel/config/crash_dm_crypt_keys/count
Documentation/admin-guide/kdump/kdump.rst +0 -0
@@ -591,15 +592,18 @@ with 
 /sys/kernel/config/crash_dm_crypt_keys for setup,

     cat /sys/kernel/config/crash_dm_crypt_keys/count
     2
-    # To support CPU/memory hot-plugging, reuse keys already saved to reserved
-    # memory
-    echo true > /sys/kernel/config/crash_dm_crypt_key/reuse
-
 2. Load the dump-capture kernel
 3. After the dump-capture kerne get booted, restore the keys to user keyring echo yes > /sys/kernel/crash_dm_crypt_keys/restore
+For CPU/memory hot-plugging, you can reuse keys already saved
to reserved

Documentation/admin-guide/kdump/kdump.rst +4 -0
+memory before reloading the kdump image,
+    echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse
+
+Note if crash hotplug is supported, this API is totally 

unnecessary thus will

Documentation/admin-guide/kdump/kdump.rst +2 -0
+be disabled automatically.
+

 Contact
 =======

kernel/crash_dump_dm_crypt.c +0 -0
index b5b78656cf06..46d6b31c3ca4 100644
@@ -310,6 +310,15 @@ static ssize_t 
 config_keys_reuse_show(struct config_item *item, char *page)
      return sysfs_emit(page, "%d\n", is_dm_key_reused);
Quoted 8 lines

kernel/crash_dump_dm_crypt.c static ssize_t config_keys_reuse_show(struct config_item *item, char *page) · line 316

Quoted 5 lines
kernel/crash_dump_dm_crypt.c +4 -0
  }
+static bool crash_hotplug_support(struct kimage *image)
+{
+#ifdef CONFIG_CRASH_HOTPLUG
+    return image->hotplug_support;

I don't think it is good idea to access kexec_crash_image properties without holding the kexec lock.

Thanks for raising the concern! Let's discuss this issue in [PATCH v3 03/10].

Quoted 2 lines

Also, would it make sense to move this API somewhere else?

Quoted 7 lines
Quoted 2 lines

Can you help me understand why to move this API? Is it because using ifdef in .c file is discouraged?

This API is used to verify crash hotplug support, which is not directly related to crash_dm_crypt_keys. It also seems to be generally useful.

- Sourabh Jain

Thanks for the clarification! After trying to make use of existing crash_check_hotplug_support API in kernel/crash_core.c, I realize there is no need to explicitly disallow configfs/crash_dm_crypt_key/reuse. Because image->dm_crypt_keys_addr will be set to 0 when crash hotplug is supported. Thus in fact the reuse API has already been implicitly disallowed. So I will drop the unnecessary crash_hotplug_support function in next version.

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

PATCH v3 10/10 Documentation: kdump: Add arm64 and ppc64le to encrypted dump target support list

In reply to Coiby Xu

The encrypted dump target support is now extended to arm64 and ppc64le.

Fixes e3a84be1ec2f ("arm64,ppc64le/kdump: pass dm-crypt keys to kdump kernel")
---
 Documentation/admin-guide/kdump/kdump.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +2 -2
index 0bf2eb100a05..36ad6125c670 100644
@@ -572,8 +572,8 @@ Write the dump file to encrypted disk volume
 ============================================
 
 CONFIG_CRASH_DM_CRYPT can be enabled to support saving the dump file to an
-encrypted disk volume (only x86_64 supported for now). User space can interact
-with /sys/kernel/config/crash_dm_crypt_keys for setup,
+encrypted disk volume (only x86_64, arm64, ppc64le supported for now). User
+space can interact with /sys/kernel/config/crash_dm_crypt_keys for setup,
 
 1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
     # Add key #1
Signature · 1 line
2.55.0
Reviewed-by on v3 10/10
#28 lore raw

Re: [PATCH v3 10/10] Documentation: kdump: Add arm64 and ppc64le to encrypted dump target support list

Quoted 14 lines · Coiby Xu

The encrypted dump target support is now extended to arm64 and ppc64le.

Fixes e3a84be1ec2f ("arm64,ppc64le/kdump: pass dm-crypt keys to kdump kernel")
---
  Documentation/admin-guide/kdump/kdump.rst | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/admin-guide/kdump/kdump.rst +0 -0
index 0bf2eb100a05..36ad6125c670 100644

Documentation/admin-guide/kdump/kdump.rst Write the dump file to encrypted disk volume · line 579

Quoted 10 lines
Documentation/admin-guide/kdump/kdump.rst +2 -2
@@ -572,8 +572,8 @@ Write the dump file to encrypted disk volume
  ============================================
 
  CONFIG_CRASH_DM_CRYPT can be enabled to support saving the dump file to an
-encrypted disk volume (only x86_64 supported for now). User space can interact
-with /sys/kernel/config/crash_dm_crypt_keys for setup,
+encrypted disk volume (only x86_64, arm64, ppc64le supported for now). User
+space can interact with /sys/kernel/config/crash_dm_crypt_keys for setup,
 
  1. Tell the first kernel what logon keys are needed to unlock the disk volumes,
      # Add key #1

Changes look good to me. Feel free to add.