]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/namespace.h
systemctl: make dbus PID cgroup tree output look more like systemd-cgls
[thirdparty/systemd.git] / src / core / namespace.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 /***
5 Copyright © 2016 Djalal Harouni
6 ***/
7
8 typedef struct NamespaceInfo NamespaceInfo;
9 typedef struct BindMount BindMount;
10 typedef struct TemporaryFileSystem TemporaryFileSystem;
11 typedef struct MountImage MountImage;
12
13 #include <stdbool.h>
14
15 #include "dissect-image.h"
16 #include "fs-util.h"
17 #include "macro.h"
18 #include "string-util.h"
19
20 typedef enum ProtectHome {
21 PROTECT_HOME_NO,
22 PROTECT_HOME_YES,
23 PROTECT_HOME_READ_ONLY,
24 PROTECT_HOME_TMPFS,
25 _PROTECT_HOME_MAX,
26 _PROTECT_HOME_INVALID = -EINVAL,
27 } ProtectHome;
28
29 typedef enum NamespaceType {
30 NAMESPACE_MOUNT,
31 NAMESPACE_CGROUP,
32 NAMESPACE_UTS,
33 NAMESPACE_IPC,
34 NAMESPACE_USER,
35 NAMESPACE_PID,
36 NAMESPACE_NET,
37 _NAMESPACE_TYPE_MAX,
38 _NAMESPACE_TYPE_INVALID = -EINVAL,
39 } NamespaceType;
40
41 typedef enum ProtectSystem {
42 PROTECT_SYSTEM_NO,
43 PROTECT_SYSTEM_YES,
44 PROTECT_SYSTEM_FULL,
45 PROTECT_SYSTEM_STRICT,
46 _PROTECT_SYSTEM_MAX,
47 _PROTECT_SYSTEM_INVALID = -EINVAL,
48 } ProtectSystem;
49
50 typedef enum ProtectProc {
51 PROTECT_PROC_DEFAULT,
52 PROTECT_PROC_NOACCESS, /* hidepid=noaccess */
53 PROTECT_PROC_INVISIBLE, /* hidepid=invisible */
54 PROTECT_PROC_PTRACEABLE, /* hidepid=ptraceable */
55 _PROTECT_PROC_MAX,
56 _PROTECT_PROC_INVALID = -EINVAL,
57 } ProtectProc;
58
59 typedef enum ProcSubset {
60 PROC_SUBSET_ALL,
61 PROC_SUBSET_PID, /* subset=pid */
62 _PROC_SUBSET_MAX,
63 _PROC_SUBSET_INVALID = -EINVAL,
64 } ProcSubset;
65
66 struct NamespaceInfo {
67 bool ignore_protect_paths;
68 bool private_dev;
69 bool private_mounts;
70 bool protect_control_groups;
71 bool protect_kernel_tunables;
72 bool protect_kernel_modules;
73 bool protect_kernel_logs;
74 bool mount_apivfs;
75 bool protect_hostname;
76 bool private_ipc;
77 bool mount_nosuid;
78 ProtectHome protect_home;
79 ProtectSystem protect_system;
80 ProtectProc protect_proc;
81 ProcSubset proc_subset;
82 };
83
84 struct BindMount {
85 char *source;
86 char *destination;
87 bool read_only;
88 bool nosuid;
89 bool recursive;
90 bool ignore_enoent;
91 };
92
93 struct TemporaryFileSystem {
94 char *path;
95 char *options;
96 };
97
98 typedef enum MountImageType {
99 MOUNT_IMAGE_DISCRETE,
100 MOUNT_IMAGE_EXTENSION,
101 _MOUNT_IMAGE_TYPE_MAX,
102 _MOUNT_IMAGE_TYPE_INVALID = -EINVAL,
103 } MountImageType;
104
105 struct MountImage {
106 char *source;
107 char *destination; /* Unused if MountImageType == MOUNT_IMAGE_EXTENSION */
108 LIST_HEAD(MountOptions, mount_options);
109 bool ignore_enoent;
110 MountImageType type;
111 };
112
113 int setup_namespace(
114 const char *root_directory,
115 const char *root_image,
116 const MountOptions *root_image_options,
117 const NamespaceInfo *ns_info,
118 char **read_write_paths,
119 char **read_only_paths,
120 char **inaccessible_paths,
121 char **exec_paths,
122 char **no_exec_paths,
123 char **empty_directories,
124 const BindMount *bind_mounts,
125 size_t n_bind_mounts,
126 const TemporaryFileSystem *temporary_filesystems,
127 size_t n_temporary_filesystems,
128 const MountImage *mount_images,
129 size_t n_mount_images,
130 const char *tmp_dir,
131 const char *var_tmp_dir,
132 const char *creds_path,
133 const char *log_namespace,
134 unsigned long mount_flags,
135 const void *root_hash,
136 size_t root_hash_size,
137 const char *root_hash_path,
138 const void *root_hash_sig,
139 size_t root_hash_sig_size,
140 const char *root_hash_sig_path,
141 const char *root_verity,
142 const MountImage *extension_images,
143 size_t n_extension_images,
144 const char *propagate_dir,
145 const char *incoming_dir,
146 const char *notify_socket,
147 char **error_path);
148
149 #define RUN_SYSTEMD_EMPTY "/run/systemd/empty"
150
151 static inline char* namespace_cleanup_tmpdir(char *p) {
152 PROTECT_ERRNO;
153 if (!streq_ptr(p, RUN_SYSTEMD_EMPTY))
154 (void) rmdir(p);
155 return mfree(p);
156 }
157 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, namespace_cleanup_tmpdir);
158
159 int setup_tmp_dirs(
160 const char *id,
161 char **tmp_dir,
162 char **var_tmp_dir);
163
164 int setup_shareable_ns(const int ns_storage_socket[static 2], unsigned long nsflag);
165 int open_shareable_ns_path(const int netns_storage_socket[static 2], const char *path, unsigned long nsflag);
166
167 const char* protect_home_to_string(ProtectHome p) _const_;
168 ProtectHome protect_home_from_string(const char *s) _pure_;
169
170 const char* protect_system_to_string(ProtectSystem p) _const_;
171 ProtectSystem protect_system_from_string(const char *s) _pure_;
172
173 const char* protect_proc_to_string(ProtectProc i) _const_;
174 ProtectProc protect_proc_from_string(const char *s) _pure_;
175
176 const char* proc_subset_to_string(ProcSubset i) _const_;
177 ProcSubset proc_subset_from_string(const char *s) _pure_;
178
179 void bind_mount_free_many(BindMount *b, size_t n);
180 int bind_mount_add(BindMount **b, size_t *n, const BindMount *item);
181
182 void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n);
183 int temporary_filesystem_add(TemporaryFileSystem **t, size_t *n,
184 const char *path, const char *options);
185
186 MountImage* mount_image_free_many(MountImage *m, size_t *n);
187 int mount_image_add(MountImage **m, size_t *n, const MountImage *item);
188
189 const char* namespace_type_to_string(NamespaceType t) _const_;
190 NamespaceType namespace_type_from_string(const char *s) _pure_;
191
192 bool ns_type_supported(NamespaceType type);