From 1123af361dbea6f1c3ae0289aa676233a6e9d75b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 29 May 2021 16:59:40 +0200 Subject: [PATCH] Kernel: Convert Process::get_syscall_path_argument() to KString This API now returns a KResultOr> and allocation failures should be propagated everywhere nicely. :^) --- Kernel/Process.cpp | 12 ++++++------ Kernel/Process.h | 6 +++--- Kernel/Syscalls/access.cpp | 2 +- Kernel/Syscalls/chdir.cpp | 2 +- Kernel/Syscalls/chmod.cpp | 2 +- Kernel/Syscalls/chown.cpp | 2 +- Kernel/Syscalls/chroot.cpp | 2 +- Kernel/Syscalls/execve.cpp | 2 +- Kernel/Syscalls/inode_watcher.cpp | 2 +- Kernel/Syscalls/keymap.cpp | 9 ++++----- Kernel/Syscalls/link.cpp | 2 +- Kernel/Syscalls/mkdir.cpp | 2 +- Kernel/Syscalls/mknod.cpp | 2 +- Kernel/Syscalls/module.cpp | 2 +- Kernel/Syscalls/mount.cpp | 2 +- Kernel/Syscalls/open.cpp | 4 ++-- Kernel/Syscalls/readlink.cpp | 2 +- Kernel/Syscalls/realpath.cpp | 2 +- Kernel/Syscalls/rename.cpp | 2 +- Kernel/Syscalls/rmdir.cpp | 2 +- Kernel/Syscalls/stat.cpp | 2 +- Kernel/Syscalls/statvfs.cpp | 2 +- Kernel/Syscalls/unlink.cpp | 2 +- Kernel/Syscalls/unveil.cpp | 13 +++++++------ Kernel/Syscalls/utime.cpp | 2 +- 25 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 28bf00063d9..d55fbac717c 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -434,19 +434,19 @@ Custody& Process::current_directory() return *m_cwd; } -KResultOr Process::get_syscall_path_argument(const char* user_path, size_t path_length) const +KResultOr> Process::get_syscall_path_argument(char const* user_path, size_t path_length) const { if (path_length == 0) return EINVAL; if (path_length > PATH_MAX) return ENAMETOOLONG; - auto copied_string = copy_string_from_user(user_path, path_length); - if (copied_string.is_null()) - return EFAULT; - return copied_string; + auto string_or_error = try_copy_kstring_from_user(user_path, path_length); + if (string_or_error.is_error()) + return string_or_error.error(); + return string_or_error.release_value(); } -KResultOr Process::get_syscall_path_argument(const Syscall::StringArgument& path) const +KResultOr> Process::get_syscall_path_argument(Syscall::StringArgument const& path) const { return get_syscall_path_argument(path.characters, path.length); } diff --git a/Kernel/Process.h b/Kernel/Process.h index acd1b147564..176bcd665d1 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -546,12 +546,12 @@ private: KResultOr do_waitid(idtype_t idtype, int id, int options); - KResultOr get_syscall_path_argument(const char* user_path, size_t path_length) const; - KResultOr get_syscall_path_argument(Userspace user_path, size_t path_length) const + KResultOr> get_syscall_path_argument(const char* user_path, size_t path_length) const; + KResultOr> get_syscall_path_argument(Userspace user_path, size_t path_length) const { return get_syscall_path_argument(user_path.unsafe_userspace_ptr(), path_length); } - KResultOr get_syscall_path_argument(const Syscall::StringArgument&) const; + KResultOr> get_syscall_path_argument(const Syscall::StringArgument&) const; bool has_tracee_thread(ProcessID tracer_pid); diff --git a/Kernel/Syscalls/access.cpp b/Kernel/Syscalls/access.cpp index 8151b40e247..98d240b632c 100644 --- a/Kernel/Syscalls/access.cpp +++ b/Kernel/Syscalls/access.cpp @@ -16,7 +16,7 @@ KResultOr Process::sys$access(Userspace user_path, size_t path auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - return VFS::the().access(path.value(), mode, current_directory()); + return VFS::the().access(path.value()->view(), mode, current_directory()); } } diff --git a/Kernel/Syscalls/chdir.cpp b/Kernel/Syscalls/chdir.cpp index c85c4eb9027..c0aff802115 100644 --- a/Kernel/Syscalls/chdir.cpp +++ b/Kernel/Syscalls/chdir.cpp @@ -17,7 +17,7 @@ KResultOr Process::sys$chdir(Userspace user_path, size_t path_ auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - auto directory_or_error = VFS::the().open_directory(path.value(), current_directory()); + auto directory_or_error = VFS::the().open_directory(path.value()->view(), current_directory()); if (directory_or_error.is_error()) return directory_or_error.error(); m_cwd = *directory_or_error.value(); diff --git a/Kernel/Syscalls/chmod.cpp b/Kernel/Syscalls/chmod.cpp index 6e138c2fea5..eb352c5fbd6 100644 --- a/Kernel/Syscalls/chmod.cpp +++ b/Kernel/Syscalls/chmod.cpp @@ -17,7 +17,7 @@ KResultOr Process::sys$chmod(Userspace user_path, size_t path_ auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - return VFS::the().chmod(path.value(), mode, current_directory()); + return VFS::the().chmod(path.value()->view(), mode, current_directory()); } KResultOr Process::sys$fchmod(int fd, mode_t mode) diff --git a/Kernel/Syscalls/chown.cpp b/Kernel/Syscalls/chown.cpp index 4e0282c69b1..bfb50a666a0 100644 --- a/Kernel/Syscalls/chown.cpp +++ b/Kernel/Syscalls/chown.cpp @@ -27,7 +27,7 @@ KResultOr Process::sys$chown(Userspace use auto path = get_syscall_path_argument(params.path); if (path.is_error()) return path.error(); - return VFS::the().chown(path.value(), params.uid, params.gid, current_directory()); + return VFS::the().chown(path.value()->view(), params.uid, params.gid, current_directory()); } } diff --git a/Kernel/Syscalls/chroot.cpp b/Kernel/Syscalls/chroot.cpp index 4d6a63747c0..3313e86020a 100644 --- a/Kernel/Syscalls/chroot.cpp +++ b/Kernel/Syscalls/chroot.cpp @@ -19,7 +19,7 @@ KResultOr Process::sys$chroot(Userspace user_path, size_t path auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - auto directory_or_error = VFS::the().open_directory(path.value(), current_directory()); + auto directory_or_error = VFS::the().open_directory(path.value()->view(), current_directory()); if (directory_or_error.is_error()) return directory_or_error.error(); auto directory = directory_or_error.value(); diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 842202b889b..394d70958ea 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -920,7 +920,7 @@ KResultOr Process::sys$execve(Userspace u auto path_arg = get_syscall_path_argument(params.path); if (path_arg.is_error()) return path_arg.error(); - path = path_arg.value(); + path = path_arg.value()->view(); } auto copy_user_strings = [](const auto& list, auto& output) { diff --git a/Kernel/Syscalls/inode_watcher.cpp b/Kernel/Syscalls/inode_watcher.cpp index 537301974f9..6753f0c2d41 100644 --- a/Kernel/Syscalls/inode_watcher.cpp +++ b/Kernel/Syscalls/inode_watcher.cpp @@ -59,7 +59,7 @@ KResultOr Process::sys$inode_watcher_add_watch(Userspaceview(), current_directory()); if (custody_or_error.is_error()) return custody_or_error.error(); diff --git a/Kernel/Syscalls/keymap.cpp b/Kernel/Syscalls/keymap.cpp index f0ef2697873..0ba64ae214f 100644 --- a/Kernel/Syscalls/keymap.cpp +++ b/Kernel/Syscalls/keymap.cpp @@ -36,13 +36,12 @@ KResultOr Process::sys$setkeymap(Userspace map_name_max_size) { + if (map_name.value()->length() > map_name_max_size) return ENAMETOOLONG; - } - HIDManagement::the().set_maps(character_map_data, map_name.value()); + + HIDManagement::the().set_maps(character_map_data, map_name.value()->view()); return 0; } diff --git a/Kernel/Syscalls/link.cpp b/Kernel/Syscalls/link.cpp index 49b05c3c174..13f8dc07d98 100644 --- a/Kernel/Syscalls/link.cpp +++ b/Kernel/Syscalls/link.cpp @@ -37,7 +37,7 @@ KResultOr Process::sys$symlink(Userspace auto linkpath = get_syscall_path_argument(params.linkpath); if (linkpath.is_error()) return linkpath.error(); - return VFS::the().symlink(target.value(), linkpath.value(), current_directory()); + return VFS::the().symlink(target.value()->view(), linkpath.value()->view(), current_directory()); } } diff --git a/Kernel/Syscalls/mkdir.cpp b/Kernel/Syscalls/mkdir.cpp index 28fb0c21145..8af8348543b 100644 --- a/Kernel/Syscalls/mkdir.cpp +++ b/Kernel/Syscalls/mkdir.cpp @@ -16,6 +16,6 @@ KResultOr Process::sys$mkdir(Userspace user_path, size_t path_ auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory()); + return VFS::the().mkdir(path.value()->view(), mode & ~umask(), current_directory()); } } diff --git a/Kernel/Syscalls/mknod.cpp b/Kernel/Syscalls/mknod.cpp index d354089b4ab..5d3861c341c 100644 --- a/Kernel/Syscalls/mknod.cpp +++ b/Kernel/Syscalls/mknod.cpp @@ -21,7 +21,7 @@ KResultOr Process::sys$mknod(Userspace use auto path = get_syscall_path_argument(params.path); if (path.is_error()) return path.error(); - return VFS::the().mknod(path.value(), params.mode & ~umask(), params.dev, current_directory()); + return VFS::the().mknod(path.value()->view(), params.mode & ~umask(), params.dev, current_directory()); } } diff --git a/Kernel/Syscalls/module.cpp b/Kernel/Syscalls/module.cpp index 8b1ad648334..1ab7dd2ef08 100644 --- a/Kernel/Syscalls/module.cpp +++ b/Kernel/Syscalls/module.cpp @@ -25,7 +25,7 @@ KResultOr Process::sys$module_load(Userspace user_path, size_t auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory()); + auto description_or_error = VFS::the().open(path.value()->view(), O_RDONLY, 0, current_directory()); if (description_or_error.is_error()) return description_or_error.error(); auto& description = description_or_error.value(); diff --git a/Kernel/Syscalls/mount.cpp b/Kernel/Syscalls/mount.cpp index f9ad4ac934b..917d6968b47 100644 --- a/Kernel/Syscalls/mount.cpp +++ b/Kernel/Syscalls/mount.cpp @@ -119,7 +119,7 @@ KResultOr Process::sys$umount(Userspace user_mountpoint, size_ if (mountpoint.is_error()) return mountpoint.error(); - auto custody_or_error = VFS::the().resolve_path(mountpoint.value(), current_directory()); + auto custody_or_error = VFS::the().resolve_path(mountpoint.value()->view(), current_directory()); if (custody_or_error.is_error()) return custody_or_error.error(); diff --git a/Kernel/Syscalls/open.cpp b/Kernel/Syscalls/open.cpp index 8af325ff899..4239839fd11 100644 --- a/Kernel/Syscalls/open.cpp +++ b/Kernel/Syscalls/open.cpp @@ -43,7 +43,7 @@ KResultOr Process::sys$open(Userspace user_ if (path.is_error()) return path.error(); - dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value(), options, mode); + dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode); int fd = alloc_fd(); if (fd < 0) return fd; @@ -62,7 +62,7 @@ KResultOr Process::sys$open(Userspace user_ base = base_description->custody(); } - auto result = VFS::the().open(path.value(), options, mode & ~umask(), *base); + auto result = VFS::the().open(path.value()->view(), options, mode & ~umask(), *base); if (result.is_error()) return result.error(); auto description = result.value(); diff --git a/Kernel/Syscalls/readlink.cpp b/Kernel/Syscalls/readlink.cpp index 0305f551740..ad331a10336 100644 --- a/Kernel/Syscalls/readlink.cpp +++ b/Kernel/Syscalls/readlink.cpp @@ -23,7 +23,7 @@ KResultOr Process::sys$readlink(Userspaceview(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory()); if (result.is_error()) return result.error(); auto description = result.value(); diff --git a/Kernel/Syscalls/realpath.cpp b/Kernel/Syscalls/realpath.cpp index c58b3443a8b..5bbe29db7bf 100644 --- a/Kernel/Syscalls/realpath.cpp +++ b/Kernel/Syscalls/realpath.cpp @@ -23,7 +23,7 @@ KResultOr Process::sys$realpath(Userspaceview(), current_directory()); if (custody_or_error.is_error()) return custody_or_error.error(); auto& custody = custody_or_error.value(); diff --git a/Kernel/Syscalls/rename.cpp b/Kernel/Syscalls/rename.cpp index 22fda00580b..4aaea17db2d 100644 --- a/Kernel/Syscalls/rename.cpp +++ b/Kernel/Syscalls/rename.cpp @@ -22,7 +22,7 @@ KResultOr Process::sys$rename(Userspace u auto new_path = get_syscall_path_argument(params.new_path); if (new_path.is_error()) return new_path.error(); - return VFS::the().rename(old_path.value(), new_path.value(), current_directory()); + return VFS::the().rename(old_path.value()->view(), new_path.value()->view(), current_directory()); } } diff --git a/Kernel/Syscalls/rmdir.cpp b/Kernel/Syscalls/rmdir.cpp index 627c32536e4..921871f4417 100644 --- a/Kernel/Syscalls/rmdir.cpp +++ b/Kernel/Syscalls/rmdir.cpp @@ -16,7 +16,7 @@ KResultOr Process::sys$rmdir(Userspace user_path, size_t path_ auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - return VFS::the().rmdir(path.value(), current_directory()); + return VFS::the().rmdir(path.value()->view(), current_directory()); } } diff --git a/Kernel/Syscalls/stat.cpp b/Kernel/Syscalls/stat.cpp index d75f6a677fb..810daed284a 100644 --- a/Kernel/Syscalls/stat.cpp +++ b/Kernel/Syscalls/stat.cpp @@ -47,7 +47,7 @@ KResultOr Process::sys$stat(Userspace user_ return EINVAL; base = base_description->custody(); } - auto metadata_or_error = VFS::the().lookup_metadata(path.value(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR); + auto metadata_or_error = VFS::the().lookup_metadata(path.value()->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR); if (metadata_or_error.is_error()) return metadata_or_error.error(); stat statbuf; diff --git a/Kernel/Syscalls/statvfs.cpp b/Kernel/Syscalls/statvfs.cpp index 2f6d4d35539..5103567e2a3 100644 --- a/Kernel/Syscalls/statvfs.cpp +++ b/Kernel/Syscalls/statvfs.cpp @@ -80,7 +80,7 @@ KResultOr Process::sys$statvfs(Userspace if (path.is_error()) return path.error(); - return do_statvfs(path.value(), params.buf); + return do_statvfs(path.value()->view(), params.buf); } KResultOr Process::sys$fstatvfs(int fd, statvfs* buf) diff --git a/Kernel/Syscalls/unlink.cpp b/Kernel/Syscalls/unlink.cpp index 8e8538d7c84..4eb5f99d352 100644 --- a/Kernel/Syscalls/unlink.cpp +++ b/Kernel/Syscalls/unlink.cpp @@ -16,7 +16,7 @@ KResultOr Process::sys$unlink(Userspace user_path, size_t path auto path = get_syscall_path_argument(user_path, path_length); if (path.is_error()) return path.error(); - return VFS::the().unlink(path.value(), current_directory()); + return VFS::the().unlink(path.value()->view(), current_directory()); } } diff --git a/Kernel/Syscalls/unveil.cpp b/Kernel/Syscalls/unveil.cpp index 9ccee903153..9e4257964c9 100644 --- a/Kernel/Syscalls/unveil.cpp +++ b/Kernel/Syscalls/unveil.cpp @@ -32,11 +32,12 @@ KResultOr Process::sys$unveil(Userspace u if (params.permissions.length > 5) return EINVAL; - auto path = get_syscall_path_argument(params.path); - if (path.is_error()) - return path.error(); + auto path_or_error = get_syscall_path_argument(params.path); + if (path_or_error.is_error()) + return path_or_error.error(); + auto& path = *path_or_error.value(); - if (path.value().is_empty() || path.value().characters()[0] != '/') + if (path.is_empty() || !path.view().starts_with('/')) return EINVAL; auto permissions = copy_string_from_user(params.permissions); @@ -74,11 +75,11 @@ KResultOr Process::sys$unveil(Userspace u // If this case is encountered, the parent node of the path is returned and the custody of that inode is used instead. RefPtr parent_custody; // Parent inode in case of ENOENT String new_unveiled_path; - auto custody_or_error = VFS::the().resolve_path_without_veil(path.value(), root_directory(), &parent_custody); + auto custody_or_error = VFS::the().resolve_path_without_veil(path.view(), root_directory(), &parent_custody); if (!custody_or_error.is_error()) { new_unveiled_path = custody_or_error.value()->absolute_path(); } else if (custody_or_error.error() == -ENOENT && parent_custody && (new_permissions & UnveilAccess::CreateOrRemove)) { - String basename = LexicalPath(path.value()).basename(); + String basename = LexicalPath(path.view()).basename(); new_unveiled_path = String::formatted("{}/{}", parent_custody->absolute_path(), basename); } else { // FIXME Should this be EINVAL? diff --git a/Kernel/Syscalls/utime.cpp b/Kernel/Syscalls/utime.cpp index 31c22d04687..f7ec599339d 100644 --- a/Kernel/Syscalls/utime.cpp +++ b/Kernel/Syscalls/utime.cpp @@ -25,7 +25,7 @@ KResultOr Process::sys$utime(Userspace user_path, size_t path_ // Not a bug! buf = { now, now }; } - return VFS::the().utime(path.value(), current_directory(), buf.actime, buf.modtime); + return VFS::the().utime(path.value()->view(), current_directory(), buf.actime, buf.modtime); } }