]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
15ae422b LP |
2 | |
3 | #include <errno.h> | |
e08f94ac | 4 | #include <linux/loop.h> |
07630cea | 5 | #include <sched.h> |
15ae422b | 6 | #include <stdio.h> |
07630cea | 7 | #include <sys/mount.h> |
07630cea | 8 | #include <unistd.h> |
25e870b5 | 9 | #include <linux/fs.h> |
15ae422b | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
10404d52 | 12 | #include "base-filesystem.h" |
f4351959 | 13 | #include "chase-symlinks.h" |
7f112f50 | 14 | #include "dev-setup.h" |
93f59701 LB |
15 | #include "env-util.h" |
16 | #include "escape.h" | |
17 | #include "extension-release.h" | |
3ffd4af2 | 18 | #include "fd-util.h" |
e5f10caf | 19 | #include "format-util.h" |
e908468b | 20 | #include "label.h" |
b3d13314 | 21 | #include "list.h" |
915e6d16 | 22 | #include "loop-util.h" |
07630cea | 23 | #include "loopback-setup.h" |
35cd0ba5 | 24 | #include "mkdir-label.h" |
4349cd7c | 25 | #include "mount-util.h" |
049af8ad | 26 | #include "mountpoint-util.h" |
0cb8e3d1 | 27 | #include "namespace-util.h" |
3ffd4af2 | 28 | #include "namespace.h" |
54c2459d | 29 | #include "nsflags.h" |
d8b4d14d | 30 | #include "nulstr-util.h" |
93f59701 | 31 | #include "os-util.h" |
07630cea | 32 | #include "path-util.h" |
d7b8eec7 | 33 | #include "selinux-util.h" |
2583fbea | 34 | #include "socket-util.h" |
760877e9 | 35 | #include "sort-util.h" |
36ce7110 | 36 | #include "stat-util.h" |
8b43440b | 37 | #include "string-table.h" |
07630cea LP |
38 | #include "string-util.h" |
39 | #include "strv.h" | |
a652f050 | 40 | #include "tmpfile-util.h" |
affb60b1 | 41 | #include "umask-util.h" |
ee104e11 | 42 | #include "user-util.h" |
15ae422b | 43 | |
737ba3c8 | 44 | #define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC) |
45 | ||
c17ec25e | 46 | typedef enum MountMode { |
15ae422b LP |
47 | /* This is ordered by priority! */ |
48 | INACCESSIBLE, | |
93f59701 | 49 | OVERLAY_MOUNT, |
b3d13314 | 50 | MOUNT_IMAGES, |
d2d6c096 LP |
51 | BIND_MOUNT, |
52 | BIND_MOUNT_RECURSIVE, | |
ac0930c8 | 53 | PRIVATE_TMP, |
56a13a49 | 54 | PRIVATE_TMP_READONLY, |
7f112f50 | 55 | PRIVATE_DEV, |
5d997827 | 56 | BIND_DEV, |
6c47cd7d | 57 | EMPTY_DIR, |
5d997827 LP |
58 | SYSFS, |
59 | PROCFS, | |
94293d65 | 60 | RUN, |
5d997827 | 61 | READONLY, |
59eeb84b | 62 | READWRITE, |
ddc155b2 TM |
63 | NOEXEC, |
64 | EXEC, | |
2abd4e38 | 65 | TMPFS, |
93f59701 | 66 | EXTENSION_IMAGES, /* Mounted outside the root directory, and used by subsequent mounts */ |
80271a44 | 67 | MQUEUEFS, |
1e05071d | 68 | READWRITE_IMPLICIT, /* Should have the lowest priority. */ |
5beb8688 | 69 | _MOUNT_MODE_MAX, |
c17ec25e | 70 | } MountMode; |
15ae422b | 71 | |
34de407a | 72 | typedef struct MountEntry { |
5327c910 | 73 | const char *path_const; /* Memory allocated on stack or static */ |
cfbeb4ef | 74 | MountMode mode:5; |
5327c910 LP |
75 | bool ignore:1; /* Ignore if path does not exist? */ |
76 | bool has_prefix:1; /* Already is prefixed by the root dir? */ | |
cfbeb4ef | 77 | bool read_only:1; /* Shall this mount point be read-only? */ |
9ce4e4b0 | 78 | bool nosuid:1; /* Shall set MS_NOSUID on the mount itself */ |
ddc155b2 TM |
79 | bool noexec:1; /* Shall set MS_NOEXEC on the mount itself */ |
80 | bool exec:1; /* Shall clear MS_NOEXEC on the mount itself */ | |
088696fe | 81 | bool applied:1; /* Already applied */ |
55fe7432 | 82 | char *path_malloc; /* Use this instead of 'path_const' if we had to allocate memory */ |
809ceb82 LB |
83 | const char *unprefixed_path_const; /* If the path was amended with a prefix, these will save the original */ |
84 | char *unprefixed_path_malloc; | |
b3d13314 | 85 | const char *source_const; /* The source path, for bind mounts or images */ |
d2d6c096 | 86 | char *source_malloc; |
2abd4e38 YW |
87 | const char *options_const;/* Mount options for tmpfs */ |
88 | char *options_malloc; | |
89 | unsigned long flags; /* Mount flags used by EMPTY_DIR and TMPFS. Do not include MS_RDONLY here, but please use read_only. */ | |
088696fe | 90 | unsigned n_followed; |
427353f6 | 91 | LIST_HEAD(MountOptions, image_options); |
34de407a | 92 | } MountEntry; |
15ae422b | 93 | |
94293d65 | 94 | /* If MountAPIVFS= is used, let's mount /sys, /proc, /dev and /run into the it, but only as a fallback if the user hasn't mounted |
3fe91079 | 95 | * something there already. These mounts are hence overridden by any other explicitly configured mounts. */ |
5d997827 LP |
96 | static const MountEntry apivfs_table[] = { |
97 | { "/proc", PROCFS, false }, | |
98 | { "/dev", BIND_DEV, false }, | |
99 | { "/sys", SYSFS, false }, | |
94293d65 | 100 | { "/run", RUN, false, .options_const = "mode=755" TMPFS_LIMITS_RUN, .flags = MS_NOSUID|MS_NODEV|MS_STRICTATIME }, |
5d997827 | 101 | }; |
f471b2af | 102 | |
11a30cec | 103 | /* ProtectKernelTunables= option and the related filesystem APIs */ |
34de407a | 104 | static const MountEntry protect_kernel_tunables_table[] = { |
1e05071d YW |
105 | { "/proc/acpi", READONLY, true }, |
106 | { "/proc/apm", READONLY, true }, /* Obsolete API, there's no point in permitting access to this, ever */ | |
107 | { "/proc/asound", READONLY, true }, | |
108 | { "/proc/bus", READONLY, true }, | |
109 | { "/proc/fs", READONLY, true }, | |
110 | { "/proc/irq", READONLY, true }, | |
111 | { "/proc/kallsyms", INACCESSIBLE, true }, | |
112 | { "/proc/kcore", INACCESSIBLE, true }, | |
113 | { "/proc/latency_stats", READONLY, true }, | |
114 | { "/proc/mtrr", READONLY, true }, | |
115 | { "/proc/scsi", READONLY, true }, | |
4e399953 | 116 | { "/proc/sys", READONLY, true }, |
1e05071d YW |
117 | { "/proc/sysrq-trigger", READONLY, true }, |
118 | { "/proc/timer_stats", READONLY, true }, | |
119 | { "/sys", READONLY, false }, | |
120 | { "/sys/fs/bpf", READONLY, true }, | |
121 | { "/sys/fs/cgroup", READWRITE_IMPLICIT, false }, /* READONLY is set by ProtectControlGroups= option */ | |
122 | { "/sys/fs/selinux", READWRITE_IMPLICIT, true }, | |
123 | { "/sys/kernel/debug", READONLY, true }, | |
124 | { "/sys/kernel/tracing", READONLY, true }, | |
11a30cec DH |
125 | }; |
126 | ||
c575770b | 127 | /* ProtectKernelModules= option */ |
34de407a | 128 | static const MountEntry protect_kernel_modules_table[] = { |
349cc4a5 | 129 | #if HAVE_SPLIT_USR |
c6232fb0 | 130 | { "/lib/modules", INACCESSIBLE, true }, |
c575770b | 131 | #endif |
c6232fb0 | 132 | { "/usr/lib/modules", INACCESSIBLE, true }, |
c575770b DH |
133 | }; |
134 | ||
94a7b275 KK |
135 | /* ProtectKernelLogs= option */ |
136 | static const MountEntry protect_kernel_logs_table[] = { | |
137 | { "/proc/kmsg", INACCESSIBLE, true }, | |
138 | { "/dev/kmsg", INACCESSIBLE, true }, | |
139 | }; | |
140 | ||
b6c432ca DH |
141 | /* |
142 | * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of | |
143 | * system should be protected by ProtectSystem= | |
144 | */ | |
34de407a | 145 | static const MountEntry protect_home_read_only_table[] = { |
c6232fb0 LP |
146 | { "/home", READONLY, true }, |
147 | { "/run/user", READONLY, true }, | |
148 | { "/root", READONLY, true }, | |
b6c432ca DH |
149 | }; |
150 | ||
e4da7d8c YW |
151 | /* ProtectHome=tmpfs table */ |
152 | static const MountEntry protect_home_tmpfs_table[] = { | |
7d85383e TM |
153 | { "/home", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, |
154 | { "/run/user", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, | |
155 | { "/root", TMPFS, true, .read_only = true, .options_const = "mode=0700" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, | |
e4da7d8c YW |
156 | }; |
157 | ||
b6c432ca | 158 | /* ProtectHome=yes table */ |
34de407a | 159 | static const MountEntry protect_home_yes_table[] = { |
c6232fb0 LP |
160 | { "/home", INACCESSIBLE, true }, |
161 | { "/run/user", INACCESSIBLE, true }, | |
162 | { "/root", INACCESSIBLE, true }, | |
b6c432ca DH |
163 | }; |
164 | ||
f471b2af | 165 | /* ProtectSystem=yes table */ |
34de407a | 166 | static const MountEntry protect_system_yes_table[] = { |
c6232fb0 LP |
167 | { "/usr", READONLY, false }, |
168 | { "/boot", READONLY, true }, | |
169 | { "/efi", READONLY, true }, | |
7486f305 AB |
170 | #if HAVE_SPLIT_USR |
171 | { "/lib", READONLY, true }, | |
172 | { "/lib64", READONLY, true }, | |
173 | { "/bin", READONLY, true }, | |
671f0f8d | 174 | # if HAVE_SPLIT_BIN |
7486f305 | 175 | { "/sbin", READONLY, true }, |
671f0f8d | 176 | # endif |
7486f305 | 177 | #endif |
f471b2af DH |
178 | }; |
179 | ||
180 | /* ProtectSystem=full includes ProtectSystem=yes */ | |
34de407a | 181 | static const MountEntry protect_system_full_table[] = { |
c6232fb0 LP |
182 | { "/usr", READONLY, false }, |
183 | { "/boot", READONLY, true }, | |
184 | { "/efi", READONLY, true }, | |
185 | { "/etc", READONLY, false }, | |
7486f305 AB |
186 | #if HAVE_SPLIT_USR |
187 | { "/lib", READONLY, true }, | |
188 | { "/lib64", READONLY, true }, | |
189 | { "/bin", READONLY, true }, | |
671f0f8d | 190 | # if HAVE_SPLIT_BIN |
7486f305 | 191 | { "/sbin", READONLY, true }, |
671f0f8d | 192 | # endif |
7486f305 | 193 | #endif |
f471b2af DH |
194 | }; |
195 | ||
196 | /* | |
197 | * ProtectSystem=strict table. In this strict mode, we mount everything | |
198 | * read-only, except for /proc, /dev, /sys which are the kernel API VFS, | |
199 | * which are left writable, but PrivateDevices= + ProtectKernelTunables= | |
200 | * protect those, and these options should be fully orthogonal. | |
201 | * (And of course /home and friends are also left writable, as ProtectHome= | |
202 | * shall manage those, orthogonally). | |
203 | */ | |
34de407a | 204 | static const MountEntry protect_system_strict_table[] = { |
1e05071d YW |
205 | { "/", READONLY, false }, |
206 | { "/proc", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */ | |
207 | { "/sys", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */ | |
208 | { "/dev", READWRITE_IMPLICIT, false }, /* PrivateDevices= */ | |
209 | { "/home", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
210 | { "/run/user", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
211 | { "/root", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
f471b2af DH |
212 | }; |
213 | ||
5beb8688 YW |
214 | static const char * const mount_mode_table[_MOUNT_MODE_MAX] = { |
215 | [INACCESSIBLE] = "inaccessible", | |
93f59701 | 216 | [OVERLAY_MOUNT] = "overlay", |
5beb8688 YW |
217 | [BIND_MOUNT] = "bind", |
218 | [BIND_MOUNT_RECURSIVE] = "rbind", | |
219 | [PRIVATE_TMP] = "private-tmp", | |
220 | [PRIVATE_DEV] = "private-dev", | |
221 | [BIND_DEV] = "bind-dev", | |
222 | [EMPTY_DIR] = "empty", | |
223 | [SYSFS] = "sysfs", | |
224 | [PROCFS] = "procfs", | |
225 | [READONLY] = "read-only", | |
226 | [READWRITE] = "read-write", | |
227 | [TMPFS] = "tmpfs", | |
b3d13314 | 228 | [MOUNT_IMAGES] = "mount-images", |
5beb8688 | 229 | [READWRITE_IMPLICIT] = "rw-implicit", |
ddc155b2 TM |
230 | [EXEC] = "exec", |
231 | [NOEXEC] = "noexec", | |
80271a44 | 232 | [MQUEUEFS] = "mqueuefs", |
5beb8688 YW |
233 | }; |
234 | ||
235 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(mount_mode, MountMode); | |
236 | ||
34de407a | 237 | static const char *mount_entry_path(const MountEntry *p) { |
f0a4feb0 DH |
238 | assert(p); |
239 | ||
5327c910 LP |
240 | /* Returns the path of this bind mount. If the malloc()-allocated ->path_buffer field is set we return that, |
241 | * otherwise the stack/static ->path field is returned. */ | |
f0a4feb0 | 242 | |
5327c910 | 243 | return p->path_malloc ?: p->path_const; |
f0a4feb0 DH |
244 | } |
245 | ||
809ceb82 LB |
246 | static const char *mount_entry_unprefixed_path(const MountEntry *p) { |
247 | assert(p); | |
248 | ||
249 | /* Returns the unprefixed path (ie: before prefix_where_needed() ran), if any */ | |
250 | ||
251 | return p->unprefixed_path_malloc ?: p->unprefixed_path_const ?: mount_entry_path(p); | |
252 | } | |
253 | ||
254 | static void mount_entry_consume_prefix(MountEntry *p, char *new_path) { | |
255 | assert(p); | |
256 | assert(p->path_malloc || p->path_const); | |
257 | assert(new_path); | |
258 | ||
259 | /* Saves current path in unprefixed_ variable, and takes over new_path */ | |
260 | ||
261 | free_and_replace(p->unprefixed_path_malloc, p->path_malloc); | |
262 | /* If we didn't have a path on the heap, then it's a static one */ | |
263 | if (!p->unprefixed_path_malloc) | |
264 | p->unprefixed_path_const = p->path_const; | |
265 | p->path_malloc = new_path; | |
266 | p->has_prefix = true; | |
267 | } | |
268 | ||
34de407a | 269 | static bool mount_entry_read_only(const MountEntry *p) { |
cfbeb4ef LP |
270 | assert(p); |
271 | ||
56a13a49 | 272 | return p->read_only || IN_SET(p->mode, READONLY, INACCESSIBLE, PRIVATE_TMP_READONLY); |
cfbeb4ef LP |
273 | } |
274 | ||
ddc155b2 TM |
275 | static bool mount_entry_noexec(const MountEntry *p) { |
276 | assert(p); | |
277 | ||
988bcc7c | 278 | return p->noexec || IN_SET(p->mode, NOEXEC, INACCESSIBLE, SYSFS, PROCFS); |
ddc155b2 TM |
279 | } |
280 | ||
281 | static bool mount_entry_exec(const MountEntry *p) { | |
282 | assert(p); | |
283 | ||
284 | return p->exec || p->mode == EXEC; | |
285 | } | |
286 | ||
d2d6c096 LP |
287 | static const char *mount_entry_source(const MountEntry *p) { |
288 | assert(p); | |
289 | ||
290 | return p->source_malloc ?: p->source_const; | |
291 | } | |
292 | ||
2abd4e38 YW |
293 | static const char *mount_entry_options(const MountEntry *p) { |
294 | assert(p); | |
295 | ||
296 | return p->options_malloc ?: p->options_const; | |
297 | } | |
298 | ||
1eb7e08e LP |
299 | static void mount_entry_done(MountEntry *p) { |
300 | assert(p); | |
301 | ||
302 | p->path_malloc = mfree(p->path_malloc); | |
809ceb82 | 303 | p->unprefixed_path_malloc = mfree(p->unprefixed_path_malloc); |
1eb7e08e | 304 | p->source_malloc = mfree(p->source_malloc); |
2abd4e38 | 305 | p->options_malloc = mfree(p->options_malloc); |
427353f6 | 306 | p->image_options = mount_options_free_all(p->image_options); |
1eb7e08e LP |
307 | } |
308 | ||
d18aff04 | 309 | static int append_access_mounts(MountEntry **p, char **strv, MountMode mode, bool forcibly_require_prefix) { |
15ae422b LP |
310 | char **i; |
311 | ||
613b411c LP |
312 | assert(p); |
313 | ||
1e05071d | 314 | /* Adds a list of user-supplied READWRITE/READWRITE_IMPLICIT/READONLY/INACCESSIBLE entries */ |
5327c910 | 315 | |
15ae422b | 316 | STRV_FOREACH(i, strv) { |
5327c910 LP |
317 | bool ignore = false, needs_prefix = false; |
318 | const char *e = *i; | |
15ae422b | 319 | |
5327c910 LP |
320 | /* Look for any prefixes */ |
321 | if (startswith(e, "-")) { | |
322 | e++; | |
9c94d52e | 323 | ignore = true; |
ea92ae33 | 324 | } |
5327c910 LP |
325 | if (startswith(e, "+")) { |
326 | e++; | |
327 | needs_prefix = true; | |
328 | } | |
ea92ae33 | 329 | |
baaa35ad ZJS |
330 | if (!path_is_absolute(e)) |
331 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
332 | "Path is not absolute: %s", e); | |
15ae422b | 333 | |
34de407a | 334 | *((*p)++) = (MountEntry) { |
5327c910 LP |
335 | .path_const = e, |
336 | .mode = mode, | |
337 | .ignore = ignore, | |
d18aff04 | 338 | .has_prefix = !needs_prefix && !forcibly_require_prefix, |
5327c910 | 339 | }; |
15ae422b LP |
340 | } |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
6c47cd7d LP |
345 | static int append_empty_dir_mounts(MountEntry **p, char **strv) { |
346 | char **i; | |
347 | ||
348 | assert(p); | |
349 | ||
350 | /* Adds tmpfs mounts to provide readable but empty directories. This is primarily used to implement the | |
351 | * "/private/" boundary directories for DynamicUser=1. */ | |
352 | ||
353 | STRV_FOREACH(i, strv) { | |
354 | ||
355 | *((*p)++) = (MountEntry) { | |
356 | .path_const = *i, | |
357 | .mode = EMPTY_DIR, | |
358 | .ignore = false, | |
6c47cd7d | 359 | .read_only = true, |
7d85383e | 360 | .options_const = "mode=755" TMPFS_LIMITS_EMPTY_OR_ALMOST, |
2abd4e38 | 361 | .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, |
6c47cd7d LP |
362 | }; |
363 | } | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
da6053d0 | 368 | static int append_bind_mounts(MountEntry **p, const BindMount *binds, size_t n) { |
d2d6c096 LP |
369 | assert(p); |
370 | ||
fe96c0f8 | 371 | for (size_t i = 0; i < n; i++) { |
d2d6c096 LP |
372 | const BindMount *b = binds + i; |
373 | ||
374 | *((*p)++) = (MountEntry) { | |
375 | .path_const = b->destination, | |
376 | .mode = b->recursive ? BIND_MOUNT_RECURSIVE : BIND_MOUNT, | |
377 | .read_only = b->read_only, | |
9ce4e4b0 | 378 | .nosuid = b->nosuid, |
d2d6c096 | 379 | .source_const = b->source, |
4ca763a9 | 380 | .ignore = b->ignore_enoent, |
d2d6c096 LP |
381 | }; |
382 | } | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
b3d13314 LB |
387 | static int append_mount_images(MountEntry **p, const MountImage *mount_images, size_t n) { |
388 | assert(p); | |
389 | ||
390 | for (size_t i = 0; i < n; i++) { | |
391 | const MountImage *m = mount_images + i; | |
392 | ||
393 | *((*p)++) = (MountEntry) { | |
394 | .path_const = m->destination, | |
395 | .mode = MOUNT_IMAGES, | |
396 | .source_const = m->source, | |
427353f6 | 397 | .image_options = m->mount_options, |
b3d13314 LB |
398 | .ignore = m->ignore_enoent, |
399 | }; | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
93f59701 LB |
405 | static int append_extension_images( |
406 | MountEntry **p, | |
407 | const char *root, | |
408 | const char *extension_dir, | |
409 | char **hierarchies, | |
410 | const MountImage *mount_images, | |
411 | size_t n) { | |
412 | ||
413 | _cleanup_strv_free_ char **overlays = NULL; | |
414 | char **hierarchy; | |
415 | int r; | |
416 | ||
417 | assert(p); | |
418 | assert(extension_dir); | |
419 | ||
420 | if (n == 0) | |
421 | return 0; | |
422 | ||
423 | /* Prepare a list of overlays, that will have as each element a string suitable for being | |
7a7d2f16 | 424 | * passed as a lowerdir= parameter, so start with the hierarchy on the root. |
93f59701 LB |
425 | * The overlays vector will have the same number of elements and will correspond to the |
426 | * hierarchies vector, so they can be iterated upon together. */ | |
427 | STRV_FOREACH(hierarchy, hierarchies) { | |
428 | _cleanup_free_ char *prefixed_hierarchy = NULL; | |
429 | ||
430 | prefixed_hierarchy = path_join(root, *hierarchy); | |
431 | if (!prefixed_hierarchy) | |
432 | return -ENOMEM; | |
433 | ||
434 | r = strv_consume(&overlays, TAKE_PTR(prefixed_hierarchy)); | |
435 | if (r < 0) | |
436 | return r; | |
437 | } | |
438 | ||
439 | /* First, prepare a mount for each image, but these won't be visible to the unit, instead | |
440 | * they will be mounted in our propagate directory, and used as a source for the overlay. */ | |
441 | for (size_t i = 0; i < n; i++) { | |
442 | _cleanup_free_ char *mount_point = NULL; | |
443 | const MountImage *m = mount_images + i; | |
444 | ||
445 | r = asprintf(&mount_point, "%s/%zu", extension_dir, i); | |
446 | if (r < 0) | |
447 | return -ENOMEM; | |
448 | ||
449 | for (size_t j = 0; hierarchies && hierarchies[j]; ++j) { | |
450 | _cleanup_free_ char *prefixed_hierarchy = NULL, *escaped = NULL, *lowerdir = NULL; | |
451 | ||
452 | prefixed_hierarchy = path_join(mount_point, hierarchies[j]); | |
453 | if (!prefixed_hierarchy) | |
454 | return -ENOMEM; | |
455 | ||
456 | escaped = shell_escape(prefixed_hierarchy, ",:"); | |
457 | if (!escaped) | |
458 | return -ENOMEM; | |
459 | ||
460 | /* Note that lowerdir= parameters are in 'reverse' order, so the | |
461 | * top-most directory in the overlay comes first in the list. */ | |
462 | lowerdir = strjoin(escaped, ":", overlays[j]); | |
463 | if (!lowerdir) | |
464 | return -ENOMEM; | |
465 | ||
466 | free_and_replace(overlays[j], lowerdir); | |
467 | } | |
468 | ||
469 | *((*p)++) = (MountEntry) { | |
470 | .path_malloc = TAKE_PTR(mount_point), | |
471 | .image_options = m->mount_options, | |
472 | .ignore = m->ignore_enoent, | |
473 | .source_const = m->source, | |
474 | .mode = EXTENSION_IMAGES, | |
475 | .has_prefix = true, | |
476 | }; | |
477 | } | |
478 | ||
479 | /* Then, for each hierarchy, prepare an overlay with the list of lowerdir= strings | |
480 | * set up earlier. */ | |
481 | for (size_t i = 0; hierarchies && hierarchies[i]; ++i) { | |
482 | _cleanup_free_ char *prefixed_hierarchy = NULL; | |
483 | ||
484 | prefixed_hierarchy = path_join(root, hierarchies[i]); | |
485 | if (!prefixed_hierarchy) | |
486 | return -ENOMEM; | |
487 | ||
488 | *((*p)++) = (MountEntry) { | |
489 | .path_malloc = TAKE_PTR(prefixed_hierarchy), | |
490 | .options_malloc = TAKE_PTR(overlays[i]), | |
491 | .mode = OVERLAY_MOUNT, | |
492 | .has_prefix = true, | |
493 | .ignore = true, /* If the source image doesn't set the ignore bit it will fail earlier. */ | |
494 | }; | |
495 | } | |
496 | ||
497 | return 0; | |
498 | } | |
499 | ||
da6053d0 | 500 | static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs, size_t n) { |
2abd4e38 YW |
501 | assert(p); |
502 | ||
b67ec8e5 | 503 | for (size_t i = 0; i < n; i++) { |
2abd4e38 YW |
504 | const TemporaryFileSystem *t = tmpfs + i; |
505 | _cleanup_free_ char *o = NULL, *str = NULL; | |
ad8e66dc | 506 | unsigned long flags; |
2abd4e38 | 507 | bool ro = false; |
b67ec8e5 | 508 | int r; |
2abd4e38 | 509 | |
baaa35ad ZJS |
510 | if (!path_is_absolute(t->path)) |
511 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
512 | "Path is not absolute: %s", | |
513 | t->path); | |
2abd4e38 | 514 | |
b67ec8e5 | 515 | str = strjoin("mode=0755" NESTED_TMPFS_LIMITS ",", t->options); |
ad8e66dc AJ |
516 | if (!str) |
517 | return -ENOMEM; | |
2abd4e38 | 518 | |
ad8e66dc AJ |
519 | r = mount_option_mangle(str, MS_NODEV|MS_STRICTATIME, &flags, &o); |
520 | if (r < 0) | |
521 | return log_debug_errno(r, "Failed to parse mount option '%s': %m", str); | |
2abd4e38 | 522 | |
ad8e66dc AJ |
523 | ro = flags & MS_RDONLY; |
524 | if (ro) | |
525 | flags ^= MS_RDONLY; | |
2abd4e38 YW |
526 | |
527 | *((*p)++) = (MountEntry) { | |
528 | .path_const = t->path, | |
529 | .mode = TMPFS, | |
530 | .read_only = ro, | |
ad8e66dc | 531 | .options_malloc = TAKE_PTR(o), |
2abd4e38 YW |
532 | .flags = flags, |
533 | }; | |
2abd4e38 YW |
534 | } |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
da6053d0 | 539 | static int append_static_mounts(MountEntry **p, const MountEntry *mounts, size_t n, bool ignore_protect) { |
11a30cec | 540 | assert(p); |
f471b2af | 541 | assert(mounts); |
11a30cec | 542 | |
5327c910 | 543 | /* Adds a list of static pre-defined entries */ |
f471b2af | 544 | |
fe96c0f8 | 545 | for (size_t i = 0; i < n; i++) |
34de407a LP |
546 | *((*p)++) = (MountEntry) { |
547 | .path_const = mount_entry_path(mounts+i), | |
5327c910 LP |
548 | .mode = mounts[i].mode, |
549 | .ignore = mounts[i].ignore || ignore_protect, | |
550 | }; | |
f471b2af DH |
551 | |
552 | return 0; | |
553 | } | |
554 | ||
34de407a | 555 | static int append_protect_home(MountEntry **p, ProtectHome protect_home, bool ignore_protect) { |
c575770b DH |
556 | assert(p); |
557 | ||
5327c910 | 558 | switch (protect_home) { |
b6c432ca | 559 | |
5327c910 | 560 | case PROTECT_HOME_NO: |
b6c432ca DH |
561 | return 0; |
562 | ||
b6c432ca | 563 | case PROTECT_HOME_READ_ONLY: |
5327c910 LP |
564 | return append_static_mounts(p, protect_home_read_only_table, ELEMENTSOF(protect_home_read_only_table), ignore_protect); |
565 | ||
e4da7d8c YW |
566 | case PROTECT_HOME_TMPFS: |
567 | return append_static_mounts(p, protect_home_tmpfs_table, ELEMENTSOF(protect_home_tmpfs_table), ignore_protect); | |
568 | ||
b6c432ca | 569 | case PROTECT_HOME_YES: |
5327c910 LP |
570 | return append_static_mounts(p, protect_home_yes_table, ELEMENTSOF(protect_home_yes_table), ignore_protect); |
571 | ||
b6c432ca | 572 | default: |
04499a70 | 573 | assert_not_reached(); |
b6c432ca | 574 | } |
b6c432ca DH |
575 | } |
576 | ||
34de407a | 577 | static int append_protect_system(MountEntry **p, ProtectSystem protect_system, bool ignore_protect) { |
f471b2af DH |
578 | assert(p); |
579 | ||
5327c910 LP |
580 | switch (protect_system) { |
581 | ||
582 | case PROTECT_SYSTEM_NO: | |
f471b2af DH |
583 | return 0; |
584 | ||
f471b2af | 585 | case PROTECT_SYSTEM_STRICT: |
5327c910 LP |
586 | return append_static_mounts(p, protect_system_strict_table, ELEMENTSOF(protect_system_strict_table), ignore_protect); |
587 | ||
f471b2af | 588 | case PROTECT_SYSTEM_YES: |
5327c910 LP |
589 | return append_static_mounts(p, protect_system_yes_table, ELEMENTSOF(protect_system_yes_table), ignore_protect); |
590 | ||
f471b2af | 591 | case PROTECT_SYSTEM_FULL: |
5327c910 LP |
592 | return append_static_mounts(p, protect_system_full_table, ELEMENTSOF(protect_system_full_table), ignore_protect); |
593 | ||
f471b2af | 594 | default: |
04499a70 | 595 | assert_not_reached(); |
f471b2af | 596 | } |
11a30cec DH |
597 | } |
598 | ||
93bab288 | 599 | static int mount_path_compare(const MountEntry *a, const MountEntry *b) { |
a0827e2b | 600 | int d; |
15ae422b | 601 | |
93f59701 LB |
602 | /* EXTENSION_IMAGES will be used by other mounts as a base, so sort them first |
603 | * regardless of the prefix - they are set up in the propagate directory anyway */ | |
604 | d = -CMP(a->mode == EXTENSION_IMAGES, b->mode == EXTENSION_IMAGES); | |
605 | if (d != 0) | |
606 | return d; | |
607 | ||
6ee1a919 | 608 | /* If the paths are not equal, then order prefixes first */ |
93bab288 | 609 | d = path_compare(mount_entry_path(a), mount_entry_path(b)); |
6ee1a919 LP |
610 | if (d != 0) |
611 | return d; | |
15ae422b | 612 | |
6ee1a919 | 613 | /* If the paths are equal, check the mode */ |
93bab288 | 614 | return CMP((int) a->mode, (int) b->mode); |
15ae422b LP |
615 | } |
616 | ||
da6053d0 | 617 | static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) { |
4a756839 | 618 | /* Prefixes all paths in the bind mount table with the root directory if the entry needs that. */ |
5327c910 | 619 | |
fe96c0f8 SS |
620 | assert(m || n == 0); |
621 | ||
622 | for (size_t i = 0; i < n; i++) { | |
5327c910 LP |
623 | char *s; |
624 | ||
625 | if (m[i].has_prefix) | |
626 | continue; | |
627 | ||
c6134d3e | 628 | s = path_join(root_directory, mount_entry_path(m+i)); |
5327c910 LP |
629 | if (!s) |
630 | return -ENOMEM; | |
631 | ||
809ceb82 | 632 | mount_entry_consume_prefix(&m[i], s); |
5327c910 LP |
633 | } |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
da6053d0 | 638 | static void drop_duplicates(MountEntry *m, size_t *n) { |
34de407a | 639 | MountEntry *f, *t, *previous; |
15ae422b | 640 | |
c17ec25e | 641 | assert(m); |
15ae422b | 642 | assert(n); |
15ae422b | 643 | |
fe3c2583 LP |
644 | /* Drops duplicate entries. Expects that the array is properly ordered already. */ |
645 | ||
1d54cd5d | 646 | for (f = m, t = m, previous = NULL; f < m + *n; f++) { |
15ae422b | 647 | |
fe3c2583 | 648 | /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare() |
088696fe LP |
649 | * above. Note that we only drop duplicates that haven't been mounted yet. */ |
650 | if (previous && | |
651 | path_equal(mount_entry_path(f), mount_entry_path(previous)) && | |
652 | !f->applied && !previous->applied) { | |
5beb8688 | 653 | log_debug("%s (%s) is duplicate.", mount_entry_path(f), mount_mode_to_string(f->mode)); |
ddc155b2 TM |
654 | /* Propagate the flags to the remaining entry */ |
655 | previous->read_only = previous->read_only || mount_entry_read_only(f); | |
656 | previous->noexec = previous->noexec || mount_entry_noexec(f); | |
657 | previous->exec = previous->exec || mount_entry_exec(f); | |
1eb7e08e | 658 | mount_entry_done(f); |
15ae422b | 659 | continue; |
fe3c2583 | 660 | } |
15ae422b | 661 | |
e2d7c1a0 | 662 | *t = *f; |
15ae422b | 663 | previous = t; |
fe3c2583 LP |
664 | t++; |
665 | } | |
666 | ||
667 | *n = t - m; | |
668 | } | |
669 | ||
da6053d0 | 670 | static void drop_inaccessible(MountEntry *m, size_t *n) { |
34de407a | 671 | MountEntry *f, *t; |
fe3c2583 LP |
672 | const char *clear = NULL; |
673 | ||
674 | assert(m); | |
675 | assert(n); | |
676 | ||
677 | /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly | |
678 | * ordered already. */ | |
679 | ||
1d54cd5d | 680 | for (f = m, t = m; f < m + *n; f++) { |
fe3c2583 LP |
681 | |
682 | /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop | |
683 | * it, as inaccessible paths really should drop the entire subtree. */ | |
34de407a LP |
684 | if (clear && path_startswith(mount_entry_path(f), clear)) { |
685 | log_debug("%s is masked by %s.", mount_entry_path(f), clear); | |
1eb7e08e | 686 | mount_entry_done(f); |
fe3c2583 LP |
687 | continue; |
688 | } | |
15ae422b | 689 | |
34de407a | 690 | clear = f->mode == INACCESSIBLE ? mount_entry_path(f) : NULL; |
fe3c2583 LP |
691 | |
692 | *t = *f; | |
15ae422b LP |
693 | t++; |
694 | } | |
695 | ||
c17ec25e | 696 | *n = t - m; |
15ae422b LP |
697 | } |
698 | ||
da6053d0 | 699 | static void drop_nop(MountEntry *m, size_t *n) { |
34de407a | 700 | MountEntry *f, *t; |
7648a565 LP |
701 | |
702 | assert(m); | |
703 | assert(n); | |
704 | ||
705 | /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the | |
706 | * list is ordered by prefixes. */ | |
707 | ||
1d54cd5d | 708 | for (f = m, t = m; f < m + *n; f++) { |
7648a565 | 709 | |
1e05071d YW |
710 | /* Only suppress such subtrees for READONLY, READWRITE and READWRITE_IMPLICIT entries */ |
711 | if (IN_SET(f->mode, READONLY, READWRITE, READWRITE_IMPLICIT)) { | |
34de407a | 712 | MountEntry *p; |
7648a565 LP |
713 | bool found = false; |
714 | ||
715 | /* Now let's find the first parent of the entry we are looking at. */ | |
716 | for (p = t-1; p >= m; p--) { | |
34de407a | 717 | if (path_startswith(mount_entry_path(f), mount_entry_path(p))) { |
7648a565 LP |
718 | found = true; |
719 | break; | |
720 | } | |
721 | } | |
722 | ||
723 | /* We found it, let's see if it's the same mode, if so, we can drop this entry */ | |
724 | if (found && p->mode == f->mode) { | |
5beb8688 YW |
725 | log_debug("%s (%s) is made redundant by %s (%s)", |
726 | mount_entry_path(f), mount_mode_to_string(f->mode), | |
727 | mount_entry_path(p), mount_mode_to_string(p->mode)); | |
1eb7e08e | 728 | mount_entry_done(f); |
7648a565 LP |
729 | continue; |
730 | } | |
731 | } | |
732 | ||
733 | *t = *f; | |
734 | t++; | |
735 | } | |
736 | ||
737 | *n = t - m; | |
738 | } | |
739 | ||
da6053d0 | 740 | static void drop_outside_root(const char *root_directory, MountEntry *m, size_t *n) { |
34de407a | 741 | MountEntry *f, *t; |
cd2902c9 LP |
742 | |
743 | assert(m); | |
744 | assert(n); | |
745 | ||
1d54cd5d | 746 | /* Nothing to do */ |
cd2902c9 LP |
747 | if (!root_directory) |
748 | return; | |
749 | ||
750 | /* Drops all mounts that are outside of the root directory. */ | |
751 | ||
1d54cd5d | 752 | for (f = m, t = m; f < m + *n; f++) { |
cd2902c9 | 753 | |
93f59701 LB |
754 | /* ExtensionImages bases are opened in /run/systemd/unit-extensions on the host */ |
755 | if (f->mode != EXTENSION_IMAGES && !path_startswith(mount_entry_path(f), root_directory)) { | |
34de407a | 756 | log_debug("%s is outside of root directory.", mount_entry_path(f)); |
1eb7e08e | 757 | mount_entry_done(f); |
cd2902c9 LP |
758 | continue; |
759 | } | |
760 | ||
761 | *t = *f; | |
762 | t++; | |
763 | } | |
764 | ||
765 | *n = t - m; | |
766 | } | |
767 | ||
b2a60844 LP |
768 | static int clone_device_node( |
769 | const char *d, | |
770 | const char *temporary_mount, | |
771 | bool *make_devnode) { | |
772 | ||
773 | _cleanup_free_ char *sl = NULL; | |
774 | const char *dn, *bn, *t; | |
b5e99f23 ДГ |
775 | struct stat st; |
776 | int r; | |
777 | ||
414b304b | 778 | if (stat(d, &st) < 0) { |
b2a60844 LP |
779 | if (errno == ENOENT) { |
780 | log_debug_errno(errno, "Device node '%s' to clone does not exist, ignoring.", d); | |
af984e13 | 781 | return -ENXIO; |
b2a60844 LP |
782 | } |
783 | ||
784 | return log_debug_errno(errno, "Failed to stat() device node '%s' to clone, ignoring: %m", d); | |
b5e99f23 ДГ |
785 | } |
786 | ||
787 | if (!S_ISBLK(st.st_mode) && | |
baaa35ad ZJS |
788 | !S_ISCHR(st.st_mode)) |
789 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
790 | "Device node '%s' to clone is not a device node, ignoring.", | |
791 | d); | |
b5e99f23 | 792 | |
6f7f3a33 | 793 | dn = strjoina(temporary_mount, d); |
b5e99f23 | 794 | |
b2a60844 | 795 | /* First, try to create device node properly */ |
16498617 CB |
796 | if (*make_devnode) { |
797 | mac_selinux_create_file_prepare(d, st.st_mode); | |
798 | r = mknod(dn, st.st_mode, st.st_rdev); | |
799 | mac_selinux_create_file_clear(); | |
b2a60844 LP |
800 | if (r >= 0) |
801 | goto add_symlink; | |
16498617 CB |
802 | if (errno != EPERM) |
803 | return log_debug_errno(errno, "mknod failed for %s: %m", d); | |
804 | ||
b2a60844 | 805 | /* This didn't work, let's not try this again for the next iterations. */ |
16498617 CB |
806 | *make_devnode = false; |
807 | } | |
808 | ||
d73020f2 | 809 | /* We're about to fall back to bind-mounting the device node. So create a dummy bind-mount target. |
1acf344d | 810 | * Do not prepare device-node SELinux label (see issue 13762) */ |
16498617 | 811 | r = mknod(dn, S_IFREG, 0); |
16498617 | 812 | if (r < 0 && errno != EEXIST) |
b2a60844 | 813 | return log_debug_errno(errno, "mknod() fallback failed for '%s': %m", d); |
16498617 | 814 | |
21935150 LP |
815 | /* Fallback to bind-mounting: The assumption here is that all used device nodes carry standard |
816 | * properties. Specifically, the devices nodes we bind-mount should either be owned by root:root or | |
817 | * root:tty (e.g. /dev/tty, /dev/ptmx) and should not carry ACLs. */ | |
818 | r = mount_nofollow_verbose(LOG_DEBUG, d, dn, NULL, MS_BIND, NULL); | |
819 | if (r < 0) | |
820 | return r; | |
b2a60844 LP |
821 | |
822 | add_symlink: | |
823 | bn = path_startswith(d, "/dev/"); | |
824 | if (!bn) | |
825 | return 0; | |
826 | ||
827 | /* Create symlinks like /dev/char/1:9 → ../urandom */ | |
cbc056c8 ZJS |
828 | if (asprintf(&sl, "%s/dev/%s/%u:%u", |
829 | temporary_mount, | |
830 | S_ISCHR(st.st_mode) ? "char" : "block", | |
831 | major(st.st_rdev), minor(st.st_rdev)) < 0) | |
b2a60844 LP |
832 | return log_oom(); |
833 | ||
834 | (void) mkdir_parents(sl, 0755); | |
835 | ||
836 | t = strjoina("../", bn); | |
b2a60844 | 837 | if (symlink(t, sl) < 0) |
2e4a4fae | 838 | log_debug_errno(errno, "Failed to symlink '%s' to '%s', ignoring: %m", t, sl); |
b5e99f23 | 839 | |
af984e13 | 840 | return 0; |
b5e99f23 ДГ |
841 | } |
842 | ||
5d997827 | 843 | static int mount_private_dev(MountEntry *m) { |
7f112f50 LP |
844 | static const char devnodes[] = |
845 | "/dev/null\0" | |
846 | "/dev/zero\0" | |
847 | "/dev/full\0" | |
848 | "/dev/random\0" | |
849 | "/dev/urandom\0" | |
850 | "/dev/tty\0"; | |
851 | ||
2b85f4e1 | 852 | char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; |
63cc4c31 | 853 | const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL; |
16498617 | 854 | bool can_mknod = true; |
7f112f50 LP |
855 | int r; |
856 | ||
857 | assert(m); | |
858 | ||
2b85f4e1 | 859 | if (!mkdtemp(temporary_mount)) |
2e4a4fae | 860 | return log_debug_errno(errno, "Failed to create temporary directory '%s': %m", temporary_mount); |
2b85f4e1 | 861 | |
63c372cb | 862 | dev = strjoina(temporary_mount, "/dev"); |
dc751688 | 863 | (void) mkdir(dev, 0755); |
21935150 LP |
864 | r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=755" TMPFS_LIMITS_DEV); |
865 | if (r < 0) | |
2b85f4e1 | 866 | goto fail; |
21935150 | 867 | |
c3151977 TM |
868 | r = label_fix_container(dev, "/dev", 0); |
869 | if (r < 0) { | |
870 | log_debug_errno(errno, "Failed to fix label of '%s' as /dev: %m", dev); | |
871 | goto fail; | |
872 | } | |
2b85f4e1 | 873 | |
63c372cb | 874 | devpts = strjoina(temporary_mount, "/dev/pts"); |
dc751688 | 875 | (void) mkdir(devpts, 0755); |
21935150 LP |
876 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev/pts", devpts, NULL, MS_BIND, NULL); |
877 | if (r < 0) | |
2b85f4e1 | 878 | goto fail; |
2b85f4e1 | 879 | |
2e4a4fae YW |
880 | /* /dev/ptmx can either be a device node or a symlink to /dev/pts/ptmx. |
881 | * When /dev/ptmx a device node, /dev/pts/ptmx has 000 permissions making it inaccessible. | |
882 | * Thus, in that case make a clone. | |
883 | * In nspawn and other containers it will be a symlink, in that case make it a symlink. */ | |
36ce7110 | 884 | r = is_symlink("/dev/ptmx"); |
2e4a4fae YW |
885 | if (r < 0) { |
886 | log_debug_errno(r, "Failed to detect whether /dev/ptmx is a symlink or not: %m"); | |
3164e3cb | 887 | goto fail; |
2e4a4fae | 888 | } else if (r > 0) { |
414b304b ДГ |
889 | devptmx = strjoina(temporary_mount, "/dev/ptmx"); |
890 | if (symlink("pts/ptmx", devptmx) < 0) { | |
2e4a4fae | 891 | r = log_debug_errno(errno, "Failed to create a symlink '%s' to pts/ptmx: %m", devptmx); |
414b304b ДГ |
892 | goto fail; |
893 | } | |
894 | } else { | |
16498617 | 895 | r = clone_device_node("/dev/ptmx", temporary_mount, &can_mknod); |
152c475f LP |
896 | if (r < 0) |
897 | goto fail; | |
414b304b | 898 | } |
e06b6479 | 899 | |
63c372cb | 900 | devshm = strjoina(temporary_mount, "/dev/shm"); |
8d953682 | 901 | (void) mkdir(devshm, 0755); |
21935150 LP |
902 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev/shm", devshm, NULL, MS_BIND, NULL); |
903 | if (r < 0) | |
2b85f4e1 | 904 | goto fail; |
2b85f4e1 | 905 | |
63c372cb | 906 | devmqueue = strjoina(temporary_mount, "/dev/mqueue"); |
dc751688 | 907 | (void) mkdir(devmqueue, 0755); |
21935150 | 908 | (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); |
2b85f4e1 | 909 | |
63c372cb | 910 | devhugepages = strjoina(temporary_mount, "/dev/hugepages"); |
dc751688 | 911 | (void) mkdir(devhugepages, 0755); |
21935150 | 912 | (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); |
2b85f4e1 | 913 | |
63c372cb | 914 | devlog = strjoina(temporary_mount, "/dev/log"); |
2e4a4fae YW |
915 | if (symlink("/run/systemd/journal/dev-log", devlog) < 0) |
916 | log_debug_errno(errno, "Failed to create a symlink '%s' to /run/systemd/journal/dev-log, ignoring: %m", devlog); | |
82d25240 | 917 | |
7f112f50 | 918 | NULSTR_FOREACH(d, devnodes) { |
16498617 | 919 | r = clone_device_node(d, temporary_mount, &can_mknod); |
37b22b3b | 920 | /* ENXIO means the *source* is not a device file, skip creation in that case */ |
af984e13 | 921 | if (r < 0 && r != -ENXIO) |
2b85f4e1 | 922 | goto fail; |
7f112f50 LP |
923 | } |
924 | ||
2e4a4fae YW |
925 | r = dev_setup(temporary_mount, UID_INVALID, GID_INVALID); |
926 | if (r < 0) | |
105a1a36 | 927 | log_debug_errno(r, "Failed to set up basic device tree at '%s', ignoring: %m", temporary_mount); |
7f112f50 | 928 | |
d73020f2 LP |
929 | /* Create the /dev directory if missing. It is more likely to be missing when the service is started |
930 | * with RootDirectory. This is consistent with mount units creating the mount points when missing. */ | |
34de407a | 931 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
ee818b89 | 932 | |
9e5f8252 | 933 | /* Unmount everything in old /dev */ |
2e4a4fae YW |
934 | r = umount_recursive(mount_entry_path(m), 0); |
935 | if (r < 0) | |
936 | log_debug_errno(r, "Failed to unmount directories below '%s', ignoring: %m", mount_entry_path(m)); | |
937 | ||
21935150 LP |
938 | r = mount_nofollow_verbose(LOG_DEBUG, dev, mount_entry_path(m), NULL, MS_MOVE, NULL); |
939 | if (r < 0) | |
2b85f4e1 | 940 | goto fail; |
7f112f50 | 941 | |
1019a48f LP |
942 | (void) rmdir(dev); |
943 | (void) rmdir(temporary_mount); | |
7f112f50 | 944 | |
2b85f4e1 | 945 | return 0; |
7f112f50 | 946 | |
2b85f4e1 LP |
947 | fail: |
948 | if (devpts) | |
21935150 | 949 | (void) umount_verbose(LOG_DEBUG, devpts, UMOUNT_NOFOLLOW); |
7f112f50 | 950 | |
2b85f4e1 | 951 | if (devshm) |
21935150 | 952 | (void) umount_verbose(LOG_DEBUG, devshm, UMOUNT_NOFOLLOW); |
7f112f50 | 953 | |
2b85f4e1 | 954 | if (devhugepages) |
21935150 | 955 | (void) umount_verbose(LOG_DEBUG, devhugepages, UMOUNT_NOFOLLOW); |
7f112f50 | 956 | |
2b85f4e1 | 957 | if (devmqueue) |
21935150 | 958 | (void) umount_verbose(LOG_DEBUG, devmqueue, UMOUNT_NOFOLLOW); |
7f112f50 | 959 | |
21935150 | 960 | (void) umount_verbose(LOG_DEBUG, dev, UMOUNT_NOFOLLOW); |
1019a48f LP |
961 | (void) rmdir(dev); |
962 | (void) rmdir(temporary_mount); | |
7f112f50 | 963 | |
2b85f4e1 | 964 | return r; |
7f112f50 LP |
965 | } |
966 | ||
2a2969fd | 967 | static int mount_bind_dev(const MountEntry *m) { |
5d997827 LP |
968 | int r; |
969 | ||
970 | assert(m); | |
971 | ||
d73020f2 LP |
972 | /* Implements the little brother of mount_private_dev(): simply bind mounts the host's /dev into the |
973 | * service's /dev. This is only used when RootDirectory= is set. */ | |
5d997827 | 974 | |
645767d6 LP |
975 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
976 | ||
5d997827 LP |
977 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
978 | if (r < 0) | |
979 | return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m"); | |
980 | if (r > 0) /* make this a NOP if /dev is already a mount point */ | |
981 | return 0; | |
982 | ||
21935150 LP |
983 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL); |
984 | if (r < 0) | |
985 | return r; | |
5d997827 LP |
986 | |
987 | return 1; | |
988 | } | |
989 | ||
2a2969fd | 990 | static int mount_sysfs(const MountEntry *m) { |
5d997827 LP |
991 | int r; |
992 | ||
993 | assert(m); | |
994 | ||
645767d6 LP |
995 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
996 | ||
5d997827 LP |
997 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
998 | if (r < 0) | |
999 | return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m"); | |
1000 | if (r > 0) /* make this a NOP if /sys is already a mount point */ | |
1001 | return 0; | |
1002 | ||
1003 | /* Bind mount the host's version so that we get all child mounts of it, too. */ | |
21935150 LP |
1004 | r = mount_nofollow_verbose(LOG_DEBUG, "/sys", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL); |
1005 | if (r < 0) | |
1006 | return r; | |
5d997827 LP |
1007 | |
1008 | return 1; | |
1009 | } | |
1010 | ||
4e399953 | 1011 | static int mount_procfs(const MountEntry *m, const NamespaceInfo *ns_info) { |
61f8a7bd | 1012 | _cleanup_free_ char *opts = NULL; |
4e399953 | 1013 | const char *entry_path; |
61f8a7bd | 1014 | int r, n; |
5d997827 LP |
1015 | |
1016 | assert(m); | |
4e399953 | 1017 | assert(ns_info); |
5d997827 | 1018 | |
4e399953 LP |
1019 | if (ns_info->protect_proc != PROTECT_PROC_DEFAULT || |
1020 | ns_info->proc_subset != PROC_SUBSET_ALL) { | |
4e399953 LP |
1021 | |
1022 | /* Starting with kernel 5.8 procfs' hidepid= logic is truly per-instance (previously it | |
1023 | * pretended to be per-instance but actually was per-namespace), hence let's make use of it | |
1024 | * if requested. To make sure this logic succeeds only on kernels where hidepid= is | |
1025 | * per-instance, we'll exclusively use the textual value for hidepid=, since support was | |
1026 | * added in the same commit: if it's supported it is thus also per-instance. */ | |
1027 | ||
1028 | opts = strjoin("hidepid=", | |
1029 | ns_info->protect_proc == PROTECT_PROC_DEFAULT ? "off" : | |
1030 | protect_proc_to_string(ns_info->protect_proc), | |
1031 | ns_info->proc_subset == PROC_SUBSET_PID ? ",subset=pid" : ""); | |
1032 | if (!opts) | |
1033 | return -ENOMEM; | |
4e399953 LP |
1034 | } |
1035 | ||
61f8a7bd YW |
1036 | entry_path = mount_entry_path(m); |
1037 | (void) mkdir_p_label(entry_path, 0755); | |
1038 | ||
1039 | /* Mount a new instance, so that we get the one that matches our user namespace, if we are running in | |
1040 | * one. i.e we don't reuse existing mounts here under any condition, we want a new instance owned by | |
1041 | * our user namespace and with our hidepid= settings applied. Hence, let's get rid of everything | |
1042 | * mounted on /proc/ first. */ | |
1043 | ||
1044 | n = umount_recursive(entry_path, 0); | |
1045 | ||
1046 | r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, opts); | |
1047 | if (r == -EINVAL && opts) | |
1048 | /* If this failed with EINVAL then this likely means the textual hidepid= stuff is | |
1049 | * not supported by the kernel, and thus the per-instance hidepid= neither, which | |
1050 | * means we really don't want to use it, since it would affect our host's /proc | |
1051 | * mount. Hence let's gracefully fallback to a classic, unrestricted version. */ | |
1052 | r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL); | |
1053 | if (r == -EPERM) { | |
24ee0f9d | 1054 | /* When we do not have enough privileges to mount /proc, fallback to use existing /proc. */ |
61f8a7bd YW |
1055 | |
1056 | if (n > 0) | |
1057 | /* /proc or some of sub-mounts are umounted in the above. Refuse incomplete tree. | |
1058 | * Propagate the original error code returned by mount() in the above. */ | |
1059 | return -EPERM; | |
1060 | ||
1061 | r = path_is_mount_point(entry_path, NULL, 0); | |
1062 | if (r < 0) | |
1063 | return log_debug_errno(r, "Unable to determine whether /proc is already mounted: %m"); | |
1064 | if (r == 0) | |
1065 | /* /proc is not mounted. Propagate the original error code. */ | |
1066 | return -EPERM; | |
ad74f28a YW |
1067 | } else if (r < 0) |
1068 | return r; | |
5d997827 LP |
1069 | |
1070 | return 1; | |
1071 | } | |
1072 | ||
2abd4e38 | 1073 | static int mount_tmpfs(const MountEntry *m) { |
df6b900a | 1074 | const char *entry_path, *inner_path; |
abad72be | 1075 | int r; |
abad72be | 1076 | |
6c47cd7d LP |
1077 | assert(m); |
1078 | ||
df6b900a | 1079 | entry_path = mount_entry_path(m); |
809ceb82 | 1080 | inner_path = mount_entry_unprefixed_path(m); |
df6b900a | 1081 | |
d73020f2 LP |
1082 | /* First, get rid of everything that is below if there is anything. Then, overmount with our new |
1083 | * tmpfs */ | |
6c47cd7d | 1084 | |
abad72be CG |
1085 | (void) mkdir_p_label(entry_path, 0755); |
1086 | (void) umount_recursive(entry_path, 0); | |
6c47cd7d | 1087 | |
21935150 LP |
1088 | r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", entry_path, "tmpfs", m->flags, mount_entry_options(m)); |
1089 | if (r < 0) | |
1090 | return r; | |
abad72be | 1091 | |
df6b900a | 1092 | r = label_fix_container(entry_path, inner_path, 0); |
abad72be | 1093 | if (r < 0) |
df6b900a | 1094 | return log_debug_errno(r, "Failed to fix label of '%s' as '%s': %m", entry_path, inner_path); |
6c47cd7d LP |
1095 | |
1096 | return 1; | |
1097 | } | |
1098 | ||
94293d65 LB |
1099 | static int mount_run(const MountEntry *m) { |
1100 | int r; | |
1101 | ||
1102 | assert(m); | |
1103 | ||
1104 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); | |
1105 | if (r < 0 && r != -ENOENT) | |
1106 | return log_debug_errno(r, "Unable to determine whether /run is already mounted: %m"); | |
1107 | if (r > 0) /* make this a NOP if /run is already a mount point */ | |
1108 | return 0; | |
1109 | ||
1110 | return mount_tmpfs(m); | |
1111 | } | |
1112 | ||
80271a44 XR |
1113 | static int mount_mqueuefs(const MountEntry *m) { |
1114 | int r; | |
1115 | const char *entry_path; | |
1116 | ||
1117 | assert(m); | |
1118 | ||
1119 | entry_path = mount_entry_path(m); | |
1120 | ||
1121 | (void) mkdir_p_label(entry_path, 0755); | |
1122 | (void) umount_recursive(entry_path, 0); | |
1123 | ||
1124 | r = mount_nofollow_verbose(LOG_DEBUG, "mqueue", entry_path, "mqueue", m->flags, mount_entry_options(m)); | |
1125 | if (r < 0) | |
1126 | return r; | |
1127 | ||
1128 | return 0; | |
1129 | } | |
1130 | ||
93f59701 LB |
1131 | static int mount_image(const MountEntry *m, const char *root_directory) { |
1132 | ||
1133 | _cleanup_free_ char *host_os_release_id = NULL, *host_os_release_version_id = NULL, | |
1134 | *host_os_release_sysext_level = NULL; | |
b3d13314 LB |
1135 | int r; |
1136 | ||
89e62e0b LP |
1137 | assert(m); |
1138 | ||
93f59701 LB |
1139 | if (m->mode == EXTENSION_IMAGES) { |
1140 | r = parse_os_release( | |
1141 | empty_to_root(root_directory), | |
1142 | "ID", &host_os_release_id, | |
1143 | "VERSION_ID", &host_os_release_version_id, | |
1144 | "SYSEXT_LEVEL", &host_os_release_sysext_level, | |
1145 | NULL); | |
1146 | if (r < 0) | |
1147 | return log_debug_errno(r, "Failed to acquire 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory)); | |
1148 | } | |
1149 | ||
1150 | r = verity_dissect_and_mount( | |
1151 | mount_entry_source(m), mount_entry_path(m), m->image_options, | |
60c5f700 | 1152 | host_os_release_id, host_os_release_version_id, host_os_release_sysext_level, NULL); |
b850a9b2 LB |
1153 | if (r == -ENOENT && m->ignore) |
1154 | return 0; | |
db4c8a25 LB |
1155 | if (r == -ESTALE && host_os_release_id) |
1156 | return log_error_errno(r, | |
1157 | "Failed to mount image %s, extension-release metadata does not match the lower layer's: ID=%s%s%s%s%s", | |
1158 | mount_entry_source(m), | |
1159 | host_os_release_id, | |
1160 | host_os_release_version_id ? " VERSION_ID=" : "", | |
1161 | strempty(host_os_release_version_id), | |
1162 | host_os_release_sysext_level ? " SYSEXT_LEVEL=" : "", | |
1163 | strempty(host_os_release_sysext_level)); | |
b3d13314 | 1164 | if (r < 0) |
4beda316 | 1165 | return log_debug_errno(r, "Failed to mount image %s on %s: %m", mount_entry_source(m), mount_entry_path(m)); |
b3d13314 LB |
1166 | |
1167 | return 1; | |
1168 | } | |
1169 | ||
93f59701 LB |
1170 | static int mount_overlay(const MountEntry *m) { |
1171 | const char *options; | |
1172 | int r; | |
1173 | ||
1174 | assert(m); | |
1175 | ||
1176 | options = strjoina("lowerdir=", mount_entry_options(m)); | |
1177 | ||
1178 | (void) mkdir_p_label(mount_entry_path(m), 0755); | |
1179 | ||
1180 | r = mount_nofollow_verbose(LOG_DEBUG, "overlay", mount_entry_path(m), "overlay", MS_RDONLY, options); | |
1181 | if (r == -ENOENT && m->ignore) | |
1182 | return 0; | |
1183 | if (r < 0) | |
1184 | return r; | |
1185 | ||
1186 | return 1; | |
1187 | } | |
1188 | ||
088696fe | 1189 | static int follow_symlink( |
d2d6c096 | 1190 | const char *root_directory, |
088696fe | 1191 | MountEntry *m) { |
d2d6c096 | 1192 | |
088696fe | 1193 | _cleanup_free_ char *target = NULL; |
8fceda93 LP |
1194 | int r; |
1195 | ||
088696fe LP |
1196 | /* Let's chase symlinks, but only one step at a time. That's because depending where the symlink points we |
1197 | * might need to change the order in which we mount stuff. Hence: let's normalize piecemeal, and do one step at | |
1198 | * a time by specifying CHASE_STEP. This function returns 0 if we resolved one step, and > 0 if we reached the | |
1199 | * end and already have a fully normalized name. */ | |
8fceda93 | 1200 | |
a5648b80 | 1201 | r = chase_symlinks(mount_entry_path(m), root_directory, CHASE_STEP|CHASE_NONEXISTENT, &target, NULL); |
088696fe LP |
1202 | if (r < 0) |
1203 | return log_debug_errno(r, "Failed to chase symlinks '%s': %m", mount_entry_path(m)); | |
1204 | if (r > 0) /* Reached the end, nothing more to resolve */ | |
1205 | return 1; | |
8fceda93 | 1206 | |
baaa35ad ZJS |
1207 | if (m->n_followed >= CHASE_SYMLINKS_MAX) /* put a boundary on things */ |
1208 | return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), | |
1209 | "Symlink loop on '%s'.", | |
1210 | mount_entry_path(m)); | |
8fceda93 | 1211 | |
088696fe | 1212 | log_debug("Followed mount entry path symlink %s → %s.", mount_entry_path(m), target); |
8fceda93 | 1213 | |
809ceb82 | 1214 | mount_entry_consume_prefix(m, TAKE_PTR(target)); |
8fceda93 | 1215 | |
088696fe LP |
1216 | m->n_followed ++; |
1217 | ||
1218 | return 0; | |
8fceda93 LP |
1219 | } |
1220 | ||
82fb2da2 | 1221 | static int apply_one_mount( |
8fceda93 | 1222 | const char *root_directory, |
4e399953 LP |
1223 | MountEntry *m, |
1224 | const NamespaceInfo *ns_info) { | |
ac0930c8 | 1225 | |
e5f10caf | 1226 | _cleanup_free_ char *inaccessible = NULL; |
a227a4be | 1227 | bool rbind = true, make = false; |
15ae422b | 1228 | const char *what; |
15ae422b | 1229 | int r; |
15ae422b | 1230 | |
c17ec25e | 1231 | assert(m); |
4e399953 | 1232 | assert(ns_info); |
15ae422b | 1233 | |
34de407a | 1234 | log_debug("Applying namespace mount on %s", mount_entry_path(m)); |
fe3c2583 | 1235 | |
c17ec25e | 1236 | switch (m->mode) { |
15ae422b | 1237 | |
160cfdbe | 1238 | case INACCESSIBLE: { |
e5f10caf AZ |
1239 | _cleanup_free_ char *tmp = NULL; |
1240 | const char *runtime_dir; | |
160cfdbe | 1241 | struct stat target; |
6d313367 LP |
1242 | |
1243 | /* First, get rid of everything that is below if there | |
1244 | * is anything... Then, overmount it with an | |
c4b41707 | 1245 | * inaccessible path. */ |
34de407a | 1246 | (void) umount_recursive(mount_entry_path(m), 0); |
6d313367 | 1247 | |
088696fe LP |
1248 | if (lstat(mount_entry_path(m), &target) < 0) { |
1249 | if (errno == ENOENT && m->ignore) | |
1250 | return 0; | |
1251 | ||
cbc056c8 ZJS |
1252 | return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m", |
1253 | mount_entry_path(m)); | |
088696fe | 1254 | } |
15ae422b | 1255 | |
e5f10caf | 1256 | if (geteuid() == 0) |
48b747fa | 1257 | runtime_dir = "/run"; |
e5f10caf | 1258 | else { |
48b747fa LP |
1259 | if (asprintf(&tmp, "/run/user/" UID_FMT, geteuid()) < 0) |
1260 | return -ENOMEM; | |
e5f10caf AZ |
1261 | |
1262 | runtime_dir = tmp; | |
1263 | } | |
1264 | ||
1265 | r = mode_to_inaccessible_node(runtime_dir, target.st_mode, &inaccessible); | |
1266 | if (r < 0) | |
baaa35ad ZJS |
1267 | return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), |
1268 | "File type not supported for inaccessible mounts. Note that symlinks are not allowed"); | |
e5f10caf | 1269 | what = inaccessible; |
c4b41707 | 1270 | break; |
160cfdbe | 1271 | } |
fe3c2583 | 1272 | |
15ae422b | 1273 | case READONLY: |
15ae422b | 1274 | case READWRITE: |
1e05071d | 1275 | case READWRITE_IMPLICIT: |
ddc155b2 TM |
1276 | case EXEC: |
1277 | case NOEXEC: | |
8fceda93 | 1278 | r = path_is_mount_point(mount_entry_path(m), root_directory, 0); |
088696fe LP |
1279 | if (r == -ENOENT && m->ignore) |
1280 | return 0; | |
d944dc95 | 1281 | if (r < 0) |
cbc056c8 ZJS |
1282 | return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m", |
1283 | mount_entry_path(m)); | |
1284 | if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY | |
ddc155b2 | 1285 | * and MS_NOEXEC bits for the mount point if needed. */ |
6b7c9f8b | 1286 | return 0; |
6b7c9f8b | 1287 | /* This isn't a mount point yet, let's make it one. */ |
34de407a | 1288 | what = mount_entry_path(m); |
6b7c9f8b | 1289 | break; |
15ae422b | 1290 | |
d2d6c096 LP |
1291 | case BIND_MOUNT: |
1292 | rbind = false; | |
d2d6c096 | 1293 | |
4831981d | 1294 | _fallthrough_; |
088696fe LP |
1295 | case BIND_MOUNT_RECURSIVE: { |
1296 | _cleanup_free_ char *chased = NULL; | |
5d997827 | 1297 | |
cbc056c8 ZJS |
1298 | /* Since mount() will always follow symlinks we chase the symlinks on our own first. Note |
1299 | * that bind mount source paths are always relative to the host root, hence we pass NULL as | |
1300 | * root directory to chase_symlinks() here. */ | |
088696fe | 1301 | |
a5648b80 | 1302 | r = chase_symlinks(mount_entry_source(m), NULL, CHASE_TRAIL_SLASH, &chased, NULL); |
088696fe LP |
1303 | if (r == -ENOENT && m->ignore) { |
1304 | log_debug_errno(r, "Path %s does not exist, ignoring.", mount_entry_source(m)); | |
1305 | return 0; | |
1306 | } | |
1307 | if (r < 0) | |
1308 | return log_debug_errno(r, "Failed to follow symlinks on %s: %m", mount_entry_source(m)); | |
1309 | ||
1310 | log_debug("Followed source symlinks %s → %s.", mount_entry_source(m), chased); | |
1311 | ||
1312 | free_and_replace(m->source_malloc, chased); | |
d2d6c096 LP |
1313 | |
1314 | what = mount_entry_source(m); | |
a227a4be | 1315 | make = true; |
d2d6c096 | 1316 | break; |
088696fe | 1317 | } |
d2d6c096 | 1318 | |
6c47cd7d | 1319 | case EMPTY_DIR: |
2abd4e38 YW |
1320 | case TMPFS: |
1321 | return mount_tmpfs(m); | |
6c47cd7d | 1322 | |
ac0930c8 | 1323 | case PRIVATE_TMP: |
56a13a49 | 1324 | case PRIVATE_TMP_READONLY: |
89bd586c | 1325 | what = mount_entry_source(m); |
a227a4be | 1326 | make = true; |
15ae422b | 1327 | break; |
e364ad06 | 1328 | |
d6797c92 | 1329 | case PRIVATE_DEV: |
5d997827 LP |
1330 | return mount_private_dev(m); |
1331 | ||
1332 | case BIND_DEV: | |
1333 | return mount_bind_dev(m); | |
1334 | ||
1335 | case SYSFS: | |
1336 | return mount_sysfs(m); | |
1337 | ||
1338 | case PROCFS: | |
4e399953 | 1339 | return mount_procfs(m, ns_info); |
d6797c92 | 1340 | |
94293d65 LB |
1341 | case RUN: |
1342 | return mount_run(m); | |
1343 | ||
80271a44 XR |
1344 | case MQUEUEFS: |
1345 | return mount_mqueuefs(m); | |
1346 | ||
b3d13314 | 1347 | case MOUNT_IMAGES: |
93f59701 LB |
1348 | return mount_image(m, NULL); |
1349 | ||
1350 | case EXTENSION_IMAGES: | |
1351 | return mount_image(m, root_directory); | |
1352 | ||
1353 | case OVERLAY_MOUNT: | |
1354 | return mount_overlay(m); | |
b3d13314 | 1355 | |
e364ad06 | 1356 | default: |
04499a70 | 1357 | assert_not_reached(); |
15ae422b LP |
1358 | } |
1359 | ||
ac0930c8 | 1360 | assert(what); |
15ae422b | 1361 | |
21935150 LP |
1362 | r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL); |
1363 | if (r < 0) { | |
a227a4be | 1364 | bool try_again = false; |
a227a4be LP |
1365 | |
1366 | if (r == -ENOENT && make) { | |
8bab8029 | 1367 | int q; |
a227a4be | 1368 | |
cbc056c8 ZJS |
1369 | /* Hmm, either the source or the destination are missing. Let's see if we can create |
1370 | the destination, then try again. */ | |
a227a4be | 1371 | |
8bab8029 | 1372 | (void) mkdir_parents(mount_entry_path(m), 0755); |
a227a4be | 1373 | |
8bab8029 LB |
1374 | q = make_mount_point_inode_from_path(what, mount_entry_path(m), 0755); |
1375 | if (q < 0) | |
1376 | log_error_errno(q, "Failed to create destination mount point node '%s': %m", | |
1377 | mount_entry_path(m)); | |
1378 | else | |
1379 | try_again = true; | |
a227a4be LP |
1380 | } |
1381 | ||
21935150 LP |
1382 | if (try_again) |
1383 | r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL); | |
a227a4be | 1384 | if (r < 0) |
5dc60faa | 1385 | return log_error_errno(r, "Failed to mount %s to %s: %m", what, mount_entry_path(m)); |
a227a4be | 1386 | } |
6b7c9f8b | 1387 | |
34de407a | 1388 | log_debug("Successfully mounted %s to %s", what, mount_entry_path(m)); |
6b7c9f8b | 1389 | return 0; |
ac0930c8 | 1390 | } |
15ae422b | 1391 | |
6b000af4 | 1392 | static int make_read_only(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) { |
9ce4e4b0 | 1393 | unsigned long new_flags = 0, flags_mask = 0; |
57ccd9f6 YW |
1394 | bool submounts; |
1395 | int r; | |
15ae422b | 1396 | |
c17ec25e | 1397 | assert(m); |
ac9de0b3 | 1398 | assert(proc_self_mountinfo); |
ac0930c8 | 1399 | |
9ce4e4b0 LP |
1400 | if (mount_entry_read_only(m) || m->mode == PRIVATE_DEV) { |
1401 | new_flags |= MS_RDONLY; | |
1402 | flags_mask |= MS_RDONLY; | |
1403 | } | |
1404 | ||
1405 | if (m->nosuid) { | |
1406 | new_flags |= MS_NOSUID; | |
1407 | flags_mask |= MS_NOSUID; | |
1408 | } | |
1409 | ||
1410 | if (flags_mask == 0) /* No Change? */ | |
6b7c9f8b LP |
1411 | return 0; |
1412 | ||
9ce4e4b0 LP |
1413 | /* We generally apply these changes recursively, except for /dev, and the cases we know there's |
1414 | * nothing further down. Set /dev readonly, but not submounts like /dev/shm. Also, we only set the | |
1415 | * per-mount read-only flag. We can't set it on the superblock, if we are inside a user namespace | |
1416 | * and running Linux <= 4.17. */ | |
1417 | submounts = | |
1418 | mount_entry_read_only(m) && | |
1419 | !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1420 | if (submounts) | |
6b000af4 | 1421 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo); |
9ce4e4b0 | 1422 | else |
7cce68e1 | 1423 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo); |
9ce4e4b0 | 1424 | |
ddc155b2 | 1425 | /* Note that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked |
867189b5 LP |
1426 | * read-only already stays this way. This improves compatibility with container managers, where we |
1427 | * won't attempt to undo read-only mounts already applied. */ | |
ac0930c8 | 1428 | |
8fceda93 | 1429 | if (r == -ENOENT && m->ignore) |
867189b5 | 1430 | return 0; |
763a260a | 1431 | if (r < 0) |
9ce4e4b0 | 1432 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), |
763a260a | 1433 | submounts ? " and its submounts" : ""); |
763a260a | 1434 | return 0; |
d944dc95 LP |
1435 | } |
1436 | ||
ddc155b2 TM |
1437 | static int make_noexec(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) { |
1438 | unsigned long new_flags = 0, flags_mask = 0; | |
57ccd9f6 YW |
1439 | bool submounts; |
1440 | int r; | |
ddc155b2 TM |
1441 | |
1442 | assert(m); | |
1443 | assert(proc_self_mountinfo); | |
1444 | ||
1445 | if (mount_entry_noexec(m)) { | |
1446 | new_flags |= MS_NOEXEC; | |
1447 | flags_mask |= MS_NOEXEC; | |
1448 | } else if (mount_entry_exec(m)) { | |
1449 | new_flags &= ~MS_NOEXEC; | |
1450 | flags_mask |= MS_NOEXEC; | |
1451 | } | |
1452 | ||
1453 | if (flags_mask == 0) /* No Change? */ | |
1454 | return 0; | |
1455 | ||
1456 | submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1457 | ||
1458 | if (submounts) | |
1459 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo); | |
1460 | else | |
1461 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo); | |
1462 | ||
1463 | if (r == -ENOENT && m->ignore) | |
1464 | return 0; | |
1465 | if (r < 0) | |
1466 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), | |
1467 | submounts ? " and its submounts" : ""); | |
1468 | return 0; | |
1469 | } | |
1470 | ||
6720e356 | 1471 | static int make_nosuid(const MountEntry *m, FILE *proc_self_mountinfo) { |
57ccd9f6 YW |
1472 | bool submounts; |
1473 | int r; | |
6720e356 YW |
1474 | |
1475 | assert(m); | |
1476 | assert(proc_self_mountinfo); | |
1477 | ||
1478 | submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1479 | ||
1480 | if (submounts) | |
1481 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, NULL, proc_self_mountinfo); | |
1482 | else | |
1483 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, proc_self_mountinfo); | |
1484 | if (r == -ENOENT && m->ignore) | |
1485 | return 0; | |
1486 | if (r < 0) | |
1487 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), | |
1488 | submounts ? " and its submounts" : ""); | |
1489 | return 0; | |
1490 | } | |
1491 | ||
9b68367b | 1492 | static bool namespace_info_mount_apivfs(const NamespaceInfo *ns_info) { |
5d997827 LP |
1493 | assert(ns_info); |
1494 | ||
9c988f93 DH |
1495 | /* |
1496 | * ProtectControlGroups= and ProtectKernelTunables= imply MountAPIVFS=, | |
1497 | * since to protect the API VFS mounts, they need to be around in the | |
9b68367b | 1498 | * first place... |
9c988f93 | 1499 | */ |
5d997827 | 1500 | |
9b68367b YW |
1501 | return ns_info->mount_apivfs || |
1502 | ns_info->protect_control_groups || | |
4e399953 LP |
1503 | ns_info->protect_kernel_tunables || |
1504 | ns_info->protect_proc != PROTECT_PROC_DEFAULT || | |
1505 | ns_info->proc_subset != PROC_SUBSET_ALL; | |
5d997827 LP |
1506 | } |
1507 | ||
da6053d0 | 1508 | static size_t namespace_calculate_mounts( |
bb0ff3fb | 1509 | const NamespaceInfo *ns_info, |
2652c6c1 DH |
1510 | char** read_write_paths, |
1511 | char** read_only_paths, | |
1512 | char** inaccessible_paths, | |
ddc155b2 TM |
1513 | char** exec_paths, |
1514 | char** no_exec_paths, | |
6c47cd7d | 1515 | char** empty_directories, |
da6053d0 LP |
1516 | size_t n_bind_mounts, |
1517 | size_t n_temporary_filesystems, | |
b3d13314 | 1518 | size_t n_mount_images, |
93f59701 LB |
1519 | size_t n_extension_images, |
1520 | size_t n_hierarchies, | |
2652c6c1 DH |
1521 | const char* tmp_dir, |
1522 | const char* var_tmp_dir, | |
bbb4e7f3 | 1523 | const char *creds_path, |
5e8deb94 | 1524 | const char* log_namespace, |
3bdc25a4 LP |
1525 | bool setup_propagate, |
1526 | const char* notify_socket) { | |
2652c6c1 | 1527 | |
da6053d0 LP |
1528 | size_t protect_home_cnt; |
1529 | size_t protect_system_cnt = | |
52b3d652 | 1530 | (ns_info->protect_system == PROTECT_SYSTEM_STRICT ? |
f471b2af | 1531 | ELEMENTSOF(protect_system_strict_table) : |
52b3d652 | 1532 | ((ns_info->protect_system == PROTECT_SYSTEM_FULL) ? |
f471b2af | 1533 | ELEMENTSOF(protect_system_full_table) : |
52b3d652 | 1534 | ((ns_info->protect_system == PROTECT_SYSTEM_YES) ? |
f471b2af DH |
1535 | ELEMENTSOF(protect_system_yes_table) : 0))); |
1536 | ||
b6c432ca | 1537 | protect_home_cnt = |
52b3d652 | 1538 | (ns_info->protect_home == PROTECT_HOME_YES ? |
b6c432ca | 1539 | ELEMENTSOF(protect_home_yes_table) : |
52b3d652 | 1540 | ((ns_info->protect_home == PROTECT_HOME_READ_ONLY) ? |
e4da7d8c | 1541 | ELEMENTSOF(protect_home_read_only_table) : |
52b3d652 | 1542 | ((ns_info->protect_home == PROTECT_HOME_TMPFS) ? |
e4da7d8c | 1543 | ELEMENTSOF(protect_home_tmpfs_table) : 0))); |
b6c432ca | 1544 | |
2652c6c1 DH |
1545 | return !!tmp_dir + !!var_tmp_dir + |
1546 | strv_length(read_write_paths) + | |
1547 | strv_length(read_only_paths) + | |
1548 | strv_length(inaccessible_paths) + | |
ddc155b2 TM |
1549 | strv_length(exec_paths) + |
1550 | strv_length(no_exec_paths) + | |
6c47cd7d | 1551 | strv_length(empty_directories) + |
d2d6c096 | 1552 | n_bind_mounts + |
b3d13314 | 1553 | n_mount_images + |
93f59701 | 1554 | (n_extension_images > 0 ? n_hierarchies + n_extension_images : 0) + /* Mount each image plus an overlay per hierarchy */ |
2abd4e38 | 1555 | n_temporary_filesystems + |
c575770b DH |
1556 | ns_info->private_dev + |
1557 | (ns_info->protect_kernel_tunables ? ELEMENTSOF(protect_kernel_tunables_table) : 0) + | |
c575770b | 1558 | (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) + |
94a7b275 KK |
1559 | (ns_info->protect_kernel_logs ? ELEMENTSOF(protect_kernel_logs_table) : 0) + |
1560 | (ns_info->protect_control_groups ? 1 : 0) + | |
5d997827 | 1561 | protect_home_cnt + protect_system_cnt + |
aecd5ac6 | 1562 | (ns_info->protect_hostname ? 2 : 0) + |
91dd5f7c | 1563 | (namespace_info_mount_apivfs(ns_info) ? ELEMENTSOF(apivfs_table) : 0) + |
bbb4e7f3 | 1564 | (creds_path ? 2 : 1) + |
3bdc25a4 LP |
1565 | !!log_namespace + |
1566 | setup_propagate + /* /run/systemd/incoming */ | |
80271a44 XR |
1567 | !!notify_socket + |
1568 | ns_info->private_ipc; /* /dev/mqueue */ | |
2652c6c1 DH |
1569 | } |
1570 | ||
da6053d0 | 1571 | static void normalize_mounts(const char *root_directory, MountEntry *mounts, size_t *n_mounts) { |
9b68367b | 1572 | assert(root_directory); |
f8b64b57 LP |
1573 | assert(n_mounts); |
1574 | assert(mounts || *n_mounts == 0); | |
1575 | ||
93bab288 | 1576 | typesafe_qsort(mounts, *n_mounts, mount_path_compare); |
f8b64b57 LP |
1577 | |
1578 | drop_duplicates(mounts, n_mounts); | |
1579 | drop_outside_root(root_directory, mounts, n_mounts); | |
1580 | drop_inaccessible(mounts, n_mounts); | |
1581 | drop_nop(mounts, n_mounts); | |
1582 | } | |
1583 | ||
df61e79a LB |
1584 | static int create_symlinks_from_tuples(const char *root, char **strv_symlinks) { |
1585 | char **src, **dst; | |
1586 | int r; | |
1587 | ||
1588 | STRV_FOREACH_PAIR(src, dst, strv_symlinks) { | |
1589 | _cleanup_free_ char *src_abs = NULL, *dst_abs = NULL; | |
1590 | ||
1591 | src_abs = path_join(root, *src); | |
1592 | dst_abs = path_join(root, *dst); | |
1593 | if (!src_abs || !dst_abs) | |
1594 | return -ENOMEM; | |
1595 | ||
1596 | r = mkdir_parents_label(dst_abs, 0755); | |
1597 | if (r < 0) | |
1598 | return r; | |
1599 | ||
1600 | r = symlink_idempotent(src_abs, dst_abs, true); | |
1601 | if (r < 0) | |
1602 | return r; | |
1603 | } | |
1604 | ||
1605 | return 0; | |
1606 | } | |
1607 | ||
82fb2da2 LB |
1608 | static int apply_mounts( |
1609 | const char *root, | |
1610 | const NamespaceInfo *ns_info, | |
1611 | MountEntry *mounts, | |
1612 | size_t *n_mounts, | |
df61e79a | 1613 | char **exec_dir_symlinks, |
82fb2da2 LB |
1614 | char **error_path) { |
1615 | ||
1616 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; | |
1617 | _cleanup_free_ char **deny_list = NULL; | |
82fb2da2 LB |
1618 | int r; |
1619 | ||
1620 | if (n_mounts == 0) /* Shortcut: nothing to do */ | |
1621 | return 0; | |
1622 | ||
1623 | assert(root); | |
1624 | assert(mounts); | |
1625 | assert(n_mounts); | |
1626 | ||
1627 | /* Open /proc/self/mountinfo now as it may become unavailable if we mount anything on top of | |
1628 | * /proc. For example, this is the case with the option: 'InaccessiblePaths=/proc'. */ | |
1629 | proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); | |
1630 | if (!proc_self_mountinfo) { | |
d60e3b40 LP |
1631 | r = -errno; |
1632 | ||
82fb2da2 LB |
1633 | if (error_path) |
1634 | *error_path = strdup("/proc/self/mountinfo"); | |
d60e3b40 LP |
1635 | |
1636 | return log_debug_errno(r, "Failed to open /proc/self/mountinfo: %m"); | |
82fb2da2 LB |
1637 | } |
1638 | ||
1639 | /* First round, establish all mounts we need */ | |
1640 | for (;;) { | |
1641 | bool again = false; | |
1642 | ||
1643 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1644 | ||
1645 | if (m->applied) | |
1646 | continue; | |
1647 | ||
93f59701 LB |
1648 | /* ExtensionImages are first opened in the propagate directory, not in the root_directory */ |
1649 | r = follow_symlink(m->mode != EXTENSION_IMAGES ? root : NULL, m); | |
82fb2da2 LB |
1650 | if (r < 0) { |
1651 | if (error_path && mount_entry_path(m)) | |
1652 | *error_path = strdup(mount_entry_path(m)); | |
1653 | return r; | |
1654 | } | |
1655 | if (r == 0) { | |
1656 | /* We hit a symlinked mount point. The entry got rewritten and might | |
1657 | * point to a very different place now. Let's normalize the changed | |
1658 | * list, and start from the beginning. After all to mount the entry | |
1659 | * at the new location we might need some other mounts first */ | |
1660 | again = true; | |
1661 | break; | |
1662 | } | |
1663 | ||
1664 | r = apply_one_mount(root, m, ns_info); | |
1665 | if (r < 0) { | |
1666 | if (error_path && mount_entry_path(m)) | |
1667 | *error_path = strdup(mount_entry_path(m)); | |
1668 | return r; | |
1669 | } | |
1670 | ||
1671 | m->applied = true; | |
1672 | } | |
1673 | ||
1674 | if (!again) | |
1675 | break; | |
1676 | ||
1677 | normalize_mounts(root, mounts, n_mounts); | |
1678 | } | |
1679 | ||
df61e79a LB |
1680 | /* Now that all filesystems have been set up, but before the |
1681 | * read-only switches are flipped, create the exec dirs symlinks. | |
1682 | * Note that when /var/lib is not empty/tmpfs, these symlinks will already | |
1683 | * exist, which means this will be a no-op. */ | |
1684 | r = create_symlinks_from_tuples(root, exec_dir_symlinks); | |
1685 | if (r < 0) | |
1686 | return r; | |
1687 | ||
82fb2da2 LB |
1688 | /* Create a deny list we can pass to bind_mount_recursive() */ |
1689 | deny_list = new(char*, (*n_mounts)+1); | |
1690 | if (!deny_list) | |
1691 | return -ENOMEM; | |
8794e6db | 1692 | for (size_t j = 0; j < *n_mounts; j++) |
82fb2da2 | 1693 | deny_list[j] = (char*) mount_entry_path(mounts+j); |
8794e6db | 1694 | deny_list[*n_mounts] = NULL; |
82fb2da2 LB |
1695 | |
1696 | /* Second round, flip the ro bits if necessary. */ | |
1697 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1698 | r = make_read_only(m, deny_list, proc_self_mountinfo); | |
1699 | if (r < 0) { | |
1700 | if (error_path && mount_entry_path(m)) | |
1701 | *error_path = strdup(mount_entry_path(m)); | |
1702 | return r; | |
1703 | } | |
1704 | } | |
1705 | ||
1706 | /* Third round, flip the noexec bits with a simplified deny list. */ | |
8794e6db | 1707 | for (size_t j = 0; j < *n_mounts; j++) |
82fb2da2 LB |
1708 | if (IN_SET((mounts+j)->mode, EXEC, NOEXEC)) |
1709 | deny_list[j] = (char*) mount_entry_path(mounts+j); | |
8794e6db | 1710 | deny_list[*n_mounts] = NULL; |
82fb2da2 LB |
1711 | |
1712 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1713 | r = make_noexec(m, deny_list, proc_self_mountinfo); | |
1714 | if (r < 0) { | |
1715 | if (error_path && mount_entry_path(m)) | |
1716 | *error_path = strdup(mount_entry_path(m)); | |
1717 | return r; | |
1718 | } | |
1719 | } | |
1720 | ||
6720e356 YW |
1721 | /* Fourth round, flip the nosuid bits without a deny list. */ |
1722 | if (ns_info->mount_nosuid) | |
1723 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1724 | r = make_nosuid(m, proc_self_mountinfo); | |
1725 | if (r < 0) { | |
1726 | if (error_path && mount_entry_path(m)) | |
1727 | *error_path = strdup(mount_entry_path(m)); | |
1728 | return r; | |
1729 | } | |
1730 | } | |
1731 | ||
82fb2da2 LB |
1732 | return 1; |
1733 | } | |
1734 | ||
c8c535d5 LP |
1735 | static bool root_read_only( |
1736 | char **read_only_paths, | |
1737 | ProtectSystem protect_system) { | |
1738 | ||
1739 | /* Determine whether the root directory is going to be read-only given the configured settings. */ | |
1740 | ||
1741 | if (protect_system == PROTECT_SYSTEM_STRICT) | |
1742 | return true; | |
1743 | ||
de46b2be | 1744 | if (prefixed_path_strv_contains(read_only_paths, "/")) |
c8c535d5 LP |
1745 | return true; |
1746 | ||
1747 | return false; | |
1748 | } | |
1749 | ||
1750 | static bool home_read_only( | |
1751 | char** read_only_paths, | |
1752 | char** inaccessible_paths, | |
1753 | char** empty_directories, | |
1754 | const BindMount *bind_mounts, | |
1755 | size_t n_bind_mounts, | |
1756 | const TemporaryFileSystem *temporary_filesystems, | |
1757 | size_t n_temporary_filesystems, | |
1758 | ProtectHome protect_home) { | |
1759 | ||
c8c535d5 LP |
1760 | /* Determine whether the /home directory is going to be read-only given the configured settings. Yes, |
1761 | * this is a bit sloppy, since we don't bother checking for cases where / is affected by multiple | |
1762 | * settings. */ | |
1763 | ||
1764 | if (protect_home != PROTECT_HOME_NO) | |
1765 | return true; | |
1766 | ||
de46b2be TM |
1767 | if (prefixed_path_strv_contains(read_only_paths, "/home") || |
1768 | prefixed_path_strv_contains(inaccessible_paths, "/home") || | |
1769 | prefixed_path_strv_contains(empty_directories, "/home")) | |
c8c535d5 LP |
1770 | return true; |
1771 | ||
8794e6db | 1772 | for (size_t i = 0; i < n_temporary_filesystems; i++) |
c8c535d5 LP |
1773 | if (path_equal(temporary_filesystems[i].path, "/home")) |
1774 | return true; | |
1775 | ||
1776 | /* If /home is overmounted with some dir from the host it's not writable. */ | |
8794e6db | 1777 | for (size_t i = 0; i < n_bind_mounts; i++) |
c8c535d5 LP |
1778 | if (path_equal(bind_mounts[i].destination, "/home")) |
1779 | return true; | |
1780 | ||
1781 | return false; | |
1782 | } | |
1783 | ||
89e62e0b LP |
1784 | static int verity_settings_prepare( |
1785 | VeritySettings *verity, | |
1786 | const char *root_image, | |
1787 | const void *root_hash, | |
1788 | size_t root_hash_size, | |
1789 | const char *root_hash_path, | |
1790 | const void *root_hash_sig, | |
1791 | size_t root_hash_sig_size, | |
1792 | const char *root_hash_sig_path, | |
1793 | const char *verity_data_path) { | |
1794 | ||
1795 | int r; | |
1796 | ||
1797 | assert(verity); | |
1798 | ||
1799 | if (root_hash) { | |
1800 | void *d; | |
1801 | ||
1802 | d = memdup(root_hash, root_hash_size); | |
1803 | if (!d) | |
1804 | return -ENOMEM; | |
1805 | ||
1806 | free_and_replace(verity->root_hash, d); | |
1807 | verity->root_hash_size = root_hash_size; | |
aee36b4e | 1808 | verity->designator = PARTITION_ROOT; |
89e62e0b LP |
1809 | } |
1810 | ||
1811 | if (root_hash_sig) { | |
1812 | void *d; | |
1813 | ||
1814 | d = memdup(root_hash_sig, root_hash_sig_size); | |
1815 | if (!d) | |
1816 | return -ENOMEM; | |
1817 | ||
1818 | free_and_replace(verity->root_hash_sig, d); | |
1819 | verity->root_hash_sig_size = root_hash_sig_size; | |
aee36b4e | 1820 | verity->designator = PARTITION_ROOT; |
89e62e0b LP |
1821 | } |
1822 | ||
1823 | if (verity_data_path) { | |
1824 | r = free_and_strdup(&verity->data_path, verity_data_path); | |
1825 | if (r < 0) | |
1826 | return r; | |
1827 | } | |
1828 | ||
1829 | r = verity_settings_load( | |
1830 | verity, | |
1831 | root_image, | |
1832 | root_hash_path, | |
1833 | root_hash_sig_path); | |
1834 | if (r < 0) | |
1835 | return log_debug_errno(r, "Failed to load root hash: %m"); | |
1836 | ||
1837 | return 0; | |
1838 | } | |
1839 | ||
613b411c | 1840 | int setup_namespace( |
ee818b89 | 1841 | const char* root_directory, |
915e6d16 | 1842 | const char* root_image, |
18d73705 | 1843 | const MountOptions *root_image_options, |
bb0ff3fb | 1844 | const NamespaceInfo *ns_info, |
2a624c36 AP |
1845 | char** read_write_paths, |
1846 | char** read_only_paths, | |
1847 | char** inaccessible_paths, | |
ddc155b2 TM |
1848 | char** exec_paths, |
1849 | char** no_exec_paths, | |
6c47cd7d | 1850 | char** empty_directories, |
df61e79a | 1851 | char** exec_dir_symlinks, |
d2d6c096 | 1852 | const BindMount *bind_mounts, |
da6053d0 | 1853 | size_t n_bind_mounts, |
2abd4e38 | 1854 | const TemporaryFileSystem *temporary_filesystems, |
da6053d0 | 1855 | size_t n_temporary_filesystems, |
b3d13314 LB |
1856 | const MountImage *mount_images, |
1857 | size_t n_mount_images, | |
a004cb4c LP |
1858 | const char* tmp_dir, |
1859 | const char* var_tmp_dir, | |
bbb4e7f3 | 1860 | const char *creds_path, |
91dd5f7c | 1861 | const char *log_namespace, |
915e6d16 | 1862 | unsigned long mount_flags, |
0389f4fa LB |
1863 | const void *root_hash, |
1864 | size_t root_hash_size, | |
1865 | const char *root_hash_path, | |
d4d55b0d LB |
1866 | const void *root_hash_sig, |
1867 | size_t root_hash_sig_size, | |
1868 | const char *root_hash_sig_path, | |
89e62e0b | 1869 | const char *verity_data_path, |
93f59701 LB |
1870 | const MountImage *extension_images, |
1871 | size_t n_extension_images, | |
5e8deb94 LB |
1872 | const char *propagate_dir, |
1873 | const char *incoming_dir, | |
3bdc25a4 | 1874 | const char *notify_socket, |
7cc5ef5f | 1875 | char **error_path) { |
15ae422b | 1876 | |
915e6d16 | 1877 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
78ebe980 | 1878 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; |
915e6d16 | 1879 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
aee36b4e | 1880 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
93f59701 | 1881 | _cleanup_strv_free_ char **hierarchies = NULL; |
5f7a690a | 1882 | MountEntry *m = NULL, *mounts = NULL; |
5e8deb94 | 1883 | bool require_prefix = false, setup_propagate = false; |
93f59701 | 1884 | const char *root, *extension_dir = "/run/systemd/unit-extensions"; |
9cb1709b LP |
1885 | DissectImageFlags dissect_image_flags = |
1886 | DISSECT_IMAGE_GENERIC_ROOT | | |
1887 | DISSECT_IMAGE_REQUIRE_ROOT | | |
1888 | DISSECT_IMAGE_DISCARD_ON_LOOP | | |
1889 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
1890 | DISSECT_IMAGE_FSCK | | |
c65f854a LP |
1891 | DISSECT_IMAGE_USR_NO_ROOT | |
1892 | DISSECT_IMAGE_GROWFS; | |
89e62e0b LP |
1893 | size_t n_mounts; |
1894 | int r; | |
15ae422b | 1895 | |
915e6d16 LP |
1896 | assert(ns_info); |
1897 | ||
cdf42f9b LP |
1898 | /* Make sure that all mknod(), mkdir() calls we do are unaffected by the umask, and the access modes |
1899 | * we configure take effect */ | |
1900 | BLOCK_WITH_UMASK(0000); | |
1901 | ||
5e8deb94 LB |
1902 | if (!isempty(propagate_dir) && !isempty(incoming_dir)) |
1903 | setup_propagate = true; | |
1904 | ||
613b411c | 1905 | if (mount_flags == 0) |
c17ec25e | 1906 | mount_flags = MS_SHARED; |
ac0930c8 | 1907 | |
915e6d16 | 1908 | if (root_image) { |
c8c535d5 LP |
1909 | /* Make the whole image read-only if we can determine that we only access it in a read-only fashion. */ |
1910 | if (root_read_only(read_only_paths, | |
52b3d652 | 1911 | ns_info->protect_system) && |
c8c535d5 LP |
1912 | home_read_only(read_only_paths, inaccessible_paths, empty_directories, |
1913 | bind_mounts, n_bind_mounts, temporary_filesystems, n_temporary_filesystems, | |
52b3d652 | 1914 | ns_info->protect_home) && |
c9ef8573 | 1915 | strv_isempty(read_write_paths)) |
915e6d16 LP |
1916 | dissect_image_flags |= DISSECT_IMAGE_READ_ONLY; |
1917 | ||
89e62e0b LP |
1918 | r = verity_settings_prepare( |
1919 | &verity, | |
1920 | root_image, | |
1921 | root_hash, root_hash_size, root_hash_path, | |
1922 | root_hash_sig, root_hash_sig_size, root_hash_sig_path, | |
1923 | verity_data_path); | |
915e6d16 | 1924 | if (r < 0) |
89e62e0b LP |
1925 | return r; |
1926 | ||
1927 | SET_FLAG(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE, verity.data_path); | |
915e6d16 | 1928 | |
89e62e0b LP |
1929 | r = loop_device_make_by_path( |
1930 | root_image, | |
ef9c184d | 1931 | FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1 /* < 0 means writable if possible, read-only as fallback */, |
89e62e0b LP |
1932 | FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, |
1933 | &loop_device); | |
78ebe980 | 1934 | if (r < 0) |
89e62e0b LP |
1935 | return log_debug_errno(r, "Failed to create loop device for root image: %m"); |
1936 | ||
1937 | r = dissect_image( | |
1938 | loop_device->fd, | |
1939 | &verity, | |
1940 | root_image_options, | |
a3642997 | 1941 | loop_device->diskseq, |
75dc190d | 1942 | loop_device->uevent_seqnum_not_before, |
4a62257d | 1943 | loop_device->timestamp_not_before, |
89e62e0b LP |
1944 | dissect_image_flags, |
1945 | &dissected_image); | |
78ebe980 | 1946 | if (r < 0) |
763a260a | 1947 | return log_debug_errno(r, "Failed to dissect image: %m"); |
78ebe980 | 1948 | |
88b3300f LP |
1949 | r = dissected_image_load_verity_sig_partition( |
1950 | dissected_image, | |
1951 | loop_device->fd, | |
1952 | &verity); | |
1953 | if (r < 0) | |
1954 | return r; | |
1955 | ||
89e62e0b LP |
1956 | r = dissected_image_decrypt( |
1957 | dissected_image, | |
1958 | NULL, | |
1959 | &verity, | |
1960 | dissect_image_flags, | |
1961 | &decrypted_image); | |
915e6d16 | 1962 | if (r < 0) |
763a260a | 1963 | return log_debug_errno(r, "Failed to decrypt dissected image: %m"); |
915e6d16 LP |
1964 | } |
1965 | ||
e908468b LP |
1966 | if (root_directory) |
1967 | root = root_directory; | |
0722b359 | 1968 | else { |
77f16dbd DDM |
1969 | /* /run/systemd should have been created by PID 1 early on already, but in some cases, like |
1970 | * when running tests (test-execute), it might not have been created yet so let's make sure | |
1971 | * we create it if it doesn't already exist. */ | |
1972 | (void) mkdir_p_label("/run/systemd", 0755); | |
1973 | ||
d73020f2 LP |
1974 | /* Always create the mount namespace in a temporary directory, instead of operating directly |
1975 | * in the root. The temporary directory prevents any mounts from being potentially obscured | |
1976 | * my other mounts we already applied. We use the same mount point for all images, which is | |
1977 | * safe, since they all live in their own namespaces after all, and hence won't see each | |
1978 | * other. */ | |
e908468b LP |
1979 | |
1980 | root = "/run/systemd/unit-root"; | |
1981 | (void) mkdir_label(root, 0700); | |
d18aff04 | 1982 | require_prefix = true; |
0722b359 | 1983 | } |
e908468b | 1984 | |
93f59701 LB |
1985 | if (n_extension_images > 0) { |
1986 | r = parse_env_extension_hierarchies(&hierarchies); | |
1987 | if (r < 0) | |
1988 | return r; | |
1989 | } | |
1990 | ||
cfbeb4ef LP |
1991 | n_mounts = namespace_calculate_mounts( |
1992 | ns_info, | |
1993 | read_write_paths, | |
1994 | read_only_paths, | |
1995 | inaccessible_paths, | |
ddc155b2 TM |
1996 | exec_paths, |
1997 | no_exec_paths, | |
6c47cd7d | 1998 | empty_directories, |
f5c52a77 | 1999 | n_bind_mounts, |
2abd4e38 | 2000 | n_temporary_filesystems, |
b3d13314 | 2001 | n_mount_images, |
93f59701 LB |
2002 | n_extension_images, |
2003 | strv_length(hierarchies), | |
cfbeb4ef | 2004 | tmp_dir, var_tmp_dir, |
bbb4e7f3 | 2005 | creds_path, |
5e8deb94 | 2006 | log_namespace, |
3bdc25a4 LP |
2007 | setup_propagate, |
2008 | notify_socket); | |
613b411c | 2009 | |
f0a4feb0 | 2010 | if (n_mounts > 0) { |
5f7a690a LP |
2011 | m = mounts = new0(MountEntry, n_mounts); |
2012 | if (!mounts) | |
2013 | return -ENOMEM; | |
2014 | ||
d18aff04 | 2015 | r = append_access_mounts(&m, read_write_paths, READWRITE, require_prefix); |
613b411c | 2016 | if (r < 0) |
f0a4feb0 | 2017 | goto finish; |
613b411c | 2018 | |
d18aff04 | 2019 | r = append_access_mounts(&m, read_only_paths, READONLY, require_prefix); |
613b411c | 2020 | if (r < 0) |
f0a4feb0 | 2021 | goto finish; |
613b411c | 2022 | |
d18aff04 | 2023 | r = append_access_mounts(&m, inaccessible_paths, INACCESSIBLE, require_prefix); |
613b411c | 2024 | if (r < 0) |
f0a4feb0 | 2025 | goto finish; |
7ff7394d | 2026 | |
ddc155b2 TM |
2027 | r = append_access_mounts(&m, exec_paths, EXEC, require_prefix); |
2028 | if (r < 0) | |
2029 | goto finish; | |
2030 | ||
2031 | r = append_access_mounts(&m, no_exec_paths, NOEXEC, require_prefix); | |
2032 | if (r < 0) | |
2033 | goto finish; | |
2034 | ||
6c47cd7d LP |
2035 | r = append_empty_dir_mounts(&m, empty_directories); |
2036 | if (r < 0) | |
2037 | goto finish; | |
2038 | ||
d2d6c096 LP |
2039 | r = append_bind_mounts(&m, bind_mounts, n_bind_mounts); |
2040 | if (r < 0) | |
2041 | goto finish; | |
2042 | ||
2abd4e38 YW |
2043 | r = append_tmpfs_mounts(&m, temporary_filesystems, n_temporary_filesystems); |
2044 | if (r < 0) | |
2045 | goto finish; | |
2046 | ||
613b411c | 2047 | if (tmp_dir) { |
56a13a49 ZJS |
2048 | bool ro = streq(tmp_dir, RUN_SYSTEMD_EMPTY); |
2049 | ||
34de407a | 2050 | *(m++) = (MountEntry) { |
5327c910 | 2051 | .path_const = "/tmp", |
56a13a49 | 2052 | .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP, |
89bd586c | 2053 | .source_const = tmp_dir, |
5327c910 | 2054 | }; |
613b411c | 2055 | } |
7ff7394d | 2056 | |
613b411c | 2057 | if (var_tmp_dir) { |
56a13a49 ZJS |
2058 | bool ro = streq(var_tmp_dir, RUN_SYSTEMD_EMPTY); |
2059 | ||
34de407a | 2060 | *(m++) = (MountEntry) { |
5327c910 | 2061 | .path_const = "/var/tmp", |
56a13a49 | 2062 | .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP, |
89bd586c | 2063 | .source_const = var_tmp_dir, |
5327c910 | 2064 | }; |
7ff7394d | 2065 | } |
ac0930c8 | 2066 | |
b3d13314 LB |
2067 | r = append_mount_images(&m, mount_images, n_mount_images); |
2068 | if (r < 0) | |
2069 | goto finish; | |
2070 | ||
93f59701 LB |
2071 | r = append_extension_images(&m, root, extension_dir, hierarchies, extension_images, n_extension_images); |
2072 | if (r < 0) | |
2073 | goto finish; | |
2074 | ||
d46b79bb | 2075 | if (ns_info->private_dev) |
34de407a | 2076 | *(m++) = (MountEntry) { |
5327c910 LP |
2077 | .path_const = "/dev", |
2078 | .mode = PRIVATE_DEV, | |
9ce4e4b0 | 2079 | .flags = DEV_MOUNT_OPTIONS, |
5327c910 | 2080 | }; |
7f112f50 | 2081 | |
c575770b | 2082 | if (ns_info->protect_kernel_tunables) { |
cbc056c8 ZJS |
2083 | r = append_static_mounts(&m, |
2084 | protect_kernel_tunables_table, | |
2085 | ELEMENTSOF(protect_kernel_tunables_table), | |
2086 | ns_info->ignore_protect_paths); | |
c575770b | 2087 | if (r < 0) |
f0a4feb0 | 2088 | goto finish; |
c575770b DH |
2089 | } |
2090 | ||
2091 | if (ns_info->protect_kernel_modules) { | |
cbc056c8 ZJS |
2092 | r = append_static_mounts(&m, |
2093 | protect_kernel_modules_table, | |
2094 | ELEMENTSOF(protect_kernel_modules_table), | |
2095 | ns_info->ignore_protect_paths); | |
c575770b | 2096 | if (r < 0) |
f0a4feb0 | 2097 | goto finish; |
c575770b | 2098 | } |
59eeb84b | 2099 | |
94a7b275 | 2100 | if (ns_info->protect_kernel_logs) { |
cbc056c8 ZJS |
2101 | r = append_static_mounts(&m, |
2102 | protect_kernel_logs_table, | |
2103 | ELEMENTSOF(protect_kernel_logs_table), | |
2104 | ns_info->ignore_protect_paths); | |
94a7b275 KK |
2105 | if (r < 0) |
2106 | goto finish; | |
2107 | } | |
2108 | ||
d46b79bb | 2109 | if (ns_info->protect_control_groups) |
34de407a | 2110 | *(m++) = (MountEntry) { |
5327c910 LP |
2111 | .path_const = "/sys/fs/cgroup", |
2112 | .mode = READONLY, | |
2113 | }; | |
59eeb84b | 2114 | |
52b3d652 | 2115 | r = append_protect_home(&m, ns_info->protect_home, ns_info->ignore_protect_paths); |
b6c432ca | 2116 | if (r < 0) |
f0a4feb0 | 2117 | goto finish; |
417116f2 | 2118 | |
52b3d652 | 2119 | r = append_protect_system(&m, ns_info->protect_system, false); |
f471b2af | 2120 | if (r < 0) |
f0a4feb0 | 2121 | goto finish; |
417116f2 | 2122 | |
9b68367b | 2123 | if (namespace_info_mount_apivfs(ns_info)) { |
cbc056c8 ZJS |
2124 | r = append_static_mounts(&m, |
2125 | apivfs_table, | |
2126 | ELEMENTSOF(apivfs_table), | |
2127 | ns_info->ignore_protect_paths); | |
5d997827 LP |
2128 | if (r < 0) |
2129 | goto finish; | |
2130 | } | |
2131 | ||
aecd5ac6 TM |
2132 | if (ns_info->protect_hostname) { |
2133 | *(m++) = (MountEntry) { | |
2134 | .path_const = "/proc/sys/kernel/hostname", | |
2135 | .mode = READONLY, | |
2136 | }; | |
2137 | *(m++) = (MountEntry) { | |
2138 | .path_const = "/proc/sys/kernel/domainname", | |
2139 | .mode = READONLY, | |
2140 | }; | |
2141 | } | |
2142 | ||
57ac6959 | 2143 | if (ns_info->private_ipc) |
80271a44 XR |
2144 | *(m++) = (MountEntry) { |
2145 | .path_const = "/dev/mqueue", | |
2146 | .mode = MQUEUEFS, | |
2147 | .flags = MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, | |
2148 | }; | |
80271a44 | 2149 | |
bbb4e7f3 LP |
2150 | if (creds_path) { |
2151 | /* If our service has a credentials store configured, then bind that one in, but hide | |
2152 | * everything else. */ | |
2153 | ||
2154 | *(m++) = (MountEntry) { | |
2155 | .path_const = "/run/credentials", | |
2156 | .mode = TMPFS, | |
2157 | .read_only = true, | |
2158 | .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, | |
2159 | .flags = MS_NODEV|MS_STRICTATIME|MS_NOSUID|MS_NOEXEC, | |
2160 | }; | |
2161 | ||
2162 | *(m++) = (MountEntry) { | |
2163 | .path_const = creds_path, | |
2164 | .mode = BIND_MOUNT, | |
2165 | .read_only = true, | |
2166 | .source_const = creds_path, | |
2167 | }; | |
2168 | } else { | |
2169 | /* If our service has no credentials store configured, then make the whole | |
2170 | * credentials tree inaccessible wholesale. */ | |
2171 | ||
2172 | *(m++) = (MountEntry) { | |
2173 | .path_const = "/run/credentials", | |
2174 | .mode = INACCESSIBLE, | |
2175 | .ignore = true, | |
2176 | }; | |
2177 | } | |
2178 | ||
91dd5f7c | 2179 | if (log_namespace) { |
c2b2df60 | 2180 | _cleanup_free_ char *q = NULL; |
91dd5f7c LP |
2181 | |
2182 | q = strjoin("/run/systemd/journal.", log_namespace); | |
2183 | if (!q) { | |
2184 | r = -ENOMEM; | |
2185 | goto finish; | |
2186 | } | |
2187 | ||
2188 | *(m++) = (MountEntry) { | |
2189 | .path_const = "/run/systemd/journal", | |
2190 | .mode = BIND_MOUNT_RECURSIVE, | |
2191 | .read_only = true, | |
2192 | .source_malloc = TAKE_PTR(q), | |
2193 | }; | |
2194 | } | |
2195 | ||
5e8deb94 LB |
2196 | /* Will be used to add bind mounts at runtime */ |
2197 | if (setup_propagate) | |
2198 | *(m++) = (MountEntry) { | |
2199 | .source_const = propagate_dir, | |
2200 | .path_const = incoming_dir, | |
2201 | .mode = BIND_MOUNT, | |
2202 | .read_only = true, | |
2203 | }; | |
2204 | ||
3bdc25a4 LP |
2205 | if (notify_socket) |
2206 | *(m++) = (MountEntry) { | |
2207 | .path_const = notify_socket, | |
2208 | .source_const = notify_socket, | |
2209 | .mode = BIND_MOUNT, | |
2210 | .read_only = true, | |
2211 | }; | |
2212 | ||
f0a4feb0 | 2213 | assert(mounts + n_mounts == m); |
ac0930c8 | 2214 | |
5327c910 | 2215 | /* Prepend the root directory where that's necessary */ |
e908468b | 2216 | r = prefix_where_needed(mounts, n_mounts, root); |
5327c910 LP |
2217 | if (r < 0) |
2218 | goto finish; | |
2219 | ||
839f1877 | 2220 | normalize_mounts(root, mounts, &n_mounts); |
15ae422b LP |
2221 | } |
2222 | ||
1beab8b0 LP |
2223 | /* All above is just preparation, figuring out what to do. Let's now actually start doing something. */ |
2224 | ||
d944dc95 | 2225 | if (unshare(CLONE_NEWNS) < 0) { |
763a260a | 2226 | r = log_debug_errno(errno, "Failed to unshare the mount namespace: %m"); |
1beab8b0 | 2227 | if (IN_SET(r, -EACCES, -EPERM, -EOPNOTSUPP, -ENOSYS)) |
cbc056c8 ZJS |
2228 | /* If the kernel doesn't support namespaces, or when there's a MAC or seccomp filter |
2229 | * in place that doesn't allow us to create namespaces (or a missing cap), then | |
2230 | * propagate a recognizable error back, which the caller can use to detect this case | |
2231 | * (and only this) and optionally continue without namespacing applied. */ | |
1beab8b0 LP |
2232 | r = -ENOANO; |
2233 | ||
d944dc95 LP |
2234 | goto finish; |
2235 | } | |
1e4e94c8 | 2236 | |
5e8deb94 LB |
2237 | /* Create the source directory to allow runtime propagation of mounts */ |
2238 | if (setup_propagate) | |
2239 | (void) mkdir_p(propagate_dir, 0600); | |
2240 | ||
57ac6959 | 2241 | if (n_extension_images > 0) |
d73020f2 LP |
2242 | /* ExtensionImages mountpoint directories will be created while parsing the mounts to create, |
2243 | * so have the parent ready */ | |
93f59701 | 2244 | (void) mkdir_p(extension_dir, 0600); |
93f59701 | 2245 | |
9b68367b YW |
2246 | /* Remount / as SLAVE so that nothing now mounted in the namespace |
2247 | * shows up in the parent */ | |
2248 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) { | |
763a260a | 2249 | r = log_debug_errno(errno, "Failed to remount '/' as SLAVE: %m"); |
9b68367b | 2250 | goto finish; |
ee818b89 AC |
2251 | } |
2252 | ||
915e6d16 | 2253 | if (root_image) { |
e908468b | 2254 | /* A root image is specified, mount it to the right place */ |
21b61b1d | 2255 | r = dissected_image_mount(dissected_image, root, UID_INVALID, UID_INVALID, dissect_image_flags); |
763a260a YW |
2256 | if (r < 0) { |
2257 | log_debug_errno(r, "Failed to mount root image: %m"); | |
915e6d16 | 2258 | goto finish; |
763a260a | 2259 | } |
915e6d16 | 2260 | |
07ce7407 TM |
2261 | if (decrypted_image) { |
2262 | r = decrypted_image_relinquish(decrypted_image); | |
763a260a YW |
2263 | if (r < 0) { |
2264 | log_debug_errno(r, "Failed to relinquish decrypted image: %m"); | |
07ce7407 | 2265 | goto finish; |
763a260a | 2266 | } |
07ce7407 | 2267 | } |
78ebe980 | 2268 | |
915e6d16 LP |
2269 | loop_device_relinquish(loop_device); |
2270 | ||
2271 | } else if (root_directory) { | |
2272 | ||
e908468b LP |
2273 | /* A root directory is specified. Turn its directory into bind mount, if it isn't one yet. */ |
2274 | r = path_is_mount_point(root, NULL, AT_SYMLINK_FOLLOW); | |
763a260a YW |
2275 | if (r < 0) { |
2276 | log_debug_errno(r, "Failed to detect that %s is a mount point or not: %m", root); | |
d944dc95 | 2277 | goto finish; |
763a260a | 2278 | } |
8f1ad200 | 2279 | if (r == 0) { |
21935150 LP |
2280 | r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL); |
2281 | if (r < 0) | |
8f1ad200 | 2282 | goto finish; |
d944dc95 | 2283 | } |
e908468b | 2284 | |
9b68367b | 2285 | } else { |
e908468b | 2286 | /* Let's mount the main root directory to the root directory to use */ |
21935150 LP |
2287 | r = mount_nofollow_verbose(LOG_DEBUG, "/", root, NULL, MS_BIND|MS_REC, NULL); |
2288 | if (r < 0) | |
e908468b | 2289 | goto finish; |
ee818b89 | 2290 | } |
c2c13f2d | 2291 | |
4e0c20de LP |
2292 | /* Try to set up the new root directory before mounting anything else there. */ |
2293 | if (root_image || root_directory) | |
2294 | (void) base_filesystem_create(root, UID_INVALID, GID_INVALID); | |
2295 | ||
82fb2da2 | 2296 | /* Now make the magic happen */ |
df61e79a | 2297 | r = apply_mounts(root, ns_info, mounts, &n_mounts, exec_dir_symlinks, error_path); |
82fb2da2 LB |
2298 | if (r < 0) |
2299 | goto finish; | |
15ae422b | 2300 | |
9b68367b YW |
2301 | /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */ |
2302 | r = mount_move_root(root); | |
763a260a YW |
2303 | if (r < 0) { |
2304 | log_debug_errno(r, "Failed to mount root with MS_MOVE: %m"); | |
9b68367b | 2305 | goto finish; |
763a260a | 2306 | } |
ee818b89 | 2307 | |
55fe7432 | 2308 | /* Remount / as the desired mode. Note that this will not |
c2c13f2d LP |
2309 | * reestablish propagation from our side to the host, since |
2310 | * what's disconnected is disconnected. */ | |
d944dc95 | 2311 | if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) { |
763a260a | 2312 | r = log_debug_errno(errno, "Failed to remount '/' with desired mount flags: %m"); |
d944dc95 LP |
2313 | goto finish; |
2314 | } | |
15ae422b | 2315 | |
5e8deb94 LB |
2316 | /* bind_mount_in_namespace() will MS_MOVE into that directory, and that's only |
2317 | * supported for non-shared mounts. This needs to happen after remounting / or it will fail. */ | |
2318 | if (setup_propagate) { | |
2319 | r = mount(NULL, incoming_dir, NULL, MS_SLAVE, NULL); | |
2320 | if (r < 0) { | |
2321 | log_error_errno(r, "Failed to remount %s with MS_SLAVE: %m", incoming_dir); | |
2322 | goto finish; | |
2323 | } | |
2324 | } | |
2325 | ||
d944dc95 | 2326 | r = 0; |
15ae422b | 2327 | |
d944dc95 | 2328 | finish: |
0cd41757 LP |
2329 | if (n_mounts > 0) |
2330 | for (m = mounts; m < mounts + n_mounts; m++) | |
2331 | mount_entry_done(m); | |
613b411c | 2332 | |
5f7a690a LP |
2333 | free(mounts); |
2334 | ||
613b411c LP |
2335 | return r; |
2336 | } | |
2337 | ||
da6053d0 | 2338 | void bind_mount_free_many(BindMount *b, size_t n) { |
d2d6c096 LP |
2339 | assert(b || n == 0); |
2340 | ||
fe96c0f8 | 2341 | for (size_t i = 0; i < n; i++) { |
d2d6c096 LP |
2342 | free(b[i].source); |
2343 | free(b[i].destination); | |
2344 | } | |
2345 | ||
2346 | free(b); | |
2347 | } | |
2348 | ||
da6053d0 | 2349 | int bind_mount_add(BindMount **b, size_t *n, const BindMount *item) { |
d2d6c096 LP |
2350 | _cleanup_free_ char *s = NULL, *d = NULL; |
2351 | BindMount *c; | |
2352 | ||
2353 | assert(b); | |
2354 | assert(n); | |
2355 | assert(item); | |
2356 | ||
2357 | s = strdup(item->source); | |
2358 | if (!s) | |
2359 | return -ENOMEM; | |
2360 | ||
2361 | d = strdup(item->destination); | |
2362 | if (!d) | |
2363 | return -ENOMEM; | |
2364 | ||
aa484f35 | 2365 | c = reallocarray(*b, *n + 1, sizeof(BindMount)); |
d2d6c096 LP |
2366 | if (!c) |
2367 | return -ENOMEM; | |
2368 | ||
2369 | *b = c; | |
2370 | ||
2371 | c[(*n) ++] = (BindMount) { | |
1cc6c93a YW |
2372 | .source = TAKE_PTR(s), |
2373 | .destination = TAKE_PTR(d), | |
d2d6c096 | 2374 | .read_only = item->read_only, |
9ce4e4b0 | 2375 | .nosuid = item->nosuid, |
d2d6c096 LP |
2376 | .recursive = item->recursive, |
2377 | .ignore_enoent = item->ignore_enoent, | |
2378 | }; | |
2379 | ||
d2d6c096 LP |
2380 | return 0; |
2381 | } | |
2382 | ||
b3d13314 | 2383 | MountImage* mount_image_free_many(MountImage *m, size_t *n) { |
b3d13314 LB |
2384 | assert(n); |
2385 | assert(m || *n == 0); | |
2386 | ||
fe96c0f8 | 2387 | for (size_t i = 0; i < *n; i++) { |
b3d13314 LB |
2388 | free(m[i].source); |
2389 | free(m[i].destination); | |
427353f6 | 2390 | mount_options_free_all(m[i].mount_options); |
b3d13314 LB |
2391 | } |
2392 | ||
2393 | free(m); | |
2394 | *n = 0; | |
2395 | return NULL; | |
2396 | } | |
2397 | ||
2398 | int mount_image_add(MountImage **m, size_t *n, const MountImage *item) { | |
2399 | _cleanup_free_ char *s = NULL, *d = NULL; | |
427353f6 LB |
2400 | _cleanup_(mount_options_free_allp) MountOptions *options = NULL; |
2401 | MountOptions *i; | |
b3d13314 LB |
2402 | MountImage *c; |
2403 | ||
2404 | assert(m); | |
2405 | assert(n); | |
2406 | assert(item); | |
2407 | ||
2408 | s = strdup(item->source); | |
2409 | if (!s) | |
2410 | return -ENOMEM; | |
2411 | ||
93f59701 LB |
2412 | if (item->destination) { |
2413 | d = strdup(item->destination); | |
2414 | if (!d) | |
2415 | return -ENOMEM; | |
2416 | } | |
b3d13314 | 2417 | |
427353f6 | 2418 | LIST_FOREACH(mount_options, i, item->mount_options) { |
c2b2df60 | 2419 | _cleanup_(mount_options_free_allp) MountOptions *o = NULL; |
427353f6 LB |
2420 | |
2421 | o = new(MountOptions, 1); | |
2422 | if (!o) | |
2423 | return -ENOMEM; | |
2424 | ||
2425 | *o = (MountOptions) { | |
2426 | .partition_designator = i->partition_designator, | |
2427 | .options = strdup(i->options), | |
2428 | }; | |
2429 | if (!o->options) | |
2430 | return -ENOMEM; | |
2431 | ||
2432 | LIST_APPEND(mount_options, options, TAKE_PTR(o)); | |
2433 | } | |
2434 | ||
b3d13314 LB |
2435 | c = reallocarray(*m, *n + 1, sizeof(MountImage)); |
2436 | if (!c) | |
2437 | return -ENOMEM; | |
2438 | ||
2439 | *m = c; | |
2440 | ||
2441 | c[(*n) ++] = (MountImage) { | |
2442 | .source = TAKE_PTR(s), | |
2443 | .destination = TAKE_PTR(d), | |
427353f6 | 2444 | .mount_options = TAKE_PTR(options), |
b3d13314 | 2445 | .ignore_enoent = item->ignore_enoent, |
93f59701 | 2446 | .type = item->type, |
b3d13314 LB |
2447 | }; |
2448 | ||
2449 | return 0; | |
2450 | } | |
2451 | ||
da6053d0 | 2452 | void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n) { |
2abd4e38 YW |
2453 | assert(t || n == 0); |
2454 | ||
fe96c0f8 | 2455 | for (size_t i = 0; i < n; i++) { |
2abd4e38 YW |
2456 | free(t[i].path); |
2457 | free(t[i].options); | |
2458 | } | |
2459 | ||
2460 | free(t); | |
2461 | } | |
2462 | ||
2463 | int temporary_filesystem_add( | |
2464 | TemporaryFileSystem **t, | |
da6053d0 | 2465 | size_t *n, |
2abd4e38 YW |
2466 | const char *path, |
2467 | const char *options) { | |
2468 | ||
2469 | _cleanup_free_ char *p = NULL, *o = NULL; | |
2470 | TemporaryFileSystem *c; | |
2471 | ||
2472 | assert(t); | |
2473 | assert(n); | |
2474 | assert(path); | |
2475 | ||
2476 | p = strdup(path); | |
2477 | if (!p) | |
2478 | return -ENOMEM; | |
2479 | ||
2480 | if (!isempty(options)) { | |
2481 | o = strdup(options); | |
2482 | if (!o) | |
2483 | return -ENOMEM; | |
2484 | } | |
2485 | ||
aa484f35 | 2486 | c = reallocarray(*t, *n + 1, sizeof(TemporaryFileSystem)); |
2abd4e38 YW |
2487 | if (!c) |
2488 | return -ENOMEM; | |
2489 | ||
2490 | *t = c; | |
2491 | ||
2492 | c[(*n) ++] = (TemporaryFileSystem) { | |
1cc6c93a YW |
2493 | .path = TAKE_PTR(p), |
2494 | .options = TAKE_PTR(o), | |
2abd4e38 YW |
2495 | }; |
2496 | ||
2abd4e38 YW |
2497 | return 0; |
2498 | } | |
2499 | ||
a652f050 JR |
2500 | static int make_tmp_prefix(const char *prefix) { |
2501 | _cleanup_free_ char *t = NULL; | |
96603ea0 | 2502 | _cleanup_close_ int fd = -1; |
a652f050 JR |
2503 | int r; |
2504 | ||
2505 | /* Don't do anything unless we know the dir is actually missing */ | |
2506 | r = access(prefix, F_OK); | |
2507 | if (r >= 0) | |
2508 | return 0; | |
2509 | if (errno != ENOENT) | |
2510 | return -errno; | |
2511 | ||
30443439 LP |
2512 | RUN_WITH_UMASK(000) |
2513 | r = mkdir_parents(prefix, 0755); | |
a652f050 JR |
2514 | if (r < 0) |
2515 | return r; | |
2516 | ||
2517 | r = tempfn_random(prefix, NULL, &t); | |
2518 | if (r < 0) | |
2519 | return r; | |
2520 | ||
96603ea0 LP |
2521 | /* umask will corrupt this access mode, but that doesn't matter, we need to call chmod() anyway for |
2522 | * the suid bit, below. */ | |
2523 | fd = open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0777); | |
2524 | if (fd < 0) | |
2525 | return fd; | |
a652f050 | 2526 | |
96603ea0 LP |
2527 | r = RET_NERRNO(fchmod(fd, 01777)); |
2528 | if (r < 0) { | |
a652f050 JR |
2529 | (void) rmdir(t); |
2530 | return r; | |
2531 | } | |
2532 | ||
96603ea0 LP |
2533 | r = RET_NERRNO(rename(t, prefix)); |
2534 | if (r < 0) { | |
a652f050 JR |
2535 | (void) rmdir(t); |
2536 | return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */ | |
2537 | } | |
2538 | ||
2539 | return 0; | |
2540 | ||
2541 | } | |
2542 | ||
56a13a49 | 2543 | static int setup_one_tmp_dir(const char *id, const char *prefix, char **path, char **tmp_path) { |
613b411c | 2544 | _cleanup_free_ char *x = NULL; |
19cd4e19 | 2545 | _cleanup_free_ char *y = NULL; |
6b46ea73 | 2546 | sd_id128_t boot_id; |
56a13a49 | 2547 | bool rw = true; |
6b46ea73 | 2548 | int r; |
613b411c LP |
2549 | |
2550 | assert(id); | |
2551 | assert(prefix); | |
2552 | assert(path); | |
2553 | ||
6b46ea73 LP |
2554 | /* We include the boot id in the directory so that after a |
2555 | * reboot we can easily identify obsolete directories. */ | |
2556 | ||
2557 | r = sd_id128_get_boot(&boot_id); | |
2558 | if (r < 0) | |
2559 | return r; | |
2560 | ||
85b55869 | 2561 | x = strjoin(prefix, "/systemd-private-", SD_ID128_TO_STRING(boot_id), "-", id, "-XXXXXX"); |
613b411c LP |
2562 | if (!x) |
2563 | return -ENOMEM; | |
2564 | ||
a652f050 JR |
2565 | r = make_tmp_prefix(prefix); |
2566 | if (r < 0) | |
2567 | return r; | |
2568 | ||
613b411c | 2569 | RUN_WITH_UMASK(0077) |
56a13a49 ZJS |
2570 | if (!mkdtemp(x)) { |
2571 | if (errno == EROFS || ERRNO_IS_DISK_SPACE(errno)) | |
2572 | rw = false; | |
2573 | else | |
2574 | return -errno; | |
2575 | } | |
613b411c | 2576 | |
56a13a49 | 2577 | if (rw) { |
19cd4e19 | 2578 | y = strjoin(x, "/tmp"); |
2579 | if (!y) | |
2580 | return -ENOMEM; | |
2581 | ||
30443439 | 2582 | RUN_WITH_UMASK(0000) |
19cd4e19 | 2583 | if (mkdir(y, 0777 | S_ISVTX) < 0) |
2584 | return -errno; | |
19cd4e19 | 2585 | |
2586 | r = label_fix_container(y, prefix, 0); | |
56a13a49 ZJS |
2587 | if (r < 0) |
2588 | return r; | |
19cd4e19 | 2589 | |
2590 | if (tmp_path) | |
2591 | *tmp_path = TAKE_PTR(y); | |
56a13a49 ZJS |
2592 | } else { |
2593 | /* Trouble: we failed to create the directory. Instead of failing, let's simulate /tmp being | |
2594 | * read-only. This way the service will get the EROFS result as if it was writing to the real | |
2595 | * file system. */ | |
30443439 LP |
2596 | RUN_WITH_UMASK(0000) |
2597 | r = mkdir_p(RUN_SYSTEMD_EMPTY, 0500); | |
56a13a49 ZJS |
2598 | if (r < 0) |
2599 | return r; | |
613b411c | 2600 | |
3f181262 LP |
2601 | r = free_and_strdup(&x, RUN_SYSTEMD_EMPTY); |
2602 | if (r < 0) | |
2603 | return r; | |
c17ec25e | 2604 | } |
15ae422b | 2605 | |
1cc6c93a | 2606 | *path = TAKE_PTR(x); |
613b411c LP |
2607 | return 0; |
2608 | } | |
2609 | ||
2610 | int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) { | |
56a13a49 ZJS |
2611 | _cleanup_(namespace_cleanup_tmpdirp) char *a = NULL; |
2612 | _cleanup_(rmdir_and_freep) char *a_tmp = NULL; | |
2613 | char *b; | |
613b411c LP |
2614 | int r; |
2615 | ||
2616 | assert(id); | |
2617 | assert(tmp_dir); | |
2618 | assert(var_tmp_dir); | |
2619 | ||
56a13a49 | 2620 | r = setup_one_tmp_dir(id, "/tmp", &a, &a_tmp); |
613b411c LP |
2621 | if (r < 0) |
2622 | return r; | |
2623 | ||
56a13a49 ZJS |
2624 | r = setup_one_tmp_dir(id, "/var/tmp", &b, NULL); |
2625 | if (r < 0) | |
613b411c | 2626 | return r; |
613b411c | 2627 | |
56a13a49 ZJS |
2628 | a_tmp = mfree(a_tmp); /* avoid rmdir */ |
2629 | *tmp_dir = TAKE_PTR(a); | |
2630 | *var_tmp_dir = TAKE_PTR(b); | |
613b411c LP |
2631 | |
2632 | return 0; | |
2633 | } | |
2634 | ||
54c2459d XR |
2635 | int setup_shareable_ns(const int ns_storage_socket[static 2], unsigned long nsflag) { |
2636 | _cleanup_close_ int ns = -1; | |
3ee897d6 | 2637 | int r, q; |
54c2459d | 2638 | const char *ns_name, *ns_path; |
613b411c | 2639 | |
54c2459d XR |
2640 | assert(ns_storage_socket); |
2641 | assert(ns_storage_socket[0] >= 0); | |
2642 | assert(ns_storage_socket[1] >= 0); | |
2643 | ||
2644 | ns_name = namespace_single_flag_to_string(nsflag); | |
2645 | assert(ns_name); | |
613b411c LP |
2646 | |
2647 | /* We use the passed socketpair as a storage buffer for our | |
76cd584b LP |
2648 | * namespace reference fd. Whatever process runs this first |
2649 | * shall create a new namespace, all others should just join | |
2650 | * it. To serialize that we use a file lock on the socket | |
2651 | * pair. | |
613b411c LP |
2652 | * |
2653 | * It's a bit crazy, but hey, works great! */ | |
2654 | ||
54c2459d | 2655 | if (lockf(ns_storage_socket[0], F_LOCK, 0) < 0) |
613b411c LP |
2656 | return -errno; |
2657 | ||
54c2459d XR |
2658 | ns = receive_one_fd(ns_storage_socket[0], MSG_DONTWAIT); |
2659 | if (ns == -EAGAIN) { | |
44ffcbae | 2660 | /* Nothing stored yet, so let's create a new namespace. */ |
613b411c | 2661 | |
54c2459d | 2662 | if (unshare(nsflag) < 0) { |
613b411c LP |
2663 | r = -errno; |
2664 | goto fail; | |
2665 | } | |
2666 | ||
44ffcbae | 2667 | (void) loopback_setup(); |
613b411c | 2668 | |
54c2459d XR |
2669 | ns_path = strjoina("/proc/self/ns/", ns_name); |
2670 | ns = open(ns_path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
2671 | if (ns < 0) { | |
613b411c LP |
2672 | r = -errno; |
2673 | goto fail; | |
2674 | } | |
2675 | ||
2676 | r = 1; | |
613b411c | 2677 | |
54c2459d XR |
2678 | } else if (ns < 0) { |
2679 | r = ns; | |
3ee897d6 | 2680 | goto fail; |
613b411c | 2681 | |
3ee897d6 LP |
2682 | } else { |
2683 | /* Yay, found something, so let's join the namespace */ | |
54c2459d | 2684 | if (setns(ns, nsflag) < 0) { |
613b411c LP |
2685 | r = -errno; |
2686 | goto fail; | |
2687 | } | |
2688 | ||
2689 | r = 0; | |
2690 | } | |
2691 | ||
54c2459d | 2692 | q = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT); |
3ee897d6 LP |
2693 | if (q < 0) { |
2694 | r = q; | |
613b411c LP |
2695 | goto fail; |
2696 | } | |
2697 | ||
2698 | fail: | |
54c2459d | 2699 | (void) lockf(ns_storage_socket[0], F_ULOCK, 0); |
15ae422b LP |
2700 | return r; |
2701 | } | |
417116f2 | 2702 | |
54c2459d XR |
2703 | int open_shareable_ns_path(const int ns_storage_socket[static 2], const char *path, unsigned long nsflag) { |
2704 | _cleanup_close_ int ns = -1; | |
51af7fb2 LP |
2705 | int q, r; |
2706 | ||
54c2459d XR |
2707 | assert(ns_storage_socket); |
2708 | assert(ns_storage_socket[0] >= 0); | |
2709 | assert(ns_storage_socket[1] >= 0); | |
51af7fb2 LP |
2710 | assert(path); |
2711 | ||
54c2459d XR |
2712 | /* If the storage socket doesn't contain a ns fd yet, open one via the file system and store it in |
2713 | * it. This is supposed to be called ahead of time, i.e. before setup_shareable_ns() which will | |
2714 | * allocate a new anonymous ns if needed. */ | |
51af7fb2 | 2715 | |
54c2459d | 2716 | if (lockf(ns_storage_socket[0], F_LOCK, 0) < 0) |
51af7fb2 LP |
2717 | return -errno; |
2718 | ||
54c2459d XR |
2719 | ns = receive_one_fd(ns_storage_socket[0], MSG_DONTWAIT); |
2720 | if (ns == -EAGAIN) { | |
51af7fb2 LP |
2721 | /* Nothing stored yet. Open the file from the file system. */ |
2722 | ||
54c2459d XR |
2723 | ns = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC); |
2724 | if (ns < 0) { | |
51af7fb2 LP |
2725 | r = -errno; |
2726 | goto fail; | |
2727 | } | |
2728 | ||
54c2459d XR |
2729 | r = fd_is_ns(ns, nsflag); |
2730 | if (r == 0) { /* Not a ns of our type? Refuse early. */ | |
51af7fb2 LP |
2731 | r = -EINVAL; |
2732 | goto fail; | |
2733 | } | |
2734 | if (r < 0 && r != -EUCLEAN) /* EUCLEAN: we don't know */ | |
2735 | goto fail; | |
2736 | ||
2737 | r = 1; | |
2738 | ||
54c2459d XR |
2739 | } else if (ns < 0) { |
2740 | r = ns; | |
51af7fb2 LP |
2741 | goto fail; |
2742 | } else | |
2743 | r = 0; /* Already allocated */ | |
2744 | ||
54c2459d | 2745 | q = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT); |
51af7fb2 LP |
2746 | if (q < 0) { |
2747 | r = q; | |
2748 | goto fail; | |
2749 | } | |
2750 | ||
2751 | fail: | |
54c2459d | 2752 | (void) lockf(ns_storage_socket[0], F_ULOCK, 0); |
51af7fb2 LP |
2753 | return r; |
2754 | } | |
2755 | ||
6e2d7c4f MS |
2756 | bool ns_type_supported(NamespaceType type) { |
2757 | const char *t, *ns_proc; | |
2758 | ||
0fa5b831 LP |
2759 | t = namespace_type_to_string(type); |
2760 | if (!t) /* Don't know how to translate this? Then it's not supported */ | |
6e2d7c4f MS |
2761 | return false; |
2762 | ||
6e2d7c4f | 2763 | ns_proc = strjoina("/proc/self/ns/", t); |
6e2d7c4f MS |
2764 | return access(ns_proc, F_OK) == 0; |
2765 | } | |
2766 | ||
1b8689f9 | 2767 | static const char *const protect_home_table[_PROTECT_HOME_MAX] = { |
cbc056c8 ZJS |
2768 | [PROTECT_HOME_NO] = "no", |
2769 | [PROTECT_HOME_YES] = "yes", | |
1b8689f9 | 2770 | [PROTECT_HOME_READ_ONLY] = "read-only", |
cbc056c8 | 2771 | [PROTECT_HOME_TMPFS] = "tmpfs", |
417116f2 LP |
2772 | }; |
2773 | ||
1e8c7bd5 | 2774 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_home, ProtectHome, PROTECT_HOME_YES); |
5e1c6154 | 2775 | |
1b8689f9 | 2776 | static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = { |
cbc056c8 ZJS |
2777 | [PROTECT_SYSTEM_NO] = "no", |
2778 | [PROTECT_SYSTEM_YES] = "yes", | |
2779 | [PROTECT_SYSTEM_FULL] = "full", | |
3f815163 | 2780 | [PROTECT_SYSTEM_STRICT] = "strict", |
1b8689f9 LP |
2781 | }; |
2782 | ||
1e8c7bd5 | 2783 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_system, ProtectSystem, PROTECT_SYSTEM_YES); |
03c791aa | 2784 | |
6e2d7c4f | 2785 | static const char* const namespace_type_table[] = { |
cbc056c8 | 2786 | [NAMESPACE_MOUNT] = "mnt", |
6e2d7c4f | 2787 | [NAMESPACE_CGROUP] = "cgroup", |
cbc056c8 ZJS |
2788 | [NAMESPACE_UTS] = "uts", |
2789 | [NAMESPACE_IPC] = "ipc", | |
2790 | [NAMESPACE_USER] = "user", | |
2791 | [NAMESPACE_PID] = "pid", | |
2792 | [NAMESPACE_NET] = "net", | |
6e2d7c4f MS |
2793 | }; |
2794 | ||
2795 | DEFINE_STRING_TABLE_LOOKUP(namespace_type, NamespaceType); | |
4e399953 LP |
2796 | |
2797 | static const char* const protect_proc_table[_PROTECT_PROC_MAX] = { | |
2798 | [PROTECT_PROC_DEFAULT] = "default", | |
2799 | [PROTECT_PROC_NOACCESS] = "noaccess", | |
2800 | [PROTECT_PROC_INVISIBLE] = "invisible", | |
2801 | [PROTECT_PROC_PTRACEABLE] = "ptraceable", | |
2802 | }; | |
2803 | ||
2804 | DEFINE_STRING_TABLE_LOOKUP(protect_proc, ProtectProc); | |
2805 | ||
2806 | static const char* const proc_subset_table[_PROC_SUBSET_MAX] = { | |
2807 | [PROC_SUBSET_ALL] = "all", | |
2808 | [PROC_SUBSET_PID] = "pid", | |
2809 | }; | |
2810 | ||
2811 | DEFINE_STRING_TABLE_LOOKUP(proc_subset, ProcSubset); |