From 628b3badfbb676bc598e10cffe7beb5f85c59740 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 31 Jul 2020 18:46:55 +0200 Subject: [PATCH] Kernel+AK: Add and use Userspace::unsafe_userspace_ptr() Since we already have the type information in the Userspace template, it was a bit silly to cast manually everywhere. Just add a sufficiently scary-sounding getter for a typed pointer. Thanks @alimpfard for pointing out that I was being silly with tossing out the type. In the future we may want to make this API non-public as well. --- AK/Userspace.h | 1 + Kernel/Process.h | 14 +++++++------- Kernel/StdLib.h | 8 ++++---- Kernel/Syscalls/read.cpp | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/AK/Userspace.h b/AK/Userspace.h index 7d61c41625..8f02f6d72f 100644 --- a/AK/Userspace.h +++ b/AK/Userspace.h @@ -45,6 +45,7 @@ public: } FlatPtr ptr() const { return m_ptr; } + T unsafe_userspace_ptr() const { return (T)m_ptr; } #else Userspace(T ptr) : m_ptr(ptr) diff --git a/Kernel/Process.h b/Kernel/Process.h index f5732aaaa1..25a88a9fd6 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -360,13 +360,13 @@ public: template [[nodiscard]] bool validate_read(Userspace ptr, size_t size) const { - return validate_read((const T*)ptr.ptr(), size); + return validate_read(ptr.unsafe_userspace_ptr(), size); } template [[nodiscard]] bool validate_write(Userspace ptr, size_t size) const { - return validate_write((T*)ptr.ptr(), size); + return validate_write(ptr.unsafe_userspace_ptr(), size); } template @@ -402,9 +402,9 @@ public: template [[nodiscard]] bool validate_read_and_copy_typed(T* dest, Userspace src) { - bool validated = validate_read_typed((const T*)src.ptr()); + bool validated = validate_read_typed(src); if (validated) { - copy_from_user(dest, (const T*)src.ptr()); + copy_from_user(dest, src); } return validated; } @@ -426,7 +426,7 @@ public: size *= count; if (size.has_overflow()) return false; - return validate_write((T*)value.ptr(), size.value()); + return validate_write(value, size.value()); } template @@ -445,7 +445,7 @@ public: [[nodiscard]] String validate_and_copy_string_from_user(Userspace user_characters, size_t size) const { - return validate_and_copy_string_from_user((const char*)user_characters.ptr(), size); + return validate_and_copy_string_from_user(user_characters.unsafe_userspace_ptr(), size); } [[nodiscard]] String validate_and_copy_string_from_user(const Syscall::StringArgument&) const; @@ -597,7 +597,7 @@ private: 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((const char*)user_path.ptr(), path_length); + return get_syscall_path_argument(user_path.unsafe_userspace_ptr(), path_length); } KResultOr get_syscall_path_argument(const Syscall::StringArgument&) const; diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index 983e6ab8a2..994c514123 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -75,23 +75,23 @@ inline void copy_to_user(T* dest, const T* src) template inline void copy_from_user(T* dest, Userspace src) { - copy_from_user(dest, (const T*)src.ptr(), sizeof(T)); + copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T)); } template inline void copy_to_user(Userspace dest, const T* src) { - copy_to_user((T*)dest.ptr(), src, sizeof(T)); + copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T)); } template inline void copy_to_user(Userspace dest, const void* src, size_t size) { - copy_to_user((void*)dest.ptr(), src, size); + copy_to_user(dest.unsafe_userspace_ptr(), src, size); } template inline void copy_from_user(void* dest, Userspace src, size_t size) { - copy_from_user(dest, (const void*)src.ptr(), size); + copy_from_user(dest, src.unsafe_userspace_ptr(), size); } diff --git a/Kernel/Syscalls/read.cpp b/Kernel/Syscalls/read.cpp index f76bbe5106..9832b09b76 100644 --- a/Kernel/Syscalls/read.cpp +++ b/Kernel/Syscalls/read.cpp @@ -57,7 +57,7 @@ ssize_t Process::sys$read(int fd, Userspace buffer, ssize_t size) } } // FIXME: We should have a read() that takes a Userspace - return description->read((u8*)buffer.ptr(), size); + return description->read(buffer.unsafe_userspace_ptr(), size); } }