about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-01-08 17:52:49 +0100
committerptitSeb <sebastien.chev@gmail.com>2024-01-08 17:52:49 +0100
commit5b700cb6e6f397d2074c49659f7f9915f4a33c5f (patch)
treef41de7c45c3285d632734002c24499cccd54b663 /src
parentdcb31fa783b42b32dc98612ab95e1aba98c47e22 (diff)
downloadbox64-5b700cb6e6f397d2074c49659f7f9915f4a33c5f.tar.gz
box64-5b700cb6e6f397d2074c49659f7f9915f4a33c5f.zip
[ELFLOADER] Try to harden the memory allocation
Diffstat (limited to 'src')
-rw-r--r--src/elfs/elfloader.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index 72c2b4f6..c86f28ae 100644
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -208,10 +208,20 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
         munmap(image, head->memsz);
         image = mmap64(find47bitBlockElf(head->memsz, mainbin, max_align), head->memsz, 0, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
     }
+    if(image!=MAP_FAILED && !head->vaddr && image!=(void*)offs && (uintptr_t)image&max_align) {
+        munmap(image, head->memsz);
+        loadProtectionFromMap();
+        offs = (uintptr_t)find47bitBlockElf(head->memsz, mainbin, max_align);
+        image = mmap64((void*)(head->vaddr?head->vaddr:offs), head->memsz, 0, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
+    }
     #endif
     if(image!=MAP_FAILED && !head->vaddr && image!=(void*)offs) {
-        printf_log(LOG_INFO, "Mmap64 for (@%p 0x%zx) for elf \"%s\" returned %p instead\n", (void*)(head->vaddr?head->vaddr:offs), head->memsz, head->name, image);
+        printf_log(LOG_INFO, "%s: Mmap64 for (@%p 0x%zx) for elf \"%s\" returned %p instead\n", ((uintptr_t)image&max_align)?"Error":"Warning", (void*)(head->vaddr?head->vaddr:offs), head->memsz, head->name, image);
         offs = (uintptr_t)image;
+        if((uintptr_t)image&max_align) {
+            munmap(image, head->memsz);
+            return 1;   // that's an error, alocated memory is not aligned properly
+        }
     }
     if(image==MAP_FAILED || image!=(void*)(head->vaddr?head->vaddr:offs)) {
         printf_log(LOG_NONE, "%s cannot create memory map (@%p 0x%zx) for elf \"%s\"", (image==MAP_FAILED)?"Error:":"Warning:", (void*)(head->vaddr?head->vaddr:offs), head->memsz, head->name);