From df19628b2b6665468e698e290dfd1568720ba042 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Thu, 28 Mar 2024 09:40:50 +0300 Subject: elf-load: Respect PT_GNU_STACK If a bootstrap ELF contains a PT_GNU_STACK phdr, take stack protection from there. Otherwise, default to VM_PROT_ALL. --- kern/bootstrap.c | 8 ++++---- kern/elf-load.c | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'kern') diff --git a/kern/bootstrap.c b/kern/bootstrap.c index 49358ac6..0470e1b6 100644 --- a/kern/bootstrap.c +++ b/kern/bootstrap.c @@ -620,10 +620,10 @@ build_args_and_stack(struct exec_info *boot_exec_info, stack_size = round_page(STACK_SIZE); stack_base = user_stack_low(stack_size); - (void) vm_allocate(current_task()->map, - &stack_base, - stack_size, - FALSE); + (void) vm_map(current_map(), &stack_base, stack_size, + 0, FALSE, IP_NULL, 0, FALSE, + boot_exec_info->stack_prot, VM_PROT_ALL, + VM_INHERIT_DEFAULT); arg_pos = (char *) set_user_regs(stack_base, stack_size, boot_exec_info, arg_len); diff --git a/kern/elf-load.c b/kern/elf-load.c index ce86327c..596233a8 100644 --- a/kern/elf-load.c +++ b/kern/elf-load.c @@ -73,6 +73,8 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, if (actual < phsize) return EX_CORRUPT; + out_info->stack_prot = VM_PROT_ALL; + for (i = 0; i < x.e_phnum; i++) { ph = (Elf_Phdr *)((vm_offset_t)phdr + i * x.e_phentsize); @@ -89,6 +91,11 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, ph->p_vaddr + loadbase, ph->p_memsz, type); if (result) return result; + } else if (ph->p_type == PT_GNU_STACK) { + out_info->stack_prot = 0; + if (ph->p_flags & PF_R) out_info->stack_prot |= VM_PROT_READ; + if (ph->p_flags & PF_W) out_info->stack_prot |= VM_PROT_WRITE; + if (ph->p_flags & PF_X) out_info->stack_prot |= VM_PROT_EXECUTE; } } -- cgit v1.2.3