Skip to contentSkip to conversation

Some reserved memory hardenings

PATCH Superseded Tero Kristo 3 participants 13 messages Jun 12, 2020 – Oct 20, 2020 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Some reserved memory hardenings

Hi,

This series applies some reserved memory checks to the boot commands, and enhances the bdinfo command to dump out reserved memory sections. Motivation behind these is mostly to help detecting overwriting either DTB, ramdisk or Linux image with other images. Right now, the boot in most cases just continues but will hang before anything is printed out from Linux. In some cases the boot continues even if u-boot detects something that is terribly wrong (like DTB has been overwritten.)

Patch #3 has some holes in it (it does not pass the OS image / size in some cases.) I wasn't quite sure what parameter to pass in these cases so I just left them at zero, meaning no checks are done.

The layout of the reserved memory section printout with patch #1 could maybe also made look bit neater, I just re-used the existing lmb debug functionality for this purpose.

-Tero

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

#1 lore raw

PATCH 1/4 lmb/bdinfo: dump lmb info via bdinfo

Dump lmb status from the bdinfo command. This is useful for seeing the reserved memory regions from the u-boot cmdline.

---
 cmd/bdinfo.c  |  8 +++++++-
 include/lmb.h |  1 +
 lib/lmb.c     | 42 +++++++++++++++++++++++-------------------
 3 files changed, 31 insertions(+), 20 deletions(-)
cmd/bdinfo.c +7 -1
index dba552b03f..05e1b27de0 100644
@@ -10,6 +10,7 @@
 #include <common.h>
 #include <command.h>
 #include <env.h>
+#include <lmb.h>
 #include <net.h>
 #include <vsprintf.h>
 #include <asm/cache.h>
@@ -365,8 +366,13 @@ static int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
 	print_num("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
 #endif
-	if (gd->fdt_blob)
+	if (gd->fdt_blob) {
+		struct lmb lmb;
 		print_num("fdt_blob", (ulong)gd->fdt_blob);
+		lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
+		lmb_dump_all_force(&lmb);
+	}
+
 	print_cpu_word_size();
 
 	return 0;
include/lmb.h +1 -0
index 3b338dfee0..0d90ab1d65 100644
@@ -48,6 +48,7 @@ extern int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr);
 extern long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size);
 
 extern void lmb_dump_all(struct lmb *lmb);
+extern void lmb_dump_all_force(struct lmb *lmb);
 
 static inline phys_size_t
 lmb_size_bytes(struct lmb_region *type, unsigned long region_nr)
lib/lmb.c +23 -19

56 lines not on this page · raw message

Signature · 4 lines
2.17.1

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#2 lore raw

PATCH 1/4 lmb/bdinfo: dump lmb info via bdinfo

Quoted 27 lines · Tero Kristo

Dump lmb status from the bdinfo command. This is useful for seeing the reserved memory regions from the u-boot cmdline.

---
 cmd/bdinfo.c  |  8 +++++++-
 include/lmb.h |  1 +
 lib/lmb.c     | 42 +++++++++++++++++++++++-------------------
 3 files changed, 31 insertions(+), 20 deletions(-)
cmd/bdinfo.c +1 -1
index dba552b03f..05e1b27de0 100644
@@ -10,6 +10,7 @@
 #include <common.h>
 #include <command.h>
 #include <env.h>
+#include <lmb.h>
 #include <net.h>
 #include <vsprintf.h>
 #include <asm/cache.h>
