1
0
mirror of https://github.com/systemd/systemd.git synced 2025-08-03 08:22:21 +03:00

test: add a testcase that dir_fd_is_root() is not confused by bind mount

See the comment in dir_fd_is_root() and
https://github.com/systemd/systemd/pull/26820#issuecomment-1469554966.
This commit is contained in:
Yu Watanabe
2023-03-16 04:50:08 +09:00
committed by Daan De Meyer
parent 77c6bb895a
commit 58dfcc57f2

View File

@ -2,6 +2,7 @@
#include <fcntl.h>
#include <sys/eventfd.h>
#include <sys/mount.h>
#include <unistd.h>
#include "alloc-util.h"
@ -11,11 +12,14 @@
#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);