[PATCH 1/2] crash_dump: serialize access to dm-crypt key descriptions

From: Yingjie Wang

Date: Thu Dec 03 2026 - 03:24:34 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Yingjie Wang <x-professor@xxxxxxxxxxx>
---
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