summaryrefslogtreecommitdiff
path: root/Kernel/TTY/TTY.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/TTY/TTY.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/TTY/TTY.cpp')
-rw-r--r--Kernel/TTY/TTY.cpp71
1 files changed, 37 insertions, 34 deletions
diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp
index 672e1885d2..3130b3cceb 100644
--- a/Kernel/TTY/TTY.cpp
+++ b/Kernel/TTY/TTY.cpp
@@ -52,7 +52,7 @@ void TTY::set_default_termios()
memcpy(m_termios.c_cc, default_cc, sizeof(default_cc));
}
-KResultOr<size_t> TTY::read(FileDescription&, size_t, u8* buffer, size_t size)
+KResultOr<size_t> TTY::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
{
if (Process::current()->pgid() != pgid()) {
// FIXME: Should we propigate this error path somehow?
@@ -63,33 +63,40 @@ KResultOr<size_t> TTY::read(FileDescription&, size_t, u8* buffer, size_t size)
if (m_input_buffer.size() < static_cast<size_t>(size))
size = m_input_buffer.size();
+ ssize_t nwritten;
if (in_canonical_mode()) {
- size_t i = 0;
- for (; i < size; i++) {
- u8 ch = m_input_buffer.dequeue();
- if (ch == '\0') {
- //Here we handle a ^D line, so we don't add the
- //character to the output.
- m_available_lines--;
- break;
- } else if (ch == '\n' || is_eol(ch)) {
- buffer[i] = ch;
- i++;
- m_available_lines--;
- break;
+ nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) {
+ size_t i = 0;
+ for (; i < data_size; i++) {
+ u8 ch = m_input_buffer.dequeue();
+ if (ch == '\0') {
+ //Here we handle a ^D line, so we don't add the
+ //character to the output.
+ m_available_lines--;
+ break;
+ } else if (ch == '\n' || is_eol(ch)) {
+ data[i] = ch;
+ i++;
+ m_available_lines--;
+ break;
+ }
+ data[i] = ch;
}
- buffer[i] = ch;
- }
- return i;
+ return (ssize_t)i;
+ });
+ } else {
+ nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) {
+ for (size_t i = 0; i < data_size; i++)
+ data[i] = m_input_buffer.dequeue();
+ return (ssize_t)data_size;
+ });
}
-
- for (size_t i = 0; i < size; i++)
- buffer[i] = m_input_buffer.dequeue();
-
- return size;
+ if (nwritten < 0)
+ return KResult(nwritten);
+ return (size_t)nwritten;
}
-KResultOr<size_t> TTY::write(FileDescription&, size_t, const u8* buffer, size_t size)
+KResultOr<size_t> TTY::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
{
if (Process::current()->pgid() != pgid()) {
(void)Process::current()->send_signal(SIGTTOU, nullptr);
@@ -337,19 +344,17 @@ int TTY::ioctl(FileDescription&, unsigned request, FlatPtr arg)
}
case TCGETS: {
user_termios = reinterpret_cast<termios*>(arg);
- if (!current_process.validate_write(user_termios, sizeof(termios)))
+ if (!copy_to_user(user_termios, &m_termios))
return -EFAULT;
- copy_to_user(user_termios, &m_termios);
return 0;
}
case TCSETS:
case TCSETSF:
case TCSETSW: {
user_termios = reinterpret_cast<termios*>(arg);
- if (!current_process.validate_read(user_termios, sizeof(termios)))
- return -EFAULT;
termios termios;
- copy_from_user(&termios, user_termios);
+ if (!copy_from_user(&termios, user_termios))
+ return -EFAULT;
set_termios(termios);
if (request == TCSETSF)
flush_input();
@@ -365,21 +370,19 @@ int TTY::ioctl(FileDescription&, unsigned request, FlatPtr arg)
return 0;
case TIOCGWINSZ:
user_winsize = reinterpret_cast<winsize*>(arg);
- if (!current_process.validate_write(user_winsize, sizeof(winsize)))
- return -EFAULT;
winsize ws;
ws.ws_row = m_rows;
ws.ws_col = m_columns;
ws.ws_xpixel = 0;
ws.ws_ypixel = 0;
- copy_to_user(user_winsize, &ws);
+ if (!copy_to_user(user_winsize, &ws))
+ return -EFAULT;
return 0;
case TIOCSWINSZ: {
user_winsize = reinterpret_cast<winsize*>(arg);
- if (!current_process.validate_read(user_winsize, sizeof(winsize)))
- return -EFAULT;
winsize ws;
- copy_from_user(&ws, user_winsize);
+ if (!copy_from_user(&ws, user_winsize))
+ return -EFAULT;
if (ws.ws_col == m_columns && ws.ws_row == m_rows)
return 0;
m_rows = ws.ws_row;