Debian Codegraph @

src/test/test-mount-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4 #include <sys/stat.h>
5 #include <sys/statvfs.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "capability-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "libmount-util.h"
15 #include "mkdir.h"
16 #include "mount-util.h"
17 #include "mountpoint-util.h"
18 #include "path-util.h"
19 #include "process-util.h"
20 #include "random-util.h"
21 #include "rm-rf.h"
22 #include "socket-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "tests.h"
26 #include "tmpfile-util.h"
27 #include "virt.h"
28
29 #define FORK_COMMON_FLAGS \
30 (FORK_CLOSE_ALL_FDS | \
31 FORK_RESET_SIGNALS | \
32 FORK_DEATHSIG_SIGTERM | \
33 FORK_LOG | \
34 FORK_REOPEN_LOG | \
35 FORK_WAIT | \
36 FORK_NEW_MOUNTNS | \
37 FORK_MOUNTNS_SLAVE)
38
39 #define CHECK_PRIV \
40 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) \
41 return (void) log_tests_skipped("Not privileged"); \
42 if (running_in_chroot() != 0) \
43 return (void) log_tests_skipped("running in chroot");
44
45 TEST(mount_option_mangle) {
46 char *opts = NULL;
47 unsigned long f;
48
49 assert_se(mount_option_mangle(NULL, MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
50 assert_se(f == (MS_RDONLY|MS_NOSUID));
51 ASSERT_NULL(opts);
52
53 assert_se(mount_option_mangle("", MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
54 assert_se(f == (MS_RDONLY|MS_NOSUID));
55 ASSERT_NULL(opts);
56
57 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec", 0, &f, &opts) == 0);
58 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
59 ASSERT_NULL(opts);
60
61 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec,mode=0755", 0, &f, &opts) == 0);
62 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
63 ASSERT_STREQ(opts, "mode=0755");
64 opts = mfree(opts);
65
66 assert_se(mount_option_mangle("rw,nosuid,foo,hogehoge,nodev,mode=0755", 0, &f, &opts) == 0);
67 assert_se(f == (MS_NOSUID|MS_NODEV));
68 ASSERT_STREQ(opts, "foo,hogehoge,mode=0755");
69 opts = mfree(opts);
70
71 assert_se(mount_option_mangle("rw,nosuid,nodev,noexec,relatime,net_cls,net_prio", MS_RDONLY, &f, &opts) == 0);
72 assert_se(f == (MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME));
73 ASSERT_STREQ(opts, "net_cls,net_prio");
74 opts = mfree(opts);
75
76 assert_se(mount_option_mangle("rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000", MS_RDONLY, &f, &opts) == 0);
77 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
78 ASSERT_STREQ(opts, "size=1630748k,mode=0700,uid=1000,gid=1000");
79 opts = mfree(opts);
80
81 assert_se(mount_option_mangle("size=1630748k,rw,gid=1000,,,nodev,relatime,,mode=0700,nosuid,uid=1000", MS_RDONLY, &f, &opts) == 0);
82 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
83 ASSERT_STREQ(opts, "size=1630748k,gid=1000,mode=0700,uid=1000");
84 opts = mfree(opts);
85
86 assert_se(mount_option_mangle("rw,exec,size=8143984k,nr_inodes=2035996,mode=0755", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, &f, &opts) == 0);
87 assert_se(f == (MS_NOSUID|MS_NODEV));
88 ASSERT_STREQ(opts, "size=8143984k,nr_inodes=2035996,mode=0755");
89 opts = mfree(opts);
90
91 assert_se(mount_option_mangle("rw,relatime,fmask=0022,,,dmask=0022", MS_RDONLY, &f, &opts) == 0);
92 assert_se(f == MS_RELATIME);
93 ASSERT_STREQ(opts, "fmask=0022,dmask=0022");
94 opts = mfree(opts);
95
96 assert_se(mount_option_mangle("rw,relatime,fmask=0022,dmask=0022,\"hogehoge", MS_RDONLY, &f, &opts) < 0);
97
98 assert_se(mount_option_mangle("mode=01777,size=10%,nr_inodes=400k,uid=496107520,gid=496107520,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c0,c1\"", 0, &f, &opts) == 0);
99 assert_se(f == 0);
100 ASSERT_STREQ(opts, "mode=01777,size=10%,nr_inodes=400k,uid=496107520,gid=496107520,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c0,c1\"");
101 opts = mfree(opts);
102 }
103
104 static void test_mount_flags_to_string_one(unsigned long flags, const char *expected) {
105 _cleanup_free_ char *x = NULL;
106 int r;
107
108 r = mount_flags_to_string(flags, &x);
109 log_info("flags: %#lX → %d/\"%s\"", flags, r, strnull(x));
110 assert_se(r >= 0);
111 ASSERT_STREQ(x, expected);
112 }
113
114 TEST(mount_flags_to_string) {
115 test_mount_flags_to_string_one(0, "0");
116 test_mount_flags_to_string_one(MS_RDONLY, "MS_RDONLY");
117 test_mount_flags_to_string_one(MS_NOSUID, "MS_NOSUID");
118 test_mount_flags_to_string_one(MS_NODEV, "MS_NODEV");
119 test_mount_flags_to_string_one(MS_NOEXEC, "MS_NOEXEC");
120 test_mount_flags_to_string_one(MS_SYNCHRONOUS, "MS_SYNCHRONOUS");
121 test_mount_flags_to_string_one(MS_REMOUNT, "MS_REMOUNT");
122 test_mount_flags_to_string_one(MS_MANDLOCK, "MS_MANDLOCK");
123 test_mount_flags_to_string_one(MS_DIRSYNC, "MS_DIRSYNC");
124 test_mount_flags_to_string_one(MS_NOSYMFOLLOW, "MS_NOSYMFOLLOW");
125 test_mount_flags_to_string_one(MS_NOATIME, "MS_NOATIME");
126 test_mount_flags_to_string_one(MS_NODIRATIME, "MS_NODIRATIME");
127 test_mount_flags_to_string_one(MS_BIND, "MS_BIND");
128 test_mount_flags_to_string_one(MS_MOVE, "MS_MOVE");
129 test_mount_flags_to_string_one(MS_REC, "MS_REC");
130 test_mount_flags_to_string_one(MS_SILENT, "MS_SILENT");
131 test_mount_flags_to_string_one(MS_POSIXACL, "MS_POSIXACL");
132 test_mount_flags_to_string_one(MS_UNBINDABLE, "MS_UNBINDABLE");
133 test_mount_flags_to_string_one(MS_PRIVATE, "MS_PRIVATE");
134 test_mount_flags_to_string_one(MS_SLAVE, "MS_SLAVE");
135 test_mount_flags_to_string_one(MS_SHARED, "MS_SHARED");
136 test_mount_flags_to_string_one(MS_RELATIME, "MS_RELATIME");
137 test_mount_flags_to_string_one(MS_KERNMOUNT, "MS_KERNMOUNT");
138 test_mount_flags_to_string_one(MS_I_VERSION, "MS_I_VERSION");
139 test_mount_flags_to_string_one(MS_STRICTATIME, "MS_STRICTATIME");
140 test_mount_flags_to_string_one(MS_LAZYTIME, "MS_LAZYTIME");
141 test_mount_flags_to_string_one(MS_LAZYTIME|MS_STRICTATIME, "MS_STRICTATIME|MS_LAZYTIME");
142 test_mount_flags_to_string_one(UINT_MAX,
143 "MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS|MS_REMOUNT|"
144 "MS_MANDLOCK|MS_DIRSYNC|MS_NOSYMFOLLOW|MS_NOATIME|MS_NODIRATIME|"
145 "MS_BIND|MS_MOVE|MS_REC|MS_SILENT|MS_POSIXACL|MS_UNBINDABLE|"
146 "MS_PRIVATE|MS_SLAVE|MS_SHARED|MS_RELATIME|MS_KERNMOUNT|"
147 "MS_I_VERSION|MS_STRICTATIME|MS_LAZYTIME|fc000200");
148 }
149
150 TEST(bind_remount_recursive) {
151 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
152 _cleanup_free_ char *subdir = NULL;
153 int r;
154
155 CHECK_PRIV;
156
157 assert_se(mkdtemp_malloc("/tmp/XXXXXX", &tmp) >= 0);
158 subdir = path_join(tmp, "subdir");
159 assert_se(subdir);
160 assert_se(mkdir(subdir, 0755) >= 0);
161
162 FOREACH_STRING(p, "/usr", "/sys", "/", tmp) {
163 r = ASSERT_OK(pidref_safe_fork("(bind-remount-recursive)", FORK_COMMON_FLAGS, NULL));
164 if (r == 0) { /* child */
165 struct statvfs svfs;
166
167 /* Check that the subdir is writable (it must be because it's in /tmp) */
168 assert_se(statvfs(subdir, &svfs) >= 0);
169 assert_se(!FLAGS_SET(svfs.f_flag, ST_RDONLY));
170
171 /* Make the subdir a bind mount */
172 assert_se(mount_nofollow(subdir, subdir, NULL, MS_BIND|MS_REC, NULL) >= 0);
173
174 /* Ensure it's still writable */
175 assert_se(statvfs(subdir, &svfs) >= 0);
176 assert_se(!FLAGS_SET(svfs.f_flag, ST_RDONLY));
177
178 /* Now mark the path we currently run for read-only */
179 assert_se(bind_remount_recursive(p, MS_RDONLY, MS_RDONLY, path_equal(p, "/sys") ? STRV_MAKE("/sys/kernel") : NULL) >= 0);
180
181 /* Ensure that this worked on the top-level */
182 assert_se(statvfs(p, &svfs) >= 0);
183 assert_se(FLAGS_SET(svfs.f_flag, ST_RDONLY));
184
185 /* And ensure this had an effect on the subdir exactly if we are talking about a path above the subdir */
186 assert_se(statvfs(subdir, &svfs) >= 0);
187 assert_se(FLAGS_SET(svfs.f_flag, ST_RDONLY) == !!path_startswith(subdir, p));
188
189 _exit(EXIT_SUCCESS);
190 }
191 }
192 }
193
194 TEST(bind_remount_one) {
195 int r;
196
197 CHECK_PRIV;
198
199 r = ASSERT_OK(pidref_safe_fork("(remount-one-with-mountinfo)", FORK_COMMON_FLAGS, NULL));
200 if (r == 0) { /* child */
201 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
202
203 assert_se(fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo) >= 0);
204
205 assert_se(bind_remount_one_with_mountinfo("/run", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) >= 0);
206 assert_se(bind_remount_one_with_mountinfo("/run", MS_NOEXEC, MS_RDONLY|MS_NOEXEC, proc_self_mountinfo) >= 0);
207 assert_se(bind_remount_one_with_mountinfo("/proc/idontexist", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) == -ENOENT);
208 assert_se(bind_remount_one_with_mountinfo("/proc/self", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) == -EINVAL);
209 assert_se(bind_remount_one_with_mountinfo("/", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) >= 0);
210
211 _exit(EXIT_SUCCESS);
212 }
213
214 r = ASSERT_OK(pidref_safe_fork("(remount-one)", FORK_COMMON_FLAGS, NULL));
215 if (r == 0) { /* child */
216 assert_se(bind_remount_one("/run", MS_RDONLY, MS_RDONLY) >= 0);
217 assert_se(bind_remount_one("/run", MS_NOEXEC, MS_RDONLY|MS_NOEXEC) >= 0);
218 assert_se(bind_remount_one("/proc/idontexist", MS_RDONLY, MS_RDONLY) == -ENOENT);
219 assert_se(bind_remount_one("/proc/self", MS_RDONLY, MS_RDONLY) == -EINVAL);
220 assert_se(bind_remount_one("/", MS_RDONLY, MS_RDONLY) >= 0);
221
222 _exit(EXIT_SUCCESS);
223 }
224 }
225
226 TEST(make_mount_point_inode) {
227 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
228 const char *src_file, *src_dir, *dst_file, *dst_dir;
229 struct stat st;
230
231 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
232
233 src_file = strjoina(d, "/src/file");
234 src_dir = strjoina(d, "/src/dir");
235 dst_file = strjoina(d, "/dst/file");
236 dst_dir = strjoina(d, "/dst/dir");
237
238 assert_se(mkdir_p(src_dir, 0755) >= 0);
239 assert_se(mkdir_parents(dst_file, 0755) >= 0);
240 assert_se(touch(src_file) >= 0);
241
242 assert_se(make_mount_point_inode_from_path(src_file, dst_file, 0755) >= 0);
243 assert_se(make_mount_point_inode_from_path(src_dir, dst_dir, 0755) >= 0);
244
245 assert_se(stat(dst_dir, &st) == 0);
246 assert_se(S_ISDIR(st.st_mode));
247 assert_se(stat(dst_file, &st) == 0);
248 assert_se(S_ISREG(st.st_mode));
249 assert_se(!(S_IXUSR & st.st_mode));
250 assert_se(!(S_IXGRP & st.st_mode));
251 assert_se(!(S_IXOTH & st.st_mode));
252
253 assert_se(unlink(dst_file) == 0);
254 assert_se(rmdir(dst_dir) == 0);
255
256 assert_se(stat(src_file, &st) == 0);
257 assert_se(make_mount_point_inode_from_mode(AT_FDCWD, dst_file, st.st_mode, 0755) >= 0);
258 assert_se(stat(src_dir, &st) == 0);
259 assert_se(make_mount_point_inode_from_mode(AT_FDCWD, dst_dir, st.st_mode, 0755) >= 0);
260
261 assert_se(stat(dst_dir, &st) == 0);
262 assert_se(S_ISDIR(st.st_mode));
263 assert_se(stat(dst_file, &st) == 0);
264 assert_se(S_ISREG(st.st_mode));
265 assert_se(!(S_IXUSR & st.st_mode));
266 assert_se(!(S_IXGRP & st.st_mode));
267 assert_se(!(S_IXOTH & st.st_mode));
268 }
269
270 TEST(make_mount_switch_root) {
271 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
272 _cleanup_free_ char *s = NULL;
273 int r;
274
275 CHECK_PRIV;
276
277 assert_se(mkdtemp_malloc(NULL, &t) >= 0);
278
279 assert_se(asprintf(&s, "%s/somerandomname%" PRIu64, t, random_u64()) >= 0);
280 assert_se(s);
281 assert_se(touch(s) >= 0);
282
283 struct {
284 const char *path;
285 bool force_ms_move;
286 } table[] = {
287 { t, false },
288 { t, true },
289 { "/", false },
290 { "/", true },
291 };
292
293 FOREACH_ELEMENT(i, table) {
294 r = ASSERT_OK(pidref_safe_fork("(switch-root)", FORK_COMMON_FLAGS, NULL));
295 if (r == 0) {
296 assert_se(make_mount_point(i->path) >= 0);
297 assert_se(mount_switch_root_full(i->path, /* mount_propagation_flag= */ 0, i->force_ms_move) >= 0);
298
299 if (!path_equal(i->path, "/")) {
300 assert_se(access(ASSERT_PTR(strrchr(s, '/')), F_OK) >= 0); /* absolute */
301 assert_se(access(ASSERT_PTR(strrchr(s, '/')) + 1, F_OK) >= 0); /* relative */
302 assert_se(access(s, F_OK) < 0 && errno == ENOENT); /* doesn't exist in our new environment */
303 }
304
305 _exit(EXIT_SUCCESS);
306 }
307 }
308 }
309
310 TEST(umount_recursive) {
311 static const struct {
312 const char *prefix;
313 const char * const keep[3];
314 } test_table[] = {
315 {
316 .prefix = NULL,
317 .keep = {},
318 },
319 {
320 .prefix = "/run",
321 .keep = {},
322 },
323 {
324 .prefix = NULL,
325 .keep = { "/dev/shm", NULL },
326 },
327 {
328 .prefix = "/dev",
329 .keep = { "/dev/pts", "/dev/shm", NULL },
330 },
331 };
332
333 int r;
334
335 CHECK_PRIV;
336
337 FOREACH_ELEMENT(t, test_table) {
338 r = ASSERT_OK(pidref_safe_fork("(umount-rec)", FORK_COMMON_FLAGS, NULL));
339 if (r == 0) { /* child */
340 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
341 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
342 _cleanup_fclose_ FILE *f = NULL;
343 _cleanup_free_ char *k = NULL;
344
345 /* Open /p/s/m file before we unmount everything (which might include /proc/) */
346 f = fopen("/proc/self/mountinfo", "re");
347 if (!f) {
348 log_error_errno(errno, "Failed to open %s: %m", "/proc/self/mountinfo");
349 _exit(EXIT_FAILURE);
350 }
351
352 assert_se(k = strv_join((char**) t->keep, " "));
353 log_info("detaching just %s (keep: %s)", strna(t->prefix), strna(empty_to_null(k)));
354
355 assert_se(umount_recursive_full(t->prefix, MNT_DETACH, (char**) t->keep) >= 0);
356
357 r = libmount_parse_mountinfo(f, &table, &iter);
358 if (r < 0) {
359 log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
360 _exit(EXIT_FAILURE);
361 }
362
363 for (;;) {
364 struct libmnt_fs *fs;
365
366 r = sym_mnt_table_next_fs(table, iter, &fs);
367 if (r == 1)
368 break;
369 if (r < 0) {
370 log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
371 _exit(EXIT_FAILURE);
372 }
373
374 log_debug("left after complete umount: %s", sym_mnt_fs_get_target(fs));
375 }
376
377 _exit(EXIT_SUCCESS);
378 }
379 }
380 }
381
382 TEST(fd_make_mount_point) {
383 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
384 _cleanup_free_ char *s = NULL;
385 int r;
386
387 CHECK_PRIV;
388
389 assert_se(mkdtemp_malloc(NULL, &t) >= 0);
390
391 assert_se(asprintf(&s, "%s/somerandomname%" PRIu64, t, random_u64()) >= 0);
392 assert_se(s);
393 assert_se(mkdir(s, 0700) >= 0);
394
395 r = ASSERT_OK(pidref_safe_fork("(make-mount-point)", FORK_COMMON_FLAGS, NULL));
396 if (r == 0) {
397 _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
398
399 fd = open(s, O_PATH|O_CLOEXEC);
400 assert_se(fd >= 0);
401
402 assert_se(is_mount_point_at(fd, NULL, AT_SYMLINK_FOLLOW) == 0);
403
404 assert_se(fd_make_mount_point(fd) > 0);
405
406 /* Reopen the inode so that we end up on the new mount */
407 fd2 = open(s, O_PATH|O_CLOEXEC);
408
409 assert_se(is_mount_point_at(fd2, NULL, AT_SYMLINK_FOLLOW) > 0);
410
411 assert_se(fd_make_mount_point(fd2) == 0);
412
413 _exit(EXIT_SUCCESS);
414 }
415 }
416
417 TEST(bind_mount_submounts) {
418 _cleanup_(rmdir_and_freep) char *a = NULL, *b = NULL;
419 int r;
420
421 CHECK_PRIV;
422
423 assert_se(mkdtemp_malloc(NULL, &a) >= 0);
424 assert_se(mkdtemp_malloc(NULL, &b) >= 0);
425
426 r = ASSERT_OK(pidref_safe_fork("(bind-mount-submounts)", FORK_COMMON_FLAGS, NULL));
427 if (r == 0) {
428 char *x;
429
430 ASSERT_OK(mount_nofollow_verbose(LOG_INFO, "tmpfs", a, "tmpfs", 0, NULL));
431
432 assert_se(x = path_join(a, "foo"));
433 assert_se(touch(x) >= 0);
434 free(x);
435
436 assert_se(x = path_join(a, "x"));
437 assert_se(mkdir(x, 0755) >= 0);
438 assert_se(mount_nofollow_verbose(LOG_INFO, "tmpfs", x, "tmpfs", 0, NULL) >= 0);
439 free(x);
440
441 assert_se(x = path_join(a, "x/xx"));
442 assert_se(touch(x) >= 0);
443 free(x);
444
445 assert_se(x = path_join(a, "y"));
446 assert_se(mkdir(x, 0755) >= 0);
447 assert_se(mount_nofollow_verbose(LOG_INFO, "tmpfs", x, "tmpfs", 0, NULL) >= 0);
448 free(x);
449
450 assert_se(x = path_join(a, "y/yy"));
451 assert_se(touch(x) >= 0);
452 free(x);
453
454 assert_se(mount_nofollow_verbose(LOG_INFO, "tmpfs", b, "tmpfs", 0, NULL) >= 0);
455
456 assert_se(x = path_join(b, "x"));
457 assert_se(mkdir(x, 0755) >= 0);
458 free(x);
459
460 assert_se(x = path_join(b, "y"));
461 assert_se(mkdir(x, 0755) >= 0);
462 free(x);
463
464 assert_se(bind_mount_submounts(a, b) >= 0);
465
466 assert_se(x = path_join(b, "foo"));
467 assert_se(access(x, F_OK) < 0 && errno == ENOENT);
468 free(x);
469
470 assert_se(x = path_join(b, "x/xx"));
471 assert_se(access(x, F_OK) >= 0);
472 free(x);
473
474 assert_se(x = path_join(b, "y/yy"));
475 assert_se(access(x, F_OK) >= 0);
476 free(x);
477
478 assert_se(x = path_join(b, "x"));
479 assert_se(path_is_mount_point(x) > 0);
480 free(x);
481
482 assert_se(x = path_join(b, "y"));
483 assert_se(path_is_mount_point(x) > 0);
484 free(x);
485
486 assert_se(umount_recursive(a, 0) >= 0);
487 assert_se(umount_recursive(b, 0) >= 0);
488
489 _exit(EXIT_SUCCESS);
490 }
491 }
492
493 TEST(path_is_network_fs_harder) {
494 _cleanup_close_ int dir_fd = -EBADF;
495 int r;
496
497 ASSERT_OK(dir_fd = open("/", O_PATH | O_CLOEXEC));
498 FOREACH_STRING(s,
499 "/", "/dev/", "/proc/", "/run/", "/sys/", "/tmp/", "/usr/", "/var/tmp/",
500 "", ".", "../../../", "/this/path/should/not/exist/for/test-mount-util/") {
501
502 r = path_is_network_fs_harder(s);
503 log_debug("path_is_network_fs_harder(%s) → %i: %s", s, r, r < 0 ? STRERROR(r) : yes_no(r));
504
505 const char *q = path_startswith(s, "/") ?: s;
506 r = path_is_network_fs_harder_at(dir_fd, q);
507 log_debug("path_is_network_fs_harder_at(root, %s) → %i: %s", q, r, r < 0 ? STRERROR(r) : yes_no(r));
508 }
509
510 CHECK_PRIV;
511
512 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
513 assert_se(mkdtemp_malloc("/tmp/test-mount-util.path_is_network_fs_harder.XXXXXXX", &t) >= 0);
514
515 r = ASSERT_OK(pidref_safe_fork("(path-is-network-fs-harder)", FORK_COMMON_FLAGS, NULL));
516 if (r == 0) {
517 ASSERT_OK(mount_nofollow_verbose(LOG_INFO, "tmpfs", t, "tmpfs", 0, NULL));
518 ASSERT_OK_ZERO(path_is_network_fs_harder(t));
519 ASSERT_OK_ERRNO(umount(t));
520
521 ASSERT_OK(mount_nofollow_verbose(LOG_INFO, "tmpfs", t, "tmpfs", 0, "x-systemd-growfs,x-systemd-automount"));
522 ASSERT_OK_ZERO(path_is_network_fs_harder(t));
523 ASSERT_OK_ERRNO(umount(t));
524
525 _exit(EXIT_SUCCESS);
526 }
527 }
528
529 TEST(umountat) {
530 CHECK_PRIV;
531
532 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
533 _cleanup_close_ int dfd = mkdtemp_open(NULL, O_CLOEXEC, &p);
534 ASSERT_OK(dfd);
535
536 ASSERT_OK(mkdirat(dfd, "foo", 0777));
537
538 _cleanup_free_ char *q = ASSERT_PTR(path_join(p, "foo"));
539
540 ASSERT_OK(mount_nofollow_verbose(LOG_ERR, "tmpfs", q, "tmpfs", 0, NULL));
541 ASSERT_OK(umountat_detach_verbose(LOG_ERR, dfd, "foo"));
542 ASSERT_ERROR(umountat_detach_verbose(LOG_ERR, dfd, "foo"), EINVAL);
543 }
544
545 TEST(mount_fd_clone) {
546 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
547 _cleanup_close_pair_ int fds[2] = EBADF_PAIR;
548 int r;
549
550 CHECK_PRIV;
551
552 ASSERT_OK(mkdtemp_malloc(NULL, &t));
553
554 /* Set up a socket pair to transfer the mount fd from the child (in a different mountns) to us. */
555 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, fds));
556
557 r = ASSERT_OK(pidref_safe_fork_full(
558 "(mount-fd-clone-setup)",
559 /* stdio_fds= */ NULL,
560 &fds[1], 1,
561 FORK_COMMON_FLAGS,
562 NULL));
563 if (r == 0) {
564 /* Create a tmpfs mount in this child's mountns. */
565 ASSERT_OK(mount_nofollow_verbose(LOG_ERR, "tmpfs", t, "tmpfs", 0, NULL));
566
567 /* Create a file in it to verify the mount later. */
568 _cleanup_free_ char *marker = ASSERT_NOT_NULL(path_join(t, "marker"));
569 ASSERT_OK(touch(marker));
570
571 /* Clone the mount as a detached mount fd. */
572 _cleanup_close_ int mount_fd = ASSERT_OK_ERRNO(open_tree(AT_FDCWD, t, OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC));
573
574 /* Send the mount fd to the parent. */
575 ASSERT_OK(send_one_fd(fds[1], mount_fd, 0));
576
577 _exit(EXIT_SUCCESS);
578 }
579
580 fds[1] = safe_close(fds[1]);
581
582 /* Parent: Receive the mount fd, clone it with mount_fd_clone(), and verify we can attach it. */
583 _cleanup_close_ int foreign_mount_fd = ASSERT_OK(receive_one_fd(fds[0], 0));
584 _cleanup_close_ int first_clone = ASSERT_OK(
585 mount_fd_clone(foreign_mount_fd, /* recursive= */ true, &foreign_mount_fd));
586 _cleanup_close_ _unused_ int second_clone = ASSERT_OK(
587 mount_fd_clone(foreign_mount_fd, /* recursive= */ true, /* replacement_fd= */ NULL));
588 _cleanup_free_ char *target = ASSERT_NOT_NULL(path_join(t, "target"));
589 ASSERT_OK_ERRNO(mkdir(target, 0755));
590
591 r = ASSERT_OK(pidref_safe_fork_full(
592 "(mount-fd-clone-verify)",
593 /* stdio_fds= */ NULL,
594 &first_clone, 1,
595 FORK_COMMON_FLAGS,
596 NULL));
597 if (r == 0) {
598 ASSERT_OK_ERRNO(move_mount(first_clone, "", AT_FDCWD, target, MOVE_MOUNT_F_EMPTY_PATH));
599
600 _cleanup_free_ char *marker = ASSERT_NOT_NULL(path_join(target, "marker"));
601 ASSERT_OK_ERRNO(access(marker, F_OK));
602
603 _exit(EXIT_SUCCESS);
604 }
605 }
606
607 DEFINE_TEST_MAIN(LOG_DEBUG);