]>
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 LP |
9 | #include <linux/loop.h> |
10 | #include <linux/fs.h> | |
4349cd7c | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
f4351959 | 13 | #include "chase-symlinks.h" |
70599967 | 14 | #include "dissect-image.h" |
45a68ed3 | 15 | #include "exec-util.h" |
9e7f941a | 16 | #include "extract-word.h" |
4349cd7c LP |
17 | #include "fd-util.h" |
18 | #include "fileio.h" | |
e1873695 | 19 | #include "fs-util.h" |
93cc7779 | 20 | #include "hashmap.h" |
9c653536 | 21 | #include "label.h" |
13dcfe46 | 22 | #include "libmount-util.h" |
1c092b62 | 23 | #include "missing_mount.h" |
35fd3558 | 24 | #include "missing_syscall.h" |
6af52c3a | 25 | #include "mkdir.h" |
4349cd7c | 26 | #include "mount-util.h" |
049af8ad | 27 | #include "mountpoint-util.h" |
2338a175 | 28 | #include "namespace-util.h" |
4349cd7c LP |
29 | #include "parse-util.h" |
30 | #include "path-util.h" | |
6af52c3a | 31 | #include "process-util.h" |
4349cd7c | 32 | #include "set.h" |
28126409 | 33 | #include "stat-util.h" |
15a5e950 | 34 | #include "stdio-util.h" |
4349cd7c | 35 | #include "string-util.h" |
6b7c9f8b | 36 | #include "strv.h" |
6af52c3a | 37 | #include "tmpfile-util.h" |
70599967 | 38 | #include "user-util.h" |
4349cd7c | 39 | |
28126409 LP |
40 | int mount_fd(const char *source, |
41 | int target_fd, | |
42 | const char *filesystemtype, | |
43 | unsigned long mountflags, | |
44 | const void *data) { | |
45 | ||
ddb6eeaf | 46 | if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) { |
28126409 LP |
47 | if (errno != ENOENT) |
48 | return -errno; | |
49 | ||
50 | /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't | |
51 | * mounted. Check for the latter to generate better error messages. */ | |
52 | if (proc_mounted() == 0) | |
53 | return -ENOSYS; | |
54 | ||
55 | return -ENOENT; | |
56 | } | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
61 | int mount_nofollow( | |
62 | const char *source, | |
63 | const char *target, | |
64 | const char *filesystemtype, | |
65 | unsigned long mountflags, | |
66 | const void *data) { | |
67 | ||
68 | _cleanup_close_ int fd = -1; | |
69 | ||
70 | /* In almost all cases we want to manipulate the mount table without following symlinks, hence | |
71 | * mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is | |
72 | * not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early | |
73 | * initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the | |
74 | * fs to mount) we can only use traditional mount() directly. | |
75 | * | |
76 | * Note that this disables following only for the final component of the target, i.e symlinks within | |
77 | * the path of the target are honoured, as are symlinks in the source path everywhere. */ | |
78 | ||
79 | fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW); | |
80 | if (fd < 0) | |
81 | return -errno; | |
82 | ||
83 | return mount_fd(source, fd, filesystemtype, mountflags, data); | |
84 | } | |
85 | ||
4349cd7c | 86 | int umount_recursive(const char *prefix, int flags) { |
4349cd7c | 87 | int n = 0, r; |
f8b1904f | 88 | bool again; |
4349cd7c | 89 | |
9d0619de LP |
90 | /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and |
91 | * keep unmounting them until they are gone. */ | |
4349cd7c LP |
92 | |
93 | do { | |
13dcfe46 ZJS |
94 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
95 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
4349cd7c LP |
96 | |
97 | again = false; | |
4349cd7c | 98 | |
2f2d81d9 | 99 | r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter); |
fdeea3f4 | 100 | if (r < 0) |
13dcfe46 | 101 | return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); |
35bbbf85 | 102 | |
4349cd7c | 103 | for (;;) { |
13dcfe46 ZJS |
104 | struct libmnt_fs *fs; |
105 | const char *path; | |
4349cd7c | 106 | |
13dcfe46 ZJS |
107 | r = mnt_table_next_fs(table, iter, &fs); |
108 | if (r == 1) | |
109 | break; | |
110 | if (r < 0) | |
111 | return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
4349cd7c | 112 | |
13dcfe46 ZJS |
113 | path = mnt_fs_get_target(fs); |
114 | if (!path) | |
115 | continue; | |
4349cd7c | 116 | |
13dcfe46 | 117 | if (!path_startswith(path, prefix)) |
4349cd7c LP |
118 | continue; |
119 | ||
827ea521 LP |
120 | if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) { |
121 | log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path); | |
4349cd7c LP |
122 | continue; |
123 | } | |
124 | ||
13dcfe46 | 125 | log_debug("Successfully unmounted %s", path); |
6b7c9f8b | 126 | |
4349cd7c LP |
127 | again = true; |
128 | n++; | |
129 | ||
130 | break; | |
131 | } | |
4349cd7c LP |
132 | } while (again); |
133 | ||
13dcfe46 | 134 | return n; |
4349cd7c LP |
135 | } |
136 | ||
be3f3752 | 137 | /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it |
64e82c19 LP |
138 | * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */ |
139 | int bind_remount_recursive_with_mountinfo( | |
140 | const char *prefix, | |
141 | unsigned long new_flags, | |
142 | unsigned long flags_mask, | |
6b000af4 | 143 | char **deny_list, |
64e82c19 LP |
144 | FILE *proc_self_mountinfo) { |
145 | ||
ba8dced2 | 146 | _cleanup_set_free_ Set *done = NULL; |
670e8efd | 147 | unsigned n_tries = 0; |
4349cd7c LP |
148 | int r; |
149 | ||
8403219f | 150 | assert(prefix); |
ac9de0b3 TR |
151 | assert(proc_self_mountinfo); |
152 | ||
ddc155b2 TM |
153 | /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY, |
154 | * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it | |
155 | * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it | |
156 | * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can | |
157 | * access, too. When mounts are stacked on the same mount point we only care for each individual | |
158 | * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do | |
159 | * not have any effect on future submounts that might get propagated, they might be writable | |
4b6ef527 LP |
160 | * etc. This includes future submounts that have been triggered via autofs. Also note that we can't |
161 | * operate atomically here. Mounts established while we process the tree might or might not get | |
162 | * noticed and thus might or might not be covered. | |
6b7c9f8b | 163 | * |
6b000af4 LP |
164 | * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the |
165 | * remount operation. Note that we'll ignore the deny list for the top-level path. */ | |
4349cd7c | 166 | |
4349cd7c | 167 | for (;;) { |
13dcfe46 ZJS |
168 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
169 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
ba8dced2 | 170 | _cleanup_hashmap_free_ Hashmap *todo = NULL; |
4349cd7c | 171 | bool top_autofs = false; |
4349cd7c | 172 | |
670e8efd LP |
173 | if (n_tries++ >= 32) /* Let's not retry this loop forever */ |
174 | return -EBUSY; | |
175 | ||
ac9de0b3 | 176 | rewind(proc_self_mountinfo); |
4349cd7c | 177 | |
e2857b3d | 178 | r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter); |
13dcfe46 ZJS |
179 | if (r < 0) |
180 | return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m"); | |
4349cd7c | 181 | |
13dcfe46 | 182 | for (;;) { |
ba8dced2 LP |
183 | _cleanup_free_ char *d = NULL; |
184 | const char *path, *type, *opts; | |
185 | unsigned long flags = 0; | |
13dcfe46 | 186 | struct libmnt_fs *fs; |
4349cd7c | 187 | |
13dcfe46 | 188 | r = mnt_table_next_fs(table, iter, &fs); |
d6bfab11 | 189 | if (r == 1) /* EOF */ |
13dcfe46 | 190 | break; |
4349cd7c | 191 | if (r < 0) |
13dcfe46 | 192 | return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); |
4349cd7c | 193 | |
13dcfe46 | 194 | path = mnt_fs_get_target(fs); |
d6bfab11 | 195 | if (!path) |
6b7c9f8b LP |
196 | continue; |
197 | ||
c6111b85 | 198 | if (!path_startswith(path, prefix)) |
13dcfe46 ZJS |
199 | continue; |
200 | ||
d6bfab11 LP |
201 | type = mnt_fs_get_fstype(fs); |
202 | if (!type) | |
203 | continue; | |
204 | ||
205 | /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid | |
206 | * triggering them, as we don't make any guarantees for future submounts anyway. If | |
207 | * they are already triggered, then we will find another entry for this. */ | |
208 | if (streq(type, "autofs")) { | |
209 | top_autofs = top_autofs || path_equal(path, prefix); | |
210 | continue; | |
211 | } | |
212 | ||
213 | if (set_contains(done, path)) | |
214 | continue; | |
215 | ||
6b000af4 | 216 | /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount |
13dcfe46 | 217 | * we shall operate on. */ |
c6111b85 | 218 | if (!path_equal(path, prefix)) { |
6b000af4 | 219 | bool deny_listed = false; |
6b7c9f8b LP |
220 | char **i; |
221 | ||
6b000af4 | 222 | STRV_FOREACH(i, deny_list) { |
c6111b85 | 223 | if (path_equal(*i, prefix)) |
6b7c9f8b LP |
224 | continue; |
225 | ||
c6111b85 | 226 | if (!path_startswith(*i, prefix)) |
6b7c9f8b LP |
227 | continue; |
228 | ||
13dcfe46 | 229 | if (path_startswith(path, *i)) { |
6b000af4 | 230 | deny_listed = true; |
d6bfab11 | 231 | log_debug("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix); |
6b7c9f8b LP |
232 | break; |
233 | } | |
234 | } | |
d6bfab11 | 235 | |
6b000af4 | 236 | if (deny_listed) |
6b7c9f8b LP |
237 | continue; |
238 | } | |
239 | ||
ba8dced2 LP |
240 | opts = mnt_fs_get_vfs_options(fs); |
241 | if (opts) { | |
242 | r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP)); | |
243 | if (r < 0) | |
244 | log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path); | |
245 | } | |
246 | ||
247 | d = strdup(path); | |
248 | if (!d) | |
249 | return -ENOMEM; | |
250 | ||
251 | r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags)); | |
252 | if (r == -EEXIST) | |
e0103063 LB |
253 | /* If the same path was recorded, but with different mount flags, update it: |
254 | * it means a mount point is overmounted, and libmount returns the "bottom" (or | |
255 | * older one) first, but we want to reapply the flags from the "top" (or newer | |
256 | * one). See: https://github.com/systemd/systemd/issues/20032 | |
257 | * Note that this shouldn't really fail, as we were just told that the key | |
258 | * exists, and it's an update so we want 'd' to be freed immediately. */ | |
259 | r = hashmap_update(todo, d, ULONG_TO_PTR(flags)); | |
d6bfab11 LP |
260 | if (r < 0) |
261 | return r; | |
ba8dced2 LP |
262 | if (r > 0) |
263 | TAKE_PTR(d); | |
4349cd7c LP |
264 | } |
265 | ||
5c5753b9 LP |
266 | /* Check if the top-level directory was among what we have seen so far. For that check both |
267 | * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll | |
268 | * not include it in either set but will set this bool. */ | |
c6111b85 | 269 | if (!set_contains(done, prefix) && |
ba8dced2 | 270 | !(top_autofs || hashmap_contains(todo, prefix))) { |
5c5753b9 | 271 | |
6b7c9f8b | 272 | /* The prefix directory itself is not yet a mount, make it one. */ |
c6111b85 | 273 | r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL); |
511a8cfe LP |
274 | if (r < 0) |
275 | return r; | |
4349cd7c | 276 | |
5c5753b9 LP |
277 | /* Immediately rescan, so that we pick up the new mount's flags */ |
278 | continue; | |
4349cd7c LP |
279 | } |
280 | ||
5c5753b9 | 281 | /* If we have no submounts to process anymore, we are done */ |
ba8dced2 | 282 | if (hashmap_isempty(todo)) |
5c5753b9 LP |
283 | return 0; |
284 | ||
ba8dced2 LP |
285 | for (;;) { |
286 | unsigned long flags; | |
287 | char *x = NULL; | |
288 | ||
289 | /* Take the first mount from our list of mounts to still process */ | |
290 | flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x)); | |
291 | if (!x) | |
292 | break; | |
4349cd7c | 293 | |
ba8dced2 | 294 | r = set_ensure_consume(&done, &path_hash_ops_free, x); |
4c701096 | 295 | if (IN_SET(r, 0, -EEXIST)) |
ba8dced2 | 296 | continue; /* Already done */ |
4349cd7c LP |
297 | if (r < 0) |
298 | return r; | |
299 | ||
ba8dced2 LP |
300 | /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's |
301 | * the default anyway, thus redundant, and in userns we'll get an error if we try to | |
302 | * explicitly enable it) */ | |
303 | r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL); | |
065b4774 | 304 | if (r < 0) { |
ba8dced2 LP |
305 | int q; |
306 | ||
307 | /* OK, so the remount of this entry failed. We'll ultimately ignore this in | |
308 | * almost all cases (there are simply so many reasons why this can fail, | |
309 | * think autofs, NFS, FUSE, …), but let's generate useful debug messages at | |
310 | * the very least. */ | |
311 | ||
312 | q = path_is_mount_point(x, NULL, 0); | |
313 | if (IN_SET(q, 0, -ENOENT)) { | |
314 | /* Hmm, whaaaa? The mount point is not actually a mount point? Then | |
315 | * it is either obstructed by a later mount or somebody has been | |
316 | * racing against us and removed it. Either way the mount point | |
317 | * doesn't matter to us, let's ignore it hence. */ | |
318 | log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x); | |
319 | continue; | |
320 | } | |
321 | if (q < 0) /* Any other error on this? Just log and continue */ | |
322 | log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x); | |
323 | ||
324 | if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */ | |
325 | log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x); | |
326 | continue; | |
327 | } | |
328 | ||
329 | /* Make this fatal if this is the top-level mount */ | |
330 | if (path_equal(x, prefix)) | |
065b4774 LP |
331 | return r; |
332 | ||
ba8dced2 LP |
333 | /* If this is not the top-level mount, then handle this gracefully: log but |
334 | * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons | |
335 | * this might fail without a chance for us to do anything about it, let's | |
336 | * hence be strict on the top-level mount and lenient on the inner ones. */ | |
337 | log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x); | |
ef454fd1 YW |
338 | continue; |
339 | } | |
98df8089 | 340 | |
ba8dced2 | 341 | log_debug("Remounted %s.", x); |
4349cd7c LP |
342 | } |
343 | } | |
344 | } | |
345 | ||
8403219f LP |
346 | int bind_remount_recursive( |
347 | const char *prefix, | |
348 | unsigned long new_flags, | |
349 | unsigned long flags_mask, | |
6b000af4 | 350 | char **deny_list) { |
8403219f | 351 | |
ac9de0b3 | 352 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; |
fdeea3f4 | 353 | int r; |
ac9de0b3 | 354 | |
fdeea3f4 ZJS |
355 | r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo); |
356 | if (r < 0) | |
357 | return r; | |
35bbbf85 | 358 | |
6b000af4 | 359 | return bind_remount_recursive_with_mountinfo(prefix, new_flags, flags_mask, deny_list, proc_self_mountinfo); |
ac9de0b3 TR |
360 | } |
361 | ||
7cce68e1 LP |
362 | int bind_remount_one_with_mountinfo( |
363 | const char *path, | |
364 | unsigned long new_flags, | |
365 | unsigned long flags_mask, | |
366 | FILE *proc_self_mountinfo) { | |
367 | ||
368 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; | |
2c5ff8ea LP |
369 | unsigned long flags = 0; |
370 | struct libmnt_fs *fs; | |
371 | const char *opts; | |
7cce68e1 LP |
372 | int r; |
373 | ||
374 | assert(path); | |
375 | assert(proc_self_mountinfo); | |
376 | ||
377 | rewind(proc_self_mountinfo); | |
378 | ||
379 | table = mnt_new_table(); | |
380 | if (!table) | |
381 | return -ENOMEM; | |
382 | ||
383 | r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo"); | |
384 | if (r < 0) | |
385 | return r; | |
386 | ||
2c5ff8ea | 387 | fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD); |
0338df47 LP |
388 | if (!fs) { |
389 | if (laccess(path, F_OK) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */ | |
390 | return -errno; | |
391 | ||
2c5ff8ea | 392 | return -EINVAL; /* Not a mount point we recognize */ |
0338df47 | 393 | } |
2c5ff8ea LP |
394 | |
395 | opts = mnt_fs_get_vfs_options(fs); | |
396 | if (opts) { | |
397 | r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP)); | |
398 | if (r < 0) | |
399 | log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path); | |
400 | } | |
7cce68e1 | 401 | |
2c5ff8ea | 402 | r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL); |
b23c6a64 LP |
403 | if (r < 0) { |
404 | if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again, | |
405 | * since kernel adds it in | |
406 | * everywhere, because it's the | |
407 | * default. */ | |
408 | return r; | |
409 | ||
410 | /* Let's handle redundant remounts gracefully */ | |
411 | log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path); | |
412 | } | |
7cce68e1 LP |
413 | |
414 | return 0; | |
415 | } | |
416 | ||
4349cd7c LP |
417 | int mount_move_root(const char *path) { |
418 | assert(path); | |
419 | ||
420 | if (chdir(path) < 0) | |
421 | return -errno; | |
422 | ||
423 | if (mount(path, "/", NULL, MS_MOVE, NULL) < 0) | |
424 | return -errno; | |
425 | ||
426 | if (chroot(".") < 0) | |
427 | return -errno; | |
428 | ||
429 | if (chdir("/") < 0) | |
430 | return -errno; | |
431 | ||
432 | return 0; | |
433 | } | |
4e036b7a | 434 | |
3f2c0bec LP |
435 | int repeat_unmount(const char *path, int flags) { |
436 | bool done = false; | |
437 | ||
438 | assert(path); | |
439 | ||
440 | /* If there are multiple mounts on a mount point, this | |
441 | * removes them all */ | |
442 | ||
443 | for (;;) { | |
444 | if (umount2(path, flags) < 0) { | |
445 | ||
446 | if (errno == EINVAL) | |
447 | return done; | |
448 | ||
449 | return -errno; | |
450 | } | |
451 | ||
452 | done = true; | |
453 | } | |
454 | } | |
c4b41707 | 455 | |
48b747fa LP |
456 | int mode_to_inaccessible_node( |
457 | const char *runtime_dir, | |
458 | mode_t mode, | |
459 | char **ret) { | |
460 | ||
461 | /* This function maps a node type to a corresponding inaccessible file node. These nodes are created | |
462 | * during early boot by PID 1. In some cases we lacked the privs to create the character and block | |
463 | * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a | |
464 | * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that | |
465 | * case we use an AF_UNIX file node instead, which is not the same, but close enough for most | |
466 | * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory | |
467 | * file nodes, and that's the most important thing that matters. | |
468 | * | |
469 | * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if | |
470 | * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */ | |
471 | ||
e5f10caf AZ |
472 | _cleanup_free_ char *d = NULL; |
473 | const char *node = NULL; | |
e5f10caf | 474 | |
48b747fa LP |
475 | assert(ret); |
476 | ||
477 | if (!runtime_dir) | |
478 | runtime_dir = "/run"; | |
fe80fcc7 | 479 | |
c4b41707 AP |
480 | switch(mode & S_IFMT) { |
481 | case S_IFREG: | |
48b747fa | 482 | node = "/systemd/inaccessible/reg"; |
e5f10caf | 483 | break; |
fe80fcc7 | 484 | |
c4b41707 | 485 | case S_IFDIR: |
48b747fa | 486 | node = "/systemd/inaccessible/dir"; |
e5f10caf | 487 | break; |
fe80fcc7 | 488 | |
c4b41707 | 489 | case S_IFCHR: |
48b747fa | 490 | node = "/systemd/inaccessible/chr"; |
e5f10caf | 491 | break; |
fe80fcc7 | 492 | |
c4b41707 | 493 | case S_IFBLK: |
48b747fa | 494 | node = "/systemd/inaccessible/blk"; |
e5f10caf | 495 | break; |
fe80fcc7 | 496 | |
c4b41707 | 497 | case S_IFIFO: |
48b747fa | 498 | node = "/systemd/inaccessible/fifo"; |
e5f10caf | 499 | break; |
fe80fcc7 | 500 | |
c4b41707 | 501 | case S_IFSOCK: |
48b747fa | 502 | node = "/systemd/inaccessible/sock"; |
e5f10caf | 503 | break; |
c4b41707 | 504 | } |
e5f10caf AZ |
505 | if (!node) |
506 | return -EINVAL; | |
507 | ||
48b747fa LP |
508 | d = path_join(runtime_dir, node); |
509 | if (!d) | |
510 | return -ENOMEM; | |
511 | ||
cbed1dc8 LP |
512 | /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also |
513 | * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no | |
514 | * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the | |
515 | * inaccessible block device node let's see if the block device node actually exists, and if not, | |
516 | * fall back to the character device node. From there fall back to the socket device node. This means | |
517 | * in the best case we'll get the right device node type — but if not we'll hopefully at least get a | |
518 | * device node at all. */ | |
519 | ||
520 | if (S_ISBLK(mode) && | |
521 | access(d, F_OK) < 0 && errno == ENOENT) { | |
522 | free(d); | |
523 | d = path_join(runtime_dir, "/systemd/inaccessible/chr"); | |
524 | if (!d) | |
525 | return -ENOMEM; | |
526 | } | |
527 | ||
528 | if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) && | |
529 | access(d, F_OK) < 0 && errno == ENOENT) { | |
48b747fa LP |
530 | free(d); |
531 | d = path_join(runtime_dir, "/systemd/inaccessible/sock"); | |
532 | if (!d) | |
533 | return -ENOMEM; | |
534 | } | |
e5f10caf | 535 | |
48b747fa | 536 | *ret = TAKE_PTR(d); |
e5f10caf | 537 | return 0; |
c4b41707 | 538 | } |
60e76d48 | 539 | |
51bb6a10 | 540 | int mount_flags_to_string(long unsigned flags, char **ret) { |
1c092b62 YW |
541 | static const struct { |
542 | long unsigned flag; | |
543 | const char *name; | |
544 | } map[] = { | |
545 | { .flag = MS_RDONLY, .name = "MS_RDONLY", }, | |
546 | { .flag = MS_NOSUID, .name = "MS_NOSUID", }, | |
547 | { .flag = MS_NODEV, .name = "MS_NODEV", }, | |
548 | { .flag = MS_NOEXEC, .name = "MS_NOEXEC", }, | |
549 | { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", }, | |
550 | { .flag = MS_REMOUNT, .name = "MS_REMOUNT", }, | |
551 | { .flag = MS_MANDLOCK, .name = "MS_MANDLOCK", }, | |
552 | { .flag = MS_DIRSYNC, .name = "MS_DIRSYNC", }, | |
553 | { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", }, | |
554 | { .flag = MS_NOATIME, .name = "MS_NOATIME", }, | |
555 | { .flag = MS_NODIRATIME, .name = "MS_NODIRATIME", }, | |
556 | { .flag = MS_BIND, .name = "MS_BIND", }, | |
557 | { .flag = MS_MOVE, .name = "MS_MOVE", }, | |
558 | { .flag = MS_REC, .name = "MS_REC", }, | |
559 | { .flag = MS_SILENT, .name = "MS_SILENT", }, | |
560 | { .flag = MS_POSIXACL, .name = "MS_POSIXACL", }, | |
561 | { .flag = MS_UNBINDABLE, .name = "MS_UNBINDABLE", }, | |
562 | { .flag = MS_PRIVATE, .name = "MS_PRIVATE", }, | |
563 | { .flag = MS_SLAVE, .name = "MS_SLAVE", }, | |
564 | { .flag = MS_SHARED, .name = "MS_SHARED", }, | |
565 | { .flag = MS_RELATIME, .name = "MS_RELATIME", }, | |
566 | { .flag = MS_KERNMOUNT, .name = "MS_KERNMOUNT", }, | |
567 | { .flag = MS_I_VERSION, .name = "MS_I_VERSION", }, | |
568 | { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", }, | |
569 | { .flag = MS_LAZYTIME, .name = "MS_LAZYTIME", }, | |
570 | }; | |
571 | _cleanup_free_ char *str = NULL; | |
572 | ||
4bee2333 YW |
573 | assert(ret); |
574 | ||
1c092b62 YW |
575 | for (size_t i = 0; i < ELEMENTSOF(map); i++) |
576 | if (flags & map[i].flag) { | |
577 | if (!strextend_with_separator(&str, "|", map[i].name)) | |
578 | return -ENOMEM; | |
579 | flags &= ~map[i].flag; | |
580 | } | |
581 | ||
582 | if (!str || flags != 0) | |
583 | if (strextendf_with_separator(&str, "|", "%lx", flags) < 0) | |
584 | return -ENOMEM; | |
585 | ||
586 | *ret = TAKE_PTR(str); | |
587 | return 0; | |
60e76d48 ZJS |
588 | } |
589 | ||
511a8cfe | 590 | int mount_verbose_full( |
60e76d48 ZJS |
591 | int error_log_level, |
592 | const char *what, | |
593 | const char *where, | |
594 | const char *type, | |
595 | unsigned long flags, | |
511a8cfe LP |
596 | const char *options, |
597 | bool follow_symlink) { | |
60e76d48 | 598 | |
6ef8df2b YW |
599 | _cleanup_free_ char *fl = NULL, *o = NULL; |
600 | unsigned long f; | |
601 | int r; | |
602 | ||
603 | r = mount_option_mangle(options, flags, &f, &o); | |
604 | if (r < 0) | |
605 | return log_full_errno(error_log_level, r, | |
606 | "Failed to mangle mount options %s: %m", | |
607 | strempty(options)); | |
60e76d48 | 608 | |
1c092b62 | 609 | (void) mount_flags_to_string(f, &fl); |
60e76d48 | 610 | |
6ef8df2b | 611 | if ((f & MS_REMOUNT) && !what && !type) |
60e76d48 | 612 | log_debug("Remounting %s (%s \"%s\")...", |
6ef8df2b | 613 | where, strnull(fl), strempty(o)); |
60e76d48 ZJS |
614 | else if (!what && !type) |
615 | log_debug("Mounting %s (%s \"%s\")...", | |
6ef8df2b YW |
616 | where, strnull(fl), strempty(o)); |
617 | else if ((f & MS_BIND) && !type) | |
60e76d48 | 618 | log_debug("Bind-mounting %s on %s (%s \"%s\")...", |
6ef8df2b YW |
619 | what, where, strnull(fl), strempty(o)); |
620 | else if (f & MS_MOVE) | |
afe682bc | 621 | log_debug("Moving mount %s → %s (%s \"%s\")...", |
6ef8df2b | 622 | what, where, strnull(fl), strempty(o)); |
60e76d48 | 623 | else |
3b493d94 LP |
624 | log_debug("Mounting %s (%s) on %s (%s \"%s\")...", |
625 | strna(what), strna(type), where, strnull(fl), strempty(o)); | |
511a8cfe LP |
626 | |
627 | if (follow_symlink) | |
628 | r = mount(what, where, type, f, o) < 0 ? -errno : 0; | |
629 | else | |
630 | r = mount_nofollow(what, where, type, f, o); | |
631 | if (r < 0) | |
632 | return log_full_errno(error_log_level, r, | |
3ccf6126 LP |
633 | "Failed to mount %s (type %s) on %s (%s \"%s\"): %m", |
634 | strna(what), strna(type), where, strnull(fl), strempty(o)); | |
60e76d48 ZJS |
635 | return 0; |
636 | } | |
637 | ||
30f5d104 LP |
638 | int umount_verbose( |
639 | int error_log_level, | |
640 | const char *what, | |
641 | int flags) { | |
642 | ||
643 | assert(what); | |
644 | ||
60e76d48 | 645 | log_debug("Umounting %s...", what); |
30f5d104 LP |
646 | |
647 | if (umount2(what, flags) < 0) | |
648 | return log_full_errno(error_log_level, errno, | |
649 | "Failed to unmount %s: %m", what); | |
650 | ||
60e76d48 ZJS |
651 | return 0; |
652 | } | |
83555251 | 653 | |
9e7f941a YW |
654 | int mount_option_mangle( |
655 | const char *options, | |
656 | unsigned long mount_flags, | |
657 | unsigned long *ret_mount_flags, | |
658 | char **ret_remaining_options) { | |
659 | ||
660 | const struct libmnt_optmap *map; | |
661 | _cleanup_free_ char *ret = NULL; | |
9e7f941a YW |
662 | int r; |
663 | ||
664 | /* This extracts mount flags from the mount options, and store | |
665 | * non-mount-flag options to '*ret_remaining_options'. | |
666 | * E.g., | |
667 | * "rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000" | |
668 | * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and | |
669 | * "size=1630748k,mode=700,uid=1000,gid=1000". | |
670 | * See more examples in test-mount-utils.c. | |
671 | * | |
672 | * Note that if 'options' does not contain any non-mount-flag options, | |
5238e957 | 673 | * then '*ret_remaining_options' is set to NULL instead of empty string. |
9e7f941a YW |
674 | * Note that this does not check validity of options stored in |
675 | * '*ret_remaining_options'. | |
676 | * Note that if 'options' is NULL, then this just copies 'mount_flags' | |
677 | * to '*ret_mount_flags'. */ | |
678 | ||
679 | assert(ret_mount_flags); | |
680 | assert(ret_remaining_options); | |
681 | ||
682 | map = mnt_get_builtin_optmap(MNT_LINUX_MAP); | |
683 | if (!map) | |
684 | return -EINVAL; | |
685 | ||
25086b4c | 686 | for (const char *p = options;;) { |
9e7f941a YW |
687 | _cleanup_free_ char *word = NULL; |
688 | const struct libmnt_optmap *ent; | |
689 | ||
9b23679e | 690 | r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE); |
9e7f941a YW |
691 | if (r < 0) |
692 | return r; | |
693 | if (r == 0) | |
694 | break; | |
695 | ||
696 | for (ent = map; ent->name; ent++) { | |
697 | /* All entries in MNT_LINUX_MAP do not take any argument. | |
698 | * Thus, ent->name does not contain "=" or "[=]". */ | |
699 | if (!streq(word, ent->name)) | |
700 | continue; | |
701 | ||
702 | if (!(ent->mask & MNT_INVERT)) | |
703 | mount_flags |= ent->id; | |
704 | else if (mount_flags & ent->id) | |
705 | mount_flags ^= ent->id; | |
706 | ||
707 | break; | |
708 | } | |
709 | ||
710 | /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */ | |
c2bc710b | 711 | if (!ent->name && !strextend_with_separator(&ret, ",", word)) |
9e7f941a YW |
712 | return -ENOMEM; |
713 | } | |
714 | ||
715 | *ret_mount_flags = mount_flags; | |
ae2a15bc | 716 | *ret_remaining_options = TAKE_PTR(ret); |
9e7f941a YW |
717 | |
718 | return 0; | |
719 | } | |
6af52c3a | 720 | |
70599967 | 721 | static int mount_in_namespace( |
6af52c3a LB |
722 | pid_t target, |
723 | const char *propagate_path, | |
724 | const char *incoming_path, | |
725 | const char *src, | |
726 | const char *dest, | |
727 | bool read_only, | |
70599967 LB |
728 | bool make_file_or_directory, |
729 | const MountOptions *options, | |
730 | bool is_image) { | |
6af52c3a LB |
731 | |
732 | _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 }; | |
f7c18d3d | 733 | _cleanup_close_ int self_mntns_fd = -1, mntns_fd = -1, root_fd = -1, pidns_fd = -1, chased_src_fd = -1; |
ddb6eeaf | 734 | char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p; |
6af52c3a LB |
735 | bool mount_slave_created = false, mount_slave_mounted = false, |
736 | mount_tmp_created = false, mount_tmp_mounted = false, | |
737 | mount_outside_created = false, mount_outside_mounted = false; | |
2338a175 | 738 | struct stat st, self_mntns_st; |
6af52c3a LB |
739 | pid_t child; |
740 | int r; | |
741 | ||
742 | assert(target > 0); | |
743 | assert(propagate_path); | |
744 | assert(incoming_path); | |
745 | assert(src); | |
746 | assert(dest); | |
70599967 | 747 | assert(!options || is_image); |
6af52c3a | 748 | |
98f654fd | 749 | r = namespace_open(target, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd); |
2338a175 LB |
750 | if (r < 0) |
751 | return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m"); | |
752 | ||
753 | if (fstat(mntns_fd, &st) < 0) | |
754 | return log_debug_errno(errno, "Failed to fstat mount namespace FD of target process: %m"); | |
755 | ||
756 | r = namespace_open(0, NULL, &self_mntns_fd, NULL, NULL, NULL); | |
757 | if (r < 0) | |
758 | return log_debug_errno(r, "Failed to retrieve FDs of systemd's namespace: %m"); | |
759 | ||
760 | if (fstat(self_mntns_fd, &self_mntns_st) < 0) | |
761 | return log_debug_errno(errno, "Failed to fstat mount namespace FD of systemd: %m"); | |
762 | ||
763 | /* We can't add new mounts at runtime if the process wasn't started in a namespace */ | |
764 | if (st.st_ino == self_mntns_st.st_ino && st.st_dev == self_mntns_st.st_dev) | |
765 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace"); | |
766 | ||
ddb6eeaf LP |
767 | /* One day, when bind mounting /proc/self/fd/n works across namespace boundaries we should rework |
768 | * this logic to make use of it... */ | |
6af52c3a LB |
769 | |
770 | p = strjoina(propagate_path, "/"); | |
771 | r = laccess(p, F_OK); | |
772 | if (r < 0) | |
773 | return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points"); | |
774 | ||
f7c18d3d | 775 | r = chase_symlinks(src, NULL, CHASE_TRAIL_SLASH, NULL, &chased_src_fd); |
6af52c3a LB |
776 | if (r < 0) |
777 | return log_debug_errno(r, "Failed to resolve source path of %s: %m", src); | |
778 | ||
f7c18d3d LB |
779 | if (fstat(chased_src_fd, &st) < 0) |
780 | return log_debug_errno(errno, "Failed to stat() resolved source path %s: %m", src); | |
6af52c3a | 781 | 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… */ |
f7c18d3d | 782 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Source directory %s can't be a symbolic link", src); |
6af52c3a LB |
783 | |
784 | /* Our goal is to install a new bind mount into the container, | |
785 | possibly read-only. This is irritatingly complex | |
786 | unfortunately, currently. | |
787 | ||
788 | First, we start by creating a private playground in /tmp, | |
789 | that we can mount MS_SLAVE. (Which is necessary, since | |
790 | MS_MOVE cannot be applied to mounts with MS_SHARED parent | |
791 | mounts.) */ | |
792 | ||
793 | if (!mkdtemp(mount_slave)) | |
794 | return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave); | |
795 | ||
796 | mount_slave_created = true; | |
797 | ||
798 | r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL); | |
799 | if (r < 0) | |
800 | goto finish; | |
801 | ||
802 | mount_slave_mounted = true; | |
803 | ||
804 | r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL); | |
805 | if (r < 0) | |
806 | goto finish; | |
807 | ||
808 | /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */ | |
809 | mount_tmp = strjoina(mount_slave, "/mount"); | |
70599967 LB |
810 | if (is_image) |
811 | r = mkdir_p(mount_tmp, 0700); | |
812 | else | |
813 | r = make_mount_point_inode_from_stat(&st, mount_tmp, 0700); | |
6af52c3a LB |
814 | if (r < 0) { |
815 | log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp); | |
816 | goto finish; | |
817 | } | |
818 | ||
819 | mount_tmp_created = true; | |
820 | ||
70599967 | 821 | if (is_image) |
ddb6eeaf | 822 | r = verity_dissect_and_mount(FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, options, NULL, NULL, NULL); |
70599967 | 823 | else |
ddb6eeaf | 824 | r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL); |
6af52c3a LB |
825 | if (r < 0) |
826 | goto finish; | |
827 | ||
828 | mount_tmp_mounted = true; | |
829 | ||
830 | /* Third, we remount the new bind mount read-only if requested. */ | |
831 | if (read_only) { | |
832 | r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL); | |
833 | if (r < 0) | |
834 | goto finish; | |
835 | } | |
836 | ||
837 | /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only | |
838 | * right-away. */ | |
839 | ||
840 | mount_outside = strjoina(propagate_path, "/XXXXXX"); | |
70599967 | 841 | if (is_image || S_ISDIR(st.st_mode)) |
6af52c3a LB |
842 | r = mkdtemp(mount_outside) ? 0 : -errno; |
843 | else { | |
844 | r = mkostemp_safe(mount_outside); | |
845 | safe_close(r); | |
846 | } | |
847 | if (r < 0) { | |
848 | log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside); | |
849 | goto finish; | |
850 | } | |
851 | ||
852 | mount_outside_created = true; | |
853 | ||
854 | r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL); | |
855 | if (r < 0) | |
856 | goto finish; | |
857 | ||
858 | mount_outside_mounted = true; | |
859 | mount_tmp_mounted = false; | |
860 | ||
70599967 | 861 | if (is_image || S_ISDIR(st.st_mode)) |
6af52c3a LB |
862 | (void) rmdir(mount_tmp); |
863 | else | |
864 | (void) unlink(mount_tmp); | |
865 | mount_tmp_created = false; | |
866 | ||
867 | (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW); | |
868 | mount_slave_mounted = false; | |
869 | ||
870 | (void) rmdir(mount_slave); | |
871 | mount_slave_created = false; | |
872 | ||
873 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) { | |
874 | log_debug_errno(errno, "Failed to create pipe: %m"); | |
875 | goto finish; | |
876 | } | |
877 | ||
2338a175 | 878 | r = namespace_fork("(sd-bindmnt)", "(sd-bindmnt-inner)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
98f654fd | 879 | pidns_fd, mntns_fd, -1, -1, root_fd, &child); |
6af52c3a LB |
880 | if (r < 0) |
881 | goto finish; | |
882 | if (r == 0) { | |
2338a175 | 883 | const char *mount_inside; |
6af52c3a LB |
884 | |
885 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); | |
886 | ||
6af52c3a | 887 | if (make_file_or_directory) { |
70599967 LB |
888 | if (!is_image) { |
889 | (void) mkdir_parents(dest, 0755); | |
890 | (void) make_mount_point_inode_from_stat(&st, dest, 0700); | |
891 | } else | |
892 | (void) mkdir_p(dest, 0755); | |
6af52c3a LB |
893 | } |
894 | ||
895 | /* Fifth, move the mount to the right place inside */ | |
896 | mount_inside = strjoina(incoming_path, basename(mount_outside)); | |
897 | r = mount_nofollow_verbose(LOG_ERR, mount_inside, dest, NULL, MS_MOVE, NULL); | |
898 | if (r < 0) | |
899 | goto child_fail; | |
900 | ||
901 | _exit(EXIT_SUCCESS); | |
902 | ||
903 | child_fail: | |
904 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
905 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
906 | ||
907 | _exit(EXIT_FAILURE); | |
908 | } | |
909 | ||
910 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
911 | ||
912 | r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0); | |
913 | if (r < 0) { | |
914 | log_debug_errno(r, "Failed to wait for child: %m"); | |
915 | goto finish; | |
916 | } | |
917 | if (r != EXIT_SUCCESS) { | |
918 | if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r)) | |
919 | log_debug_errno(r, "Failed to mount: %m"); | |
920 | else | |
921 | log_debug("Child failed."); | |
922 | goto finish; | |
923 | } | |
924 | ||
925 | finish: | |
926 | if (mount_outside_mounted) | |
927 | (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW); | |
928 | if (mount_outside_created) { | |
70599967 | 929 | if (is_image || S_ISDIR(st.st_mode)) |
6af52c3a LB |
930 | (void) rmdir(mount_outside); |
931 | else | |
932 | (void) unlink(mount_outside); | |
933 | } | |
934 | ||
935 | if (mount_tmp_mounted) | |
936 | (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW); | |
937 | if (mount_tmp_created) { | |
70599967 | 938 | if (is_image || S_ISDIR(st.st_mode)) |
6af52c3a LB |
939 | (void) rmdir(mount_tmp); |
940 | else | |
941 | (void) unlink(mount_tmp); | |
942 | } | |
943 | ||
944 | if (mount_slave_mounted) | |
945 | (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW); | |
946 | if (mount_slave_created) | |
947 | (void) rmdir(mount_slave); | |
948 | ||
949 | return r; | |
950 | } | |
70599967 LB |
951 | |
952 | int bind_mount_in_namespace( | |
953 | pid_t target, | |
954 | const char *propagate_path, | |
955 | const char *incoming_path, | |
956 | const char *src, | |
957 | const char *dest, | |
958 | bool read_only, | |
959 | bool make_file_or_directory) { | |
960 | ||
961 | return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, NULL, false); | |
962 | } | |
963 | ||
964 | int mount_image_in_namespace( | |
965 | pid_t target, | |
966 | const char *propagate_path, | |
967 | const char *incoming_path, | |
968 | const char *src, | |
969 | const char *dest, | |
970 | bool read_only, | |
971 | bool make_file_or_directory, | |
972 | const MountOptions *options) { | |
973 | ||
974 | return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, options, true); | |
975 | } | |
14a25e1f LP |
976 | |
977 | int make_mount_point(const char *path) { | |
978 | int r; | |
979 | ||
980 | assert(path); | |
981 | ||
982 | /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */ | |
983 | ||
984 | r = path_is_mount_point(path, NULL, 0); | |
985 | if (r < 0) | |
986 | return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path); | |
987 | if (r > 0) | |
988 | return 0; | |
989 | ||
990 | r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL); | |
991 | if (r < 0) | |
992 | return r; | |
993 | ||
994 | return 1; | |
995 | } | |
35fd3558 LP |
996 | |
997 | static int make_userns(uid_t uid_shift, uid_t uid_range) { | |
998 | char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1], line[DECIMAL_STR_MAX(uid_t)*3+3+1]; | |
999 | _cleanup_(sigkill_waitp) pid_t pid = 0; | |
1000 | _cleanup_close_ int userns_fd = -1; | |
1001 | int r; | |
1002 | ||
1003 | /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child | |
1004 | * process whose only purpose is to give us a new user namespace. It's killed when we got it. */ | |
1005 | ||
1006 | r = safe_fork("(sd-mkuserns)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NEW_USERNS, &pid); | |
1007 | if (r < 0) | |
1008 | return r; | |
45a68ed3 | 1009 | if (r == 0) |
35fd3558 LP |
1010 | /* Child. We do nothing here, just freeze until somebody kills us. */ |
1011 | freeze(); | |
35fd3558 LP |
1012 | |
1013 | xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, uid_shift, uid_range); | |
1014 | ||
1015 | xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid); | |
1016 | r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER); | |
1017 | if (r < 0) | |
1018 | return log_error_errno(r, "Failed to write UID map: %m"); | |
1019 | ||
1020 | /* We always assign the same UID and GID ranges */ | |
1021 | xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid); | |
1022 | r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER); | |
1023 | if (r < 0) | |
1024 | return log_error_errno(r, "Failed to write GID map: %m"); | |
1025 | ||
1026 | r = namespace_open(pid, NULL, NULL, NULL, &userns_fd, NULL); | |
1027 | if (r < 0) | |
1028 | return r; | |
1029 | ||
1030 | return TAKE_FD(userns_fd); | |
1031 | } | |
1032 | ||
1033 | int remount_idmap( | |
1034 | const char *p, | |
1035 | uid_t uid_shift, | |
1036 | uid_t uid_range) { | |
1037 | ||
1038 | _cleanup_close_ int mount_fd = -1, userns_fd = -1; | |
1039 | int r; | |
1040 | ||
1041 | assert(p); | |
1042 | ||
1043 | if (!userns_shift_range_valid(uid_shift, uid_range)) | |
1044 | return -EINVAL; | |
1045 | ||
1046 | /* Clone the mount point */ | |
1047 | mount_fd = open_tree(-1, p, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC); | |
1048 | if (mount_fd < 0) | |
1049 | return log_debug_errno(errno, "Failed to open tree of mounted filesystem '%s': %m", p); | |
1050 | ||
1051 | /* Create a user namespace mapping */ | |
1052 | userns_fd = make_userns(uid_shift, uid_range); | |
1053 | if (userns_fd < 0) | |
1054 | return userns_fd; | |
1055 | ||
1056 | /* Set the user namespace mapping attribute on the cloned mount point */ | |
1057 | if (mount_setattr(mount_fd, "", AT_EMPTY_PATH | AT_RECURSIVE, | |
1058 | &(struct mount_attr) { | |
1059 | .attr_set = MOUNT_ATTR_IDMAP, | |
1060 | .userns_fd = userns_fd, | |
1061 | }, sizeof(struct mount_attr)) < 0) | |
1062 | return log_debug_errno(errno, "Failed to change bind mount attributes for '%s': %m", p); | |
1063 | ||
1064 | /* Remove the old mount point */ | |
1065 | r = umount_verbose(LOG_DEBUG, p, UMOUNT_NOFOLLOW); | |
1066 | if (r < 0) | |
1067 | return r; | |
1068 | ||
1069 | /* And place the cloned version in its place */ | |
1070 | if (move_mount(mount_fd, "", -1, p, MOVE_MOUNT_F_EMPTY_PATH) < 0) | |
1071 | return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", p); | |
1072 | ||
1073 | return 0; | |
1074 | } | |
9c653536 ZJS |
1075 | |
1076 | int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode) { | |
1077 | assert(st); | |
1078 | assert(dest); | |
1079 | ||
1080 | if (S_ISDIR(st->st_mode)) | |
1081 | return mkdir_label(dest, mode); | |
1082 | else | |
1083 | return mknod(dest, S_IFREG|(mode & ~0111), 0); | |
1084 | } | |
1085 | ||
1086 | int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode) { | |
1087 | struct stat st; | |
1088 | ||
1089 | assert(source); | |
1090 | assert(dest); | |
1091 | ||
1092 | if (stat(source, &st) < 0) | |
1093 | return -errno; | |
1094 | ||
1095 | return make_mount_point_inode_from_stat(&st, dest, mode); | |
1096 | } |