From 5b427808df78d6398eb60e1a93cbc81098e01fad Mon Sep 17 00:00:00 2001 From: nakst <> Date: Fri, 17 Dec 2021 09:24:59 +0000 Subject: [PATCH] additional logging for elf load errors --- kernel/elf.cpp | 67 +++++++++++++++++++++++++++++++++++++------- kernel/scheduler.cpp | 9 ++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/kernel/elf.cpp b/kernel/elf.cpp index 90c704b..cd22246 100644 --- a/kernel/elf.cpp +++ b/kernel/elf.cpp @@ -130,7 +130,11 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { { BundleHeader header; size_t bytesRead = FSFileReadSync(node, (uint8_t *) &header, 0, sizeof(BundleHeader), 0); - if (bytesRead != sizeof(BundleHeader)) return ES_ERROR_UNSUPPORTED_EXECUTABLE; + + if (bytesRead != sizeof(BundleHeader)) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not read the bundle header.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } if (header.signature == BUNDLE_SIGNATURE && header.fileCount < 0x100000 @@ -144,10 +148,12 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { } if (ArchCheckBundleHeader() || header.mapAddress & (K_PAGE_SIZE - 1)) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Invalid bundle mapping addresses.\n"); return ES_ERROR_UNSUPPORTED_EXECUTABLE; } if (header.version != 1) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Invalid bundle version.\n"); return ES_ERROR_UNSUPPORTED_EXECUTABLE; } @@ -156,6 +162,7 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { if (!MMMapFile(thisProcess->vmm, (FSFile *) node, 0, fileSize, ES_MEMORY_MAP_OBJECT_READ_ONLY, (uint8_t *) header.mapAddress)) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not map the bundle file.\n"); return ES_ERROR_INSUFFICIENT_RESOURCES; } @@ -175,6 +182,7 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { } if (executableOffset >= fileSize || !found) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not find the executable section for the current architecture.\n"); return ES_ERROR_UNSUPPORTED_EXECUTABLE; } @@ -186,29 +194,66 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { ElfHeader header; size_t bytesRead = FSFileReadSync(node, (uint8_t *) &header, executableOffset, sizeof(ElfHeader), 0); - if (bytesRead != sizeof(ElfHeader)) return ES_ERROR_UNSUPPORTED_EXECUTABLE; + + if (bytesRead != sizeof(ElfHeader)) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not read the ELF header.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } size_t programHeaderEntrySize = header.programHeaderEntrySize; - if (header.magicNumber != 0x464C457F) return ES_ERROR_UNSUPPORTED_EXECUTABLE; - if (header.bits != 2) return ES_ERROR_UNSUPPORTED_EXECUTABLE; - if (header.endianness != 1) return ES_ERROR_UNSUPPORTED_EXECUTABLE; - if (header.abi != 0) return ES_ERROR_UNSUPPORTED_EXECUTABLE; - if (header.type != 2) return ES_ERROR_UNSUPPORTED_EXECUTABLE; - if (header.instructionSet != 0x3E) return ES_ERROR_UNSUPPORTED_EXECUTABLE; + if (header.magicNumber != 0x464C457F) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable magic number.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } + + if (header.bits != 2) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable bits.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } + + if (header.endianness != 1) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable endianness.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } + + if (header.abi != 0) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable ABI.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } + + if (header.type != 2) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable type.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } + + if (header.instructionSet != 0x3E) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Incorrect executable instruction set.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } ElfProgramHeader *programHeaders = (ElfProgramHeader *) EsHeapAllocate(programHeaderEntrySize * header.programHeaderEntries, false, K_PAGED); - if (!programHeaders) return ES_ERROR_INSUFFICIENT_RESOURCES; + + if (!programHeaders) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not allocate the program headers.\n"); + return ES_ERROR_INSUFFICIENT_RESOURCES; + } + EsDefer(EsHeapFree(programHeaders, 0, K_PAGED)); bytesRead = FSFileReadSync(node, (uint8_t *) programHeaders, executableOffset + header.programHeaderTable, programHeaderEntrySize * header.programHeaderEntries, 0); - if (bytesRead != programHeaderEntrySize * header.programHeaderEntries) return ES_ERROR_UNSUPPORTED_EXECUTABLE; + + if (bytesRead != programHeaderEntrySize * header.programHeaderEntries) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not read the program headers.\n"); + return ES_ERROR_UNSUPPORTED_EXECUTABLE; + } for (uintptr_t i = 0; i < header.programHeaderEntries; i++) { ElfProgramHeader *header = (ElfProgramHeader *) ((uint8_t *) programHeaders + programHeaderEntrySize * i); if (header->type == 1 /* PT_LOAD */) { if (ArchCheckELFHeader()) { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Rejected ELF program header.\n"); return ES_ERROR_UNSUPPORTED_EXECUTABLE; } @@ -245,6 +290,8 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) { if (success) { uint8_t *from = (uint8_t *) header->virtualAddress + header->dataInFile; EsMemoryZero(from, (uint8_t *) zeroStart - from); + } else { + KernelLog(LOG_ERROR, "ELF", "executable load error", "Could not memory map program header %d.\n", i); } #endif diff --git a/kernel/scheduler.cpp b/kernel/scheduler.cpp index d4b26ec..a5cce61 100644 --- a/kernel/scheduler.cpp +++ b/kernel/scheduler.cpp @@ -730,6 +730,8 @@ void ProcessLoadExecutable() { } CloseHandleToObject(node.node, KERNEL_OBJECT_NODE, fileFlags); + } else { + KernelLog(LOG_ERROR, "Scheduler", "desktop executable open error", "The Desktop executable could not be opened.\n"); } } @@ -737,6 +739,10 @@ void ProcessLoadExecutable() { if (thisProcess->type != PROCESS_DESKTOP && loadError == ES_SUCCESS) { loadError = KLoadELF(thisProcess->executableNode, &application); + + if (loadError != ES_SUCCESS) { + KernelLog(LOG_ERROR, "Scheduler", "executable load error", "The executable could not be loaded by the ELF loader.\n"); + } } bool success = loadError == ES_SUCCESS; @@ -747,6 +753,7 @@ void ProcessLoadExecutable() { if (!MMMapShared(thisProcess->vmm, mmAPITableRegion, 0, mmAPITableRegion->sizeBytes, ES_FLAGS_DEFAULT, ES_API_BASE)) { success = false; + KernelLog(LOG_ERROR, "Scheduler", "executable load error", "The API table could not be mapped.\n"); } } @@ -758,6 +765,7 @@ void ProcessLoadExecutable() { if (!startupInformation) { success = false; + KernelLog(LOG_ERROR, "Scheduler", "executable load error", "The process startup information could not be allocated.\n"); } else { startupInformation->isDesktop = thisProcess->type == PROCESS_DESKTOP; startupInformation->isBundle = application.isBundle; @@ -787,6 +795,7 @@ void ProcessLoadExecutable() { if (!thisProcess->executableMainThread) { success = false; + KernelLog(LOG_ERROR, "Scheduler", "executable load error", "The main thread could not be spawned.\n"); } }