diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 49a2fb499c..c7ba34b4b2 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 38de3ba60e..bdebc53d16 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 809f2c99af..a48199ff0e 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 1746c15354..9f873a88c0 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 03115179d4..3bd6547ff2 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 442ecd8bb1..922bfb44b2 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); }