mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 17:55:08 +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
|
@ -116,7 +116,7 @@ off_t FileDescription::seek(off_t offset, int whence)
|
|||
return m_current_offset;
|
||||
}
|
||||
|
||||
KResultOr<size_t> FileDescription::read(u8* buffer, size_t count)
|
||||
KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
|
||||
{
|
||||
LOCKER(m_lock);
|
||||
Checked<size_t> new_offset = m_current_offset;
|
||||
|
@ -130,7 +130,7 @@ KResultOr<size_t> FileDescription::read(u8* buffer, size_t count)
|
|||
return nread_or_error;
|
||||
}
|
||||
|
||||
KResultOr<size_t> FileDescription::write(const u8* data, size_t size)
|
||||
KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
|
||||
{
|
||||
LOCKER(m_lock);
|
||||
Checked<size_t> new_offset = m_current_offset;
|
||||
|
@ -162,7 +162,7 @@ KResultOr<KBuffer> FileDescription::read_entire_file()
|
|||
return m_inode->read_entire(this);
|
||||
}
|
||||
|
||||
ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
|
||||
ssize_t FileDescription::get_dir_entries(UserOrKernelBuffer& buffer, ssize_t size)
|
||||
{
|
||||
LOCKER(m_lock, Lock::Mode::Shared);
|
||||
if (!is_directory())
|
||||
|
@ -195,7 +195,8 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
|
|||
if (static_cast<size_t>(size) < temp_buffer.size())
|
||||
return -EINVAL;
|
||||
|
||||
copy_to_user(buffer, temp_buffer.data(), temp_buffer.size());
|
||||
if (!buffer.write(temp_buffer.data(), temp_buffer.size()))
|
||||
return -EFAULT;
|
||||
return stream.offset();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue