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