From f0be4f86a60a0e744dece9e0ae148bdd3e035548 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 30 Jul 2024 11:51:07 +0300 Subject: [PATCH] LibC/Kernel: Implement access --- kernel/include/kernel/Process.h | 1 + kernel/kernel/Process.cpp | 26 +++++++++++++++++++ .../libraries/LibC/include/sys/syscall.h | 1 + userspace/libraries/LibC/unistd.cpp | 5 ++++ 4 files changed, 33 insertions(+) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 5387d4f017..55d8542685 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -106,6 +106,7 @@ namespace Kernel BAN::ErrorOr sys_close(int fd); BAN::ErrorOr sys_read(int fd, void* buffer, size_t count); BAN::ErrorOr sys_write(int fd, const void* buffer, size_t count); + BAN::ErrorOr sys_access(const char* path, int amode); BAN::ErrorOr sys_create(const char*, mode_t); BAN::ErrorOr sys_create_dir(const char*, mode_t); BAN::ErrorOr sys_unlink(const char*); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 54c24a01b6..4b85181a8c 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -784,6 +784,32 @@ namespace Kernel return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count))); } + BAN::ErrorOr Process::sys_access(const char* path, int amode) + { + int flags = 0; + if (amode & F_OK) + flags |= O_SEARCH; + if (amode & R_OK) + flags |= O_RDONLY; + if (amode & W_OK) + flags |= O_WRONLY; + if (amode & X_OK) + flags |= O_EXEC; + static_assert((O_RDONLY | O_WRONLY) == O_RDWR); + + LockGuard _(m_process_lock); + TRY(validate_string_access(path)); + + auto credentials = m_credentials; + credentials.set_euid(credentials.ruid()); + credentials.set_egid(credentials.rgid()); + + auto absolute_path = TRY(absolute_path_of(path)); + + TRY(VirtualFileSystem::get().file_from_absolute_path(credentials, absolute_path, flags)); + return 0; + } + BAN::ErrorOr Process::sys_create(const char* path, mode_t mode) { LockGuard _(m_process_lock); diff --git a/userspace/libraries/LibC/include/sys/syscall.h b/userspace/libraries/LibC/include/sys/syscall.h index f8366c72a2..f03e7d5fa0 100644 --- a/userspace/libraries/LibC/include/sys/syscall.h +++ b/userspace/libraries/LibC/include/sys/syscall.h @@ -83,6 +83,7 @@ __BEGIN_DECLS O(SYS_SETSOCKOPT, setsockopt) \ O(SYS_REALPATH, realpath) \ O(SYS_TTYNAME, ttyname) \ + O(SYS_ACCESS, access) \ enum Syscall { diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index c7277d95a3..3a68e5ed44 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -496,3 +496,8 @@ char* ttyname(int fildes) return nullptr; return storage; } + +int access(const char* path, int amode) +{ + return syscall(SYS_ACCESS, path, amode); +}