Commit graph

4027 commits

Author SHA1 Message Date
Andreas Kling d677a73b0e Kernel: Move process extra_gids into protected data :^) 2021-03-10 22:30:02 +01:00
Andreas Kling cbcf891040 Kernel: Move select Process members into protected memory
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-10 22:30:02 +01:00
Andreas Kling 839d2d70a4 Kernel: Add non-const KBuffer::impl() getter 2021-03-10 22:30:02 +01:00
Andreas Kling 9b5c9efd73 Kernel: Build with -Wvla
Now that all use of VLA's (variable-length arrays) has been purged from
the kernel, let's make sure we don't reintroduce them.
2021-03-10 16:33:55 +01:00
Andreas Kling e58a600d52 Kernel: Remove VLA usage in Ext2FS block traversal code
This was using up to 12KB of kernel stack in the triply indirect case
and looks generally spooky. Let's just allocate a ByteBuffer for now
and take the performance hit (of heap allocation). Longer term we can
reorganize the code to reduce the majority of the heap churn.
2021-03-10 16:33:47 +01:00
Andreas Kling 3dbb9c8448 Kernel: Turn a VLA into a statically-sized array in dump_backtrace() 2021-03-10 16:23:11 +01:00
Andreas Kling 54f6436598 Kernel: Convert klog() => dmesgln() in TCPSocket 2021-03-09 23:06:47 +01:00
Andreas Kling b007bc07b7 Kernel: Convert klog() => dmesgln() in MemoryManager 2021-03-09 22:44:04 +01:00
Andreas Kling 232738fb7a Kernel: Use dbgln_if() and PANIC() in Thread.cpp 2021-03-09 22:35:51 +01:00
Andreas Kling c67d550df1 Kernel: Convert klog() => dmesgln() in IPv4Socket 2021-03-09 22:25:09 +01:00
Andreas Kling b12734cf13 Kernel: Convert klog() => dmesgln() in PageDirectory 2021-03-09 22:10:41 +01:00
Andreas Kling 65131334e7 Kernel: Convert klog() => dmesgln() in E1000NetworkAdapter 2021-03-09 22:10:41 +01:00
Andreas Kling c8a8923842 Kernel: Convert klog() => dmesgln() in init() 2021-03-09 22:10:41 +01:00
Andreas Kling ac1c01cc30 Kernel: Convert klog() => dmesgln() in ARP/routing code 2021-03-09 22:10:41 +01:00
Andreas Kling aef6474ea7 Kernel: Convert klog() to dmesgln() in Region 2021-03-09 22:10:41 +01:00
Andreas Kling 07564577c0 Kernel: Convert klog() => dmesgln() in AnonymousVMObject 2021-03-09 22:10:41 +01:00
Andreas Kling db0bca4153 Kernel: Convert klog() => dmesgln() in HPET 2021-03-09 22:10:41 +01:00
Andreas Kling ed8d68d8fe Kernel: Convert klog() => dmesgln() in KernelRng 2021-03-09 22:10:41 +01:00
Andreas Kling a906670d1c Kernel: Convert klog() => dmesgln() in VMWareBackdoor 2021-03-09 22:10:41 +01:00
Andreas Kling 5fd3006db2 Kernel: Convert klog() => dmesgln() in PS2MouseDevice 2021-03-09 22:10:41 +01:00
Andreas Kling 52ef08081c Kernel: Remove some unused things in kmalloc.cpp 2021-03-09 22:10:41 +01:00
Andreas Kling cb4fcaa4b5 Kernel: Convert klog() => dmesgln() in kmalloc 2021-03-09 22:10:41 +01:00
Andreas Kling 10f10abaa3 Kernel: Convert klog() => dmesgln() in filesystem code 2021-03-09 22:10:41 +01:00
Andreas Kling 2fc684f6bc Kernel: Remove ancient FIFO_DEBUG code 2021-03-09 22:10:41 +01:00
Andreas Kling 84725ef3a5 Kernel+UserspaceEmulator: Add sys$emuctl() system call
This returns ENOSYS if you are running in the real kernel, and some
other result if you are running in UserspaceEmulator.

