From nobody Mon Nov 25 09:52:34 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 AE633201002 for ; Tue, 29 Oct 2024 05:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181189; cv=none; b=Dy6S69rNgkgM1Sle0/4Y+cq8yK/7OaNUB8WzsWYPCOFuyLjQ/a+NBkkSS5q1t8gX/PxLtL6blmAqvZwJigUb57eRZN1erq4VuDJKqSw3AKZ3+MW7D40a4YS4XeQPbWgu0TapRu4mGOI5n4BRV8oAK9YQCv5RnUk1cwkk5NS7XJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181189; c=relaxed/simple; bh=amcEsuu1ofuhisISwYDap2aQL+BHS5SoYaLr4f6SoU8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LB2dLeVBYv/i8HX+Fp5Evp3LrP0FyB0LeAKyBJ/12jAJEzN87yEkmjjgxD/w6T7ZWG/GOFkqgkSPOQFUxXIXN66IvLr/LTuX74B74TSV98OS+Cpb+sK8T78uOqRmZnOXwFzkigzq/Wfx1pbaiM83KZi31I4W+vcx0eTQAeBS+UE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JIG/KxcW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JIG/KxcW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qZg6/j2slJCNip4CZzSegjPi2gvUTG55985ztTmoO0Y=; b=JIG/KxcWMdPRasOskxMdOxYitAphD1bsRDdUPtPei70hpZhCCDdkE6n3OPKmhHDF7rPevW egtaTPqNvjN6zcLXtQGOrT34C8mywSS9LKLwyrGrHXNNgraUJ3gvj73CGy1pDq9XdYwHr8 BwjTxE+JV0TevPF72nlMXRCmBDIIV9U= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-589-A9fr-rT7MCuPR2h-xi9UOg-1; Tue, 29 Oct 2024 01:52:59 -0400 X-MC-Unique: A9fr-rT7MCuPR2h-xi9UOg-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2e2cc47ce63so4903911a91.3 for ; Mon, 28 Oct 2024 22:52:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181176; x=1730785976; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qZg6/j2slJCNip4CZzSegjPi2gvUTG55985ztTmoO0Y=; b=KlDbvvimQqYAuGjkQZWKJPKaoBPvZHREiC6GaBqSmsV19gcnchPjVaY5xdyz71zfaK 1TFu2fWhlSAzXrAZggflnQk341p/iaBgDpn1qiP47Zv/dyjW+yIIEanQ6UrvGliPovL4 +LSYDbib3m8T9G1XO1AfuGdxnhMbwoD4JnpXYb56eQZpTRTpvg7Vy9/RHEE3Wqv4fXyN aN0xpj9I/UEPh2An3gVkrmJ6T36NjeeUhiMUd6BxARTMDEv90z3Lg9HDtsdX4e6jKsCc 2hqRP/d7dF7Co3svGM18l8+nVj1VP1AHodYJLqKHwZWz8FmHiF6zdkMGcmbt/OdVWtNi jYTQ== X-Forwarded-Encrypted: i=1; AJvYcCVQXae9ZTHsePt0Flckj+3aHgjE/4mL9st1IANofphBXDob5F07fzRTccNLbw+6Qtjtb8mF3t/7qcawO4M=@vger.kernel.org X-Gm-Message-State: AOJu0YxeLP0JLOOqaGM9N7jsVtnYus4gIsAT02OVxqiPnZ8zSFncRJS6 hh3Yg+J6fk1XXUHzE2N92WyuABgITtS77tkHTzROM0V1m9Hj3J6F7WQ/5+7K+bOWc9qPSex+9xx 9z0svSYKe4DwImFINw6FAwXQlHDSjRCfZVjGZpxxw3szYQhXQBiZMhYWFPdLG6A== X-Received: by 2002:a17:902:f546:b0:20b:9f8c:e9de with SMTP id d9443c01a7336-210c68995ccmr134170115ad.13.1730181176308; Mon, 28 Oct 2024 22:52:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGk9Es5TcHJ+1fDLYHIVVDN7Qi0lRdnOpcpue5a43EpNjkaC+h6H+NJ0KXWLPIin0nJAJGNfg== X-Received: by 2002:a17:902:f546:b0:20b:9f8c:e9de with SMTP id d9443c01a7336-210c68995ccmr134169925ad.13.1730181175900; Mon, 28 Oct 2024 22:52:55 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-210bbf44321sm59445635ad.30.2024.10.28.22.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:55 -0700 (PDT) From: Coiby Xu To: kexec@lists.infradead.org Cc: Ondrej Kozina , Milan Broz , Thomas Staudt , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Kairui Song , Jan Pazdziora , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Greg KH , Vivek Goyal Subject: [PATCH v6 5/7] crash_dump: retrieve dm crypt keys in kdump kernel Date: Tue, 29 Oct 2024 13:52:18 +0800 Message-ID: <20241029055223.210039-6-coxu@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241029055223.210039-1-coxu@redhat.com> References: <20241029055223.210039-1-coxu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Coiby Xu --- include/linux/crash_core.h | 1 + include/linux/crash_dump.h | 2 + kernel/crash_dump_dm_crypt.c | 134 ++++++++++++++++++++++++++++++++++- 3 files changed, 136 insertions(+), 1 deletion(-) diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index 2e6782239034..d35726d6a415 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -36,6 +36,7 @@ static inline void arch_kexec_unprotect_crashkres(void) {= } =20 #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 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; =20 +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/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c index 51431f93fc1e..f72a88b7d106 100644 --- a/kernel/crash_dump_dm_crypt.c +++ b/kernel/crash_dump_dm_crypt.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include =20 @@ -28,6 +29,61 @@ static size_t get_keys_header_size(size_t total_keys) return struct_size(keys_header, keys, total_keys); } =20 +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 =3D memparse(arg, &end); + if (end > arg) + return 0; + + dm_crypt_keys_addr =3D 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 =3D { .iov_base =3D buf, .iov_len =3D 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_E= NCRYPT)); +} + +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 =3D 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 =3D key_ref_to_ptr(key_ref)->serial; + key_ref_put(key_ref); + kexec_dprintk("Success adding key %s", dm_key->key_desc); + } else { + r =3D 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_memory(void) arch_kexec_protect_crashkres(); } =20 +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 =3D + 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 =3D 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 =3D dm_crypt_keys_addr; + + keys_header_size =3D get_keys_header_size(key_count); + keys_header =3D 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 =3D 0; i < keys_header->total_keys; i++) { + key =3D &keys_header->keys[i]; + kexec_dprintk("Get key (size=3D%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 config_keys_type = =3D { .ct_owner =3D THIS_MODULE, }; =20 +static bool restore; + +static ssize_t config_keys_restore_show(struct config_item *item, char *pa= ge) +{ + 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[] =3D { + &config_keys_attr_restore, + NULL, +}; + +static const struct config_item_type kdump_config_keys_type =3D { + .ct_attrs =3D kdump_config_keys_attrs, + .ct_owner =3D THIS_MODULE, +}; + static struct configfs_subsystem config_keys_subsys =3D { .su_group =3D { .cg_item =3D { @@ -302,11 +430,15 @@ int crash_load_dm_crypt_keys(struct kimage *image) return r; } =20 - static int __init configfs_dmcrypt_keys_init(void) { int ret; =20 + if (is_kdump_kernel()) { + config_keys_subsys.su_group.cg_item.ci_type =3D + &kdump_config_keys_type; + } + config_group_init(&config_keys_subsys.su_group); mutex_init(&config_keys_subsys.su_mutex); ret =3D configfs_register_subsystem(&config_keys_subsys); --=20 2.47.0