Skip to contentSkip to conversation

tomoyo: Enforce connect policy in TCP Fast Open

PATCH In review Matthieu Buffet 2 participants 4 messages Jun 19 – Oct 4

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

PATCH 1/1 tomoyo: Enforce connect policy in TCP Fast Open

Tomoyo restricted TCP connections in 2011 in commit 059d84dbb389 ("TOMOYO: Add socket operation restriction support.") using the socket_connect() LSM hook.

However, the MSG_FASTOPEN sendmsg() flag was added in 2012 to allow combining connect() and the first sendmsg(). Tomoyo was not updated to take this into account in its send hook.

This resulted in a TCP connect policy bypass similar to that reported in Landlock in 2024 (see Link below), with the difference that Tomoyo was fine when originally merged, and the problem got introduced when adding fastopen support, possibly due to lack of synchronization between lsm and netdev worlds.

Add MSG_FASTOPEN handling in Tomoyo's existing send hook.

---
 security/tomoyo/network.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
security/tomoyo/network.c +15 -1
index cfc2a019de1e..7d9ba7268dc2 100644
@@ -764,11 +764,25 @@ int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
 	struct tomoyo_addr_info address;
 	const u8 family = tomoyo_sock_family(sock->sk);
 	const unsigned int type = sock->type;
+	int ret;
 
+	address.protocol = type;
+
+	if ((msg->msg_flags & MSG_FASTOPEN) != 0 && msg->msg_name != NULL &&
+	    (sk_is_tcp(sock->sk) ||
+	     (sk_is_inet(sock->sk) && type == SOCK_STREAM &&
+	      sock->sk->sk_protocol == IPPROTO_MPTCP))) {
+		address.operation = TOMOYO_NETWORK_CONNECT;
+		ret = tomoyo_check_inet_address(
+			(struct sockaddr *)msg->msg_name, msg->msg_namelen,
+			sock->sk->sk_protocol, &address);
+		if (ret != 0)
+			return ret;
+	}
 	if (!msg->msg_name || !family ||
 	    (type != SOCK_DGRAM && type != SOCK_RAW))
 		return 0;
-	address.protocol = type;
+
 	address.operation = TOMOYO_NETWORK_SEND;
 	if (family == PF_UNIX)
 		return tomoyo_check_unix_address((struct sockaddr *)
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 16 lines · Matthieu Buffet

Tomoyo restricted TCP connections in 2011 in commit 059d84dbb389 ("TOMOYO: Add socket operation restriction support.") using the socket_connect() LSM hook.

However, the MSG_FASTOPEN sendmsg() flag was added in 2012 to allow combining connect() and the first sendmsg(). Tomoyo was not updated to take this into account in its send hook.

This resulted in a TCP connect policy bypass similar to that reported in Landlock in 2024 (see Link below), with the difference that Tomoyo was fine when originally merged, and the problem got introduced when adding fastopen support, possibly due to lack of synchronization between lsm and netdev worlds.

Add MSG_FASTOPEN handling in Tomoyo's existing send hook.

Quoted 9 lines
---
 security/tomoyo/network.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Thank you for finding this problem and making a patch.
I updated your patch like below in order to exclude kernel threads from this check. If we are OK to go with modifying individual LSM, I'll apply this change.

 security/tomoyo/network.c | 9 +++++++++
 1 file changed, 9 insertions(+)
security/tomoyo/network.c +9 -0
index cfc2a019de1e..50d27c773b10 100644
@@ -765,6 +765,15 @@ int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
 	const u8 family = tomoyo_sock_family(sock->sk);
 	const unsigned int type = sock->type;
 
+	if ((msg->msg_flags & MSG_FASTOPEN) && msg->msg_name && type == SOCK_STREAM &&
+	    (family == PF_INET || family == PF_INET6) &&
+	    (sock->sk->sk_protocol == IPPROTO_TCP || sock->sk->sk_protocol == IPPROTO_MPTCP)) {
+		address.protocol = SOCK_STREAM;
+		address.operation = TOMOYO_NETWORK_CONNECT;
+		return tomoyo_check_inet_address((struct sockaddr *)msg->msg_name,
+						 msg->msg_namelen, 0, &address);
+	}
+
 	if (!msg->msg_name || !family ||
 	    (type != SOCK_DGRAM && type != SOCK_RAW))
 		return 0;
Signature · 1 line
2.54.0
#2 lore raw

Hi Tomoyo maintainers,

Quoted 2 lines · Tetsuo Handa

I updated your patch like below in order to exclude kernel threads from this check. If we are OK to go with modifying individual LSM, I'll apply this change.

Archiving this patch, I see you have had no answer from other maintainers to your question about the approach taken (per-LSM instead of aiming for a hook-wide fix). Patches have indeed been merged for other affected LSMs:
4d587cd8a721 ("apparmor: mediate the implicit connect of TCP fast open sendmsg")
44c74d27d1b9 ("selinux: check connect-related permissions on TCP Fast Open")
33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

Have a nice day!

Matthieu

maintainer
#3 lore raw
Quoted 12 lines · Matthieu Buffet

Hi Tomoyo maintainers,

Quoted 2 lines · Tetsuo Handa

I updated your patch like below in order to exclude kernel threads from this check. If we are OK to go with modifying individual LSM, I'll apply this change.

Archiving this patch, I see you have had no answer from other maintainers to your question about the approach taken (per-LSM instead of aiming for a hook-wide fix). Patches have indeed been merged for other affected LSMs:
4d587cd8a721 ("apparmor: mediate the implicit connect of TCP fast open sendmsg") 44c74d27d1b9 ("selinux: check connect-related permissions on TCP Fast Open") 33cb713db016 ("landlock: Fix TCP Fast Open connection bypass")

Quoted 3 lines

Have a nice day!

Matthieu

Thank you for letting me know.

I am currently modifying your patch into a series that can please sashiko's review. Since sashiko finds many corner cases, I need to make several trial and errors. Please wait patiently.