Skip to contentSkip to conversation

selinux: check connect-related permissions on TCP Fast Open

PATCH In review Stephen Smalley 3 participants 4 messages Jun 18 – Jul 1

Subsystems (2)
lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 selinux: check connect-related permissions on TCP Fast Open

Similar to Landlock, SELinux was not updated when TCP Fast Open support was introduced to ensure connect-related permissions are checked when using TCP Fast Open. Update its socket_sendmsg() hook to call selinux_socket_connect() when MSG_FASTOPEN is passed.

---
NB Build-tested only so far.

 security/selinux/hooks.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
security/selinux/hooks.c +17 -1
index 97801966bf32..855cc32a7653 100644
@@ -5270,7 +5270,23 @@ static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
 				  int size)
 {
-	return sock_has_perm(sock->sk, SOCKET__WRITE);
+	int rc;
+	struct sockaddr *const addr = msg->msg_name;
+	const int addrlen = msg->msg_namelen;
+
+	rc = sock_has_perm(sock->sk, SOCKET__WRITE);
+	if (rc)
+		return rc;
+
+	if (addr && (msg->msg_flags & MSG_FASTOPEN) && (sk_is_tcp(sock->sk) ||
+		(sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM &&
+		sock->sk->sk_protocol == IPPROTO_MPTCP))) {
+		rc = selinux_socket_connect(sock, addr, addrlen);
+		if (rc)
+			return rc;
+	}
+
+	return 0;
 }
 
 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
Signature · 1 line
2.54.0
author maintainer
#1 lore raw
Quoted 6 lines · Stephen Smalley

Similar to Landlock, SELinux was not updated when TCP Fast Open support was introduced to ensure connect-related permissions are checked when using TCP Fast Open. Update its socket_sendmsg() hook to call selinux_socket_connect() when MSG_FASTOPEN is passed.

Quoted 8 lines

---
NB Build-tested only so far.

Note that I have subsequently tested the selinux-testsuite with this applied, both with and without the new tests in:
https://lore.kernel.org/selinux/[email protected]/T/#u

Quoted 36 lines
 security/selinux/hooks.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
security/selinux/hooks.c +17 -2
index 97801966bf32..855cc32a7653 100644
@@ -5270,7 +5270,23 @@ static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
                                  int size)
 {
-       return sock_has_perm(sock->sk, SOCKET__WRITE);
+       int rc;
+       struct sockaddr *const addr = msg->msg_name;
+       const int addrlen = msg->msg_namelen;
+
+       rc = sock_has_perm(sock->sk, SOCKET__WRITE);
+       if (rc)
+               return rc;
+
+       if (addr && (msg->msg_flags & MSG_FASTOPEN) && (sk_is_tcp(sock->sk) ||
+               (sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM &&
+               sock->sk->sk_protocol == IPPROTO_MPTCP))) {
+               rc = selinux_socket_connect(sock, addr, addrlen);
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
 }
 
 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
--

2.54.0

Tested-by on v1 1/1
#2 lore raw
In reply to Stephen Smalley

Tested this on x86-64. I built mainline with and without the patch and ran it under a SELinux domain (enforcing) that lacks the tcp_socket connect permission.

Unpatched, connect(2) is denied but sendto(MSG_FASTOPEN) still reaches the listener. With the patch the fastopen send is denied too, and the AVC shows the connect check firing on the sendmsg path. Same for TCP, TCP6 and MPTCP. The TCP_FASTOPEN_CONNECT path was already mediated at connect(2), and a domain that allows connect is unaffected.

A/B logs on request.

maintainer
#3 lore raw
In reply to Stephen Smalley
Quoted 13 lines · On Jun 18, 2026 Stephen Smalley <[email protected]> wrote:

Similar to Landlock, SELinux was not updated when TCP Fast Open support was introduced to ensure connect-related permissions are checked when using TCP Fast Open. Update its socket_sendmsg() hook to call selinux_socket_connect() when MSG_FASTOPEN is passed.

Quoted 5 lines

---
NB Build-tested only so far.

 security/selinux/hooks.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

I tweaked the alignment of the if-conditional to better follow the scope/parens, but otherwise this looks good to me. Thanks everyone for the report, the fix, and the testing!

I've tagged this for stable and I'm merging it into the selinux/stable-7.2 branch to send up to Linus after some soak time in linux-next.

Quoted 31 lines
security/selinux/hooks.c +17 -1
index 97801966bf32..855cc32a7653 100644
@@ -5270,7 +5270,23 @@ static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
 				  int size)
 {
-	return sock_has_perm(sock->sk, SOCKET__WRITE);
+	int rc;
+	struct sockaddr *const addr = msg->msg_name;
+	const int addrlen = msg->msg_namelen;
+
+	rc = sock_has_perm(sock->sk, SOCKET__WRITE);
+	if (rc)
+		return rc;
+
+	if (addr && (msg->msg_flags & MSG_FASTOPEN) && (sk_is_tcp(sock->sk) ||
+		(sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM &&
+		sock->sk->sk_protocol == IPPROTO_MPTCP))) {
+		rc = selinux_socket_connect(sock, addr, addrlen);
+		if (rc)
+			return rc;
+	}
+
+	return 0;
 }
 
 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
Signature · 1 line
2.54.0

--
paul-moore.com