:: commit c77d46986fb9ebd765006e89604673cdd9e4b345

mintsuki <mintsuki@protonmail.com> — 2021-12-05 23:21

parents: ae013123d4

elf: Add guards to prevent non-higher half 64-bit kernels from loading when using PMRs or fully virtual mappings

diff --git a/stage23/lib/elf.c b/stage23/lib/elf.c
index 80863635..38203594 100644
--- a/stage23/lib/elf.c
+++ b/stage23/lib/elf.c
@@ -371,6 +371,10 @@ static void elf64_get_ranges(uint8_t *elf, uint64_t slide, bool use_paddr, struc
         ranges_count++;
     }
 
+    if (ranges_count == 0) {
+        panic("elf: Attempted to use PMRs but no higher half PHDR exists");
+    }
+
     struct elf_range *ranges = ext_mem_alloc(ranges_count * sizeof(struct elf_range));
 
     size_t r = 0;
@@ -466,6 +470,10 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_sl
             }
         }
 
+        if (max_vaddr == 0 || min_vaddr == (uint64_t)-1) {
+            panic("elf: Attempted to use fully virtual mappings but no higher half PHDR exists");
+        }
+
         image_size = max_vaddr - min_vaddr;
 
         *physical_base = (uintptr_t)ext_mem_alloc_type_aligned(image_size, alloc_type, max_align);
tab: 248 wrap: offon