From nobody Mon Nov 25 10:17:33 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 AAFB820102B for ; Tue, 29 Oct 2024 05:52:50 +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=1730181173; cv=none; b=aEsaSDEesuYzm7FyoajZABV6yyCtv2VNcFqL7kKRCSgsjIOre002I8QlPSj+UPMw9ei1L4HyjgmjjwBYD+LwLkDyelHyJemRJ72F/Ik+oSFM+atmYOrMuo4NMjOiz7eY/9T9wyUEQoCBMAuGhqKzAeN2+Inz8jt12IWyyhjF9t8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181173; c=relaxed/simple; bh=WoOyI9TXIoYQuqBxF4j5//164/5wwRcPWG9wIxPqrUI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TE50XfCTa0X2l1quoOjhGSyUFxiQPwbAJnBvA043HirZsWewQ/BBcS3i2gFvAATArCY3Iip+cxc+a9SOtzPup5vcZDKN4UMoxUUP2cuAfuFr8utZCLA5yMvVbFftEjr2HW2wYYTb9QJ1CVPHX0+MY/bfBFO9dhoNitnGt3L+Ag0= 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=MhRiVWKw; 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="MhRiVWKw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181169; 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=Ue6MNdOcnLTaMHu3j/S0yuX7xj3aU0+QyusUiHz2OBc=; b=MhRiVWKwfksR8q5tMRPyW+tCNLjhbu0me8pPuqvNk7E2zkT4SO65eNssy79IMwbL+qPpBl 9XKWoGAluFqHnxVlEMpXUBsClEENUDouLk0uGLuZqjD2r0Vjp8sbyG2IpcUn+Jq7WyVoY2 n7Re2+9U+somvyKvAH1Y5uygtmjDxJg= 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-657-YZiLW-boN7mqpKJQA_0rEQ-1; Tue, 29 Oct 2024 01:52:48 -0400 X-MC-Unique: YZiLW-boN7mqpKJQA_0rEQ-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2e2ca4fb175so5065262a91.3 for ; Mon, 28 Oct 2024 22:52:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181164; x=1730785964; 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=Ue6MNdOcnLTaMHu3j/S0yuX7xj3aU0+QyusUiHz2OBc=; b=g1SWR7WzT1WKhxYV6S9nNJnNtXiRdA+tRfzAUFfpEd8+NDUgsx8Bg1AhDAALlSbnLE PS65ep5FB9hfhOoaLdZXyNTrt8dH/d3XZ5qgYma3UzDgEhHXIsBMB8t6dt3NPvrxyzuV OsLezPDK8wOLY3TgZ/+Z5KZq9eYWUcYyIB8hBXzdbzpStK6RBLkyzuUXNrIKWTvUnXZx 7hSGI1gmGE1p7AJRC6uNkp+fVhWO64kHM2wLIockKFrlL+s7XWmnT/kkD7WwW6rQO1a/ uRyiLQvOgYfUQ97nNanRF/vDB5gCqLQXHap/OFJqLhqlYcyEetNtxYXvs47MSjEvoqGF ZL/g== X-Forwarded-Encrypted: i=1; AJvYcCWUvG9uPqDtQic4cgEZJ+pb+xKXkgMg12d920mZ3TOZNvwfw5PDI68q9Q2eKqpSr30I69waIUuVUmDnOSM=@vger.kernel.org X-Gm-Message-State: AOJu0YyQXKw3kEE145Kb5Fa8MAo6o3FtTUfq1icjrphP5k/1eCEpD9mu Ob9vWltQIsmnGqYUBhk9ul1SToluiiX60NuRGRF8TJtQH1uQg/8gTwYvm2LSuxHD1kNSfr+hTqU WC/wScy8EbLXYSOjpdI4SyglBIS8FM/pwjdpELsCLNRtp/EhuSFewfzOFqZz6qA== X-Received: by 2002:a17:90a:780a:b0:2e0:cac6:15f7 with SMTP id 98e67ed59e1d1-2e8f0d5dc37mr12203231a91.0.1730181164372; Mon, 28 Oct 2024 22:52:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHXNSApOHp5LxPu6upUbkZyjY7lVTpe+dj4/6+cnO8DL3RuJSlwTfDsx4oR9LJijhRuCmJCow== X-Received: by 2002:a17:90a:780a:b0:2e0:cac6:15f7 with SMTP id 98e67ed59e1d1-2e8f0d5dc37mr12203213a91.0.1730181164007; Mon, 28 Oct 2024 22:52:44 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e8e3555c71sm8472340a91.11.2024.10.28.22.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:43 -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 , Eric Biederman , Kees Cook , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by\b) Subject: [PATCH v6 3/7] crash_dump: store dm crypt keys in kdump reserved memory Date: Tue, 29 Oct 2024 13:52:16 +0800 Message-ID: <20241029055223.210039-4-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" 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. Assume a key won't exceed 256 bytes thus MAX_KEY_SIZE=3D256 according to "cryptsetup benchmark". Signed-off-by: Coiby Xu --- include/linux/crash_core.h | 6 +- include/linux/kexec.h | 4 ++ kernel/crash_dump_dm_crypt.c | 129 +++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index 44305336314e..2e6782239034 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -34,7 +34,11 @@ static inline void arch_kexec_protect_crashkres(void) { } static inline void arch_kexec_unprotect_crashkres(void) { } #endif =20 - +#ifdef CONFIG_CRASH_DM_CRYPT +int crash_load_dm_crypt_keys(struct kimage *image); +#else +static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0= ; } +#endif =20 #ifndef arch_crash_handle_hotplug_event static inline void arch_crash_handle_hotplug_event(struct kimage *image, v= oid *arg) { } diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 0dc66ca2506a..5bda0978bab3 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -396,6 +396,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; }; =20 /* kexec interface functions */ diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c index 62a3c47d8b3b..ec2ec2967242 100644 --- a/kernel/crash_dump_dm_crypt.c +++ b/kernel/crash_dump_dm_crypt.c @@ -1,14 +1,62 @@ // SPDX-License-Identifier: GPL-2.0-only +#include +#include #include #include #include #include =20 #define KEY_NUM_MAX 128 /* maximum dm crypt keys */ +#define KEY_SIZE_MAX 256 /* maximum dm crypt key size */ #define KEY_DESC_MAX_LEN 128 /* maximum dm crypt key description size */ =20 static unsigned int key_count; =20 +struct dm_crypt_key { + unsigned int key_size; + char key_desc[KEY_DESC_MAX_LEN]; + u8 data[KEY_SIZE_MAX]; +}; + +static struct keys_header { + unsigned int total_keys; + struct dm_crypt_key keys[] __counted_by(total_keys); +} *keys_header; + +static size_t get_keys_header_size(size_t total_keys) +{ + return struct_size(keys_header, keys, total_keys); +} + +static int read_key_from_user_keying(struct dm_crypt_key *dm_key) +{ + const struct user_key_payload *ukp; + struct key *key; + + kexec_dprintk("Requesting key %s", dm_key->key_desc); + key =3D 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 =3D user_key_payload_locked(key); + if (!ukp) + return -EKEYREVOKED; + + if (ukp->datalen > KEY_SIZE_MAX) { + pr_err("Key size %u exceeds maximum (%u)\n", ukp->datalen, KEY_SIZE_MAX); + return -EINVAL; + } + + memcpy(dm_key->data, ukp->data, ukp->datalen); + dm_key->key_size =3D ukp->datalen; + kexec_dprintk("Get dm crypt key (size=3D%u) %s: %8ph\n", dm_key->key_size, + dm_key->key_desc, dm_key->data); + return 0; +} + struct config_key { struct config_item item; const char *description; @@ -130,6 +178,87 @@ static struct configfs_subsystem config_keys_subsys = =3D { }, }; =20 +static int build_keys_header(void) +{ + struct config_item *item =3D NULL; + struct config_key *key; + int i, r; + + if (keys_header !=3D NULL) + kvfree(keys_header); + + keys_header =3D kzalloc(get_keys_header_size(key_count), GFP_KERNEL); + if (!keys_header) + return -ENOMEM; + + keys_header->total_keys =3D key_count; + + i =3D 0; + list_for_each_entry(item, &config_keys_subsys.su_group.cg_children, + ci_entry) { + if (item->ci_type !=3D &config_key_type) + continue; + + key =3D to_config_key(item); + + strscpy(keys_header->keys[i].key_desc, key->description, + KEY_DESC_MAX_LEN); + r =3D read_key_from_user_keying(&keys_header->keys[i]); + if (r !=3D 0) { + kexec_dprintk("Failed to read key %s\n", + keys_header->keys[i].key_desc); + return r; + } + i++; + kexec_dprintk("Found key: %s\n", item->ci_name); + } + + return 0; +} + +int crash_load_dm_crypt_keys(struct kimage *image) +{ + struct kexec_buf kbuf =3D { + .image =3D image, + .buf_min =3D 0, + .buf_max =3D ULONG_MAX, + .top_down =3D false, + .random =3D true, + }; + int r; + + + if (key_count <=3D 0) { + kexec_dprintk("No dm-crypt keys\n"); + return -EINVAL; + } + + image->dm_crypt_keys_addr =3D 0; + r =3D build_keys_header(); + if (r) + return r; + + kbuf.buffer =3D keys_header; + kbuf.bufsz =3D get_keys_header_size(key_count); + + kbuf.memsz =3D kbuf.bufsz; + kbuf.buf_align =3D ELF_CORE_HEADER_ALIGN; + kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; + r =3D kexec_add_buffer(&kbuf); + if (r) { + kvfree((void *)kbuf.buffer); + return r; + } + image->dm_crypt_keys_addr =3D kbuf.mem; + image->dm_crypt_keys_sz =3D kbuf.bufsz; + kexec_dprintk( + "Loaded dm crypt keys to kexec_buffer bufsz=3D0x%lx memsz=3D0x%lx\n", + kbuf.bufsz, kbuf.bufsz); + + return r; +} + + static int __init configfs_dmcrypt_keys_init(void) { int ret; --=20 2.47.0