@@ -365,8 +366,13 @@ static int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
 	print_num("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
 #endif
-	if (gd->fdt_blob)

cmd/bdinfo.c static int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, · line 378

Quoted 10 lines
cmd/bdinfo.c +6 -0
+	if (gd->fdt_blob) {
+		struct lmb lmb;
 		print_num("fdt_blob", (ulong)gd->fdt_blob);
+		lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
+		lmb_dump_all_force(&lmb);
+	}
+
 	print_cpu_word_size();
 
 	return 0;

Can you please rebase this on top of master? Simon's series to cleanup bdinfo stuff has been applied, thanks. The rest of your changes are fine and under test now.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200717/0fa9e377/attachment.sig>
#3 lore raw

PATCH 1/4 lmb/bdinfo: dump lmb info via bdinfo

Quoted 38 lines · Tom Rini
Quoted 36 lines · Tero Kristo

Dump lmb status from the bdinfo command. This is useful for seeing the reserved memory regions from the u-boot cmdline.

---
  cmd/bdinfo.c  |  8 +++++++-
  include/lmb.h |  1 +
  lib/lmb.c     | 42 +++++++++++++++++++++++-------------------
  3 files changed, 31 insertions(+), 20 deletions(-)
cmd/bdinfo.c +7 -1
index dba552b03f..05e1b27de0 100644
@@ -10,6 +10,7 @@
  #include <common.h>
  #include <command.h>
  #include <env.h>
+#include <lmb.h>
  #include <net.h>
  #include <vsprintf.h>
  #include <asm/cache.h>
@@ -365,8 +366,13 @@ static int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
  #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
  	print_num("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
  #endif
-	if (gd->fdt_blob)
+	if (gd->fdt_blob) {
+		struct lmb lmb;
  		print_num("fdt_blob", (ulong)gd->fdt_blob);
+		lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
+		lmb_dump_all_force(&lmb);
+	}
+
  	print_cpu_word_size();
 
Quoted 5 lines

cmd/bdinfo.c static int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, · line 378

Quoted 1 line
cmd/bdinfo.c +0 -0
  	return 0;

Can you please rebase this on top of master? Simon's series to cleanup bdinfo stuff has been applied, thanks. The rest of your changes are fine and under test now.

Yeah, I can repost this single patch. Thanks for the heads-up.

-Tero

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

#4 lore raw

PATCH 2/4 image: fdt: bail out with error if no boot time FDT image found

In reply to Tero Kristo

Currently the boot continues if the FDT image is clearly corrupted, which just causes the loaded OS to hang. Abort boot properly if the FDT is corrupted.

---
 common/image-fdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/image-fdt.c +1 -1
index b63e772bd6..7005b34966 100644
@@ -426,7 +426,7 @@ int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
 			break;
 		default:
 			puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
-			goto no_fdt;
+			goto error;
 		}
 
 		printf("   Booting using the fdt blob at %#08lx\n", fdt_addr);
Signature · 4 lines
2.17.1

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#5 lore raw

PATCH 2/4 image: fdt: bail out with error if no boot time FDT image found

Quoted 5 lines · Tero Kristo

Currently the boot continues if the FDT image is clearly corrupted, which just causes the loaded OS to hang. Abort boot properly if the FDT is corrupted.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200717/0a2c2d76/attachment.sig>
#6 lore raw

PATCH 3/4 common: bootm: add checks to verify if ramdisk / fdtimage overlaps OS image

In reply to Tero Kristo

These cases are typically fatal and are difficult to debug for random users. Add checks for detecting overlapping images and abort if overlap is detected.

---
 cmd/booti.c       |  2 +-
 cmd/bootz.c       |  2 +-
 common/bootm.c    | 29 +++++++++++++++++++++++++++--
 common/bootm_os.c |  4 ++--
 include/bootm.h   |  3 ++-
 5 files changed, 33 insertions(+), 7 deletions(-)
cmd/booti.c +1 -1
index ae37975494..6153b229cf 100644
@@ -93,7 +93,7 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, relocated_addr, image_size))
 		return 1;
 
 	return 0;
cmd/bootz.c +1 -1
index bc15fd8ec4..1c8b0cf89f 100644
@@ -54,7 +54,7 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, zi_start, zi_end - zi_start))
 		return 1;
 
 	return 0;
common/bootm.c +27 -2
index 2ed7521520..247b600d9c 100644
@@ -228,6 +228,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
  * @flag: Ignored Argument
  * @argc: command argument count
  * @argv: command argument list
+ * @start: OS image start address
+ * @size: OS image size
  *
  * boot_find_images() will attempt to load an available ramdisk,
  * flattened device tree, as well as specifically marked