There are other ways we could check if we're inside an emulator, but
it seemed easier to just ask. :^)
2021-03-09 08:58:26 +01:00
Brian Gianforcaro 5f6ab77352 Kernel: Add bitwise operators for Thread::FileBlocker::BlockFlags enum
Switch to using type-safe bitwise operators for the BlockFlags class,
this cleans up a lot of boilerplate casts which are necessary when the
enum is declared as `enum class`.
2021-03-08 18:47:40 +01:00
Brian Gianforcaro eaef57443c Kernel: Make MemoryManager API type-safe for Region::Access enum
Increase type-safety moving the MemoryManager APIs which take a
Region::Access to actually use that type instead of a `u8`.

Eventually the actually m_access can be moved there as well, but
I hit some weird bug where it wasn't using the correct operators
in `set_access_bit(..)` even though it's declared (and tested).
Something to fix-up later.
2021-03-08 18:47:40 +01:00
Emanuele Torre 1f81bc6879 Everywhere: Remove unnecessary whitespace at the end of some lines. 2021-03-08 09:20:53 +01:00
Luke 368f78d03c Kernel: Manually reset the XCR0 register
According to the Intel manual: "After reset, all bits (except bit 0) in
XCR0 are cleared to zero; XCR0[0] is set to 1."
Sadly we can't trust this, for example VirtualBox starts with
bits 0-4 set, so let's do it ourselves.

Fixes #5653
2021-03-08 08:56:38 +01:00
Ben Wiederhake 501952852c Kernel: Fix pointer over/underflow in create_thread
The expression
    (u8*)params.m_stack_location + stack_size
… causes UBSan to spit out the warning
    KUBSAN: addition of unsigned offset to 0x00000002 overflowed to 0xb0000003
… even though there is no actual overflow happening here.
This can be reproduced by running:
    $ syscall create_thread 0 [ 0 0 0 0 0xb0000001 2 ]
Technically, this is a true-positive: The C++-reference is incredibly strict
about pointer-arithmetic:
    > A pointer to non-array object is treated as a pointer to the first element
    > of an array with size 1. […] [A]ttempts to generate a pointer that isn't
    > pointing at an element of the same array or one past the end invoke
    > undefined behavior.
    https://en.cppreference.com/w/cpp/language/operator_arithmetic
Frankly, this feels silly. So let's just use FlatPtr instead.

Found by fuzz-syscalls. Undocumented bug.

Note that FlatPtr is an unsigned type, so
    user_esp.value() - 4
is defined even if we end up with a user_esp of 0 (this can happen for example
when params.m_stack_size = 0 and params.m_stack_location = 0). The result would
be a Kernelspace-pointer, which would then be immediately flagged by
'MM.validate_user_stack' as invalid, as intended.
2021-03-07 17:31:25 +01:00
Ben Wiederhake 00131d244e Kernel: Expose sysctl 'ubsan_is_deadly' to panic the Kernel on UB
This makes it easier to find UB, for example when fuzzing the Kernel.

This can be enabled by default, thanks to @boricj's work in
32e1354b9b.
2021-03-07 17:31:25 +01:00
Jean-Baptiste Boric 32e1354b9b Kernel: Fix unaligned read inside map_ebda() 2021-03-07 14:05:17 +01:00
Liav A b807e725e0 Kernel: Address all 32 HPET comparators correctly
Instead of declaring a reserved area from byte 0x160 to 0x400, we
change the declaration of TimerStructure array to be 32 units.

Also, a static_assert was added, to ensure that the calculation is
right.
2021-03-06 15:58:24 +01:00
Liav A bbe1d7e239 Revert "Kernel: Fix HPET timer structure layout"
This reverts commit af22204488.

According to the HPET specification, each theoretical comparator takes
32 bytes in the MMIO space.

Although I hardly believe that any system will implement all 32
comparators, in practice if a machine happens to have more than 3
comparators, we need to address the comparators correctly if we want to
use them.
2021-03-06 15:58:24 +01:00
Liav A 17e7cdc6cc Kernel: Implement ScatterList for handling scattered DMA pages
This class is used in the AHCI code to handle a big request of
read/write to the disk. If we happen to encounter such request,
we will get the needed amount of physical pages from the
already-allocated physical pages in AHCIPort, and with that we
will create a ScatterList that will create a Region that maps
all of these pages in a contiguous virtual memory range.

