123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <AK/LexicalPath.h>
- #include <AK/ScopeGuard.h>
- #include <AK/TemporaryChange.h>
- #include <Kernel/FileSystem/Custody.h>
- #include <Kernel/FileSystem/FileDescription.h>
- #include <Kernel/Process.h>
- #include <Kernel/Profiling.h>
- #include <Kernel/Random.h>
- #include <Kernel/Time/TimeManagement.h>
- #include <Kernel/VM/AllocationStrategy.h>
- #include <Kernel/VM/MemoryManager.h>
- #include <Kernel/VM/PageDirectory.h>
- #include <Kernel/VM/Region.h>
- #include <Kernel/VM/SharedInodeVMObject.h>
- #include <LibC/limits.h>
- #include <LibELF/AuxiliaryVector.h>
- #include <LibELF/Image.h>
- #include <LibELF/Validation.h>
- //#define EXEC_DEBUG
- namespace Kernel {
- static Vector<ELF::AuxiliaryValue> generate_auxiliary_vector(FlatPtr load_base, FlatPtr entry_eip, uid_t uid, uid_t euid, gid_t gid, gid_t egid, String executable_path, int main_program_fd);
- static bool validate_stack_size(const Vector<String>& arguments, const Vector<String>& environment)
- {
- size_t total_blob_size = 0;
- for (auto& a : arguments)
- total_blob_size += a.length() + 1;
- for (auto& e : environment)
- total_blob_size += e.length() + 1;
- size_t total_meta_size = sizeof(char*) * (arguments.size() + 1) + sizeof(char*) * (environment.size() + 1);
- // FIXME: This doesn't account for the size of the auxiliary vector
- return (total_blob_size + total_meta_size) < Thread::default_userspace_stack_size;
- }
- static KResultOr<FlatPtr> make_userspace_stack_for_main_thread(Region& region, Vector<String> arguments, Vector<String> environment, Vector<ELF::AuxiliaryValue> auxiliary_values)
- {
- FlatPtr new_esp = region.vaddr().offset(Thread::default_userspace_stack_size).get();
- auto push_on_new_stack = [&new_esp](u32 value) {
- new_esp -= 4;
- Userspace<u32*> stack_ptr = new_esp;
- return copy_to_user(stack_ptr, &value);
- };
- auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
- new_esp -= sizeof(auxv_t);
- Userspace<auxv_t*> stack_ptr = new_esp;
- return copy_to_user(stack_ptr, &value);
- };
- auto push_string_on_new_stack = [&new_esp](const String& string) {
- new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
- Userspace<u32*> stack_ptr = new_esp;
- return copy_to_user(stack_ptr, string.characters(), string.length() + 1);
- };
- Vector<FlatPtr> argv_entries;
- for (auto& argument : arguments) {
- push_string_on_new_stack(argument);
- argv_entries.append(new_esp);
- }
- Vector<FlatPtr> env_entries;
- for (auto& variable : environment) {
- push_string_on_new_stack(variable);
- env_entries.append(new_esp);
- }
- for (auto& value : auxiliary_values) {
- if (!value.optional_string.is_empty()) {
- push_string_on_new_stack(value.optional_string);
- value.auxv.a_un.a_ptr = (void*)new_esp;
- }
- }
- for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
- auto& value = auxiliary_values[i];
- push_aux_value_on_new_stack(value.auxv);
- }
- push_on_new_stack(0);
- for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
- push_on_new_stack(env_entries[i]);
- FlatPtr envp = new_esp;
- push_on_new_stack(0);
- for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
- push_on_new_stack(argv_entries[i]);
- FlatPtr argv = new_esp;
- // NOTE: The stack needs to be 16-byte aligned.
- new_esp -= new_esp % 16;
- push_on_new_stack((FlatPtr)envp);
- push_on_new_stack((FlatPtr)argv);
- push_on_new_stack((FlatPtr)argv_entries.size());
- push_on_new_stack(0);
- return new_esp;
- }
- KResultOr<Process::LoadResult> Process::load_elf_object(FileDescription& object_description, FlatPtr load_offset, ShouldAllocateTls should_allocate_tls)
- {
- auto& inode = *(object_description.inode());
- auto vmobject = SharedInodeVMObject::create_with_inode(inode);
- if (vmobject->writable_mappings()) {
- dbgln("Refusing to execute a write-mapped program");
- return KResult(-ETXTBSY);
- }
- size_t executable_size = inode.size();
- auto region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(executable_size), "ELF loading", Region::Access::Read);
- if (!region) {
- dbgln("Could not allocate memory for ELF loading");
- return KResult(-ENOMEM);
- }
- auto elf_image = ELF::Image(region->vaddr().as_ptr(), executable_size);
- if (!elf_image.is_valid())
- return KResult(-ENOEXEC);
- Region* master_tls_region { nullptr };
- size_t master_tls_size = 0;
- size_t master_tls_alignment = 0;
- FlatPtr load_base_address = 0;
- String elf_name = object_description.absolute_path();
- ASSERT(!Processor::current().in_critical());
- KResult ph_load_result = KSuccess;
- elf_image.for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
- if (program_header.type() == PT_TLS) {
- ASSERT(should_allocate_tls == ShouldAllocateTls::Yes);
- ASSERT(program_header.size_in_memory());
- if (!elf_image.is_within_image(program_header.raw_data(), program_header.size_in_image())) {
- dbgln("Shenanigans! ELF PT_TLS header sneaks outside of executable.");
- ph_load_result = KResult(-ENOEXEC);
- return IterationDecision::Break;
- }
- master_tls_region = allocate_region({}, program_header.size_in_memory(), String::formatted("{} (master-tls)", elf_name), PROT_READ | PROT_WRITE, AllocationStrategy::Reserve);
- if (!master_tls_region) {
- ph_load_result = KResult(-ENOMEM);
- return IterationDecision::Break;
- }
- master_tls_size = program_header.size_in_memory();
- master_tls_alignment = program_header.alignment();
- if (!copy_to_user(master_tls_region->vaddr().as_ptr(), program_header.raw_data(), program_header.size_in_image())) {
- ph_load_result = KResult(-EFAULT);
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- }
- if (program_header.type() != PT_LOAD)
- return IterationDecision::Continue;
- if (program_header.is_writable()) {
- // Writable section: create a copy in memory.
- ASSERT(program_header.size_in_memory());
- ASSERT(program_header.alignment() == PAGE_SIZE);
- if (!elf_image.is_within_image(program_header.raw_data(), program_header.size_in_image())) {
- dbgln("Shenanigans! Writable ELF PT_LOAD header sneaks outside of executable.");
- ph_load_result = KResult(-ENOEXEC);
- return IterationDecision::Break;
- }
- int prot = 0;
- if (program_header.is_readable())
- prot |= PROT_READ;
- if (program_header.is_writable())
- prot |= PROT_WRITE;
- auto region_name = String::formatted("{} (data-{}{})", elf_name, program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : "");
- auto* region = allocate_region(program_header.vaddr().offset(load_offset), program_header.size_in_memory(), move(region_name), prot, AllocationStrategy::Reserve);
- if (!region) {
- ph_load_result = KResult(-ENOMEM);
- return IterationDecision::Break;
- }
- // It's not always the case with PIE executables (and very well shouldn't be) that the
- // virtual address in the program header matches the one we end up giving the process.
- // In order to copy the data image correctly into memory, we need to copy the data starting at
- // the right initial page offset into the pages allocated for the elf_alloc-XX section.
- // FIXME: There's an opportunity to munmap, or at least mprotect, the padding space between
- // the .text and .data PT_LOAD sections of the executable.
- // Accessing it would definitely be a bug.
- auto page_offset = program_header.vaddr();
- page_offset.mask(~PAGE_MASK);
- if (!copy_to_user((u8*)region->vaddr().as_ptr() + page_offset.get(), program_header.raw_data(), program_header.size_in_image())) {
- ph_load_result = KResult(-EFAULT);
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- }
- // Non-writable section: map the executable itself in memory.
- ASSERT(program_header.size_in_memory());
- ASSERT(program_header.alignment() == PAGE_SIZE);
- int prot = 0;
- if (program_header.is_readable())
- prot |= PROT_READ;
- if (program_header.is_writable())
- prot |= PROT_WRITE;
- if (program_header.is_executable())
- prot |= PROT_EXEC;
- auto* region = allocate_region_with_vmobject(program_header.vaddr().offset(load_offset), program_header.size_in_memory(), *vmobject, program_header.offset(), elf_name, prot, true);
- if (!region) {
- ph_load_result = KResult(-ENOMEM);
- return IterationDecision::Break;
- }
- if (program_header.offset() == 0)
- load_base_address = (FlatPtr)region->vaddr().as_ptr();
- return IterationDecision::Continue;
- });
- if (ph_load_result.is_error()) {
- dbgln("do_exec: Failure loading program ({})", ph_load_result.error());
- return ph_load_result;
- }
- if (!elf_image.entry().offset(load_offset).get()) {
- dbgln("do_exec: Failure loading program, entry pointer is invalid! {})", elf_image.entry().offset(load_offset));
- return KResult(-ENOEXEC);
- }
- auto* stack_region = allocate_region(VirtualAddress(), Thread::default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, AllocationStrategy::Reserve);
- if (!stack_region)
- return KResult(-ENOMEM);
- stack_region->set_stack(true);
- return LoadResult {
- load_base_address,
- elf_image.entry().offset(load_offset).get(),
- executable_size,
- VirtualAddress(elf_image.program_header_table_offset()).offset(load_offset).get(),
- elf_image.program_header_count(),
- master_tls_region ? master_tls_region->make_weak_ptr() : nullptr,
- master_tls_size,
- master_tls_alignment,
- stack_region->make_weak_ptr()
- };
- }
- KResultOr<Process::LoadResult> Process::load(NonnullRefPtr<FileDescription> main_program_description, RefPtr<FileDescription> interpreter_description)
- {
- RefPtr<PageDirectory> old_page_directory;
- NonnullOwnPtrVector<Region> old_regions;
- {
- auto page_directory = PageDirectory::create_for_userspace(*this);
- if (!page_directory)
- return KResult(-ENOMEM);
- // Need to make sure we don't swap contexts in the middle
- ScopedCritical critical;
- old_page_directory = move(m_page_directory);
- old_regions = move(m_regions);
- m_page_directory = page_directory.release_nonnull();
- MM.enter_process_paging_scope(*this);
- }
- ArmedScopeGuard rollback_regions_guard([&]() {
- ASSERT(Process::current() == this);
- // Need to make sure we don't swap contexts in the middle
- ScopedCritical critical;
- // Explicitly clear m_regions *before* restoring the page directory,
- // otherwise we may silently corrupt memory!
- m_regions.clear();
- // Now that we freed the regions, revert to the original page directory
- // and restore the original regions
- m_page_directory = move(old_page_directory);
- MM.enter_process_paging_scope(*this);
- m_regions = move(old_regions);
- });
- if (!interpreter_description) {
- auto result = load_elf_object(main_program_description, FlatPtr { 0 }, ShouldAllocateTls::Yes);
- if (result.is_error())
- return result.error();
- rollback_regions_guard.disarm();
- return result;
- }
- // TODO: I'm sure this can be randomized even better. :^)
- FlatPtr random_offset = get_good_random<u16>() * PAGE_SIZE;
- FlatPtr interpreter_load_offset = 0x08000000 + random_offset;
- auto interpreter_load_result = load_elf_object(*interpreter_description, interpreter_load_offset, ShouldAllocateTls::No);
- if (interpreter_load_result.is_error())
- return interpreter_load_result.error();
- // TLS allocation will be done in userspace by the loader
- ASSERT(!interpreter_load_result.value().tls_region);
- ASSERT(!interpreter_load_result.value().tls_alignment);
- ASSERT(!interpreter_load_result.value().tls_size);
- rollback_regions_guard.disarm();
- return interpreter_load_result;
- }
- int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags)
- {
- ASSERT(is_user_process());
- ASSERT(!Processor::current().in_critical());
- auto path = main_program_description->absolute_path();
- #ifdef EXEC_DEBUG
- dbgln("do_exec({})", path);
- #endif
- // FIXME: How much stack space does process startup need?
- if (!validate_stack_size(arguments, environment))
- return -E2BIG;
- auto parts = path.split('/');
- if (parts.is_empty())
- return -ENOENT;
- // Disable profiling temporarily in case it's running on this process.
- bool was_profiling = is_profiling();
- TemporaryChange profiling_disabler(m_profiling, false);
- // Mark this thread as the current thread that does exec
- // No other thread from this process will be scheduled to run
- auto current_thread = Thread::current();
- m_exec_tid = current_thread->tid();
- // NOTE: We switch credentials before altering the memory layout of the process.
- // This ensures that ptrace access control takes the right credentials into account.
- // FIXME: This still feels rickety. Perhaps it would be better to simply block ptrace
- // clients until we're ready to be traced? Or reject them with EPERM?
- auto main_program_metadata = main_program_description->metadata();
- auto old_euid = m_euid;
- auto old_suid = m_suid;
- auto old_egid = m_egid;
- auto old_sgid = m_sgid;
- ArmedScopeGuard cred_restore_guard = [&] {
- m_euid = old_euid;
- m_suid = old_suid;
- m_egid = old_egid;
- m_sgid = old_sgid;
- };
- bool executable_is_setid = false;
- if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
- if (main_program_metadata.is_setuid()) {
- executable_is_setid = true;
- m_euid = m_suid = main_program_metadata.uid;
- }
- if (main_program_metadata.is_setgid()) {
- executable_is_setid = true;
- m_egid = m_sgid = main_program_metadata.gid;
- }
- }
- auto load_result_or_error = load(main_program_description, interpreter_description);
- if (load_result_or_error.is_error()) {
- dbgln("do_exec({}): Failed to load main program or interpreter", path);
- return load_result_or_error.error();
- }
- auto& load_result = load_result_or_error.value();
- // We can commit to the new credentials at this point.
- cred_restore_guard.disarm();
- kill_threads_except_self();
- #ifdef EXEC_DEBUG
- dbgln("Memory layout after ELF load:");
- dump_regions();
- #endif
- m_executable = main_program_description->custody();
- m_promises = m_execpromises;
- m_veil_state = VeilState::None;
- m_unveiled_paths.clear();
- current_thread->set_default_signal_dispositions();
- current_thread->clear_signals();
- m_futex_queues.clear();
- m_region_lookup_cache = {};
- disown_all_shared_buffers();
- set_dumpable(!executable_is_setid);
- for (size_t i = 0; i < m_fds.size(); ++i) {
- auto& description_and_flags = m_fds[i];
- if (description_and_flags.description() && description_and_flags.flags() & FD_CLOEXEC)
- description_and_flags = {};
- }
- int main_program_fd = -1;
- if (interpreter_description) {
- main_program_fd = alloc_fd();
- ASSERT(main_program_fd >= 0);
- main_program_description->seek(0, SEEK_SET);
- main_program_description->set_readable(true);
- m_fds[main_program_fd].set(move(main_program_description), FD_CLOEXEC);
- }
- new_main_thread = nullptr;
- if (¤t_thread->process() == this) {
- new_main_thread = current_thread;
- } else {
- for_each_thread([&](auto& thread) {
- new_main_thread = &thread;
- return IterationDecision::Break;
- });
- }
- ASSERT(new_main_thread);
- auto auxv = generate_auxiliary_vector(load_result.load_base, load_result.entry_eip, m_uid, m_euid, m_gid, m_egid, path, main_program_fd);
- // NOTE: We create the new stack before disabling interrupts since it will zero-fault
- // and we don't want to deal with faults after this point.
- auto make_stack_result = make_userspace_stack_for_main_thread(*load_result.stack_region.unsafe_ptr(), move(arguments), move(environment), move(auxv));
- if (make_stack_result.is_error())
- return make_stack_result.error();
- u32 new_userspace_esp = make_stack_result.value();
- if (wait_for_tracer_at_next_execve())
- Thread::current()->send_urgent_signal_to_self(SIGSTOP);
- // We enter a critical section here because we don't want to get interrupted between do_exec()
- // and Processor::assume_context() or the next context switch.
- // If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
- Processor::current().enter_critical(prev_flags);
- // NOTE: Be careful to not trigger any page faults below!
- m_name = parts.take_last();
- new_main_thread->set_name(m_name);
- // FIXME: PID/TID ISSUE
- m_pid = new_main_thread->tid().value();
- auto tsr_result = new_main_thread->make_thread_specific_region({});
- if (tsr_result.is_error())
- return tsr_result.error();
- new_main_thread->reset_fpu_state();
- auto& tss = new_main_thread->m_tss;
- tss.cs = GDT_SELECTOR_CODE3 | 3;
- tss.ds = GDT_SELECTOR_DATA3 | 3;
- tss.es = GDT_SELECTOR_DATA3 | 3;
- tss.ss = GDT_SELECTOR_DATA3 | 3;
- tss.fs = GDT_SELECTOR_DATA3 | 3;
- tss.gs = GDT_SELECTOR_TLS | 3;
- tss.eip = load_result.entry_eip;
- tss.esp = new_userspace_esp;
- tss.cr3 = m_page_directory->cr3();
- tss.ss2 = m_pid.value();
- if (was_profiling)
- Profiling::did_exec(path);
- {
- ScopedSpinLock lock(g_scheduler_lock);
- new_main_thread->set_state(Thread::State::Runnable);
- }
- u32 lock_count_to_restore;
- [[maybe_unused]] auto rc = big_lock().force_unlock_if_locked(lock_count_to_restore);
- ASSERT_INTERRUPTS_DISABLED();
- ASSERT(Processor::current().in_critical());
- return 0;
- }
- static Vector<ELF::AuxiliaryValue> generate_auxiliary_vector(FlatPtr load_base, FlatPtr entry_eip, uid_t uid, uid_t euid, gid_t gid, gid_t egid, String executable_path, int main_program_fd)
- {
- Vector<ELF::AuxiliaryValue> auxv;
- // PHDR/EXECFD
- // PH*
- auxv.append({ ELF::AuxiliaryValue::PageSize, PAGE_SIZE });
- auxv.append({ ELF::AuxiliaryValue::BaseAddress, (void*)load_base });
- auxv.append({ ELF::AuxiliaryValue::Entry, (void*)entry_eip });
- // NOTELF
- auxv.append({ ELF::AuxiliaryValue::Uid, (long)uid });
- auxv.append({ ELF::AuxiliaryValue::EUid, (long)euid });
- auxv.append({ ELF::AuxiliaryValue::Gid, (long)gid });
- auxv.append({ ELF::AuxiliaryValue::EGid, (long)egid });
- // FIXME: Don't hard code this? We might support other platforms later.. (e.g. x86_64)
- auxv.append({ ELF::AuxiliaryValue::Platform, "i386" });
- // FIXME: This is platform specific
- auxv.append({ ELF::AuxiliaryValue::HwCap, (long)CPUID(1).edx() });
- auxv.append({ ELF::AuxiliaryValue::ClockTick, (long)TimeManagement::the().ticks_per_second() });
- // FIXME: Also take into account things like extended filesystem permissions? That's what linux does...
- auxv.append({ ELF::AuxiliaryValue::Secure, ((uid != euid) || (gid != egid)) ? 1 : 0 });
- char random_bytes[16] {};
- get_fast_random_bytes((u8*)random_bytes, sizeof(random_bytes));
- auxv.append({ ELF::AuxiliaryValue::Random, String(random_bytes, sizeof(random_bytes)) });
- auxv.append({ ELF::AuxiliaryValue::ExecFilename, executable_path });
- auxv.append({ ELF::AuxiliaryValue::ExecFileDescriptor, main_program_fd });
- auxv.append({ ELF::AuxiliaryValue::Null, 0L });
- return auxv;
- }
- static KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const char first_page[], int nread)
- {
- int word_start = 2;
- int word_length = 0;
- if (nread > 2 && first_page[0] == '#' && first_page[1] == '!') {
- Vector<String> interpreter_words;
- for (int i = 2; i < nread; ++i) {
- if (first_page[i] == '\n') {
- break;
- }
- if (first_page[i] != ' ') {
- ++word_length;
- }
- if (first_page[i] == ' ') {
- if (word_length > 0) {
- interpreter_words.append(String(&first_page[word_start], word_length));
- }
- word_length = 0;
- word_start = i + 1;
- }
- }
- if (word_length > 0)
- interpreter_words.append(String(&first_page[word_start], word_length));
- if (!interpreter_words.is_empty())
- return interpreter_words;
- }
- return KResult(-ENOEXEC);
- }
- KResultOr<NonnullRefPtr<FileDescription>> Process::find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size)
- {
- if (nread < (int)sizeof(Elf32_Ehdr))
- return KResult(-ENOEXEC);
- auto elf_header = (Elf32_Ehdr*)first_page;
- if (!ELF::validate_elf_header(*elf_header, file_size)) {
- dbgln("exec({}): File has invalid ELF header", path);
- return KResult(-ENOEXEC);
- }
- // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
- String interpreter_path;
- if (!ELF::validate_program_headers(*elf_header, file_size, (u8*)first_page, nread, &interpreter_path)) {
- dbgln("exec({}): File has invalid ELF Program headers", path);
- return KResult(-ENOEXEC);
- }
- if (!interpreter_path.is_empty()) {
- #ifdef EXEC_DEBUG
- dbgln("exec({}): Using program interpreter {}", path, interpreter_path);
- #endif
- auto interp_result = VFS::the().open(interpreter_path, O_EXEC, 0, current_directory());
- if (interp_result.is_error()) {
- dbgln("exec({}): Unable to open program interpreter {}", path, interpreter_path);
- return interp_result.error();
- }
- auto interpreter_description = interp_result.value();
- auto interp_metadata = interpreter_description->metadata();
- ASSERT(interpreter_description->inode());
- // Validate the program interpreter as a valid elf binary.
- // If your program interpreter is a #! file or something, it's time to stop playing games :)
- if (interp_metadata.size < (int)sizeof(Elf32_Ehdr))
- return KResult(-ENOEXEC);
- memset(first_page, 0, sizeof(first_page));
- auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
- auto nread_or_error = interpreter_description->read(first_page_buffer, sizeof(first_page));
- if (nread_or_error.is_error())
- return KResult(-ENOEXEC);
- nread = nread_or_error.value();
- if (nread < (int)sizeof(Elf32_Ehdr))
- return KResult(-ENOEXEC);
- elf_header = (Elf32_Ehdr*)first_page;
- if (!ELF::validate_elf_header(*elf_header, interp_metadata.size)) {
- dbgln("exec({}): Interpreter ({}) has invalid ELF header", path, interpreter_description->absolute_path());
- return KResult(-ENOEXEC);
- }
- // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
- String interpreter_interpreter_path;
- if (!ELF::validate_program_headers(*elf_header, interp_metadata.size, (u8*)first_page, nread, &interpreter_interpreter_path)) {
- dbgln("exec({}): Interpreter ({}) has invalid ELF Program headers", path, interpreter_description->absolute_path());
- return KResult(-ENOEXEC);
- }
- if (!interpreter_interpreter_path.is_empty()) {
- dbgln("exec({}): Interpreter ({}) has its own interpreter ({})! No thank you!", path, interpreter_description->absolute_path(), interpreter_interpreter_path);
- return KResult(-ELOOP);
- }
- return interpreter_description;
- }
- if (elf_header->e_type != ET_EXEC) {
- // We can't exec an ET_REL, that's just an object file from the compiler
- // If it's ET_DYN with no PT_INTERP, then we can't load it properly either
- return KResult(-ENOEXEC);
- }
- // No interpreter, but, path refers to a valid elf image
- return KResult(KSuccess);
- }
- int Process::exec(String path, Vector<String> arguments, Vector<String> environment, int recursion_depth)
- {
- if (recursion_depth > 2) {
- dbgln("exec({}): SHENANIGANS! recursed too far trying to find #! interpreter", path);
- return -ELOOP;
- }
- // Open the file to check what kind of binary format it is
- // Currently supported formats:
- // - #! interpreted file
- // - ELF32
- // * ET_EXEC binary that just gets loaded
- // * ET_DYN binary that requires a program interpreter
- //
- auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
- if (result.is_error())
- return result.error();
- auto description = result.release_value();
- auto metadata = description->metadata();
- // Always gonna need at least 3 bytes. these are for #!X
- if (metadata.size < 3)
- return -ENOEXEC;
- ASSERT(description->inode());
- // Read the first page of the program into memory so we can validate the binfmt of it
- char first_page[PAGE_SIZE];
- auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
- auto nread_or_error = description->read(first_page_buffer, sizeof(first_page));
- if (nread_or_error.is_error())
- return -ENOEXEC;
- // 1) #! interpreted file
- auto shebang_result = find_shebang_interpreter_for_executable(first_page, nread_or_error.value());
- if (!shebang_result.is_error()) {
- Vector<String> new_arguments(shebang_result.value());
- new_arguments.append(path);
- arguments.remove(0);
- new_arguments.append(move(arguments));
- return exec(shebang_result.value().first(), move(new_arguments), move(environment), ++recursion_depth);
- }
- // #2) ELF32 for i386
- auto elf_result = find_elf_interpreter_for_executable(path, first_page, nread_or_error.value(), metadata.size);
- RefPtr<FileDescription> interpreter_description;
- // We're getting either an interpreter, an error, or KSuccess (i.e. no interpreter but file checks out)
- if (!elf_result.is_error())
- interpreter_description = elf_result.value();
- else if (elf_result.error().is_error())
- return elf_result.error();
- // The bulk of exec() is done by do_exec(), which ensures that all locals
- // are cleaned up by the time we yield-teleport below.
- Thread* new_main_thread = nullptr;
- u32 prev_flags = 0;
- int rc = do_exec(move(description), move(arguments), move(environment), move(interpreter_description), new_main_thread, prev_flags);
- m_exec_tid = 0;
- if (rc < 0)
- return rc;
- ASSERT_INTERRUPTS_DISABLED();
- ASSERT(Processor::current().in_critical());
- auto current_thread = Thread::current();
- if (current_thread == new_main_thread) {
- // We need to enter the scheduler lock before changing the state
- // and it will be released after the context switch into that
- // thread. We should also still be in our critical section
- ASSERT(!g_scheduler_lock.own_lock());
- ASSERT(Processor::current().in_critical() == 1);
- g_scheduler_lock.lock();
- current_thread->set_state(Thread::State::Running);
- Processor::assume_context(*current_thread, prev_flags);
- ASSERT_NOT_REACHED();
- }
- Processor::current().leave_critical(prev_flags);
- return 0;
- }
- int Process::sys$execve(Userspace<const Syscall::SC_execve_params*> user_params)
- {
- REQUIRE_PROMISE(exec);
- // NOTE: Be extremely careful with allocating any kernel memory in exec().
- // On success, the kernel stack will be lost.
- Syscall::SC_execve_params params;
- if (!copy_from_user(¶ms, user_params))
- return -EFAULT;
- if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
- return -E2BIG;
- String path;
- {
- auto path_arg = get_syscall_path_argument(params.path);
- if (path_arg.is_error())
- return path_arg.error();
- path = path_arg.value();
- }
- auto copy_user_strings = [](const auto& list, auto& output) {
- if (!list.length)
- return true;
- Checked size = sizeof(list.strings);
- size *= list.length;
- if (size.has_overflow())
- return false;
- Vector<Syscall::StringArgument, 32> strings;
- strings.resize(list.length);
- if (!copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument)))
- return false;
- for (size_t i = 0; i < list.length; ++i) {
- auto string = copy_string_from_user(strings[i]);
- if (string.is_null())
- return false;
- output.append(move(string));
- }
- return true;
- };
- Vector<String> arguments;
- if (!copy_user_strings(params.arguments, arguments))
- return -EFAULT;
- Vector<String> environment;
- if (!copy_user_strings(params.environment, environment))
- return -EFAULT;
- int rc = exec(move(path), move(arguments), move(environment));
- ASSERT(rc < 0); // We should never continue after a successful exec!
- return rc;
- }
- }
|