summaryrefslogtreecommitdiff
path: root/Kernel/StdLib.h
AgeCommit message (Collapse)Author
2021-09-07Kernel: Remove redundant [[nodiscard]] on KResult return valuesAndreas Kling
Both KResult and KResultOr are [[nodiscard]] at the class level, so there's no need to have functions return `[[nodiscard]] KResult`.
2021-09-07Kernel: Make copy_time_from_user() helpers use KResultOr<Time>Andreas Kling
...and use TRY() for smooth error propagation everywhere.
2021-09-05Kernel: Add copy_typed_from_userspace<T>(Userspace<T const*>)Andreas Kling
This allows easy retrieval of typed POD values from userspace with implicit error propagation.
2021-09-05Kernel: Make copy_{from,to}_user() return KResult and use TRY()Andreas Kling
This makes EFAULT propagation flow much more naturally. :^)
2021-09-05AK+Kernel: Move KResult.h to Kernel/API for userspace accesssin-ack
This commit moves the KResult and KResultOr objects to Kernel/API to signify that they may now be freely used by userspace code at points where a syscall-related error result is to be expected. It also exposes KResult and KResultOr to the global namespace to make it nicer to use for userspace code.
2021-08-15Kernel: Remove copy_string_from_user() as it's no longer usedAndreas Kling
2021-08-13Kernel: Remove char* versions of path argument / kstring copy methodsBrian Gianforcaro
The only two paths for copying strings in the kernel should be going through the existing Userspace<char const*>, or StringArgument methods. Lets enforce this by removing the option for using the raw cstring APIs that were previously available.
2021-07-04Everywhere: Fix incorrect usages of AK::CheckedIdan Horowitz
Specifically, explicitly specify the checked type, use the resulting value instead of doing the same calculation twice, and break down calculations to discrete operations to ensure no intermediary overflows are missed.
2021-05-28Kernel: Add try_copy_kstring_from_user()Andreas Kling
This is a convenience function that works the same as our old copy_string_from_user(), but this returns a KString (and can fail!)
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-10AK+Everywhere: Make StdLibExtras templates less wrapper-yAnotherTest
This commit makes the user-facing StdLibExtras templates and utilities arguably more nice-looking by removing the need to reach into the wrapper structs generated by them to get the value/type needed. The C++ standard library had to invent `_v` and `_t` variants (likely because of backwards compat), but we don't need to cater to any codebase except our own, so might as well have good things for free. :^)
2021-03-02Kernel: Prevent using copy_from_user() for timespec/timevalBen Wiederhake
These structs can be inconsistent, for example if the amount of microseconds is negative or larger than 1'000'000. Therefore, they should not be copied as-is. Use copy_time_from_user instead.
2021-03-02Kernel: Implement 'copy_time_from_user' functions to sanitize argumentsBen Wiederhake
2021-02-15Kernel: Mark more StdLib functions as [[nodiscard]]Brian Gianforcaro
In the never ending journey to catch bugs, mark more functions as [[nodiscard]] to find incorrect call sites.
2021-01-17Kernel: Add safe atomic functionsTom
This allows us to perform atomic operations on potentially unsafe user space pointers.
2020-10-02Kernel: Add checks for is_trivially_copyable to copy_to/from_userTom
If we're copying structures, we only ever want to copy trivially copyable structures.
2020-09-17Kernel+LibC+UserspaceEmulator: Mostly add recvmsg(), sendmsg()Nico Weber
The implementation only supports a single iovec for now. Some might say having more than one iovec is the main point of recvmsg() and sendmsg(), but I'm interested in the control message bits.
2020-09-13Kernel: Make copy_to/from_user safe and remove unnecessary checksTom
Since the CPU already does almost all necessary validation steps for us, we don't really need to attempt to do this. Doing it ourselves doesn't really work very reliably, because we'd have to account for other processors modifying virtual memory, and we'd have to account for e.g. pages not being able to be allocated due to insufficient resources. So change the copy_to/from_user (and associated helper functions) to use the new safe_memcpy, which will return whether it succeeded or not. The only manual validation step needed (which the CPU can't perform for us) is making sure the pointers provided by user mode aren't pointing to kernel mappings. To make it easier to read/write from/to either kernel or user mode data add the UserOrKernelBuffer helper class, which will internally either use copy_from/to_user or directly memcpy, or pass the data through directly using a temporary buffer on the stack. Last but not least we need to keep syscall params trivial as we need to copy them from/to user mode using copy_from/to_user.
2020-08-24Kernel: Remove strcpy()Ben Wiederhake
These are not called in the kernel or by libstdc++ anyway. Remove the tempting function, and prevent future overflows.
2020-08-17Kernel: Remove strncpy() and strrchr()Andreas Kling
These are not called anywhere in the kernel anyway.
2020-08-13Kernel: Remove strdup() since nothing uses itAndreas Kling
2020-08-01Kernel+LibC: Implement 'memmem'AnotherTest
This commit adds an implementation of memmem, using the Bitap text search algorithm for needles smaller than 32 bytes, and a naive loop search for longer needles.
2020-07-31Kernel+AK: Add and use Userspace<T>::unsafe_userspace_ptr()Andreas Kling
Since we already have the type information in the Userspace template, it was a bit silly to cast manually everywhere. Just add a sufficiently scary-sounding getter for a typed pointer. Thanks @alimpfard for pointing out that I was being silly with tossing out the type. In the future we may want to make this API non-public as well.
2020-07-31Kernel: Use Userspace<T> in sys$getcwd() and sys$chdir()Andreas Kling
Add more validation helper overloads as we go. :^)
2020-07-31Kernel: Use Userspace<T> for sys$read() and sys$stat()Andreas Kling
Add validation helper overloads as needed.
2020-05-23Kernel: Dont't static_assert that size_t is 32-bit :^)Andreas Kling
2020-05-16Kernel: Absorb LibBareMetal back into the kernelAndreas Kling
This was supposed to be the foundation for some kind of pre-kernel environment, but nobody is working on it right now, so let's move everything back into the kernel and remove all the confusion.
2020-02-09Kernel: Apply changes to use LibBareMetal definitionsLiav A
2020-01-19Kernel: Assert that copy_to/from_user() are called with user addressesAndreas Kling
This will panic the kernel immediately if these functions are misused so we can catch it and fix the misuse. This patch fixes a couple of misuses: - create_signal_trampolines() writes to a user-accessible page above the 3GB address mark. We should really get rid of this page but that's a whole other thing. - CoW faults need to use copy_from_user rather than copy_to_user since it's the *source* pointer that points to user memory. - Inode faults need to use memcpy rather than copy_to_user since we're copying a kernel stack buffer into a quickmapped page. This should make the copy_to/from_user() functions slightly less useful for exploitation. Before this, they were essentially just glorified memcpy() with SMAP disabled. :^)
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2020-01-11Kernel: Pass a parameter struct to mknod()Andreas Kling
2020-01-10Kernel: Pass characters+length to link()Andreas Kling
2020-01-05Kernel: Remove SmapDisablers in open(), openat() and set_thread_name()Andreas Kling
This patch introduces a helpful copy_string_from_user() function that takes a bounded null-terminated string from userspace memory and copies it into a String object.
2020-01-05Kernel: Start implementing x86 SMAP supportAndreas Kling
Supervisor Mode Access Prevention (SMAP) is an x86 CPU feature that prevents the kernel from accessing userspace memory. With SMAP enabled, trying to read/write a userspace memory address while in the kernel will now generate a page fault. Since it's sometimes necessary to read/write userspace memory, there are two new instructions that quickly switch the protection on/off: STAC (disables protection) and CLAC (enables protection.) These are exposed in kernel code via the stac() and clac() helpers. There's also a SmapDisabler RAII object that can be used to ensure that you don't forget to re-enable protection before returning to userspace code. THis patch also adds copy_to_user(), copy_from_user() and memset_user() which are the "correct" way of doing things. These functions allow us to briefly disable protection for a specific purpose, and then turn it back on immediately after it's done. Going forward all kernel code should be moved to using these and all uses of SmapDisabler are to be considered FIXME's. Note that we're not realizing the full potential of this feature since I've used SmapDisabler quite liberally in this initial bring-up patch.
2020-01-02Kernel: Introduce the ACPI subsystemLiav A
ACPI subsystem includes 3 types of parsers that are created during runtime, each one capable of parsing ACPI tables at different level. ACPIParser is the most basic parser which is essentialy a parser that can't parse anything useful, due to a user request to disable ACPI support in a kernel boot parameter. ACPIStaticParser is a derived class from ACPIParser, which is able to parse only static data (e.g. FADT, HPET, MCFG and other tables), thus making it not able to parse AML (ACPI Machine Language) nor to support handling of hardware events and power management. This type of parser can be created with a kernel boot parameter. ACPIDynamicParser is a derived class from ACPIStaticParser, which includes all the capabilities of the latter, but *should* implement an AML interpretation, (by building the ACPI AML namespace) and handling power & hardware events. Currently the methods to support AML interpretation are not implemented. This type of parser is created automatically during runtime if the user didn't specify a boot parameter related to ACPI initialization. Also, adding strncmp function definition in StdLib.h, to be able to use it in ACPIStaticParser class.
2019-08-11Kernel: Add strncmp()Sergey Bugaev
2019-07-03AK: Rename the common integer typedefs to make it obvious what they are.Andreas Kling
These types can be picked up by including <AK/Types.h>: * u8, u16, u32, u64 (unsigned) * i8, i16, i32, i64 (signed)
2019-05-28Add clang-format fileRobin Burchell
Also run it across the whole tree to get everything using the One True Style. We don't yet run this in an automated fashion as it's a little slow, but there is a snippet to do so in makeall.sh.
2019-04-06Kernel: Get rid of Kernel/types.h, separate LinearAddress/PhysicalAddress.Andreas Kling
2019-03-23Kernel: Introduce threads, and refactor everything in support of it.Andreas Kling
The scheduler now operates on threads, rather than on processes. Each process has a main thread, and can have any number of additional threads. The process exits when the main thread exits. This patch doesn't actually spawn any additional threads, it merely does all the plumbing needed to make it possible. :^)
2019-03-11Kernel: Bring up enough networking code that we can respond to ARP requests.Andreas Kling
This is all pretty rickety but we can now respond to "arping" from the host while running inside QEMU. Very cool. :^)
2019-02-22Start fixing things up to build with a proper cross-compiler.Andreas Kling
2019-02-07Kernel: Don't disable interrupts to access the system hostname.Andreas Kling
2018-12-02Make it possible to build the Kernel on a macOS host.Andreas Kling
It still requires an ELF compiler and linker, but at least it builds. I need to get rid of the "Unix" namespace. This does a lot of that.
2018-11-17Make bash-2.05b build with minimal changes.Andreas Kling
This is really neat. :^)
2018-11-09Make kernel build with clang.Andreas Kling
It's a bit faster than g++ and seems to generate perfectly fine code. The kernel is also roughly 10% smaller(!)
2018-11-05Implement COW pages! :^)Andreas Kling
sys$fork() now clones all writable regions with per-page COW bits. The pages are then mapped read-only and we handle a PF by COWing the pages. This is quite delightful. Obviously there's lots of work to do still, and it needs better data structures, but the general concept works.
2018-10-31Fix busted display of tty names in /proc/summary.Andreas Kling
2018-10-27Greatly improve /proc/PID/stack by tracing the ebp frame chain.Andreas Kling
I also added a generator cache to FileHandle. This way, multiple reads to a generated file (i.e in a synthfs) can transparently handle multiple calls to read() without the contents changing between calls. The cache is discarded at EOF (or when the FileHandle is destroyed.)
2018-10-17Integrate ext2 from VFS into Kernel.Andreas Kling