]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4349cd7c | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <stdlib.h> | |
4349cd7c | 5 | #include <sys/mount.h> |
11c3a366 | 6 | #include <sys/stat.h> |
4349cd7c | 7 | #include <sys/statvfs.h> |
11c3a366 | 8 | #include <unistd.h> |
35fd3558 | 9 | #include <linux/loop.h> |
3657d3a0 | 10 | #if WANT_LINUX_FS_H |
35fd3558 | 11 | #include <linux/fs.h> |
3657d3a0 | 12 | #endif |
4349cd7c | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
f461a28d | 15 | #include "chase.h" |
70599967 | 16 | #include "dissect-image.h" |
45a68ed3 | 17 | #include "exec-util.h" |
9e7f941a | 18 | #include "extract-word.h" |
4349cd7c LP |
19 | #include "fd-util.h" |
20 | #include "fileio.h" | |
e1873695 | 21 | #include "fs-util.h" |
e2341b6b | 22 | #include "glyph-util.h" |
93cc7779 | 23 | #include "hashmap.h" |
2e776ed6 | 24 | #include "initrd-util.h" |
0690160e | 25 | #include "label-util.h" |
13dcfe46 | 26 | #include "libmount-util.h" |
1c092b62 | 27 | #include "missing_mount.h" |
35fd3558 | 28 | #include "missing_syscall.h" |
35cd0ba5 | 29 | #include "mkdir-label.h" |
4349cd7c | 30 | #include "mount-util.h" |
049af8ad | 31 | #include "mountpoint-util.h" |
2338a175 | 32 | #include "namespace-util.h" |
4349cd7c LP |
33 | #include "parse-util.h" |
34 | #include "path-util.h" | |
6af52c3a | 35 | #include "process-util.h" |
4349cd7c | 36 | #include "set.h" |
f63a2c48 | 37 | #include "sort-util.h" |
28126409 | 38 | #include "stat-util.h" |
15a5e950 | 39 | #include "stdio-util.h" |
57c10a56 | 40 | #include "string-table.h" |
4349cd7c | 41 | #include "string-util.h" |
6b7c9f8b | 42 | #include "strv.h" |
6af52c3a | 43 | #include "tmpfile-util.h" |
70599967 | 44 | #include "user-util.h" |
4349cd7c | 45 | |
84bcb394 | 46 | int umount_recursive_full(const char *prefix, int flags, char **keep) { |
890c14e3 | 47 | _cleanup_fclose_ FILE *f = NULL; |
4349cd7c LP |
48 | int n = 0, r; |
49 | ||
9d0619de LP |
50 | /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and |
51 | * keep unmounting them until they are gone. */ | |
4349cd7c | 52 | |
890c14e3 LP |
53 | f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */ |
54 | if (!f) | |
55 | return log_debug_errno(errno, "Failed to open /proc/self/mountinfo: %m"); | |
56 | ||
57 | for (;;) { | |
13dcfe46 ZJS |
58 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
59 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
890c14e3 | 60 | bool again = false; |
4349cd7c | 61 | |
890c14e3 | 62 | r = libmount_parse("/proc/self/mountinfo", f, &table, &iter); |
fdeea3f4 | 63 | if (r < 0) |
13dcfe46 | 64 | return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); |
35bbbf85 | 65 | |
4349cd7c | 66 | for (;;) { |
84bcb394 | 67 | bool shall_keep = false; |
13dcfe46 ZJS |
68 | struct libmnt_fs *fs; |
69 | const char *path; | |
4349cd7c | 70 | |
13dcfe46 ZJS |
71 | r = mnt_table_next_fs(table, iter, &fs); |
72 | if (r == 1) | |
73 | break; | |
74 | if (r < 0) | |
75 | return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
4349cd7c | 76 | |
13dcfe46 ZJS |
77 | path = mnt_fs_get_target(fs); |
78 | if (!path) | |
79 | continue; | |
4349cd7c | 80 | |
ef742415 | 81 | if (prefix && !path_startswith(path, prefix)) { |
62c505e1 | 82 | log_trace("Not unmounting %s, outside of prefix: %s", path, prefix); |
4349cd7c | 83 | continue; |
ef742415 | 84 | } |
4349cd7c | 85 | |
84bcb394 LP |
86 | STRV_FOREACH(k, keep) |
87 | /* Match against anything in the path to the dirs to keep, or below the dirs to keep */ | |
88 | if (path_startswith(path, *k) || path_startswith(*k, path)) { | |
89 | shall_keep = true; | |
90 | break; | |
91 | } | |
92 | if (shall_keep) { | |
93 | log_debug("Not unmounting %s, referenced by keep list.", path); | |
94 | continue; | |
95 | } | |
96 | ||
827ea521 LP |
97 | if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) { |
98 | log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path); | |
4349cd7c LP |
99 | continue; |
100 | } | |
101 | ||
30868c1c | 102 | log_trace("Successfully unmounted %s", path); |
6b7c9f8b | 103 | |
4349cd7c LP |
104 | again = true; |
105 | n++; | |
106 | ||
107 | break; | |
108 | } | |
890c14e3 LP |
109 | |
110 | if (!again) | |
111 | break; | |
112 | ||
113 | rewind(f); | |
114 | } | |
4349cd7c | 115 | |
13dcfe46 | 116 | return n; |
4349cd7c LP |
117 | } |
118 | ||
4f5644db LP |
119 | #define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW) |
120 | ||
121 | static uint64_t ms_flags_to_mount_attr(unsigned long a) { | |
122 | uint64_t f = 0; | |
123 | ||
124 | if (FLAGS_SET(a, MS_RDONLY)) | |
125 | f |= MOUNT_ATTR_RDONLY; | |
126 | ||
127 | if (FLAGS_SET(a, MS_NOSUID)) | |
128 | f |= MOUNT_ATTR_NOSUID; | |
129 | ||
130 | if (FLAGS_SET(a, MS_NODEV)) | |
131 | f |= MOUNT_ATTR_NODEV; | |
132 | ||
133 | if (FLAGS_SET(a, MS_NOEXEC)) | |
134 | f |= MOUNT_ATTR_NOEXEC; | |
135 | ||
136 | if (FLAGS_SET(a, MS_NOSYMFOLLOW)) | |
137 | f |= MOUNT_ATTR_NOSYMFOLLOW; | |
138 | ||
139 | return f; | |
140 | } | |
141 | ||
142 | static bool skip_mount_set_attr = false; | |
143 | ||
be3f3752 | 144 | /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it |
64e82c19 LP |
145 | * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */ |
146 | int bind_remount_recursive_with_mountinfo( | |
147 | const char *prefix, | |
148 | unsigned long new_flags, | |
149 | unsigned long flags_mask, | |
6b000af4 | 150 | char **deny_list, |
64e82c19 LP |
151 | FILE *proc_self_mountinfo) { |
152 | ||
0289948e | 153 | _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL; |
ba8dced2 | 154 | _cleanup_set_free_ Set *done = NULL; |
670e8efd | 155 | unsigned n_tries = 0; |
4349cd7c LP |
156 | int r; |
157 | ||
8403219f | 158 | assert(prefix); |
ac9de0b3 | 159 | |
874052c5 LP |
160 | if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) { |
161 | /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */ | |
162 | ||
163 | if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE, | |
164 | &(struct mount_attr) { | |
165 | .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask), | |
166 | .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask), | |
167 | }, MOUNT_ATTR_SIZE_VER0) < 0) { | |
168 | ||
169 | log_debug_errno(errno, "mount_setattr() failed, falling back to classic remounting: %m"); | |
170 | ||
171 | /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We | |
172 | * also do this for all other kinds of errors since they are so many different, and | |
173 | * mount_setattr() has no graceful mode where it continues despite seeing errors one | |
174 | * some mounts, but we want that. Moreover mount_setattr() only works on the mount | |
175 | * point inode itself, not a non-mount point inode, and we want to support arbitrary | |
176 | * prefixes here. */ | |
177 | ||
178 | if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */ | |
179 | skip_mount_set_attr = true; | |
180 | } else | |
181 | return 0; /* Nice, this worked! */ | |
182 | } | |
183 | ||
0289948e LP |
184 | if (!proc_self_mountinfo) { |
185 | r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened); | |
186 | if (r < 0) | |
187 | return r; | |
188 | ||
189 | proc_self_mountinfo = proc_self_mountinfo_opened; | |
190 | } | |
191 | ||
ddc155b2 TM |
192 | /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY, |
193 | * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it | |
194 | * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it | |
195 | * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can | |
196 | * access, too. When mounts are stacked on the same mount point we only care for each individual | |
197 | * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do | |
198 | * not have any effect on future submounts that might get propagated, they might be writable | |
4b6ef527 LP |
199 | * etc. This includes future submounts that have been triggered via autofs. Also note that we can't |
200 | * operate atomically here. Mounts established while we process the tree might or might not get | |
201 | * noticed and thus might or might not be covered. | |
6b7c9f8b | 202 | * |
6b000af4 LP |
203 | * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the |
204 | * remount operation. Note that we'll ignore the deny list for the top-level path. */ | |
4349cd7c | 205 | |
4349cd7c | 206 | for (;;) { |
13dcfe46 ZJS |
207 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
208 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
ba8dced2 | 209 | _cleanup_hashmap_free_ Hashmap *todo = NULL; |
4349cd7c | 210 | bool top_autofs = false; |
4349cd7c | 211 | |
670e8efd LP |
212 | if (n_tries++ >= 32) /* Let's not retry this loop forever */ |
213 | return -EBUSY; | |
214 | ||
ac9de0b3 | 215 | rewind(proc_self_mountinfo); |
4349cd7c | 216 | |
e2857b3d | 217 | r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter); |
13dcfe46 ZJS |
218 | if (r < 0) |
219 | return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); | |
4349cd7c | 220 | |
13dcfe46 | 221 | for (;;) { |
ba8dced2 LP |
222 | _cleanup_free_ char *d = NULL; |
223 | const char *path, *type, *opts; | |
224 | unsigned long flags = 0; | |
13dcfe46 | 225 | struct libmnt_fs *fs; |
4349cd7c | 226 | |
13dcfe46 | 227 | r = mnt_table_next_fs(table, iter, &fs); |
d6bfab11 | 228 | if (r == 1) /* EOF */ |
13dcfe46 | 229 | break; |
4349cd7c | 230 | if (r < 0) |
13dcfe46 | 231 | return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); |
4349cd7c | 232 | |
13dcfe46 | 233 | path = mnt_fs_get_target(fs); |
d6bfab11 | 234 | if (!path) |
6b7c9f8b LP |
235 | continue; |
236 | ||
c6111b85 | 237 | if (!path_startswith(path, prefix)) |
13dcfe46 ZJS |
238 | continue; |
239 | ||
d6bfab11 LP |
240 | type = mnt_fs_get_fstype(fs); |
241 | if (!type) | |
242 | continue; | |
243 | ||
244 | /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid | |
245 | * triggering them, as we don't make any guarantees for future submounts anyway. If | |
246 | * they are already triggered, then we will find another entry for this. */ | |
247 | if (streq(type, "autofs")) { | |
248 | top_autofs = top_autofs || path_equal(path, prefix); | |
249 | continue; | |
250 | } | |
251 | ||
252 | if (set_contains(done, path)) | |
253 | continue; | |
254 | ||
6b000af4 | 255 | /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount |
13dcfe46 | 256 | * we shall operate on. */ |
c6111b85 | 257 | if (!path_equal(path, prefix)) { |
6b000af4 | 258 | bool deny_listed = false; |
6b7c9f8b | 259 | |
6b000af4 | 260 | STRV_FOREACH(i, deny_list) { |
c6111b85 | 261 | if (path_equal(*i, prefix)) |
6b7c9f8b LP |
262 | continue; |
263 | ||
c6111b85 | 264 | if (!path_startswith(*i, prefix)) |
6b7c9f8b LP |
265 | continue; |
266 | ||
13dcfe46 | 267 | if (path_startswith(path, *i)) { |
6b000af4 | 268 | deny_listed = true; |
61f695f4 | 269 | log_trace("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix); |
6b7c9f8b LP |
270 | break; |
271 | } | |
272 | } | |
d6bfab11 | 273 | |
6b000af4 | 274 | if (deny_listed) |
6b7c9f8b LP |
275 | continue; |
276 | } | |
277 | ||
ba8dced2 LP |
278 | opts = mnt_fs_get_vfs_options(fs); |
279 | if (opts) { | |
280 | r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP)); | |
281 | if (r < 0) | |
282 | log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path); | |
283 | } | |
284 | ||
285 | d = strdup(path); | |
286 | if (!d) | |
287 | return -ENOMEM; | |
288 | ||
289 | r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags)); | |
290 | if (r == -EEXIST) | |
e0103063 LB |
291 | /* If the same path was recorded, but with different mount flags, update it: |
292 | * it means a mount point is overmounted, and libmount returns the "bottom" (or | |
293 | * older one) first, but we want to reapply the flags from the "top" (or newer | |
294 | * one). See: https://github.com/systemd/systemd/issues/20032 | |
295 | * Note that this shouldn't really fail, as we were just told that the key | |
296 | * exists, and it's an update so we want 'd' to be freed immediately. */ | |
297 | r = hashmap_update(todo, d, ULONG_TO_PTR(flags)); | |
d6bfab11 LP |
298 | if (r < 0) |
299 | return r; | |
ba8dced2 LP |
300 | if (r > 0) |
301 | TAKE_PTR(d); | |
4349cd7c LP |
302 | } |
303 | ||
5c5753b9 LP |
304 | /* Check if the top-level directory was among what we have seen so far. For that check both |
305 | * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll | |
306 | * not include it in either set but will set this bool. */ | |
c6111b85 | 307 | if (!set_contains(done, prefix) && |
ba8dced2 | 308 | !(top_autofs || hashmap_contains(todo, prefix))) { |
5c5753b9 | 309 | |
6b7c9f8b | 310 | /* The prefix directory itself is not yet a mount, make it one. */ |
c6111b85 | 311 | r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL); |
511a8cfe LP |
312 | if (r < 0) |
313 | return r; | |
4349cd7c | 314 | |
5c5753b9 LP |
315 | /* Immediately rescan, so that we pick up the new mount's flags */ |
316 | continue; | |
4349cd7c LP |
317 | } |
318 | ||
5c5753b9 | 319 | /* If we have no submounts to process anymore, we are done */ |
ba8dced2 | 320 | if (hashmap_isempty(todo)) |
5c5753b9 LP |
321 | return 0; |
322 | ||
ba8dced2 LP |
323 | for (;;) { |
324 | unsigned long flags; | |
325 | char *x = NULL; | |
326 | ||
327 | /* Take the first mount from our list of mounts to still process */ | |
328 | flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x)); | |
329 | if (!x) | |
330 | break; | |
4349cd7c | 331 | |
ba8dced2 | 332 | r = set_ensure_consume(&done, &path_hash_ops_free, x); |
4c701096 | 333 | if (IN_SET(r, 0, -EEXIST)) |
ba8dced2 | 334 | continue; /* Already done */ |
4349cd7c LP |
335 | if (r < 0) |
336 | return r; | |
337 | ||
ba8dced2 LP |
338 | /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's |
339 | * the default anyway, thus redundant, and in userns we'll get an error if we try to | |
340 | * explicitly enable it) */ | |
341 | r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL); | |
065b4774 | 342 | if (r < 0) { |
ba8dced2 LP |
343 | int q; |
344 | ||
345 | /* OK, so the remount of this entry failed. We'll ultimately ignore this in | |
346 | * almost all cases (there are simply so many reasons why this can fail, | |
347 | * think autofs, NFS, FUSE, …), but let's generate useful debug messages at | |
348 | * the very least. */ | |
349 | ||
350 | q = path_is_mount_point(x, NULL, 0); | |
351 | if (IN_SET(q, 0, -ENOENT)) { | |
352 | /* Hmm, whaaaa? The mount point is not actually a mount point? Then | |
353 | * it is either obstructed by a later mount or somebody has been | |
354 | * racing against us and removed it. Either way the mount point | |
355 | * doesn't matter to us, let's ignore it hence. */ | |
356 | log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x); | |
357 | continue; | |
358 | } | |
359 | if (q < 0) /* Any other error on this? Just log and continue */ | |
360 | log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x); | |
361 | ||
362 | if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */ | |
363 | log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x); | |
364 | continue; | |
365 | } | |
366 | ||
367 | /* Make this fatal if this is the top-level mount */ | |
368 | if (path_equal(x, prefix)) | |
065b4774 LP |
369 | return r; |
370 | ||
ba8dced2 LP |
371 | /* If this is not the top-level mount, then handle this gracefully: log but |
372 | * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons | |
373 | * this might fail without a chance for us to do anything about it, let's | |
374 | * hence be strict on the top-level mount and lenient on the inner ones. */ | |
375 | log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x); | |
ef454fd1 YW |
376 | continue; |
377 | } | |
98df8089 | 378 | |
30868c1c | 379 | log_trace("Remounted %s.", x); |
4349cd7c LP |
380 | } |
381 | } | |
382 | } | |
383 | ||
7cce68e1 LP |
384 | int bind_remount_one_with_mountinfo( |
385 | const char *path, | |
386 | unsigned long new_flags, | |
387 | unsigned long flags_mask, | |
388 | FILE *proc_self_mountinfo) { | |
389 | ||
390 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; | |
2c5ff8ea LP |
391 | unsigned long flags = 0; |
392 | struct libmnt_fs *fs; | |
393 | const char *opts; | |
7cce68e1 LP |
394 | int r; |
395 | ||
396 | assert(path); | |
397 | assert(proc_self_mountinfo); | |
398 | ||
4f5644db LP |
399 | if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) { |
400 | /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */ | |
401 | ||
402 | if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, | |
403 | &(struct mount_attr) { | |
404 | .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask), | |
405 | .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask), | |
406 | }, MOUNT_ATTR_SIZE_VER0) < 0) { | |
407 | ||
408 | log_debug_errno(errno, "mount_setattr() didn't work, falling back to classic remounting: %m"); | |
409 | ||
410 | if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */ | |
411 | skip_mount_set_attr = true; | |
412 | } else | |
413 | return 0; /* Nice, this worked! */ | |
414 | } | |
415 | ||
7cce68e1 LP |
416 | rewind(proc_self_mountinfo); |
417 | ||
418 | table = mnt_new_table(); | |
419 | if (!table) | |
420 | return -ENOMEM; | |
421 | ||
422 | r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo"); | |
423 | if (r < 0) | |
424 | return r; | |
425 | ||
2c5ff8ea | 426 | fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD); |
0338df47 LP |
427 | if (!fs) { |
428 | if (laccess(path, F_OK) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */ | |
429 | return -errno; | |
430 | ||
2c5ff8ea | 431 | return -EINVAL; /* Not a mount point we recognize */ |
0338df47 | 432 | } |
2c5ff8ea LP |
433 | |
434 | opts = mnt_fs_get_vfs_options(fs); | |
435 | if (opts) { | |
436 | r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP)); | |
437 | if (r < 0) | |
438 | log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path); | |
439 | } | |
7cce68e1 | 440 | |
2c5ff8ea | 441 | r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL); |
b23c6a64 LP |
442 | if (r < 0) { |
443 | if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again, | |
444 | * since kernel adds it in | |
445 | * everywhere, because it's the | |
446 | * default. */ | |
447 | return r; | |
448 | ||
449 | /* Let's handle redundant remounts gracefully */ | |
450 | log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path); | |
451 | } | |
7cce68e1 LP |
452 | |
453 | return 0; | |
454 | } | |
455 | ||
ea0f3289 LP |
456 | static int mount_switch_root_pivot(int fd_newroot, const char *path) { |
457 | assert(fd_newroot >= 0); | |
458 | assert(path); | |
2e776ed6 | 459 | |
ea0f3289 LP |
460 | /* Change into the new rootfs. */ |
461 | if (fchdir(fd_newroot) < 0) | |
12cc9601 | 462 | return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path); |
2e776ed6 | 463 | |
2e776ed6 CB |
464 | /* Let the kernel tuck the new root under the old one. */ |
465 | if (pivot_root(".", ".") < 0) | |
466 | return log_debug_errno(errno, "Failed to pivot root to new rootfs '%s': %m", path); | |
467 | ||
ea0f3289 LP |
468 | /* Get rid of the old root and reveal our brand new root. (This will always operate on the top-most |
469 | * mount on our cwd, regardless what our current directory actually points to.) */ | |
2e776ed6 CB |
470 | if (umount2(".", MNT_DETACH) < 0) |
471 | return log_debug_errno(errno, "Failed to unmount old rootfs: %m"); | |
472 | ||
2e776ed6 CB |
473 | return 0; |
474 | } | |
475 | ||
ea0f3289 LP |
476 | static int mount_switch_root_move(int fd_newroot, const char *path) { |
477 | assert(fd_newroot >= 0); | |
478 | assert(path); | |
479 | ||
480 | /* Change into the new rootfs. */ | |
481 | if (fchdir(fd_newroot) < 0) | |
12cc9601 | 482 | return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path); |
ea0f3289 LP |
483 | |
484 | /* Move the new root fs */ | |
485 | if (mount(".", "/", NULL, MS_MOVE, NULL) < 0) | |
57c10a56 CB |
486 | return log_debug_errno(errno, "Failed to move new rootfs '%s': %m", path); |
487 | ||
12cc9601 | 488 | /* Also change root dir */ |
57c10a56 CB |
489 | if (chroot(".") < 0) |
490 | return log_debug_errno(errno, "Failed to chroot to new rootfs '%s': %m", path); | |
491 | ||
57c10a56 CB |
492 | return 0; |
493 | } | |
494 | ||
ea0f3289 | 495 | int mount_switch_root_full(const char *path, unsigned long mount_propagation_flag, bool force_ms_move) { |
57c10a56 | 496 | _cleanup_close_ int fd_newroot = -EBADF; |
6c6eb219 | 497 | int r; |
57c10a56 CB |
498 | |
499 | assert(path); | |
9d50f850 | 500 | assert(mount_propagation_flag_is_valid(mount_propagation_flag)); |
57c10a56 CB |
501 | |
502 | fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); | |
503 | if (fd_newroot < 0) | |
504 | return log_debug_errno(errno, "Failed to open new rootfs '%s': %m", path); | |
505 | ||
ea0f3289 LP |
506 | if (!force_ms_move) { |
507 | r = mount_switch_root_pivot(fd_newroot, path); | |
508 | if (r < 0) { | |
509 | log_debug_errno(r, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path); | |
510 | force_ms_move = true; | |
511 | } | |
512 | } | |
513 | if (force_ms_move) { | |
514 | /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is | |
515 | * an initramfs in which case pivot_root() isn't supported. */ | |
516 | r = mount_switch_root_move(fd_newroot, path); | |
517 | if (r < 0) | |
518 | return log_debug_errno(r, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path); | |
57c10a56 | 519 | } |
57c10a56 | 520 | |
9d50f850 YW |
521 | /* Finally, let's establish the requested propagation flags. */ |
522 | if (mount_propagation_flag == 0) | |
523 | return 0; | |
524 | ||
525 | if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, 0) < 0) | |
57c10a56 | 526 | return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m", |
9d50f850 | 527 | mount_propagation_flag_to_string(mount_propagation_flag), path); |
57c10a56 CB |
528 | |
529 | return 0; | |
530 | } | |
2e776ed6 | 531 | |
3f2c0bec LP |
532 | int repeat_unmount(const char *path, int flags) { |
533 | bool done = false; | |
534 | ||
535 | assert(path); | |
536 | ||
537 | /* If there are multiple mounts on a mount point, this | |
538 | * removes them all */ | |
539 | ||
540 | for (;;) { | |
541 | if (umount2(path, flags) < 0) { | |
542 | ||
543 | if (errno == EINVAL) | |
544 | return done; | |
545 | ||
546 | return -errno; | |
547 | } | |
548 | ||
549 | done = true; | |
550 | } | |
551 | } | |
c4b41707 | 552 | |
48b747fa LP |
553 | int mode_to_inaccessible_node( |
554 | const char *runtime_dir, | |
555 | mode_t mode, | |
556 | char **ret) { | |
557 | ||
558 | /* This function maps a node type to a corresponding inaccessible file node. These nodes are created | |
559 | * during early boot by PID 1. In some cases we lacked the privs to create the character and block | |
560 | * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a | |
561 | * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that | |
562 | * case we use an AF_UNIX file node instead, which is not the same, but close enough for most | |
563 | * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory | |
564 | * file nodes, and that's the most important thing that matters. | |
565 | * | |
566 | * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if | |
567 | * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */ | |
568 | ||
e5f10caf | 569 | _cleanup_free_ char *d = NULL; |
11e2be3a | 570 | const char *node; |
e5f10caf | 571 | |
48b747fa LP |
572 | assert(ret); |
573 | ||
574 | if (!runtime_dir) | |
575 | runtime_dir = "/run"; | |
fe80fcc7 | 576 | |
11e2be3a LP |
577 | if (S_ISLNK(mode)) |
578 | return -EINVAL; | |
fe80fcc7 | 579 | |
11e2be3a | 580 | node = inode_type_to_string(mode); |
e5f10caf AZ |
581 | if (!node) |
582 | return -EINVAL; | |
583 | ||
11e2be3a | 584 | d = path_join(runtime_dir, "systemd/inaccessible", node); |
48b747fa LP |
585 | if (!d) |
586 | return -ENOMEM; | |
587 | ||
cbed1dc8 LP |
588 | /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also |
589 | * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no | |
590 | * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the | |
591 | * inaccessible block device node let's see if the block device node actually exists, and if not, | |
592 | * fall back to the character device node. From there fall back to the socket device node. This means | |
593 | * in the best case we'll get the right device node type — but if not we'll hopefully at least get a | |
594 | * device node at all. */ | |
595 | ||
596 | if (S_ISBLK(mode) && | |
597 | access(d, F_OK) < 0 && errno == ENOENT) { | |
598 | free(d); | |
599 | d = path_join(runtime_dir, "/systemd/inaccessible/chr"); | |
600 | if (!d) | |
601 | return -ENOMEM; | |
602 | } | |
603 | ||
604 | if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) && | |
605 | access(d, F_OK) < 0 && errno == ENOENT) { | |
48b747fa LP |
606 | free(d); |
607 | d = path_join(runtime_dir, "/systemd/inaccessible/sock"); | |
608 | if (!d) | |
609 | return -ENOMEM; | |
610 | } | |
e5f10caf | 611 | |
48b747fa | 612 | *ret = TAKE_PTR(d); |
e5f10caf | 613 | return 0; |
c4b41707 | 614 | } |
60e76d48 | 615 | |
da185cd0 | 616 | int mount_flags_to_string(unsigned long flags, char **ret) { |
1c092b62 | 617 | static const struct { |
da185cd0 | 618 | unsigned long flag; |
1c092b62 YW |
619 | const char *name; |
620 | } map[] = { | |
621 | { .flag = MS_RDONLY, .name = "MS_RDONLY", }, | |
622 | { .flag = MS_NOSUID, .name = "MS_NOSUID", }, | |
623 | { .flag = MS_NODEV, .name = "MS_NODEV", }, | |
624 | { .flag = MS_NOEXEC, .name = "MS_NOEXEC", }, | |
625 | { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", }, | |
626 | { .flag = MS_REMOUNT, .name = "MS_REMOUNT", }, | |
627 | { .flag = MS_MANDLOCK, .name = "MS_MANDLOCK", }, | |
628 | { .flag = MS_DIRSYNC, .name = "MS_DIRSYNC", }, | |
629 | { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", }, | |
630 | { .flag = MS_NOATIME, .name = "MS_NOATIME", }, | |
631 | { .flag = MS_NODIRATIME, .name = "MS_NODIRATIME", }, | |
632 | { .flag = MS_BIND, .name = "MS_BIND", }, | |
633 | { .flag = MS_MOVE, .name = "MS_MOVE", }, | |
634 | { .flag = MS_REC, .name = "MS_REC", }, | |
635 | { .flag = MS_SILENT, .name = "MS_SILENT", }, | |
636 | { .flag = MS_POSIXACL, .name = "MS_POSIXACL", }, | |
637 | { .flag = MS_UNBINDABLE, .name = "MS_UNBINDABLE", }, | |
638 | { .flag = MS_PRIVATE, .name = "MS_PRIVATE", }, | |
639 | { .flag = MS_SLAVE, .name = "MS_SLAVE", }, | |
640 | { .flag = MS_SHARED, .name = "MS_SHARED", }, | |
641 | { .flag = MS_RELATIME, .name = "MS_RELATIME", }, | |
642 | { .flag = MS_KERNMOUNT, .name = "MS_KERNMOUNT", }, | |
643 | { .flag = MS_I_VERSION, .name = "MS_I_VERSION", }, | |
644 | { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", }, | |
645 | { .flag = MS_LAZYTIME, .name = "MS_LAZYTIME", }, | |
646 | }; | |
647 | _cleanup_free_ char *str = NULL; | |
648 | ||
4bee2333 YW |
649 | assert(ret); |
650 | ||
1c092b62 YW |
651 | for (size_t i = 0; i < ELEMENTSOF(map); i++) |
652 | if (flags & map[i].flag) { | |
653 | if (!strextend_with_separator(&str, "|", map[i].name)) | |
654 | return -ENOMEM; | |
655 | flags &= ~map[i].flag; | |
656 | } | |
657 | ||
658 | if (!str || flags != 0) | |
659 | if (strextendf_with_separator(&str, "|", "%lx", flags) < 0) | |
660 | return -ENOMEM; | |
661 | ||
662 | *ret = TAKE_PTR(str); | |
663 | return 0; | |
60e76d48 ZJS |
664 | } |
665 | ||
511a8cfe | 666 | int mount_verbose_full( |
60e76d48 ZJS |
667 | int error_log_level, |
668 | const char *what, | |
669 | const char *where, | |
670 | const char *type, | |
671 | unsigned long flags, | |
511a8cfe LP |
672 | const char *options, |
673 | bool follow_symlink) { | |
60e76d48 | 674 | |
6ef8df2b YW |
675 | _cleanup_free_ char *fl = NULL, *o = NULL; |
676 | unsigned long f; | |
677 | int r; | |
678 | ||
679 | r = mount_option_mangle(options, flags, &f, &o); | |
680 | if (r < 0) | |
681 | return log_full_errno(error_log_level, r, | |
682 | "Failed to mangle mount options %s: %m", | |
683 | strempty(options)); | |
60e76d48 | 684 | |
1c092b62 | 685 | (void) mount_flags_to_string(f, &fl); |
60e76d48 | 686 | |
f56f9c6b LP |
687 | if (FLAGS_SET(f, MS_REMOUNT|MS_BIND)) |
688 | log_debug("Changing mount flags %s (%s \"%s\")...", | |
6ef8df2b | 689 | where, strnull(fl), strempty(o)); |
f56f9c6b LP |
690 | else if (f & MS_REMOUNT) |
691 | log_debug("Remounting superblock %s (%s \"%s\")...", | |
6ef8df2b | 692 | where, strnull(fl), strempty(o)); |
f56f9c6b LP |
693 | else if (f & (MS_SHARED|MS_PRIVATE|MS_SLAVE|MS_UNBINDABLE)) |
694 | log_debug("Changing mount propagation %s (%s \"%s\")", | |
695 | where, strnull(fl), strempty(o)); | |
696 | else if (f & MS_BIND) | |
60e76d48 | 697 | log_debug("Bind-mounting %s on %s (%s \"%s\")...", |
6ef8df2b YW |
698 | what, where, strnull(fl), strempty(o)); |
699 | else if (f & MS_MOVE) | |
e2341b6b DT |
700 | log_debug("Moving mount %s %s %s (%s \"%s\")...", |
701 | what, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), where, strnull(fl), strempty(o)); | |
60e76d48 | 702 | else |
3b493d94 LP |
703 | log_debug("Mounting %s (%s) on %s (%s \"%s\")...", |
704 | strna(what), strna(type), where, strnull(fl), strempty(o)); | |
511a8cfe LP |
705 | |
706 | if (follow_symlink) | |
7c248223 | 707 | r = RET_NERRNO(mount(what, where, type, f, o)); |
511a8cfe LP |
708 | else |
709 | r = mount_nofollow(what, where, type, f, o); | |
710 | if (r < 0) | |
711 | return log_full_errno(error_log_level, r, | |
3ccf6126 LP |
712 | "Failed to mount %s (type %s) on %s (%s \"%s\"): %m", |
713 | strna(what), strna(type), where, strnull(fl), strempty(o)); | |
60e76d48 ZJS |
714 | return 0; |
715 | } | |
716 | ||
30f5d104 LP |
717 | int umount_verbose( |
718 | int error_log_level, | |
719 | const char *what, | |
720 | int flags) { | |
721 | ||
722 | assert(what); | |
723 | ||
60e76d48 | 724 | log_debug("Umounting %s...", what); |
30f5d104 LP |
725 | |
726 | if (umount2(what, flags) < 0) | |
727 | return log_full_errno(error_log_level, errno, | |
728 | "Failed to unmount %s: %m", what); | |
729 | ||
60e76d48 ZJS |
730 | return 0; |
731 | } | |
83555251 | 732 | |
9e7f941a YW |
733 | int mount_option_mangle( |
734 | const char *options, | |
735 | unsigned long mount_flags, | |
736 | unsigned long *ret_mount_flags, | |
737 | char **ret_remaining_options) { | |
738 | ||
739 | const struct libmnt_optmap *map; | |
740 | _cleanup_free_ char *ret = NULL; | |
9e7f941a YW |
741 | int r; |
742 | ||
3ca4ec20 | 743 | /* This extracts mount flags from the mount options, and stores |
9e7f941a YW |
744 | * non-mount-flag options to '*ret_remaining_options'. |
745 | * E.g., | |
9f563f27 | 746 | * "rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000" |
9e7f941a | 747 | * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and |
9f563f27 | 748 | * "size=1630748k,mode=0700,uid=1000,gid=1000". |
3ca4ec20 | 749 | * See more examples in test-mount-util.c. |
9e7f941a | 750 | * |
3ca4ec20 | 751 | * If 'options' does not contain any non-mount-flag options, |
5238e957 | 752 | * then '*ret_remaining_options' is set to NULL instead of empty string. |
3ca4ec20 ZJS |
753 | * The validity of options stored in '*ret_remaining_options' is not checked. |
754 | * If 'options' is NULL, this just copies 'mount_flags' to *ret_mount_flags. */ | |
9e7f941a YW |
755 | |
756 | assert(ret_mount_flags); | |
757 | assert(ret_remaining_options); | |
758 | ||
759 | map = mnt_get_builtin_optmap(MNT_LINUX_MAP); | |
760 | if (!map) | |
761 | return -EINVAL; | |
762 | ||
25086b4c | 763 | for (const char *p = options;;) { |
9e7f941a YW |
764 | _cleanup_free_ char *word = NULL; |
765 | const struct libmnt_optmap *ent; | |
766 | ||
9b23679e | 767 | r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE); |
9e7f941a YW |
768 | if (r < 0) |
769 | return r; | |
770 | if (r == 0) | |
771 | break; | |
772 | ||
773 | for (ent = map; ent->name; ent++) { | |
774 | /* All entries in MNT_LINUX_MAP do not take any argument. | |
775 | * Thus, ent->name does not contain "=" or "[=]". */ | |
776 | if (!streq(word, ent->name)) | |
777 | continue; | |
778 | ||
779 | if (!(ent->mask & MNT_INVERT)) | |
780 | mount_flags |= ent->id; | |
781 | else if (mount_flags & ent->id) | |
782 | mount_flags ^= ent->id; | |
783 | ||
784 | break; | |
785 | } | |
786 | ||
787 | /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */ | |
ac6086fd LB |
788 | if (!ent->name && |
789 | !startswith_no_case(word, "x-") && | |
790 | !strextend_with_separator(&ret, ",", word)) | |
9e7f941a YW |
791 | return -ENOMEM; |
792 | } | |
793 | ||
794 | *ret_mount_flags = mount_flags; | |
ae2a15bc | 795 | *ret_remaining_options = TAKE_PTR(ret); |
9e7f941a YW |
796 | |
797 | return 0; | |
798 | } | |
6af52c3a | 799 | |
a7e0199e LB |
800 | static int mount_in_namespace_legacy( |
801 | const char *chased_src_path, | |
802 | int chased_src_fd, | |
803 | struct stat *chased_src_st, | |
6af52c3a LB |
804 | const char *propagate_path, |
805 | const char *incoming_path, | |
6af52c3a | 806 | const char *dest, |
a7e0199e LB |
807 | int pidns_fd, |
808 | int mntns_fd, | |
809 | int root_fd, | |
6af52c3a | 810 | bool read_only, |
70599967 LB |
811 | bool make_file_or_directory, |
812 | const MountOptions *options, | |
84be0c71 | 813 | const ImagePolicy *image_policy, |
70599967 | 814 | bool is_image) { |
6af52c3a | 815 | |
19ee48a6 | 816 | _cleanup_close_pair_ int errno_pipe_fd[2] = PIPE_EBADF; |
ddb6eeaf | 817 | char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p; |
6af52c3a LB |
818 | bool mount_slave_created = false, mount_slave_mounted = false, |
819 | mount_tmp_created = false, mount_tmp_mounted = false, | |
820 | mount_outside_created = false, mount_outside_mounted = false; | |
6af52c3a LB |
821 | pid_t child; |
822 | int r; | |
823 | ||
a7e0199e LB |
824 | assert(chased_src_path); |
825 | assert(chased_src_fd >= 0); | |
826 | assert(chased_src_st); | |
6af52c3a LB |
827 | assert(propagate_path); |
828 | assert(incoming_path); | |
6af52c3a | 829 | assert(dest); |
a7e0199e LB |
830 | assert(pidns_fd >= 0); |
831 | assert(mntns_fd >= 0); | |
832 | assert(root_fd >= 0); | |
70599967 | 833 | assert(!options || is_image); |
6af52c3a | 834 | |
6af52c3a LB |
835 | p = strjoina(propagate_path, "/"); |
836 | r = laccess(p, F_OK); | |
837 | if (r < 0) | |
838 | return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points"); | |
839 | ||
6af52c3a LB |
840 | /* Our goal is to install a new bind mount into the container, |
841 | possibly read-only. This is irritatingly complex | |
842 | unfortunately, currently. | |
843 | ||
844 | First, we start by creating a private playground in /tmp, | |
845 | that we can mount MS_SLAVE. (Which is necessary, since | |
846 | MS_MOVE cannot be applied to mounts with MS_SHARED parent | |
847 | mounts.) */ | |
848 | ||
849 | if (!mkdtemp(mount_slave)) | |
850 | return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave); | |
851 | ||
852 | mount_slave_created = true; | |
853 | ||
854 | r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL); | |
855 | if (r < 0) | |
856 | goto finish; | |
857 | ||
858 | mount_slave_mounted = true; | |
859 | ||
860 | r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL); | |
861 | if (r < 0) | |
862 | goto finish; | |
863 | ||
864 | /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */ | |
865 | mount_tmp = strjoina(mount_slave, "/mount"); | |
70599967 LB |
866 | if (is_image) |
867 | r = mkdir_p(mount_tmp, 0700); | |
868 | else | |
a7e0199e | 869 | r = make_mount_point_inode_from_stat(chased_src_st, mount_tmp, 0700); |
6af52c3a LB |
870 | if (r < 0) { |
871 | log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp); | |
872 | goto finish; | |
873 | } | |
874 | ||
875 | mount_tmp_created = true; | |
876 | ||
70599967 | 877 | if (is_image) |
3e107272 LB |
878 | r = verity_dissect_and_mount( |
879 | chased_src_fd, | |
880 | chased_src_path, | |
881 | mount_tmp, | |
882 | options, | |
883 | image_policy, | |
884 | /* required_host_os_release_id= */ NULL, | |
885 | /* required_host_os_release_version_id= */ NULL, | |
886 | /* required_host_os_release_sysext_level= */ NULL, | |
887 | /* required_sysext_scope= */ NULL, | |
888 | /* ret_image= */ NULL); | |
70599967 | 889 | else |
ddb6eeaf | 890 | r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL); |
6af52c3a LB |
891 | if (r < 0) |
892 | goto finish; | |
893 | ||
894 | mount_tmp_mounted = true; | |
895 | ||
896 | /* Third, we remount the new bind mount read-only if requested. */ | |
897 | if (read_only) { | |
898 | r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL); | |
899 | if (r < 0) | |
900 | goto finish; | |
901 | } | |
902 | ||
903 | /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only | |
904 | * right-away. */ | |
905 | ||
906 | mount_outside = strjoina(propagate_path, "/XXXXXX"); | |
a7e0199e | 907 | if (is_image || S_ISDIR(chased_src_st->st_mode)) |
6af52c3a LB |
908 | r = mkdtemp(mount_outside) ? 0 : -errno; |
909 | else { | |
910 | r = mkostemp_safe(mount_outside); | |
911 | safe_close(r); | |
912 | } | |
913 | if (r < 0) { | |
914 | log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside); | |
915 | goto finish; | |
916 | } | |
917 | ||
918 | mount_outside_created = true; | |
919 | ||
920 | r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL); | |
921 | if (r < 0) | |
922 | goto finish; | |
923 | ||
924 | mount_outside_mounted = true; | |
925 | mount_tmp_mounted = false; | |
926 | ||
a7e0199e | 927 | if (is_image || S_ISDIR(chased_src_st->st_mode)) |
6af52c3a LB |
928 | (void) rmdir(mount_tmp); |
929 | else | |
930 | (void) unlink(mount_tmp); | |
931 | mount_tmp_created = false; | |
932 | ||
933 | (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW); | |
934 | mount_slave_mounted = false; | |
935 | ||
936 | (void) rmdir(mount_slave); | |
937 | mount_slave_created = false; | |
938 | ||
939 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) { | |
940 | log_debug_errno(errno, "Failed to create pipe: %m"); | |
941 | goto finish; | |
942 | } | |
943 | ||
2338a175 | 944 | r = namespace_fork("(sd-bindmnt)", "(sd-bindmnt-inner)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
98f654fd | 945 | pidns_fd, mntns_fd, -1, -1, root_fd, &child); |
6af52c3a LB |
946 | if (r < 0) |
947 | goto finish; | |
948 | if (r == 0) { | |
03469b77 | 949 | _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL; |
6af52c3a LB |
950 | |
951 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); | |
952 | ||
6af52c3a | 953 | if (make_file_or_directory) { |
70599967 LB |
954 | if (!is_image) { |
955 | (void) mkdir_parents(dest, 0755); | |
a7e0199e | 956 | (void) make_mount_point_inode_from_stat(chased_src_st, dest, 0700); |
70599967 LB |
957 | } else |
958 | (void) mkdir_p(dest, 0755); | |
6af52c3a LB |
959 | } |
960 | ||
961 | /* Fifth, move the mount to the right place inside */ | |
03469b77 LP |
962 | r = path_extract_filename(mount_outside, &mount_outside_fn); |
963 | if (r < 0) { | |
964 | log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside); | |
965 | goto child_fail; | |
966 | } | |
967 | ||
968 | mount_inside = path_join(incoming_path, mount_outside_fn); | |
969 | if (!mount_inside) { | |
970 | r = log_oom_debug(); | |
971 | goto child_fail; | |
972 | } | |
973 | ||
974 | r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest, NULL, MS_MOVE, NULL); | |
6af52c3a LB |
975 | if (r < 0) |
976 | goto child_fail; | |
977 | ||
978 | _exit(EXIT_SUCCESS); | |
979 | ||
980 | child_fail: | |
981 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
982 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
983 | ||
984 | _exit(EXIT_FAILURE); | |
985 | } | |
986 | ||
987 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
988 | ||
989 | r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0); | |
990 | if (r < 0) { | |
991 | log_debug_errno(r, "Failed to wait for child: %m"); | |
992 | goto finish; | |
993 | } | |
994 | if (r != EXIT_SUCCESS) { | |
995 | if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r)) | |
996 | log_debug_errno(r, "Failed to mount: %m"); | |
997 | else | |
998 | log_debug("Child failed."); | |
999 | goto finish; | |
1000 | } | |
1001 | ||
1002 | finish: | |
1003 | if (mount_outside_mounted) | |
1004 | (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW); | |
1005 | if (mount_outside_created) { | |
a7e0199e | 1006 | if (is_image || S_ISDIR(chased_src_st->st_mode)) |
6af52c3a LB |
1007 | (void) rmdir(mount_outside); |
1008 | else | |
1009 | (void) unlink(mount_outside); | |
1010 | } | |
1011 | ||
1012 | if (mount_tmp_mounted) | |
1013 | (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW); | |
1014 | if (mount_tmp_created) { | |
a7e0199e | 1015 | if (is_image || S_ISDIR(chased_src_st->st_mode)) |
6af52c3a LB |
1016 | (void) rmdir(mount_tmp); |
1017 | else | |
1018 | (void) unlink(mount_tmp); | |
1019 | } | |
1020 | ||
1021 | if (mount_slave_mounted) | |
1022 | (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW); | |
1023 | if (mount_slave_created) | |
1024 | (void) rmdir(mount_slave); | |
1025 | ||
1026 | return r; | |
1027 | } | |
70599967 | 1028 | |
a7e0199e LB |
1029 | static int mount_in_namespace( |
1030 | pid_t target, | |
1031 | const char *propagate_path, | |
1032 | const char *incoming_path, | |
1033 | const char *src, | |
1034 | const char *dest, | |
1035 | bool read_only, | |
1036 | bool make_file_or_directory, | |
1037 | const MountOptions *options, | |
1038 | const ImagePolicy *image_policy, | |
1039 | bool is_image) { | |
1040 | ||
ddba5a1a | 1041 | _cleanup_(dissected_image_unrefp) DissectedImage *img = NULL; |
a7e0199e LB |
1042 | _cleanup_close_pair_ int errno_pipe_fd[2] = PIPE_EBADF; |
1043 | _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, chased_src_fd = -EBADF, | |
1044 | new_mount_fd = -EBADF; | |
1045 | _cleanup_free_ char *chased_src_path = NULL; | |
1046 | struct stat st; | |
1047 | pid_t child; | |
1048 | int r; | |
1049 | ||
1050 | assert(target > 0); | |
1051 | assert(propagate_path); | |
1052 | assert(incoming_path); | |
1053 | assert(src); | |
1054 | assert(dest); | |
1055 | assert(!options || is_image); | |
1056 | ||
1057 | r = namespace_open(target, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd); | |
1058 | if (r < 0) | |
1059 | return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m"); | |
1060 | ||
1061 | r = in_same_namespace(target, 0, NAMESPACE_MOUNT); | |
1062 | if (r < 0) | |
1063 | return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m"); | |
1064 | /* We can't add new mounts at runtime if the process wasn't started in a namespace */ | |
1065 | if (r > 0) | |
1066 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace"); | |
1067 | ||
1068 | r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd); | |
1069 | if (r < 0) | |
1070 | return log_debug_errno(r, "Failed to resolve source path of %s: %m", src); | |
1071 | log_debug("Chased source path of %s to %s", src, chased_src_path); | |
1072 | ||
1073 | if (fstat(chased_src_fd, &st) < 0) | |
1074 | return log_debug_errno(errno, "Failed to stat() resolved source path %s: %m", src); | |
1075 | if (S_ISLNK(st.st_mode)) /* This shouldn't really happen, given that we just chased the symlinks above, but let's better be safe… */ | |
1076 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Source directory %s can't be a symbolic link", src); | |
1077 | ||
ddba5a1a | 1078 | if (!mount_new_api_supported()) /* Fallback if we can't use the new mount API */ |
a7e0199e LB |
1079 | return mount_in_namespace_legacy( |
1080 | chased_src_path, | |
1081 | chased_src_fd, | |
1082 | &st, | |
1083 | propagate_path, | |
1084 | incoming_path, | |
1085 | dest, | |
1086 | pidns_fd, | |
1087 | mntns_fd, | |
1088 | root_fd, | |
1089 | read_only, | |
1090 | make_file_or_directory, | |
1091 | options, | |
1092 | image_policy, | |
1093 | is_image); | |
1094 | ||
ddba5a1a LB |
1095 | if (is_image) { |
1096 | r = verity_dissect_and_mount( | |
1097 | chased_src_fd, | |
1098 | chased_src_path, | |
1099 | /* dest= */ NULL, | |
1100 | options, | |
1101 | image_policy, | |
1102 | /* required_host_os_release_id= */ NULL, | |
1103 | /* required_host_os_release_version_id= */ NULL, | |
1104 | /* required_host_os_release_sysext_level= */ NULL, | |
1105 | /* required_sysext_scope= */ NULL, | |
1106 | &img); | |
1107 | if (r < 0) | |
1108 | return log_debug_errno( | |
1109 | r, | |
1110 | "Failed to dissect and mount image %s: %m", | |
1111 | chased_src_path); | |
1112 | } else { | |
1113 | new_mount_fd = open_tree( | |
1114 | chased_src_fd, | |
1115 | "", | |
1116 | OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH); | |
1117 | if (new_mount_fd < 0) | |
1118 | return log_debug_errno( | |
1119 | errno, | |
1120 | "Failed to open mount point \"%s\": %m", | |
1121 | chased_src_path); | |
1122 | ||
1123 | if (read_only && mount_setattr(new_mount_fd, "", AT_EMPTY_PATH, | |
1124 | &(struct mount_attr) { | |
1125 | .attr_set = MOUNT_ATTR_RDONLY, | |
1126 | }, MOUNT_ATTR_SIZE_VER0) < 0) | |
1127 | return log_debug_errno( | |
1128 | errno, | |
1129 | "Failed to set mount flags for \"%s\": %m", | |
1130 | chased_src_path); | |
1131 | } | |
a7e0199e LB |
1132 | |
1133 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) | |
1134 | return log_debug_errno(errno, "Failed to create pipe: %m"); | |
1135 | ||
1136 | r = namespace_fork("(sd-bindmnt)", | |
1137 | "(sd-bindmnt-inner)", | |
1138 | /* except_fds= */ NULL, | |
1139 | /* n_except_fds= */ 0, | |
1140 | FORK_RESET_SIGNALS|FORK_DEATHSIG, | |
1141 | pidns_fd, | |
1142 | mntns_fd, | |
1143 | /* netns_fd= */ -1, | |
1144 | /* userns_fd= */ -1, | |
1145 | root_fd, | |
1146 | &child); | |
1147 | if (r < 0) | |
1148 | return log_debug_errno(r, "Failed to fork off: %m"); | |
1149 | if (r == 0) { | |
1150 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); | |
1151 | ||
ddba5a1a | 1152 | if (make_file_or_directory) |
a7e0199e | 1153 | (void) mkdir_parents(dest, 0755); |
a7e0199e | 1154 | |
ddba5a1a LB |
1155 | if (img) { |
1156 | DissectImageFlags f = 0; | |
1157 | ||
1158 | if (make_file_or_directory) | |
1159 | f |= DISSECT_IMAGE_MKDIR; | |
1160 | ||
1161 | if (read_only) | |
1162 | f |= DISSECT_IMAGE_READ_ONLY; | |
1163 | ||
1164 | r = dissected_image_mount( | |
1165 | img, | |
1166 | dest, | |
1167 | /* uid_shift= */ UID_INVALID, | |
1168 | /* uid_range= */ UID_INVALID, | |
1169 | /* userns_fd= */ -EBADF, | |
1170 | f); | |
1171 | } else { | |
1172 | if (make_file_or_directory) | |
1173 | (void) make_mount_point_inode_from_stat(&st, dest, 0700); | |
1174 | ||
1175 | r = RET_NERRNO(move_mount(new_mount_fd, | |
1176 | "", | |
1177 | -EBADF, | |
1178 | dest, | |
1179 | MOVE_MOUNT_F_EMPTY_PATH)); | |
1180 | } | |
1181 | if (r < 0) { | |
1182 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
a7e0199e LB |
1183 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
1184 | ||
1185 | _exit(EXIT_FAILURE); | |
1186 | } | |
1187 | ||
1188 | _exit(EXIT_SUCCESS); | |
1189 | } | |
1190 | ||
1191 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
1192 | ||
1193 | r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0); | |
1194 | if (r < 0) | |
1195 | return log_debug_errno(r, "Failed to wait for child: %m"); | |
1196 | if (r != EXIT_SUCCESS) { | |
1197 | if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r)) | |
1198 | return log_debug_errno(r, "Failed to mount: %m"); | |
1199 | ||
1200 | return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed."); | |
1201 | } | |
1202 | ||
1203 | return 0; | |
1204 | } | |
1205 | ||
70599967 LB |
1206 | int bind_mount_in_namespace( |
1207 | pid_t target, | |
1208 | const char *propagate_path, | |
1209 | const char *incoming_path, | |
1210 | const char *src, | |
1211 | const char *dest, | |
1212 | bool read_only, | |
1213 | bool make_file_or_directory) { | |
1214 | ||
84be0c71 | 1215 | return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, /* options= */ NULL, /* image_policy= */ NULL, /* is_image= */ false); |
70599967 LB |
1216 | } |
1217 | ||
1218 | int mount_image_in_namespace( | |
1219 | pid_t target, | |
1220 | const char *propagate_path, | |
1221 | const char *incoming_path, | |
1222 | const char *src, | |
1223 | const char *dest, | |
1224 | bool read_only, | |
1225 | bool make_file_or_directory, | |
84be0c71 LP |
1226 | const MountOptions *options, |
1227 | const ImagePolicy *image_policy) { | |
70599967 | 1228 | |
84be0c71 | 1229 | return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, options, image_policy, /* is_image=*/ true); |
70599967 | 1230 | } |
14a25e1f LP |
1231 | |
1232 | int make_mount_point(const char *path) { | |
1233 | int r; | |
1234 | ||
1235 | assert(path); | |
1236 | ||
1237 | /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */ | |
1238 | ||
1239 | r = path_is_mount_point(path, NULL, 0); | |
1240 | if (r < 0) | |
1241 | return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path); | |
1242 | if (r > 0) | |
1243 | return 0; | |
1244 | ||
1245 | r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL); | |
1246 | if (r < 0) | |
1247 | return r; | |
1248 | ||
1249 | return 1; | |
1250 | } | |
35fd3558 | 1251 | |
f9ad896e LP |
1252 | int fd_make_mount_point(int fd) { |
1253 | int r; | |
1254 | ||
1255 | assert(fd >= 0); | |
1256 | ||
1257 | r = fd_is_mount_point(fd, NULL, 0); | |
1258 | if (r < 0) | |
1259 | return log_debug_errno(r, "Failed to determine whether file descriptor is a mount point: %m"); | |
1260 | if (r > 0) | |
1261 | return 0; | |
1262 | ||
1263 | r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(fd), FORMAT_PROC_FD_PATH(fd), NULL, MS_BIND|MS_REC, NULL); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | return 1; | |
1268 | } | |
1269 | ||
17b798d9 | 1270 | int make_userns(uid_t uid_shift, uid_t uid_range, uid_t owner, RemountIdmapping idmapping) { |
254d1313 | 1271 | _cleanup_close_ int userns_fd = -EBADF; |
50ae2966 | 1272 | _cleanup_free_ char *line = NULL; |
35fd3558 LP |
1273 | |
1274 | /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child | |
1275 | * process whose only purpose is to give us a new user namespace. It's killed when we got it. */ | |
1276 | ||
17b798d9 LP |
1277 | if (!userns_shift_range_valid(uid_shift, uid_range)) |
1278 | return -EINVAL; | |
1279 | ||
2b2777ed QD |
1280 | if (IN_SET(idmapping, REMOUNT_IDMAPPING_NONE, REMOUNT_IDMAPPING_HOST_ROOT)) { |
1281 | if (asprintf(&line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0u, uid_shift, uid_range) < 0) | |
50ae2966 | 1282 | return log_oom_debug(); |
35fd3558 | 1283 | |
2b2777ed QD |
1284 | /* If requested we'll include an entry in the mapping so that the host root user can make |
1285 | * changes to the uidmapped mount like it normally would. Specifically, we'll map the user | |
1286 | * with UID_MAPPED_ROOT on the backing fs to UID 0. This is useful, since nspawn code wants | |
1287 | * to create various missing inodes in the OS tree before booting into it, and this becomes | |
1288 | * very easy and straightforward to do if it can just do it under its own regular UID. Note | |
1289 | * that in that case the container's runtime uidmap (i.e. the one the container payload | |
1290 | * processes run in) will leave this UID unmapped, i.e. if we accidentally leave files owned | |
1291 | * by host root in the already uidmapped tree around they'll show up as owned by 'nobody', | |
1292 | * which is safe. (Of course, we shouldn't leave such inodes around, but always chown() them | |
1293 | * to the container's own UID range, but it's good to have a safety net, in case we | |
1294 | * forget it.) */ | |
1295 | if (idmapping == REMOUNT_IDMAPPING_HOST_ROOT) | |
1296 | if (strextendf(&line, | |
1297 | UID_FMT " " UID_FMT " " UID_FMT "\n", | |
1298 | UID_MAPPED_ROOT, 0u, 1u) < 0) | |
1299 | return log_oom_debug(); | |
1300 | } | |
1301 | ||
1302 | if (idmapping == REMOUNT_IDMAPPING_HOST_OWNER) { | |
1303 | /* Remap the owner of the bind mounted directory to the root user within the container. This | |
1304 | * way every file written by root within the container to the bind-mounted directory will | |
1305 | * be owned by the original user. All other user will remain unmapped. */ | |
1306 | if (asprintf(&line, UID_FMT " " UID_FMT " " UID_FMT "\n", owner, uid_shift, 1u) < 0) | |
1307 | return log_oom_debug(); | |
1308 | } | |
1309 | ||
35fd3558 | 1310 | /* We always assign the same UID and GID ranges */ |
979b0ff2 LP |
1311 | userns_fd = userns_acquire(line, line); |
1312 | if (userns_fd < 0) | |
1313 | return log_debug_errno(userns_fd, "Failed to acquire new userns: %m"); | |
35fd3558 LP |
1314 | |
1315 | return TAKE_FD(userns_fd); | |
1316 | } | |
1317 | ||
17b798d9 | 1318 | int remount_idmap_fd( |
35fd3558 | 1319 | const char *p, |
17b798d9 | 1320 | int userns_fd) { |
35fd3558 | 1321 | |
17b798d9 | 1322 | _cleanup_close_ int mount_fd = -EBADF; |
35fd3558 LP |
1323 | int r; |
1324 | ||
1325 | assert(p); | |
17b798d9 | 1326 | assert(userns_fd >= 0); |
35fd3558 LP |
1327 | |
1328 | /* Clone the mount point */ | |
1329 | mount_fd = open_tree(-1, p, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC); | |
1330 | if (mount_fd < 0) | |
1331 | return log_debug_errno(errno, "Failed to open tree of mounted filesystem '%s': %m", p); | |
1332 | ||
35fd3558 LP |
1333 | /* Set the user namespace mapping attribute on the cloned mount point */ |
1334 | if (mount_setattr(mount_fd, "", AT_EMPTY_PATH | AT_RECURSIVE, | |
1335 | &(struct mount_attr) { | |
1336 | .attr_set = MOUNT_ATTR_IDMAP, | |
1337 | .userns_fd = userns_fd, | |
1338 | }, sizeof(struct mount_attr)) < 0) | |
1339 | return log_debug_errno(errno, "Failed to change bind mount attributes for '%s': %m", p); | |
1340 | ||
1341 | /* Remove the old mount point */ | |
1342 | r = umount_verbose(LOG_DEBUG, p, UMOUNT_NOFOLLOW); | |
1343 | if (r < 0) | |
1344 | return r; | |
1345 | ||
1346 | /* And place the cloned version in its place */ | |
1347 | if (move_mount(mount_fd, "", -1, p, MOVE_MOUNT_F_EMPTY_PATH) < 0) | |
1348 | return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", p); | |
1349 | ||
1350 | return 0; | |
1351 | } | |
9c653536 | 1352 | |
17b798d9 LP |
1353 | int remount_idmap(const char *p, uid_t uid_shift, uid_t uid_range, uid_t owner, RemountIdmapping idmapping) { |
1354 | _cleanup_close_ int userns_fd = -EBADF; | |
1355 | ||
1356 | userns_fd = make_userns(uid_shift, uid_range, owner, idmapping); | |
1357 | if (userns_fd < 0) | |
1358 | return userns_fd; | |
1359 | ||
1360 | return remount_idmap_fd(p, userns_fd); | |
1361 | } | |
1362 | ||
f63a2c48 YW |
1363 | typedef struct SubMount { |
1364 | char *path; | |
1365 | int mount_fd; | |
1366 | } SubMount; | |
1367 | ||
1368 | static void sub_mount_clear(SubMount *s) { | |
1369 | assert(s); | |
1370 | ||
1371 | s->path = mfree(s->path); | |
1372 | s->mount_fd = safe_close(s->mount_fd); | |
1373 | } | |
1374 | ||
1375 | static void sub_mount_array_free(SubMount *s, size_t n) { | |
1376 | assert(s || n == 0); | |
1377 | ||
1378 | for (size_t i = 0; i < n; i++) | |
1379 | sub_mount_clear(s + i); | |
1380 | ||
1381 | free(s); | |
1382 | } | |
1383 | ||
1384 | static int sub_mount_compare(const SubMount *a, const SubMount *b) { | |
1385 | assert(a); | |
1386 | assert(b); | |
1387 | assert(a->path); | |
1388 | assert(b->path); | |
1389 | ||
1390 | return path_compare(a->path, b->path); | |
1391 | } | |
1392 | ||
1393 | static void sub_mount_drop(SubMount *s, size_t n) { | |
1394 | assert(s || n == 0); | |
1395 | ||
1396 | for (size_t m = 0, i = 1; i < n; i++) { | |
1397 | if (path_startswith(s[i].path, s[m].path)) | |
1398 | sub_mount_clear(s + i); | |
1399 | else | |
1400 | m = i; | |
1401 | } | |
1402 | } | |
1403 | ||
1b618bf1 LP |
1404 | static int get_sub_mounts( |
1405 | const char *prefix, | |
1b618bf1 LP |
1406 | SubMount **ret_mounts, |
1407 | size_t *ret_n_mounts) { | |
f63a2c48 YW |
1408 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
1409 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
1410 | SubMount *mounts = NULL; | |
1411 | size_t n = 0; | |
1412 | int r; | |
1413 | ||
1414 | CLEANUP_ARRAY(mounts, n, sub_mount_array_free); | |
1415 | ||
1416 | assert(prefix); | |
1417 | assert(ret_mounts); | |
1418 | assert(ret_n_mounts); | |
1419 | ||
1420 | r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter); | |
1421 | if (r < 0) | |
1422 | return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); | |
1423 | ||
1424 | for (;;) { | |
1425 | _cleanup_close_ int mount_fd = -EBADF; | |
1426 | _cleanup_free_ char *p = NULL; | |
1427 | struct libmnt_fs *fs; | |
1428 | const char *path; | |
1429 | int id1, id2; | |
1430 | ||
1431 | r = mnt_table_next_fs(table, iter, &fs); | |
1432 | if (r == 1) | |
1433 | break; /* EOF */ | |
1434 | if (r < 0) | |
1435 | return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
1436 | ||
1437 | path = mnt_fs_get_target(fs); | |
1438 | if (!path) | |
1439 | continue; | |
1440 | ||
1441 | if (isempty(path_startswith(path, prefix))) | |
1442 | continue; | |
1443 | ||
1444 | id1 = mnt_fs_get_id(fs); | |
1445 | r = path_get_mnt_id(path, &id2); | |
1446 | if (r < 0) { | |
1447 | log_debug_errno(r, "Failed to get mount ID of '%s', ignoring: %m", path); | |
1448 | continue; | |
1449 | } | |
1450 | if (id1 != id2) { | |
1451 | /* The path may be hidden by another over-mount or already remounted. */ | |
1452 | log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.", | |
1453 | path, id1, id2); | |
1454 | continue; | |
1455 | } | |
1456 | ||
2b60ce54 | 1457 | mount_fd = open(path, O_CLOEXEC|O_PATH); |
f63a2c48 YW |
1458 | if (mount_fd < 0) { |
1459 | if (errno == ENOENT) /* The path may be hidden by another over-mount or already unmounted. */ | |
1460 | continue; | |
1461 | ||
1b618bf1 | 1462 | return log_debug_errno(errno, "Failed to open subtree of mounted filesystem '%s': %m", path); |
f63a2c48 YW |
1463 | } |
1464 | ||
1465 | p = strdup(path); | |
1466 | if (!p) | |
1467 | return log_oom_debug(); | |
1468 | ||
1469 | if (!GREEDY_REALLOC(mounts, n + 1)) | |
1470 | return log_oom_debug(); | |
1471 | ||
1472 | mounts[n++] = (SubMount) { | |
1473 | .path = TAKE_PTR(p), | |
1474 | .mount_fd = TAKE_FD(mount_fd), | |
1475 | }; | |
1476 | } | |
1477 | ||
1478 | typesafe_qsort(mounts, n, sub_mount_compare); | |
1479 | sub_mount_drop(mounts, n); | |
1480 | ||
1481 | *ret_mounts = TAKE_PTR(mounts); | |
1482 | *ret_n_mounts = n; | |
1483 | return 0; | |
1484 | } | |
1485 | ||
1b618bf1 LP |
1486 | int bind_mount_submounts( |
1487 | const char *source, | |
1488 | const char *target) { | |
1489 | ||
1490 | SubMount *mounts = NULL; | |
1491 | size_t n = 0; | |
1492 | int ret = 0, r; | |
1493 | ||
1494 | /* Bind mounts all child mounts of 'source' to 'target'. Useful when setting up a new procfs instance | |
1495 | * with new mount options to copy the original submounts over. */ | |
1496 | ||
1497 | assert(source); | |
1498 | assert(target); | |
1499 | ||
1500 | CLEANUP_ARRAY(mounts, n, sub_mount_array_free); | |
1501 | ||
2b60ce54 | 1502 | r = get_sub_mounts(source, &mounts, &n); |
1b618bf1 LP |
1503 | if (r < 0) |
1504 | return r; | |
1505 | ||
1506 | FOREACH_ARRAY(m, mounts, n) { | |
1507 | _cleanup_free_ char *t = NULL; | |
1508 | const char *suffix; | |
1509 | ||
1510 | if (isempty(m->path)) | |
1511 | continue; | |
1512 | ||
1513 | assert_se(suffix = path_startswith(m->path, source)); | |
1514 | ||
1515 | t = path_join(target, suffix); | |
1516 | if (!t) | |
1517 | return -ENOMEM; | |
1518 | ||
1519 | r = path_is_mount_point(t, NULL, 0); | |
1520 | if (r < 0) { | |
1521 | log_debug_errno(r, "Failed to detect if '%s' already is a mount point, ignoring: %m", t); | |
1522 | continue; | |
1523 | } | |
1524 | if (r > 0) { | |
1525 | log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix, source, target); | |
1526 | continue; | |
1527 | } | |
1528 | ||
1529 | r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(m->mount_fd), t, NULL, MS_BIND|MS_REC, NULL); | |
1530 | if (r < 0 && ret == 0) | |
1531 | ret = r; | |
1532 | } | |
1533 | ||
1534 | return ret; | |
1535 | } | |
1536 | ||
9c653536 ZJS |
1537 | int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode) { |
1538 | assert(st); | |
1539 | assert(dest); | |
1540 | ||
1541 | if (S_ISDIR(st->st_mode)) | |
1542 | return mkdir_label(dest, mode); | |
1543 | else | |
b6ca2b28 | 1544 | return RET_NERRNO(mknod(dest, S_IFREG|(mode & ~0111), 0)); |
9c653536 ZJS |
1545 | } |
1546 | ||
1547 | int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode) { | |
1548 | struct stat st; | |
1549 | ||
1550 | assert(source); | |
1551 | assert(dest); | |
1552 | ||
1553 | if (stat(source, &st) < 0) | |
1554 | return -errno; | |
1555 | ||
1556 | return make_mount_point_inode_from_stat(&st, dest, mode); | |
1557 | } | |
506c1bb5 DDM |
1558 | |
1559 | int trigger_automount_at(int dir_fd, const char *path) { | |
1560 | _cleanup_free_ char *nested = NULL; | |
1561 | ||
1562 | assert(dir_fd >= 0 || dir_fd == AT_FDCWD); | |
1563 | ||
1564 | nested = path_join(path, "a"); | |
1565 | if (!nested) | |
1566 | return -ENOMEM; | |
1567 | ||
1568 | (void) faccessat(dir_fd, nested, F_OK, 0); | |
1569 | ||
1570 | return 0; | |
1571 | } | |
1155f44f LP |
1572 | |
1573 | unsigned long credentials_fs_mount_flags(bool ro) { | |
1574 | /* A tight set of mount flags for credentials mounts */ | |
1575 | return MS_NODEV|MS_NOEXEC|MS_NOSUID|ms_nosymfollow_supported()|(ro ? MS_RDONLY : 0); | |
1576 | } | |
1577 | ||
1578 | int mount_credentials_fs(const char *path, size_t size, bool ro) { | |
1579 | _cleanup_free_ char *opts = NULL; | |
1580 | int r, noswap_supported; | |
1581 | ||
1582 | /* Mounts a file system we can place credentials in, i.e. with tight access modes right from the | |
1583 | * beginning, and ideally swapping turned off. In order of preference: | |
1584 | * | |
1585 | * 1. tmpfs if it supports "noswap" | |
1586 | * 2. ramfs | |
1587 | * 3. tmpfs if it doesn't support "noswap" | |
1588 | */ | |
1589 | ||
1590 | noswap_supported = mount_option_supported("tmpfs", "noswap", NULL); /* Check explicitly to avoid kmsg noise */ | |
1591 | if (noswap_supported > 0) { | |
1592 | _cleanup_free_ char *noswap_opts = NULL; | |
1593 | ||
1594 | if (asprintf(&noswap_opts, "mode=0700,nr_inodes=1024,size=%zu,noswap", size) < 0) | |
1595 | return -ENOMEM; | |
1596 | ||
1597 | /* Best case: tmpfs with noswap (needs kernel >= 6.3) */ | |
1598 | ||
1599 | r = mount_nofollow_verbose( | |
1600 | LOG_DEBUG, | |
1601 | "tmpfs", | |
1602 | path, | |
1603 | "tmpfs", | |
1604 | credentials_fs_mount_flags(ro), | |
1605 | noswap_opts); | |
1606 | if (r >= 0) | |
1607 | return r; | |
1608 | } | |
1609 | ||
1610 | r = mount_nofollow_verbose( | |
1611 | LOG_DEBUG, | |
1612 | "ramfs", | |
1613 | path, | |
1614 | "ramfs", | |
1615 | credentials_fs_mount_flags(ro), | |
1616 | "mode=0700"); | |
1617 | if (r >= 0) | |
1618 | return r; | |
1619 | ||
1620 | if (asprintf(&opts, "mode=0700,nr_inodes=1024,size=%zu", size) < 0) | |
1621 | return -ENOMEM; | |
1622 | ||
1623 | return mount_nofollow_verbose( | |
1624 | LOG_DEBUG, | |
1625 | "tmpfs", | |
1626 | path, | |
1627 | "tmpfs", | |
1628 | credentials_fs_mount_flags(ro), | |
1629 | opts); | |
1630 | } | |
44cc82bf LP |
1631 | |
1632 | int make_fsmount( | |
1633 | int error_log_level, | |
1634 | const char *what, | |
1635 | const char *type, | |
1636 | unsigned long flags, | |
1637 | const char *options, | |
1638 | int userns_fd) { | |
1639 | ||
1640 | _cleanup_close_ int fs_fd = -EBADF, mnt_fd = -EBADF; | |
1641 | _cleanup_free_ char *o = NULL; | |
1642 | unsigned long f; | |
1643 | int r; | |
1644 | ||
1645 | assert(type); | |
1646 | assert(what); | |
1647 | ||
1648 | r = mount_option_mangle(options, flags, &f, &o); | |
1649 | if (r < 0) | |
1650 | return log_full_errno( | |
1651 | error_log_level, r, "Failed to mangle mount options %s: %m", | |
1652 | strempty(options)); | |
1653 | ||
1654 | if (DEBUG_LOGGING) { | |
1655 | _cleanup_free_ char *fl = NULL; | |
1656 | (void) mount_flags_to_string(f, &fl); | |
1657 | ||
1658 | log_debug("Creating mount fd for %s (%s) (%s \"%s\")...", | |
1659 | strna(what), strna(type), strnull(fl), strempty(o)); | |
1660 | } | |
1661 | ||
1662 | fs_fd = fsopen(type, FSOPEN_CLOEXEC); | |
1663 | if (fs_fd < 0) | |
1664 | return log_full_errno(error_log_level, errno, "Failed to open superblock for \"%s\": %m", type); | |
1665 | ||
1666 | if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "source", what, 0) < 0) | |
1667 | return log_full_errno(error_log_level, errno, "Failed to set mount source for \"%s\" to \"%s\": %m", type, what); | |
1668 | ||
1669 | if (FLAGS_SET(f, MS_RDONLY)) | |
1670 | if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "ro", NULL, 0) < 0) | |
1671 | return log_full_errno(error_log_level, errno, "Failed to set read only mount flag for \"%s\": %m", type); | |
1672 | ||
1673 | for (const char *p = o;;) { | |
1674 | _cleanup_free_ char *word = NULL; | |
1675 | char *eq; | |
1676 | ||
1677 | r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE); | |
1678 | if (r < 0) | |
1679 | return log_full_errno(error_log_level, r, "Failed to parse mount option string \"%s\": %m", o); | |
1680 | if (r == 0) | |
1681 | break; | |
1682 | ||
1683 | eq = strchr(word, '='); | |
1684 | if (eq) { | |
1685 | *eq = 0; | |
1686 | eq++; | |
1687 | ||
1688 | if (fsconfig(fs_fd, FSCONFIG_SET_STRING, word, eq, 0) < 0) | |
1689 | return log_full_errno(error_log_level, errno, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word, eq, type); | |
1690 | } else { | |
1691 | if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, word, NULL, 0) < 0) | |
1692 | return log_full_errno(error_log_level, errno, "Failed to set mount flag \"%s\" for \"%s\": %m", word, type); | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) | |
1697 | return log_full_errno(error_log_level, errno, "Failed to realize fs fd for \"%s\" (\"%s\"): %m", what, type); | |
1698 | ||
1699 | mnt_fd = fsmount(fs_fd, FSMOUNT_CLOEXEC, 0); | |
1700 | if (mnt_fd < 0) | |
1701 | return log_full_errno(error_log_level, errno, "Failed to create mount fd for \"%s\" (\"%s\"): %m", what, type); | |
1702 | ||
1703 | if (mount_setattr(mnt_fd, "", AT_EMPTY_PATH|AT_RECURSIVE, | |
1704 | &(struct mount_attr) { | |
1705 | .attr_set = ms_flags_to_mount_attr(f) | (userns_fd >= 0 ? MOUNT_ATTR_IDMAP : 0), | |
1706 | .userns_fd = userns_fd, | |
1707 | }, MOUNT_ATTR_SIZE_VER0) < 0) | |
1708 | return log_full_errno(error_log_level, | |
1709 | errno, | |
1710 | "Failed to set mount flags for \"%s\" (\"%s\"): %m", | |
1711 | what, | |
1712 | type); | |
1713 | ||
1714 | return TAKE_FD(mnt_fd); | |
1715 | } |