Skip to content

Commit e5c10b1

Browse files
committed
selftests: test subdirectory mounting
This tests mounting a subdirectory without ever having to expose the filesystem to a non-anonymous mount namespace. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 12e40cb commit e5c10b1

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

tools/testing/selftests/mount_setattr/mount_setattr_test.c

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdarg.h>
2121
#include <linux/mount.h>
2222

23+
#include "../filesystems/overlayfs/wrappers.h"
2324
#include "../kselftest_harness.h"
2425

2526
#ifndef CLONE_NEWNS
@@ -177,51 +178,6 @@ static inline int sys_open_tree(int dfd, const char *filename, unsigned int flag
177178
return syscall(__NR_open_tree, dfd, filename, flags);
178179
}
179180

180-
/* move_mount() flags */
181-
#ifndef MOVE_MOUNT_F_SYMLINKS
182-
#define MOVE_MOUNT_F_SYMLINKS 0x00000001 /* Follow symlinks on from path */
183-
#endif
184-
185-
#ifndef MOVE_MOUNT_F_AUTOMOUNTS
186-
#define MOVE_MOUNT_F_AUTOMOUNTS 0x00000002 /* Follow automounts on from path */
187-
#endif
188-
189-
#ifndef MOVE_MOUNT_F_EMPTY_PATH
190-
#define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */
191-
#endif
192-
193-
#ifndef MOVE_MOUNT_T_SYMLINKS
194-
#define MOVE_MOUNT_T_SYMLINKS 0x00000010 /* Follow symlinks on to path */
195-
#endif
196-
197-
#ifndef MOVE_MOUNT_T_AUTOMOUNTS
198-
#define MOVE_MOUNT_T_AUTOMOUNTS 0x00000020 /* Follow automounts on to path */
199-
#endif
200-
201-
#ifndef MOVE_MOUNT_T_EMPTY_PATH
202-
#define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
203-
#endif
204-
205-
#ifndef MOVE_MOUNT_SET_GROUP
206-
#define MOVE_MOUNT_SET_GROUP 0x00000100 /* Set sharing group instead */
207-
#endif
208-
209-
#ifndef MOVE_MOUNT_BENEATH
210-
#define MOVE_MOUNT_BENEATH 0x00000200 /* Mount beneath top mount */
211-
#endif
212-
213-
#ifndef MOVE_MOUNT__MASK
214-
#define MOVE_MOUNT__MASK 0x00000377
215-
#endif
216-
217-
static inline int sys_move_mount(int from_dfd, const char *from_pathname,
218-
int to_dfd, const char *to_pathname,
219-
unsigned int flags)
220-
{
221-
return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd,
222-
to_pathname, flags);
223-
}
224-
225181
static ssize_t write_nointr(int fd, const void *buf, size_t count)
226182
{
227183
ssize_t ret;
@@ -1789,6 +1745,41 @@ TEST_F(mount_setattr, open_tree_detached_fail3)
17891745
ASSERT_EQ(errno, EINVAL);
17901746
}
17911747

1748+
TEST_F(mount_setattr, open_tree_subfolder)
1749+
{
1750+
int fd_context, fd_tmpfs, fd_tree;
1751+
1752+
fd_context = sys_fsopen("tmpfs", 0);
1753+
ASSERT_GE(fd_context, 0);
1754+
1755+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
1756+
1757+
fd_tmpfs = sys_fsmount(fd_context, 0, 0);
1758+
ASSERT_GE(fd_tmpfs, 0);
1759+
1760+
EXPECT_EQ(close(fd_context), 0);
1761+
1762+
ASSERT_EQ(mkdirat(fd_tmpfs, "subdir", 0755), 0);
1763+
1764+
fd_tree = sys_open_tree(fd_tmpfs, "subdir",
1765+
AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
1766+
AT_RECURSIVE | OPEN_TREE_CLOEXEC |
1767+
OPEN_TREE_CLONE);
1768+
ASSERT_GE(fd_tree, 0);
1769+
1770+
EXPECT_EQ(close(fd_tmpfs), 0);
1771+
1772+
ASSERT_EQ(mkdirat(-EBADF, "/mnt/open_tree_subfolder", 0755), 0);
1773+
1774+
ASSERT_EQ(sys_move_mount(fd_tree, "", -EBADF, "/mnt/open_tree_subfolder", MOVE_MOUNT_F_EMPTY_PATH), 0);
1775+
1776+
EXPECT_EQ(close(fd_tree), 0);
1777+
1778+
ASSERT_EQ(umount2("/mnt/open_tree_subfolder", 0), 0);
1779+
1780+
EXPECT_EQ(rmdir("/mnt/open_tree_subfolder"), 0);
1781+
}
1782+
17921783
TEST_F(mount_setattr, mount_detached_mount_on_detached_mount_then_close)
17931784
{
17941785
int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;

0 commit comments

Comments
 (0)