From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD9CA33E1 for ; Sun, 4 May 2025 05:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746335727; cv=none; b=pPDI7xGmRh3x+uwNZShs5LBsWxmyOMde3vaizBNr/y0JL/aDMEuz8ib1v5A1V5Ovc4iDQEtuiiSLL3ZOAYBoYAB0F+9vQ9kIi/520BsszQ3stOYDfCICSFJuVbx3aUOGDAH5CfYqanblJn08rYLSYoWMhInmd0GraSFFlTJZpuY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746335727; c=relaxed/simple; bh=zuyDArLgxfQwafO4AUh6QDzReSbK8WTXPwE9MKf3Sww=; h=Date:To:From:Subject:Message-Id; b=SgA+JiypFY7PDZcF10Oixv2jwiQyvvE9fS2nC7UPkRbvQoH1+QgyJbVkJjp13NZ58zV1ezBe4BbpiXikofK70DalL5dlK+Da5Gs3ta0Ix+dyRWgc44e/yMiDcmNwxAndoaqiKT0u1iD7YgPcYU0z4SziVuYjk/WDrVA7zJ/IsqU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=LXtuVsDh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="LXtuVsDh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88DF6C4CEED; Sun, 4 May 2025 05:15:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1746335727; bh=zuyDArLgxfQwafO4AUh6QDzReSbK8WTXPwE9MKf3Sww=; h=Date:To:From:Subject:From; b=LXtuVsDhXD8yr0DR6C3hixP/Zv6wqUNCgBwREUlvHL5tqmZwktzsYVebgyEXpBrqs t+CNLSpQnZedhK7VlqRb/hMjHpfK8sGMFuKI+OZlM6xyJbL1c6Rc/NnmguLqGLX7al VzgYLZcQECFZU2Fa4LAiAU6PSF/OcZNLJE0eDCTY= Date: Sat, 03 May 2025 22:15:26 -0700 To: mm-commits@vger.kernel.org,vkuznets@redhat.com,okozina@redhat.com,kernelfans@gmail.com,jpazdziora@redhat.com,gmazyland@gmail.com,dyoung@redhat.com,dave.hansen@intel.com,bhe@redhat.com,berrange@redhat.com,coxu@redhat.com,akpm@linux-foundation.org From: Andrew Morton Subject: + crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel.patch added to mm-nonmm-unstable branch Message-Id: <20250504051527.88DF6C4CEED@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The patch titled Subject: crash_dump: retrieve dm crypt keys in kdump kernel has been added to the -mm mm-nonmm-unstable branch. Its filename is crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Coiby Xu Subject: crash_dump: retrieve dm crypt keys in kdump kernel Date: Fri, 2 May 2025 09:12:39 +0800 Crash kernel will retrieve the dm crypt keys based on the dmcryptkeys command line parameter. When user space writes the key description to /sys/kernel/config/crash_dm_crypt_key/restore, the crash kernel will save the encryption keys to the user keyring. Then user space e.g. cryptsetup's --volume-key-keyring API can use it to unlock the encrypted device. Link: https://lkml.kernel.org/r/20250502011246.99238-6-coxu@redhat.com Signed-off-by: Coiby Xu Acked-by: Baoquan He Cc: "Daniel P. Berrange" Cc: Dave Hansen Cc: Dave Young Cc: Jan Pazdziora Cc: Liu Pingfan Cc: Milan Broz Cc: Ondrej Kozina Cc: Vitaly Kuznetsov Signed-off-by: Andrew Morton --- include/linux/crash_core.h | 1 include/linux/crash_dump.h | 2 kernel/crash_dump_dm_crypt.c | 133 +++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) --- a/include/linux/crash_core.h~crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel +++ a/include/linux/crash_core.h @@ -36,6 +36,7 @@ static inline void arch_kexec_unprotect_ #ifdef CONFIG_CRASH_DM_CRYPT int crash_load_dm_crypt_keys(struct kimage *image); +ssize_t dm_crypt_keys_read(char *buf, size_t count, u64 *ppos); #else static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; } #endif --- a/include/linux/crash_dump.h~crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel +++ a/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); --- a/kernel/crash_dump_dm_crypt.c~crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel +++ a/kernel/crash_dump_dm_crypt.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,61 @@ static size_t get_keys_header_size(size_ return struct_size(keys_header, keys, total_keys); } +unsigned long long dm_crypt_keys_addr; +EXPORT_SYMBOL_GPL(dm_crypt_keys_addr); + +static int __init setup_dmcryptkeys(char *arg) +{ + char *end; + + if (!arg) + return -EINVAL; + dm_crypt_keys_addr = memparse(arg, &end); + if (end > arg) + return 0; + + dm_crypt_keys_addr = 0; + return -EINVAL; +} + +early_param("dmcryptkeys", setup_dmcryptkeys); + +/* + * Architectures may override this function to read dm crypt keys + */ +ssize_t __weak dm_crypt_keys_read(char *buf, size_t count, u64 *ppos) +{ + struct kvec kvec = { .iov_base = buf, .iov_len = count }; + struct iov_iter iter; + + iov_iter_kvec(&iter, READ, &kvec, 1, count); + return read_from_oldmem(&iter, count, ppos, cc_platform_has(CC_ATTR_MEM_ENCRYPT)); +} + +static int add_key_to_keyring(struct dm_crypt_key *dm_key, + key_ref_t keyring_ref) +{ + key_ref_t key_ref; + int r; + + /* create or update the requested key and add it to the target keyring */ + key_ref = key_create_or_update(keyring_ref, "user", dm_key->key_desc, + dm_key->data, dm_key->key_size, + KEY_USR_ALL, KEY_ALLOC_IN_QUOTA); + + if (!IS_ERR(key_ref)) { + r = key_ref_to_ptr(key_ref)->serial; + key_ref_put(key_ref); + kexec_dprintk("Success adding key %s", dm_key->key_desc); + } else { + r = PTR_ERR(key_ref); + kexec_dprintk("Error when adding key"); + } + + key_ref_put(keyring_ref); + return r; +} + static void get_keys_from_kdump_reserved_memory(void) { struct keys_header *keys_header_loaded; @@ -42,6 +98,47 @@ static void get_keys_from_kdump_reserved arch_kexec_protect_crashkres(); } +static int restore_dm_crypt_keys_to_thread_keyring(void) +{ + struct dm_crypt_key *key; + size_t keys_header_size; + key_ref_t keyring_ref; + u64 addr; + + /* find the target keyring (which must be writable) */ + keyring_ref = + lookup_user_key(KEY_SPEC_USER_KEYRING, 0x01, KEY_NEED_WRITE); + if (IS_ERR(keyring_ref)) { + kexec_dprintk("Failed to get the user keyring\n"); + return PTR_ERR(keyring_ref); + } + + addr = dm_crypt_keys_addr; + dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr); + if (key_count < 0 || key_count > KEY_NUM_MAX) { + kexec_dprintk("Failed to read the number of dm-crypt keys\n"); + return -1; + } + + kexec_dprintk("There are %u keys\n", key_count); + addr = dm_crypt_keys_addr; + + keys_header_size = get_keys_header_size(key_count); + keys_header = kzalloc(keys_header_size, GFP_KERNEL); + if (!keys_header) + return -ENOMEM; + + dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr); + + for (int i = 0; i < keys_header->total_keys; i++) { + key = &keys_header->keys[i]; + kexec_dprintk("Get key (size=%u)\n", key->key_size); + add_key_to_keyring(key, keyring_ref); + } + + return 0; +} + static int read_key_from_user_keying(struct dm_crypt_key *dm_key) { const struct user_key_payload *ukp; @@ -211,6 +308,37 @@ static const struct config_item_type con .ct_owner = THIS_MODULE, }; +static bool restore; + +static ssize_t config_keys_restore_show(struct config_item *item, char *page) +{ + return sprintf(page, "%d\n", restore); +} + +static ssize_t config_keys_restore_store(struct config_item *item, + const char *page, size_t count) +{ + if (!restore) + restore_dm_crypt_keys_to_thread_keyring(); + + if (kstrtobool(page, &restore)) + return -EINVAL; + + return count; +} + +CONFIGFS_ATTR(config_keys_, restore); + +static struct configfs_attribute *kdump_config_keys_attrs[] = { + &config_keys_attr_restore, + NULL, +}; + +static const struct config_item_type kdump_config_keys_type = { + .ct_attrs = kdump_config_keys_attrs, + .ct_owner = THIS_MODULE, +}; + static struct configfs_subsystem config_keys_subsys = { .su_group = { .cg_item = { @@ -311,6 +439,11 @@ static int __init configfs_dmcrypt_keys_ { int ret; + if (is_kdump_kernel()) { + config_keys_subsys.su_group.cg_item.ci_type = + &kdump_config_keys_type; + } + config_group_init(&config_keys_subsys.su_group); mutex_init(&config_keys_subsys.su_mutex); ret = configfs_register_subsystem(&config_keys_subsys); _ Patches currently in -mm which might be from coxu@redhat.com are kexec_file-allow-to-place-kexec_buf-randomly.patch crash_dump-make-dm-crypt-keys-persist-for-the-kdump-kernel.patch crash_dump-store-dm-crypt-keys-in-kdump-reserved-memory.patch crash_dump-reuse-saved-dm-crypt-keys-for-cpu-memory-hot-plugging.patch crash_dump-retrieve-dm-crypt-keys-in-kdump-kernel.patch revert-x86-mm-remove-unused-__set_memory_prot.patch x86-crash-pass-dm-crypt-keys-to-kdump-kernel.patch x86-crash-make-the-page-that-stores-the-dm-crypt-keys-inaccessible.patch