]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mountpoint-util.c
ceb59ce4555e5250e14df93de43c30cc7f9de34e
[thirdparty/systemd.git] / src / test / test-mountpoint-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <stdlib.h>
5 #include <sys/mount.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "hashmap.h"
13 #include "log.h"
14 #include "mountpoint-util.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "stat-util.h"
18 #include "string-util.h"
19 #include "tests.h"
20 #include "tmpfile-util.h"
21
22 static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
23 unsigned long flags;
24
25 log_info("/* %s(%s) */", __func__, strnull(name));
26
27 assert_se(mount_propagation_flag_from_string(name, &flags) == ret);
28
29 if (ret >= 0) {
30 const char *c;
31
32 assert_se(flags == expected);
33
34 c = mount_propagation_flag_to_string(flags);
35 if (isempty(name))
36 assert_se(isempty(c));
37 else
38 ASSERT_STREQ(c, name);
39 }
40 }
41
42 TEST(mount_propagation_flag) {
43 test_mount_propagation_flag_one("shared", 0, MS_SHARED);
44 test_mount_propagation_flag_one("slave", 0, MS_SLAVE);
45 test_mount_propagation_flag_one("private", 0, MS_PRIVATE);
46 test_mount_propagation_flag_one(NULL, 0, 0);
47 test_mount_propagation_flag_one("", 0, 0);
48 test_mount_propagation_flag_one("xxxx", -EINVAL, 0);
49 test_mount_propagation_flag_one(" ", -EINVAL, 0);
50 }
51
52 TEST(mnt_id) {
53 _cleanup_fclose_ FILE *f = NULL;
54 _cleanup_hashmap_free_ Hashmap *h = NULL;
55 char *p;
56 void *k;
57 int r;
58
59 assert_se(f = fopen("/proc/self/mountinfo", "re"));
60 assert_se(h = hashmap_new(&trivial_hash_ops_value_free));
61
62 for (;;) {
63 _cleanup_free_ char *line = NULL, *path = NULL;
64 int mnt_id;
65
66 r = read_line(f, LONG_LINE_MAX, &line);
67 if (r == 0)
68 break;
69 assert_se(r > 0);
70
71 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
72 #if HAS_FEATURE_MEMORY_SANITIZER
73 /* We don't know the length of the string, so we need to unpoison it one char at a time */
74 for (const char *c = path; ; c++) {
75 msan_unpoison(c, 1);
76 if (!*c)
77 break;
78 }
79 #endif
80 log_debug("mountinfo: %s → %i", path, mnt_id);
81
82 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
83 path = NULL;
84 }
85
86 HASHMAP_FOREACH_KEY(p, k, h) {
87 int mnt_id = PTR_TO_INT(k), mnt_id2;
88 const char *q;
89
90 r = path_get_mnt_id(p, &mnt_id2);
91 if (r < 0) {
92 log_debug_errno(r, "Failed to get the mnt id of %s: %m", p);
93 continue;
94 }
95
96 if (mnt_id == mnt_id2) {
97 log_debug("mnt ids of %s is %i.", p, mnt_id);
98 continue;
99 } else
100 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p, mnt_id, mnt_id2);
101
102 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
103 * See #11505. */
104 assert_se(q = hashmap_get(h, INT_TO_PTR(mnt_id2)));
105
106 assert_se((r = path_is_mount_point_full(p, NULL, 0)) >= 0);
107 if (r == 0) {
108 /* If the path is not a mount point anymore, then it must be a sub directory of
109 * the path corresponds to mnt_id2. */
110 log_debug("The path %s for mnt id %i is not a mount point.", p, mnt_id2);
111 assert_se(!isempty(path_startswith(p, q)));
112 } else {
113 /* If the path is still a mount point, then it must be equivalent to the path
114 * corresponds to mnt_id2 */
115 log_debug("There are multiple mounts on the same path %s.", p);
116 assert_se(path_equal(p, q));
117 }
118 }
119 }
120
121 TEST(path_is_mount_point) {
122 int fd;
123 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
124 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
125 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
126 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
127
128 assert_se(path_is_mount_point_full("/", NULL, AT_SYMLINK_FOLLOW) > 0);
129 assert_se(path_is_mount_point_full("/", NULL, 0) > 0);
130 assert_se(path_is_mount_point_full("//", NULL, AT_SYMLINK_FOLLOW) > 0);
131 assert_se(path_is_mount_point_full("//", NULL, 0) > 0);
132
133 assert_se(path_is_mount_point_full("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
134 assert_se(path_is_mount_point_full("/proc", NULL, 0) > 0);
135 assert_se(path_is_mount_point_full("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
136 assert_se(path_is_mount_point_full("/proc/", NULL, 0) > 0);
137
138 assert_se(path_is_mount_point_full("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
139 assert_se(path_is_mount_point_full("/proc/1", NULL, 0) == 0);
140 assert_se(path_is_mount_point_full("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
141 assert_se(path_is_mount_point_full("/proc/1/", NULL, 0) == 0);
142
143 /* we'll create a hierarchy of different kinds of dir/file/link
144 * layouts:
145 *
146 * <tmp>/file1, <tmp>/file2
147 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
148 * <tmp>/dir1/
149 * <tmp>/dir1/file
150 * <tmp>/dirlink1 -> dir1
151 * <tmp>/dirlink1file -> dirlink1/file
152 * <tmp>/dir2/
153 * <tmp>/dir2/file
154 */
155
156 /* file mountpoints */
157 ASSERT_NOT_NULL(mkdtemp(tmp_dir));
158 file1 = path_join(tmp_dir, "file1");
159 assert_se(file1);
160 file2 = path_join(tmp_dir, "file2");
161 assert_se(file2);
162 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
163 assert_se(fd > 0);
164 close(fd);
165 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
166 assert_se(fd > 0);
167 close(fd);
168 link1 = path_join(tmp_dir, "link1");
169 assert_se(link1);
170 assert_se(symlink("file1", link1) == 0);
171 link2 = path_join(tmp_dir, "link2");
172 assert_se(link1);
173 assert_se(symlink("file2", link2) == 0);
174
175 assert_se(path_is_mount_point_full(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
176 assert_se(path_is_mount_point_full(file1, NULL, 0) == 0);
177 assert_se(path_is_mount_point_full(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
178 assert_se(path_is_mount_point_full(link1, NULL, 0) == 0);
179
180 /* directory mountpoints */
181 dir1 = path_join(tmp_dir, "dir1");
182 assert_se(dir1);
183 assert_se(mkdir(dir1, 0755) == 0);
184 dirlink1 = path_join(tmp_dir, "dirlink1");
185 assert_se(dirlink1);
186 assert_se(symlink("dir1", dirlink1) == 0);
187 dirlink1file = path_join(tmp_dir, "dirlink1file");
188 assert_se(dirlink1file);
189 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
190 dir2 = path_join(tmp_dir, "dir2");
191 assert_se(dir2);
192 assert_se(mkdir(dir2, 0755) == 0);
193
194 assert_se(path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
195 assert_se(path_is_mount_point_full(dir1, NULL, 0) == 0);
196 assert_se(path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
197 assert_se(path_is_mount_point_full(dirlink1, NULL, 0) == 0);
198
199 /* file in subdirectory mountpoints */
200 dir1file = path_join(dir1, "file");
201 assert_se(dir1file);
202 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
203 assert_se(fd > 0);
204 close(fd);
205
206 assert_se(path_is_mount_point_full(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
207 assert_se(path_is_mount_point_full(dir1file, NULL, 0) == 0);
208 assert_se(path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
209 assert_se(path_is_mount_point_full(dirlink1file, NULL, 0) == 0);
210
211 /* these tests will only work as root */
212 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
213 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
214 const char *file2d;
215
216 /* files */
217 /* capture results in vars, to avoid dangling mounts on failure */
218 log_info("%s: %s", __func__, file2);
219 rf = path_is_mount_point_full(file2, NULL, 0);
220 rt = path_is_mount_point_full(file2, NULL, AT_SYMLINK_FOLLOW);
221
222 file2d = strjoina(file2, "/");
223 log_info("%s: %s", __func__, file2d);
224 rdf = path_is_mount_point_full(file2d, NULL, 0);
225 rdt = path_is_mount_point_full(file2d, NULL, AT_SYMLINK_FOLLOW);
226
227 log_info("%s: %s", __func__, link2);
228 rlf = path_is_mount_point_full(link2, NULL, 0);
229 rlt = path_is_mount_point_full(link2, NULL, AT_SYMLINK_FOLLOW);
230
231 assert_se(umount(file2) == 0);
232
233 assert_se(rf == 1);
234 assert_se(rt == 1);
235 assert_se(rdf == -ENOTDIR);
236 assert_se(rdt == -ENOTDIR);
237 assert_se(rlf == 0);
238 assert_se(rlt == 1);
239
240 /* dirs */
241 dir2file = path_join(dir2, "file");
242 assert_se(dir2file);
243 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
244 assert_se(fd > 0);
245 close(fd);
246
247 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
248
249 log_info("%s: %s", __func__, dir1);
250 rf = path_is_mount_point_full(dir1, NULL, 0);
251 rt = path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW);
252 log_info("%s: %s", __func__, dirlink1);
253 rlf = path_is_mount_point_full(dirlink1, NULL, 0);
254 rlt = path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW);
255 log_info("%s: %s", __func__, dirlink1file);
256 /* its parent is a mount point, but not /file itself */
257 rl1f = path_is_mount_point_full(dirlink1file, NULL, 0);
258 rl1t = path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
259
260 assert_se(umount(dir1) == 0);
261
262 assert_se(rf == 1);
263 assert_se(rt == 1);
264 assert_se(rlf == 0);
265 assert_se(rlt == 1);
266 assert_se(rl1f == 0);
267 assert_se(rl1t == 0);
268
269 } else
270 log_info("Skipping bind mount file test");
271
272 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
273 }
274
275 TEST(is_mount_point_at) {
276 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
277 _cleanup_free_ char *pwd = NULL, *tmpdir_basename = NULL;
278 _cleanup_close_ int fd = -EBADF;
279 int r;
280
281 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
282 assert_se(fd >= 0);
283
284 /* Not allowed, since "/" is a path, not a plain filename */
285 assert_se(is_mount_point_at(fd, "/", 0) == -EINVAL);
286 assert_se(is_mount_point_at(fd, "..", 0) == -EINVAL);
287 assert_se(is_mount_point_at(fd, "../", 0) == -EINVAL);
288 assert_se(is_mount_point_at(fd, "/proc", 0) == -EINVAL);
289 assert_se(is_mount_point_at(fd, "/proc/", 0) == -EINVAL);
290 assert_se(is_mount_point_at(fd, "proc/sys", 0) == -EINVAL);
291 assert_se(is_mount_point_at(fd, "proc/sys/", 0) == -EINVAL);
292
293 /* This one definitely is a mount point */
294 assert_se(is_mount_point_at(fd, "proc", 0) > 0);
295 assert_se(is_mount_point_at(fd, "proc/", 0) > 0);
296
297 safe_close(fd);
298 fd = open("/tmp", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
299 assert_se(fd >= 0);
300
301 assert_se(mkdtemp_malloc("/tmp/not-mounted-XXXXXX", &tmpdir) >= 0);
302 ASSERT_OK(path_extract_filename(tmpdir, &tmpdir_basename));
303 ASSERT_OK_ZERO(is_mount_point_at(fd, tmpdir_basename, 0));
304 ASSERT_OK_ZERO(is_mount_point_at(fd, strjoina(tmpdir_basename, "/"), 0));
305
306 safe_close(fd);
307 fd = open("/proc", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
308 assert_se(fd >= 0);
309
310 ASSERT_OK_POSITIVE(is_mount_point_at(fd, NULL, 0));
311 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "", 0));
312 ASSERT_OK_POSITIVE(is_mount_point_at(fd, ".", 0));
313 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "./", 0));
314 ASSERT_OK_ZERO(is_mount_point_at(fd, "version", 0));
315
316 ASSERT_OK(safe_getcwd(&pwd));
317 ASSERT_OK_ERRNO(fchdir(fd));
318
319 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, NULL, 0));
320 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, "", 0));
321 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, "./", 0));
322
323 ASSERT_OK_ERRNO(chdir(pwd));
324
325 safe_close(fd);
326 fd = open("/proc/version", O_RDONLY|O_CLOEXEC|O_NOCTTY);
327 assert_se(fd >= 0);
328
329 r = is_mount_point_at(fd, NULL, 0);
330 assert_se(IN_SET(r, 0, -ENOTDIR)); /* on old kernels we can't determine if regular files are mount points if we have no directory fd */
331
332 if (!mount_new_api_supported())
333 return;
334
335 /* Symlinks can be mount points with new mount API */
336 _cleanup_close_ int mfd = -EBADF, rfd = -EBADF;
337 _cleanup_free_ char *t = NULL;
338 struct stat st;
339
340 safe_close(fd);
341 ASSERT_OK_ERRNO(fd = open(tmpdir, O_DIRECTORY|O_PATH|O_CLOEXEC));
342
343 ASSERT_OK_ERRNO(symlinkat("/usr", fd, "symlink"));
344
345 mfd = open_tree(fd, "symlink", AT_SYMLINK_NOFOLLOW|OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC);
346 if (mfd < 0 && ERRNO_IS_PRIVILEGE(errno))
347 return;
348 ASSERT_OK_ERRNO(mfd);
349
350 ASSERT_OK_ERRNO(rfd = openat(fd, "regular", O_CLOEXEC|O_CREAT|O_EXCL, 0644));
351
352 ASSERT_OK_ERRNO(move_mount(mfd, "", rfd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH));
353
354 ASSERT_OK_ERRNO(fstatat(fd, "regular", &st, AT_SYMLINK_NOFOLLOW));
355 ASSERT_OK(stat_verify_symlink(&st));
356 ASSERT_OK(readlinkat_malloc(fd, "regular", &t));
357 ASSERT_STREQ(t, "/usr");
358
359 ASSERT_OK(is_mount_point_at(fd, "regular", 0));
360 }
361
362 TEST(ms_nosymfollow_supported) {
363 log_info("MS_NOSYMFOLLOW supported: %s", yes_no(ms_nosymfollow_supported()));
364 }
365
366 TEST(mount_option_supported) {
367 int r;
368
369 r = mount_option_supported("tmpfs", "size", "64M");
370 log_info("tmpfs supports size=64M: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
371 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
372
373 r = mount_option_supported("ext4", "discard", NULL);
374 log_info("ext4 supports discard: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
375 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
376
377 r = mount_option_supported("tmpfs", "idontexist", "64M");
378 log_info("tmpfs supports idontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
379 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
380
381 r = mount_option_supported("tmpfs", "ialsodontexist", NULL);
382 log_info("tmpfs supports ialsodontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
383 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
384
385 r = mount_option_supported("proc", "hidepid", "1");
386 log_info("proc supports hidepid=1: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
387 assert_se(r >= 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
388 }
389
390 TEST(fstype_can_discard) {
391 assert_se(fstype_can_discard("ext4"));
392 assert_se(!fstype_can_discard("squashfs"));
393 assert_se(!fstype_can_discard("iso9660"));
394 }
395
396 TEST(fstype_can_norecovery) {
397 ASSERT_STREQ(fstype_norecovery_option("ext4"), "norecovery");
398 ASSERT_NULL(fstype_norecovery_option("vfat"));
399 ASSERT_NULL(fstype_norecovery_option("tmpfs"));
400 }
401
402 TEST(fstype_can_fmask_dmask) {
403 assert_se(fstype_can_fmask_dmask("vfat"));
404 assert_se(!fstype_can_fmask_dmask("tmpfs"));
405 }
406
407 TEST(path_get_mnt_id_at_null) {
408 _cleanup_close_ int root_fd = -EBADF, run_fd = -EBADF;
409 int id1, id2;
410
411 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run/", &id1) >= 0);
412 assert_se(id1 > 0);
413
414 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run", &id2) >= 0);
415 assert_se(id1 == id2);
416 id2 = -1;
417
418 root_fd = open("/", O_DIRECTORY|O_CLOEXEC);
419 assert_se(root_fd >= 0);
420
421 assert_se(path_get_mnt_id_at(root_fd, "/run/", &id2) >= 0);
422 assert_se(id1 = id2);
423 id2 = -1;
424
425 assert_se(path_get_mnt_id_at(root_fd, "/run", &id2) >= 0);
426 assert_se(id1 = id2);
427 id2 = -1;
428
429 assert_se(path_get_mnt_id_at(root_fd, "run", &id2) >= 0);
430 assert_se(id1 = id2);
431 id2 = -1;
432
433 assert_se(path_get_mnt_id_at(root_fd, "run/", &id2) >= 0);
434 assert_se(id1 = id2);
435 id2 = -1;
436
437 run_fd = openat(root_fd, "run", O_DIRECTORY|O_CLOEXEC);
438 assert_se(run_fd >= 0);
439
440 id2 = -1;
441 assert_se(path_get_mnt_id_at(run_fd, "", &id2) >= 0);
442 assert_se(id1 = id2);
443 id2 = -1;
444
445 assert_se(path_get_mnt_id_at(run_fd, NULL, &id2) >= 0);
446 assert_se(id1 = id2);
447 id2 = -1;
448
449 assert_se(path_get_mnt_id_at(run_fd, ".", &id2) >= 0);
450 assert_se(id1 = id2);
451 id2 = -1;
452 }
453
454 static int intro(void) {
455 /* let's move into our own mount namespace with all propagation from the host turned off, so
456 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
457
458 if (unshare(CLONE_NEWNS) < 0) {
459 if (!ERRNO_IS_PRIVILEGE(errno))
460 return log_error_errno(errno, "Failed to detach mount namespace: %m");
461
462 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
463 } else
464 assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0);
465
466 return EXIT_SUCCESS;
467 }
468
469 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);