From patchwork Wed Apr 25 06:26:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 134234 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp472000lji; Tue, 24 Apr 2018 23:28:34 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/F6xPGZfRw9E0ZzVjGr7IqKERdawW1w0d4NRIuj1ytyJDlvAqPGNVvdD8+lMUpnTTOYyMB X-Received: by 2002:a17:902:3181:: with SMTP id x1-v6mr28125568plb.198.1524637714462; Tue, 24 Apr 2018 23:28:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524637714; cv=none; d=google.com; s=arc-20160816; b=AHtzLL+QiOv8QOcSB58bTZ3LBMCrk6pg36C4sLK2Rwk9zIO9QLcPSOynGqsefEQd1t 4NMF2uxjmYuTDQdC/owvCtJ+wcpp8e7xTQFmycgaI9Vr1rSImEcaTLVkMkW/8/goh2sy ztHOKGGcvGO4HhVozSTllqjeExBR/UG8MCIi1bAoIl3JJydnPVcky7W2l8qU0Gfusz4k uVR7Q92t3Vv52WeyO+rwCn5CMpp+8ZL4CR5ow6QcK65gESAOSZW0e2RA+Okew0ksxoy1 qy4pibHJhRv5ztIQLZLtBCC8XOvNSATJK8bvDmoAbkx2dty8BPfPvLJLV/ucB54vXeF4 3rZg== 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=Hk17H4nlIgaxnc/FjRGL0OgFtKXzMGdb7qKjidLoWTw=; b=vUMDdDVx1jWIoH0who0MeDw70YeM1B4YBXVCEMBhnP4wvALdZkZ16ayZVJC7t/KdiL vb3DJ4Bih40JD6760je+csnA9irZVprEQziKAdJgArfi/RtZD4PO9nd89X2GVQFpQ9Rn m9trBeJSOEday0bU0qA9Kp0ucwLatMllR4+p3e+9uuuqNTzk4l5leEZmRi9VHzvajUDT IQ2CDXCHBoLO9uHsv11kFTd2O6ylsZsMoYWSkOid3VUiztTpT2cw9JcSkzMFk5ILOhYS xo0RlJqG4kVhK7cuIcUvVGdT+6x28yiswDUq89gsNpWtJahp4TZd9Up/4JRgJHq6pnAm eWsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SGUpMOso; 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 r1si15899498pff.24.2018.04.24.23.28.34; Tue, 24 Apr 2018 23:28:34 -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=SGUpMOso; 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 S1751910AbeDYG2b (ORCPT + 29 others); Wed, 25 Apr 2018 02:28:31 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:36262 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751497AbeDYG1Q (ORCPT ); Wed, 25 Apr 2018 02:27:16 -0400 Received: by mail-pg0-f67.google.com with SMTP id i6so12608655pgv.3 for ; Tue, 24 Apr 2018 23:27:16 -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=Hk17H4nlIgaxnc/FjRGL0OgFtKXzMGdb7qKjidLoWTw=; b=SGUpMOsoibd+grSs4DZrgSBYy/ZGyQj442W7EGF7pixl52xCTCSDiRtqDgJFIt3p07 tKgMq8aIVcEbnZVO6OxdugxgPIgpSJ5eJ3UeR1oVGZK0p5jNjsOvVIRmriqym4d2xgH/ idYJf9KCssqu3kK/EmPDUbNO6R+6YT8FETqE8= 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=Hk17H4nlIgaxnc/FjRGL0OgFtKXzMGdb7qKjidLoWTw=; b=cUcRndwBnffb25t5Ato6Mr6doGBICXG0rxrspw7bZidfNU54MhYzxzOB25IxYzzybn xUllOr8vyS1l2ZF2TDgh4zZCFr2bNTR1mVqr5kLkXGRRcXQ5Ry+TjR9s3rhdMU3ezt8V ODw24pYcBgH+WOCc9rvQQ7igqwct3wXlUkpmnlU3talxSRiX7YJdSTnpSe9+xm1zrHDF B5TspcUJ5OYdHYS3L2w8x7BO8QbSZGzpRWmCoBBkvivF3y8/uC+NxdhhDlJCnONJLdj1 +TfZuTlVZ3d/6xA8Tn7/xCw3pMaiw76hoBLcntSKQQjf9B+MncKzLCMeVjuyObEKwNm5 uNAQ== X-Gm-Message-State: ALQs6tAAiK1DVVX9kzPmAO3Kxi8iPOOnSAqQgta2QWUgck7nsORQM7bm TVOvxz+EL4mBEpYbe60uwDFThw== X-Received: by 10.98.101.131 with SMTP id z125mr26862764pfb.208.1524637636253; Tue, 24 Apr 2018 23:27:16 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 186sm30043693pfe.109.2018.04.24.23.27.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 23:27:15 -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 06/11] arm64: kexec_file: allow for loading Image-format kernel Date: Wed, 25 Apr 2018 15:26:24 +0900 Message-Id: <20180425062629.29404-7-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 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 | 50 ++++++++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/kexec_image.c | 79 ++++++++++++++++++++++++++ arch/arm64/kernel/machine_kexec_file.c | 1 + 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/kexec_image.c -- 2.17.0 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index e4de1223715f..3cba4161818a 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -102,6 +102,56 @@ struct kimage_arch { void *dtb_buf; }; +/** + * struct arm64_image_header - arm64 kernel image header + * + * @pe_sig: Optional PE format 'MZ' signature + * @branch_code: Instruction to branch to stext + * @text_offset: Image load offset, little endian + * @image_size: Effective image size, little endian + * @flags: + * Bit 0: Kernel endianness. 0=little endian, 1=big endian + * @reserved: Reserved + * @magic: Magic number, "ARM\x64" + * @pe_header: Optional offset to a PE format header + **/ + +struct arm64_image_header { + u8 pe_sig[2]; + u8 pad[2]; + u32 branch_code; + u64 text_offset; + u64 image_size; + u64 flags; + u64 reserved[3]; + u8 magic[4]; + u32 pe_header; +}; + +static const u8 arm64_image_magic[4] = {'A', 'R', 'M', 0x64U}; + +/** + * arm64_header_check_magic - Helper to check the arm64 image header. + * + * Returns non-zero if header is OK. + */ + +static inline int arm64_header_check_magic(const struct arm64_image_header *h) +{ + if (!h) + return 0; + + if (!h->text_offset) + return 0; + + return (h->magic[0] == arm64_image_magic[0] + && h->magic[1] == arm64_image_magic[1] + && h->magic[2] == arm64_image_magic[2] + && h->magic[3] == arm64_image_magic[3]); +} + +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 2f2b2757ae7a..1e110aa571dd 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..4dd524ad6611 --- /dev/null +++ b/arch/arm64/kernel/kexec_image.c @@ -0,0 +1,79 @@ +// 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 + +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 ((kernel_len < sizeof(*h)) || !arm64_header_check_magic(h)) + 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 kexec_buf kbuf; + struct arm64_image_header *h = (struct arm64_image_header *)kernel; + unsigned long text_offset; + int ret; + + /* 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; + + image->arch.kern_segment = image->nr_segments - 1; + image->segment[image->arch.kern_segment].mem += text_offset; + image->segment[image->arch.kern_segment].memsz -= text_offset; + image->start = image->segment[image->arch.kern_segment].mem; + + pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + image->segment[image->arch.kern_segment].mem, + kbuf.bufsz, kbuf.memsz); + + /* Load additional data */ + ret = load_other_segments(image, 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 b3b9b1725d8a..37c0a9dc2e47 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -23,6 +23,7 @@ static int __dt_root_addr_cells; static int __dt_root_size_cells; const struct kexec_file_ops * const kexec_file_loaders[] = { + &kexec_image_ops, NULL };