@@ -239,7 +241,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
  *     0, if all existing images were loaded correctly
  *     1, if an image is found but corrupted, or invalid
  */
-int bootm_find_images(int flag, int argc, char *const argv[])
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size)
 {
 	int ret;
 
@@ -251,6 +254,16 @@ int bootm_find_images(int flag, int argc, char *const argv[])
 		return 1;
 	}
 
+	/* check if ramdisk overlaps OS image */
+	if (images.rd_start && (((ulong)images.rd_start >= start &&
+				 (ulong)images.rd_start <= start + size) ||
+				((ulong)images.rd_end >= start &&
+				 (ulong)images.rd_end <= start + size))) {
+		printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
 #if IMAGE_ENABLE_OF_LIBFDT
 	/* find flattened device tree */
 	ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
@@ -259,6 +272,18 @@ int bootm_find_images(int flag, int argc, char *const argv[])
 		puts("Could not find a valid device tree\n");
 		return 1;
 	}
+
+	/* check if FDT overlaps OS image */
+	if (images.ft_addr &&
+	    (((ulong)images.ft_addr >= start &&
+	      (ulong)images.ft_addr <= start + size) ||
+	     ((ulong)images.ft_addr + images.ft_len >= start &&
+	      (ulong)images.ft_addr + images.ft_len <= start + size))) {
+		printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
 	if (CONFIG_IS_ENABLED(CMD_FDT))
 		set_working_fdt_addr(map_to_sysmem(images.ft_addr));
 #endif
@@ -294,7 +319,7 @@ static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
 	     (images.os.type == IH_TYPE_MULTI)) &&
 	    (images.os.os == IH_OS_LINUX ||
 		 images.os.os == IH_OS_VXWORKS))
-		return bootm_find_images(flag, argc, argv);
+		return bootm_find_images(flag, argc, argv, 0, 0);
 
 	return 0;
 }
common/bootm_os.c +2 -2
index 55296483f7..a3c360e80a 100644
@@ -495,7 +495,7 @@ static int do_bootm_tee(int flag, int argc, char *const argv[],
 		return ret;
 
 	/* Locate FDT etc */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
 	if (ret)
 		return ret;
 
@@ -516,7 +516,7 @@ static int do_bootm_efi(int flag, int argc, char *const argv[],
 		return 0;
 
 	/* Locate FDT, if provided */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
 	if (ret)
 		return ret;
 
include/bootm.h +2 -1
index 1e7f29e134..0350c349f3 100644
@@ -53,7 +53,8 @@ int boot_selected_os(int argc, char *const argv[], int state,
 ulong bootm_disable_interrupts(void);
 
 /* This is a special function used by booti/bootz */
-int bootm_find_images(int flag, int argc, char *const argv[]);
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size);
 
 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 		    char *const argv[], int states, bootm_headers_t *images,
Signature · 4 lines
2.17.1

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#7 lore raw

PATCH 3/4 common: bootm: add checks to verify if ramdisk / fdtimage overlaps OS image

Quoted 5 lines · Tero Kristo

These cases are typically fatal and are difficult to debug for random users. Add checks for detecting overlapping images and abort if overlap is detected.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200717/bd911fa3/attachment.sig>
#8 lore raw

PATCH 3/4 common: bootm: add checks to verify if ramdisk / fdtimage overlaps OS image

In reply to Tero Kristo

Dear Tero,

Quoted 3 lines · Tero Kristo

These cases are typically fatal and are difficult to debug for random users. Add checks for detecting overlapping images and abort if overlap is detected.

I have a question about your patch.. because I have confused... So i want to clear what i missed.

During booting with ramdisk, my target is stopped.

ramdisk start address = 0x2700000
size = 0x800000
kernel start - 0x2F00000

bootm 0x2f00000 0x2700000:0x800000 0x02600000

ramdisk.end is ramdisk_start + size (0x2F00000)

Then it will be used until 0x2efffff, not 0x2f00000. When i have checked, it doesn't overwrite RD image.

common/bootm.c int bootm_find_images(int flag, int argc, char *const argv[]) · line 265

Quoted 8 lines
common/bootm.c +8 -0
+	/* check if ramdisk overlaps OS image */
+	if (images.rd_start && (((ulong)images.rd_start >= start &&
+				 (ulong)images.rd_start <= start + size) ||
+				((ulong)images.rd_end >= start &&
+				 (ulong)images.rd_end <= start + size))) {
+		printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);> +		return 1;
+	}

Because of hit this condition...So doesn't boot...

I think that it needs to change the below conditions..

images.rd_start < start + size or images.rd_start < start + size - 1. images.rd_end > start or image.rd_end - 1 >= start

If i misunderstood something, let me know, plz.

Best Regards,
Jaehoon Chung

Quoted 141 lines
---
 cmd/booti.c       |  2 +-
 cmd/bootz.c       |  2 +-
 common/bootm.c    | 29 +++++++++++++++++++++++++++--
 common/bootm_os.c |  4 ++--
 include/bootm.h   |  3 ++-
 5 files changed, 33 insertions(+), 7 deletions(-)
cmd/booti.c +1 -1
index ae37975494..6153b229cf 100644
@@ -93,7 +93,7 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, relocated_addr, image_size))
 		return 1;
 
 	return 0;
cmd/bootz.c +1 -1
index bc15fd8ec4..1c8b0cf89f 100644
@@ -54,7 +54,7 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, zi_start, zi_end - zi_start))
 		return 1;
 
 	return 0;
common/bootm.c +27 -2
index 2ed7521520..247b600d9c 100644
@@ -228,6 +228,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
  * @flag: Ignored Argument
  * @argc: command argument count
  * @argv: command argument list
+ * @start: OS image start address
+ * @size: OS image size
  *
  * boot_find_images() will attempt to load an available ramdisk,
  * flattened device tree, as well as specifically marked
@@ -239,7 +241,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
  *     0, if all existing images were loaded correctly
  *     1, if an image is found but corrupted, or invalid
  */
-int bootm_find_images(int flag, int argc, char *const argv[])
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size)
 {
 	int ret;
 
@@ -251,6 +254,16 @@ int bootm_find_images(int flag, int argc, char *const argv[])
 		return 1;
 	}
 
+	/* check if ramdisk overlaps OS image */
+	if (images.rd_start && (((ulong)images.rd_start >= start &&
+				 (ulong)images.rd_start <= start + size) ||
+				((ulong)images.rd_end >= start &&
+				 (ulong)images.rd_end <= start + size))) {
+		printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
 #if IMAGE_ENABLE_OF_LIBFDT
 	/* find flattened device tree */
 	ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
@@ -259,6 +272,18 @@ int bootm_find_images(int flag, int argc, char *const argv[])
 		puts("Could not find a valid device tree\n");
 		return 1;
 	}
+
+	/* check if FDT overlaps OS image */
+	if (images.ft_addr &&
+	    (((ulong)images.ft_addr >= start &&
+	      (ulong)images.ft_addr <= start + size) ||
+	     ((ulong)images.ft_addr + images.ft_len >= start &&
+	      (ulong)images.ft_addr + images.ft_len <= start + size))) {
+		printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
 	if (CONFIG_IS_ENABLED(CMD_FDT))
 		set_working_fdt_addr(map_to_sysmem(images.ft_addr));
 #endif
@@ -294,7 +319,7 @@ static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
 	     (images.os.type == IH_TYPE_MULTI)) &&
 	    (images.os.os == IH_OS_LINUX ||
 		 images.os.os == IH_OS_VXWORKS))
-		return bootm_find_images(flag, argc, argv);
+		return bootm_find_images(flag, argc, argv, 0, 0);
 
 	return 0;
 }
