From patchwork Wed Apr 25 06:26:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10361749 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 334846032C for ; Wed, 25 Apr 2018 06:36:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2288F28F37 for ; Wed, 25 Apr 2018 06:36:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1743F28F60; Wed, 25 Apr 2018 06:36:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 84FC928F37 for ; Wed, 25 Apr 2018 06:36:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=BCgiSgFyPIQpvEThK/WawcwrUxPK+GYp5KF8eulUDW0=; b=P6dE0M+nMmzdafyJxxKmHgtviO ZFsx049QZqDhL+vFe3lAIWKL21u5VllYtIC15KUSyuF8L83zdGJ+yUTXuY4XpBX0JveQSxVUC4pzY HebbmtkmP8Y1hpkniu5XvHHlvRCJR9julDnIgA2B13XXyZbGVhH6R7bU/oE/QfEfECFBIfESzcK8/ RKhx1ytR2tfaeDCDqamDqObH6KA7xQxAuceadHOXMikwSryXVXflxHR+ns0Hz9zHVz5QRLuuD4Nyi a3jEoepOTNCGh0lWteNOdP8bfLiZUkxj6PnCUj6OVL7uLTw/QZDCPmxELWpjL9K1nQe1QZwJcbunu qFScXGrw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBE2o-0004No-Dz; Wed, 25 Apr 2018 06:36:46 +0000 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBDtv-0006XA-58 for linux-arm-kernel@lists.infradead.org; Wed, 25 Apr 2018 06:27:40 +0000 Received: by mail-pf0-x244.google.com with SMTP id p15so14305921pff.11 for ; Tue, 24 Apr 2018 23:27:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Tp+ZjioygloSn7ojZ/L8ExtDAHDB6CBDOV7WCLXOlSA=; b=itskFRdxXdpVdmZbHFFCQj3l/1j9tBslwRvw0fmgpBor2YXdtoRv6L5JqrN3IMi8R+ ODqRXKa66DPkk9ZDrjhYEc2DQgphOSeLKLPe5vg1/T8FMp9b79wVI//AswN+ix6b9jd1 SQc2dUD+xOcvIrtIuV0j2gKoQsnJzwyCDiVBg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Tp+ZjioygloSn7ojZ/L8ExtDAHDB6CBDOV7WCLXOlSA=; b=B+G8MBk3sUxcBQtrqgY7FCD0Gmpm6xL37fyEGWo0euMN7Zgbgx6y3cHRBDnK7tBE1/ EYOSndX43aA9m09DC6LfniCaDLVwJV5Z6KG/P7ovZXlWp5sccZMJkcD1sdg8ZyJDhdGz nFhKOd7ZjlgCeqo9ttNc9zSWc4R8g/2e9o/ACb7OAwLhO94Rcrky6CBRej/u2+bInm84 7fiSY92LwovqSwqMq1jMcaf+hnnJ9Eukl1J/qjcFFLaKkXv6GpgRaTDlC3dAEv0qvLPh vISZkI26cG2T1k4LN4xt8x83Vyqa0i5dpCdycmT/j6xsx3vaxtKcWtFGLRYovPtQQIFx qxpw== X-Gm-Message-State: ALQs6tCUgpr849GZQSLudWRZt84wkE5cXQO12rBtLAmshjGjXKknjCNq Dj+cNTl123Bg3pad9x7+9ZbBAQ== X-Google-Smtp-Source: AIpwx4/J1suBIDaPnx6kEJNukLgoDLCk6iQezT48bCdMPMqyYO9zuZxH2RWKGY+it/v3D6QzmGq3jw== X-Received: by 2002:a17:902:20eb:: with SMTP id v40-v6mr27843684plg.277.1524637651318; Tue, 24 Apr 2018 23:27:31 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id l3sm34094124pfg.49.2018.04.24.23.27.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 23:27:30 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de Subject: [PATCH v9 10/11] arm64: kexec_file: add kernel signature verification support Date: Wed, 25 Apr 2018 15:26:28 +0900 Message-Id: <20180425062629.29404-11-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425062629.29404-1-takahiro.akashi@linaro.org> References: <20180425062629.29404-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180424_232735_278592_EBCFD7FE X-CRM114-Status: GOOD ( 16.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ard.biesheuvel@linaro.org, bhsharma@redhat.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro , james.morse@arm.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP With this patch, kernel verification can be done without IMA security subsystem enabled. Turn on CONFIG_KEXEC_VERIFY_SIG instead. On x86, a signature is embedded into a PE file (Microsoft's format) header of binary. Since arm64's "Image" can also be seen as a PE file as far as CONFIG_EFI is enabled, we adopt this format for kernel signing. You can create a signed kernel image with: $ sbsign --key ${KEY} --cert ${CERT} Image Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/Kconfig | 24 ++++++++++++++++++++++++ arch/arm64/include/asm/kexec.h | 16 ++++++++++++++++ arch/arm64/kernel/kexec_image.c | 15 +++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index d8f0dcdb8b96..5c772601840d 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -857,6 +857,30 @@ config KEXEC_FILE for kernel and initramfs as opposed to list of segments as accepted by previous system call. +config KEXEC_VERIFY_SIG + bool "Verify kernel signature during kexec_file_load() syscall" + depends on KEXEC_FILE + help + Select this option to verify a signature with loaded kernel + image. If configured, any attempt of loading a image without + valid signature will fail. + + In addition to that option, you need to enable signature + verification for the corresponding kernel image type being + loaded in order for this to work. + +config KEXEC_IMAGE_VERIFY_SIG + bool "Enable Image signature verification support" + default y + depends on KEXEC_VERIFY_SIG + depends on EFI && SIGNED_PE_FILE_VERIFICATION + help + Enable Image signature verification support. + +comment "Image signature verification is missing yet" + depends on KEXEC_VERIFY_SIG + depends on !EFI || !SIGNED_PE_FILE_VERIFICATION + config CRASH_DUMP bool "Build kdump crash kernel" help diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 77f05bcf6a42..891f2484969d 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -133,6 +133,7 @@ struct arm64_image_header { }; static const u8 arm64_image_magic[4] = {'A', 'R', 'M', 0x64U}; +static const u8 arm64_image_pe_sig[2] = {'M', 'Z'}; /** * arm64_header_check_magic - Helper to check the arm64 image header. @@ -154,6 +155,21 @@ static inline int arm64_header_check_magic(const struct arm64_image_header *h) && h->magic[3] == arm64_image_magic[3]); } +/** + * arm64_header_check_pe_sig - Helper to check the arm64 image header. + * + * Returns non-zero if 'MZ' signature is found. + */ + +static inline int arm64_header_check_pe_sig(const struct arm64_image_header *h) +{ + if (!h) + return 0; + + return (h->pe_sig[0] == arm64_image_pe_sig[0] + && h->pe_sig[1] == arm64_image_pe_sig[1]); +} + extern const struct kexec_file_ops kexec_image_ops; struct kimage; diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c index 2b3baf7285e0..7c11beefe65f 100644 --- a/arch/arm64/kernel/kexec_image.c +++ b/arch/arm64/kernel/kexec_image.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,9 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len) if ((kernel_len < sizeof(*h)) || !arm64_header_check_magic(h)) return -EINVAL; + pr_debug("PE format: %s\n", + (arm64_header_check_pe_sig(h) ? "yes" : "no")); + return 0; } @@ -78,7 +82,18 @@ static void *image_load(struct kimage *image, return ERR_PTR(ret); } +#ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG +static int image_verify_sig(const char *kernel, unsigned long kernel_len) +{ + return verify_pefile_signature(kernel, kernel_len, NULL, + VERIFYING_KEXEC_PE_SIGNATURE); +} +#endif + const struct kexec_file_ops kexec_image_ops = { .probe = image_probe, .load = image_load, +#ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG + .verify_sig = image_verify_sig, +#endif };