Skip to contentSkip to conversation

kexec/arm64: Add support for zlib compressed kernel images (Image.gz)

PATCH Applied Bhupesh Sharma 2 participants 7 messages Jul 15, 2019 – Jul 16, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover kexec/arm64: Add support for zlib compressed kernel images (Image.gz)

Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.

This patchset adds the support for zlib compressed kernel images (Image.gz) for arm64 kexec-tools.

This was discussed a bit with the arm64 kernel maintainers (see [0]) and after discussing the pros and cons of adding this support in kernel-space v/s in user-space, we decided it makes more sense to add this support to the user-space kexec-tools.

Note that this series adds support for unsigned arm64 Image.gz files only as signing an Image.gz type file is not a easy implementation from kexec-tools p-o-v. Also even standard signing tools like sbsign fail to recongize the Image.gz format:

 $ sbsign --key certs/signing_key.pem  --cert certs/signing_key.pem Image.gz
   Invalid DOS header magic

... because that gzip file isn't a PE32+ that can be loaded by UEFI. So I will work on the same in coming days and try to send a RFC patchset for the same later on.

[0]. http://lists.infradead.org/pipermail/kexec/2019-June/023156.html

Bhupesh Sharma (4):
kexec/kexec.c: Add the missing close() for fd used for
kexec_file_load()
kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe() kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function kexec/arm64: Add support for handling zlib compressed (Image.gz) image

 kexec/arch/arm64/Makefile             |   3 +-
 kexec/arch/arm64/kexec-arm64.c        |   1 +
 kexec/arch/arm64/kexec-arm64.h        |   7 ++
 kexec/arch/arm64/kexec-image-arm64.c  |   4 +-
 kexec/arch/arm64/kexec-uImage-arm64.c |  13 +-
 kexec/arch/arm64/kexec-zImage-arm64.c | 226 ++++++++++++++++++++++++++++++++++
 kexec/kexec-zlib.h                    |   1 +
 kexec/kexec.c                         |  14 +++
 kexec/zlib.c                          |  38 ++++++
 9 files changed, 303 insertions(+), 4 deletions(-)
 create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
Signature · 7 lines
2.7.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH v2 1/4 kexec/kexec.c: Add the missing close() for fd used for kexec_file_load()

In kexec/kexec.c, we open() the kernel Image file and pass this file descriptor to the kexec_file_load() system call, but never call a corresponding close().

Fix the same via this patch.

---
 kexec/kexec.c | 2 ++
 1 file changed, 2 insertions(+)
kexec/kexec.c +2 -0
index 32ae56c8f1c3..8ca3b457cac8 100644
@@ -1234,6 +1234,8 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 	if (ret != 0)
 		fprintf(stderr, "kexec_file_load failed: %s\n",
 					strerror(errno));
+
+	close(kernel_fd);
 	return ret;
 }
 
Signature · 7 lines
2.7.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

PATCH v2 2/4 kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe()

In reply to Bhupesh Sharma

Commit bf06cf2095e1 ("kexec/uImage: probe to identify a corrupted image"), defined the 'uImage_probe_kernel()' function return values and correspondingly ;uImage_arm64_probe()' returns the same (0 -> If the image is valid 'type' image, -1 -> If the image is corrupted and 1 -> If the image is not a uImage).

This causes issues because, in later patches we introduce zImage support for arm64, and since it is probed after uImage, the return values from 'uImage_arm64_probe()' needs to be fixed to make sure that kexec will not return with an invalid error code.

Now, 'uImage_arm64_probe()' returns the following values instead:
0 - valid uImage.
-1 - uImage is corrupted.
1 - image is not a uImage.

---
 kexec/arch/arm64/kexec-uImage-arm64.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kexec/arch/arm64/kexec-uImage-arm64.c +12 -1
index 126ea9c2555b..c4669131b667 100644
@@ -11,7 +11,18 @@
 
 int uImage_arm64_probe(const char *buf, off_t len)
 {
-	return uImage_probe_kernel(buf, len, IH_ARCH_ARM64);
+	int ret;
+
+	ret = uImage_probe_kernel(buf, len, IH_ARCH_ARM64);
+
+	/*  0 - valid uImage.
+	 * -1 - uImage is corrupted.
+	 *  1 - image is not a uImage.
+	 */
+	if (!ret)
+		return 0;
+	else
+		return -1;
 }
 
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
Signature · 7 lines
2.7.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#3 lore raw

PATCH v2 3/4 kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function

In reply to Bhupesh Sharma

This patch adds 'is_zlib_file()' helper function which can be used to quickly determine with the passed kernel image is a zlib compressed kernel image.

This is specifically useful for arm64 zImage (or Image.gz) support, which is introduced by later patches in this patchset.

---
 kexec/kexec-zlib.h |  1 +
 kexec/zlib.c       | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
