mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 23:48:11 +00:00
Kernel: Remove SmapDisablers in open(), openat() and set_thread_name()
This patch introduces a helpful copy_string_from_user() function that takes a bounded null-terminated string from userspace memory and copies it into a String object.
This commit is contained in:
parent
c4a1ea34c2
commit
80cbb72f2f
3 changed files with 37 additions and 33 deletions
|
@ -1600,34 +1600,22 @@ int Process::number_of_open_file_descriptors() const
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::sys$open(const Syscall::SC_open_params* params)
|
int Process::sys$open(const Syscall::SC_open_params* user_params)
|
||||||
{
|
{
|
||||||
if (!validate_read_typed(params))
|
if (!validate_read_typed(user_params))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
const char* path_data;
|
Syscall::SC_open_params params;
|
||||||
int path_length;
|
copy_from_user(¶ms, user_params, sizeof(params));
|
||||||
int options;
|
auto options = params.options;
|
||||||
u16 mode;
|
auto mode = params.mode;
|
||||||
|
|
||||||
{
|
if (params.path_length <= 0)
|
||||||
SmapDisabler disabler;
|
|
||||||
path_data = params->path;
|
|
||||||
path_length = params->path_length;
|
|
||||||
options = params->options;
|
|
||||||
mode = params->mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!path_length)
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
if (!validate_read(path_data, path_length))
|
if (!validate_read(params.path, params.path_length))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
String path;
|
String path = copy_string_from_user(params.path, params.path_length);
|
||||||
{
|
|
||||||
SmapDisabler disabler;
|
|
||||||
path = String(path_data, path_length);
|
|
||||||
}
|
|
||||||
int fd = alloc_fd();
|
int fd = alloc_fd();
|
||||||
#ifdef DEBUG_IO
|
#ifdef DEBUG_IO
|
||||||
dbgprintf("%s(%u) sys$open(\"%s\") -> %d\n", name().characters(), pid(), path, fd);
|
dbgprintf("%s(%u) sys$open(\"%s\") -> %d\n", name().characters(), pid(), path, fd);
|
||||||
|
@ -1645,20 +1633,22 @@ int Process::sys$open(const Syscall::SC_open_params* params)
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::sys$openat(const Syscall::SC_openat_params* params)
|
int Process::sys$openat(const Syscall::SC_openat_params* user_params)
|
||||||
{
|
{
|
||||||
if (!validate_read_typed(params))
|
if (!validate_read_typed(user_params))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
SmapDisabler disabler;
|
Syscall::SC_openat_params params;
|
||||||
int dirfd = params->dirfd;
|
copy_from_user(¶ms, user_params, sizeof(params));
|
||||||
const char* path = params->path;
|
int dirfd = params.dirfd;
|
||||||
int path_length = params->path_length;
|
int options = params.options;
|
||||||
int options = params->options;
|
u16 mode = params.mode;
|
||||||
u16 mode = params->mode;
|
|
||||||
|
|
||||||
if (!validate_read(path, path_length))
|
if (params.path_length <= 0)
|
||||||
|
return -EINVAL;
|
||||||
|
if (!validate_read(params.path, params.path_length))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
auto path = copy_string_from_user(params.path, params.path_length);
|
||||||
#ifdef DEBUG_IO
|
#ifdef DEBUG_IO
|
||||||
dbgprintf("%s(%u) sys$openat(%d, \"%s\")\n", dirfd, name().characters(), pid(), path);
|
dbgprintf("%s(%u) sys$openat(%d, \"%s\")\n", dirfd, name().characters(), pid(), path);
|
||||||
#endif
|
#endif
|
||||||
|
@ -3377,17 +3367,17 @@ int Process::sys$set_thread_name(int tid, const char* buffer, int buffer_size)
|
||||||
if (!validate_read(buffer, buffer_size))
|
if (!validate_read(buffer, buffer_size))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
SmapDisabler disabler;
|
auto name = copy_string_from_user(buffer, buffer_size);
|
||||||
|
|
||||||
const size_t max_thread_name_size = 64;
|
const size_t max_thread_name_size = 64;
|
||||||
if (strnlen(buffer, (size_t)buffer_size) > max_thread_name_size)
|
if (name.length() > max_thread_name_size)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
auto* thread = Thread::from_tid(tid);
|
auto* thread = Thread::from_tid(tid);
|
||||||
if (!thread || thread->pid() != pid())
|
if (!thread || thread->pid() != pid())
|
||||||
return -ESRCH;
|
return -ESRCH;
|
||||||
|
|
||||||
thread->set_name({ buffer, (size_t)buffer_size });
|
thread->set_name(name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int Process::sys$get_thread_name(int tid, char* buffer, int buffer_size)
|
int Process::sys$get_thread_name(int tid, char* buffer, int buffer_size)
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <Kernel/Arch/i386/CPU.h>
|
#include <Kernel/Arch/i386/CPU.h>
|
||||||
#include <Kernel/Heap/kmalloc.h>
|
#include <Kernel/Heap/kmalloc.h>
|
||||||
#include <Kernel/StdLib.h>
|
#include <Kernel/StdLib.h>
|
||||||
|
|
||||||
|
String copy_string_from_user(const char* user_str, size_t user_str_size)
|
||||||
|
{
|
||||||
|
SmapDisabler disabler;
|
||||||
|
size_t length = strnlen(user_str, user_str_size);
|
||||||
|
return String(user_str, length);
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
void* copy_to_user(void* dest_ptr, const void* src_ptr, size_t n)
|
void* copy_to_user(void* dest_ptr, const void* src_ptr, size_t n)
|
||||||
|
|
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
|
namespace AK {
|
||||||
|
class String;
|
||||||
|
}
|
||||||
|
|
||||||
|
AK::String copy_string_from_user(const char*, size_t);
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
static_assert(sizeof(size_t) == 4);
|
static_assert(sizeof(size_t) == 4);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue