2023-02-03 21:18:52 +00:00
|
|
|
#include "fs/VFS.h"
|
2023-03-10 21:18:48 +00:00
|
|
|
#include "Log.h"
|
2023-04-08 11:12:49 +00:00
|
|
|
#include "thread/Thread.h"
|
|
|
|
#include <bits/modes.h>
|
2023-03-10 21:18:48 +00:00
|
|
|
#include <luna/PathParser.h>
|
2023-04-16 09:25:32 +00:00
|
|
|
#include <luna/Utf8.h>
|
2023-02-03 21:18:52 +00:00
|
|
|
|
|
|
|
namespace VFS
|
|
|
|
{
|
|
|
|
SharedPtr<FileSystem> root_fs;
|
2023-02-25 17:05:25 +00:00
|
|
|
|
|
|
|
Inode& root_inode()
|
|
|
|
{
|
2023-03-10 21:18:48 +00:00
|
|
|
return *root_fs->root_inode();
|
|
|
|
}
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
|
2023-03-10 21:18:48 +00:00
|
|
|
{
|
2023-04-15 18:26:15 +00:00
|
|
|
if (*path == '\0') return err(ENOENT);
|
|
|
|
|
2023-03-10 21:18:48 +00:00
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
SharedPtr<Inode> current_inode;
|
2023-03-10 21:18:48 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
if (parser.is_absolute() || !working_directory) current_inode = root_fs->root_inode();
|
|
|
|
else
|
|
|
|
current_inode = working_directory;
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
const char* section;
|
2023-04-08 11:12:49 +00:00
|
|
|
while (parser.next().try_set_value(section))
|
|
|
|
{
|
|
|
|
if (!can_execute(current_inode, auth)) return err(EACCES);
|
|
|
|
current_inode = TRY(current_inode->find(section));
|
|
|
|
}
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
return current_inode;
|
2023-02-25 17:05:25 +00:00
|
|
|
}
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
|
2023-03-11 00:13:44 +00:00
|
|
|
{
|
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
auto parent_path = TRY(parser.dirname());
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
2023-04-08 11:12:49 +00:00
|
|
|
|
|
|
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
2023-03-11 00:13:44 +00:00
|
|
|
|
|
|
|
auto child_name = TRY(parser.basename());
|
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
TRY(validate_filename(child_name.view()));
|
|
|
|
|
2023-03-11 00:13:44 +00:00
|
|
|
return parent_inode->create_subdirectory(child_name.chars());
|
|
|
|
}
|
2023-03-11 09:23:46 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
|
2023-03-11 09:23:46 +00:00
|
|
|
{
|
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
auto parent_path = TRY(parser.dirname());
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
2023-04-08 11:12:49 +00:00
|
|
|
|
|
|
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
2023-03-11 09:23:46 +00:00
|
|
|
|
|
|
|
auto child_name = TRY(parser.basename());
|
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
TRY(validate_filename(child_name.view()));
|
|
|
|
|
2023-03-11 09:23:46 +00:00
|
|
|
return parent_inode->create_file(child_name.chars());
|
|
|
|
}
|
2023-04-08 11:12:49 +00:00
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
Result<void> validate_filename(StringView name)
|
|
|
|
{
|
|
|
|
// Forbid problematic characters that could cause trouble in shell scripts and the like.
|
|
|
|
if (strpbrk(name.chars(), "*?:[]\"<>\\")) return err(EINVAL);
|
|
|
|
|
|
|
|
// Forbid filenames with leading spaces.
|
|
|
|
if (!name.is_empty() && name[0] == ' ') return err(EINVAL);
|
|
|
|
|
|
|
|
// Forbid filenames with trailing spaces.
|
|
|
|
if (!name.is_empty() && name[name.length() - 1] == ' ') return err(EINVAL);
|
|
|
|
|
|
|
|
for (const auto& c : name)
|
|
|
|
{
|
|
|
|
// Forbid filenames with control characters.
|
|
|
|
if (c < 32 || c == 127) return err(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forbid filenames that are not valid UTF-8.
|
|
|
|
|
|
|
|
Utf8StringDecoder decoder(name.chars());
|
|
|
|
|
|
|
|
// This will fail if the filename is invalid UTF-8.
|
|
|
|
TRY(decoder.code_points());
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IXUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IXGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IXOTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_write(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IWUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IWGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IWOTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_read(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IRUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IRGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IROTH;
|
|
|
|
}
|
2023-04-08 14:32:56 +00:00
|
|
|
|
|
|
|
bool is_setuid(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->mode() & S_ISUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_setgid(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->mode() & S_ISGID;
|
|
|
|
}
|
2023-02-03 21:18:52 +00:00
|
|
|
}
|