From: Baoquan He <bhe@redhat.com>
To: Coiby Xu <coxu@redhat.com>
Cc: kexec@lists.infradead.org, "Ondrej Kozina" <okozina@redhat.com>,
"Milan Broz" <gmazyland@gmail.com>,
"Thomas Staudt" <tstaudt@de.ibm.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Kairui Song" <ryncsn@gmail.com>
Subject: Re: [PATCH v3 3/7] crash_dump: store dm keys in kdump reserved memory
Date: Tue, 21 May 2024 11:42:52 +0800 [thread overview]
Message-ID: <ZkwYIf2YgDfSZ1y8@MiWiFi-R3L-srv> (raw)
In-Reply-To: <20240425100434.198925-4-coxu@redhat.com>
On 04/25/24 at 06:04pm, Coiby Xu wrote:
> When the kdump kernel image and initrd are loaded, the dm crypts keys
> will be read from keyring and then stored in kdump reserved memory.
>
> Signed-off-by: Coiby Xu <coxu@redhat.com>
> ---
> include/linux/crash_core.h | 3 ++
> include/linux/crash_dump.h | 2 +
> include/linux/kexec.h | 4 ++
> kernel/crash_dump_dm_crypt.c | 87 ++++++++++++++++++++++++++++++++++++
> 4 files changed, 96 insertions(+)
>
> diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
> index 98825b7e0ea6..1f3d5a4fa6c1 100644
> --- a/include/linux/crash_core.h
> +++ b/include/linux/crash_core.h
> @@ -37,6 +37,9 @@ static inline void arch_kexec_unprotect_crashkres(void) { }
> #ifdef CONFIG_CRASH_DM_CRYPT
> int crash_sysfs_dm_crypt_keys_read(char *buf);
> int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count);
> +int crash_load_dm_crypt_keys(struct kimage *image);
> +#else
> +static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; }
> #endif
>
> #ifndef arch_crash_handle_hotplug_event
> diff --git a/include/linux/crash_dump.h b/include/linux/crash_dump.h
> index acc55626afdc..dfd8e4fe6129 100644
> --- a/include/linux/crash_dump.h
> +++ b/include/linux/crash_dump.h
> @@ -15,6 +15,8 @@
> extern unsigned long long elfcorehdr_addr;
> extern unsigned long long elfcorehdr_size;
>
> +extern unsigned long long dm_crypt_keys_addr;
> +
> #ifdef CONFIG_CRASH_DUMP
> extern int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size);
> extern void elfcorehdr_free(unsigned long long addr);
> diff --git a/include/linux/kexec.h b/include/linux/kexec.h
> index fc1e20d565d5..b6cedce66828 100644
> --- a/include/linux/kexec.h
> +++ b/include/linux/kexec.h
> @@ -368,6 +368,10 @@ struct kimage {
> void *elf_headers;
> unsigned long elf_headers_sz;
> unsigned long elf_load_addr;
> +
> + /* dm crypt keys buffer */
> + unsigned long dm_crypt_keys_addr;
> + unsigned long dm_crypt_keys_sz;
> };
>
> /* kexec interface functions */
> diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
> index 847499cdcd42..b9997fb53351 100644
> --- a/kernel/crash_dump_dm_crypt.c
> +++ b/kernel/crash_dump_dm_crypt.c
> @@ -1,4 +1,6 @@
> // SPDX-License-Identifier: GPL-2.0-only
> +#include <linux/key.h>
> +#include <linux/keyctl.h>
> #include <keys/user-type.h>
> #include <linux/crash_dump.h>
>
> @@ -111,3 +113,88 @@ int crash_sysfs_dm_crypt_keys_read(char *buf)
> return sprintf(buf, "%s\n", STATE_STR[state]);
> }
> EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_read);
> +
> +static int read_key_from_user_keying(struct dm_crypt_key *dm_key)
> +{
> + const struct user_key_payload *ukp;
> + struct key *key;
> +
> + pr_debug("Requesting key %s", dm_key->key_desc);
> + key = request_key(&key_type_logon, dm_key->key_desc, NULL);
> +
> + if (IS_ERR(key)) {
> + pr_warn("No such key %s\n", dm_key->key_desc);
> + return PTR_ERR(key);
> + }
> +
> + ukp = user_key_payload_locked(key);
> + if (!ukp)
> + return -EKEYREVOKED;
> +
> + memcpy(dm_key->data, ukp->data, ukp->datalen);
> + dm_key->key_size = ukp->datalen;
> + pr_debug("Get dm crypt key (size=%u) %s: %8ph\n", dm_key->key_size,
> + dm_key->key_desc, dm_key->data);
> + return 0;
> +}
> +
> +static int build_keys_header(void)
> +{
> + int i, r;
> +
> + for (i = 0; i < key_count; i++) {
> + r = read_key_from_user_keying(&keys_header->keys[i]);
> + if (r != 0) {
> + pr_err("Failed to read key %s\n", keys_header->keys[i].key_desc);
> + return r;
> + }
> + }
> +
> + return 0;
> +}
> +
> +int crash_load_dm_crypt_keys(struct kimage *image)
> +{
> + struct kexec_buf kbuf = {
> + .image = image,
> + .buf_min = 0,
> + .buf_max = ULONG_MAX,
> + .top_down = false,
> + .random = true,
> + };
> +
> + int r;
> +
> + if (state == FRESH)
> + return 0;
> +
> + if (key_count != keys_header->key_count) {
> + pr_err("Only record %u keys (%u in total)\n", key_count,
> + keys_header->key_count);
> + return -EINVAL;
> + }
> +
> + image->dm_crypt_keys_addr = 0;
> + r = build_keys_header();
> + if (r)
> + return r;
> +
> + kbuf.buffer = keys_header;
> + kbuf.bufsz = keys_header_size;
> +
> + kbuf.memsz = kbuf.bufsz;
> + kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
> + r = kexec_add_buffer(&kbuf);
> + if (r) {
> + vfree((void *)kbuf.buffer);
> + return r;
> + }
> + state = LOADED;
> + image->dm_crypt_keys_addr = kbuf.mem;
> + image->dm_crypt_keys_sz = kbuf.bufsz;
> + pr_debug("Loaded dm crypt keys at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
> + image->dm_crypt_keys_addr, kbuf.bufsz, kbuf.bufsz);
Please use kexec_dprintk() instead to print debugging message.
And you don't worry this printing will leak the key position and the
information?
> +
> + return r;
> +}
> --
> 2.44.0
>
next prev parent reply other threads:[~2024-05-21 3:43 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-25 10:04 [PATCH v3 0/7] Support kdump with LUKS encryption by reusing LUKS volume keys Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 1/7] kexec_file: allow to place kexec_buf randomly Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-05-20 6:16 ` Baoquan He
2024-05-20 6:16 ` Baoquan He
2024-05-21 1:58 ` Coiby Xu
2024-05-21 1:58 ` Coiby Xu
2024-05-21 3:13 ` Baoquan He
2024-05-21 3:13 ` Baoquan He
2024-05-24 7:22 ` Coiby Xu
2024-05-24 7:22 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 2/7] crash_dump: make dm crypt keys persist for the kdump kernel Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-04-26 13:10 ` kernel test robot
2024-04-26 13:10 ` kernel test robot
2024-05-21 3:20 ` Baoquan He
2024-05-21 3:20 ` Baoquan He
2024-05-23 5:34 ` Coiby Xu
2024-05-23 5:34 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 3/7] crash_dump: store dm keys in kdump reserved memory Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-05-21 3:42 ` Baoquan He [this message]
2024-05-24 7:38 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 4/7] crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-05-21 3:48 ` Baoquan He
2024-05-21 3:48 ` Baoquan He
2024-05-24 7:40 ` Coiby Xu
2024-05-24 7:40 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 5/7] crash_dump: retrieve dm crypt keys in kdump kernel Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 6/7] x86/crash: pass dm crypt keys to " Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-04-25 10:04 ` [PATCH v3 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible Coiby Xu
2024-04-25 10:04 ` Coiby Xu
2024-05-21 3:51 ` Baoquan He
2024-05-24 7:43 ` Coiby Xu
2024-05-20 6:18 ` [PATCH v3 0/7] Support kdump with LUKS encryption by reusing LUKS volume keys Baoquan He
2024-05-20 6:18 ` Baoquan He
2024-05-21 1:43 ` Coiby Xu
2024-05-21 1:43 ` Coiby Xu
2024-05-21 3:19 ` Baoquan He
2024-05-21 3:19 ` Baoquan He
2024-05-30 9:33 ` Dave Young
2024-05-30 9:33 ` Dave Young
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZkwYIf2YgDfSZ1y8@MiWiFi-R3L-srv \
--to=bhe@redhat.com \
--cc=berrange@redhat.com \
--cc=coxu@redhat.com \
--cc=gmazyland@gmail.com \
--cc=kexec@lists.infradead.org \
--cc=okozina@redhat.com \
--cc=ryncsn@gmail.com \
--cc=tstaudt@de.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.