Debian Codegraph @

src/test/test-mountpoint-util.c

raw
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 "errno-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "hashmap.h"
14 #include "log.h"
15 #include "mountpoint-util.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "stat-util.h"
19 #include "string-util.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22 #include "virt.h"
23
24 static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
25 unsigned long flags;
26
27 log_info("/* %s(%s) */", __func__, strnull(name));
28
29 assert_se(mount_propagation_flag_from_string(name, &flags) == ret);
30
31 if (ret >= 0) {
32 const char *c;
33
34 assert_se(flags == expected);
35
36 c = mount_propagation_flag_to_string(flags);
37 if (isempty(name))
38 assert_se(isempty(c));
39 else
40 ASSERT_STREQ(c, name);
41 }
42 }
43
44 TEST(mount_propagation_flag) {
45 test_mount_propagation_flag_one("shared", 0, MS_SHARED);
46 test_mount_propagation_flag_one("slave", 0, MS_SLAVE);
47 test_mount_propagation_flag_one("private", 0, MS_PRIVATE);
48 test_mount_propagation_flag_one(NULL, 0, 0);
49 test_mount_propagation_flag_one("", 0, 0);
50 test_mount_propagation_flag_one("xxxx", -EINVAL, 0);
51 test_mount_propagation_flag_one(" ", -EINVAL, 0);
52 }
53
54 TEST(mnt_id) {
55 _cleanup_fclose_ FILE *f = NULL;
56 _cleanup_hashmap_free_ Hashmap *h = NULL;
57 char *p;
58 void *k;
59 int r;
60
61 assert_se(f = fopen("/proc/self/mountinfo", "re"));
62 assert_se(h = hashmap_new(&trivial_hash_ops_value_free));
63
64 for (;;) {
65 _cleanup_free_ char *line = NULL, *path = NULL;
66 int mnt_id;
67
68 r = read_line(f, LONG_LINE_MAX, &line);
69 if (r == 0)
70 break;
71 assert_se(r > 0);
72
73 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
74 #if HAS_FEATURE_MEMORY_SANITIZER
75 /* We don't know the length of the string, so we need to unpoison it one char at a time */
76 for (const char *c = path; ; c++) {
77 msan_unpoison(c, 1);
78 if (!*c)
79 break;
80 }
81 #endif
82 log_debug("mountinfo: %s → %i", path, mnt_id);
83
84 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
85 path = NULL;
86 }
87
88 HASHMAP_FOREACH_KEY(p, k, h) {
89 int mnt_id = PTR_TO_INT(k), mnt_id2;
90 const char *q;
91
92 r = path_get_mnt_id(p, &mnt_id2);
93 if (r < 0) {
94 log_debug_errno(r, "Failed to get the mnt id of %s: %m", p);
95 continue;
96 }
97
98 if (mnt_id == mnt_id2) {
99 log_debug("mnt ids of %s is %i.", p, mnt_id);
100 continue;
101 } else
102 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p, mnt_id, mnt_id2);
103
104 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
105 * See #11505. */
106 assert_se(q = hashmap_get(h, INT_TO_PTR(mnt_id2)));
107
108 assert_se((r = path_is_mount_point_full(p, NULL, 0)) >= 0);
109 if (r == 0) {
110 /* If the path is not a mount point anymore, then it must be a sub directory of
111 * the path corresponds to mnt_id2. */
112 log_debug("The path %s for mnt id %i is not a mount point.", p, mnt_id2);
113 assert_se(!isempty(path_startswith(p, q)));
114 } else {
115 /* If the path is still a mount point, then it must be equivalent to the path
116 * corresponds to mnt_id2 */
117 log_debug("There are multiple mounts on the same path %s.", p);
118 assert_se(path_equal(p, q));
119 }
120 }
121 }
122
123 TEST(path_is_mount_point) {
124 int fd;
125 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
126 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
127 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
128 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
129
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 assert_se(path_is_mount_point_full("//", NULL, AT_SYMLINK_FOLLOW) > 0);
133 assert_se(path_is_mount_point_full("//", NULL, 0) > 0);
134
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 assert_se(path_is_mount_point_full("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
138 assert_se(path_is_mount_point_full("/proc/", NULL, 0) > 0);
139
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 assert_se(path_is_mount_point_full("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
143 assert_se(path_is_mount_point_full("/proc/1/", NULL, 0) == 0);
144
145 /* we'll create a hierarchy of different kinds of dir/file/link
146 * layouts:
147 *
148 * <tmp>/file1, <tmp>/file2
149 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
150 * <tmp>/dir1/
151 * <tmp>/dir1/file
152 * <tmp>/dirlink1 -> dir1
153 * <tmp>/dirlink1file -> dirlink1/file
154 * <tmp>/dir2/
155 * <tmp>/dir2/file
156 */
157
158 /* file mountpoints */
159 ASSERT_NOT_NULL(mkdtemp(tmp_dir));
160 file1 = path_join(tmp_dir, "file1");
161 assert_se(file1);
162 file2 = path_join(tmp_dir, "file2");
163 assert_se(file2);
164 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
165 assert_se(fd > 0);
166 close(fd);
167 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
168 assert_se(fd > 0);
169 close(fd);
170 link1 = path_join(tmp_dir, "link1");
171 assert_se(link1);
172 assert_se(symlink("file1", link1) == 0);
173 link2 = path_join(tmp_dir, "link2");
174 assert_se(link1);
175 assert_se(symlink("file2", link2) == 0);
176
177 assert_se(path_is_mount_point_full(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
178 assert_se(path_is_mount_point_full(file1, NULL, 0) == 0);
179 assert_se(path_is_mount_point_full(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
180 assert_se(path_is_mount_point_full(link1, NULL, 0) == 0);
181
182 /* directory mountpoints */
183 dir1 = path_join(tmp_dir, "dir1");
184 assert_se(dir1);
185 assert_se(mkdir(dir1, 0755) == 0);
186 dirlink1 = path_join(tmp_dir, "dirlink1");
187 assert_se(dirlink1);
188 assert_se(symlink("dir1", dirlink1) == 0);
189 dirlink1file = path_join(tmp_dir, "dirlink1file");
190 assert_se(dirlink1file);
191 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
192 dir2 = path_join(tmp_dir, "dir2");
193 assert_se(dir2);
194 assert_se(mkdir(dir2, 0755) == 0);
195
196 assert_se(path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
197 assert_se(path_is_mount_point_full(dir1, NULL, 0) == 0);
198 assert_se(path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
199 assert_se(path_is_mount_point_full(dirlink1, NULL, 0) == 0);
200
201 /* file in subdirectory mountpoints */
202 dir1file = path_join(dir1, "file");
203 assert_se(dir1file);
204 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
205 assert_se(fd > 0);
206 close(fd);
207
208 assert_se(path_is_mount_point_full(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
209 assert_se(path_is_mount_point_full(dir1file, NULL, 0) == 0);
210 assert_se(path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
211 assert_se(path_is_mount_point_full(dirlink1file, NULL, 0) == 0);
212
213 /* these tests will only work as root */
214 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
215 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
216 const char *file2d;
217
218 /* files */
219 /* capture results in vars, to avoid dangling mounts on failure */
220 log_info("%s: %s", __func__, file2);
221 rf = path_is_mount_point_full(file2, NULL, 0);
222 rt = path_is_mount_point_full(file2, NULL, AT_SYMLINK_FOLLOW);
223
224 file2d = strjoina(file2, "/");
225 log_info("%s: %s", __func__, file2d);
226 rdf = path_is_mount_point_full(file2d, NULL, 0);
227 rdt = path_is_mount_point_full(file2d, NULL, AT_SYMLINK_FOLLOW);
228
229 log_info("%s: %s", __func__, link2);
230 rlf = path_is_mount_point_full(link2, NULL, 0);
231 rlt = path_is_mount_point_full(link2, NULL, AT_SYMLINK_FOLLOW);
232
233 assert_se(umount(file2) == 0);
234
235 assert_se(rf == 1);
236 assert_se(rt == 1);
237 assert_se(rdf == -ENOTDIR);
238 assert_se(rdt == -ENOTDIR);
239 assert_se(rlf == 0);
240 assert_se(rlt == 1);
241
242 /* dirs */
243 dir2file = path_join(dir2, "file");
244 assert_se(dir2file);
245 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
246 assert_se(fd > 0);
247 close(fd);
248
249 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
250
251 log_info("%s: %s", __func__, dir1);
252 rf = path_is_mount_point_full(dir1, NULL, 0);
253 rt = path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW);
254 log_info("%s: %s", __func__, dirlink1);
255 rlf = path_is_mount_point_full(dirlink1, NULL, 0);
256 rlt = path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW);
257 log_info("%s: %s", __func__, dirlink1file);
258 /* its parent is a mount point, but not /file itself */
259 rl1f = path_is_mount_point_full(dirlink1file, NULL, 0);
260 rl1t = path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
261
262 assert_se(umount(dir1) == 0);
263
264 assert_se(rf == 1);
265 assert_se(rt == 1);
266 assert_se(rlf == 0);
267 assert_se(rlt == 1);
268 assert_se(rl1f == 0);
269 assert_se(rl1t == 0);
270
271 } else
272 log_info("Skipping bind mount file test");
273
274 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
275 }
276
277 TEST(is_mount_point_at) {
278 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
279 _cleanup_free_ char *pwd = NULL, *tmpdir_basename = NULL;
280 _cleanup_close_ int fd = -EBADF;
281 int r;
282
283 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
284 assert_se(fd >= 0);
285
286 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "/", /* flags= */ 0));
287 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "..", /* flags= */ 0));
288 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "../", /* flags= */ 0));
289 r = ASSERT_OK(proc_mounted());
290 ASSERT_OK_EQ(is_mount_point_at(fd, "/proc", /* flags= */ 0), r);
291 ASSERT_OK_EQ(is_mount_point_at(fd, "/proc/", /* flags= */ 0), r);
292 ASSERT_OK_EQ(is_mount_point_at(fd, "proc", /* flags= */ 0), r);
293 ASSERT_OK_EQ(is_mount_point_at(fd, "proc/", /* flags= */ 0), r);
294 ASSERT_OK_ZERO(is_mount_point_at(fd, "usr/lib", /* flags= */ 0));
295 ASSERT_OK_ZERO(is_mount_point_at(fd, "usr/lib", /* flags= */ 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(mount_option_supported) {
363 int r;
364
365 r = mount_option_supported("tmpfs", "size", "64M");
366 log_info("tmpfs supports size=64M: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
367 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
368
369 r = mount_option_supported("ext4", "discard", NULL);
370 log_info("ext4 supports discard: %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("tmpfs", "idontexist", "64M");
374 log_info("tmpfs supports idontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
375 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
376
377 r = mount_option_supported("tmpfs", "ialsodontexist", NULL);
378 log_info("tmpfs supports ialsodontexist: %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("proc", "hidepid", "1");
382 log_info("proc supports hidepid=1: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
383 assert_se(r >= 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
384 }
385
386 TEST(fstype_can_discard) {
387 assert_se(fstype_can_discard("ext4"));
388 assert_se(!fstype_can_discard("squashfs"));
389 assert_se(!fstype_can_discard("iso9660"));
390 }
391
392 TEST(fstype_can_norecovery) {
393 ASSERT_STREQ(fstype_norecovery_option("ext4"), "norecovery");
394 ASSERT_NULL(fstype_norecovery_option("vfat"));
395 ASSERT_NULL(fstype_norecovery_option("tmpfs"));
396 }
397
398 TEST(fstype_can_fmask_dmask) {
399 assert_se(fstype_can_fmask_dmask("vfat"));
400 assert_se(!fstype_can_fmask_dmask("tmpfs"));
401 }
402
403 TEST(path_get_mnt_id_at_null) {
404 _cleanup_close_ int root_fd = -EBADF, run_fd = -EBADF;
405 int id1, id2;
406
407 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run/", &id1) >= 0);
408 assert_se(id1 > 0);
409
410 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run", &id2) >= 0);
411 assert_se(id1 == id2);
412 id2 = -1;
413
414 root_fd = open("/", O_DIRECTORY|O_CLOEXEC);
415 assert_se(root_fd >= 0);
416
417 assert_se(path_get_mnt_id_at(root_fd, "/run/", &id2) >= 0);
418 assert_se(id1 = id2);
419 id2 = -1;
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 run_fd = openat(root_fd, "run", O_DIRECTORY|O_CLOEXEC);
434 assert_se(run_fd >= 0);
435
436 id2 = -1;
437 assert_se(path_get_mnt_id_at(run_fd, "", &id2) >= 0);
438 assert_se(id1 = id2);
439 id2 = -1;
440
441 assert_se(path_get_mnt_id_at(run_fd, NULL, &id2) >= 0);
442 assert_se(id1 = id2);
443 id2 = -1;
444
445 assert_se(path_get_mnt_id_at(run_fd, ".", &id2) >= 0);
446 assert_se(id1 = id2);
447 id2 = -1;
448 }
449
450 static int intro(void) {
451 /* let's move into our own mount namespace with all propagation from the host turned off, so
452 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
453
454 if (running_in_chroot() != 0) {
455 /* We cannot remount file system with MS_PRIVATE when running in chroot. */
456 log_notice("Running in chroot, proceeding in originating mount namespace.");
457 return EXIT_SUCCESS;
458 }
459
460 if (unshare(CLONE_NEWNS) < 0) {
461 if (!ERRNO_IS_PRIVILEGE(errno))
462 return log_error_errno(errno, "Failed to detach mount namespace: %m");
463
464 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
465 } else
466 assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0);
467
468 return EXIT_SUCCESS;
469 }
470
471 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);