mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-03 08:52:54 +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
Notes:
sideshowbarker
2024-07-19 02:42:35 +09:00
Author: https://github.com/tomuta
Commit: c8d9f1b9c9
Pull-request: https://github.com/SerenityOS/serenity/pull/3467
Reviewed-by: https://github.com/awesomekling
149 changed files with 1585 additions and 1244 deletions
|
@ -423,7 +423,8 @@ KResultOr<NonnullRefPtr<FileDescription>> Process::find_elf_interpreter_for_exec
|
|||
return KResult(-ENOEXEC);
|
||||
|
||||
memset(first_page, 0, sizeof(first_page));
|
||||
auto nread_or_error = interpreter_description->read((u8*)&first_page, sizeof(first_page));
|
||||
auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
|
||||
auto nread_or_error = interpreter_description->read(first_page_buffer, sizeof(first_page));
|
||||
if (nread_or_error.is_error())
|
||||
return KResult(-ENOEXEC);
|
||||
nread = nread_or_error.value();
|
||||
|
@ -490,7 +491,8 @@ int Process::exec(String path, Vector<String> arguments, Vector<String> environm
|
|||
|
||||
// Read the first page of the program into memory so we can validate the binfmt of it
|
||||
char first_page[PAGE_SIZE];
|
||||
auto nread_or_error = description->read((u8*)&first_page, sizeof(first_page));
|
||||
auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
|
||||
auto nread_or_error = description->read(first_page_buffer, sizeof(first_page));
|
||||
if (nread_or_error.is_error())
|
||||
return -ENOEXEC;
|
||||
|
||||
|
@ -554,7 +556,7 @@ int Process::sys$execve(Userspace<const Syscall::SC_execve_params*> user_params)
|
|||
// NOTE: Be extremely careful with allocating any kernel memory in exec().
|
||||
// On success, the kernel stack will be lost.
|
||||
Syscall::SC_execve_params params;
|
||||
if (!validate_read_and_copy_typed(¶ms, user_params))
|
||||
if (!copy_from_user(¶ms, user_params))
|
||||
return -EFAULT;
|
||||
|
||||
if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
|
||||
|
@ -574,13 +576,16 @@ int Process::sys$execve(Userspace<const Syscall::SC_execve_params*> user_params)
|
|||
auto copy_user_strings = [this](const auto& list, auto& output) {
|
||||
if (!list.length)
|
||||
return true;
|
||||
if (!validate_read_typed(list.strings, list.length))
|
||||
Checked size = sizeof(list.length);
|
||||
size *= list.length;
|
||||
if (size.has_overflow())
|
||||
return false;
|
||||
Vector<Syscall::StringArgument, 32> strings;
|
||||
strings.resize(list.length);
|
||||
copy_from_user(strings.data(), list.strings.unsafe_userspace_ptr(), list.length * sizeof(Syscall::StringArgument));
|
||||
if (!copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument)))
|
||||
return false;
|
||||
for (size_t i = 0; i < list.length; ++i) {
|
||||
auto string = validate_and_copy_string_from_user(strings[i]);
|
||||
auto string = copy_string_from_user(strings[i]);
|
||||
if (string.is_null())
|
||||
return false;
|
||||
output.append(move(string));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue