summaryrefslogtreecommitdiff
path: root/Kernel/DoubleBuffer.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/DoubleBuffer.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/DoubleBuffer.cpp')
-rw-r--r--Kernel/DoubleBuffer.cpp14
1 files changed, 8 insertions, 6 deletions
diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp
index 6cc74f5ed7..4b9285e327 100644
--- a/Kernel/DoubleBuffer.cpp
+++ b/Kernel/DoubleBuffer.cpp
@@ -58,7 +58,7 @@ void DoubleBuffer::flip()
compute_lockfree_metadata();
}
-size_t DoubleBuffer::write(const u8* data, size_t size)
+ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size)
{
if (!size)
return 0;
@@ -68,11 +68,12 @@ size_t DoubleBuffer::write(const u8* data, size_t size)
u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
m_write_buffer->size += bytes_to_write;
compute_lockfree_metadata();
- memcpy(write_ptr, data, bytes_to_write);
- return bytes_to_write;
+ if (!data.read(write_ptr, bytes_to_write))
+ return -EFAULT;
+ return (ssize_t)bytes_to_write;
}
-size_t DoubleBuffer::read(u8* data, size_t size)
+ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size)
{
if (!size)
return 0;
@@ -83,10 +84,11 @@ size_t DoubleBuffer::read(u8* data, size_t size)
if (m_read_buffer_index >= m_read_buffer->size)
return 0;
size_t nread = min(m_read_buffer->size - m_read_buffer_index, size);
- memcpy(data, m_read_buffer->data + m_read_buffer_index, nread);
+ if (!data.write(m_read_buffer->data + m_read_buffer_index, nread))
+ return -EFAULT;
m_read_buffer_index += nread;
compute_lockfree_metadata();
- return nread;
+ return (ssize_t)nread;
}
}