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