Skip to content

Commit

Permalink
Kernel: Use the templated copy_to/from_user() in more places
Browse files Browse the repository at this point in the history
These ensure that the "to" and "from" pointers have the same type,
and also that we copy the correct number of bytes.
  • Loading branch information
awesomekling committed Jan 20, 2020
1 parent d5426fc commit e901a36
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
32 changes: 16 additions & 16 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ int Process::sys$lstat(const char* user_path, size_t path_length, stat* user_sta
auto result = metadata_or_error.value().stat(statbuf);
if (result.is_error())
return result;
copy_to_user(user_statbuf, &statbuf, sizeof(statbuf));
copy_to_user(user_statbuf, &statbuf);
return 0;
}

Expand All @@ -1802,7 +1802,7 @@ int Process::sys$stat(const char* user_path, size_t path_length, stat* user_stat
auto result = metadata_or_error.value().stat(statbuf);
if (result.is_error())
return result;
copy_to_user(user_statbuf, &statbuf, sizeof(statbuf));
copy_to_user(user_statbuf, &statbuf);
return 0;
}

Expand Down Expand Up @@ -2003,12 +2003,12 @@ int Process::sys$pipe(int pipefd[2], int flags)
int reader_fd = alloc_fd();
m_fds[reader_fd].set(fifo->open_direction(FIFO::Direction::Reader), fd_flags);
m_fds[reader_fd].description->set_readable(true);
copy_to_user(&pipefd[0], &reader_fd, sizeof(reader_fd));
copy_to_user(&pipefd[0], &reader_fd);

int writer_fd = alloc_fd();
m_fds[writer_fd].set(fifo->open_direction(FIFO::Direction::Writer), fd_flags);
m_fds[writer_fd].description->set_writable(true);
copy_to_user(&pipefd[1], &writer_fd, sizeof(writer_fd));
copy_to_user(&pipefd[1], &writer_fd);

return 0;
}
Expand Down Expand Up @@ -2333,7 +2333,7 @@ pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
}

if (wstatus)
copy_to_user(wstatus, &exit_status, sizeof(exit_status));
copy_to_user(wstatus, &exit_status);
return waitee_pid;
}

Expand Down Expand Up @@ -2517,7 +2517,7 @@ int Process::sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
if (old_set) {
if (!validate_write_typed(old_set))
return -EFAULT;
copy_to_user(old_set, &current->m_signal_mask, sizeof(current->m_signal_mask));
copy_to_user(old_set, &current->m_signal_mask);
}
if (set) {
if (!validate_read_typed(set))
Expand Down Expand Up @@ -2546,7 +2546,7 @@ int Process::sys$sigpending(sigset_t* set)
REQUIRE_PROMISE(stdio);
if (!validate_write_typed(set))
return -EFAULT;
copy_to_user(set, &current->m_pending_signals, sizeof(current->m_pending_signals));
copy_to_user(set, &current->m_pending_signals);
return 0;
}

Expand All @@ -2562,11 +2562,11 @@ int Process::sys$sigaction(int signum, const sigaction* act, sigaction* old_act)
if (old_act) {
if (!validate_write_typed(old_act))
return -EFAULT;
copy_to_user(&old_act->sa_flags, &action.flags, sizeof(action.flags));
copy_to_user(&old_act->sa_flags, &action.flags);
copy_to_user(&old_act->sa_sigaction, &action.handler_or_sigaction, sizeof(action.handler_or_sigaction));
}
copy_from_user(&action.flags, &act->sa_flags);
copy_from_user(&action.handler_or_sigaction, &act->sa_sigaction, sizeof(action.flags));
copy_from_user(&action.handler_or_sigaction, &act->sa_sigaction, sizeof(action.handler_or_sigaction));
return 0;
}

Expand Down Expand Up @@ -2649,10 +2649,10 @@ clock_t Process::sys$times(tms* times)
REQUIRE_PROMISE(stdio);
if (!validate_write_typed(times))
return -EFAULT;
copy_to_user(&times->tms_utime, &m_ticks_in_user, sizeof(m_ticks_in_user));
copy_to_user(&times->tms_stime, &m_ticks_in_kernel, sizeof(m_ticks_in_kernel));
copy_to_user(&times->tms_cutime, &m_ticks_in_user_for_dead_children, sizeof(m_ticks_in_user_for_dead_children));
copy_to_user(&times->tms_cstime, &m_ticks_in_kernel_for_dead_children, sizeof(m_ticks_in_kernel_for_dead_children));
copy_to_user(&times->tms_utime, &m_ticks_in_user);
copy_to_user(&times->tms_stime, &m_ticks_in_kernel);
copy_to_user(&times->tms_cutime, &m_ticks_in_user_for_dead_children);
copy_to_user(&times->tms_cstime, &m_ticks_in_kernel_for_dead_children);
return g_uptime & 0x7fffffff;
}

Expand Down Expand Up @@ -3297,7 +3297,7 @@ int Process::sys$sched_setparam(pid_t pid, const struct sched_param* param)
return -EFAULT;

int desired_priority;
copy_from_user(&desired_priority, &param->sched_priority, sizeof(desired_priority));
copy_from_user(&desired_priority, &param->sched_priority);

InterruptDisabler disabler;
auto* peer = this;
Expand Down Expand Up @@ -3336,7 +3336,7 @@ int Process::sys$sched_getparam(pid_t pid, struct sched_param* param)

// FIXME: This doesn't seem like the way to get the right thread!
int priority = peer->any_thread().priority();
copy_to_user(&param->sched_priority, &priority, sizeof(priority));
copy_to_user(&param->sched_priority, &priority);
return 0;
}

Expand Down Expand Up @@ -3708,7 +3708,7 @@ int Process::sys$join_thread(int tid, void** exit_value)
thread = nullptr;

if (exit_value)
copy_to_user(exit_value, &joinee_exit_value, sizeof(joinee_exit_value));
copy_to_user(exit_value, &joinee_exit_value);
return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions Kernel/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ bool Thread::has_signal_handler(u8 signal) const
static void push_value_on_user_stack(u32* stack, u32 data)
{
*stack -= 4;
copy_to_user((u32*)*stack, &data, sizeof(u32));
copy_to_user((u32*)*stack, &data);
}

ShouldUnblockThread Thread::dispatch_signal(u8 signal)
Expand Down Expand Up @@ -601,11 +601,11 @@ void Thread::set_default_signal_dispositions()
m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN);
}

void Thread::push_value_on_stack(u32 value)
void Thread::push_value_on_stack(uintptr_t value)
{
m_tss.esp -= 4;
u32* stack_ptr = (u32*)m_tss.esp;
copy_to_user(stack_ptr, &value, sizeof(value));
uintptr_t* stack_ptr = (uintptr_t*)m_tss.esp;
copy_to_user(stack_ptr, &value);
}

RegisterDump& Thread::get_register_dump_from_stack()
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class Thread {
FPUState& fpu_state() { return *m_fpu_state; }

void set_default_signal_dispositions();
void push_value_on_stack(u32);
void push_value_on_stack(uintptr_t);

u32 make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment);

Expand Down

0 comments on commit e901a36

Please sign in to comment.