Skip to contentSkip to conversation

spl: spl_legacy: Add extra address checks

PATCH In review Marek Vasut 4 participants 4 messages Jun 19, 2023 – Jun 20, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spl: spl_legacy: Add extra address checks

Check whether the loaded image or entry point does not overlap SPL.

---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Heiko Schocher <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Ye Li <[email protected]>

---
 common/spl/spl_legacy.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
common/spl/spl_legacy.c +20 -0
index 16851c55eb5..b21173baf55 100644
@@ -7,6 +7,7 @@
 #include <image.h>
 #include <log.h>
 #include <malloc.h>
+#include <asm/sections.h>
 #include <spl.h>
 
 #include <lzma/LzmaTypes.h>
@@ -15,6 +16,22 @@
 
 #define LZMA_LEN	(1 << 20)
 
+static void spl_parse_legacy_validate(uintptr_t start, uintptr_t size)
+{
+	uintptr_t spl_start = (uintptr_t)__image_copy_start;
+	uintptr_t spl_end = (uintptr_t)__image_copy_end;
+	uintptr_t end = start + size;
+
+	if ((start >= spl_start && start < spl_end) ||
+	    (end > spl_start && end <= spl_end) ||
+	    (start < spl_start && end >= spl_end) ||
+	    (start > end && end > spl_start))
+		panic("SPL: Image overlaps SPL\n");
+
+	if (size > CONFIG_SYS_BOOTM_LEN)
+		panic("SPL: Image too large\n");
+}
+
 int spl_parse_legacy_header(struct spl_image_info *spl_image,
 			    const struct legacy_img_hdr *header)
 {
@@ -58,6 +75,9 @@ int spl_parse_legacy_header(struct spl_image_info *spl_image,
 	      "payload image: %32s load addr: 0x%lx size: %d\n",
 	      spl_image->name, spl_image->load_addr, spl_image->size);
 
+	spl_parse_legacy_validate(spl_image->load_addr, spl_image->size);
+	spl_parse_legacy_validate(spl_image->entry_point, 0);
+
 	return 0;
 }
 
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#2 lore raw
In reply to Marek Vasut
Quoted 51 lines

Subject: [PATCH] spl: spl_legacy: Add extra address checks

Check whether the loaded image or entry point does not overlap SPL.

---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Heiko Schocher <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Ye Li <[email protected]>

---
 common/spl/spl_legacy.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
common/spl/spl_legacy.c index renamed from common/spl/spl_legacy.c +0 -0

16851c55eb5..b21173baf55 100644

common/spl/spl_legacy.c +17 -0
@@ -7,6 +7,7 @@
 #include <image.h>
 #include <log.h>
 #include <malloc.h>
+#include <asm/sections.h>
 #include <spl.h>
 
 #include <lzma/LzmaTypes.h>
@@ -15,6 +16,22 @@
 
 #define LZMA_LEN	(1 << 20)
 
+static void spl_parse_legacy_validate(uintptr_t start, uintptr_t size)
+{
+	uintptr_t spl_start = (uintptr_t)__image_copy_start;
+	uintptr_t spl_end = (uintptr_t)__image_copy_end;
+	uintptr_t end = start + size;
+
+	if ((start >= spl_start && start < spl_end) ||
+	    (end > spl_start && end <= spl_end) ||
+	    (start < spl_start && end >= spl_end) ||
+	    (start > end && end > spl_start))
+		panic("SPL: Image overlaps SPL\n");
+
+	if (size > CONFIG_SYS_BOOTM_LEN)
+		panic("SPL: Image too large\n");
+}
+

common/spl/spl_legacy.c

Quoted 10 lines
common/spl/spl_legacy.c +4 -0
 int spl_parse_legacy_header(struct spl_image_info *spl_image,
 			    const struct legacy_img_hdr *header)  { @@ -58,6
+75,9 @@ int spl_parse_legacy_header(struct spl_image_info *spl_image,
 	      "payload image: %32s load addr: 0x%lx size: %d\n",
 	      spl_image->name, spl_image->load_addr, spl_image->size);
 
+	spl_parse_legacy_validate(spl_image->load_addr, spl_image->size);
+	spl_parse_legacy_validate(spl_image->entry_point, 0);
+
 	return 0;
Quoted 4 lines

}

--
2.39.2

Reviewed-by on v1 1/1
#3 lore raw
In reply to Marek Vasut

Hello Marek,

Quoted 13 lines · Marek Vasut

Check whether the loaded image or entry point does not overlap SPL.

---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Heiko Schocher <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Ye Li <[email protected]>

Quoted 3 lines
---
 common/spl/spl_legacy.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Good catch!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]