Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp379385lqb; Fri, 24 May 2024 00:40:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVsYpv3Di+lwLpWtz/sj5d1n0wK8kUR18tUs7JdQRRdO+PTjkrfnIn4cFBmJtie2VEh0j22jXAngQArPsnWKcSYncmeeYqOenaajhN3zw== X-Google-Smtp-Source: AGHT+IE2RH513rGMtqPfRhfAKYiUYQ9dZYuhVvyrOzyvHh4yNsWH5Jd6wTn35oKAC8fnotuMQRF0 X-Received: by 2002:a50:9e6d:0:b0:572:9dbf:1de5 with SMTP id 4fb4d7f45d1cf-578519ba565mr701642a12.40.1716536447480; Fri, 24 May 2024 00:40:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716536447; cv=pass; d=google.com; s=arc-20160816; b=mjtwJgAfOxHVCxNVmIWwW94QdZoJlDvzp1/X6GXjvJ1pzhyU+gsVQYE5jSYVi2Oxbr 5+KvZ80TtbYqNHVvBEDUs88q+y6VDPUYI7IPGr6V4PZsSihSfqdC46io7HnviiuUo7iv 7xsgOdRpz2yaJcqWmWsUsEmUQrpLcWxO9wJdBYbv2xfdLqfpfgwtkJ3HM0bsVHap+wob 0SxlvOEmOjMnO8rxaALDAji5JVOSwPoR58KrSW/bfKSrz/LsDg8oJA2vEVKH6o+eXliH 1VrC+6Ex45XsEPdpurbgBmAOTrO79ci/fXZdCEGzilSeJVKG87nPvuSAT8ljty8KIUpE QChw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=vuXQQ/QoUSZ0kyXep/CDerc5KOfI2DPPitPZJXtY6YU=; fh=u0IvAA+gK2FuiLtmy4B6PeVkoD0GN47bzip7/s1dsGc=; b=cs9+Fr+PDdplGw8lQ7bkCEXZYp+J1E1ZGAZDrVaLP7I9uXAx2Dk0xDzc6wkXKaj4Dv myZvn1aCNzOeP2XPhfenKnoF/bbd+6xVxDX2dLqSGr9X1wLw0RT+aZyitgrzGpn6dyvT rReaPvpzRn9x+N0S95ouCPQbRHZzEt+/ZzgjTDHzGK7dfFmuPIlijW/Sp5cF6LO4jcYC XoasqxkmdaY0wt2kMh9N8K1qisyHHy0ylwMgXOxoESxwMKpfV60nkT2Ex1TlRJnhB6te wcEdvqFChCFAKwwb1jxti3enY/2FeMDFPIvF7M1YuUCa2N9C+4Mt7iLiczox2zQheW3R C8lQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aeXJaPjK; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-188408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-578524968aesi572745a12.455.2024.05.24.00.40.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 00:40:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aeXJaPjK; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-188408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0DC481F21D8C for ; Fri, 24 May 2024 07:40:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D371D1292DE; Fri, 24 May 2024 07:40:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aeXJaPjK" 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 4A316328DB for ; Fri, 24 May 2024 07:40: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=1716536437; cv=none; b=Psff/KMo4sfqwbHTNXmsxjbi5QjjY7nAmQozpw4Jke/JebSZyJOXo9+1FP/LifTeCfc1z0GX18u/1F2ef2Y9hdJstUIaqm6U2T1j0guTOJQoH9tQf4m4HqW87uYHAp2K1wkfCZ/wZmmWR98AXD7myd/UopNFlkl4mabRN+v/fng= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716536437; c=relaxed/simple; bh=VGZ4F79Z6aBD2yasaUBYYvEj2xZ7NAf2OTG5tZgYV7A=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Chzz+9lXBa7Kj+J8sQv1TfGZ7f3v6KBqYTLjHNmZkQOl4FhBU5w8WUmwO6NB94cbu1g4kjCjHsTZ6Q46CDbs+DeORh/8LTDXn9gsjMLMaOhvmlTBLUWwyeMbi1hZo2A80OvGNautUPWsj1PTX4ToJi31XFSl42vjhJ3vLMqOiSI= 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=aeXJaPjK; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716536435; 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: in-reply-to:in-reply-to:references:references; bh=vuXQQ/QoUSZ0kyXep/CDerc5KOfI2DPPitPZJXtY6YU=; b=aeXJaPjKJXvFCEH+VY8PfhhZGluB3gEp3qcEBpX/9pmh3j/fWKO7asu5DzdGUGnPldNpg9 oZa0TZ6U5Gtq07OC13ywMZ5W2gVgXjPoeNMVWO4qNO72Q6iCe2OsBuSrTAValspRDyXIes rEWRncPKxy3Xyn4npVJHVb0cLRhFptE= 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-404-LILXIoSIMeSbK38DOu3v0A-1; Fri, 24 May 2024 03:40:31 -0400 X-MC-Unique: LILXIoSIMeSbK38DOu3v0A-1 Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-68197de746fso709346a12.2 for ; Fri, 24 May 2024 00:40:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716536431; x=1717141231; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vuXQQ/QoUSZ0kyXep/CDerc5KOfI2DPPitPZJXtY6YU=; b=XDPEwRGSEqqPDN7nzq76kY21ck2dav2eE4Y3FLskmfQpviXZiw8imf7gGnr+YnIJU8 YiXB8NB8FKf13Tov54WK8Ienb2In+cZGOUXsqLTJ3HNzwvFtqjdZ/89jHKl1RolU5ddz RqAvpp26sEikzQahe4X5wdg24q5HVTjVxDXaK5vVRCXfBvsQZQiiSW/FxE3wlexfLMo4 qLj+HE2VwR3ILG/4Gz+0xXwQjNfRSZucx2WcrLR9jtmTNA313EqWvIv+5Ys8lvhQQTKe +PiH3IKBYrdtTs6jDYo5YTkgomxj2DDNz4TWl406bv0zmUg40MAVa/dMlTfnSLDJqLu+ jLLw== X-Forwarded-Encrypted: i=1; AJvYcCXOqdKpY0xuAWqIaOrHZnxa0ay5c1UdUr3Hjqs8CGIZxWcqtEGWM7NMLn9vSGGv3HUxSdtDxy9WzeI8cajUoGAxEKWyIS9lhVgLjKt5 X-Gm-Message-State: AOJu0YxyXJv2zOJMfl6XPUTEcJQge/tefsgzuaZaXdkZJvjs7jzkgb+4 AXxANaSj3K+6u0m7SXUsSatqKa5UiiSnZs1svsLmdwZCe3CI7rZsn4YWzhG5+P+6GUbHQYCZlRF 8dgUwShNEBId/FFr6jQzpjbvnKPKVefiyRXzYzg1d4fz6Psu4/FzcwWAchI1ARA== X-Received: by 2002:a05:6a20:72a8:b0:1b1:c745:5c66 with SMTP id adf61e73a8af0-1b212e64d95mr1966611637.54.1716536430401; Fri, 24 May 2024 00:40:30 -0700 (PDT) X-Received: by 2002:a05:6a20:72a8:b0:1b1:c745:5c66 with SMTP id adf61e73a8af0-1b212e64d95mr1966565637.54.1716536429531; Fri, 24 May 2024 00:40:29 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-68221891986sm613441a12.22.2024.05.24.00.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 00:40:28 -0700 (PDT) Date: Fri, 24 May 2024 15:38:18 +0800 From: Coiby Xu To: Baoquan He Cc: kexec@lists.infradead.org, Ondrej Kozina , Milan Broz , Thomas Staudt , Daniel P =?utf-8?B?LiBCZXJyYW5nw6k=?= , Kairui Song ,  Jan Pazdziora , Pingfan Liu , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Vivek Goyal , Eric Biederman Subject: Re: [PATCH v3 3/7] crash_dump: store dm keys in kdump reserved memory Message-ID: References: <20240425100434.198925-1-coxu@redhat.com> <20240425100434.198925-4-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=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: On Tue, May 21, 2024 at 11:42:52AM +0800, Baoquan He wrote: >On 04/25/24 at 06:04pm, Coiby Xu wrote: >> 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. >> >> Signed-off-by: Coiby Xu >> --- >> include/linux/crash_core.h | 3 ++ >> include/linux/crash_dump.h | 2 + >> include/linux/kexec.h | 4 ++ >> kernel/crash_dump_dm_crypt.c | 87 ++++++++++++++++++++++++++++++++++++ >> 4 files changed, 96 insertions(+) >> >> diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h >> index 98825b7e0ea6..1f3d5a4fa6c1 100644 >> --- a/include/linux/crash_core.h >> +++ b/include/linux/crash_core.h >> @@ -37,6 +37,9 @@ static inline void arch_kexec_unprotect_crashkres(void) { } >> #ifdef CONFIG_CRASH_DM_CRYPT >> int crash_sysfs_dm_crypt_keys_read(char *buf); >> int crash_sysfs_dm_crypt_keys_write(const char *buf, size_t count); >> +int crash_load_dm_crypt_keys(struct kimage *image); >> +#else >> +static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; } >> #endif >> >> #ifndef arch_crash_handle_hotplug_event >> 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; >> >> +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/include/linux/kexec.h b/include/linux/kexec.h >> index fc1e20d565d5..b6cedce66828 100644 >> --- a/include/linux/kexec.h >> +++ b/include/linux/kexec.h >> @@ -368,6 +368,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; >> }; >> >> /* kexec interface functions */ >> diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c >> index 847499cdcd42..b9997fb53351 100644 >> --- a/kernel/crash_dump_dm_crypt.c >> +++ b/kernel/crash_dump_dm_crypt.c >> @@ -1,4 +1,6 @@ >> // SPDX-License-Identifier: GPL-2.0-only >> +#include >> +#include >> #include >> #include >> >> @@ -111,3 +113,88 @@ int crash_sysfs_dm_crypt_keys_read(char *buf) >> return sprintf(buf, "%s\n", STATE_STR[state]); >> } >> EXPORT_SYMBOL(crash_sysfs_dm_crypt_keys_read); >> + >> +static int read_key_from_user_keying(struct dm_crypt_key *dm_key) >> +{ >> + const struct user_key_payload *ukp; >> + struct key *key; >> + >> + pr_debug("Requesting key %s", dm_key->key_desc); >> + key = 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 = user_key_payload_locked(key); >> + if (!ukp) >> + return -EKEYREVOKED; >> + >> + memcpy(dm_key->data, ukp->data, ukp->datalen); >> + dm_key->key_size = ukp->datalen; >> + pr_debug("Get dm crypt key (size=%u) %s: %8ph\n", dm_key->key_size, >> + dm_key->key_desc, dm_key->data); >> + return 0; >> +} >> + >> +static int build_keys_header(void) >> +{ >> + int i, r; >> + >> + for (i = 0; i < key_count; i++) { >> + r = read_key_from_user_keying(&keys_header->keys[i]); >> + if (r != 0) { >> + pr_err("Failed to read key %s\n", keys_header->keys[i].key_desc); >> + return r; >> + } >> + } >> + >> + return 0; >> +} >> + >> +int crash_load_dm_crypt_keys(struct kimage *image) >> +{ >> + struct kexec_buf kbuf = { >> + .image = image, >> + .buf_min = 0, >> + .buf_max = ULONG_MAX, >> + .top_down = false, >> + .random = true, >> + }; >> + >> + int r; >> + >> + if (state == FRESH) >> + return 0; >> + >> + if (key_count != keys_header->key_count) { >> + pr_err("Only record %u keys (%u in total)\n", key_count, >> + keys_header->key_count); >> + return -EINVAL; >> + } >> + >> + image->dm_crypt_keys_addr = 0; >> + r = build_keys_header(); >> + if (r) >> + return r; >> + >> + kbuf.buffer = keys_header; >> + kbuf.bufsz = keys_header_size; >> + >> + kbuf.memsz = kbuf.bufsz; >> + kbuf.buf_align = ELF_CORE_HEADER_ALIGN; >> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; >> + r = kexec_add_buffer(&kbuf); >> + if (r) { >> + vfree((void *)kbuf.buffer); >> + return r; >> + } >> + state = LOADED; >> + image->dm_crypt_keys_addr = kbuf.mem; >> + image->dm_crypt_keys_sz = kbuf.bufsz; >> + pr_debug("Loaded dm crypt keys at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >> + image->dm_crypt_keys_addr, kbuf.bufsz, kbuf.bufsz); > >Please use kexec_dprintk() instead to print debugging message. Thanks for pointing me to kexec_dprintk! I'll use kexec_dprintk. >And you don't worry this printing will leak the key position and the >information? Thanks for raising this concern! I'll remove the key position info as it seems kernel dyndbg can be easily enabled. -- Best regards, Coiby