mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:07:34 +00:00
Kernel: Make copy_to/from_user safe and remove unnecessary checks
Since the CPU already does almost all necessary validation steps for us, we don't really need to attempt to do this. Doing it ourselves doesn't really work very reliably, because we'd have to account for other processors modifying virtual memory, and we'd have to account for e.g. pages not being able to be allocated due to insufficient resources. So change the copy_to/from_user (and associated helper functions) to use the new safe_memcpy, which will return whether it succeeded or not. The only manual validation step needed (which the CPU can't perform for us) is making sure the pointers provided by user mode aren't pointing to kernel mappings. To make it easier to read/write from/to either kernel or user mode data add the UserOrKernelBuffer helper class, which will internally either use copy_from/to_user or directly memcpy, or pass the data through directly using a temporary buffer on the stack. Last but not least we need to keep syscall params trivial as we need to copy them from/to user mode using copy_from/to_user.
This commit is contained in:
parent
7d1b8417bd
commit
c8d9f1b9c9
149 changed files with 1585 additions and 1244 deletions
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Checked.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/Userspace.h>
|
||||
|
||||
|
@ -34,12 +35,13 @@ struct StringArgument;
|
|||
}
|
||||
|
||||
String copy_string_from_user(const char*, size_t);
|
||||
String copy_string_from_user(Userspace<const char*>, size_t);
|
||||
|
||||
extern "C" {
|
||||
|
||||
void copy_to_user(void*, const void*, size_t);
|
||||
void copy_from_user(void*, const void*, size_t);
|
||||
void memset_user(void*, int, size_t);
|
||||
[[nodiscard]] bool copy_to_user(void*, const void*, size_t);
|
||||
[[nodiscard]] bool copy_from_user(void*, const void*, size_t);
|
||||
[[nodiscard]] bool memset_user(void*, int, size_t);
|
||||
|
||||
void* memcpy(void*, const void*, size_t);
|
||||
int strncmp(const char* s1, const char* s2, size_t n);
|
||||
|
@ -57,37 +59,77 @@ inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_from_user(T* dest, const T* src)
|
||||
[[nodiscard]] inline bool copy_from_user(T* dest, const T* src)
|
||||
{
|
||||
copy_from_user(dest, src, sizeof(T));
|
||||
return copy_from_user(dest, src, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_to_user(T* dest, const T* src)
|
||||
[[nodiscard]] inline bool copy_to_user(T* dest, const T* src)
|
||||
{
|
||||
copy_to_user(dest, src, sizeof(T));
|
||||
return copy_to_user(dest, src, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_from_user(T* dest, Userspace<const T*> src)
|
||||
[[nodiscard]] inline bool copy_from_user(T* dest, Userspace<const T*> src)
|
||||
{
|
||||
copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T));
|
||||
return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_to_user(Userspace<T*> dest, const T* src)
|
||||
[[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const T* src)
|
||||
{
|
||||
copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T));
|
||||
return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_to_user(Userspace<T*> dest, const void* src, size_t size)
|
||||
[[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const void* src, size_t size)
|
||||
{
|
||||
copy_to_user(dest.unsafe_userspace_ptr(), src, size);
|
||||
return copy_to_user(dest.unsafe_userspace_ptr(), src, size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void copy_from_user(void* dest, Userspace<const T*> src, size_t size)
|
||||
[[nodiscard]] inline bool copy_from_user(void* dest, Userspace<const T*> src, size_t size)
|
||||
{
|
||||
copy_from_user(dest, src.unsafe_userspace_ptr(), size);
|
||||
return copy_from_user(dest, src.unsafe_userspace_ptr(), size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline bool copy_n_from_user(T* dest, const T* src, size_t count)
|
||||
{
|
||||
Checked size = sizeof(T);
|
||||
size *= count;
|
||||
if (size.has_overflow())
|
||||
return false;
|
||||
return copy_from_user(dest, src, sizeof(T) * count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline bool copy_n_to_user(T* dest, const T* src, size_t count)
|
||||
{
|
||||
Checked size = sizeof(T);
|
||||
size *= count;
|
||||
if (size.has_overflow())
|
||||
return false;
|
||||
return copy_to_user(dest, src, sizeof(T) * count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline bool copy_n_from_user(T* dest, Userspace<const T*> src, size_t count)
|
||||
{
|
||||
Checked size = sizeof(T);
|
||||
size *= count;
|
||||
if (size.has_overflow())
|
||||
return false;
|
||||
return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T) * count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline bool copy_n_to_user(Userspace<T*> dest, const T* src, size_t count)
|
||||
{
|
||||
Checked size = sizeof(T);
|
||||
size *= count;
|
||||
if (size.has_overflow())
|
||||
return false;
|
||||
return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T) * count);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue