summaryrefslogtreecommitdiff
path: root/Kernel/Ptrace.cpp
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-09-11 21:11:07 -0600
committerAndreas Kling <kling@serenityos.org>2020-09-13 21:19:15 +0200
commitc8d9f1b9c920e0314bb9ca67f183c8df743e845a (patch)
tree773e9fb30d26602598ba309291b8e6d2e80475e6 /Kernel/Ptrace.cpp
parent7d1b8417bdf5d2818d1c9d310786cdf59650b104 (diff)
downloadserenity-c8d9f1b9c920e0314bb9ca67f183c8df743e845a.zip
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.
Diffstat (limited to 'Kernel/Ptrace.cpp')
-rw-r--r--Kernel/Ptrace.cpp16
1 files changed, 7 insertions, 9 deletions
diff --git a/Kernel/Ptrace.cpp b/Kernel/Ptrace.cpp
index ab085c3054..8e8bc6c7bf 100644
--- a/Kernel/Ptrace.cpp
+++ b/Kernel/Ptrace.cpp
@@ -105,10 +105,9 @@ KResultOr<u32> handle_syscall(const Kernel::Syscall::SC_ptrace_params& params, P
if (!tracer->has_regs())
return KResult(-EINVAL);
- auto* regs = reinterpret_cast<PtraceRegisters*>(params.addr.unsafe_userspace_ptr());
- if (!caller.validate_write_typed(regs))
+ auto* regs = reinterpret_cast<PtraceRegisters*>(params.addr);
+ if (!copy_to_user(regs, &tracer->regs()))
return KResult(-EFAULT);
- copy_to_user(regs, &tracer->regs());
break;
}
@@ -117,7 +116,7 @@ KResultOr<u32> handle_syscall(const Kernel::Syscall::SC_ptrace_params& params, P
return KResult(-EINVAL);
PtraceRegisters regs;
- if (!caller.validate_read_and_copy_typed(&regs, (const PtraceRegisters*)params.addr.unsafe_userspace_ptr()))
+ if (!copy_from_user(&regs, (const PtraceRegisters*)params.addr))
return KResult(-EFAULT);
auto& peer_saved_registers = peer->get_register_dump_from_stack();
@@ -132,21 +131,20 @@ KResultOr<u32> handle_syscall(const Kernel::Syscall::SC_ptrace_params& params, P
case PT_PEEK: {
Kernel::Syscall::SC_ptrace_peek_params peek_params;
- if (!caller.validate_read_and_copy_typed(&peek_params, reinterpret_cast<Kernel::Syscall::SC_ptrace_peek_params*>(params.addr.unsafe_userspace_ptr())))
+ if (!copy_from_user(&peek_params, reinterpret_cast<Kernel::Syscall::SC_ptrace_peek_params*>(params.addr)))
return -EFAULT;
// read validation is done inside 'peek_user_data'
- auto result = peer->process().peek_user_data(peek_params.address);
+ auto result = peer->process().peek_user_data((FlatPtr)peek_params.address);
if (result.is_error())
return -EFAULT;
- if (!caller.validate_write(peek_params.out_data, sizeof(u32)))
+ if (!copy_to_user(peek_params.out_data, &result.value()))
return -EFAULT;
- copy_to_user(peek_params.out_data, &result.value());
break;
}
case PT_POKE: {
- Userspace<u32*> addr = reinterpret_cast<FlatPtr>(params.addr.ptr());
+ Userspace<u32*> addr = reinterpret_cast<FlatPtr>(params.addr);
// write validation is done inside 'poke_user_data'
return peer->process().poke_user_data(addr, params.data);
}