1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +00:00

Kernel: Handle promise violations in the syscall handler

Previously we would crash the process immediately when a promise
violation was found during a syscall. This is error prone, as we
don't unwind the stack. This means that in certain cases we can
leak resources, like an OwnPtr / RefPtr tracked on the stack. Or
even leak a lock acquired in a ScopeLockLocker.

To remedy this situation we move the promise violation handling to
the syscall handler, right before we return to user space. This
allows the code to follow the normal unwind path, and grantees
there is no longer any cleanup that needs to occur.

The Process::require_promise() and Process::require_no_promises()
functions were modified to return ErrorOr<void> so we enforce that
the errors are always propagated by the caller.
This commit is contained in:
Brian Gianforcaro 2021-12-29 01:11:45 -08:00 committed by Andreas Kling
parent c444a3fc9e
commit 54b9a4ec1e
66 changed files with 156 additions and 148 deletions

View file

@ -14,7 +14,7 @@ namespace Kernel {
ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
TRY(require_promise(Pledge::thread));
auto params = TRY(copy_typed_from_user(user_params));
unsigned detach_state = params.detach_state;
@ -74,7 +74,12 @@ ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<con
void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
auto result = require_promise(Pledge::thread);
if (result.is_error()) {
// Crash now, as we will never reach back to the syscall handler.
crash(SIGABRT, 0);
}
if (this->thread_count() == 1) {
// If this is the last thread, instead kill the process.
@ -98,7 +103,7 @@ void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stac
ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
TRY(require_promise(Pledge::thread));
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return ESRCH;
@ -113,7 +118,7 @@ ErrorOr<FlatPtr> Process::sys$detach_thread(pid_t tid)
ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
TRY(require_promise(Pledge::thread));
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
@ -148,7 +153,7 @@ ErrorOr<FlatPtr> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_valu
ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
TRY(require_promise(Pledge::thread));
if (signal < 0 || signal >= 32)
return EINVAL;
@ -166,7 +171,7 @@ ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::stdio);
TRY(require_promise(Pledge::stdio));
auto name = TRY(try_copy_kstring_from_user(user_name, user_name_length));
@ -185,7 +190,7 @@ ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*>
ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
require_promise(Pledge::thread);
TRY(require_promise(Pledge::thread));
if (buffer_size == 0)
return EINVAL;
@ -212,7 +217,7 @@ ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer
ErrorOr<FlatPtr> Process::sys$gettid()
{
VERIFY_NO_PROCESS_BIG_LOCK(this)
require_promise(Pledge::stdio);
TRY(require_promise(Pledge::stdio));
return Thread::current()->tid().value();
}