ladybird/Kernel/Syscalls/sched.cpp
Liav A 5e062414c1 Kernel: Add support for jails
Our implementation for Jails resembles much of how FreeBSD jails are
working - it's essentially only a matter of using a RefPtr in the
Process class to a Jail object. Then, when we iterate over all processes
in various cases, we could ensure if either the current process is in
jail and therefore should be restricted what is visible in terms of
PID isolation, and also to be able to expose metadata about Jails in
/sys/kernel/jails node (which does not reveal anything to a process
which is in jail).

A lifetime model for the Jail object is currently plain simple - there's
simpy no way to manually delete a Jail object once it was created. Such
feature should be carefully designed to allow safe destruction of a Jail
without the possibility of releasing a process which is in Jail from the
actual jail. Each process which is attached into a Jail cannot leave it
until the end of a Process (i.e. when finalizing a Process). All jails
are kept being referenced in the JailManagement. When a last attached
process is finalized, the Jail is automatically destroyed.
2022-11-05 18:00:58 -06:00

118 lines
3.9 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/Syscall.h>
#include <Kernel/Process.h>
#include <Kernel/Scheduler.h>
namespace Kernel {
ErrorOr<FlatPtr> Process::sys$yield()
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::stdio));
Thread::current()->yield_without_releasing_big_lock();
return 0;
}
ErrorOr<NonnullRefPtr<Thread>> Process::get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode)
{
VERIFY(g_scheduler_lock.is_locked_by_current_processor());
Thread* peer;
switch (mode) {
case Syscall::SchedulerParametersMode::Thread: {
peer = Thread::current();
if (pid_or_tid != 0)
peer = Thread::from_tid(pid_or_tid);
// Only superuser can access other processes' threads.
if (!credentials()->is_superuser() && peer && &peer->process() != this)
return EPERM;
break;
}
case Syscall::SchedulerParametersMode::Process: {
auto* searched_process = this;
if (pid_or_tid != 0)
searched_process = Process::from_pid_in_same_jail(pid_or_tid);
if (searched_process == nullptr)
return ESRCH;
auto pid = searched_process->pid().value();
// Main thread has tid == pid
this->thread_list().for_each([&](auto& thread) {
if (thread.tid().value() == pid)
peer = &thread;
});
break;
}
default:
VERIFY_NOT_REACHED();
}
if (!peer)
return ESRCH;
return NonnullRefPtr<Thread> { *peer };
}
ErrorOr<FlatPtr> Process::sys$scheduler_set_parameters(Userspace<Syscall::SC_scheduler_parameters_params const*> user_param)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::proc));
auto parameters = TRY(copy_typed_from_user(user_param));
if (parameters.parameters.sched_priority < THREAD_PRIORITY_MIN || parameters.parameters.sched_priority > THREAD_PRIORITY_MAX)
return EINVAL;
SpinlockLocker lock(g_scheduler_lock);
auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
auto credentials = this->credentials();
auto peer_credentials = peer->process().credentials();
if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid())
return EPERM;
peer->set_priority((u32)parameters.parameters.sched_priority);
// POSIX says that process scheduling parameters have precedence over thread scheduling parameters.
// We don't track them separately, so overwrite the thread scheduling settings manually for now.
if (parameters.mode == Syscall::SchedulerParametersMode::Process) {
peer->process().for_each_thread([&](auto& thread) {
thread.set_priority((u32)parameters.parameters.sched_priority);
});
}
return 0;
}
ErrorOr<FlatPtr> Process::sys$scheduler_get_parameters(Userspace<Syscall::SC_scheduler_parameters_params*> user_param)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::proc));
Syscall::SC_scheduler_parameters_params parameters;
TRY(copy_from_user(&parameters, user_param));
int priority;
{
SpinlockLocker lock(g_scheduler_lock);
auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
auto credentials = this->credentials();
auto peer_credentials = peer->process().credentials();
if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid())
return EPERM;
priority = (int)peer->priority();
}
parameters.parameters.sched_priority = priority;
TRY(copy_to_user(user_param, &parameters));
return 0;
}
}