2020-07-30 20:01:41 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-12-15 06:42:40 -03:00
|
|
|
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
|
2020-07-30 20:01:41 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 20:01:41 -03:00
|
|
|
*/
|
|
|
|
|
|
2023-07-17 13:22:01 -03:00
|
|
|
#include <AK/FixedStringBuffer.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2022-12-15 06:42:40 -03:00
|
|
|
#include <Kernel/FileSystem/MountFile.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2023-02-24 14:45:37 -03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2022-12-15 06:42:40 -03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$fsopen(Userspace<Syscall::SC_fsopen_params const*> user_params)
|
Kernel: Simplify mount syscall flow for regular calls
We do this by putting a distinction between two types of filesystems -
the first type is backed in RAM, and includes TmpFS, ProcFS, SysFS,
DevPtsFS and DevTmpFS. Because these filesystems are backed in RAM,
trying to mount them doesn't require source open file description.
The second type is filesystems that are backed by a file, therefore the
userspace program has to open them (hence it has a open file description
on them) and provide the appropriate source open file description.
By putting this distinction, we can early check if the user tried to
mount the second type of filesystems without a valid file description,
and fail with EBADF then.
Otherwise, we can proceed to either mount either type of filesystem,
provided that the fs_type is valid.
2022-05-28 08:52:48 -03:00
|
|
|
{
|
2022-12-15 06:42:40 -03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
|
|
|
|
TRY(require_promise(Pledge::mount));
|
|
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
|
|
|
|
return Error::from_errno(EPERM);
|
|
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2023-07-17 13:22:01 -03:00
|
|
|
// NOTE: 16 characters should be enough for any fstype today and in the future.
|
|
|
|
|
auto fs_type_string = TRY(get_syscall_name_string_fixed_buffer<16>(params.fs_type));
|
2022-12-15 06:42:40 -03:00
|
|
|
|
|
|
|
|
// NOTE: If some userspace program uses MS_REMOUNT, return EINVAL to indicate that we never want this
|
|
|
|
|
// flag to appear in the mount table...
|
|
|
|
|
if (params.flags & MS_REMOUNT || params.flags & MS_BIND)
|
|
|
|
|
return Error::from_errno(EINVAL);
|
|
|
|
|
|
2023-07-17 13:22:01 -03:00
|
|
|
auto const* fs_type_initializer = TRY(VirtualFileSystem::find_filesystem_type_initializer(fs_type_string.representable_view()));
|
2022-12-15 06:42:40 -03:00
|
|
|
VERIFY(fs_type_initializer);
|
|
|
|
|
auto mount_file = TRY(MountFile::create(*fs_type_initializer, params.flags));
|
|
|
|
|
auto description = TRY(OpenFileDescription::try_create(move(mount_file)));
|
|
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
|
|
|
|
auto new_fd = TRY(fds.allocate());
|
|
|
|
|
fds[new_fd.fd].set(move(description), FD_CLOEXEC);
|
|
|
|
|
return new_fd.fd;
|
|
|
|
|
});
|
Kernel: Simplify mount syscall flow for regular calls
We do this by putting a distinction between two types of filesystems -
the first type is backed in RAM, and includes TmpFS, ProcFS, SysFS,
DevPtsFS and DevTmpFS. Because these filesystems are backed in RAM,
trying to mount them doesn't require source open file description.
The second type is filesystems that are backed by a file, therefore the
userspace program has to open them (hence it has a open file description
on them) and provide the appropriate source open file description.
By putting this distinction, we can early check if the user tried to
mount the second type of filesystems without a valid file description,
and fail with EBADF then.
Otherwise, we can proceed to either mount either type of filesystem,
provided that the fs_type is valid.
2022-05-28 08:52:48 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 06:42:40 -03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$fsmount(Userspace<Syscall::SC_fsmount_params const*> user_params)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2022-12-15 06:42:40 -03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
|
|
|
|
TRY(require_promise(Pledge::mount));
|
2022-08-20 19:21:01 -03:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
2022-12-15 06:42:40 -03:00
|
|
|
return Error::from_errno(EPERM);
|
2020-07-30 18:38:15 -03:00
|
|
|
|
2021-09-05 12:51:37 -03:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2022-12-15 06:42:40 -03:00
|
|
|
auto mount_description = TRY(open_file_description(params.mount_fd));
|
|
|
|
|
if (!mount_description->is_mount_file())
|
|
|
|
|
return Error::from_errno(EINVAL);
|
2020-07-30 18:38:15 -03:00
|
|
|
|
2022-12-15 06:42:40 -03:00
|
|
|
RefPtr<OpenFileDescription> source_description = TRY(open_file_description_ignoring_negative(params.source_fd));
|
2021-09-05 13:20:57 -03:00
|
|
|
auto target = TRY(try_copy_kstring_from_user(params.target));
|
2022-08-20 19:21:01 -03:00
|
|
|
auto target_custody = TRY(VirtualFileSystem::the().resolve_path(credentials, target->view(), current_directory()));
|
2022-12-15 06:42:40 -03:00
|
|
|
auto flags = mount_description->mount_file()->mount_flags();
|
|
|
|
|
TRY(VirtualFileSystem::the().mount(*mount_description->mount_file(), source_description.ptr(), target_custody, flags));
|
2021-11-07 20:51:39 -03:00
|
|
|
return 0;
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-25 14:30:28 -03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$remount(Userspace<Syscall::SC_remount_params const*> user_params)
|
|
|
|
|
{
|
|
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2022-12-15 06:42:40 -03:00
|
|
|
TRY(require_promise(Pledge::mount));
|
2023-02-25 14:30:28 -03:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
|
|
|
|
if (params.flags & MS_REMOUNT)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
if (params.flags & MS_BIND)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
|
|
auto target = TRY(try_copy_kstring_from_user(params.target));
|
|
|
|
|
auto target_custody = TRY(VirtualFileSystem::the().resolve_path(credentials, target->view(), current_directory()));
|
|
|
|
|
TRY(VirtualFileSystem::the().remount(target_custody, params.flags));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<FlatPtr> Process::sys$bindmount(Userspace<Syscall::SC_bindmount_params const*> user_params)
|
|
|
|
|
{
|
|
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2022-12-15 06:42:40 -03:00
|
|
|
TRY(require_promise(Pledge::mount));
|
2023-02-25 14:30:28 -03:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
|
|
|
|
if (params.flags & MS_REMOUNT)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
if (params.flags & MS_BIND)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
|
|
auto source_fd = params.source_fd;
|
|
|
|
|
auto target = TRY(try_copy_kstring_from_user(params.target));
|
|
|
|
|
auto target_custody = TRY(VirtualFileSystem::the().resolve_path(credentials, target->view(), current_directory()));
|
|
|
|
|
|
|
|
|
|
auto description = TRY(open_file_description(source_fd));
|
|
|
|
|
if (!description->custody()) {
|
|
|
|
|
// NOTE: We only support bind-mounting inodes, not arbitrary files.
|
|
|
|
|
return ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TRY(VirtualFileSystem::the().bind_mount(*description->custody(), target_custody, params.flags));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$umount(Userspace<char const*> user_mountpoint, size_t mountpoint_length)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2023-05-26 07:58:31 -03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2022-08-20 19:21:01 -03:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
2021-03-01 09:49:16 -03:00
|
|
|
return EPERM;
|
2020-07-30 18:38:15 -03:00
|
|
|
|
2022-12-15 06:42:40 -03:00
|
|
|
TRY(require_promise(Pledge::mount));
|
2020-07-30 18:38:15 -03:00
|
|
|
|
2021-09-05 13:18:23 -03:00
|
|
|
auto mountpoint = TRY(get_syscall_path_argument(user_mountpoint, mountpoint_length));
|
2022-08-20 19:21:01 -03:00
|
|
|
auto custody = TRY(VirtualFileSystem::the().resolve_path(credentials, mountpoint->view(), current_directory()));
|
2022-08-19 18:03:24 -03:00
|
|
|
TRY(VirtualFileSystem::the().unmount(*custody));
|
2021-11-07 20:51:39 -03:00
|
|
|
return 0;
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|