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