From patchwork Wed Jul 11 07:41:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 141676 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp4800646ljj; Wed, 11 Jul 2018 00:42:56 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdDJaLojM3TkTpwU5vjCA5bK0b5S/i//eF7VltjRT92imB6jgPHhMTs+PihpRHrXFpsmyVS X-Received: by 2002:a62:f704:: with SMTP id h4-v6mr29183162pfi.169.1531294976419; Wed, 11 Jul 2018 00:42:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531294976; cv=none; d=google.com; s=arc-20160816; b=Puu7IsflpTiZslguNcymaC6CO7lGnJ2b50vhHrXRDA5V3dtnsWUgapeLEKlAFQdACF DI7NCIfwiLD9gcAB2NXpP+xTS2wlYMBOzWsGWgfodJlNUPZsAZV7eKvG8vxjvHerWwso AsYylFDL4brFh5DtaGHJK2tN+GJ3SykUwl5QHQK6gwlkEt0q6CHfLRPqpcjnhRt3YspZ NPaAwETdJRPo6nXjwlOMkitnMX5UVHMyQocenBduMY9u3vvkZC2qoZiGm1eeLmDEZRZ4 EtCXXQteIbGXl4QJIarmypp8K2P+0PnxfIUT3ScPUoj7tOUtlREuhn5LYMwwe0I7TQcL EoUw== 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=ogWh3dYAB/qczfbEkAaaqUWh2MaeasdU2LxmAA1AnSU=; b=y/lUnDJ8Z92vcinY/cwJyzxlKPlpaWLxfqVqcj9qDtaxgFT/yyoIZWQ4+dixDT2QjQ pqdBjnuorhzrHzdqsWmD8yFDy6fAA41Bluy8kurc98lc5phdztquyxXGr3smvUwh5nze kTXOlofLXS5bqhaKrsVj/GsyWAGsHr1YvbjFygCVm4NYYoQARXiqKF1DhVwMh7aTz1zQ wys6Byyo4tzTg0+PQT3FypdFpK6p154s/P+HwNUNuYAx/NQNEsRg/UFkEv6k78EBLkGx vUHeIhEMbxP9HU1ttjgCHIpy0/L7rq7W5Cfist1Y5MmeRcxQdn7iXm4IyohM9dLDHfyx +MnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Iin9d3kt; 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 s9-v6si18693118pfm.85.2018.07.11.00.42.56; Wed, 11 Jul 2018 00:42:56 -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=Iin9d3kt; 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 S1732339AbeGKHpt (ORCPT + 22 others); Wed, 11 Jul 2018 03:45:49 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:39587 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726306AbeGKHpt (ORCPT ); Wed, 11 Jul 2018 03:45:49 -0400 Received: by mail-pf0-f196.google.com with SMTP id s21-v6so17886495pfm.6 for ; Wed, 11 Jul 2018 00:42:52 -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=ogWh3dYAB/qczfbEkAaaqUWh2MaeasdU2LxmAA1AnSU=; b=Iin9d3ktrch/m+5hSHCq1o/2GQGTtE650zql3/smMCFtO7rzc0Nozijxi/yW3wX8jL O87hmIFmbxDMPuoPyrPwKj52dXD2biqVOOcO9FexX8pwuIPBsh35QuVWOgt6hx0jxA/L vMQGdyLyhnxToauTw85GYru53bOgX6lGGb9Lk= 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=ogWh3dYAB/qczfbEkAaaqUWh2MaeasdU2LxmAA1AnSU=; b=MQ0j1khDLx0mIjrkJ//0eQ/fSo3CKmofFXUYthY1UUjkl+W0K8wvgpu7NcM7UEmcHs Pjqz0MSwMhvfRhHdz+FA6aNjj3hylwxxuCvkagV7ku8YIFEmoDXNRjw10tC4oU1h2lgf 8a/uFRijxLPOHdaZUuflWioNYrdlL8j8uu1mhYVGAUtXphjC0KBC0LQoEaEC3RXJE6HP yF72gbo2Vw9AWp9YlIbNFP1TEgY6Aq5sYsT7uZjG69dOroocAIzKjBwiDEwNln5dYeU/ I4SfMXfV0aaUDc8AYw47yYOFROibOuNYuRk8Kj6Y+WvoXHuSQVnXKo1f3h4a4GYdr6KR CfMw== X-Gm-Message-State: APt69E0N9WYRY1shtt+OFdTyXAtgdVGNdFry3J+/Z25nejNsIrUeR9s5 ma9LStXo9M9j1gwDAUZrTbRjjg== X-Received: by 2002:a63:842:: with SMTP id 63-v6mr26271279pgi.406.1531294972038; Wed, 11 Jul 2018 00:42:52 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id m86-v6sm40460534pfi.47.2018.07.11.00.42.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Jul 2018 00:42:51 -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 v11 10/15] arm64: kexec_file: allow for loading Image-format kernel Date: Wed, 11 Jul 2018 16:41:58 +0900 Message-Id: <20180711074203.3019-11-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180711074203.3019-1-takahiro.akashi@linaro.org> References: <20180711074203.3019-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch provides kexec_file_ops for "Image"-format kernel. In this implementation, a binary is always loaded with a fixed offset identified in text_offset field of its header. Regarding signature verification for trusted boot, this patch doesn't contains CONFIG_KEXEC_VERIFY_SIG support, which is to be added later in this series, but file-attribute-based verification is still a viable option by enabling IMA security subsystem. You can sign(label) a to-be-kexec'ed kernel image on target file system with: $ evmctl ima_sign --key /path/to/private_key.pem Image On live system, you must have IMA enforced with, at least, the following security policy: "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig" See more details about IMA here: https://sourceforge.net/p/linux-ima/wiki/Home/ Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/include/asm/kexec.h | 28 +++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/kexec_image.c | 108 +++++++++++++++++++++++++ arch/arm64/kernel/machine_kexec_file.c | 1 + 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/kexec_image.c -- 2.17.0 Reviewed-by: James Morse diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 01bbf6cebf12..69333694e3e2 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -101,6 +101,34 @@ struct kimage_arch { void *dtb_buf; }; +/** + * struct arm64_image_header - arm64 kernel image header + * See Documentation/arm64/booting.txt for details + * + * @mz_magic: DOS header magic number ('MZ', optional) + * @code1: Instruction (branch to stext) + * @text_offset: Image load offset + * @image_size: Effective image size + * @flags: Bit-field flags + * @reserved: Reserved + * @magic: Magic number + * @pe_header: Offset to PE COFF header (optional) + **/ + +struct arm64_image_header { + __le16 mz_magic; /* also code0 */ + __le16 pad; + __le32 code1; + __le64 text_offset; + __le64 image_size; + __le64 flags; + __le64 reserved[3]; + __le32 magic; + __le32 pe_header; +}; + +extern const struct kexec_file_ops kexec_image_ops; + struct kimage; extern int load_other_segments(struct kimage *image, diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index 06281e1ad7ed..a9cc7752f276 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -50,7 +50,7 @@ arm64-obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o arm64-obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o arm64-obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \ cpu-reset.o -arm64-obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o +arm64-obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o kexec_image.o arm64-obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c new file mode 100644 index 000000000000..a47cf9bc699e --- /dev/null +++ b/arch/arm64/kernel/kexec_image.c @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Kexec image loader + + * Copyright (C) 2018 Linaro Limited + * Author: AKASHI Takahiro + */ + +#define pr_fmt(fmt) "kexec_file(Image): " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int image_probe(const char *kernel_buf, unsigned long kernel_len) +{ + const struct arm64_image_header *h; + + h = (const struct arm64_image_header *)(kernel_buf); + + if (!h || (kernel_len < sizeof(*h)) || + !memcmp(&h->magic, ARM64_MAGIC, sizeof(ARM64_MAGIC))) + return -EINVAL; + + return 0; +} + +static void *image_load(struct kimage *image, + char *kernel, unsigned long kernel_len, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct arm64_image_header *h; + u64 flags, value; + struct kexec_buf kbuf; + unsigned long text_offset; + struct kexec_segment *kernel_segment; + int ret; + + /* Don't support old kernel */ + h = (struct arm64_image_header *)kernel; + if (!h->text_offset) + return ERR_PTR(-EINVAL); + + /* Check cpu features */ + flags = le64_to_cpu(h->flags); + value = head_flag_field(flags, HEAD_FLAG_BE); + if (((value == HEAD_FLAG_BE) && !IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) || + ((value != HEAD_FLAG_BE) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))) + if (!system_supports_mixed_endian()) + return ERR_PTR(-EINVAL); + + value = head_flag_field(flags, HEAD_FLAG_PAGE_SIZE); + if (((value == HEAD_FLAG_PAGE_SIZE_4K) && + !system_supports_4kb_granule()) || + ((value == HEAD_FLAG_PAGE_SIZE_64K) && + !system_supports_64kb_granule()) || + ((value == HEAD_FLAG_PAGE_SIZE_16K) && + !system_supports_16kb_granule())) + return ERR_PTR(-EINVAL); + + /* Load the kernel */ + kbuf.image = image; + kbuf.buf_min = 0; + kbuf.buf_max = ULONG_MAX; + kbuf.top_down = false; + + kbuf.buffer = kernel; + kbuf.bufsz = kernel_len; + kbuf.memsz = le64_to_cpu(h->image_size); + text_offset = le64_to_cpu(h->text_offset); + kbuf.buf_align = SZ_2M; + + /* Adjust kernel segment with TEXT_OFFSET */ + kbuf.memsz += text_offset; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out; + + kernel_segment = &image->segment[image->nr_segments - 1]; + kernel_segment->mem += text_offset; + kernel_segment->memsz -= text_offset; + image->start = kernel_segment->mem; + + pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + kernel_segment->mem, kbuf.bufsz, + kernel_segment->memsz); + + /* Load additional data */ + ret = load_other_segments(image, + kernel_segment->mem, kernel_segment->memsz, + initrd, initrd_len, cmdline, cmdline_len); + +out: + return ERR_PTR(ret); +} + +const struct kexec_file_ops kexec_image_ops = { + .probe = image_probe, + .load = image_load, +}; diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index ca00681c25c6..a0b44fe18b95 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -20,6 +20,7 @@ #include const struct kexec_file_ops * const kexec_file_loaders[] = { + &kexec_image_ops, NULL };