kexec/kexec-zlib.h +1 -0
index 43c107bf4a72..16300f294759 100644
@@ -6,5 +6,6 @@
 
 #include "config.h"
 
+int is_zlib_file(const char *filename, off_t *r_size);
 char *zlib_decompress_file(const char *filename, off_t *r_size);
 #endif /* __KEXEC_ZLIB_H */
kexec/zlib.c +38 -0
index 95b608059d41..9bc340d85001 100644
@@ -23,6 +23,38 @@ static void _gzerror(gzFile fp, int *errnum, const char **errmsg)
 	}
 }
 
+int is_zlib_file(const char *filename, off_t *r_size)
+{
+	gzFile fp;
+	int errnum;
+	int is_zlib_file = 0; /* default: It's not in gzip format */
+	const char *msg;
+	ssize_t result;
+
+	if (!filename)
+		goto out;
+
+	fp = gzopen(filename, "rb");
+	if (fp == 0) {
+		_gzerror(fp, &errnum, &msg);
+		dbgprintf("Cannot open `%s': %s\n", filename, msg);
+		goto out;
+	}
+
+	if (!gzdirect(fp))
+		/* It's in gzip format */
+		is_zlib_file = 1;
+
+	result = gzclose(fp);
+	if (result != Z_OK) {
+		_gzerror(fp, &errnum, &msg);
+		dbgprintf(" Close of %s failed: %s\n", filename, msg);
+	}
+
+out:
+	return is_zlib_file;
+}
+
 char *zlib_decompress_file(const char *filename, off_t *r_size)
 {
 	gzFile fp;
@@ -84,6 +116,12 @@ fail:
 	return buf;
 }
 #else
