Skip to contentSkip to conversation

kexec: Support for uImage RAMDISK

PATCH In review Suzuki K. Poulose 2 participants 6 messages Mar 6, 2013 – Mar 8, 2013

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

v2 cover kexec: Support for uImage RAMDISK

The following series adds support and APIs for handling uImage formatted RAMDISK. The support for PPC has been implemented.

Changes since V1:
* Do not uncompress RAMDISK uImage (Pointed by Sebastian)
* Introduce new APIs uImage_probe_kernel/uImage_probe_ramdisk to be used by the arch specific code.
* Fix coding style related issues pointed by Sebastian.
* Add a new patch for fixing payload length calculation and also fix a vulnerability where the image is manipulated. (catches only the file size related changes. When compiled with zlib, CRC check is enabled by
default and the contents are verified.)

TODO: Implement crc check in kexec, if ZLIB is not compiled in

---

Suzuki K. Poulose (4):
kexec/uImage: Introduce uImage_probe_kernel kexec/uImage: Fix the payload length in uImage_load kexec/uImage: Recognize uImage RAM Disks ppc/uImage: Add support for RAM Disks

 include/kexec-uImage.h            |    2 ++
 kexec/arch/arm/kexec-uImage-arm.c |    2 +-
 kexec/arch/ppc/kexec-uImage-ppc.c |   29 +++++++++++++++++++++-
 kexec/arch/sh/kexec-uImage-sh.c   |    2 +-
 kexec/kexec-uImage.c              |   48 ++++++++++++++++++++++++++++++++++---
 5 files changed, 75 insertions(+), 8 deletions(-)
Signature · 7 lines
Suzuki


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

PATCH v2 1/4 kexec/uImage: Introduce uImage_probe_kernel

From: Suzuki K. Poulose <[email protected]>

uImage supports different types of payloads, including kernel, ramdisks etc. uImage_probe() as of now checks whether the supplied payload is of type KERNEL ( i.e, IH_TYPE_KERNEL or IH_TYPE_KERNEL_NOLOAD ).

Change this behaviour to return the image type, if it is one of the supported payloads. This change is in prepartion to support ramdisks in uImage format.

Introduce a uImage_probe_kernel() which can be used by the archs to check if the supplied payload is one of the KERNEL types.

---
 include/kexec-uImage.h            |    1 +
 kexec/arch/arm/kexec-uImage-arm.c |    2 +-
 kexec/arch/ppc/kexec-uImage-ppc.c |    2 +-
 kexec/arch/sh/kexec-uImage-sh.c   |    2 +-
 kexec/kexec-uImage.c              |   14 +++++++++++++-
 5 files changed, 17 insertions(+), 4 deletions(-)
include/kexec-uImage.h +1 -0
index 8c7707d..266ca73 100644
@@ -9,5 +9,6 @@ struct Image_info {
 };
 
 int uImage_probe(const unsigned char *buf, off_t len, unsigned int arch);
+int uImage_probe_kernel(const unsigned char *buf, off_t len, unsigned int arch);
 int uImage_load(const unsigned char *buf, off_t len, struct Image_info *info);
 #endif