Then, we could easily copy to/from this range, before and after
calling the operation on the StorageDevice as needed with
read or write operations.
2021-03-05 11:29:34 +01:00
Liav A ecb169b61e Kernel: Add AnonymousVMObject constructor for a Vector of physical pages
This will be used later on by the AHCI code to create a Region
that spans over scattered DMA pages.
2021-03-05 11:29:34 +01:00
Liav A c4463cb5df Kernel: Add basic AHCI functionality
The hierarchy is AHCIController, AHCIPortHandler, AHCIPort and
SATADiskDevice. Each AHCIController has at least one AHCIPortHandler.

An AHCIPortHandler is an interrupt handler that takes care of
enumeration of handled AHCI ports when an interrupt occurs. Each
AHCIPort takes care of one SATADiskDevice, and later on we can add
support for Port multiplier.

When we implement support of Message signalled interrupts, we can spawn
many AHCIPortHandlers, and allow each one of them to be responsible for
a set of AHCIPorts.
2021-03-05 11:29:34 +01:00
Liav A b59e45e65c Kernel: Use global mechanism to determine minor number of Storage Device 2021-03-05 11:29:34 +01:00
Liav A 566c10b8b8 Kernel: Make ATA macros to be public 2021-03-05 11:29:34 +01:00
Liav A 0e9a2345f9 Kernel: Add enum values for later implementation of AHCI 2021-03-05 11:29:34 +01:00
Liav A 4f32dc2043 Kernel: Add Declaration of IDEChannel class in PATADiskDevice.h 2021-03-05 11:29:34 +01:00
Andreas Kling 50632af4de Kernel: Define a KERNEL_VIRTUAL_BASE in the linker script
Slightly nicer than saying "0xc0000000" over and over.
2021-03-04 18:25:01 +01:00
Andreas Kling adb2e6be5f Kernel: Make the kernel compile & link for x86_64
It's now possible to build the whole kernel with an x86_64 toolchain.
There's no bootstrap code so it doesn't work yet (obviously.)
2021-03-04 18:25:01 +01:00
Andreas Kling aae91dda66 Kernel: Remove repeated_{in,out}16()
These were some wrappers around x86 "insw/outsw" that are no longer
used for anything, so let's remove them.
2021-03-04 18:25:01 +01:00
Andreas Kling 2871df6f0d Kernel: Stop trying to keep InodeVMObject in sync with disk changes
As it turns out, Dr. POSIX doesn't require that post-mmap() changes
to a file are reflected in the memory mappings. So we don't actually
have to care about the file size changing (or the contents.)

IIUC, as long as all the MAP_SHARED mappings that refer to the same
inode are in sync, we're good.

This means that VMObjects don't need resizing capabilities. I'm sure
there are ways we can take advantage of this fact.
2021-03-04 15:42:51 +01:00
Andreas Kling 38c8dc22cf Kernel: Remove unused KMALLOC_DEBUG_LARGE_ALLOCATIONS mode
This was a thing back when the system was so little that any kernel
allocation above 1 MiB was basically guaranteed to be a bug. :^)
2021-03-04 11:25:45 +01:00
Andreas Kling 1208fc7d37 AK: Simplify Bitmap and implement in terms of BitmapView
Add Bitmap::view() and forward most of the calls to BitmapView since
the code was identical.

Bitmap is now primarily concerned with its dynamically allocated
backing store and BitmapView deals with the rest.
2021-03-04 11:25:45 +01:00
Andreas Kling a1d1a3b50b Kernel: Use BitmapView instead of Bitmap::wrap() 2021-03-04 11:25:45 +01:00
William McPherson 2479ead718 Everywhere: Remove unnecessary clang-format offs
Mostly due to the fact that clang-format allows aligned comments via
AlignTrailingComments.

We could also use raw string literals in inline asm, which clang-format
deals with properly (and would be nicer in a lot of places).
2021-03-04 11:01:48 +01:00
Andreas Kling 4515652001 Kernel: Remove 1 level of indirection for AnonymousVMObject CoW bitmaps
Instead of keeping AnonymousVMObject::m_cow_map in an OwnPtr<Bitmap>,
just make the Bitmap a regular value member. This increases the size
of the VMObject by 8 bytes, but removes some of the kmalloc/kfree spam
incurred by sys$fork().
2021-03-04 10:11:37 +01:00