Commit graph

4027 commits

Author SHA1 Message Date
Andreas Kling e47bffdc8c Kernel: Add some bits of randomness to the userspace stack pointer
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-14 11:53:49 +01:00
Andreas Kling 4188373020 Kernel: Fix TOCTOU in syscall entry region validation
We were doing stack and syscall-origin region validations before
taking the big process lock. There was a window of time where those
regions could then be unmapped/remapped by another thread before we
proceed with our syscall.

This patch closes that window, and makes sys$get_stack_bounds() rely
on the fact that we now know the userspace stack pointer to be valid.

Thanks to @BenWiederhake for spotting this! :^)
2021-02-14 11:47:14 +01:00
Andreas Kling 10b7f6b77e Kernel: Mark handle_crash() as [[noreturn]] 2021-02-14 11:47:14 +01:00
Ben Wiederhake c0692f1f95 Kernel: Avoid magic number in sys$poll 2021-02-14 10:57:33 +01:00
Andreas Kling cc341c95aa Kernel: Panic on sys$get_stack_bounds() in stack-less process 2021-02-14 10:51:18 +01:00
Andreas Kling 3131281747 Kernel: Panic on syscall from process with IOPL != 0
If this happens then the kernel is in an undefined state, so we should
rather panic than attempt to limp along.
2021-02-14 10:51:17 +01:00
Andreas Kling 781d29a337 Kernel+Userland: Give sys$recvfd() an options argument for O_CLOEXEC
@bugaevc pointed out that we shouldn't be setting this flag in
userspace, and he's right of course.
2021-02-14 10:39:48 +01:00
Andreas Kling 09b1b09c19 Kernel: Assert if rounding-up-to-page-size would wrap around to 0
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-14 10:01:50 +01:00
Andreas Kling 198d641808 Kernel: Panic on attempt to map mmap'ed page at a kernel address
If we somehow get tricked into mapping user-controlled mmap memory
at a kernel address, let's just panic the kernel.
2021-02-14 09:36:58 +01:00
Andreas Kling b712345c92 Kernel: Use PANIC() in a bunch of places :^) 2021-02-14 09:36:58 +01:00
Andreas Kling c598a95b1c Kernel: Add a PANIC() function
Let's be a little more expressive when inducing a kernel panic. :^)
PANIC(...) passes any arguments you give it to dmesgln(), then prints
a backtrace and hangs the machine.
2021-02-14 09:36:58 +01:00
Andreas Kling 4021264201 Kernel: Make the Region constructor private
We can use adopt_own(*new T) instead of make<T>().
2021-02-14 01:39:04 +01:00
Andreas Kling 8415866c03 Kernel: Remove user/kernel flags from Region
Now that we no longer need to support the signal trampolines being
user-accessible inside the kernel memory range, we can get rid of the
"kernel" and "user-accessible" flags on Region and simply use the
address of the region to determine whether it's kernel or user.

This also tightens the page table mapping code, since it can now set
user-accessibility based solely on the virtual address of a page.
2021-02-14 01:34:23 +01:00
Andreas Kling 1593219a41 Kernel: Map signal trampoline into each process's address space
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-14 01:33:17 +01:00
Andreas Kling ffdfbf1dba Kernel: Fix wrong sizeof() type in sys$execve() argument overflow check 2021-02-14 00:15:01 +01:00
Andreas Kling 34a83aba71 Kernel: Convert klog() => dbgln()/dmesgln() in Arch/i386/CPU.cpp 2021-02-13 21:51:16 +01:00
Jean-Baptiste Boric 9ce0639383 Kernel: Use divide_rounded_up inside write_block_list_for_inode 2021-02-13 19:56:49 +01:00
Jean-Baptiste Boric 869b33d6dd Kernel: Support triply indirect blocks for BlockListShape computation 2021-02-13 19:56:49 +01:00
Tom b445f15131 Kernel: Avoid flushing the tlb if there's only one thread
If we're flushing user space pointers and the process only has one
thread, we do not need to broadcast this to other processors as
they will all discard that request anyway.
2021-02-13 19:46:45 +01:00
Andreas Kling c877612211 Kernel: Round down base of partial ranges provided to munmap/mprotect
We were failing to round down the base of partial VM ranges. This led
to split regions being constructed that could have a non-page-aligned
base address. This would then trip assertions in the VM code.

Found by fuzz-syscalls. :^)
2021-02-13 01:49:44 +01:00
Andreas Kling af0e52ca54 Kernel: Don't assert on sys$setsockopt() with unexpected level
Just error out with ENOPROTOOPT instead.

Found by fuzz-syscalls. :^)
2021-02-13 01:29:28 +01:00
Andreas Kling a5def4e98c Kernel: Sanity check the VM range when constructing a Region
This should help us catch bogus VM ranges ending up in a process's
address space sooner.
2021-02-13 01:18:03 +01:00
Andreas Kling 62f0f73bf0 Kernel: Limit the number of file descriptors sys$poll() can handle
Just slap an arbitrary limit on there so we don't panic if somebody
asks us to poll 1 fajillion fds.

Found by fuzz-syscalls. :^)
2021-02-13 01:18:03 +01:00
Andreas Kling 7551090056 Kernel: Round up ranges to page size multiples in munmap and mprotect
This prevents passing bad inputs to RangeAllocator who then asserts.

Found by fuzz-syscalls. :^)
2021-02-13 01:18:03 +01:00
Ben Wiederhake 46e5890152 Kernel: Add forgotten 'const' flag 2021-02-13 00:40:31 +01:00
Ben Wiederhake 546cdde776 Kernel: clock_nanosleep's 'flags' is not a bitset
This had the interesting effect that most, but not all, non-zero values
were interpreted as an absolute value.
2021-02-13 00:40:31 +01:00
Ben Wiederhake e1db8094b6 Kernel: Avoid casting arbitrary user-controlled int to enum
This caused a load-invalid-value warning by KUBSan.

Found by fuzz-syscalls. Can be reproduced by running this in the Shell:

    $ syscall waitid [ 1234 ]
2021-02-13 00:40:31 +01:00
Ben Wiederhake c6027ed7cc Kernel: Refuse excessively long iovec list
If a program attempts to write from more than a million different locations,
there is likely shenaniganery afoot! Refuse to write to prevent kmem exhaustion.

Found by fuzz-syscalls. Can be reproduced by running this in the Shell:

    $ syscall writev 1 [ 0 ] 0x08000000
2021-02-13 00:40:31 +01:00
Ben Wiederhake 987b7f7917 Kernel: Forbid empty and whitespace-only process names
Those only exist to confuse the user anyway.

Found while using fuzz-syscalls.
2021-02-13 00:40:31 +01:00
Ben Wiederhake 4c42d1e35a Kernel: Do not try to print the string that cannot be read
What a silly bug :^)

Found by fuzz-syscalls. Can be reproduced by running this in the Shell:

    $ syscall set_thread_name 14 14 14
2021-02-13 00:40:31 +01:00
Ben Wiederhake 1e630fb78a Kernel: Avoid creating unkillable processes
Found by fuzz-syscalls. Can be reproduced by running this in the Shell:

    $ syscall exit_thread

This leaves the process in the 'Dying' state but never actually removes it.

