Compare commits
2 Commits
82e7b0e860
...
3b4214c8be
Author | SHA1 | Date | |
---|---|---|---|
3b4214c8be | |||
e6954d2e49 |
@ -3,6 +3,7 @@
|
|||||||
#include "thread/Thread.h"
|
#include "thread/Thread.h"
|
||||||
#include <bits/modes.h>
|
#include <bits/modes.h>
|
||||||
#include <luna/PathParser.h>
|
#include <luna/PathParser.h>
|
||||||
|
#include <luna/Utf8.h>
|
||||||
|
|
||||||
namespace VFS
|
namespace VFS
|
||||||
{
|
{
|
||||||
@ -46,6 +47,8 @@ namespace VFS
|
|||||||
|
|
||||||
auto child_name = TRY(parser.basename());
|
auto child_name = TRY(parser.basename());
|
||||||
|
|
||||||
|
TRY(validate_filename(child_name.view()));
|
||||||
|
|
||||||
return parent_inode->create_subdirectory(child_name.chars());
|
return parent_inode->create_subdirectory(child_name.chars());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,9 +63,41 @@ namespace VFS
|
|||||||
|
|
||||||
auto child_name = TRY(parser.basename());
|
auto child_name = TRY(parser.basename());
|
||||||
|
|
||||||
|
TRY(validate_filename(child_name.view()));
|
||||||
|
|
||||||
return parent_inode->create_file(child_name.chars());
|
return parent_inode->create_file(child_name.chars());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 starting with a hyphen.
|
||||||
|
if (!name.is_empty() && name[0] == '-') 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 {};
|
||||||
|
}
|
||||||
|
|
||||||
bool can_execute(SharedPtr<Inode> inode, Credentials auth)
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth)
|
||||||
{
|
{
|
||||||
if (inode->uid() == auth.euid) { return inode->mode() & S_IXUSR; }
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IXUSR; }
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/SharedPtr.h>
|
#include <luna/SharedPtr.h>
|
||||||
#include <luna/StaticString.h>
|
#include <luna/StaticString.h>
|
||||||
|
#include <luna/StringView.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
struct Credentials;
|
struct Credentials;
|
||||||
@ -211,6 +212,8 @@ namespace VFS
|
|||||||
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
|
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
|
||||||
SharedPtr<VFS::Inode> working_directory = {});
|
SharedPtr<VFS::Inode> working_directory = {});
|
||||||
|
|
||||||
|
Result<void> validate_filename(StringView name);
|
||||||
|
|
||||||
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
|
||||||
bool can_read(SharedPtr<Inode> inode, Credentials auth);
|
bool can_read(SharedPtr<Inode> inode, Credentials auth);
|
||||||
bool can_write(SharedPtr<Inode> inode, Credentials auth);
|
bool can_write(SharedPtr<Inode> inode, Credentials auth);
|
||||||
|
@ -22,6 +22,8 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
|||||||
auto dirname = TRY(parser.dirname());
|
auto dirname = TRY(parser.dirname());
|
||||||
auto basename = TRY(parser.basename());
|
auto basename = TRY(parser.basename());
|
||||||
|
|
||||||
|
TRY(VFS::validate_filename(basename.view()));
|
||||||
|
|
||||||
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
|
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
|
||||||
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
|
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
|
||||||
|
|
||||||
|
@ -32,6 +32,8 @@ extern "C"
|
|||||||
usize len = strlen(path);
|
usize len = strlen(path);
|
||||||
if (!len) return dot;
|
if (!len) return dot;
|
||||||
|
|
||||||
|
if (len == 1 && *path != '/') return dot;
|
||||||
|
|
||||||
// Strip trailing slashes.
|
// Strip trailing slashes.
|
||||||
char* it = path + len - 1;
|
char* it = path + len - 1;
|
||||||
while (*it == '/' && it != path) { it--; }
|
while (*it == '/' && it != path) { it--; }
|
||||||
|
Loading…
Reference in New Issue
Block a user