2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-07-11 00:25:24 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-31 15:36:49 +02:00
|
|
|
#include <AK/Badge.h>
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Function.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/HashMap.h>
|
2019-07-24 09:15:33 +02:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-05-31 15:36:49 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2021-07-11 00:46:06 +02:00
|
|
|
#include <Kernel/FileSystem/Mount.h>
|
2021-08-06 14:11:45 +02:00
|
|
|
#include <Kernel/FileSystem/UnveilNode.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2022-08-19 20:53:40 +02:00
|
|
|
#include <Kernel/Library/LockRefPtr.h>
|
2022-02-03 01:37:46 +01:00
|
|
|
#include <Kernel/Locking/SpinlockProtected.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
2020-01-21 13:34:39 +01:00
|
|
|
|
2021-08-14 19:46:18 +02:00
|
|
|
// Kernel internal options.
|
|
|
|
|
#define O_NOFOLLOW_NOERROR (1 << 29)
|
|
|
|
|
#define O_UNLINK_INTERNAL (1 << 30)
|
|
|
|
|
|
2020-01-03 20:13:21 +01:00
|
|
|
struct UidAndGid {
|
2021-08-28 22:11:16 +02:00
|
|
|
UserID uid;
|
|
|
|
|
GroupID gid;
|
2020-01-03 20:13:21 +01:00
|
|
|
};
|
|
|
|
|
|
2021-07-11 00:25:24 +02:00
|
|
|
class VirtualFileSystem {
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2021-12-21 16:11:19 +01:00
|
|
|
// Required to be at least 8 by POSIX
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
|
|
|
|
|
static constexpr int symlink_recursion_limit = 8;
|
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
static void initialize();
|
2021-07-11 00:25:24 +02:00
|
|
|
static VirtualFileSystem& the();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-07-11 00:25:24 +02:00
|
|
|
VirtualFileSystem();
|
|
|
|
|
~VirtualFileSystem();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> mount_root(FileSystem&);
|
|
|
|
|
ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags);
|
|
|
|
|
ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags);
|
|
|
|
|
ErrorOr<void> remount(Custody& mount_point, int new_flags);
|
|
|
|
|
ErrorOr<void> unmount(Inode& guest_inode);
|
|
|
|
|
|
2022-08-21 16:02:24 +02:00
|
|
|
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
|
|
|
|
|
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> create(Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
|
|
|
|
|
ErrorOr<void> mkdir(Credentials const&, StringView path, mode_t mode, Custody& base);
|
|
|
|
|
ErrorOr<void> link(Credentials const&, StringView old_path, StringView new_path, Custody& base);
|
|
|
|
|
ErrorOr<void> unlink(Credentials const&, StringView path, Custody& base);
|
|
|
|
|
ErrorOr<void> symlink(Credentials const&, StringView target, StringView linkpath, Custody& base);
|
|
|
|
|
ErrorOr<void> rmdir(Credentials const&, StringView path, Custody& base);
|
|
|
|
|
ErrorOr<void> chmod(Credentials const&, StringView path, mode_t, Custody& base, int options = 0);
|
|
|
|
|
ErrorOr<void> chmod(Credentials const&, Custody&, mode_t);
|
|
|
|
|
ErrorOr<void> chown(Credentials const&, StringView path, UserID, GroupID, Custody& base, int options);
|
|
|
|
|
ErrorOr<void> chown(Credentials const&, Custody&, UserID, GroupID);
|
|
|
|
|
ErrorOr<void> access(Credentials const&, StringView path, int mode, Custody& base);
|
|
|
|
|
ErrorOr<InodeMetadata> lookup_metadata(Credentials const&, StringView path, Custody& base, int options = 0);
|
|
|
|
|
ErrorOr<void> utime(Credentials const&, StringView path, Custody& base, time_t atime, time_t mtime);
|
|
|
|
|
ErrorOr<void> utimensat(Credentials const&, StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0);
|
|
|
|
|
ErrorOr<void> rename(Credentials const&, StringView oldpath, StringView newpath, Custody& base);
|
|
|
|
|
ErrorOr<void> mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base);
|
|
|
|
|
ErrorOr<NonnullRefPtr<Custody>> open_directory(Credentials const&, StringView path, Custody& base);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const;
|
2018-10-26 18:43:25 +02:00
|
|
|
|
2018-11-18 23:28:43 +01:00
|
|
|
InodeIdentifier root_inode_id() const;
|
|
|
|
|
|
2021-07-11 00:26:17 +02:00
|
|
|
static void sync();
|
2018-12-20 00:39:29 +01:00
|
|
|
|
2022-08-21 01:04:35 +02:00
|
|
|
NonnullRefPtr<Custody> root_custody();
|
2022-08-21 16:02:24 +02:00
|
|
|
ErrorOr<NonnullRefPtr<Custody>> resolve_path(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
|
|
|
|
|
ErrorOr<NonnullRefPtr<Custody>> resolve_path_without_veil(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
|
2019-05-30 17:46:08 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2021-09-07 13:39:11 +02:00
|
|
|
friend class OpenFileDescription;
|
2018-10-24 12:43:52 +02:00
|
|
|
|
2021-06-06 23:13:26 +02:00
|
|
|
UnveilNode const& find_matching_unveiled_path(StringView path);
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options);
|
|
|
|
|
ErrorOr<void> validate_path_against_process_veil(StringView path, int options);
|
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
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
bool is_vfs_root(InodeIdentifier) const;
|
|
|
|
|
|
2021-11-10 15:42:39 +01:00
|
|
|
ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-08-10 18:50:23 +03:00
|
|
|
bool mount_point_exists_at_inode(InodeIdentifier inode);
|
|
|
|
|
|
2022-08-21 01:04:35 +02:00
|
|
|
// FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us.
|
2018-11-15 15:10:12 +01:00
|
|
|
Mount* find_mount_for_host(InodeIdentifier);
|
|
|
|
|
Mount* find_mount_for_guest(InodeIdentifier);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Inode> m_root_inode;
|
2021-08-16 01:40:19 +02:00
|
|
|
|
2022-08-21 01:04:35 +02:00
|
|
|
SpinlockProtected<RefPtr<Custody>> m_root_custody;
|
|
|
|
|
|
|
|
|
|
SpinlockProtected<Vector<NonnullOwnPtr<Mount>, 16>> m_mounts { LockRank::None };
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|