Therefore, avoid this scenario by pretending to exit the entire process.
2021-02-13 00:40:31 +01:00
Ben Wiederhake b5e5e43d4b Kernel: Fix typo 2021-02-13 00:40:31 +01:00
Ben Wiederhake caeb41d92b Kernel: Don't crash on syscall with kernel-space argument
Fixes #5198.
2021-02-13 00:40:31 +01:00
Andreas Kling 9ae02d4c92 Kernel: Don't use a VLA for outgoing UDP packets
We had the same exact problem as da981578e3 but for UDP sockets.
2021-02-12 23:46:15 +01:00
Andreas Kling da981578e3 Kernel: Don't use a VLA for outgoing TCP packets
Since the payload size is user-controlled, this could be used to
overflow the kernel stack.

We should probably also be breaking things into smaller packets at a
higher level, e.g TCPSocket::protocol_send(), but let's do that as
a separate exercise.

Fixes #5310.
2021-02-12 23:00:25 +01:00
Andreas Kling 29045f84d4 Kernel: Decrease default userspace stack size to 1 MiB
Not sure why this was 4 MiB in the first place, but that's a lot of
memory to reserve for each thread when we're running with 512 MiB
total in the default testing setup. :^)
2021-02-12 19:17:09 +01:00
Andreas Kling e050577f0a Kernel: Make MAP_RANDOMIZED honor alignment requests
Previously, we only cared about the alignment on the fallback path.
2021-02-12 19:15:59 +01:00
Andreas Kling 4e2802bf91 Kernel: Move region dumps from dmesg to debug log
Also fix a broken format string caught by the new format string checks.
2021-02-12 16:33:58 +01:00
Andreas Kling 1ef43ec89a Kernel: Move get_interpreter_load_offset() out of Process class
This is only used inside the sys$execve() implementation so just make
it a execve.cpp local function.
2021-02-12 16:30:29 +01:00
Andreas Kling c4db224c94 Kernel: Convert klog() => dmesgln() / dbgln() in MemoryManager 2021-02-12 16:24:40 +01:00
Andreas Kling 5af69d6e93 Kernel: Convert klog() to dmesgln() in RangeAllocator 2021-02-12 16:24:40 +01:00
Andreas Kling 0a45cfee01 DevFS: Use strongly typed InodeIndex
Also add an assertion for the DevFS inode index allocator overflowing.
2021-02-12 16:24:40 +01:00
Sergey Bugaev 4717009e3e Kernel: Hold less locks when receiving ICMP packets
* We don't have to lock the "all IPv4 sockets" in exclusive mode, shared mode is
  enough for just reading the list (as opposed to modifying it).
* We don't have to lock socket's own lock at all, the IPv4Socket::did_receive()
  implementation takes care of this.
* Most importantly, we don't have to hold the "all IPv4 sockets" across the
  IPv4Socket::did_receive() call(s). We can copy the current ICMP socket list
  while holding the lock, then release the lock, and then call
  IPv4Socket::did_receive() on all the ICMP sockets in our list.

These changes fix a deadlock triggered by receiving ICMP messages when using tap
networking setup (as opposed to QEMU's default user/SLIRP networking) on the host.
2021-02-12 15:37:28 +01:00
Andreas Kling ffa39f98e8 Kernel: Fix build with BBFS_DEBUG 2021-02-12 13:51:34 +01:00
Andreas Kling c62c00e7db Ext2FS: Make Ext2FS::GroupIndex a distinct integer type 2021-02-12 13:33:58 +01:00
Andreas Kling 489317e573 Kernel: Make BlockBasedFS::BlockIndex a distinct integer type 2021-02-12 11:59:27 +01:00
Andreas Kling e44c1792a7 Kernel: Add distinct InodeIndex type
Use the DistinctNumeric mechanism to make InodeIndex a strongly typed
integer type.
2021-02-12 10:26:29 +01:00
Andreas Kling c8a90a31b6 Kernel: Remove default arguments from Inode::resolve_as_link()
Nobody was calling it without specifying all arguments anyway.
2021-02-12 09:06:03 +01:00
Owen Smith c2de22a635 Kernel: Merge split function and data sections into one during linking
Also add an assertion to make sure the safemem sections are never
discarded by the linker.
2021-02-12 08:57:26 +01:00
Andreas Kling 8c694ed6eb Kernel: Don't call Thread::set_should_die() twice on every thread
This stops the "should already die" debug spam we've been seeing.
2021-02-11 23:33:42 +01:00
Andreas Kling 95064f8b58 Ext2FS: Convert #if EXT2_DEBUG => dbgln_if() and constexpr-if 2021-02-11 23:05:16 +01:00
Andreas Kling abe4463b1c Kernel: Remove an unnecessary InterruptDisabler in early initialization 2021-02-11 22:56:14 +01:00
Andreas Kling a280cdf9ba Ext2FS: Shrink Ext2FSDirectoryEntry from 16 to 12 bytes
The way we read/write directories is very inefficient, and this doesn't
solve any of that. It does however reduce memory usage of directory
entry vectors by 25% which has nice immediate benefits.
2021-02-11 22:45:50 +01:00
Andreas Kling cef73f2010 Kernel: Remove CMake spam when setting up KUBSAN flags 2021-02-11 22:16:28 +01:00
Andreas Kling 54986228bf Kernel: Oops, add missing #include to fix ENABLE_ALL_THE_DEBUG_MACROS 2021-02-11 22:15:55 +01:00
Andreas Kling 0dbb22e9e0 Kernel: Remove a handful of unused things in VM/ directory
Also add some missing initializers.
2021-02-11 22:02:39 +01:00
Andreas Kling ba42d741cb Kernel: Add explicit __serenity__ define to workaround CLion problem
CLion doesn't understand that we switch compilers mid-build (which I
can understand since it's a bit unusual.) Defining __serenity__ makes
the majority of IDE features work correctly in the kernel context.
2021-02-11 21:23:31 +01:00
Jean-Baptiste Boric f8c352a022 Kernel: Fix undefined signed overflow in KernelRng's RTC fallback 2021-02-11 20:58:39 +01:00
Jean-Baptiste Boric eedb6480df Kernel: Don't assert if RTC believes we're in the past 2021-02-11 20:58:39 +01:00
Hendiadyoin1 4d5496b2b2
KUBSAN: Add nearly all missing -fsanitize handlers (#5254) 2021-02-11 20:58:01 +01:00
Andreas Kling 085f80aeac Kernel: Remove unused root directory computation in Process creation
sys$fork() already takes care of children inheriting the parent's root
directory, so there was no need to do the same thing when creating a
new user process.
2021-02-09 19:18:13 +01:00
Andreas Kling 1f277f0bd9 Kernel: Convert all *Builder::appendf() => appendff() 2021-02-09 19:18:13 +01:00
Andreas Kling e8f040139b Kernel: Remove unused Thread::is_runnable_state() 2021-02-08 23:07:33 +01:00
Andreas Kling 4ff0f971f7 Kernel: Prevent execve/ptrace race
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-08 23:05:41 +01:00
Andreas Kling 4b7b92c201 Kernel: Remove two unused fields from sys$execve's LoadResult 2021-02-08 22:31:03 +01:00
Andreas Kling 4cd2c475a8 Kernel: Make the space lock a RecursiveSpinLock 2021-02-08 22:28:48 +01:00
Andreas Kling 0d7af498d7 Kernel: Move ShouldAllocateTls enum from Process to execve.cpp 2021-02-08 22:24:37 +01:00
Andreas Kling 9ca42c4c0e Kernel: Always hold space lock while calculating memory statistics
And put the locker at the top of the functions for clarity.
2021-02-08 22:23:29 +01:00
Andreas Kling 8bda30edd2 Kernel: Move memory statistics helpers from Process to Space 2021-02-08 22:23:29 +01:00
Andreas Kling b1c9f93fa3 Kernel: Skip generic region lookup in sys$futex and sys$get_stack_bounds
Just ask the process space directly instead of using the generic region
lookup that also checks for kernel regions.
2021-02-08 22:23:29 +01:00
Andreas Kling f39c2b653e Kernel: Reorganize ptrace implementation a bit
The generic parts of ptrace now live in Kernel/Syscalls/ptrace.cpp
and the i386 specific parts are moved to Arch/i386/CPU.cpp
2021-02-08 19:34:41 +01:00
Andreas Kling 45231051e6 Kernel: Set the dumpable flag before switching spaces in sys$execve() 2021-02-08 19:15:42 +01:00
Andreas Kling d746639171 Kernel: Remove outdated code to dump memory layout after exec load 2021-02-08 19:07:29 +01:00
Andreas Kling f1b5def8fd Kernel: Factor address space management out of the Process class
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-08 18:27:28 +01:00
Andreas Kling b2cba3036e Kernel: Remove unused MemoryManager::validate_range()
This is no longer used since we've switched to using the MMU to
generate EFAULT errors.
2021-02-08 18:27:28 +01:00
Andreas Kling cf5ab665e0 Kernel: Remove unused Process::for_each_thread_in_coredump() 2021-02-08 18:27:28 +01:00
AnotherTest 09a43969ba Everywhere: Replace dbgln<flag>(...) with dbgln_if(flag, ...)
Replacement made by `find Kernel Userland -name '*.h' -o -name '*.cpp' | sed -i -Ee 's/dbgln\b<(\w+)>\(/dbgln_if(\1, /g'`
2021-02-08 18:08:55 +01:00
AnotherTest 1f8a633cc7 Kernel: Make Arch/i386/CPU.cpp safe to run through clang-format
This file was far too messy, and touching it was a major pain.
Also enable clang-format linting on it.
2021-02-08 18:08:55 +01:00
AnotherTest 53ce923e10 Everywhere: Fix obvious dbgln() bugs
This will allow compiletime dbgln() checks to pass
2021-02-08 18:08:55 +01:00
Ben Wiederhake 0a2304ba05 Everywhere: Fix weird includes 2021-02-08 18:03:57 +01:00
Tom 1d843c46eb Kernel: KResultOr can use the same storage as the object for the error
Since it can only hold either an object or an error code, we can share
the same storage to hold either.
2021-02-08 18:00:38 +01:00
Tom 27a395d964 Kernel: Fix KResultOr copy-move from itself case
If move-assigning from itself we shouldn't do anything.
2021-02-07 23:02:57 +01:00
Tom b22740c08e Kernel: Use KResultOr::release_value in Process::create_kernel_thread
This should avoid an unneccessary reference bump.
2021-02-07 22:25:15 +01:00
Tom f74e31c74d Kernel: Change KResultOr::take_value to use move semantics
This may be more light weight than copying the object.
2021-02-07 22:25:15 +01:00
Andreas Kling ad42d873e5 Kernel: Remove ancient unused Scheduler::beep() declaration 2021-02-07 20:45:09 +01:00
Andreas Kling 0d8262cbab Kernel: Remove a handful of unused things from Thread 2021-02-07 20:26:53 +01:00
Andreas Kling 2ec8b4e177 Kernel: Don't allocate kernel stack twice per thread :^) 2021-02-07 20:13:51 +01:00
Andreas Kling b466ede1ea Kernel: Make sure we can allocate kernel stack before creating thread
Wrap thread creation in a Thread::try_create() helper that first
allocates a kernel stack region. If that allocation fails, we propagate
an ENOMEM error to the caller.

This avoids the situation where a thread is half-constructed, without a
valid kernel stack, and avoids having to do messy cleanup in that case.
2021-02-07 19:27:00 +01:00
Andreas Kling 5c45b0d32d Kernel: Combine Thread::backtrace() and backtrace_impl() into one 2021-02-07 19:27:00 +01:00
Andreas Kling fd3eca3acc Kernel: Add initializer for Thread::m_tss 2021-02-07 19:27:00 +01:00
Andreas Kling 5c1c82cd33 Kernel: Remove unused function Process::backtrace() 2021-02-07 19:27:00 +01:00
Andreas Kling b1813e5dae Kernel: Remove some unused declarations from Process 2021-02-07 19:27:00 +01:00
Brian Gianforcaro c95d48c8d6 Kernel: KUBSAN implementation of returns-nonnull-attribute
This didn't find anything in the current source.
2021-02-07 10:22:03 +01:00
William Bowling b97d23a71f
Kernel: Use the resolved parent path when testing create veil (#5231) 2021-02-06 19:11:44 +01:00
Andreas Kling 04ff46bff4 Kernel: And some more KUBSAN checks :^)
Here comes a few more:

* enum
* object-size
* vptr
2021-02-06 17:39:49 +01:00
Andreas Kling fad0332898 Kernel: Implement some more KUBSAN checks :^)
This patch enables the following -fsanitize sub-options:

* bounds
* bounds-strict
* integer-divide-by-zero
* return
* shift
* shift-base
* shift-exponent
2021-02-06 17:39:49 +01:00
Andreas Kling 930e3ce00d Kernel: Don't left-shift 1 (signed) 31 times
Found by KUBSAN :^)
2021-02-05 21:28:06 +01:00
Andreas Kling 4c0707e56c Kernel: Don't create a zero-length VLA in Ext2FS block list walk
Found by KUBSAN :^)
2021-02-05 21:23:11 +01:00
Andreas Kling d164f89ada Kenrel: Implement two more KUBSAN checks
This patch adds the following UndefinedBehaviorSanitizer sub-options:

* signed-integer-overflow
* vla-bound
2021-02-05 21:23:11 +01:00
Andreas Kling f4eb1f261f Kernel: Add missing initializer for SharedIRQHandler::m_enabled
Found by KUBSAN :^)
2021-02-05 21:23:11 +01:00
Andreas Kling d44be96893 Kernel: KUBSAN! (Kernel Undefined Behavior SANitizer) :^)
We now build the kernel with partial UBSAN support.
The following -fsanitize sub-options are enabled:

* nonnull-attribute
* bool

If the kernel detects UB at runtime, it will now print a debug message
with a stack trace. This is very cool! I'm leaving it on by default for
now, but we'll probably have to re-evaluate this as more options are
enabled and slowdown increases.
2021-02-05 21:23:11 +01:00
Andreas Kling e87eac9273 Userland: Add LibSystem and funnel all syscalls through it
This achieves two things:

