summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/filesystems/mntns_cleanup/mntns_cleanup_test.c
blob: 5209712568b1756b5ea1a12d0660a5c90dd74d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: GPL-2.0

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>

#include "../../kselftest_harness.h"

FIXTURE(mntns_cleanup) {
};

FIXTURE_SETUP(mntns_cleanup)
{
	if (geteuid() != 0)
		SKIP(return, "test requires CAP_SYS_ADMIN");

	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
	ASSERT_EQ(mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);

	rmdir("/mnt_dir");
	ASSERT_EQ(mkdir("/mnt_dir", 0755), 0);
	ASSERT_EQ(mount("tmpfs", "/mnt_dir", "tmpfs", 0, NULL), 0);
	ASSERT_EQ(mkdir("/mnt_dir/hidden", 0755), 0);
	ASSERT_EQ(mkdir("/mnt_dir/hidden/secret", 0755), 0);
	ASSERT_EQ(mount("tmpfs", "/mnt_dir/hidden", "tmpfs", 0, NULL), 0);
}

FIXTURE_TEARDOWN(mntns_cleanup)
{
}

/* Mounts must stay connected when a mount namespace is cleaned up. */
TEST_F(mntns_cleanup, keeps_mounts_connected)
{
	int fd, sfd, err;

	fd = open("/mnt_dir", O_PATH | O_DIRECTORY | O_CLOEXEC);
	ASSERT_GE(fd, 0);

	/* Destroy the namespace; the fd keeps /mnt_dir alive. */
	ASSERT_EQ(unshare(CLONE_NEWNS), 0);

	sfd = openat(fd, "hidden/secret", O_RDONLY);
	err = errno;
	if (sfd >= 0)
		close(sfd);
	close(fd);

	ASSERT_LT(sfd, 0)
		TH_LOG("mount namespace teardown revealed what the overmount covered");
	ASSERT_EQ(err, ENOENT);
}

TEST_HARNESS_MAIN