PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
namespace.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
6#include <kernel/utils/map.h>
7#include <kernel/utils/ref.h>
8#include <stdint.h>
9#include <sys/fs.h>
10#include <sys/list.h>
11
12typedef struct namespace namespace_t;
13typedef struct mount mount_t;
14typedef struct process process_t;
15typedef struct dentry dentry_t;
16
17/**
18 * @brief Per-process Namespaces.
19 * @defgroup kernel_fs_namespace Namespaces
20 * @ingroup kernel_fs
21 *
22 * The per-process namespace system allows each process to have its own view of the filesystem hierarchy, acting as the
23 * primary form of security.
24 *
25 * @{
26 */
27
28/**
29 * @brief Maximum number of iterative mount traversals when following mountpoints.
30 */
31#define NAMESPACE_MAX_TRAVERSE 32
32
33/**
34 * @brief Maximum number of mounts that can be mounted to a single mountpoint.
35 */
36#define MOUNT_STACK_MAX_MOUNTS 8
37
38/**
39 * @brief Mount stack.
40 * @struct mount_stack_t
41 *
42 * Used to store a stack of mounts for a single path. The last mount added to the stack is given priority.
43 */
51
52/**
53 * @brief Namespace structure.
54 * @struct namespace_t
55 */
56typedef struct namespace
57{
59 list_entry_t entry; ///< The entry for the parent's children list.
60 list_t children; ///< List of child namespaces.
61 namespace_t* parent; ///< The parent namespace, can be `NULL`.
62 list_t stacks; ///< List of `mount_stack_t` in this namespace.
63 map_t mountMap; ///< Map used to go from source dentries to namespace mount stacks.
64 mount_stack_t root; ///< The root mount stack.
66 // clang-format off
68// clang-format on
69
70/**
71 * @brief Create a new namespace.
72 *
73 * There is no `namespace_free()` instead use `UNREF()`.
74 *
75 * @param parent The parent namespace, or `NULL` to create a root namespace.
76 * @return On success, the new namespace. On failure, `NULL` and `errno` is set to:
77 * - `ENOMEM`: Out of memory.
78 */
80
81/**
82 * @brief Copy mounts from one namespace to another.
83 *
84 * @param dest The destination namespace.
85 * @param src The source namespace.
86 * @return On success, `0`. On failure, `ERR` and `errno`
87 */
89
90/**
91 * @brief Check if mounts in a namespace can be propagated to another namespace.
92 *
93 * This is equivalent to checkin if `other` is a child of `handle` and is intended to be used for security checks.
94 *
95 * If `handle` stores the same namespace as `other`, this will also return `true`.
96 *
97 * @param ns The source namespace.
98 * @param other The target namespace.
99 * @return `true` if mounts can be propagated, `false` otherwise.
100 */
102
103/**
104 * @brief If the given path is a mountpoint in the namespace, traverse to the mounted filesystem in an RCU read critical
105 * section, else no-op.
106 *
107 * @warning Will not increase the reference count of the returned path's mount and dentry, the caller must ensure that
108 * they are not freed while in use.
109 *
110 * @param ns The namespace containing the namespace to traverse.
111 * @param mount The output mount after traversal, may be unchanged if not traversed.
112 * @param dentry The output dentry after traversal, may be unchanged if not traversed.
113 * @return `true` if the path was modified, `false` otherwise.
114 */
116
117/**
118 * @brief Mount a filesystem in a namespace.
119 *
120 * @param ns The namespace containing the namespace to mount to.
121 * @param target The target path to mount to, can be `NULL` to mount to root.
122 * @param fs The filesystem to mount.
123 * @param options A string containing filesystem defined `key=value` pairs, with multiple options separated by commas,
124 * or `NULL`.
125 * @param flags Mount flags.
126 * @param mode The mode specifying permissions and mount behaviour.
127 * @param private Private data for the filesystem's mount function.
128 * @return On success, the new mount. On failure, returns `NULL` and `errno` is set to:
129 * - `EINVAL`: Invalid parameters.
130 * - `EIO`: The filesystem returned a invalid root dentry.
131 * - `EXDEV`: The target path is not visible in the namespace.
132 * - `ENODEV`: The specified filesystem does not exist.
133 * - `EBUSY`: Attempt to mount to already existing root.
134 * - `ENOMEM`: Out of memory.
135 * - `ENOENT`: The root does not exist or the target is negative.
136 * - Other errors as returned by the filesystem's `mount()` operation or `mount_new()`.
137 */
138mount_t* namespace_mount(namespace_t* ns, path_t* target, filesystem_t* fs, const char* options, mode_t mode,
139 void* data);
140
141/**
142 * @brief Bind a source path to a target path in a namespace.
143 *
144 * @param ns The namespace containing the namespace to bind in.
145 * @param target The target path to bind to, can be `NULL` to bind to root.
146 * @param source The source path to bind from, could be either a file or directory and from any filesystem.
147 * @param mode The mode specifying permissions and mount behaviour.
148 * @return On success, the new mount. On failure, returns `NULL` and `errno` is set to:
149 * - `EINVAL`: Invalid parameters.
150 * - `EACCES`: The requested mode exceeds the maximum allowed permissions.
151 * - `ENOMEM`: Out of memory.
152 * - Other errors as returned by `mount_new()`.
153 */
155
156/**
157 * @brief Remove a mount in a namespace.
158 *
159 * @param ns The namespace containing the namespace to unmount from.
160 * @param mount The mount to remove.
161 * @param mode The mode specifying unmount behaviour.
162 */
164
165/**
166 * @brief Get the root path of a namespace.
167 *
168 * @param ns The namespace containing the namespace to get the root of.
169 * @param out The output root path, may be a invalid `NULL` path if the namespace is empty.
170 */
172
173/**
174 * @brief Get the root mount of a namespace in an RCU read critical section.
175 *
176 * @warning Will not increase the reference count of the returned mount, the caller must ensure that the mount is not
177 * freed while in use.
178 *
179 * @param ns The namespace containing the namespace to get the root mount of.
180 * @param mount The output root mount, may be `NULL` if the namespace is empty.
181 * @param dentry The output root dentry, may be `NULL` if the namespace is empty.
182 */
184
185/** @} */
static fd_t data
Definition dwm.c:21
static clock_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:193
mount_t * namespace_bind(namespace_t *ns, path_t *target, path_t *source, mode_t mode)
Bind a source path to a target path in a namespace.
Definition namespace.c:427
void namespace_unmount(namespace_t *ns, mount_t *mount, mode_t mode)
Remove a mount in a namespace.
Definition namespace.c:459
#define MOUNT_STACK_MAX_MOUNTS
Maximum number of mounts that can be mounted to a single mountpoint.
Definition namespace.h:36
uint64_t namespace_copy(namespace_t *dest, namespace_t *src)
Copy mounts from one namespace to another.
Definition namespace.c:287
namespace_t * namespace_new(namespace_t *parent)
Create a new namespace.
Definition namespace.c:253
mount_t * namespace_mount(namespace_t *ns, path_t *target, filesystem_t *fs, const char *options, mode_t mode, void *data)
Mount a filesystem in a namespace.
Definition namespace.c:387
void namespace_rcu_get_root(namespace_t *ns, mount_t **mount, dentry_t **dentry)
Get the root mount of a namespace in an RCU read critical section.
Definition namespace.c:490
void namespace_get_root(namespace_t *ns, path_t *out)
Get the root path of a namespace.
Definition namespace.c:470
bool namespace_rcu_traverse(namespace_t *ns, mount_t **mount, dentry_t **dentry)
If the given path is a mountpoint in the namespace, traverse to the mounted filesystem in an RCU read...
Definition namespace.c:352
bool namespace_accessible(namespace_t *ns, namespace_t *other)
Check if mounts in a namespace can be propagated to another namespace.
Definition namespace.c:341
mode_t
Path flags and permissions.
Definition path.h:79
uint64_t mount(const char *mountpoint, const char *fs, const char *options)
System call for mounting a filesystem.
Definition mount.c:5
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:155
Filesystem structure, represents a filesystem type, e.g. fat32, tmpfs, devfs, etc.
Definition filesystem.h:53
A entry in a doubly linked list.
Definition list.h:37
A doubly linked list.
Definition list.h:46
Map entry structure.
Definition map.h:69
Hash map structure.
Definition map.h:90
Mount stack.
Definition namespace.h:45
uint64_t count
Definition namespace.h:49
list_entry_t entry
Definition namespace.h:46
map_entry_t mapEntry
Definition namespace.h:47
Mount structure.
Definition mount.h:48
Namespace structure.
Definition namespace.h:57
mount_stack_t root
The root mount stack.
Definition namespace.h:64
list_entry_t entry
The entry for the parent's children list.
Definition namespace.h:59
ref_t ref
Definition namespace.h:58
rwlock_t lock
Definition namespace.h:65
namespace_t * parent
The parent namespace, can be NULL.
Definition namespace.h:61
list_t stacks
List of mount_stack_t in this namespace.
Definition namespace.h:62
map_t mountMap
Map used to go from source dentries to namespace mount stacks.
Definition namespace.h:63
list_t children
List of child namespaces.
Definition namespace.h:60
Path structure.
Definition path.h:127
Process structure.
Definition process.h:76
Reference counting structure.
Definition ref.h:52
Read-Write Ticket Lock structure.
Definition rwlock.h:66