- Programs can now intentionally perform arbitrary syscalls by calling
  syscall(). This allows us to work on things like syscall fuzzing.

- It restricts the ability of userspace to make syscalls to a single
  4KB page of code. In order to call the kernel directly, an attacker
  must now locate this page and call through it.
2021-02-05 12:23:39 +01:00
Jean-Baptiste Boric edd2362f39 Kernel: Add NE2000 network card driver
Remember, friends don't let friends use NE2000 network cards :^)
2021-02-05 09:35:02 +01:00
Liav A 865aade42b Kernel: Clear pending interrupts before enabling IRQs of IDE Channel
Calling detect_disks() can generate interrupts, so we must clear it to
allow proper function when booting with kernel argument smp=on.
2021-02-05 09:10:37 +01:00
Liav A f2faf11d61 Kernel: Try to detect Sound Blaster 16 before creating an instance
We shouldn't create a SB16 instance without checking if the Sound
Blaster 16 card is actually installed in the system.
2021-02-05 08:54:02 +01:00
Andreas Kling 54d28df97d Kernel: Make /proc/PID/stacks/TID a JSON array
The contents of these files are now raw JSON arrays. We no longer
symbolicate the addresses. That's up to userspace from now on.
2021-02-04 22:55:39 +01:00
asynts 6a00e338a8 Make it possible to overwrite debug macros locally.
Leaking macros across headers is a terrible thing, but I can't think of
a better way of achieving this.

  - We need some way of modifying debug macros from CMake to implement
    ENABLE_ALL_THE_DEBUG_MACROS.

  - We need some way of modifying debug macros in specific source files
    because otherwise we need to rebuild too many files.

This was done using the following script:

    sed -i -E 's/#cmakedefine01 ([A-Z0-9_]+)/#ifndef \1\n\0\n#endif\n/' AK/Debug.h.in
    sed -i -E 's/#cmakedefine01 ([A-Z0-9_]+)/#ifndef \1\n\0\n#endif\n/' Kernel/Debug.h.in
2021-02-04 18:26:22 +01:00
Andreas Kling c10e0adaca Kernel: Move perf event backtrace capture out of Thread class
There's no need for this to be generic and support running from an
arbitrary thread context. Perf events are always generated from within
the thread being profiled, so take advantage of that to simplify the
code. Also use Vector capacity to avoid heap allocations.
2021-02-03 11:53:05 +01:00
Andreas Kling 9c77980965 Everywhere: Remove some bitrotted "#if 0" blocks 2021-02-03 11:17:47 +01:00
Andreas Kling ac59903c89 Kernel: Don't try to symbolicate user addresses with ksyms
That's just not gonna work. :^)
2021-02-03 11:08:23 +01:00
Andreas Kling e1236dac3e Kernel: Check for off_t overflow in FileDescription::read/write
We were checking for size_t (unsigned) overflow but the current offset
is actually stored as off_t (signed). Fix this, and also fail with
EOVERFLOW correctly.
2021-02-03 10:54:35 +01:00
Andreas Kling 9f05044c50 Kernel: Check for off_t overflow before reading/writing InodeFile
Let's double-check before calling the Inode. This way we don't have to
trust every Inode subclass to validate user-supplied inputs.
2021-02-03 10:51:37 +01:00
Liav A 36f6351edc Kernel: Restore IDE PIO functionality
This change can be actually seen as two logical changes, the first
change is about to ensure we only read the ATA Status register only
once, because if we read it multiple times, we acknowledge interrupts
unintentionally. To solve this issue, we always use the alternate Status
register and only read the original status register in the IRQ handler.

The second change is how we handle interrupts - if we use DMA, we can
just complete the request and return from the IRQ handler. For PIO mode,
it's more complicated. For PIO write operation, after setting the ATA
registers, we send out the data to IO port, and wait for an interrupt.
For PIO read operation, we set the ATA registers, and wait for an
interrupt to fire, then we just read from the data IO port.
2021-02-03 10:18:16 +01:00
Andreas Kling d4dd4a82bb Kernel: Don't allow sys$msyscall() on non-mmap regions 2021-02-02 20:16:13 +01:00
Andreas Kling 823186031d Kernel: Add a way to specify which memory regions can make syscalls
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-02-02 20:13:44 +01:00
Andreas Kling d4f40241f1 Ext2FS: Avoid unnecessary parent inode lookup during inode creation
Creation of new inodes is always driven by the parent inode, so we can
just refer directly to it instead of looking up the parent by ID.
2021-02-02 18:58:26 +01:00
Andreas Kling 9e4dd834ab Ext2FS: Simplify inode creation by always starting empty
We had two ways of creating a new Ext2FS inode. Either they were empty,
or they started with some pre-allocated size.

In practice, the pre-sizing code path was only used for new directories
and it didn't actually improve anything as far as I can tell.

This patch simplifies inode creation by simply always allocating empty
inodes. Block allocation and block list generation now always happens
on the same code path.
2021-02-02 18:58:26 +01:00
Andreas Kling dbb668ddd3 Ext2FS: Propagate error codes from write_directory() 2021-02-02 18:58:26 +01:00
Andreas Kling b0b51c3955 Kernel: Limit the size of stack traces
Let's not allow infinitely long stack traces. Cap it at 4096 frames.
2021-02-02 18:58:26 +01:00
Linus Groh ee41d6e154 Base: Rename some keymaps to use xx-xx format where appropriate
- en.json -> en-us.json
- gb.json -> en-gb.json
- ptbr.json -> pt-br.json
- ptpt.json -> pt-pt.json
2021-02-02 16:53:11 +01:00
Liav A 65c27bfe52 Kernel: Set file size for smbios_entry_point and DMI blobs in ProcFS
This is needed to support dmidecode version 3.3, so it can read the 2
blobs in ProcFS.
2021-02-01 17:13:23 +01:00
Liav A a1e20aa04f Kernel: Fix enum of sysconf values to be in the correct order
This prevented from dmidecode to get the right PAGE_SIZE when using the
sysconf syscall.

I found this bug, when I tried to figure why dmidecode fails to mmap
/dev/mem when I passed --no-procfs, and the conclusion is that it tried
to mmap unaligned physical address 0xf5ae0 (SMBIOS data), and that was
caused by a wrong value returned after using the sysconf syscall to get
the plaform page size, therefore, allowing to send an unaligned address
to the mmap syscall.
2021-02-01 17:13:23 +01:00
Liav A 5ab1864497 Kernel: Introduce the MemoryDevice
This is a character device that is being used by the dmidecode utility.
We only allow to map the BIOS ROM area to userspace with this device.
2021-02-01 17:13:23 +01:00
Liav A df59b80e23 Kernel: Expose SMBIOS blobs in ProcFS 2021-02-01 17:13:23 +01:00
Ben Wiederhake cbee0c26e1 Kernel+keymap+KeyboardMapper: New pledge for getkeymap 2021-02-01 09:54:32 +01:00
Ben Wiederhake 272df54a3e Kernel+LibKeyboard: Define the default keymap only in one place
Because it was 'static const' and also shared with userland programs,
the default keymap was defined in multiple places. This commit should
save several kilobytes! :^)
2021-02-01 09:54:32 +01:00
Ben Wiederhake 0e3408d4d6 LibKeyboard: Don't assert on failure 2021-02-01 09:54:32 +01:00
Ben Wiederhake dd4e670f72 LibKeyboard+keymap: Support querying the keymap via commandline 2021-02-01 09:54:32 +01:00
Ben Wiederhake a2c21a55e1 Kernel+LibKeyboard: Enable querying the current keymap 2021-02-01 09:54:32 +01:00
Jean-Baptiste Boric b48d8d1d6d Userland: Rename PCI slot to PCI device terminology 2021-01-31 19:06:40 +01:00
Jean-Baptiste Boric 06d76a4717 Kernel: Fix PCI bridge enumeration
The enumeration code is already enumerating all buses, recursively
enumerating bridges (which are buses) makes devices on bridges being
enumerated multiple times. Also, the PCI code was incorrectly mixing up
terminology; let's settle down on bus, device and function because ever
since PCIe came along "slots" isn't really a thing anymore.
2021-01-31 19:06:40 +01:00
Andreas Kling 1320b9351e Revert "Kernel: Don't clone kernel mappings for bottom 2 MiB VM into processes"
This reverts commit da7b21dc06.

