From patchwork Fri Mar 18 09:41:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Coiby Xu X-Patchwork-Id: 12785078 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 D6C56C433EF for ; Fri, 18 Mar 2022 09:42:47 +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=ZOvGbbAVT1xZlH pEKeMNt01CEcyCLs1Ba019ynSdlwr5y7ZronXiKuCGfjvlJx3WrBCtMkT2IuenLxyOHQ59iV9MGBC FbLRsz414MVG+lQMkfW880fHds1RM3tS8/ir0lbgXCqQ1NvTKB7rlCvPCrRYOEQtQMI19ZdlxIbk5 kGUOr+U/KMpQhYPAODPRmrPSvfO8S/de0SUrPkfW/zPSWIy1GKZ9OuA3q++nj/cMtaEbEFP3KkLx1 +DCD07ygdhiTnobpFWlNQGorTHwpq2tpWBtS/fSL3ltGV/NzEzN0A8Z5mVzLjH4cJE+PMFEjd8YMc e1XQA6nh6RR4VuIckQcw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nV973-00102m-8b; Fri, 18 Mar 2022 09:41:37 +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 1nV96h-000zuH-06 for linux-arm-kernel@lists.infradead.org; Fri, 18 Mar 2022 09:41:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647596474; 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=WdLsD6qLqu8ka1Z8ad0N8Hc5Pc0ewwAGsVIzk/yxaVSNJ7eARarvEhPaIo6+/EYqbCVd1k 1o6U++qNOkSjgLl1FacIyzrfyH0hj85TBnpaRDzLPk2eEv0cUE3foHKPuU1ThDv7Z8vZZf C2ZQhA8zI8lC8PDxcLNiErj09aUHYGw= Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-299-c2b24lKTMe6O2RpgJ-T-nw-1; Fri, 18 Mar 2022 05:41:12 -0400 X-MC-Unique: c2b24lKTMe6O2RpgJ-T-nw-1 Received: by mail-pf1-f198.google.com with SMTP id i2-20020a056a00224200b004fa60c248a1so1852219pfu.13 for ; Fri, 18 Mar 2022 02:41:12 -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=EFbrWNmf9XOPMmRGyrdExd+tPqOpPTeDC3u2yJJhgOUz7v+tqmmD3siojR2oMz3mNw +htCN9LJE2BigBgsKfn6wvhGBFg+aHaQecdV9D9jVFjlkWarwEjFFFcnObsiZ0/NziwD 3yUNcukygA1xR0tuAin2b/llkhUumFIJtmXJTI9aEhVEAHxCxe6nwgC1qkGjx1Exmzbp 62ZS2mkLCiMZSHivX8b8lywbfG3iOjA/IruHDlzeVe3s7lbVxEA0Bi8yU0QTWHitYI5M MpV2aDC31nhfZ0Nlv/XGzRd78cbRvzq0nK+2h9oSIX6lhAFdvt3A6/LPhRgdGJO7Yq1U NkYg== X-Gm-Message-State: AOAM532iKUBr2oYOAgsY5zgsXKF++DgrerVFij9ldWMjbaHH+ZJ76ocE AP+mp5JBZdD9eqhsCpNxzzwG+wuGNgOQrzO470yszw6ezT16iQMd4KOlUPNcQ6Tu1Tw4MUi3rJM WOg0KHxTYKD/5UjNKtv1f64nzdrdfrEGgoFw= X-Received: by 2002:a63:ab47:0:b0:375:5d05:9f79 with SMTP id k7-20020a63ab47000000b003755d059f79mr7232003pgp.192.1647596471696; Fri, 18 Mar 2022 02:41:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyC67EGQnlUHMa8v+f0Jvrco6hTVeignBbFV846ZQZwUN1ugwR1tKrHp9uyTiLPMVPVkFvsXA== X-Received: by 2002:a63:ab47:0:b0:375:5d05:9f79 with SMTP id k7-20020a63ab47000000b003755d059f79mr7231986pgp.192.1647596471470; Fri, 18 Mar 2022 02:41:11 -0700 (PDT) Received: from localhost ([240e:3a1:2e5:800:f995:6136:f760:a3d0]) by smtp.gmail.com with ESMTPSA id q16-20020a056a00151000b004f7cdeddfffsm9107299pfu.115.2022.03.18.02.41.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 02:41:10 -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 v4 2/3] kexec, KEYS: make the code in bzImage64_verify_sig generic Date: Fri, 18 Mar 2022 17:41:00 +0800 Message-Id: <20220318094101.274950-3-coxu@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220318094101.274950-1-coxu@redhat.com> References: <20220318094101.274950-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-20220318_024115_179748_E3B384BF X-CRM114-Status: GOOD ( 14.31 ) 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 --- 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) {