From nobody Sun Dec 14 13:49:29 2025 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 1EF4F18A6C1 for ; Thu, 16 Jan 2025 06:58:36 +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=1737010718; cv=none; b=nAkVOLYEDqLvb74uul2IbQ3TH5qkEd5yUswJ/9tBfEiBB7iOv8BFEC8Li/pmnsEdIENhOxWw9uNejNDZZZaPR2QqPMa+Pz+6kVkNsgTHgNha4AN8/HJW27NKMrH2IMCiICdzzzXH7PgFK6YqKWcraf30XAxFLQEP3UyiAWhpZLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737010718; c=relaxed/simple; bh=vN57Sv7PcuSwPJfpkzLqhjEXvS/vYxtQzrdFlJelTjU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r1aRchJl0Il39M9SVboJqpCQ/viBDBhLxBYWxTbUuFwFVPR+YrAD3+wFYWggH3ZbzoePQg/+HNTs9YCXlEMlJbvADzNWcun2NgAGjSjUVqjaH9QpftxU4rRcBRtHHOxHcg8T5FU9RkMlVXMlDA7vfI9XZ9hmziJezDXFpe4tF8w= 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=QI5Emff5; 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="QI5Emff5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737010715; 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=D2+CoYcPBnmfcrz0SBgbGqcrlBUqnsAYzTPenVz1caU=; b=QI5Emff5/Bh8GJbPw6UB8N1IUDgrz8HEEXPZi9yARH6ceoSwKLXhe2KjDBMR0KrRGQNLDI 1HlZL5WwJJ8W0uki0jU0GkVlsV2wQuH3Y2xh/wipqAg29cdS3Rdlh4fsXX+oXRJrcMzZpv dSA1+Y8RgofZCFM7IO+k2IaLBUBPemA= 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-563-E32d1El0MCCowhqWdo8FmA-1; Thu, 16 Jan 2025 01:58:33 -0500 X-MC-Unique: E32d1El0MCCowhqWdo8FmA-1 X-Mimecast-MFC-AGG-ID: E32d1El0MCCowhqWdo8FmA Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2eebfd6d065so2284408a91.3 for ; Wed, 15 Jan 2025 22:58:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737010712; x=1737615512; 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=D2+CoYcPBnmfcrz0SBgbGqcrlBUqnsAYzTPenVz1caU=; b=GRg8om5SnNqriWwYTyJlA8D3f6pHJLoSZf8ldZ8pKnlCFf2ab2iBC/8KYoQKErV3s0 R6Ur0+bV3H+xPBUlcZo9icpEl9+A+K09WM9xUd+Y+cQF9oY1MOrtIakce9pQQK2jUS0X h9hXhu3esiw3yalqXRFAbDZd/lwRo5Gadly+ok8/+Ailmpcva/oswaDknOvbMs2eomhU n0Is3KYeTOt1N+oPBoWrnIRP+EVTeWw1j59akvDwcmcN5wEeP8uwXrX6QHZmynBhwn/Y uAsFGeWsj6doSf0pph4twPaWaxb0aPHwKJqjTbujzmlQPpGOG9qhUs2hqHGwKOvho1Ye adYw== X-Forwarded-Encrypted: i=1; AJvYcCWHHzS5ZL3x0juxtCV8/mXGjqNv4L2vYpUSS4o26AmAUnLe0CxjrxAryWWPcZgbmmTVzMHwcxnJBp7VNCc=@vger.kernel.org X-Gm-Message-State: AOJu0YyiEyKsAkQHF3uSnh2jIc/n/QKAVxF0aARPRdB+mlp/emDHdXL3 0ETrUP5i8GC+SvDs46UWpKMeAymg/9N8NSsWAokUtITbOEtIkrvtEOTMYBhvT3pLprNkwa/4If1 eMBDlioIAocQjwPMLlGir5YSS1oGV6NgltSDpwkEbIsEDkUt99tw0GSTMnnJ6dg== X-Gm-Gg: ASbGncs9kF0AWBSTHdUgvuJFeKQ5dqR+pnKN5659PM7lZOKEzpmYWL/eFgASkVevTRm gRFsqIBOo+xa6lEvUSQwO3oexpVjXoDVlvTLwTyY0C741eZX8aKaTlFIXvkBVddLMA9USAjx5/f L+BZ1or3hOSmqC18PxU4NRrRYuCsp2ynPbsxHW0pXpkh6I12iWj9LzBNehpkldqurR8gpWGmpDZ Wx0jGBofsJT92R19YAKf9+GEbi6OkoCM3NOXttmEOIYB12J X-Received: by 2002:a17:90b:51c8:b0:2f4:4003:f3d4 with SMTP id 98e67ed59e1d1-2f548f5f883mr45301036a91.30.1737010712342; Wed, 15 Jan 2025 22:58:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZwFoiLlhavnDUatbgb2kTU1Y2y/dIgU8v9im+rqhrRya3h6VZuF6F+cRCzUWzSL0lLNhCmw== X-Received: by 2002:a17:90b:51c8:b0:2f4:4003:f3d4 with SMTP id 98e67ed59e1d1-2f548f5f883mr45301018a91.30.1737010712075; Wed, 15 Jan 2025 22:58:32 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f72c2bae59sm2754048a91.30.2025.01.15.22.58.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 22:58:31 -0800 (PST) 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 , Eric Biederman Subject: [PATCH v7 1/7] kexec_file: allow to place kexec_buf randomly Date: Thu, 16 Jan 2025 14:58:16 +0800 Message-ID: <20250116065825.1041558-2-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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. Note this feature is enabled only when CONFIG_CRASH_DUMP is enabled. So it only takes effect for kdump and won't impact kexec reboot. Suggested-by: Jan Pazdziora Signed-off-by: Coiby Xu --- include/linux/kexec.h | 30 ++++++++++++++++++++++++++++++ kernel/kexec_file.c | 3 +++ 2 files changed, 33 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..61269e97502a 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,33 @@ 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_range_start(unsigned long start, + unsigned long end, + 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_range_start(unsigned long start, + unsigned long end, + 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..875fe108cc83 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_range_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_range_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.1 From nobody Sun Dec 14 13:49:29 2025 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 6FF581946C3 for ; Thu, 16 Jan 2025 06:58:41 +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=1737010723; cv=none; b=o0Mmy3biT5eMav51il5WH7gimdaoPsapDOXaSzsOgpkckDRbdvhZNGqn3+gli4Lww3eU08aaNkKjMHeS28vMunArfAPMZtyeDHYT7iylYpqhU8jXBdppTMGw4VApL3ZRKl1rBNJAwUfSgJY+wY2iNx3uSpioRL8fnZT4ynkaj74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737010723; c=relaxed/simple; bh=D16J/WHAbaaOHTd/nUMP7WV1oMg7HXcfapvspWL7FWs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bvdxempt8WRWnG9//cKk9Sh/PNHy4jtAzeKod/5+lkfTu9I/jwSiK0IYG/iwn+GwJGIUvuhA8HSdTiOugVJXic1VcVkM9kXBP6WYpjA32gyVIO40cBqHuzrngVVKU0LfvThfrqNZuwcb4ZTcBj6xEFVQbxn/0jwSucfGKIm9SbY= 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=IpGArR4A; 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="IpGArR4A" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737010720; 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=WIRiDBJu9+QDyyb56ofJIWczEtwhblONzUKJ53stfmU=; b=IpGArR4AF3H9VhtB1BaRB/vapSDSY9iTFB75zkYqu9EQXjH19P6FjNqdSE6xeUp7/kFrZK BiA7Pg48EFqCVerpIhXQczXkQzYB4V+EgdQGyLgh/fHOmSYrl3UTtt8/hYGEffVEZq7vQc ilwWnYWswdskkc4tnXPzTT7GulQH3Eg= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-653-qZcx1wdPPyaqcWbNEaBQHA-1; Thu, 16 Jan 2025 01:58:38 -0500 X-MC-Unique: qZcx1wdPPyaqcWbNEaBQHA-1 X-Mimecast-MFC-AGG-ID: qZcx1wdPPyaqcWbNEaBQHA Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2ee5668e09bso1496378a91.3 for ; Wed, 15 Jan 2025 22:58:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737010717; x=1737615517; 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=WIRiDBJu9+QDyyb56ofJIWczEtwhblONzUKJ53stfmU=; b=oM0MnPACm4BsRb3miLlb3ruhA0ogLSGstj171hZ1QM9cYp2eINvpUVPeFuSgTLWB/U XEm38K9vkHJIzCxDjkIaUvZAmMIFJdtd3DGAUGQwd7rBYLGorMuQ9sa9EVGfxuwjZ0mR YmAb4Z+UETVmf7/p5we6n+NF8V2kuIDvzlrYz60NJMs3eCBlSZHGsxl0pk6+iHjBp+Wy U99WwWn1nz/jchAqhLGZcIzJAb516zNWvyTYS/gpIpGBTSAp3HNF77fOqb0Ol7HL/rQW ro+0117K/4xFEuGObG6SIKP09Q/j8/V1XveGGD+tNtENpYxU1j/nrsTCKNliXv9iF+YL RCvw== X-Forwarded-Encrypted: i=1; AJvYcCWAjwPQBE57oOgyG0XvfeYI6bATRVotTFnV1f8T/6xbTbbjcw6bF5w35E8TqLscSsCEEsYX0hmcD2fDSRw=@vger.kernel.org X-Gm-Message-State: AOJu0YwjzL7DER0kSW2WoiHbg33GwRQEADZJ0kAYNardbiHt55yMPCq2 6nos5KD0W3T03KirN0o8I4+8yphaYKLsnMKgrLrOWNOXQ7UeOo53L5e7OmHogKCSdBFCMhHjJ1u Vim6coOcubSEbsFLhgFCTGjCEjMNoSEJHQVlISGfrkks+xR1bTc3mleM8ruU9jA== X-Gm-Gg: ASbGncvDydTYxxyq31m7itgTiap28WWBkdMCEErbiv/wIP3WNvw03gDVmgTb2xiNkRT QcZWWsCtnJoAVTKkifLSGeRNEgb84q9YWi4IAe44feuy6wRUfUWESgGd2qxx30THR57SI3t8bcP rCLlkQtGKjjHcLnRhHLad6Qxthc4pY+IDTOhVDhw7mQkl+mHROsQ08Kz2ofBaHRy8QsAdDttyF7 kfcIwLkkTdioq61TigyfiyzVfu2pMn65CFYOFuDUcIhc0tt X-Received: by 2002:a05:6a00:39a3:b0:727:3ccc:25b0 with SMTP id d2e1a72fcca58-72d21fde276mr44027987b3a.16.1737010716714; Wed, 15 Jan 2025 22:58:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IHlw/4FDu2VfUFgBy+S6RCRgqpdNLoWkdfX1siUiDXh3z0KKvPCgaeFfQdiUXl0ijbCzJ0zYw== X-Received: by 2002:a05:6a00:39a3:b0:727:3ccc:25b0 with SMTP id d2e1a72fcca58-72d21fde276mr44027962b3a.16.1737010716208; Wed, 15 Jan 2025 22:58:36 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d4065c475sm10319945b3a.117.2025.01.15.22.58.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 22:58:35 -0800 (PST) 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 , Vivek Goyal , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION) Subject: [PATCH v7 2/7] crash_dump: make dm crypt keys persist for the kdump kernel Date: Thu, 16 Jan 2025 14:58:17 +0800 Message-ID: <20250116065825.1041558-3-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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 within 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=3D128. 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 --- Documentation/admin-guide/kdump/kdump.rst | 28 ++++ kernel/Kconfig.kexec | 10 ++ kernel/Makefile | 1 + kernel/crash_dump_dm_crypt.c | 154 ++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 kernel/crash_dump_dm_crypt.c diff --git a/Documentation/admin-guide/kdump/kdump.rst b/Documentation/admi= n-guide/kdump/kdump.rst index 5376890adbeb..192d6796ab94 100644 --- a/Documentation/admin-guide/kdump/kdump.rst +++ b/Documentation/admin-guide/kdump/kdump.rst @@ -551,6 +551,34 @@ from within add_taint() whenever the value set in this= bitmask matches with the bit flag being set by add_taint(). This will cause a kdump to occur at the add_taint()->panic() call. =20 +Write the dump file to encrypted disk volume +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +CONFIG_CRASH_DM_CRYPT can be enabled to support saving the dump file to +encrypted disk volume. User space can interact with +/sys/kernel/config/crash_dm_crypt_keys for setup, + +1. Tell the 1st kernel what keys are needed to unlock the disk volumes, + # Add key #1 + mkdir /sys/kernel/config/crash_dm_crypt_keys/7d26b7b4-e342-4d2d-b660-7= 426b0996720 + # Add key #1's description + echo cryptsetup:7d26b7b4-e342-4d2d-b660-7426b0996720 > /sys/kernel/con= fig/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 + +2. Load the dump-capture kernel + +3. After dump-capture kerne get booted, restore the keys to user keyring + echo yes > /sys/kernel/crash_dm_crypt_keys/restore + Contact =3D=3D=3D=3D=3D=3D=3D =20 diff --git a/kernel/Kconfig.kexec b/kernel/Kconfig.kexec index 4d111f871951..5226775fd4c6 100644 --- a/kernel/Kconfig.kexec +++ b/kernel/Kconfig.kexec @@ -116,6 +116,16 @@ 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 KEXEC_FILE + 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 dump-capture 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.1 From nobody Sun Dec 14 13:49:29 2025 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 56CD018FC8F for ; Thu, 16 Jan 2025 06:58:45 +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=1737010728; cv=none; b=EYKhO5IjLyj1Rgjcpt7VvphSIzaFurcA3oIziTMRO0nPuxqCQNPWOYQBedgHrAvvgMqbxDMqGyiit9NJmRcyixCRdY0CnALDiUCHy8A7GCqJDacscZHkgrt2/5Me56nk5R0fTkrPTbbDbGvbtksrNF7/lJRIOVQtJQenAefcA/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737010728; c=relaxed/simple; bh=0ssQhuDfl/5VpyiMqxSR0IwAfjRFUrcMVdKYNqGH8bE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hR1XcDnhY7/3hjm+Rezg1ZjZVWfSH9rKhLVTojFhXYJpYt3IPyqg7SZQ3l8IFTEuTy6wJXT0wwcNX8gb7f70GU1/14Vrkqr+vIgvuQH2vcs9cFqrXtagUnP58KAsMtsaawQsogJcvD8ybqJ1kOqH60EgoPT6fk7+Fi9N5M9fwB8= 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=WpxgOuUu; 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="WpxgOuUu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737010725; 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=tQvCsR9EXSN3ga2DmGxkK/BjC0F26mqQj7WZLS4qcAw=; b=WpxgOuUuIM8LJgQZsVPl83qmdOsjFA/ydL5ruQCThydJ+nJtx8r0zz3PPWUnY9nQl0sDJJ MII/FN1/iGpRCo1rVhw0/qxi9OypGq5OegwoFXY1pLCdL5rB7yulu7c4hH+cRdZGmHg0O/ Q1tBK84f/5tkESTZxqTmxZQQ7gHEVQA= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-458-Jg0zh-v3PlKcZgNJ7g5yfQ-1; Thu, 16 Jan 2025 01:58:43 -0500 X-MC-Unique: Jg0zh-v3PlKcZgNJ7g5yfQ-1 X-Mimecast-MFC-AGG-ID: Jg0zh-v3PlKcZgNJ7g5yfQ Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-21655569152so12614695ad.2 for ; Wed, 15 Jan 2025 22:58:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737010720; x=1737615520; 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=tQvCsR9EXSN3ga2DmGxkK/BjC0F26mqQj7WZLS4qcAw=; b=RucO+4G4yMfj5+wWHjgCWdNr4eCxsFL18+2QjSfbPdYcfRs9Pj/PehmmaPkZHlMGx7 1Si/7TN8E1v658N1MRkxgJRufY59ibe3zdNgmI97qB0/mV9RB2W/nKxJ68dugRreQo6k RyN4M2cLRLlSdACANopfy2WNRcoHLcg5377O7R8ncW4nKNc0Imce4b3koCTGIU5cNi/R OoQMbTWjlz5s3dM/5uaBOVchiri5U8qK2sD7jVGp2I8VXD6biq21RdX0xbzlbR46xbAd kUbN6cK6maaorWve1Xo+kbv5pOgcjoDaUzBYgJKKf+7wN8eXjyskwDd9sTbKceMXE1Mu ig5w== X-Forwarded-Encrypted: i=1; AJvYcCWDteEQ+6cpwwWkLDmONxmOJvtvTy0lql7VLVqmQO9JzTi2kP7yqkHy9Wcu6ZYUkP+Xzf+f5azI5v20BX0=@vger.kernel.org X-Gm-Message-State: AOJu0YwerztA+q1mnk+5HCy1XHv4c2sRlu1pV4u/k6rbOdMu76aBSjm4 PNuI1CjhlHoiZKiy8piiHhCGqGKAnTOoAauhXb6N1VaWe1UWLqJ2q60JUYuAjhRUh/xGD9A41sx aOJkXplMoYzMA+kUJAGRYLn+kL5ylcvpdYpn1oHZoqWr3U1eWYYY359WkLC8g6g== X-Gm-Gg: ASbGncsERgnke0Iw4MPAXaXwUXzIROqrf0IBiCnWk61M0pk6cdLMtfzoaAgFUyJ8TrJ rC5HlH7oxc7cZVYgZfwA8vmIxc19R9soQUAc06/X2zXKPRnuC7F958MgxANI6rd87k+/g37y5wH finhPYZDycKPv3CcNG0Hdliw12ROxiJLnvQV4e2ylDBp6epIC584TrxhD8VZ13Z1mVXVnJ8jItt +8J8UCzUqV19rU13DVR2Hh0vzug4B6SFwzx0xWnv8oadQZx X-Received: by 2002:a05:6a20:4325:b0:1e1:b062:f3fa with SMTP id adf61e73a8af0-1e88d1dba97mr60123649637.34.1737010720562; Wed, 15 Jan 2025 22:58:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0mUbg5KfGVjWvvefswEmAHcx/8ryY08l4nYmf/QtfmhIT1/A0W8mtD2C6GcsE+LcNZkwPgw== X-Received: by 2002:a05:6a20:4325:b0:1e1:b062:f3fa with SMTP id adf61e73a8af0-1e88d1dba97mr60123596637.34.1737010720061; Wed, 15 Jan 2025 22:58:40 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d40658efdsm10700322b3a.90.2025.01.15.22.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 22:58:39 -0800 (PST) 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 , 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(_le|_be)?\b) Subject: [PATCH v7 3/7] crash_dump: store dm crypt keys in kdump reserved memory Date: Thu, 16 Jan 2025 14:58:18 +0800 Message-ID: <20250116065825.1041558-4-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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 61269e97502a..ec7504ba80e9 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -398,6 +398,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..8c093c743d58 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 -ENOENT; + } + + 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.memsz); + + return r; +} + + static int __init configfs_dmcrypt_keys_init(void) { int ret; --=20 2.47.1 From nobody Sun Dec 14 13:49:29 2025 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 1E1A5199FDE for ; Thu, 16 Jan 2025 06:58:48 +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=1737010730; cv=none; b=HIFSDAJTnwNyQYdUL1d0f241X5wHZEmCJRO3iYlocDUFr8S26UilrIR6ozxBqfnGoQC2IZ2g6slMARfIzwsSzItMAnQhGmrgy9F7ZEFadCGZqtXGbZrliEQYr6JvxofF6buD37UenBxG7LOCZcbYY1L1FGdz86lCjL91kEEkawQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737010730; c=relaxed/simple; bh=0IzIgEB8lMBr0Rn/YmZNidG+wG0qR7sON2umg+tTrvY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VTGYnp0vdhfnsDyWyHvyR+cnfuewSJlbcbTUEL8hTel9RfT5lvQkwMOe1KWJWO0BXOzmYx+63lRuz6MyrxSaV0q4/r8y5dQZh31wnkuyDJXzfU5E8Z54rJwID6g45DBjhR31ej+VP6tb8nGdvGJuqIN6QaM/g2QnjGye6eHsPrQ= 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=QWI/I6c5; 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="QWI/I6c5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737010728; 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=F93kQVkCsCJNL9Y5XwGO7Li96Jeb80TvMhX68lBiTYM=; b=QWI/I6c5rCX1RHP22vdzvxbGBzzcSRT5zxMcdcJGf+tNThq/TJ+diQH7POdmRNZC/XZgQK m4uMVUBSglpuOIIzYXnQcphTMbXocEcXghPUo2xHXDc+4jNno4N//KzzjMEcwBEVUqNPT2 hXjNz1tK6bQcswT9LU3upVlvaokOxZs= 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-463-yaB3eiKAOs2u83unaRp7FQ-1; Thu, 16 Jan 2025 01:58:46 -0500 X-MC-Unique: yaB3eiKAOs2u83unaRp7FQ-1 X-Mimecast-MFC-AGG-ID: yaB3eiKAOs2u83unaRp7FQ Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2ef9204f898so1373996a91.2 for ; Wed, 15 Jan 2025 22:58:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737010725; x=1737615525; 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=F93kQVkCsCJNL9Y5XwGO7Li96Jeb80TvMhX68lBiTYM=; b=LVBcTf21gPFSptt/AkuZiFronWCGbxAfqWrJul7WMeswcoD7N14CLpda938HQe5dfg esLYHYmuJkJ/upLMCT8gipbaz4wVw2Rsk1uoOGJDYLWzALZAl1RF5A0VWrT1w8IkDjE2 c8P2VW3d4cSHD4ImdpHAMLqC1x/KnEzan4NJyhMtLJdrzmXBqCLp/i9MNWAPJ7Y+hlAm QorbRcS4ifz1BiQxVI5VQ/DjbjpEYsP9YnPA07FwOnNYZI0+bgCiehZYAIKStOyjU2Nx PO04jw/+K6rlyfTd5XxsBSpH/aVIH7rA5Z19EMKyuZTDv/4GVaC/msC2Exfr8d2shaUx G0nA== X-Forwarded-Encrypted: i=1; AJvYcCWcoPlOvTc4U9q/nESGwjpBANQFwnkcodosYr1swrQoAj9LHuARK42UPbmLttbpiyuz3mttsaAfbKvK9zI=@vger.kernel.org X-Gm-Message-State: AOJu0YzS0mJXyDEHLFAbDox1hCs+BFgaoOv77ly6BWA28Ch5xlUPcrnB n78uG/as6U+vzflTWHgCgCOeF2vaT0oCRREZp2udwOIeUVOd4H1yaGxiVVhlNbcHfTyyX9PgUkc 3OIA4V4YJBdbSwgEJGFOsDTSR9TSAGprZdM9nDDPE2jVsdtbalmVWFUiEsZbWCg== X-Gm-Gg: ASbGncucbuX2ZDTJyHB3GRKkjaRDWwDwWdSX2GhquYm++eyS7JgaxeP+S+t04xOMEBX CuT+Ti2sDYqtX6vc9snIlmPiiqIx9PqEFSINKxMSKAxRrEgG13AGYnYDIpzccSFxpkSb0P2zvJm /jYbz/4kttpYH4/E38DfaRXbogIS5b5h5e+GfZwPO+3QlPab97avXygUJ+IrjC5FTro61wN4hwi OxQ+wT+nXdaU5pQauh+GY2BWvnO5noHr4HAsB3EB5PHN1fc X-Received: by 2002:a17:90b:2545:b0:2ee:693e:ed7c with SMTP id 98e67ed59e1d1-2f5490edb5fmr51202224a91.33.1737010725538; Wed, 15 Jan 2025 22:58:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IHwhoEAEGa8uJD6eJLSOncFzlqkhgLCFTLeey8Y1WHbpWJxLac0mMSmPS3qjC2CepgVd/2MxQ== X-Received: by 2002:a17:90b:2545:b0:2ee:693e:ed7c with SMTP id 98e67ed59e1d1-2f5490edb5fmr51202183a91.33.1737010725215; Wed, 15 Jan 2025 22:58:45 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f72c156b9esm2552352a91.4.2025.01.15.22.58.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 22:58:44 -0800 (PST) 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 , Vivek Goyal , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION) Subject: [PATCH v7 4/7] crash_dump: reuse saved dm crypt keys for CPU/memory hot-plugging Date: Thu, 16 Jan 2025 14:58:19 +0800 Message-ID: <20250116065825.1041558-5-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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 --- Documentation/admin-guide/kdump/kdump.rst | 4 ++ kernel/crash_dump_dm_crypt.c | 52 +++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Documentation/admin-guide/kdump/kdump.rst b/Documentation/admi= n-guide/kdump/kdump.rst index 192d6796ab94..cecfa5d34f01 100644 --- a/Documentation/admin-guide/kdump/kdump.rst +++ b/Documentation/admin-guide/kdump/kdump.rst @@ -574,6 +574,10 @@ encrypted disk volume. User space can interact with cat /sys/kernel/config/crash_dm_crypt_keys/count 2 =20 + # To support CPU/memory hot-plugging, re-use keys already saved to res= erved + # memory + echo true > /sys/kernel/config/crash_dm_crypt_key/reuse + 2. Load the dump-capture kernel =20 3. After dump-capture kerne get booted, restore the keys to user keyring diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c index 8c093c743d58..328d3dd0d8f6 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 is_dm_key_reused; + +static ssize_t config_keys_reuse_show(struct config_item *item, char *page) +{ + return sprintf(page, "%d\n", is_dm_key_reused); +} + +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, &is_dm_key_reused)) + return -EINVAL; + + if (is_dm_key_reused) + 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 -ENOENT; } =20 - image->dm_crypt_keys_addr =3D 0; - r =3D build_keys_header(); - if (r) - return r; + if (!is_dm_key_reused) { + 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.1 From nobody Sun Dec 14 13:49:29 2025 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 4224319D082 for ; Thu, 16 Jan 2025 07:29:08 +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=1737012550; cv=none; b=bkwnsYUWBvPMxCGfZdox6o/psMeaaBg5pAFvb41LMf0qdy6DYWMeJKgqQSi69aIBA+/7KL4o1ZibOTIVeNvd0sXfg7aoYUbo++808Ffsx6FTjggF7BjCb2WmZnJG23I7PtNCG9OXt7a4LhOzlGBSpucLRBc5jCo/ML0Dw2lzC/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737012550; c=relaxed/simple; bh=yi8i0nltFof+Q5hp6axYD/aoENb0f60LA5/HNIhHfcI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=il5aw0Ci2CwSR93dGb1ennSgIuQhvDXYlyQG38tApK5l4WpQkOrLClDeK6WUze1x6cAIhnJUyqBxdzfSvkwz+TS7RaBqh3kPSF/7JWvjTN9SjT6KusBnc+lkohB5xS7sSjHFoUl2O6PVlgu1LfK5btKVOPBdPBoUCao2NYpFqhU= 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=Vz4thBYG; 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="Vz4thBYG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737012547; 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=BviejlZNnl86qGI7QFJAbkZWtNOpYFXBNGiYqPEuqFo=; b=Vz4thBYGwCHDYenXjiuq+2riXJie+fmUMM+eZLlOAvwcRMEPMdlt4o6yu4eyGc6KZzCY1/ BWVhoxTHVpjqI4liKBrN3IwiOH5kTj7kFABLpREc1Jse3hg3Bv7BhdKIsPDGFOG/bGgqo9 A35MTars4gw6xOyX1TzXWk5cRMR8Fu0= 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-636-c0XIrYIoPcW1DTVoniViWg-1; Thu, 16 Jan 2025 02:29:05 -0500 X-MC-Unique: c0XIrYIoPcW1DTVoniViWg-1 X-Mimecast-MFC-AGG-ID: c0XIrYIoPcW1DTVoniViWg Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2ef91d5c863so1554178a91.2 for ; Wed, 15 Jan 2025 23:29:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737012541; x=1737617341; 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=BviejlZNnl86qGI7QFJAbkZWtNOpYFXBNGiYqPEuqFo=; b=qFzidf5ue2ullMhJb9vZd4M9BRqp/5FlWa7HOZwoPf20EjA/YoVyKD7AjzqBbY9Ked /zkqxxO0LtoDqL+d2phX8glXQgQEmsk2C7bsbvDcRMr3j9zS9W9r7eiuI3KU5v5XUw6x puHpokU0ocxFMOUtbUTo0IqqjTaJ9wfR2kWTN7x9neqb66MVF4U9BHwzu3mIoUYXtiug fIba7ymF/8h2rWSnF2pEBtRpiim1gkx15vuMy/WPT+ar96RdauBHtDympTDX4dgZsYRF YY+/+6Za3/JHqZHWZRgr3UGrV4+ePaXCNJhAl3R6ZPRKczkIXvZ0f7hnGIwqldW+DV9k /UTA== X-Forwarded-Encrypted: i=1; AJvYcCWLEaNOnEdXpW3XhDWOm8rP2VMZrt3x5aKXUU4BMg0BCxTzZwZoSnGUrjwDdBqiboPTAiEIJQLadHE22uI=@vger.kernel.org X-Gm-Message-State: AOJu0YyfqI0xh7IypqfAHzBF/mLf+Cxkqn1s83Lz39NVZm3xDOtcZx1s /xUnQbP09geYDWdlgmNSY5SFdejqV933w7uE27J6+Xt6wL1z91B07BcTvfdC/cCxcZszKRM5LVw 519mYGTYwtTI5J7THJ7/xHenUsYXkxiVk+wuxBcmkMD5CbwQ0KXmOaPZOtCKeDg== X-Gm-Gg: ASbGncvLKXq7voJT1iuxlqr2f3+xVUpnH3XpLMvGsMr89mLkSg4lOSayUgsoefKtr+/ fmylXWf2+iRtMiuTRFDoOMKCy3daIdxRFtsPKIdH9mzV2zAy7KkkMUBguirZjRUo/xHBO+PC0P3 PURikOmvmOMIMLVGVAoEgepW5srN13BZTncrgJfmhxbYOdWIIRv5Zxj14ry5iIME4bRfYZFg0Xh gpBzSiAyn+1pY/spuOwSCh8JN1sxpzLD/FFjAVk6U7SP6Rk X-Received: by 2002:a05:6a20:4393:b0:1e1:ca91:b0e3 with SMTP id adf61e73a8af0-1e88d09e17fmr54696907637.36.1737012541650; Wed, 15 Jan 2025 23:29:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IHio3Q2Kz/zxkpeLrjgicFr6Y9U5ULOIgFM/y3NNp5ky9pUuLwqSiLamGdZ9OG5RK5mUgglkA== X-Received: by 2002:a05:6a20:4393:b0:1e1:ca91:b0e3 with SMTP id adf61e73a8af0-1e88d09e17fmr54696866637.36.1737012541275; Wed, 15 Jan 2025 23:29:01 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d4054847esm10654849b3a.11.2025.01.15.23.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 23:29:00 -0800 (PST) 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 , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Vivek Goyal Subject: [PATCH v7 5/7] crash_dump: retrieve dm crypt keys in kdump kernel Date: Thu, 16 Jan 2025 15:28:57 +0800 Message-ID: <20250116072858.1043529-1-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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 328d3dd0d8f6..993d9e08d774 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.1 From nobody Sun Dec 14 13:49:29 2025 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 29F3213AA38 for ; Thu, 16 Jan 2025 07:31:00 +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=1737012664; cv=none; b=Zxlv8aNjtR4ai92HIj9WscT3TOgphsncSh3JpTeabkq9/0QdcQHS43N2aNySG8nU2apzCHVmWq8u2/rxGLGnJAOoD3sirnX4wMKHxYFhYk9pYhPNSvMTs9dEGoONB979i7BiwRXSO/xalOjD7lVZOjqsoMH15HvnX/rb+lo6j0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737012664; c=relaxed/simple; bh=RP9HogcZw/sWNsSOpTqj7w6ztdG6MlxQKFyARtdx4Q0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T4xPYZRbRNXVXHOwOAJchbc36CKv687MzvB7aE2Q1cxKvlLZ2vZ8zS5/9m6B8r0MRqdbW8DJEMtWU+L0PJahqhSrM1/DkApZc/erYtCQnpMdUE1EmuuF3o9rZmWM7lC33sDHW4tgBCt2s3nyO8I1HfbzsMZramyvx9ZX/4d28bU= 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=MfLfMnUc; 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="MfLfMnUc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737012659; 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=aHQUprvYUxpGchCCyLRuJWs5QUXLD5ilgW79Ys2jOQY=; b=MfLfMnUcEyCbgHGUR+56AcmcUmmp/MY3aWWXPssdUlbq9moI13nMzDezqz3yD6JO70Kjx3 HsQ94iyOk/v3K/JUTZgknNFE0LaYqxssNQWbXSKWJ84bKuClmPJqJowzS2sApsCbCzjhpT bwjjhPRVPwbujKheXfVIq0y/HJU7az8= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-101-kicIXkqYMMaxtS1arFfQWA-1; Thu, 16 Jan 2025 02:30:58 -0500 X-MC-Unique: kicIXkqYMMaxtS1arFfQWA-1 X-Mimecast-MFC-AGG-ID: kicIXkqYMMaxtS1arFfQWA Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2ef775ec883so1442578a91.1 for ; Wed, 15 Jan 2025 23:30:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737012657; x=1737617457; 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=aHQUprvYUxpGchCCyLRuJWs5QUXLD5ilgW79Ys2jOQY=; b=ijTZouoVvZ2V8UiblZr/WmDh/kn1zTGPh1OVOKAKbZ6zNiE57Iv+CYlB4hXzn64U6W aEt4swcWLiJh4alpqgKXeXBDU7BNL2HhQ0YglHIIUN7jAucw/k5VDhvEMuYZHpPqnyxQ cKffz3XG1bLemDSnmFpieQxq8VvTwhQ7P9VCoJRrhZi0lnJqARyxLxAdaVIMH3v1KInX 85iX0Zb25mzWSG8ls2X/03AF/qSbkxQZs42mqDxi1p9hDFqnh0QnjuH0yA6LnRjVebky ZBeLvBHYsSiSTx7t64LT4phbGG93os/keDJCfKHNeYwwaZy5PKZ5uYblpbyS4CBWSG8t Z63Q== X-Forwarded-Encrypted: i=1; AJvYcCVwcOM2su2O8hstZUBKZ89XnKYhcJCROVH5AMsxD+88RANXScPu/cmxzMJwJqtT+teOtxjVuIxUMemdmUg=@vger.kernel.org X-Gm-Message-State: AOJu0YwueaDGd8cHnAS6uLd8PegvfuhY8G0110wjeSHuZVg5hcTMXYwp oH22bb6B4Gku0r6raDgI8NbxHKcchgFtM1JfF1Cpz49LMCuO92NleRGMCLQZXTGbX5SZT6pYSM7 ONd1UijJ1lvPtL+ReJsro/oq4ihIkf2j4A1lBTB3vfx/8YNKUXI8yiHrPLyy5nA== X-Gm-Gg: ASbGncs2p6P8S3evyoqD1H4wp39OUSEOtjFf84rI+zuG46+uOg7ihetyCbKMrJVuslg 7y53Umt9ZqFdHratsTUx2KaRDIe1JuJ+sCL/Uhx4utBauOoh0cqRvUH6oukXBxcdjT99+77FVld QGnG5dCTB2qgwkFqEcdzSgz89hA2yGOjKfxqlZ49OYlJSDbMdJXOTjvxTlS1yAhCGrpX7Q/WN7+ WillCrQsiXwIYuBoY6bwnvdrLbMpgct6lZ/mMRXOw5b0Pdf X-Received: by 2002:a05:6a00:230a:b0:725:90f9:daf9 with SMTP id d2e1a72fcca58-72d21f650a1mr43969905b3a.15.1737012657118; Wed, 15 Jan 2025 23:30:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWDRrztzuIkg9Q5tBVV6pQohQ2pViv+9rbr1S07QCHNZTPnu4LF6hxA89i8/UG0IYrbzHiVg== X-Received: by 2002:a05:6a00:230a:b0:725:90f9:daf9 with SMTP id d2e1a72fcca58-72d21f650a1mr43969860b3a.15.1737012656652; Wed, 15 Jan 2025 23:30:56 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d405493basm10334829b3a.27.2025.01.15.23.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 23:30:56 -0800 (PST) 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 , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Vivek Goyal , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , linux-doc@vger.kernel.org (open list:DOCUMENTATION) Subject: [PATCH v7 6/7] x86/crash: pass dm crypt keys to kdump kernel Date: Thu, 16 Jan 2025 15:30:52 +0800 Message-ID: <20250116073053.1043873-1-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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 --- Documentation/admin-guide/kdump/kdump.rst | 4 ++-- arch/x86/kernel/crash.c | 26 +++++++++++++++++++++-- arch/x86/kernel/kexec-bzimage64.c | 11 ++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Documentation/admin-guide/kdump/kdump.rst b/Documentation/admi= n-guide/kdump/kdump.rst index cecfa5d34f01..c4bd6ecb6ab7 100644 --- a/Documentation/admin-guide/kdump/kdump.rst +++ b/Documentation/admin-guide/kdump/kdump.rst @@ -555,8 +555,8 @@ Write the dump file to encrypted disk volume =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 CONFIG_CRASH_DM_CRYPT can be enabled to support saving the dump file to -encrypted disk volume. User space can interact with -/sys/kernel/config/crash_dm_crypt_keys for setup, +encrypted disk volume (only x86_64 supported for now). User space can inte= ract +with /sys/kernel/config/crash_dm_crypt_keys for setup, =20 1. Tell the 1st kernel what keys are needed to unlock the disk volumes, # Add key #1 diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 340af8155658..a525ee639b63 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,43 @@ 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 nr_ranges =3D 0; 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)); + /* + * Using random kexec_buf for passing dm crypt keys may cause a range + * split. So use two slots here. + */ + nr_ranges =3D 2; + cmem =3D vzalloc(struct_size(cmem, ranges, nr_ranges)); if (!cmem) return -ENOMEM; =20 + cmem->max_nr_ranges =3D nr_ranges; + cmem->nr_ranges =3D 0; + 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..5604a5109858 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,13 @@ 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 =3D=3D -ENOENT) { + kexec_dprintk("No dm crypt key to load\n"); + } else if (ret) { + pr_err("Failed to load dm crypt keys\n"); + return ERR_PTR(ret); + } } #endif =20 --=20 2.47.1 From nobody Sun Dec 14 13:49:29 2025 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 3A8C324A7E8 for ; Thu, 16 Jan 2025 07:31:38 +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=1737012700; cv=none; b=iqpqcl8C5zufFX6nfo8e10N7eEn61rLSE6qvLQcTBEDUygONNE4zq8Ft0micDjxxTLdojJDe1HnMcnUNXvqFFCCoU2mb4J++MZOdGVnutTyjJQY2h3s5naKt5UujwFhN+H3/z7NrkUtHMjgJLaUSsxUyAlu9bVpuOkgj/wd6Jq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737012700; c=relaxed/simple; bh=EThvmEzVlpHYF9dCWrf7epLqiSWrsbtL4xk2fKAeHP4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hMTUFD2tHQ2mkBCbWHZS1qxaR6OOyTWxzIA+94a+ylkTtS38mrwihBO2zAL+HhpuV6Xr5sSuA3FkLQX78BG9YcxGpS5mfRzG8V0zgEhh/0WFnIPts3Hdbk2GxROenevNuLAZby+bHvedOhyJ0N+DS+pM9dorzqGZ8p4+LfI+BFc= 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=DXsCyRxi; 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="DXsCyRxi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737012698; 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=4HyMInoAYn9hX25Xcky+1R5gRqRXU1eKRQBtwuBzyQo=; b=DXsCyRxiJG8FQLDeo+pUFFMrYhJmXV2YYWpb7UVWNXW8kwXbiPWqDZRm2B9foZc6I902Vs Oi4A0Zvzx29K+uWQDeK7GFdBXmIUkOjunP15C5bTFl7eETq+r0GKdXuKGZHW9z7gWFtgwJ ZDLz1Sgnn1d6J0K42em7/GxCa3+ZA9k= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-victoL4wMDGq2LUFazHzzg-1; Thu, 16 Jan 2025 02:31:36 -0500 X-MC-Unique: victoL4wMDGq2LUFazHzzg-1 X-Mimecast-MFC-AGG-ID: victoL4wMDGq2LUFazHzzg Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2165433e229so14166145ad.1 for ; Wed, 15 Jan 2025 23:31:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737012695; x=1737617495; 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=4HyMInoAYn9hX25Xcky+1R5gRqRXU1eKRQBtwuBzyQo=; b=No8Whr0JMufmD2Evd1l1aTD82LcqML9viM46QYivFctfY1S+GAf6Hs5ZJH3mYaf86w z/9nbCGgkZu8FYkDnGagH416tx3RKzv71sIzk0DQGaw5hLQLMj+ipswPv8mX3O8BrrKj 1ewxIqXTGus4A3xEzjUmOm2HQdYbQfdeWaolmi30DG44ZnmH9f/HVX9LPAbAE9NPbFmd XNqcpDezUpi+b3tYF4rJ3DFS90us4roGZVb1e4uhKyufox5yxF4L9osiQA7CEqGw8XrG 6Lbjpuc/ngTjnf7sKJ1ESvmzxac+Om1dfGTPmFYx7DNRNgSjbUlSoHLCoabHBi1pcgWh /gXA== X-Forwarded-Encrypted: i=1; AJvYcCVJxCUnQjuch71r/3kS46xaVcR1DprrBDzXPXwAcHcK+pAqAzjBM/oorjMH9n68oK6eOIPUbAlz65nL5OI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6xIOwFFfRU+lwl6n1KfUkR7h9sxJ6uzHowKclVrMzkwpBVKQK eaXP0avFtVRC14fH73GzBXTkG5MNgzJiWMxZKMagZh+LuJg2s/e8gPZS2XEu2a6c6Z67V4ujA1R t/AZzafD+JVeh+Mpfu5fjvpZhEZsPBTXsENS9Pyvl8tGjStThuXuuVt2pz6AQPA== X-Gm-Gg: ASbGncvXY9X5mN91V6jJHcM55AoOMRYT1M78EBDtwOYRJaCwgpZbDqtUlMTxDlmePLE tOGC5bsUCnw8Zphx91obcSqpZRkxs0LNGqgl0urdeEx4pX8CKRzzvbVSWErQAxsqRvV4bg7ncE5 kN4mGyW556d4oSwfhIU8TFmSd4gFaOesz2LYsi1VvjrzCwkqSB7PxALczzeH7l8KPlik0vNd7UX RX9bSUi87JEid50tRQ7wVsw9ys8ByDr0ibmSInpnZA5c3Ku X-Received: by 2002:a17:902:cec3:b0:216:7ee9:220b with SMTP id d9443c01a7336-21a83f4f43amr572986885ad.22.1737012694940; Wed, 15 Jan 2025 23:31:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnDcvWN0Dcty6Dcu7TZDQFkD2YMlJT6ykiEDCc5NZUb+o/QPLJQrH8BeqNF7oR5PEH5HIFtA== X-Received: by 2002:a17:902:cec3:b0:216:7ee9:220b with SMTP id d9443c01a7336-21a83f4f43amr572986675ad.22.1737012694645; Wed, 15 Jan 2025 23:31:34 -0800 (PST) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f256fe3sm93416395ad.243.2025.01.15.23.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 23:31:34 -0800 (PST) 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 , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v7 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible Date: Thu, 16 Jan 2025 15:31:31 +0800 Message-ID: <20250116073131.1044000-1-coxu@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116065825.1041558-1-coxu@redhat.com> References: <20250116065825.1041558-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.1