Commit Graph

227 Commits

Author SHA1 Message Date
Lenny Maiorani
190cf1507b Kernel: Use default constructors/destructors
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 00:51:36 -07:00
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
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
210689281f Kernel: Protect mounted filesystem list with spinlock instead of mutex 2022-02-03 16:11:26 +01: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
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
circl
63760603f3 Kernel+LibC+LibCore: Add lchown and fchownat functions
This modifies sys$chown to allow specifying whether or not to follow
symlinks and in which directory.

This was then used to implement lchown and fchownat in LibC and LibCore.
2022-01-01 15:08:49 +01:00
Daniel Bertalan
726c023f9e Kernel: Propagate allocation failure in resolve_path_without_veil 2021-12-30 14:16:03 +01:00
Martin Bříza
f75bab2a25 Kernel: Move symlink recursion limit to .h, increase it to 8
As pointed out by BertalanD on Discord, POSIX specifies that
_SC_SYMLOOP_MAX (implemented in the following commit) always needs to be
equal or more than _POSIX_SYMLOOP_MAX (8, defined in
LibC/bits/posix1_lim.h), hence I've increased it to that value to
comply with the standard.

The move to header is required for the following commit - to make this
constant accessible outside of the VFS class, namely in sysconf.
2021-12-21 12:54:11 -08:00
sin-ack
69ef211925 Kernel+LibC: Move errno definitions to Kernel/API/POSIX
This fixes at least half of our LibC includes in the kernel. The source
of truth for errno codes and their description strings now lives in
Kernel/API/POSIX/errno.h as an enumeration, which LibC includes.
2021-12-16 22:21:35 +03:30
Andreas Kling
0f9ca51c76 Kernel: Remove unused Inode::preopen_fd() 2021-12-06 19:22:16 +01:00
Sam Atkins
c6a97ea843 Kernel: Mark kernel smart-pointer classes as [[nodiscard]]
And cast the unused return values to void.
2021-12-05 15:31:03 +01:00
Andreas Kling
5ce753b74d Kernel: Make Inode::traverse_as_directory() callback return ErrorOr
This allows us to propagate errors from inside the callback with TRY().
2021-11-10 21:58:58 +01:00
Andreas Kling
79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00
Ben Wiederhake
c05c5a7ff4 Kernel: Clarify ambiguous {File,Description}::absolute_path
Found due to smelly code in InodeFile::absolute_path.

In particular, this replaces the following misleading methods:

File::absolute_path
This method *never* returns an actual path, and if called on an
InodeFile (which is impossible), it would VERIFY_NOT_REACHED().

OpenFileDescription::try_serialize_absolute_path
OpenFileDescription::absolute_path
These methods do not guarantee to return an actual path (just like the
other method), and just like Custody::absolute_path they do not
guarantee accuracy. In particular, just renaming the method made a
TOCTOU bug obvious.

The new method signatures use KResultOr, just like
try_serialize_absolute_path() already did.
2021-10-31 12:06:28 +01:00
Ben Wiederhake
88ca12f037 Kernel: Enable early-returns from VFS::for_each_mount 2021-10-31 12:06:28 +01:00
Liav A
aee4786d8e Kernel: Introduce the DeviceManagement singleton
This singleton simplifies many aspects that we struggled with before:
1. There's no need to make derived classes of Device expose the
constructor as public anymore. The singleton is a friend of them, so he
can call the constructor. This solves the issue with try_create_device
helper neatly, hopefully for good.
2. Getting a reference of the NullDevice is now being done from this
singleton, which means that NullDevice no longer needs to use its own
singleton, and we can apply the try_create_device helper on it too :)
3. We can now defer registration completely after the Device constructor
which means the Device constructor is merely assigning the major and
minor numbers of the Device, and the try_create_device helper ensures it
calls the after_inserting method immediately after construction. This
creates a great opportunity to make registration more OOM-safe.
2021-09-17 01:02:48 +03:00
Ralf Donau
ec03f29fd1 Kernel/VFS: Restrict special unveil rule for Loader.so 2021-09-09 11:32:16 +02:00
Liav A
4f04cb98c1 Kernel/VFS: Silence mknod debug spam
Since we populate the DevFS now in userspace, this creates a bunch of
unnecessary noise in the kernel log.
2021-09-08 00:42:20 +02:00
Liav A
e0d712c6f7 Kernel+SystemServer: Defer creation of device nodes to userspace
Don't create these device nodes in the Kernel, so we essentially enforce
userspace (SystemServer) to take control of this operation and to decide
how to create these device nodes.

This makes the DevFS to resemble linux devtmpfs, and allows us to remove
a bunch of unneeded overriding implementations of device name creation
in the Kernel.
2021-09-08 00:42:20 +02:00
Andreas Kling
ed5d04b0ea Kernel: Use KResultOr and TRY() for FIFO 2021-09-07 13:58:16 +02:00
Andreas Kling
4a9c18afb9 Kernel: Rename FileDescription => OpenFileDescription
Dr. POSIX really calls these "open file description", not just
"file description", so let's call them exactly that. :^)
2021-09-07 13:53:14 +02:00
Andreas Kling
0d44cdb7a2 Kernel: Wrap two VirtualFileSystem directory traversals in TRY() 2021-09-06 20:30:18 +02:00
Andreas Kling
56a2594de7 Kernel: Make KString factories return KResultOr + use TRY() everywhere
There are a number of places that don't have an error propagation path
right now, so I've added FIXME's about that.
2021-09-06 19:25:36 +02:00
Andreas Kling
cda2b9e71c Kernel: Improvements to Custody absolute path serialization
- Renamed try_create_absolute_path() => try_serialize_absolute_path()
- Use KResultOr and TRY() to propagate errors
- Don't call this when it's only for debug logging
2021-09-06 13:06:05 +02:00
Andreas Kling
0cf65cf7ec Kernel: Use TRY() even more in VirtualFileSystem
Allowing TRY() with KResult unlocked a whole lot more opportunities.
2021-09-05 14:55:51 +02:00
Andreas Kling
71187d865e Kernel: Tidy up VirtualFileSystem::mount_root() a little bit
- Return KResult instead of bool
- Use TRY()
2021-09-05 14:46:44 +02:00
Andreas Kling
a5e71debef Kernel: Use TRY() in VirtualFileSystem 2021-09-05 14:08:12 +02:00
Andreas Kling
ae197deb6b Kernel: Strongly typed user & group ID's
Prior to this change, both uid_t and gid_t were typedef'ed to `u32`.
This made it easy to use them interchangeably. Let's not allow that.