This broke SMP boot, oops! :^)
2021-01-31 19:00:53 +01:00
Andreas Kling da7b21dc06 Kernel: Don't clone kernel mappings for bottom 2 MiB VM into processes
I can't think of anything that needs these mappings anymore, so let's
get rid of them.
2021-01-31 15:20:18 +01:00
Andreas Kling 9984201634 Kernel: Use KResult a bit more in the IPv4 networking code 2021-01-31 12:13:16 +01:00
Ben Wiederhake b00799b9ce Kernel: Make /proc/self/ work again
I have no idea when it broke.

Inspired by https://www.thanassis.space/bashheimer.html
2021-01-31 12:03:14 +01:00
Andreas Kling 6e4e3a7612 Kernel: Remove pledge exception for sys$getsockopt() with SO_PEERCRED
We had an exception that allowed SOL_SOCKET + SO_PEERCRED on local
socket to support LibIPC's PID exchange mechanism. This is no longer
needed so let's just remove the exception.
2021-01-31 09:29:27 +01:00
Andreas Kling 4d777a9bf4 Kernel: Allow changing thread names with the "stdio" promise
It's useful for programs to change their thread names to say something
interesting about what they are working on. Let's not require "thread"
for this since single-threaded programs may want to do it without
pledging "thread".
2021-01-30 23:38:57 +01:00
Peter Elliott c0e88b9710 Kernel: Add FIBMAP ioctl to Ext2FileSystem
FIBMAP is a linux ioctl that gives the location on disk of a specific
block of a file
2021-01-30 22:54:51 +01:00
Andreas Kling 90343eeaeb Revert "Kernel: Return -ENOTDIR for non-directory mount target"
This reverts commit b7b09470ca.

Mounting a file on top of a file is a valid thing we support.
2021-01-30 13:52:12 +01:00
Andreas Kling 123c37e1c0 Kernel: Fix mix-up between MAP_STACK/MAP_ANONYMOUS in prot validation 2021-01-30 10:30:17 +01:00
Andreas Kling e55ef70e5e Kernel: Remove "has made executable exception for dynamic loader" flag
As Idan pointed out, this flag is actually not needed, since we don't
allow transitioning from previously-executable to writable anyway.
2021-01-30 10:06:52 +01:00
Linus Groh a3da5bc925 Meta: Expect sync-local.sh script at repository root
This used to be in Kernel/, next to the build-root-filesystem.sh script,
which was then moved to Meta/ during the transition to CMake but has the
working directory set to Build/, effectively expecting it there - which
seems silly.

TL;DR: Very confusing. Use an explicit path relative to SERENITY_ROOT
instead and update the .gitignore files.
2021-01-30 09:18:46 +01:00
Andreas Kling a8c823f242 Kernel: Bump the number of fd's that can be queued on a local socket 2021-01-29 22:11:59 +01:00
Luke 40de84ba67 Kernel/Storage: Rewrite IDE disk detection and disk access
This replaces the current disk detection and disk access code with
code based on https://wiki.osdev.org/IDE

This allows the system to boot on VirtualBox with serial debugging
enabled and VMWare Player.

I believe there were several issues with the current code:
- It didn't utilise the last 8 bits of the LBA in 24-bit mode.
- {read,write}_sectors_with_dma was not setting the obsolete bits,
  which according to OSdev wiki aren't used but should be set.
- The PIO and DMA methods were using slightly different copy
  and pasted access code, which is now put into a single
  function called "ata_access"
- PIO mode doesn't work. This doesn't fix that and should
  be looked into in the future.
- The detection code was not checking for ATA/ATAPI.
- The detection code accidentally had cyls/heads/spt as 8-bit,
  when they're 16-bit.
- The capabilities of the device were not considered. This is now
  brought in and is currently used to check if the device supports
  LBA. If not, use CHS.
2021-01-29 21:20:38 +01:00
Andreas Kling d0c5979d96 Kernel: Add "prot_exec" pledge promise and require it for PROT_EXEC
This prevents sys$mmap() and sys$mprotect() from creating executable
memory mappings in pledged programs that don't have this promise.

