]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fd-util.c
networkd: Correct documentation for LinkLocalAddressing
[thirdparty/systemd.git] / src / test / test-fd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/eventfd.h>
5 #include <sys/mount.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "data-fd-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "macro.h"
14 #include "memory-util.h"
15 #include "missing_syscall.h"
16 #include "mkdir.h"
17 #include "mount-util.h"
18 #include "namespace-util.h"
19 #include "path-util.h"
20 #include "process-util.h"
21 #include "random-util.h"
22 #include "rlimit-util.h"
23 #include "rm-rf.h"
24 #include "seccomp-util.h"
25 #include "serialize.h"
26 #include "stat-util.h"
27 #include "string-util.h"
28 #include "tests.h"
29 #include "tmpfile-util.h"
30
31 TEST(close_many) {
32 int fds[3];
33 _cleanup_(unlink_tempfilep) char name0[] = "/tmp/test-close-many.XXXXXX";
34 _cleanup_(unlink_tempfilep) char name1[] = "/tmp/test-close-many.XXXXXX";
35 _cleanup_(unlink_tempfilep) char name2[] = "/tmp/test-close-many.XXXXXX";
36
37 fds[0] = mkostemp_safe(name0);
38 fds[1] = mkostemp_safe(name1);
39 fds[2] = mkostemp_safe(name2);
40
41 close_many(fds, 2);
42
43 assert_se(fcntl(fds[0], F_GETFD) == -1);
44 assert_se(fcntl(fds[1], F_GETFD) == -1);
45 assert_se(fcntl(fds[2], F_GETFD) >= 0);
46
47 safe_close(fds[2]);
48 }
49
50 TEST(close_nointr) {
51 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-test-close_nointr.XXXXXX";
52 int fd;
53
54 fd = mkostemp_safe(name);
55 assert_se(fd >= 0);
56 assert_se(close_nointr(fd) >= 0);
57 assert_se(close_nointr(fd) < 0);
58 }
59
60 TEST(same_fd) {
61 _cleanup_close_pair_ int p[2];
62 _cleanup_close_ int a, b, c;
63
64 assert_se(pipe2(p, O_CLOEXEC) >= 0);
65 assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
66 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
67 assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0);
68
69 assert_se(same_fd(p[0], p[0]) > 0);
70 assert_se(same_fd(p[1], p[1]) > 0);
71 assert_se(same_fd(a, a) > 0);
72 assert_se(same_fd(b, b) > 0);
73
74 assert_se(same_fd(a, p[0]) > 0);
75 assert_se(same_fd(p[0], a) > 0);
76 assert_se(same_fd(c, p[0]) > 0);
77 assert_se(same_fd(p[0], c) > 0);
78 assert_se(same_fd(a, c) > 0);
79 assert_se(same_fd(c, a) > 0);
80
81 assert_se(same_fd(p[0], p[1]) == 0);
82 assert_se(same_fd(p[1], p[0]) == 0);
83 assert_se(same_fd(p[0], b) == 0);
84 assert_se(same_fd(b, p[0]) == 0);
85 assert_se(same_fd(p[1], a) == 0);
86 assert_se(same_fd(a, p[1]) == 0);
87 assert_se(same_fd(p[1], b) == 0);
88 assert_se(same_fd(b, p[1]) == 0);
89
90 assert_se(same_fd(a, b) == 0);
91 assert_se(same_fd(b, a) == 0);
92 }
93
94 TEST(open_serialization_fd) {
95 _cleanup_close_ int fd = -EBADF;
96
97 fd = open_serialization_fd("test");
98 assert_se(fd >= 0);
99
100 assert_se(write(fd, "test\n", 5) == 5);
101 }
102
103 TEST(open_serialization_file) {
104 _cleanup_fclose_ FILE *f = NULL;
105 int r;
106
107 r = open_serialization_file("test", &f);
108 assert_se(r >= 0);
109 assert_se(f);
110
111 assert_se(fwrite("test\n", 1, 5, f) == 5);
112 }
113
114 TEST(fd_move_above_stdio) {
115 int original_stdin, new_fd;
116
117 original_stdin = fcntl(0, F_DUPFD, 3);
118 assert_se(original_stdin >= 3);
119 assert_se(close_nointr(0) != EBADF);
120
121 new_fd = open("/dev/null", O_RDONLY);
122 assert_se(new_fd == 0);
123
124 new_fd = fd_move_above_stdio(new_fd);
125 assert_se(new_fd >= 3);
126
127 assert_se(dup(original_stdin) == 0);
128 assert_se(close_nointr(original_stdin) != EBADF);
129 assert_se(close_nointr(new_fd) != EBADF);
130 }
131
132 TEST(rearrange_stdio) {
133 pid_t pid;
134 int r;
135
136 r = safe_fork("rearrange", FORK_WAIT|FORK_LOG, &pid);
137 assert_se(r >= 0);
138
139 if (r == 0) {
140 _cleanup_free_ char *path = NULL;
141 int pipe_read_fd, pair[2];
142 char buffer[10];
143
144 /* Child */
145
146 safe_close(STDERR_FILENO); /* Let's close an fd < 2, to make it more interesting */
147
148 assert_se(rearrange_stdio(-EBADF, -EBADF, -EBADF) >= 0);
149 /* Reconfigure logging after rearranging stdout/stderr, so we still log to somewhere if the
150 * following tests fail, making it slightly less annoying to debug */
151 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
152 log_open();
153
154 assert_se(fd_get_path(STDIN_FILENO, &path) >= 0);
155 assert_se(path_equal(path, "/dev/null"));
156 path = mfree(path);
157
158 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0);
159 assert_se(path_equal(path, "/dev/null"));
160 path = mfree(path);
161
162 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0);
163 assert_se(path_equal(path, "/dev/null"));
164 path = mfree(path);
165
166 safe_close(STDIN_FILENO);
167 safe_close(STDOUT_FILENO);
168 safe_close(STDERR_FILENO);
169
170 assert_se(pipe(pair) >= 0);
171 assert_se(pair[0] == 0);
172 assert_se(pair[1] == 1);
173 pipe_read_fd = fd_move_above_stdio(0);
174 assert_se(pipe_read_fd >= 3);
175
176 assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0);
177 assert_se(acquire_data_fd("foobar") == 2);
178
179 assert_se(rearrange_stdio(2, 0, 1) >= 0);
180
181 assert_se(write(1, "x", 1) < 0 && errno == ENOSPC);
182 assert_se(write(2, "z", 1) == 1);
183 assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1);
184 assert_se(buffer[0] == 'z');
185 assert_se(read(0, buffer, sizeof(buffer)) == 6);
186 assert_se(memcmp(buffer, "foobar", 6) == 0);
187
188 assert_se(rearrange_stdio(-EBADF, 1, 2) >= 0);
189 assert_se(write(1, "a", 1) < 0 && errno == ENOSPC);
190 assert_se(write(2, "y", 1) == 1);
191 assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1);
192 assert_se(buffer[0] == 'y');
193
194 assert_se(fd_get_path(0, &path) >= 0);
195 assert_se(path_equal(path, "/dev/null"));
196 path = mfree(path);
197
198 _exit(EXIT_SUCCESS);
199 }
200 }
201
202 TEST(read_nr_open) {
203 log_info("nr-open: %i", read_nr_open());
204 }
205
206 static size_t validate_fds(
207 bool opened,
208 const int *fds,
209 size_t n_fds) {
210
211 size_t c = 0;
212
213 /* Validates that fds in the specified array are one of the following three:
214 *
215 * 1. < 0 (test is skipped) or
216 * 2. opened (if 'opened' param is true) or
217 * 3. closed (if 'opened' param is false)
218 */
219
220 for (size_t i = 0; i < n_fds; i++) {
221 if (fds[i] < 0)
222 continue;
223
224 if (opened)
225 assert_se(fcntl(fds[i], F_GETFD) >= 0);
226 else
227 assert_se(fcntl(fds[i], F_GETFD) < 0 && errno == EBADF);
228
229 c++;
230 }
231
232 return c; /* Return number of fds >= 0 in the array */
233 }
234
235 static void test_close_all_fds_inner(void) {
236 _cleanup_free_ int *fds = NULL, *keep = NULL;
237 size_t n_fds, n_keep;
238 int max_fd;
239
240 log_info("/* %s */", __func__);
241
242 rlimit_nofile_bump(-1);
243
244 max_fd = get_max_fd();
245 assert_se(max_fd > 10);
246
247 if (max_fd > 7000) {
248 /* If the worst fallback is activated we need to iterate through all possible fds, hence,
249 * let's lower the limit a small bit, so that we don't run for too long. Yes, this undoes the
250 * rlimit_nofile_bump() call above partially. */
251
252 (void) setrlimit_closest(RLIMIT_NOFILE, &(struct rlimit) { 7000, 7000 });
253 max_fd = 7000;
254 }
255
256 /* Try to use 5000 fds, but when we can't bump the rlimit to make that happen use the whole limit minus 10 */
257 n_fds = MIN(((size_t) max_fd & ~1U) - 10U, 5000U);
258 assert_se((n_fds & 1U) == 0U); /* make sure even number of fds */
259
260 /* Allocate the determined number of fds, always two at a time */
261 assert_se(fds = new(int, n_fds));
262 for (size_t i = 0; i < n_fds; i += 2)
263 assert_se(pipe2(fds + i, O_CLOEXEC) >= 0);
264
265 /* Validate this worked */
266 assert_se(validate_fds(true, fds, n_fds) == n_fds);
267
268 /* Randomized number of fds to keep, but at most every second */
269 n_keep = (random_u64() % (n_fds / 2));
270
271 /* Now randomly select a number of fds from the array above to keep */
272 assert_se(keep = new(int, n_keep));
273 for (size_t k = 0; k < n_keep; k++) {
274 for (;;) {
275 size_t p;
276
277 p = random_u64() % n_fds;
278 if (fds[p] >= 0) {
279 keep[k] = TAKE_FD(fds[p]);
280 break;
281 }
282 }
283 }
284
285 /* Check that all fds from both arrays are still open, and test how many in each are >= 0 */
286 assert_se(validate_fds(true, fds, n_fds) == n_fds - n_keep);
287 assert_se(validate_fds(true, keep, n_keep) == n_keep);
288
289 /* Close logging fd first, so that we don't confuse it by closing its fd */
290 log_close();
291 log_set_open_when_needed(true);
292 log_settle_target();
293
294 /* Close all but the ones to keep */
295 assert_se(close_all_fds(keep, n_keep) >= 0);
296
297 assert_se(validate_fds(false, fds, n_fds) == n_fds - n_keep);
298 assert_se(validate_fds(true, keep, n_keep) == n_keep);
299
300 /* Close everything else too! */
301 assert_se(close_all_fds(NULL, 0) >= 0);
302
303 assert_se(validate_fds(false, fds, n_fds) == n_fds - n_keep);
304 assert_se(validate_fds(false, keep, n_keep) == n_keep);
305
306 log_set_open_when_needed(false);
307 log_open();
308 }
309
310 static int seccomp_prohibit_close_range(void) {
311 #if HAVE_SECCOMP && defined(__SNR_close_range)
312 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
313 int r;
314
315 r = seccomp_init_for_arch(&seccomp, SCMP_ARCH_NATIVE, SCMP_ACT_ALLOW);
316 if (r < 0)
317 return log_warning_errno(r, "Failed to acquire seccomp context, ignoring: %m");
318
319 r = seccomp_rule_add_exact(
320 seccomp,
321 SCMP_ACT_ERRNO(EPERM),
322 SCMP_SYS(close_range),
323 0);
324 if (r < 0)
325 return log_warning_errno(r, "Failed to add close_range() rule, ignoring: %m");
326
327 r = seccomp_load(seccomp);
328 if (r < 0)
329 return log_warning_errno(r, "Failed to apply close_range() restrictions, ignoring: %m");
330
331 return 0;
332 #else
333 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Seccomp support or close_range() syscall definition not available.");
334 #endif
335 }
336
337 TEST(close_all_fds) {
338 int r;
339
340 /* Runs the test four times. Once as is. Once with close_range() syscall blocked via seccomp, once
341 * with /proc/ overmounted, and once with the combination of both. This should trigger all fallbacks
342 * in the close_range_all() function. */
343
344 r = safe_fork("(caf-plain)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
345 if (r == 0) {
346 test_close_all_fds_inner();
347 _exit(EXIT_SUCCESS);
348 }
349 assert_se(r >= 0);
350
351 if (geteuid() != 0)
352 return (void) log_tests_skipped("Lacking privileges for test with close_range() blocked and /proc/ overmounted");
353
354 r = safe_fork("(caf-noproc)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
355 if (r == 0) {
356 r = mount_nofollow_verbose(LOG_WARNING, "tmpfs", "/proc", "tmpfs", 0, NULL);
357 if (r < 0)
358 log_notice("Overmounting /proc/ didn't work, skipping close_all_fds() with masked /proc/.");
359 else
360 test_close_all_fds_inner();
361 _exit(EXIT_SUCCESS);
362 }
363 assert_se(r >= 0);
364
365 if (!is_seccomp_available())
366 return (void) log_tests_skipped("Seccomp not available");
367
368 r = safe_fork("(caf-seccomp)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
369 if (r == 0) {
370 r = seccomp_prohibit_close_range();
371 if (r < 0)
372 log_notice("Applying seccomp filter didn't work, skipping close_all_fds() test with masked close_range().");
373 else
374 test_close_all_fds_inner();
375
376 _exit(EXIT_SUCCESS);
377 }
378 assert_se(r >= 0);
379
380 r = safe_fork("(caf-scnp)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
381 if (r == 0) {
382 r = seccomp_prohibit_close_range();
383 if (r < 0)
384 log_notice("Applying seccomp filter didn't work, skipping close_all_fds() test with masked close_range().");
385 else {
386 r = mount_nofollow_verbose(LOG_WARNING, "tmpfs", "/proc", "tmpfs", 0, NULL);
387 if (r < 0)
388 log_notice("Overmounting /proc/ didn't work, skipping close_all_fds() with masked /proc/.");
389 else
390 test_close_all_fds_inner();
391 }
392
393 test_close_all_fds_inner();
394 _exit(EXIT_SUCCESS);
395 }
396 assert_se(r >= 0);
397 }
398
399 TEST(format_proc_fd_path) {
400 ASSERT_STREQ(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0");
401 ASSERT_STREQ(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1");
402 ASSERT_STREQ(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2");
403 ASSERT_STREQ(FORMAT_PROC_FD_PATH(3), "/proc/self/fd/3");
404 ASSERT_STREQ(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647");
405 }
406
407 TEST(fd_reopen) {
408 _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF;
409 struct stat st1, st2;
410 int fl;
411
412 /* Test this with a directory */
413 fd1 = open("/proc", O_DIRECTORY|O_PATH|O_CLOEXEC);
414 assert_se(fd1 >= 0);
415
416 ASSERT_OK_ERRNO(fstat(fd1, &st1));
417 assert_se(S_ISDIR(st1.st_mode));
418
419 fl = fcntl(fd1, F_GETFL);
420 assert_se(fl >= 0);
421 assert_se(FLAGS_SET(fl, O_DIRECTORY));
422 assert_se(FLAGS_SET(fl, O_PATH));
423
424 /* fd_reopen() with O_NOFOLLOW will systematically fail, since it is implemented via a symlink in /proc/self/fd/ */
425 assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC|O_NOFOLLOW) == -ELOOP);
426 assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW) == -ELOOP);
427
428 fd2 = fd_reopen(fd1, O_RDONLY|O_DIRECTORY|O_CLOEXEC); /* drop the O_PATH */
429 assert_se(fd2 >= 0);
430
431 ASSERT_OK_ERRNO(fstat(fd2, &st2));
432 assert_se(S_ISDIR(st2.st_mode));
433 assert_se(stat_inode_same(&st1, &st2));
434
435 fl = fcntl(fd2, F_GETFL);
436 assert_se(fl >= 0);
437 assert_se(FLAGS_SET(fl, O_DIRECTORY));
438 assert_se(!FLAGS_SET(fl, O_PATH));
439
440 safe_close(fd1);
441
442 fd1 = fd_reopen(fd2, O_DIRECTORY|O_PATH|O_CLOEXEC); /* reacquire the O_PATH */
443 assert_se(fd1 >= 0);
444
445 ASSERT_OK_ERRNO(fstat(fd1, &st1));
446 assert_se(S_ISDIR(st1.st_mode));
447 assert_se(stat_inode_same(&st1, &st2));
448
449 fl = fcntl(fd1, F_GETFL);
450 assert_se(fl >= 0);
451 assert_se(FLAGS_SET(fl, O_DIRECTORY));
452 assert_se(FLAGS_SET(fl, O_PATH));
453
454 safe_close(fd1);
455
456 /* And now, test this with a file. */
457 fd1 = open("/proc/version", O_PATH|O_CLOEXEC);
458 assert_se(fd1 >= 0);
459
460 ASSERT_OK_ERRNO(fstat(fd1, &st1));
461 assert_se(S_ISREG(st1.st_mode));
462
463 fl = fcntl(fd1, F_GETFL);
464 assert_se(fl >= 0);
465 assert_se(!FLAGS_SET(fl, O_DIRECTORY));
466 assert_se(FLAGS_SET(fl, O_PATH));
467
468 assert_se(fd_reopen(fd1, O_RDONLY|O_DIRECTORY|O_CLOEXEC) == -ENOTDIR);
469 fd2 = fd_reopen(fd1, O_RDONLY|O_CLOEXEC); /* drop the O_PATH */
470 assert_se(fd2 >= 0);
471
472 ASSERT_OK_ERRNO(fstat(fd2, &st2));
473 assert_se(S_ISREG(st2.st_mode));
474 assert_se(stat_inode_same(&st1, &st2));
475
476 fl = fcntl(fd2, F_GETFL);
477 assert_se(fl >= 0);
478 assert_se(!FLAGS_SET(fl, O_DIRECTORY));
479 assert_se(!FLAGS_SET(fl, O_PATH));
480
481 safe_close(fd1);
482
483 assert_se(fd_reopen(fd2, O_DIRECTORY|O_PATH|O_CLOEXEC) == -ENOTDIR);
484 fd1 = fd_reopen(fd2, O_PATH|O_CLOEXEC); /* reacquire the O_PATH */
485 assert_se(fd1 >= 0);
486
487 ASSERT_OK_ERRNO(fstat(fd1, &st1));
488 assert_se(S_ISREG(st1.st_mode));
489 assert_se(stat_inode_same(&st1, &st2));
490
491 fl = fcntl(fd1, F_GETFL);
492 assert_se(fl >= 0);
493 assert_se(!FLAGS_SET(fl, O_DIRECTORY));
494 assert_se(FLAGS_SET(fl, O_PATH));
495
496 /* Also check the right error is generated if the fd is already closed */
497 safe_close(fd1);
498 assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC) == -EBADF);
499 fd1 = -EBADF;
500
501 /* Validate what happens if we reopen a symlink */
502 fd1 = open("/proc/self", O_PATH|O_CLOEXEC|O_NOFOLLOW);
503 assert_se(fd1 >= 0);
504 ASSERT_OK_ERRNO(fstat(fd1, &st1));
505 assert_se(S_ISLNK(st1.st_mode));
506
507 fd2 = fd_reopen(fd1, O_PATH|O_CLOEXEC);
508 assert_se(fd2 >= 0);
509 ASSERT_OK_ERRNO(fstat(fd2, &st2));
510 assert_se(S_ISLNK(st2.st_mode));
511 assert_se(stat_inode_same(&st1, &st2));
512 fd2 = safe_close(fd2);
513
514 /* So here's the thing: if we have an O_PATH fd to a symlink, we *cannot* convert it to a regular fd
515 * with that. i.e. you cannot have the VFS follow a symlink pinned via an O_PATH fd. */
516 assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC) == -ELOOP);
517 }
518
519 TEST(fd_reopen_condition) {
520 _cleanup_close_ int fd1 = -EBADF, fd3 = -EBADF;
521 int fd2, fl;
522
523 /* Open without O_PATH */
524 fd1 = open("/usr/", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
525 assert_se(fd1 >= 0);
526
527 fl = fcntl(fd1, F_GETFL);
528 assert_se(FLAGS_SET(fl, O_DIRECTORY));
529 assert_se(!FLAGS_SET(fl, O_PATH));
530
531 fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
532 assert_se(fd2 == fd1);
533 assert_se(fd3 < 0);
534
535 /* Switch on O_PATH */
536 fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3);
537 assert_se(fd2 != fd1);
538 assert_se(fd3 == fd2);
539
540 fl = fcntl(fd2, F_GETFL);
541 assert_se(FLAGS_SET(fl, O_DIRECTORY));
542 assert_se(FLAGS_SET(fl, O_PATH));
543
544 close_and_replace(fd1, fd3);
545
546 fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3);
547 assert_se(fd2 == fd1);
548 assert_se(fd3 < 0);
549
550 /* Switch off O_PATH again */
551 fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
552 assert_se(fd2 != fd1);
553 assert_se(fd3 == fd2);
554
555 fl = fcntl(fd2, F_GETFL);
556 assert_se(FLAGS_SET(fl, O_DIRECTORY));
557 assert_se(!FLAGS_SET(fl, O_PATH));
558
559 close_and_replace(fd1, fd3);
560
561 fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3);
562 assert_se(fd2 == fd1);
563 assert_se(fd3 < 0);
564 }
565
566 TEST(take_fd) {
567 _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF;
568 int array[2] = EBADF_PAIR, i = 0;
569
570 assert_se(fd1 == -EBADF);
571 assert_se(fd2 == -EBADF);
572
573 fd1 = eventfd(0, EFD_CLOEXEC);
574 assert_se(fd1 >= 0);
575
576 fd2 = TAKE_FD(fd1);
577 assert_se(fd1 == -EBADF);
578 assert_se(fd2 >= 0);
579
580 assert_se(array[0] == -EBADF);
581 assert_se(array[1] == -EBADF);
582
583 array[0] = TAKE_FD(fd2);
584 assert_se(fd1 == -EBADF);
585 assert_se(fd2 == -EBADF);
586 assert_se(array[0] >= 0);
587 assert_se(array[1] == -EBADF);
588
589 array[1] = TAKE_FD(array[i]);
590 assert_se(array[0] == -EBADF);
591 assert_se(array[1] >= 0);
592
593 i = 1 - i;
594 array[0] = TAKE_FD(*(array + i));
595 assert_se(array[0] >= 0);
596 assert_se(array[1] == -EBADF);
597
598 i = 1 - i;
599 fd1 = TAKE_FD(array[i]);
600 assert_se(fd1 >= 0);
601 assert_se(array[0] == -EBADF);
602 assert_se(array[1] == -EBADF);
603 }
604
605 TEST(dir_fd_is_root) {
606 _cleanup_close_ int fd = -EBADF;
607 int r;
608
609 assert_se(dir_fd_is_root_or_cwd(AT_FDCWD) > 0);
610
611 assert_se((fd = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
612 assert_se(dir_fd_is_root(fd) > 0);
613 assert_se(dir_fd_is_root_or_cwd(fd) > 0);
614
615 fd = safe_close(fd);
616
617 assert_se((fd = open("/usr", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
618 assert_se(dir_fd_is_root(fd) == 0);
619 assert_se(dir_fd_is_root_or_cwd(fd) == 0);
620
621 r = detach_mount_namespace();
622 if (r < 0)
623 return (void) log_tests_skipped_errno(r, "Failed to detach mount namespace");
624
625 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
626 _cleanup_free_ char *x = NULL, *y = NULL;
627
628 assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);
629 assert_se(x = path_join(tmp, "x"));
630 assert_se(y = path_join(tmp, "x/y"));
631 assert_se(mkdir_p(y, 0755) >= 0);
632 assert_se(mount_nofollow_verbose(LOG_DEBUG, x, y, NULL, MS_BIND, NULL) >= 0);
633
634 fd = safe_close(fd);
635
636 assert_se((fd = open(tmp, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
637 assert_se(dir_fd_is_root(fd) == 0);
638 assert_se(dir_fd_is_root_or_cwd(fd) == 0);
639
640 fd = safe_close(fd);
641
642 assert_se((fd = open(x, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
643 assert_se(dir_fd_is_root(fd) == 0);
644 assert_se(dir_fd_is_root_or_cwd(fd) == 0);
645
646 fd = safe_close(fd);
647
648 assert_se((fd = open(y, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
649 assert_se(dir_fd_is_root(fd) == 0);
650 assert_se(dir_fd_is_root_or_cwd(fd) == 0);
651 }
652
653 TEST(fds_are_same_mount) {
654 _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF, fd3 = -EBADF, fd4 = -EBADF;
655
656 fd1 = open("/sys", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW);
657 fd2 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW);
658 fd3 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW);
659 fd4 = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW);
660
661 if (fd1 < 0 || fd2 < 0 || fd3 < 0 || fd4 < 0)
662 return (void) log_tests_skipped_errno(errno, "Failed to open /sys or /proc or /");
663
664 if (fds_are_same_mount(fd1, fd4) > 0 && fds_are_same_mount(fd2, fd4) > 0)
665 return (void) log_tests_skipped("Cannot test fds_are_same_mount() as /sys and /proc are not mounted");
666
667 assert_se(fds_are_same_mount(fd1, fd2) == 0);
668 assert_se(fds_are_same_mount(fd2, fd3) > 0);
669 }
670
671 TEST(fd_get_path) {
672 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
673 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
674 _cleanup_free_ char *p = NULL, *q = NULL, *saved_cwd = NULL;
675
676 tfd = mkdtemp_open(NULL, O_PATH, &t);
677 assert_se(tfd >= 0);
678 assert_se(fd_get_path(tfd, &p) >= 0);
679 ASSERT_STREQ(p, t);
680
681 p = mfree(p);
682
683 assert_se(safe_getcwd(&saved_cwd) >= 0);
684 assert_se(chdir(t) >= 0);
685
686 assert_se(fd_get_path(AT_FDCWD, &p) >= 0);
687 ASSERT_STREQ(p, t);
688
689 p = mfree(p);
690
691 assert_se(q = path_join(t, "regular"));
692 assert_se(touch(q) >= 0);
693 assert_se(mkdirat_parents(tfd, "subdir/symlink", 0755) >= 0);
694 assert_se(symlinkat("../regular", tfd, "subdir/symlink") >= 0);
695 assert_se(symlinkat("subdir", tfd, "symdir") >= 0);
696
697 fd = openat(tfd, "regular", O_CLOEXEC|O_PATH);
698 assert_se(fd >= 0);
699 assert_se(fd_get_path(fd, &p) >= 0);
700 ASSERT_STREQ(p, q);
701
702 p = mfree(p);
703 fd = safe_close(fd);
704
705 fd = openat(AT_FDCWD, "regular", O_CLOEXEC|O_PATH);
706 assert_se(fd >= 0);
707 assert_se(fd_get_path(fd, &p) >= 0);
708 ASSERT_STREQ(p, q);
709
710 p = mfree(p);
711 fd = safe_close(fd);
712
713 fd = openat(tfd, "subdir/symlink", O_CLOEXEC|O_PATH);
714 assert_se(fd >= 0);
715 assert_se(fd_verify_regular(fd) >= 0);
716 assert_se(fd_get_path(fd, &p) >= 0);
717 ASSERT_STREQ(p, q);
718
719 p = mfree(p);
720 fd = safe_close(fd);
721
722 fd = openat(AT_FDCWD, "subdir/symlink", O_CLOEXEC|O_PATH);
723 assert_se(fd >= 0);
724 assert_se(fd_verify_regular(fd) >= 0);
725 assert_se(fd_get_path(fd, &p) >= 0);
726 ASSERT_STREQ(p, q);
727
728 p = mfree(p);
729 fd = safe_close(fd);
730
731 fd = openat(tfd, "symdir//./symlink", O_CLOEXEC|O_PATH);
732 assert_se(fd >= 0);
733 assert_se(fd_verify_regular(fd) >= 0);
734 assert_se(fd_get_path(fd, &p) >= 0);
735 ASSERT_STREQ(p, q);
736
737 p = mfree(p);
738 fd = safe_close(fd);
739
740 fd = openat(AT_FDCWD, "symdir//./symlink", O_CLOEXEC|O_PATH);
741 assert_se(fd >= 0);
742 assert_se(fd_verify_regular(fd) >= 0);
743 assert_se(fd_get_path(fd, &p) >= 0);
744 ASSERT_STREQ(p, q);
745
746 p = mfree(p);
747 q = mfree(q);
748 fd = safe_close(fd);
749
750 assert_se(q = path_join(t, "subdir/symlink"));
751 fd = openat(tfd, "subdir/symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW);
752 assert_se(fd >= 0);
753 assert_se(fd_verify_regular(fd) == -ELOOP);
754 assert_se(fd_get_path(fd, &p) >= 0);
755 ASSERT_STREQ(p, q);
756
757 p = mfree(p);
758 fd = safe_close(fd);
759
760 fd = openat(AT_FDCWD, "subdir/symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW);
761 assert_se(fd >= 0);
762 assert_se(fd_verify_regular(fd) == -ELOOP);
763 assert_se(fd_get_path(fd, &p) >= 0);
764 ASSERT_STREQ(p, q);
765
766 p = mfree(p);
767 fd = safe_close(fd);
768
769 fd = openat(tfd, "symdir//./symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW);
770 assert_se(fd >= 0);
771 assert_se(fd_verify_regular(fd) == -ELOOP);
772 assert_se(fd_get_path(fd, &p) >= 0);
773 ASSERT_STREQ(p, q);
774
775 p = mfree(p);
776 fd = safe_close(fd);
777
778 fd = openat(AT_FDCWD, "symdir//./symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW);
779 assert_se(fd >= 0);
780 assert_se(fd_verify_regular(fd) == -ELOOP);
781 assert_se(fd_get_path(fd, &p) >= 0);
782 ASSERT_STREQ(p, q);
783
784 assert_se(chdir(saved_cwd) >= 0);
785 }
786
787 DEFINE_TEST_MAIN(LOG_DEBUG);