From 708a720d9dbe9e63189f79a9e7b589b7909bb91a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 17 Sep 2024 16:16:47 +0300 Subject: [PATCH] Kernel: Remove SYS_OPEN This can be done with SYS_OPENAT with fd set to AT_FDCWD --- kernel/include/kernel/Process.h | 2 - kernel/kernel/Process.cpp | 60 +++++++------------ userspace/libraries/LibC/fcntl.cpp | 2 +- .../libraries/LibC/include/sys/syscall.h | 1 - userspace/programs/DynamicLoader/main.cpp | 4 +- userspace/programs/DynamicLoader/utils.cpp | 2 +- 6 files changed, 27 insertions(+), 44 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 49a2fb49..c7ba34b4 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -102,8 +102,6 @@ namespace Kernel BAN::ErrorOr open_inode(VirtualFileSystem::File&&, int flags); BAN::ErrorOr create_file_or_dir(const VirtualFileSystem::File& parent, BAN::StringView path, mode_t mode) const; - BAN::ErrorOr open_file_impl(const VirtualFileSystem::File& parent, BAN::StringView path, int oflag, mode_t = 0); - BAN::ErrorOr sys_open(const char* path, int, mode_t); BAN::ErrorOr sys_openat(int, const char* path, int, mode_t); BAN::ErrorOr sys_close(int fd); BAN::ErrorOr sys_read(int fd, void* buffer, size_t count); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 38de3ba6..bdebc53d 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -849,14 +849,31 @@ namespace Kernel return TRY(m_open_file_descriptors.open(BAN::move(file), flags)); } - BAN::ErrorOr Process::open_file_impl(const VirtualFileSystem::File& parent, BAN::StringView path, int flags, mode_t mode) + BAN::ErrorOr Process::sys_openat(int fd, const char* path, int flags, mode_t mode) { if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT)) return BAN::Error::from_errno(EINVAL); LockGuard _(m_process_lock); - auto file_or_error = VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags | O_NOFOLLOW); + TRY(validate_string_access(path)); + + VirtualFileSystem::File parent_file; + if (path[0] == '/') + parent_file = VirtualFileSystem::get().root_file(); + else if (fd == AT_FDCWD) + parent_file = TRY(m_working_directory.clone()); + else + { + int flags = TRY(m_open_file_descriptors.flags_of(fd)); + if (!(flags & O_RDONLY) && !(flags & O_SEARCH)) + return BAN::Error::from_errno(EBADF); + if (!TRY(m_open_file_descriptors.inode_of(fd))->mode().ifdir()) + return BAN::Error::from_errno(ENOTDIR); + parent_file = TRY(m_open_file_descriptors.file_of(fd)); + } + + auto file_or_error = VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags | O_NOFOLLOW); VirtualFileSystem::File file; if (file_or_error.is_error()) @@ -865,8 +882,8 @@ namespace Kernel return file_or_error.release_error(); // FIXME: There is a race condition between next two lines - TRY(create_file_or_dir(parent, path, (mode & 0777) | Inode::Mode::IFREG)); - file = TRY(VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags)); + TRY(create_file_or_dir(parent_file, path, (mode & 0777) | Inode::Mode::IFREG)); + file = TRY(VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags)); } else { @@ -879,13 +896,13 @@ namespace Kernel if (!file.inode->mode().ifdir() && (flags & O_DIRECTORY)) return BAN::Error::from_errno(ENOTDIR); if (file.inode->mode().iflnk() && !(flags & O_NOFOLLOW)) - file = TRY(VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags)); + file = TRY(VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags)); } auto inode = file.inode; ASSERT(inode); - int fd = TRY(m_open_file_descriptors.open(BAN::move(file), flags)); + fd = TRY(m_open_file_descriptors.open(BAN::move(file), flags)); // Open controlling terminal if (!(flags & O_NOCTTY) && inode->is_tty() && is_session_leader() && !m_controlling_terminal) @@ -894,37 +911,6 @@ namespace Kernel return fd; } - BAN::ErrorOr Process::sys_open(const char* path, int flags, mode_t mode) - { - LockGuard _(m_process_lock); - TRY(validate_string_access(path)); - if (path[0] == '/') - return open_file_impl(VirtualFileSystem::get().root_file(), path, flags, mode); - return open_file_impl(m_working_directory, path, flags, mode); - } - - BAN::ErrorOr Process::sys_openat(int fd, const char* path, int flags, mode_t mode) - { - LockGuard _(m_process_lock); - - TRY(validate_string_access(path)); - - VirtualFileSystem::File parent_file; - if (fd == AT_FDCWD) - parent_file = TRY(m_working_directory.clone()); - else if (path[0] != '/') - { - int flags = TRY(m_open_file_descriptors.flags_of(fd)); - if (!(flags & O_RDONLY) && !(flags & O_SEARCH)) - return BAN::Error::from_errno(EBADF); - if (!TRY(m_open_file_descriptors.inode_of(fd))->mode().ifdir()) - return BAN::Error::from_errno(ENOTDIR); - parent_file = TRY(m_open_file_descriptors.file_of(fd)); - } - - return open_file_impl(parent_file, path, flags, mode); - } - BAN::ErrorOr Process::sys_close(int fd) { LockGuard _(m_process_lock); diff --git a/userspace/libraries/LibC/fcntl.cpp b/userspace/libraries/LibC/fcntl.cpp index 809f2c99..a48199ff 100644 --- a/userspace/libraries/LibC/fcntl.cpp +++ b/userspace/libraries/LibC/fcntl.cpp @@ -15,7 +15,7 @@ int open(const char* path, int oflag, ...) mode_t mode = va_arg(args, mode_t); va_end(args); - return syscall(SYS_OPEN, path, oflag, __UMASKED_MODE(mode)); + return openat(AT_FDCWD, path, oflag, mode); } int openat(int fd, const char* path, int oflag, ...) diff --git a/userspace/libraries/LibC/include/sys/syscall.h b/userspace/libraries/LibC/include/sys/syscall.h index 1746c153..9f873a88 100644 --- a/userspace/libraries/LibC/include/sys/syscall.h +++ b/userspace/libraries/LibC/include/sys/syscall.h @@ -11,7 +11,6 @@ __BEGIN_DECLS O(SYS_WRITE, write) \ O(SYS_TERMID, termid) \ O(SYS_CLOSE, close) \ - O(SYS_OPEN, open) \ O(SYS_OPENAT, openat) \ O(SYS_SEEK, seek) \ O(SYS_TELL, tell) \ diff --git a/userspace/programs/DynamicLoader/main.cpp b/userspace/programs/DynamicLoader/main.cpp index 03115179..3bd6547f 100644 --- a/userspace/programs/DynamicLoader/main.cpp +++ b/userspace/programs/DynamicLoader/main.cpp @@ -557,7 +557,7 @@ static void handle_dynamic(LoadedElf& elf) if (auto ret = syscall(SYS_REALPATH, path_buffer, realpath); ret < 0) print_error_and_exit("realpath", ret); - int library_fd = syscall(SYS_OPEN, realpath, O_RDONLY); + int library_fd = syscall(SYS_OPENAT, AT_FDCWD, realpath, O_RDONLY); if (library_fd < 0) print_error_and_exit("could not open library", library_fd); @@ -882,7 +882,7 @@ int _entry(int argc, char** argv, char** envp, int fd) argc--; argv++; - fd = syscall(SYS_OPEN, argv[0], O_RDONLY); + fd = syscall(SYS_OPENAT, AT_FDCWD, argv[0], O_RDONLY); if (fd < 0) print_error_and_exit("could not open program", fd); } diff --git a/userspace/programs/DynamicLoader/utils.cpp b/userspace/programs/DynamicLoader/utils.cpp index 442ecd8b..922bfb44 100644 --- a/userspace/programs/DynamicLoader/utils.cpp +++ b/userspace/programs/DynamicLoader/utils.cpp @@ -68,7 +68,7 @@ static int s_random_fd; void init_random() { - s_random_fd = syscall(SYS_OPEN, "/dev/random", O_RDONLY); + s_random_fd = syscall(SYS_OPENAT, AT_FDCWD, "/dev/random", O_RDONLY); if (s_random_fd < 0) print_error_and_exit("could not open /dev/random", s_random_fd); }