1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:18:11 +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:
Tom 2020-09-11 21:11:07 -06:00 committed by Andreas Kling
parent 7d1b8417bd
commit c8d9f1b9c9
149 changed files with 1585 additions and 1244 deletions

View file

@ -36,22 +36,16 @@ namespace Kernel {
int Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
{
REQUIRE_PROMISE(thread);
if (!validate_read((const void*)entry, sizeof(void*)))
return -EFAULT;
Syscall::SC_create_thread_params params;
if (!validate_read_and_copy_typed(&params, user_params))
if (!copy_from_user(&params, user_params))
return -EFAULT;
unsigned detach_state = params.m_detach_state;
int schedule_priority = params.m_schedule_priority;
Userspace<void*> stack_location = params.m_stack_location;
unsigned stack_size = params.m_stack_size;
if (!validate_write(stack_location, stack_size))
return -EFAULT;
u32 user_stack_address = reinterpret_cast<u32>(stack_location.ptr()) + stack_size;
auto user_stack_address = (u8*)params.m_stack_location + stack_size;
if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
return -EFAULT;
@ -83,7 +77,7 @@ int Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::S
tss.eip = (FlatPtr)entry;
tss.eflags = 0x0202;
tss.cr3 = page_directory().cr3();
tss.esp = user_stack_address;
tss.esp = (u32)user_stack_address;
thread->make_thread_specific_region({});
thread->set_state(Thread::State::Runnable);
@ -120,8 +114,6 @@ int Process::sys$detach_thread(pid_t tid)
int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
{
REQUIRE_PROMISE(thread);
if (exit_value && !validate_write_typed(exit_value))
return -EFAULT;
InterruptDisabler disabler;
auto* thread = Thread::from_tid(tid);
@ -164,15 +156,15 @@ int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
// NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
thread = nullptr;
if (exit_value)
copy_to_user(exit_value, &joinee_exit_value);
if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
return -EFAULT;
return 0;
}
int Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
{
REQUIRE_PROMISE(thread);
auto name = validate_and_copy_string_from_user(user_name, user_name_length);
auto name = copy_string_from_user(user_name, user_name_length);
if (name.is_null())
return -EFAULT;
@ -185,7 +177,7 @@ int Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, si
if (!thread || thread->pid() != pid())
return -ESRCH;
thread->set_name(name);
thread->set_name(move(name));
return 0;
}
@ -195,9 +187,6 @@ int Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buff
if (buffer_size == 0)
return -EINVAL;
if (!validate_write(buffer, buffer_size))
return -EFAULT;
InterruptDisabler disabler;
auto* thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
@ -206,7 +195,8 @@ int Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buff
if (thread->name().length() + 1 > (size_t)buffer_size)
return -ENAMETOOLONG;
copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1);
if (!copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1))
return -EFAULT;
return 0;
}