From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14DFD3DBD76 for ; Fri, 8 May 2026 12:40:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778244037; cv=none; b=jnNW/cQVxju6mYQ81I+ZPTtLJFZ6rTVC38jkH3043sWYbmymS8WD/x7dRjXcQRm22Mm0HpkhBIY+ZHGIunL7TlhW//VmTxcSMpoJoTZuKp2Fvc+cGwEgkJOZPxOg6SwFT5GCVig4CQhjj5TT8y/Mp9ryl5mzWJCEEHZhW0cplCU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778244037; c=relaxed/simple; bh=5Gty2G9YuCZGu2/vItoQNHibAs9Zk5tGQP/LA01LLz8=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mXxb+aeXAn0uqJsyCi5JsYFEPXRo9ivJQzR+EnzJa8/bYUldntm8CgDY7KWtaGIB0VD4T4avhgBJS+sEbkTuUjLR9EOtX6LT1G1OfNM+MVP5YZx5vBKcgX1auEpSQILKZHSff/yxVmZZ5mcLEe1pPB7BmLFxvPdTA8//P+IG+uw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XPjBRU7g; arc=none smtp.client-ip=209.85.215.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XPjBRU7g" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-c8021c8c42fso741606a12.3 for ; Fri, 08 May 2026 05:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778244035; x=1778848835; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:date:from:from:to :cc:subject:date:message-id:reply-to; bh=pq4oubkGSHD3iNhoYVouRXapXaXo+/ohcQ6g5Iy0JNI=; b=XPjBRU7gMZRzq0TAfCIa90FIBkSFtgQ33xaO+ING+LRqQr1r/43nU5bYqhg/e2wuhs QsJRK3Eq5DnmwzIqMqZHjAW865EAo0XQspe+AUKQFviRnM3mc3CQa5oWHgXyBTrrzrnH +HDNw/73HU10WmP84o+06txHr5mTzoBtfdM4Ju2SBV/e8GNrxkZ6LiDsBPl5AyXFRgO4 Qyx5t2485l5f6am5Q3IGXXCYwk0tDhIBwRX4NMKj8WW2V8OeuZ+dRI76W+6nKGv49ycz PsDhV5EchjbrC0wOTwbDDfcbW9v0Z3gKPEddvnpmD50HZi7ZpU4+DAOaCipna3yR8Eh5 rGIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778244035; x=1778848835; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pq4oubkGSHD3iNhoYVouRXapXaXo+/ohcQ6g5Iy0JNI=; b=asGqPcDm5g1OPFtMzDlbY0yPRYw5E0snB8I/sI7NP76raFhA24d79jnQTxX3YdekGN upXNxLDOfIyVKUDN5K64Ea4g1T/PRcK9FGg/SwfWD2VhNo5klVhbma+iKfpEjZ3x2Sir 6VEICRCu6KG/0UTXZ4UWP/fn2yKxilQwFtvKzLOfS0EmIb6JvFIIa69gVNf84Js4Oj4k hUdFidh5WAR0GLnd56UKyFDhmHzIciQ8ZIuw9bMrxExOidV2LrhxlIFfJA2LiEqKsnPs KFPxLVJQHaEgMzsCbAqKl+LeNCZbRtucie4rUxkHAtDnEX/b76m07EVfgRHyvgidk53Y Ii0Q== X-Forwarded-Encrypted: i=1; AFNElJ/lGgvx/gWsa6hNW8S4GevBnefwL7EumrSUG95gRKeWVcyxrbFowUTPW18rDFRSxIwZVmWI9eGHthOTKoY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw52dFYBdqNqH7xm2GM+a8oi7/dzm10U5E/JgMkvGwlbR4LLWdz /WvKbkdur3nnm+dt/157LGnj82N58ZknG9365jBFZAn8iv0qCkdX6r1j X-Gm-Gg: AeBDietgbInRUGUgIP7/nRdRTo9atAMDzvWtuiLNeUw4Qxm5rjfR2XRHEewolieqOpx G5wSUc6fMETjco1pS3SCj5zAEXkyyv3w4VHQ5wX8VLjIzknEJe/G6XK3fe3csQ2Gk7kS+1Cv0oi EZCY9PNA4aGQ32CB7guO1nU7IOT1rV0Yc5ufbZsgh9JdaksEkcoRRqDscpUB67N29KiXe1T6h6t tVG7tXtbLmznjO4KG3AQDh5mdRE60OSUKhfIxn6f82mdnfrVWQuOiLe0rd8ESwMli1PUaFY9WWc TxXcoxM+U2P8ZqKjApNFTTXlePzNGDTuw+1X1G9ISwA2/gWAN5+9AQF035RZixlKSeZh7jTdSkT hRzeB+SCkmqv2Vhn/J2Utx5PQeRsbb/kb6IVX0kF3tEgS9GGeLjRBJP2Smg2D/F6Om/s8PFnhx8 UrQ07ZnPa9IM1DQw2G3es= X-Received: by 2002:a05:6a21:328b:b0:3a2:e8f1:b859 with SMTP id adf61e73a8af0-3aab16e9fc8mr3271432637.28.1778244035114; Fri, 08 May 2026 05:40:35 -0700 (PDT) Received: from localhost ([121.237.249.41]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839682a20ebsm12491875b3a.53.2026.05.08.05.40.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2026 05:40:34 -0700 (PDT) From: Coiby Xu X-Google-Original-From: Coiby Xu Date: Fri, 8 May 2026 20:33:14 +0800 To: Sourabh Jain Cc: kexec@lists.infradead.org, Andrew Morton , Baoquan He , Dave Young , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Coiby Xu , open list Subject: Re: [PATCH v2 2/9] crash_dump: Fix potential double free and UAF of keys_header Message-ID: References: <20260501234342.2518281-1-coiby.xu@gmail.com> <20260501234342.2518281-3-coiby.xu@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Wed, May 06, 2026 at 05:58:31PM +0530, Sourabh Jain wrote: >Hello Coiby, Hi Sourabh, Thanks for reviewing the patch! > >On 02/05/26 05:13, Coiby Xu wrote: >>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") >>Reported-by: Sourabh Jain >>Signed-off-by: Coiby Xu >>--- >> include/linux/kexec.h | 6 ++++ >> kernel/crash_dump_dm_crypt.c | 65 ++++++++++++++++++++++++++---------- >> kernel/kexec_file.c | 2 ++ >> 3 files changed, 56 insertions(+), 17 deletions(-) >> >>diff --git a/include/linux/kexec.h b/include/linux/kexec.h >>index 8a22bc9b8c6c..91256d7ff434 100644 >>--- a/include/linux/kexec.h >>+++ b/include/linux/kexec.h >>@@ -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 */ >>diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c >>index eac4f436a8d4..4d8a3331bbe7 100644 >>--- a/kernel/crash_dump_dm_crypt.c >>+++ b/kernel/crash_dump_dm_crypt.c >>@@ -84,18 +84,25 @@ static int add_key_to_keyring(struct dm_crypt_key *dm_key, >> return r; >> } >>-static void get_keys_from_kdump_reserved_memory(void) >>+static int get_keys_from_kdump_reserved_memory(void) >> { >> struct keys_header *keys_header_loaded; >>+ size_t keys_header_size; >>- arch_kexec_unprotect_crashkres(); >>+ keys_header_size = get_keys_header_size(key_count); >>+ keys_header = kzalloc(keys_header_size, GFP_KERNEL); >>+ if (!keys_header) >>+ return -ENOMEM; >>+ arch_kexec_unprotect_crashkres(); >> keys_header_loaded = kmap_local_page(pfn_to_page( >> kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT)); > >Accessing kexec_crash_image without holding the kexec lock could lead to >undefined behavior. > >I think this section of code should be called by holding kexec lock. > > >>- memcpy(keys_header, keys_header_loaded, get_keys_header_size(key_count)); >>+ memcpy(keys_header, keys_header_loaded, keys_header_size); >> kunmap_local(keys_header_loaded); >> arch_kexec_protect_crashkres(); >>+ >>+ return 0; >> } >> static int restore_dm_crypt_keys_to_thread_keyring(void) >>@@ -283,17 +290,28 @@ 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) >> { >>+ bool val; >>+ int r; >>+ >> if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) { > >The above check should be performed after acquiring the kexec lock. Good suggestion! I'll try to hold the kexec lock in next version. > > > >> kexec_dprintk( >> "dm-crypt keys haven't be saved to crash-reserved memory\n"); >> return -EINVAL; >> } >>- if (kstrtobool(page, &is_dm_key_reused)) >>+ if (kstrtobool(page, &val) || !val) >> return -EINVAL; > >Why can’t we allow the user to set is_dm_key_reused = false and free the >key_header? That way, the next kdump kernel load will recreate the > >For example, a user may add more keys and want the new keys to be included >in the kdump image from next kdump kernel load. Personally, I want to limit the reuse configfs API to the case of CPU/memory hotplug thus to make the code simpler. And for the case of a user adding more keys, I don't think there is a need for using the reuse API. > > >>- if (is_dm_key_reused) >>- get_keys_from_kdump_reserved_memory(); >>+ if (is_dm_key_reused) { >>+ pr_info("Already got dm-crypt keys, please continue with kexec_file_load syscall\n"); >>+ } else { >>+ r = get_keys_from_kdump_reserved_memory(); >>+ if (r) { >>+ pr_warn("Failed to get dm-crypt keys from reserved memory\n"); >>+ return r; >>+ } >>+ is_dm_key_reused = true; >>+ } >> return count; >> } >>@@ -366,9 +384,6 @@ static int build_keys_header(void) >> struct config_key *key; >> int i, r; >>- if (keys_header != NULL) >>- kvfree(keys_header); >>- >> keys_header = kzalloc(get_keys_header_size(key_count), GFP_KERNEL); >> if (!keys_header) >> return -ENOMEM; >>@@ -412,7 +427,7 @@ int crash_load_dm_crypt_keys(struct kimage *image) >> .top_down = false, >> .random = true, >> }; >>- int r; >>+ int r = 0; >> if (key_count <= 0) { >>@@ -421,14 +436,15 @@ int crash_load_dm_crypt_keys(struct kimage *image) >> } >> if (!is_dm_key_reused) { >>- image->dm_crypt_keys_addr = 0; >> r = build_keys_header(); >>- if (r) { >>- pr_err("Failed to build dm-crypt keys header, ret=%d\n", r); >>- return r; >>- } >>+ if (r) >>+ goto out; >> } >>+ /* >>+ * keys_header will be copied to reserver memory later and then be >>+ * cleaned up at the end of kexec_file_load syscall >>+ */ >> kbuf.buffer = keys_header; >> kbuf.bufsz = get_keys_header_size(key_count); >>@@ -438,18 +454,33 @@ 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); >>- return r; >>+ goto out; >> } >>+ >> image->dm_crypt_keys_addr = kbuf.mem; >> image->dm_crypt_keys_sz = kbuf.bufsz; >> kexec_dprintk( >> "Loaded dm crypt keys to kexec_buffer bufsz=0x%lx memsz=0x%lx\n", >> kbuf.bufsz, kbuf.memsz); >>+out: >>+ is_dm_key_reused = false; >> return r; >> } >>+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 >>+ */ >>+ if (!is_dm_key_reused) { >>+ kfree_sensitive(keys_header); >>+ keys_header = NULL; > >Since crash_load_dm_crypt_keys() sets is_dm_key_reused = false, >keys_header will >always be released here, right? Then why is the above free under an if >condition? Thanks for raising the question! This is to prevent "kexec -u" from cleaning up keys_headers because kexec_file_post_load_cleanup_dm_crypt will also be called during "kexec -u". Without the if condition, keys_headers will not be available during reloading. > >IIUC, for the case where CONFIG_CRASH_HOTPLUG is not enabled, this is >how key >restore works: > >After loading the kdump kernel for the first time, the state of the >variables is: > >is_dm_key_reused = false >keys_header = NULL > >For example, if 2 CPUs are hot-removed and kdump is reloaded twice: > >Then the sequence of operations needed to ensure the loaded keys can >be reused is: > >Udev rule triggered on the 1st CPU hotplug: >echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse >Restore the key header from the reserved area >Reload the kdump service/kernel > >Udev rule triggered on the 2nd CPU hotplug: >echo true > /sys/kernel/config/crash_dm_crypt_keys/reuse >Restore the key header from the reserved area >Reload the kdump service/kernel > >What I don’t understand is the need to restore the key header from >crashkernel >memory for every hotplug operation. I think there is no easy way to tell if there is another hotplug and considering hotplug is a rare event. So I guess it's OK to restore the key headers for every hotplug operation. -- Best regards, Coiby