From patchwork Sat Jun 23 02:20:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10483445 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 A73E5602CB for ; Sat, 23 Jun 2018 02:50:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9676C28F98 for ; Sat, 23 Jun 2018 02:50:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89FA92907A; Sat, 23 Jun 2018 02:50:14 +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 0713428F98 for ; Sat, 23 Jun 2018 02:50:13 +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=Kq/douVcC7B9wW44aPL62GmwwkCiApVJUORG5oZezAY=; b=NfWIIsEbW4aegk/ZfnUyobIzwD 8KbvHMVV5Kq6FAo9GrT60SxIjdCytVmS1Kj0dBv3ZmVWd51sYn1HnMM+933wP2yQiMvXF14IN22/O phjh4fcAh5PgrxWvFkskwJogHRaP9SvLRvptxZQH4wBb1PWuVt9Hupr7se92Xo2FFL5fvfLySKeg5 tX0VmP1T2+AmB7xHt51YQtvoDe+SIuTfdwjco4VHgm0P0t78Wvnt1jXvA3ziz+Tb6G8xvVEaeB88G jCDjCBVvVij1EZMZkTZLM0Vnm7A6P5IC+2EkS2gEKeKyD/W/pigWEhb+ni99U15AoxPhlrfniOS/b FBw/1cnw==; 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 1fWYct-0001rm-Ko; Sat, 23 Jun 2018 02:50:11 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fWYRb-0004lx-5K for linux-arm-kernel@bombadil.infradead.org; Sat, 23 Jun 2018 02:38:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=yeGoM3EFxFbCiZ0pTQN0LbMNYGMSzZ9MYrt6IRMCBfE=; b=B0aFDa3Sylh0aIsGMajT2qm3X iInSrwWYedfLxhsH4gCxVp6gEVcgvrIDK9H9GHW8R7esx0eKwVrZ1/zl05qC/8EwTCgL4uW/2dUPX DlNvkVlWE6yBthQ+sLD4mMthtrTt3hEBb/QATocVY9Ha6U6k2HUXSQGhYXqtwa0AiR1pAoTXcabop szfEiSgTno5DEhdeba6oFu4MkeITZjVAysriWdoPx/+9MpT/upnjTc+IL84dTlRIQCU62Qz0xTHWD y6tvJZTCvDxorfqaRF0t+roDWaYU57LKX7mmLcp987qxeT6ecKp3AzX7hbpSxSnULXCVOSmN0C2Rn t46brS7nQ==; Received: from mail-pl0-x244.google.com ([2607:f8b0:400e:c01::244]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fWYBF-0003MN-UH for linux-arm-kernel@lists.infradead.org; Sat, 23 Jun 2018 02:21:42 +0000 Received: by mail-pl0-x244.google.com with SMTP id 30-v6so4254203pld.13 for ; Fri, 22 Jun 2018 19:21:33 -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=yeGoM3EFxFbCiZ0pTQN0LbMNYGMSzZ9MYrt6IRMCBfE=; b=jap1Iu7MUFgvUPylb7x96BhLEP8ufMgho9sJXw+1vAB1+zlRMaAvzFuZaoW4AUjlQw JB3WuhiYzbJ+SwX4N8or7bgkEiJ6KsN700nZKW38YsyT7Vm4+sSN2wPky1NJaGD3KyPA fqznRaE0a9bWD2ZuckSPFHrE7Vp2oV2AOGQVo= 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=yeGoM3EFxFbCiZ0pTQN0LbMNYGMSzZ9MYrt6IRMCBfE=; b=nhMFgsIgdIXzsEiw04t+U6eG8Pn0I1YBjwZ1JZLX2SuTuND1XGSPfQqkW6GUhbAbOY Qizgc816c6mJ1pUH7nNUagw6V+BYtCBPEpa11Gzs7B1bEKcZLNUnYd7jKnWL46E5Ob30 Lbd5OWJCrajyLADApEIvj7hWXcYrlM8ry9ow0gSiRy7YqgJIlBVd7as34aYNtyfy5RN0 jjKRG1pgdRSrjG3VruRbd41ZlJoW3IdgvtrF7BgEBHjBIPjivVAnuV9B0GGGCusXQGo/ JUykvGRgFzlOBqL3GjI4aj+pt6ohWLL8M+XBxWfSv+CLnF+bct5pAAzu4ZIRLPTwCwRb 6bEQ== X-Gm-Message-State: APt69E1KJuwlMUOyu6jDXbdGixIfIJbyhwld+Qg4b6TFI4B9KUKzCrcn UmHU6EFEXqBWUfHmmn5XdmlRxg== X-Google-Smtp-Source: ADUXVKJhzYaNgmhSmLtPcKCiht877vyRaJRTLAlVTQVUEkBPNVsgKYMIq9KNdStxThzo8Dx5ew+++g== X-Received: by 2002:a17:902:b217:: with SMTP id t23-v6mr3905535plr.312.1529720491947; Fri, 22 Jun 2018 19:21:31 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id l11-v6sm12787249pff.50.2018.06.22.19.21.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Jun 2018 19:21:31 -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 v10 09/14] arm64: kexec_file: allow for loading Image-format kernel Date: Sat, 23 Jun 2018 11:20:53 +0900 Message-Id: <20180623022058.10935-10-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180623022058.10935-1-takahiro.akashi@linaro.org> References: <20180623022058.10935-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180623_032138_098376_751BFEAE X-CRM114-Status: GOOD ( 24.15 ) 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 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 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 7115c4f915dc..199e86e791c9 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 };