Note that the dynamic loader runs before pledging happens, so it's
unaffected by this.
2021-01-29 18:56:34 +01:00
Jorropo df30b3e54c
Kernel: RangeAllocator randomized correctly check if size is in bound. (#5164)
The random address proposals were not checked with the size so it was
increasely likely to try to allocate outside of available space with
larger and larger sizes.

Now they will be ignored instead of triggering a Kernel assertion
failure.

This is a continuation of: c8e7baf4b8
2021-01-29 17:18:23 +01:00
Andreas Kling 51df44534b Kernel: Disallow mapping anonymous memory as executable
This adds another layer of defense against introducing new code into a
running process. The only permitted way of doing so is by mmapping an
open file with PROT_READ | PROT_EXEC.

This does make any future JIT implementations slightly more complicated
but I think it's a worthwhile trade-off at this point. :^)
2021-01-29 14:52:34 +01:00
Andreas Kling af3d3c5c4a Kernel: Enforce W^X more strictly (like PaX MPROTECT)
This patch adds enforcement of two new rules:

- Memory that was previously writable cannot become executable
- Memory that was previously executable cannot become writable

Unfortunately we have to make an exception for text relocations in the
dynamic loader. Since those necessitate writing into a private copy
of library code, we allow programs to transition from RW to RX under
very specific conditions. See the implementation of sys$mprotect()'s
should_make_executable_exception_for_dynamic_loader() for details.
2021-01-29 14:52:27 +01:00
Andreas Kling c8e7baf4b8 Kernel: Check for alignment size overflow when allocating VM ranges
Also add some sanity check assertions that we're generating and
returning ranges contained within the RangeAllocator's total range.

Fixes #5162.
2021-01-29 12:11:42 +01:00
Linus Groh dbbc378fb2 Kernel: Return -ENOTBLK for non-block device Ext2FS mount source
When mounting an Ext2FS, a block device source is required. All other
filesystem types are unaffected, as most of them ignore the source file
descriptor anyway.

Fixes #5153.
2021-01-29 08:45:56 +01:00
Linus Groh b7b09470ca Kernel: Return -ENOTDIR for non-directory mount target
The absence of this check allowed silly things like this:

    # touch file
    # mount /dev/hda file
2021-01-29 08:45:56 +01:00
Sahan Fernando 6876b9a514 Kernel: Prevent mmap-ing as both fixed and randomized 2021-01-29 07:45:00 +01:00
Jorropo 22b0ff05d4
Kernel: sys$mmap PAGE_ROUND_UP size before calling allocate_randomized (#5154)
`allocate_randomized` assert an already sanitized size but `mmap` were
just forwarding whatever the process asked so it was possible to
trigger a kernel panic from an unpriviliged process just by asking some
randomly placed memory and a size non alligned with the page size.
This fixes this issue by rounding up to the next page size before
calling `allocate_randomized`.

Fixes #5149
2021-01-28 22:36:20 +01:00
Tom affb4ef01b Kernel: Allow specifying a physical alignment when allocating
Some drivers may require allocating contiguous physical pages with
a specific alignment for the physical address.
2021-01-28 18:52:59 +01:00
Tom d5472426ec Kernel: Retire SchedulerData and add Thread lookup table
This allows us to get rid of the thread lists in SchedulerData.
Also, instead of iterating over all threads to find a thread by id,
just use a lookup table. In the rare case of having to iterate over
all threads, just iterate the lookup table.
2021-01-28 17:35:41 +01:00
Andreas Kling 80837d43a2 Kernel: Remove outdated debug logging from RangeAllocator
If someone wants to debug this code, it's better that they rewrite the
logging code to take randomization and guard pages into account.
2021-01-28 16:23:38 +01:00
Andreas Kling b6937e2560 Kernel+LibC: Add MAP_RANDOMIZED flag for sys$mmap()
This can be used to request random VM placement instead of the highly
predictable regular mmap(nullptr, ...) VM allocation strategy.

It will soon be used to implement ASLR in the dynamic loader. :^)
2021-01-28 16:23:38 +01:00
Andreas Kling d3de138d64 Kernel: Add sanity check assertion in RangeAllocator::allocate_specific
The specific virtual address should always be page aligned.
2021-01-28 16:23:38 +01:00
Andreas Kling 27d07796b4 Kernel: Add sanity check assertion in RangeAllocator::allocate_anywhere
The requested alignment should always be a multiple of the page size.
2021-01-28 16:23:38 +01:00
Andreas Kling 5ff355c0cd Kernel: Generate coredump backtraces from "threads for coredump" list
This broke with the change that gave each process a list of its own
threads. Since threads are removed slightly earlier from that list
during process teardown, we're not able to use it for generating
coredump backtraces. Fortunately we have the "threads for coredump"
list for just this purpose. :^)
2021-01-28 08:41:18 +01:00
Andreas Kling b72f067f0d Kernel+Userland: Remove unused "effective priority" from threads
This has been merged with the regular Thread::priority field after
the recent changes to the scheduler.
2021-01-28 08:25:53 +01:00
Tom e2d7945e0c Kernel: Make KernelRng thread-safe
This adds an optional argument to get_good_random_bytes that can be
used to only return randomness if it doesn't have to block.
Also add a SpinLock around using FortunaPRNG.

Fixes #5132
2021-01-28 08:15:26 +01:00
Tom 8104abf640 Kernel: Remove colonel special-case from Process::for_each_thread
Since each Process now has its own list of threads, we don't need
to treat colonel any different anymore. This also means that it
reports all kernel threads, not just the idle threads.
2021-01-28 08:14:12 +01:00
Peter Elliott 985ce3b701 BXVGADevice+MBVGADevice: Correctly check page-aligned mmaps
In ab14b0ac64, mmap was changed so that
the size of the region is aligned before it was passed to the device
driver. The previous logic would assert when the framebuffer size was
not a multiple of the page size. I've also taken the liberty of
returning an error on mmap failure rather than asserting.
2021-01-28 08:14:04 +01:00
Andreas Kling f2decb6665 Revert "Kernel: Fix Thread::relock_process leaving critical section"
This reverts commit e9e76b8074.

This was causing a noticeable slowdown, and we're not sure that it was
actually necessary.
2021-01-27 23:23:21 +01:00
Tom db1448b21a Kernel: Add a compile-time switch to enable scheduling on all CPUs
This is meant to be temporary only and should be removed once scheduling
on all CPUs is stable.
2021-01-27 22:48:41 +01:00
Tom e9e76b8074 Kernel: Fix Thread::relock_process leaving critical section
We don't want to explicitly enable interrupts when leaving the
critical section to trigger a context switch.
2021-01-27 22:48:41 +01:00
Tom ccaeb47401 Kernel: Don't hold scheduler lock while setting up blocker in Thread::block
This fixes a deadlock when one processor is trying to block while another is
trying to unblock the same.
2021-01-27 22:48:41 +01:00
Tom 250a310454 Kernel: Release MM lock while yielding from inode page fault handler
We need to make sure other processors can grab the MM lock while we
wait, so release it when we might block. Reading the page from
disk may also block, so release it during that time as well.
2021-01-27 22:48:41 +01:00
Tom ac3927086f Kernel: Keep a list of threads per Process
This allow us to iterate only the threads of the process.
2021-01-27 22:48:41 +01:00
Tom 03a9ee79fa Kernel: Implement thread priority queues
Rather than walking all Thread instances and putting them into
a vector to be sorted by priority, queue them into priority sorted
linked lists as soon as they become ready to be executed.
2021-01-27 22:48:41 +01:00
Tom c531084873 Kernel: Track processor idle state and wake processors when waking threads
Attempt to wake idle processors to get threads to be scheduled more quickly.
We don't want to wait until the next timer tick if we have processors that
aren't doing anything.
2021-01-27 22:48:41 +01:00
Tom 39f408daa0 Kernel: Use the CPU# as logical apic id 2021-01-27 22:48:41 +01:00
Andreas Kling e67402c702 Kernel: Remove Range "valid" state and use Optional<Range> instead
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-27 21:14:42 +01:00
Andreas Kling 5ab27e4bdc Kernel: sys$mmap() without MAP_FIXED should consider address a hint
If we can't use that specific address, it's still okay to put it
anywhere else in VM.
2021-01-27 21:14:42 +01:00
Tom e2f9e557d3 Kernel: Make Processor::id a static function
This eliminates the window between calling Processor::current and
the member function where a thread could be moved to another
processor. This is generally not as big of a concern as with
Processor::current_thread, but also slightly more light weight.
2021-01-27 21:12:24 +01:00
Tom 21d288a10e Kernel: Make Thread::current smp-safe
Change Thread::current to be a static function and read using the fs
register, which eliminates a window between Processor::current()
returning and calling a function on it, which can trigger preemption
and a move to a different processor, which then causes operating
on the wrong object.
2021-01-27 21:12:24 +01:00
Tom f88a8b16d7 Kernel: Make entering and leaving critical sections atomic
We also need to store m_in_critical in the Thread upon switching,
and we need to restore it. This solves a problem where threads
moving between different processors could end up with an unexpected
value.
2021-01-27 21:12:24 +01:00
Tom 33cdc1d2f1 Kernel: Use new Thread::previous_mode to track ticks 2021-01-27 21:12:24 +01:00
Tom 0bd558081e Kernel: Track previous mode when entering/exiting traps
This allows us to determine what the previous mode (user or kernel)
was, e.g. in the timer interrupt. This is used e.g. to determine
whether a signal handler should be set up.

