2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-31 15:36:49 +02:00
|
|
|
#include <AK/Badge.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-06-21 18:45:35 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2020-01-11 18:25:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-05-31 15:36:49 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2019-02-25 20:47:56 +01:00
|
|
|
#include <Kernel/KResult.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-01-21 13:14:26 +01:00
|
|
|
#define O_RDONLY 1
|
|
|
|
#define O_WRONLY 2
|
|
|
|
#define O_RDWR 3
|
2020-01-11 18:33:35 +03:00
|
|
|
#define O_EXEC 4
|
2018-11-13 01:36:31 +01:00
|
|
|
#define O_CREAT 0100
|
|
|
|
#define O_EXCL 0200
|
|
|
|
#define O_NOCTTY 0400
|
|
|
|
#define O_TRUNC 01000
|
|
|
|
#define O_APPEND 02000
|
|
|
|
#define O_NONBLOCK 04000
|
2018-10-28 14:11:51 +01:00
|
|
|
#define O_DIRECTORY 00200000
|
|
|
|
#define O_NOFOLLOW 00400000
|
2018-11-13 01:36:31 +01:00
|
|
|
#define O_CLOEXEC 02000000
|
2019-11-05 19:35:12 +01:00
|
|
|
#define O_DIRECT 04000000
|
2018-10-28 14:11:51 +01:00
|
|
|
#define O_NOFOLLOW_NOERROR 0x4000000
|
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
|
|
|
#define O_UNLINK_INTERNAL 0x8000000
|
2018-10-28 14:11:51 +01:00
|
|
|
|
2020-01-11 18:45:38 +03:00
|
|
|
#define MS_NODEV 1
|
|
|
|
#define MS_NOEXEC 2
|
|
|
|
#define MS_NOSUID 4
|
|
|
|
#define MS_BIND 8
|
|
|
|
|
2019-05-30 17:46:08 +02:00
|
|
|
class Custody;
|
2019-02-16 00:47:20 +01:00
|
|
|
class Device;
|
2019-06-07 09:36:51 +02:00
|
|
|
class FileDescription;
|
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
|
|
|
class UnveiledPath;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-01-03 20:13:21 +01:00
|
|
|
struct UidAndGid {
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
};
|
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
class VFS {
|
2018-10-31 23:19:15 +01:00
|
|
|
AK_MAKE_ETERNAL
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2018-10-26 18:43:25 +02:00
|
|
|
class Mount {
|
|
|
|
public:
|
2020-01-11 18:25:26 +03:00
|
|
|
Mount(FS&, Custody* host_custody, int flags);
|
2020-01-12 19:22:24 +03:00
|
|
|
Mount(Inode& source, Custody& host_custody, int flags);
|
2018-10-26 18:43:25 +02:00
|
|
|
|
2019-05-30 21:29:26 +02:00
|
|
|
InodeIdentifier host() const;
|
2018-10-26 18:43:25 +02:00
|
|
|
InodeIdentifier guest() const { return m_guest; }
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
const FS& guest_fs() const { return *m_guest_fs; }
|
2018-10-26 18:43:25 +02:00
|
|
|
|
2019-05-30 21:29:26 +02:00
|
|
|
String absolute_path() const;
|
|
|
|
|
2020-01-11 18:25:26 +03:00
|
|
|
int flags() const { return m_flags; }
|
|
|
|
|
2018-10-26 18:43:25 +02:00
|
|
|
private:
|
|
|
|
InodeIdentifier m_host;
|
|
|
|
InodeIdentifier m_guest;
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<FS> m_guest_fs;
|
|
|
|
RefPtr<Custody> m_host_custody;
|
2020-01-11 18:25:26 +03:00
|
|
|
int m_flags;
|
2018-10-26 18:43:25 +02:00
|
|
|
};
|
|
|
|
|
2019-07-16 13:44:41 +02:00
|
|
|
static VFS& the();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
VFS();
|
|
|
|
~VFS();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-01-11 18:05:24 +03:00
|
|
|
bool mount_root(FS&);
|
2020-01-11 18:25:26 +03:00
|
|
|
KResult mount(FS&, Custody& mount_point, int flags);
|
2020-01-12 19:22:24 +03:00
|
|
|
KResult bind_mount(Custody& source, Custody& mount_point, int flags);
|
2019-08-17 14:24:50 +02:00
|
|
|
KResult unmount(InodeIdentifier guest_inode_id);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-01-03 20:13:21 +01: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 18:58:59 +02: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);
|
2019-06-02 12:36:38 +02:00
|
|
|
KResult chmod(Inode&, mode_t);
|
2019-05-30 18:58:59 +02:00
|
|
|
KResult chown(StringView path, uid_t, gid_t, Custody& base);
|
2019-06-02 12:30:24 +02:00
|
|
|
KResult chown(Inode&, uid_t, gid_t);
|
2019-05-30 18:58:59 +02:00
|
|
|
KResult access(StringView path, int mode, Custody& base);
|
2019-08-02 19:23:23 +02:00
|
|
|
KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
|
2019-05-30 18:58:59 +02: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 18:37:47 +02:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-15 15:10:12 +01:00
|
|
|
size_t mount_count() const { return m_mounts.size(); }
|
|
|
|
void for_each_mount(Function<void(const Mount&)>) const;
|
2018-10-26 18:43:25 +02:00
|
|
|
|
2018-11-18 23:28:43 +01:00
|
|
|
InodeIdentifier root_inode_id() const;
|
|
|
|
|
2018-12-20 00:39:29 +01:00
|
|
|
void sync();
|
|
|
|
|
2019-05-30 17:46:08 +02:00
|
|
|
Custody& root_custody();
|
2020-01-15 10:52:33 +03:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, 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:
|
2019-06-07 09:36:51 +02:00
|
|
|
friend class FileDescription;
|
2018-10-24 12:43:52 +02:00
|
|
|
|
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
|
|
|
const UnveiledPath* find_matching_unveiled_path(StringView path);
|
|
|
|
KResult validate_path_against_process_veil(StringView path, int options);
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Inode> get_inode(InodeIdentifier);
|
2018-11-13 23:44:54 +01:00
|
|
|
|
2018-11-15 14:43:10 +01:00
|
|
|
bool is_vfs_root(InodeIdentifier) const;
|
|
|
|
|
2018-12-19 21:18:28 +01:00
|
|
|
void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
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
|
|
|
|
2019-08-11 23:56:39 +10:00
|
|
|
Lock m_lock { "VFSLock" };
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Inode> m_root_inode;
|
2020-01-11 18:05:24 +03:00
|
|
|
Vector<Mount> m_mounts;
|
2019-05-30 17:46:08 +02:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Custody> m_root_custody;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|