1 // SPDX-License-Identifier: GPL-2.0-only
3 * Landlock tests - Network
5 * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
6 * Copyright © 2023 Microsoft Corporation
10 #include <arpa/inet.h>
13 #include <linux/landlock.h>
18 #include <sys/prctl.h>
19 #include <sys/socket.h>
20 #include <sys/syscall.h>
25 const short sock_port_start
= (1 << 10);
27 static const char loopback_ipv4
[] = "127.0.0.1";
28 static const char loopback_ipv6
[] = "::1";
30 /* Number pending connections queue to be hold. */
31 const short backlog
= 10;
35 /* This may be used to test rules that allow *and* deny accesses. */
39 static int set_service(struct service_fixture
*const srv
,
40 const struct protocol_variant prot
,
41 const unsigned short index
)
43 memset(srv
, 0, sizeof(*srv
));
46 * Copies all protocol properties in case of the variant only contains
51 /* Checks for port overflow. */
54 srv
->port
= sock_port_start
<< (2 * index
);
56 switch (prot
.domain
) {
59 srv
->ipv4_addr
.sin_family
= prot
.domain
;
60 srv
->ipv4_addr
.sin_port
= htons(srv
->port
);
61 srv
->ipv4_addr
.sin_addr
.s_addr
= inet_addr(loopback_ipv4
);
65 srv
->ipv6_addr
.sin6_family
= prot
.domain
;
66 srv
->ipv6_addr
.sin6_port
= htons(srv
->port
);
67 inet_pton(AF_INET6
, loopback_ipv6
, &srv
->ipv6_addr
.sin6_addr
);
71 set_unix_address(srv
, index
);
77 static void setup_loopback(struct __test_metadata
*const _metadata
)
79 set_cap(_metadata
, CAP_SYS_ADMIN
);
80 ASSERT_EQ(0, unshare(CLONE_NEWNET
));
81 clear_cap(_metadata
, CAP_SYS_ADMIN
);
83 set_ambient_cap(_metadata
, CAP_NET_ADMIN
);
84 ASSERT_EQ(0, system("ip link set dev lo up"));
85 clear_ambient_cap(_metadata
, CAP_NET_ADMIN
);
88 static bool is_restricted(const struct protocol_variant
*const prot
,
89 const enum sandbox_type sandbox
)
91 switch (prot
->domain
) {
96 return sandbox
== TCP_SANDBOX
;
103 static int socket_variant(const struct service_fixture
*const srv
)
107 ret
= socket(srv
->protocol
.domain
, srv
->protocol
.type
| SOCK_CLOEXEC
,
114 #ifndef SIN6_LEN_RFC2133
115 #define SIN6_LEN_RFC2133 24
118 static socklen_t
get_addrlen(const struct service_fixture
*const srv
,
121 switch (srv
->protocol
.domain
) {
124 return sizeof(srv
->ipv4_addr
);
128 return SIN6_LEN_RFC2133
;
129 return sizeof(srv
->ipv6_addr
);
133 return sizeof(srv
->unix_addr
) -
134 sizeof(srv
->unix_addr
.sun_path
);
135 return srv
->unix_addr_len
;
142 static void set_port(struct service_fixture
*const srv
, uint16_t port
)
144 switch (srv
->protocol
.domain
) {
147 srv
->ipv4_addr
.sin_port
= htons(port
);
151 srv
->ipv6_addr
.sin6_port
= htons(port
);
159 static uint16_t get_binded_port(int socket_fd
,
160 const struct protocol_variant
*const prot
)
162 struct sockaddr_in ipv4_addr
;
163 struct sockaddr_in6 ipv6_addr
;
164 socklen_t ipv4_addr_len
, ipv6_addr_len
;
166 /* Gets binded port. */
167 switch (prot
->domain
) {
170 ipv4_addr_len
= sizeof(ipv4_addr
);
171 getsockname(socket_fd
, &ipv4_addr
, &ipv4_addr_len
);
172 return ntohs(ipv4_addr
.sin_port
);
175 ipv6_addr_len
= sizeof(ipv6_addr
);
176 getsockname(socket_fd
, &ipv6_addr
, &ipv6_addr_len
);
177 return ntohs(ipv6_addr
.sin6_port
);
184 static int bind_variant_addrlen(const int sock_fd
,
185 const struct service_fixture
*const srv
,
186 const socklen_t addrlen
)
190 switch (srv
->protocol
.domain
) {
193 ret
= bind(sock_fd
, &srv
->ipv4_addr
, addrlen
);
197 ret
= bind(sock_fd
, &srv
->ipv6_addr
, addrlen
);
201 ret
= bind(sock_fd
, &srv
->unix_addr
, addrlen
);
205 errno
= EAFNOSUPPORT
;
214 static int bind_variant(const int sock_fd
,
215 const struct service_fixture
*const srv
)
217 return bind_variant_addrlen(sock_fd
, srv
, get_addrlen(srv
, false));
220 static int connect_variant_addrlen(const int sock_fd
,
221 const struct service_fixture
*const srv
,
222 const socklen_t addrlen
)
226 switch (srv
->protocol
.domain
) {
229 ret
= connect(sock_fd
, &srv
->ipv4_addr
, addrlen
);
233 ret
= connect(sock_fd
, &srv
->ipv6_addr
, addrlen
);
237 ret
= connect(sock_fd
, &srv
->unix_addr
, addrlen
);
241 errno
= -EAFNOSUPPORT
;
250 static int connect_variant(const int sock_fd
,
251 const struct service_fixture
*const srv
)
253 return connect_variant_addrlen(sock_fd
, srv
, get_addrlen(srv
, false));
258 struct service_fixture srv0
, srv1
, srv2
, unspec_any0
, unspec_srv0
;
261 FIXTURE_VARIANT(protocol
)
263 const enum sandbox_type sandbox
;
264 const struct protocol_variant prot
;
267 FIXTURE_SETUP(protocol
)
269 const struct protocol_variant prot_unspec
= {
274 disable_caps(_metadata
);
276 ASSERT_EQ(0, set_service(&self
->srv0
, variant
->prot
, 0));
277 ASSERT_EQ(0, set_service(&self
->srv1
, variant
->prot
, 1));
278 ASSERT_EQ(0, set_service(&self
->srv2
, variant
->prot
, 2));
280 ASSERT_EQ(0, set_service(&self
->unspec_srv0
, prot_unspec
, 0));
282 ASSERT_EQ(0, set_service(&self
->unspec_any0
, prot_unspec
, 0));
283 self
->unspec_any0
.ipv4_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
285 setup_loopback(_metadata
);
288 FIXTURE_TEARDOWN(protocol
)
292 /* clang-format off */
293 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_ipv4_tcp
) {
294 /* clang-format on */
295 .sandbox
= NO_SANDBOX
,
302 /* clang-format off */
303 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_ipv6_tcp
) {
304 /* clang-format on */
305 .sandbox
= NO_SANDBOX
,
312 /* clang-format off */
313 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_ipv4_udp
) {
314 /* clang-format on */
315 .sandbox
= NO_SANDBOX
,
322 /* clang-format off */
323 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_ipv6_udp
) {
324 /* clang-format on */
325 .sandbox
= NO_SANDBOX
,
332 /* clang-format off */
333 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_unix_stream
) {
334 /* clang-format on */
335 .sandbox
= NO_SANDBOX
,
342 /* clang-format off */
343 FIXTURE_VARIANT_ADD(protocol
, no_sandbox_with_unix_datagram
) {
344 /* clang-format on */
345 .sandbox
= NO_SANDBOX
,
352 /* clang-format off */
353 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_ipv4_tcp
) {
354 /* clang-format on */
355 .sandbox
= TCP_SANDBOX
,
362 /* clang-format off */
363 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_ipv6_tcp
) {
364 /* clang-format on */
365 .sandbox
= TCP_SANDBOX
,
372 /* clang-format off */
373 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_ipv4_udp
) {
374 /* clang-format on */
375 .sandbox
= TCP_SANDBOX
,
382 /* clang-format off */
383 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_ipv6_udp
) {
384 /* clang-format on */
385 .sandbox
= TCP_SANDBOX
,
392 /* clang-format off */
393 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_unix_stream
) {
394 /* clang-format on */
395 .sandbox
= TCP_SANDBOX
,
402 /* clang-format off */
403 FIXTURE_VARIANT_ADD(protocol
, tcp_sandbox_with_unix_datagram
) {
404 /* clang-format on */
405 .sandbox
= TCP_SANDBOX
,
412 static void test_bind_and_connect(struct __test_metadata
*const _metadata
,
413 const struct service_fixture
*const srv
,
414 const bool deny_bind
, const bool deny_connect
)
417 int inval_fd
, bind_fd
, client_fd
, status
, ret
;
420 /* Starts invalid addrlen tests with bind. */
421 inval_fd
= socket_variant(srv
);
422 ASSERT_LE(0, inval_fd
)
424 TH_LOG("Failed to create socket: %s", strerror(errno
));
427 /* Tries to bind with zero as addrlen. */
428 EXPECT_EQ(-EINVAL
, bind_variant_addrlen(inval_fd
, srv
, 0));
430 /* Tries to bind with too small addrlen. */
431 EXPECT_EQ(-EINVAL
, bind_variant_addrlen(inval_fd
, srv
,
432 get_addrlen(srv
, true) - 1));
434 /* Tries to bind with minimal addrlen. */
435 ret
= bind_variant_addrlen(inval_fd
, srv
, get_addrlen(srv
, true));
437 EXPECT_EQ(-EACCES
, ret
);
441 TH_LOG("Failed to bind to socket: %s", strerror(errno
));
444 EXPECT_EQ(0, close(inval_fd
));
446 /* Starts invalid addrlen tests with connect. */
447 inval_fd
= socket_variant(srv
);
448 ASSERT_LE(0, inval_fd
);
450 /* Tries to connect with zero as addrlen. */
451 EXPECT_EQ(-EINVAL
, connect_variant_addrlen(inval_fd
, srv
, 0));
453 /* Tries to connect with too small addrlen. */
454 EXPECT_EQ(-EINVAL
, connect_variant_addrlen(inval_fd
, srv
,
455 get_addrlen(srv
, true) - 1));
457 /* Tries to connect with minimal addrlen. */
458 ret
= connect_variant_addrlen(inval_fd
, srv
, get_addrlen(srv
, true));
459 if (srv
->protocol
.domain
== AF_UNIX
) {
460 EXPECT_EQ(-EINVAL
, ret
);
461 } else if (deny_connect
) {
462 EXPECT_EQ(-EACCES
, ret
);
463 } else if (srv
->protocol
.type
== SOCK_STREAM
) {
464 /* No listening server, whatever the value of deny_bind. */
465 EXPECT_EQ(-ECONNREFUSED
, ret
);
469 TH_LOG("Failed to connect to socket: %s",
473 EXPECT_EQ(0, close(inval_fd
));
475 /* Starts connection tests. */
476 bind_fd
= socket_variant(srv
);
477 ASSERT_LE(0, bind_fd
);
479 ret
= bind_variant(bind_fd
, srv
);
481 EXPECT_EQ(-EACCES
, ret
);
485 /* Creates a listening socket. */
486 if (srv
->protocol
.type
== SOCK_STREAM
)
487 EXPECT_EQ(0, listen(bind_fd
, backlog
));
495 /* Closes listening socket for the child. */
496 EXPECT_EQ(0, close(bind_fd
));
498 /* Starts connection tests. */
499 connect_fd
= socket_variant(srv
);
500 ASSERT_LE(0, connect_fd
);
501 ret
= connect_variant(connect_fd
, srv
);
503 EXPECT_EQ(-EACCES
, ret
);
504 } else if (deny_bind
) {
505 /* No listening server. */
506 EXPECT_EQ(-ECONNREFUSED
, ret
);
509 EXPECT_EQ(1, write(connect_fd
, ".", 1));
512 EXPECT_EQ(0, close(connect_fd
));
513 _exit(_metadata
->exit_code
);
517 /* Accepts connection from the child. */
519 if (!deny_bind
&& !deny_connect
) {
520 if (srv
->protocol
.type
== SOCK_STREAM
) {
521 client_fd
= accept(bind_fd
, NULL
, 0);
522 ASSERT_LE(0, client_fd
);
525 EXPECT_EQ(1, read(client_fd
, &buf
, 1));
529 EXPECT_EQ(child
, waitpid(child
, &status
, 0));
530 EXPECT_EQ(1, WIFEXITED(status
));
531 EXPECT_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
533 /* Closes connection, if any. */
534 if (client_fd
!= bind_fd
)
535 EXPECT_LE(0, close(client_fd
));
537 /* Closes listening socket. */
538 EXPECT_EQ(0, close(bind_fd
));
541 TEST_F(protocol
, bind
)
543 if (variant
->sandbox
== TCP_SANDBOX
) {
544 const struct landlock_ruleset_attr ruleset_attr
= {
545 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
546 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
548 const struct landlock_net_port_attr tcp_bind_connect_p0
= {
549 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
550 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
551 .port
= self
->srv0
.port
,
553 const struct landlock_net_port_attr tcp_connect_p1
= {
554 .allowed_access
= LANDLOCK_ACCESS_NET_CONNECT_TCP
,
555 .port
= self
->srv1
.port
,
559 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
560 sizeof(ruleset_attr
), 0);
561 ASSERT_LE(0, ruleset_fd
);
563 /* Allows connect and bind for the first port. */
565 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
566 &tcp_bind_connect_p0
, 0));
568 /* Allows connect and denies bind for the second port. */
570 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
571 &tcp_connect_p1
, 0));
573 enforce_ruleset(_metadata
, ruleset_fd
);
574 EXPECT_EQ(0, close(ruleset_fd
));
577 /* Binds a socket to the first port. */
578 test_bind_and_connect(_metadata
, &self
->srv0
, false, false);
580 /* Binds a socket to the second port. */
581 test_bind_and_connect(_metadata
, &self
->srv1
,
582 is_restricted(&variant
->prot
, variant
->sandbox
),
585 /* Binds a socket to the third port. */
586 test_bind_and_connect(_metadata
, &self
->srv2
,
587 is_restricted(&variant
->prot
, variant
->sandbox
),
588 is_restricted(&variant
->prot
, variant
->sandbox
));
591 TEST_F(protocol
, connect
)
593 if (variant
->sandbox
== TCP_SANDBOX
) {
594 const struct landlock_ruleset_attr ruleset_attr
= {
595 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
596 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
598 const struct landlock_net_port_attr tcp_bind_connect_p0
= {
599 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
600 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
601 .port
= self
->srv0
.port
,
603 const struct landlock_net_port_attr tcp_bind_p1
= {
604 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
605 .port
= self
->srv1
.port
,
609 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
610 sizeof(ruleset_attr
), 0);
611 ASSERT_LE(0, ruleset_fd
);
613 /* Allows connect and bind for the first port. */
615 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
616 &tcp_bind_connect_p0
, 0));
618 /* Allows bind and denies connect for the second port. */
620 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
623 enforce_ruleset(_metadata
, ruleset_fd
);
624 EXPECT_EQ(0, close(ruleset_fd
));
627 test_bind_and_connect(_metadata
, &self
->srv0
, false, false);
629 test_bind_and_connect(_metadata
, &self
->srv1
, false,
630 is_restricted(&variant
->prot
, variant
->sandbox
));
632 test_bind_and_connect(_metadata
, &self
->srv2
,
633 is_restricted(&variant
->prot
, variant
->sandbox
),
634 is_restricted(&variant
->prot
, variant
->sandbox
));
637 TEST_F(protocol
, bind_unspec
)
639 const struct landlock_ruleset_attr ruleset_attr
= {
640 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
,
642 const struct landlock_net_port_attr tcp_bind
= {
643 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
644 .port
= self
->srv0
.port
,
648 if (variant
->sandbox
== TCP_SANDBOX
) {
649 const int ruleset_fd
= landlock_create_ruleset(
650 &ruleset_attr
, sizeof(ruleset_attr
), 0);
651 ASSERT_LE(0, ruleset_fd
);
655 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
657 enforce_ruleset(_metadata
, ruleset_fd
);
658 EXPECT_EQ(0, close(ruleset_fd
));
661 bind_fd
= socket_variant(&self
->srv0
);
662 ASSERT_LE(0, bind_fd
);
664 /* Allowed bind on AF_UNSPEC/INADDR_ANY. */
665 ret
= bind_variant(bind_fd
, &self
->unspec_any0
);
666 if (variant
->prot
.domain
== AF_INET
) {
669 TH_LOG("Failed to bind to unspec/any socket: %s",
673 EXPECT_EQ(-EINVAL
, ret
);
675 EXPECT_EQ(0, close(bind_fd
));
677 if (variant
->sandbox
== TCP_SANDBOX
) {
678 const int ruleset_fd
= landlock_create_ruleset(
679 &ruleset_attr
, sizeof(ruleset_attr
), 0);
680 ASSERT_LE(0, ruleset_fd
);
683 enforce_ruleset(_metadata
, ruleset_fd
);
684 EXPECT_EQ(0, close(ruleset_fd
));
687 bind_fd
= socket_variant(&self
->srv0
);
688 ASSERT_LE(0, bind_fd
);
690 /* Denied bind on AF_UNSPEC/INADDR_ANY. */
691 ret
= bind_variant(bind_fd
, &self
->unspec_any0
);
692 if (variant
->prot
.domain
== AF_INET
) {
693 if (is_restricted(&variant
->prot
, variant
->sandbox
)) {
694 EXPECT_EQ(-EACCES
, ret
);
699 EXPECT_EQ(-EINVAL
, ret
);
701 EXPECT_EQ(0, close(bind_fd
));
703 /* Checks bind with AF_UNSPEC and the loopback address. */
704 bind_fd
= socket_variant(&self
->srv0
);
705 ASSERT_LE(0, bind_fd
);
706 ret
= bind_variant(bind_fd
, &self
->unspec_srv0
);
707 if (variant
->prot
.domain
== AF_INET
) {
708 EXPECT_EQ(-EAFNOSUPPORT
, ret
);
710 EXPECT_EQ(-EINVAL
, ret
)
712 TH_LOG("Wrong bind error: %s", strerror(errno
));
715 EXPECT_EQ(0, close(bind_fd
));
718 TEST_F(protocol
, connect_unspec
)
720 const struct landlock_ruleset_attr ruleset_attr
= {
721 .handled_access_net
= LANDLOCK_ACCESS_NET_CONNECT_TCP
,
723 const struct landlock_net_port_attr tcp_connect
= {
724 .allowed_access
= LANDLOCK_ACCESS_NET_CONNECT_TCP
,
725 .port
= self
->srv0
.port
,
727 int bind_fd
, client_fd
, status
;
730 /* Specific connection tests. */
731 bind_fd
= socket_variant(&self
->srv0
);
732 ASSERT_LE(0, bind_fd
);
733 EXPECT_EQ(0, bind_variant(bind_fd
, &self
->srv0
));
734 if (self
->srv0
.protocol
.type
== SOCK_STREAM
)
735 EXPECT_EQ(0, listen(bind_fd
, backlog
));
742 /* Closes listening socket for the child. */
743 EXPECT_EQ(0, close(bind_fd
));
745 connect_fd
= socket_variant(&self
->srv0
);
746 ASSERT_LE(0, connect_fd
);
747 EXPECT_EQ(0, connect_variant(connect_fd
, &self
->srv0
));
749 /* Tries to connect again, or set peer. */
750 ret
= connect_variant(connect_fd
, &self
->srv0
);
751 if (self
->srv0
.protocol
.type
== SOCK_STREAM
) {
752 EXPECT_EQ(-EISCONN
, ret
);
757 if (variant
->sandbox
== TCP_SANDBOX
) {
758 const int ruleset_fd
= landlock_create_ruleset(
759 &ruleset_attr
, sizeof(ruleset_attr
), 0);
760 ASSERT_LE(0, ruleset_fd
);
762 /* Allows connect. */
763 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
,
764 LANDLOCK_RULE_NET_PORT
,
766 enforce_ruleset(_metadata
, ruleset_fd
);
767 EXPECT_EQ(0, close(ruleset_fd
));
770 /* Disconnects already connected socket, or set peer. */
771 ret
= connect_variant(connect_fd
, &self
->unspec_any0
);
772 if (self
->srv0
.protocol
.domain
== AF_UNIX
&&
773 self
->srv0
.protocol
.type
== SOCK_STREAM
) {
774 EXPECT_EQ(-EINVAL
, ret
);
779 /* Tries to reconnect, or set peer. */
780 ret
= connect_variant(connect_fd
, &self
->srv0
);
781 if (self
->srv0
.protocol
.domain
== AF_UNIX
&&
782 self
->srv0
.protocol
.type
== SOCK_STREAM
) {
783 EXPECT_EQ(-EISCONN
, ret
);
788 if (variant
->sandbox
== TCP_SANDBOX
) {
789 const int ruleset_fd
= landlock_create_ruleset(
790 &ruleset_attr
, sizeof(ruleset_attr
), 0);
791 ASSERT_LE(0, ruleset_fd
);
793 /* Denies connect. */
794 enforce_ruleset(_metadata
, ruleset_fd
);
795 EXPECT_EQ(0, close(ruleset_fd
));
798 ret
= connect_variant(connect_fd
, &self
->unspec_any0
);
799 if (self
->srv0
.protocol
.domain
== AF_UNIX
&&
800 self
->srv0
.protocol
.type
== SOCK_STREAM
) {
801 EXPECT_EQ(-EINVAL
, ret
);
803 /* Always allowed to disconnect. */
807 EXPECT_EQ(0, close(connect_fd
));
808 _exit(_metadata
->exit_code
);
813 if (self
->srv0
.protocol
.type
== SOCK_STREAM
) {
814 client_fd
= accept(bind_fd
, NULL
, 0);
815 ASSERT_LE(0, client_fd
);
818 EXPECT_EQ(child
, waitpid(child
, &status
, 0));
819 EXPECT_EQ(1, WIFEXITED(status
));
820 EXPECT_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
822 /* Closes connection, if any. */
823 if (client_fd
!= bind_fd
)
824 EXPECT_LE(0, close(client_fd
));
826 /* Closes listening socket. */
827 EXPECT_EQ(0, close(bind_fd
));
832 struct service_fixture srv0
, srv1
;
835 FIXTURE_VARIANT(ipv4
)
837 const enum sandbox_type sandbox
;
841 /* clang-format off */
842 FIXTURE_VARIANT_ADD(ipv4
, no_sandbox_with_tcp
) {
843 /* clang-format on */
844 .sandbox
= NO_SANDBOX
,
848 /* clang-format off */
849 FIXTURE_VARIANT_ADD(ipv4
, tcp_sandbox_with_tcp
) {
850 /* clang-format on */
851 .sandbox
= TCP_SANDBOX
,
855 /* clang-format off */
856 FIXTURE_VARIANT_ADD(ipv4
, no_sandbox_with_udp
) {
857 /* clang-format on */
858 .sandbox
= NO_SANDBOX
,
862 /* clang-format off */
863 FIXTURE_VARIANT_ADD(ipv4
, tcp_sandbox_with_udp
) {
864 /* clang-format on */
865 .sandbox
= TCP_SANDBOX
,
871 const struct protocol_variant prot
= {
873 .type
= variant
->type
,
876 disable_caps(_metadata
);
878 set_service(&self
->srv0
, prot
, 0);
879 set_service(&self
->srv1
, prot
, 1);
881 setup_loopback(_metadata
);
884 FIXTURE_TEARDOWN(ipv4
)
888 TEST_F(ipv4
, from_unix_to_inet
)
890 int unix_stream_fd
, unix_dgram_fd
;
892 if (variant
->sandbox
== TCP_SANDBOX
) {
893 const struct landlock_ruleset_attr ruleset_attr
= {
894 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
895 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
897 const struct landlock_net_port_attr tcp_bind_connect_p0
= {
898 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
899 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
900 .port
= self
->srv0
.port
,
904 /* Denies connect and bind to check errno value. */
905 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
906 sizeof(ruleset_attr
), 0);
907 ASSERT_LE(0, ruleset_fd
);
909 /* Allows connect and bind for srv0. */
911 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
912 &tcp_bind_connect_p0
, 0));
914 enforce_ruleset(_metadata
, ruleset_fd
);
915 EXPECT_EQ(0, close(ruleset_fd
));
918 unix_stream_fd
= socket(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
919 ASSERT_LE(0, unix_stream_fd
);
921 unix_dgram_fd
= socket(AF_UNIX
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
922 ASSERT_LE(0, unix_dgram_fd
);
924 /* Checks unix stream bind and connect for srv0. */
925 EXPECT_EQ(-EINVAL
, bind_variant(unix_stream_fd
, &self
->srv0
));
926 EXPECT_EQ(-EINVAL
, connect_variant(unix_stream_fd
, &self
->srv0
));
928 /* Checks unix stream bind and connect for srv1. */
929 EXPECT_EQ(-EINVAL
, bind_variant(unix_stream_fd
, &self
->srv1
))
931 TH_LOG("Wrong bind error: %s", strerror(errno
));
933 EXPECT_EQ(-EINVAL
, connect_variant(unix_stream_fd
, &self
->srv1
));
935 /* Checks unix datagram bind and connect for srv0. */
936 EXPECT_EQ(-EINVAL
, bind_variant(unix_dgram_fd
, &self
->srv0
));
937 EXPECT_EQ(-EINVAL
, connect_variant(unix_dgram_fd
, &self
->srv0
));
939 /* Checks unix datagram bind and connect for srv1. */
940 EXPECT_EQ(-EINVAL
, bind_variant(unix_dgram_fd
, &self
->srv1
));
941 EXPECT_EQ(-EINVAL
, connect_variant(unix_dgram_fd
, &self
->srv1
));
946 struct service_fixture srv0
, srv1
;
949 FIXTURE_VARIANT(tcp_layers
)
951 const size_t num_layers
;
955 FIXTURE_SETUP(tcp_layers
)
957 const struct protocol_variant prot
= {
958 .domain
= variant
->domain
,
962 disable_caps(_metadata
);
964 ASSERT_EQ(0, set_service(&self
->srv0
, prot
, 0));
965 ASSERT_EQ(0, set_service(&self
->srv1
, prot
, 1));
967 setup_loopback(_metadata
);
970 FIXTURE_TEARDOWN(tcp_layers
)
974 /* clang-format off */
975 FIXTURE_VARIANT_ADD(tcp_layers
, no_sandbox_with_ipv4
) {
976 /* clang-format on */
981 /* clang-format off */
982 FIXTURE_VARIANT_ADD(tcp_layers
, one_sandbox_with_ipv4
) {
983 /* clang-format on */
988 /* clang-format off */
989 FIXTURE_VARIANT_ADD(tcp_layers
, two_sandboxes_with_ipv4
) {
990 /* clang-format on */
995 /* clang-format off */
996 FIXTURE_VARIANT_ADD(tcp_layers
, three_sandboxes_with_ipv4
) {
997 /* clang-format on */
1002 /* clang-format off */
1003 FIXTURE_VARIANT_ADD(tcp_layers
, no_sandbox_with_ipv6
) {
1004 /* clang-format on */
1009 /* clang-format off */
1010 FIXTURE_VARIANT_ADD(tcp_layers
, one_sandbox_with_ipv6
) {
1011 /* clang-format on */
1016 /* clang-format off */
1017 FIXTURE_VARIANT_ADD(tcp_layers
, two_sandboxes_with_ipv6
) {
1018 /* clang-format on */
1023 /* clang-format off */
1024 FIXTURE_VARIANT_ADD(tcp_layers
, three_sandboxes_with_ipv6
) {
1025 /* clang-format on */
1030 TEST_F(tcp_layers
, ruleset_overlap
)
1032 const struct landlock_ruleset_attr ruleset_attr
= {
1033 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1034 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1036 const struct landlock_net_port_attr tcp_bind
= {
1037 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1038 .port
= self
->srv0
.port
,
1040 const struct landlock_net_port_attr tcp_bind_connect
= {
1041 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1042 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1043 .port
= self
->srv0
.port
,
1046 if (variant
->num_layers
>= 1) {
1049 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1050 sizeof(ruleset_attr
), 0);
1051 ASSERT_LE(0, ruleset_fd
);
1055 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1057 /* Also allows bind, but allows connect too. */
1059 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1060 &tcp_bind_connect
, 0));
1061 enforce_ruleset(_metadata
, ruleset_fd
);
1062 EXPECT_EQ(0, close(ruleset_fd
));
1065 if (variant
->num_layers
>= 2) {
1068 /* Creates another ruleset layer. */
1069 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1070 sizeof(ruleset_attr
), 0);
1071 ASSERT_LE(0, ruleset_fd
);
1073 /* Only allows bind. */
1075 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1077 enforce_ruleset(_metadata
, ruleset_fd
);
1078 EXPECT_EQ(0, close(ruleset_fd
));
1081 if (variant
->num_layers
>= 3) {
1084 /* Creates another ruleset layer. */
1085 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1086 sizeof(ruleset_attr
), 0);
1087 ASSERT_LE(0, ruleset_fd
);
1089 /* Try to allow bind and connect. */
1091 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1092 &tcp_bind_connect
, 0));
1093 enforce_ruleset(_metadata
, ruleset_fd
);
1094 EXPECT_EQ(0, close(ruleset_fd
));
1098 * Forbids to connect to the socket because only one ruleset layer
1101 test_bind_and_connect(_metadata
, &self
->srv0
, false,
1102 variant
->num_layers
>= 2);
1105 TEST_F(tcp_layers
, ruleset_expand
)
1107 if (variant
->num_layers
>= 1) {
1108 const struct landlock_ruleset_attr ruleset_attr
= {
1109 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1111 /* Allows bind for srv0. */
1112 const struct landlock_net_port_attr bind_srv0
= {
1113 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1114 .port
= self
->srv0
.port
,
1118 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1119 sizeof(ruleset_attr
), 0);
1120 ASSERT_LE(0, ruleset_fd
);
1122 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1124 enforce_ruleset(_metadata
, ruleset_fd
);
1125 EXPECT_EQ(0, close(ruleset_fd
));
1128 if (variant
->num_layers
>= 2) {
1129 /* Expands network mask with connect action. */
1130 const struct landlock_ruleset_attr ruleset_attr
= {
1131 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1132 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1134 /* Allows bind for srv0 and connect to srv0. */
1135 const struct landlock_net_port_attr tcp_bind_connect_p0
= {
1136 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1137 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1138 .port
= self
->srv0
.port
,
1140 /* Try to allow bind for srv1. */
1141 const struct landlock_net_port_attr tcp_bind_p1
= {
1142 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1143 .port
= self
->srv1
.port
,
1147 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1148 sizeof(ruleset_attr
), 0);
1149 ASSERT_LE(0, ruleset_fd
);
1151 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1152 &tcp_bind_connect_p0
, 0));
1154 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1156 enforce_ruleset(_metadata
, ruleset_fd
);
1157 EXPECT_EQ(0, close(ruleset_fd
));
1160 if (variant
->num_layers
>= 3) {
1161 const struct landlock_ruleset_attr ruleset_attr
= {
1162 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1163 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1165 /* Allows connect to srv0, without bind rule. */
1166 const struct landlock_net_port_attr tcp_bind_p0
= {
1167 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1168 .port
= self
->srv0
.port
,
1172 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1173 sizeof(ruleset_attr
), 0);
1174 ASSERT_LE(0, ruleset_fd
);
1176 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1178 enforce_ruleset(_metadata
, ruleset_fd
);
1179 EXPECT_EQ(0, close(ruleset_fd
));
1182 test_bind_and_connect(_metadata
, &self
->srv0
, false,
1183 variant
->num_layers
>= 3);
1185 test_bind_and_connect(_metadata
, &self
->srv1
, variant
->num_layers
>= 1,
1186 variant
->num_layers
>= 2);
1189 /* clang-format off */
1191 /* clang-format on */
1195 disable_caps(_metadata
);
1197 setup_loopback(_metadata
);
1200 FIXTURE_TEARDOWN(mini
)
1204 /* clang-format off */
1206 #define ACCESS_LAST LANDLOCK_ACCESS_NET_CONNECT_TCP
1208 #define ACCESS_ALL ( \
1209 LANDLOCK_ACCESS_NET_BIND_TCP | \
1210 LANDLOCK_ACCESS_NET_CONNECT_TCP)
1212 /* clang-format on */
1214 TEST_F(mini
, network_access_rights
)
1216 const struct landlock_ruleset_attr ruleset_attr
= {
1217 .handled_access_net
= ACCESS_ALL
,
1219 struct landlock_net_port_attr net_port
= {
1220 .port
= sock_port_start
,
1226 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1227 ASSERT_LE(0, ruleset_fd
);
1229 for (access
= 1; access
<= ACCESS_LAST
; access
<<= 1) {
1230 net_port
.allowed_access
= access
;
1232 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1235 TH_LOG("Failed to add rule with access 0x%llx: %s",
1236 access
, strerror(errno
));
1239 EXPECT_EQ(0, close(ruleset_fd
));
1242 /* Checks invalid attribute, out of landlock network access range. */
1243 TEST_F(mini
, ruleset_with_unknown_access
)
1247 for (access_mask
= 1ULL << 63; access_mask
!= ACCESS_LAST
;
1248 access_mask
>>= 1) {
1249 const struct landlock_ruleset_attr ruleset_attr
= {
1250 .handled_access_net
= access_mask
,
1253 EXPECT_EQ(-1, landlock_create_ruleset(&ruleset_attr
,
1254 sizeof(ruleset_attr
), 0));
1255 EXPECT_EQ(EINVAL
, errno
);
1259 TEST_F(mini
, rule_with_unknown_access
)
1261 const struct landlock_ruleset_attr ruleset_attr
= {
1262 .handled_access_net
= ACCESS_ALL
,
1264 struct landlock_net_port_attr net_port
= {
1265 .port
= sock_port_start
,
1271 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1272 ASSERT_LE(0, ruleset_fd
);
1274 for (access
= 1ULL << 63; access
!= ACCESS_LAST
; access
>>= 1) {
1275 net_port
.allowed_access
= access
;
1277 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1279 EXPECT_EQ(EINVAL
, errno
);
1281 EXPECT_EQ(0, close(ruleset_fd
));
1284 TEST_F(mini
, rule_with_unhandled_access
)
1286 struct landlock_ruleset_attr ruleset_attr
= {
1287 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1289 struct landlock_net_port_attr net_port
= {
1290 .port
= sock_port_start
,
1296 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1297 ASSERT_LE(0, ruleset_fd
);
1299 for (access
= 1; access
> 0; access
<<= 1) {
1302 net_port
.allowed_access
= access
;
1303 err
= landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1305 if (access
== ruleset_attr
.handled_access_net
) {
1309 EXPECT_EQ(EINVAL
, errno
);
1313 EXPECT_EQ(0, close(ruleset_fd
));
1318 const struct landlock_ruleset_attr ruleset_attr
= {
1319 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
1321 const struct landlock_net_port_attr tcp_bind_connect
= {
1322 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1323 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1324 .port
= sock_port_start
,
1326 const struct landlock_net_port_attr tcp_denied
= {
1327 .allowed_access
= 0,
1328 .port
= sock_port_start
,
1330 const struct landlock_net_port_attr tcp_bind
= {
1331 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1332 .port
= sock_port_start
,
1337 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1338 ASSERT_LE(0, ruleset_fd
);
1340 /* Checks unhandled allowed_access. */
1341 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1342 &tcp_bind_connect
, 0));
1343 EXPECT_EQ(EINVAL
, errno
);
1345 /* Checks zero access value. */
1346 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1348 EXPECT_EQ(ENOMSG
, errno
);
1350 /* Adds with legitimate values. */
1351 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1355 TEST_F(mini
, tcp_port_overflow
)
1357 const struct landlock_ruleset_attr ruleset_attr
= {
1358 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1359 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1361 const struct landlock_net_port_attr port_max_bind
= {
1362 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1365 const struct landlock_net_port_attr port_max_connect
= {
1366 .allowed_access
= LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1369 const struct landlock_net_port_attr port_overflow1
= {
1370 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1371 .port
= UINT16_MAX
+ 1,
1373 const struct landlock_net_port_attr port_overflow2
= {
1374 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1375 .port
= UINT16_MAX
+ 2,
1377 const struct landlock_net_port_attr port_overflow3
= {
1378 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1379 .port
= UINT32_MAX
+ 1UL,
1381 const struct landlock_net_port_attr port_overflow4
= {
1382 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1383 .port
= UINT32_MAX
+ 2UL,
1385 const struct protocol_variant ipv4_tcp
= {
1387 .type
= SOCK_STREAM
,
1389 struct service_fixture srv_denied
, srv_max_allowed
;
1392 ASSERT_EQ(0, set_service(&srv_denied
, ipv4_tcp
, 0));
1394 /* Be careful to avoid port inconsistencies. */
1395 srv_max_allowed
= srv_denied
;
1396 srv_max_allowed
.port
= port_max_bind
.port
;
1397 srv_max_allowed
.ipv4_addr
.sin_port
= htons(port_max_bind
.port
);
1400 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1401 ASSERT_LE(0, ruleset_fd
);
1403 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1404 &port_max_bind
, 0));
1406 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1407 &port_overflow1
, 0));
1408 EXPECT_EQ(EINVAL
, errno
);
1410 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1411 &port_overflow2
, 0));
1412 EXPECT_EQ(EINVAL
, errno
);
1414 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1415 &port_overflow3
, 0));
1416 EXPECT_EQ(EINVAL
, errno
);
1418 /* Interleaves with invalid rule additions. */
1419 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1420 &port_max_connect
, 0));
1422 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1423 &port_overflow4
, 0));
1424 EXPECT_EQ(EINVAL
, errno
);
1426 enforce_ruleset(_metadata
, ruleset_fd
);
1428 test_bind_and_connect(_metadata
, &srv_denied
, true, true);
1429 test_bind_and_connect(_metadata
, &srv_max_allowed
, false, false);
1434 struct service_fixture srv0
, srv1
;
1437 FIXTURE_SETUP(ipv4_tcp
)
1439 const struct protocol_variant ipv4_tcp
= {
1441 .type
= SOCK_STREAM
,
1444 disable_caps(_metadata
);
1446 ASSERT_EQ(0, set_service(&self
->srv0
, ipv4_tcp
, 0));
1447 ASSERT_EQ(0, set_service(&self
->srv1
, ipv4_tcp
, 1));
1449 setup_loopback(_metadata
);
1452 FIXTURE_TEARDOWN(ipv4_tcp
)
1456 TEST_F(ipv4_tcp
, port_endianness
)
1458 const struct landlock_ruleset_attr ruleset_attr
= {
1459 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1460 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1462 const struct landlock_net_port_attr bind_host_endian_p0
= {
1463 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1464 /* Host port format. */
1465 .port
= self
->srv0
.port
,
1467 const struct landlock_net_port_attr connect_big_endian_p0
= {
1468 .allowed_access
= LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1469 /* Big endian port format. */
1470 .port
= htons(self
->srv0
.port
),
1472 const struct landlock_net_port_attr bind_connect_host_endian_p1
= {
1473 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1474 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1475 /* Host port format. */
1476 .port
= self
->srv1
.port
,
1478 const unsigned int one
= 1;
1479 const char little_endian
= *(const char *)&one
;
1483 landlock_create_ruleset(&ruleset_attr
, sizeof(ruleset_attr
), 0);
1484 ASSERT_LE(0, ruleset_fd
);
1485 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1486 &bind_host_endian_p0
, 0));
1487 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1488 &connect_big_endian_p0
, 0));
1489 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1490 &bind_connect_host_endian_p1
, 0));
1491 enforce_ruleset(_metadata
, ruleset_fd
);
1493 /* No restriction for big endinan CPU. */
1494 test_bind_and_connect(_metadata
, &self
->srv0
, false, little_endian
);
1496 /* No restriction for any CPU. */
1497 test_bind_and_connect(_metadata
, &self
->srv1
, false, false);
1500 TEST_F(ipv4_tcp
, with_fs
)
1502 const struct landlock_ruleset_attr ruleset_attr_fs_net
= {
1503 .handled_access_fs
= LANDLOCK_ACCESS_FS_READ_DIR
,
1504 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1506 struct landlock_path_beneath_attr path_beneath
= {
1507 .allowed_access
= LANDLOCK_ACCESS_FS_READ_DIR
,
1510 struct landlock_net_port_attr tcp_bind
= {
1511 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
,
1512 .port
= self
->srv0
.port
,
1514 int ruleset_fd
, bind_fd
, dir_fd
;
1516 /* Creates ruleset both for filesystem and network access. */
1517 ruleset_fd
= landlock_create_ruleset(&ruleset_attr_fs_net
,
1518 sizeof(ruleset_attr_fs_net
), 0);
1519 ASSERT_LE(0, ruleset_fd
);
1521 /* Adds a filesystem rule. */
1522 path_beneath
.parent_fd
= open("/dev", O_PATH
| O_DIRECTORY
| O_CLOEXEC
);
1523 ASSERT_LE(0, path_beneath
.parent_fd
);
1524 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_PATH_BENEATH
,
1526 EXPECT_EQ(0, close(path_beneath
.parent_fd
));
1528 /* Adds a network rule. */
1529 ASSERT_EQ(0, landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1532 enforce_ruleset(_metadata
, ruleset_fd
);
1533 EXPECT_EQ(0, close(ruleset_fd
));
1535 /* Tests file access. */
1536 dir_fd
= open("/dev", O_RDONLY
);
1537 EXPECT_LE(0, dir_fd
);
1538 EXPECT_EQ(0, close(dir_fd
));
1540 dir_fd
= open("/", O_RDONLY
);
1541 EXPECT_EQ(-1, dir_fd
);
1542 EXPECT_EQ(EACCES
, errno
);
1544 /* Tests port binding. */
1545 bind_fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
1546 ASSERT_LE(0, bind_fd
);
1547 EXPECT_EQ(0, bind_variant(bind_fd
, &self
->srv0
));
1548 EXPECT_EQ(0, close(bind_fd
));
1550 bind_fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
1551 ASSERT_LE(0, bind_fd
);
1552 EXPECT_EQ(-EACCES
, bind_variant(bind_fd
, &self
->srv1
));
1555 FIXTURE(port_specific
)
1557 struct service_fixture srv0
;
1560 FIXTURE_VARIANT(port_specific
)
1562 const enum sandbox_type sandbox
;
1563 const struct protocol_variant prot
;
1566 /* clang-format off */
1567 FIXTURE_VARIANT_ADD(port_specific
, no_sandbox_with_ipv4
) {
1568 /* clang-format on */
1569 .sandbox
= NO_SANDBOX
,
1572 .type
= SOCK_STREAM
,
1576 /* clang-format off */
1577 FIXTURE_VARIANT_ADD(port_specific
, sandbox_with_ipv4
) {
1578 /* clang-format on */
1579 .sandbox
= TCP_SANDBOX
,
1582 .type
= SOCK_STREAM
,
1586 /* clang-format off */
1587 FIXTURE_VARIANT_ADD(port_specific
, no_sandbox_with_ipv6
) {
1588 /* clang-format on */
1589 .sandbox
= NO_SANDBOX
,
1592 .type
= SOCK_STREAM
,
1596 /* clang-format off */
1597 FIXTURE_VARIANT_ADD(port_specific
, sandbox_with_ipv6
) {
1598 /* clang-format on */
1599 .sandbox
= TCP_SANDBOX
,
1602 .type
= SOCK_STREAM
,
1606 FIXTURE_SETUP(port_specific
)
1608 disable_caps(_metadata
);
1610 ASSERT_EQ(0, set_service(&self
->srv0
, variant
->prot
, 0));
1612 setup_loopback(_metadata
);
1615 FIXTURE_TEARDOWN(port_specific
)
1619 TEST_F(port_specific
, bind_connect_zero
)
1621 int bind_fd
, connect_fd
, ret
;
1624 /* Adds a rule layer with bind and connect actions. */
1625 if (variant
->sandbox
== TCP_SANDBOX
) {
1626 const struct landlock_ruleset_attr ruleset_attr
= {
1627 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1628 LANDLOCK_ACCESS_NET_CONNECT_TCP
1630 const struct landlock_net_port_attr tcp_bind_connect_zero
= {
1631 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1632 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1637 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1638 sizeof(ruleset_attr
), 0);
1639 ASSERT_LE(0, ruleset_fd
);
1641 /* Checks zero port value on bind and connect actions. */
1643 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1644 &tcp_bind_connect_zero
, 0));
1646 enforce_ruleset(_metadata
, ruleset_fd
);
1647 EXPECT_EQ(0, close(ruleset_fd
));
1650 bind_fd
= socket_variant(&self
->srv0
);
1651 ASSERT_LE(0, bind_fd
);
1653 connect_fd
= socket_variant(&self
->srv0
);
1654 ASSERT_LE(0, connect_fd
);
1656 /* Sets address port to 0 for both protocol families. */
1657 set_port(&self
->srv0
, 0);
1659 * Binds on port 0, which selects a random port within
1660 * ip_local_port_range.
1662 ret
= bind_variant(bind_fd
, &self
->srv0
);
1665 EXPECT_EQ(0, listen(bind_fd
, backlog
));
1667 /* Connects on port 0. */
1668 ret
= connect_variant(connect_fd
, &self
->srv0
);
1669 EXPECT_EQ(-ECONNREFUSED
, ret
);
1671 /* Sets binded port for both protocol families. */
1672 port
= get_binded_port(bind_fd
, &variant
->prot
);
1674 set_port(&self
->srv0
, port
);
1675 /* Connects on the binded port. */
1676 ret
= connect_variant(connect_fd
, &self
->srv0
);
1677 if (is_restricted(&variant
->prot
, variant
->sandbox
)) {
1678 /* Denied by Landlock. */
1679 EXPECT_EQ(-EACCES
, ret
);
1684 EXPECT_EQ(0, close(connect_fd
));
1685 EXPECT_EQ(0, close(bind_fd
));
1688 TEST_F(port_specific
, bind_connect_1023
)
1690 int bind_fd
, connect_fd
, ret
;
1692 /* Adds a rule layer with bind and connect actions. */
1693 if (variant
->sandbox
== TCP_SANDBOX
) {
1694 const struct landlock_ruleset_attr ruleset_attr
= {
1695 .handled_access_net
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1696 LANDLOCK_ACCESS_NET_CONNECT_TCP
1698 /* A rule with port value less than 1024. */
1699 const struct landlock_net_port_attr tcp_bind_connect_low_range
= {
1700 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1701 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1704 /* A rule with 1024 port. */
1705 const struct landlock_net_port_attr tcp_bind_connect
= {
1706 .allowed_access
= LANDLOCK_ACCESS_NET_BIND_TCP
|
1707 LANDLOCK_ACCESS_NET_CONNECT_TCP
,
1712 ruleset_fd
= landlock_create_ruleset(&ruleset_attr
,
1713 sizeof(ruleset_attr
), 0);
1714 ASSERT_LE(0, ruleset_fd
);
1717 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1718 &tcp_bind_connect_low_range
, 0));
1720 landlock_add_rule(ruleset_fd
, LANDLOCK_RULE_NET_PORT
,
1721 &tcp_bind_connect
, 0));
1723 enforce_ruleset(_metadata
, ruleset_fd
);
1724 EXPECT_EQ(0, close(ruleset_fd
));
1727 bind_fd
= socket_variant(&self
->srv0
);
1728 ASSERT_LE(0, bind_fd
);
1730 connect_fd
= socket_variant(&self
->srv0
);
1731 ASSERT_LE(0, connect_fd
);
1733 /* Sets address port to 1023 for both protocol families. */
1734 set_port(&self
->srv0
, 1023);
1735 /* Binds on port 1023. */
1736 ret
= bind_variant(bind_fd
, &self
->srv0
);
1737 /* Denied by the system. */
1738 EXPECT_EQ(-EACCES
, ret
);
1740 /* Binds on port 1023. */
1741 set_cap(_metadata
, CAP_NET_BIND_SERVICE
);
1742 ret
= bind_variant(bind_fd
, &self
->srv0
);
1743 clear_cap(_metadata
, CAP_NET_BIND_SERVICE
);
1745 EXPECT_EQ(0, listen(bind_fd
, backlog
));
1747 /* Connects on the binded port 1023. */
1748 ret
= connect_variant(connect_fd
, &self
->srv0
);
1751 EXPECT_EQ(0, close(connect_fd
));
1752 EXPECT_EQ(0, close(bind_fd
));
1754 bind_fd
= socket_variant(&self
->srv0
);
1755 ASSERT_LE(0, bind_fd
);
1757 connect_fd
= socket_variant(&self
->srv0
);
1758 ASSERT_LE(0, connect_fd
);
1760 /* Sets address port to 1024 for both protocol families. */
1761 set_port(&self
->srv0
, 1024);
1762 /* Binds on port 1024. */
1763 ret
= bind_variant(bind_fd
, &self
->srv0
);
1765 EXPECT_EQ(0, listen(bind_fd
, backlog
));
1767 /* Connects on the binded port 1024. */
1768 ret
= connect_variant(connect_fd
, &self
->srv0
);
1771 EXPECT_EQ(0, close(connect_fd
));
1772 EXPECT_EQ(0, close(bind_fd
));