Skip to contentSkip to conversation

crash_dump: fix dm-crypt key description lifetime

PATCH New Yingjie Wang 2 participants 4 messages Dec 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover crash_dump: fix dm-crypt key description lifetime

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/[email protected]

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

Signature · 1 line
2.43.0
#1 lore raw

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

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")
---
 kernel/crash_dump_dm_crypt.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
kernel/crash_dump_dm_crypt.c +26 -6
index c685497cd470..fcfc05edaadc 100644
@@ -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",
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 crash_dump: free dm-crypt key description on release

In reply to Yingjie Wang

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")
---
 kernel/crash_dump_dm_crypt.c | 1 +
 1 file changed, 1 insertion(+)
kernel/crash_dump_dm_crypt.c +1 -0
index fcfc05edaadc..9f5c2d7c15da 100644
@@ -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--;
Signature · 1 line
2.43.0