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