+
+int is_zlib_file(const char *filename, off_t *r_size)
+{
+	return 0;
+}
+
 char *zlib_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size))
 {
 	return NULL;
Signature · 7 lines
2.7.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#4 lore raw

PATCH v2 4/4 kexec/arm64: Add support for handling zlib compressed (Image.gz) image

In reply to Bhupesh Sharma

Currently the kexec_file_load() support for arm64 doesn't allow handling zlib compressed (i.e. Image.gz) image.

Since most distributions use 'make zinstall' rule inside 'arch/arm64/boot/Makefile' to install the arm64
Image.gz compressed file inside the boot destination directory (for e.g. /boot), currently we cannot use kexec_file_load() to load vmlinuz (or Image.gz):

 # file /boot/vmlinuz
   /boot/vmlinuz: gzip compressed data, was "Image", <..snip..>, max
   compression, from Unix, original size 21945120

Now, since via kexec_file_load() we pass the 'fd' of Image.gz (compressed file) via the following command line ...

 # kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname
   -r`.img --reuse-cmdline

... kernel returns -EINVAL error value, as it is not able to locate the magic number =0x644d5241, which is expected in the 64-byte header of the decompressed kernel image.

We can fix this in user-space kexec-tools, which handles an 'Image.gz' being passed via kexec_file_load(), using an approach as follows:

a). Copy the contents of Image.gz to a temporary file. b). Decompress (gunzip-decompress) the contents inside the
temporary file.
c). Pass the 'fd' of the temporary file to the kernel space. So
basically the kernel space still gets a decompressed kernel image to load via kexec-tools

I tested this patch for the following three use-cases:

1. Uncompressed Image file:
#kexec -s -l Image --initrd=/boot/initramfs-uname -r.img --reuse-cmdline

2. Signed Image file:
#kexec -s -l Image.signed --initrd=/boot/initramfs-uname -r.img --reuse-cmdline

3. zlib compressed Image.gz file:
#kexec -s -l /boot/vmlinuz-uname -r --initrd=/boot/initramfs-uname -r.img --reuse-cmdline

---
 kexec/arch/arm64/Makefile             |   3 +-
 kexec/arch/arm64/kexec-arm64.c        |   1 +
 kexec/arch/arm64/kexec-arm64.h        |   7 ++
 kexec/arch/arm64/kexec-image-arm64.c  |   4 +-
 kexec/arch/arm64/kexec-zImage-arm64.c | 226 ++++++++++++++++++++++++++++++++++
 kexec/kexec.c                         |  12 ++
 6 files changed, 250 insertions(+), 3 deletions(-)
 create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
kexec/arch/arm64/Makefile +2 -1
index 9d9111caa8ed..d27c8ee1b5e7 100644
@@ -15,7 +15,8 @@ arm64_KEXEC_SRCS += \
 	kexec/arch/arm64/kexec-arm64.c \
 	kexec/arch/arm64/kexec-elf-arm64.c \
 	kexec/arch/arm64/kexec-uImage-arm64.c \
-	kexec/arch/arm64/kexec-image-arm64.c
+	kexec/arch/arm64/kexec-image-arm64.c \
+	kexec/arch/arm64/kexec-zImage-arm64.c
 
 arm64_UIMAGE = kexec/kexec-uImage.c
 
kexec/arch/arm64/kexec-arm64.c +1 -0
index 2992bce8139f..eb3a3a37307c 100644
@@ -71,6 +71,7 @@ struct file_type file_type[] = {
 	{"vmlinux", elf_arm64_probe, elf_arm64_load, elf_arm64_usage},
 	{"Image", image_arm64_probe, image_arm64_load, image_arm64_usage},
 	{"uImage", uImage_arm64_probe, uImage_arm64_load, uImage_arm64_usage},
+	{"zImage", zImage_arm64_probe, zImage_arm64_load, zImage_arm64_usage},
 };
 
 int file_types = sizeof(file_type) / sizeof(file_type[0]);
kexec/arch/arm64/kexec-arm64.h +7 -0
index cc3419f4c10f..628de79b7f70 100644
@@ -38,11 +38,18 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size);
 int image_arm64_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info);
 void image_arm64_usage(void);
+
 int uImage_arm64_probe(const char *buf, off_t len);
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
 		      struct kexec_info *info);
 void uImage_arm64_usage(void);
 
+int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size);
+int zImage_arm64_load(int argc, char **argv, const char *kernel_buf,
+	off_t kernel_size, struct kexec_info *info);
+void zImage_arm64_usage(void);
+
+
 off_t initrd_base;
 off_t initrd_size;
 
kexec/arch/arm64/kexec-image-arm64.c +2 -2
index 685a99352e39..aa8f2e22d72b 100644
@@ -114,6 +114,6 @@ exit:
 void image_arm64_usage(void)
 {
 	printf(
-"     An ARM64 binary image, compressed or not, big or little endian.\n"
-"     Typically an Image, Image.gz or Image.lzma file.\n\n");
+"     An ARM64 binary image, uncompressed, big or little endian.\n"
+"     Typically an Image file.\n\n");
 }
kexec/arch/arm64/kexec-zImage-arm64.c new file +226 -0

226 lines not on this page · raw message

kexec/kexec.c +12 -0
index 8ca3b457cac8..bc6ab3dbd10b 100644
@@ -1206,8 +1206,20 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 	kernel_buf = slurp_decompress_file(kernel, &kernel_size);
 
 	for (i = 0; i < file_types; i++) {
+#ifdef __aarch64__
+		/* handle Image.gz like cases */
+		if (is_zlib_file(kernel, &kernel_size)) {
+			if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) {
+				kernel_fd = ret;
+				break;
+			}
+		} else
+			if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
+				break;
+#else
 		if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
 			break;
+#endif
 	}
 
 	if (i == file_types) {
Signature · 7 lines
2.7.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Applied
#5 lore raw
In reply to Bhupesh Sharma
Quoted 21 lines · Bhupesh Sharma

Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.

This patchset adds the support for zlib compressed kernel images (Image.gz) for arm64 kexec-tools.

This was discussed a bit with the arm64 kernel maintainers (see [0]) and after discussing the pros and cons of adding this support in kernel-space v/s in user-space, we decided it makes more sense to add this support to the user-space kexec-tools.

Note that this series adds support for unsigned arm64 Image.gz files only as signing an Image.gz type file is not a easy implementation from kexec-tools p-o-v. Also even standard signing tools like sbsign fail to recongize the Image.gz format:

Quoted 8 lines
 $ sbsign --key certs/signing_key.pem  --cert certs/signing_key.pem Image.gz
   Invalid DOS header magic

... because that gzip file isn't a PE32+ that can be loaded by UEFI. So I will work on the same in coming days and try to send a RFC patchset for the same later on.

[0]. http://lists.infradead.org/pipermail/kexec/2019-June/023156.html

Thanks, applied for inclusion in v2.0.20.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#6 lore raw
Quoted 26 lines · Simon Horman
Quoted 24 lines · Bhupesh Sharma

Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.

This patchset adds the support for zlib compressed kernel images (Image.gz) for arm64 kexec-tools.

This was discussed a bit with the arm64 kernel maintainers (see [0]) and after discussing the pros and cons of adding this support in kernel-space v/s in user-space, we decided it makes more sense to add this support to the user-space kexec-tools.

Note that this series adds support for unsigned arm64 Image.gz files only as signing an Image.gz type file is not a easy implementation from kexec-tools p-o-v. Also even standard signing tools like sbsign fail to recongize the Image.gz format:

 $ sbsign --key certs/signing_key.pem  --cert certs/signing_key.pem Image.gz
   Invalid DOS header magic
Quoted 7 lines
Quoted 5 lines

... because that gzip file isn't a PE32+ that can be loaded by UEFI. So I will work on the same in coming days and try to send a RFC patchset for the same later on.

[0]. http://lists.infradead.org/pipermail/kexec/2019-June/023156.html

Thanks, applied for inclusion in v2.0.20.

Thanks Simon.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec