From e37ff67da2d733f9276651817d9287ab032f591c Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 19:22:08 +0200 Subject: [PATCH] Make exec return an error if the loaded executable would use more memory than is currently available --- kernel/src/sys/exec.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index 5c242fbb..8d561dea 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -4,6 +4,7 @@ #include "errno.h" #include "interrupts/Interrupts.h" #include "memory/MemoryManager.h" +#include "memory/PMM.h" #include "std/stdlib.h" #include "std/string.h" #include "sys/elf/ELFLoader.h" @@ -32,7 +33,8 @@ void sys_exec(Context* context, const char* pathname) return; } - if (ELFLoader::check_elf_image(program) < 0) + long memusage; + if ((memusage = ELFLoader::check_elf_image(program)) < 0) { context->rax = -ENOEXEC; return; @@ -45,6 +47,13 @@ void sys_exec(Context* context, const char* pathname) return; } + if (memusage > PMM::get_free()) + { + MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK); + context->rax = -ENOMEM; + return; + } + Interrupts::disable(); ASSERT(!Interrupts::are_enabled()); // This part is pretty sensitive.