From 03ae9fdb0a5257767099566fb7bae74abd3f4ad8 Mon Sep 17 00:00:00 2001 From: "Liav A." Date: Sat, 20 Jul 2024 05:00:08 +0300 Subject: [PATCH] Kernel: Check condition earlier for ELF file type It makes no sense to do all of the loading work just to figure out that the ELF file is an object file that is a result of compiling and not an actual executable. In addition to that, we should disallow running coredumps as well, so the condition is changed now to only allow ET_DYN or ET_EXEC ELF files. --- Kernel/Syscalls/execve.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 188d70b0815..3a1f67fd548 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -739,6 +739,13 @@ static ErrorOr>> find_shebang_interpreter_for_exec ErrorOr> Process::find_elf_interpreter_for_executable(StringView path, Elf_Ehdr const& main_executable_header, size_t main_executable_header_size, size_t file_size, Optional& minimum_stack_size) { + // NOTE: We can't exec an ET_REL, as that's just an object file from the compiler, + // and we can't exec an ET_CORE as it's just a coredump. + // The only allowed ELF files on execve are executables or shared object files + // which are dynamically linked programs (or static-pie programs like the dynamic loader). + if (main_executable_header.e_type != ET_EXEC && main_executable_header.e_type != ET_DYN) + return ENOEXEC; + // Not using ErrorOr here because we'll want to do the same thing in userspace in the RTLD StringBuilder interpreter_path_builder; Optional main_executable_requested_stack_size {}; @@ -794,10 +801,6 @@ ErrorOr> Process::find_elf_interpreter_for_executabl return interpreter_description; } - if (main_executable_header.e_type == ET_REL) { - // We can't exec an ET_REL, that's just an object file from the compiler - return ENOEXEC; - } if (main_executable_header.e_type == ET_DYN) { // If it's ET_DYN with no PT_INTERP, then it's a dynamic executable responsible // for its own relocation (i.e. it's /usr/lib/Loader.so)