]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
Merge pull request #14388 from anitazha/man_uid_updates
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/mount.h>
4 #include <linux/magic.h>
5
6 #include "alloc-util.h"
7 #include "escape.h"
8 #include "fd-util.h"
9 #include "format-util.h"
10 #include "fs-util.h"
11 #include "label.h"
12 #include "mkdir.h"
13 #include "mount-util.h"
14 #include "mountpoint-util.h"
15 #include "nspawn-mount.h"
16 #include "parse-util.h"
17 #include "path-util.h"
18 #include "rm-rf.h"
19 #include "set.h"
20 #include "sort-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "tmpfile-util.h"
25 #include "user-util.h"
26
27 CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
28 CustomMount *c, *ret;
29
30 assert(l);
31 assert(n);
32 assert(t >= 0);
33 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
34
35 c = reallocarray(*l, *n + 1, sizeof(CustomMount));
36 if (!c)
37 return NULL;
38
39 *l = c;
40 ret = *l + *n;
41 (*n)++;
42
43 *ret = (CustomMount) { .type = t };
44
45 return ret;
46 }
47
48 void custom_mount_free_all(CustomMount *l, size_t n) {
49 size_t i;
50
51 for (i = 0; i < n; i++) {
52 CustomMount *m = l + i;
53
54 free(m->source);
55 free(m->destination);
56 free(m->options);
57
58 if (m->work_dir) {
59 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
60 free(m->work_dir);
61 }
62
63 if (m->rm_rf_tmpdir) {
64 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
65 free(m->rm_rf_tmpdir);
66 }
67
68 strv_free(m->lower);
69 free(m->type_argument);
70 }
71
72 free(l);
73 }
74
75 static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
76 int r;
77
78 r = path_compare(a->destination, b->destination);
79 if (r != 0)
80 return r;
81
82 return CMP(a->type, b->type);
83 }
84
85 static bool source_path_is_valid(const char *p) {
86 assert(p);
87
88 if (*p == '+')
89 p++;
90
91 return path_is_absolute(p);
92 }
93
94 static char *resolve_source_path(const char *dest, const char *source) {
95
96 if (!source)
97 return NULL;
98
99 if (source[0] == '+')
100 return path_join(dest, source + 1);
101
102 return strdup(source);
103 }
104
105 static int allocate_temporary_source(CustomMount *m) {
106 assert(m);
107 assert(!m->source);
108 assert(!m->rm_rf_tmpdir);
109
110 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
111 if (!m->rm_rf_tmpdir)
112 return log_oom();
113
114 if (!mkdtemp(m->rm_rf_tmpdir)) {
115 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
116 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
117 }
118
119 m->source = path_join(m->rm_rf_tmpdir, "src");
120 if (!m->source)
121 return log_oom();
122
123 if (mkdir(m->source, 0755) < 0)
124 return log_error_errno(errno, "Failed to create %s: %m", m->source);
125
126 return 0;
127 }
128
129 int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
130 size_t i;
131 int r;
132
133 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
134 * parent process, so that we know the temporary directories to remove on exit before we fork off the
135 * children. */
136
137 assert(l || n == 0);
138
139 /* Order the custom mounts, and make sure we have a working directory */
140 typesafe_qsort(l, n, custom_mount_compare);
141
142 for (i = 0; i < n; i++) {
143 CustomMount *m = l + i;
144
145 /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount
146 * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in
147 * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in
148 * the inner child, not the outer one. Determine this here. */
149 m->in_userns = path_startswith(m->destination, "/proc");
150
151 if (m->type == CUSTOM_MOUNT_BIND) {
152 if (m->source) {
153 char *s;
154
155 s = resolve_source_path(dest, m->source);
156 if (!s)
157 return log_oom();
158
159 free_and_replace(m->source, s);
160 } else {
161 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
162
163 r = allocate_temporary_source(m);
164 if (r < 0)
165 return r;
166 }
167 }
168
169 if (m->type == CUSTOM_MOUNT_OVERLAY) {
170 char **j;
171
172 STRV_FOREACH(j, m->lower) {
173 char *s;
174
175 s = resolve_source_path(dest, *j);
176 if (!s)
177 return log_oom();
178
179 free_and_replace(*j, s);
180 }
181
182 if (m->source) {
183 char *s;
184
185 s = resolve_source_path(dest, m->source);
186 if (!s)
187 return log_oom();
188
189 free_and_replace(m->source, s);
190 } else {
191 r = allocate_temporary_source(m);
192 if (r < 0)
193 return r;
194 }
195
196 if (m->work_dir) {
197 char *s;
198
199 s = resolve_source_path(dest, m->work_dir);
200 if (!s)
201 return log_oom();
202
203 free_and_replace(m->work_dir, s);
204 } else {
205 r = tempfn_random(m->source, NULL, &m->work_dir);
206 if (r < 0)
207 return log_error_errno(r, "Failed to acquire working directory: %m");
208 }
209
210 (void) mkdir_label(m->work_dir, 0700);
211 }
212 }
213
214 return 0;
215 }
216
217 int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
218 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
219 const char *p = s;
220 CustomMount *m;
221 int r;
222
223 assert(l);
224 assert(n);
225
226 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
227 if (r < 0)
228 return r;
229 if (r == 0)
230 return -EINVAL;
231 if (r == 1) {
232 destination = strdup(source[0] == '+' ? source+1 : source);
233 if (!destination)
234 return -ENOMEM;
235 }
236 if (r == 2 && !isempty(p)) {
237 opts = strdup(p);
238 if (!opts)
239 return -ENOMEM;
240 }
241
242 if (isempty(source))
243 source = mfree(source);
244 else if (!source_path_is_valid(source))
245 return -EINVAL;
246
247 if (!path_is_absolute(destination))
248 return -EINVAL;
249
250 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
251 if (!m)
252 return -ENOMEM;
253
254 m->source = TAKE_PTR(source);
255 m->destination = TAKE_PTR(destination);
256 m->read_only = read_only;
257 m->options = TAKE_PTR(opts);
258
259 return 0;
260 }
261
262 int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
263 _cleanup_free_ char *path = NULL, *opts = NULL;
264 const char *p = s;
265 CustomMount *m;
266 int r;
267
268 assert(l);
269 assert(n);
270 assert(s);
271
272 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
273 if (r < 0)
274 return r;
275 if (r == 0)
276 return -EINVAL;
277
278 if (isempty(p))
279 opts = strdup("mode=0755");
280 else
281 opts = strdup(p);
282 if (!opts)
283 return -ENOMEM;
284
285 if (!path_is_absolute(path))
286 return -EINVAL;
287
288 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
289 if (!m)
290 return -ENOMEM;
291
292 m->destination = TAKE_PTR(path);
293 m->options = TAKE_PTR(opts);
294
295 return 0;
296 }
297
298 int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
299 _cleanup_free_ char *upper = NULL, *destination = NULL;
300 _cleanup_strv_free_ char **lower = NULL;
301 CustomMount *m;
302 int k;
303
304 k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
305 if (k < 0)
306 return k;
307 if (k < 2)
308 return -EADDRNOTAVAIL;
309 if (k == 2) {
310 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
311 * we'll also define the destination mount point the same as the upper. */
312
313 if (!source_path_is_valid(lower[0]) ||
314 !source_path_is_valid(lower[1]))
315 return -EINVAL;
316
317 upper = TAKE_PTR(lower[1]);
318
319 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
320 if (!destination)
321 return -ENOMEM;
322 } else {
323 char **i;
324
325 /* If more than two parameters are specified, the last one is the destination, the second to last one
326 * the "upper", and all before that the "lower" directories. */
327
328 destination = lower[k - 1];
329 upper = TAKE_PTR(lower[k - 2]);
330
331 STRV_FOREACH(i, lower)
332 if (!source_path_is_valid(*i))
333 return -EINVAL;
334
335 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
336 * in /var/tmp */
337 if (isempty(upper))
338 upper = mfree(upper);
339 else if (!source_path_is_valid(upper))
340 return -EINVAL;
341
342 if (!path_is_absolute(destination))
343 return -EINVAL;
344 }
345
346 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
347 if (!m)
348 return -ENOMEM;
349
350 m->destination = TAKE_PTR(destination);
351 m->source = TAKE_PTR(upper);
352 m->lower = TAKE_PTR(lower);
353 m->read_only = read_only;
354
355 return 0;
356 }
357
358 int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
359 _cleanup_free_ char *path = NULL;
360 CustomMount *m;
361
362 assert(l);
363 assert(n);
364 assert(s);
365
366 if (!path_is_absolute(s))
367 return -EINVAL;
368
369 path = strdup(s);
370 if (!path)
371 return -ENOMEM;
372
373 m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
374 if (!m)
375 return -ENOMEM;
376
377 m->destination = TAKE_PTR(path);
378 return 0;
379 }
380
381 int tmpfs_patch_options(
382 const char *options,
383 uid_t uid_shift,
384 const char *selinux_apifs_context,
385 char **ret) {
386
387 char *buf = NULL;
388
389 if (uid_shift != UID_INVALID) {
390 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
391 strempty(options), options ? "," : "",
392 uid_shift, uid_shift) < 0)
393 return -ENOMEM;
394
395 options = buf;
396 }
397
398 #if HAVE_SELINUX
399 if (selinux_apifs_context) {
400 char *t;
401
402 t = strjoin(strempty(options), options ? "," : "",
403 "context=\"", selinux_apifs_context, "\"");
404 free(buf);
405 if (!t)
406 return -ENOMEM;
407
408 buf = t;
409 }
410 #endif
411
412 if (!buf && options) {
413 buf = strdup(options);
414 if (!buf)
415 return -ENOMEM;
416 }
417 *ret = buf;
418
419 return !!buf;
420 }
421
422 int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
423 const char *full, *top, *x;
424 int r;
425 unsigned long extra_flags = 0;
426
427 top = prefix_roota(dest, "/sys");
428 r = path_is_fs_type(top, SYSFS_MAGIC);
429 if (r < 0)
430 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
431 /* /sys might already be mounted as sysfs by the outer child in the
432 * !netns case. In this case, it's all good. Don't touch it because we
433 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
434 */
435 if (r > 0)
436 return 0;
437
438 full = prefix_roota(top, "/full");
439
440 (void) mkdir(full, 0755);
441
442 if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO))
443 extra_flags |= MS_RDONLY;
444
445 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
446 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
447 if (r < 0)
448 return r;
449
450 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
451 _cleanup_free_ char *from = NULL, *to = NULL;
452
453 from = path_join(full, x);
454 if (!from)
455 return log_oom();
456
457 to = path_join(top, x);
458 if (!to)
459 return log_oom();
460
461 (void) mkdir(to, 0755);
462
463 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
464 if (r < 0)
465 return r;
466
467 r = mount_verbose(LOG_ERR, NULL, to, NULL,
468 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
469 if (r < 0)
470 return r;
471 }
472
473 r = umount_verbose(full);
474 if (r < 0)
475 return r;
476
477 if (rmdir(full) < 0)
478 return log_error_errno(errno, "Failed to remove %s: %m", full);
479
480 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
481 * remount /sys read-only.
482 */
483 x = prefix_roota(top, "/fs/cgroup");
484 (void) mkdir_p(x, 0755);
485
486 return mount_verbose(LOG_ERR, NULL, top, NULL,
487 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
488 }
489
490 static int mkdir_userns(const char *path, mode_t mode, uid_t uid_shift) {
491 int r;
492
493 assert(path);
494
495 r = mkdir_errno_wrapper(path, mode);
496 if (r < 0 && r != -EEXIST)
497 return r;
498
499 if (uid_shift == UID_INVALID)
500 return 0;
501
502 if (lchown(path, uid_shift, uid_shift) < 0)
503 return -errno;
504
505 return 0;
506 }
507
508 static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, uid_t uid_shift) {
509 const char *p, *e;
510 int r;
511
512 assert(path);
513
514 if (prefix && !path_startswith(path, prefix))
515 return -ENOTDIR;
516
517 /* create every parent directory in the path, except the last component */
518 p = path + strspn(path, "/");
519 for (;;) {
520 char t[strlen(path) + 1];
521
522 e = p + strcspn(p, "/");
523 p = e + strspn(e, "/");
524
525 /* Is this the last component? If so, then we're done */
526 if (*p == 0)
527 break;
528
529 memcpy(t, path, e - path);
530 t[e-path] = 0;
531
532 if (prefix && path_startswith(prefix, t))
533 continue;
534
535 r = mkdir_userns(t, mode, uid_shift);
536 if (r < 0)
537 return r;
538 }
539
540 return mkdir_userns(path, mode, uid_shift);
541 }
542
543 int mount_all(const char *dest,
544 MountSettingsMask mount_settings,
545 uid_t uid_shift,
546 const char *selinux_apifs_context) {
547
548 #define PROC_INACCESSIBLE_REG(path) \
549 { "/run/systemd/inaccessible/reg", (path), NULL, NULL, MS_BIND, \
550 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
551 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
552 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
553
554 #define PROC_READ_ONLY(path) \
555 { (path), (path), NULL, NULL, MS_BIND, \
556 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
557 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
558 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
559
560 typedef struct MountPoint {
561 const char *what;
562 const char *where;
563 const char *type;
564 const char *options;
565 unsigned long flags;
566 MountSettingsMask mount_settings;
567 } MountPoint;
568
569 static const MountPoint mount_table[] = {
570 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
571 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
572 MOUNT_FATAL|MOUNT_IN_USERNS },
573
574 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND,
575 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
576
577 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND,
578 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
579
580 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
581 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
582
583 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
584 * internals or the host's execution environment to the container */
585 PROC_INACCESSIBLE_REG("/proc/kallsyms"),
586 PROC_INACCESSIBLE_REG("/proc/kcore"),
587 PROC_INACCESSIBLE_REG("/proc/keys"),
588 PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"),
589 PROC_INACCESSIBLE_REG("/proc/timer_list"),
590
591 /* Make these directories read-only to container payloads: they show hardware information, and in some
592 * cases contain tunables the container really shouldn't have access to. */
593 PROC_READ_ONLY("/proc/acpi"),
594 PROC_READ_ONLY("/proc/apm"),
595 PROC_READ_ONLY("/proc/asound"),
596 PROC_READ_ONLY("/proc/bus"),
597 PROC_READ_ONLY("/proc/fs"),
598 PROC_READ_ONLY("/proc/irq"),
599 PROC_READ_ONLY("/proc/scsi"),
600
601 { "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
602 MOUNT_IN_USERNS },
603
604 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
605 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
606 MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP },
607 { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID|MS_NOEXEC|MS_NODEV,
608 MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS },
609 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV,
610 MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */
611 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
612 MOUNT_FATAL }, /* skipped if above was mounted */
613 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME,
614 MOUNT_FATAL },
615 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
616 MOUNT_FATAL },
617 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
618 MOUNT_FATAL },
619
620 #if HAVE_SELINUX
621 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND,
622 0 }, /* Bind mount first */
623 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
624 0 }, /* Then, make it r/o */
625 #endif
626 };
627
628 bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS);
629 bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS);
630 bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO);
631 bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS);
632 bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP);
633 size_t k;
634 int r;
635
636 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
637 _cleanup_free_ char *where = NULL, *options = NULL;
638 const char *o;
639 bool fatal = FLAGS_SET(mount_table[k].mount_settings, MOUNT_FATAL);
640
641 if (in_userns != FLAGS_SET(mount_table[k].mount_settings, MOUNT_IN_USERNS))
642 continue;
643
644 if (!netns && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_NETNS))
645 continue;
646
647 if (!ro && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_RO))
648 continue;
649
650 if (!tmpfs_tmp && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_TMPFS_TMP))
651 continue;
652
653 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
654 if (r < 0)
655 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
656
657 /* Skip this entry if it is not a remount. */
658 if (mount_table[k].what) {
659 r = path_is_mount_point(where, NULL, 0);
660 if (r < 0 && r != -ENOENT)
661 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
662 if (r > 0)
663 continue;
664 }
665
666 r = mkdir_userns_p(dest, where, 0755, (use_userns && !in_userns) ? uid_shift : UID_INVALID);
667 if (r < 0 && r != -EEXIST) {
668 if (fatal && r != -EROFS)
669 return log_error_errno(r, "Failed to create directory %s: %m", where);
670
671 log_debug_errno(r, "Failed to create directory %s: %m", where);
672 /* If we failed mkdir() or chown() due to the root
673 * directory being read only, attempt to mount this fs
674 * anyway and let mount_verbose log any errors */
675 if (r != -EROFS)
676 continue;
677 }
678
679 o = mount_table[k].options;
680 if (streq_ptr(mount_table[k].type, "tmpfs")) {
681 r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
682 if (r < 0)
683 return log_oom();
684 if (r > 0)
685 o = options;
686 }
687
688 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
689 mount_table[k].what,
690 where,
691 mount_table[k].type,
692 mount_table[k].flags,
693 o);
694 if (r < 0 && fatal)
695 return r;
696 }
697
698 return 0;
699 }
700
701 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
702 const char *p = options;
703 unsigned long flags = *mount_flags;
704 char *opts = NULL;
705 int r;
706
707 assert(options);
708
709 for (;;) {
710 _cleanup_free_ char *word = NULL;
711
712 r = extract_first_word(&p, &word, ",", 0);
713 if (r < 0)
714 return log_error_errno(r, "Failed to extract mount option: %m");
715 if (r == 0)
716 break;
717
718 if (streq(word, "rbind"))
719 flags |= MS_REC;
720 else if (streq(word, "norbind"))
721 flags &= ~MS_REC;
722 else {
723 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
724 "Invalid bind mount option: %s",
725 word);
726 }
727 }
728
729 *mount_flags = flags;
730 /* in the future mount_opts will hold string options for mount(2) */
731 *mount_opts = opts;
732
733 return 0;
734 }
735
736 static int mount_bind(const char *dest, CustomMount *m) {
737 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
738 unsigned long mount_flags = MS_BIND | MS_REC;
739 struct stat source_st, dest_st;
740 int r;
741
742 assert(dest);
743 assert(m);
744
745 if (m->options) {
746 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
747 if (r < 0)
748 return r;
749 }
750
751 if (stat(m->source, &source_st) < 0)
752 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
753
754 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
755 if (r < 0)
756 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
757 if (r > 0) { /* Path exists already? */
758
759 if (stat(where, &dest_st) < 0)
760 return log_error_errno(errno, "Failed to stat %s: %m", where);
761
762 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
763 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
764 "Cannot bind mount directory %s on file %s.",
765 m->source, where);
766
767 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
768 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
769 "Cannot bind mount file %s on directory %s.",
770 m->source, where);
771
772 } else { /* Path doesn't exist yet? */
773 r = mkdir_parents_label(where, 0755);
774 if (r < 0)
775 return log_error_errno(r, "Failed to make parents of %s: %m", where);
776
777 /* Create the mount point. Any non-directory file can be
778 * mounted on any non-directory file (regular, fifo, socket,
779 * char, block).
780 */
781 if (S_ISDIR(source_st.st_mode))
782 r = mkdir_label(where, 0755);
783 else
784 r = touch(where);
785 if (r < 0)
786 return log_error_errno(r, "Failed to create mount point %s: %m", where);
787 }
788
789 r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
790 if (r < 0)
791 return r;
792
793 if (m->read_only) {
794 r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
795 if (r < 0)
796 return log_error_errno(r, "Read-only bind mount failed: %m");
797 }
798
799 return 0;
800 }
801
802 static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) {
803
804 const char *options;
805 _cleanup_free_ char *buf = NULL, *where = NULL;
806 int r;
807
808 assert(dest);
809 assert(m);
810
811 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
812 if (r < 0)
813 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
814 if (r == 0) { /* Doesn't exist yet? */
815 r = mkdir_p_label(where, 0755);
816 if (r < 0)
817 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
818 }
819
820 r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
821 if (r < 0)
822 return log_oom();
823 options = r > 0 ? buf : m->options;
824
825 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
826 }
827
828 static char *joined_and_escaped_lower_dirs(char **lower) {
829 _cleanup_strv_free_ char **sv = NULL;
830
831 sv = strv_copy(lower);
832 if (!sv)
833 return NULL;
834
835 strv_reverse(sv);
836
837 if (!strv_shell_escape(sv, ",:"))
838 return NULL;
839
840 return strv_join(sv, ":");
841 }
842
843 static int mount_overlay(const char *dest, CustomMount *m) {
844 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
845 const char *options;
846 int r;
847
848 assert(dest);
849 assert(m);
850
851 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
852 if (r < 0)
853 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
854 if (r == 0) { /* Doesn't exist yet? */
855 r = mkdir_label(where, 0755);
856 if (r < 0)
857 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
858 }
859
860 (void) mkdir_p_label(m->source, 0755);
861
862 lower = joined_and_escaped_lower_dirs(m->lower);
863 if (!lower)
864 return log_oom();
865
866 escaped_source = shell_escape(m->source, ",:");
867 if (!escaped_source)
868 return log_oom();
869
870 if (m->read_only)
871 options = strjoina("lowerdir=", escaped_source, ":", lower);
872 else {
873 _cleanup_free_ char *escaped_work_dir = NULL;
874
875 escaped_work_dir = shell_escape(m->work_dir, ",:");
876 if (!escaped_work_dir)
877 return log_oom();
878
879 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
880 }
881
882 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
883 }
884
885 static int mount_inaccessible(const char *dest, CustomMount *m) {
886 _cleanup_free_ char *where = NULL, *source = NULL;
887 struct stat st;
888 int r;
889
890 assert(dest);
891 assert(m);
892
893 r = chase_symlinks_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st, NULL);
894 if (r < 0) {
895 log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
896 return m->graceful ? 0 : r;
897 }
898
899 r = mode_to_inaccessible_node("/run/systemd", st.st_mode, &source);
900 if (r < 0)
901 return m->graceful ? 0 : r;
902
903 r = mount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
904 if (r < 0)
905 return m->graceful ? 0 : r;
906
907 r = mount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
908 if (r < 0) {
909 umount_verbose(where);
910 return m->graceful ? 0 : r;
911 }
912
913 return 0;
914 }
915
916 static int mount_arbitrary(const char *dest, CustomMount *m) {
917 _cleanup_free_ char *where = NULL;
918 int r;
919
920 assert(dest);
921 assert(m);
922
923 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
924 if (r < 0)
925 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
926 if (r == 0) { /* Doesn't exist yet? */
927 r = mkdir_p_label(where, 0755);
928 if (r < 0)
929 return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
930 }
931
932 return mount_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
933 }
934
935 int mount_custom(
936 const char *dest,
937 CustomMount *mounts, size_t n,
938 uid_t uid_shift,
939 const char *selinux_apifs_context,
940 MountSettingsMask mount_settings) {
941
942 size_t i;
943 int r;
944
945 assert(dest);
946
947 for (i = 0; i < n; i++) {
948 CustomMount *m = mounts + i;
949
950 if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
951 continue;
952
953 if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
954 continue;
955
956 if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/"))
957 continue;
958
959 switch (m->type) {
960
961 case CUSTOM_MOUNT_BIND:
962 r = mount_bind(dest, m);
963 break;
964
965 case CUSTOM_MOUNT_TMPFS:
966 r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
967 break;
968
969 case CUSTOM_MOUNT_OVERLAY:
970 r = mount_overlay(dest, m);
971 break;
972
973 case CUSTOM_MOUNT_INACCESSIBLE:
974 r = mount_inaccessible(dest, m);
975 break;
976
977 case CUSTOM_MOUNT_ARBITRARY:
978 r = mount_arbitrary(dest, m);
979 break;
980
981 default:
982 assert_not_reached("Unknown custom mount type");
983 }
984
985 if (r < 0)
986 return r;
987 }
988
989 return 0;
990 }
991
992 static int setup_volatile_state(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
993
994 _cleanup_free_ char *buf = NULL;
995 const char *p, *options;
996 int r;
997
998 assert(directory);
999
1000 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
1001
1002 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
1003 if (r < 0)
1004 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1005
1006 p = prefix_roota(directory, "/var");
1007 r = mkdir(p, 0755);
1008 if (r < 0 && errno != EEXIST)
1009 return log_error_errno(errno, "Failed to create %s: %m", directory);
1010
1011 options = "mode=755";
1012 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1013 if (r < 0)
1014 return log_oom();
1015 if (r > 0)
1016 options = buf;
1017
1018 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
1019 }
1020
1021 static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
1022
1023 bool tmpfs_mounted = false, bind_mounted = false;
1024 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1025 _cleanup_free_ char *buf = NULL, *bindir = NULL;
1026 const char *f, *t, *options;
1027 struct stat st;
1028 int r;
1029
1030 assert(directory);
1031
1032 /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
1033 * that read-only. Before we start setting this up let's validate if the image has the /usr merge
1034 * implemented, and let's output a friendly log message if it hasn't. */
1035
1036 bindir = path_join(directory, "/bin");
1037 if (!bindir)
1038 return log_oom();
1039 if (lstat(bindir, &st) < 0) {
1040 if (errno != ENOENT)
1041 return log_error_errno(errno, "Failed to stat /bin directory below image: %m");
1042
1043 /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1044 * rest. */
1045 } else if (S_ISDIR(st.st_mode))
1046 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
1047 "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1048 "Please work with your distribution and help them adopt the merged /usr scheme.");
1049 else if (!S_ISLNK(st.st_mode))
1050 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1051 "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically).");
1052
1053 if (!mkdtemp(template))
1054 return log_error_errno(errno, "Failed to create temporary directory: %m");
1055
1056 options = "mode=755";
1057 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1058 if (r < 0)
1059 goto fail;
1060 if (r > 0)
1061 options = buf;
1062
1063 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1064 if (r < 0)
1065 goto fail;
1066
1067 tmpfs_mounted = true;
1068
1069 f = prefix_roota(directory, "/usr");
1070 t = prefix_roota(template, "/usr");
1071
1072 r = mkdir(t, 0755);
1073 if (r < 0 && errno != EEXIST) {
1074 r = log_error_errno(errno, "Failed to create %s: %m", t);
1075 goto fail;
1076 }
1077
1078 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1079 if (r < 0)
1080 goto fail;
1081
1082 bind_mounted = true;
1083
1084 r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
1085 if (r < 0) {
1086 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1087 goto fail;
1088 }
1089
1090 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1091 if (r < 0)
1092 goto fail;
1093
1094 (void) rmdir(template);
1095
1096 return 0;
1097
1098 fail:
1099 if (bind_mounted)
1100 (void) umount_verbose(t);
1101
1102 if (tmpfs_mounted)
1103 (void) umount_verbose(template);
1104 (void) rmdir(template);
1105 return r;
1106 }
1107
1108 static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
1109
1110 _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
1111 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1112 const char *upper, *work, *options;
1113 bool tmpfs_mounted = false;
1114 int r;
1115
1116 assert(directory);
1117
1118 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1119
1120 if (!mkdtemp(template))
1121 return log_error_errno(errno, "Failed to create temporary directory: %m");
1122
1123 options = "mode=755";
1124 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1125 if (r < 0)
1126 goto finish;
1127 if (r > 0)
1128 options = buf;
1129
1130 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1131 if (r < 0)
1132 goto finish;
1133
1134 tmpfs_mounted = true;
1135
1136 upper = strjoina(template, "/upper");
1137 work = strjoina(template, "/work");
1138
1139 if (mkdir(upper, 0755) < 0) {
1140 r = log_error_errno(errno, "Failed to create %s: %m", upper);
1141 goto finish;
1142 }
1143 if (mkdir(work, 0755) < 0) {
1144 r = log_error_errno(errno, "Failed to create %s: %m", work);
1145 goto finish;
1146 }
1147
1148 /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1149 * that the kernel allows us to do that without going through some mount point rearrangements. */
1150
1151 escaped_directory = shell_escape(directory, ",:");
1152 escaped_upper = shell_escape(upper, ",:");
1153 escaped_work = shell_escape(work, ",:");
1154 if (!escaped_directory || !escaped_upper || !escaped_work) {
1155 r = -ENOMEM;
1156 goto finish;
1157 }
1158
1159 options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
1160 r = mount_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
1161
1162 finish:
1163 if (tmpfs_mounted)
1164 (void) umount_verbose(template);
1165
1166 (void) rmdir(template);
1167 return r;
1168 }
1169
1170 int setup_volatile_mode(
1171 const char *directory,
1172 VolatileMode mode,
1173 uid_t uid_shift,
1174 const char *selinux_apifs_context) {
1175
1176 switch (mode) {
1177
1178 case VOLATILE_YES:
1179 return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
1180
1181 case VOLATILE_STATE:
1182 return setup_volatile_state(directory, uid_shift, selinux_apifs_context);
1183
1184 case VOLATILE_OVERLAY:
1185 return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
1186
1187 default:
1188 return 0;
1189 }
1190 }
1191
1192 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1193 int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1194 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1195 const char *p = s;
1196 int r;
1197
1198 assert(pivot_root_new);
1199 assert(pivot_root_old);
1200
1201 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1202 if (r < 0)
1203 return r;
1204 if (r == 0)
1205 return -EINVAL;
1206
1207 if (isempty(p))
1208 root_old = NULL;
1209 else {
1210 root_old = strdup(p);
1211 if (!root_old)
1212 return -ENOMEM;
1213 }
1214
1215 if (!path_is_absolute(root_new))
1216 return -EINVAL;
1217 if (root_old && !path_is_absolute(root_old))
1218 return -EINVAL;
1219
1220 free_and_replace(*pivot_root_new, root_new);
1221 free_and_replace(*pivot_root_old, root_old);
1222
1223 return 0;
1224 }
1225
1226 int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1227 _cleanup_free_ char *directory_pivot_root_new = NULL;
1228 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1229 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1230 bool remove_pivot_tmp = false;
1231 int r;
1232
1233 assert(directory);
1234
1235 if (!pivot_root_new)
1236 return 0;
1237
1238 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1239 * If pivot_root_old is NULL, the existing / disappears.
1240 * This requires a temporary directory, pivot_tmp, which is
1241 * not a child of either.
1242 *
1243 * This is typically used for OSTree-style containers, where
1244 * the root partition contains several sysroots which could be
1245 * run. Normally, one would be chosen by the bootloader and
1246 * pivoted to / by initramfs.
1247 *
1248 * For example, for an OSTree deployment, pivot_root_new
1249 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1250 * code doesn’t do the /var mount which OSTree expects: use
1251 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1252 *
1253 * So in the OSTree case, we’ll end up with something like:
1254 * - directory = /tmp/nspawn-root-123456
1255 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1256 * - pivot_root_old = /sysroot
1257 * - directory_pivot_root_new =
1258 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1259 * - pivot_tmp = /tmp/nspawn-pivot-123456
1260 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1261 *
1262 * Requires all file systems at directory and below to be mounted
1263 * MS_PRIVATE or MS_SLAVE so they can be moved.
1264 */
1265 directory_pivot_root_new = path_join(directory, pivot_root_new);
1266 if (!directory_pivot_root_new)
1267 return log_oom();
1268
1269 /* Remount directory_pivot_root_new to make it movable. */
1270 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1271 if (r < 0)
1272 goto done;
1273
1274 if (pivot_root_old) {
1275 if (!mkdtemp(pivot_tmp)) {
1276 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1277 goto done;
1278 }
1279
1280 remove_pivot_tmp = true;
1281 pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
1282 if (!pivot_tmp_pivot_root_old) {
1283 r = log_oom();
1284 goto done;
1285 }
1286
1287 r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1288 if (r < 0)
1289 goto done;
1290
1291 r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1292 if (r < 0)
1293 goto done;
1294
1295 r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1296 if (r < 0)
1297 goto done;
1298 } else {
1299 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1300 if (r < 0)
1301 goto done;
1302 }
1303
1304 done:
1305 if (remove_pivot_tmp)
1306 (void) rmdir(pivot_tmp);
1307
1308 return r;
1309 }