linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: rmk@armlinux.org.uk (Russell King)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 2/2] arm: use zImage size from header
Date: Thu, 07 Jul 2016 15:01:24 +0100	[thread overview]
Message-ID: <E1bL9ro-0002P2-Cr@e0050434b2927.dyn.armlinux.org.uk> (raw)
In-Reply-To: <20160707102003.GO1041@n2100.armlinux.org.uk>

We do not support zImage with an appended DTB as we need to supply our
own DTB to the kernel, and an appended DTB will override any that we
try to supply.  To ensure that we do not load an appended DTB, clamp
the kernel size to the size specified in the zImage header.

Signed-off-by: Russell King <rmk@armlinux.org.uk>
---
 kexec/arch/arm/kexec-zImage-arm.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/kexec/arch/arm/kexec-zImage-arm.c b/kexec/arch/arm/kexec-zImage-arm.c
index c8b90f96..7f02b931 100644
--- a/kexec/arch/arm/kexec-zImage-arm.c
+++ b/kexec/arch/arm/kexec-zImage-arm.c
@@ -28,6 +28,14 @@ off_t initrd_base = 0, initrd_size = 0;
 unsigned int kexec_arm_image_size = 0;
 unsigned long long user_page_offset = (-1ULL);
 
+struct zimage_header {
+	uint32_t instr[9];
+	uint32_t magic;
+#define ZIMAGE_MAGIC cpu_to_le32(0x016f2818)
+	uint32_t start;
+	uint32_t end;
+};
+
 struct android_image {
 	char magic[8];
 	uint32_t kernel_size;
@@ -435,6 +443,34 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	if (dtb_file)
 		dtb_buf = slurp_file(dtb_file, &dtb_length);
 
+	if (len > 0x34) {
+		const struct zimage_header *hdr;
+		off_t size;
+
+		hdr = (const struct zimage_header *)buf;
+
+		dbgprintf("zImage header: 0x%08x 0x%08x 0x%08x\n",
+			  hdr->magic, hdr->start, hdr->end);
+
+		if (hdr->magic == ZIMAGE_MAGIC) {
+			size = le32_to_cpu(hdr->end) - le32_to_cpu(hdr->start);
+
+			dbgprintf("zImage size 0x%llx, file size 0x%llx\n",
+				  (unsigned long long)size,
+				  (unsigned long long)len);
+
+			if (size > len) {
+				fprintf(stderr,
+					"zImage is truncated - file 0x%llx vs header 0x%llx\n",
+					(unsigned long long)len,
+					(unsigned long long)size);
+				return -1;
+			}
+			if (size < len)
+				len = size;
+		}
+	}
+
 	/* Handle android images, 2048 is the minimum page size */
 	if (len > 2048 && !strncmp(buf, "ANDROID!", 8)) {
 		const struct android_image *aimg = (const void *)buf;
-- 
1.9.1

  parent reply	other threads:[~2016-07-07 14:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-14 17:59 kexec failures with DEBUG_RODATA Russell King - ARM Linux
2016-06-14 18:05 ` Kees Cook
2016-06-15  2:43   ` Baoquan He
2016-06-15 21:13   ` Russell King - ARM Linux
2016-06-15 22:20     ` Kees Cook
2016-06-15 22:42       ` Russell King - ARM Linux
2016-06-15 22:54         ` Kees Cook
2016-06-15 23:13           ` Russell King - ARM Linux
2016-06-21 11:48             ` Pratyush Anand
2016-06-21 15:37               ` Russell King - ARM Linux
2016-07-07 10:20         ` Russell King - ARM Linux
2016-07-07 14:01           ` [PATCH 1/2] arm: plug a zImage corner case Russell King
2016-07-15  4:13             ` Simon Horman
2016-07-07 14:01           ` Russell King [this message]
2016-07-21  7:00           ` kexec failures with DEBUG_RODATA Tony Lindgren
2016-07-07 10:00     ` Russell King - ARM Linux
2016-06-15  7:55 ` Pratyush Anand
2016-06-15 19:13   ` Russell King - ARM Linux

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1bL9ro-0002P2-Cr@e0050434b2927.dyn.armlinux.org.uk \
    --to=rmk@armlinux.org.uk \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).