common/bootm_os.c +2 -2
index 55296483f7..a3c360e80a 100644
@@ -495,7 +495,7 @@ static int do_bootm_tee(int flag, int argc, char *const argv[],
 		return ret;
 
 	/* Locate FDT etc */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
 	if (ret)
 		return ret;
 
@@ -516,7 +516,7 @@ static int do_bootm_efi(int flag, int argc, char *const argv[],
 		return 0;
 
 	/* Locate FDT, if provided */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
 	if (ret)
 		return ret;
 
include/bootm.h +2 -1
index 1e7f29e134..0350c349f3 100644
@@ -53,7 +53,8 @@ int boot_selected_os(int argc, char *const argv[], int state,
 ulong bootm_disable_interrupts(void);
 
 /* This is a special function used by booti/bootz */
-int bootm_find_images(int flag, int argc, char *const argv[]);
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size);
 
 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 		    char *const argv[], int states, bootm_headers_t *images,
#9 lore raw

PATCH 3/4 common: bootm: add checks to verify if ramdisk / fdtimage overlaps OS image

Quoted 36 lines · Jaehoon Chung

Dear Tero,

Quoted 3 lines · Tero Kristo

These cases are typically fatal and are difficult to debug for random users. Add checks for detecting overlapping images and abort if overlap is detected.

I have a question about your patch.. because I have confused... So i want to clear what i missed.

During booting with ramdisk, my target is stopped.

ramdisk start address = 0x2700000
size = 0x800000
kernel start - 0x2F00000

bootm 0x2f00000 0x2700000:0x800000 0x02600000

ramdisk.end is ramdisk_start + size (0x2F00000)

Then it will be used until 0x2efffff, not 0x2f00000. When i have checked, it doesn't overwrite RD image.

common/bootm.c int bootm_find_images(int flag, int argc, char *const argv[]) · line 265

Quoted 8 lines
common/bootm.c +8 -0
+	/* check if ramdisk overlaps OS image */
+	if (images.rd_start && (((ulong)images.rd_start >= start &&
+				 (ulong)images.rd_start <= start + size) ||
+				((ulong)images.rd_end >= start &&
+				 (ulong)images.rd_end <= start + size))) {
+		printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);> +		return 1;
+	}

Because of hit this condition...So doesn't boot...

Quoted 4 lines

I think that it needs to change the below conditions..

images.rd_start < start + size or images.rd_start < start + size - 1. images.rd_end > start or image.rd_end - 1 >= start

I believe you are actually right. However, both checks for rd_end should take the last byte into account, and would need to change the last check to be images.rd_end < start + size in addition to your changes above. Also, we would need to add a check to see if rd_start < start && rd_end

Quoted 1 line

= start + size in addition to everything we check here to cover every

possible overlap scenario...

-Tero

Quoted 139 lines

If i misunderstood something, let me know, plz.

Best Regards,
Jaehoon Chung

Quoted 133 lines
---
  cmd/booti.c       |  2 +-
  cmd/bootz.c       |  2 +-
  common/bootm.c    | 29 +++++++++++++++++++++++++++--
  common/bootm_os.c |  4 ++--
  include/bootm.h   |  3 ++-
  5 files changed, 33 insertions(+), 7 deletions(-)
cmd/booti.c +1 -1
index ae37975494..6153b229cf 100644
@@ -93,7 +93,7 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
  	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  	 * have a header that provide this informaiton.
  	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, relocated_addr, image_size))
  		return 1;
 
  	return 0;
cmd/bootz.c +1 -1
index bc15fd8ec4..1c8b0cf89f 100644
@@ -54,7 +54,7 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc,
  	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  	 * have a header that provide this informaiton.
  	 */
-	if (bootm_find_images(flag, argc, argv))
+	if (bootm_find_images(flag, argc, argv, zi_start, zi_end - zi_start))
  		return 1;
 
  	return 0;
common/bootm.c +27 -2
index 2ed7521520..247b600d9c 100644
@@ -228,6 +228,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
   * @flag: Ignored Argument
   * @argc: command argument count
   * @argv: command argument list
+ * @start: OS image start address
+ * @size: OS image size
   *
   * boot_find_images() will attempt to load an available ramdisk,
   * flattened device tree, as well as specifically marked
@@ -239,7 +241,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
   *     0, if all existing images were loaded correctly
   *     1, if an image is found but corrupted, or invalid
   */
