]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add a testcase that dir_fd_is_root() is not confused by bind mount
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Mar 2023 19:50:08 +0000 (04:50 +0900)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 17 Mar 2023 08:49:21 +0000 (09:49 +0100)
See the comment in dir_fd_is_root() and
https://github.com/systemd/systemd/pull/26820#issuecomment-1469554966.

src/test/test-fd-util.c

index c5f32aa6792c543c7fd3a86dddee0a85566b3113..1f14f0b6cb4b85d78e7a758eba1cfd5314b6a069 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <fcntl.h>
 #include <sys/eventfd.h>
+#include <sys/mount.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
 #include "macro.h"
 #include "memory-util.h"
 #include "missing_syscall.h"
+#include "mkdir.h"
 #include "mount-util.h"
+#include "namespace-util.h"
 #include "path-util.h"
 #include "process-util.h"
 #include "random-util.h"
 #include "rlimit-util.h"
+#include "rm-rf.h"
 #include "seccomp-util.h"
 #include "serialize.h"
 #include "string-util.h"
@@ -571,6 +575,7 @@ TEST(take_fd) {
 
 TEST(dir_fd_is_root) {
         _cleanup_close_ int fd = -EBADF;
+        int r;
 
         assert_se((fd = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
         assert_se(dir_fd_is_root(fd) > 0);
@@ -579,6 +584,34 @@ TEST(dir_fd_is_root) {
 
         assert_se((fd = open("/usr", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
         assert_se(dir_fd_is_root(fd) == 0);
+
+        r = detach_mount_namespace();
+        if (r < 0)
+                return (void) log_tests_skipped_errno(r, "Failed to detach mount namespace");
+
+        _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
+        _cleanup_free_ char *x = NULL, *y = NULL;
+
+        assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);
+        assert_se(x = path_join(tmp, "x"));
+        assert_se(y = path_join(tmp, "x/y"));
+        assert_se(mkdir_p(y, 0755) >= 0);
+        assert_se(mount_nofollow_verbose(LOG_DEBUG, x, y, NULL, MS_BIND, NULL) >= 0);
+
+        fd = safe_close(fd);
+
+        assert_se((fd = open(tmp, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
+        assert_se(dir_fd_is_root(fd) == 0);
+
+        fd = safe_close(fd);
+
+        assert_se((fd = open(x, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
+        assert_se(dir_fd_is_root(fd) == 0);
+
+        fd = safe_close(fd);
+
+        assert_se((fd = open(y, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0);
+        assert_se(dir_fd_is_root(fd) == 0);
 }
 
 DEFINE_TEST_MAIN(LOG_DEBUG);