Re: [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header

From: Coiby Xu

Date: Mon Aug 31 2026 - 10:29:17 EST


On Mon, Aug 31, 2026 at 09:24:07AM +0800, Jinjie Ruan wrote:


在 2026/8/30 13:59, Sourabh Jain 写道:
Hello Jinjie,

Coiby is handling this issue in the below patch series:
https://lore.kernel.org/all/20260828084900.1496839-2-coiby.xu@xxxxxxxxx/

Since the above patch series is all about crash_load_dm_crypt_keys,
could you
please consider dropping this patch from your series and reviewing his
patch
instead?

Absolutely, happy to do so — I'll drop this patch and review Coiby's
series instead.

Hi Jinjie,

Thank you for agreeing to drop your patch and review mine instead!



Thanks,
Sourabh Jain

On 26/08/26 14:55, Jinjie Ruan wrote:
`keys_header` was freed in `build_keys_header()` without being reset
to NULL, and the error path in `crash_load_dm_crypt_keys()` freed it
unconditionally even when reused, leading to double-free or
use-after-free.

Add `free_keys_header()` to centralize freeing and NULL-setting.
Use it in `build_keys_header()` and only free in the error path when
the header was newly built (`!is_dm_key_reused`).

Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Baoquan He <bhe@xxxxxxxxxx>
Cc: Mike Rapoport <rppt@xxxxxxxxxx>
Cc: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
Cc: Pratyush Yadav <pratyush@xxxxxxxxxx>
Cc: Dave Young <ruirui.yang@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Fixes: e3a84be1ec2f ("arm64,ppc64le/kdump: pass dm-crypt keys to kdump
kernel")
Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
  kernel/crash_dump_dm_crypt.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
index c685497cd470..ed0960ff0987 100644
--- a/kernel/crash_dump_dm_crypt.c
+++ b/kernel/crash_dump_dm_crypt.c
@@ -363,15 +363,21 @@ static struct configfs_subsystem
config_keys_subsys = {
      },
  };
  +static void free_keys_header(void)
+{
+    if (keys_header) {
+        kvfree(keys_header);
+        keys_header = NULL;
+    }
+}
+
  static int build_keys_header(void)
  {
      struct config_item *item = NULL;
      struct config_key *key;
      int i, r;
  -    if (keys_header != NULL)
-        kvfree(keys_header);
-
+    free_keys_header();
      keys_header = kzalloc(get_keys_header_size(key_count), GFP_KERNEL);
      if (!keys_header)
          return -ENOMEM;
@@ -441,7 +447,8 @@ int crash_load_dm_crypt_keys(struct kimage *image)
      r = kexec_add_buffer(&kbuf);
      if (r) {
          pr_err("Failed to call kexec_add_buffer, ret=%d\n", r);
-        kvfree((void *)kbuf.buffer);
+        if (!is_dm_key_reused)
+            free_keys_header();
          return r;
      }
      image->dm_crypt_keys_addr = kbuf.mem;



--
Best regards,
Coiby