-int bootm_find_images(int flag, int argc, char *const argv[])
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size)
  {
  	int ret;
 
@@ -251,6 +254,16 @@ int bootm_find_images(int flag, int argc, char *const argv[])
  		return 1;
  	}
 
+	/* check if ramdisk overlaps OS image */
+	if (images.rd_start && (((ulong)images.rd_start >= start &&
+				 (ulong)images.rd_start <= start + size) ||
+				((ulong)images.rd_end >= start &&
+				 (ulong)images.rd_end <= start + size))) {
+		printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
  #if IMAGE_ENABLE_OF_LIBFDT
  	/* find flattened device tree */
  	ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
@@ -259,6 +272,18 @@ int bootm_find_images(int flag, int argc, char *const argv[])
  		puts("Could not find a valid device tree\n");
  		return 1;
  	}
+
+	/* check if FDT overlaps OS image */
+	if (images.ft_addr &&
+	    (((ulong)images.ft_addr >= start &&
+	      (ulong)images.ft_addr <= start + size) ||
+	     ((ulong)images.ft_addr + images.ft_len >= start &&
+	      (ulong)images.ft_addr + images.ft_len <= start + size))) {
+		printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
+		       start, start + size);
+		return 1;
+	}
+
  	if (CONFIG_IS_ENABLED(CMD_FDT))
  		set_working_fdt_addr(map_to_sysmem(images.ft_addr));
  #endif
@@ -294,7 +319,7 @@ static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
  	     (images.os.type == IH_TYPE_MULTI)) &&
  	    (images.os.os == IH_OS_LINUX ||
  		 images.os.os == IH_OS_VXWORKS))
-		return bootm_find_images(flag, argc, argv);
+		return bootm_find_images(flag, argc, argv, 0, 0);
 
  	return 0;
  }