kexec/arch/arm/kexec-uImage-arm.c +1 -1
index 4875185..03c2f4d 100644
@@ -11,7 +11,7 @@
 
 int uImage_arm_probe(const char *buf, off_t len)
 {
-	return uImage_probe(buf, len, IH_ARCH_ARM);
+	return uImage_probe_kernel(buf, len, IH_ARCH_ARM);
 }
 
 int uImage_arm_load(int argc, char **argv, const char *buf, off_t len,
kexec/arch/ppc/kexec-uImage-ppc.c +1 -1
index e55bf94..ef9adc4 100644
@@ -48,7 +48,7 @@ void uImage_ppc_usage(void)
 
 int uImage_ppc_probe(const char *buf, off_t len)
 {
-	return uImage_probe(buf, len, IH_ARCH_PPC);
+	return uImage_probe_kernel(buf, len, IH_ARCH_PPC);
 }
 
 static int ppc_load_bare_bits(int argc, char **argv, const char *buf,
kexec/arch/sh/kexec-uImage-sh.c +1 -1
index e983165..130f12c 100644
@@ -13,7 +13,7 @@
 
 int uImage_sh_probe(const char *buf, off_t len)
 {
-	return uImage_probe(buf, len, IH_ARCH_SH);
+	return uImage_probe_kernel(buf, len, IH_ARCH_SH);
 }
 
 int uImage_sh_load(int argc, char **argv, const char *buf, off_t len,
kexec/kexec-uImage.c +13 -1
index 3bc85c5..3799a3b 100644
@@ -16,6 +16,10 @@
  * Basic uImage loader. Not rocket science.
  */
 
+/*
+ * Returns the image type if everything goes well. This would
+ * allow the user to decide if the image is of their interest.
+ */
 int uImage_probe(const unsigned char *buf, off_t len, unsigned int arch)
 {
 	struct image_header header;
@@ -84,7 +88,15 @@ int uImage_probe(const unsigned char *buf, off_t len, unsigned int arch)
 		return -1;
 	}
 #endif
-	return 0;
+	return (int)header.ih_type;
+}
+
+int uImage_probe_kernel(const unsigned char *buf, off_t len, unsigned int arch)
+{
+	int type = uImage_probe(buf, len, arch);
+
+	return (type == IH_TYPE_KERNEL || type == IH_TYPE_KERNEL_NOLOAD) ? 
+			0 : -1;
 }
 
 #ifdef HAVE_LIBZ

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

#2 lore raw

PATCH v2 2/4 kexec/uImage: Fix the payload length in uImage_load

In reply to Suzuki K. Poulose

From: Suzuki K. Poulose <[email protected]>

For payloads without any compression, the image->len is set to the length of the entire uImage which includes the uImage header. This should be filled in from ih_size field of the uImage header.

This can cause a buffer overflow, leading the sha256_process to overrun the initrd buffer. Also, prevents a vulnerability where the image has been appended with additional data. The crc check is performed only when compiled with zlib.

TODO: Implement CRC check if ZLIB is not compiled in.

---
 kexec/kexec-uImage.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
kexec/kexec-uImage.c +13 -2
index 3799a3b..9e275b2 100644
@@ -208,14 +208,25 @@ int uImage_load(const unsigned char *buf, off_t len, struct Image_info *image)
 {
 	const struct image_header *header = (const struct image_header *)buf;
 	const unsigned char *img_buf = buf + sizeof(struct image_header);
-	off_t img_len = len - sizeof(struct image_header);
+	off_t img_len = header->ih_size;
+
+	/*
+	 * Prevent loading a modified image.
+	 * CRC check is perfomed only when zlib is compiled
+	 * in. This check will help us to detect
+	 * size related vulnerabilities. 	
+	 */
+ 	if (img_len != (len - sizeof(struct image_header))) {
+		printf("Image size doesn't match the header\n");
+		return -1;
+	}
 
 	image->base = cpu_to_be32(header->ih_load);
 	image->ep = cpu_to_be32(header->ih_ep);
 	switch (header->ih_comp) {
 	case IH_COMP_NONE:
 		image->buf = img_buf;
-		image->len = len;
+		image->len = img_len;
 		return 0;
 		break;
 

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

#3 lore raw

PATCH v2 3/4 kexec/uImage: Recognize uImage RAM Disks

In reply to Suzuki K. Poulose

From: Suzuki K. Poulose <[email protected]>

Add IH_TYPE_RAMDISK as a recognized image type.

uImage_load shouldn't decompress the RAMDISK type images, since uboot doesn't do it.

---
 include/kexec-uImage.h |    1 +
 kexec/kexec-uImage.c   |   19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
include/kexec-uImage.h +1 -0
index 266ca73..4725157 100644
@@ -10,5 +10,6 @@ struct Image_info {
 
 int uImage_probe(const unsigned char *buf, off_t len, unsigned int arch);
 int uImage_probe_kernel(const unsigned char *buf, off_t len, unsigned int arch);
+int uImage_probe_ramdisk(const unsigned char *buf, off_t len, unsigned int arch);
 int uImage_load(const unsigned char *buf, off_t len, struct Image_info *info);
 #endif
kexec/kexec-uImage.c +18 -1
index 9e275b2..1ad02f4 100644
@@ -47,6 +47,8 @@ int uImage_probe(const unsigned char *buf, off_t len, unsigned int arch)
 	case IH_TYPE_KERNEL:
 	case IH_TYPE_KERNEL_NOLOAD:
 		break;
+	case IH_TYPE_RAMDISK:
+		break;
 	default:
 		printf("uImage type %d unsupported\n", header.ih_type);
 		return -1;
@@ -99,6 +101,12 @@ int uImage_probe_kernel(const unsigned char *buf, off_t len, unsigned int arch)
 			0 : -1;
 }
 
+int uImage_probe_ramdisk(const unsigned char *buf, off_t len, unsigned int arch)
+{
+	int type = uImage_probe(buf, len, arch);
+	return (type == IH_TYPE_RAMDISK) ? 0 : -1;
+}
+
 #ifdef HAVE_LIBZ
 /* gzip flag byte */
 #define ASCII_FLAG	0x01 /* bit 0 set: file probably ascii text */
@@ -231,7 +239,16 @@ int uImage_load(const unsigned char *buf, off_t len, struct Image_info *image)
 		break;
 
 	case IH_COMP_GZIP:
-		return uImage_gz_load(img_buf, img_len, image);
+		/*
+		 * uboot doesn't decompress the RAMDISK images.
+		 * Comply to the uboot behaviour.
+		 */
+		if (header->ih_type == IH_TYPE_RAMDISK) {
+			image->buf = img_buf;
+			image->len = img_len;
+			return 0;
+		} else
+			return uImage_gz_load(img_buf, img_len, image);
 		break;
 
 	default:

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

#4 lore raw

PATCH v2 4/4 ppc/uImage: Add support for RAM Disks

In reply to Suzuki K. Poulose

From: Suzuki K. Poulose <[email protected]>

Handle the RAM Disks in uImage format

---
 kexec/arch/ppc/kexec-uImage-ppc.c |   27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
kexec/arch/ppc/kexec-uImage-ppc.c +26 -1
index ef9adc4..e0bc7bb 100644
@@ -46,6 +46,31 @@ void uImage_ppc_usage(void)
 	);
 }
 
+/*
+ * Load the ramdisk into buffer.
+ *  If the supplied image is in uImage format use
+ *  uImage_load() to read the payload from the image.
+ */
+char *slurp_ramdisk_ppc(const char *filename, off_t *r_size)
+{
+	struct Image_info img;
+	off_t size;
+	const unsigned char *buf = slurp_file(filename, &size);
+
+	/* Check if this is a uImage RAMDisk */
+	if (buf &&
+	    uImage_probe_ramdisk(buf, size, IH_ARCH_PPC) == 0) {
+		if (uImage_load(buf, size, &img) != 0)
+			die("uImage: Reading %ld bytes from %s failed\n",
+				size, filename);
+		buf = img.buf;
+		size = img.len;
+	}
+
+	*r_size = size;
+	return buf;
+}
+	
 int uImage_ppc_probe(const char *buf, off_t len)
 {
 	return uImage_probe_kernel(buf, len, IH_ARCH_PPC);
@@ -190,7 +215,7 @@ static int ppc_load_bare_bits(int argc, char **argv, const char *buf,
 	blob_buf = fixup_dtb_init(info, blob_buf, &blob_size, load_addr, &dtb_addr);
 
 	if (ramdisk) {
-		seg_buf = slurp_file(ramdisk, &seg_size);
+		seg_buf = slurp_ramdisk_ppc(ramdisk, &seg_size);
 		/* Load ramdisk at top of memory */
 		hole_addr = add_buffer(info, seg_buf, seg_size, seg_size,
 				0, dtb_addr + blob_size, max_addr, -1);

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

#5 lore raw
In reply to Suzuki K. Poulose
Quoted 17 lines · Suzuki K. Poulose

The following series adds support and APIs for handling uImage formatted RAMDISK. The support for PPC has been implemented.

Changes since V1:
* Do not uncompress RAMDISK uImage (Pointed by Sebastian)
* Introduce new APIs uImage_probe_kernel/uImage_probe_ramdisk to be used by the arch specific code.
* Fix coding style related issues pointed by Sebastian.
* Add a new patch for fixing payload length calculation and also fix a vulnerability where the image is manipulated. (catches only the file size related changes. When compiled with zlib, CRC check is enabled by
default and the contents are verified.)

TODO: Implement crc check in kexec, if ZLIB is not compiled in

Quoted 7 lines

---

Suzuki K. Poulose (4):
kexec/uImage: Introduce uImage_probe_kernel kexec/uImage: Fix the payload length in uImage_load kexec/uImage: Recognize uImage RAM Disks ppc/uImage: Add support for RAM Disks

Thanks, all 4 patches applied.

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