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