mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:18:11 +00:00

Found due to smelly code in InodeFile::absolute_path. In particular, this replaces the following misleading methods: File::absolute_path This method *never* returns an actual path, and if called on an InodeFile (which is impossible), it would VERIFY_NOT_REACHED(). OpenFileDescription::try_serialize_absolute_path OpenFileDescription::absolute_path These methods do not guarantee to return an actual path (just like the other method), and just like Custody::absolute_path they do not guarantee accuracy. In particular, just renaming the method made a TOCTOU bug obvious. The new method signatures use KResultOr, just like try_serialize_absolute_path() already did.
85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
/*
|
|
* Copyright (c) 2021, Justin Mietzner <sw1tchbl4d3@sw1tchbl4d3.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
#include <Kernel/Process.h>
|
|
|
|
namespace Kernel {
|
|
|
|
KResultOr<FlatPtr> Process::do_statvfs(StringView path, statvfs* buf)
|
|
{
|
|
auto custody = TRY(VirtualFileSystem::the().resolve_path(path, current_directory(), nullptr, 0));
|
|
auto& inode = custody->inode();
|
|
auto& fs = inode.fs();
|
|
|
|
statvfs kernelbuf = {};
|
|
|
|
kernelbuf.f_bsize = static_cast<u64>(fs.block_size());
|
|
kernelbuf.f_frsize = fs.fragment_size();
|
|
kernelbuf.f_blocks = fs.total_block_count();
|
|
kernelbuf.f_bfree = fs.free_block_count();
|
|
|
|
// FIXME: Implement "available blocks" into Filesystem
|
|
kernelbuf.f_bavail = fs.free_block_count();
|
|
|
|
kernelbuf.f_files = fs.total_inode_count();
|
|
kernelbuf.f_ffree = fs.free_inode_count();
|
|
kernelbuf.f_favail = fs.free_inode_count(); // FIXME: same as f_bavail
|
|
|
|
kernelbuf.f_fsid = 0; // FIXME: Implement "Filesystem ID" into Filesystem
|
|
|
|
kernelbuf.f_namemax = 255;
|
|
|
|
Custody* current_custody = custody;
|
|
|
|
while (current_custody) {
|
|
VirtualFileSystem::the().for_each_mount([&kernelbuf, ¤t_custody](auto& mount) {
|
|
if (¤t_custody->inode() == &mount.guest()) {
|
|
int mountflags = mount.flags();
|
|
int flags = 0;
|
|
if (mountflags & MS_RDONLY)
|
|
flags = flags | ST_RDONLY;
|
|
if (mountflags & MS_NOSUID)
|
|
flags = flags | ST_NOSUID;
|
|
|
|
kernelbuf.f_flag = flags;
|
|
current_custody = nullptr;
|
|
return IterationDecision::Break;
|
|
}
|
|
return IterationDecision::Continue;
|
|
});
|
|
|
|
if (current_custody) {
|
|
current_custody = current_custody->parent();
|
|
}
|
|
}
|
|
|
|
return copy_to_user(buf, &kernelbuf);
|
|
}
|
|
|
|
KResultOr<FlatPtr> Process::sys$statvfs(Userspace<const Syscall::SC_statvfs_params*> user_params)
|
|
{
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
|
REQUIRE_PROMISE(rpath);
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
|
|
|
auto path = TRY(get_syscall_path_argument(params.path));
|
|
return do_statvfs(path->view(), params.buf);
|
|
}
|
|
|
|
KResultOr<FlatPtr> Process::sys$fstatvfs(int fd, statvfs* buf)
|
|
{
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
auto description = TRY(fds().open_file_description(fd));
|
|
auto absolute_path = TRY(description->original_absolute_path());
|
|
// FIXME: TOCTOU bug! The file connected to the fd may or may not have been moved, and the name possibly taken by a different file.
|
|
return do_statvfs(absolute_path->view(), buf);
|
|
}
|
|
|
|
}
|