Commit Graph

354 Commits

Author SHA1 Message Date
Andreas Kling
c44b4d61f3 Kernel: Make Inode::lookup() return a RefPtr<Inode>
Previously this API would return an InodeIdentifier, which meant that
there was a race in path resolution where an inode could be unlinked
in between finding the InodeIdentifier for a path component, and
actually resolving that to an Inode object.

Attaching a test that would quickly trip an assertion before.

Test: Kernel/path-resolution-race.cpp
2020-02-01 10:56:17 +01:00
Andreas Kling
625ab1f527 Kernel: LocalSocket should fail with EADDRINUSE for already-bound files 2020-01-30 22:15:45 +01:00
Sergey Bugaev
3ffdff5c02 Kernel: Dump backtrace when denying a path because of a veil
This will make it much easier to see why a process wants to open the file.
2020-01-30 12:23:22 +01:00
Andreas Kling
bd059e32e1 Kernel: Tweak some include statements 2020-01-28 20:42:27 +01:00
Andreas Kling
23ffd6c319 Kernel+LibC+Userland: Switch to 64-bit time_t
Let's not have that 2038 problem people are talking about. :^)
2020-01-27 10:59:29 +01:00
Andreas Kling
1d506a935c Ext2FS: Give names to some KBuffers
The more we give names to KBuffers, the easier it gets to understand
what's what in a kernel region dump. :^)
2020-01-26 10:18:18 +01:00
Marios Prokopakis
da296f5865 Ext2FS: allocate_blocks allocates contiguous blocks (#1095)
This implementation uses the new helper method of Bitmap called
find_longest_range_of_unset_bits. This method looks for the biggest 
range of contiguous bits unset in the bitmap and returns the start of
the range back to the caller.
2020-01-26 09:48:24 +01:00
Andreas Kling
f309381d4e Ext2FS: Use more dbg() in Ext2FS code
We should use dbg() instead of dbgprintf() as much as possible to
protect ourselves against format string bugs. Here's a bunch of
conversions in Ext2FS.
2020-01-25 14:30:53 +01:00
Andreas Kling
b0192cfb38 Meta: Remove some copyright headers added in error 2020-01-25 10:34:32 +01:00
Sergey Bugaev
c0b32f7b76 Meta: Claim copyright for files created by me
This changes copyright holder to myself for the source code files that I've
created or have (almost) completely rewritten. Not included are the files
that were significantly changed by others even though it was me who originally
created them (think HtmlView), or the many other files I've contributed code to.
2020-01-24 15:15:16 +01:00
Andreas Kling
30ad7953ca Kernel: Rename UnveilState to VeilState 2020-01-21 19:28:59 +01:00
Andreas Kling
66598f60fe SystemMonitor: Show process unveil() state as "Veil"
A process has one of three veil states:

- None: unveil() has never been called.
- Dropped: unveil() has been called, and can be called again.
- Locked: unveil() has been called, and cannot be called again.
2020-01-21 18:56:23 +01:00
Andreas Kling
f38cfb3562 Kernel: Tidy up debug logging a little bit
When using dbg() in the kernel, the output is automatically prefixed
with [Process(PID:TID)]. This makes it a lot easier to understand which
thread is generating the output.

This patch also cleans up some common logging messages and removes the
now-unnecessary "dbg() << *current << ..." pattern.
2020-01-21 16:16:20 +01:00
Andreas Kling
07075cd001 Kernel+LibC: Clean up open() flag (O_*) definitions
These were using a mix of decimal, octal and hexadecimal for no reason.
2020-01-21 13:34:39 +01:00
Andreas Kling
6081c76515 Kernel: Make O_RDONLY non-zero
Sergey suggested that having a non-zero O_RDONLY would make some things
less confusing, and it seems like he's right about that.

We can now easily check read/write permissions separately instead of
dancing around with the bits.

This patch also fixes unveil() validation for O_RDWR which previously
forgot to check for "r" permission.
2020-01-21 13:27:08 +01:00
Andreas Kling
02406b7305 ProcFS: Add /proc/PID/unveil
This file exposes a JSON array of all the unveiled paths in a process.
2020-01-20 22:19:02 +01:00
Andreas Kling
0569123ad7 Kernel: Add a basic implementation of unveil()
This syscall is a complement to pledge() and adds the same sort of
incremental relinquishing of capabilities for filesystem access.

The first call to unveil() will "drop a veil" on the process, and from
now on, only unveiled parts of the filesystem are visible to it.

Each call to unveil() specifies a path to either a directory or a file
along with permissions for that path. The permissions are a combination
of the following:

- r: Read access (like the "rpath" promise)
- w: Write access (like the "wpath" promise)
- x: Execute access
- c: Create/remove access (like the "cpath" promise)

Attempts to open a path that has not been unveiled with fail with
ENOENT. If the unveiled path lacks sufficient permissions, it will fail
with EACCES.

Like pledge(), subsequent calls to unveil() with the same path can only
remove permissions, not add them.

Once you call unveil(nullptr, nullptr), the veil is locked, and it's no
longer possible to unveil any more paths for the process, ever.

This concept comes from OpenBSD, and their implementation does various
things differently, I'm sure. This is just a first implementation for
SerenityOS, and we'll keep improving on it as we go. :^)
2020-01-20 22:12:04 +01:00
Andreas Kling
f4f958f99f Kernel: Make DoubleBuffer use a KBuffer instead of kmalloc()ing
Background: DoubleBuffer is a handy buffer class in the kernel that
allows you to keep writing to it from the "outside" while the "inside"
reads from it. It's used for things like LocalSocket and TTY's.
Internally, it has a read buffer and a write buffer, but the two will
swap places when the read buffer is exhausted (by reading from it.)

Before this patch, it was internally implemented as two Vector<u8>
that we would swap between when the reader side had exhausted the data
in the read buffer. Now instead we preallocate a large KBuffer (64KB*2)
on DoubleBuffer construction and use that throughout its lifetime.

This removes all the kmalloc heap traffic caused by DoubleBuffers :^)
2020-01-20 16:08:49 +01:00
Andreas Kling
167b57a6b7 TmpFS: Grow the underlying inode buffer with 2x factor when written to
Before this, we would end up in memcpy() churn hell when a program was
doing repeated write() calls to a file in /tmp.

