From nobody Mon Nov 25 07:59:23 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1A1EE201023 for ; Tue, 29 Oct 2024 05:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181159; cv=none; b=pFHGddePBBzUpChhqzxJPzV5fCWfp8TrlNgGgCkIs1rQU3NR4BO977qQbistCDQACa8fKd8ZjtYVutxRMvbj3GxWs0oV8eEIeSg1wNrJic+ZLKoDhru45V/Ne/U2dFnXJYqHuXnIgGqlPdCpUaABrYD+Ef8ZEH8q9xipW+HFMrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181159; c=relaxed/simple; bh=dyLLggNeeWqOcdpYXsP/Woa7wEigD3m5xP7IGasrprc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UzM91m5WxxBy/OGJECccEKD8QVpVcXjwzhePMs176CsLgM6/RFbTtnsvfGYpTzltrl02u5/p/OeOCWnOHM00WrT/wyH03wX/znhkJM6/TFK0pTXqN18tEW3kc4Vg3w/rj/FAZ1IRnp2WjTsbpjpirzx/XyxgEs/YBrtgUSW+cUY= 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=iC9il/Cr; arc=none smtp.client-ip=170.10.133.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="iC9il/Cr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181156; 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=wfQRV/dv4U0HCeNe7nsPQHTJHsNlbpGtvoqQINu/Ues=; b=iC9il/Cr5DleEYXPXg+pDzowXUQt73nPqq8OrtETJqufAzMieAoVe41/f05sNITuoMIH5U q2HBn/fQ+j2qCp4AR11ZYfdhC7A+nIIQp+ImgxcP0JXk7oBPO39Pdyrj+3weyfteXJBMAg Y+2ff3cCUFwrc19qvTbt4ClDcDco7g8= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-cJz7b4iOPbu_zHnx7j2CNg-1; Tue, 29 Oct 2024 01:52:34 -0400 X-MC-Unique: cJz7b4iOPbu_zHnx7j2CNg-1 Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-7202dfaa53fso7136512b3a.0 for ; Mon, 28 Oct 2024 22:52:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181153; x=1730785953; 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=wfQRV/dv4U0HCeNe7nsPQHTJHsNlbpGtvoqQINu/Ues=; b=T7fuAopVCbV4oMHJM0w9QnvqY8d6bptVmhNIOkdajShJzU4BTBNdtB4mm+qwqXb5ng TjGFDa6uDGpHyf/kk/atxoqu44YB7lQOmGUZN5BPjoGLQ5ax0m5mh5PMVGC9sy65zGie sellATvHSULCGwuZ6Gsw1njPZn3ziUyN94xRW4k/asPEX2Wpr0kfNGDEAhmFM4fdJ43M FPAnUe5jwLmPlIpyDW0GJFT21xXTf53QBxUbb2Ej5Ox5eRSitK9EHwMbzo4eRRLkdq0V 7LxVXEP2/tox+QATomkQMLiQJGtiU1O+vh29Fg8HAVjeFXO454HJwoM7zHmhBSbJLnPh DsRQ== X-Forwarded-Encrypted: i=1; AJvYcCV1aUmWG397bXnaiK+Yq7jbYpoC2XGYBcW9sFENlbSB0VqdSFR+owP5sgXuG0xLvQHl/YbZqbE0teotatc=@vger.kernel.org X-Gm-Message-State: AOJu0YxZTwyRjKj/slrTm/aMxqNXe8bVD1zF2bE3F+u1HXzYNNO/ucJU j5jJLFjPG4C6kYug8z2bZpARDzta8ZDF1f0RVT5MPiAIU53hYk8pP54ISdkuQf4mNli6Mj+Xpk1 XDKtm45Jdvnvc/r2g+wa+EpKDGMpicAVGt38+ggN+S4PmRGM/OnOnp5SG90VDPg== X-Received: by 2002:a05:6a00:cca:b0:71e:64fe:965f with SMTP id d2e1a72fcca58-72063072a39mr16213213b3a.20.1730181153484; Mon, 28 Oct 2024 22:52:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLxIlu9DkjWvKRHGMJ0mqkMLya+w2yF4n0UBh5fuzAHKNAUx2a3Ph3LsR4pd+vmQ0zxRU0oQ== X-Received: by 2002:a05:6a00:cca:b0:71e:64fe:965f with SMTP id d2e1a72fcca58-72063072a39mr16213193b3a.20.1730181153128; Mon, 28 Oct 2024 22:52:33 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057a21b79sm6906605b3a.176.2024.10.28.22.52.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:32 -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 , Eric Biederman Subject: [PATCH v6 1/7] kexec_file: allow to place kexec_buf randomly Date: Tue, 29 Oct 2024 13:52:14 +0800 Message-ID: <20241029055223.210039-2-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" Currently, kexec_buf is placed in order which means for the same machine, the info in the kexec_buf is always located at the same position each time the machine is booted. This may cause a risk for sensitive information like LUKS volume key. Now struct kexec_buf has a new field random which indicates it's supposed to be placed in a random position. Suggested-by: Jan Pazdziora Signed-off-by: Coiby Xu --- include/linux/kexec.h | 28 ++++++++++++++++++++++++++++ kernel/kexec_file.c | 3 +++ 2 files changed, 31 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..0dc66ca2506a 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -25,6 +25,10 @@ =20 extern note_buf_t __percpu *crash_notes; =20 +#ifdef CONFIG_CRASH_DUMP +#include +#endif + #ifdef CONFIG_KEXEC_CORE #include #include @@ -171,6 +175,7 @@ int kexec_image_post_load_cleanup_default(struct kimage= *image); * @buf_min: The buffer can't be placed below this address. * @buf_max: The buffer can't be placed above this address. * @top_down: Allocate from top of memory. + * @random: Place the buffer at a random position. */ struct kexec_buf { struct kimage *image; @@ -182,8 +187,31 @@ struct kexec_buf { unsigned long buf_min; unsigned long buf_max; bool top_down; +#ifdef CONFIG_CRASH_DUMP + bool random; +#endif }; =20 + +#ifdef CONFIG_CRASH_DUMP +static inline void kexec_random_start(unsigned long start, unsigned long e= nd, + struct kexec_buf *kbuf, + unsigned long *temp_start) +{ + unsigned short i; + + if (kbuf->random) { + get_random_bytes(&i, sizeof(unsigned short)); + *temp_start =3D start + (end - start) / USHRT_MAX * i; + } +} +#else +static inline void kexec_random_start(unsigned long start, unsigned long e= nd, + struct kexec_buf *kbuf, + unsigned long *temp_start) +{} +#endif + int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf); int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, void *buf, unsigned int size, diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3eedb8c226ad..06565d867b69 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -445,6 +445,7 @@ static int locate_mem_hole_top_down(unsigned long start= , unsigned long end, =20 temp_end =3D min(end, kbuf->buf_max); temp_start =3D temp_end - kbuf->memsz + 1; + kexec_random_start(temp_start, temp_end, kbuf, &temp_start); =20 do { /* align down start */ @@ -483,6 +484,8 @@ static int locate_mem_hole_bottom_up(unsigned long star= t, unsigned long end, =20 temp_start =3D max(start, kbuf->buf_min); =20 + kexec_random_start(temp_start, end, kbuf, &temp_start); + do { temp_start =3D ALIGN(temp_start, kbuf->buf_align); temp_end =3D temp_start + kbuf->memsz - 1; --=20 2.47.0 From nobody Mon Nov 25 07:59:23 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 B9D5F20101C for ; Tue, 29 Oct 2024 05:52:47 +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=1730181170; cv=none; b=A/SDifYuTvm+44sBVgx1h3oHUPFUj2j7/5bilKLRjbHNxnOza4GU3LgdsChsGlpDAAikKEI951eCcWncoLelTzmod+2FECHbg2WclBBeVwWcqRi5HRINJJcfOpjOZCZGMkaUvvd0DTGbafBpRAilq5nQMODus9F0GJY7Lit4PnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181170; c=relaxed/simple; bh=PvqoVwtrQdEEGP9Jzyf0mJOhNlqbvegQzqnz0S/J1CU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fXbAHQMQdtplLo4akprLYbW22Rw1e1ZtjXySb2drW3cSsDCs9v74Su55MEiX/rwzmkA6Noedcw8UqKNRZF1N4VtlyHq0Qz8EwM0XT4C9KkUIiBkjlGfiJ56jp6fXkfxrHnOjhJhhQ6w5e13iTvU3gOXk63XdagLTD3oM285+iPU= 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=ilByqf1z; 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="ilByqf1z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181166; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zu6hE/UbIftfaQR7QgBZda/S19K5fTxOQ48ZhHGy+/E=; b=ilByqf1zdCoe9483CEoyMufMrHSov4QEyHAF+smkCRaVGYDNxS/KxbbPlme0odbouSZlde KYyN4N41WJo36zctdQSXfkVgtmHxZGDayOfcme5HKGs+t1iCZ9ZQtYEJ9xaLroHH03EV43 BgOAPns8SjDL7Dyn15LUA07kPgXfbXU= Received: from mail-oo1-f70.google.com (mail-oo1-f70.google.com [209.85.161.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-0gRLHU5cMLioA0eCLKv0lw-1; Tue, 29 Oct 2024 01:52:39 -0400 X-MC-Unique: 0gRLHU5cMLioA0eCLKv0lw-1 Received: by mail-oo1-f70.google.com with SMTP id 006d021491bc7-5ebb90bcb4eso4440833eaf.2 for ; Mon, 28 Oct 2024 22:52:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181159; x=1730785959; 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=zu6hE/UbIftfaQR7QgBZda/S19K5fTxOQ48ZhHGy+/E=; b=YTF0o9NHDg4hkzZO0WOioFFmeInIlDc7iGFyV2w9SRlvU18qdtNeIGRixeyCvlqNPq UNBrbuwmBFAERC4GiSIqCIT+i/SBQmNDYyV3O5q91PDjQyDIYThyo9clzJpxAXmeFCsS cOwUJKv2zN4yvJ9bDJIt1gUk+zFZuOFTLd4WQRbxDeCa+XjzASWuQHIB3jA1GNsGK3n+ Zke9zWbTP8BG7XJVxmvRY4r/KMuDmnpdy6Ri1C7LfZIZD5KrylN362FO91C9MuJaRlZM /Rbb4ct5+X2lJUF55BonAmX4Ssmc9gWA+qtejCmPxU6urEPDKT73qEdk4gGXx23KQylT ONQA== X-Forwarded-Encrypted: i=1; AJvYcCUEPUVgN0EmYrP1J8wgfNkduy/q/WfxsMEXEwTT5ubEFOfrJXgqfF0nfTBvEZoeyZcLzmkNWnOhAmK8684=@vger.kernel.org X-Gm-Message-State: AOJu0YzVYK+RI+Tz8Kp7dMBO8Y2z4DEKQz7vmdClmmYw8pKbB/CqSPl5 n7mu0X8hRYk3s6CLtlPhgUWfHdOYhAPtAEv6TIk4d5ohKTllaT3Xqz5IR2r2hr1r0SscusrTfLc l+FNLO7UbBrkXtY4tv6wdH2j24YQ9NkQhKW4uFpgI4ERNtuUT88UyO8YTxkxm/w== X-Received: by 2002:a05:6358:70e:b0:1c3:d56f:75d5 with SMTP id e5c5f4694b2df-1c3f9ebd7bamr405152255d.12.1730181158955; Mon, 28 Oct 2024 22:52:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIeyDZ4qibE0q70t5rCmLQFuJDwF14hYHZlaTWK1aizOjK1w3KufvbIODFSuq+EGJJ77esMg== X-Received: by 2002:a05:6358:70e:b0:1c3:d56f:75d5 with SMTP id e5c5f4694b2df-1c3f9ebd7bamr405151255d.12.1730181158544; Mon, 28 Oct 2024 22:52:38 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7edc8679ce9sm6759853a12.29.2024.10.28.22.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:38 -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 2/7] crash_dump: make dm crypt keys persist for the kdump kernel Date: Tue, 29 Oct 2024 13:52:15 +0800 Message-ID: <20241029055223.210039-3-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable A configfs /sys/kernel/config/crash_dm_crypt_keys is provided for user space to make the dm crypt keys persist for the kdump kernel. Take the case of dumping to a LUKS-encrypted target as an example, here is the life cycle of the kdump copies of LUKS volume keys, 1. After the 1st kernel loads the initramfs during boot, systemd uses an user-input passphrase to de-crypt the LUKS volume keys or simply TPM-sealed volume keys and then save the volume keys to specified keyring (using the --link-vk-to-keyring API) and the keys will expire within specified time. 2. A user space tool (kdump initramfs loader like kdump-utils) create key items inside /sys/kernel/config/crash_dm_crypt_keys to inform the 1st kernel which keys are needed. 3. When the kdump initramfs is loaded by the kexec_file_load syscall, the 1st kernel will iterate created key items, save the keys to kdump reserved memory. 4. When the 1st kernel crashes and the kdump initramfs is booted, the kdump initramfs asks the kdump kernel to create a user key using the key stored in kdump reserved memory by writing yes to /sys/kernel/crash_dm_crypt_keys/restore. Then the LUKS encrypted device is unlocked with libcryptsetup's --volume-key-keyring API. 5. The system gets rebooted to the 1st kernel after dumping vmcore to the LUKS encrypted device is finished Eventually the keys have to stay in the kdump reserved memory for the kdump kernel to unlock encrypted volumes. During this process, some measures like letting the keys expire withing specified time are desirable to reduce security risk. This patch assumes, 1) there are 128 LUKS devices at maximum to be unlocked thus MAX_KEY_NUM=3D= 128. 2) a key description won't exceed 128 bytes thus KEY_DESC_MAX_LEN=3D128. And here is a demo on how to interact with /sys/kernel/config/crash_dm_crypt_keys, # Add key #1 mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-74= 26b0996720 # Add key #1's description echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/conf= ig/crash_dm_crypt_keys/description # how many keys do we have now? cat /sys/kernel/config/crash_dm_crypt_keys/count 1 # Add key#2 in the same way # how many keys do we have now? cat /sys/kernel/config/crash_dm_crypt_keys/count 2 # the tree structure of /crash_dm_crypt_keys configfs tree /sys/kernel/config/crash_dm_crypt_keys/ /sys/kernel/config/crash_dm_crypt_keys/ =E2=94=9C=E2=94=80=E2=94=80 7d26b7b4-e342-4d2d-b660-7426b0996720 =E2=94=82=C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 description =E2=94=9C=E2=94=80=E2=94=80 count =E2=94=9C=E2=94=80=E2=94=80 fce2cd38-4d59-4317-8ce2-1fd24d52c46a =E2=94=82=C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 description Signed-off-by: Coiby Xu --- kernel/Kconfig.kexec | 9 ++ kernel/Makefile | 1 + kernel/crash_dump_dm_crypt.c | 154 +++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 kernel/crash_dump_dm_crypt.c diff --git a/kernel/Kconfig.kexec b/kernel/Kconfig.kexec index 6c34e63c88ff..1349878d6565 100644 --- a/kernel/Kconfig.kexec +++ b/kernel/Kconfig.kexec @@ -116,6 +116,15 @@ config CRASH_DUMP For s390, this option also enables zfcpdump. See also =20 +config CRASH_DM_CRYPT + bool "Support saving crash dump to dm-crypt encrypted volume" + depends on CRASH_DUMP + depends on DM_CRYPT + help + With this option enabled, user space can intereact with + /sys/kernel/config/crash_dm_crypt_keys to make the dm crypt keys + persistent for the crash dump kernel. + config CRASH_HOTPLUG bool "Update the crash elfcorehdr on system configuration changes" default y diff --git a/kernel/Makefile b/kernel/Makefile index 87866b037fbe..9d1cabf1ec46 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -72,6 +72,7 @@ obj-$(CONFIG_VMCORE_INFO) +=3D vmcore_info.o elfcorehdr.o obj-$(CONFIG_CRASH_RESERVE) +=3D crash_reserve.o obj-$(CONFIG_KEXEC_CORE) +=3D kexec_core.o obj-$(CONFIG_CRASH_DUMP) +=3D crash_core.o +obj-$(CONFIG_CRASH_DM_CRYPT) +=3D crash_dump_dm_crypt.o obj-$(CONFIG_KEXEC) +=3D kexec.o obj-$(CONFIG_KEXEC_FILE) +=3D kexec_file.o obj-$(CONFIG_KEXEC_ELF) +=3D kexec_elf.o diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c new file mode 100644 index 000000000000..62a3c47d8b3b --- /dev/null +++ b/kernel/crash_dump_dm_crypt.c @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include + +#define KEY_NUM_MAX 128 /* maximum dm crypt keys */ +#define KEY_DESC_MAX_LEN 128 /* maximum dm crypt key description size */ + +static unsigned int key_count; + +struct config_key { + struct config_item item; + const char *description; +}; + +static inline struct config_key *to_config_key(struct config_item *item) +{ + return container_of(item, struct config_key, item); +} + +static ssize_t config_key_description_show(struct config_item *item, char = *page) +{ + return sprintf(page, "%s\n", to_config_key(item)->description); +} + +static ssize_t config_key_description_store(struct config_item *item, + const char *page, size_t count) +{ + struct config_key *config_key =3D to_config_key(item); + size_t len; + int ret; + + ret =3D -EINVAL; + len =3D strcspn(page, "\n"); + + if (len > KEY_DESC_MAX_LEN) { + pr_err("The key description shouldn't exceed %u characters", KEY_DESC_MA= X_LEN); + return ret; + } + + if (!len) + return ret; + + kfree(config_key->description); + ret =3D -ENOMEM; + config_key->description =3D kmemdup_nul(page, len, GFP_KERNEL); + if (!config_key->description) + return ret; + + return count; +} + +CONFIGFS_ATTR(config_key_, description); + +static struct configfs_attribute *config_key_attrs[] =3D { + &config_key_attr_description, + NULL, +}; + +static void config_key_release(struct config_item *item) +{ + kfree(to_config_key(item)); + key_count--; +} + +static struct configfs_item_operations config_key_item_ops =3D { + .release =3D config_key_release, +}; + +static const struct config_item_type config_key_type =3D { + .ct_item_ops =3D &config_key_item_ops, + .ct_attrs =3D config_key_attrs, + .ct_owner =3D THIS_MODULE, +}; + +static struct config_item *config_keys_make_item(struct config_group *grou= p, + const char *name) +{ + struct config_key *config_key; + + if (key_count > KEY_NUM_MAX) { + pr_err("Only %u keys at maximum to be created\n", KEY_NUM_MAX); + return ERR_PTR(-EINVAL); + } + + config_key =3D kzalloc(sizeof(struct config_key), GFP_KERNEL); + if (!config_key) + return ERR_PTR(-ENOMEM); + + config_item_init_type_name(&config_key->item, name, &config_key_type); + + key_count++; + + return &config_key->item; +} + +static ssize_t config_keys_count_show(struct config_item *item, char *page) +{ + return sprintf(page, "%d\n", key_count); +} + +CONFIGFS_ATTR_RO(config_keys_, count); + +static struct configfs_attribute *config_keys_attrs[] =3D { + &config_keys_attr_count, + NULL, +}; + +/* + * Note that, since no extra work is required on ->drop_item(), + * no ->drop_item() is provided. + */ +static struct configfs_group_operations config_keys_group_ops =3D { + .make_item =3D config_keys_make_item, +}; + +static const struct config_item_type config_keys_type =3D { + .ct_group_ops =3D &config_keys_group_ops, + .ct_attrs =3D config_keys_attrs, + .ct_owner =3D THIS_MODULE, +}; + +static struct configfs_subsystem config_keys_subsys =3D { + .su_group =3D { + .cg_item =3D { + .ci_namebuf =3D "crash_dm_crypt_keys", + .ci_type =3D &config_keys_type, + }, + }, +}; + +static int __init configfs_dmcrypt_keys_init(void) +{ + int ret; + + config_group_init(&config_keys_subsys.su_group); + mutex_init(&config_keys_subsys.su_mutex); + ret =3D configfs_register_subsystem(&config_keys_subsys); + if (ret) { + pr_err("Error %d while registering subsystem %s\n", ret, + config_keys_subsys.su_group.cg_item.ci_namebuf); + goto out_unregister; + } + + return 0; + +out_unregister: + configfs_unregister_subsystem(&config_keys_subsys); + + return ret; +} + +module_init(configfs_dmcrypt_keys_init); --=20 2.47.0 From nobody Mon Nov 25 07:59:23 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 From nobody Mon Nov 25 07:59:23 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 34C9020127A for ; Tue, 29 Oct 2024 05:52:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181177; cv=none; b=rcvi+XmKN7tgSn4btJ1Azrijc8fnZrn9KG5aiIyirBn36yXUW/HuMSbtDxXWm/QByyaxCGmqHcjq4EZlbt038NoU8w0rP8IvR2H8sgU/5VWwmDOEH6R4kXnTn0inPOw4IOOF/KjHo2Kq/nmA82NsebB3e3VXC6bfQntvnG+mFAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181177; c=relaxed/simple; bh=l6XG8mRUC//SlLNTRFRg+qa2UDiqGVwkf0ym7z2vsXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pjO158cP0FHOjGqDPo1jMxjjrVNNNQRvQHUvBesI44s+cGjzQBkighI6Pwb4YZvUeg1HbSMTXCAQ7HgQakwVNoKXDwXXQvA13FunFkecDJhYUa86leafYMBIaNA98UymHkP97uqKdOu5oKHlFKhSVXe4AL2lfb9seXNuW4fO9AA= 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=R/hFyasG; arc=none smtp.client-ip=170.10.133.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="R/hFyasG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181174; 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=0FowNBWjWbVItvQ2T5CYr3SncrK/lJISla1OHhnStC4=; b=R/hFyasGJi8y/lISiH/K6VTyu2kq+6Ek6gv3Y3WuqSVuYZrmAvyW9JAHYicOjkeaBgxvW1 ydQQoxyaHbzN/RLkGUti9XA5I30DeeVo4QaUtneDXGFJv3AVtyGJDja0kaTfH+PneUb1Jw H2N1FFi1lqF6mPo8pgVfNFezjhoAD+w= Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-VbDpjaU5NQKIM7UfHUVLlA-1; Tue, 29 Oct 2024 01:52:52 -0400 X-MC-Unique: VbDpjaU5NQKIM7UfHUVLlA-1 Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2e2e146c77cso6329824a91.3 for ; Mon, 28 Oct 2024 22:52:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181172; x=1730785972; 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=0FowNBWjWbVItvQ2T5CYr3SncrK/lJISla1OHhnStC4=; b=pyrzxlSebMwIzN0mvtZ6d6QahXjnNEJ845dcSk2DhujDTz2TXzCg3wzfO1EwJoxToa kPoRTfxgQ2Izk9MPlAHiue0AqdyKXQfPpvEr1faHZ8fArSVMo0TAsNtMt2eGfzuoXM0g 1j9ASiiuRol+phcsYsC60B17D5Cz8MQzMdyL/yFn/t/18Zr50jBS74tKB+j0GNJdo1La gQfsdtzbCEbP/xkFvVlMz8JEiVyi4IAk2RYzAa0wM2WOclIHHM0PA5bsVjsfv6N5PGOz z8FtDnECzDymLcpXFKRZTCWgveae7IOK9LNqUQwPDOLJZdcHRAqJtr0YJ+mTOR5xar9k kkQg== X-Forwarded-Encrypted: i=1; AJvYcCWfmNsRh0Po0uFZ7vbvBNB8Yog6tLLOPEfLr1pQeLVBtMCzpezJe3iol33ws+E/z+hpq+b8KI/+L0YcWnQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzOpiynkpG2W/cN6UdlFad4DWSRAgGDSmTLclTs8rpBOJdxF90S AeFM1zJYRAEjQRHS8yHte0pnwiq2ZQOHz3yxGSlXfoPAPqLZvi/wd38F3lnHQmmsVg/NJHDz85G VfPdYDNz1DLCmwxkuVNAaGUtfqP+KXiBIorWUcW3MDLf+mGlQPFkw6DmMJjF3ZA== X-Received: by 2002:a17:90b:4c8c:b0:2e2:9522:278f with SMTP id 98e67ed59e1d1-2e8f11ab132mr12515455a91.32.1730181171801; Mon, 28 Oct 2024 22:52:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEaZ9dQJ+Cd+rSku/B/Cyoe7dof5e0gGIxGIvrF0pzJfr4i782JDqBryN6BPGY3NXt1ZlWUyw== X-Received: by 2002:a17:90b:4c8c:b0:2e2:9522:278f with SMTP id 98e67ed59e1d1-2e8f11ab132mr12515437a91.32.1730181171510; Mon, 28 Oct 2024 22:52:51 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e77e48bb51sm10510164a91.11.2024.10.28.22.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:49 -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 4/7] crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging Date: Tue, 29 Oct 2024 13:52:17 +0800 Message-ID: <20241029055223.210039-5-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 there are CPU and memory hot un/plugs, the dm crypt keys may need to be reloaded again depending on the solution for crash hotplug support. Currently, there are two solutions. One is to utilizes udev to instruct user space to reload the kdump kernel image and initrd, elfcorehdr and etc again. The other is to only update the elfcorehdr segment introduced in commit 247262756121 ("crash: add generic infrastructure for crash hotplug support"). For the 1st solution, the dm crypt keys need to be reloaded again. The user space can write true to /sys/kernel/config/crash_dm_crypt_key/reuse so the stored keys can be re-used. For the 2nd solution, the dm crypt keys don't need to be reloaded. Currently, only x86 supports the 2nd solution. If the 2nd solution gets extended to all arches, this patch can be dropped. Signed-off-by: Coiby Xu --- kernel/crash_dump_dm_crypt.c | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c index ec2ec2967242..51431f93fc1e 100644 --- a/kernel/crash_dump_dm_crypt.c +++ b/kernel/crash_dump_dm_crypt.c @@ -28,6 +28,20 @@ static size_t get_keys_header_size(size_t total_keys) return struct_size(keys_header, keys, total_keys); } =20 +static void get_keys_from_kdump_reserved_memory(void) +{ + struct keys_header *keys_header_loaded; + + arch_kexec_unprotect_crashkres(); + + keys_header_loaded =3D kmap_local_page(pfn_to_page( + kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT)); + + memcpy(keys_header, keys_header_loaded, get_keys_header_size(key_count)); + kunmap_local(keys_header_loaded); + arch_kexec_protect_crashkres(); +} + static int read_key_from_user_keying(struct dm_crypt_key *dm_key) { const struct user_key_payload *ukp; @@ -150,8 +164,36 @@ static ssize_t config_keys_count_show(struct config_it= em *item, char *page) =20 CONFIGFS_ATTR_RO(config_keys_, count); =20 +static bool reuse; + +static ssize_t config_keys_reuse_show(struct config_item *item, char *page) +{ + return sprintf(page, "%d\n", reuse); +} + +static ssize_t config_keys_reuse_store(struct config_item *item, + const char *page, size_t count) +{ + if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) { + kexec_dprintk( + "dm-crypt keys haven't be saved to crash-reserved memory\n"); + return -EINVAL; + } + + if (kstrtobool(page, &reuse)) + return -EINVAL; + + if (reuse) + get_keys_from_kdump_reserved_memory(); + + return count; +} + +CONFIGFS_ATTR(config_keys_, reuse); + static struct configfs_attribute *config_keys_attrs[] =3D { &config_keys_attr_count, + &config_keys_attr_reuse, NULL, }; =20 @@ -233,10 +275,12 @@ int crash_load_dm_crypt_keys(struct kimage *image) return -EINVAL; } =20 - image->dm_crypt_keys_addr =3D 0; - r =3D build_keys_header(); - if (r) - return r; + if (!reuse) { + image->dm_crypt_keys_addr =3D 0; + r =3D build_keys_header(); + if (r) + return r; + } =20 kbuf.buffer =3D keys_header; kbuf.bufsz =3D get_keys_header_size(key_count); --=20 2.47.0 From nobody Mon Nov 25 07:59:24 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 From nobody Mon Nov 25 07:59:24 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 6923C201005 for ; Tue, 29 Oct 2024 05:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181188; cv=none; b=RSsP4RyC+GUPZACBeR1501TGwZCWI9BYgoZhxST210I0MegZdpZnYUYXK5WVVyjfGHfvMavnYYu3giIC6kxRve5Hkwmay9FxtdRaA8N0WU1JExsqoqYUdh6eeV5u1cPecjIekwjjRYRPmSDol4J0r5Tojmnx7d7gC+czlKg3kqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181188; c=relaxed/simple; bh=OGf1IBa2xW8q/phmDcwzFethFRTNER1r47OWYEHFXh0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fHl2puyOQJtLb3PMpuX2zTtXYLHBvNYZ7+L/qQKZOPpDQkX27I8pOnAZ85Kzk7yKc5sCuAuTxJVzrTrmESseThUyQ7VNowfo0w+4fV9zhu1DQJbE2tLvm2nGUofjoHLYZclSEV+qMk8BoFS+Toq2wwyCnCQCA/1BRwPGOSHXW1A= 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=AgMDRuHG; arc=none smtp.client-ip=170.10.133.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="AgMDRuHG" 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=yxN33tdMukhisw/yIqoyNXhrPQQ42HlFh3ca/0Qlc2k=; b=AgMDRuHG0p36RTHgA33nLxhId1htH3rj7wPCCX8AoRCH8hjVgBfAWuucQBqBh1n825GiGW Md2RTelEypK0mVa/QiuLpGNVQt+Ts+5+h5MZuQrNsYkqM9CGU7ZknRX06wzpml6uQ+uGKv UY4sZF85brueeXy3ABbSJywQg0UXShA= Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-X4smrvfBPPi6LX0K1I3B0g-1; Tue, 29 Oct 2024 01:53:03 -0400 X-MC-Unique: X4smrvfBPPi6LX0K1I3B0g-1 Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-7eb07db7812so3740442a12.0 for ; Mon, 28 Oct 2024 22:53:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181182; x=1730785982; 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=yxN33tdMukhisw/yIqoyNXhrPQQ42HlFh3ca/0Qlc2k=; b=OhNkxy7wbIRW2+FVk5D/XrDYeK6vmTPqyDDA1wrAXi0CRBSu+cOwm0GlUaCMaNkXjZ 43tKO7V84KZofmWRK2IpxFwN9dF7e1t1mRWU/FYVEfrNad91sf2u8utdSouOsLhCQSqx ZOh/zbFVu3f+dtTcOMs20Dkwf/wZ19Ra7QrXTAI0Q1jWazGfrH2k9ethsoiihKusaEft mAP6KWXQaZDPSC28tkHp/ClLKsadyvoMhasntDa9OE1S5D7EhSRdHvY9vV4jou3iuGfG BwsWZtecCIPEiPKkAfUww4837iNy6kTGfiNtHwJPxDze6p3evAelynAQcoA9qclMHEPb PI5g== X-Forwarded-Encrypted: i=1; AJvYcCWufFgUZlInkvxW2EmVFKNBy5i9VihKbbgjzVRTQS0mIER/F/Xbeg2ej+1VvSO94zXrByqEQcxeCtt4xu4=@vger.kernel.org X-Gm-Message-State: AOJu0Yyt8sEV6r1DjgAYyvX/XQRhmDodd1/KFUOeSP7FX+wBbaUo0zpV MkGWQjjsyZQjGDr4lMuUhd1OGa/bFzE60sCH8hvKHXf6RCdkBF4IxvIFXz4eyPz9xIq7QB4gUFu ZZGfn4sMLeXS+FpaGVbVY/OXKoUfi4Iur0RJGluiSUhtlUJHQZev2oJY7Mv/umA== X-Received: by 2002:a05:6a20:1594:b0:1d9:1661:56b8 with SMTP id adf61e73a8af0-1d9e1a37b6cmr1403781637.24.1730181182368; Mon, 28 Oct 2024 22:53:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+5lOO2h8SrB779IPVLSPIhRIwwoXPjJUnB9hq/AGjJ6AhPouE5mbdohA59r2JGo+YR4/Uog== X-Received: by 2002:a05:6a20:1594:b0:1d9:1661:56b8 with SMTP id adf61e73a8af0-1d9e1a37b6cmr1403764637.24.1730181181979; Mon, 28 Oct 2024 22:53:01 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7edc8a39b15sm6630484a12.79.2024.10.28.22.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:52:59 -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 , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v6 6/7] x86/crash: pass dm crypt keys to kdump kernel Date: Tue, 29 Oct 2024 13:52:19 +0800 Message-ID: <20241029055223.210039-7-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" 1st kernel will build up the kernel command parameter dmcryptkeys as similar to elfcorehdr to pass the memory address of the stored info of dm crypt key to kdump kernel. Signed-off-by: Coiby Xu --- arch/x86/kernel/crash.c | 20 ++++++++++++++++++-- arch/x86/kernel/kexec-bzimage64.c | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 340af8155658..99d50c31db02 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -278,6 +278,7 @@ static int memmap_exclude_ranges(struct kimage *image, = struct crash_mem *cmem, unsigned long long mend) { unsigned long start, end; + int ret; =20 cmem->ranges[0].start =3D mstart; cmem->ranges[0].end =3D mend; @@ -286,22 +287,37 @@ static int memmap_exclude_ranges(struct kimage *image= , struct crash_mem *cmem, /* Exclude elf header region */ start =3D image->elf_load_addr; end =3D start + image->elf_headers_sz - 1; - return crash_exclude_mem_range(cmem, start, end); + ret =3D crash_exclude_mem_range(cmem, start, end); + + if (ret) + return ret; + + /* Exclude dm crypt keys region */ + if (image->dm_crypt_keys_addr) { + start =3D image->dm_crypt_keys_addr; + end =3D start + image->dm_crypt_keys_sz - 1; + return crash_exclude_mem_range(cmem, start, end); + } + + return ret; } =20 /* Prepare memory map for crash dump kernel */ int crash_setup_memmap_entries(struct kimage *image, struct boot_params *p= arams) { + unsigned int max_nr_ranges =3D 3; int i, ret =3D 0; unsigned long flags; struct e820_entry ei; struct crash_memmap_data cmd; struct crash_mem *cmem; =20 - cmem =3D vzalloc(struct_size(cmem, ranges, 1)); + cmem =3D vzalloc(struct_size(cmem, ranges, max_nr_ranges)); if (!cmem) return -ENOMEM; =20 + cmem->max_nr_ranges =3D max_nr_ranges; + memset(&cmd, 0, sizeof(struct crash_memmap_data)); cmd.params =3D params; =20 diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzim= age64.c index 68530fad05f7..9c94428927bd 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -76,6 +76,10 @@ static int setup_cmdline(struct kimage *image, struct bo= ot_params *params, if (image->type =3D=3D KEXEC_TYPE_CRASH) { len =3D sprintf(cmdline_ptr, "elfcorehdr=3D0x%lx ", image->elf_load_addr); + + if (image->dm_crypt_keys_addr !=3D 0) + len +=3D sprintf(cmdline_ptr + len, + "dmcryptkeys=3D0x%lx ", image->dm_crypt_keys_addr); } memcpy(cmdline_ptr + len, cmdline, cmdline_len); cmdline_len +=3D len; @@ -441,6 +445,9 @@ static void *bzImage64_load(struct kimage *image, char = *kernel, ret =3D crash_load_segments(image); if (ret) return ERR_PTR(ret); + ret =3D crash_load_dm_crypt_keys(image); + if (ret) + pr_debug("Either no dm crypt key or error to retrieve the dm crypt key\= n"); } #endif =20 --=20 2.47.0 From nobody Mon Nov 25 07:59:24 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 BEE2B2010E8 for ; Tue, 29 Oct 2024 05:53:14 +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=1730181196; cv=none; b=ZFVLitEdPc+/ajqxVJA3wvAA4BSPDHWEx1iuUy20QolWjjhjLGh3dfn8NfSEnb6cVz/CmWy0QE0alPzjCOYO7Me6EG6wVhL1HTqi3fSBqFTKpGrTViTNDc/HlKCuk7U0G3/btDsrhldtIMTncYJrbi3a1TjQEi6pEm7MIgSDgkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730181196; c=relaxed/simple; bh=d8Bt5adGDGwo3V2Z0dttiq7ezDVmTkWZxy3JE0RUu/Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iV7GQh2Mcpewp0QjA3cecNkzJljtldZcRGVgidOFD6KSil/jpFyqLTVzHsAx83GHPz3jFEBTLsH7J4473i8cQFF2qWJD+3wYkEE9vpBykN71B0JCuO6uixXtrYbleucahe8yQbiBsZ/S25f2f4sT/eB5Z5Yb/lmDn5yHkJyr7rA= 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=P717lp3h; 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="P717lp3h" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730181193; 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=uo5L+VgmTjh6XwtSjWx3uno6HruWIf6FNemxob1bKlI=; b=P717lp3hPpLPnMGmKCre0jJ3KDpEQbVZR8jOvPRe68AehJ+m/WHzz6uk+XtUjhf6WgSixR grV5ySJAlVqGKhfKIUOgt0wptqbRorCnL/jwbk/bsXrBcx/yS9q13iESG7uO7veN9O6+xD nEeeb7Ojnz6q+VPvbBT5xx+BbJA/zyE= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-31-1xpZ7wO_OYOkpv1VrdTHAQ-1; Tue, 29 Oct 2024 01:53:11 -0400 X-MC-Unique: 1xpZ7wO_OYOkpv1VrdTHAQ-1 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-71806ab1a97so4885022a34.0 for ; Mon, 28 Oct 2024 22:53:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730181191; x=1730785991; 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=uo5L+VgmTjh6XwtSjWx3uno6HruWIf6FNemxob1bKlI=; b=GjVNsGGLZq8pEbaWkpwBKUcgF8XcxJcpysD7rEHXBJ4dabCzQdvorKKjsgTKweMSnn pBfHOKoVmu5tl+iL0e2DfoHzP2BrQAGAtYIvHBHWLLfOyppNxolrwFP3x84dODJaNaPz TeB43JVDJXuaRaiKZhEH9/o4TdNoWlgtfR2uijPdwXGFeoc58Kb1zNjaXM9g2uAXNFeB clCDyy792rP4dkBxmUakeaMpKeGCrFV/PV7xi4zuBgixLUMdWyvC7RXndAU8/i4auf9s hmvPyUZFEZHIk23q2GjoBLQeQkngoxyGZ7pUgVfss9L9FR1Y8KAVFvHo/kueZ+WE+ctz TRvQ== X-Forwarded-Encrypted: i=1; AJvYcCUiqIaGgZt17xRYONjQk+QXjMbF85jk/mNWl+uMq9wkwpzKVpxn3T7beb1uFKOAwOSSlPr0rw/AHcNC0Sc=@vger.kernel.org X-Gm-Message-State: AOJu0YybWAiHwjo4Z73A+nzAfpGx4uEpsdnsD7OiKQBFEGbcodGaxBBg OtVZ4NrWqgGhgUc4n9Uu+27rKRspzwQuZYIkQ/54wD+1OgiS4O7XBCRg+bueV7rnlWKNvAr/CwV JuSz34vUvEz4yATb3knHTGKYX9zXS1m5zDh7OE+Fe1l5aW14tSsZ9NOCR/eKwsg== X-Received: by 2002:a05:6870:6590:b0:278:1863:f4b6 with SMTP id 586e51a60fabf-29051b5edb6mr9553423fac.15.1730181190830; Mon, 28 Oct 2024 22:53:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGOrme0N7lDABZ6zeXKC9s2HBoZLQciyQpqHGHhIZzIWySUR20Lx1U8S8dKA1C/4iFsv3PKjQ== X-Received: by 2002:a05:6870:6590:b0:278:1863:f4b6 with SMTP id 586e51a60fabf-29051b5edb6mr9553412fac.15.1730181190562; Mon, 28 Oct 2024 22:53:10 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm6757783b3a.33.2024.10.28.22.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 22:53:09 -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 , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v6 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible Date: Tue, 29 Oct 2024 13:52:20 +0800 Message-ID: <20241029055223.210039-8-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" This adds an addition layer of protection for the saved copy of dm crypt key. Trying to access the saved copy will cause page fault. Suggested-by: Pingfan Liu Signed-off-by: Coiby Xu --- arch/x86/kernel/machine_kexec_64.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 9c9ac606893e..7389b912ba43 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -579,13 +579,35 @@ static void kexec_mark_crashkres(bool protect) kexec_mark_range(control, crashk_res.end, protect); } =20 +/* make the memory storing dm crypt keys in/accessible */ +static void kexec_mark_dm_crypt_keys(bool protect) +{ + unsigned long start_paddr, end_paddr; + unsigned int nr_pages; + + if (kexec_crash_image->dm_crypt_keys_addr) { + start_paddr =3D kexec_crash_image->dm_crypt_keys_addr; + end_paddr =3D start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1; + nr_pages =3D (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE= _SIZE; + if (protect) + set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages); + else + __set_memory_prot( + (unsigned long)phys_to_virt(start_paddr), + nr_pages, + __pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW)); + } +} + void arch_kexec_protect_crashkres(void) { kexec_mark_crashkres(true); + kexec_mark_dm_crypt_keys(true); } =20 void arch_kexec_unprotect_crashkres(void) { + kexec_mark_dm_crypt_keys(false); kexec_mark_crashkres(false); } #endif --=20 2.47.0