Fixes #5096
2021-01-27 21:12:24 +01:00
Andreas Kling 76a69be217 Kernel: Assert in RangeAllocator that sizes are multiple of PAGE_SIZE 2021-01-27 19:45:53 +01:00
asynts 7cf0c7cc0d Meta: Split debug defines into multiple headers.
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-26 21:20:00 +01:00
Maciej Zygmanowski 645657865d Kernel: Add RTC as fallback entropy source if HPET is not found 2021-01-26 21:14:44 +01:00
Maciej Zygmanowski 0a61924727 Kernel: Use HPET as entropy source if CPU doesn't support RDRAND
We don't have anything better for these CPUs for now.
2021-01-26 21:14:44 +01:00
Maciej Zygmanowski ceb5682b54 Kernel: Add HPET::read_main_counter() 2021-01-26 21:14:44 +01:00
Maciej Zygmanowski 67e93745cb Kernel: Ensure that HPET is initialized before using random the first time 2021-01-26 21:14:44 +01:00
Andreas Kling 3ff88a1d77 Kernel: Assert on attempt to map private region backed by shared inode
If we find ourselves with a user-accessible, non-shared Region backed by
a SharedInodeVMObject, that's pretty bad news, so let's just panic the
kernel instead of getting abused.

There might be a better place for this kind of check, so I've added a
FIXME about putting more thought into that.
2021-01-26 18:35:10 +01:00
Andreas Kling a131927c75 Kernel: sys$munmap() region splitting did not preserve "shared" flag
This was exploitable since the shared flag determines whether inode
permission checks are applied in sys$mprotect().

The bug was pretty hard to spot due to default arguments being used
instead. This patch removes the default arguments to make explicit
at each call site what's being done.
2021-01-26 18:35:04 +01:00
Linus Groh e7183cc762 Kernel: Don't drop pledge()'d promises/execpromises when passing nullptr
When passing nullptr for either promises or execpromises to pledge(),
the expected behaviour is to not change their current value at all - we
were accidentally resetting them to 0, effectively dropping previously
pledge()'d promises.
2021-01-26 18:18:01 +01:00
Andreas Kling c7858622ec Kernel: Update process promise states on execve() and fork()
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-26 15:26:37 +01:00
Andreas Kling 1e25d2b734 Kernel: Remove allocate_region() functions that don't take a Range
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-26 14:13:57 +01:00
Linus Groh 629180b7d8 Kernel: Support pledge() with empty promises
This tells the kernel that the process wants to use pledge, but without
pledging anything - effectively restricting it to syscalls that don't
require a certain promise. This is part of OpenBSD's pledge() as well,
which served as basis for Serenity's.
2021-01-25 23:22:21 +01:00
Tom b580c005f1 Kernel: Fix possible context switch within first context switch of a thread
We were enabling interrupts too early, before the first context switch to
a thread was complete. This could then trigger another context switch
within the context switch, which lead to a crash.
2021-01-25 23:22:12 +01:00
Tom bd73102513 Kernel: Fix some race conditions with Lock and waiting/waking threads
There is a window between acquiring/releasing the lock with the atomic
variables and subsequently waiting or waking threads. With a single
processor this window was closed by using a critical section, but
this doesn't prevent other processors from running these code paths.
To solve this, set a flag in the WaitQueue while holding m_lock which
determines if threads should be blocked at all.
2021-01-25 22:54:55 +01:00
Tom 4cf0859612 Kernel: Fix race condition in Lock::lock that may leave corrupted state 2021-01-25 22:54:55 +01:00
Andreas Kling ab14b0ac64 Kernel: Hoist VM range allocation up to sys$mmap() itself
Instead of letting each File subclass do range allocation in their
mmap() override, do it up front in sys$mmap().

This makes us honor alignment requests for file-backed memory mappings
and simplifies the code somwhat.
2021-01-25 18:57:06 +01:00
asynts 8571209560 Kernel: Fix crash when booting with ACPI. 2021-01-25 13:00:49 +01:00
asynts eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts 8465683dcf Everywhere: Debug macros instead of constexpr.
This was done with the following script:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/dbgln<debug_([a-z_]+)>/dbgln<\U\1_DEBUG>/' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/if constexpr \(debug_([a-z0-9_]+)/if constexpr \(\U\1_DEBUG/' {} \;
2021-01-25 09:47:36 +01:00
asynts acdcf59a33 Everywhere: Remove unnecessary debug comments.
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-25 09:47:36 +01:00
asynts 1a3a0836c0 Everywhere: Use CMake to generate AK/Debug.h.
This was done with the help of several scripts, I dump them here to
easily find them later:

    awk '/#ifdef/ { print "#cmakedefine01 "$2 }' AK/Debug.h.in

    for debug_macro in $(awk '/#ifdef/ { print $2 }' AK/Debug.h.in)
    do
        find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/#ifdef '$debug_macro'/#if '$debug_macro'/' {} \;
    done

    # Remember to remove WRAPPER_GERNERATOR_DEBUG from the list.
    awk '/#cmake/ { print "set("$2" ON)" }' AK/Debug.h.in
2021-01-25 09:47:36 +01:00
Andreas Kling f5d916a881 Kernel: Make sys$anon_create() fail if size == 0
An empty anonymous file is useless since it cannot be resized anyway,
so let's not support creating it.
2021-01-25 09:36:42 +01:00
Jean-Baptiste Boric 4d755725bf Kernel: Allow disabling of IDE controllers with disable_ide
The kernel doesn't like the IDE controllers on an Asus A7N8X-E Deluxe
motherboard, so add an option to disable them.
2021-01-24 22:16:18 +01:00
Jean-Baptiste Boric 7eaefa5aa6 Kernel: Make use of interrupts as an entropy source
Booting old computers without RDRAND/RDSEED and without a disk makes
the system severely starved for entropy. Uses interrupts as a source
to side-step that issue.

Also warn whenever the system is starved of entropy, because that's
a non-obvious failure mode.
2021-01-24 22:16:18 +01:00
Andreas Kling 2112b79986 Revert "Kernel: Make VFS::create() fail with EINVAL on invalid file mode"
This reverts commit ca3489eec7.

Fixes #5087.
2021-01-24 08:32:09 +01:00
Luke 50a2cb38e5 Kernel: Fix two error codes being returned as positive in Process::exec
This made the assertion on line 921 think it was a successful exec, when it wasn't.

