exec: Copy pathname into kernel memory, since the user memory where it resides is going to be freed

This commit is contained in:
apio 2022-10-12 18:04:20 +02:00
parent bcbf43e55c
commit e9df5fd663

View File

@ -4,6 +4,8 @@
#include "errno.h" #include "errno.h"
#include "interrupts/Interrupts.h" #include "interrupts/Interrupts.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "std/stdlib.h"
#include "std/string.h"
#include "sys/elf/ELFLoader.h" #include "sys/elf/ELFLoader.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
@ -31,22 +33,28 @@ void sys_exec(Context* context, const char* pathname)
return; return;
} }
// FIXME: This should be done later, but since there is a very high chance that loading the executed program's ELF char* kpathname = strdup(
// image will overwrite ours, we have to do it here. pathname); // Since we are going to free the original task's memory, we cannot use anything coming from it.
// FIXME: This should be done later, but since there is a very high chance that loading the executed program's
// ELF image will overwrite ours, we have to do it here.
ELFLoader::release_elf_image(Scheduler::current_task()->image); ELFLoader::release_elf_image(Scheduler::current_task()->image);
// FIXME: Check the ELF image is valid before loading it into memory. This will allow us to first check, then free // FIXME: Check the ELF image is valid before loading it into memory. This will allow us to first check, then free
// the previous image, then load, which should reduce the chances of loading failing to almost zero. // the previous image, then load, which should reduce the chances of loading failing to almost zero.
ELFImage* image = ELFLoader::load_elf_from_filesystem(pathname); ELFImage* image = ELFLoader::load_elf_from_filesystem(kpathname);
if (!image) if (!image)
{ {
MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK); MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK);
Scheduler::current_task()->image = nullptr; Scheduler::current_task()->image = nullptr;
kwarnln("exec(): ERROR: Failed to load program. Previous program has already been freed, thus cannot " kfree(kpathname);
kerrorln("exec(): ERROR: Failed to load program. Previous program has already been freed, thus cannot "
"return to it."); "return to it.");
return Scheduler::task_exit(context, -255); return Scheduler::task_exit(context, -255);
} }
kfree(kpathname);
Interrupts::disable(); Interrupts::disable();
ASSERT(!Interrupts::are_enabled()); // This part is pretty sensitive. ASSERT(!Interrupts::are_enabled()); // This part is pretty sensitive.