]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8e274523 | 2 | |
8e274523 | 3 | #include <errno.h> |
cf0fbc49 | 4 | #include <ftw.h> |
8e274523 | 5 | #include <stdlib.h> |
cf0fbc49 | 6 | #include <sys/mount.h> |
6f7729c1 | 7 | #include <sys/statvfs.h> |
5c0532d1 | 8 | #include <unistd.h> |
8e274523 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
64824462 | 11 | #include "bus-util.h" |
4349cd7c | 12 | #include "cgroup-util.h" |
48da02ec | 13 | #include "conf-files.h" |
fdb3deca | 14 | #include "cgroup-setup.h" |
4349cd7c | 15 | #include "dev-setup.h" |
0bb2f0f1 ZJS |
16 | #include "dirent-util.h" |
17 | #include "efi-loader.h" | |
65e183d7 | 18 | #include "fd-util.h" |
e07aefbd | 19 | #include "fileio.h" |
c4b41707 | 20 | #include "fs-util.h" |
4349cd7c | 21 | #include "label.h" |
8e274523 | 22 | #include "log.h" |
c9af1080 | 23 | #include "macro.h" |
49e942b2 | 24 | #include "mkdir.h" |
4349cd7c | 25 | #include "mount-setup.h" |
7d85383e | 26 | #include "mount-util.h" |
049af8ad | 27 | #include "mountpoint-util.h" |
d8b4d14d | 28 | #include "nulstr-util.h" |
9eb977db | 29 | #include "path-util.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 }, |
b67ec8e5 | 69 | { "devtmpfs", "/dev", "devtmpfs", "mode=755" TMPFS_LIMITS_DEV, MS_NOSUID|MS_NOEXEC|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 LP |
191 | if (FLAGS_SET(p->mode, MNT_FOLLOW_SYMLINK)) |
192 | r = mount(p->what, p->where, p->type, p->flags, p->options) < 0 ? -errno : 0; | |
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 | ||
250 | const char *const *x, *const *y; | |
251 | ||
252 | assert(controller); | |
253 | ||
254 | /* This will lookup which controller to mount another controller with. Input is a controller name, and output | |
255 | * is the other controller name. The function works both ways: you can input one and get the other, and input | |
256 | * the other to get the one. */ | |
257 | ||
258 | STRV_FOREACH_PAIR(x, y, pairs) { | |
259 | if (streq(controller, *x)) | |
260 | return *y; | |
261 | if (streq(controller, *y)) | |
262 | return *x; | |
263 | } | |
264 | ||
265 | return NULL; | |
266 | } | |
267 | ||
268 | static int symlink_controller(const char *target, const char *alias) { | |
269 | const char *a; | |
270 | int r; | |
271 | ||
272 | assert(target); | |
273 | assert(alias); | |
274 | ||
275 | a = strjoina("/sys/fs/cgroup/", alias); | |
276 | ||
277 | r = symlink_idempotent(target, a, false); | |
278 | if (r < 0) | |
279 | return log_error_errno(r, "Failed to create symlink %s: %m", a); | |
280 | ||
281 | #ifdef SMACK_RUN_LABEL | |
282 | const char *p; | |
283 | ||
284 | p = strjoina("/sys/fs/cgroup/", target); | |
285 | ||
286 | r = mac_smack_copy(a, p); | |
287 | if (r < 0 && r != -EOPNOTSUPP) | |
288 | return log_error_errno(r, "Failed to copy smack label from %s to %s: %m", p, a); | |
289 | #endif | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | int mount_cgroup_controllers(void) { | |
a6b26d90 | 295 | _cleanup_set_free_free_ Set *controllers = NULL; |
a641dcd9 | 296 | int r; |
2076ca54 | 297 | |
efdb0237 LP |
298 | if (!cg_is_legacy_wanted()) |
299 | return 0; | |
300 | ||
670802d4 | 301 | /* Mount all available cgroup controllers that are built into the kernel. */ |
6925a0de | 302 | r = cg_kernel_controllers(&controllers); |
b12afc8c LP |
303 | if (r < 0) |
304 | return log_error_errno(r, "Failed to enumerate cgroup controllers: %m"); | |
0c85a4f3 LP |
305 | |
306 | for (;;) { | |
a641dcd9 | 307 | _cleanup_free_ char *options = NULL, *controller = NULL, *where = NULL; |
143fadf3 | 308 | const char *other_controller; |
a6b26d90 ZJS |
309 | MountPoint p = { |
310 | .what = "cgroup", | |
311 | .type = "cgroup", | |
312 | .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
313 | .mode = MNT_IN_CONTAINER, | |
314 | }; | |
0c85a4f3 LP |
315 | |
316 | controller = set_steal_first(controllers); | |
317 | if (!controller) | |
318 | break; | |
319 | ||
143fadf3 LP |
320 | /* Check if we shall mount this together with another controller */ |
321 | other_controller = join_with(controller); | |
322 | if (other_controller) { | |
323 | _cleanup_free_ char *c = NULL; | |
324 | ||
325 | /* Check if the other controller is actually available in the kernel too */ | |
326 | c = set_remove(controllers, other_controller); | |
327 | if (c) { | |
328 | ||
329 | /* Join the two controllers into one string, and maintain a stable ordering */ | |
330 | if (strcmp(controller, other_controller) < 0) | |
331 | options = strjoin(controller, ",", other_controller); | |
332 | else | |
333 | options = strjoin(other_controller, ",", controller); | |
334 | if (!options) | |
335 | return log_oom(); | |
0c85a4f3 | 336 | } |
143fadf3 | 337 | } |
0c85a4f3 | 338 | |
143fadf3 LP |
339 | /* The simple case, where there's only one controller to mount together */ |
340 | if (!options) | |
ae2a15bc | 341 | options = TAKE_PTR(controller); |
0c85a4f3 | 342 | |
b910cc72 | 343 | where = path_join("/sys/fs/cgroup", options); |
a641dcd9 LP |
344 | if (!where) |
345 | return log_oom(); | |
346 | ||
347 | p.where = where; | |
0c85a4f3 | 348 | p.options = options; |
2076ca54 | 349 | |
4ef31082 | 350 | r = mount_one(&p, true); |
a6b26d90 ZJS |
351 | if (r < 0) |
352 | return r; | |
0c85a4f3 | 353 | |
143fadf3 LP |
354 | /* Create symlinks from the individual controller names, in case we have a joined mount */ |
355 | if (controller) | |
356 | (void) symlink_controller(options, controller); | |
357 | if (other_controller) | |
358 | (void) symlink_controller(options, other_controller); | |
2076ca54 LP |
359 | } |
360 | ||
143fadf3 | 361 | /* Now that we mounted everything, let's make the tmpfs the cgroup file systems are mounted into read-only. */ |
21935150 | 362 | (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 | 363 | |
a6b26d90 | 364 | return 0; |
2076ca54 LP |
365 | } |
366 | ||
f9fa32f0 | 367 | #if HAVE_SELINUX || ENABLE_SMACK |
1829dc9d LP |
368 | static int nftw_cb( |
369 | const char *fpath, | |
370 | const struct stat *sb, | |
371 | int tflag, | |
372 | struct FTW *ftwbuf) { | |
373 | ||
9fe117ea | 374 | /* No need to label /dev twice in a row... */ |
edb49778 LP |
375 | if (_unlikely_(ftwbuf->level == 0)) |
376 | return FTW_CONTINUE; | |
377 | ||
08c84981 | 378 | (void) label_fix(fpath, 0); |
af65c248 | 379 | |
edb49778 | 380 | /* /run/initramfs is static data and big, no need to |
af65c248 | 381 | * dynamically relabel its contents at boot... */ |
edb49778 LP |
382 | if (_unlikely_(ftwbuf->level == 1 && |
383 | tflag == FTW_D && | |
384 | streq(fpath, "/run/initramfs"))) | |
385 | return FTW_SKIP_SUBTREE; | |
9fe117ea | 386 | |
edb49778 | 387 | return FTW_CONTINUE; |
1829dc9d | 388 | }; |
6f7729c1 KN |
389 | |
390 | static int relabel_cgroup_filesystems(void) { | |
391 | int r; | |
392 | struct statfs st; | |
393 | ||
394 | r = cg_all_unified(); | |
395 | if (r == 0) { | |
396 | /* Temporarily remount the root cgroup filesystem to give it a proper label. Do this | |
397 | only when the filesystem has been already populated by a previous instance of systemd | |
398 | running from initrd. Otherwise don't remount anything and leave the filesystem read-write | |
399 | for the cgroup filesystems to be mounted inside. */ | |
771b7ead | 400 | if (statfs("/sys/fs/cgroup", &st) < 0) |
6f7729c1 | 401 | return log_error_errno(errno, "Failed to determine mount flags for /sys/fs/cgroup: %m"); |
6f7729c1 KN |
402 | |
403 | if (st.f_flags & ST_RDONLY) | |
21935150 | 404 | (void) mount_nofollow(NULL, "/sys/fs/cgroup", NULL, MS_REMOUNT, NULL); |
6f7729c1 KN |
405 | |
406 | (void) label_fix("/sys/fs/cgroup", 0); | |
771b7ead | 407 | (void) nftw("/sys/fs/cgroup", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL); |
6f7729c1 KN |
408 | |
409 | if (st.f_flags & ST_RDONLY) | |
21935150 | 410 | (void) mount_nofollow(NULL, "/sys/fs/cgroup", NULL, MS_REMOUNT|MS_RDONLY, NULL); |
771b7ead | 411 | |
6f7729c1 KN |
412 | } else if (r < 0) |
413 | return log_error_errno(r, "Failed to determine whether we are in all unified mode: %m"); | |
414 | ||
415 | return 0; | |
416 | } | |
65e183d7 LP |
417 | |
418 | static int relabel_extra(void) { | |
48da02ec ZJS |
419 | _cleanup_strv_free_ char **files = NULL; |
420 | char **file; | |
65e183d7 LP |
421 | int r, c = 0; |
422 | ||
423 | /* Support for relabelling additional files or directories after loading the policy. For this, code in the | |
424 | * initrd simply has to drop in *.relabel files into /run/systemd/relabel-extra.d/. We'll read all such files | |
425 | * expecting one absolute path by line and will relabel each (and everyone below that in case the path refers | |
426 | * to a directory). These drop-in files are supposed to be absolutely minimal, and do not understand comments | |
427 | * and such. After the operation succeeded the files are removed, and the drop-in directory as well, if | |
428 | * possible. | |
429 | */ | |
430 | ||
48da02ec ZJS |
431 | r = conf_files_list(&files, ".relabel", NULL, |
432 | CONF_FILES_FILTER_MASKED | CONF_FILES_REGULAR, | |
36b12282 | 433 | "/run/systemd/relabel-extra.d/"); |
48da02ec ZJS |
434 | if (r < 0) |
435 | return log_error_errno(r, "Failed to enumerate /run/systemd/relabel-extra.d/, ignoring: %m"); | |
65e183d7 | 436 | |
48da02ec | 437 | STRV_FOREACH(file, files) { |
65e183d7 | 438 | _cleanup_fclose_ FILE *f = NULL; |
65e183d7 | 439 | |
48da02ec | 440 | f = fopen(*file, "re"); |
65e183d7 | 441 | if (!f) { |
48da02ec | 442 | log_warning_errno(errno, "Failed to open %s, ignoring: %m", *file); |
65e183d7 LP |
443 | continue; |
444 | } | |
65e183d7 LP |
445 | |
446 | for (;;) { | |
447 | _cleanup_free_ char *line = NULL; | |
448 | ||
449 | r = read_line(f, LONG_LINE_MAX, &line); | |
450 | if (r < 0) { | |
48da02ec | 451 | log_warning_errno(r, "Failed to read %s, ignoring: %m", *file); |
65e183d7 LP |
452 | break; |
453 | } | |
454 | if (r == 0) /* EOF */ | |
455 | break; | |
456 | ||
457 | path_simplify(line, true); | |
458 | ||
459 | if (!path_is_normalized(line)) { | |
460 | log_warning("Path to relabel is not normalized, ignoring: %s", line); | |
461 | continue; | |
462 | } | |
463 | ||
464 | if (!path_is_absolute(line)) { | |
465 | log_warning("Path to relabel is not absolute, ignoring: %s", line); | |
466 | continue; | |
467 | } | |
468 | ||
469 | log_debug("Relabelling additional file/directory '%s'.", line); | |
71de6847 | 470 | (void) label_fix(line, 0); |
65e183d7 LP |
471 | (void) nftw(line, nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL); |
472 | c++; | |
473 | } | |
474 | ||
48da02ec ZJS |
475 | if (unlink(*file) < 0) |
476 | log_warning_errno(errno, "Failed to remove %s, ignoring: %m", *file); | |
65e183d7 LP |
477 | } |
478 | ||
48da02ec | 479 | /* Remove when we complete things. */ |
90b059b6 ZJS |
480 | if (rmdir("/run/systemd/relabel-extra.d") < 0 && |
481 | errno != ENOENT) | |
65e183d7 LP |
482 | log_warning_errno(errno, "Failed to remove /run/systemd/relabel-extra.d/ directory: %m"); |
483 | ||
484 | return c; | |
485 | } | |
0fff82e5 | 486 | #endif |
1829dc9d | 487 | |
f74349d8 | 488 | int mount_setup(bool loaded_policy, bool leave_propagation) { |
4428c49d | 489 | int r; |
8e274523 | 490 | |
400fac06 | 491 | r = mount_points_setup(ELEMENTSOF(mount_table), loaded_policy); |
68d4c452 LP |
492 | if (r < 0) |
493 | return r; | |
494 | ||
f9fa32f0 | 495 | #if HAVE_SELINUX || ENABLE_SMACK |
f1d19aa4 LP |
496 | /* Nodes in devtmpfs and /run need to be manually updated for |
497 | * the appropriate labels, after mounting. The other virtual | |
498 | * API file systems like /sys and /proc do not need that, they | |
499 | * use the same label for all their files. */ | |
0b3325e7 LP |
500 | if (loaded_policy) { |
501 | usec_t before_relabel, after_relabel; | |
502 | char timespan[FORMAT_TIMESPAN_MAX]; | |
c4217b43 | 503 | const char *i; |
65e183d7 | 504 | int n_extra; |
0b3325e7 LP |
505 | |
506 | before_relabel = now(CLOCK_MONOTONIC); | |
507 | ||
c4217b43 LP |
508 | FOREACH_STRING(i, "/dev", "/dev/shm", "/run") |
509 | (void) nftw(i, nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL); | |
0b3325e7 | 510 | |
70a74ec6 | 511 | (void) relabel_cgroup_filesystems(); |
8739f23e | 512 | |
65e183d7 LP |
513 | n_extra = relabel_extra(); |
514 | ||
0b3325e7 LP |
515 | after_relabel = now(CLOCK_MONOTONIC); |
516 | ||
65e183d7 LP |
517 | log_info("Relabelled /dev, /dev/shm, /run, /sys/fs/cgroup%s in %s.", |
518 | n_extra > 0 ? ", additional files" : "", | |
2fa4092c | 519 | format_timespan(timespan, sizeof(timespan), after_relabel - before_relabel, 0)); |
3bbecb2f | 520 | } |
0fff82e5 | 521 | #endif |
1829dc9d | 522 | |
5c0532d1 | 523 | /* Create a few default symlinks, which are normally created |
f1d19aa4 | 524 | * by udevd, but some scripts might need them before we start |
5c0532d1 | 525 | * udevd. */ |
03cfe0d5 | 526 | dev_setup(NULL, UID_INVALID, GID_INVALID); |
5c0532d1 | 527 | |
dee22f39 LP |
528 | /* Mark the root directory as shared in regards to mount propagation. The kernel defaults to "private", but we |
529 | * think it makes more sense to have a default of "shared" so that nspawn and the container tools work out of | |
530 | * the box. If specific setups need other settings they can reset the propagation mode to private if | |
531 | * needed. Note that we set this only when we are invoked directly by the kernel. If we are invoked by a | |
532 | * container manager we assume the container manager knows what it is doing (for example, because it set up | |
533 | * some directories with different propagation modes). */ | |
f74349d8 | 534 | if (detect_container() <= 0 && !leave_propagation) |
c481f78b | 535 | if (mount(NULL, "/", NULL, MS_REC|MS_SHARED, NULL) < 0) |
56f64d95 | 536 | log_warning_errno(errno, "Failed to set up the root directory for shared mount propagation: %m"); |
b3ac5f8c | 537 | |
dee22f39 LP |
538 | /* Create a few directories we always want around, Note that sd_booted() checks for /run/systemd/system, so |
539 | * this mkdir really needs to stay for good, otherwise software that copied sd-daemon.c into their sources will | |
540 | * misdetect systemd. */ | |
c4b41707 AP |
541 | (void) mkdir_label("/run/systemd", 0755); |
542 | (void) mkdir_label("/run/systemd/system", 0755); | |
dee22f39 | 543 | |
bbb4e7f3 LP |
544 | /* Make sure we have a mount point to hide in sandboxes */ |
545 | (void) mkdir_label("/run/credentials", 0755); | |
546 | ||
48b747fa | 547 | /* Also create /run/systemd/inaccessible nodes, so that we always have something to mount |
9fac5029 | 548 | * inaccessible nodes from. If we run in a container the host might have created these for us already |
69e3234d | 549 | * in /run/host/inaccessible/. Use those if we can, since that way we likely get access to block/char |
9fac5029 LP |
550 | * device nodes that are inaccessible, and if userns is used to nodes that are on mounts owned by a |
551 | * userns outside the container and thus nicely read-only and not remountable. */ | |
552 | if (access("/run/host/inaccessible/", F_OK) < 0) { | |
553 | if (errno != ENOENT) | |
554 | log_debug_errno(errno, "Failed to check if /run/host/inaccessible exists, ignoring: %m"); | |
555 | ||
556 | (void) make_inaccessible_nodes("/run/systemd", UID_INVALID, GID_INVALID); | |
557 | } else | |
558 | (void) symlink("../host/inaccessible", "/run/systemd/inaccessible"); | |
fe80fcc7 | 559 | |
0c85a4f3 | 560 | return 0; |
8e274523 | 561 | } |