common/bootm_os.c +2 -2
index 55296483f7..a3c360e80a 100644
@@ -495,7 +495,7 @@ static int do_bootm_tee(int flag, int argc, char *const argv[],
  		return ret;
 
  	/* Locate FDT etc */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
  	if (ret)
  		return ret;
 
@@ -516,7 +516,7 @@ static int do_bootm_efi(int flag, int argc, char *const argv[],
  		return 0;
 
  	/* Locate FDT, if provided */
-	ret = bootm_find_images(flag, argc, argv);
+	ret = bootm_find_images(flag, argc, argv, 0, 0);
  	if (ret)
  		return ret;
 
include/bootm.h +0 -0
index 1e7f29e134..0350c349f3 100644
@@ -53,7 +53,8 @@ int boot_selected_os(int argc, char *const argv[], int state,
  ulong bootm_disable_interrupts(void);
 
Quoted 9 lines

include/bootm.h int boot_selected_os(int argc, char *const argv[], int state, · line 60

Quoted 8 lines
include/bootm.h +2 -1
  /* This is a special function used by booti/bootz */
-int bootm_find_images(int flag, int argc, char *const argv[]);
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+		      ulong size);
 
  int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
  		    char *const argv[], int states, bootm_headers_t *images,

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

#10 lore raw

PATCH 3/4 common: bootm: add checks to verify if ramdisk / fdtimage overlaps OS image

Quoted 35 lines · Tero Kristo
Quoted 34 lines · Jaehoon Chung

Dear Tero,

Quoted 3 lines · Tero Kristo

These cases are typically fatal and are difficult to debug for random users. Add checks for detecting overlapping images and abort if overlap is detected.

I have a question about your patch.. because I have confused... So i want to clear what i missed.

During booting with ramdisk, my target is stopped.

ramdisk start address = 0x2700000
size = 0x800000
kernel start - 0x2F00000

bootm 0x2f00000 0x2700000:0x800000 0x02600000

ramdisk.end is ramdisk_start + size (0x2F00000)

Then it will be used until 0x2efffff, not 0x2f00000. When i have checked, it doesn't overwrite RD image.

Quoted 8 lines

+??? /* check if ramdisk overlaps OS image */
+??? if (images.rd_start && (((ulong)images.rd_start >= start && +???????????????? (ulong)images.rd_start <= start + size) || +??????????????? ((ulong)images.rd_end >= start && +???????????????? (ulong)images.rd_end <= start + size))) { +??????? printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n", +?????????????? start, start + size);> +??????? return 1; +??? }

Quoted 8 lines
Quoted 6 lines

Because of hit this condition...So doesn't boot...

I think that it needs to change the below conditions..

images.rd_start < start + size or images.rd_start < start + size - 1. images.rd_end > start or image.rd_end - 1 >= start

I believe you are actually right. However, both checks for rd_end should take the last byte into account, and would need to change the last check to be images.rd_end < start + size in addition to your changes above. Also, we would need to add a check to see if rd_start < start && rd_end >= start + size in addition to everything we check here to cover every possible overlap scenario...

common/bootm.c +5 -3
index b3377490b3..36273be1cf 100644
@@ -256,9 +256,11 @@ int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
 
        /* check if ramdisk overlaps OS image */
        if (images.rd_start && (((ulong)images.rd_start >= start &&
-                                (ulong)images.rd_start <= start + size) ||
-                               ((ulong)images.rd_end >= start &&
-                                (ulong)images.rd_end <= start + size))) {
+                                (ulong)images.rd_start < start + size) ||
+                               ((ulong)images.rd_end > start &&
+                                (ulong)images.rd_end <= start + size)) ||
+                               ((ulong)images.rd_start < start &&
+                                (ulong)images.rd_end >= start + size)) {
                printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
                       start, start + size);
                return 1;

If you're ok, then i will send patch for fixing.

Best Regards,
Jaehoon Chung

Quoted 144 lines

-Tero

Quoted 137 lines

If i misunderstood something, let me know, plz.

Best Regards,
Jaehoon Chung

Quoted 130 lines

---

? cmd/booti.c?????? |? 2 +-
? cmd/bootz.c?????? |? 2 +-
? common/bootm.c??? | 29 +++++++++++++++++++++++++++--
? common/bootm_os.c |? 4 ++--
? include/bootm.h?? |? 3 ++-

? 5 files changed, 33 insertions(+), 7 deletions(-)

cmd/booti.c +0 -0
index ae37975494..6153b229cf 100644
@@ -93,7 +93,7 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,

?????? * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not ?????? * have a header that provide this informaiton.
?????? */
-??? if (bootm_find_images(flag, argc, argv))
+??? if (bootm_find_images(flag, argc, argv, relocated_addr, image_size)) ????????? return 1;
? ????? return 0;

cmd/bootz.c +0 -0
index bc15fd8ec4..1c8b0cf89f 100644
@@ -54,7 +54,7 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc,

?????? * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not ?????? * have a header that provide this informaiton.
?????? */
-??? if (bootm_find_images(flag, argc, argv))
+??? if (bootm_find_images(flag, argc, argv, zi_start, zi_end - zi_start)) ????????? return 1;
? ????? return 0;

common/bootm.c +0 -0
index 2ed7521520..247b600d9c 100644
@@ -228,6 +228,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,

?? * @flag: Ignored Argument
?? * @argc: command argument count
?? * @argv: command argument list
+ * @start: OS image start address
+ * @size: OS image size
?? *
?? * boot_find_images() will attempt to load an available ramdisk, ?? * flattened device tree, as well as specifically marked

common/bootm.c +0 -0
@@ -239,7 +241,8 @@ static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,

?? *???? 0, if all existing images were loaded correctly ?? *???? 1, if an image is found but corrupted, or invalid ?? */

common/bootm.c +2 -1
-int bootm_find_images(int flag, int argc, char *const argv[])
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+????????????? ulong size)

? {
????? int ret;
? @@ -251,6 +254,16 @@ int bootm_find_images(int flag, int argc, char *const argv[]) ????????? return 1;
????? }
? +??? /* check if ramdisk overlaps OS image */
+??? if (images.rd_start && (((ulong)images.rd_start >= start && +???????????????? (ulong)images.rd_start <= start + size) || +??????????????? ((ulong)images.rd_end >= start &&
+???????????????? (ulong)images.rd_end <= start + size))) {
+??????? printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n", +?????????????? start, start + size);
+??????? return 1;
+??? }
+
? #if IMAGE_ENABLE_OF_LIBFDT
????? /* find flattened device tree */
????? ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,

common/bootm.c +12 -0
@@ -259,6 +272,18 @@ int bootm_find_images(int flag, int argc, char *const argv[])
 ????????? puts("Could not find a valid device tree\n");
 ????????? return 1;
 ????? }
