]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mountpoint-util.c
c4a87d253e915dd45e59d51f373f4545b751c6a7
[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 <sys/mount.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "constants.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "hashmap.h"
12 #include "log.h"
13 #include "mountpoint-util.h"
14 #include "path-util.h"
15 #include "rm-rf.h"
16 #include "string-util.h"
17 #include "tests.h"
18 #include "tmpfile-util.h"
19
20 static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
21 unsigned long flags;
22
23 log_info("/* %s(%s) */", __func__, strnull(name));
24
25 assert_se(mount_propagation_flag_from_string(name, &flags) == ret);
26
27 if (ret >= 0) {
28 const char *c;
29
30 assert_se(flags == expected);
31
32 c = mount_propagation_flag_to_string(flags);
33 if (isempty(name))
34 assert_se(isempty(c));
35 else
36 assert_se(streq(c, name));
37 }
38 }
39
40 TEST(mount_propagation_flag) {
41 test_mount_propagation_flag_one("shared", 0, MS_SHARED);
42 test_mount_propagation_flag_one("slave", 0, MS_SLAVE);
43 test_mount_propagation_flag_one("private", 0, MS_PRIVATE);
44 test_mount_propagation_flag_one(NULL, 0, 0);
45 test_mount_propagation_flag_one("", 0, 0);
46 test_mount_propagation_flag_one("xxxx", -EINVAL, 0);
47 test_mount_propagation_flag_one(" ", -EINVAL, 0);
48 }
49
50 TEST(mnt_id) {
51 _cleanup_fclose_ FILE *f = NULL;
52 _cleanup_hashmap_free_free_ Hashmap *h = NULL;
53 char *p;
54 void *k;
55 int r;
56
57 assert_se(f = fopen("/proc/self/mountinfo", "re"));
58 assert_se(h = hashmap_new(&trivial_hash_ops));
59
60 for (;;) {
61 _cleanup_free_ char *line = NULL, *path = NULL;
62 int mnt_id;
63
64 r = read_line(f, LONG_LINE_MAX, &line);
65 if (r == 0)
66 break;
67 assert_se(r > 0);
68
69 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
70 #if HAS_FEATURE_MEMORY_SANITIZER
71 /* We don't know the length of the string, so we need to unpoison it one char at a time */
72 for (const char *c = path; ;c++) {
73 msan_unpoison(c, 1);
74 if (!*c)
75 break;
76 }
77 #endif
78 log_debug("mountinfo: %s → %i", path, mnt_id);
79
80 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
81 path = NULL;
82 }
83
84 HASHMAP_FOREACH_KEY(p, k, h) {
85 int mnt_id = PTR_TO_INT(k), mnt_id2;
86 const char *q;
87
88 r = path_get_mnt_id(p, &mnt_id2);
89 if (r < 0) {
90 log_debug_errno(r, "Failed to get the mnt id of %s: %m", p);
91 continue;
92 }
93
94 if (mnt_id == mnt_id2) {
95 log_debug("mnt ids of %s is %i.", p, mnt_id);
96 continue;
97 } else
98 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p, mnt_id, mnt_id2);
99
100 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
101 * See #11505. */
102 assert_se(q = hashmap_get(h, INT_TO_PTR(mnt_id2)));
103
104 assert_se((r = path_is_mount_point(p, NULL, 0)) >= 0);
105 if (r == 0) {
106 /* If the path is not a mount point anymore, then it must be a sub directory of
107 * the path corresponds to mnt_id2. */
108 log_debug("The path %s for mnt id %i is not a mount point.", p, mnt_id2);
109 assert_se(!isempty(path_startswith(p, q)));
110 } else {
111 /* If the path is still a mount point, then it must be equivalent to the path
112 * corresponds to mnt_id2 */
113 log_debug("There are multiple mounts on the same path %s.", p);
114 assert_se(path_equal(p, q));
115 }
116 }
117 }
118
119 TEST(path_is_mount_point) {
120 int fd;
121 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
122 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
123 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
124 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
125
126 assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0);
127 assert_se(path_is_mount_point("/", NULL, 0) > 0);
128 assert_se(path_is_mount_point("//", NULL, AT_SYMLINK_FOLLOW) > 0);
129 assert_se(path_is_mount_point("//", NULL, 0) > 0);
130
131 assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
132 assert_se(path_is_mount_point("/proc", NULL, 0) > 0);
133 assert_se(path_is_mount_point("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
134 assert_se(path_is_mount_point("/proc/", NULL, 0) > 0);
135
136 assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
137 assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0);
138 assert_se(path_is_mount_point("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
139 assert_se(path_is_mount_point("/proc/1/", NULL, 0) == 0);
140
141 assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0);
142 assert_se(path_is_mount_point("/sys", NULL, 0) > 0);
143 assert_se(path_is_mount_point("/sys/", NULL, AT_SYMLINK_FOLLOW) > 0);
144 assert_se(path_is_mount_point("/sys/", NULL, 0) > 0);
145
146 /* we'll create a hierarchy of different kinds of dir/file/link
147 * layouts:
148 *
149 * <tmp>/file1, <tmp>/file2
150 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
151 * <tmp>/dir1/
152 * <tmp>/dir1/file
153 * <tmp>/dirlink1 -> dir1
154 * <tmp>/dirlink1file -> dirlink1/file
155 * <tmp>/dir2/
156 * <tmp>/dir2/file
157 */
158
159 /* file mountpoints */
160 assert_se(mkdtemp(tmp_dir) != NULL);
161 file1 = path_join(tmp_dir, "file1");
162 assert_se(file1);
163 file2 = path_join(tmp_dir, "file2");
164 assert_se(file2);
165 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
166 assert_se(fd > 0);
167 close(fd);
168 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
169 assert_se(fd > 0);
170 close(fd);
171 link1 = path_join(tmp_dir, "link1");
172 assert_se(link1);
173 assert_se(symlink("file1", link1) == 0);
174 link2 = path_join(tmp_dir, "link2");
175 assert_se(link1);
176 assert_se(symlink("file2", link2) == 0);
177
178 assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
179 assert_se(path_is_mount_point(file1, NULL, 0) == 0);
180 assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
181 assert_se(path_is_mount_point(link1, NULL, 0) == 0);
182
183 /* directory mountpoints */
184 dir1 = path_join(tmp_dir, "dir1");
185 assert_se(dir1);
186 assert_se(mkdir(dir1, 0755) == 0);
187 dirlink1 = path_join(tmp_dir, "dirlink1");
188 assert_se(dirlink1);
189 assert_se(symlink("dir1", dirlink1) == 0);
190 dirlink1file = path_join(tmp_dir, "dirlink1file");
191 assert_se(dirlink1file);
192 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
193 dir2 = path_join(tmp_dir, "dir2");
194 assert_se(dir2);
195 assert_se(mkdir(dir2, 0755) == 0);
196
197 assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
198 assert_se(path_is_mount_point(dir1, NULL, 0) == 0);
199 assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
200 assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0);
201
202 /* file in subdirectory mountpoints */
203 dir1file = path_join(dir1, "file");
204 assert_se(dir1file);
205 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
206 assert_se(fd > 0);
207 close(fd);
208
209 assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
210 assert_se(path_is_mount_point(dir1file, NULL, 0) == 0);
211 assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
212 assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0);
213
214 /* these tests will only work as root */
215 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
216 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
217 const char *file2d;
218
219 /* files */
220 /* capture results in vars, to avoid dangling mounts on failure */
221 log_info("%s: %s", __func__, file2);
222 rf = path_is_mount_point(file2, NULL, 0);
223 rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW);
224
225 file2d = strjoina(file2, "/");
226 log_info("%s: %s", __func__, file2d);
227 rdf = path_is_mount_point(file2d, NULL, 0);
228 rdt = path_is_mount_point(file2d, NULL, AT_SYMLINK_FOLLOW);
229
230 log_info("%s: %s", __func__, link2);
231 rlf = path_is_mount_point(link2, NULL, 0);
232 rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW);
233
234 assert_se(umount(file2) == 0);
235
236 assert_se(rf == 1);
237 assert_se(rt == 1);
238 assert_se(rdf == -ENOTDIR);
239 assert_se(rdt == -ENOTDIR);
240 assert_se(rlf == 0);
241 assert_se(rlt == 1);
242
243 /* dirs */
244 dir2file = path_join(dir2, "file");
245 assert_se(dir2file);
246 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
247 assert_se(fd > 0);
248 close(fd);
249
250 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
251
252 log_info("%s: %s", __func__, dir1);
253 rf = path_is_mount_point(dir1, NULL, 0);
254 rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW);
255 log_info("%s: %s", __func__, dirlink1);
256 rlf = path_is_mount_point(dirlink1, NULL, 0);
257 rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW);
258 log_info("%s: %s", __func__, dirlink1file);
259 /* its parent is a mount point, but not /file itself */
260 rl1f = path_is_mount_point(dirlink1file, NULL, 0);
261 rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
262
263 assert_se(umount(dir1) == 0);
264
265 assert_se(rf == 1);
266 assert_se(rt == 1);
267 assert_se(rlf == 0);
268 assert_se(rlt == 1);
269 assert_se(rl1f == 0);
270 assert_se(rl1t == 0);
271
272 } else
273 log_info("Skipping bind mount file test");
274
275 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
276 }
277
278 TEST(fd_is_mount_point) {
279 _cleanup_close_ int fd = -1;
280 int r;
281
282 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
283 assert_se(fd >= 0);
284
285 /* Not allowed, since "/" is a path, not a plain filename */
286 assert_se(fd_is_mount_point(fd, "/", 0) == -EINVAL);
287 assert_se(fd_is_mount_point(fd, ".", 0) == -EINVAL);
288 assert_se(fd_is_mount_point(fd, "./", 0) == -EINVAL);
289 assert_se(fd_is_mount_point(fd, "..", 0) == -EINVAL);
290 assert_se(fd_is_mount_point(fd, "../", 0) == -EINVAL);
291 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
292 assert_se(fd_is_mount_point(fd, "/proc", 0) == -EINVAL);
293 assert_se(fd_is_mount_point(fd, "/proc/", 0) == -EINVAL);
294 assert_se(fd_is_mount_point(fd, "proc/sys", 0) == -EINVAL);
295 assert_se(fd_is_mount_point(fd, "proc/sys/", 0) == -EINVAL);
296
297 /* This one definitely is a mount point */
298 assert_se(fd_is_mount_point(fd, "proc", 0) > 0);
299 assert_se(fd_is_mount_point(fd, "proc/", 0) > 0);
300
301 /* /root's entire reason for being is to be on the root file system (i.e. not in /home/ which
302 * might be split off), so that the user can always log in, so it cannot be a mount point unless
303 * the system is borked. Let's allow for it to be missing though. */
304 assert_se(IN_SET(fd_is_mount_point(fd, "root", 0), -ENOENT, 0));
305 assert_se(IN_SET(fd_is_mount_point(fd, "root/", 0), -ENOENT, 0));
306
307 safe_close(fd);
308 fd = open("/proc", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
309 assert_se(fd >= 0);
310
311 assert_se(fd_is_mount_point(fd, NULL, 0) > 0);
312 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
313 assert_se(fd_is_mount_point(fd, "version", 0) == 0);
314
315 safe_close(fd);
316 fd = open("/proc/version", O_RDONLY|O_CLOEXEC|O_NOCTTY);
317 assert_se(fd >= 0);
318
319 r = fd_is_mount_point(fd, NULL, 0);
320 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 */
321 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
322 }
323
324 static int intro(void) {
325 /* let's move into our own mount namespace with all propagation from the host turned off, so
326 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
327
328 if (unshare(CLONE_NEWNS) < 0) {
329 if (!ERRNO_IS_PRIVILEGE(errno))
330 return log_error_errno(errno, "Failed to detach mount namespace: %m");
331
332 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
333 } else
334 assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0);
335
336 return EXIT_SUCCESS;
337 }
338
339 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);