1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <sys/statvfs.h>
9 #include "alloc-util.h"
11 #include "cgroup-setup.h"
12 #include "cgroup-util.h"
13 #include "conf-files.h"
14 #include "dev-setup.h"
15 #include "dirent-util.h"
16 #include "efi-loader.h"
20 #include "label-util.h"
23 #include "mkdir-label.h"
24 #include "mount-setup.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "nulstr-util.h"
28 #include "path-util.h"
29 #include "recurse-dir.h"
31 #include "smack-util.h"
33 #include "user-util.h"
36 typedef enum MountMode
{
39 MNT_IN_CONTAINER
= 1 << 1,
40 MNT_CHECK_WRITABLE
= 1 << 2,
41 MNT_FOLLOW_SYMLINK
= 1 << 3,
44 typedef struct MountPoint
{
50 bool (*condition_fn
)(void);
54 /* The first three entries we might need before SELinux is up. The
55 * fourth (securityfs) is needed by IMA to load a custom policy. The
56 * other ones we can delay until SELinux and IMA are loaded. When
57 * SMACK is enabled we need smackfs, too, so it's a fifth one. */
59 #define N_EARLY_MOUNT 5
61 #define N_EARLY_MOUNT 4
64 static bool check_recursiveprot_supported(void) {
67 if (!cg_is_unified_wanted())
70 r
= mount_option_supported("cgroup2", "memory_recursiveprot", NULL
);
72 log_debug_errno(r
, "Failed to determiner whether the 'memory_recursiveprot' mount option is supported, assuming not: %m");
74 log_debug("This kernel version does not support 'memory_recursiveprot', not using mount option.");
79 static const MountPoint mount_table
[] = {
80 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
81 NULL
, MNT_FATAL
|MNT_IN_CONTAINER
|MNT_FOLLOW_SYMLINK
},
82 { "sysfs", "/sys", "sysfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
83 NULL
, MNT_FATAL
|MNT_IN_CONTAINER
},
84 { "devtmpfs", "/dev", "devtmpfs", "mode=0755" TMPFS_LIMITS_DEV
, MS_NOSUID
|MS_STRICTATIME
,
85 NULL
, MNT_FATAL
|MNT_IN_CONTAINER
},
86 { "securityfs", "/sys/kernel/security", "securityfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
89 { "smackfs", "/sys/fs/smackfs", "smackfs", "smackfsdef=*", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
90 mac_smack_use
, MNT_FATAL
},
91 { "tmpfs", "/dev/shm", "tmpfs", "mode=01777,smackfsroot=*", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
92 mac_smack_use
, MNT_FATAL
},
94 { "tmpfs", "/dev/shm", "tmpfs", "mode=01777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
95 NULL
, MNT_FATAL
|MNT_IN_CONTAINER
},
96 { "devpts", "/dev/pts", "devpts", "mode=0620,gid=" STRINGIFY(TTY_GID
), MS_NOSUID
|MS_NOEXEC
,
97 NULL
, MNT_IN_CONTAINER
},
99 { "tmpfs", "/run", "tmpfs", "mode=0755,smackfsroot=*" TMPFS_LIMITS_RUN
, MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
100 mac_smack_use
, MNT_FATAL
},
102 { "tmpfs", "/run", "tmpfs", "mode=0755" TMPFS_LIMITS_RUN
, MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
103 NULL
, MNT_FATAL
|MNT_IN_CONTAINER
},
104 { "cgroup2", "/sys/fs/cgroup", "cgroup2", "nsdelegate,memory_recursiveprot", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
105 check_recursiveprot_supported
, MNT_IN_CONTAINER
|MNT_CHECK_WRITABLE
},
106 { "cgroup2", "/sys/fs/cgroup", "cgroup2", "nsdelegate", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
107 cg_is_unified_wanted
, MNT_IN_CONTAINER
|MNT_CHECK_WRITABLE
},
108 { "cgroup2", "/sys/fs/cgroup", "cgroup2", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
109 cg_is_unified_wanted
, MNT_IN_CONTAINER
|MNT_CHECK_WRITABLE
},
111 { "pstore", "/sys/fs/pstore", "pstore", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
115 { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
116 is_efi_boot
, MNT_NONE
},
118 { "bpf", "/sys/fs/bpf", "bpf", "mode=0700", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
122 assert_cc(N_EARLY_MOUNT
<= ELEMENTSOF(mount_table
));
124 bool mount_point_is_api(const char *path
) {
125 /* Checks if this mount point is considered "API", and hence
126 * should be ignored */
128 FOREACH_ARRAY(i
, mount_table
, ELEMENTSOF(mount_table
))
129 if (path_equal(path
, i
->where
))
132 return path_startswith(path
, "/sys/fs/cgroup/");
135 bool mount_point_ignore(const char *path
) {
136 /* These are API file systems that might be mounted by other software, we just list them here so that
137 * we know that we should ignore them. */
139 /* SELinux file systems */
141 /* Container bind mounts */
145 "/proc/sys/kernel/random/boot_id")
146 if (path_equal(path
, i
))
149 if (path_startswith(path
, "/run/host")) /* All mounts passed in from the container manager are
150 * something we better ignore. */
156 static int mount_one(const MountPoint
*p
, bool relabel
) {
164 priority
= FLAGS_SET(p
->mode
, MNT_FATAL
) ? LOG_ERR
: LOG_DEBUG
;
166 if (p
->condition_fn
&& !p
->condition_fn())
169 /* Relabel first, just in case */
171 (void) label_fix(p
->where
, LABEL_IGNORE_ENOENT
|LABEL_IGNORE_EROFS
);
173 r
= path_is_mount_point_full(p
->where
, /* root = */ NULL
, AT_SYMLINK_FOLLOW
);
174 if (r
< 0 && r
!= -ENOENT
) {
175 log_full_errno(priority
, r
, "Failed to determine whether %s is a mount point: %m", p
->where
);
176 return FLAGS_SET(p
->mode
, MNT_FATAL
) ? r
: 0;
181 /* Skip securityfs in a container */
182 if (!FLAGS_SET(p
->mode
, MNT_IN_CONTAINER
) && detect_container() > 0)
185 /* The access mode here doesn't really matter too much, since
186 * the mounted file system will take precedence anyway. */
188 (void) mkdir_p_label(p
->where
, 0755);
190 (void) mkdir_p(p
->where
, 0755);
192 log_debug("Mounting %s to %s of type %s with options %s.",
198 if (FLAGS_SET(p
->mode
, MNT_FOLLOW_SYMLINK
))
199 r
= mount_follow_verbose(priority
, p
->what
, p
->where
, p
->type
, p
->flags
, p
->options
);
201 r
= mount_nofollow_verbose(priority
, p
->what
, p
->where
, p
->type
, p
->flags
, p
->options
);
203 return FLAGS_SET(p
->mode
, MNT_FATAL
) ? r
: 0;
205 /* Relabel again, since we now mounted something fresh here */
207 (void) label_fix(p
->where
, 0);
209 if (FLAGS_SET(p
->mode
, MNT_CHECK_WRITABLE
))
210 if (access(p
->where
, W_OK
) < 0) {
213 (void) umount2(p
->where
, UMOUNT_NOFOLLOW
);
214 (void) rmdir(p
->where
);
216 log_full_errno(priority
, r
, "Mount point '%s' not writable after mounting, undoing: %m", p
->where
);
217 return FLAGS_SET(p
->mode
, MNT_FATAL
) ? r
: 0;
223 static int mount_points_setup(size_t n
, bool loaded_policy
) {
226 assert(n
<= ELEMENTSOF(mount_table
));
228 FOREACH_ARRAY(mp
, mount_table
, n
)
229 RET_GATHER(r
, mount_one(mp
, loaded_policy
));
234 int mount_setup_early(void) {
235 /* Do a minimal mount of /proc and friends to enable the most basic stuff, such as SELinux */
236 return mount_points_setup(N_EARLY_MOUNT
, /* loaded_policy= */ false);
239 static const char *join_with(const char *controller
) {
241 static const char* const pairs
[] = {
243 "net_cls", "net_prio",
249 /* This will lookup which controller to mount another controller with. Input is a controller name, and output
250 * is the other controller name. The function works both ways: you can input one and get the other, and input
251 * the other to get the one. */
253 STRV_FOREACH_PAIR(x
, y
, pairs
) {
254 if (streq(controller
, *x
))
256 if (streq(controller
, *y
))
263 static int symlink_controller(const char *target
, const char *alias
) {
270 a
= strjoina("/sys/fs/cgroup/", alias
);
272 r
= symlink_idempotent(target
, a
, false);
274 return log_error_errno(r
, "Failed to create symlink %s: %m", a
);
276 #if HAVE_SMACK_RUN_LABEL
279 p
= strjoina("/sys/fs/cgroup/", target
);
281 r
= mac_smack_copy(a
, p
);
282 if (r
< 0 && !ERRNO_IS_NOT_SUPPORTED(r
))
283 return log_error_errno(r
, "Failed to copy smack label from %s to %s: %m", p
, a
);
289 #if HAVE_SELINUX || ENABLE_SMACK
290 static int relabel_cb(
291 RecurseDirEvent event
,
295 const struct dirent
*de
,
296 const struct statx
*sx
,
301 case RECURSE_DIR_LEAVE
:
302 case RECURSE_DIR_SKIP_MOUNT
:
303 /* If we already saw this dirent when entering it or this is a dirent that on a different
304 * mount, don't relabel it. */
305 return RECURSE_DIR_CONTINUE
;
307 case RECURSE_DIR_ENTER
:
308 /* /run/initramfs/ + /run/nextroot/ are static data and big, no need to dynamically relabel
309 * its contents at boot... */
310 if (PATH_STARTSWITH_SET(path
, "/run/initramfs", "/run/nextroot"))
311 return RECURSE_DIR_SKIP_ENTRY
;
316 /* Otherwise, label it, even if we had trouble stat()ing it and similar. SELinux can figure this out */
317 (void) label_fix(path
, 0);
318 return RECURSE_DIR_CONTINUE
;
322 static int relabel_tree(const char *path
) {
325 r
= recurse_dir_at(AT_FDCWD
, path
, 0, UINT_MAX
, RECURSE_DIR_ENSURE_TYPE
|RECURSE_DIR_SAME_MOUNT
, relabel_cb
, NULL
);
327 log_debug_errno(r
, "Failed to recursively relabel '%s': %m", path
);
332 static int relabel_extra(void) {
333 _cleanup_strv_free_
char **files
= NULL
;
336 /* Support for relabelling additional files or directories after loading the policy. For this, code in the
337 * initrd simply has to drop in *.relabel files into /run/systemd/relabel-extra.d/. We'll read all such files
338 * expecting one absolute path by line and will relabel each (and everyone below that in case the path refers
339 * to a directory). These drop-in files are supposed to be absolutely minimal, and do not understand comments
340 * and such. After the operation succeeded the files are removed, and the drop-in directory as well, if
344 r
= conf_files_list(&files
, ".relabel", NULL
,
345 CONF_FILES_FILTER_MASKED
| CONF_FILES_REGULAR
,
346 "/run/systemd/relabel-extra.d/");
348 return log_error_errno(r
, "Failed to enumerate /run/systemd/relabel-extra.d/, ignoring: %m");
350 STRV_FOREACH(file
, files
) {
351 _cleanup_fclose_
FILE *f
= NULL
;
353 f
= fopen(*file
, "re");
355 log_warning_errno(errno
, "Failed to open %s, ignoring: %m", *file
);
360 _cleanup_free_
char *line
= NULL
;
362 r
= read_line(f
, LONG_LINE_MAX
, &line
);
364 log_warning_errno(r
, "Failed to read %s, ignoring: %m", *file
);
367 if (r
== 0) /* EOF */
372 if (!path_is_normalized(line
)) {
373 log_warning("Path to relabel is not normalized, ignoring: %s", line
);
377 if (!path_is_absolute(line
)) {
378 log_warning("Path to relabel is not absolute, ignoring: %s", line
);
382 log_debug("Relabelling additional file/directory '%s'.", line
);
383 (void) label_fix(line
, 0);
384 (void) relabel_tree(line
);
388 if (unlink(*file
) < 0)
389 log_warning_errno(errno
, "Failed to remove %s, ignoring: %m", *file
);
392 /* Remove when we complete things. */
393 if (rmdir("/run/systemd/relabel-extra.d") < 0 &&
395 log_warning_errno(errno
, "Failed to remove /run/systemd/relabel-extra.d/ directory: %m");
401 int mount_setup(bool loaded_policy
, bool leave_propagation
) {
404 r
= mount_points_setup(ELEMENTSOF(mount_table
), loaded_policy
);
408 #if HAVE_SELINUX || ENABLE_SMACK
409 /* Nodes in devtmpfs and /run need to be manually updated for
410 * the appropriate labels, after mounting. The other virtual
411 * API file systems like /sys and /proc do not need that, they
412 * use the same label for all their files. */
414 usec_t before_relabel
, after_relabel
;
417 before_relabel
= now(CLOCK_MONOTONIC
);
419 FOREACH_STRING(i
, "/dev", "/dev/shm", "/run")
420 (void) relabel_tree(i
);
422 n_extra
= relabel_extra();
424 after_relabel
= now(CLOCK_MONOTONIC
);
426 log_info("Relabeled /dev/, /dev/shm/, /run/%s in %s.",
427 n_extra
> 0 ? ", and additional files" : "",
428 FORMAT_TIMESPAN(after_relabel
- before_relabel
, 0));
432 /* Create a few default symlinks, which are normally created
433 * by udevd, but some scripts might need them before we start
435 dev_setup(NULL
, UID_INVALID
, GID_INVALID
);
437 /* Mark the root directory as shared in regards to mount propagation. The kernel defaults to "private", but we
438 * think it makes more sense to have a default of "shared" so that nspawn and the container tools work out of
439 * the box. If specific setups need other settings they can reset the propagation mode to private if
440 * needed. Note that we set this only when we are invoked directly by the kernel. If we are invoked by a
441 * container manager we assume the container manager knows what it is doing (for example, because it set up
442 * some directories with different propagation modes). */
443 if (detect_container() <= 0 && !leave_propagation
)
444 if (mount(NULL
, "/", NULL
, MS_REC
|MS_SHARED
, NULL
) < 0)
445 log_warning_errno(errno
, "Failed to set up the root directory for shared mount propagation: %m");
447 /* Create a few directories we always want around, Note that sd_booted() checks for /run/systemd/system, so
448 * this mkdir really needs to stay for good, otherwise software that copied sd-daemon.c into their sources will
449 * misdetect systemd. */
450 (void) mkdir_label("/run/systemd", 0755);
451 (void) mkdir_label("/run/systemd/system", 0755);
453 /* Make sure there's always a place where sandboxed environments can mount root file systems they are
454 * about to move into, even when unprivileged, without having to create a temporary one in /tmp/
455 * (which they then have to keep track of and clean) */
456 (void) mkdir_label("/run/systemd/mount-rootfs", 0555);
458 /* Make sure we have a mount point to hide in sandboxes */
459 (void) mkdir_label("/run/credentials", 0755);
461 /* Also create /run/systemd/inaccessible nodes, so that we always have something to mount
462 * inaccessible nodes from. If we run in a container the host might have created these for us already
463 * in /run/host/inaccessible/. Use those if we can, since that way we likely get access to block/char
464 * device nodes that are inaccessible, and if userns is used to nodes that are on mounts owned by a
465 * userns outside the container and thus nicely read-only and not remountable. */
466 if (access("/run/host/inaccessible/", F_OK
) < 0) {
468 log_debug_errno(errno
, "Failed to check if /run/host/inaccessible exists, ignoring: %m");
470 (void) make_inaccessible_nodes("/run/systemd", UID_INVALID
, GID_INVALID
);
472 (void) symlink("../host/inaccessible", "/run/systemd/inaccessible");
477 static const MountPoint cgroupv1_mount_table
[] = {
478 { "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=0755" TMPFS_LIMITS_SYS_FS_CGROUP
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
,
479 cg_is_legacy_wanted
, MNT_FATAL
|MNT_IN_CONTAINER
},
480 { "cgroup2", "/sys/fs/cgroup/unified", "cgroup2", "nsdelegate", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
481 cg_is_hybrid_wanted
, MNT_IN_CONTAINER
|MNT_CHECK_WRITABLE
},
482 { "cgroup2", "/sys/fs/cgroup/unified", "cgroup2", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
483 cg_is_hybrid_wanted
, MNT_IN_CONTAINER
|MNT_CHECK_WRITABLE
},
484 { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd,xattr", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
485 cg_is_legacy_wanted
, MNT_IN_CONTAINER
},
486 { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
487 cg_is_legacy_wanted
, MNT_FATAL
|MNT_IN_CONTAINER
},
490 static void relabel_cgroup_legacy_hierarchy(void) {
491 #if HAVE_SELINUX || ENABLE_SMACK
494 assert(cg_is_legacy_wanted());
496 /* Temporarily remount the root cgroup filesystem to give it a proper label. Do this
497 only when the filesystem has been already populated by a previous instance of systemd
498 running from initrd. Otherwise don't remount anything and leave the filesystem read-write
499 for the cgroup filesystems to be mounted inside. */
500 if (statfs("/sys/fs/cgroup", &st
) < 0)
501 return (void) log_error_errno(errno
, "Failed to determine mount flags for /sys/fs/cgroup/: %m");
503 if (st
.f_flags
& ST_RDONLY
)
504 (void) mount_nofollow(NULL
, "/sys/fs/cgroup", NULL
, MS_REMOUNT
, NULL
);
506 (void) label_fix("/sys/fs/cgroup", 0);
507 (void) relabel_tree("/sys/fs/cgroup");
509 if (st
.f_flags
& ST_RDONLY
)
510 (void) mount_nofollow(NULL
, "/sys/fs/cgroup", NULL
, MS_REMOUNT
|MS_RDONLY
, NULL
);
514 int mount_cgroup_legacy_controllers(bool loaded_policy
) {
515 _cleanup_set_free_ Set
*controllers
= NULL
;
518 if (!cg_is_legacy_wanted())
521 FOREACH_ARRAY(mp
, cgroupv1_mount_table
, ELEMENTSOF(cgroupv1_mount_table
)) {
522 r
= mount_one(mp
, loaded_policy
);
528 relabel_cgroup_legacy_hierarchy();
530 /* Mount all available cgroup controllers that are built into the kernel. */
531 r
= cg_kernel_controllers(&controllers
);
533 return log_error_errno(r
, "Failed to enumerate cgroup controllers: %m");
536 _cleanup_free_
char *options
= NULL
, *controller
= NULL
, *where
= NULL
;
537 const char *other_controller
;
541 .flags
= MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
542 .mode
= MNT_IN_CONTAINER
,
545 controller
= set_steal_first(controllers
);
549 /* Check if we shall mount this together with another controller */
550 other_controller
= join_with(controller
);
551 if (other_controller
) {
552 _cleanup_free_
char *c
= NULL
;
554 /* Check if the other controller is actually available in the kernel too */
555 c
= set_remove(controllers
, other_controller
);
558 /* Join the two controllers into one string, and maintain a stable ordering */
559 if (strcmp(controller
, other_controller
) < 0)
560 options
= strjoin(controller
, ",", other_controller
);
562 options
= strjoin(other_controller
, ",", controller
);
568 /* The simple case, where there's only one controller to mount together */
570 options
= TAKE_PTR(controller
);
572 where
= path_join("/sys/fs/cgroup", options
);
579 r
= mount_one(&p
, true);
583 /* Create symlinks from the individual controller names, in case we have a joined mount */
585 (void) symlink_controller(options
, controller
);
586 if (other_controller
)
587 (void) symlink_controller(options
, other_controller
);
590 /* Now that we mounted everything, let's make the tmpfs the cgroup file systems are mounted into read-only. */
591 (void) mount_nofollow("tmpfs", "/sys/fs/cgroup", "tmpfs",
592 MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
,
593 "mode=0755" TMPFS_LIMITS_SYS_FS_CGROUP
);