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: 134233 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp471640lji; Tue, 24 Apr 2018 23:28:05 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/JWBV7O7bXHp9J0HBEHr0otYDcVSDqv8ueSqP5BM8McLUU1Oh6hWJUEszNHFEIGr3fzvgU X-Received: by 2002:a17:902:e8:: with SMTP id a95-v6mr27861192pla.274.1524637685612; Tue, 24 Apr 2018 23:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524637685; cv=none; d=google.com; s=arc-20160816; b=WvI7xfsN8fU55Dgk+WLffw9Q+vldmttaP34OHWq6wZ69ZKcacjXYgfxb7cJQKVc/PG esC9lwi0L28vL++nJYmUi6e0Ri1qiv1xGk7wA0t1rMdn/R1htUdYf2G26LJ1k//lKxHZ cQ1tgm90NRD0im4+PXgpTHnRC4DUYDwrkM0tvG6KLyGaNonSyVvgQXv4xOcvrljBgvOj Nfuzel7dHmhQZpSCE1iqjjYpc6RKmm291AjXM9ZS6Q8oxjdHM48+sK73Ljitpl+cjMEv AEtw7NXX8nXi0XhVKq26eudBdiri/DTL5v+jEaFV+Ac1CjiplVdP2TJbYTtva3ijI3RS KgxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Tp+ZjioygloSn7ojZ/L8ExtDAHDB6CBDOV7WCLXOlSA=; b=HnI0z6sXaa1mVEnX/sanWfNvA0RJxB4iWcg3wlt2HHmTLCwEjCS440Lh1COO+MBtZm RNTCbbuaFSdFL1dY6IBsWQLuEmkaOo9bMGgUk70qeBFpZ9H8hYrG+dwZwz1ZtlTrMS2D MMlEgWWwwyCdU9f1LNyNUFodiiBabRnn4p8ochYA3zNCaiBUAEw9y/gwpL50UGiLroub o1hwvEGjNIe3vNE4xvER74IAgus7capifu6Li6yKIw56/kdNzFkElK4C9HctTPLXhQV0 olK2GGdCYWxq5QtgDP8iHeJiFsqnLEv37ij664zWSPxVZTMgQnlpMM4uHSHzzNxmqFXk sqRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=itskFRdx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23si11316463pfh.295.2018.04.24.23.28.05; Tue, 24 Apr 2018 23:28:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=itskFRdx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751822AbeDYG2B (ORCPT + 29 others); Wed, 25 Apr 2018 02:28:01 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:46269 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751573AbeDYG1b (ORCPT ); Wed, 25 Apr 2018 02:27:31 -0400 Received: by mail-pf0-f195.google.com with SMTP id h69so14312181pfe.13 for ; Tue, 24 Apr 2018 23:27:31 -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=KkP3DfeGXSmFJ/rkUHgqZrHf8viG2c8cOE40qs20vt5/GhpkdOmzxHHVodZoaRc6am fea7Sggywukx6VRzNZ03g3OcsF0ZskdQGECsGciyCgW3t2jjbjbNHfYHyjyNLpFzgGv2 VrGRo2ilt0a1omUiYxg6iJsd/kqVOiqwn9mkagtHj7/9L2WFkaZXRTSioMs5LUmno3tp HQSa3Glx/0ZL8qRk7MPzbTOdbldiPNLWsbPrRv77DoWBjq1RV3VG4mT/F9xbpnAlQv80 izJCRkrCZGNZMPM04Z8YMVQO/jGzWH4A5I7YSLXW2TCBTQkzNnlNdCDhbEXaGNGBmhAY Syjw== X-Gm-Message-State: ALQs6tAv2usgJQzqFhbpC1DAd5uTUQwWOV1khj6Z8DVI9EaNLWVLlUWl 60FyQX1LKG3wWjtUgf/4TajouA== 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 Cc: ard.biesheuvel@linaro.org, james.morse@arm.com, bhsharma@redhat.com, kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(+) -- 2.17.0 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 };