summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/execve.cpp
AgeCommit message (Collapse)Author
2021-06-04Kernel: Process::exec(): Check if path is a regular fileJelle Raaijmakers
https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html [EACCES] The new process image file is not a regular file and the implementation does not support execution of files of its type. Let's check whether the passed `path` is indeed a regular file.
2021-05-29Kernel: Convert Process::get_syscall_path_argument() to KStringAndreas Kling
This API now returns a KResultOr<NonnullOwnPtr<KString>> and allocation failures should be propagated everywhere nicely. :^)
2021-05-23Kernel: Make sure we only log profiling events when m_profiling is trueGunnar Beutner
Previously the process' m_profiling flag was ignored for all event types other than CPU samples. The kfree tracing code relies on temporarily disabling tracing during exec. This didn't work for per-process profiles and would instead panic. This updates the profiling code so that the m_profiling flag isn't ignored.
2021-05-19Kernel: Add support for profiling kmalloc()/kfree()Gunnar Beutner
2021-05-16AK+Kernel+LibELF: Remove the need for `IteratorDecision::Continue`Nicholas Baron
By constraining two implementations, the compiler will select the best fitting one. All this will require is duplicating the implementation and simplifying for the `void` case. This constraining also informs both the caller and compiler by passing the callback parameter types as part of the constraint (e.g.: `IterationFunction<int>`). Some `for_each` functions in LibELF only take functions which return `void`. This is a minimal correctness check, as it removes one way for a function to incompletely do something. There seems to be a possible idiom where inside a lambda, a `return;` is the same as `continue;` in a for-loop.
2021-05-10Kernel: Fix loading ELF images without PT_INTERPGunnar Beutner
Previously we'd try to load ELF images which did not have an interpreter set with an incorrect load offset of 0, i.e. way outside of the part of the address space where we'd expect either the dynamic loader or the user's executable to reside. This fixes the problem by using get_load_offset for both executables which have an interpreter set and those which don't. Notably this allows us to actually successfully execute the Loader.so binary: courage:~ $ /usr/lib/Loader.so You have invoked `Loader.so'. This is the helper program for programs that use shared libraries. Special directives embedded in executables tell the kernel to load this program. This helper program loads the shared libraries needed by the program, prepares the program to run, and runs it. You do not need to invoke this helper program directly. courage:~ $
2021-05-07Kernel: Move process creation perf events to PerformanceManagerBrian Gianforcaro
2021-05-01Kernel: Make processes start with a 16-byte-aligned stackSahan Fernando
2021-04-29Kernel: Harden sys$execve Vector usage against OOM.Brian Gianforcaro
2021-04-26Kernel+Profiler: Improve profiling subsystemGunnar Beutner
This turns the perfcore format into more a log than it was before, which lets us properly log process, thread and region creation/destruction. This also makes it unnecessary to dump the process' regions every time it is scheduled like we did before. Incidentally this also fixes 'profile -c' because we previously ended up incorrectly dumping the parent's region map into the profile data. Log-based mmap support enables profiling shared libraries which are loaded at runtime, e.g. via dlopen(). This enables profiling both the parent and child process for programs which use execve(). Previously we'd discard the profiling data for the old process. The Profiler tool has been updated to not treat thread IDs as process IDs anymore. This enables support for processes with more than one thread. Also, there's a new widget to filter which process should be displayed.
2021-04-25Everywhere: Remove empty line after function body opening curly braceLinus Groh
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-18Kernel: Allow system calls from the dynamic loaderGunnar Beutner
Previously the dynamic loader would become unused after it had invoked the program's entry function. However, in order to support exceptions and - at a later point - dlfcn functionality we need to call back into the dynamic loader at runtime. Because the dynamic loader has a static copy of LibC it'll attempt to invoke syscalls directly from its text segment. For this to work the executable region for the dynamic loader needs to have syscalls enabled.
2021-03-19Kernel: Set TLS-related members of Process after loading static programItamar
We previously ignored these values in the return value of load_elf_object, which causes us to not allocate a TLS region for statically-linked programs.
2021-03-19Kernel: Make FileDescription::seek() return KResultOr<off_t>Andreas Kling
This exposed a bunch of places where errors were not propagated, so this patch is forced to deal with them as well.
2021-03-12Kernel: Fix rounding of PT_LOAD mappings in sys$execve()Andreas Kling
We were not rounding the mappings down/up correctly, which could lead to executables missing the last 4 KB of text and/or data.
2021-03-11Kernel: Move process signal trampoline address into protected dataAndreas Kling
2021-03-11Kernel: Don't keep protected Process data in a separate allocationAndreas Kling
The previous architecture had a huge flaw: the pointer to the protected data was itself unprotected, allowing you to overwrite it at any time. This patch reorganizes the protected data so it's part of the Process class itself. (Actually, it's a new ProcessBase helper class.) We use the first 4 KB of Process objects themselves as the new storage location for protected data. Then we make Process objects page-aligned using MAKE_ALIGNED_ALLOCATED. This allows us to easily turn on/off write-protection for everything in the ProcessBase portion of Process. :^) Thanks to @bugaevc for pointing out the flaw! This is still not perfect but it's an improvement.
2021-03-10Kernel: Move process pledge promises into protected dataAndreas Kling
2021-03-10Kernel: Move select Process members into protected memoryAndreas Kling
Process member variable like m_euid are very valuable targets for kernel exploits and until now they have been writable at all times. This patch moves m_euid along with a whole bunch of other members into a new Process::ProtectedData struct. This struct is remapped as read-only memory whenever we don't need to write to it. This means that a kernel write primitive is no longer enough to overwrite a process's effective UID, you must first unprotect the protected data where the UID is stored. :^)
2021-03-01Kernel: Make all syscall functions return KResultOr<T>Andreas Kling
This makes it a lot easier to return errors since we no longer have to worry about negating EFOO errors and can just return them flat.
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED) Since all of these checks are done in release builds as well, let's rename them to VERIFY to prevent confusion, as everyone is used to assertions being compiled out in release. We can introduce a new ASSERT macro that is specifically for debug checks, but I'm doing this wholesale conversion first since we've accumulated thousands of these already, and it's not immediately obvious which ones are suitable for ASSERT.
2021-02-21Kernel: Populate ELF::AuxilaryValue::Platform from Processor object.Brian Gianforcaro
Move this to the processor object so it can easily be implemented when Serenity is compiled for a different architecture.
2021-02-21Kernel: Remove unneeded Thread::set_default_signal_dispositionsBrian Gianforcaro
The `default_signal_action(u8 signal)` function already has the full mapping. The only caveat being that now we need to make sure the thread constructor and clear_signals() method do the work of resetting the m_signal_action_data array, instead or relying on the previous logic in set_default_signal_dispositions.
2021-02-19Kernel: Release ptrace lock in exec before stopping due to PT_TRACE_MEAndreas Kling
If we have a tracer process waiting for us to exec, we need to release the ptrace lock before stopping ourselves, since otherwise the tracer will block forever on the lock. Fixes #5409.
2021-02-18Kernel: Use KResult a bit more in sys$execve()Andreas Kling
2021-02-14Kernel: Add some bits of randomness to the userspace stack pointerAndreas Kling
This patch adds a random offset between 0 and 4096 to the initial stack pointer in new processes. Since the stack has to be 16-byte aligned, the bottom bits can't be randomized. Yet another thing to make things less predictable. :^)
2021-02-14Kernel: Assert if rounding-up-to-page-size would wrap around to 0Andreas Kling
If we try to align a number above 0xfffff000 to the next multiple of the page size (4 KiB), it would wrap around to 0. This is most likely never what we want, so let's assert if that happens.
2021-02-14Kernel: Map signal trampoline into each process's address spaceAndreas Kling
The signal trampoline was previously in kernelspace memory, but with a special exception to make it user-accessible. This patch moves it into each process's regular address space so we can stop supporting user-allowed memory above 0xc0000000.
2021-02-14Kernel: Fix wrong sizeof() type in sys$execve() argument overflow checkAndreas Kling
2021-02-12Kernel: Move get_interpreter_load_offset() out of Process classAndreas Kling
This is only used inside the sys$execve() implementation so just make it a execve.cpp local function.
2021-02-08Kernel: Prevent execve/ptrace raceAndreas Kling
Add a per-process ptrace lock and use it to prevent ptrace access to a process after it decides to commit to a new executable in sys$execve(). Fixes #5230.
2021-02-08Kernel: Remove two unused fields from sys$execve's LoadResultAndreas Kling
2021-02-08Kernel: Move ShouldAllocateTls enum from Process to execve.cppAndreas Kling
2021-02-08Kernel: Set the dumpable flag before switching spaces in sys$execve()Andreas Kling
2021-02-08Kernel: Remove outdated code to dump memory layout after exec loadAndreas Kling
2021-02-08Kernel: Factor address space management out of the Process classAndreas Kling
This patch adds Space, a class representing a process's address space. - Each Process has a Space. - The Space owns the PageDirectory and all Regions in the Process. This allows us to reorganize sys$execve() so that it constructs and populates a new Space fully before committing to it. Previously, we would construct the new address space while still running in the old one, and encountering an error meant we had to do tedious and error-prone rollback. Those problems are now gone, replaced by what's hopefully a set of much smaller problems and missing cleanups. :^)
2021-02-02Kernel: Add a way to specify which memory regions can make syscallsAndreas Kling
This patch adds sys$msyscall() which is loosely based on an OpenBSD mechanism for preventing syscalls from non-blessed memory regions. It works similarly to pledge and unveil, you can call it as many times as you like, and when you're finished, you call it with a null pointer and it will stop accepting new regions from then on. If a syscall later happens and doesn't originate from one of the previously blessed regions, the kernel will simply crash the process.
2021-01-27Kernel: Remove Range "valid" state and use Optional<Range> insteadAndreas Kling
It's easier to understand VM ranges if they are always valid. We can simply use an empty Optional<Range> to encode absence when needed.
2021-01-26Meta: Split debug defines into multiple headers.asynts
The following script was used to make these changes: #!/bin/bash set -e tmp=$(mktemp -d) echo "tmp=$tmp" find Kernel \( -name '*.cpp' -o -name '*.h' \) | sort > $tmp/Kernel.files find . \( -path ./Toolchain -prune -o -path ./Build -prune -o -path ./Kernel -prune \) -o \( -name '*.cpp' -o -name '*.h' \) -print | sort > $tmp/EverythingExceptKernel.files cat $tmp/Kernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/Kernel.macros cat $tmp/EverythingExceptKernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/EverythingExceptKernel.macros comm -23 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/Kernel.unique comm -1 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/EverythingExceptKernel.unique cat $tmp/Kernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/Kernel.header cat $tmp/EverythingExceptKernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/EverythingExceptKernel.header for macro in $(cat $tmp/Kernel.unique) do cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.new-includes ||: done cat $tmp/Kernel.new-includes | sort > $tmp/Kernel.new-includes.sorted for macro in $(cat $tmp/EverythingExceptKernel.unique) do cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.old-includes ||: done cat $tmp/Kernel.old-includes | sort > $tmp/Kernel.old-includes.sorted comm -23 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.new comm -13 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.old comm -12 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.mixed for file in $(cat $tmp/Kernel.includes.new) do sed -i -E 's/#include <AK\/Debug\.h>/#include <Kernel\/Debug\.h>/' $file done for file in $(cat $tmp/Kernel.includes.mixed) do echo "mixed include in $file, requires manual editing." done
2021-01-26Kernel: Update process promise states on execve() and fork()Andreas Kling
We now move the execpromises state into the regular promises, and clear the execpromises state. Also make sure to duplicate the promise state on fork. This fixes an issue where "su" would launch a shell which immediately crashed due to not having pledged "stdio".
2021-01-26Kernel: Remove allocate_region() functions that don't take a RangeAndreas Kling
Let's force callers to provide a VM range when allocating a region. This makes ENOMEM error handling more visible and removes implicit VM allocation which felt a bit magical.
2021-01-25Everywhere: Hook up remaining debug macros to Debug.h.asynts
2021-01-25Everywhere: Remove unnecessary debug comments.asynts
It would be tempting to uncomment these statements, but that won't work with the new changes. This was done with the following commands: find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \; find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-24Kernel: Fix two error codes being returned as positive in Process::execLuke
This made the assertion on line 921 think it was a successful exec, when it wasn't. Fixes #5084
2021-01-23Kernel: Clear coredump metadata on exec()Andreas Kling
If for some reason a process wants to exec after saving some coredump metadata, we should just throw away the data.
2021-01-20Kernel+LibC: Turn errno codes into a strongly typed enumAndreas Kling
..and allow implicit creation of KResult and KResultOr from ErrnoCode. This means that kernel functions that return those types can finally do "return EINVAL;" and it will just work. There's a handful of functions that still deal with signed integers that should be converted to return KResults.
2021-01-17Kernel: Some futex improvementsTom
This adds support for FUTEX_WAKE_OP, FUTEX_WAIT_BITSET, FUTEX_WAKE_BITSET, FUTEX_REQUEUE, and FUTEX_CMP_REQUEUE, as well well as global and private futex and absolute/relative timeouts against the appropriate clock. This also changes the implementation so that kernel resources are only used when a thread is blocked on a futex. Global futexes are implemented as offsets in VMObjects, so that different processes can share a futex against the same VMObject despite potentially being mapped at different virtual addresses.
2021-01-17Kernel: Limit exec arguments and environment to 1/8th of stack eachAndreas Kling
This sort-of matches what some other systems do and seems like a generally sane thing to do instead of allowing programs to spawn a child with a nearly full stack.
2021-01-17Kernel+Userland: Remove shared buffers (shbufs)Andreas Kling
All users of this mechanism have been switched to anonymous files and passing file descriptors with sendfd()/recvfd(). Shbufs got us where we are today, but it's time we say good-bye to them and welcome a much more idiomatic replacement. :^)