Add test that validates behaviour of Landlock after rule of LANDLOCK_RULE_SOCKET type is added * with all possible access rights; * with unknown access rights; * with unhandled access rights; * with empty access. Signed-off-by: Mikhail Ivanov --- Changes since v3: * Squashes commits related to checking rule adding * Removes "protocol" fixture dependency. Use single TCP protocol for testing instead. * Fixes semantics of test "rule_with_unhandled_access". Changes since v2: * Replaces EXPECT_EQ with ASSERT_EQ for close(). * Refactors commit message and title. Changes since v1: * Formats code with clang-format. * Refactors commit message. --- .../testing/selftests/landlock/socket_test.c | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/tools/testing/selftests/landlock/socket_test.c b/tools/testing/selftests/landlock/socket_test.c index d5716149d03f..fcc0f92075a7 100644 --- a/tools/testing/selftests/landlock/socket_test.c +++ b/tools/testing/selftests/landlock/socket_test.c @@ -44,4 +44,111 @@ TEST_F(mini, ruleset_with_unknown_access) } } +TEST_F(mini, rule_with_supported_access) +{ + const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_socket = ACCESS_ALL, + }; + struct landlock_socket_attr protocol = { + .family = AF_INET, + .type = SOCK_STREAM, + .protocol = 0, + }; + int ruleset_fd; + __u64 access; + + ruleset_fd = + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + ASSERT_LE(0, ruleset_fd); + + for (access = 1; access <= ACCESS_LAST; access <<= 1) { + protocol.allowed_access = access; + EXPECT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET, + &protocol, 0)) + { + TH_LOG("Failed to add rule with access 0x%llx: %s", + access, strerror(errno)); + } + } + ASSERT_EQ(0, close(ruleset_fd)); +} + +TEST_F(mini, rule_with_unknown_access) +{ + const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_socket = ACCESS_ALL, + }; + struct landlock_socket_attr protocol = { .family = AF_INET, + .type = SOCK_STREAM, + .protocol = 0 }; + int ruleset_fd; + __u64 access; + + ruleset_fd = + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + ASSERT_LE(0, ruleset_fd); + + for (access = 1ULL << 63; access != ACCESS_LAST; access >>= 1) { + protocol.allowed_access = access; + EXPECT_EQ(-1, + landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET, + &protocol, 0)); + EXPECT_EQ(EINVAL, errno); + } + ASSERT_EQ(0, close(ruleset_fd)); +} + +TEST_F(mini, rule_with_unhandled_access) +{ + /* Prepares ruleset that handles network access instead of socket access. */ + const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP, + }; + struct landlock_socket_attr protocol = { .family = AF_UNIX, + .type = SOCK_STREAM, + .protocol = 0 }; + int ruleset_fd; + __u64 access; + + ruleset_fd = + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + ASSERT_LE(0, ruleset_fd); + + for (access = ACCESS_LAST; access > 0; access <<= 1) { + int err; + + protocol.allowed_access = access; + err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET, + &protocol, 0); + EXPECT_EQ(-1, err); + EXPECT_EQ(EINVAL, errno); + } + + ASSERT_EQ(0, close(ruleset_fd)); +} + +TEST_F(mini, rule_with_empty_access) +{ + const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_socket = LANDLOCK_ACCESS_SOCKET_CREATE + }; + const struct landlock_socket_attr protocol_denied = { + .allowed_access = 0, + .family = AF_UNIX, + .type = SOCK_STREAM, + .protocol = 0, + }; + int ruleset_fd; + + ruleset_fd = + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + ASSERT_LE(0, ruleset_fd); + + /* Checks zero access value. */ + EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET, + &protocol_denied, 0)); + EXPECT_EQ(ENOMSG, errno); + ASSERT_EQ(0, close(ruleset_fd)); +} + TEST_HARNESS_MAIN -- 2.34.1