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