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