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