Commit Graph

1124 Commits

Author SHA1 Message Date
Andreas Kling
580d89f093 Kernel: Put Process unveil state in a SpinlockProtected container
This makes path resolution safe to perform without holding the big lock.
2022-03-08 00:19:49 +01:00
Idan Horowitz
feb00b7105 Everywhere: Make JSON serialization fallible
This allows us to eliminate a major source of infallible allocation in
the Kernel, as well as lay down the groundwork for OOM fallibility in
userland.
2022-02-27 20:37:57 +01:00
Idan Horowitz
316fa0c3f3 AK+Kernel: Specialize Trie for NNOP<KString> and use it in UnveilNode
This let's us avoid the infallible String allocations.
2022-02-16 22:21:37 +01:00
Ali Mohammad Pur
a1cb2c371a AK+Kernel: OOM-harden most parts of Trie
The only part of Unveil that can't handle OOM gracefully is the
String::formatted() use in the node metadata.
2022-02-15 18:03:02 +02:00
Liav A
e23dda81de Kernel: Honor permission mode when creating new directories in DevTmpFS 2022-02-14 08:45:32 -05:00
Idan Horowitz
e37e4a7980 Kernel: Make Inode::set_shared_vmobject() OOM-fallible
Allocating a WeakPtr can fail, so this let's us properly propagate said
failure.
2022-02-14 11:35:20 +01:00
Idan Horowitz
c8ab7bde3b Kernel: Use try_make_weak_ptr() instead of make_weak_ptr() 2022-02-13 23:02:57 +01:00
Max Wipfli
b0df096298 Kernel/VFS: Add FIXMEs about error codes leaking data from veiled paths
Error codes can leak information about veiled paths, if the path
resolution fails with e.g. EACCESS.

This is non-trivial to fix, as there is a group of error codes we want
to propagate to the caller, such as ENOMEM.
2022-02-13 21:58:26 +02:00
Max Wipfli
e8f491b01d Kernel/VFS: Validate paths against process veil in mkdir()
VirtualFileSystem::mkdir() relies on resolve_path() returning an error,
since it is only interested in the out_parent passed as a pointer. Since
resolve_path_without_veil returns an error, no process veil validation
is done by resolve_path() in that case. Due to this problem, mkdir()
should use resolve_path_without_veil() and then manually validate if the
parent directory of the to-be-created directory is unveiled with 'c'
permissions.

This fixes a bug where the mkdir syscall would not respect the process
veil at all.
2022-02-13 21:58:26 +02:00
Max Wipfli
8c7010f282 Kernel/VFS: Clear out_parent if path is veiled
Previously, VirtualFileSystem::resolve_path() could return a non-null
RefPtr<Custody>* out_parent even if the function errored because the
path has been veiled.

If code relies on recieving the parent custody even if the path is
veiled, it should just call resolve_path_without_veil and do the veil
validation manually. This is because it could be that the parent is
unveiled but the child isn't or the other way round.
2022-02-13 21:58:26 +02:00
Andreas Kling
cda56f8049 Kernel: Robustify and rename Inode bound socket API
Rename the bound socket accessor from socket() to bound_socket().
Also return RefPtr<LocalSocket> instead of a raw pointer, to make it
harder for callers to mess up.
2022-02-07 13:02:34 +01:00
Andreas Kling
34f6c88ffd Revert "Kernel: Protect InodeWatcher internals with spinlock instead of mutex"
This reverts commit 0bebf013e3.

This caused a deadlock when handling a crashed process, so let's revert
it until we can figure out what went wrong.
2022-02-03 18:25:55 +01:00
Andreas Kling
e7dc9f71b8 Kernel: Protect Inode flock list with spinlock instead of mutex 2022-02-03 17:28:45 +01:00
Andreas Kling
0bebf013e3 Kernel: Protect InodeWatcher internals with spinlock instead of mutex 2022-02-03 16:11:26 +01:00
Andreas Kling
e0d9472ced Kernel: Protect Inode's list of watchers with spinlock instead of mutex 2022-02-03 16:11:26 +01:00
Andreas Kling
210689281f Kernel: Protect mounted filesystem list with spinlock instead of mutex 2022-02-03 16:11:26 +01:00
Andreas Kling
248832f438 Kernel: Convert OpenFileDescriptor from mutex to spinlock
A mutex is useful when we need to be able to block the current thread
until it's available. This is overkill for OpenFileDescriptor.

