1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:47:45 +00:00

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.
This commit is contained in:
sin-ack 2022-10-01 19:29:59 +00:00 committed by Andrew Kaster
parent 3275015786
commit ef6921d7c7
9 changed files with 43 additions and 7 deletions

View file

@ -487,7 +487,7 @@ typedef struct {
#define PT_GNU_EH_FRAME 0x6474e550 /* Exception handling info */
#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
#define PT_GNU_STACK 0x6474e551 /* Stack permissions info */
#define PT_GNU_STACK 0x6474e551 /* Stack permissions & size info */
#define PT_OPENBSD_RANDOMIZE 0x65a3dbe6 /* fill with random data */
#define PT_OPENBSD_WXNEEDED 0x65a3dbe7 /* program performs W^X violations */

View file

@ -7,6 +7,7 @@
#pragma once
#include <Kernel/API/POSIX/sys/limits.h>
#include <Kernel/API/serenity_limits.h>
#include <bits/posix1_lim.h>
#include <bits/stdint.h>
#include <bits/wchar.h>
@ -86,8 +87,6 @@
#define ARG_MAX 65536
#define PTHREAD_STACK_MIN 65536
#define SSIZE_MAX 2147483647
#define LINK_MAX 4096

View file

@ -34,7 +34,6 @@ using PthreadAttrImpl = Syscall::SC_create_thread_params;
static constexpr size_t required_stack_alignment = 4 * MiB;
static constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
static constexpr size_t highest_reasonable_stack_size = 8 * MiB; // That's the default in Ubuntu?
__thread void* s_stack_location;
__thread size_t s_stack_size;
@ -467,7 +466,7 @@ int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
if (!attributes_impl)
return EINVAL;
if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
if (stack_size < PTHREAD_STACK_MIN || stack_size > PTHREAD_STACK_MAX)
return EINVAL;
attributes_impl->stack_size = stack_size;

View file

@ -12,6 +12,7 @@
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <Kernel/API/serenity_limits.h>
#include <LibELF/Image.h>
#include <LibELF/Validation.h>
#include <limits.h>

View file

@ -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)) {