Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime
@ 2026-12-03  8:24 Yingjie Wang
  2026-12-03  8:24 ` [PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions Yingjie Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yingjie Wang @ 2026-12-03  8:24 UTC (permalink / raw)
  To: Andrew Morton, Baoquan He
  Cc: Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
	Coiby Xu, Sourabh Jain, kexec@lists.infradead.org,
	linux-kernel@vger.kernel.org

The description attribute of /sys/kernel/config/crash_dm_crypt_keys/<key>
has no locking of its own, and configfs only serializes attribute I/O per
open file. Patch 1 adds a per-key mutex covering the store, the show and
the copy made by build_keys_header() during kexec_file_load(). Patch 2
frees the description when a key directory is removed.

Relation to Coiby's "Bug fixes and enhancements for kdump LUKS support"
v5 [1]:

- That series does not touch the description attribute. Its patch 3/9
  holds config_keys_subsys.su_mutex across crash_load_dm_crypt_keys(),
  but configfs takes su_mutex only for directory operations, not for
  attribute reads and writes, so these races remain with it applied.

- This series does not fix kexec_file_load() racing with mkdir/rmdir of
  a key directory. build_keys_header() walks cg_children without
  su_mutex, so removing a key during the walk is already a
  use-after-free of the config_key itself. v5 3/9 fixes that: with it,
  a key that build_keys_header() sees stays linked, so it cannot be
  released (which frees its lock and description) until su_mutex is
  dropped. The lock order is then su_mutex -> config_key->lock; the
  store and show paths never take su_mutex.

The two series apply on top of each other in either order, with the
same result. This one is based on mainline; kernel/crash_dump_dm_crypt.c
is the same in mm-nonmm-unstable and linux-next.

Testing (x86_64 QEMU guest, KASAN, PROVE_LOCKING, KMEMLEAK,
crashkernel=256M, booted with oops=panic panic_on_warn=1):

                                       unpatched           patched
  3 writers + 3 readers, separate fds  double-free         clean, 120 s
  1 writer + 7 readers                 UAF in show         clean, 120 s
  1 writer vs. kexec_file_load(crash)  UAF in              clean, 300 s
                                       build_keys_header
  create, describe, remove 50 keys     47 kmemleak reports 0 reports

In the kexec_file_load() test no logon key matches the description, so
each of the 23050 load attempts copied the description and then failed
with ENOKEY. A functional check (store/read back, 128- and 129-character
and empty descriptions, count, and loading a crash image with a matching
logon key using KEXEC_FILE_DEBUG to confirm "Loaded dm crypt keys")
passes on both kernels.

With the v5 series (1-8/9) applied alone, the first three tests still
hit the double-free, the UAF in show and the UAF in build_keys_header().
With both series applied, all four tests are clean, and so is a 120 s
run that removes and recreates a key directory in a loop (678439 times)
during kexec_file_load() (4147 load attempts), with no lockdep reports.

The reproducers are available on request.

Not tested: arm64 and powerpc, which call crash_load_dm_crypt_keys() from
their own kexec_file loaders.

[1] https://lore.kernel.org/r/20260909003657.1570544-1-coiby.xu@gmail.com

Yingjie Wang (2):
  crash_dump: serialize access to dm-crypt key descriptions
  crash_dump: free dm-crypt key description on release

 kernel/crash_dump_dm_crypt.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)


base-commit: 5a29cfaf51105039d156dd8a61d2783e8b653667
-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions
  2026-12-03  8:24 [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime Yingjie Wang
@ 2026-12-03  8:24 ` Yingjie Wang
  2026-12-03  8:24 ` [PATCH 2/2] crash_dump: free dm-crypt key description on release Yingjie Wang
  2026-12-03  8:42 ` [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Yingjie Wang @ 2026-12-03  8:24 UTC (permalink / raw)
  To: Andrew Morton, Baoquan He
  Cc: Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
	Coiby Xu, Sourabh Jain, kexec@lists.infradead.org,
	linux-kernel@vger.kernel.org

configfs serializes attribute I/O only per open file:
configfs_write_iter() and configfs_read_iter() take the mutex of the
configfs_buffer attached to that file, and flush_write_buffer() takes
frag_sem only for read. Two file descriptors for the same attribute can
therefore call the item's ->show() and ->store() concurrently.

config_key_description_store() frees the old description before
installing a new one, and config_key_description_show() formats the
same pointer without any locking. Concurrent stores can free the same
string twice:

  BUG: KASAN: double-free in config_key_description_store+0x91/0x130
  Free of addr ffff8881085b19c0 by task desc_race/324
   kfree+0x269/0x5e0
   config_key_description_store+0x91/0x130
   configfs_write_iter+0x322/0x4f0
   vfs_write+0x6be/0x11e0

and a store racing with a show frees the string while sysfs_emit() is
reading it:

  BUG: KASAN: slab-use-after-free in string+0x4a9/0x4f0
  Read of size 1 at addr ffff888108f4f100 by task desc_uaf/324
   string+0x4a9/0x4f0
   vsnprintf+0x396/0x12a0
   sysfs_emit+0x110/0x180
   config_key_description_show+0x3f/0x50
   configfs_read_iter+0x3b8/0x660

build_keys_header() also copies the description while kexec_file_load()
assembles a crash kernel image. It runs under neither of the configfs
locks above, so a concurrent store can free the string while it is being
copied:

  BUG: KASAN: slab-use-after-free in strnlen+0x6c/0x80
  Read of size 1 at addr ffff888106160100 by task dmc-check/320
   strnlen+0x6c/0x80
   crash_load_dm_crypt_keys+0x443/0xc10
   bzImage64_load+0xd7f/0x2be0
   __do_sys_kexec_file_load+0xa7f/0x1600

Add a mutex to struct config_key and take it around every access to the
description. Allocate the new description before taking the lock, so
that an allocation failure also keeps the previous description instead
of leaving the key without one.

Fixes: 180cf31af7c3 ("crash_dump: make dm crypt keys persist for the kdump kernel")
Fixes: 479e58549b0f ("crash_dump: store dm crypt keys in kdump reserved memory")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 kernel/crash_dump_dm_crypt.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
index c685497cd470..fcfc05edaadc 100644
--- a/kernel/crash_dump_dm_crypt.c
+++ b/kernel/crash_dump_dm_crypt.c
@@ -6,6 +6,7 @@
 #include <linux/cc_platform.h>
 #include <linux/configfs.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/sysfs.h>
 
 #define KEY_NUM_MAX 128	/* maximum dm crypt keys */
@@ -185,6 +186,7 @@ static int read_key_from_user_keyring(struct dm_crypt_key *dm_key)
 
 struct config_key {
 	struct config_item item;
+	struct mutex lock;	/* protects description */
 	const char *description;
 };
 
@@ -195,13 +197,21 @@ static inline struct config_key *to_config_key(struct config_item *item)
 
 static ssize_t config_key_description_show(struct config_item *item, char *page)
 {
-	return sysfs_emit(page, "%s\n", to_config_key(item)->description);
+	struct config_key *config_key = to_config_key(item);
+	ssize_t ret;
+
+	mutex_lock(&config_key->lock);
+	ret = sysfs_emit(page, "%s\n", config_key->description);
+	mutex_unlock(&config_key->lock);
+
+	return ret;
 }
 
 static ssize_t config_key_description_store(struct config_item *item,
 					    const char *page, size_t count)
 {
 	struct config_key *config_key = to_config_key(item);
+	const char *description;
 	size_t len;
 	int ret;
 
@@ -216,11 +226,14 @@ static ssize_t config_key_description_store(struct config_item *item,
 	if (!len)
 		return ret;
 
+	description = kmemdup_nul(page, len, GFP_KERNEL);
+	if (!description)
+		return -ENOMEM;
+
+	mutex_lock(&config_key->lock);
 	kfree(config_key->description);
-	ret = -ENOMEM;
-	config_key->description = kmemdup_nul(page, len, GFP_KERNEL);
-	if (!config_key->description)
-		return ret;
+	config_key->description = description;
+	mutex_unlock(&config_key->lock);
 
 	return count;
 }
@@ -234,7 +247,10 @@ static struct configfs_attribute *config_key_attrs[] = {
 
 static void config_key_release(struct config_item *item)
 {
-	kfree(to_config_key(item));
+	struct config_key *config_key = to_config_key(item);
+
+	mutex_destroy(&config_key->lock);
+	kfree(config_key);
 	key_count--;
 }
 
@@ -262,6 +278,7 @@ static struct config_item *config_keys_make_item(struct config_group *group,
 	if (!config_key)
 		return ERR_PTR(-ENOMEM);
 
+	mutex_init(&config_key->lock);
 	config_item_init_type_name(&config_key->item, name, &config_key_type);
 
 	key_count++;
@@ -386,13 +403,16 @@ static int build_keys_header(void)
 
 		key = to_config_key(item);
 
+		mutex_lock(&key->lock);
 		if (!key->description) {
+			mutex_unlock(&key->lock);
 			pr_warn("No key description for key %s\n", item->ci_name);
 			return -EINVAL;
 		}
 
 		strscpy(keys_header->keys[i].key_desc, key->description,
 			KEY_DESC_MAX_LEN);
+		mutex_unlock(&key->lock);
 		r = read_key_from_user_keyring(&keys_header->keys[i]);
 		if (r != 0) {
 			kexec_dprintk("Failed to read key %s\n",
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] crash_dump: free dm-crypt key description on release
  2026-12-03  8:24 [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime Yingjie Wang
  2026-12-03  8:24 ` [PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions Yingjie Wang
@ 2026-12-03  8:24 ` Yingjie Wang
  2026-12-03  8:42 ` [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Yingjie Wang @ 2026-12-03  8:24 UTC (permalink / raw)
  To: Andrew Morton, Baoquan He
  Cc: Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
	Coiby Xu, Sourabh Jain, kexec@lists.infradead.org,
	linux-kernel@vger.kernel.org

config_key_release() frees the config_key but not the description
string allocated by config_key_description_store(), so every removed
key directory leaks its description.

Free the description when the item is released.

Fixes: 180cf31af7c3 ("crash_dump: make dm crypt keys persist for the kdump kernel")
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 kernel/crash_dump_dm_crypt.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
index fcfc05edaadc..9f5c2d7c15da 100644
--- a/kernel/crash_dump_dm_crypt.c
+++ b/kernel/crash_dump_dm_crypt.c
@@ -249,6 +249,7 @@ static void config_key_release(struct config_item *item)
 {
 	struct config_key *config_key = to_config_key(item);
 
+	kfree(config_key->description);
 	mutex_destroy(&config_key->lock);
 	kfree(config_key);
 	key_count--;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime
  2026-12-03  8:24 [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime Yingjie Wang
  2026-12-03  8:24 ` [PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions Yingjie Wang
  2026-12-03  8:24 ` [PATCH 2/2] crash_dump: free dm-crypt key description on release Yingjie Wang
@ 2026-12-03  8:42 ` sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03  8:42 UTC (permalink / raw)
  To: Yingjie Wang, Andrew Morton, Baoquan He
  Cc: Mike Rapoport, Pasha Tatashin, Pratyush Yadav, Dave Young,
	Coiby Xu, Sourabh Jain, kexec@lists.infradead.org

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203082410.1165407-1-x-professor@outlook.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03  8:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  8:24 [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime Yingjie Wang
2026-12-03  8:24 ` [PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions Yingjie Wang
2026-12-03  8:24 ` [PATCH 2/2] crash_dump: free dm-crypt key description on release Yingjie Wang
2026-12-03  8:42 ` [PATCH 0/2] crash_dump: fix dm-crypt key description lifetime sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox