diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2021-02-21 19:18:55 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-02 08:36:08 +0100 |
commit | 649abc01bcacd67abebdbc23c89a3a9b9917fc8f (patch) | |
tree | dc65c30a16e1d106e045f40edbd481401c6f7e9d | |
parent | bd6be910e5e518407e6a356b473c62d1c3555f75 (diff) | |
download | serenity-649abc01bcacd67abebdbc23c89a3a9b9917fc8f.zip |
Kernel: Implement 'copy_time_from_user' functions to sanitize arguments
-rw-r--r-- | Kernel/StdLib.cpp | 26 | ||||
-rw-r--r-- | Kernel/StdLib.h | 6 |
2 files changed, 32 insertions, 0 deletions
diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 00a7010453..48e7208409 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -63,6 +63,32 @@ String copy_string_from_user(Userspace<const char*> user_str, size_t user_str_si return copy_string_from_user(user_str.unsafe_userspace_ptr(), user_str_size); } +[[nodiscard]] Optional<Time> copy_time_from_user(const timespec* ts_user) +{ + timespec ts; + if (!copy_from_user(&ts, ts_user, sizeof(timespec))) { + return {}; + } + return Time::from_timespec(ts); +} +[[nodiscard]] Optional<Time> copy_time_from_user(const timeval* tv_user) +{ + timeval tv; + if (!copy_from_user(&tv, tv_user, sizeof(timeval))) { + return {}; + } + return Time::from_timeval(tv); +} + +template<> +[[nodiscard]] Optional<Time> copy_time_from_user<const timeval>(Userspace<const timeval*> src) { return copy_time_from_user(src.unsafe_userspace_ptr()); } +template<> +[[nodiscard]] Optional<Time> copy_time_from_user<timeval>(Userspace<timeval*> src) { return copy_time_from_user(src.unsafe_userspace_ptr()); } +template<> +[[nodiscard]] Optional<Time> copy_time_from_user<const timespec>(Userspace<const timespec*> src) { return copy_time_from_user(src.unsafe_userspace_ptr()); } +template<> +[[nodiscard]] Optional<Time> copy_time_from_user<timespec>(Userspace<timespec*> src) { return copy_time_from_user(src.unsafe_userspace_ptr()); } + Optional<u32> user_atomic_fetch_add_relaxed(volatile u32* var, u32 val) { if (FlatPtr(var) & 3) diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index 5fc7396f86..129cb1d470 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -28,7 +28,9 @@ #include <AK/Checked.h> #include <AK/Forward.h> +#include <AK/Time.h> #include <AK/Userspace.h> +#include <Kernel/UnixTypes.h> namespace Syscall { struct StringArgument; @@ -36,6 +38,10 @@ struct StringArgument; [[nodiscard]] String copy_string_from_user(const char*, size_t); [[nodiscard]] String copy_string_from_user(Userspace<const char*>, size_t); +[[nodiscard]] Optional<Time> copy_time_from_user(const timespec*); +[[nodiscard]] Optional<Time> copy_time_from_user(const timeval*); +template<typename T> +[[nodiscard]] Optional<Time> copy_time_from_user(Userspace<T*> src); [[nodiscard]] Optional<u32> user_atomic_fetch_add_relaxed(volatile u32* var, u32 val); [[nodiscard]] Optional<u32> user_atomic_exchange_relaxed(volatile u32* var, u32 val); |