From patchwork Fri Nov 4 11:29:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Coiby Xu X-Patchwork-Id: 15480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp341872wru; Fri, 4 Nov 2022 04:40:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5l7VkwF2AZie71Q5R736YZN48sJO5cUb72uVrnify0upbW1xirdMrnmGMqmTh1GX+LOzgM X-Received: by 2002:a05:6a00:2187:b0:56d:4d02:5776 with SMTP id h7-20020a056a00218700b0056d4d025776mr29278181pfi.4.1667562031136; Fri, 04 Nov 2022 04:40:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667562031; cv=none; d=google.com; s=arc-20160816; b=J7qo7F10jafX3jefBFbVP6/JPX07vdMn4w4pP7oek+q4RGxq1tgtlfUd1TfRFjGXd0 PJGcw7Y6n3qL8ZOeqxyUd7jkrJwuNagA5eMS6+ZLdcAZvSiZkMTv7BzhlDkzH/Y5ut8A 4Bqt8ZI/Lqd98Ykssl+KrEDnRBOiHPgHpRpsfjI2lqpA3tJ27Y6UKABPNx15C4d2KxEe KZvjFrSgl4XdTnyCc2bqehUXLCc8jduGo4mbVY0Hn0EQ/ZQi014/7Y926TqSd3OHy4rz aAGNZFpFCjDoqxZKkcxsafOK+v30jLVfJAhioHxi6YOJSouuujsHdZyh3N6OeylKm2Vr XU2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=s1xZ0vxushGJcgjg7hlatryVRu1/dXqezckzHs4WjGk=; b=HatPnSNhVujSCHO+5QDml3SvKnG6kTVH3ZtIbCXYwQ2Wqy+G8uWR7JLQ+yxvL0cR/W PkLzMtxf4qP/8JcylnRI/41DJseJZVBcA5ptbXjxpeigvP5qAjLsqdPFHksRmSJbjMFD flbBiuNh674Tjs3Rg/0+etP4V63UOIRv2TWq0gp2OsNxgLddzEN5lxYV9g5mEAx22tvA ahAT71o7mozv52Km+g67MkIU8LjmoxSD7KIw8P9NuWy1tQycSDSx8oKBqqBGAEO9zu8K Shilp3Z1fkSia3YnPI+4T4GOjIfDw6rS17gqDgcsYpRBw1meqIqrG3S1L0KIMDB++nUf 6ecA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=COW5uz0a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3-20020a6546c3000000b0045750bb568asi2160408pgr.15.2022.11.04.04.40.09; Fri, 04 Nov 2022 04:40:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=COW5uz0a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231540AbiKDLbn (ORCPT + 99 others); Fri, 4 Nov 2022 07:31:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229804AbiKDLbk (ORCPT ); Fri, 4 Nov 2022 07:31:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC822AF for ; Fri, 4 Nov 2022 04:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667561446; 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=s1xZ0vxushGJcgjg7hlatryVRu1/dXqezckzHs4WjGk=; b=COW5uz0ajxL5hZkFMm7LmdxJrrcernOz1LjEw1/Ht3FNNvzkahG5YpsV0qFncjoru2MukE OKM701B3t2LR7vO1/Ce7RbSI1QTwn7uYaYegx0yW6/mJg2BdUXuz3RGW6Vrk1p4Lu/1x2d sTUWQTV3V0W8oAtBIY/vEvMUsIjcIF4= 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_128_GCM_SHA256) id us-mta-1-P2Eaq2iKMt-wSsMCpOVQQA-1; Fri, 04 Nov 2022 07:30:29 -0400 X-MC-Unique: P2Eaq2iKMt-wSsMCpOVQQA-1 Received: by mail-pl1-f198.google.com with SMTP id z15-20020a170903018f00b0018862d520fbso1622208plg.11 for ; Fri, 04 Nov 2022 04:30:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=s1xZ0vxushGJcgjg7hlatryVRu1/dXqezckzHs4WjGk=; b=ZBnHE5lnNCmnbZRO0W47nvp5qAFY4XSpZ57hqbrTlov1vCBR+vfIbameNJYxOpUGMc p6AQDKnCGFBcoNMZUb2UtYoysekbZhPP5ozK1vzt9xj/hrkHbWSsdFujZTcoZeYprm5H hJfbp5Pv99xrRo0zu8UKds+J0xsgmt6SJI9cq4hTm+OrUXKtHXN7PSZK427ujO7oHmQL jAAezF6nr3jE686lXy/GAWiys3d1+nGlhaF8tqSqjAtlmNleWPtV+ODmoRW3L4tMMFNx tISR3CRzZIEYO0pLJxocZWpjDPX1Tzr9/hzmAmv3yQVEchYVLSLraqasO/kUKUlihNEl a6/g== X-Gm-Message-State: ACrzQf1eWIxvnPJLxmMzAjvWzJ0A3zSP82h1esparu3e9tBJsv/1YRLK Bkm9FGU/T677rsMtfrfs0ft9WqOjtcXKuKjJDDX6QW4+Q96SPkRhJLwH0X3xJM+++pUN27gFwIY oFN6f837zfKNDAqRaVuIAfGFs X-Received: by 2002:a17:90b:4f8a:b0:213:48f0:296f with SMTP id qe10-20020a17090b4f8a00b0021348f0296fmr53282527pjb.140.1667561428157; Fri, 04 Nov 2022 04:30:28 -0700 (PDT) X-Received: by 2002:a17:90b:4f8a:b0:213:48f0:296f with SMTP id qe10-20020a17090b4f8a00b0021348f0296fmr53282509pjb.140.1667561427911; Fri, 04 Nov 2022 04:30:27 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id p12-20020a170902a40c00b00186b1bfbe79sm2429710plq.66.2022.11.04.04.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 04:30:27 -0700 (PDT) From: Coiby Xu To: kexec@lists.infradead.org Cc: Milan Broz , Thomas Staudt , Kairui Song , dm-devel@redhat.com, Jan Pazdziora , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, Vivek Goyal Subject: [RFC v2 2/5] crash_dump: save the LUKS volume key temporarily Date: Fri, 4 Nov 2022 19:29:57 +0800 Message-Id: <20221104113000.487098-3-coxu@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221104113000.487098-1-coxu@redhat.com> References: <20221104113000.487098-1-coxu@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748565524528022938?= X-GMAIL-MSGID: =?utf-8?q?1748565524528022938?= After having the volume key, crytpsetup/systemd-cryptsetup saves the volume key as a logon key to its thread keyring and this key is destroyed immediately with the terminated thread. So a temporary copy of the volume key is needed in order to later save it to kdump reserved memory when the crash kernel is loaded later. crytpsetup/systemd-cryptsetup will write the key description to /sys/kernel/crash_luks_volume_key so the kernel will read the logon key and save a temporary copy for later user. kdump has 1 hour at maximum to get the temporary copy before the key gets wiped. And after kdump retrieves the key, the key will be wiped immediately. Signed-off-by: Coiby Xu --- include/linux/crash_core.h | 2 + kernel/crash_dump.c | 88 ++++++++++++++++++++++++++++++++++++++ kernel/ksysfs.c | 19 ++++++++ 3 files changed, 109 insertions(+) diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index de62a722431e..596d83b8f362 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -83,5 +83,7 @@ int parse_crashkernel_high(char *cmdline, unsigned long long system_ram, unsigned long long *crash_size, unsigned long long *crash_base); int parse_crashkernel_low(char *cmdline, unsigned long long system_ram, unsigned long long *crash_size, unsigned long long *crash_base); +int crash_sysfs_luks_volume_key_write(const char *key_des, size_t count); +int crash_pass_temp_luks_volume_key(void **addr, unsigned long *sz); #endif /* LINUX_CRASH_CORE_H */ diff --git a/kernel/crash_dump.c b/kernel/crash_dump.c index 92da32275af5..9c202bffbb8d 100644 --- a/kernel/crash_dump.c +++ b/kernel/crash_dump.c @@ -5,6 +5,7 @@ #include #include +#include /* * stores the physical address of elf header of crash image * @@ -39,3 +40,90 @@ static int __init setup_elfcorehdr(char *arg) return end > arg ? 0 : -EINVAL; } early_param("elfcorehdr", setup_elfcorehdr); + +static u8 *luks_volume_key; +static unsigned int luks_volume_key_size; + +void wipe_luks_volume_key(void) +{ + if (luks_volume_key) { + memset(luks_volume_key, 0, luks_volume_key_size * sizeof(u8)); + kfree(luks_volume_key); + luks_volume_key = NULL; + } +} + +static void _wipe_luks_volume_key(struct work_struct *dummy) +{ + wipe_luks_volume_key(); +} + +static DECLARE_DELAYED_WORK(wipe_luks_volume_key_work, _wipe_luks_volume_key); + +static unsigned __read_mostly wipe_key_delay = 3600; /* 1 hour */ + +static int crash_save_temp_luks_volume_key(const char *key_desc, size_t count) +{ + const struct user_key_payload *ukp; + struct key *key; + + + if (luks_volume_key) { + memset(luks_volume_key, 0, luks_volume_key_size * sizeof(u8)); + kfree(luks_volume_key); + } + + pr_debug("Requesting key %s", key_desc); + key = request_key(&key_type_logon, key_desc, NULL); + + if (IS_ERR(key)) { + pr_debug("No such key %s", key_desc); + return PTR_ERR(key); + } + + ukp = user_key_payload_locked(key); + if (!ukp) + return -EKEYREVOKED; + + luks_volume_key = kmalloc(ukp->datalen, GFP_KERNEL); + if (!luks_volume_key) + return -ENOMEM; + memcpy(luks_volume_key, ukp->data, ukp->datalen); + luks_volume_key_size = ukp->datalen; + pr_debug("LUKS master key (size=%u): %8ph\n", luks_volume_key_size, luks_volume_key); + schedule_delayed_work(&wipe_luks_volume_key_work, + round_jiffies_relative(wipe_key_delay * HZ)); + return 0; +} + +int crash_sysfs_luks_volume_key_write(const char *key_desc, size_t count) +{ + if (!is_kdump_kernel()) + return crash_save_temp_luks_volume_key(key_desc, count); + return -EINVAL; +} +EXPORT_SYMBOL(crash_sysfs_luks_volume_key_write); + +int crash_pass_temp_luks_volume_key(void **addr, unsigned long *sz) +{ + unsigned long luks_key_sz; + unsigned char *buf; + unsigned int *size_ptr; + + if (!luks_volume_key) + return -EINVAL; + + luks_key_sz = sizeof(unsigned int) + luks_volume_key_size * sizeof(u8); + + buf = vzalloc(luks_key_sz); + if (!buf) + return -ENOMEM; + + size_ptr = (unsigned int *)buf; + memcpy(size_ptr, &luks_volume_key_size, sizeof(unsigned int)); + memcpy(size_ptr + 1, luks_volume_key, luks_volume_key_size * sizeof(u8)); + *addr = buf; + *sz = luks_key_sz; + wipe_luks_volume_key(); + return 0; +} diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c index b1292a57c2a5..e7a7433cb951 100644 --- a/kernel/ksysfs.c +++ b/kernel/ksysfs.c @@ -135,6 +135,24 @@ static ssize_t vmcoreinfo_show(struct kobject *kobj, } KERNEL_ATTR_RO(vmcoreinfo); +static ssize_t crash_luks_volume_key_show(struct kobject *kobj, + struct kobj_attribute *attr, + char *buf) +{ + return 0; +} + +static ssize_t crash_luks_volume_key_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int ret; + + ret = crash_sysfs_luks_volume_key_write(buf, count); + return ret < 0 ? ret : count; +} +KERNEL_ATTR_RW(crash_luks_volume_key); + #endif /* CONFIG_CRASH_CORE */ /* whether file capabilities are enabled */ @@ -223,6 +241,7 @@ static struct attribute * kernel_attrs[] = { #endif #ifdef CONFIG_CRASH_CORE &vmcoreinfo_attr.attr, + &crash_luks_volume_key_attr.attr, #endif #ifndef CONFIG_TINY_RCU &rcu_expedited_attr.attr,