Landlock: LANDLOCK_ACCESS_NET_CONNECT_TCP bypass via TCP Fast Open
Subsystems (5)
Hello Mickaël, and Landlock folks,
A task confined by a Landlock ruleset that handles LANDLOCK_ACCESS_NET_CONNECT_TCP and is denied connecting to a given port can still establish a TCP connection to that port by using TCP Fast Open, i.e. sendto(fd, ..., MSG_FASTOPEN, &dst, dstlen) on a fresh stream socket. The network-egress confinement for TCP connect is silently bypassed.
Affected
--------
Any kernel with CONFIG_SECURITY_LANDLOCK=y and Landlock enabled that supports the TCP network access rights (Landlock ABI >= 4, since Linux 6.7). Confirmed by source inspection on mainline (v7.1-rc7) and reproduced on Linux 7.0.11 (Landlock ABI 8). No CONFIG beyond Landlock + IPv4/IPv6 TCP; TCP Fast Open client is enabled by the per-netns default (net.ipv4.tcp_fastopen has TFO_CLIENT_ENABLE set), so no sysctl change and no setsockopt are required.
Root cause
----------LANDLOCK_ACCESS_NET_CONNECT_TCP is enforced only by the socket_connect LSM hook (hook_socket_connect -> current_check_access_socket). security_socket_connect() has exactly one call site in the tree, net/socket.c (the connect(2) syscall).
TCP Fast Open performs an implicit connect inside sendmsg:
tcp_sendmsg_locked() net/ipv4/tcp.c (MSG_FASTOPEN branch)
-> tcp_sendmsg_fastopen() net/ipv4/tcp.c
-> __inet_stream_connect(..., is_sendmsg=1) net/ipv4/af_inet.c
-> sk->sk_prot->connect() net/ipv4/af_inet.c -> tcp_v4_connect()
This path establishes the connection to the address taken from msg_name but never calls security_socket_connect(). The only LSM hook fired on the sendmsg path is security_socket_sendmsg(), and Landlock registers no socket_sendmsg hook, so LANDLOCK_ACCESS_NET_CONNECT_TCP is never re-checked. __inet_stream_connect() itself carries no LSM hook (only the cgroup-BPF pre_connect, a different mechanism).
Notably the kernel already mediates the analogous AF_UNIX implicit-connect on the send path via the unix_may_send hook, which Landlock does register (hook_unix_may_send) -- so the sendmsg-implies-connect pattern is recognized, but the TCP Fast Open case has no equivalent coverage. The MPTCP fast-open path (mptcp_sendmsg_fastopen -> __inet_stream_connect) is a second producer of the same unmediated connect (by source inspection; not separately reproduced).
Reproducer
----------
A self-contained, fully unprivileged PoC is available on request. It forks an unconfined TFO-capable loopback listener, then in a child applies a Landlock ruleset handling LANDLOCK_ACCESS_NET_CONNECT_TCP with no allow rule (landlock_create_ruleset() with handled_access_net = LANDLOCK_ACCESS_NET_CONNECT_TCP, no landlock_add_rule(), then landlock_restrict_self(); every TCP connect is denied) and tries the forbidden port two ways:
(1) connect(fd, &dst) -> -EACCES (Landlock enforces CONNECT_TCP)
(2) sendto(fd2, buf, len, MSG_FASTOPEN, &dst, dstlen)
-> succeeds; the listener accepts the
connection and reads the payload.
Observed on Linux 7.0.11 (Landlock ABI 8):
[1] connect(2) -> ret=-1 errno=13 (Permission denied)
[2] sendto(MSG_FASTOPEN) -> ret=14 errno=0 (OK/queued) [+] listener ACCEPTED the confined child's connection; payload="..."
connect(2) to the port is denied while sendto(MSG_FASTOPEN) reaches the identical port and delivers data.
Impact
------
A sandbox that uses LANDLOCK_ACCESS_NET_CONNECT_TCP to restrict outbound TCP (e.g. to keep a confined component from reaching an internal service or a metadata endpoint) can be escaped by an unprivileged, self-confined task with no CAP and no namespace transition -- for any destination port, since the implicit-connect path never consults the connect hook regardless of address (the run above shows one port). It is an integrity
bypass of the network-confinement property; no memory safety is involved. I score it CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:C/C:N/I:H/A:N (6.5 Medium) -- the confined task escapes the policy authority that defined its sandbox, a scope change; 5.5 if you treat the Landlock boundary as the same authority (S:U).
Note on the in-flight UDP series
--------------------------------
The "landlock: Add UDP access control support" series (v5, Matthieu Buffet,
https://lore.kernel.org/r/[email protected]) adds a socket_sendmsg hook, hook_socket_sendmsg(), but it returns 0 for non-UDP sockets:
if (sk_is_udp(sock->sk))
access_request = LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
else
return 0;so a TCP socket using MSG_FASTOPEN still bypasses LANDLOCK_ACCESS_NET_CONNECT_TCP even after that series lands. It may be most convenient to fix this there.
Suggested direction
-------------------
Re-check LANDLOCK_ACCESS_NET_CONNECT_TCP on the implicit-connect path: either have the socket_sendmsg hook evaluate CONNECT_TCP for stream sockets when the call performs an implicit connect (mirroring the AF_UNIX unix_may_send handling), or place the check inside __inet_stream_connect() so a single chokepoint covers connect(2), TCP Fast Open, and the MPTCP fast-open sibling.
I am happy to send a patch for this if you would like me to.
Best regards,
Bryam Vargas
Independent security researcher, HEXLAB S.A.S., Cali, Colombia [email protected]
Mickaël Salaün
maintainerHi,
Thanks for the report. This was previously identified by Mikhail and Matthieu, see the related issue:
https://github.com/landlock-lsm/linux/issues/41
Quoted 104 lines · Bryam Vargas
Hello Mickaël, and Landlock folks,
A task confined by a Landlock ruleset that handles
LANDLOCK_ACCESS_NET_CONNECT_TCPand is denied connecting to a given port can still establish a TCP connection to that port by using TCP Fast Open, i.e.sendto(fd, ..., MSG_FASTOPEN, &dst, dstlen)on a fresh stream socket. The network-egress confinement for TCP connect is silently bypassed.Affected
--------
Any kernel withCONFIG_SECURITY_LANDLOCK=yand Landlock enabled that supports the TCP network access rights (Landlock ABI >= 4, since Linux 6.7). Confirmed by source inspection on mainline (v7.1-rc7) and reproduced on Linux 7.0.11 (Landlock ABI 8). No CONFIG beyond Landlock + IPv4/IPv6 TCP; TCP Fast Open client is enabled by the per-netns default (net.ipv4.tcp_fastopen hasTFO_CLIENT_ENABLEset), so no sysctl change and no setsockopt are required.Root cause
----------LANDLOCK_ACCESS_NET_CONNECT_TCPis enforced only by thesocket_connectLSM hook (hook_socket_connect->current_check_access_socket).security_socket_connect()has exactly one call site in the tree,net/socket.c(the connect(2) syscall).TCP Fast Open performs an implicit connect inside sendmsg:
tcp_sendmsg_locked()net/ipv4/tcp.c(MSG_FASTOPENbranch)
->tcp_sendmsg_fastopen()net/ipv4/tcp.c
->__inet_stream_connect(..., is_sendmsg=1)net/ipv4/af_inet.c
->sk->sk_prot->connect()net/ipv4/af_inet.c->tcp_v4_connect()This path establishes the connection to the address taken from
msg_namebut never callssecurity_socket_connect(). The only LSM hook fired on the sendmsg path issecurity_socket_sendmsg(), and Landlock registers nosocket_sendmsghook, soLANDLOCK_ACCESS_NET_CONNECT_TCPis never re-checked.__inet_stream_connect()itself carries no LSM hook (only the cgroup-BPFpre_connect, a different mechanism).Notably the kernel already mediates the analogous
AF_UNIXimplicit-connect on the send path via theunix_may_sendhook, which Landlock does register (hook_unix_may_send) -- so the sendmsg-implies-connect pattern is recognized, but the TCP Fast Open case has no equivalent coverage. The MPTCP fast-open path (mptcp_sendmsg_fastopen->__inet_stream_connect) is a second producer of the same unmediated connect (by source inspection; not separately reproduced).Reproducer
----------
A self-contained, fully unprivileged PoC is available on request. It forks an unconfined TFO-capable loopback listener, then in a child applies a Landlock ruleset handlingLANDLOCK_ACCESS_NET_CONNECT_TCPwith no allow rule (landlock_create_ruleset()withhandled_access_net=LANDLOCK_ACCESS_NET_CONNECT_TCP, nolandlock_add_rule(), thenlandlock_restrict_self(); every TCP connect is denied) and tries the forbidden port two ways:(1)
connect(fd, &dst)->-EACCES(Landlock enforcesCONNECT_TCP)
(2)sendto(fd2, buf, len, MSG_FASTOPEN, &dst, dstlen)
-> succeeds; the listener accepts the
connection and reads the payload.Observed on Linux 7.0.11 (Landlock ABI 8):
[1] connect(2) -> ret=-1 errno=13 (Permission denied)
[2]sendto(MSG_FASTOPEN)-> ret=14 errno=0 (OK/queued) [+] listener ACCEPTED the confined child's connection; payload="..."connect(2) to the port is denied while
sendto(MSG_FASTOPEN)reaches the identical port and delivers data.Impact
------
A sandbox that usesLANDLOCK_ACCESS_NET_CONNECT_TCPto restrict outbound TCP (e.g. to keep a confined component from reaching an internal service or a metadata endpoint) can be escaped by an unprivileged, self-confined task with no CAP and no namespace transition -- for any destination port, since the implicit-connect path never consults the connect hook regardless of address (the run above shows one port). It is an integrity
bypass of the network-confinement property; no memory safety is involved. I score it CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:C/C:N/I:H/A:N (6.5 Medium) -- the confined task escapes the policy authority that defined its sandbox, a scope change; 5.5 if you treat the Landlock boundary as the same authority (S:U).Note on the in-flight UDP series
--------------------------------
The "landlock: Add UDP access control support" series (v5, Matthieu Buffet,
https://lore.kernel.org/r/[email protected]) adds asocket_sendmsghook,hook_socket_sendmsg(), but it returns 0 for non-UDP sockets:if (sk_is_udp(sock->sk)) access_request = LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP; else return 0;so a TCP socket using
MSG_FASTOPENstill bypassesLANDLOCK_ACCESS_NET_CONNECT_TCPeven after that series lands. It may be most convenient to fix this there.Suggested direction
-------------------
Re-checkLANDLOCK_ACCESS_NET_CONNECT_TCPon the implicit-connect path: either have thesocket_sendmsghook evaluateCONNECT_TCPfor stream sockets when the call performs an implicit connect (mirroring theAF_UNIXunix_may_sendhandling), or place the check inside__inet_stream_connect()so a single chokepoint covers connect(2), TCP Fast Open, and the MPTCP fast-open sibling.
Quoted 1 line
I am happy to send a patch for this if you would like me to.
Yes please.
Quoted 8 lines
Best regards,
Bryam Vargas
Independent security researcher, HEXLABS.A.S., Cali, Colombia [email protected]
Hi,
Quoted 3 lines · Mickaël Salaün
Thanks for the report. This was previously identified by Mikhail and Matthieu, see the related issue:
https://github.com/landlock-lsm/linux/issues/41
(I worked on a v0 patch for that issue after I first reported it to Mickaël, missing the fact that it was already documented as a github issue. Then tried a more generic approach that failed. Here's the v0, rebased on the beggining of -next to ease backporting, it might be a good start. For instance, someone with more performance/benchmarking background might want to add an unlikely() around the MSG_FASTOPEN condition in the hot code path?)
Have a nice day!
Matthieu Buffet (2):
landlock: fix TCP Fast Open connection bypass
selftests/landlock: Add test for TCP fast open
security/landlock/net.c | 17 +++ tools/testing/selftests/landlock/net_test.c | 155 ++++++++++++++++++++ 2 files changed, 172 insertions(+)
base-commit: 0ce4243509d1580349dd0d50624036d6b097e958
Signature · 1 line
2.47.3
RFC PATCH 1/2 landlock: fix TCP Fast Open connection bypass
The documentation of the socket_connect() LSM hook states that it controls connecting a socket to a remote address. It has not been the case since the addition of TCP Fast Open (RFC 7413) support, which allows opening a TCP connection (thus, setting a socket's destination address) via the MSG_FASTOPEN flag passed to sendto()/sendmsg()/sendmmsg(). The problem then got duplicated into MPTCP.
Landlock did not take it into account when its TCP support was added, leaving a bypass of TCP connect policy.
Ideally a call to the LSM hook would be added in the fastopen code path, in order to fix this generically. But connect() hooks are designed to run with the socket locked, unlike sendmsg() hooks.
--- security/landlock/net.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
security/landlock/net.c +17 -0
@@ -246,9 +246,26 @@ static int hook_socket_connect(struct socket *const sock, access_request); } +static int hook_socket_sendmsg (struct socket *const sock, +struct msghdr *const msg,const int size) +{ +struct sockaddr *const address = msg->msg_name; +const int addrlen = msg->msg_namelen; + +if (sk_is_tcp (sock->sk) && address !=NULL && + (msg->msg_flags &MSG_FASTOPEN ) !=0 ) { +return current_check_access_socket ( + sock, address, addrlen, +LANDLOCK_ACCESS_NET_CONNECT_TCP ); + } + +return 0 ; +} +static struct security_hook_list landlock_hooks[]__ro_after_init = {LSM_HOOK_INIT (socket_bind, hook_socket_bind),LSM_HOOK_INIT (socket_connect, hook_socket_connect), +LSM_HOOK_INIT (socket_sendmsg, hook_socket_sendmsg), };__init void landlock_add_net_hooks (void )
Signature · 1 line
2.47.3
Re: [RFC PATCH 1/2] landlock: fix TCP Fast Open connection bypass
Thanks Matthieu, your #41, so no competing patch from me. I built your v0 (Landlock + MPTCP) and ran an A/B: without it, a confined task with CONNECT_TCP denied still reaches the port via sendto(MSG_FASTOPEN); with it, that path is now denied too, on IPv4 and IPv6.
One scope note, since you mention MPTCP: an MPTCP socket isn't covered. sk_is_tcp() is false for the mptcp parent (sk_protocol is IPPROTO_MPTCP), so neither the new sendmsg hook nor the existing socket_connect one mediates it. On the patched kernel my MPTCP arm still reaches the blocked port via both connect() and MSG_FASTOPEN. If MPTCP is meant to be in scope for CONNECT_TCP, the guard wants || sk->sk_protocol == IPPROTO_MPTCP (not sk_is_mptcp(), which is the subflow flag).
Bryam
Re: [RFC PATCH 1/2] landlock: fix TCP Fast Open connection bypass
Hi Bryam,
Quoted 7 lines · Bryam Vargas
One scope note, since you mention MPTCP: an MPTCP socket isn't covered.
sk_is_tcp()is false for the mptcp parent (sk_protocolisIPPROTO_MPTCP), so neither the new sendmsg hook nor the existingsocket_connectone mediates it. On the patched kernel my MPTCP arm still reaches the blocked port via bothconnect()andMSG_FASTOPEN. If MPTCP is meant to be in scope forCONNECT_TCP, the guard wants|| sk->sk_protocol == IPPROTO_MPTCP(notsk_is_mptcp(), which is the subflow flag).
Indeed, the patch does not try to filter MPTCP: it is not meant to be in the scope of LANDLOCK_ACCESS_NET_*_TCP rights. It used to be, but it was a bug, see:
https://lore.kernel.org/all/[email protected]/
Have a nice day!
Signature · 1 line
Matthieu
Re: [RFC PATCH 1/2] landlock: fix TCP Fast Open connection bypass
Thanks, that settles it: MPTCP is out of scope by design, not a gap.
I read 854277e2cc8c ("landlock: Fix non-TCP sockets restriction"). It changed the sock->type != SOCK_STREAM test to !sk_is_tcp(sock->sk), dropping SMC/MPTCP/SCTP from the TCP rights on purpose, and 3d4033985ff5 pins that with a "MPTCP actions are not restricted" selftest. So my "|| sk_protocol == IPPROTO_MPTCP" suggestion was wrong: it would revert that decision and break the selftest. Please disregard it.
That leaves the series complete as-is on this axis. Keeping both the v0 guard and the 2/2 selftest sk_is_tcp()-only is correct, and the Tested-by stands for the TCP and IPv6 fast-open path the patch fixes.
Bryam
Mickaël Salaün
maintainerRe: [RFC PATCH 1/2] landlock: fix TCP Fast Open connection bypass
In reply to Matthieu BuffetThanks Matthieu, could you please rebase this serise on the master branch (especially on top of your UDP changes)?
This patch will be useful for backports though.
Quoted 27 lines · Matthieu Buffet
The documentation of the
socket_connect()LSM hook states that it controls connecting a socket to a remote address. It has not been the case since the addition of TCP Fast Open (RFC 7413) support, which allows opening a TCP connection (thus, setting a socket's destination address) via theMSG_FASTOPENflag passed to sendto()/sendmsg()/sendmmsg(). The problem then got duplicated into MPTCP.Landlock did not take it into account when its TCP support was added, leaving a bypass of TCP connect policy.
Ideally a call to the LSM hook would be added in the fastopen code path, in order to fix this generically. But
connect()hooks are designed to run with the socket locked, unlikesendmsg()hooks.Fixes fff69fb03dde ("landlock: Support network rules with TCP bind and connect")Signed-off-by Matthieu Buffet <[email protected]>--- security/landlock/net.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)security/landlock/net.c +0
-0@@ -246,9 +246,26 @@ static int hook_socket_connect(struct socket *const sock, access_request);
security/landlock/net.c static int hook_socket_connect(struct socket *const sock, · line 256
Quoted 10 lines
security/landlock/net.c +8
-0} +static int hook_socket_sendmsg (struct socket *const sock, +struct msghdr *const msg,const int size) +{ +struct sockaddr *const address = msg->msg_name; +const int addrlen = msg->msg_namelen; + +if (sk_is_tcp (sock->sk) && address !=NULL && + (msg->msg_flags &MSG_FASTOPEN ) !=0 ) {
This might be a bit better:
if ((msg->msg_flags & MSG_FASTOPEN) && address && sk_is_tcp(sock->sk))
Quoted 19 lines
security/landlock/net.c +9
-0+return current_check_access_socket ( + sock, address, addrlen, +LANDLOCK_ACCESS_NET_CONNECT_TCP ); + } + +return 0 ; +} +static struct security_hook_list landlock_hooks[]__ro_after_init = {LSM_HOOK_INIT (socket_bind, hook_socket_bind),LSM_HOOK_INIT (socket_connect, hook_socket_connect), +LSM_HOOK_INIT (socket_sendmsg, hook_socket_sendmsg), };__init void landlock_add_net_hooks (void )Signature · 1 line
2.47.3
Re: [RFC PATCH 1/2] landlock: fix TCP Fast Open connection bypass
Hi Mickaël,
Quoted 2 lines · Mickaël Salaün
Thanks Matthieu, could you please rebase this serise on the master branch (especially on top of your UDP changes)?
Yes I hoped I could send it just in time before the UDP merge, but no luck. Here is the patch with your feedback, and rebased on next over UDP.
Have a nice day!
Signature · 1 line
Matthieu
PATCH v2 1/2 landlock: fix TCP Fast Open connection bypass
The documentation of the socket_connect() LSM hook states that it controls connecting a socket to a remote address. It has not been the case since the addition of TCP Fast Open (RFC 7413) support, which allows opening a TCP connection (thus, setting a socket's destination address) via the MSG_FASTOPEN flag passed to sendto()/sendmsg()/sendmmsg(). The problem then got duplicated into MPTCP.
Landlock did not take it into account when its TCP support was added, leaving a bypass of TCP connect policy.
Ideally a call to the LSM hook would be added in the fastopen code path, in order to fix this generically. But connect() hooks are designed to run with the socket locked, unlike sendmsg() hooks.
--- security/landlock/net.c | 8 ++++++++ 1 file changed, 8 insertions(+)
security/landlock/net.c +8 -0
@@ -351,6 +351,14 @@ static int hook_socket_sendmsg(struct socket *const sock,access_mask_t access_request;int ret =0 ; +if ((msg->msg_flags &MSG_FASTOPEN ) && address &&sk_is_tcp (sock->sk)) { + ret =current_check_access_socket ( + sock, address, addrlen,LANDLOCK_ACCESS_NET_CONNECT_TCP , + true); +if (ret !=0 ) +return ret; + } +if (sk_is_udp (sock->sk)) access_request =LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP ;else
Signature · 1 line
2.47.3
PATCH v2 2/2 selftests/landlock: Add test for TCP fast open
Enforce that TCP Fast Open is controlled by LANDLOCK_ACCESS_NET_CONNECT_TCP. Semantics of connect() and sendmsg(MSG_FASTOPEN) should be identical from Landlock's perspective. Also enforce error code consistency, since UDP sockets ignore the MSG_FASTOPEN flag while Unix sockets reject it.
--- tools/testing/selftests/landlock/net_test.c | 94 +++++++++++++++++++++ 1 file changed, 94 insertions(+)
tools/testing/selftests/landlock/net_test.c +94 -0
@@ -1281,6 +1281,100 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 ,close (bind_fd)); } +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 struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +int bind_fd, client_fd, status; +char buf; +pid_t child; + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); +EXPECT_EQ (0 ,bind_variant (bind_fd, &self->srv0)); +if (self->srv0.protocol.type ==SOCK_STREAM ) +EXPECT_EQ (0 ,listen (bind_fd, backlog)); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +int connect_fd, ret; + +/* Closes listening socket for the child. */ +EXPECT_EQ (0 ,close (bind_fd)); + + connect_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , connect_fd); + +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* Fast Open with no address. */ + ret =sendto_variant (connect_fd,NULL ,NULL ,0 ,MSG_FASTOPEN ); +if (self->srv0.protocol.domain ==AF_UNIX ) { +EXPECT_EQ (-ENOTCONN , ret); + }else if (self->srv0.protocol.type ==SOCK_DGRAM ) { +EXPECT_EQ (-EDESTADDRREQ , ret); + }else { +EXPECT_EQ (-EINVAL , ret); + } + +/* Fast Open to a denied address. */ + ret =sendto_variant (connect_fd, &self->srv0,"A" ,1 ,MSG_FASTOPEN ); +if (restricted) { +EXPECT_EQ (-EACCES , ret); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EOPNOTSUPP , ret); + }else { +EXPECT_EQ (0 , ret); + } + +EXPECT_EQ (0 ,close (connect_fd)); +_exit (_metadata->exit_code); +return ; + } + + client_fd = bind_fd; +if (!restricted && self->srv0.protocol.type ==SOCK_STREAM && + self->srv0.protocol.domain !=AF_UNIX ) { + client_fd =accept (bind_fd,NULL ,0 ); +ASSERT_LE (0 , client_fd); + } + +if (restricted) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (ENOTCONN , errno); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (EINVAL , errno); + }else { +EXPECT_EQ (1 ,read (client_fd, &buf,1 )); +EXPECT_EQ ('A' , buf); + } + +EXPECT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (1 ,WIFEXITED (status)); +EXPECT_EQ (EXIT_SUCCESS ,WEXITSTATUS (status)); + +if (client_fd != bind_fd) +EXPECT_LE (0 ,close (client_fd)); + +EXPECT_EQ (0 ,close (bind_fd)); +} + TEST_F(protocol, sendmsg_stream) {int srv0_fd, tmp_fd, client_fd, res;
Signature · 1 line
2.47.3
Re: [PATCH v2 1/2] landlock: fix TCP Fast Open connection bypass
In reply to Matthieu BuffetThanks, applied!
Quoted 44 lines · Matthieu Buffet
The documentation of the
socket_connect()LSM hook states that it controls connecting a socket to a remote address. It has not been the case since the addition of TCP Fast Open (RFC 7413) support, which allows opening a TCP connection (thus, setting a socket's destination address) via theMSG_FASTOPENflag passed to sendto()/sendmsg()/sendmmsg(). The problem then got duplicated into MPTCP.Landlock did not take it into account when its TCP support was added, leaving a bypass of TCP connect policy.
Ideally a call to the LSM hook would be added in the fastopen code path, in order to fix this generically. But
connect()hooks are designed to run with the socket locked, unlikesendmsg()hooks.Fixes fff69fb03dde ("landlock: Support network rules with TCP bind and connect")Signed-off-by Matthieu Buffet <[email protected]>--- security/landlock/net.c | 8 ++++++++ 1 file changed, 8 insertions(+)security/landlock/net.c +8
-0@@ -351,6 +351,14 @@ static int hook_socket_sendmsg(struct socket *const sock,access_mask_t access_request;int ret =0 ; +if ((msg->msg_flags &MSG_FASTOPEN ) && address &&sk_is_tcp (sock->sk)) { + ret =current_check_access_socket ( + sock, address, addrlen,LANDLOCK_ACCESS_NET_CONNECT_TCP , + true); +if (ret !=0 ) +return ret; + } +if (sk_is_udp (sock->sk)) access_request =LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP ;else Signature · 1 line
2.47.3
RFC PATCH 2/2 selftests/landlock: Add test for TCP fast open
In reply to Matthieu BuffetEnforce that TCP Fast Open is controlled by LANDLOCK_ACCESS_NET_CONNECT_TCP. Semantics of connect() and sendmsg(MSG_FASTOPEN) should be identical from Landlock's perspective. Also enforce error code consistency, since UDP sockets ignore the MSG_FASTOPEN flag while Unix sockets reject it.
--- tools/testing/selftests/landlock/net_test.c | 155 ++++++++++++++++++++ 1 file changed, 155 insertions(+)
tools/testing/selftests/landlock/net_test.c +155 -0
@@ -258,6 +258,64 @@ static int connect_variant(const int sock_fd,return connect_variant_addrlen (sock_fd, srv,get_addrlen (srv, false)); } +static int sendto_variant_addrlen (const int sock_fd, +const struct service_fixture *const srv, +const socklen_t addrlen,void *buf, +size_t len,size_t flags) +{ +const struct sockaddr *dst =NULL ; +ssize_t ret; + +/* +* We never want our processes to be killed by SIGPIPE: we check return +* codes and errno, so that we have actual error messages. +*/ + flags |=MSG_NOSIGNAL ; + +if (srv !=NULL ) { +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + dst = (const struct sockaddr *)&srv->ipv4_addr; +break ; + +case AF_INET6 : + dst = (const struct sockaddr *)&srv->ipv6_addr; +break ; + +case AF_UNIX : + dst = (const struct sockaddr *)&srv->unix_addr; +break ; + +default : + errno =EAFNOSUPPORT ; +return -errno; + } + } + + ret =sendto (sock_fd, buf, len, flags, dst, addrlen); +if (ret <0 ) +return -errno; + +/* errno is not set in cases of partial writes. */ +if (ret != len) +return -EINTR ; + +return 0 ; +} + +static int sendto_variant (const int sock_fd, +const struct service_fixture *const srv,void *buf, +size_t len,size_t flags) +{ +socklen_t addrlen =0 ; + +if (srv !=NULL ) + addrlen =get_addrlen (srv, false); + +return sendto_variant_addrlen (sock_fd, srv, addrlen, buf, len, flags); +} +FIXTURE (protocol) {struct service_fixture srv0, srv1, srv2, unspec_any0, unspec_srv0;
@@ -950,6 +1008,103 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 ,close (bind_fd)); } +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 struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +int bind_fd, client_fd, status; +char buf; +pid_t child; + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); +EXPECT_EQ (0 ,bind_variant (bind_fd, &self->srv0)); +if (self->srv0.protocol.type ==SOCK_STREAM ) +EXPECT_EQ (0 ,listen (bind_fd, backlog)); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +int connect_fd, ret; + +/* Closes listening socket for the child. */ +EXPECT_EQ (0 ,close (bind_fd)); + + connect_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , connect_fd); + +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* Fast Open with no address. */ + ret =sendto_variant (connect_fd,NULL ,NULL ,0 ,MSG_FASTOPEN ); +if (self->srv0.protocol.domain ==AF_UNIX ) { +ASSERT_EQ (-ENOTCONN , ret); + }else if (self->srv0.protocol.type ==SOCK_DGRAM ) { +ASSERT_EQ (-EDESTADDRREQ , ret); + }else { +ASSERT_EQ (-EINVAL , ret); + } + +/* Fast Open to a denied address. */ + ret =sendto_variant (connect_fd, &self->srv0,"A" ,1 , +MSG_FASTOPEN ); +if (restricted) { +ASSERT_EQ (-EACCES , ret); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +ASSERT_EQ (-EOPNOTSUPP , ret); + }else { +ASSERT_EQ (0 , ret); + } + +EXPECT_EQ (0 ,close (connect_fd)); +_exit (_metadata->exit_code); +return ; + } + + client_fd = bind_fd; +if (!restricted && self->srv0.protocol.type ==SOCK_STREAM && + self->srv0.protocol.domain !=AF_UNIX ) { + client_fd =accept (bind_fd,NULL ,0 ); +ASSERT_LE (0 , client_fd); + } + +if (restricted) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (ENOTCONN , errno); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (EINVAL , errno); + }else { +EXPECT_EQ (1 ,read (client_fd, &buf,1 )); +EXPECT_EQ ('A' , buf); + } + +EXPECT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (1 ,WIFEXITED (status)); +EXPECT_EQ (EXIT_SUCCESS ,WEXITSTATUS (status)); + +if (client_fd != bind_fd) +EXPECT_LE (0 ,close (client_fd)); + +EXPECT_EQ (0 ,close (bind_fd)); +} +FIXTURE (ipv4) {struct service_fixture srv0, srv1;
Signature · 1 line
2.47.3
Mickaël Salaün
maintainerRe: [RFC PATCH 2/2] selftests/landlock: Add test for TCP fast open
Quoted 21 lines · Matthieu Buffet
Enforce that TCP Fast Open is controlled by
LANDLOCK_ACCESS_NET_CONNECT_TCP. Semantics ofconnect()andsendmsg(MSG_FASTOPEN)should be identical from Landlock's perspective. Also enforce error code consistency, since UDP sockets ignore theMSG_FASTOPENflag while Unix sockets reject it.Signed-off-by Matthieu Buffet <[email protected]>--- tools/testing/selftests/landlock/net_test.c | 155 ++++++++++++++++++++ 1 file changed, 155 insertions(+)tools/testing/selftests/landlock/net_test.c +2
-0@@ -258,6 +258,64 @@ static int connect_variant(const int sock_fd,return connect_variant_addrlen (sock_fd, srv,get_addrlen (srv, false)); } +static int sendto_variant_addrlen(const int sock_fd, + const struct service_fixture *const srv,
tools/testing/selftests/landlock/net_test.c static int connect_variant(const int sock_fd, · line 272
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c +10
-0+ const socklen_t addrlen, void *buf, + size_t len, size_t flags) +{ + const struct sockaddr *dst = NULL; +ssize_t ret; + +/* +* We never want our processes to be killed by SIGPIPE: we check return +* codes and errno, so that we have actual error messages. +*/
There are some extra spaces above.
tools/testing/selftests/landlock/net_test.c static int connect_variant(const int sock_fd, · line 275
Quoted 3 lines
tools/testing/selftests/landlock/net_test.c +3
-0+ flags |=MSG_NOSIGNAL ; + +if (srv !=NULL ) {
Just if (srv) {
Quoted 28 lines
tools/testing/selftests/landlock/net_test.c +28
-0+switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + dst = (const struct sockaddr *)&srv->ipv4_addr; +break ; + +case AF_INET6 : + dst = (const struct sockaddr *)&srv->ipv6_addr; +break ; + +case AF_UNIX : + dst = (const struct sockaddr *)&srv->unix_addr; +break ; + +default : + errno =EAFNOSUPPORT ; +return -errno; + } + } + + ret =sendto (sock_fd, buf, len, flags, dst, addrlen); +if (ret <0 ) +return -errno; + +/* errno is not set in cases of partial writes. */ +if (ret != len) +return -EINTR ; +
tools/testing/selftests/landlock/net_test.c static int connect_variant(const int sock_fd, · line 313
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c +10
-0+return 0 ; +} + +static int sendto_variant (const int sock_fd, +const struct service_fixture *const srv,void *buf, +size_t len,size_t flags) +{ +socklen_t addrlen =0 ; + +if (srv !=NULL )
ditto
Quoted 66 lines
tools/testing/selftests/landlock/net_test.c +57
-0+ addrlen =get_addrlen (srv, false); + +return sendto_variant_addrlen (sock_fd, srv, addrlen, buf, len, flags); +} +FIXTURE (protocol) {struct service_fixture srv0, srv1, srv2, unspec_any0, unspec_srv0;@@ -950,6 +1008,103 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 ,close (bind_fd)); } +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 struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +int bind_fd, client_fd, status; +char buf; +pid_t child; + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); +EXPECT_EQ (0 ,bind_variant (bind_fd, &self->srv0)); +if (self->srv0.protocol.type ==SOCK_STREAM ) +EXPECT_EQ (0 ,listen (bind_fd, backlog)); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +int connect_fd, ret; + +/* Closes listening socket for the child. */ +EXPECT_EQ (0 ,close (bind_fd)); + + connect_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , connect_fd); + +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* Fast Open with no address. */ + ret =sendto_variant (connect_fd,NULL ,NULL ,0 ,MSG_FASTOPEN ); +if (self->srv0.protocol.domain ==AF_UNIX ) { +ASSERT_EQ (-ENOTCONN , ret); + }else if (self->srv0.protocol.type ==SOCK_DGRAM ) { +ASSERT_EQ (-EDESTADDRREQ , ret); + }else { +ASSERT_EQ (-EINVAL , ret); + } + +/* Fast Open to a denied address. */
tools/testing/selftests/landlock/net_test.c TEST_F(protocol, connect_unspec) · line 1074
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c +10
-0+ ret =sendto_variant (connect_fd, &self->srv0,"A" ,1 , +MSG_FASTOPEN ); +if (restricted) { +ASSERT_EQ (-EACCES , ret); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +ASSERT_EQ (-EOPNOTSUPP , ret); + }else { +ASSERT_EQ (0 , ret); + }
All these ret checks should be done with EXPECT_EQ because they don't block the test itself and we can get more info by checking more after that.
Quoted 42 lines
tools/testing/selftests/landlock/net_test.c +35
-0+ +EXPECT_EQ (0 ,close (connect_fd)); +_exit (_metadata->exit_code); +return ; + } + + client_fd = bind_fd; +if (!restricted && self->srv0.protocol.type ==SOCK_STREAM && + self->srv0.protocol.domain !=AF_UNIX ) { + client_fd =accept (bind_fd,NULL ,0 ); +ASSERT_LE (0 , client_fd); + } + +if (restricted) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (ENOTCONN , errno); + }else if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-1 ,read (client_fd, &buf,1 )); +EXPECT_EQ (EINVAL , errno); + }else { +EXPECT_EQ (1 ,read (client_fd, &buf,1 )); +EXPECT_EQ ('A' , buf); + } + +EXPECT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (1 ,WIFEXITED (status)); +EXPECT_EQ (EXIT_SUCCESS ,WEXITSTATUS (status)); + +if (client_fd != bind_fd) +EXPECT_LE (0 ,close (client_fd)); + +EXPECT_EQ (0 ,close (bind_fd)); +} +FIXTURE (ipv4){
structservice_fixturesrv0, srv1;Signature · 1 line
2.47.3