+
+ struct landlock_ruleset_attr ruleset_attr = {
+ .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
+ LANDLOCK_ACCESS_NET_CONNECT_TCP,
+ };
+ struct landlock_net_service_attr net_service_1 = {
+ .allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
+ LANDLOCK_ACCESS_NET_CONNECT_TCP,
+ .port = port[0],
+ };
+ struct landlock_net_service_attr net_service_2 = {
+ .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+ .port = port[1],
+ };
+ struct landlock_net_service_attr net_service_3 = {
+ .allowed_access = 0,
+ .port = port[2],
+ };
+
+ const int ruleset_fd = landlock_create_ruleset(&ruleset_attr,
+ sizeof(ruleset_attr), 0);
+ ASSERT_LE(0, ruleset_fd);
+
+ /* Allows connect and bind operations to the port[0] socket. */
+ ASSERT_EQ(0, landlock_add_rule(ruleset_fd,
LANDLOCK_RULE_NET_SERVICE,
+ &net_service_1, 0));
+ /* Allows connect and deny bind operations to the port[1]
socket. */
+ ASSERT_EQ(0, landlock_add_rule(ruleset_fd,
LANDLOCK_RULE_NET_SERVICE,
+ &net_service_2, 0));
+ /* Empty allowed_access (i.e. deny rules) are ignored in network
actions
+ * for port[2] socket.
+ */
+ ASSERT_EQ(-1, landlock_add_rule(ruleset_fd,
LANDLOCK_RULE_NET_SERVICE,
+ &net_service_3, 0));
+ ASSERT_EQ(ENOMSG, errno);
+
+ /* Enforces the ruleset. */
+ enforce_ruleset(_metadata, ruleset_fd);
+
+ sockfd_1 = create_socket(_metadata);
+ ASSERT_LE(0, sockfd_1);
+ /* Binds a socket to port[0] */
+ ASSERT_EQ(0, bind(sockfd_1, (struct sockaddr *)&addr[0],
sizeof(addr[0])));
+
+ /* Close bounded socket*/
+ ASSERT_EQ(0, close(sockfd_1));
+
+ sockfd_2 = create_socket(_metadata);
+ ASSERT_LE(0, sockfd_2);
+ /* Binds a socket to port[1] */
+ ASSERT_EQ(-1, bind(sockfd_2, (struct sockaddr *)&addr[1],
sizeof(addr[1])));
+ ASSERT_EQ(EACCES, errno);
+
+ sockfd_3 = create_socket(_metadata);
+ ASSERT_LE(0, sockfd_3);
+ /* Binds a socket to port[2] */
+ ASSERT_EQ(-1, bind(sockfd_3, (struct sockaddr *)&addr[2],
sizeof(addr[2])));
+ ASSERT_EQ(EACCES, errno);
+}
+TEST_HARNESS_MAIN
--
2.25.1