Debian Codegraph @

src/core/namespace.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 /***
5 Copyright © 2016 Djalal Harouni
6 ***/
7
8 #include "core-forward.h"
9 #include "runtime-scope.h"
10
11 typedef enum ProtectHome {
12 PROTECT_HOME_NO,
13 PROTECT_HOME_YES,
14 PROTECT_HOME_READ_ONLY,
15 PROTECT_HOME_TMPFS,
16 _PROTECT_HOME_MAX,
17 _PROTECT_HOME_INVALID = -EINVAL,
18 } ProtectHome;
19
20 typedef enum ProtectHostname {
21 PROTECT_HOSTNAME_NO,
22 PROTECT_HOSTNAME_YES,
23 PROTECT_HOSTNAME_PRIVATE,
24 _PROTECT_HOSTNAME_MAX,
25 _PROTECT_HOSTNAME_INVALID = -EINVAL,
26 } ProtectHostname;
27
28 typedef enum ProtectSystem {
29 PROTECT_SYSTEM_NO,
30 PROTECT_SYSTEM_YES,
31 PROTECT_SYSTEM_FULL,
32 PROTECT_SYSTEM_STRICT,
33 _PROTECT_SYSTEM_MAX,
34 _PROTECT_SYSTEM_INVALID = -EINVAL,
35 } ProtectSystem;
36
37 typedef enum ProtectProc {
38 PROTECT_PROC_DEFAULT,
39 PROTECT_PROC_NOACCESS, /* hidepid=noaccess */
40 PROTECT_PROC_INVISIBLE, /* hidepid=invisible */
41 PROTECT_PROC_PTRACEABLE, /* hidepid=ptraceable */
42 _PROTECT_PROC_MAX,
43 _PROTECT_PROC_INVALID = -EINVAL,
44 } ProtectProc;
45
46 typedef enum ProcSubset {
47 PROC_SUBSET_ALL,
48 PROC_SUBSET_PID, /* subset=pid */
49 _PROC_SUBSET_MAX,
50 _PROC_SUBSET_INVALID = -EINVAL,
51 } ProcSubset;
52
53 typedef enum PrivateBPF {
54 PRIVATE_BPF_NO,
55 PRIVATE_BPF_YES,
56 _PRIVATE_BPF_MAX,
57 _PRIVATE_BPF_INVALID = -EINVAL,
58 } PrivateBPF;
59
60 typedef enum PrivateTmp {
61 PRIVATE_TMP_NO,
62 PRIVATE_TMP_CONNECTED, /* Bind mounted from the host's filesystem */
63 PRIVATE_TMP_DISCONNECTED, /* A completely private tmpfs, invisible from the host */
64 _PRIVATE_TMP_MAX,
65 _PRIVATE_TMP_INVALID = -EINVAL,
66 } PrivateTmp;
67
68 typedef enum PrivateUsers {
69 PRIVATE_USERS_NO,
70 PRIVATE_USERS_SELF,
71 PRIVATE_USERS_IDENTITY,
72 PRIVATE_USERS_FULL,
73 PRIVATE_USERS_MANAGED,
74 _PRIVATE_USERS_MAX,
75 _PRIVATE_USERS_INVALID = -EINVAL,
76 } PrivateUsers;
77
78 typedef enum ProtectControlGroups {
79 PROTECT_CONTROL_GROUPS_NO,
80 PROTECT_CONTROL_GROUPS_YES,
81 PROTECT_CONTROL_GROUPS_PRIVATE,
82 PROTECT_CONTROL_GROUPS_STRICT,
83 _PROTECT_CONTROL_GROUPS_MAX,
84 _PROTECT_CONTROL_GROUPS_INVALID = -EINVAL,
85 } ProtectControlGroups;
86
87 typedef enum PrivatePIDs {
88 PRIVATE_PIDS_NO,
89 PRIVATE_PIDS_YES,
90 _PRIVATE_PIDS_MAX,
91 _PRIVATE_PIDS_INVALID = -EINVAL,
92 } PrivatePIDs;
93
94 typedef struct PinnedResource {
95 /* Pins a disk image, directory or mstack by file descriptors. The paths are stored too, but they are
96 * intended to be decoration only, to enhance log messages and should not be load-bearing
97 * otherwise. */
98 int directory_fd;
99 char *directory;
100 int image_fd;
101 char *image;
102 MStack *mstack_loaded;
103 char *mstack;
104 } PinnedResource;
105
106 #define PINNED_RESOURCE_NULL \
107 (PinnedResource) { \
108 .directory_fd = -EBADF, \
109 .image_fd = -EBADF, \
110 }
111
112 typedef struct BindMount {
113 char *source;
114 char *destination;
115 bool read_only;
116 bool nodev;
117 bool nosuid;
118 bool noexec;
119 bool recursive;
120 bool ignore_enoent;
121 bool idmapped;
122 uid_t uid;
123 gid_t gid;
124 } BindMount;
125
126 typedef struct TemporaryFileSystem {
127 char *path;
128 char *options;
129 } TemporaryFileSystem;
130
131 typedef enum MountImageType {
132 MOUNT_IMAGE_DISCRETE,
133 MOUNT_IMAGE_EXTENSION,
134 _MOUNT_IMAGE_TYPE_MAX,
135 _MOUNT_IMAGE_TYPE_INVALID = -EINVAL,
136 } MountImageType;
137
138 typedef struct MountImage {
139 char *source;
140 char *destination; /* Unused if MountImageType == MOUNT_IMAGE_EXTENSION */
141 MountOptions *mount_options;
142 bool ignore_enoent;
143 MountImageType type;
144 } MountImage;
145
146 typedef struct NamespaceParameters {
147 RuntimeScope runtime_scope;
148
149 const PinnedResource *rootfs;
150 const MountOptions *root_image_options;
151 const ImagePolicy *root_image_policy;
152
153 char **read_write_paths;
154 char **read_only_paths;
155 char **inaccessible_paths;
156
157 char **exec_paths;
158 char **no_exec_paths;
159
160 char **empty_directories;
161 char **symlinks;
162
163 const BindMount *bind_mounts;
164 size_t n_bind_mounts;
165
166 const TemporaryFileSystem *temporary_filesystems;
167 size_t n_temporary_filesystems;
168
169 const MountImage *mount_images;
170 size_t n_mount_images;
171 const ImagePolicy *mount_image_policy;
172
173 const char *tmp_dir;
174 const char *var_tmp_dir;
175
176 const char *creds_path;
177 const char *log_namespace;
178
179 unsigned long mount_propagation_flag;
180 VeritySettings *verity;
181
182 const MountImage *extension_images;
183 size_t n_extension_images;
184 const ImagePolicy *extension_image_policy;
185 char **extension_directories;
186
187 const char *propagate_dir;
188 const char *incoming_dir;
189
190 const char *private_namespace_dir;
191 const char *host_notify_socket;
192 const char *notify_socket_path;
193 const char *host_os_release_stage;
194
195 bool ignore_protect_paths;
196
197 bool protect_kernel_tunables;
198 bool protect_kernel_modules;
199 bool protect_kernel_logs;
200
201 bool private_dev;
202 bool private_network;
203 bool private_ipc;
204
205 bool mount_apivfs;
206 bool bind_log_sockets;
207 bool mount_nosuid;
208
209 ProtectControlGroups protect_control_groups;
210 ProtectHome protect_home;
211 ProtectHostname protect_hostname;
212 ProtectSystem protect_system;
213 ProtectProc protect_proc;
214 ProcSubset proc_subset;
215 PrivateBPF private_bpf;
216 PrivateTmp private_tmp;
217 PrivateTmp private_var_tmp;
218 PrivatePIDs private_pids;
219 PrivateUsers private_users;
220
221 PidRef *bpffs_pidref;
222 int bpffs_socket_fd;
223 int bpffs_errno_pipe;
224
225 sd_varlink *mountfsd_link;
226 } NamespaceParameters;
227
228 int setup_namespace(const NamespaceParameters *p, char **reterr_path);
229
230 #define RUN_SYSTEMD_EMPTY "/run/systemd/empty"
231
232 char* namespace_cleanup_tmpdir(char *p);
233 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, namespace_cleanup_tmpdir);
234
235 int setup_tmp_dir_one(const char *id, const char *prefix, char **ret_path);
236
237 int setup_shareable_ns(int ns_storage_socket[static 2], unsigned long nsflag);
238 int open_shareable_ns_path(int netns_storage_socket[static 2], const char *path, unsigned long nsflag);
239
240 DECLARE_STRING_TABLE_LOOKUP(protect_home, ProtectHome);
241
242 DECLARE_STRING_TABLE_LOOKUP(protect_hostname, ProtectHostname);
243
244 DECLARE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem);
245
246 DECLARE_STRING_TABLE_LOOKUP(protect_proc, ProtectProc);
247
248 DECLARE_STRING_TABLE_LOOKUP(proc_subset, ProcSubset);
249
250 DECLARE_STRING_TABLE_LOOKUP(private_bpf, PrivateBPF);
251
252 DECLARE_STRING_TABLE_LOOKUP(bpf_delegate_cmd, uint64_t);
253
254 DECLARE_STRING_TABLE_LOOKUP(bpf_delegate_map_type, uint64_t);
255
256 DECLARE_STRING_TABLE_LOOKUP(bpf_delegate_prog_type, uint64_t);
257
258 DECLARE_STRING_TABLE_LOOKUP(bpf_delegate_attach_type, uint64_t);
259
260 char* bpf_delegate_to_string(uint64_t u, const char * (*parser)(uint64_t) _const_);
261 int bpf_delegate_from_string(const char *s, uint64_t *ret, uint64_t (*parser)(const char *));
262
263 static inline int bpf_delegate_commands_from_string(const char *s, uint64_t *ret) {
264 return bpf_delegate_from_string(s, ret, bpf_delegate_cmd_from_string);
265 }
266
267 static inline char * bpf_delegate_commands_to_string(uint64_t u) {
268 return bpf_delegate_to_string(u, bpf_delegate_cmd_to_string);
269 }
270
271 static inline int bpf_delegate_maps_from_string(const char *s, uint64_t *ret) {
272 return bpf_delegate_from_string(s, ret, bpf_delegate_map_type_from_string);
273 }
274
275 static inline char * bpf_delegate_maps_to_string(uint64_t u) {
276 return bpf_delegate_to_string(u, bpf_delegate_map_type_to_string);
277 }
278
279 static inline int bpf_delegate_programs_from_string(const char *s, uint64_t *ret) {
280 return bpf_delegate_from_string(s, ret, bpf_delegate_prog_type_from_string);
281 }
282
283 static inline char * bpf_delegate_programs_to_string(uint64_t u) {
284 return bpf_delegate_to_string(u, bpf_delegate_prog_type_to_string);
285 }
286
287 static inline int bpf_delegate_attachments_from_string(const char *s, uint64_t *ret) {
288 return bpf_delegate_from_string(s, ret, bpf_delegate_attach_type_from_string);
289 }
290
291 static inline char * bpf_delegate_attachments_to_string(uint64_t u) {
292 return bpf_delegate_to_string(u, bpf_delegate_attach_type_to_string);
293 }
294
295 DECLARE_STRING_TABLE_LOOKUP(private_tmp, PrivateTmp);
296
297 DECLARE_STRING_TABLE_LOOKUP(private_users, PrivateUsers);
298
299 DECLARE_STRING_TABLE_LOOKUP(protect_control_groups, ProtectControlGroups);
300
301 DECLARE_STRING_TABLE_LOOKUP(private_pids, PrivatePIDs);
302
303 void bind_mount_free_many(BindMount *b, size_t n);
304 int bind_mount_add(BindMount **b, size_t *n, const BindMount *item);
305
306 void mount_image_free_array(MountImage *array, size_t n);
307 int mount_image_add(MountImage **m, size_t *n, const MountImage *item);
308
309 void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n);
310 int temporary_filesystem_add(
311 TemporaryFileSystem **t,
312 size_t *n,
313 const char *path,
314 const char *options);
315
316 int refresh_extensions_in_namespace(
317 const PidRef *target,
318 const char *hierarchy_env,
319 const NamespaceParameters *p);
320
321 void pinned_resource_done(PinnedResource *p);
322 bool pinned_resource_is_set(const PinnedResource *p);