Fixes #5084
2021-01-24 01:06:24 +01:00
Andreas Kling 8a9853d5da Kernel: Create core dumps with S_IFREG set (regular file)
Otherwise, the VFS will refuse to create the file.
2021-01-23 17:59:42 +01:00
Jean-Baptiste Boric adb2fae69c Kernel: Evaluate block conditions inside VirtualConsole::emit()
This makes text mode boot usable again, because the shell never
received keyboard input otherwise.
2021-01-23 16:53:03 +01:00
Jean-Baptiste Boric 8941d831bb Kernel: Allow "serial_debug" everywhere on the command line 2021-01-23 16:53:03 +01:00
asynts 1c1e577a5e Everywhere: Deprecate dbg(). 2021-01-23 16:46:26 +01:00
Andreas Kling d7345cf560 Kernel: Use current EUID/EGID for LocalSocket prebind credentials 2021-01-23 16:45:05 +01:00
Andreas Kling ca3489eec7 Kernel: Make VFS::create() fail with EINVAL on invalid file mode
Instead of trying to fix up the mode to look like a regular file,
just fail instead.
2021-01-23 16:45:05 +01:00
Andreas Kling bfb254ed14 Ext2FS: Assert that create_directory() is called with valid mode 2021-01-23 16:45:05 +01:00
Andreas Kling c32176db27 Kernel: Don't preserve set-uid bit in open() and bind() modes
For some reason we were keeping the bits 04777 in file modes. That
doesn't seem right and I can't think of a reason why the set-uid bit
should be allowed to slip through.
2021-01-23 16:45:05 +01:00
Andreas Kling f2ea6c3d4c Ext2FS: Don't create a directory when asked to create a socket file
(mode & S_IFDIR) is not enough to check if "mode" is a directory,
we have to check all the bits in the S_IFMT mask.

Use the is_directory() helper to fix this bug.
2021-01-23 16:45:05 +01:00
Andreas Kling 54f421e170 Kernel: Clear coredump metadata on exec()
If for some reason a process wants to exec after saving some coredump
metadata, we should just throw away the data.
2021-01-23 09:41:11 +01:00
Andreas Kling 82c879c315 Kernel: Fix PATADiskDevice device names
This broke the regular QEMU boot.
2021-01-22 22:24:43 +01:00
Jean-Baptiste Boric 666936a06b Kernel: Find boot device by enumerating devices
Since devices are enumerable and can compute their own name inside the
/dev hierarchy, there is no need to try and parse "root=/dev/xxx" by
hand.

This also makes any block device a candidate for the boot device, which
now includes ramdisk devices, so SerenityOS can now boot diskless too.
The disk image generated for QEMU is suitable, as long as it fits in
memory with room to spare for the rest of the system.
2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric f64e287b82 Kernel: Make device generate their own names
Besides removing the monolithic DevFSDeviceInode::determine_name()
method, being able to determine a device's name inside the /dev
hierarchy outside of DevFS has its uses.
2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric a2601e1308 Kernel: Hook Ramdisk devices inside DevFS 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric 225957283e Kernel: Implement RamdiskDevice 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric ec056f3bd1 Kernel: Parse boot modules from Multiboot specification 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric 3cbe805486 Kernel: Move kmalloc heaps and super pages inside .bss segment
The kernel ignored the first 8 MiB of RAM while parsing the memory map
because the kmalloc heaps and the super physical pages lived here. Move
all that stuff inside the .bss segment so that those memory regions are
accounted for, otherwise we risk overwriting boot modules placed next
to the kernel.
2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric 5cd1217b6e Kernel: Remove trace log in MemoryManager::deallocate_user_physical_page() 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric 06b73eea94 Kernel: Do not assert if unable to load kernel symbols 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric 2f8b047339 Kernel: Untangle StorageController from PCI::DeviceController 2021-01-22 22:17:39 +01:00
asynts ea7b7d8ceb Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 27bc48e06c Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 7b0a1a98d9 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts a348ab55b0 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts c6ebca5b45 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts dd727d1fec Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 67583bc424 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 78b2be5a2a Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 9d588cc9cc Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 5356aae3cc Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
Andreas Kling 2cd07c6212 Kernel+Userland: Remove "dns" pledge promise alias
This was just an alias for "unix" that I added early on back when there
was some belief that we might be compatible with OpenBSD. We're clearly
never going to be compatible with their pledges so just drop the alias.
2021-01-22 19:39:44 +01:00
Linus Groh 421587c15c Everywhere: Fix typos 2021-01-22 18:41:29 +01:00
Tom 2830ce5383 Kernel: Unsupported TTY ioctls should return EINVAL gracefully
Fixes #4971
2021-01-22 09:25:04 +01:00
Andreas Kling c9a7f81dc3 Kernel: Create new files with the current process EUID/EGID
We were using the UID/GID and not the EUID/EGID, which didn't match
other systems.
2021-01-21 11:08:16 +01:00
Andreas Kling 928ee2c791 Kernel: Don't let signals unblock threads while handling a page fault
It was possible to signal a process while it was paging in an inode
backed VM object. This would cause the inode read to EINTR, and the
page fault handler would assert.

Solve this by simply not unblocking threads due to signals if they are
currently busy handling a page fault. This is probably not the best way
to solve this issue, so I've added a FIXME to that effect.
2021-01-21 00:14:56 +01:00
Andreas Kling 1f53dd0943 Ext2FS: Propagate I/O errors from Ext2FSInode::write_bytes() 2021-01-21 00:14:56 +01:00
Andreas Kling 57ca15f126 Kernel: Remove commented-out code from Thread::dispatch_signal() 2021-01-20 23:27:23 +01:00
Andreas Kling 19d3f8cab7 Kernel+LibC: Turn errno codes into a strongly typed enum
..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-20 23:20:02 +01:00
Andreas Kling e279b45aed Kernel: Make BlockBasedFS read/write functions return a KResult
This way, if something goes wrong, we get to keep the actual error.
Also, KResults are nodiscard, so we have to deal with that in Ext2FS
instead of just silently ignoring I/O errors(!)
2021-01-20 22:57:36 +01:00
Linus Groh 678919e9c1 Kernel: Set "pledge_violation" coredump metadata in REQUIRE_PROMISE()
Similar to LibC storing an assertion message before aborting, process
death by pledge violation now sets a "pledge_violation" key with the
respective pledge name as value in its coredump metadata, which the
CrashReporter will then show.
2021-01-20 21:01:15 +01:00
Andreas Kling b7248be251 Kernel: Allow sys$chmod() to change the sticky bit
We were incorrectly masking off the sticky bit when setting file modes.
2021-01-19 20:28:13 +01:00
Andreas Kling 8601108e21 Kernel: Implement the same symlink protection as Linux
Path resolution will now refuse to follow symlinks in some cases where
you don't own the symlink, or when it's in a sticky world-writable
directory and the link has a different owner than the directory.

The point of all this is to prevent classic TOCTOU bugs in /tmp etc.

Fixes #4934
2021-01-19 20:28:09 +01:00
Andreas Kling 9681e3eca0 Kernel: Implement the same hard link protection as Linux
sys$link() will now fail to create hard links in some cases where you
don't own or have write access to the link target.

Work towards #4934
2021-01-19 20:28:04 +01:00
Jean-Baptiste Boric 6677ab1ccd Boot: Fix undefined Multiboot behaviors
Both ESP and GDTR are left undefined by the Multiboot specification and
OS images must not rely on these values to be valid. Fix the undefined
behaviors so that booting with PXELINUX does not triple-fault the CPU.
2021-01-19 09:03:37 +01:00