]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8e274523 | 2 | |
8e274523 | 3 | #include <errno.h> |
8e274523 | 4 | #include <stdlib.h> |
cf0fbc49 | 5 | #include <sys/mount.h> |
6f7729c1 | 6 | #include <sys/statvfs.h> |
5c0532d1 | 7 | #include <unistd.h> |
8e274523 | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
64824462 | 10 | #include "bus-util.h" |
aea0fe53 | 11 | #include "cgroup-setup.h" |
4349cd7c | 12 | #include "cgroup-util.h" |
48da02ec | 13 | #include "conf-files.h" |
4349cd7c | 14 | #include "dev-setup.h" |
0bb2f0f1 ZJS |
15 | #include "dirent-util.h" |
16 | #include "efi-loader.h" | |
65e183d7 | 17 | #include "fd-util.h" |
e07aefbd | 18 | #include "fileio.h" |
c4b41707 | 19 | #include "fs-util.h" |
4349cd7c | 20 | #include "label.h" |
8e274523 | 21 | #include "log.h" |
c9af1080 | 22 | #include "macro.h" |
35cd0ba5 | 23 | #include "mkdir-label.h" |
4349cd7c | 24 | #include "mount-setup.h" |
7d85383e | 25 | #include "mount-util.h" |
049af8ad | 26 | #include "mountpoint-util.h" |
d8b4d14d | 27 | #include "nulstr-util.h" |
9eb977db | 28 | #include "path-util.h" |
aea0fe53 | 29 | #include "recurse-dir.h" |
4349cd7c | 30 | #include "set.h" |
8552b176 | 31 | #include "smack-util.h" |
4349cd7c | 32 | #include "strv.h" |
ee104e11 | 33 | #include "user-util.h" |
4349cd7c | 34 | #include "virt.h" |
bef2733f | 35 | |
6aa220e0 | 36 | typedef enum MountMode { |
ef31828d LP |
37 | MNT_NONE = 0, |
38 | MNT_FATAL = 1 << 0, | |
39 | MNT_IN_CONTAINER = 1 << 1, | |
40 | MNT_CHECK_WRITABLE = 1 << 2, | |
21935150 | 41 | MNT_FOLLOW_SYMLINK = 1 << 3, |
6aa220e0 KS |
42 | } MountMode; |
43 | ||
ca714c0e LP |
44 | typedef struct MountPoint { |
45 | const char *what; | |
46 | const char *where; | |
47 | const char *type; | |
48 | const char *options; | |
49 | unsigned long flags; | |
6aa220e0 KS |
50 | bool (*condition_fn)(void); |
51 | MountMode mode; | |
ca714c0e LP |
52 | } MountPoint; |
53 | ||
4ef31082 | 54 | /* The first three entries we might need before SELinux is up. The |
160481f6 | 55 | * fourth (securityfs) is needed by IMA to load a custom policy. The |
7c96ab1d LP |
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. */ | |
f9fa32f0 | 58 | #if ENABLE_SMACK |
ffbd2c4d | 59 | #define N_EARLY_MOUNT 5 |
7c96ab1d LP |
60 | #else |
61 | #define N_EARLY_MOUNT 4 | |
62 | #endif | |
4ef31082 | 63 | |
ca714c0e | 64 | static const MountPoint mount_table[] = { |
b67ec8e5 | 65 | { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
21935150 LP |
66 | NULL, MNT_FATAL|MNT_IN_CONTAINER|MNT_FOLLOW_SYMLINK }, |
67 | { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
68d4c452 | 68 | NULL, MNT_FATAL|MNT_IN_CONTAINER }, |
4eb105fa | 69 | { "devtmpfs", "/dev", "devtmpfs", "mode=755" TMPFS_LIMITS_DEV, MS_NOSUID|MS_STRICTATIME, |
68d4c452 | 70 | NULL, MNT_FATAL|MNT_IN_CONTAINER }, |
b67ec8e5 | 71 | { "securityfs", "/sys/kernel/security", "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
68d4c452 | 72 | NULL, MNT_NONE }, |
f9fa32f0 | 73 | #if ENABLE_SMACK |
b67ec8e5 | 74 | { "smackfs", "/sys/fs/smackfs", "smackfs", "smackfsdef=*", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
68d4c452 | 75 | mac_smack_use, MNT_FATAL }, |
b67ec8e5 | 76 | { "tmpfs", "/dev/shm", "tmpfs", "mode=1777,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
68d4c452 | 77 | mac_smack_use, MNT_FATAL }, |
d407c940 | 78 | #endif |
b67ec8e5 | 79 | { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
68d4c452 | 80 | NULL, MNT_FATAL|MNT_IN_CONTAINER }, |
b67ec8e5 | 81 | { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, |
68d4c452 | 82 | NULL, MNT_IN_CONTAINER }, |
f9fa32f0 | 83 | #if ENABLE_SMACK |
b67ec8e5 | 84 | { "tmpfs", "/run", "tmpfs", "mode=755,smackfsroot=*" TMPFS_LIMITS_RUN, MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
68d4c452 | 85 | mac_smack_use, MNT_FATAL }, |
d407c940 | 86 | #endif |
b67ec8e5 | 87 | { "tmpfs", "/run", "tmpfs", "mode=755" TMPFS_LIMITS_RUN, MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
68d4c452 | 88 | NULL, MNT_FATAL|MNT_IN_CONTAINER }, |
56f47800 BB |
89 | { "cgroup2", "/sys/fs/cgroup", "cgroup2", "nsdelegate,memory_recursiveprot", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
90 | cg_is_unified_wanted, MNT_IN_CONTAINER|MNT_CHECK_WRITABLE }, | |
b67ec8e5 | 91 | { "cgroup2", "/sys/fs/cgroup", "cgroup2", "nsdelegate", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
2d56b80a | 92 | cg_is_unified_wanted, MNT_IN_CONTAINER|MNT_CHECK_WRITABLE }, |
b67ec8e5 | 93 | { "cgroup2", "/sys/fs/cgroup", "cgroup2", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
2d56b80a | 94 | cg_is_unified_wanted, MNT_IN_CONTAINER|MNT_CHECK_WRITABLE }, |
b67ec8e5 | 95 | { "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=755" TMPFS_LIMITS_SYS_FS_CGROUP, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, |
efdb0237 | 96 | cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER }, |
b67ec8e5 | 97 | { "cgroup2", "/sys/fs/cgroup/unified", "cgroup2", "nsdelegate", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
e07aefbd | 98 | cg_is_hybrid_wanted, MNT_IN_CONTAINER|MNT_CHECK_WRITABLE }, |
b67ec8e5 | 99 | { "cgroup2", "/sys/fs/cgroup/unified", "cgroup2", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
e07aefbd | 100 | cg_is_hybrid_wanted, MNT_IN_CONTAINER|MNT_CHECK_WRITABLE }, |
b67ec8e5 | 101 | { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd,xattr", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
2977724b | 102 | cg_is_legacy_wanted, MNT_IN_CONTAINER }, |
b67ec8e5 | 103 | { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
2977724b | 104 | cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER }, |
b67ec8e5 | 105 | { "pstore", "/sys/fs/pstore", "pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
68d4c452 | 106 | NULL, MNT_NONE }, |
349cc4a5 | 107 | #if ENABLE_EFI |
b67ec8e5 | 108 | { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
68d4c452 | 109 | is_efi_boot, MNT_NONE }, |
c06bf414 | 110 | #endif |
b67ec8e5 | 111 | { "bpf", "/sys/fs/bpf", "bpf", "mode=700", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
43b7f24b | 112 | NULL, MNT_NONE, }, |
63cc4c31 DM |
113 | }; |
114 | ||
dad08730 LP |
115 | bool mount_point_is_api(const char *path) { |
116 | unsigned i; | |
117 | ||
118 | /* Checks if this mount point is considered "API", and hence | |
119 | * should be ignored */ | |
120 | ||
ca714c0e | 121 | for (i = 0; i < ELEMENTSOF(mount_table); i ++) |
449ddb2d | 122 | if (path_equal(path, mount_table[i].where)) |
dad08730 LP |
123 | return true; |
124 | ||
57f2a956 KS |
125 | return path_startswith(path, "/sys/fs/cgroup/"); |
126 | } | |
127 | ||
128 | bool mount_point_ignore(const char *path) { | |
6f997852 | 129 | |
eaeb18db | 130 | const char *i; |
57f2a956 | 131 | |
6f997852 LP |
132 | /* These are API file systems that might be mounted by other software, we just list them here so that |
133 | * we know that we should ignore them. */ | |
134 | FOREACH_STRING(i, | |
135 | /* SELinux file systems */ | |
136 | "/sys/fs/selinux", | |
137 | /* Container bind mounts */ | |
138 | "/dev/console", | |
139 | "/proc/kmsg", | |
140 | "/proc/sys", | |
141 | "/proc/sys/kernel/random/boot_id") | |
eaeb18db | 142 | if (path_equal(path, i)) |
949c6510 | 143 | return true; |
6f997852 LP |
144 | |
145 | if (path_startswith(path, "/run/host")) /* All mounts passed in from the container manager are | |
146 | * something we better ignore. */ | |
147 | return true; | |
949c6510 | 148 | |
57f2a956 | 149 | return false; |
dad08730 LP |
150 | } |
151 | ||
4ef31082 | 152 | static int mount_one(const MountPoint *p, bool relabel) { |
713a8875 | 153 | int r, priority; |
8e274523 | 154 | |
ca714c0e | 155 | assert(p); |
8e274523 | 156 | |
713a8875 LP |
157 | priority = (p->mode & MNT_FATAL) ? LOG_ERR : LOG_DEBUG; |
158 | ||
6aa220e0 KS |
159 | if (p->condition_fn && !p->condition_fn()) |
160 | return 0; | |
161 | ||
51b4af2c | 162 | /* Relabel first, just in case */ |
4ef31082 | 163 | if (relabel) |
08c84981 | 164 | (void) label_fix(p->where, LABEL_IGNORE_ENOENT|LABEL_IGNORE_EROFS); |
51b4af2c | 165 | |
e1873695 | 166 | r = path_is_mount_point(p->where, NULL, AT_SYMLINK_FOLLOW); |
1411b094 | 167 | if (r < 0 && r != -ENOENT) { |
713a8875 | 168 | log_full_errno(priority, r, "Failed to determine whether %s is a mount point: %m", p->where); |
1411b094 LP |
169 | return (p->mode & MNT_FATAL) ? r : 0; |
170 | } | |
8e274523 | 171 | if (r > 0) |
51b4af2c | 172 | return 0; |
8e274523 | 173 | |
c481f78b | 174 | /* Skip securityfs in a container */ |
75f86906 | 175 | if (!(p->mode & MNT_IN_CONTAINER) && detect_container() > 0) |
c481f78b LP |
176 | return 0; |
177 | ||
a04f58d6 LP |
178 | /* The access mode here doesn't really matter too much, since |
179 | * the mounted file system will take precedence anyway. */ | |
c4bfd169 | 180 | if (relabel) |
1411b094 | 181 | (void) mkdir_p_label(p->where, 0755); |
c4bfd169 | 182 | else |
1411b094 | 183 | (void) mkdir_p(p->where, 0755); |
a04f58d6 | 184 | |
8e274523 | 185 | log_debug("Mounting %s to %s of type %s with options %s.", |
ca714c0e LP |
186 | p->what, |
187 | p->where, | |
188 | p->type, | |
189 | strna(p->options)); | |
190 | ||
21935150 | 191 | if (FLAGS_SET(p->mode, MNT_FOLLOW_SYMLINK)) |
7c248223 | 192 | r = RET_NERRNO(mount(p->what, p->where, p->type, p->flags, p->options)); |
21935150 LP |
193 | else |
194 | r = mount_nofollow(p->what, p->where, p->type, p->flags, p->options); | |
195 | if (r < 0) { | |
196 | log_full_errno(priority, r, "Failed to mount %s at %s: %m", p->type, p->where); | |
197 | return (p->mode & MNT_FATAL) ? r : 0; | |
8e274523 LP |
198 | } |
199 | ||
51b4af2c | 200 | /* Relabel again, since we now mounted something fresh here */ |
4ef31082 | 201 | if (relabel) |
08c84981 | 202 | (void) label_fix(p->where, 0); |
5275d3c1 | 203 | |
e07aefbd | 204 | if (p->mode & MNT_CHECK_WRITABLE) { |
713a8875 LP |
205 | if (access(p->where, W_OK) < 0) { |
206 | r = -errno; | |
207 | ||
21935150 | 208 | (void) umount2(p->where, UMOUNT_NOFOLLOW); |
1ff654e2 | 209 | (void) rmdir(p->where); |
713a8875 LP |
210 | |
211 | log_full_errno(priority, r, "Mount point %s not writable after mounting: %m", p->where); | |
e07aefbd CB |
212 | return (p->mode & MNT_FATAL) ? r : 0; |
213 | } | |
214 | } | |
215 | ||
0c85a4f3 | 216 | return 1; |
8e274523 LP |
217 | } |
218 | ||
400fac06 | 219 | static int mount_points_setup(unsigned n, bool loaded_policy) { |
4ef31082 LP |
220 | unsigned i; |
221 | int r = 0; | |
222 | ||
400fac06 | 223 | for (i = 0; i < n; i ++) { |
4ef31082 LP |
224 | int j; |
225 | ||
400fac06 | 226 | j = mount_one(mount_table + i, loaded_policy); |
7ff307bc | 227 | if (j != 0 && r >= 0) |
4ef31082 LP |
228 | r = j; |
229 | } | |
230 | ||
231 | return r; | |
232 | } | |
233 | ||
400fac06 AK |
234 | int mount_setup_early(void) { |
235 | assert_cc(N_EARLY_MOUNT <= ELEMENTSOF(mount_table)); | |
236 | ||
237 | /* Do a minimal mount of /proc and friends to enable the most | |
238 | * basic stuff, such as SELinux */ | |
239 | return mount_points_setup(N_EARLY_MOUNT, false); | |
240 | } | |
241 | ||
143fadf3 LP |
242 | static const char *join_with(const char *controller) { |
243 | ||
244 | static const char* const pairs[] = { | |
245 | "cpu", "cpuacct", | |
246 | "net_cls", "net_prio", | |
247 | NULL | |
248 | }; | |
249 | ||
143fadf3 LP |
250 | assert(controller); |
251 | ||
252 | /* This will lookup which controller to mount another controller with. Input is a controller name, and output | |
253 | * is the other controller name. The function works both ways: you can input one and get the other, and input | |
254 | * the other to get the one. */ | |
255 | ||
256 | STRV_FOREACH_PAIR(x, y, pairs) { | |
257 | if (streq(controller, *x)) | |
258 | return *y; | |
259 | if (streq(controller, *y)) | |
260 | return *x; | |
261 | } | |
262 | ||
263 | return NULL; | |
264 | } | |
265 | ||
266 | static int symlink_controller(const char *target, const char *alias) { | |
267 | const char *a; | |
268 | int r; | |
269 | ||
270 | assert(target); | |
271 | assert(alias); | |
272 | ||
273 | a = strjoina("/sys/fs/cgroup/", alias); | |
274 | ||
275 | r = symlink_idempotent(target, a, false); | |
276 | if (r < 0) | |
277 | return log_error_errno(r, "Failed to create symlink %s: %m", a); | |
278 | ||
07b382cc | 279 | #if HAVE_SMACK_RUN_LABEL |
143fadf3 LP |
280 | const char *p; |
281 | ||
282 | p = strjoina("/sys/fs/cgroup/", target); | |
283 | ||
284 | r = mac_smack_copy(a, p); | |
285 | if (r < 0 && r != -EOPNOTSUPP) | |
286 | return log_error_errno(r, "Failed to copy smack label from %s to %s: %m", p, a); | |
287 | #endif | |
288 | ||
289 | return 0; | |
290 | } | |
291 | ||
292 | int mount_cgroup_controllers(void) { | |
594c3835 | 293 | _cleanup_set_free_ Set *controllers = NULL; |
a641dcd9 | 294 | int r; |
2076ca54 | 295 | |
efdb0237 LP |
296 | if (!cg_is_legacy_wanted()) |
297 | return 0; | |
298 | ||
670802d4 | 299 | /* Mount all available cgroup controllers that are built into the kernel. */ |
6925a0de | 300 | r = cg_kernel_controllers(&controllers); |
b12afc8c LP |
301 | if (r < 0) |
302 | return log_error_errno(r, "Failed to enumerate cgroup controllers: %m"); | |
0c85a4f3 LP |
303 | |
304 | for (;;) { | |
a641dcd9 | 305 | _cleanup_free_ char *options = NULL, *controller = NULL, *where = NULL; |
143fadf3 | 306 | const char *other_controller; |
a6b26d90 ZJS |
307 | MountPoint p = { |
308 | .what = "cgroup", | |
309 | .type = "cgroup", | |
310 | .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
311 | .mode = MNT_IN_CONTAINER, | |
312 | }; | |
0c85a4f3 LP |
313 | |
314 | controller = set_steal_first(controllers); | |
315 | if (!controller) | |
316 | break; | |
317 | ||
143fadf3 LP |
318 | /* Check if we shall mount this together with another controller */ |
319 | other_controller = join_with(controller); | |
320 | if (other_controller) { | |
321 | _cleanup_free_ char *c = NULL; | |
322 | ||
323 | /* Check if the other controller is actually available in the kernel too */ | |
324 | c = set_remove(controllers, other_controller); | |
325 | if (c) { | |
326 | ||
327 | /* Join the two controllers into one string, and maintain a stable ordering */ | |
328 | if (strcmp(controller, other_controller) < 0) | |
329 | options = strjoin(controller, ",", other_controller); | |
330 | else | |
331 | options = strjoin(other_controller, ",", controller); | |
332 | if (!options) | |
333 | return log_oom(); | |
0c85a4f3 | 334 | } |
143fadf3 | 335 | } |
0c85a4f3 | 336 | |
143fadf3 LP |
337 | /* The simple case, where there's only one controller to mount together */ |
338 | if (!options) | |
ae2a15bc | 339 | options = TAKE_PTR(controller); |
0c85a4f3 | 340 | |
b910cc72 | 341 | where = path_join("/sys/fs/cgroup", options); |
a641dcd9 LP |
342 | if (!where) |
343 | return log_oom(); | |
344 | ||
345 | p.where = where; | |
0c85a4f3 | 346 | p.options = options; |
2076ca54 | 347 | |
4ef31082 | 348 | r = mount_one(&p, true); |
a6b26d90 ZJS |
349 | if (r < 0) |
350 | return r; | |
0c85a4f3 | 351 | |
143fadf3 LP |
352 | /* Create symlinks from the individual controller names, in case we have a joined mount */ |
353 | if (controller) | |
354 | (void) symlink_controller(options, controller); | |
355 | if (other_controller) | |
356 | (void) symlink_controller(options, other_controller); | |
2076ca54 LP |
357 | } |
358 | ||
143fadf3 | 359 | /* Now that we mounted everything, let's make the tmpfs the cgroup file systems are mounted into read-only. */ |
21935150 | 360 | (void) mount_nofollow("tmpfs", "/sys/fs/cgroup", "tmpfs", MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755" TMPFS_LIMITS_SYS_FS_CGROUP); |
679142ce | 361 | |
a6b26d90 | 362 | return 0; |
2076ca54 LP |
363 | } |
364 | ||
f9fa32f0 | 365 | #if HAVE_SELINUX || ENABLE_SMACK |
aea0fe53 LP |
366 | static int relabel_cb( |
367 | RecurseDirEvent event, | |
368 | const char *path, | |
369 | int dir_fd, | |
370 | int inode_fd, | |
371 | const struct dirent *de, | |
372 | const struct statx *sx, | |
373 | void *userdata) { | |
374 | ||
375 | switch (event) { | |
376 | ||
377 | case RECURSE_DIR_LEAVE: | |
378 | case RECURSE_DIR_SKIP_MOUNT: | |
379 | /* If we already saw this dirent when entering it or this is a dirent that on a different | |
380 | * mount, don't relabel it. */ | |
381 | return RECURSE_DIR_CONTINUE; | |
382 | ||
383 | case RECURSE_DIR_ENTER: | |
384 | /* /run/initramfs is static data and big, no need to dynamically relabel its contents at boot... */ | |
385 | if (path_equal(path, "/run/initramfs")) | |
386 | return RECURSE_DIR_SKIP_ENTRY; | |
387 | ||
388 | _fallthrough_; | |
389 | ||
390 | default: | |
391 | /* Otherwise, label it, even if we had trouble stat()ing it and similar. SELinux can figure this out */ | |
392 | (void) label_fix(path, 0); | |
393 | return RECURSE_DIR_CONTINUE; | |
394 | } | |
395 | } | |
396 | ||
397 | static int relabel_tree(const char *path) { | |
398 | int r; | |
399 | ||
400 | r = recurse_dir_at(AT_FDCWD, path, 0, UINT_MAX, RECURSE_DIR_ENSURE_TYPE|RECURSE_DIR_SAME_MOUNT, relabel_cb, NULL); | |
401 | if (r < 0) | |
402 | log_debug_errno(r, "Failed to recursively relabel '%s': %m", path); | |
403 | ||
404 | return r; | |
405 | } | |
6f7729c1 KN |
406 | |
407 | static int relabel_cgroup_filesystems(void) { | |
408 | int r; | |
409 | struct statfs st; | |
410 | ||
411 | r = cg_all_unified(); | |
412 | if (r == 0) { | |
413 | /* Temporarily remount the root cgroup filesystem to give it a proper label. Do this | |
414 | only when the filesystem has been already populated by a previous instance of systemd | |
415 | running from initrd. Otherwise don't remount anything and leave the filesystem read-write | |
416 | for the cgroup filesystems to be mounted inside. */ | |
771b7ead | 417 | if (statfs("/sys/fs/cgroup", &st) < 0) |
6f7729c1 | 418 | return log_error_errno(errno, "Failed to determine mount flags for /sys/fs/cgroup: %m"); |
6f7729c1 KN |
419 | |
420 | if (st.f_flags & ST_RDONLY) | |
21935150 | 421 | (void) mount_nofollow(NULL, "/sys/fs/cgroup", NULL, MS_REMOUNT, NULL); |
6f7729c1 KN |
422 | |
423 | (void) label_fix("/sys/fs/cgroup", 0); | |
aea0fe53 | 424 | (void) relabel_tree("/sys/fs/cgroup"); |
6f7729c1 KN |
425 | |
426 | if (st.f_flags & ST_RDONLY) | |
21935150 | 427 | (void) mount_nofollow(NULL, "/sys/fs/cgroup", NULL, MS_REMOUNT|MS_RDONLY, NULL); |
771b7ead | 428 | |
6f7729c1 KN |
429 | } else if (r < 0) |
430 | return log_error_errno(r, "Failed to determine whether we are in all unified mode: %m"); | |
431 | ||
432 | return 0; | |
433 | } | |
65e183d7 LP |
434 | |
435 | static int relabel_extra(void) { | |
48da02ec | 436 | _cleanup_strv_free_ char **files = NULL; |
65e183d7 LP |
437 | int r, c = 0; |
438 | ||
439 | /* Support for relabelling additional files or directories after loading the policy. For this, code in the | |
440 | * initrd simply has to drop in *.relabel files into /run/systemd/relabel-extra.d/. We'll read all such files | |
441 | * expecting one absolute path by line and will relabel each (and everyone below that in case the path refers | |
442 | * to a directory). These drop-in files are supposed to be absolutely minimal, and do not understand comments | |
443 | * and such. After the operation succeeded the files are removed, and the drop-in directory as well, if | |
444 | * possible. | |
445 | */ | |
446 | ||
48da02ec ZJS |
447 | r = conf_files_list(&files, ".relabel", NULL, |
448 | CONF_FILES_FILTER_MASKED | CONF_FILES_REGULAR, | |
36b12282 | 449 | "/run/systemd/relabel-extra.d/"); |
48da02ec ZJS |
450 | if (r < 0) |
451 | return log_error_errno(r, "Failed to enumerate /run/systemd/relabel-extra.d/, ignoring: %m"); | |
65e183d7 | 452 | |
48da02ec | 453 | STRV_FOREACH(file, files) { |
65e183d7 | 454 | _cleanup_fclose_ FILE *f = NULL; |
65e183d7 | 455 | |
48da02ec | 456 | f = fopen(*file, "re"); |
65e183d7 | 457 | if (!f) { |
48da02ec | 458 | log_warning_errno(errno, "Failed to open %s, ignoring: %m", *file); |
65e183d7 LP |
459 | continue; |
460 | } | |
65e183d7 LP |
461 | |
462 | for (;;) { | |
463 | _cleanup_free_ char *line = NULL; | |
464 | ||
465 | r = read_line(f, LONG_LINE_MAX, &line); | |
466 | if (r < 0) { | |
48da02ec | 467 | log_warning_errno(r, "Failed to read %s, ignoring: %m", *file); |
65e183d7 LP |
468 | break; |
469 | } | |
470 | if (r == 0) /* EOF */ | |
471 | break; | |
472 | ||
4ff361cc | 473 | path_simplify(line); |
65e183d7 LP |
474 | |
475 | if (!path_is_normalized(line)) { | |
476 | log_warning("Path to relabel is not normalized, ignoring: %s", line); | |
477 | continue; | |
478 | } | |
479 | ||
480 | if (!path_is_absolute(line)) { | |
481 | log_warning("Path to relabel is not absolute, ignoring: %s", line); | |
482 | continue; | |
483 | } | |
484 | ||
485 | log_debug("Relabelling additional file/directory '%s'.", line); | |
71de6847 | 486 | (void) label_fix(line, 0); |
aea0fe53 | 487 | (void) relabel_tree(line); |
65e183d7 LP |
488 | c++; |
489 | } | |
490 | ||
48da02ec ZJS |
491 | if (unlink(*file) < 0) |
492 | log_warning_errno(errno, "Failed to remove %s, ignoring: %m", *file); | |
65e183d7 LP |
493 | } |
494 | ||
48da02ec | 495 | /* Remove when we complete things. */ |
90b059b6 ZJS |
496 | if (rmdir("/run/systemd/relabel-extra.d") < 0 && |
497 | errno != ENOENT) | |
65e183d7 LP |
498 | log_warning_errno(errno, "Failed to remove /run/systemd/relabel-extra.d/ directory: %m"); |
499 | ||
500 | return c; | |
501 | } | |
0fff82e5 | 502 | #endif |
1829dc9d | 503 | |
f74349d8 | 504 | int mount_setup(bool loaded_policy, bool leave_propagation) { |
4428c49d | 505 | int r; |
8e274523 | 506 | |
400fac06 | 507 | r = mount_points_setup(ELEMENTSOF(mount_table), loaded_policy); |
68d4c452 LP |
508 | if (r < 0) |
509 | return r; | |
510 | ||
f9fa32f0 | 511 | #if HAVE_SELINUX || ENABLE_SMACK |
f1d19aa4 LP |
512 | /* Nodes in devtmpfs and /run need to be manually updated for |
513 | * the appropriate labels, after mounting. The other virtual | |
514 | * API file systems like /sys and /proc do not need that, they | |
515 | * use the same label for all their files. */ | |
0b3325e7 LP |
516 | if (loaded_policy) { |
517 | usec_t before_relabel, after_relabel; | |
c4217b43 | 518 | const char *i; |
65e183d7 | 519 | int n_extra; |
0b3325e7 LP |
520 | |
521 | before_relabel = now(CLOCK_MONOTONIC); | |
522 | ||
c4217b43 | 523 | FOREACH_STRING(i, "/dev", "/dev/shm", "/run") |
aea0fe53 | 524 | (void) relabel_tree(i); |
0b3325e7 | 525 | |
70a74ec6 | 526 | (void) relabel_cgroup_filesystems(); |
8739f23e | 527 | |
65e183d7 LP |
528 | n_extra = relabel_extra(); |
529 | ||
0b3325e7 LP |
530 | after_relabel = now(CLOCK_MONOTONIC); |
531 | ||
65e183d7 LP |
532 | log_info("Relabelled /dev, /dev/shm, /run, /sys/fs/cgroup%s in %s.", |
533 | n_extra > 0 ? ", additional files" : "", | |
5291f26d | 534 | FORMAT_TIMESPAN(after_relabel - before_relabel, 0)); |
3bbecb2f | 535 | } |
0fff82e5 | 536 | #endif |
1829dc9d | 537 | |
5c0532d1 | 538 | /* Create a few default symlinks, which are normally created |
f1d19aa4 | 539 | * by udevd, but some scripts might need them before we start |
5c0532d1 | 540 | * udevd. */ |
03cfe0d5 | 541 | dev_setup(NULL, UID_INVALID, GID_INVALID); |
5c0532d1 | 542 | |
dee22f39 LP |
543 | /* Mark the root directory as shared in regards to mount propagation. The kernel defaults to "private", but we |
544 | * think it makes more sense to have a default of "shared" so that nspawn and the container tools work out of | |
545 | * the box. If specific setups need other settings they can reset the propagation mode to private if | |
546 | * needed. Note that we set this only when we are invoked directly by the kernel. If we are invoked by a | |
547 | * container manager we assume the container manager knows what it is doing (for example, because it set up | |
548 | * some directories with different propagation modes). */ | |
f74349d8 | 549 | if (detect_container() <= 0 && !leave_propagation) |
c481f78b | 550 | if (mount(NULL, "/", NULL, MS_REC|MS_SHARED, NULL) < 0) |
56f64d95 | 551 | log_warning_errno(errno, "Failed to set up the root directory for shared mount propagation: %m"); |
b3ac5f8c | 552 | |
dee22f39 LP |
553 | /* Create a few directories we always want around, Note that sd_booted() checks for /run/systemd/system, so |
554 | * this mkdir really needs to stay for good, otherwise software that copied sd-daemon.c into their sources will | |
555 | * misdetect systemd. */ | |
c4b41707 AP |
556 | (void) mkdir_label("/run/systemd", 0755); |
557 | (void) mkdir_label("/run/systemd/system", 0755); | |
dee22f39 | 558 | |
bbb4e7f3 LP |
559 | /* Make sure we have a mount point to hide in sandboxes */ |
560 | (void) mkdir_label("/run/credentials", 0755); | |
561 | ||
48b747fa | 562 | /* Also create /run/systemd/inaccessible nodes, so that we always have something to mount |
9fac5029 | 563 | * inaccessible nodes from. If we run in a container the host might have created these for us already |
69e3234d | 564 | * in /run/host/inaccessible/. Use those if we can, since that way we likely get access to block/char |
9fac5029 LP |
565 | * device nodes that are inaccessible, and if userns is used to nodes that are on mounts owned by a |
566 | * userns outside the container and thus nicely read-only and not remountable. */ | |
567 | if (access("/run/host/inaccessible/", F_OK) < 0) { | |
568 | if (errno != ENOENT) | |
569 | log_debug_errno(errno, "Failed to check if /run/host/inaccessible exists, ignoring: %m"); | |
570 | ||
571 | (void) make_inaccessible_nodes("/run/systemd", UID_INVALID, GID_INVALID); | |
572 | } else | |
573 | (void) symlink("../host/inaccessible", "/run/systemd/inaccessible"); | |
fe80fcc7 | 574 | |
0c85a4f3 | 575 | return 0; |
8e274523 | 576 | } |