An even better solution will be to only grow the VM allocation of the
underlying buffer and keep using the same physical pages. This would
eliminate all the memcpy() work.

I've benchmarked this using g++ to compile Kernel/Process.cpp.
With these changes, compilation goes from ~35 sec to ~31 sec. :^)
2020-01-19 14:01:32 +01:00
Sergey Bugaev
d0d13e2bf5 Kernel: Move setting file flags and r/w mode to VFS::open()
Previously, VFS::open() would only use the passed flags for permission checking
purposes, and Process::sys$open() would set them on the created FileDescription
explicitly. Now, they should be set by VFS::open() on any files being opened,
including files that the kernel opens internally.

This also lets us get rid of the explicit check for whether or not the returned
FileDescription was a preopen fd, and in fact, fixes a bug where a read-only
preopen fd without any other flags would be considered freshly opened (due to
O_RDONLY being indistinguishable from 0) and granted a new set of flags.
2020-01-18 23:51:22 +01:00
Sergey Bugaev
7d4a267504 Kernel: Fix identifier casing 2020-01-18 23:51:22 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Sergey Bugaev
4417bd97d7 Kernel: Misc tweaks 2020-01-17 21:49:58 +01:00
Sergey Bugaev
68aeefa49b ProcFS: Implement symlink magic 2020-01-17 21:49:58 +01:00
Sergey Bugaev
8642a7046c Kernel: Let inodes provide pre-open file descriptions
Some magical inodes, such as /proc/pid/fd/fileno, are going to want to open() to
a custom FileDescription, so add a hook for that.
2020-01-17 21:49:58 +01:00
Sergey Bugaev
ae64fd1b27 Kernel: Let symlinks resolve themselves
Symlink resolution is now a virtual method on an inode,
Inode::resolve_as_symlink(). The default implementation just reads the stored
inode contents, treats them as a path and calls through to VFS::resolve_path().

This will let us support other, magical files that appear to be plain old
symlinks but resolve to something else. This is particularly useful for ProcFS.
2020-01-17 21:49:58 +01:00
Sergey Bugaev
d6184afcae Kernel: Simplify VFS::resolve_path() further
It turns out we don't even need to store the whole custody chain, as we only
ever access its last element. So we can just store one custody. This also fixes
a performance FIXME :^)

Also, rename parent_custody to out_parent.
2020-01-17 21:49:58 +01:00
Andreas Kling
d4d17ce423 Kernel: Trying to sys$link() a directory should fail with EPERM 2020-01-15 22:11:44 +01:00
Andreas Kling
e91f03cb39 Ext2FS: Assert that inline symlink read/write always uses offset=0 2020-01-15 22:11:44 +01:00
Andreas Kling
5a13a5416e Kernel: Avoid an extra call to read_bytes() in Inode::read_entire()
If we slurp up the entire inode in a single read_bytes(), no need to
call read_bytes() again.
2020-01-15 22:11:44 +01:00
Andreas Kling
9e54c7c17f Ext2FS: Don't allow creating new files in removed directories
Also don't uncache inodes when they reach i_links_count==0 unless they
also have no ref counts other than the +1 from the inode cache.
This prevents the FS from deleting the on-disk inode too soon.
2020-01-15 22:11:44 +01:00
Andreas Kling
e23536d682 Kernel: Use Vector::unstable_remove() in a couple of places 2020-01-15 19:26:41 +01:00
Sergey Bugaev
b913e30011 Kernel: Refactor/rewrite VFS::resolve_path()
This makes the implementation easier to follow, but also fixes multiple issues
with the old implementation. In particular, it now deals properly with . and ..
in paths, including around mount points.

