Skip to contentSkip to conversation

landlock: Support MPTCP bind and connect restrictions

PATCH In review Günther Noack 5 participants 15 messages Aug 30 – Sep 30

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover landlock: Support MPTCP bind and connect restrictions

Hello!

This patch set makes it possible to restrict MPTCP bind(2) and connect(2) operations by port, adding the access rights LANDLOCK_ACCESS_NET_BIND_MPTCP and LANDLOCK_ACCESS_NET_CONNECT_MPTCP.

Motivation
==========

With MPTCP operations being unrestrictable, some aspects of Landlock's existing TCP restrictions were not useful. Notably, bind(2) and listen(2) on MPTCP sockets was possible, sidestepping a bind(2) restriction that might exist for plain TCP sockets. This patch set fixes that gap by restricting bind(2) and connect(2) operations in the same way as for TCP.

As listening on MPTCP sockets is backwards compatible with plain TCP, it has gained more support and has become the default in common networking libraries such as Go's net.Listen() function since Go 1.24 [1].

Historical background
=====================

In the initial implementation, Landlock's TCP bind(2) and connect(2) access rights worked on IP stream ports independent of their protocol as specified in socket(2). This was corrected in Landlock erratum 1 in commit 854277e2cc8c ("landlock: Fix non-TCP sockets restriction")
[2] [3], but also meant that MPTCP sockets were now not restrictable with Landlock any more, even though MPTCP operates on the same TCP ports as plain TCP.

That MPTCP should often be treated the same as plain TCP was also pointed out in [4] and [5].

Implementation notes
====================

* The tests are an extension of the existing exhaustive TCP/UDP selftest coverage.
* MPTCP subflows are separate connections with their own port numbers. As it is the Linux kernel which negotiates these ports with the remote system, the ports used in subflows are not subject to this Landlock restriction.
* MPTCP Fast Open is treated the same as for TCP.

Apart from these, MPTCP support is a relatively straightforward implementation, mirroring the TCP logic in most places.

Alternatives considered
=======================

Making MPTCP sockets subject to "plain TCP" Landlock access rights is technically feasible, but would undo erratum 1 [3], which could be confusing to users and might introduce potential incompatibilities with existing programs.

Open questions
==============

I am on the edge about the helper functions that I added to the selftests; maybe would be better to flatten these decisions out into the fixture data for improved clarity and to not run the risk of reimplementing the same code that we want to test.

Let me know what you think!
–Günther

[1] https://go.dev/doc/go1.24#netpkgnet
[2] commit 854277e2cc8c ("landlock: Fix non-TCP sockets restriction")

    https://lore.kernel.org/r/[email protected]

[3] Landlock erratum 1, security/landlock/errata/abi-4.h
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Günther Noack (6):
samples/landlock: Implement best-effort fallback for network rules. selftests/landlock: Generalize net test helpers for multiple socket
types
landlock: Add MPTCP bind and connect access rights selftests/landlock: Add MPTCP network access tests samples/landlock: Support MPTCP access rights
landlock: Document MPTCP access rights

 Documentation/userspace-api/landlock.rst     |  27 +-
 include/linux/landlock.h                     |   5 +-
 include/uapi/linux/landlock.h                |  24 ++
 samples/landlock/sandboxer.c                 |  72 +++-
 security/landlock/limits.h                   |   2 +-
 security/landlock/net.c                      |  68 ++--
 security/landlock/syscalls.c                 |   2 +-
 tools/testing/selftests/landlock/base_test.c |   2 +-
 tools/testing/selftests/landlock/net_test.c  | 341 ++++++++++++++-----
 9 files changed, 425 insertions(+), 118 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/6 samples/landlock: Implement best-effort fallback for network rules.

The sandboxer sample tool added network rules unconditionally, even on systems that only support lower Landlock ABI versions, resulting in errors.

This change implements the correct best-effort fallback: As on older Landlock ABI versions, the given operation is not restrictable, is also does not need to be allow-listed.

---
 samples/landlock/sandboxer.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
samples/landlock/sandboxer.c +16 -4
index 030583273f3f..1c514efecafb 100644
@@ -198,6 +198,10 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
 		.allowed_access = allowed_access,
 	};
 
+	/* A rule without access rights and flags is a no-op. */
+	if (!allowed_access && !flags)
+		return 0;
+
 	env_port_name = getenv(env_var);
 	if (!env_port_name)
 		return 0;
@@ -657,19 +661,27 @@ int main(const int argc, char *const argv[], char *const *const envp)
 	}
 
 	if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
-				 LANDLOCK_ACCESS_NET_BIND_TCP, 0)) {
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_BIND_TCP,
+				 0)) {
 		goto err_close_ruleset;
 	}
 	if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
-				 LANDLOCK_ACCESS_NET_CONNECT_TCP, 0)) {
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_CONNECT_TCP,
+				 0)) {
 		goto err_close_ruleset;
 	}
 	if (populate_ruleset_net(ENV_UDP_BIND_NAME, ruleset_fd,
-				 LANDLOCK_ACCESS_NET_BIND_UDP, 0)) {
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_BIND_UDP,
+				 0)) {
 		goto err_close_ruleset;
 	}
 	if (populate_ruleset_net(ENV_UDP_CONNECT_SEND_NAME, ruleset_fd,
-				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, 0)) {
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
+				 0)) {
 		goto err_close_ruleset;
 	}
 
Signature · 1 line
2.55.0
#3 lore raw

PATCH 2/6 selftests/landlock: Generalize net test helpers for multiple socket types

In reply to Günther Noack

Create helper methods for determining the access rights to be tested based on socket type (TCP or UDP). This makes it simpler to add more socket types with similar bind(2) and connect(2) restrictions in the future.

---
 tools/testing/selftests/landlock/net_test.c | 85 +++++++++++----------
 1 file changed, 46 insertions(+), 39 deletions(-)
tools/testing/selftests/landlock/net_test.c +46 -39
index a18761e0fd82..3a0482beca5f 100644
@@ -108,11 +108,41 @@ static bool prot_is_udp(const struct protocol_variant *const prot)
 static bool is_restricted(const struct protocol_variant *const prot,
 			  const enum sandbox_type sandbox)
 {
-	if (sandbox == TCP_SANDBOX)
+	switch (sandbox) {
+	case TCP_SANDBOX:
 		return prot_is_tcp(prot);
-	else if (sandbox == UDP_SANDBOX)
+	case UDP_SANDBOX:
 		return prot_is_udp(prot);
-	return false;
+	case NO_SANDBOX:
+	default:
+		return false;
+	}
+}
+
+static __u64 sandbox_bind_access(const enum sandbox_type sandbox)
+{
+	switch (sandbox) {
+	case TCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_BIND_TCP;
+	case UDP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_BIND_UDP;
+	case NO_SANDBOX:
+	default:
+		return 0;
+	}
+}
+
+static __u64 sandbox_connect_access(const enum sandbox_type sandbox)
+{
+	switch (sandbox) {
+	case TCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_CONNECT_TCP;
+	case UDP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+	case NO_SANDBOX:
+	default:
+		return 0;
+	}
 }
 
 static int socket_variant(const struct service_fixture *const srv)
