]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
Merge pull request #4351 from keszybz/nspawn-debugging
[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(*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 strv_free(m->lower);
79 }
80
81 free(l);
82 }
83
84 int custom_mount_compare(const void *a, const void *b) {
85 const CustomMount *x = a, *y = b;
86 int r;
87
88 r = path_compare(x->destination, y->destination);
89 if (r != 0)
90 return r;
91
92 if (x->type < y->type)
93 return -1;
94 if (x->type > y->type)
95 return 1;
96
97 return 0;
98 }
99
100 int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
101 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
102 const char *p = s;
103 CustomMount *m;
104 int r;
105
106 assert(l);
107 assert(n);
108
109 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
110 if (r < 0)
111 return r;
112 if (r == 0)
113 return -EINVAL;
114
115 if (r == 1) {
116 destination = strdup(source);
117 if (!destination)
118 return -ENOMEM;
119 }
120
121 if (r == 2 && !isempty(p)) {
122 opts = strdup(p);
123 if (!opts)
124 return -ENOMEM;
125 }
126
127 if (!path_is_absolute(source))
128 return -EINVAL;
129
130 if (!path_is_absolute(destination))
131 return -EINVAL;
132
133 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
134 if (!m)
135 return log_oom();
136
137 m->source = source;
138 m->destination = destination;
139 m->read_only = read_only;
140 m->options = opts;
141
142 source = destination = opts = NULL;
143 return 0;
144 }
145
146 int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) {
147 _cleanup_free_ char *path = NULL, *opts = NULL;
148 const char *p = s;
149 CustomMount *m;
150 int r;
151
152 assert(l);
153 assert(n);
154 assert(s);
155
156 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
157 if (r < 0)
158 return r;
159 if (r == 0)
160 return -EINVAL;
161
162 if (isempty(p))
163 opts = strdup("mode=0755");
164 else
165 opts = strdup(p);
166 if (!opts)
167 return -ENOMEM;
168
169 if (!path_is_absolute(path))
170 return -EINVAL;
171
172 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
173 if (!m)
174 return -ENOMEM;
175
176 m->destination = path;
177 m->options = opts;
178
179 path = opts = NULL;
180 return 0;
181 }
182
183 static int tmpfs_patch_options(
184 const char *options,
185 bool userns,
186 uid_t uid_shift, uid_t uid_range,
187 bool patch_ids,
188 const char *selinux_apifs_context,
189 char **ret) {
190
191 char *buf = NULL;
192
193 if ((userns && uid_shift != 0) || patch_ids) {
194 assert(uid_shift != UID_INVALID);
195
196 if (options)
197 (void) asprintf(&buf, "%s,uid=" UID_FMT ",gid=" UID_FMT, options, uid_shift, uid_shift);
198 else
199 (void) asprintf(&buf, "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift);
200 if (!buf)
201 return -ENOMEM;
202
203 options = buf;
204 }
205
206 #ifdef HAVE_SELINUX
207 if (selinux_apifs_context) {
208 char *t;
209
210 if (options)
211 t = strjoin(options, ",context=\"", selinux_apifs_context, "\"", NULL);
212 else
213 t = strjoin("context=\"", selinux_apifs_context, "\"", NULL);
214 if (!t) {
215 free(buf);
216 return -ENOMEM;
217 }
218
219 free(buf);
220 buf = t;
221 }
222 #endif
223
224 if (!buf && options) {
225 buf = strdup(options);
226 if (!buf)
227 return -ENOMEM;
228 }
229 *ret = buf;
230
231 return !!buf;
232 }
233
234 int mount_sysfs(const char *dest) {
235 const char *full, *top, *x;
236 int r;
237
238 top = prefix_roota(dest, "/sys");
239 r = path_check_fstype(top, SYSFS_MAGIC);
240 if (r < 0)
241 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
242 /* /sys might already be mounted as sysfs by the outer child in the
243 * !netns case. In this case, it's all good. Don't touch it because we
244 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
245 */
246 if (r > 0)
247 return 0;
248
249 full = prefix_roota(top, "/full");
250
251 (void) mkdir(full, 0755);
252
253 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
254 MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
255 if (r < 0)
256 return r;
257
258 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
259 _cleanup_free_ char *from = NULL, *to = NULL;
260
261 from = prefix_root(full, x);
262 if (!from)
263 return log_oom();
264
265 to = prefix_root(top, x);
266 if (!to)
267 return log_oom();
268
269 (void) mkdir(to, 0755);
270
271 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
272 if (r < 0)
273 return r;
274
275 r = mount_verbose(LOG_ERR, NULL, to, NULL,
276 MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL);
277 if (r < 0)
278 return r;
279 }
280
281 r = umount_verbose(full);
282 if (r < 0)
283 return r;
284
285 if (rmdir(full) < 0)
286 return log_error_errno(errno, "Failed to remove %s: %m", full);
287
288 x = prefix_roota(top, "/fs/kdbus");
289 (void) mkdir_p(x, 0755);
290
291 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
292 * remount /sys read-only.
293 */
294 if (cg_ns_supported()) {
295 x = prefix_roota(top, "/fs/cgroup");
296 (void) mkdir_p(x, 0755);
297 }
298
299 return mount_verbose(LOG_ERR, NULL, top, NULL,
300 MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL);
301 }
302
303 int mount_all(const char *dest,
304 bool use_userns, bool in_userns,
305 bool use_netns,
306 uid_t uid_shift, uid_t uid_range,
307 const char *selinux_apifs_context) {
308
309 typedef struct MountPoint {
310 const char *what;
311 const char *where;
312 const char *type;
313 const char *options;
314 unsigned long flags;
315 bool fatal;
316 bool in_userns;
317 bool use_netns;
318 } MountPoint;
319
320 static const MountPoint mount_table[] = {
321 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true, true, false },
322 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, true, true, false }, /* Bind mount first ...*/
323 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, true, true, true }, /* (except for this) */
324 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, true, true, false }, /* ... then, make it r/o */
325 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL, NULL, MS_BIND, false, true, false }, /* Bind mount first ...*/
326 { NULL, "/proc/sysrq-trigger", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, false, true, false }, /* ... then, make it r/o */
327 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, true, false, true },
328 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, true, false, false },
329 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, true, false, false },
330 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, true, false, false },
331 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, true, false, false },
332 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME, true, true, false },
333 #ifdef HAVE_SELINUX
334 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, false, false, false }, /* Bind mount first */
335 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, false, false, false }, /* Then, make it r/o */
336 #endif
337 };
338
339 unsigned k;
340 int r;
341
342 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
343 _cleanup_free_ char *where = NULL, *options = NULL;
344 const char *o;
345
346 if (in_userns != mount_table[k].in_userns)
347 continue;
348
349 if (!use_netns && mount_table[k].use_netns)
350 continue;
351
352 where = prefix_root(dest, mount_table[k].where);
353 if (!where)
354 return log_oom();
355
356 r = path_is_mount_point(where, AT_SYMLINK_FOLLOW);
357 if (r < 0 && r != -ENOENT)
358 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
359
360 /* Skip this entry if it is not a remount. */
361 if (mount_table[k].what && r > 0)
362 continue;
363
364 r = mkdir_p(where, 0755);
365 if (r < 0 && r != -EEXIST) {
366 if (mount_table[k].fatal)
367 return log_error_errno(r, "Failed to create directory %s: %m", where);
368
369 log_debug_errno(r, "Failed to create directory %s: %m", where);
370 continue;
371 }
372
373 o = mount_table[k].options;
374 if (streq_ptr(mount_table[k].type, "tmpfs")) {
375 if (in_userns)
376 r = tmpfs_patch_options(o, use_userns, 0, uid_range, true, selinux_apifs_context, &options);
377 else
378 r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
379 if (r < 0)
380 return log_oom();
381 if (r > 0)
382 o = options;
383 }
384
385 r = mount_verbose(mount_table[k].fatal ? LOG_ERR : LOG_WARNING,
386 mount_table[k].what,
387 where,
388 mount_table[k].type,
389 mount_table[k].flags,
390 o);
391 if (r < 0 && mount_table[k].fatal)
392 return r;
393 }
394
395 return 0;
396 }
397
398 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
399 const char *p = options;
400 unsigned long flags = *mount_flags;
401 char *opts = NULL;
402
403 assert(options);
404
405 for (;;) {
406 _cleanup_free_ char *word = NULL;
407 int r = extract_first_word(&p, &word, ",", 0);
408 if (r < 0)
409 return log_error_errno(r, "Failed to extract mount option: %m");
410 if (r == 0)
411 break;
412
413 if (streq(word, "rbind"))
414 flags |= MS_REC;
415 else if (streq(word, "norbind"))
416 flags &= ~MS_REC;
417 else {
418 log_error("Invalid bind mount option: %s", word);
419 return -EINVAL;
420 }
421 }
422
423 *mount_flags = flags;
424 /* in the future mount_opts will hold string options for mount(2) */
425 *mount_opts = opts;
426
427 return 0;
428 }
429
430 static int mount_bind(const char *dest, CustomMount *m) {
431 struct stat source_st, dest_st;
432 const char *where;
433 unsigned long mount_flags = MS_BIND | MS_REC;
434 _cleanup_free_ char *mount_opts = NULL;
435 int r;
436
437 assert(m);
438
439 if (m->options) {
440 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
441 if (r < 0)
442 return r;
443 }
444
445 if (stat(m->source, &source_st) < 0)
446 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
447
448 where = prefix_roota(dest, m->destination);
449
450 if (stat(where, &dest_st) >= 0) {
451 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) {
452 log_error("Cannot bind mount directory %s on file %s.", m->source, where);
453 return -EINVAL;
454 }
455
456 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) {
457 log_error("Cannot bind mount file %s on directory %s.", m->source, where);
458 return -EINVAL;
459 }
460
461 } else if (errno == ENOENT) {
462 r = mkdir_parents_label(where, 0755);
463 if (r < 0)
464 return log_error_errno(r, "Failed to make parents of %s: %m", where);
465
466 /* Create the mount point. Any non-directory file can be
467 * mounted on any non-directory file (regular, fifo, socket,
468 * char, block).
469 */
470 if (S_ISDIR(source_st.st_mode))
471 r = mkdir_label(where, 0755);
472 else
473 r = touch(where);
474 if (r < 0)
475 return log_error_errno(r, "Failed to create mount point %s: %m", where);
476
477 } else
478 return log_error_errno(errno, "Failed to stat %s: %m", where);
479
480 r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
481 if (r < 0)
482 return r;
483
484 if (m->read_only) {
485 r = bind_remount_recursive(where, true, NULL);
486 if (r < 0)
487 return log_error_errno(r, "Read-only bind mount failed: %m");
488 }
489
490 return 0;
491 }
492
493 static int mount_tmpfs(
494 const char *dest,
495 CustomMount *m,
496 bool userns, uid_t uid_shift, uid_t uid_range,
497 const char *selinux_apifs_context) {
498
499 const char *where, *options;
500 _cleanup_free_ char *buf = NULL;
501 int r;
502
503 assert(dest);
504 assert(m);
505
506 where = prefix_roota(dest, m->destination);
507
508 r = mkdir_p_label(where, 0755);
509 if (r < 0 && r != -EEXIST)
510 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
511
512 r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
513 if (r < 0)
514 return log_oom();
515 options = r > 0 ? buf : m->options;
516
517 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
518 }
519
520 static char *joined_and_escaped_lower_dirs(char * const *lower) {
521 _cleanup_strv_free_ char **sv = NULL;
522
523 sv = strv_copy(lower);
524 if (!sv)
525 return NULL;
526
527 strv_reverse(sv);
528
529 if (!strv_shell_escape(sv, ",:"))
530 return NULL;
531
532 return strv_join(sv, ":");
533 }
534
535 static int mount_overlay(const char *dest, CustomMount *m) {
536 _cleanup_free_ char *lower = NULL;
537 const char *where, *options;
538 int r;
539
540 assert(dest);
541 assert(m);
542
543 where = prefix_roota(dest, m->destination);
544
545 r = mkdir_label(where, 0755);
546 if (r < 0 && r != -EEXIST)
547 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
548
549 (void) mkdir_p_label(m->source, 0755);
550
551 lower = joined_and_escaped_lower_dirs(m->lower);
552 if (!lower)
553 return log_oom();
554
555 if (m->read_only) {
556 _cleanup_free_ char *escaped_source = NULL;
557
558 escaped_source = shell_escape(m->source, ",:");
559 if (!escaped_source)
560 return log_oom();
561
562 options = strjoina("lowerdir=", escaped_source, ":", lower);
563 } else {
564 _cleanup_free_ char *escaped_source = NULL, *escaped_work_dir = NULL;
565
566 assert(m->work_dir);
567 (void) mkdir_label(m->work_dir, 0700);
568
569 escaped_source = shell_escape(m->source, ",:");
570 if (!escaped_source)
571 return log_oom();
572 escaped_work_dir = shell_escape(m->work_dir, ",:");
573 if (!escaped_work_dir)
574 return log_oom();
575
576 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
577 }
578
579 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
580 }
581
582 int mount_custom(
583 const char *dest,
584 CustomMount *mounts, unsigned n,
585 bool userns, uid_t uid_shift, uid_t uid_range,
586 const char *selinux_apifs_context) {
587
588 unsigned i;
589 int r;
590
591 assert(dest);
592
593 for (i = 0; i < n; i++) {
594 CustomMount *m = mounts + i;
595
596 switch (m->type) {
597
598 case CUSTOM_MOUNT_BIND:
599 r = mount_bind(dest, m);
600 break;
601
602 case CUSTOM_MOUNT_TMPFS:
603 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
604 break;
605
606 case CUSTOM_MOUNT_OVERLAY:
607 r = mount_overlay(dest, m);
608 break;
609
610 default:
611 assert_not_reached("Unknown custom mount type");
612 }
613
614 if (r < 0)
615 return r;
616 }
617
618 return 0;
619 }
620
621 /* Retrieve existing subsystems. This function is called in a new cgroup
622 * namespace.
623 */
624 static int get_controllers(Set *subsystems) {
625 _cleanup_fclose_ FILE *f = NULL;
626 char line[LINE_MAX];
627
628 assert(subsystems);
629
630 f = fopen("/proc/self/cgroup", "re");
631 if (!f)
632 return errno == ENOENT ? -ESRCH : -errno;
633
634 FOREACH_LINE(line, f, return -errno) {
635 int r;
636 char *e, *l, *p;
637
638 l = strchr(line, ':');
639 if (!l)
640 continue;
641
642 l++;
643 e = strchr(l, ':');
644 if (!e)
645 continue;
646
647 *e = 0;
648
649 if (STR_IN_SET(l, "", "name=systemd"))
650 continue;
651
652 p = strdup(l);
653 if (!p)
654 return -ENOMEM;
655
656 r = set_consume(subsystems, p);
657 if (r < 0)
658 return r;
659 }
660
661 return 0;
662 }
663
664 static int mount_legacy_cgroup_hierarchy(const char *dest, const char *controller, const char *hierarchy,
665 CGroupUnified unified_requested, bool read_only) {
666 const char *to, *fstype, *opts;
667 int r;
668
669 to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
670
671 r = path_is_mount_point(to, 0);
672 if (r < 0 && r != -ENOENT)
673 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
674 if (r > 0)
675 return 0;
676
677 mkdir_p(to, 0755);
678
679 /* The superblock mount options of the mount point need to be
680 * identical to the hosts', and hence writable... */
681 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
682 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
683 fstype = "cgroup2";
684 opts = NULL;
685 } else {
686 fstype = "cgroup";
687 opts = "none,name=systemd,xattr";
688 }
689 } else {
690 fstype = "cgroup";
691 opts = controller;
692 }
693
694 r = mount_verbose(LOG_ERR, "cgroup", to, fstype, MS_NOSUID|MS_NOEXEC|MS_NODEV, opts);
695 if (r < 0)
696 return r;
697
698 /* ... hence let's only make the bind mount read-only, not the superblock. */
699 if (read_only) {
700 r = mount_verbose(LOG_ERR, NULL, to, NULL,
701 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
702 if (r < 0)
703 return r;
704 }
705
706 return 1;
707 }
708
709 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
710 static int mount_legacy_cgns_supported(
711 CGroupUnified unified_requested, bool userns, uid_t uid_shift,
712 uid_t uid_range, const char *selinux_apifs_context) {
713 _cleanup_set_free_free_ Set *controllers = NULL;
714 const char *cgroup_root = "/sys/fs/cgroup", *c;
715 int r;
716
717 (void) mkdir_p(cgroup_root, 0755);
718
719 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
720 r = path_is_mount_point(cgroup_root, AT_SYMLINK_FOLLOW);
721 if (r < 0)
722 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
723 if (r == 0) {
724 _cleanup_free_ char *options = NULL;
725
726 /* When cgroup namespaces are enabled and user namespaces are
727 * used then the mount of the cgroupfs is done *inside* the new
728 * user namespace. We're root in the new user namespace and the
729 * kernel will happily translate our uid/gid to the correct
730 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
731 * pass uid 0 and not uid_shift to tmpfs_patch_options().
732 */
733 r = tmpfs_patch_options("mode=755", userns, 0, uid_range, true, selinux_apifs_context, &options);
734 if (r < 0)
735 return log_oom();
736
737 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
738 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
739 if (r < 0)
740 return r;
741 }
742
743 if (cg_all_unified() > 0)
744 goto skip_controllers;
745
746 controllers = set_new(&string_hash_ops);
747 if (!controllers)
748 return log_oom();
749
750 r = get_controllers(controllers);
751 if (r < 0)
752 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
753
754 for (;;) {
755 _cleanup_free_ const char *controller = NULL;
756
757 controller = set_steal_first(controllers);
758 if (!controller)
759 break;
760
761 r = mount_legacy_cgroup_hierarchy("", controller, controller, unified_requested, !userns);
762 if (r < 0)
763 return r;
764
765 /* When multiple hierarchies are co-mounted, make their
766 * constituting individual hierarchies a symlink to the
767 * co-mount.
768 */
769 c = controller;
770 for (;;) {
771 _cleanup_free_ char *target = NULL, *tok = NULL;
772
773 r = extract_first_word(&c, &tok, ",", 0);
774 if (r < 0)
775 return log_error_errno(r, "Failed to extract co-mounted cgroup controller: %m");
776 if (r == 0)
777 break;
778
779 target = prefix_root("/sys/fs/cgroup", tok);
780 if (!target)
781 return log_oom();
782
783 if (streq(controller, tok))
784 break;
785
786 r = symlink_idempotent(controller, target);
787 if (r == -EINVAL)
788 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
789 if (r < 0)
790 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
791 }
792 }
793
794 skip_controllers:
795 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false);
796 if (r < 0)
797 return r;
798
799 if (!userns)
800 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
801 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
802
803 return 0;
804 }
805
806 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
807 static int mount_legacy_cgns_unsupported(
808 const char *dest,
809 CGroupUnified unified_requested, bool userns, uid_t uid_shift, uid_t uid_range,
810 const char *selinux_apifs_context) {
811 _cleanup_set_free_free_ Set *controllers = NULL;
812 const char *cgroup_root;
813 int r;
814
815 cgroup_root = prefix_roota(dest, "/sys/fs/cgroup");
816
817 (void) mkdir_p(cgroup_root, 0755);
818
819 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
820 r = path_is_mount_point(cgroup_root, AT_SYMLINK_FOLLOW);
821 if (r < 0)
822 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
823 if (r == 0) {
824 _cleanup_free_ char *options = NULL;
825
826 r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
827 if (r < 0)
828 return log_oom();
829
830 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
831 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
832 if (r < 0)
833 return r;
834 }
835
836 if (cg_all_unified() > 0)
837 goto skip_controllers;
838
839 controllers = set_new(&string_hash_ops);
840 if (!controllers)
841 return log_oom();
842
843 r = cg_kernel_controllers(controllers);
844 if (r < 0)
845 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
846
847 for (;;) {
848 _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL;
849
850 controller = set_steal_first(controllers);
851 if (!controller)
852 break;
853
854 origin = prefix_root("/sys/fs/cgroup/", controller);
855 if (!origin)
856 return log_oom();
857
858 r = readlink_malloc(origin, &combined);
859 if (r == -EINVAL) {
860 /* Not a symbolic link, but directly a single cgroup hierarchy */
861
862 r = mount_legacy_cgroup_hierarchy(dest, controller, controller, unified_requested, true);
863 if (r < 0)
864 return r;
865
866 } else if (r < 0)
867 return log_error_errno(r, "Failed to read link %s: %m", origin);
868 else {
869 _cleanup_free_ char *target = NULL;
870
871 target = prefix_root(dest, origin);
872 if (!target)
873 return log_oom();
874
875 /* A symbolic link, a combination of controllers in one hierarchy */
876
877 if (!filename_is_valid(combined)) {
878 log_warning("Ignoring invalid combined hierarchy %s.", combined);
879 continue;
880 }
881
882 r = mount_legacy_cgroup_hierarchy(dest, combined, combined, unified_requested, true);
883 if (r < 0)
884 return r;
885
886 r = symlink_idempotent(combined, target);
887 if (r == -EINVAL)
888 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
889 if (r < 0)
890 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
891 }
892 }
893
894 skip_controllers:
895 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false);
896 if (r < 0)
897 return r;
898
899 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
900 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
901 }
902
903 static int mount_unified_cgroups(const char *dest) {
904 const char *p;
905 int r;
906
907 assert(dest);
908
909 p = prefix_roota(dest, "/sys/fs/cgroup");
910
911 (void) mkdir_p(p, 0755);
912
913 r = path_is_mount_point(p, AT_SYMLINK_FOLLOW);
914 if (r < 0)
915 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
916 if (r > 0) {
917 p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs");
918 if (access(p, F_OK) >= 0)
919 return 0;
920 if (errno != ENOENT)
921 return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
922
923 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
924 return -EINVAL;
925 }
926
927 return mount_verbose(LOG_ERR, "cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
928 }
929
930 int mount_cgroups(
931 const char *dest,
932 CGroupUnified unified_requested,
933 bool userns, uid_t uid_shift, uid_t uid_range,
934 const char *selinux_apifs_context,
935 bool use_cgns) {
936
937 if (unified_requested >= CGROUP_UNIFIED_ALL)
938 return mount_unified_cgroups(dest);
939 else if (use_cgns)
940 return mount_legacy_cgns_supported(unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
941
942 return mount_legacy_cgns_unsupported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
943 }
944
945 int mount_systemd_cgroup_writable(
946 const char *dest,
947 CGroupUnified unified_requested) {
948
949 _cleanup_free_ char *own_cgroup_path = NULL;
950 const char *systemd_root, *systemd_own;
951 int r;
952
953 assert(dest);
954
955 r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
956 if (r < 0)
957 return log_error_errno(r, "Failed to determine our own cgroup path: %m");
958
959 /* If we are living in the top-level, then there's nothing to do... */
960 if (path_equal(own_cgroup_path, "/"))
961 return 0;
962
963 if (unified_requested >= CGROUP_UNIFIED_ALL) {
964 systemd_own = strjoina(dest, "/sys/fs/cgroup", own_cgroup_path);
965 systemd_root = prefix_roota(dest, "/sys/fs/cgroup");
966 } else {
967 systemd_own = strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path);
968 systemd_root = prefix_roota(dest, "/sys/fs/cgroup/systemd");
969 }
970
971 /* Make our own cgroup a (writable) bind mount */
972 r = mount_verbose(LOG_ERR, systemd_own, systemd_own, NULL, MS_BIND, NULL);
973 if (r < 0)
974 return r;
975
976 /* And then remount the systemd cgroup root read-only */
977 return mount_verbose(LOG_ERR, NULL, systemd_root, NULL,
978 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
979 }
980
981 int setup_volatile_state(
982 const char *directory,
983 VolatileMode mode,
984 bool userns, uid_t uid_shift, uid_t uid_range,
985 const char *selinux_apifs_context) {
986
987 _cleanup_free_ char *buf = NULL;
988 const char *p, *options;
989 int r;
990
991 assert(directory);
992
993 if (mode != VOLATILE_STATE)
994 return 0;
995
996 /* --volatile=state means we simply overmount /var
997 with a tmpfs, and the rest read-only. */
998
999 r = bind_remount_recursive(directory, true, NULL);
1000 if (r < 0)
1001 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1002
1003 p = prefix_roota(directory, "/var");
1004 r = mkdir(p, 0755);
1005 if (r < 0 && errno != EEXIST)
1006 return log_error_errno(errno, "Failed to create %s: %m", directory);
1007
1008 options = "mode=755";
1009 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1010 if (r < 0)
1011 return log_oom();
1012 if (r > 0)
1013 options = buf;
1014
1015 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
1016 }
1017
1018 int setup_volatile(
1019 const char *directory,
1020 VolatileMode mode,
1021 bool userns, uid_t uid_shift, uid_t uid_range,
1022 const char *selinux_apifs_context) {
1023
1024 bool tmpfs_mounted = false, bind_mounted = false;
1025 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1026 _cleanup_free_ char *buf = NULL;
1027 const char *f, *t, *options;
1028 int r;
1029
1030 assert(directory);
1031
1032 if (mode != VOLATILE_YES)
1033 return 0;
1034
1035 /* --volatile=yes means we mount a tmpfs to the root dir, and
1036 the original /usr to use inside it, and that read-only. */
1037
1038 if (!mkdtemp(template))
1039 return log_error_errno(errno, "Failed to create temporary directory: %m");
1040
1041 options = "mode=755";
1042 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1043 if (r < 0)
1044 return log_oom();
1045 if (r > 0)
1046 options = buf;
1047
1048 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1049 if (r < 0)
1050 goto fail;
1051
1052 tmpfs_mounted = true;
1053
1054 f = prefix_roota(directory, "/usr");
1055 t = prefix_roota(template, "/usr");
1056
1057 r = mkdir(t, 0755);
1058 if (r < 0 && errno != EEXIST) {
1059 r = log_error_errno(errno, "Failed to create %s: %m", t);
1060 goto fail;
1061 }
1062
1063 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1064 if (r < 0)
1065 goto fail;
1066
1067 bind_mounted = true;
1068
1069 r = bind_remount_recursive(t, true, NULL);
1070 if (r < 0) {
1071 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1072 goto fail;
1073 }
1074
1075 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1076 if (r < 0)
1077 goto fail;
1078
1079 (void) rmdir(template);
1080
1081 return 0;
1082
1083 fail:
1084 if (bind_mounted)
1085 (void) umount_verbose(t);
1086
1087 if (tmpfs_mounted)
1088 (void) umount_verbose(template);
1089 (void) rmdir(template);
1090 return r;
1091 }
1092
1093 VolatileMode volatile_mode_from_string(const char *s) {
1094 int b;
1095
1096 if (isempty(s))
1097 return _VOLATILE_MODE_INVALID;
1098
1099 b = parse_boolean(s);
1100 if (b > 0)
1101 return VOLATILE_YES;
1102 if (b == 0)
1103 return VOLATILE_NO;
1104
1105 if (streq(s, "state"))
1106 return VOLATILE_STATE;
1107
1108 return _VOLATILE_MODE_INVALID;
1109 }