Hopefully there aren't many new bugs this introduces :^)
2020-01-14 12:24:19 +01:00
Andreas Kling
0c44a12247 Kernel: read() and write() should EOVERFLOW if (offset+size) overflows 2020-01-12 20:20:17 +01:00
Andreas Kling
14d4b1058e Kernel: Add a basic lock to FileDescription
Let's prevent two processes sharing a FileDescription from messing with
it at the same time for now.
2020-01-12 20:09:44 +01:00
Sergey Bugaev
33c0dc08a7 Kernel: Don't forget to copy & destroy root_directory_for_procfs
Also, rename it to root_directory_relative_to_global_root.
2020-01-12 20:02:11 +01:00
Sergey Bugaev
fee6d0a3a6 Kernel+Base: Mount root as nodev,nosuid
Then bind-mount /dev and /bin while adding back the appropriate permissions :^)
2020-01-12 20:02:11 +01:00
Sergey Bugaev
93ff911473 Kernel: Properly propagate bind mount flags
Previously, when performing a bind mount flags other than MS_BIND were ignored.
Now, they're properly propagated the same way a for any other mount.
2020-01-12 20:02:11 +01:00
Sergey Bugaev
3393b78623 Kernel: Allow getting a Device from a FileDescription
Like we already do for other kinds of files.
2020-01-12 20:02:11 +01:00
Andreas Kling
cb59f9e0f2 Kernel: Put some VFS debug spam behind VFS_DEBUG 2020-01-12 10:01:22 +01:00
Andreas Kling
b36608f47c ProcFS: Expose process pledge promises in /proc/all 2020-01-11 21:33:12 +01:00
Sergey Bugaev
0cb0f54783 Kernel: Implement bind mounts
You can now bind-mount files and directories. This essentially exposes an
existing part of the file system in another place, and can be used as an
alternative to symlinks or hardlinks.

Here's an example of doing this:

    # mkdir /tmp/foo
    # mount /home/anon/myfile.txt /tmp/foo -o bind
    # cat /tmp/foo
    This is anon's file.
2020-01-11 18:57:53 +01:00
Sergey Bugaev
61c1106d9f Kernel+LibC: Implement a few mount flags
We now support these mount flags:
* MS_NODEV: disallow opening any devices from this file system
* MS_NOEXEC: disallow executing any executables from this file system
* MS_NOSUID: ignore set-user-id bits on executables from this file system

The fourth flag, MS_BIND, is defined, but currently ignored.
2020-01-11 18:57:53 +01:00
Sergey Bugaev
2fcbb846fb Kernel+LibC: Add O_EXEC, move exec permission checking to VFS::open()
O_EXEC is mentioned by POSIX, so let's have it. Currently, it is only used
inside the kernel to ensure the process has the right permissions when opening
an executable.
2020-01-11 18:57:53 +01:00
Sergey Bugaev
4566c2d811 Kernel+LibC: Add support for mount flags
At the moment, the actual flags are ignored, but we correctly propagate them all
the way from the original mount() syscall to each custody that resides on the
mounted FS.
2020-01-11 18:57:53 +01:00
Sergey Bugaev
1e6ab0ed22 Kernel: Simplify VFS::Mount handling
No need to pass around RefPtr<>s and NonnullRefPtr<>s and no need to
heap-allocate them.

Also remove VFS::mount(NonnullRefPtr<FS>&&, StringView path) - it has been
unused for a long time.
2020-01-11 18:57:53 +01:00
Andreas Kling
29b3d95004 Kernel: Expose a process's filesystem root as a /proc/PID/root symlink
In order to preserve the absolute path of the process root, we save the
custody used by chroot() before stripping it to become the new "/".
There's probably a better way to do this.
2020-01-10 23:48:44 +01:00
Andreas Kling
ddd0b19281 Kernel: Add a basic chroot() syscall :^)
The chroot() syscall now allows the superuser to isolate a process into
a specific subtree of the filesystem. This is not strictly permanent,
as it is also possible for a superuser to break *out* of a chroot, but
it is a useful mechanism for isolating unprivileged processes.

The VFS now uses the current process's root_directory() as the root for
path resolution purposes. The root directory is stored as an uncached
Custody in the Process object.
2020-01-10 23:14:04 +01:00
Andreas Kling
944fbf507a Kernel: Custody::absolute_path() should always return "/" for roots
A Custody with no parent is always *a* root (although not necessarily
the *real* root.)
2020-01-10 23:09:58 +01:00
Andreas Kling
b1ffde6199 Kernel: unlink() should not follow symlinks 2020-01-10 14:07:36 +01:00