]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/mount-util.c
Merge pull request #12441 from ssahani/bridge-fdb
[thirdparty/systemd.git] / src / shared / mount-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mount.h>
7 #include <sys/stat.h>
8 #include <sys/statvfs.h>
9 #include <unistd.h>
10
11 #include "alloc-util.h"
12 #include "extract-word.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fs-util.h"
16 #include "hashmap.h"
17 #include "libmount-util.h"
18 #include "mount-util.h"
19 #include "mountpoint-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "set.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26
27 int umount_recursive(const char *prefix, int flags) {
28 int n = 0, r;
29 bool again;
30
31 /* Try to umount everything recursively below a
32 * directory. Also, take care of stacked mounts, and keep
33 * unmounting them until they are gone. */
34
35 do {
36 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
37 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
38
39 again = false;
40
41 r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter);
42 if (r < 0)
43 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
44
45 for (;;) {
46 struct libmnt_fs *fs;
47 const char *path;
48
49 r = mnt_table_next_fs(table, iter, &fs);
50 if (r == 1)
51 break;
52 if (r < 0)
53 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
54
55 path = mnt_fs_get_target(fs);
56 if (!path)
57 continue;
58
59 if (!path_startswith(path, prefix))
60 continue;
61
62 if (umount2(path, flags) < 0) {
63 r = log_debug_errno(errno, "Failed to umount %s: %m", path);
64 continue;
65 }
66
67 log_debug("Successfully unmounted %s", path);
68
69 again = true;
70 n++;
71
72 break;
73 }
74
75 } while (again);
76
77 return n;
78 }
79
80 static int get_mount_flags(const char *path, unsigned long *flags) {
81 struct statvfs buf;
82
83 if (statvfs(path, &buf) < 0)
84 return -errno;
85 *flags = buf.f_flag;
86 return 0;
87 }
88
89 /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
90 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
91 int bind_remount_recursive_with_mountinfo(
92 const char *prefix,
93 unsigned long new_flags,
94 unsigned long flags_mask,
95 char **blacklist,
96 FILE *proc_self_mountinfo) {
97
98 _cleanup_set_free_free_ Set *done = NULL;
99 _cleanup_free_ char *cleaned = NULL;
100 int r;
101
102 assert(proc_self_mountinfo);
103
104 /* Recursively remount a directory (and all its submounts) read-only or read-write. If the directory is already
105 * mounted, we reuse the mount and simply mark it MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write
106 * operation). If it isn't we first make it one. Afterwards we apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to
107 * all submounts we can access, too. When mounts are stacked on the same mount point we only care for each
108 * individual "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We
109 * do not have any effect on future submounts that might get propagated, they migt be writable. This includes
110 * future submounts that have been triggered via autofs.
111 *
112 * If the "blacklist" parameter is specified it may contain a list of subtrees to exclude from the
113 * remount operation. Note that we'll ignore the blacklist for the top-level path. */
114
115 cleaned = strdup(prefix);
116 if (!cleaned)
117 return -ENOMEM;
118
119 path_simplify(cleaned, false);
120
121 done = set_new(&path_hash_ops);
122 if (!done)
123 return -ENOMEM;
124
125 for (;;) {
126 _cleanup_set_free_free_ Set *todo = NULL;
127 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
128 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
129 bool top_autofs = false;
130 char *x;
131 unsigned long orig_flags;
132
133 todo = set_new(&path_hash_ops);
134 if (!todo)
135 return -ENOMEM;
136
137 rewind(proc_self_mountinfo);
138
139 r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter);
140 if (r < 0)
141 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
142
143 for (;;) {
144 struct libmnt_fs *fs;
145 const char *path, *type;
146
147 r = mnt_table_next_fs(table, iter, &fs);
148 if (r == 1)
149 break;
150 if (r < 0)
151 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
152
153 path = mnt_fs_get_target(fs);
154 type = mnt_fs_get_fstype(fs);
155 if (!path || !type)
156 continue;
157
158 if (!path_startswith(path, cleaned))
159 continue;
160
161 /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount
162 * we shall operate on. */
163 if (!path_equal(path, cleaned)) {
164 bool blacklisted = false;
165 char **i;
166
167 STRV_FOREACH(i, blacklist) {
168 if (path_equal(*i, cleaned))
169 continue;
170
171 if (!path_startswith(*i, cleaned))
172 continue;
173
174 if (path_startswith(path, *i)) {
175 blacklisted = true;
176 log_debug("Not remounting %s blacklisted by %s, called for %s",
177 path, *i, cleaned);
178 break;
179 }
180 }
181 if (blacklisted)
182 continue;
183 }
184
185 /* Let's ignore autofs mounts. If they aren't
186 * triggered yet, we want to avoid triggering
187 * them, as we don't make any guarantees for
188 * future submounts anyway. If they are
189 * already triggered, then we will find
190 * another entry for this. */
191 if (streq(type, "autofs")) {
192 top_autofs = top_autofs || path_equal(path, cleaned);
193 continue;
194 }
195
196 if (!set_contains(done, path)) {
197 r = set_put_strdup(todo, path);
198 if (r < 0)
199 return r;
200 }
201 }
202
203 /* If we have no submounts to process anymore and if
204 * the root is either already done, or an autofs, we
205 * are done */
206 if (set_isempty(todo) &&
207 (top_autofs || set_contains(done, cleaned)))
208 return 0;
209
210 if (!set_contains(done, cleaned) &&
211 !set_contains(todo, cleaned)) {
212 /* The prefix directory itself is not yet a mount, make it one. */
213 if (mount(cleaned, cleaned, NULL, MS_BIND|MS_REC, NULL) < 0)
214 return -errno;
215
216 orig_flags = 0;
217 (void) get_mount_flags(cleaned, &orig_flags);
218 orig_flags &= ~MS_RDONLY;
219
220 if (mount(NULL, cleaned, NULL, (orig_flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags, NULL) < 0)
221 return -errno;
222
223 log_debug("Made top-level directory %s a mount point.", prefix);
224
225 r = set_put_strdup(done, cleaned);
226 if (r < 0)
227 return r;
228 }
229
230 while ((x = set_steal_first(todo))) {
231
232 r = set_consume(done, x);
233 if (IN_SET(r, 0, -EEXIST))
234 continue;
235 if (r < 0)
236 return r;
237
238 /* Deal with mount points that are obstructed by a later mount */
239 r = path_is_mount_point(x, NULL, 0);
240 if (IN_SET(r, 0, -ENOENT))
241 continue;
242 if (IN_SET(r, -EACCES, -EPERM)) {
243 /* Even if root user invoke this, submounts under private FUSE or NFS mount points
244 * may not be acceessed. E.g.,
245 *
246 * $ bindfs --no-allow-other ~/mnt/mnt ~/mnt/mnt
247 * $ bindfs --no-allow-other ~/mnt ~/mnt
248 *
249 * Then, root user cannot access the mount point ~/mnt/mnt.
250 * In such cases, the submounts are ignored, as we have no way to manage them. */
251 log_debug_errno(r, "Failed to determine '%s' is mount point or not, ignoring: %m", x);
252 continue;
253 }
254 if (r < 0)
255 return r;
256
257 /* Try to reuse the original flag set */
258 orig_flags = 0;
259 (void) get_mount_flags(x, &orig_flags);
260 orig_flags &= ~MS_RDONLY;
261
262 if (mount(NULL, x, NULL, (orig_flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags, NULL) < 0)
263 return -errno;
264
265 log_debug("Remounted %s read-only.", x);
266 }
267 }
268 }
269
270 int bind_remount_recursive(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **blacklist) {
271 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
272 int r;
273
274 r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
275 if (r < 0)
276 return r;
277
278 return bind_remount_recursive_with_mountinfo(prefix, new_flags, flags_mask, blacklist, proc_self_mountinfo);
279 }
280
281 int mount_move_root(const char *path) {
282 assert(path);
283
284 if (chdir(path) < 0)
285 return -errno;
286
287 if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
288 return -errno;
289
290 if (chroot(".") < 0)
291 return -errno;
292
293 if (chdir("/") < 0)
294 return -errno;
295
296 return 0;
297 }
298
299 int repeat_unmount(const char *path, int flags) {
300 bool done = false;
301
302 assert(path);
303
304 /* If there are multiple mounts on a mount point, this
305 * removes them all */
306
307 for (;;) {
308 if (umount2(path, flags) < 0) {
309
310 if (errno == EINVAL)
311 return done;
312
313 return -errno;
314 }
315
316 done = true;
317 }
318 }
319
320 const char* mode_to_inaccessible_node(mode_t mode) {
321 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created during
322 * early boot by PID 1. In some cases we lacked the privs to create the character and block devices (maybe
323 * because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a devices policy that excludes
324 * device nodes with major and minor of 0), but that's fine, in that case we use an AF_UNIX file node instead,
325 * which is not the same, but close enough for most uses. And most importantly, the kernel allows bind mounts
326 * from socket nodes to any non-directory file nodes, and that's the most important thing that matters. */
327
328 switch(mode & S_IFMT) {
329 case S_IFREG:
330 return "/run/systemd/inaccessible/reg";
331
332 case S_IFDIR:
333 return "/run/systemd/inaccessible/dir";
334
335 case S_IFCHR:
336 if (access("/run/systemd/inaccessible/chr", F_OK) == 0)
337 return "/run/systemd/inaccessible/chr";
338 return "/run/systemd/inaccessible/sock";
339
340 case S_IFBLK:
341 if (access("/run/systemd/inaccessible/blk", F_OK) == 0)
342 return "/run/systemd/inaccessible/blk";
343 return "/run/systemd/inaccessible/sock";
344
345 case S_IFIFO:
346 return "/run/systemd/inaccessible/fifo";
347
348 case S_IFSOCK:
349 return "/run/systemd/inaccessible/sock";
350 }
351 return NULL;
352 }
353
354 #define FLAG(name) (flags & name ? STRINGIFY(name) "|" : "")
355 static char* mount_flags_to_string(long unsigned flags) {
356 char *x;
357 _cleanup_free_ char *y = NULL;
358 long unsigned overflow;
359
360 overflow = flags & ~(MS_RDONLY |
361 MS_NOSUID |
362 MS_NODEV |
363 MS_NOEXEC |
364 MS_SYNCHRONOUS |
365 MS_REMOUNT |
366 MS_MANDLOCK |
367 MS_DIRSYNC |
368 MS_NOATIME |
369 MS_NODIRATIME |
370 MS_BIND |
371 MS_MOVE |
372 MS_REC |
373 MS_SILENT |
374 MS_POSIXACL |
375 MS_UNBINDABLE |
376 MS_PRIVATE |
377 MS_SLAVE |
378 MS_SHARED |
379 MS_RELATIME |
380 MS_KERNMOUNT |
381 MS_I_VERSION |
382 MS_STRICTATIME |
383 MS_LAZYTIME);
384
385 if (flags == 0 || overflow != 0)
386 if (asprintf(&y, "%lx", overflow) < 0)
387 return NULL;
388
389 x = strjoin(FLAG(MS_RDONLY),
390 FLAG(MS_NOSUID),
391 FLAG(MS_NODEV),
392 FLAG(MS_NOEXEC),
393 FLAG(MS_SYNCHRONOUS),
394 FLAG(MS_REMOUNT),
395 FLAG(MS_MANDLOCK),
396 FLAG(MS_DIRSYNC),
397 FLAG(MS_NOATIME),
398 FLAG(MS_NODIRATIME),
399 FLAG(MS_BIND),
400 FLAG(MS_MOVE),
401 FLAG(MS_REC),
402 FLAG(MS_SILENT),
403 FLAG(MS_POSIXACL),
404 FLAG(MS_UNBINDABLE),
405 FLAG(MS_PRIVATE),
406 FLAG(MS_SLAVE),
407 FLAG(MS_SHARED),
408 FLAG(MS_RELATIME),
409 FLAG(MS_KERNMOUNT),
410 FLAG(MS_I_VERSION),
411 FLAG(MS_STRICTATIME),
412 FLAG(MS_LAZYTIME),
413 y);
414 if (!x)
415 return NULL;
416 if (!y)
417 x[strlen(x) - 1] = '\0'; /* truncate the last | */
418 return x;
419 }
420
421 int mount_verbose(
422 int error_log_level,
423 const char *what,
424 const char *where,
425 const char *type,
426 unsigned long flags,
427 const char *options) {
428
429 _cleanup_free_ char *fl = NULL, *o = NULL;
430 unsigned long f;
431 int r;
432
433 r = mount_option_mangle(options, flags, &f, &o);
434 if (r < 0)
435 return log_full_errno(error_log_level, r,
436 "Failed to mangle mount options %s: %m",
437 strempty(options));
438
439 fl = mount_flags_to_string(f);
440
441 if ((f & MS_REMOUNT) && !what && !type)
442 log_debug("Remounting %s (%s \"%s\")...",
443 where, strnull(fl), strempty(o));
444 else if (!what && !type)
445 log_debug("Mounting %s (%s \"%s\")...",
446 where, strnull(fl), strempty(o));
447 else if ((f & MS_BIND) && !type)
448 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
449 what, where, strnull(fl), strempty(o));
450 else if (f & MS_MOVE)
451 log_debug("Moving mount %s → %s (%s \"%s\")...",
452 what, where, strnull(fl), strempty(o));
453 else
454 log_debug("Mounting %s on %s (%s \"%s\")...",
455 strna(type), where, strnull(fl), strempty(o));
456 if (mount(what, where, type, f, o) < 0)
457 return log_full_errno(error_log_level, errno,
458 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
459 strna(what), strna(type), where, strnull(fl), strempty(o));
460 return 0;
461 }
462
463 int umount_verbose(const char *what) {
464 log_debug("Umounting %s...", what);
465 if (umount(what) < 0)
466 return log_error_errno(errno, "Failed to unmount %s: %m", what);
467 return 0;
468 }
469
470 int mount_option_mangle(
471 const char *options,
472 unsigned long mount_flags,
473 unsigned long *ret_mount_flags,
474 char **ret_remaining_options) {
475
476 const struct libmnt_optmap *map;
477 _cleanup_free_ char *ret = NULL;
478 const char *p;
479 int r;
480
481 /* This extracts mount flags from the mount options, and store
482 * non-mount-flag options to '*ret_remaining_options'.
483 * E.g.,
484 * "rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000"
485 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
486 * "size=1630748k,mode=700,uid=1000,gid=1000".
487 * See more examples in test-mount-utils.c.
488 *
489 * Note that if 'options' does not contain any non-mount-flag options,
490 * then '*ret_remaining_options' is set to NULL instead of empty string.
491 * Note that this does not check validity of options stored in
492 * '*ret_remaining_options'.
493 * Note that if 'options' is NULL, then this just copies 'mount_flags'
494 * to '*ret_mount_flags'. */
495
496 assert(ret_mount_flags);
497 assert(ret_remaining_options);
498
499 map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
500 if (!map)
501 return -EINVAL;
502
503 p = options;
504 for (;;) {
505 _cleanup_free_ char *word = NULL;
506 const struct libmnt_optmap *ent;
507
508 r = extract_first_word(&p, &word, ",", EXTRACT_QUOTES);
509 if (r < 0)
510 return r;
511 if (r == 0)
512 break;
513
514 for (ent = map; ent->name; ent++) {
515 /* All entries in MNT_LINUX_MAP do not take any argument.
516 * Thus, ent->name does not contain "=" or "[=]". */
517 if (!streq(word, ent->name))
518 continue;
519
520 if (!(ent->mask & MNT_INVERT))
521 mount_flags |= ent->id;
522 else if (mount_flags & ent->id)
523 mount_flags ^= ent->id;
524
525 break;
526 }
527
528 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
529 if (!ent->name && !strextend_with_separator(&ret, ",", word, NULL))
530 return -ENOMEM;
531 }
532
533 *ret_mount_flags = mount_flags;
534 *ret_remaining_options = TAKE_PTR(ret);
535
536 return 0;
537 }