2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-07-11 01:25:24 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-31 16:36:49 +03:00
|
|
|
#include <AK/Badge.h>
|
2021-11-08 02:51:39 +03:00
|
|
|
#include <AK/Error.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <AK/Function.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-07-24 10:15:33 +03:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-05-31 16:36:49 +03:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2021-07-11 01:46:06 +03:00
|
|
|
#include <Kernel/FileSystem/Mount.h>
|
2021-08-06 15:11:45 +03:00
|
|
|
#include <Kernel/FileSystem/UnveilNode.h>
|
2021-07-11 12:49:16 +03:00
|
|
|
#include <Kernel/Forward.h>
|
2022-02-03 03:37:46 +03:00
|
|
|
#include <Kernel/Locking/SpinlockProtected.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
2020-01-21 15:34:39 +03:00
|
|
|
|
2021-08-14 20:46:18 +03:00
|
|
|
// Kernel internal options.
|
|
|
|
#define O_NOFOLLOW_NOERROR (1 << 29)
|
|
|
|
#define O_UNLINK_INTERNAL (1 << 30)
|
|
|
|
|
2020-01-03 22:13:21 +03:00
|
|
|
struct UidAndGid {
|
2021-08-28 23:11:16 +03:00
|
|
|
UserID uid;
|
|
|
|
GroupID gid;
|
2020-01-03 22:13:21 +03:00
|
|
|
};
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
class VirtualFileSystem {
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2021-12-21 18:11:19 +03: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-25 04:35:19 +03:00
|
|
|
static void initialize();
|
2021-07-11 01:25:24 +03:00
|
|
|
static VirtualFileSystem& the();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
VirtualFileSystem();
|
|
|
|
~VirtualFileSystem();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-11-08 02:51:39 +03: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);
|
|
|
|
|
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
|
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
|
|
|
|
ErrorOr<void> mkdir(StringView path, mode_t mode, Custody& base);
|
|
|
|
ErrorOr<void> link(StringView old_path, StringView new_path, Custody& base);
|
|
|
|
ErrorOr<void> unlink(StringView path, Custody& base);
|
|
|
|
ErrorOr<void> symlink(StringView target, StringView linkpath, Custody& base);
|
|
|
|
ErrorOr<void> rmdir(StringView path, Custody& base);
|
2022-01-11 18:51:34 +03:00
|
|
|
ErrorOr<void> chmod(StringView path, mode_t, Custody& base, int options = 0);
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> chmod(Custody&, mode_t);
|
2021-12-31 21:20:17 +03:00
|
|
|
ErrorOr<void> chown(StringView path, UserID, GroupID, Custody& base, int options);
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> chown(Custody&, UserID, GroupID);
|
|
|
|
ErrorOr<void> access(StringView path, int mode, Custody& base);
|
|
|
|
ErrorOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
|
|
|
|
ErrorOr<void> utime(StringView path, Custody& base, time_t atime, time_t mtime);
|
|
|
|
ErrorOr<void> rename(StringView oldpath, StringView newpath, Custody& base);
|
|
|
|
ErrorOr<void> mknod(StringView path, mode_t, dev_t, Custody& base);
|
|
|
|
ErrorOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2022-02-24 21:08:48 +03:00
|
|
|
ErrorOr<void> for_each_mount(Function<ErrorOr<void>(const Mount&)>) const;
|
2018-10-26 19:43:25 +03:00
|
|
|
|
2018-11-19 01:28:43 +03:00
|
|
|
InodeIdentifier root_inode_id() const;
|
|
|
|
|
2021-07-11 01:26:17 +03:00
|
|
|
static void sync();
|
2018-12-20 02:39:29 +03:00
|
|
|
|
2019-05-30 18:46:08 +03:00
|
|
|
Custody& root_custody();
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
|
|
|
|
ErrorOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
|
2019-05-30 18:46:08 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
2021-09-07 14:39:11 +03:00
|
|
|
friend class OpenFileDescription;
|
2018-10-24 13:43:52 +03:00
|
|
|
|
2021-06-07 00:13:26 +03:00
|
|
|
UnveilNode const& find_matching_unveiled_path(StringView path);
|
2021-11-08 02:51:39 +03: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-21 00:12:04 +03:00
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
bool is_vfs_root(InodeIdentifier) const;
|
|
|
|
|
2021-11-10 17:42:39 +03:00
|
|
|
ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
Mount* find_mount_for_host(InodeIdentifier);
|
|
|
|
Mount* find_mount_for_guest(InodeIdentifier);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Inode> m_root_inode;
|
|
|
|
RefPtr<Custody> m_root_custody;
|
2021-08-16 02:40:19 +03:00
|
|
|
|
2022-02-03 03:37:46 +03:00
|
|
|
SpinlockProtected<Vector<Mount, 16>> m_mounts;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|