mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:17:34 +00:00
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.
This commit is contained in:
parent
d69a0380e1
commit
5e062414c1
35 changed files with 609 additions and 160 deletions
109
Kernel/Process.h
109
Kernel/Process.h
|
@ -24,6 +24,7 @@
|
|||
#include <Kernel/FileSystem/UnveilNode.h>
|
||||
#include <Kernel/Forward.h>
|
||||
#include <Kernel/FutexQueue.h>
|
||||
#include <Kernel/Jail.h>
|
||||
#include <Kernel/Library/LockWeakPtr.h>
|
||||
#include <Kernel/Library/LockWeakable.h>
|
||||
#include <Kernel/Library/NonnullLockRefPtrVector.h>
|
||||
|
@ -70,6 +71,7 @@ Time kgettimeofday();
|
|||
__ENUMERATE_PLEDGE_PROMISE(prot_exec) \
|
||||
__ENUMERATE_PLEDGE_PROMISE(map_fixed) \
|
||||
__ENUMERATE_PLEDGE_PROMISE(getkeymap) \
|
||||
__ENUMERATE_PLEDGE_PROMISE(jail) \
|
||||
__ENUMERATE_PLEDGE_PROMISE(no_error)
|
||||
|
||||
enum class Pledge : u32 {
|
||||
|
@ -213,7 +215,8 @@ public:
|
|||
bool is_kernel_process() const { return m_is_kernel_process; }
|
||||
bool is_user_process() const { return !m_is_kernel_process; }
|
||||
|
||||
static LockRefPtr<Process> from_pid(ProcessID);
|
||||
static LockRefPtr<Process> from_pid_in_same_jail(ProcessID);
|
||||
static LockRefPtr<Process> from_pid_ignoring_jails(ProcessID);
|
||||
static SessionID get_sid_from_pgid(ProcessGroupID pgid);
|
||||
|
||||
StringView name() const { return m_name->view(); }
|
||||
|
@ -233,6 +236,8 @@ public:
|
|||
return with_protected_data([](auto& protected_data) { return protected_data.ppid; });
|
||||
}
|
||||
|
||||
SpinlockProtected<RefPtr<Jail>>& jail() { return m_attached_jail; }
|
||||
|
||||
NonnullRefPtr<Credentials> credentials() const;
|
||||
|
||||
bool is_dumpable() const
|
||||
|
@ -248,11 +253,11 @@ public:
|
|||
|
||||
// Breakable iteration functions
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
static void for_each(Callback);
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
static void for_each_in_pgrp(ProcessGroupID, Callback);
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
void for_each_child(Callback);
|
||||
static void for_each_ignoring_jails(Callback);
|
||||
|
||||
static ErrorOr<void> for_each_in_same_jail(Function<ErrorOr<void>(Process&)>);
|
||||
ErrorOr<void> for_each_in_pgrp_in_same_jail(ProcessGroupID, Function<ErrorOr<void>(Process&)>);
|
||||
ErrorOr<void> for_each_child_in_same_jail(Function<ErrorOr<void>(Process&)>);
|
||||
|
||||
template<IteratorFunction<Thread&> Callback>
|
||||
IterationDecision for_each_thread(Callback);
|
||||
|
@ -262,11 +267,7 @@ public:
|
|||
|
||||
// Non-breakable iteration functions
|
||||
template<VoidFunction<Process&> Callback>
|
||||
static void for_each(Callback);
|
||||
template<VoidFunction<Process&> Callback>
|
||||
static void for_each_in_pgrp(ProcessGroupID, Callback);
|
||||
template<VoidFunction<Process&> Callback>
|
||||
void for_each_child(Callback);
|
||||
static void for_each_ignoring_jails(Callback);
|
||||
|
||||
template<VoidFunction<Thread&> Callback>
|
||||
IterationDecision for_each_thread(Callback);
|
||||
|
@ -437,6 +438,8 @@ public:
|
|||
ErrorOr<FlatPtr> sys$statvfs(Userspace<Syscall::SC_statvfs_params const*> user_params);
|
||||
ErrorOr<FlatPtr> sys$fstatvfs(int fd, statvfs* buf);
|
||||
ErrorOr<FlatPtr> sys$map_time_page();
|
||||
ErrorOr<FlatPtr> sys$jail_create(Userspace<Syscall::SC_jail_create_params*> user_params);
|
||||
ErrorOr<FlatPtr> sys$jail_attach(Userspace<Syscall::SC_jail_attach_params const*> user_params);
|
||||
|
||||
template<bool sockname, typename Params>
|
||||
ErrorOr<void> get_sock_or_peer_name(Params const&);
|
||||
|
@ -862,6 +865,10 @@ private:
|
|||
LockRefPtr<TTY> m_tty;
|
||||
|
||||
LockWeakPtr<Memory::Region> m_master_tls_region;
|
||||
|
||||
IntrusiveListNode<Process> m_jail_list_node;
|
||||
SpinlockProtected<RefPtr<Jail>> m_attached_jail { LockRank::Process };
|
||||
|
||||
size_t m_master_tls_size { 0 };
|
||||
size_t m_master_tls_alignment { 0 };
|
||||
|
||||
|
@ -914,48 +921,6 @@ static_assert(AssertSize<Process, (PAGE_SIZE * 2)>());
|
|||
|
||||
extern RecursiveSpinlock g_profiling_lock;
|
||||
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
inline void Process::for_each(Callback callback)
|
||||
{
|
||||
Process::all_instances().with([&](auto const& list) {
|
||||
for (auto it = list.begin(); it != list.end();) {
|
||||
auto& process = *it;
|
||||
++it;
|
||||
if (callback(process) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
inline void Process::for_each_child(Callback callback)
|
||||
{
|
||||
ProcessID my_pid = pid();
|
||||
Process::all_instances().with([&](auto const& list) {
|
||||
for (auto it = list.begin(); it != list.end();) {
|
||||
auto& process = *it;
|
||||
++it;
|
||||
if (process.ppid() == my_pid || process.has_tracee_thread(pid())) {
|
||||
if (callback(process) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template<IteratorFunction<Thread&> Callback>
|
||||
inline IterationDecision Process::for_each_thread(Callback callback) const
|
||||
{
|
||||
return thread_list().with([&](auto& thread_list) -> IterationDecision {
|
||||
for (auto& thread : thread_list) {
|
||||
IterationDecision decision = callback(thread);
|
||||
if (decision != IterationDecision::Continue)
|
||||
return decision;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
template<IteratorFunction<Thread&> Callback>
|
||||
inline IterationDecision Process::for_each_thread(Callback callback)
|
||||
{
|
||||
|
@ -970,34 +935,27 @@ inline IterationDecision Process::for_each_thread(Callback callback)
|
|||
}
|
||||
|
||||
template<IteratorFunction<Process&> Callback>
|
||||
inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback)
|
||||
inline void Process::for_each_ignoring_jails(Callback callback)
|
||||
{
|
||||
Process::all_instances().with([&](auto const& list) {
|
||||
for (auto it = list.begin(); it != list.end();) {
|
||||
auto& process = *it;
|
||||
++it;
|
||||
if (!process.is_dead() && process.pgid() == pgid) {
|
||||
if (callback(process) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
if (callback(process) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template<VoidFunction<Process&> Callback>
|
||||
inline void Process::for_each(Callback callback)
|
||||
template<IteratorFunction<Thread&> Callback>
|
||||
inline IterationDecision Process::for_each_thread(Callback callback) const
|
||||
{
|
||||
return for_each([&](auto& item) {
|
||||
callback(item);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
template<VoidFunction<Process&> Callback>
|
||||
inline void Process::for_each_child(Callback callback)
|
||||
{
|
||||
return for_each_child([&](auto& item) {
|
||||
callback(item);
|
||||
return thread_list().with([&](auto& thread_list) -> IterationDecision {
|
||||
for (auto& thread : thread_list) {
|
||||
IterationDecision decision = callback(thread);
|
||||
if (decision != IterationDecision::Continue)
|
||||
return decision;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
@ -1031,15 +989,6 @@ inline IterationDecision Process::for_each_thread(Callback callback)
|
|||
return IterationDecision::Continue;
|
||||
}
|
||||
|
||||
template<VoidFunction<Process&> Callback>
|
||||
inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback)
|
||||
{
|
||||
return for_each_in_pgrp(pgid, [&](auto& item) {
|
||||
callback(item);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
inline ProcessID Thread::pid() const
|
||||
{
|
||||
return m_process->pid();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue