mirror of
https://github.com/RGBCube/serenity
synced 2025-07-10 08:57:35 +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
|
@ -32,8 +32,6 @@ namespace Kernel {
|
|||
int Process::sys$clock_gettime(clockid_t clock_id, Userspace<timespec*> user_ts)
|
||||
{
|
||||
REQUIRE_PROMISE(stdio);
|
||||
if (!validate_write_typed(user_ts))
|
||||
return -EFAULT;
|
||||
|
||||
timespec ts = {};
|
||||
|
||||
|
@ -49,7 +47,8 @@ int Process::sys$clock_gettime(clockid_t clock_id, Userspace<timespec*> user_ts)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
copy_to_user(user_ts, &ts);
|
||||
if (!copy_to_user(user_ts, &ts))
|
||||
return -EFAULT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -61,7 +60,7 @@ int Process::sys$clock_settime(clockid_t clock_id, Userspace<const timespec*> us
|
|||
return -EPERM;
|
||||
|
||||
timespec ts;
|
||||
if (!validate_read_and_copy_typed(&ts, user_ts))
|
||||
if (!copy_from_user(&ts, user_ts))
|
||||
return -EFAULT;
|
||||
|
||||
switch (clock_id) {
|
||||
|
@ -79,16 +78,11 @@ int Process::sys$clock_nanosleep(Userspace<const Syscall::SC_clock_nanosleep_par
|
|||
REQUIRE_PROMISE(stdio);
|
||||
|
||||
Syscall::SC_clock_nanosleep_params params;
|
||||
if (!validate_read_and_copy_typed(¶ms, user_params))
|
||||
return -EFAULT;
|
||||
|
||||
if (params.requested_sleep && !validate_read_typed(params.requested_sleep))
|
||||
if (!copy_from_user(¶ms, user_params))
|
||||
return -EFAULT;
|
||||
|
||||
timespec requested_sleep;
|
||||
copy_from_user(&requested_sleep, params.requested_sleep);
|
||||
|
||||
if (params.remaining_sleep && !validate_write_typed(params.remaining_sleep))
|
||||
if (!copy_from_user(&requested_sleep, params.requested_sleep))
|
||||
return -EFAULT;
|
||||
|
||||
bool is_absolute = params.flags & TIMER_ABSTIME;
|
||||
|
@ -109,18 +103,12 @@ int Process::sys$clock_nanosleep(Userspace<const Syscall::SC_clock_nanosleep_par
|
|||
if (wakeup_time > g_uptime) {
|
||||
u64 ticks_left = wakeup_time - g_uptime;
|
||||
if (!is_absolute && params.remaining_sleep) {
|
||||
if (!validate_write_typed(params.remaining_sleep)) {
|
||||
// This can happen because the lock is dropped while
|
||||
// sleeping, thus giving other threads the opportunity
|
||||
// to make the region unwritable.
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
timespec remaining_sleep = {};
|
||||
remaining_sleep.tv_sec = ticks_left / TimeManagement::the().ticks_per_second();
|
||||
ticks_left -= remaining_sleep.tv_sec * TimeManagement::the().ticks_per_second();
|
||||
remaining_sleep.tv_nsec = ticks_left * 1000000000 / TimeManagement::the().ticks_per_second();
|
||||
copy_to_user(params.remaining_sleep, &remaining_sleep);
|
||||
if (!copy_to_user(params.remaining_sleep, &remaining_sleep))
|
||||
return -EFAULT;
|
||||
}
|
||||
return -EINTR;
|
||||
}
|
||||
|
@ -134,10 +122,9 @@ int Process::sys$clock_nanosleep(Userspace<const Syscall::SC_clock_nanosleep_par
|
|||
int Process::sys$gettimeofday(Userspace<timeval*> user_tv)
|
||||
{
|
||||
REQUIRE_PROMISE(stdio);
|
||||
if (!validate_write_typed(user_tv))
|
||||
return -EFAULT;
|
||||
auto tv = kgettimeofday();
|
||||
copy_to_user(user_tv, &tv);
|
||||
if (!copy_to_user(user_tv, &tv))
|
||||
return -EFAULT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue