diff options
author | Liav A <liavalb@gmail.com> | 2022-11-02 22:26:02 +0200 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-11-05 18:00:58 -0600 |
commit | 5e062414c11df31ed595c363990005eef00fa263 (patch) | |
tree | 62281b52f5408e4b3b814553e9cf18a234cc001e /Kernel/Process.cpp | |
parent | d69a0380e1558e82a6a0bda71e66eab121cdcda4 (diff) | |
download | serenity-5e062414c11df31ed595c363990005eef00fa263.zip |
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.
Diffstat (limited to 'Kernel/Process.cpp')
-rw-r--r-- | Kernel/Process.cpp | 130 |
1 files changed, 125 insertions, 5 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index dc7ccb6e9e..5346a4880e 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -63,6 +63,95 @@ SpinlockProtected<Process::List>& Process::all_instances() return *s_all_instances; } +ErrorOr<void> Process::for_each_in_same_jail(Function<ErrorOr<void>(Process&)> callback) +{ + ErrorOr<void> result {}; + Process::all_instances().with([&](auto const& list) { + Process::current().jail().with([&](auto my_jail) { + for (auto& process : list) { + if (!my_jail) { + result = callback(process); + } else { + // Note: Don't acquire the process jail spinlock twice if it's the same process + // we are currently inspecting. + if (&Process::current() == &process) { + result = callback(process); + } else { + process.jail().with([&](auto& their_jail) { + if (their_jail.ptr() == my_jail.ptr()) + result = callback(process); + }); + } + } + if (result.is_error()) + break; + } + }); + }); + return result; +} + +ErrorOr<void> Process::for_each_child_in_same_jail(Function<ErrorOr<void>(Process&)> callback) +{ + ProcessID my_pid = pid(); + ErrorOr<void> result {}; + Process::all_instances().with([&](auto const& list) { + jail().with([&](auto my_jail) { + for (auto& process : list) { + if (!my_jail) { + if (process.ppid() == my_pid || process.has_tracee_thread(pid())) + result = callback(process); + } else { + // FIXME: Is it possible to have a child process being pointing to itself + // as the parent process under normal conditions? + // Note: Don't acquire the process jail spinlock twice if it's the same process + // we are currently inspecting. + if (&Process::current() == &process && (process.ppid() == my_pid || process.has_tracee_thread(pid()))) { + result = callback(process); + } else { + process.jail().with([&](auto& their_jail) { + if ((their_jail.ptr() == my_jail.ptr()) && (process.ppid() == my_pid || process.has_tracee_thread(pid()))) + result = callback(process); + }); + } + } + if (result.is_error()) + break; + } + }); + }); + return result; +} + +ErrorOr<void> Process::for_each_in_pgrp_in_same_jail(ProcessGroupID pgid, Function<ErrorOr<void>(Process&)> callback) +{ + ErrorOr<void> result {}; + Process::all_instances().with([&](auto const& list) { + jail().with([&](auto my_jail) { + for (auto& process : list) { + if (!my_jail) { + if (!process.is_dead() && process.pgid() == pgid) + result = callback(process); + } else { + // Note: Don't acquire the process jail spinlock twice if it's the same process + // we are currently inspecting. + if (&Process::current() == &process && !process.is_dead() && process.pgid() == pgid) { + result = callback(process); + } else { + process.jail().with([&](auto& their_jail) { + if ((their_jail.ptr() == my_jail.ptr()) && !process.is_dead() && process.pgid() == pgid) + result = callback(process); + }); + } + } + if (result.is_error()) + break; + } + }); + }); + return result; +} + ProcessID Process::allocate_pid() { // Overflow is UB, and negative PIDs wreck havoc. @@ -426,7 +515,33 @@ void Process::crash(int signal, FlatPtr ip, bool out_of_memory) VERIFY_NOT_REACHED(); } -LockRefPtr<Process> Process::from_pid(ProcessID pid) +LockRefPtr<Process> Process::from_pid_in_same_jail(ProcessID pid) +{ + return Process::current().jail().with([&](auto& my_jail) -> LockRefPtr<Process> { + return all_instances().with([&](auto const& list) -> LockRefPtr<Process> { + if (!my_jail) { + for (auto& process : list) { + if (process.pid() == pid) { + return process; + } + } + } else { + for (auto& process : list) { + if (process.pid() == pid) { + return process.jail().with([&](auto& other_process_jail) -> LockRefPtr<Process> { + if (other_process_jail.ptr() == my_jail.ptr()) + return process; + return {}; + }); + } + } + } + return {}; + }); + }); +} + +LockRefPtr<Process> Process::from_pid_ignoring_jails(ProcessID pid) { return all_instances().with([&](auto const& list) -> LockRefPtr<Process> { for (auto const& process : list) { @@ -657,20 +772,25 @@ void Process::finalize() m_fds.with_exclusive([](auto& fds) { fds.clear(); }); m_tty = nullptr; m_executable.with([](auto& executable) { executable = nullptr; }); + m_attached_jail.with([](auto& jail) { + if (jail) + jail->detach({}); + jail = nullptr; + }); m_arguments.clear(); m_environment.clear(); m_state.store(State::Dead, AK::MemoryOrder::memory_order_release); { - if (auto parent_process = Process::from_pid(ppid())) { + if (auto parent_process = Process::from_pid_ignoring_jails(ppid())) { if (parent_process->is_user_process() && (parent_process->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) != SA_NOCLDWAIT) (void)parent_process->send_signal(SIGCHLD, this); } } if (!!ppid()) { - if (auto parent = Process::from_pid(ppid())) { + if (auto parent = Process::from_pid_ignoring_jails(ppid())) { parent->m_ticks_in_user_for_dead_children += m_ticks_in_user + m_ticks_in_user_for_dead_children; parent->m_ticks_in_kernel_for_dead_children += m_ticks_in_kernel + m_ticks_in_kernel_for_dead_children; } @@ -697,9 +817,9 @@ void Process::unblock_waiters(Thread::WaitBlocker::UnblockFlags flags, u8 signal { LockRefPtr<Process> waiter_process; if (auto* my_tracer = tracer()) - waiter_process = Process::from_pid(my_tracer->tracer_pid()); + waiter_process = Process::from_pid_ignoring_jails(my_tracer->tracer_pid()); else - waiter_process = Process::from_pid(ppid()); + waiter_process = Process::from_pid_ignoring_jails(ppid()); if (waiter_process) waiter_process->m_wait_blocker_set.unblock(*this, flags, signal); |