First off, this patch wraps the main state member variables inside a
SpinlockProtected<State> to enforce synchronized access. This also
avoids "free locking" where figuring out which variables are guarded
by which lock is left as an unamusing exercise for the reader.

Then we remove mutex locking from the functions that simply call through
to the underlying File or Inode, since those fields never change anyway,
and the target objects perform their own synchronization.
2022-02-03 16:11:26 +01:00
Idan Horowitz
2e5a9b4fab Kernel: Use HashCompatible HashMap lookups instead of specifying a hash 2022-01-29 23:01:23 +02:00
Idan Horowitz
a9cd8ca841 Kernel: Make Inode::register_watcher() OOM-fallible 2022-01-26 02:37:03 +02:00
Idan Horowitz
87bd930e7e Kernel: Make InodeWatcher inode registration completely OOM-fallible
InodeWatcher::register_inode was already partially fallible, but the
insertion of the inodes and watch descriptions into their respective
hash maps was not. Note that we cannot simply TRY the insertion into
both, as that could result in an inconsistent state, instead we must
remove the inode from the inode hash map if the insertion into the
watch description hash map failed.
2022-01-26 02:37:03 +02:00
Idan Horowitz
2065a73727 Kernel: Use u64 instead of size_t for BlockBasedFileSystem::read_block
This ensures offsets will not be truncated on large filesystems on i686
2022-01-25 22:41:17 +02:00
Idan Horowitz
0b2e93cf43 Kernel: Use u64 instead of size_t for BlockBasedFileSystem::write_block
This ensures offsets will not be truncated on large filesystems on i686
2022-01-25 22:41:17 +02:00
Idan Horowitz
664ca58746 Kernel: Use u64 instead of size_t for File::can_write offset
This ensures offsets will not be truncated on large files on i686.
2022-01-25 22:41:17 +02:00
Idan Horowitz
9ce537d703 Kernel: Use u64 instead of size_t for File::can_read offset
This ensures offsets will not be truncated on large files on i686.
2022-01-25 22:41:17 +02:00
Idan Horowitz
67ce9e28a5 AK: Standardize the behaviour of GenericLexer::consume_until overloads
Before this commit all consume_until overloads aside from the Predicate
one would consume (and ignore) the stop char/string, while the
Predicate overload would not, in order to keep behaviour consistent,
the other overloads no longer consume the stop char/string as well.
2022-01-25 13:41:09 +03:30
Sam Atkins
45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00
Idan Horowitz
f39e9b19c6 Kernel: Make DiskCache::ensure OOM-fallible using ErrorOr 2022-01-24 14:09:55 +02:00
David Briggs
cf45370c47 Kernel: Use ErrorOr in BlockBased and Ext2 filesystem raw read and write
These functions used to return booleans which withheld useful
error information for callers. Internally they would suppress
and convert Error objects. We now log or propagate these errors
up the stack.
2022-01-23 14:36:01 +00:00
Idan Horowitz
3c1ca61e74 Kernel: Use KString instead of String in InodeWatcher::Event's path 2022-01-21 16:27:21 +01:00
Idan Horowitz
77a81f5eed Kernel: Use KString instead of String in Ext2FSInode's lookup cache 2022-01-21 16:27:21 +01:00
Andreas Kling
cda8f34876 Kernel/TmpFS: Remove inode map from TmpFS
The HashMap of InodeIndex->Inode in TmpFS only had one purpose: looking
up parent inodes by index.

Instead of using a map for this, we can simply give each inode a WeakPtr
to its parent inode. This saves us the trouble of dealing with the
fallibility of HashMap allocations, and it just generally simpler. :^)
2022-01-14 12:15:36 +01:00
Idan Horowitz
215e031bf5 Kenrel: Use a KString for Ext2FSDirectoryEntry::name
This brings Ext2FileSystem one step closer to being OOM-safe.
2022-01-13 00:20:08 -08:00
Idan Horowitz
709fe64c1b Revert "Kernel: Use a StringView for Ext2FSDirectoryEntry::name"
This reverts commit d1d24eaef4.