+
+??? /* check if FDT overlaps OS image */
+??? if (images.ft_addr &&
+??????? (((ulong)images.ft_addr >= start &&
+????????? (ulong)images.ft_addr <= start + size) ||
+???????? ((ulong)images.ft_addr + images.ft_len >= start &&
+????????? (ulong)images.ft_addr + images.ft_len <= start + size))) {
+??????? printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
+?????????????? start, start + size);
+??????? return 1;
+??? }
+
 ????? if (CONFIG_IS_ENABLED(CMD_FDT))
 ????????? set_working_fdt_addr(map_to_sysmem(images.ft_addr));

? #endif

common/bootm.c +0 -0
@@ -294,7 +319,7 @@ static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,

?????????? (images.os.type == IH_TYPE_MULTI)) && ????????? (images.os.os == IH_OS_LINUX || ?????????? images.os.os == IH_OS_VXWORKS)) -??????? return bootm_find_images(flag, argc, argv); +??????? return bootm_find_images(flag, argc, argv, 0, 0); ? ????? return 0;
? }

common/bootm_os.c +0 -0
index 55296483f7..a3c360e80a 100644
@@ -495,7 +495,7 @@ static int do_bootm_tee(int flag, int argc, char *const argv[],
 ????????? return ret;

? ????? /* Locate FDT etc */
-??? ret = bootm_find_images(flag, argc, argv);
+??? ret = bootm_find_images(flag, argc, argv, 0, 0);
????? if (ret)
????????? return ret;
? @@ -516,7 +516,7 @@ static int do_bootm_efi(int flag, int argc, char *const argv[], ????????? return 0;
? ????? /* Locate FDT, if provided */
-??? ret = bootm_find_images(flag, argc, argv);
+??? ret = bootm_find_images(flag, argc, argv, 0, 0);
????? if (ret)
????????? return ret;
? diff --git a/include/bootm.h b/include/bootm.h
index 1e7f29e134..0350c349f3 100644

include/bootm.h +0 -0
@@ -53,7 +53,8 @@ int boot_selected_os(int argc, char *const argv[], int state,
 ? ulong bootm_disable_interrupts(void);

? ? /* This is a special function used by booti/bootz */

include/bootm.h +2 -1
-int bootm_find_images(int flag, int argc, char *const argv[]);
+int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
+????????????? ulong size);

? ? int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, ????????????? char *const argv[], int states, bootm_headers_t *images,

Signature · 1 line
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#11 lore raw

PATCH 4/4 cmd: booti: convert the debug print about image move to printf

In reply to Tero Kristo

Moving of the OS image may have some nasty side effects like corrupting DTB. Convert the current debug print to printf so that the relocation of the OS is always obvious to the user.

---
 cmd/booti.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cmd/booti.c +2 -1
index 6153b229cf..ad04ebd8c3 100644
@@ -79,7 +79,8 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
 
 	/* Handle BOOTM_STATE_LOADOS */
 	if (relocated_addr != ld) {
-		debug("Moving Image from 0x%lx to 0x%lx\n", ld, relocated_addr);
+		printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n", ld,
+		       relocated_addr, relocated_addr + image_size);
 		memmove((void *)relocated_addr, (void *)ld, image_size);
 	}
 
Signature · 4 lines
2.17.1

--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#12 lore raw

PATCH 4/4 cmd: booti: convert the debug print about image move to printf

Quoted 5 lines · Tero Kristo

Moving of the OS image may have some nasty side effects like corrupting DTB. Convert the current debug print to printf so that the relocation of the OS is always obvious to the user.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200717/b174f49d/attachment.sig>