2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-08-18 14:22:52 +03:00
|
|
|
#include <AK/GenericLexer.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2018-10-28 14:20:25 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2020-02-16 03:50:16 +03:00
|
|
|
#include <Kernel/Devices/BlockDevice.h>
|
2019-05-30 18:46:08 +03:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2020-04-06 11:54:21 +03:00
|
|
|
#include <Kernel/FileSystem/FileBackedFileSystem.h>
|
2019-06-07 20:29:34 +03:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2019-06-07 20:29:34 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2021-07-06 12:21:52 +03:00
|
|
|
#include <Kernel/KLexicalPath.h>
|
2020-02-16 03:27:42 +03:00
|
|
|
#include <Kernel/KSyms.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-07 22:34:11 +03:00
|
|
|
static Singleton<VirtualFileSystem> s_the;
|
2019-12-24 12:39:21 +03:00
|
|
|
static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
|
2020-05-28 18:06:13 +03:00
|
|
|
static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;
|
2018-10-18 11:27:07 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
UNMAP_AFTER_INIT void VirtualFileSystem::initialize()
|
2020-08-25 04:35:19 +03:00
|
|
|
{
|
|
|
|
s_the.ensure_instance();
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
VirtualFileSystem& VirtualFileSystem::the()
|
2018-10-18 11:27:07 +03:00
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
UNMAP_AFTER_INIT VirtualFileSystem::VirtualFileSystem()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
UNMAP_AFTER_INIT VirtualFileSystem::~VirtualFileSystem()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
InodeIdentifier VirtualFileSystem::root_inode_id() const
|
2018-11-19 01:28:43 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_root_inode);
|
2019-01-16 14:57:07 +03:00
|
|
|
return m_root_inode->identifier();
|
2018-11-19 01:28:43 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags)
|
2019-08-02 20:03:50 +03:00
|
|
|
{
|
2021-08-16 02:40:19 +03:00
|
|
|
return m_mounts.with_exclusive([&](auto& mounts) -> KResult {
|
|
|
|
auto& inode = mount_point.inode();
|
2021-09-06 13:24:36 +03:00
|
|
|
dbgln("VirtualFileSystem: Mounting {} at inode {} with flags {}",
|
2021-08-16 02:40:19 +03:00
|
|
|
fs.class_name(),
|
|
|
|
inode.identifier(),
|
|
|
|
flags);
|
|
|
|
// FIXME: check that this is not already a mount point
|
|
|
|
Mount mount { fs, &mount_point, flags };
|
|
|
|
mounts.append(move(mount));
|
|
|
|
return KSuccess;
|
|
|
|
});
|
2019-08-02 20:03:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::bind_mount(Custody& source, Custody& mount_point, int flags)
|
2020-01-11 19:08:35 +03:00
|
|
|
{
|
2021-08-16 02:40:19 +03:00
|
|
|
return m_mounts.with_exclusive([&](auto& mounts) -> KResult {
|
2021-09-06 13:24:36 +03:00
|
|
|
dbgln("VirtualFileSystem: Bind-mounting inode {} at inode {}", source.inode().identifier(), mount_point.inode().identifier());
|
2021-08-16 02:40:19 +03:00
|
|
|
// FIXME: check that this is not already a mount point
|
|
|
|
Mount mount { source.inode(), mount_point, flags };
|
|
|
|
mounts.append(move(mount));
|
|
|
|
return KSuccess;
|
|
|
|
});
|
2020-01-11 19:08:35 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::remount(Custody& mount_point, int new_flags)
|
2020-05-28 21:12:13 +03:00
|
|
|
{
|
2021-09-06 13:24:36 +03:00
|
|
|
dbgln("VirtualFileSystem: Remounting inode {}", mount_point.inode().identifier());
|
2020-05-28 21:12:13 +03:00
|
|
|
|
2021-08-16 02:40:19 +03:00
|
|
|
auto* mount = find_mount_for_guest(mount_point.inode().identifier());
|
2020-05-28 21:12:13 +03:00
|
|
|
if (!mount)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENODEV;
|
2020-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
mount->set_flags(new_flags);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::unmount(Inode& guest_inode)
|
2019-08-11 16:56:39 +03:00
|
|
|
{
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln("VirtualFileSystem: unmount called with inode {}", guest_inode.identifier());
|
2019-08-17 15:24:50 +03:00
|
|
|
|
2021-08-16 02:40:19 +03:00
|
|
|
return m_mounts.with_exclusive([&](auto& mounts) -> KResult {
|
|
|
|
for (size_t i = 0; i < mounts.size(); ++i) {
|
|
|
|
auto& mount = mounts[i];
|
|
|
|
if (&mount.guest() != &guest_inode)
|
|
|
|
continue;
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(mount.guest_fs().prepare_to_unmount());
|
2021-08-16 02:40:19 +03:00
|
|
|
dbgln("VirtualFileSystem: Unmounting file system {}...", mount.guest_fs().fsid());
|
|
|
|
mounts.unstable_take(i);
|
2019-08-11 16:56:39 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
2021-08-16 02:40:19 +03:00
|
|
|
dbgln("VirtualFileSystem: Nothing mounted on inode {}", guest_inode.identifier());
|
|
|
|
return ENODEV;
|
|
|
|
});
|
2019-08-11 16:56:39 +03:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:46:44 +03:00
|
|
|
KResult VirtualFileSystem::mount_root(FileSystem& fs)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (m_root_inode) {
|
2021-07-11 01:25:24 +03:00
|
|
|
dmesgln("VirtualFileSystem: mount_root can't mount another root");
|
2021-09-05 15:46:44 +03:00
|
|
|
return EEXIST;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:20:38 +03:00
|
|
|
Mount mount { fs, nullptr, root_mount_flags };
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-07-18 02:50:47 +03:00
|
|
|
auto& root_inode = fs.root_inode();
|
|
|
|
if (!root_inode.is_directory()) {
|
|
|
|
dmesgln("VirtualFileSystem: root inode ({}) for / is not a directory :(", root_inode.identifier());
|
2021-09-05 15:46:44 +03:00
|
|
|
return ENOTDIR;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-18 02:50:47 +03:00
|
|
|
m_root_inode = root_inode;
|
2021-07-11 01:33:27 +03:00
|
|
|
dmesgln("VirtualFileSystem: mounted root from {} ({})", fs.class_name(), static_cast<FileBackedFileSystem&>(fs).file_description().absolute_path());
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-08-16 02:40:19 +03:00
|
|
|
m_mounts.with_exclusive([&](auto& mounts) {
|
|
|
|
mounts.append(move(mount));
|
|
|
|
});
|
2021-05-10 10:28:23 +03:00
|
|
|
|
2021-09-05 15:46:44 +03:00
|
|
|
m_root_custody = TRY(Custody::try_create(nullptr, "", *m_root_inode, root_mount_flags));
|
|
|
|
return KSuccess;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
auto VirtualFileSystem::find_mount_for_host(InodeIdentifier id) -> Mount*
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-08-16 02:40:19 +03:00
|
|
|
return m_mounts.with_exclusive([&](auto& mounts) -> Mount* {
|
|
|
|
for (auto& mount : mounts) {
|
|
|
|
if (mount.host() && mount.host()->identifier() == id)
|
|
|
|
return &mount;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2020-06-25 00:16:24 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
auto VirtualFileSystem::find_mount_for_guest(InodeIdentifier id) -> Mount*
|
2020-06-25 00:16:24 +03:00
|
|
|
{
|
2021-08-16 02:40:19 +03:00
|
|
|
return m_mounts.with_exclusive([&](auto& mounts) -> Mount* {
|
|
|
|
for (auto& mount : mounts) {
|
|
|
|
if (mount.guest().identifier() == id)
|
|
|
|
return &mount;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
bool VirtualFileSystem::is_vfs_root(InodeIdentifier inode) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
return inode == root_inode_id();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::traverse_directory_inode(Inode& dir_inode, Function<bool(FileSystem::DirectoryEntryView const&)> callback)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-18 13:41:27 +03:00
|
|
|
return dir_inode.traverse_as_directory([&](auto& entry) {
|
2019-01-31 19:31:23 +03:00
|
|
|
InodeIdentifier resolved_inode;
|
2018-11-15 17:10:12 +03:00
|
|
|
if (auto mount = find_mount_for_host(entry.inode))
|
2020-06-25 00:16:24 +03:00
|
|
|
resolved_inode = mount->guest().identifier();
|
2018-10-10 12:53:07 +03:00
|
|
|
else
|
2019-01-31 19:31:23 +03:00
|
|
|
resolved_inode = entry.inode;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-01-15 14:06:48 +03:00
|
|
|
// FIXME: This is now broken considering chroot and bind mounts.
|
2021-07-18 02:50:47 +03:00
|
|
|
bool is_root_inode = dir_inode.identifier() == dir_inode.fs().root_inode().identifier();
|
2020-08-18 13:41:27 +03:00
|
|
|
if (is_root_inode && !is_vfs_root(dir_inode.identifier()) && entry.name == "..") {
|
2021-07-11 01:50:08 +03:00
|
|
|
auto mount = find_mount_for_guest(dir_inode.identifier());
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(mount);
|
|
|
|
VERIFY(mount->host());
|
2020-06-25 00:16:24 +03:00
|
|
|
resolved_inode = mount->host()->identifier();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
2020-08-18 13:41:27 +03:00
|
|
|
callback({ entry.name, resolved_inode, entry.file_type });
|
2018-10-10 12:53:07 +03:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::utime(StringView path, Custody& base, time_t atime, time_t mtime)
|
2019-02-21 18:37:41 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base));
|
|
|
|
auto& inode = custody->inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!current_process.is_superuser() && inode.metadata().uid != current_process.euid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2021-09-05 15:00:18 +03:00
|
|
|
if (custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-03-07 00:14:31 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(inode.set_atime(atime));
|
|
|
|
TRY(inode.set_mtime(mtime));
|
2019-02-25 22:47:56 +03:00
|
|
|
return KSuccess;
|
2019-02-21 18:37:41 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResultOr<InodeMetadata> VirtualFileSystem::lookup_metadata(StringView path, Custody& base, int options)
|
2019-02-21 18:09:12 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base, nullptr, options));
|
|
|
|
return custody->inode().metadata();
|
2019-02-21 18:09:12 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
KResultOr<NonnullRefPtr<OpenFileDescription>> VirtualFileSystem::open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> owner)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-01-03 04:23:50 +03:00
|
|
|
if ((options & O_CREAT) && (options & O_DIRECTORY))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-01-03 04:23:50 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-06-09 20:52:03 +03:00
|
|
|
auto custody_or_error = resolve_path(path, base, &parent_custody, options);
|
2021-05-19 12:33:23 +03:00
|
|
|
if (custody_or_error.is_error()) {
|
|
|
|
// NOTE: ENOENT with a non-null parent custody signals us that the immediate parent
|
|
|
|
// of the file exists, but the file itself does not.
|
2021-08-14 16:15:11 +03:00
|
|
|
if ((options & O_CREAT) && custody_or_error.error() == ENOENT && parent_custody)
|
2020-01-03 22:13:21 +03:00
|
|
|
return create(path, options, mode, *parent_custody, move(owner));
|
2019-05-30 19:58:59 +03:00
|
|
|
return custody_or_error.error();
|
2021-05-19 12:33:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((options & O_CREAT) && (options & O_EXCL))
|
|
|
|
return EEXIST;
|
2019-02-21 18:09:12 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
auto metadata = inode.metadata();
|
2019-03-27 18:42:30 +03:00
|
|
|
|
2020-01-03 04:23:11 +03:00
|
|
|
if ((options & O_DIRECTORY) && !metadata.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTDIR;
|
2020-01-03 04:23:11 +03:00
|
|
|
|
2019-03-27 18:42:30 +03:00
|
|
|
bool should_truncate_file = false;
|
2019-02-21 18:09:12 +03:00
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if ((options & O_RDONLY) && !metadata.may_read(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-01-21 15:14:26 +03:00
|
|
|
|
|
|
|
if (options & O_WRONLY) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!metadata.may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-03-07 00:14:31 +03:00
|
|
|
if (metadata.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EISDIR;
|
2019-03-27 18:42:30 +03:00
|
|
|
should_truncate_file = options & O_TRUNC;
|
2019-02-21 17:45:31 +03:00
|
|
|
}
|
2020-01-11 18:33:35 +03:00
|
|
|
if (options & O_EXEC) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!metadata.may_execute(current_process) || (custody.mount_flags() & MS_NOEXEC))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-01-11 18:33:35 +03:00
|
|
|
}
|
2019-02-21 18:09:12 +03:00
|
|
|
|
2020-01-15 14:03:14 +03:00
|
|
|
if (auto preopen_fd = inode.preopen_fd())
|
|
|
|
return *preopen_fd;
|
|
|
|
|
2020-07-17 00:23:03 +03:00
|
|
|
if (metadata.is_fifo()) {
|
2021-09-07 14:56:10 +03:00
|
|
|
auto fifo = TRY(inode.fifo());
|
2020-07-17 00:23:03 +03:00
|
|
|
if (options & O_WRONLY) {
|
2021-09-05 15:55:25 +03:00
|
|
|
auto description = TRY(fifo->open_direction_blocking(FIFO::Direction::Writer));
|
2020-07-17 00:23:03 +03:00
|
|
|
description->set_rw_mode(options);
|
|
|
|
description->set_file_flags(options);
|
|
|
|
description->set_original_inode({}, inode);
|
|
|
|
return description;
|
|
|
|
} else if (options & O_RDONLY) {
|
2021-09-05 15:55:25 +03:00
|
|
|
auto description = TRY(fifo->open_direction_blocking(FIFO::Direction::Reader));
|
2020-07-17 00:23:03 +03:00
|
|
|
description->set_rw_mode(options);
|
|
|
|
description->set_file_flags(options);
|
|
|
|
description->set_original_inode({}, inode);
|
|
|
|
return description;
|
|
|
|
}
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-07-17 00:23:03 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:09:12 +03:00
|
|
|
if (metadata.is_device()) {
|
2020-01-11 18:45:38 +03:00
|
|
|
if (custody.mount_flags() & MS_NODEV)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-08-18 14:48:15 +03:00
|
|
|
auto device = Device::get_device(metadata.major_device, metadata.minor_device);
|
|
|
|
if (device == nullptr) {
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENODEV;
|
2019-01-16 14:57:07 +03:00
|
|
|
}
|
2021-09-05 15:00:18 +03:00
|
|
|
auto description = TRY(device->open(options));
|
|
|
|
description->set_original_inode({}, inode);
|
2021-08-14 05:04:56 +03:00
|
|
|
description->set_original_custody({}, custody);
|
2021-09-05 15:00:18 +03:00
|
|
|
return description;
|
2019-01-16 14:57:07 +03:00
|
|
|
}
|
2020-05-28 17:56:25 +03:00
|
|
|
|
|
|
|
// Check for read-only FS. Do this after handling preopen FD and devices,
|
|
|
|
// but before modifying the inode in any way.
|
|
|
|
if ((options & O_WRONLY) && custody.is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2020-01-08 15:57:22 +03:00
|
|
|
if (should_truncate_file) {
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(inode.truncate(0));
|
|
|
|
TRY(inode.set_mtime(kgettimeofday().to_truncated_seconds()));
|
2020-09-17 22:51:09 +03:00
|
|
|
}
|
2021-09-07 14:39:11 +03:00
|
|
|
auto description = TRY(OpenFileDescription::try_create(custody));
|
2021-09-05 15:55:25 +03:00
|
|
|
description->set_rw_mode(options);
|
|
|
|
description->set_file_flags(options);
|
2020-01-19 01:15:52 +03:00
|
|
|
return description;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
|
2019-05-03 23:59:58 +03:00
|
|
|
{
|
|
|
|
if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-05-03 23:59:58 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 16:30:09 +03:00
|
|
|
auto existing_file_or_error = resolve_path(path, base, &parent_custody);
|
2019-05-03 23:59:58 +03:00
|
|
|
if (!existing_file_or_error.is_error())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EEXIST;
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!parent_custody)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOENT;
|
2021-08-14 16:15:11 +03:00
|
|
|
if (existing_file_or_error.error() != ENOENT)
|
2019-05-03 23:59:58 +03:00
|
|
|
return existing_file_or_error.error();
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-05-03 23:59:58 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
auto basename = KLexicalPath::basename(path);
|
2021-08-14 07:11:30 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::mknod: '{}' mode={} dev={} in {}", basename, mode, dev, parent_inode.identifier());
|
2021-08-19 22:45:07 +03:00
|
|
|
return parent_inode.create_child(basename, mode, dev, current_process.euid(), current_process.egid()).result();
|
2019-05-03 23:59:58 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
KResultOr<NonnullRefPtr<OpenFileDescription>> VirtualFileSystem::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-07-06 12:21:52 +03:00
|
|
|
auto basename = KLexicalPath::basename(path);
|
2021-09-06 13:24:36 +03:00
|
|
|
auto parent_path = TRY(parent_custody.try_serialize_absolute_path());
|
2021-09-06 20:24:54 +03:00
|
|
|
auto full_path = TRY(KLexicalPath::try_join(parent_path->view(), basename));
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(validate_path_against_process_veil(full_path->view(), options));
|
2020-04-04 17:40:36 +03:00
|
|
|
|
2021-01-24 10:31:18 +03:00
|
|
|
if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
|
|
|
|
// Turn it into a regular file. (This feels rather hackish.)
|
|
|
|
mode |= 0100000;
|
|
|
|
}
|
2019-01-23 06:29:56 +03:00
|
|
|
|
2019-06-09 20:52:03 +03:00
|
|
|
auto& parent_inode = parent_custody.inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody.is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::create: '{}' in {}", basename, parent_inode.identifier());
|
2021-08-28 23:11:16 +03:00
|
|
|
auto uid = owner.has_value() ? owner.value().uid : current_process.euid();
|
|
|
|
auto gid = owner.has_value() ? owner.value().gid : current_process.egid();
|
2021-09-05 15:00:18 +03:00
|
|
|
|
|
|
|
auto inode = TRY(parent_inode.create_child(basename, mode, 0, uid, gid));
|
|
|
|
auto custody = TRY(Custody::try_create(&parent_custody, basename, inode, parent_custody.mount_flags()));
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
auto description = TRY(OpenFileDescription::try_create(move(custody)));
|
2021-09-05 15:55:25 +03:00
|
|
|
description->set_rw_mode(options);
|
|
|
|
description->set_file_flags(options);
|
2020-01-19 01:15:52 +03:00
|
|
|
return description;
|
2018-10-16 01:35:03 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::mkdir(StringView path, mode_t mode, Custody& base)
|
2018-10-16 01:35:03 +03:00
|
|
|
{
|
2020-02-20 17:28:36 +03:00
|
|
|
// Unlike in basically every other case, where it's only the last
|
|
|
|
// path component (the one being created) that is allowed not to
|
|
|
|
// exist, POSIX allows mkdir'ed path to have trailing slashes.
|
|
|
|
// Let's handle that case by trimming any trailing slashes.
|
2021-07-11 14:46:05 +03:00
|
|
|
path = path.trim("/"sv, TrimMode::Right);
|
|
|
|
if (path.is_empty()) {
|
|
|
|
// NOTE: This means the path was a series of slashes, which resolves to "/".
|
|
|
|
path = "/";
|
|
|
|
}
|
2020-02-20 17:28:36 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2021-07-11 14:46:05 +03:00
|
|
|
auto result = resolve_path(path, base, &parent_custody);
|
|
|
|
if (!result.is_error())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EEXIST;
|
2021-04-11 01:40:38 +03:00
|
|
|
else if (!parent_custody)
|
2019-02-25 22:47:56 +03:00
|
|
|
return result.error();
|
2021-07-11 14:46:05 +03:00
|
|
|
// NOTE: If resolve_path fails with a non-null parent custody, the error should be ENOENT.
|
2021-08-14 16:15:11 +03:00
|
|
|
VERIFY(result.error() == ENOENT);
|
2019-02-25 22:47:56 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-02-21 17:45:31 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
auto basename = KLexicalPath::basename(path);
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::mkdir: '{}' in {}", basename, parent_inode.identifier());
|
2021-08-19 22:45:07 +03:00
|
|
|
return parent_inode.create_child(basename, S_IFDIR | mode, 0, current_process.euid(), current_process.egid()).result();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::access(StringView path, int mode, Custody& base)
|
2019-02-26 17:57:59 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base));
|
|
|
|
|
|
|
|
auto& inode = custody->inode();
|
2019-05-30 19:58:59 +03:00
|
|
|
auto metadata = inode.metadata();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
2019-02-26 17:57:59 +03:00
|
|
|
if (mode & R_OK) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!metadata.may_read(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-02-26 17:57:59 +03:00
|
|
|
}
|
|
|
|
if (mode & W_OK) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!metadata.may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2021-09-05 15:00:18 +03:00
|
|
|
if (custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-02-26 17:57:59 +03:00
|
|
|
}
|
|
|
|
if (mode & X_OK) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!metadata.may_execute(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-02-26 17:57:59 +03:00
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> VirtualFileSystem::open_directory(StringView path, Custody& base)
|
2019-03-02 01:54:07 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base));
|
|
|
|
auto& inode = custody->inode();
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!inode.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTDIR;
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!inode.metadata().may_execute(Process::current()))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-05-30 19:58:59 +03:00
|
|
|
return custody;
|
2019-03-02 01:54:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::chmod(Custody& custody, mode_t mode)
|
2019-01-29 06:55:08 +03:00
|
|
|
{
|
2020-05-28 17:41:04 +03:00
|
|
|
auto& inode = custody.inode();
|
2019-02-21 17:45:31 +03:00
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (current_process.euid() != inode.metadata().uid && !current_process.is_superuser())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2020-05-28 17:56:25 +03:00
|
|
|
if (custody.is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-01-29 06:55:08 +03:00
|
|
|
|
|
|
|
// Only change the permission bits.
|
2021-01-19 20:21:43 +03:00
|
|
|
mode = (inode.mode() & ~07777u) | (mode & 07777u);
|
2019-03-01 12:39:19 +03:00
|
|
|
return inode.chmod(mode);
|
|
|
|
}
|
2019-01-29 06:55:08 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::chmod(StringView path, mode_t mode, Custody& base)
|
2019-03-01 12:39:19 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base));
|
2020-05-28 17:41:04 +03:00
|
|
|
return chmod(custody, mode);
|
2019-02-25 22:47:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::rename(StringView old_path, StringView new_path, Custody& base)
|
2019-04-08 00:35:26 +03:00
|
|
|
{
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> old_parent_custody;
|
2021-09-05 15:00:18 +03:00
|
|
|
auto old_custody = TRY(resolve_path(old_path, base, &old_parent_custody, O_NOFOLLOW_NOERROR));
|
|
|
|
auto& old_inode = old_custody->inode();
|
2019-04-08 00:35:26 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> new_parent_custody;
|
2019-05-31 16:30:09 +03:00
|
|
|
auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
|
2019-05-30 19:58:59 +03:00
|
|
|
if (new_custody_or_error.is_error()) {
|
2021-08-14 16:15:11 +03:00
|
|
|
if (new_custody_or_error.error() != ENOENT || !new_parent_custody)
|
2019-05-30 19:58:59 +03:00
|
|
|
return new_custody_or_error.error();
|
2019-04-08 00:35:26 +03:00
|
|
|
}
|
|
|
|
|
2021-05-06 20:35:34 +03:00
|
|
|
if (!old_parent_custody || !new_parent_custody) {
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
2021-08-01 23:58:50 +03:00
|
|
|
if (!new_custody_or_error.is_error()) {
|
|
|
|
auto& new_inode = new_custody_or_error.value()->inode();
|
|
|
|
|
|
|
|
if (old_inode.index() != new_inode.index() && old_inode.is_directory() && new_inode.is_directory()) {
|
|
|
|
size_t child_count = 0;
|
2021-09-06 21:30:18 +03:00
|
|
|
TRY(new_inode.traverse_as_directory([&child_count](auto&) {
|
2021-08-01 23:58:50 +03:00
|
|
|
++child_count;
|
|
|
|
return child_count <= 2;
|
2021-09-06 21:30:18 +03:00
|
|
|
}));
|
2021-08-01 23:58:50 +03:00
|
|
|
if (child_count > 2)
|
|
|
|
return ENOTEMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& old_parent_inode = old_parent_custody->inode();
|
|
|
|
auto& new_parent_inode = new_parent_custody->inode();
|
|
|
|
|
2020-01-03 06:10:05 +03:00
|
|
|
if (&old_parent_inode.fs() != &new_parent_inode.fs())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EXDEV;
|
2020-01-03 06:10:05 +03:00
|
|
|
|
2020-11-01 19:17:23 +03:00
|
|
|
for (auto* new_ancestor = new_parent_custody.ptr(); new_ancestor; new_ancestor = new_ancestor->parent()) {
|
|
|
|
if (&old_inode == &new_ancestor->inode())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EDIRINTOSELF;
|
2020-11-01 19:17:23 +03:00
|
|
|
}
|
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!new_parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-04-08 00:35:26 +03:00
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!old_parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-04-08 00:35:26 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (old_parent_inode.metadata().is_sticky()) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!current_process.is_superuser() && old_inode.metadata().uid != current_process.euid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-04-28 23:54:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
if (old_parent_custody->is_readonly() || new_parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-07-16 22:12:07 +03:00
|
|
|
auto old_basename = KLexicalPath::basename(old_path);
|
|
|
|
if (old_basename.is_empty() || old_basename == "."sv || old_basename == ".."sv)
|
|
|
|
return EINVAL;
|
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
auto new_basename = KLexicalPath::basename(new_path);
|
2021-07-16 22:12:07 +03:00
|
|
|
if (new_basename.is_empty() || new_basename == "."sv || new_basename == ".."sv)
|
|
|
|
return EINVAL;
|
2019-05-31 16:22:52 +03:00
|
|
|
|
2021-08-10 14:39:44 +03:00
|
|
|
if (old_basename == new_basename && old_parent_inode.index() == new_parent_inode.index())
|
|
|
|
return KSuccess;
|
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!new_custody_or_error.is_error()) {
|
|
|
|
auto& new_custody = *new_custody_or_error.value();
|
|
|
|
auto& new_inode = new_custody.inode();
|
2019-04-08 00:35:26 +03:00
|
|
|
// FIXME: Is this really correct? Check what other systems do.
|
2019-05-30 19:58:59 +03:00
|
|
|
if (&new_inode == &old_inode)
|
2019-04-08 00:35:26 +03:00
|
|
|
return KSuccess;
|
2019-05-30 19:58:59 +03:00
|
|
|
if (new_parent_inode.metadata().is_sticky()) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!current_process.is_superuser() && new_inode.metadata().uid != current_process.euid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-04-29 00:34:33 +03:00
|
|
|
}
|
2019-05-30 19:58:59 +03:00
|
|
|
if (new_inode.is_directory() && !old_inode.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EISDIR;
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(new_parent_inode.remove_child(new_basename));
|
2019-04-08 00:35:26 +03:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(new_parent_inode.add_child(old_inode, new_basename, old_inode.mode()));
|
|
|
|
TRY(old_parent_inode.remove_child(old_basename));
|
2019-04-08 00:35:26 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-08-28 23:11:16 +03:00
|
|
|
KResult VirtualFileSystem::chown(Custody& custody, UserID a_uid, GroupID a_gid)
|
2019-02-27 14:32:53 +03:00
|
|
|
{
|
2020-05-28 17:41:04 +03:00
|
|
|
auto& inode = custody.inode();
|
2019-06-02 11:31:25 +03:00
|
|
|
auto metadata = inode.metadata();
|
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (current_process.euid() != metadata.uid && !current_process.is_superuser())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2019-02-27 14:32:53 +03:00
|
|
|
|
2021-08-28 23:11:16 +03:00
|
|
|
UserID new_uid = metadata.uid;
|
|
|
|
GroupID new_gid = metadata.gid;
|
2019-02-27 14:32:53 +03:00
|
|
|
|
|
|
|
if (a_uid != (uid_t)-1) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (current_process.euid() != a_uid && !current_process.is_superuser())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2019-02-27 14:32:53 +03:00
|
|
|
new_uid = a_uid;
|
|
|
|
}
|
|
|
|
if (a_gid != (gid_t)-1) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!current_process.in_group(a_gid) && !current_process.is_superuser())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2019-02-27 14:32:53 +03:00
|
|
|
new_gid = a_gid;
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
if (custody.is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::chown(): inode {} <- uid={} gid={}", inode.identifier(), new_uid, new_gid);
|
2020-04-04 20:46:55 +03:00
|
|
|
|
|
|
|
if (metadata.is_setuid() || metadata.is_setgid()) {
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::chown(): Stripping SUID/SGID bits from {}", inode.identifier());
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(inode.chmod(metadata.mode & ~(04000 | 02000)));
|
2020-04-04 20:46:55 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
return inode.chown(new_uid, new_gid);
|
2019-02-27 14:32:53 +03:00
|
|
|
}
|
|
|
|
|
2021-08-28 23:11:16 +03:00
|
|
|
KResult VirtualFileSystem::chown(StringView path, UserID a_uid, GroupID a_gid, Custody& base)
|
2019-06-02 13:30:24 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base));
|
2020-05-28 17:41:04 +03:00
|
|
|
return chown(custody, a_uid, a_gid);
|
2019-06-02 13:30:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 19:59:32 +03:00
|
|
|
static bool hard_link_allowed(const Inode& inode)
|
|
|
|
{
|
|
|
|
auto metadata = inode.metadata();
|
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
if (Process::current().euid() == metadata.uid)
|
2021-01-19 19:59:32 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (metadata.is_regular_file()
|
|
|
|
&& !metadata.is_setuid()
|
|
|
|
&& !(metadata.is_setgid() && metadata.mode & S_IXGRP)
|
2021-08-19 22:45:07 +03:00
|
|
|
&& metadata.may_write(Process::current())) {
|
2021-01-19 19:59:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::link(StringView old_path, StringView new_path, Custody& base)
|
2019-02-21 15:26:40 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto old_custody = TRY(resolve_path(old_path, base));
|
|
|
|
auto& old_inode = old_custody->inode();
|
2019-02-21 15:26:40 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 16:30:09 +03:00
|
|
|
auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!new_custody_or_error.is_error())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EEXIST;
|
2019-01-22 09:03:44 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!parent_custody)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOENT;
|
2019-02-27 17:31:26 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
|
|
|
|
if (parent_inode.fsid() != old_inode.fsid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EXDEV;
|
2019-02-27 17:31:26 +03:00
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!parent_inode.metadata().may_write(Process::current()))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-02-27 17:31:26 +03:00
|
|
|
|
2020-01-16 00:10:38 +03:00
|
|
|
if (old_inode.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2020-01-16 00:10:38 +03:00
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-01-19 19:59:32 +03:00
|
|
|
if (!hard_link_allowed(old_inode))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EPERM;
|
2021-01-19 19:59:32 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
return parent_inode.add_child(old_inode, KLexicalPath::basename(new_path), old_inode.mode());
|
2019-02-21 15:26:40 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::unlink(StringView path, Custody& base)
|
2019-02-21 15:26:40 +03:00
|
|
|
{
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL));
|
|
|
|
auto& inode = custody->inode();
|
2019-01-23 07:35:42 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (inode.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EISDIR;
|
2019-02-21 17:45:31 +03:00
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
// We have just checked that the inode is not a directory, and thus it's not
|
|
|
|
// the root. So it should have a parent. Note that this would be invalidated
|
|
|
|
// if we were to support bind-mounting regular files on top of the root.
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(parent_custody);
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-01-22 09:03:44 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (parent_inode.metadata().is_sticky()) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!current_process.is_superuser() && inode.metadata().uid != current_process.euid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-04-28 23:54:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
return parent_inode.remove_child(KLexicalPath::basename(path));
|
2019-01-22 09:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::symlink(StringView target, StringView linkpath, Custody& base)
|
2019-03-02 03:50:34 +03:00
|
|
|
{
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 16:30:09 +03:00
|
|
|
auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!existing_custody_or_error.is_error())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EEXIST;
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!parent_custody)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOENT;
|
2021-08-14 16:15:11 +03:00
|
|
|
if (existing_custody_or_error.is_error() && existing_custody_or_error.error() != ENOENT)
|
2019-05-30 19:58:59 +03:00
|
|
|
return existing_custody_or_error.error();
|
|
|
|
auto& parent_inode = parent_custody->inode();
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_inode.metadata().may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-05-28 17:56:25 +03:00
|
|
|
if (parent_custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2019-03-02 03:50:34 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
auto basename = KLexicalPath::basename(linkpath);
|
2021-07-11 01:25:24 +03:00
|
|
|
dbgln_if(VFS_DEBUG, "VirtualFileSystem::symlink: '{}' (-> '{}') in {}", basename, target, parent_inode.identifier());
|
2021-09-05 15:00:18 +03:00
|
|
|
|
|
|
|
auto inode = TRY(parent_inode.create_child(basename, S_IFLNK | 0644, 0, current_process.euid(), current_process.egid()));
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
auto target_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)target.characters_without_null_termination()));
|
2021-09-05 15:00:18 +03:00
|
|
|
TRY(inode->write_bytes(0, target.length(), target_buffer, nullptr));
|
2019-03-02 03:50:34 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::rmdir(StringView path, Custody& base)
|
2019-01-28 06:16:01 +03:00
|
|
|
{
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Custody> parent_custody;
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path(path, base, &parent_custody));
|
|
|
|
auto& inode = custody->inode();
|
2019-01-28 06:16:01 +03:00
|
|
|
|
|
|
|
// FIXME: We should return EINVAL if the last component of the path is "."
|
|
|
|
// FIXME: We should return ENOTEMPTY if the last component of the path is ".."
|
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
if (!inode.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTDIR;
|
2019-02-21 17:45:31 +03:00
|
|
|
|
2020-04-19 19:07:16 +03:00
|
|
|
if (!parent_custody)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EBUSY;
|
2020-04-19 19:07:16 +03:00
|
|
|
|
2019-05-30 19:58:59 +03:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
2021-01-10 12:12:15 +03:00
|
|
|
auto parent_metadata = parent_inode.metadata();
|
2019-05-30 19:58:59 +03:00
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!parent_metadata.may_write(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-01-28 06:16:01 +03:00
|
|
|
|
2021-01-10 12:12:15 +03:00
|
|
|
if (parent_metadata.is_sticky()) {
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!current_process.is_superuser() && inode.metadata().uid != current_process.euid())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2021-01-10 12:12:15 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 23:34:43 +03:00
|
|
|
size_t child_count = 0;
|
2021-09-06 21:30:18 +03:00
|
|
|
TRY(inode.traverse_as_directory([&child_count](auto&) {
|
2021-07-17 23:34:43 +03:00
|
|
|
++child_count;
|
|
|
|
return true;
|
2021-09-06 21:30:18 +03:00
|
|
|
}));
|
2020-08-05 11:00:18 +03:00
|
|
|
|
2021-07-17 23:34:43 +03:00
|
|
|
if (child_count != 2)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTEMPTY;
|
2019-01-28 06:16:01 +03:00
|
|
|
|
2021-09-05 15:00:18 +03:00
|
|
|
if (custody->is_readonly())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EROFS;
|
2020-05-28 17:56:25 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(inode.remove_child("."));
|
|
|
|
TRY(inode.remove_child(".."));
|
2019-01-28 06:16:01 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
return parent_inode.remove_child(KLexicalPath::basename(path));
|
2019-01-28 06:16:01 +03:00
|
|
|
}
|
|
|
|
|
2021-08-16 02:40:19 +03:00
|
|
|
void VirtualFileSystem::for_each_mount(Function<void(Mount const&)> callback) const
|
2018-10-26 19:43:25 +03:00
|
|
|
{
|
2021-08-16 02:40:19 +03:00
|
|
|
m_mounts.with_shared([&](auto& mounts) {
|
|
|
|
for (auto& mount : mounts) {
|
|
|
|
callback(mount);
|
|
|
|
}
|
|
|
|
});
|
2018-10-26 19:43:25 +03:00
|
|
|
}
|
2018-12-20 02:39:29 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
void VirtualFileSystem::sync()
|
2018-12-20 02:39:29 +03:00
|
|
|
{
|
2021-07-11 01:20:38 +03:00
|
|
|
FileSystem::sync();
|
2018-12-20 02:39:29 +03:00
|
|
|
}
|
2019-05-30 18:46:08 +03:00
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
Custody& VirtualFileSystem::root_custody()
|
2019-05-30 18:46:08 +03:00
|
|
|
{
|
|
|
|
return *m_root_custody;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
UnveilNode const& VirtualFileSystem::find_matching_unveiled_path(StringView path)
|
2020-12-26 13:24:34 +03:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
VERIFY(current_process.veil_state() != VeilState::None);
|
|
|
|
auto& unveil_root = current_process.unveiled_paths();
|
2020-12-26 13:24:34 +03:00
|
|
|
|
2021-07-06 12:21:52 +03:00
|
|
|
auto path_parts = KLexicalPath::parts(path);
|
2021-06-07 00:13:26 +03:00
|
|
|
return unveil_root.traverse_until_last_accessible_node(path_parts.begin(), path_parts.end());
|
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
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::validate_path_against_process_veil(Custody const& custody, int options)
|
2021-07-05 18:15:07 +03:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
if (Process::current().veil_state() == VeilState::None)
|
2021-07-05 18:15:07 +03:00
|
|
|
return KSuccess;
|
2021-09-06 13:24:36 +03:00
|
|
|
auto absolute_path = TRY(custody.try_serialize_absolute_path());
|
2021-07-06 13:58:03 +03:00
|
|
|
return validate_path_against_process_veil(absolute_path->view(), options);
|
2021-07-05 18:15:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResult VirtualFileSystem::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
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
if (Process::current().veil_state() == VeilState::None)
|
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
|
|
|
return KSuccess;
|
2020-10-17 14:39:36 +03:00
|
|
|
if (path == "/usr/lib/Loader.so")
|
|
|
|
return KSuccess;
|
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
|
|
|
|
2021-07-05 19:03:54 +03:00
|
|
|
VERIFY(path.starts_with('/'));
|
|
|
|
VERIFY(!path.contains("/../"sv) && !path.ends_with("/.."sv));
|
|
|
|
VERIFY(!path.contains("/./"sv) && !path.ends_with("/."sv));
|
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
|
|
|
|
2021-06-07 00:13:26 +03:00
|
|
|
auto& unveiled_path = find_matching_unveiled_path(path);
|
|
|
|
if (unveiled_path.permissions() == UnveilAccess::None) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled.", path);
|
2020-01-30 14:05:36 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOENT;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (options & O_CREAT) {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & UnveilAccess::CreateOrRemove)) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled with 'c' permission.", path);
|
2020-01-30 14:05:36 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options & O_UNLINK_INTERNAL) {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & UnveilAccess::CreateOrRemove)) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' for unlink since it hasn't been unveiled with 'c' permission.", path);
|
2020-01-30 14:05:36 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
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
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-01-21 15:14:26 +03:00
|
|
|
if (options & O_RDONLY) {
|
2020-11-21 22:55:20 +03:00
|
|
|
if (options & O_DIRECTORY) {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & (UnveilAccess::Read | UnveilAccess::Browse))) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled with 'r' or 'b' permissions.", path);
|
2020-11-21 22:55:20 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-11-21 22:55:20 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & UnveilAccess::Read)) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled with 'r' permission.", path);
|
2020-11-21 22:55:20 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2020-11-21 22:55:20 +03:00
|
|
|
}
|
2020-01-21 15:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options & O_WRONLY) {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & UnveilAccess::Write)) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled with 'w' permission.", path);
|
2020-01-30 14:05:36 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
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
|
|
|
}
|
2020-01-21 15:14:26 +03:00
|
|
|
}
|
|
|
|
if (options & O_EXEC) {
|
2021-06-07 00:13:26 +03:00
|
|
|
if (!(unveiled_path.permissions() & UnveilAccess::Execute)) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Rejecting path '{}' since it hasn't been unveiled with 'x' permission.", path);
|
2020-01-30 14:05:36 +03:00
|
|
|
dump_backtrace();
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> VirtualFileSystem::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
|
2019-05-30 18:46:08 +03:00
|
|
|
{
|
2021-09-05 15:00:18 +03:00
|
|
|
auto custody = TRY(resolve_path_without_veil(path, base, out_parent, options, symlink_recursion_level));
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(validate_path_against_process_veil(*custody, options));
|
2020-03-19 11:57:34 +03:00
|
|
|
return custody;
|
|
|
|
}
|
|
|
|
|
2021-01-19 20:12:09 +03:00
|
|
|
static bool safe_to_follow_symlink(const Inode& inode, const InodeMetadata& parent_metadata)
|
|
|
|
{
|
|
|
|
auto metadata = inode.metadata();
|
2021-08-19 22:45:07 +03:00
|
|
|
if (Process::current().euid() == metadata.uid)
|
2021-01-19 20:12:09 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!(parent_metadata.is_sticky() && parent_metadata.mode & S_IWOTH))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (metadata.uid == parent_metadata.uid)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:25:24 +03:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> VirtualFileSystem::resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
|
2020-03-19 11:57:34 +03:00
|
|
|
{
|
2019-12-24 12:39:21 +03:00
|
|
|
if (symlink_recursion_level >= symlink_recursion_limit)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ELOOP;
|
2019-08-25 19:18:51 +03:00
|
|
|
|
2019-05-30 18:46:08 +03:00
|
|
|
if (path.is_empty())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-05-30 18:46:08 +03:00
|
|
|
|
2021-05-22 01:12:32 +03:00
|
|
|
GenericLexer path_lexer(path);
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
2020-01-11 01:14:04 +03:00
|
|
|
|
2021-08-15 02:29:44 +03:00
|
|
|
NonnullRefPtr<Custody> custody = path[0] == '/' ? root_custody() : base;
|
2021-05-22 01:12:32 +03:00
|
|
|
bool extra_iteration = path[path.length() - 1] == '/';
|
|
|
|
|
|
|
|
while (!path_lexer.is_eof() || extra_iteration) {
|
|
|
|
if (path_lexer.is_eof())
|
|
|
|
extra_iteration = false;
|
|
|
|
auto part = path_lexer.consume_until('/');
|
|
|
|
path_lexer.consume_specific('/');
|
2019-05-30 18:46:08 +03:00
|
|
|
|
2020-01-15 10:52:33 +03:00
|
|
|
Custody& parent = custody;
|
|
|
|
auto parent_metadata = parent.inode().metadata();
|
2020-01-14 13:30:15 +03:00
|
|
|
if (!parent_metadata.is_directory())
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOTDIR;
|
2020-01-14 13:30:15 +03:00
|
|
|
// Ensure the current user is allowed to resolve paths inside this directory.
|
2021-08-19 22:45:07 +03:00
|
|
|
if (!parent_metadata.may_execute(current_process))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-06-13 16:33:01 +03:00
|
|
|
|
2021-05-22 01:12:32 +03:00
|
|
|
bool have_more_parts = !path_lexer.is_eof() || extra_iteration;
|
2020-01-14 13:30:15 +03:00
|
|
|
|
|
|
|
if (part == "..") {
|
|
|
|
// If we encounter a "..", take a step back, but don't go beyond the root.
|
2020-01-15 10:52:33 +03:00
|
|
|
if (custody->parent())
|
|
|
|
custody = *custody->parent();
|
2019-09-21 00:45:16 +03:00
|
|
|
continue;
|
2020-01-14 13:30:15 +03:00
|
|
|
} else if (part == "." || part.is_empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-13 16:33:01 +03:00
|
|
|
|
2020-01-14 13:30:15 +03:00
|
|
|
// Okay, let's look up this part.
|
2021-08-14 14:32:35 +03:00
|
|
|
auto child_or_error = parent.inode().lookup(part);
|
|
|
|
if (child_or_error.is_error()) {
|
2020-01-15 10:52:33 +03:00
|
|
|
if (out_parent) {
|
2020-01-14 13:30:15 +03:00
|
|
|
// ENOENT with a non-null parent custody signals to caller that
|
2020-01-03 05:53:06 +03:00
|
|
|
// we found the immediate parent of the file, but the file itself
|
|
|
|
// does not exist yet.
|
2020-01-15 10:52:33 +03:00
|
|
|
*out_parent = have_more_parts ? nullptr : &parent;
|
2020-01-03 05:53:06 +03:00
|
|
|
}
|
2021-08-14 14:32:35 +03:00
|
|
|
return child_or_error.error();
|
2020-01-03 05:53:06 +03:00
|
|
|
}
|
2021-08-14 14:32:35 +03:00
|
|
|
auto child_inode = child_or_error.release_value();
|
2020-01-14 13:30:15 +03:00
|
|
|
|
2020-01-15 10:52:33 +03:00
|
|
|
int mount_flags_for_child = parent.mount_flags();
|
2020-02-01 11:23:46 +03:00
|
|
|
|
2020-01-14 13:30:15 +03:00
|
|
|
// See if there's something mounted on the child; in that case
|
|
|
|
// we would need to return the guest inode, not the host inode.
|
2021-07-11 01:50:08 +03:00
|
|
|
if (auto mount = find_mount_for_host(child_inode->identifier())) {
|
2020-06-25 00:16:24 +03:00
|
|
|
child_inode = mount->guest();
|
2020-01-14 13:30:15 +03:00
|
|
|
mount_flags_for_child = mount->flags();
|
2019-05-30 18:46:08 +03:00
|
|
|
}
|
2019-05-31 07:42:49 +03:00
|
|
|
|
2021-09-05 15:00:18 +03:00
|
|
|
custody = TRY(Custody::try_create(&parent, part, *child_inode, mount_flags_for_child));
|
2019-05-31 07:42:49 +03:00
|
|
|
|
2020-01-14 13:30:15 +03:00
|
|
|
if (child_inode->metadata().is_symlink()) {
|
|
|
|
if (!have_more_parts) {
|
2019-05-30 18:46:08 +03:00
|
|
|
if (options & O_NOFOLLOW)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ELOOP;
|
2019-05-30 18:46:08 +03:00
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
2020-01-14 13:30:15 +03:00
|
|
|
break;
|
2019-05-30 18:46:08 +03:00
|
|
|
}
|
2021-01-19 20:12:09 +03:00
|
|
|
|
|
|
|
if (!safe_to_follow_symlink(*child_inode, parent_metadata))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2021-01-19 20:12:09 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
TRY(validate_path_against_process_veil(*custody, options));
|
2021-02-06 21:11:44 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
auto symlink_target = TRY(child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1));
|
|
|
|
if (!have_more_parts)
|
2019-06-12 16:36:05 +03:00
|
|
|
return symlink_target;
|
|
|
|
|
2020-01-14 13:30:15 +03:00
|
|
|
// Now, resolve the remaining path relative to the symlink target.
|
|
|
|
// We prepend a "." to it to ensure that it's not empty and that
|
|
|
|
// any initial slashes it might have get interpreted properly.
|
|
|
|
StringBuilder remaining_path;
|
|
|
|
remaining_path.append('.');
|
|
|
|
remaining_path.append(path.substring_view_starting_after_substring(part));
|
2019-06-12 16:36:05 +03:00
|
|
|
|
2021-09-05 15:55:25 +03:00
|
|
|
return resolve_path_without_veil(remaining_path.to_string(), symlink_target, out_parent, options, symlink_recursion_level + 1);
|
2019-05-30 18:46:08 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-14 13:30:15 +03:00
|
|
|
|
2020-01-15 10:52:33 +03:00
|
|
|
if (out_parent)
|
|
|
|
*out_parent = custody->parent();
|
|
|
|
return custody;
|
2019-05-30 18:46:08 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|