1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:27:35 +00:00

Kernel: Make realpath() take path+length, get rid of SmapDisabler

This commit is contained in:
Andreas Kling 2020-01-06 11:32:25 +01:00
parent d6b06fd5a3
commit 7c916b9fe9
4 changed files with 29 additions and 13 deletions

View file

@ -2340,21 +2340,22 @@ int Process::sys$mkdir(const char* user_path, size_t path_length, mode_t mode)
return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory()); return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory());
} }
int Process::sys$realpath(const char* pathname, char* buffer, size_t size) int Process::sys$realpath(const Syscall::SC_realpath_params* user_params)
{ {
SmapDisabler disabler; if (!validate_read_typed(user_params))
if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
size_t pathname_length = strlen(pathname); Syscall::SC_realpath_params params;
if (pathname_length == 0) copy_from_user(&params, user_params, sizeof(params));
return -EINVAL;
if (pathname_length >= size) if (!validate_write(params.buffer, params.buffer_size))
return -ENAMETOOLONG;
if (!validate_write(buffer, size))
return -EFAULT; return -EFAULT;
auto custody_or_error = VFS::the().resolve_path(pathname, current_directory()); auto path = get_syscall_path_argument(params.path, params.path_length);
if (path.is_error())
return path.error();
auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
if (custody_or_error.is_error()) if (custody_or_error.is_error())
return custody_or_error.error(); return custody_or_error.error();
auto& custody = custody_or_error.value(); auto& custody = custody_or_error.value();
@ -2366,7 +2367,10 @@ int Process::sys$realpath(const char* pathname, char* buffer, size_t size)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
strncpy(buffer, canonical_path.string().characters(), size); if (canonical_path.string().length() + 1 > params.buffer_size)
return -ENAMETOOLONG;
copy_to_user(params.buffer, canonical_path.string().characters(), canonical_path.string().length() + 1);
return 0; return 0;
}; };

View file

@ -221,7 +221,7 @@ public:
int sys$halt(); int sys$halt();
int sys$reboot(); int sys$reboot();
int sys$set_process_icon(int icon_id); int sys$set_process_icon(int icon_id);
int sys$realpath(const char* pathname, char*, size_t); int sys$realpath(const Syscall::SC_realpath_params*);
ssize_t sys$getrandom(void*, size_t, unsigned int); ssize_t sys$getrandom(void*, size_t, unsigned int);
int sys$setkeymap(const Syscall::SC_setkeymap_params*); int sys$setkeymap(const Syscall::SC_setkeymap_params*);
int sys$module_load(const char* path, size_t path_length); int sys$module_load(const char* path, size_t path_length);

View file

@ -291,6 +291,13 @@ struct SC_create_thread_params {
void* m_stack_location = nullptr; // nullptr means any, o.w. process virtual address void* m_stack_location = nullptr; // nullptr means any, o.w. process virtual address
}; };
struct SC_realpath_params {
const char* path;
size_t path_length;
char* buffer;
size_t buffer_size;
};
void initialize(); void initialize();
int sync(); int sync();

View file

@ -704,10 +704,15 @@ uint32_t arc4random_uniform(uint32_t max_bounds)
char* realpath(const char* pathname, char* buffer) char* realpath(const char* pathname, char* buffer)
{ {
if (!pathname) {
errno = EFAULT;
return nullptr;
}
size_t size = PATH_MAX; size_t size = PATH_MAX;
if (buffer == nullptr) if (buffer == nullptr)
buffer = (char*)malloc(size); buffer = (char*)malloc(size);
int rc = syscall(SC_realpath, pathname, buffer, size); Syscall::SC_realpath_params params { pathname, strlen(pathname), buffer, size };
int rc = syscall(SC_realpath, &params);
if (rc < 0) { if (rc < 0) {
errno = -rc; errno = -rc;
return nullptr; return nullptr;