Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2536879pxp; Fri, 18 Mar 2022 12:39:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk+O3R6JgYLKMAmhz05HJ2NmWQtvM6IBKJN8hdRsUlrS5YVpvkyZZtn6Tgik1o5m6WVD7j X-Received: by 2002:a05:6402:4252:b0:416:6cd8:e273 with SMTP id g18-20020a056402425200b004166cd8e273mr11435697edb.328.1647632389835; Fri, 18 Mar 2022 12:39:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647632389; cv=none; d=google.com; s=arc-20160816; b=tuNBO4G7xieDutxwIBrDM17pjiEOM3iyaX/XWlI2P8ZGKi1KQiYFdJby3+1GwMnX+h R5+XF0Ri94uJG+pRVv18SYraYh0Mo5wjk9cbdAI/HTkXPQQuAkRy4X3bRNs3kHLehWTz 0W73EdAgF+JoqEL1wgvl3XEGAQ+uZZuAC+hrZ2044KEw3uK7hkHCt1WL66/Piyon2dAV 0kuYQZSsk8ssAmhaUaeBYgvf2/19j/hFjD12HPGTlazMgDFzGj88DsP7tAyzJEXKV0ec k+rSZrFp23jnqSkdghkFQyrNIgmNUtR4rkWOsF01ar+h6v5qypCllTBl18Yn7A3BAOsq qsDA== 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=PODFzb3mjImlWQPxxoECM7lRSKqdrL/May27LmTIUmk=; b=u6xoDY0R97XaHM/Tm7Si8m3KYgGhjnxgovQpez1Y5ir1Gf44urKrVsMFIVR826n0+T MxyxJKQetFTbYUSiELFKz9KUuS3G0xu1QzwJiu/JFozllsOM3fKuuac6G+z1ikvaFXtQ Wy9xpdmaAPhRX3TvmyJ2CvXb8p6rNr3fxbQCz0kFnGh4u/XNeHTaL4LOT75CX30p7mVw gZINrHTiROv7EntYqjWv8VLctcc/2LatRFQ51RqbMnJ38UWMAzMTwaomfalvd+iqvynY jEpvD2XiR7b3JIaDE4GYFrqP8dDMeoZNmf1MHfThTQJYPC2YoaZPDOm6kzEUTQ8/NShL wQnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TnppTak9; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j6-20020a170906104600b006df76385bb1si1908879ejj.81.2022.03.18.12.39.20; Fri, 18 Mar 2022 12:39:49 -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=TnppTak9; 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 S235009AbiCRKf7 (ORCPT + 99 others); Fri, 18 Mar 2022 06:35:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234992AbiCRKfy (ORCPT ); Fri, 18 Mar 2022 06:35:54 -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 ESMTP id D82EB255A0 for ; Fri, 18 Mar 2022 03:34:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647599675; 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=PODFzb3mjImlWQPxxoECM7lRSKqdrL/May27LmTIUmk=; b=TnppTak9YM95yoJsPt9n0JEIKOasxvJ7jqw00wNvZIm7qs4iXLE9oobtGnUQZH0SHxo2tG iQr7CYfHVOceekEbshaO4S6izRZKycCn/A6z10+6T3ZCTPjkVUW2rmyuzCYJVCgZg96+bX cHDe1NP+E+H/PCl1QbLiwxIhW8Zn8UM= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-227-Aq4d69K9MqirLeG7oJgbhw-1; Fri, 18 Mar 2022 06:34:33 -0400 X-MC-Unique: Aq4d69K9MqirLeG7oJgbhw-1 Received: by mail-pf1-f197.google.com with SMTP id s3-20020aa78bc3000000b004fa59ef2a34so2143351pfd.19 for ; Fri, 18 Mar 2022 03:34:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PODFzb3mjImlWQPxxoECM7lRSKqdrL/May27LmTIUmk=; b=hp3PWRXK58OLg1GifACZJKCCc3AC9IB8WMMVHfUaLtnJgmvhw/yElFoqdzclmzIKL3 wrhVNomCpbs0AalVp3RfMjM47zpdT3L4c9NvSu4Ii7OBSmuvtaSowZTdKSszaVxI+/l7 wmCdXixz4hw9bOx4uc0XKdac+34aPoOFSdewgLvfJbYrrFLAWqDFu6IqqUJd25acP0cU M3NyZmbySoq8caTJbGKd9AJE/W+yyKqu3ia3nkK2UO8qtxFIqKBpVkGx6lzQLq8rfmWA qFmZvp2plTKOzAxRKvSxuRLHU5CwiPc71Da/xf6+PyZVnEu6A6cfWPIDKXEsDpnUHcYN UljQ== X-Gm-Message-State: AOAM5311ghNTzbQVan8eUX7j5G12SAOoUFnpYHmx1ZBpfQpKN6KEy1Z+ Ie9Lz8y3ZICoz0YRqoy2IgDYd8NtDS+qcluHo5ApnpkQPgf/6m8Wx2ZisCLMbAOEvdBNxYC/22F yCpVGlZUuw7xXD3FBovXHfR4w X-Received: by 2002:a05:6a00:1152:b0:4be:ab79:fcfa with SMTP id b18-20020a056a00115200b004beab79fcfamr9598492pfm.3.1647599671395; Fri, 18 Mar 2022 03:34:31 -0700 (PDT) X-Received: by 2002:a05:6a00:1152:b0:4be:ab79:fcfa with SMTP id b18-20020a056a00115200b004beab79fcfamr9598457pfm.3.1647599671120; Fri, 18 Mar 2022 03:34:31 -0700 (PDT) Received: from localhost ([240e:3a1:2e5:800:f995:6136:f760:a3d0]) by smtp.gmail.com with ESMTPSA id s20-20020a056a00179400b004f709998d13sm9657970pfg.10.2022.03.18.03.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 03:34:30 -0700 (PDT) From: Coiby Xu To: kexec@lists.infradead.org Cc: Milan Broz , Thomas Staudt , Kairui Song , dm-devel@redhat.com, Mike Snitzer , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, Alasdair Kergon , Eric Biederman Subject: [RFC 1/4] kexec, dm-crypt: receive LUKS master key from dm-crypt and pass it to kdump Date: Fri, 18 Mar 2022 18:34:20 +0800 Message-Id: <20220318103423.286410-2-coxu@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220318103423.286410-1-coxu@redhat.com> References: <20220318103423.286410-1-coxu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.6 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 After receiving the LUKS master key from driver/md/dm-crypt, kdump has 1 hour at maximum to ask kexec to pass the key before the key gets wiped by kexec. And after kdump retrieves the key, the key will be wiped immediately. Signed-off-by: Coiby Xu --- drivers/md/dm-crypt.c | 5 +++- include/linux/kexec.h | 3 ++ kernel/kexec_core.c | 66 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index d4ae31558826..41f9ca377312 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -41,6 +41,7 @@ #include #include +#include #include "dm-audit.h" @@ -2388,6 +2389,8 @@ static int crypt_setkey(struct crypt_config *cc) unsigned subkey_size; int err = 0, i, r; + /* save master key to kexec */ + kexec_save_luks_master_key(cc->key, cc->key_size); /* Ignore extra keys (which are used for IV etc) */ subkey_size = crypt_subkey_size(cc); @@ -3580,6 +3583,7 @@ static int crypt_message(struct dm_target *ti, unsigned argc, char **argv, DMWARN("not suspended during key manipulation."); return -EINVAL; } + if (argc == 3 && !strcasecmp(argv[1], "set")) { /* The key size may not be changed. */ key_size = get_key_size(&argv[2]); @@ -3587,7 +3591,6 @@ static int crypt_message(struct dm_target *ti, unsigned argc, char **argv, memset(argv[2], '0', strlen(argv[2])); return -EINVAL; } - ret = crypt_set_key(cc, argv[2]); if (ret) return ret; diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 0c994ae37729..91507bc684e2 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -205,6 +205,9 @@ int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf); extern int kexec_add_buffer(struct kexec_buf *kbuf); int kexec_locate_mem_hole(struct kexec_buf *kbuf); +extern int kexec_pass_luks_master_key(void **addr, unsigned long *sz); +extern int kexec_save_luks_master_key(u8 *key, unsigned int key_size); + /* Alignment required for elf header segment */ #define ELF_CORE_HEADER_ALIGN 4096 diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 68480f731192..86df36b71443 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1218,3 +1218,69 @@ void __weak arch_kexec_protect_crashkres(void) void __weak arch_kexec_unprotect_crashkres(void) {} + + +static u8 *luks_master_key; +static unsigned int luks_master_key_size; + +void wipe_luks_master_key(void) +{ + if (luks_master_key) { + memset(luks_master_key, 0, luks_master_key_size * sizeof(u8)); + kfree(luks_master_key); + luks_master_key = NULL; + } +} + +static void _wipe_luks_master_key(struct work_struct *dummy) +{ + wipe_luks_master_key(); +} + +static DECLARE_DELAYED_WORK(wipe_luks_master_key_work, _wipe_luks_master_key); + +static unsigned __read_mostly wipe_key_delay = 3600; /* 1 hour */ + +int kexec_save_luks_master_key(u8 *key, unsigned int key_size) +{ + if (luks_master_key) { + memset(luks_master_key, 0, luks_master_key_size * sizeof(u8)); + kfree(luks_master_key); + } + + luks_master_key = kmalloc(key_size * sizeof(u8), GFP_KERNEL); + + if (!luks_master_key) + return -ENOMEM; + memcpy(luks_master_key, key, key_size * sizeof(u8)); + luks_master_key_size = key_size; + pr_debug("LUKS master key (size=%u): %64ph\n", key_size, luks_master_key); + schedule_delayed_work(&wipe_luks_master_key_work, + round_jiffies_relative(wipe_key_delay * HZ)); + return 0; +} +EXPORT_SYMBOL(kexec_save_luks_master_key); + +int kexec_pass_luks_master_key(void **addr, unsigned long *sz) +{ + unsigned long luks_key_sz; + unsigned char *buf; + unsigned int *size_ptr; + + if (!luks_master_key) + return -EINVAL; + + luks_key_sz = sizeof(unsigned int) + luks_master_key_size * sizeof(u8); + + buf = vzalloc(luks_key_sz); + if (!buf) + return -ENOMEM; + + size_ptr = (unsigned int *)buf; + memcpy(size_ptr, &luks_master_key_size, sizeof(unsigned int)); + memcpy(size_ptr + 1, luks_master_key, luks_master_key_size * sizeof(u8)); + *addr = buf; + *sz = luks_key_sz; + wipe_luks_master_key(); + return 0; +} -- 2.34.1