Skip to content

Commit f12f8f8

Browse files
committed
selftests/landlock: Add tests for FS topology changes with network rules
Add 2 tests to the layout1 fixture: * topology_changes_with_net_only: Checks that FS topology changes are not denied by network-only restrictions. * topology_changes_with_net_and_fs: Make sure that FS topology changes are still denied with FS and network restrictions. This specifically test commit d722036 ("landlock: Allow FS topology changes for domains without such rule type"). Cc: Konstantin Meskhidze <konstantin.meskhidze@huawei.com> Link: https://lore.kernel.org/r/20231027154615.815134-1-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 51442e8 commit f12f8f8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,65 @@ TEST_F_FORK(layout1, move_mount)
16251625
clear_cap(_metadata, CAP_SYS_ADMIN);
16261626
}
16271627

1628+
TEST_F_FORK(layout1, topology_changes_with_net_only)
1629+
{
1630+
const struct landlock_ruleset_attr ruleset_net = {
1631+
.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
1632+
LANDLOCK_ACCESS_NET_CONNECT_TCP,
1633+
};
1634+
int ruleset_fd;
1635+
1636+
/* Add network restrictions. */
1637+
ruleset_fd =
1638+
landlock_create_ruleset(&ruleset_net, sizeof(ruleset_net), 0);
1639+
ASSERT_LE(0, ruleset_fd);
1640+
enforce_ruleset(_metadata, ruleset_fd);
1641+
ASSERT_EQ(0, close(ruleset_fd));
1642+
1643+
/* Mount, remount, move_mount, umount, and pivot_root checks. */
1644+
set_cap(_metadata, CAP_SYS_ADMIN);
1645+
ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s1d2));
1646+
ASSERT_EQ(0, mount(NULL, dir_s1d2, NULL, MS_PRIVATE | MS_REC, NULL));
1647+
ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD,
1648+
dir_s2d2, 0));
1649+
ASSERT_EQ(0, umount(dir_s2d2));
1650+
ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
1651+
ASSERT_EQ(0, chdir("/"));
1652+
clear_cap(_metadata, CAP_SYS_ADMIN);
1653+
}
1654+
1655+
TEST_F_FORK(layout1, topology_changes_with_net_and_fs)
1656+
{
1657+
const struct landlock_ruleset_attr ruleset_net_fs = {
1658+
.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
1659+
LANDLOCK_ACCESS_NET_CONNECT_TCP,
1660+
.handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE,
1661+
};
1662+
int ruleset_fd;
1663+
1664+
/* Add network and filesystem restrictions. */
1665+
ruleset_fd = landlock_create_ruleset(&ruleset_net_fs,
1666+
sizeof(ruleset_net_fs), 0);
1667+
ASSERT_LE(0, ruleset_fd);
1668+
enforce_ruleset(_metadata, ruleset_fd);
1669+
ASSERT_EQ(0, close(ruleset_fd));
1670+
1671+
/* Mount, remount, move_mount, umount, and pivot_root checks. */
1672+
set_cap(_metadata, CAP_SYS_ADMIN);
1673+
ASSERT_EQ(-1, mount_opt(&mnt_tmp, dir_s1d2));
1674+
ASSERT_EQ(EPERM, errno);
1675+
ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_PRIVATE | MS_REC, NULL));
1676+
ASSERT_EQ(EPERM, errno);
1677+
ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
1678+
dir_s2d2, 0));
1679+
ASSERT_EQ(EPERM, errno);
1680+
ASSERT_EQ(-1, umount(dir_s3d2));
1681+
ASSERT_EQ(EPERM, errno);
1682+
ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
1683+
ASSERT_EQ(EPERM, errno);
1684+
clear_cap(_metadata, CAP_SYS_ADMIN);
1685+
}
1686+
16281687
TEST_F_FORK(layout1, release_inodes)
16291688
{
16301689
const struct rule rules[] = {

0 commit comments

Comments
 (0)