]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/mount-setup.c
Merge pull request #3526 from fbuihuu/fix-console-log-color
[thirdparty/systemd.git] / src / core / mount-setup.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 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 <errno.h>
21 #include <ftw.h>
22 #include <stdlib.h>
23 #include <sys/mount.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "bus-util.h"
28 #include "cgroup-util.h"
29 #include "dev-setup.h"
30 #include "efivars.h"
31 #include "label.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "missing.h"
35 #include "mkdir.h"
36 #include "mount-setup.h"
37 #include "mount-util.h"
38 #include "path-util.h"
39 #include "set.h"
40 #include "smack-util.h"
41 #include "strv.h"
42 #include "user-util.h"
43 #include "util.h"
44 #include "virt.h"
45
46 typedef enum MountMode {
47 MNT_NONE = 0,
48 MNT_FATAL = 1 << 0,
49 MNT_IN_CONTAINER = 1 << 1,
50 } MountMode;
51
52 typedef struct MountPoint {
53 const char *what;
54 const char *where;
55 const char *type;
56 const char *options;
57 unsigned long flags;
58 bool (*condition_fn)(void);
59 MountMode mode;
60 } MountPoint;
61
62 /* The first three entries we might need before SELinux is up. The
63 * fourth (securityfs) is needed by IMA to load a custom policy. The
64 * other ones we can delay until SELinux and IMA are loaded. When
65 * SMACK is enabled we need smackfs, too, so it's a fifth one. */
66 #ifdef HAVE_SMACK
67 #define N_EARLY_MOUNT 5
68 #else
69 #define N_EARLY_MOUNT 4
70 #endif
71
72 static const MountPoint mount_table[] = {
73 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
74 NULL, MNT_FATAL|MNT_IN_CONTAINER },
75 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
76 NULL, MNT_FATAL|MNT_IN_CONTAINER },
77 { "devtmpfs", "/dev", "devtmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME,
78 NULL, MNT_FATAL|MNT_IN_CONTAINER },
79 { "securityfs", "/sys/kernel/security", "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
80 NULL, MNT_NONE },
81 #ifdef HAVE_SMACK
82 { "smackfs", "/sys/fs/smackfs", "smackfs", "smackfsdef=*", MS_NOSUID|MS_NOEXEC|MS_NODEV,
83 mac_smack_use, MNT_FATAL },
84 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
85 mac_smack_use, MNT_FATAL },
86 #endif
87 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
88 NULL, MNT_FATAL|MNT_IN_CONTAINER },
89 { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC,
90 NULL, MNT_IN_CONTAINER },
91 #ifdef HAVE_SMACK
92 { "tmpfs", "/run", "tmpfs", "mode=755,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
93 mac_smack_use, MNT_FATAL },
94 #endif
95 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
96 NULL, MNT_FATAL|MNT_IN_CONTAINER },
97 { "cgroup", "/sys/fs/cgroup", "cgroup2", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
98 cg_is_unified_wanted, MNT_FATAL|MNT_IN_CONTAINER },
99 { "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
100 cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
101 { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd,xattr", MS_NOSUID|MS_NOEXEC|MS_NODEV,
102 cg_is_legacy_wanted, MNT_IN_CONTAINER },
103 { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV,
104 cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER },
105 { "pstore", "/sys/fs/pstore", "pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
106 NULL, MNT_NONE },
107 #ifdef ENABLE_EFI
108 { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
109 is_efi_boot, MNT_NONE },
110 #endif
111 };
112
113 /* These are API file systems that might be mounted by other software,
114 * we just list them here so that we know that we should ignore them */
115
116 static const char ignore_paths[] =
117 /* SELinux file systems */
118 "/sys/fs/selinux\0"
119 /* Container bind mounts */
120 "/proc/sys\0"
121 "/dev/console\0"
122 "/proc/kmsg\0";
123
124 bool mount_point_is_api(const char *path) {
125 unsigned i;
126
127 /* Checks if this mount point is considered "API", and hence
128 * should be ignored */
129
130 for (i = 0; i < ELEMENTSOF(mount_table); i ++)
131 if (path_equal(path, mount_table[i].where))
132 return true;
133
134 return path_startswith(path, "/sys/fs/cgroup/");
135 }
136
137 bool mount_point_ignore(const char *path) {
138 const char *i;
139
140 NULSTR_FOREACH(i, ignore_paths)
141 if (path_equal(path, i))
142 return true;
143
144 return false;
145 }
146
147 static int mount_one(const MountPoint *p, bool relabel) {
148 int r;
149
150 assert(p);
151
152 if (p->condition_fn && !p->condition_fn())
153 return 0;
154
155 /* Relabel first, just in case */
156 if (relabel)
157 (void) label_fix(p->where, true, true);
158
159 r = path_is_mount_point(p->where, AT_SYMLINK_FOLLOW);
160 if (r < 0 && r != -ENOENT) {
161 log_full_errno((p->mode & MNT_FATAL) ? LOG_ERR : LOG_DEBUG, r, "Failed to determine whether %s is a mount point: %m", p->where);
162 return (p->mode & MNT_FATAL) ? r : 0;
163 }
164 if (r > 0)
165 return 0;
166
167 /* Skip securityfs in a container */
168 if (!(p->mode & MNT_IN_CONTAINER) && detect_container() > 0)
169 return 0;
170
171 /* The access mode here doesn't really matter too much, since
172 * the mounted file system will take precedence anyway. */
173 if (relabel)
174 (void) mkdir_p_label(p->where, 0755);
175 else
176 (void) mkdir_p(p->where, 0755);
177
178 log_debug("Mounting %s to %s of type %s with options %s.",
179 p->what,
180 p->where,
181 p->type,
182 strna(p->options));
183
184 if (mount(p->what,
185 p->where,
186 p->type,
187 p->flags,
188 p->options) < 0) {
189 log_full_errno((p->mode & MNT_FATAL) ? LOG_ERR : LOG_DEBUG, errno, "Failed to mount %s at %s: %m", p->type, p->where);
190 return (p->mode & MNT_FATAL) ? -errno : 0;
191 }
192
193 /* Relabel again, since we now mounted something fresh here */
194 if (relabel)
195 (void) label_fix(p->where, false, false);
196
197 return 1;
198 }
199
200 static int mount_points_setup(unsigned n, bool loaded_policy) {
201 unsigned i;
202 int r = 0;
203
204 for (i = 0; i < n; i ++) {
205 int j;
206
207 j = mount_one(mount_table + i, loaded_policy);
208 if (j != 0 && r >= 0)
209 r = j;
210 }
211
212 return r;
213 }
214
215 int mount_setup_early(void) {
216 assert_cc(N_EARLY_MOUNT <= ELEMENTSOF(mount_table));
217
218 /* Do a minimal mount of /proc and friends to enable the most
219 * basic stuff, such as SELinux */
220 return mount_points_setup(N_EARLY_MOUNT, false);
221 }
222
223 int mount_cgroup_controllers(char ***join_controllers) {
224 _cleanup_set_free_free_ Set *controllers = NULL;
225 int r;
226
227 if (!cg_is_legacy_wanted())
228 return 0;
229
230 /* Mount all available cgroup controllers that are built into the kernel. */
231
232 controllers = set_new(&string_hash_ops);
233 if (!controllers)
234 return log_oom();
235
236 r = cg_kernel_controllers(controllers);
237 if (r < 0)
238 return log_error_errno(r, "Failed to enumerate cgroup controllers: %m");
239
240 for (;;) {
241 _cleanup_free_ char *options = NULL, *controller = NULL, *where = NULL;
242 MountPoint p = {
243 .what = "cgroup",
244 .type = "cgroup",
245 .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV,
246 .mode = MNT_IN_CONTAINER,
247 };
248 char ***k = NULL;
249
250 controller = set_steal_first(controllers);
251 if (!controller)
252 break;
253
254 if (join_controllers)
255 for (k = join_controllers; *k; k++)
256 if (strv_find(*k, controller))
257 break;
258
259 if (k && *k) {
260 char **i, **j;
261
262 for (i = *k, j = *k; *i; i++) {
263
264 if (!streq(*i, controller)) {
265 _cleanup_free_ char *t;
266
267 t = set_remove(controllers, *i);
268 if (!t) {
269 free(*i);
270 continue;
271 }
272 }
273
274 *(j++) = *i;
275 }
276
277 *j = NULL;
278
279 options = strv_join(*k, ",");
280 if (!options)
281 return log_oom();
282 } else {
283 options = controller;
284 controller = NULL;
285 }
286
287 where = strappend("/sys/fs/cgroup/", options);
288 if (!where)
289 return log_oom();
290
291 p.where = where;
292 p.options = options;
293
294 r = mount_one(&p, true);
295 if (r < 0)
296 return r;
297
298 if (r > 0 && k && *k) {
299 char **i;
300
301 for (i = *k; *i; i++) {
302 _cleanup_free_ char *t = NULL;
303
304 t = strappend("/sys/fs/cgroup/", *i);
305 if (!t)
306 return log_oom();
307
308 r = symlink(options, t);
309 if (r >= 0) {
310 #ifdef SMACK_RUN_LABEL
311 _cleanup_free_ char *src;
312 src = strappend("/sys/fs/cgroup/", options);
313 if (!src)
314 return log_oom();
315 r = mac_smack_copy(t, src);
316 if (r < 0 && r != -EOPNOTSUPP)
317 return log_error_errno(r, "Failed to copy smack label from %s to %s: %m", src, t);
318 #endif
319 } else if (errno != EEXIST)
320 return log_error_errno(errno, "Failed to create symlink %s: %m", t);
321 }
322 }
323 }
324
325 /* Now that we mounted everything, let's make the tmpfs the
326 * cgroup file systems are mounted into read-only. */
327 (void) mount("tmpfs", "/sys/fs/cgroup", "tmpfs", MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
328
329 return 0;
330 }
331
332 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
333 static int nftw_cb(
334 const char *fpath,
335 const struct stat *sb,
336 int tflag,
337 struct FTW *ftwbuf) {
338
339 /* No need to label /dev twice in a row... */
340 if (_unlikely_(ftwbuf->level == 0))
341 return FTW_CONTINUE;
342
343 label_fix(fpath, false, false);
344
345 /* /run/initramfs is static data and big, no need to
346 * dynamically relabel its contents at boot... */
347 if (_unlikely_(ftwbuf->level == 1 &&
348 tflag == FTW_D &&
349 streq(fpath, "/run/initramfs")))
350 return FTW_SKIP_SUBTREE;
351
352 return FTW_CONTINUE;
353 };
354 #endif
355
356 int mount_setup(bool loaded_policy) {
357 int r = 0;
358
359 r = mount_points_setup(ELEMENTSOF(mount_table), loaded_policy);
360
361 if (r < 0)
362 return r;
363
364 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
365 /* Nodes in devtmpfs and /run need to be manually updated for
366 * the appropriate labels, after mounting. The other virtual
367 * API file systems like /sys and /proc do not need that, they
368 * use the same label for all their files. */
369 if (loaded_policy) {
370 usec_t before_relabel, after_relabel;
371 char timespan[FORMAT_TIMESPAN_MAX];
372
373 before_relabel = now(CLOCK_MONOTONIC);
374
375 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
376 nftw("/dev/shm", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
377 nftw("/run", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
378
379 after_relabel = now(CLOCK_MONOTONIC);
380
381 log_info("Relabelled /dev and /run in %s.",
382 format_timespan(timespan, sizeof(timespan), after_relabel - before_relabel, 0));
383 }
384 #endif
385
386 /* Create a few default symlinks, which are normally created
387 * by udevd, but some scripts might need them before we start
388 * udevd. */
389 dev_setup(NULL, UID_INVALID, GID_INVALID);
390
391 /* Mark the root directory as shared in regards to mount
392 * propagation. The kernel defaults to "private", but we think
393 * it makes more sense to have a default of "shared" so that
394 * nspawn and the container tools work out of the box. If
395 * specific setups need other settings they can reset the
396 * propagation mode to private if needed. */
397 if (detect_container() <= 0)
398 if (mount(NULL, "/", NULL, MS_REC|MS_SHARED, NULL) < 0)
399 log_warning_errno(errno, "Failed to set up the root directory for shared mount propagation: %m");
400
401 /* Create a few directories we always want around, Note that
402 * sd_booted() checks for /run/systemd/system, so this mkdir
403 * really needs to stay for good, otherwise software that
404 * copied sd-daemon.c into their sources will misdetect
405 * systemd. */
406 mkdir_label("/run/systemd", 0755);
407 mkdir_label("/run/systemd/system", 0755);
408 mkdir_label("/run/systemd/inaccessible", 0000);
409
410 return 0;
411 }