This patch adds UserID and GroupID using the AK::DistinctNumeric
mechanism we've already been employing for pid_t/ProcessID.
2021-08-29 01:09:19 +02:00
Andreas Kling
59335bd8ea Kernel: Rename FileDescription::create() => try_create() 2021-08-29 01:09:19 +02:00
Idan Horowitz
cf271183b4 Kernel: Make Process::current() return a Process& instead of Process*
This has several benefits:
1) We no longer just blindly derefence a null pointer in various places
2) We will get nicer runtime error messages if the current process does
turn out to be null in the call location
3) GCC no longer complains about possible nullptr dereferences when
compiling without KUBSAN
2021-08-19 23:49:53 +02:00
Timothy Flynn
02e3633b7f AK: Move FormatParser definition from header to implementation file
This is primarily to be able to remove the GenericLexer include out of
Format.h as well. A subsequent commit will add AK::Result to
GenericLexer, which will cause naming conflicts with other structures
named Result. This can be avoided (for now) by preventing nearly every
file in the system from implicitly including GenericLexer.

Other changes in this commit are to add the GenericLexer include to
files where it is missing.
2021-08-19 23:49:25 +02:00
Andreas Kling
29a58459ab Kernel: Use ProtectedValue for VirtualFileSystem::m_mounts
This is what VirtualFileSystem::m_lock was actually guarding, and
wrapping it in a ProtectedValue makes it so much more obvious how it
all works. :^)
2021-08-16 01:41:26 +02:00
Andreas Kling
1b739a72c2 Kernel+Userland: Remove chroot functionality
We are not using this for anything and it's just been sitting there
gathering dust for well over a year, so let's stop carrying all this
complexity around for no good reason.
2021-08-15 12:44:35 +02:00
Andreas Kling
7676edfb9b Kernel: Stop allowing implicit conversion from KResult to int
This patch removes KResult::operator int() and deals with the fallout.
This forces a lot of code to be more explicit in its handling of errors,
greatly improving readability.
2021-08-14 15:19:00 +02:00
Andreas Kling
ef2720bcad Kernel: Make Inode::lookup() return a KResultOr<NonnullRefPtr<Inode>>
This allows file systems to return arbitrary error codes instead of just
an Inode or not an Inode.
2021-08-14 13:34:59 +02:00
LuK1337
e1ed1d9176 VirtualFileSystem: Return early in rename() when old_path==new_path
This change fixes disappearing symlink after trying to do the following
thing:
  cp /res/icons/16x16/add-event.png .
  ln -s add-event.png a
  mv a a
2021-08-10 21:56:10 +02:00
Andreas Kling
c94c15d45c Everywhere: Replace AK::Singleton => Singleton 2021-08-08 00:03:45 +02:00
LuK1337
3dd40535c1 VirtualFileSystem: Don't let rename() overwrite non-empty directory
According to POSIX, rename shouldn't succeed if newpath is a non-empty
directory.
2021-08-02 01:04:34 +02:00
ls
222b97488a VirtualFileSystem: Check for '.' '..' and empty filenames
This commit adds a check, to prevent empty dot or dot-dot filenames when
renaming a file and returns EINVAL in that case.
2021-07-19 18:20:34 +02:00
Andreas Kling
1f18558ee2 Kernel: Make FileSystem::root_inode() return a plain Inode&
All file system classes are expected to keep their root Inode object
in memory, so this function can safely return an Inode&.
2021-07-18 01:53:04 +02:00
Andreas Kling
9457d83986 Kernel: Rename Locker => MutexLocker 2021-07-18 01:53:04 +02:00
Andreas Kling
d1bbe8b652 Kernel: Count remaining children in VirtualFileSystem::rmdir() manually
To count the remaining children, we simply need to traverse the
directory and increment a counter. No need for a custom virtual that
all file systems have to implement. :^)
2021-07-17 22:34:43 +02:00
Max Wipfli
29d53cbee2 Kernel: Return correct error numbers for the mkdir syscall
Previously, VirtualFileSystem::mkdir() would always return ENOENT if
no parent custody was returned by resolve_path(). This is incorrect when
e.g. the user has no search permission in a component of the path
prefix (=> EACCES), or if on component of the path prefix is a file (=>
ENOTDIR). This patch fixes that behavior.
2021-07-11 14:59:57 +02:00
Andreas Kling
4238e2e9be Kernel: Only allow looking up Mounts by InodeIdentifier
Let's simplify the interface by not allowing lookup by Inode&.
2021-07-11 00:51:06 +02:00
Andreas Kling
6a27de2d94 Kernel: Make VirtualFileSystem::Mount a top-level class
And move it to its own compilation unit.
2021-07-11 00:51:06 +02:00
Andreas Kling
502bbacea0 Kernel: Rename FileBackedFS => FileBackedFileSystem 2021-07-11 00:33:27 +02:00