Skip to content

Commit e2780a0

Browse files
committed
selftests/landlock: Add tests to check unhandled rule's access rights
Add two tests to make sure that we cannot add a rule to a ruleset if the rule's access rights that are not handled by the ruleset: * fs: layout1.rule_with_unhandled_access * net: mini.rule_with_unhandled_access Cc: Konstantin Meskhidze <konstantin.meskhidze@huawei.com> Reviewed-by: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20231130093616.67340-3-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 6471c9c commit e2780a0

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

tools/testing/selftests/landlock/fs_test.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,40 @@ TEST_F_FORK(layout0, rule_with_unknown_access)
632632
ASSERT_EQ(0, close(ruleset_fd));
633633
}
634634

635+
TEST_F_FORK(layout1, rule_with_unhandled_access)
636+
{
637+
struct landlock_ruleset_attr ruleset_attr = {
638+
.handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE,
639+
};
640+
struct landlock_path_beneath_attr path_beneath = {};
641+
int ruleset_fd;
642+
__u64 access;
643+
644+
ruleset_fd =
645+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
646+
ASSERT_LE(0, ruleset_fd);
647+
648+
path_beneath.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC);
649+
ASSERT_LE(0, path_beneath.parent_fd);
650+
651+
for (access = 1; access > 0; access <<= 1) {
652+
int err;
653+
654+
path_beneath.allowed_access = access;
655+
err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
656+
&path_beneath, 0);
657+
if (access == ruleset_attr.handled_access_fs) {
658+
EXPECT_EQ(0, err);
659+
} else {
660+
EXPECT_EQ(-1, err);
661+
EXPECT_EQ(EINVAL, errno);
662+
}
663+
}
664+
665+
EXPECT_EQ(0, close(path_beneath.parent_fd));
666+
EXPECT_EQ(0, close(ruleset_fd));
667+
}
668+
635669
static void add_path_beneath(struct __test_metadata *const _metadata,
636670
const int ruleset_fd, const __u64 allowed_access,
637671
const char *const path)

tools/testing/selftests/landlock/net_test.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,38 @@ TEST_F(mini, rule_with_unknown_access)
13011301
EXPECT_EQ(0, close(ruleset_fd));
13021302
}
13031303

1304+
TEST_F(mini, rule_with_unhandled_access)
1305+
{
1306+
struct landlock_ruleset_attr ruleset_attr = {
1307+
.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP,
1308+
};
1309+
struct landlock_net_port_attr net_port = {
1310+
.port = sock_port_start,
1311+
};
1312+
int ruleset_fd;
1313+
__u64 access;
1314+
1315+
ruleset_fd =
1316+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
1317+
ASSERT_LE(0, ruleset_fd);
1318+
1319+
for (access = 1; access > 0; access <<= 1) {
1320+
int err;
1321+
1322+
net_port.allowed_access = access;
1323+
err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
1324+
&net_port, 0);
1325+
if (access == ruleset_attr.handled_access_net) {
1326+
EXPECT_EQ(0, err);
1327+
} else {
1328+
EXPECT_EQ(-1, err);
1329+
EXPECT_EQ(EINVAL, errno);
1330+
}
1331+
}
1332+
1333+
EXPECT_EQ(0, close(ruleset_fd));
1334+
}
1335+
13041336
TEST_F(mini, inval)
13051337
{
13061338
const struct landlock_ruleset_attr ruleset_attr = {

0 commit comments

Comments
 (0)