diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2022-10-01 19:29:59 +0000 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-12-11 19:55:37 -0700 |
commit | ef6921d7c7c66bd8c578f94ac32b32e680b22977 (patch) | |
tree | a1b94aaae87ad4389c0b37cafc3efe7fdaf54f81 /Userland/Libraries/LibELF/Validation.cpp | |
parent | 3275015786ec0e412d465ece9792f6a128fc3879 (diff) | |
download | serenity-ef6921d7c7c66bd8c578f94ac32b32e680b22977.zip |
Kernel+LibC+LibELF: Set stack size based on PT_GNU_STACK during execve
Some programs explicitly ask for a different initial stack size than
what the OS provides. This is implemented in ELF by having a
PT_GNU_STACK header which has its p_memsz set to the amount that the
program requires. This commit implements this policy by reading the
p_memsz of the header and setting the main thread stack size to that.
ELF::Image::validate_program_headers ensures that the size attribute is
a reasonable value.
Diffstat (limited to 'Userland/Libraries/LibELF/Validation.cpp')
-rw-r--r-- | Userland/Libraries/LibELF/Validation.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Libraries/LibELF/Validation.cpp b/Userland/Libraries/LibELF/Validation.cpp index c3d01f8dfe..d334777fdf 100644 --- a/Userland/Libraries/LibELF/Validation.cpp +++ b/Userland/Libraries/LibELF/Validation.cpp @@ -7,6 +7,7 @@ #include <AK/Assertions.h> #include <AK/Checked.h> +#include <Kernel/API/serenity_limits.h> #include <LibC/elf.h> #include <LibELF/Validation.h> #include <limits.h> @@ -298,6 +299,21 @@ ErrorOr<bool> validate_program_headers(ElfW(Ehdr) const& elf_header, size_t file if (verbose) dbgln("Possible shenanigans! Validating an ELF with executable stack."); } + + if (program_header.p_memsz != 0) { + if (program_header.p_memsz < static_cast<unsigned>(PTHREAD_STACK_MIN) || program_header.p_memsz > static_cast<unsigned>(PTHREAD_STACK_MAX)) { + if (verbose) + dbgln("PT_GNU_STACK defines an unacceptable stack size."); + return false; + } + + if (program_header.p_memsz % PAGE_SIZE != 0) { + if (verbose) + dbgln("PT_GNU_STACK size is not page-aligned."); + return false; + } + } + break; case PT_GNU_RELRO: if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) { |