From patchwork Fri Apr 1 01:31:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Coiby Xu X-Patchwork-Id: 12797836 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1835CC433F5 for ; Fri, 1 Apr 2022 01:33:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Uf73j6MUsj7nM4RP3K0XGueviZ2t8MBXz4APboZN63s=; b=YAFCO7Bzi0CBfU p7VS0CIEkkQykHQgwktB8N5FEtX//MN+Ar0D17BzXF9mfWH20LG1TKQXrqtybjGJvvpr5UOM9F9d1 Kwt082x1e+/sXwyzyau2EdwlfP+vZ9emWn+45EIiwv3u0VZ2llc40n+C0dt09YvZFIw3QOziuOQPu ETfK4UOAwyLZKARov5denrIKwMcPte8uuKGBWHvbYPyuEWZthtZQEq4hq+e+xnywbVDv+aNvcWuHt iMEySOFTswAYVBcrtnx/Dess98CqYc7xb3T1HfUW6ATEe1h24OQY/FX5EEkHvZmfT0mayV1gqTUeN MqzVVM4z4vF9f+Voub9w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1na68l-004Anv-8q; Fri, 01 Apr 2022 01:31:51 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1na68S-004AjE-5W for linux-arm-kernel@lists.infradead.org; Fri, 01 Apr 2022 01:31:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648776691; 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=XVmPCPO0oF3pRYu+F7qpHdcQzaq88pJ0kcN8pmyvV4M=; b=Ae+CQpwEDuPDhjSQ0Jh4PZjw7Yv7Xf9NmUA/+fOLR1/4uKj7UBjTpdFQDpbC/pBENim+Li kIlhfTuL6KlbTAIsiCzUK9ran6OKszco210TWK31Xq9/fT0zysWew9V4B4Htxzb5pcoWfJ qzsEoUQ7ous66im3UF6+6ZBnNrQVVNo= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-97-V0apz8BmNWSWBZ61bDy59w-1; Thu, 31 Mar 2022 21:31:29 -0400 X-MC-Unique: V0apz8BmNWSWBZ61bDy59w-1 Received: by mail-pj1-f69.google.com with SMTP id mw8-20020a17090b4d0800b001c717bb058eso2848650pjb.0 for ; Thu, 31 Mar 2022 18:31:29 -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=XVmPCPO0oF3pRYu+F7qpHdcQzaq88pJ0kcN8pmyvV4M=; b=XhwQfkx3f67HmD7KEu1nezHGhK2Do5Kf0gX42kxyR6XIrBuYmUmKqIN8pNSO0ap3YH pIE5xR1uh80wgkTGnQQ/FHvKyLTGRFmOguFpiMo3GMKgtYD0JiLBHEEsTfstdAJJ65L4 K/i4tN0pyCT3TLeiALwtOuN+AB9lbmp4ZQ9ko2Wc3yAdeTpoafLpPLlgVerzJktW3Wk6 PFfEN6/7SGMks89ZmnKgdDqLiDB2dRrOKujHXPRS/hIBblIOajfkjoR3y1X0RlGadzRP bU5tl8SyYIvUSOrmZLDVWHyBWiVoAD112a1ylOU+fDXt6nnmUncblkjMNs5g29nDULiE 7SZw== X-Gm-Message-State: AOAM531CI4nzMLGpGsgROyufgMGlRErO0qJtSF/wjGMIC99HgHCuMXgG O9i1HTTwDJgmcXP4b4Mi/y+lggGLEPkfMZWyvqWtUjEF+0wFTDHrV7lWthypy8FJelOyTz/pIzh aA8iXp3VE+2GjEpBZWouxrmMvTAKEz3j8Wqk= X-Received: by 2002:a17:903:20cc:b0:154:8c7f:1ed3 with SMTP id i12-20020a17090320cc00b001548c7f1ed3mr7854403plb.131.1648776688284; Thu, 31 Mar 2022 18:31:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyk8pGREHFgs4EKOdX9WUE7Kapxs8ssHrqs3uFxMkIE8BiXg/yu0dkJv+gF0OWTAflQuu0kcg== X-Received: by 2002:a17:903:20cc:b0:154:8c7f:1ed3 with SMTP id i12-20020a17090320cc00b001548c7f1ed3mr7854375plb.131.1648776688014; Thu, 31 Mar 2022 18:31:28 -0700 (PDT) Received: from localhost ([240e:3a1:31c:360:52fc:c968:cb41:efbc]) by smtp.gmail.com with ESMTPSA id p34-20020a056a000a2200b004cd49fc15e5sm759358pfh.59.2022.03.31.18.31.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Mar 2022 18:31:27 -0700 (PDT) From: Coiby Xu To: kexec@lists.infradead.org Cc: linux-arm-kernel@lists.infradead.org, Baoquan He , Dave Young , Will Deacon , "Eric W . Biederman" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" , linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)) Subject: [PATCH v5 2/3] kexec, KEYS: make the code in bzImage64_verify_sig generic Date: Fri, 1 Apr 2022 09:31:17 +0800 Message-Id: <20220401013118.348084-3-coxu@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220401013118.348084-1-coxu@redhat.com> References: <20220401013118.348084-1-coxu@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=coxu@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220331_183132_323975_2C1B0455 X-CRM114-Status: GOOD ( 14.30 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The code in bzImage64_verify_sig could make use of system keyrings including .buitin_trusted_keys, .secondary_trusted_keys and .platform keyring to verify signed kernel image as PE file. Make it generic so both x86_64 and arm64 can use it. Signed-off-by: Coiby Xu Reviewed-by: Michal Suchanek --- arch/x86/kernel/kexec-bzimage64.c | 13 +------------ include/linux/kexec.h | 7 +++++++ kernel/kexec_file.c | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c index 170d0fd68b1f..f73aab3fde33 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -531,17 +530,7 @@ static int bzImage64_cleanup(void *loader_data) #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len) { - int ret; - - ret = verify_pefile_signature(kernel, kernel_len, - VERIFY_USE_SECONDARY_KEYRING, - VERIFYING_KEXEC_PE_SIGNATURE); - if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) { - ret = verify_pefile_signature(kernel, kernel_len, - VERIFY_USE_PLATFORM_KEYRING, - VERIFYING_KEXEC_PE_SIGNATURE); - } - return ret; + return kexec_kernel_verify_pe_sig(kernel, kernel_len); } #endif diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 755fed183224..2fe39e946988 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -19,6 +19,7 @@ #include #include +#include #ifdef CONFIG_KEXEC_CORE #include @@ -196,6 +197,12 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi, const Elf_Shdr *relsec, const Elf_Shdr *symtab); int arch_kimage_file_post_load_cleanup(struct kimage *image); +#ifdef CONFIG_KEXEC_SIG +#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION +int kexec_kernel_verify_pe_sig(const char *kernel, + unsigned long kernel_len); +#endif +#endif int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf); extern int kexec_add_buffer(struct kexec_buf *kbuf); diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3720435807eb..754885b96aab 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -165,6 +165,23 @@ void kimage_file_post_load_cleanup(struct kimage *image) } #ifdef CONFIG_KEXEC_SIG +#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION +int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len) +{ + int ret; + + ret = verify_pefile_signature(kernel, kernel_len, + VERIFY_USE_SECONDARY_KEYRING, + VERIFYING_KEXEC_PE_SIGNATURE); + if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) { + ret = verify_pefile_signature(kernel, kernel_len, + VERIFY_USE_PLATFORM_KEYRING, + VERIFYING_KEXEC_PE_SIGNATURE); + } + return ret; +} +#endif + static int kexec_image_verify_sig(struct kimage *image, void *buf, unsigned long buf_len) {