From patchwork Mon Dec 4 02:58:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 120482 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3920224qgn; Sun, 3 Dec 2017 18:56:02 -0800 (PST) X-Google-Smtp-Source: AGs4zMZOq1EOOVjJ1tFd3uMBYsPQc9oWy7el/JW2qddy8L/Cy6QcOLNbkHJeJwckAJ5bn5sjkgSk X-Received: by 10.84.196.131 with SMTP id l3mr13046083pld.194.1512356162844; Sun, 03 Dec 2017 18:56:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512356162; cv=none; d=google.com; s=arc-20160816; b=hekYZPBCrc9DXgEW+BKJpUHOQDdrDMf/PtZfW/R9C+ADUnY/q5ameZKdvhVOHM4dII IsOV5td9ZyGqvsjA6jvn0tZEYKl+dJAdjJmCzQyt2felOU42jXVNjm8KLtnNg2El0Ici T6+w5R+8NmhBRhxwpVtZBEl9Lyp2UH9h7NdiwHUWPzbzYp2dyPuCKOYm+B1gU5nGbu5E OCTPEgrvqMQn7Ib7saEGsP7l8JALz9IilSkb1ifj05gPEC6ZJZpdk8q37gNkW6N2aQiL n7V6RytLau4tVsjL2Uer4n0WnZJHJkDTf4E7Qe7rlkUY8blWCR87OXuV+rUbN5mItVf5 8abg== 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=sCm+IZSF+5l8rR3IkQ0bBKGpYawqXAbN2wz6j7SCo+g=; b=wrY/ZIWSYkTvQ0HYtJQiVfkvkEtdMJEqJvTQIU2NYHkZYscOdKK37GC/pLlSvl2YpZ YUWv9knHRtKcqh4LrcSF4BPH3blcnQWTFmjfR7djxEGw6pHgV3gAuK5jbEDCJTqdaJLF bfGMyOnDG+s2MALlVuDvfD0FZtApr8AlwoAnsmDqXw+UPrkbEVsf8XkWoTvki9fRiS2K JGFVx9PwI+D95bUDcVfoQMFQyQudHyCr9KVUb7rCe65vMqUMsmJJU8kKZZTYvfw0Q0ps GQ2mnd2ZmPWbpGP8ofRhc8TufA/PEdnseifLlTc4Rky1CGZr5l7/iNaAtuUZJGWaYR3J 3pMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FcIieWWm; 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 l81si9481109pfb.91.2017.12.03.18.56.02; Sun, 03 Dec 2017 18:56:02 -0800 (PST) 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=FcIieWWm; 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 S1753051AbdLDC4A (ORCPT + 28 others); Sun, 3 Dec 2017 21:56:00 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:36276 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753244AbdLDCza (ORCPT ); Sun, 3 Dec 2017 21:55:30 -0500 Received: by mail-pg0-f68.google.com with SMTP id k134so7217746pga.3 for ; Sun, 03 Dec 2017 18:55:30 -0800 (PST) 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=sCm+IZSF+5l8rR3IkQ0bBKGpYawqXAbN2wz6j7SCo+g=; b=FcIieWWm1j5dIxMQWmaNYEiq/cUtovzDJoEVCcrhjV4R2i4wNAI3+EGnIfFVVasZyv v34R17usIY7KdcHLcXSw3PvigxBZX79My8//7UjcrhA/wW1Yx35Tex3maHZSZB9vDG66 husT64Yyyn+BNCJnbcUX8JW3dr/rGnp4yIIzo= 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=sCm+IZSF+5l8rR3IkQ0bBKGpYawqXAbN2wz6j7SCo+g=; b=I6JUJa7ze9HU6VoVvI6a2crtkAebBRB3HLDtH5l2eFslldfrwGy3emHVx+Wrtf2AD2 h3ujqTl/6EJaB0JsMV+iDXyyyo6o5u934VYrQ4aOhU6jOSDd/hZFR11/ciXZz6dGVJP9 yJAjQsxUXWAAX8D61t8rbmUNj2C1k3V8LZB/DcSwtuTRl1D+s+PyIsoOSMPyb6ywhndZ 4xRrKPdyQ4xRKVuNNBUVjwaMhrYC7eBEve9TwUzzvWl1WuHBUxXl8fT8KEM3MTA850bg sdBJnZ0g458kSzowLArizwZQ8rRvugbqARd6UfBXLSQPY2FXv8A4LmEbJZmvpagGcWQI UFMA== X-Gm-Message-State: AJaThX7oijGVryi6id10yYY+t6vJLT25jdALeLY/6HLu5ctFJ6h9dZQl Xki7npnFmorzd/nDEPpb2fHn8A== X-Received: by 10.99.108.66 with SMTP id h63mr12721441pgc.362.1512356129586; Sun, 03 Dec 2017 18:55:29 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id i187sm21775305pfc.96.2017.12.03.18.55.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Dec 2017 18:55:29 -0800 (PST) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, bauerman@linux.vnet.ibm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, akpm@linux-foundation.org, mpe@ellerman.id.au, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, ard.biesheuvel@linaro.org, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v7 11/11] arm64: kexec_file: enable KEXEC_VERIFY_SIG for Image Date: Mon, 4 Dec 2017 11:58:01 +0900 Message-Id: <20171204025801.12161-12-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171204025801.12161-1-takahiro.akashi@linaro.org> References: <20171204025801.12161-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 | 10 ++++++++++ arch/arm64/include/asm/kexec.h | 16 ++++++++++++++++ arch/arm64/kernel/kexec_image.c | 15 +++++++++++++++ 3 files changed, 41 insertions(+) -- 2.14.1 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index c0b021736c10..289c7bede593 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -814,6 +814,16 @@ config KEXEC_FILE_IMAGE_FMT ---help--- Select this option to enable 'Image' kernel loading. +config KEXEC_VERIFY_SIG + bool "Verify kernel signature during kexec_file_load() syscall" + depends on KEXEC_FILE + select SYSTEM_DATA_VERIFICATION + select SIGNED_PE_FILE_VERIFICATION if KEXEC_FILE_IMAGE_FMT + ---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. + 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 b6469bc64a89..2a63bf5f32ea 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -131,6 +131,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. @@ -152,6 +153,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 1c106237901d..b840b6ed6ed9 100644 --- a/arch/arm64/kernel/kexec_image.c +++ b/arch/arm64/kernel/kexec_image.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,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; } @@ -84,7 +88,18 @@ static void *image_load(struct kimage *image, char *kernel, return ERR_PTR(ret); } +#ifdef CONFIG_KEXEC_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_VERIFY_SIG + .verify_sig = image_verify_sig, +#endif };