@@ -916,16 +946,10 @@ static void test_bind_and_connect(struct __test_metadata *const _metadata,
 
 TEST_F(protocol, bind)
 {
-	if (variant->sandbox == TCP_SANDBOX ||
-	    variant->sandbox == UDP_SANDBOX) {
-		const __u64 bind_access =
-			(variant->sandbox == TCP_SANDBOX ?
-				 LANDLOCK_ACCESS_NET_BIND_TCP :
-				 LANDLOCK_ACCESS_NET_BIND_UDP);
+	if (variant->sandbox != NO_SANDBOX) {
+		const __u64 bind_access = sandbox_bind_access(variant->sandbox);
 		const __u64 conn_access =
-			(variant->sandbox == TCP_SANDBOX ?
-				 LANDLOCK_ACCESS_NET_CONNECT_TCP :
-				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+			sandbox_connect_access(variant->sandbox);
 		const struct landlock_ruleset_attr ruleset_attr = {
 			.handled_access_net = bind_access | conn_access,
 		};
@@ -987,16 +1011,10 @@ TEST_F(protocol, bind)
 
 TEST_F(protocol, connect)
 {
-	if (variant->sandbox == TCP_SANDBOX ||
-	    variant->sandbox == UDP_SANDBOX) {
-		const __u64 bind_access =
-			(variant->sandbox == TCP_SANDBOX ?
-				 LANDLOCK_ACCESS_NET_BIND_TCP :
-				 LANDLOCK_ACCESS_NET_BIND_UDP);
+	if (variant->sandbox != NO_SANDBOX) {
+		const __u64 bind_access = sandbox_bind_access(variant->sandbox);
 		const __u64 conn_access =
-			(variant->sandbox == TCP_SANDBOX ?
-				 LANDLOCK_ACCESS_NET_CONNECT_TCP :
-				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+			sandbox_connect_access(variant->sandbox);
 		const struct landlock_ruleset_attr ruleset_attr = {
 			.handled_access_net = bind_access | conn_access,
 		};
@@ -1054,9 +1072,7 @@ TEST_F(protocol, connect)
 
 TEST_F(protocol, bind_unspec)
 {
-	const __u64 bind_access = (variant->sandbox == TCP_SANDBOX ?
-					   LANDLOCK_ACCESS_NET_BIND_TCP :
-					   LANDLOCK_ACCESS_NET_BIND_UDP);
+	const __u64 bind_access = sandbox_bind_access(variant->sandbox);
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_net = bind_access,
 	};
@@ -1066,8 +1082,7 @@ TEST_F(protocol, bind_unspec)
 	};
 	int bind_fd, ret;
 
-	if (variant->sandbox == TCP_SANDBOX ||
-	    variant->sandbox == UDP_SANDBOX) {
+	if (variant->sandbox != NO_SANDBOX) {
 		const int ruleset_fd = landlock_create_ruleset(
 			&ruleset_attr, sizeof(ruleset_attr), 0);
 		ASSERT_LE(0, ruleset_fd);
@@ -1103,8 +1118,7 @@ TEST_F(protocol, bind_unspec)
 	}
 	EXPECT_EQ(0, close(bind_fd));
 
-	if (variant->sandbox == TCP_SANDBOX ||
-	    variant->sandbox == UDP_SANDBOX) {
+	if (variant->sandbox != NO_SANDBOX) {
 		const int ruleset_fd = landlock_create_ruleset(
 			&ruleset_attr, sizeof(ruleset_attr), 0);
 		ASSERT_LE(0, ruleset_fd);
@@ -1150,13 +1164,8 @@ TEST_F(protocol, bind_unspec)
 
 TEST_F(protocol, connect_unspec)
 {
-	const __u64 connect_right =
-		(variant->sandbox == TCP_SANDBOX ?
-			 LANDLOCK_ACCESS_NET_CONNECT_TCP :
-			 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
-	const __u64 bind_right = (variant->sandbox == TCP_SANDBOX ?
-					  LANDLOCK_ACCESS_NET_BIND_TCP :
-					  LANDLOCK_ACCESS_NET_BIND_UDP);
+	const __u64 connect_right = sandbox_connect_access(variant->sandbox);
+	const __u64 bind_right = sandbox_bind_access(variant->sandbox);
 	const struct landlock_ruleset_attr ruleset_conn = {
 		.handled_access_net = connect_right,
 	};
@@ -1197,8 +1206,7 @@ TEST_F(protocol, connect_unspec)
 			EXPECT_EQ(0, ret);
 		}
 
-		if (variant->sandbox == TCP_SANDBOX ||
-		    variant->sandbox == UDP_SANDBOX) {
+		if (variant->sandbox != NO_SANDBOX) {
 			const int ruleset_fd = landlock_create_ruleset(
 				&ruleset_conn, sizeof(ruleset_conn), 0);
 			ASSERT_LE(0, ruleset_fd);
@@ -1229,8 +1237,7 @@ TEST_F(protocol, connect_unspec)
 			EXPECT_EQ(0, ret);
 		}
 
-		if (variant->sandbox == TCP_SANDBOX ||
-		    variant->sandbox == UDP_SANDBOX) {
+		if (variant->sandbox != NO_SANDBOX) {
 			const int ruleset_fd = landlock_create_ruleset(
 				&ruleset_conn_bind, sizeof(ruleset_conn_bind),
 				0);
Signature · 1 line
2.55.0
#5 lore raw

PATCH 3/6 landlock: Add MPTCP bind and connect access rights

In reply to Günther Noack

MPTCP sockets have equivalent bind(2) and connect(2) operations as TCP sockets, but can not currently be restricted with Landlock without explicit MPTCP access rights. As MPTCP operates on the same TCP port number space as TCP, this is a gap in Landlock's policies.

Add access rights for MPTCP bind(2) and connect(2) operations and document them in the header.

Treat TCP Fast Open the same as done for plain TCP in commit 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

The port numbers used in MPTCP subflows are negotiated by the kernel and therefore not subject to these access rights.

Bump the Landlock ABI version to 12.

---
 include/linux/landlock.h                     |  5 +-
 include/uapi/linux/landlock.h                | 24 +++++++
 security/landlock/limits.h                   |  2 +-
 security/landlock/net.c                      | 68 ++++++++++++++------
 security/landlock/syscalls.c                 |  2 +-
 tools/testing/selftests/landlock/base_test.c |  2 +-
 6 files changed, 79 insertions(+), 24 deletions(-)
include/linux/landlock.h +4 -1
index 004cbd0b9298..b04ffc7caa21 100644
@@ -46,7 +46,10 @@
 	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_TCP, "connect_tcp"), \
 	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_UDP, "bind_udp"), \
 	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, \
-			     "connect_send_udp")
+			     "connect_send_udp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_MPTCP, "bind_mptcp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_MPTCP, \
+			     "connect_mptcp")
 
 #define _LANDLOCK_SCOPE_NAMES \
 	_LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET, \
include/uapi/linux/landlock.h +24 -0
index cceda3b3b961..2a953ba7ce25 100644
@@ -448,6 +448,9 @@ struct landlock_net_port_attr {
  * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to the given
  *   remote port. Support added in Landlock ABI version 4.
  *
+ * .. note:: These rights do not apply to MPTCP sockets, which have their own
+ *   access rights (see below).
+ *
  * And similarly for UDP port numbers:
  *
  * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given local
@@ -474,12 +477,33 @@ struct landlock_net_port_attr {
  * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination address
  *   family is not supported for IPv6 UDP sockets: you will need to use a
  *   ``NULL`` address instead.
+ *
+ * MPTCP sockets (created with ``IPPROTO_MPTCP``) use TCP port numbers, but
+ * they are controlled by their own access rights:
+ *
+ * - %LANDLOCK_ACCESS_NET_BIND_MPTCP: Bind MPTCP sockets to the given local
+ *   port. Support added in Landlock ABI version 12.
+ * - %LANDLOCK_ACCESS_NET_CONNECT_MPTCP: Connect MPTCP sockets to the given
+ *   remote port. Support added in Landlock ABI version 12.
+ *
+ * .. note:: The TCP and the MPTCP access rights are independent, even though
+ *   they refer to the same port number space. Handling only
+ *   %LANDLOCK_ACCESS_NET_BIND_TCP and %LANDLOCK_ACCESS_NET_CONNECT_TCP leaves
+ *   MPTCP sockets unrestricted, and vice versa. A sandbox that wants to
+ *   control all TCP-based traffic needs to handle both sets.
+ *
+ * .. note:: These MPTCP access rights restrict the ports passed to
+ *   :manpage:`bind(2)` and :manpage:`connect(2)`. The ports used in MPTCP
+ *   subflows are negotiated in the MPTCP protocol by the kernel and are not
+ *   subject to these restrictions.
  */
 /* clang-format off */
 #define LANDLOCK_ACCESS_NET_BIND_TCP			(1ULL << 0)
 #define LANDLOCK_ACCESS_NET_CONNECT_TCP			(1ULL << 1)
 #define LANDLOCK_ACCESS_NET_BIND_UDP			(1ULL << 2)
 #define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP		(1ULL << 3)
+#define LANDLOCK_ACCESS_NET_BIND_MPTCP			(1ULL << 4)
+#define LANDLOCK_ACCESS_NET_CONNECT_MPTCP		(1ULL << 5)
 /* clang-format on */
 
 /**
security/landlock/limits.h +1 -1
index 1a7c5fb8f6fd..d25e056b7ca2 100644
@@ -23,7 +23,7 @@
 #define LANDLOCK_MASK_ACCESS_FS		((LANDLOCK_LAST_ACCESS_FS << 1) - 1)
 #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
 
-#define LANDLOCK_LAST_ACCESS_NET	LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP
+#define LANDLOCK_LAST_ACCESS_NET	LANDLOCK_ACCESS_NET_CONNECT_MPTCP
 #define LANDLOCK_MASK_ACCESS_NET	((LANDLOCK_LAST_ACCESS_NET << 1) - 1)
 #define LANDLOCK_NUM_ACCESS_NET		__const_hweight64(LANDLOCK_MASK_ACCESS_NET)
 
security/landlock/net.c +48 -20

131 lines not on this page · raw message

security/landlock/syscalls.c +1 -1
index 1d02d57f4c48..cc54d4f1d502 100644
@@ -172,7 +172,7 @@ static const struct file_operations ruleset_fops = {
  * If the change involves a fix that requires userspace awareness, also update
  * the errata documentation in Documentation/userspace-api/landlock.rst .
  */
-const int landlock_abi_version = 11;
+const int landlock_abi_version = 12;
 
 /**
  * sys_landlock_create_ruleset - Create a new ruleset
tools/testing/selftests/landlock/base_test.c +1 -1
index d20ab8f0862c..58fe322d8637 100644
@@ -76,7 +76,7 @@ TEST(abi_version)
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
 	};
-	ASSERT_EQ(11, landlock_create_ruleset(NULL, 0,
+	ASSERT_EQ(12, landlock_create_ruleset(NULL, 0,
 					      LANDLOCK_CREATE_RULESET_VERSION));
 
 	ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
Signature · 1 line
2.55.0
#10 lore raw

PATCH 4/6 selftests/landlock: Add MPTCP network access tests

In reply to Günther Noack

Test LANDLOCK_ACCESS_NET_BIND_MPTCP and LANDLOCK_ACCESS_NET_CONNECT_MPTCP:

* Add the MPTCP_SANDBOX variant to the net_test fixtures.
* Introduce prot_*() helper functions for audit tests.
* Extend existing tests as needed for MPTCP, including the tcp_fastopen test.

---
 tools/testing/selftests/landlock/net_test.c | 256 ++++++++++++++++----
 1 file changed, 210 insertions(+), 46 deletions(-)
tools/testing/selftests/landlock/net_test.c +210 -46
index 3a0482beca5f..fbb3a99bc380 100644
@@ -40,6 +40,7 @@ enum sandbox_type {
 	/* This may be used to test rules that allow *and* deny accesses. */
 	TCP_SANDBOX,
 	UDP_SANDBOX,
+	MPTCP_SANDBOX,
 };
 
 static int set_service(struct service_fixture *const srv,
@@ -105,6 +106,12 @@ static bool prot_is_udp(const struct protocol_variant *const prot)
 	       (prot->protocol == IPPROTO_UDP || prot->protocol == IPPROTO_IP);
 }
 
+static bool prot_is_mptcp(const struct protocol_variant *const prot)
+{
+	return (prot->domain == AF_INET || prot->domain == AF_INET6) &&
+	       prot->type == SOCK_STREAM && prot->protocol == IPPROTO_MPTCP;
+}
+
 static bool is_restricted(const struct protocol_variant *const prot,
 			  const enum sandbox_type sandbox)
 {
@@ -113,6 +120,8 @@ static bool is_restricted(const struct protocol_variant *const prot,
 		return prot_is_tcp(prot);
 	case UDP_SANDBOX:
 		return prot_is_udp(prot);
+	case MPTCP_SANDBOX:
+		return prot_is_mptcp(prot);
 	case NO_SANDBOX:
 	default:
 		return false;
@@ -126,6 +135,8 @@ static __u64 sandbox_bind_access(const enum sandbox_type sandbox)
 		return LANDLOCK_ACCESS_NET_BIND_TCP;
 	case UDP_SANDBOX:
 		return LANDLOCK_ACCESS_NET_BIND_UDP;
+	case MPTCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_BIND_MPTCP;
 	case NO_SANDBOX:
 	default:
 		return 0;
@@ -139,6 +150,8 @@ static __u64 sandbox_connect_access(const enum sandbox_type sandbox)
 		return LANDLOCK_ACCESS_NET_CONNECT_TCP;
 	case UDP_SANDBOX:
 		return LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+	case MPTCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_CONNECT_MPTCP;
 	case NO_SANDBOX:
 	default:
 		return 0;
@@ -815,6 +828,114 @@ FIXTURE_VARIANT_ADD(protocol, udp_sandbox_with_unix_datagram) {
 	},
 };
 
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv4_tcp1) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+		/* IPPROTO_IP == 0 */
+		.protocol = IPPROTO_IP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv4_tcp2) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_TCP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv4_mptcp) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_MPTCP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv6_tcp1) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+		/* IPPROTO_IP == 0 */
+		.protocol = IPPROTO_IP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv6_tcp2) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_TCP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv6_mptcp) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_MPTCP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv4_udp) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_DGRAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_ipv6_udp) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_DGRAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_unix_stream) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_UNIX,
+		.type = SOCK_STREAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(protocol, mptcp_sandbox_with_unix_datagram) {
+	/* clang-format on */
+	.sandbox = MPTCP_SANDBOX,
+	.prot = {
+		.domain = AF_UNIX,
+		.type = SOCK_DGRAM,
+	},
+};
+
 static void test_bind_and_connect(struct __test_metadata *const _metadata,
 				  const struct service_fixture *const srv,
 				  const bool deny_bind, const bool deny_connect)
@@ -1294,14 +1415,12 @@ TEST_F(protocol, connect_unspec)
 
 TEST_F(protocol, tcp_fastopen)
 {
-	const bool restricted = variant->sandbox == TCP_SANDBOX &&
-				variant->prot.type == SOCK_STREAM &&
-				(variant->prot.protocol == IPPROTO_TCP ||
-				 variant->prot.protocol == IPPROTO_IP) &&
-				(variant->prot.domain == AF_INET ||
-				 variant->prot.domain == AF_INET6);
+	const bool stream_sandbox = variant->sandbox == TCP_SANDBOX ||
+				    variant->sandbox == MPTCP_SANDBOX;
+	const bool restricted = stream_sandbox &&
+				is_restricted(&variant->prot, variant->sandbox);
 	const struct landlock_ruleset_attr ruleset_attr = {
-		.handled_access_net = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+		.handled_access_net = sandbox_connect_access(variant->sandbox),
 	};
 	int bind_fd, client_fd, status;
 	char buf;
@@ -1324,7 +1443,7 @@ TEST_F(protocol, tcp_fastopen)
 		connect_fd = socket_variant(&self->srv0);
 		ASSERT_LE(0, connect_fd);
 
-		if (variant->sandbox == TCP_SANDBOX) {
+		if (stream_sandbox) {
 			const int ruleset_fd = landlock_create_ruleset(
 				&ruleset_attr, sizeof(ruleset_attr), 0);
 			ASSERT_LE(0, ruleset_fd);
@@ -2219,13 +2338,15 @@ FIXTURE_TEARDOWN(mini)
 
 /* clang-format off */
 
-#define ACCESS_LAST LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP
+#define ACCESS_LAST LANDLOCK_ACCESS_NET_CONNECT_MPTCP
 
 #define ACCESS_ALL ( \
 	LANDLOCK_ACCESS_NET_BIND_TCP | \
 	LANDLOCK_ACCESS_NET_CONNECT_TCP | \
 	LANDLOCK_ACCESS_NET_BIND_UDP | \
-	LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP)
+	LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP | \
+	LANDLOCK_ACCESS_NET_BIND_MPTCP | \
+	LANDLOCK_ACCESS_NET_CONNECT_MPTCP)
 
 /* clang-format on */
 
@@ -2949,6 +3070,28 @@ FIXTURE_VARIANT_ADD(audit, ipv6_udp) {
 	},
 };
 
+/* clang-format off */
+FIXTURE_VARIANT_ADD(audit, ipv4_mptcp) {
+	/* clang-format on */
+	.addr = "127\\.0\\.0\\.1",
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_MPTCP,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(audit, ipv6_mptcp) {
+	/* clang-format on */
+	.addr = "::1",
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+		.protocol = IPPROTO_MPTCP,
+	},
+};
+
 FIXTURE_SETUP(audit)
 {
 	struct protocol_variant prot_unspec = variant->prot;
@@ -2975,17 +3118,56 @@ FIXTURE_TEARDOWN(audit)
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 }
 
+static __u64 prot_bind_access(const struct protocol_variant *const prot)
+{
+	if (prot_is_mptcp(prot))
+		return LANDLOCK_ACCESS_NET_BIND_MPTCP;
+
+	if (prot->type == SOCK_STREAM)
+		return LANDLOCK_ACCESS_NET_BIND_TCP;
+
+	return LANDLOCK_ACCESS_NET_BIND_UDP;
+}
+
+static __u64 prot_connect_access(const struct protocol_variant *const prot)
+{
+	if (prot_is_mptcp(prot))
+		return LANDLOCK_ACCESS_NET_CONNECT_MPTCP;
+
+	if (prot->type == SOCK_STREAM)
+		return LANDLOCK_ACCESS_NET_CONNECT_TCP;
+
+	return LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+}
+
+static const char *prot_bind_blocker(const struct protocol_variant *const prot)
+{
+	if (prot_is_mptcp(prot))
+		return "net\\.bind_mptcp";
+
+	if (prot->type == SOCK_STREAM)
+		return "net\\.bind_tcp";
+
+	return "net\\.bind_udp";
+}
+
+static const char *
+prot_connect_blocker(const struct protocol_variant *const prot)
+{
+	if (prot_is_mptcp(prot))
+		return "net\\.connect_mptcp";
+
+	if (prot->type == SOCK_STREAM)
+		return "net\\.connect_tcp";
+
+	return "net\\.connect_send_udp";
+}
+
 TEST_F(audit, bind)
 {
-	const char *audit_evt = (variant->prot.type == SOCK_STREAM ?
-					 "net\\.bind_tcp" :
-					 "net\\.bind_udp");
-	const __u64 access_rights =
-		(variant->prot.type == SOCK_STREAM ?
-			 LANDLOCK_ACCESS_NET_BIND_TCP |
-				 LANDLOCK_ACCESS_NET_CONNECT_TCP :
-			 LANDLOCK_ACCESS_NET_BIND_UDP |
-				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+	const char *audit_evt = prot_bind_blocker(&variant->prot);
+	const __u64 access_rights = prot_bind_access(&variant->prot) |
+				    prot_connect_access(&variant->prot);
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_net = access_rights,
 		.quiet_access_net = access_rights,
@@ -3031,15 +3213,9 @@ TEST_F(audit, bind)
 
 TEST_F(audit, connect)
 {
-	const char *audit_evt = (variant->prot.type == SOCK_STREAM ?
-					 "net\\.connect_tcp" :
-					 "net\\.connect_send_udp");
-	const __u64 bind_right = (variant->prot.type == SOCK_STREAM ?
-					  LANDLOCK_ACCESS_NET_BIND_TCP :
-					  LANDLOCK_ACCESS_NET_BIND_UDP);
-	const __u64 conn_right = (variant->prot.type == SOCK_STREAM ?
-					  LANDLOCK_ACCESS_NET_CONNECT_TCP :
-					  LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+	const char *audit_evt = prot_connect_blocker(&variant->prot);
+	const __u64 bind_right = prot_bind_access(&variant->prot);
+	const __u64 conn_right = prot_connect_access(&variant->prot);
 	const __u64 access_rights = bind_right | conn_right;
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_net = access_rights,
@@ -3104,15 +3280,9 @@ TEST_F(audit, connect)
 /* Quieting bind access has no effect on connect. */
 TEST_F(audit, connect_quiet_bind)
 {
-	const char *audit_evt = (variant->prot.type == SOCK_STREAM ?
-					 "net\\.connect_tcp" :
-					 "net\\.connect_send_udp");
-	const int bind_right = (variant->prot.type == SOCK_STREAM ?
-					LANDLOCK_ACCESS_NET_BIND_TCP :
-					LANDLOCK_ACCESS_NET_BIND_UDP);
-	const int conn_right = (variant->prot.type == SOCK_STREAM ?
-					LANDLOCK_ACCESS_NET_CONNECT_TCP :
-					LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+	const char *audit_evt = prot_connect_blocker(&variant->prot);
+	const int bind_right = prot_bind_access(&variant->prot);
+	const int conn_right = prot_connect_access(&variant->prot);
 	const int access_rights = bind_right | conn_right;
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_net = access_rights,
@@ -3194,15 +3364,9 @@ static int matches_log_connect_bound(int audit_fd, const char *const blockers,
  */
 TEST_F(audit, connect_bound)
 {
-	const __u64 bind_right = (variant->prot.type == SOCK_STREAM ?
-					  LANDLOCK_ACCESS_NET_BIND_TCP :
-					  LANDLOCK_ACCESS_NET_BIND_UDP);
-	const __u64 conn_right = (variant->prot.type == SOCK_STREAM ?
-					  LANDLOCK_ACCESS_NET_CONNECT_TCP :
-					  LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
-	const char *const audit_evt = (variant->prot.type == SOCK_STREAM ?
-					       "net\\.connect_tcp" :
-					       "net\\.connect_send_udp");
+	const __u64 bind_right = prot_bind_access(&variant->prot);
+	const __u64 conn_right = prot_connect_access(&variant->prot);
+	const char *const audit_evt = prot_connect_blocker(&variant->prot);
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_net = bind_right | conn_right,
 	};
Signature · 1 line
2.55.0
#12 lore raw

PATCH 5/6 samples/landlock: Support MPTCP access rights

In reply to Günther Noack

Add the LL_MPTCP_BIND and LL_MPTCP_CONNECT environment variables to restrict the ports MPTCP sockets may bind and connect to, and the matching "mptcp_bind" and "mptcp_connect" values for LL_QUIET_ACCESS.

Because MPTCP sockets use TCP port numbers but are not covered by the TCP access rights, LL_TCP_BIND and LL_TCP_CONNECT alone leave MPTCP unrestricted. Point that out in the help text.

Bump LANDLOCK_ABI_LAST to 12 and drop the new access rights when running on an older kernel.

---
 samples/landlock/sandboxer.c | 52 ++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)
samples/landlock/sandboxer.c +50 -2
index 1c514efecafb..5eae6fe7467f 100644
@@ -67,6 +67,8 @@ static inline int landlock_restrict_self(const int ruleset_fd,
 #define ENV_FORCE_LOG_NAME "LL_FORCE_LOG"
 #define ENV_UDP_BIND_NAME "LL_UDP_BIND"
 #define ENV_UDP_CONNECT_SEND_NAME "LL_UDP_CONNECT_SEND"
+#define ENV_MPTCP_BIND_NAME "LL_MPTCP_BIND"
+#define ENV_MPTCP_CONNECT_NAME "LL_MPTCP_CONNECT"
 #define ENV_DELIMITER ":"
 
 static int str2num(const char *numstr, __u64 *num_dst)
@@ -353,6 +355,12 @@ static int add_quiet_access(const char *const env_var,
 		else if (strcmp(str_access, "udp_connect") == 0)
 			ruleset_attr->quiet_access_net |=
 				LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+		else if (strcmp(str_access, "mptcp_bind") == 0)
+			ruleset_attr->quiet_access_net |=
+				LANDLOCK_ACCESS_NET_BIND_MPTCP;
+		else if (strcmp(str_access, "mptcp_connect") == 0)
+			ruleset_attr->quiet_access_net |=
+				LANDLOCK_ACCESS_NET_CONNECT_MPTCP;
 		else if (strcmp(str_access, "abstract_unix_socket") == 0)
 			ruleset_attr->quiet_scoped |=
 				LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
@@ -373,7 +381,7 @@ static int add_quiet_access(const char *const env_var,
 	return 0;
 }
 
-#define LANDLOCK_ABI_LAST 11
+#define LANDLOCK_ABI_LAST 12
 
 #define XSTR(s) #s
 #define STR(s) XSTR(s)
@@ -401,6 +409,12 @@ static const char help[] =
 	"* " ENV_UDP_CONNECT_SEND_NAME ": remote UDP ports allowed to connect "
 	"or send to (client: use as destination port / server: receive only from it)\n"
 	"(caution: sending requires being able to bind to a local source port)\n"
+	"* " ENV_MPTCP_BIND_NAME ": ports allowed to bind with MPTCP sockets "
+	"(server)\n"
+	"* " ENV_MPTCP_CONNECT_NAME ": ports allowed to connect to with MPTCP "
+	"sockets (client)\n"
+	"(caution: MPTCP sockets use TCP ports but are not covered by "
+	ENV_TCP_BIND_NAME " nor " ENV_TCP_CONNECT_NAME ")\n"
 	"* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n"
 	"  - \"a\" to restrict opening abstract unix sockets\n"
 	"  - \"s\" to restrict sending signals\n"
@@ -418,6 +432,8 @@ static const char help[] =
 	"  - \"tcp_connect\" to quiet tcp connect denials\n"
 	"  - \"udp_bind\" to quiet udp bind denials\n"
 	"  - \"udp_connect\" to quiet udp connect / send denials\n"
+	"  - \"mptcp_bind\" to quiet mptcp bind denials\n"
+	"  - \"mptcp_connect\" to quiet mptcp connect denials\n"
 	"  - \"abstract_unix_socket\" to quiet abstract unix socket denials\n"
 	"  - \"signal\" to quiet signal denials\n"
 	"\n"
@@ -449,7 +465,9 @@ int main(const int argc, char *const argv[], char *const *const envp)
 		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
 				      LANDLOCK_ACCESS_NET_CONNECT_TCP |
 				      LANDLOCK_ACCESS_NET_BIND_UDP |
-				      LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
+				      LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP |
+				      LANDLOCK_ACCESS_NET_BIND_MPTCP |
+				      LANDLOCK_ACCESS_NET_CONNECT_MPTCP,
 		.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
 			  LANDLOCK_SCOPE_SIGNAL,
 		.quiet_access_fs = 0,
@@ -556,6 +574,12 @@ int main(const int argc, char *const argv[], char *const *const envp)
 		supported_restrict_flags &=
 			~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
 		set_restrict_flags &= ~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
+		__attribute__((fallthrough));
+	case 11:
+		/* Removes MPTCP support for ABI < 12 */
+		ruleset_attr.handled_access_net &=
+			~(LANDLOCK_ACCESS_NET_BIND_MPTCP |
+			  LANDLOCK_ACCESS_NET_CONNECT_MPTCP);
 
 		/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
 		fprintf(stderr,
@@ -600,6 +624,18 @@ int main(const int argc, char *const argv[], char *const *const envp)
 		ruleset_attr.handled_access_net &=
 			~LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
 	}
+	/* Removes MPTCP bind access control if not supported by a user. */
+	env_port_name = getenv(ENV_MPTCP_BIND_NAME);
+	if (!env_port_name) {
+		ruleset_attr.handled_access_net &=
+			~LANDLOCK_ACCESS_NET_BIND_MPTCP;
+	}
+	/* Removes MPTCP connect access control if not supported by a user. */
+	env_port_name = getenv(ENV_MPTCP_CONNECT_NAME);
+	if (!env_port_name) {
+		ruleset_attr.handled_access_net &=
+			~LANDLOCK_ACCESS_NET_CONNECT_MPTCP;
+	}
 
 	if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
 		return 1;
@@ -684,6 +720,18 @@ int main(const int argc, char *const argv[], char *const *const envp)
 				 0)) {
 		goto err_close_ruleset;
 	}
+	if (populate_ruleset_net(ENV_MPTCP_BIND_NAME, ruleset_fd,
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_BIND_MPTCP,
+				 0)) {
+		goto err_close_ruleset;
+	}
+	if (populate_ruleset_net(ENV_MPTCP_CONNECT_NAME, ruleset_fd,
+				 ruleset_attr.handled_access_net &
+					 LANDLOCK_ACCESS_NET_CONNECT_MPTCP,
+				 0)) {
+		goto err_close_ruleset;
+	}
 
 	if (quiet_supported) {
 		if (populate_ruleset_net(ENV_NET_QUIET_NAME, ruleset_fd, 0,
Signature · 1 line
2.55.0
#13 lore raw

PATCH 6/6 landlock: Document MPTCP access rights

In reply to Günther Noack

Describe LANDLOCK_ACCESS_NET_BIND_MPTCP and LANDLOCK_ACCESS_NET_CONNECT_MPTCP in the userspace API documentation.

Extend the tutorial to handle the new access rights.

Describe MPTCP restrictions in "previous limitations".

---
 Documentation/userspace-api/landlock.rst | 27 +++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
Documentation/userspace-api/landlock.rst +24 -3
index 84cb7bf6b3ed..8bd99430514b 100644
@@ -40,7 +40,7 @@ Filesystem rules
     and the related filesystem actions are defined with
     `filesystem access rights`.
 
-Network rules (since ABI v4 for TCP and v10 for UDP)
+Network rules (since ABI v4 for TCP, v10 for UDP, and v12 for MPTCP)
     For these rules, the object is a TCP or UDP port,
     and the related actions are defined with `network access rights`.
 
@@ -51,7 +51,7 @@ We first need to define the ruleset that will contain our rules.
 
 For this example, the ruleset will contain rules that only allow some
 filesystem read actions and some specific UDP and TCP actions. Filesystem
-write actions and other TCP/UDP actions will be denied.
+write actions and other TCP/UDP/MPTCP actions will be denied.
 
 The ruleset then needs to handle all these kinds of actions.  This is
 required for backward and forward compatibility (i.e. the kernel and user
@@ -83,7 +83,9 @@ to be explicit about the denied-by-default access rights.
             LANDLOCK_ACCESS_NET_BIND_TCP |
             LANDLOCK_ACCESS_NET_CONNECT_TCP |
             LANDLOCK_ACCESS_NET_BIND_UDP |
-            LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
+            LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP |
+            LANDLOCK_ACCESS_NET_BIND_MPTCP |
+            LANDLOCK_ACCESS_NET_CONNECT_MPTCP,
         .scoped =
             LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
             LANDLOCK_SCOPE_SIGNAL,
@@ -140,6 +142,12 @@ version, and only use the available subset of access rights:
         ruleset_attr.handled_access_net &=
             ~(LANDLOCK_ACCESS_NET_BIND_UDP |
               LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+        __attribute__((fallthrough));
+    case 10 ... 11:
+        /* Removes LANDLOCK_ACCESS_NET_*_MPTCP for ABI < 12 */
+        ruleset_attr.handled_access_net &=
+            ~(LANDLOCK_ACCESS_NET_BIND_MPTCP |
+              LANDLOCK_ACCESS_NET_CONNECT_MPTCP);
     }
 
 This enables the creation of an inclusive ruleset that will contain our rules.
@@ -834,6 +842,19 @@ with ``LANDLOCK_RESTRICT_SELF_TSYNC``, no_new_privs is set on all threads
 of the process.  As explained in the tutorial above, leaving no_new_privs
 unset is risky even when Landlock does not require it.
 
+MPTCP bind and connect (ABI < 12)
+---------------------------------
+
+Starting with the Landlock ABI version 12, it is possible to restrict MPTCP
+bind and connect actions with the ``LANDLOCK_ACCESS_NET_BIND_MPTCP`` and
+``LANDLOCK_ACCESS_NET_CONNECT_MPTCP`` access rights.
+
+Because MPTCP works on the same TCP port number space as plain TCP, it
+is recommended that rulesets denying TCP operations should also deny
+the equivalent MPTCP operations.  In particular, a listening port
+created through an ``IPPROTO_MPTCP`` socket is compatible with plain
+TCP clients as well.
+
 .. _kernel_support:
 
 Kernel support
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/6] samples/landlock: Implement best-effort fallback for network rules.

In reply to Günther Noack
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The best-effort fallback logic introduces an early return that bypasses `unsetenv()`, leaking…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The best-effort fallback logic introduces an early return that bypasses unsetenv(), leaking sandbox configuration environment variables to the child process. --

commit 9984581b44a9424255f8b9ce2c25354f65e0816c
Author: Günther Noack <[email protected]>

samples/landlock: Implement best-effort fallback for network rules.

This commit adds a best-effort fallback for network rules in the sandboxer sample tool. If the Landlock ABI version does not support a requested network feature, the operation is treated as a no-op instead of causing an error.

samples/landlock/sandboxer.c static int populate_ruleset_net(const char *const env_var, const int ruleset_fd, · line 204

Quoted 12 lines
samples/landlock/sandboxer.c +4 -0
index 030583273f3f7..1c514efecafbf 100644
@@ -198,6 +198,10 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
 		.allowed_access = allowed_access,
 	};
 
+	/* A rule without access rights and flags is a no-op. */
+	if (!allowed_access && !flags)
+		return 0;
+

[Severity: Medium]
Does this early return bypass the cleanup of the environment variable?

Looking at populate_ruleset_net(), if the tool runs on an older kernel where a network feature is not supported, the feature is masked out and allowed_access becomes 0. Taking this early return avoids the unsetenv(env_var) call further down in the function:

static int populate_ruleset_net(...)

{
    ...
    if (!allowed_access && !flags)
        return 0;

    env_port_name = getenv(env_var);
    if (!env_port_name)
        return 0;
    env_port_name = strdup(env_port_name);
    unsetenv(env_var);
    ...
}

Will this inadvertently leak the sandbox configuration environment variables (like LL_TCP_BIND) to the sandboxed child process?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#11 lore raw

Re: [PATCH 4/6] selftests/landlock: Add MPTCP network access tests

In reply to Günther Noack
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] MPTCP test coverage was omitted from `port_specific` and `ipv4` test fixtures, and their test…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] MPTCP test coverage was omitted from port_specific and ipv4 test fixtures, and their test logic hardcodes TCP_SANDBOX and UDP_SANDBOX, ignoring MPTCP_SANDBOX. --

commit 9e9cead822873e8f3cd15dd893c6f35928465c42
Author: Günther Noack <[email protected]>

selftests/landlock: Add MPTCP network access tests

Test LANDLOCK_ACCESS_NET_BIND_MPTCP and LANDLOCK_ACCESS_NET_CONNECT_MPTCP:

* Add the MPTCP_SANDBOX variant to the net_test fixtures.
* Introduce prot_*() helper functions for audit tests.
* Extend existing tests as needed for MPTCP, including the tcp_fastopen test.

tools/testing/selftests/landlock/net_test.c enum sandbox_type { · line 44

Quoted 10 lines
tools/testing/selftests/landlock/net_test.c +1 -0
index 3a0482beca5f4..fbb3a99bc3801 100644
@@ -40,6 +40,7 @@ enum sandbox_type {
 	/* This may be used to test rules that allow *and* deny accesses. */
 	TCP_SANDBOX,
 	UDP_SANDBOX,
+	MPTCP_SANDBOX,
 };

[Severity: Medium]
When adding MPTCP_SANDBOX, should this patch also update the ipv4 and port_specific fixtures to include MPTCP variants and handle MPTCP_SANDBOX in their test logic?

Currently, MPTCP test coverage appears to be omitted from the port_specific and ipv4 test fixtures. This means there is missing test coverage for MPTCP port-specific rules, specifically for LANDLOCK_RULE_NET_PORT rules enforcing boundary port constraints like zero port translation.

Additionally, their test logic hardcodes TCP_SANDBOX and UDP_SANDBOX:

tools/testing/selftests/landlock/net_test.c:

TEST_F(ipv4, from_unix_to_inet) {
    ...
	if (variant->sandbox == TCP_SANDBOX ||
	    variant->sandbox == UDP_SANDBOX) {
    ...
}

tools/testing/selftests/landlock/net_test.c:

TEST_F(port_specific, bind_connect_zero) {
    ...
	if (variant->sandbox == TCP_SANDBOX ||
	    variant->sandbox == UDP_SANDBOX) {
    ...
}

tools/testing/selftests/landlock/net_test.c:

TEST_F(port_specific, bind_connect_1023) {
    ...
	if (variant->sandbox == TCP_SANDBOX ||
	    variant->sandbox == UDP_SANDBOX) {
    ...
}

Because the sandbox setup logic is hardcoded using if conditions instead of the newly updated sandbox_bind_access() helper, if MPTCP variants are added to these fixtures in the future, the MPTCP_SANDBOX condition would evaluate to false, causing the tests to silently bypass ruleset enforcement and falsely report success.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#4 lore raw

Re: [PATCH 2/6] selftests/landlock: Generalize net test helpers for multiple socket types

In reply to Günther Noack
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete refactoring: Several test fixtures retain hardcoded TCP/UDP checks instead of using…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete refactoring: Several test fixtures retain hardcoded TCP/UDP checks instead of using the newly introduced generalized sandbox access helpers. --

commit e9dd22f54640c7d9439f23efd0049803e698947e
Author: Günther Noack <[email protected]>

selftests/landlock: Generalize net test helpers for multiple socket types

Create helper methods for determining the access rights to be tested based on socket type (TCP or UDP). This makes it simpler to add more socket types with similar bind(2) and connect(2) restrictions in the future.

Quoted 4 lines
tools/testing/selftests/landlock/net_test.c +0 -0
index a18761e0fd82f..3a0482beca5f4 100644

[ ... ]

Quoted 33 lines
tools/testing/selftests/landlock/net_test.c +24 -3
@@ -108,11 +108,41 @@ static bool prot_is_udp(const struct protocol_variant *const prot)
 static bool is_restricted(const struct protocol_variant *const prot,
 			  const enum sandbox_type sandbox)
 {
-	if (sandbox == TCP_SANDBOX)
+	switch (sandbox) {
+	case TCP_SANDBOX:
 		return prot_is_tcp(prot);
-	else if (sandbox == UDP_SANDBOX)
+	case UDP_SANDBOX:
 		return prot_is_udp(prot);
-	return false;
+	case NO_SANDBOX:
+	default:
+		return false;
+	}
+}
+
+static __u64 sandbox_bind_access(const enum sandbox_type sandbox)
+{
+	switch (sandbox) {
+	case TCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_BIND_TCP;
+	case UDP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_BIND_UDP;
+	case NO_SANDBOX:
+	default:
+		return 0;
+	}
+}
+
+static __u64 sandbox_connect_access(const enum sandbox_type sandbox)
+{

tools/testing/selftests/landlock/net_test.c static bool prot_is_udp(const struct protocol_variant *const prot) · line 146

Quoted 10 lines
tools/testing/selftests/landlock/net_test.c +9 -0
+	switch (sandbox) {
+	case TCP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_CONNECT_TCP;
+	case UDP_SANDBOX:
+		return LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+	case NO_SANDBOX:
+	default:
+		return 0;
+	}
 }

[Severity: Medium]
Are there other test fixtures that also need to be updated to use these new helpers?

It looks like TEST_F(ipv4, from_unix_to_inet), TEST_F(port_specific, bind_connect_zero), and TEST_F(port_specific, bind_connect_1023) still use the hardcoded TCP_SANDBOX and UDP_SANDBOX checks instead of the generalized sandbox access helpers:

tools/testing/selftests/landlock/net_test.c:TEST_F(ipv4, from_unix_to_inet)() {
    ...
	if (variant->sandbox == TCP_SANDBOX ||
	    variant->sandbox == UDP_SANDBOX) {
		const __u64 access_rights =
			(variant->sandbox == TCP_SANDBOX ?
				 LANDLOCK_ACCESS_NET_BIND_TCP |
					 LANDLOCK_ACCESS_NET_CONNECT_TCP :
				 LANDLOCK_ACCESS_NET_BIND_UDP |
					 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
    ...
}

Will this cause these tests to silently skip ruleset application or incorrectly fail if a new sandbox variant (such as MPTCP) is added in the future?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#14 lore raw
In reply to Günther Noack

Hi Günther,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Notice: Call Traces at boot time, rebooted and continued ⚠️
- KVM Validation: debug (only selftest_mptcp_join): Notice: Call Traces at boot time, rebooted and continued ⚠️
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Notice: Call Traces at boot time, rebooted and continued - Notice: Call Traces at shutdown time, ignored and continued ⚠️
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/33333805532

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/60feb91e76eb
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1153987

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

reviewer
#6 lore raw

Re: [PATCH 3/6] landlock: Add MPTCP bind and connect access rights

In reply to Günther Noack

Hi Günther,

Quoted 147 lines · Günther Noack

MPTCP sockets have equivalent bind(2) and connect(2) operations as TCP
sockets, but can not currently be restricted with Landlock without explicit MPTCP access rights.  As MPTCP operates on the same TCP port number space as TCP, this is a gap in Landlock's policies.

Add access rights for MPTCP bind(2) and connect(2) operations and document them in the header.

Treat TCP Fast Open the same as done for plain TCP in commit 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

The port numbers used in MPTCP subflows are negotiated by the kernel and therefore not subject to these access rights.

Bump the Landlock ABI version to 12.

---
 include/linux/landlock.h                     |  5 +- include/uapi/linux/landlock.h                | 24 +++++++ security/landlock/limits.h                   |  2 +- security/landlock/net.c                      | 68 ++++++++++++++---- --
 security/landlock/syscalls.c                 |  2 +- tools/testing/selftests/landlock/base_test.c |  2 +-

 6 files changed, 79 insertions(+), 24 deletions(-)
include/linux/landlock.h +4 -1
index 004cbd0b9298..b04ffc7caa21 100644
@@ -46,7 +46,10 @@
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_TCP,
 "connect_tcp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_UDP,
 "bind_udp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, \
-			     "connect_send_udp")
+			     "connect_send_udp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_MPTCP,
 "bind_mptcp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_MPTCP, \
+			     "connect_mptcp")


 #define _LANDLOCK_SCOPE_NAMES \
  _LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET, \

include/uapi/linux/landlock.h +0 -0
index cceda3b3b961..2a953ba7ce25 100644
@@ -448,6 +448,9 @@ struct landlock_net_port_attr {
   * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to the

given
  *   remote port. Support added in Landlock ABI version 4. *
+ * .. note:: These rights do not apply to MPTCP sockets, which have their own
+ *   access rights (see below).
+ *
  * And similarly for UDP port numbers:
  *
  * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given local

include/uapi/linux/landlock.h +0 -0
@@ -474,12 +477,33 @@ struct landlock_net_port_attr {
   * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination

address
  *   family is not supported for IPv6 UDP sockets: you will need to use a
  *   ``NULL`` address instead.
+ *
+ * MPTCP sockets (created with ``IPPROTO_MPTCP``) use TCP port numbers, but
+ * they are controlled by their own access rights:
+ *
+ * - %LANDLOCK_ACCESS_NET_BIND_MPTCP: Bind MPTCP sockets to the given local
+ *   port. Support added in Landlock ABI version 12.
+ * - %LANDLOCK_ACCESS_NET_CONNECT_MPTCP: Connect MPTCP sockets to the given
+ *   remote port. Support added in Landlock ABI version 12.
+ *
+ * .. note:: The TCP and the MPTCP access rights are independent, even though
+ *   they refer to the same port number space. Handling only
+ *   %LANDLOCK_ACCESS_NET_BIND_TCP and %LANDLOCK_ACCESS_NET_CONNECT_TCP leaves
+ *   MPTCP sockets unrestricted, and vice versa. A sandbox that wants to
+ *   control all TCP-based traffic needs to handle both sets.
+ *
+ * .. note:: These MPTCP access rights restrict the ports passed to
+ *   :manpage:bind(2) and :manpage:connect(2). The ports used in MPTCP
+ *   subflows are negotiated in the MPTCP protocol by the kernel and are not
+ *   subject to these restrictions.
  */
 /* clang-format off */
 #define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0)
 #define
LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
 #define LANDLOCK_ACCESS_NET_BIND_UDP (1ULL << 2)
 #define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP (1ULL << 3)

include/uapi/linux/landlock.h +2 -0
+#define LANDLOCK_ACCESS_NET_BIND_MPTCP			(1ULL << 4)
+#define LANDLOCK_ACCESS_NET_CONNECT_MPTCP		(1ULL << 5)

 /* clang-format on */

 /**

security/landlock/limits.h +0 -0
index 1a7c5fb8f6fd..d25e056b7ca2 100644
@@ -23,7 +23,7 @@
  #define
 LANDLOCK_MASK_ACCESS_FS		((LANDLOCK_LAST_ACCESS_FS << 1) - 1)
  #define
 LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)


-#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP
+#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_MPTCP
 #define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET
<< 1) - 1)
 #define
LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET)

security/landlock/net.c +1 -0
index 8f2aaac54b33..8541b0c07d64 100644
@@ -11,6 +11,7 @@
  #include <linux/net.h>
  #include <linux/socket.h>
  #include <net/ipv6.h>
+#include <net/mptcp.h>


 #include "common.h"
 #include "cred.h"

security/landlock/net.c +0 -0
@@ -53,6 +54,26 @@ int landlock_append_net_rule(struct
 landlock_ruleset *const ruleset,
  	return err;
  }

security/landlock/net.c int landlock_append_net_rule(struct landlock_ruleset *const ruleset, · line 61

Quoted 5 lines
security/landlock/net.c +5 -0
+static bool sk_is_mptcp_socket(const struct sock *sk)
+{
+	return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM &&
+	       sk->sk_protocol == IPPROTO_MPTCP;
+}

This helper should be placed in include/net/mptcp.h. I had already implemented one in [1], called sk_is_msk(), to differentiate it from sk_is_mptcp(). If you have no concerns with my implementation, please feel free to pick it up and use it in your series.

Thanks,
-Geliang

[1]
https://patchwork.kernel.org/project/mptcp/patch/e2727ba40084f261545f1cbb140a3fb9d295ba25.1765505775.git.tanggeliang@kylinos.cn/

Quoted 193 lines
security/landlock/net.c +5 -0
+
+static bool is_connect_access(const access_mask_t access_request)
+{
+	return access_request == LANDLOCK_ACCESS_NET_CONNECT_TCP ||
+	       access_request == LANDLOCK_ACCESS_NET_CONNECT_MPTCP

||

security/landlock/net.c +1 -0
+	       access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;

security/landlock/net.c +9 -0
+}
+
+static bool is_bind_access(const access_mask_t access_request)
+{
+	return access_request == LANDLOCK_ACCESS_NET_BIND_TCP ||
+	       access_request == LANDLOCK_ACCESS_NET_BIND_MPTCP ||
+	       access_request == LANDLOCK_ACCESS_NET_BIND_UDP;
+}
+

 static bool unmask_layers_net(const struct landlock_domain *const domain,
        const struct landlock_id id, struct layer_masks *masks,

security/landlock/net.c +0 -0
@@ -104,6 +125,7 @@ static int current_check_access_socket(struct
 socket *const sock,
  	switch (address->sa_family) {

  case AF_UNSPEC:
  if (access_request ==
LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +1 -0
+		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_MPTCP ||
      (access_request ==
LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP &&
       connecting)) {
  /*

security/landlock/net.c +0 -1
@@ -147,17 +169,15 @@ static int current_check_access_socket(struct
 socket *const sock,
  					});
  				return -EACCES;
  			}
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  /*
  * Binding to an AF_UNSPEC address is treated
  * differently by IPv4 and IPv6 sockets. The socket's
  * family may change under our feet due to
  * setsockopt(IPV6_ADDRFORM), but that's ok:
we either

security/landlock/net.c +0 -3
-			 * reject entirely for IPv6 or require
-			 * %LANDLOCK_ACCESS_NET_BIND_TCP or
-			 * %LANDLOCK_ACCESS_NET_BIND_UDP for IPv4,

so it cannot

security/landlock/net.c +1 -1
-			 * be used to bypass the policy.
+			 * reject entirely for IPv6 or require the

relevant bind

security/landlock/net.c +1 -0
+			 * access right for IPv4, so it cannot be

used to bypass

security/landlock/net.c +1 -0
+			 * the policy.

  *
  * IPv4 sockets map AF_UNSPEC to AF_INET for
  * retrocompatibility for bind accesses, only if the

security/landlock/net.c +0 -0
@@ -204,12 +224,10 @@ static int current_check_access_socket(struct
 socket *const sock,
  		addr4 = (struct sockaddr_in *)address;
  		port = addr4->sin_port;

security/landlock/net.c +0 -1
-		if (access_request ==

LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +0 -1
-		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP) {

security/landlock/net.c +1 -0
+		if (is_connect_access(access_request)) {

  audit_net.dport = port; audit_net.v4info.daddr = addr4-

Quoted 1 line

sin_addr.s_addr;

security/landlock/net.c +0 -1
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  audit_net.sport = port; audit_net.v4info.saddr = addr4-

Quoted 1 line

sin_addr.s_addr;

  } else {

security/landlock/net.c +0 -0
@@ -228,12 +246,10 @@ static int current_check_access_socket(struct
 socket *const sock,
  		addr6 = (struct sockaddr_in6 *)address;
  		port = addr6->sin6_port;

security/landlock/net.c +0 -1
-		if (access_request ==

LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +0 -1
-		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP) {

security/landlock/net.c +1 -0
+		if (is_connect_access(access_request)) {

  audit_net.dport = port; audit_net.v6info.daddr = addr6->sin6_addr;

security/landlock/net.c +0 -1
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  audit_net.sport = port; audit_net.v6info.saddr = addr6->sin6_addr; } else {

security/landlock/net.c +0 -0
@@ -331,6 +347,8 @@ static int hook_socket_bind(struct socket *const
 sock,


  if (sk_is_tcp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_BIND_TCP;

security/landlock/net.c +2 -0
+	else if (sk_is_mptcp_socket(sock->sk))
+		access_request = LANDLOCK_ACCESS_NET_BIND_MPTCP;

  else if (sk_is_udp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_BIND_UDP; else

security/landlock/net.c +0 -0
@@ -349,6 +367,8 @@ static int hook_socket_connect(struct socket
 *const sock,


  if (sk_is_tcp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_CONNECT_TCP;

security/landlock/net.c +2 -0
+	else if (sk_is_mptcp_socket(sock->sk))
+		access_request = LANDLOCK_ACCESS_NET_CONNECT_MPTCP;

  else if (sk_is_udp(sock->sk))
  access_request =
LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
  else

security/landlock/net.c +0 -0
@@ -377,12 +397,20 @@ static int hook_socket_sendmsg(struct socket
 *const sock,
  	access_mask_t access_request;
  	int ret = 0;

security/landlock/net.c +0 -1
-	if ((msg->msg_flags & MSG_FASTOPEN) && address &&

sk_is_tcp(sock->sk)) {

security/landlock/net.c +0 -2
-		ret = current_check_access_socket(
-			sock, address, addrlen,

LANDLOCK_ACCESS_NET_CONNECT_TCP,

security/landlock/net.c +5 -3
-			true);
-		if (ret != 0)
-			return ret;
+	if ((msg->msg_flags & MSG_FASTOPEN) && address) {
+		access_mask_t fastopen_access = 0;
+
+		if (sk_is_tcp(sock->sk))
+			fastopen_access =

LANDLOCK_ACCESS_NET_CONNECT_TCP;

security/landlock/net.c +2 -0
+		else if (sk_is_mptcp_socket(sock->sk))
+			fastopen_access =

LANDLOCK_ACCESS_NET_CONNECT_MPTCP;

security/landlock/net.c +4 -0
+
+		if (fastopen_access) {
+			ret = current_check_access_socket(
+				sock, address, addrlen,

fastopen_access, true);

security/landlock/net.c +3 -0
+			if (ret != 0)
+				return ret;
+		}

  }

  if (sk_is_udp(sock->sk))

security/landlock/syscalls.c +0 -0
index 1d02d57f4c48..cc54d4f1d502 100644
@@ -172,7 +172,7 @@ static const struct file_operations ruleset_fops
 = {
   * If the change involves a fix that requires userspace awareness,

also update
  * the errata documentation in Documentation/userspace- api/landlock.rst .
  */

security/landlock/syscalls.c +1 -1
-const int landlock_abi_version = 11;
+const int landlock_abi_version = 12;


 /**
  * sys_landlock_create_ruleset - Create a new ruleset

tools/testing/selftests/landlock/base_test.c +1 -1
index d20ab8f0862c..58fe322d8637 100644
@@ -76,7 +76,7 @@ TEST(abi_version)
  	const struct landlock_ruleset_attr ruleset_attr = {
  		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
  	};
-	ASSERT_EQ(11, landlock_create_ruleset(NULL, 0,
+	ASSERT_EQ(12, landlock_create_ruleset(NULL, 0,


LANDLOCK_CREATE_RULESET_VERSION));

  ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,

reviewer
#7 lore raw

Re: [PATCH 3/6] landlock: Add MPTCP bind and connect access rights

Hi Günther,

Quoted 173 lines · Geliang Tang

Hi Günther,

Quoted 170 lines · Günther Noack

MPTCP sockets have equivalent bind(2) and connect(2) operations as TCP
sockets, but can not currently be restricted with Landlock without explicit MPTCP access rights.  As MPTCP operates on the same TCP port
number space as TCP, this is a gap in Landlock's policies.

Add access rights for MPTCP bind(2) and connect(2) operations and document them in the header.

Treat TCP Fast Open the same as done for plain TCP in commit 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

The port numbers used in MPTCP subflows are negotiated by the kernel
and therefore not subject to these access rights.

Bump the Landlock ABI version to 12.

---
 include/linux/landlock.h                     |  5 +- include/uapi/linux/landlock.h                | 24 +++++++ security/landlock/limits.h                   |  2 +- security/landlock/net.c                      | 68 ++++++++++++++-- --
--
 security/landlock/syscalls.c                 |  2 +- tools/testing/selftests/landlock/base_test.c |  2 +-

 6 files changed, 79 insertions(+), 24 deletions(-)
include/linux/landlock.h +2 -1
index 004cbd0b9298..b04ffc7caa21 100644
@@ -46,7 +46,10 @@
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_TCP,
 "connect_tcp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_UDP,
 "bind_udp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
 \
-			     "connect_send_udp")
+			     "connect_send_udp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_MPTCP,

"bind_mptcp"), \

include/linux/landlock.h +2 -0
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_MPTCP, \
+			     "connect_mptcp")


 #define _LANDLOCK_SCOPE_NAMES \
  _LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET, \

include/uapi/linux/landlock.h +0 -0
index cceda3b3b961..2a953ba7ce25 100644
@@ -448,6 +448,9 @@ struct landlock_net_port_attr {
   * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to the

given
  *   remote port. Support added in Landlock ABI version 4. *
+ * .. note:: These rights do not apply to MPTCP sockets, which have
their own
+ *   access rights (see below).
+ *
  * And similarly for UDP port numbers:
  *
  * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given local

include/uapi/linux/landlock.h +0 -0
@@ -474,12 +477,33 @@ struct landlock_net_port_attr {
   * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination

address
  *   family is not supported for IPv6 UDP sockets: you will need to
use a
  *   ``NULL`` address instead.
+ *
+ * MPTCP sockets (created with ``IPPROTO_MPTCP``) use TCP port numbers, but
+ * they are controlled by their own access rights:
+ *
+ * - %LANDLOCK_ACCESS_NET_BIND_MPTCP: Bind MPTCP sockets to the given local
+ *   port. Support added in Landlock ABI version 12.
+ * - %LANDLOCK_ACCESS_NET_CONNECT_MPTCP: Connect MPTCP sockets to the given
+ *   remote port. Support added in Landlock ABI version 12.
+ *
+ * .. note:: The TCP and the MPTCP access rights are independent, even though
+ *   they refer to the same port number space. Handling only
+ *   %LANDLOCK_ACCESS_NET_BIND_TCP and %LANDLOCK_ACCESS_NET_CONNECT_TCP leaves
+ *   MPTCP sockets unrestricted, and vice versa. A sandbox that wants to
+ *   control all TCP-based traffic needs to handle both sets.
+ *
+ * .. note:: These MPTCP access rights restrict the ports passed to
+ *   :manpage:bind(2) and :manpage:connect(2). The ports used in
MPTCP
+ *   subflows are negotiated in the MPTCP protocol by the kernel and
are not
+ *   subject to these restrictions.
  */
 /* clang-format off */
 #define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL <<
0)
 #define
LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
 #define LANDLOCK_ACCESS_NET_BIND_UDP (1ULL <<
2)
 #define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP (1ULL <<
3)
+#define LANDLOCK_ACCESS_NET_BIND_MPTCP (1ULL <<
4)
+#define LANDLOCK_ACCESS_NET_CONNECT_MPTCP (1ULL <<
5)
 /* clang-format on */

 /**

security/landlock/limits.h +0 -0
index 1a7c5fb8f6fd..d25e056b7ca2 100644
@@ -23,7 +23,7 @@
  #define

LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) -
1)
 #define
LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_AC
CESS_FS)

-#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_SEND_U
DP
+#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_MPTCP
 #define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET
<< 1) - 1)
 #define
LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_AC
CESS_NET)

security/landlock/net.c +1 -0
index 8f2aaac54b33..8541b0c07d64 100644
@@ -11,6 +11,7 @@
  #include <linux/net.h>
  #include <linux/socket.h>
  #include <net/ipv6.h>
+#include <net/mptcp.h>


 #include "common.h"
 #include "cred.h"

security/landlock/net.c +0 -0
@@ -53,6 +54,26 @@ int landlock_append_net_rule(struct
 landlock_ruleset *const ruleset,
  	return err;
  }

security/landlock/net.c +2 -0
+static bool sk_is_mptcp_socket(const struct sock *sk)
+{
Quoted 8 lines

security/landlock/net.c int landlock_append_net_rule(struct landlock_ruleset *const ruleset, · line 61

Quoted 3 lines
security/landlock/net.c +3 -0
+	return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM &&
+	       sk->sk_protocol == IPPROTO_MPTCP;
+}

This helper should be placed in include/net/mptcp.h. I had already implemented one in [1], called sk_is_msk(), to differentiate it from sk_is_mptcp(). If you have no concerns with my implementation, please feel free to pick it up and use it in your series.

Sorry, let me correct myself. I thought about this more carefully, and the ideal place to define sk_is_msk() is in include/net/sock.h, between sk_is_tcp() and sk_is_udp():

static inline bool sk_is_tcp(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_STREAM &&
               sk->sk_protocol == IPPROTO_TCP;
}

static inline bool sk_is_msk(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_STREAM &&
               sk->sk_protocol == IPPROTO_MPTCP;
}

static inline bool sk_is_udp(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_DGRAM &&
               sk->sk_protocol == IPPROTO_UDP;
}

If possible, please fold this code into this patch.

Thanks,
-Geliang

Quoted 207 lines

Thanks,
-Geliang

[1]
https://patchwork.kernel.org/project/mptcp/patch/e2727ba40084f261545f1cbb140a3fb9d295ba25.1765505775.git.tanggeliang@kylinos.cn/

Quoted 200 lines
security/landlock/net.c +4 -0
+
+static bool is_connect_access(const access_mask_t access_request)
+{
+	return access_request == LANDLOCK_ACCESS_NET_CONNECT_TCP

||

security/landlock/net.c +1 -0
+	       access_request == LANDLOCK_ACCESS_NET_CONNECT_MPTCP
Quoted 1 line
Quoted 1 line
security/landlock/net.c +1 -0
+	       access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;

security/landlock/net.c +9 -0
+}
+
+static bool is_bind_access(const access_mask_t access_request)
+{
+	return access_request == LANDLOCK_ACCESS_NET_BIND_TCP ||
+	       access_request == LANDLOCK_ACCESS_NET_BIND_MPTCP ||
+	       access_request == LANDLOCK_ACCESS_NET_BIND_UDP;
+}
+

 static bool unmask_layers_net(const struct landlock_domain *const domain,
        const struct landlock_id id, struct layer_masks *masks,

security/landlock/net.c +0 -0
@@ -104,6 +125,7 @@ static int current_check_access_socket(struct
 socket *const sock,
  	switch (address->sa_family) {

  case AF_UNSPEC:
  if (access_request ==
LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +1 -0
+		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_MPTCP ||
      (access_request ==
LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP &&
       connecting)) {
  /*

security/landlock/net.c +0 -1
@@ -147,17 +169,15 @@ static int current_check_access_socket(struct
 socket *const sock,
  					});
  				return -EACCES;
  			}
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  /*
  * Binding to an AF_UNSPEC address is treated
  * differently by IPv4 and IPv6 sockets. The
socket's
  * family may change under our feet due to
  * setsockopt(IPV6_ADDRFORM), but that's ok:
we either

security/landlock/net.c +0 -3
-			 * reject entirely for IPv6 or require
-			 * %LANDLOCK_ACCESS_NET_BIND_TCP or
-			 * %LANDLOCK_ACCESS_NET_BIND_UDP for IPv4,

so it cannot

security/landlock/net.c +1 -1
-			 * be used to bypass the policy.
+			 * reject entirely for IPv6 or require the

relevant bind

security/landlock/net.c +1 -0
+			 * access right for IPv4, so it cannot be

used to bypass

security/landlock/net.c +1 -0
+			 * the policy.

  *
  * IPv4 sockets map AF_UNSPEC to AF_INET for
  * retrocompatibility for bind accesses, only if the

security/landlock/net.c +0 -0
@@ -204,12 +224,10 @@ static int current_check_access_socket(struct
 socket *const sock,
  		addr4 = (struct sockaddr_in *)address;
  		port = addr4->sin_port;

security/landlock/net.c +0 -1
-		if (access_request ==

LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +0 -1
-		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP) {

security/landlock/net.c +1 -0
+		if (is_connect_access(access_request)) {

  audit_net.dport = port; audit_net.v4info.daddr = addr4-

Quoted 1 line

sin_addr.s_addr;

security/landlock/net.c +0 -1
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  audit_net.sport = port; audit_net.v4info.saddr = addr4-

Quoted 1 line

sin_addr.s_addr;

  } else {

security/landlock/net.c +0 -0
@@ -228,12 +246,10 @@ static int current_check_access_socket(struct
 socket *const sock,
  		addr6 = (struct sockaddr_in6 *)address;
  		port = addr6->sin6_port;

security/landlock/net.c +0 -1
-		if (access_request ==

LANDLOCK_ACCESS_NET_CONNECT_TCP ||

security/landlock/net.c +0 -1
-		    access_request ==

LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP) {

security/landlock/net.c +1 -0
+		if (is_connect_access(access_request)) {

  audit_net.dport = port; audit_net.v6info.daddr = addr6->sin6_addr;

security/landlock/net.c +0 -1
-		} else if (access_request ==

LANDLOCK_ACCESS_NET_BIND_TCP ||

security/landlock/net.c +0 -1
-			   access_request ==

LANDLOCK_ACCESS_NET_BIND_UDP) {

security/landlock/net.c +1 -0
+		} else if (is_bind_access(access_request)) {

  audit_net.sport = port; audit_net.v6info.saddr = addr6->sin6_addr; } else {

security/landlock/net.c +0 -0
@@ -331,6 +347,8 @@ static int hook_socket_bind(struct socket
 *const
 sock,


  if (sk_is_tcp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_BIND_TCP;

security/landlock/net.c +2 -0
+	else if (sk_is_mptcp_socket(sock->sk))
+		access_request = LANDLOCK_ACCESS_NET_BIND_MPTCP;

  else if (sk_is_udp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_BIND_UDP; else

security/landlock/net.c +0 -0
@@ -349,6 +367,8 @@ static int hook_socket_connect(struct socket
 *const sock,


  if (sk_is_tcp(sock->sk))
  access_request = LANDLOCK_ACCESS_NET_CONNECT_TCP;

security/landlock/net.c +2 -0
+	else if (sk_is_mptcp_socket(sock->sk))
+		access_request =

LANDLOCK_ACCESS_NET_CONNECT_MPTCP;
  else if (sk_is_udp(sock->sk))
  access_request =
LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
  else

security/landlock/net.c +0 -0
@@ -377,12 +397,20 @@ static int hook_socket_sendmsg(struct socket
 *const sock,
  	access_mask_t access_request;
  	int ret = 0;

security/landlock/net.c +0 -1
-	if ((msg->msg_flags & MSG_FASTOPEN) && address &&

sk_is_tcp(sock->sk)) {

security/landlock/net.c +0 -2
-		ret = current_check_access_socket(
-			sock, address, addrlen,

LANDLOCK_ACCESS_NET_CONNECT_TCP,

security/landlock/net.c +5 -3
-			true);
-		if (ret != 0)
-			return ret;
+	if ((msg->msg_flags & MSG_FASTOPEN) && address) {
+		access_mask_t fastopen_access = 0;
+
+		if (sk_is_tcp(sock->sk))
+			fastopen_access =

LANDLOCK_ACCESS_NET_CONNECT_TCP;

security/landlock/net.c +2 -0
+		else if (sk_is_mptcp_socket(sock->sk))
+			fastopen_access =

LANDLOCK_ACCESS_NET_CONNECT_MPTCP;

security/landlock/net.c +4 -0
+
+		if (fastopen_access) {
+			ret = current_check_access_socket(
+				sock, address, addrlen,

fastopen_access, true);

security/landlock/net.c +3 -0
+			if (ret != 0)
+				return ret;
+		}

  }

  if (sk_is_udp(sock->sk))

security/landlock/syscalls.c +0 -0
index 1d02d57f4c48..cc54d4f1d502 100644
@@ -172,7 +172,7 @@ static const struct file_operations

ruleset_fops
= {
  * If the change involves a fix that requires userspace awareness, also update
  * the errata documentation in Documentation/userspace- api/landlock.rst .
  */

security/landlock/syscalls.c +1 -1
-const int landlock_abi_version = 11;
+const int landlock_abi_version = 12;


 /**
  * sys_landlock_create_ruleset - Create a new ruleset

tools/testing/selftests/landlock/base_test.c +1 -1
index d20ab8f0862c..58fe322d8637 100644
@@ -76,7 +76,7 @@ TEST(abi_version)
  	const struct landlock_ruleset_attr ruleset_attr = {
  		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
  	};
-	ASSERT_EQ(11, landlock_create_ruleset(NULL, 0,
+	ASSERT_EQ(12, landlock_create_ruleset(NULL, 0,


LANDLOCK_CREATE_RULESET_VERSION));

  ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,

#8 lore raw

Re: [PATCH 3/6] landlock: Add MPTCP bind and connect access rights

Quoted 199 lines · Geliang Tang
Quoted 171 lines · Geliang Tang
Quoted 170 lines · Günther Noack

MPTCP sockets have equivalent bind(2) and connect(2) operations as TCP
sockets, but can not currently be restricted with Landlock without explicit MPTCP access rights.  As MPTCP operates on the same TCP port
number space as TCP, this is a gap in Landlock's policies.

Add access rights for MPTCP bind(2) and connect(2) operations and document them in the header.

Treat TCP Fast Open the same as done for plain TCP in commit 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

The port numbers used in MPTCP subflows are negotiated by the kernel
and therefore not subject to these access rights.

Bump the Landlock ABI version to 12.

---
 include/linux/landlock.h                     |  5 +- include/uapi/linux/landlock.h                | 24 +++++++ security/landlock/limits.h                   |  2 +- security/landlock/net.c                      | 68 ++++++++++++++-- --
--
 security/landlock/syscalls.c                 |  2 +- tools/testing/selftests/landlock/base_test.c |  2 +-

 6 files changed, 79 insertions(+), 24 deletions(-)
include/linux/landlock.h +2 -1
index 004cbd0b9298..b04ffc7caa21 100644
@@ -46,7 +46,10 @@
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_TCP,
 "connect_tcp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_UDP,
 "bind_udp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
 \
-			     "connect_send_udp")
+			     "connect_send_udp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_MPTCP,

"bind_mptcp"), \

include/linux/landlock.h +2 -0
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_MPTCP, \
+			     "connect_mptcp")


 #define _LANDLOCK_SCOPE_NAMES \
  _LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET, \

include/uapi/linux/landlock.h +0 -0
index cceda3b3b961..2a953ba7ce25 100644
@@ -448,6 +448,9 @@ struct landlock_net_port_attr {
   * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to the

given
  *   remote port. Support added in Landlock ABI version 4. *
+ * .. note:: These rights do not apply to MPTCP sockets, which have
their own
+ *   access rights (see below).
+ *
  * And similarly for UDP port numbers:
  *
  * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given local

include/uapi/linux/landlock.h +0 -0
@@ -474,12 +477,33 @@ struct landlock_net_port_attr {
   * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination

address
  *   family is not supported for IPv6 UDP sockets: you will need to
use a
  *   ``NULL`` address instead.
+ *
+ * MPTCP sockets (created with ``IPPROTO_MPTCP``) use TCP port numbers, but
+ * they are controlled by their own access rights:
+ *
+ * - %LANDLOCK_ACCESS_NET_BIND_MPTCP: Bind MPTCP sockets to the given local
+ *   port. Support added in Landlock ABI version 12.
+ * - %LANDLOCK_ACCESS_NET_CONNECT_MPTCP: Connect MPTCP sockets to the given
+ *   remote port. Support added in Landlock ABI version 12.
+ *
+ * .. note:: The TCP and the MPTCP access rights are independent, even though
+ *   they refer to the same port number space. Handling only
+ *   %LANDLOCK_ACCESS_NET_BIND_TCP and %LANDLOCK_ACCESS_NET_CONNECT_TCP leaves
+ *   MPTCP sockets unrestricted, and vice versa. A sandbox that wants to
+ *   control all TCP-based traffic needs to handle both sets.
+ *
+ * .. note:: These MPTCP access rights restrict the ports passed to
+ *   :manpage:bind(2) and :manpage:connect(2). The ports used in
MPTCP
+ *   subflows are negotiated in the MPTCP protocol by the kernel and
are not
+ *   subject to these restrictions.
  */
 /* clang-format off */
 #define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL <<
0)
 #define
LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
 #define LANDLOCK_ACCESS_NET_BIND_UDP (1ULL <<
2)
 #define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP (1ULL <<
3)
+#define LANDLOCK_ACCESS_NET_BIND_MPTCP (1ULL <<
4)
+#define LANDLOCK_ACCESS_NET_CONNECT_MPTCP (1ULL <<
5)
 /* clang-format on */

 /**

security/landlock/limits.h +0 -0
index 1a7c5fb8f6fd..d25e056b7ca2 100644
@@ -23,7 +23,7 @@
  #define

LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) -
1)
 #define
LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_AC
CESS_FS)

-#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_SEND_U
DP
+#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_MPTCP
 #define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET
<< 1) - 1)
 #define
LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_AC
CESS_NET)

security/landlock/net.c +1 -0
index 8f2aaac54b33..8541b0c07d64 100644
@@ -11,6 +11,7 @@
  #include <linux/net.h>
  #include <linux/socket.h>
  #include <net/ipv6.h>
+#include <net/mptcp.h>


 #include "common.h"
 #include "cred.h"

security/landlock/net.c +0 -0
@@ -53,6 +54,26 @@ int landlock_append_net_rule(struct
 landlock_ruleset *const ruleset,
  	return err;
  }

security/landlock/net.c +2 -0
+static bool sk_is_mptcp_socket(const struct sock *sk)
+{
Quoted 8 lines

security/landlock/net.c int landlock_append_net_rule(struct landlock_ruleset *const ruleset, · line 61

Quoted 3 lines
security/landlock/net.c +3 -0
+	return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM &&
+	       sk->sk_protocol == IPPROTO_MPTCP;
+}

This helper should be placed in include/net/mptcp.h. I had already implemented one in [1], called sk_is_msk(), to differentiate it from sk_is_mptcp(). If you have no concerns with my implementation, please feel free to pick it up and use it in your series.

Sorry, let me correct myself. I thought about this more carefully, and the ideal place to define sk_is_msk() is in include/net/sock.h, between sk_is_tcp() and sk_is_udp():

static inline bool sk_is_tcp(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_STREAM &&
               sk->sk_protocol == IPPROTO_TCP;
}

static inline bool sk_is_msk(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_STREAM &&
               sk->sk_protocol == IPPROTO_MPTCP;
}
Quoted 8 lines

static inline bool sk_is_udp(const struct sock *sk)

{
        return sk_is_inet(sk) &&
               sk->sk_type == SOCK_DGRAM &&
               sk->sk_protocol == IPPROTO_UDP;
}

If possible, please fold this code into this patch.

Thank you for the review, Geliang!

I'll include something like this in the next review round. (We are a bit bottlenecked on doing Landlock code reviews at the moment, so some delays are unfortunately expected.)

Small question about the function name sk_is_msk(): Is "msk" the right name for this? It seems to be used as abbreviation for "MPTCP" in net/mptcp, but it's also used in net/mctp as an abbreviation, and I could not find a function name in include/net/mptcp.h which used "msk" to mean "MPTCP". Should that say sk_is_mptcp() instead?

Thanks,
—Günther

reviewer
#9 lore raw

Re: [PATCH 3/6] landlock: Add MPTCP bind and connect access rights

Hi Günther,

Quoted 250 lines · Günther Noack
Quoted 233 lines · Geliang Tang
Quoted 204 lines · Geliang Tang
Quoted 197 lines · Günther Noack

MPTCP sockets have equivalent bind(2) and connect(2) operations as
TCP
sockets, but can not currently be restricted with Landlock without
explicit MPTCP access rights.  As MPTCP operates on the same TCP
port
number space as TCP, this is a gap in Landlock's policies.

Add access rights for MPTCP bind(2) and connect(2) operations and document them in the header.

Treat TCP Fast Open the same as done for plain TCP in commit 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

The port numbers used in MPTCP subflows are negotiated by the kernel
and therefore not subject to these access rights.

Bump the Landlock ABI version to 12.

---
 include/linux/landlock.h                     |  5 +- include/uapi/linux/landlock.h                | 24 +++++++ security/landlock/limits.h                   |  2 +- security/landlock/net.c                      | 68
++++++++++++++--
--
--
 security/landlock/syscalls.c                 |  2 +- tools/testing/selftests/landlock/base_test.c |  2 +-

 6 files changed, 79 insertions(+), 24 deletions(-)
include/linux/landlock.h +0 -0
index 004cbd0b9298..b04ffc7caa21 100644
@@ -46,7 +46,10 @@
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_TCP,
 "connect_tcp"), \
  	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_UDP,
 "bind_udp"), \

  _LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_SEND_ UDP,
\

include/linux/landlock.h +2 -1
-			     "connect_send_udp")
+			     "connect_send_udp"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_BIND_MPTCP,

"bind_mptcp"), \

include/linux/landlock.h +1 -0
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_ACCESS_NET_CONNECT_MPTCP

, \

include/linux/landlock.h +1 -0
+			     "connect_mptcp")


 #define _LANDLOCK_SCOPE_NAMES \
  _LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCK ET,
\

include/uapi/linux/landlock.h +0 -0
index cceda3b3b961..2a953ba7ce25 100644
@@ -448,6 +448,9 @@ struct landlock_net_port_attr {
   * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to

the
given
  *   remote port. Support added in Landlock ABI version 4. *
+ * .. note:: These rights do not apply to MPTCP sockets, which have
their own
+ *   access rights (see below).
+ *
  * And similarly for UDP port numbers:
  *
  * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given
local

include/uapi/linux/landlock.h +0 -0
@@ -474,12 +477,33 @@ struct landlock_net_port_attr {
   * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination

address
  *   family is not supported for IPv6 UDP sockets: you will need
to
use a
  *   ``NULL`` address instead.
+ *
+ * MPTCP sockets (created with ``IPPROTO_MPTCP``) use TCP port numbers, but
+ * they are controlled by their own access rights:
+ *
+ * - %LANDLOCK_ACCESS_NET_BIND_MPTCP: Bind MPTCP sockets to the
given local
+ *   port. Support added in Landlock ABI version 12.
+ * - %LANDLOCK_ACCESS_NET_CONNECT_MPTCP: Connect MPTCP sockets to
the given
+ *   remote port. Support added in Landlock ABI version 12.
+ *
+ * .. note:: The TCP and the MPTCP access rights are independent,
even though
+ *   they refer to the same port number space. Handling only
+ *   %LANDLOCK_ACCESS_NET_BIND_TCP and %LANDLOCK_ACCESS_NET_CONNECT_TCP leaves
+ *   MPTCP sockets unrestricted, and vice versa. A sandbox that
wants to
+ *   control all TCP-based traffic needs to handle both sets.
+ *
+ * .. note:: These MPTCP access rights restrict the ports passed
to
+ *   :manpage:bind(2) and :manpage:connect(2). The ports used
in
MPTCP
+ *   subflows are negotiated in the MPTCP protocol by the kernel
and
are not
+ *   subject to these restrictions.
  */
 /* clang-format off */
 #define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL
<<
0)
 #define
LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
 #define LANDLOCK_ACCESS_NET_BIND_UDP (1ULL
<<
2)
 #define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP (1ULL
<<
3)
+#define LANDLOCK_ACCESS_NET_BIND_MPTCP (1ULL
<<
4)
+#define LANDLOCK_ACCESS_NET_CONNECT_MPTCP (1ULL
<<
5)
 /* clang-format on */

 /**

security/landlock/limits.h +0 -0
index 1a7c5fb8f6fd..d25e056b7ca2 100644
@@ -23,7 +23,7 @@
  #define

LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS <<
1) -
1)
 #define
LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MAS
K_AC
CESS_FS)

-#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_SE
ND_U
DP
+#define
LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_MP
TCP
 #define
LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET
<< 1) - 1)
 #define
LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MAS
K_AC
CESS_NET)

security/landlock/net.c +1 -0
index 8f2aaac54b33..8541b0c07d64 100644
@@ -11,6 +11,7 @@
  #include <linux/net.h>
  #include <linux/socket.h>
  #include <net/ipv6.h>
+#include <net/mptcp.h>


 #include "common.h"
 #include "cred.h"

security/landlock/net.c +0 -0
@@ -53,6 +54,26 @@ int landlock_append_net_rule(struct
 landlock_ruleset *const ruleset,
  	return err;
  }

security/landlock/net.c int landlock_append_net_rule(struct landlock_ruleset *const ruleset, · line 61

Quoted 5 lines
security/landlock/net.c +5 -0
+static bool sk_is_mptcp_socket(const struct sock *sk)
+{
+	return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM &&
+	       sk->sk_protocol == IPPROTO_MPTCP;
+}
Quoted 7 lines

This helper should be placed in include/net/mptcp.h. I had already
implemented one in [1], called sk_is_msk(), to differentiate it from
sk_is_mptcp(). If you have no concerns with my implementation, please
feel free to pick it up and use it in your series.

Sorry, let me correct myself. I thought about this more carefully, and
the ideal place to define sk_is_msk() is in include/net/sock.h, between
sk_is_tcp() and sk_is_udp():

static inline bool sk_is_tcp(const struct sock *sk) {
        return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM && sk->sk_protocol == IPPROTO_TCP; }

static inline bool sk_is_msk(const struct sock *sk) {
        return sk_is_inet(sk) && sk->sk_type == SOCK_STREAM && sk->sk_protocol == IPPROTO_MPTCP; }

Quoted 8 lines

static inline bool sk_is_udp(const struct sock *sk) {
        return sk_is_inet(sk) && sk->sk_type == SOCK_DGRAM && sk->sk_protocol == IPPROTO_UDP; }

If possible, please fold this code into this patch.

Thank you for the review, Geliang!

I'll include something like this in the next review round. (We are a bit bottlenecked on doing Landlock code reviews at the moment,
so some delays are unfortunately expected.)

Quoted 4 lines

Small question about the function name sk_is_msk(): Is "msk" the right
name for this?  It seems to be used as abbreviation for "MPTCP" in net/mptcp, but it's also used in net/mctp as an abbreviation, and I

Thanks for the heads-up! I hadn't considered that "msk" is also used as an abbreviation in net/mctp.

Quoted 3 lines

could not find a function name in include/net/mptcp.h which used "msk"
to mean "MPTCP".  Should that say sk_is_mptcp() instead?

However, sk_is_mptcp() is already taken - it's defined in include/net/mptcp.h and used to check whether a socket is an MPTCP subflow (tcp_sk(sk)->is_mptcp).

So I'd like to propose the following rename scheme:

- Rename the existing sk_is_mptcp() (subflow check) to ssk_is_mptcp(), where "ssk" stands for "subflow socket".

- Rename sk_is_msk() (the one I introduced here) to sk_is_mptcp(), which checks whether the socket is the MPTCP socket itself.

This would give a consistent naming scheme:

  sk_is_mptcp()    - is this the MPTCP socket?
  ssk_is_mptcp()   - is this an MPTCP subflow?
  rsk_is_mptcp()   - is this an MPTCP request subflow?

We'll need to discuss this with the MPTCP maintainers before proceeding.

Thanks,
-Geliang

Quoted 3 lines

Thanks,
—Günther