ladybird/Kernel/Jail.h
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

48 lines
1.1 KiB
C++

/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DistinctNumeric.h>
#include <AK/Error.h>
#include <AK/IntrusiveList.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/Try.h>
#include <AK/Types.h>
#include <Kernel/KString.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/Locking/SpinlockProtected.h>
namespace Kernel {
class JailManagement;
AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, JailIndex);
class Jail : public RefCounted<Jail> {
friend class JailManagement;
public:
static ErrorOr<NonnullLockRefPtr<Jail>> create(Badge<JailManagement>, NonnullOwnPtr<KString>, JailIndex);
StringView name() const { return m_name->view(); }
JailIndex index() const { return m_index; }
void detach(Badge<Process>);
SpinlockProtected<size_t>& attach_count() { return m_attach_count; }
private:
Jail(NonnullOwnPtr<KString>, JailIndex);
NonnullOwnPtr<KString> m_name;
JailIndex const m_index;
IntrusiveListNode<Jail, NonnullLockRefPtr<Jail>> m_jail_list_node;
SpinlockProtected<size_t> m_attach_count { LockRank::None, 0 };
};
}