I missed the fact that traverse_as_directory uses a temporary buffer,
meaning that entries created based on its callback will point to free'd
memory.
2022-01-12 21:26:03 +02:00
Idan Horowitz
e236f9d85a Kernel: Convert Inode event APIs to use StringViews instead of Strings
These APIs allocate a copy internally anyways, so there's no point to
making another one for them.
2022-01-12 16:09:09 +02:00
Idan Horowitz
d1d24eaef4 Kernel: Use a StringView for Ext2FSDirectoryEntry::name
This is a temporary struct, so there's no need to allocate a long term
storage for these strings.
2022-01-12 16:09:09 +02:00
Idan Horowitz
d2ffcfb762 Kernel: Remove the Custody::absolute_path() API
With the last user removed this non-fallible API can now be removed.
2022-01-12 16:09:09 +02:00
Idan Horowitz
4e7d307166 Kernel: Convert Mount::absolute_path to ErrorOr<NonnullOwnPtr<KString>> 2022-01-12 16:09:09 +02:00
Idan Horowitz
6be2a51b10 Kernel: Use Vector::try_append in Custody::try_serialize_absolute_path 2022-01-12 16:09:09 +02:00
Daniel Bertalan
182016d7c0 Kernel+LibC+LibCore+UE: Implement fchmodat(2)
This function is an extended version of `chmod(2)` that lets one control
whether to dereference symlinks, and specify a file descriptor to a
directory that will be used as the base for relative paths.
2022-01-12 14:54:12 +01:00
Brian Gianforcaro
f8ad3cc6a0 Kernel: Harden DevPtsFSInode::traverse_as_directory against OOM
Use the try variants of AK::StringBuilder append APIs to harden this
function against OOM.
2022-01-12 13:03:44 +01:00
Andreas Kling
08e927f084 Kernel: Synchronize removals from TmpFS inode map
Previously we were uncaching inodes from TmpFSInode::one_ref_left().
This was not safe, since one_ref_left() was effectively being called
on a raw pointer after decrementing the local ref count and observing
it become 1. There was a race here where someone else could trigger
the destructor by unreffing to 0 before one_ref_left() got called,
causing us to call one_ref_left() on a deleted inode.

We fix this by using the new remove_from_secondary_lists() mechanism
in ListedRefCounted and synchronizing all access to the TmpFS inode
map with the main Inode::all_instances() lock.

There's probably a nicer way to solve this.
2022-01-11 01:12:16 +01:00
Andreas Kling
a4b3bf1d63 Kernel: Remove empty Ext2FSInode::one_ref_left() 2022-01-11 01:12:16 +01:00
Andreas Kling
b66bb11ed1 Kernel: Remove empty ISO9660Inode::one_ref_left() 2022-01-11 01:12:16 +01:00
Andreas Kling
a7b70c62d7 Kernel: Fix broken disk cache behavior with O_DIRECT
When writing into a block via an O_DIRECT open file description, we
would first flush every dirty block *except* the one we're about to
write into.

The purpose of flushing is to ensure coherency when mixing direct and
indirect accesses to the same file. This patch fixes the issue by only
flushing the affected block.

Our behavior was totally backwards and could end up doing a lot of
unnecessary work while avoiding the work that actually mattered.
2022-01-08 20:15:25 +01:00
Andreas Kling
065f79990e Kernel: Rename DiskCache::get() to ensure()
Since this function always returns a CacheEntry& (after potentially
evicting someone else to make room), let's call it "ensure" instead of
"get" to match how we usually use these terms.
2022-01-08 19:57:33 +01:00
Andreas Kling
01823746e3 Kernel: Remove redundant disk cache dirty flag in BlockBasedFileSystem
The disk cache is dirty if the dirty list has entries. No need to track
this in a separate flag.
2022-01-08 19:38:00 +01:00
mjz19910
10ec98dd38 Everywhere: Fix spelling mistakes 2022-01-07 15:44:42 +01:00
mjz19910
3102d8e160 Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
Andreas Kling
bcc0186bbc Kernel/Ext2FS: Use HashMap::remove_all_matching() in Ext2FS
This makes the inode cache eviction mechanism quite a bit easier
to understand, thanks to the new expressive API. :^)
2022-01-05 18:57:14 +01:00
Brian Gianforcaro
4b4cf06069 Kernel: Remove temporary Vector from Ext2FS::flush_writes()
Previously we were using this vector to store the inodes as we iterated.
However, we don't need to store all of them, just the previous inode, as
we know it will be safe to remove it once we've iterated past that
element.
2022-01-05 14:04:18 +01:00