af_unix: Allow BPF LSM to filter SCM_RIGHTS at sendmsg().
Subsystems (11)
v2 cover af_unix: Allow BPF LSM to filter SCM_RIGHTS at sendmsg().
From: Kuniyuki Iwashima <[email protected]>
Since commit 77cbe1a6d873 ("af_unix: Introduce SO_PASSRIGHTS."), we can disable SCM_RIGHTS per socket, but it's not flexible.
This series allows us to implement more fine-grained filtering for SCM_RIGHTS with BPF LSM.
Changes:
v2: Remove SCM_RIGHTS fd scrubbing functionality
v1: https://lore.kernel.org/bpf/[email protected]/
Kuniyuki Iwashima (4):
af_unix: Don't pass struct socket to security_unix_may_send().
af_unix: Call security_unix_may_send() in sendmsg() for all socket
types
af_unix: Pass skb to security_unix_may_send().
selftest: bpf: Add test for BPF LSM on unix_may_send().
include/linux/lsm_hook_defs.h | 3 +- include/linux/security.h | 7 +- net/unix/af_unix.c | 32 ++-- security/landlock/task.c | 16 +- security/security.c | 5 +- security/selinux/hooks.c | 14 +- security/smack/smack_lsm.c | 12 +- .../bpf/prog_tests/lsm_unix_may_send.c | 168 ++++++++++++++++++ .../selftests/bpf/progs/lsm_unix_may_send.c | 83 +++++++++ 9 files changed, 309 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_unix_may_send.c
Signature · 1 line
2.49.0
PATCH v2 1/4 af_unix: Don't pass struct socket to security_unix_may_send().
From: Kuniyuki Iwashima <[email protected]>
The next patch will invoke security_unix_may_send() in unix_stream_sendmsg().
At that point, the peer socket may not have sk->sk_socket if it has not been accept()ed yet, which would cause null-ptr-deref.
Currently, all security_unix_may_send() hooks fetch struct sock from struct socket but do not use struct socket itself.
Let's pass struct sock directly to security_unix_may_send().
--- include/linux/lsm_hook_defs.h | 2 +- include/linux/security.h | 6 +++--- net/unix/af_unix.c | 4 ++-- security/landlock/task.c | 12 ++++++------ security/security.c | 4 ++-- security/selinux/hooks.c | 10 +++++----- security/smack/smack_lsm.c | 8 ++++---- 7 files changed, 23 insertions(+), 23 deletions(-)
include/linux/lsm_hook_defs.h +1 -1
@@ -318,7 +318,7 @@ LSM_HOOK(int, 0, watch_key, struct key *key)#ifdef CONFIG_SECURITY_NETWORK LSM_HOOK(int, 0, unix_stream_connect, struct sock *sock, struct sock *other, struct sock *newsk) -LSM_HOOK(int, 0, unix_may_send, struct socket *sock, struct socket *other) +LSM_HOOK(int, 0, unix_may_send, struct sock *sock, struct sock *other) LSM_HOOK(int, 0, socket_create, int family, int type, int protocol, int kern) LSM_HOOK(int, 0, socket_post_create, struct socket *sock, int family, int type, int protocol, int kern)
include/linux/security.h +3 -3
@@ -1625,7 +1625,7 @@ static inline int security_watch_key(struct key *key)int security_netlink_send (struct sock *sk,struct sk_buff *skb);int security_unix_stream_connect (struct sock *sock,struct sock *other,struct sock *newsk); -int security_unix_may_send (struct socket *sock,struct socket *other); +int security_unix_may_send (struct sock *sk,struct sock *other);int security_socket_create (int family,int type,int protocol,int kern);int security_socket_post_create (struct socket *sock,int family,int type,int protocol,int kern);
@@ -1691,8 +1691,8 @@ static inline int security_unix_stream_connect(struct sock *sock,return 0 ; } -static inline int security_unix_may_send (struct socket *sock, -struct socket *other) +static inline int security_unix_may_send (struct sock *sk, +struct sock *other) {return 0 ; }
net/unix/af_unix.c +2 -2
@@ -1516,7 +1516,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,if (!unix_may_send (sk, other)) goto out_unlock; - err =security_unix_may_send (sk->sk_socket, other->sk_socket); + err =security_unix_may_send (sk, other);if (err) goto out_unlock;
@@ -2171,7 +2171,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, }if (sk->sk_type !=SOCK_SEQPACKET ) { - err =security_unix_may_send (sk->sk_socket, other->sk_socket); + err =security_unix_may_send (sk, other);if (err) goto out_unlock; }
security/landlock/task.c +6 -6
@@ -294,8 +294,8 @@ static int hook_unix_stream_connect(struct sock *const sock,return -EPERM ; } -static int hook_unix_may_send (struct socket *const sock, -struct socket *const other) +static int hook_unix_may_send (struct sock *const sk, +struct sock *const other) {size_t handle_layer; const struct landlock_cred_security *const subject =
@@ -309,13 +309,13 @@ static int hook_unix_may_send(struct socket *const sock,* Checks if this datagram socket was already allowed to be connected * to other. */ -if (unix_peer (sock->sk) == other->sk) +if (unix_peer (sk) == other)return 0 ; -if (!is_abstract_socket (other->sk)) +if (!is_abstract_socket (other))return 0 ; -if (!sock_is_scoped (other->sk, subject->domain)) +if (!sock_is_scoped (other, subject->domain))return 0 ; landlock_log_denial(subject, &(struct landlock_request) {
@@ -323,7 +323,7 @@ static int hook_unix_may_send(struct socket *const sock, .audit = { .type = LSM_AUDIT_DATA_NET, .u.net = &(struct lsm_network_audit ) { - .sk = other->sk, + .sk = other, }, }, .layer_plus_one = handle_layer + 1,
security/security.c +2 -2
@@ -4531,9 +4531,9 @@ EXPORT_SYMBOL(security_unix_stream_connect);* * Return: Returns 0 if permission is granted. */ -int security_unix_may_send (struct socket *sock,struct socket *other) +int security_unix_may_send (struct sock *sk,struct sock *other) { -return call_int_hook (unix_may_send, sock, other); +return call_int_hook (unix_may_send, sk, other); }EXPORT_SYMBOL (security_unix_may_send);
security/selinux/hooks.c +5 -5
@@ -5176,15 +5176,15 @@ static int selinux_socket_unix_stream_connect(struct sock *sock,return 0 ; } -static int selinux_socket_unix_may_send (struct socket *sock, -struct socket *other) +static int selinux_socket_unix_may_send (struct sock *sk, +struct sock *other) { -struct sk_security_struct *ssec =selinux_sock (sock->sk); -struct sk_security_struct *osec =selinux_sock (other->sk); +struct sk_security_struct *ssec =selinux_sock (sk); +struct sk_security_struct *osec =selinux_sock (other);struct common_audit_data ad;struct lsm_network_audit net; -ad_net_init_from_sk (&ad, &net, other->sk); +ad_net_init_from_sk (&ad, &net, other);return avc_has_perm (ssec->sid, osec->sid, osec->sclass,SOCKET__SENDTO , &ad);
security/smack/smack_lsm.c +4 -4
@@ -3889,10 +3889,10 @@ static int smack_unix_stream_connect(struct sock *sock,* Return 0 if a subject with the smack of sock could access * an object with the smack of other, otherwise an error code */ -static int smack_unix_may_send (struct socket *sock,struct socket *other) +static int smack_unix_may_send (struct sock *sk,struct sock *other) { -struct socket_smack *ssp =smack_sock (sock->sk); -struct socket_smack *osp =smack_sock (other->sk); +struct socket_smack *ssp =smack_sock (sk); +struct socket_smack *osp =smack_sock (other);struct smk_audit_info ad;int rc;
@@ -3900,7 +3900,7 @@ static int smack_unix_may_send(struct socket *sock, struct socket *other)struct lsm_network_audit net;smk_ad_init_net (&ad, __func__,LSM_AUDIT_DATA_NET , &net); -smk_ad_setfield_u_net_sk (&ad, other->sk); +smk_ad_setfield_u_net_sk (&ad, other);#endif if (smack_privileged (CAP_MAC_OVERRIDE ))
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCH v2 bpf-next 1/4] af_unix: Don't pass struct socket to security_unix_may_send().
kernel test robot noticed the following build warnings:
Hi Kuniyuki,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/af_unix-Don-t-pass-struct-socket-to-security_unix_may_send/20250614-062956 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20250613222411.1216170-2-kuni1840%40gmail.com patch subject: [PATCH v2 bpf-next 1/4] af_unix: Don't pass struct socket to security_unix_may_send(). config: arm-randconfig-001-20250614 (https://download.01.org/0day-ci/archive/20250615/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250615/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: security/smack/smack_lsm.c:3892 function parameter 'sk' not described in 'smack_unix_may_send' >> Warning: security/smack/smack_lsm.c:3892 Excess function parameter 'sock' description in 'smack_unix_may_send'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 2/4 af_unix: Call security_unix_may_send() in sendmsg() for all socket types
In reply to Kuniyuki IwashimaFrom: Kuniyuki Iwashima <[email protected]>
Currently, security_unix_may_send() is invoked only for SOCK_DGRAM sockets during connect() and sendmsg().
For SOCK_STREAM and SOCK_SEQPACKET sockets, an equivalent check already occurs during connect(), making an additional hook in sendmsg() unnecessary.
However, we want to leverage BPF LSM to inspect UNIXCB(skb) during sendmsg().
As a preparation, let's call security_unix_may_send() for SOCK_STREAM and SOCK_SEQPACKET in sendmsg().
Note that SELinux, SMACK, and Landlock use security_unix_may_send(). To avoid unintentionally triggering the hook for SOCK_STREAM and SOCK_SEQPACKET, the socket type check is added in each LSM hooks.
--- net/unix/af_unix.c | 30 +++++++++++++++++++++--------- security/landlock/task.c | 3 +++ security/selinux/hooks.c | 3 +++ security/smack/smack_lsm.c | 3 +++ 4 files changed, 30 insertions(+), 9 deletions(-)
net/unix/af_unix.c +21 -9
@@ -2170,11 +2170,9 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, goto out_unlock; } -if (sk->sk_type !=SOCK_SEQPACKET ) { - err =security_unix_may_send (sk, other); -if (err) - goto out_unlock; - } + err =security_unix_may_send (sk, other); +if (err) + goto out_unlock;/* other == sk && unix_peer(other) != sk if * - unix_peer(sk) == NULL, destination address bound to sk
@@ -2280,6 +2278,12 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other, goto out_unlock; } +if (!fds_sent) { + err =security_unix_may_send (sk, other); +if (err) + goto out_unlock; + } +unix_maybe_add_creds (skb, sk, other);scm_stat_add (other, skb);
@@ -2372,8 +2376,6 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,if (err <0 ) goto out_free; - fds_sent = true; -if (unlikely (msg->msg_flags &MSG_SPLICE_PAGES )) { skb->ip_summed =CHECKSUM_UNNECESSARY ; err = skb_splice_from_iter(skb, &msg->msg_iter, size,
@@ -2399,9 +2401,16 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, goto out_pipe_unlock;if (UNIXCB (skb).fp && !other->sk_scm_rights) { -unix_state_unlock (other); err = -EPERM ; - goto out_free; + goto out_unlock; + } + +if (!fds_sent) { + err =security_unix_may_send (sk, other); +if (err) + goto out_unlock; + + fds_sent = true; }unix_maybe_add_creds (skb, sk, other);
@@ -2425,6 +2434,9 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,return sent; +out_unlock: +unix_state_unlock (other); + goto out_free; out_pipe_unlock:unix_state_unlock (other); out_pipe:
security/landlock/task.c +3 -0
@@ -305,6 +305,9 @@ static int hook_unix_may_send(struct sock *const sk,if (!subject)return 0 ; +if (sk->sk_type !=SOCK_DGRAM ) +return 0 ; +/* * Checks if this datagram socket was already allowed to be connected * to other.
security/selinux/hooks.c +3 -0
@@ -5184,6 +5184,9 @@ static int selinux_socket_unix_may_send(struct sock *sk,struct common_audit_data ad;struct lsm_network_audit net; +if (sk->sk_type !=SOCK_DGRAM ) +return 0 ; +ad_net_init_from_sk (&ad, &net, other); return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
security/smack/smack_lsm.c +3 -0
@@ -3903,6 +3903,9 @@ static int smack_unix_may_send(struct sock *sk, struct sock *other)smk_ad_setfield_u_net_sk (&ad, other);#endif +if (sk->sk_type !=SOCK_DGRAM ) +return 0 ; +if (smack_privileged (CAP_MAC_OVERRIDE ))return 0 ;
Signature · 1 line
2.49.0
PATCH v2 3/4 af_unix: Pass skb to security_unix_may_send().
In reply to Kuniyuki IwashimaFrom: Kuniyuki Iwashima <[email protected]>
SO_PASSRIGHTS is not flexible as it cannot control what types of file descriptors are allowed to be passed.
Let's pass skb to security_unix_may_send() so that we can implement more fine-grained filtering logic with BPF LSM.
Note that only the LSM_HOOK() macro uses the __nullable suffix for skb to inform the verifier that the skb could be NULL at connect(). Without it, I was able to load a bpf prog without NULL check against skb.
--- include/linux/lsm_hook_defs.h | 3 ++- include/linux/security.h | 5 +++-- net/unix/af_unix.c | 8 ++++---- security/landlock/task.c | 3 ++- security/security.c | 5 +++-- security/selinux/hooks.c | 3 ++- security/smack/smack_lsm.c | 3 ++- 7 files changed, 18 insertions(+), 12 deletions(-)
include/linux/lsm_hook_defs.h +2 -1
@@ -318,7 +318,8 @@ LSM_HOOK(int, 0, watch_key, struct key *key)#ifdef CONFIG_SECURITY_NETWORK LSM_HOOK(int, 0, unix_stream_connect, struct sock *sock, struct sock *other, struct sock *newsk) -LSM_HOOK(int, 0, unix_may_send, struct sock *sock, struct sock *other) +LSM_HOOK(int, 0, unix_may_send, struct sock *sock, struct sock *other, + struct sk_buff *skb__nullable) LSM_HOOK(int, 0, socket_create, int family, int type, int protocol, int kern) LSM_HOOK(int, 0, socket_post_create, struct socket *sock, int family, int type, int protocol, int kern)
include/linux/security.h +3 -2
@@ -1625,7 +1625,7 @@ static inline int security_watch_key(struct key *key)int security_netlink_send (struct sock *sk,struct sk_buff *skb);int security_unix_stream_connect (struct sock *sock,struct sock *other,struct sock *newsk); -int security_unix_may_send (struct sock *sk,struct sock *other); +int security_unix_may_send (struct sock *sk,struct sock *other,struct sk_buff *skb);int security_socket_create (int family,int type,int protocol,int kern);int security_socket_post_create (struct socket *sock,int family,int type,int protocol,int kern);
@@ -1692,7 +1692,8 @@ static inline int security_unix_stream_connect(struct sock *sock, }static inline int security_unix_may_send (struct sock *sk, -struct sock *other) +struct sock *other, +struct sk_buff *skb) {return 0 ; }
net/unix/af_unix.c +4 -4
@@ -1516,7 +1516,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,if (!unix_may_send (sk, other)) goto out_unlock; - err =security_unix_may_send (sk, other); + err =security_unix_may_send (sk, other,NULL );if (err) goto out_unlock;
@@ -2170,7 +2170,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, goto out_unlock; } - err =security_unix_may_send (sk, other); + err =security_unix_may_send (sk, other, skb);if (err) goto out_unlock;
@@ -2279,7 +2279,7 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other, }if (!fds_sent) { - err =security_unix_may_send (sk, other); + err =security_unix_may_send (sk, other, skb);if (err) goto out_unlock; }
@@ -2406,7 +2406,7 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, }if (!fds_sent) { - err =security_unix_may_send (sk, other); + err =security_unix_may_send (sk, other, skb);if (err) goto out_unlock;
security/landlock/task.c +2 -1
@@ -295,7 +295,8 @@ static int hook_unix_stream_connect(struct sock *const sock, }static int hook_unix_may_send (struct sock *const sk, -struct sock *const other) +struct sock *const other, +struct sk_buff *skb) {size_t handle_layer; const struct landlock_cred_security *const subject =
security/security.c +3 -2
@@ -4531,9 +4531,10 @@ EXPORT_SYMBOL(security_unix_stream_connect);* * Return: Returns 0 if permission is granted. */ -int security_unix_may_send (struct sock *sk,struct sock *other) +int security_unix_may_send (struct sock *sk,struct sock *other, +struct sk_buff *skb) { -return call_int_hook (unix_may_send, sk, other); +return call_int_hook (unix_may_send, sk, other, skb); }EXPORT_SYMBOL (security_unix_may_send);
security/selinux/hooks.c +2 -1
@@ -5177,7 +5177,8 @@ static int selinux_socket_unix_stream_connect(struct sock *sock, }static int selinux_socket_unix_may_send (struct sock *sk, -struct sock *other) +struct sock *other, +struct sk_buff *skb) {struct sk_security_struct *ssec =selinux_sock (sk);struct sk_security_struct *osec =selinux_sock (other);
security/smack/smack_lsm.c +2 -1
@@ -3889,7 +3889,8 @@ static int smack_unix_stream_connect(struct sock *sock,* Return 0 if a subject with the smack of sock could access * an object with the smack of other, otherwise an error code */ -static int smack_unix_may_send (struct sock *sk,struct sock *other) +static int smack_unix_may_send (struct sock *sk,struct sock *other, +struct sk_buff *skb) {struct socket_smack *ssp =smack_sock (sk);struct socket_smack *osp =smack_sock (other);
Signature · 1 line
2.49.0
PATCH v2 4/4 selftest: bpf: Add test for BPF LSM on unix_may_send().
In reply to Kuniyuki IwashimaFrom: Kuniyuki Iwashima <[email protected]>
This test performs the following for all AF_UNIX socket types to demonstrate how we can inspect each struct file passed via SCM_RIGHTS.
1. Create a socket pair (sender and receiver)
2. Send the receiver's fd from the sender to the receiver
3. Receive the fd
4. Attach a BPF LSM prog that forbids self-reference SCM_RIGHTS
5. Send the receiver's fd from the sender to the receiver
6. Check if sendmsg() fails with -EPERM
7. Detach the LSM prog
How to run:
# make -C tools/testing/selftests/bpf/ # ./tools/testing/selftests/bpf/test_progs -t lsm_unix_may_send ... #182/1 lsm_unix_may_send/SOCK_STREAM:OK #182/2 lsm_unix_may_send/SOCK_DGRAM:OK #182/3 lsm_unix_may_send/SOCK_SEQPACKET:OK #182 lsm_unix_may_send:OK Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/prog_tests/lsm_unix_may_send.c | 168 ++++++++++++++++++ .../selftests/bpf/progs/lsm_unix_may_send.c | 83 +++++++++ 2 files changed, 251 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_unix_may_send.c
tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c new file +168 -0
@@ -0,0 +1,168 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright 2025 Google LLC */ + +#include "test_progs.h" +#include "lsm_unix_may_send.skel.h" + +#define MSG_HELLO "Hello" +#define MSG_WORLD "World" +#define MSG_LEN 5 + +struct scm_rights { +struct cmsghdr cmsghdr; +int fd; +}; + +static int send_fd (int sender_fd,int receiver_fd,bool lsm_attached) +{ +struct scm_rights cmsg = {}; +struct msghdr msg = {}; +struct iovec iov = {}; +int ret; + + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + msg.msg_control = &cmsg; + msg.msg_controllen =CMSG_SPACE (sizeof (cmsg.fd)); + + iov.iov_base =MSG_HELLO ; + iov.iov_len =MSG_LEN ; + + cmsg.cmsghdr.cmsg_len =CMSG_LEN (sizeof (cmsg.fd)); + cmsg.cmsghdr.cmsg_level =SOL_SOCKET ; + cmsg.cmsghdr.cmsg_type =SCM_RIGHTS ; + cmsg.fd = receiver_fd; + +/* sending "Hello" with the receiver's fd. */ + ret =sendmsg (sender_fd, &msg,0 ); + +if (lsm_attached) { +if (!ASSERT_EQ (ret,-1 ,"sendmsg(Hello)" ) || + !ASSERT_EQ (errno,EPERM ,"sendmsg(Hello) errno" )) +return -EINVAL ; + }else { +if (!ASSERT_EQ (ret,MSG_LEN ,"sendmsg(Hello)" )) +return -EINVAL ; + } + +/* sending "World" without SCM_RIGHTS. */ + ret =send (sender_fd,MSG_WORLD ,MSG_LEN ,0 ); +if (!ASSERT_EQ (ret,MSG_LEN ,"sendmsg(World)" )) +return -EINVAL ; + +return 0 ; +} + +static int recv_fd (int receiver_fd,bool lsm_attached) +{ +struct scm_rights cmsg = {}; +struct msghdr msg = {}; +char buf[MSG_LEN ] = {}; +struct iovec iov = {}; +int ret; + + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + msg.msg_control = &cmsg; + msg.msg_controllen =CMSG_SPACE (sizeof (cmsg.fd)); + + iov.iov_base = buf; + iov.iov_len =sizeof (buf); + +/* LSM is expected to drop "Hello" with the receiver's fd */ +if (lsm_attached) + goto no_hello; + + ret =recvmsg (receiver_fd, &msg,0 ); +if (!ASSERT_EQ (ret,MSG_LEN ,"recvmsg(Hello) length" ) || + !ASSERT_STRNEQ (buf,MSG_HELLO ,MSG_LEN ,"recvmsg(Hello) data" )) +return -EINVAL ; + +if (!ASSERT_OK_PTR (CMSG_FIRSTHDR (&msg),"cmsg sent" ) || + !ASSERT_EQ (cmsg.cmsghdr.cmsg_len,CMSG_LEN (sizeof (cmsg.fd)),"cmsg_len" ) || + !ASSERT_EQ (cmsg.cmsghdr.cmsg_level,SOL_SOCKET ,"cmsg_level" ) || + !ASSERT_EQ (cmsg.cmsghdr.cmsg_type,SCM_RIGHTS ,"cmsg_type" )) +return -EINVAL ; + +/* Double-check if the fd is of the receiver itself. */ + receiver_fd = cmsg.fd; + +memset (buf,0 ,sizeof (buf)); + +no_hello: + ret =recv (receiver_fd, buf,sizeof (buf),0 ); +if (!ASSERT_EQ (ret,MSG_LEN ,"recvmsg(World) length" ) || + !ASSERT_STRNEQ (buf,MSG_WORLD ,MSG_LEN ,"recvmsg(World) data" )) +return -EINVAL ; + +return 0 ; +} + +static void test_scm_rights (struct lsm_unix_may_send *skel,int type) +{ +struct bpf_link *link; +int socket_fds[2 ]; +int err; + + err =socketpair (AF_UNIX , type,0 , socket_fds); +if (!ASSERT_EQ (err,0 ,"socketpair" )) +return ; + + err =send_fd (socket_fds[0 ], socket_fds[1 ], false); +if (err) + goto close; + + err =recv_fd (socket_fds[1 ], false); +if (err) + goto close; + + link =bpf_program__attach_lsm (skel->progs.unix_may_send_filter); +if (!ASSERT_OK_PTR (link,"attach lsm" )) + goto close; + + err =send_fd (socket_fds[0 ], socket_fds[1 ], true); +if (err) + goto detach; + +recv_fd (socket_fds[1 ], true); +detach: + err =bpf_link__destroy (link); +ASSERT_EQ (err,0 ,"detach lsm" ); +close: +close (socket_fds[0 ]); +close (socket_fds[1 ]); +} + +struct sk_type { +char name[16 ]; +int type; +} sk_types[] = { + { + .name ="SOCK_STREAM" , + .type =SOCK_STREAM , + }, + { + .name ="SOCK_DGRAM" , + .type =SOCK_DGRAM , + }, + { + .name ="SOCK_SEQPACKET" , + .type =SOCK_SEQPACKET , + }, +}; + +void test_lsm_unix_may_send (void ) +{ +struct lsm_unix_may_send *skel; +int i; + + skel =lsm_unix_may_send__open_and_load (); +if (!ASSERT_OK_PTR (skel,"load skel" )) +return ; + +for (i =0 ; i <ARRAY_SIZE (sk_types); i++) +if (test__start_subtest (sk_types[i].name)) +test_scm_rights (skel, sk_types[i].type); + +lsm_unix_may_send__destroy (skel); +}
tools/testing/selftests/bpf/progs/lsm_unix_may_send.c new file +83 -0
83 lines not on this page · raw message
Signature · 1 line
2.49.0
Paul Moore
maintainerQuoted 7 lines · Kuniyuki Iwashima
From: Kuniyuki Iwashima <[email protected]>
Since commit
77cbe1a6d873("af_unix: IntroduceSO_PASSRIGHTS."), we can disableSCM_RIGHTSper socket, but it's not flexible.This series allows us to implement more fine-grained filtering for
SCM_RIGHTSwith BPF LSM.
My ability to review this over the weekend is limited due to device and network access, but I'll take a look next week.
That said, it would be good if you could clarify the "filtering" aspect of your comments; it may be obvious when I'm able to look at the full patchset in context, but the commit descriptions worry me that perhaps you are still intending on using the LSM framework to cut SCM_RIGHTS payloads from individual messages? Blocking messages at send time if they contain SCM_RIGHTS is likely okay (pending proper implementation review), but modifying packets in flight in the LSM framework is not.
Also, a quick administrative note, I see you have marked this as "bpf-next", however given the diffstat of the proposed changes this patchset should go to Linus via the LSM tree and not the BPF tree.
--
paul-moore.com
From: Paul Moore <[email protected]>
Date: Sat, 14 Jun 2025 07:43:46 -0400
Quoted 6 lines · Kuniyuki Iwashima
Quoted 5 lines
From: Kuniyuki Iwashima <[email protected]>
Since commit
77cbe1a6d873("af_unix: IntroduceSO_PASSRIGHTS."), we can disableSCM_RIGHTSper socket, but it's not flexible.
Quoted 8 lines
Quoted 2 lines
This series allows us to implement more fine-grained filtering for
SCM_RIGHTSwith BPF LSM.My ability to review this over the weekend is limited due to device and network access, but I'll take a look next week.
That said, it would be good if you could clarify the "filtering" aspect of your comments; it may be obvious when I'm able to look at the full patchset
I meant to mention that just below the quoted part :)
---8<---
Changes:
v2: Remove SCM_RIGHTS fd scrubbing functionality
---8<---
Quoted 9 lines
in context, but the commit descriptions worry me that perhaps you are still intending on using the LSM framework to cut
SCM_RIGHTSpayloads from individual messages? Blocking messages at send time if they containSCM_RIGHTSis likely okay (pending proper implementation review), but modifying packets in flight in the LSM framework is not.Also, a quick administrative note, I see you have marked this as "bpf-next", however given the diffstat of the proposed changes this patchset should go to Linus via the LSM tree and not the BPF tree.
This was to kick the BPF CI for the selftest patch, and the __nullable arg suffix in patch 3 is BPF specific stuff, but I don't have preference here and whichever is fine to me.
Paul Moore
maintainerQuoted 17 lines · Kuniyuki Iwashima
From: Paul Moore <[email protected]>
Date: Sat, 14 Jun 2025 07:43:46 -0400Quoted 6 lines · Kuniyuki Iwashima
Quoted 5 lines
From: Kuniyuki Iwashima <[email protected]>
Since commit
77cbe1a6d873("af_unix: IntroduceSO_PASSRIGHTS."), we can disableSCM_RIGHTSper socket, but it's not flexible.Quoted 8 lines
Quoted 2 lines
This series allows us to implement more fine-grained filtering for
SCM_RIGHTSwith BPF LSM.My ability to review this over the weekend is limited due to device and network access, but I'll take a look next week.
That said, it would be good if you could clarify the "filtering" aspect of your comments; it may be obvious when I'm able to look at the full patchset
Quoted 6 lines
I meant to mention that just below the quoted part :)
---8<---
Changes:
v2: RemoveSCM_RIGHTSfd scrubbing functionality
---8<---
Thanks :)
While looking at your patches tonight, I was wondering if you had ever considered adding a new LSM hook to __scm_send() that specifically targets SCM_RIGHTS? I was thinking of something like this:
net/core/scm.c +0 -0
@@ -173,6 +173,9 @@ int __scm_send(struct socket *sock, struct msghdr *msg, stru
ct scm_cookie *p)
case SCM_RIGHTS:
if (!ops || ops->family != PF_UNIX)
goto error;
+ err = security_sock_scm_rights(sock);
+ if (err<0)
+ goto error;
err=scm_fp_copy(cmsg, &p->fp);
if (err<0)
goto error;... if I'm correct in my understanding of what you are trying to accomplish, I believe this should allow you to meet your goals with a much simpler and targeted approach. Or am I thinking about this wrong?
Signature · 1 line
paul-moore.com
From: Paul Moore <[email protected]>
Date: Wed, 18 Jun 2025 23:23:31 -0400
Quoted 47 lines · Kuniyuki Iwashima
Quoted 17 lines
From: Paul Moore <[email protected]>
Date: Sat, 14 Jun 2025 07:43:46 -0400Quoted 6 lines · Kuniyuki Iwashima
Quoted 5 lines
From: Kuniyuki Iwashima <[email protected]>
Since commit
77cbe1a6d873("af_unix: IntroduceSO_PASSRIGHTS."), we can disableSCM_RIGHTSper socket, but it's not flexible.Quoted 8 lines
Quoted 2 lines
This series allows us to implement more fine-grained filtering for
SCM_RIGHTSwith BPF LSM.My ability to review this over the weekend is limited due to device and network access, but I'll take a look next week.
That said, it would be good if you could clarify the "filtering" aspect of your comments; it may be obvious when I'm able to look at the full patchset
Quoted 6 lines
I meant to mention that just below the quoted part :)
---8<---
Changes:
v2: RemoveSCM_RIGHTSfd scrubbing functionality
---8<---Thanks :)
While looking at your patches tonight, I was wondering if you had ever considered adding a new LSM hook to
__scm_send()that specifically targetsSCM_RIGHTS? I was thinking of something like this:net/core/scm.c +3
-0@@ -173,6 +173,9 @@ int __scm_send(struct socket *sock, struct msghdr *msg, stru ct scm_cookie *p) case SCM_RIGHTS:if (!ops || ops->family !=PF_UNIX ) goto error; + err =security_sock_scm_rights (sock); +if (err<0 ) + goto error; err=scm_fp_copy (cmsg, &p->fp);if (err<0 )goto error;
Quoted 4 lines
... if I'm correct in my understanding of what you are trying to accomplish, I believe this should allow you to meet your goals with a much simpler and targeted approach. Or am I thinking about this wrong?
As BPF LSM is just a hook point and not tied to a specific socket, we cannot know who will receive the message in __scm_send().
Also, the hook must be after scm_fp_copy(), otherwise struct file is not yet available there.
Another way I thought of was to reuse LSM hook in sk_filter() (SOCK_STREAM needs to add it), but it returns 0 even when we drop skb, which will be less preferable.
BTW, I was about to send v3, what target tree should be specified in subject, bpf-next or something else ?
Paul Moore
maintainerQuoted 49 lines · Kuniyuki Iwashima
From: Paul Moore <[email protected]>
Date: Wed, 18 Jun 2025 23:23:31 -0400Quoted 47 lines · Kuniyuki Iwashima
Quoted 17 lines
From: Paul Moore <[email protected]>
Date: Sat, 14 Jun 2025 07:43:46 -0400Quoted 6 lines · Kuniyuki Iwashima
Quoted 5 lines
From: Kuniyuki Iwashima <[email protected]>
Since commit
77cbe1a6d873("af_unix: IntroduceSO_PASSRIGHTS."), we can disableSCM_RIGHTSper socket, but it's not flexible.Quoted 8 lines
Quoted 2 lines
This series allows us to implement more fine-grained filtering for
SCM_RIGHTSwith BPF LSM.My ability to review this over the weekend is limited due to device and network access, but I'll take a look next week.
That said, it would be good if you could clarify the "filtering" aspect of your comments; it may be obvious when I'm able to look at the full patchset
Quoted 6 lines
I meant to mention that just below the quoted part :)
---8<---
Changes:
v2: RemoveSCM_RIGHTSfd scrubbing functionality
---8<---Thanks :)
While looking at your patches tonight, I was wondering if you had ever considered adding a new LSM hook to
__scm_send()that specifically targetsSCM_RIGHTS? I was thinking of something like this:net/core/scm.c +3
-0@@ -173,6 +173,9 @@ int __scm_send(struct socket *sock, struct msghdr *msg, stru ct scm_cookie *p) case SCM_RIGHTS:if (!ops || ops->family !=PF_UNIX ) goto error; + err =security_sock_scm_rights (sock); +if (err<0 ) + goto error; err=scm_fp_copy (cmsg, &p->fp);if (err<0 )goto error;
Quoted 7 lines
Quoted 4 lines
... if I'm correct in my understanding of what you are trying to accomplish, I believe this should allow you to meet your goals with a much simpler and targeted approach. Or am I thinking about this wrong?
As BPF LSM is just a hook point and not tied to a specific socket, we cannot know who will receive the message in
__scm_send().
Okay, based on your patches, I'm assuming you're okay with just the socket endpoint, yes? Unfortunately, it's not really possible to get the receiving task on the send side.
Beyond that, and given the immediate goal of access control based on SCM_RIGHTS files, I think I'd rather see a unix_skb_parms passed to the LSM instead of a skb as it will make the individual LSM subsystem code a bit cleaner. I'd prefer a scm_cookie, but given the destructive nature of unix_scm_to_skb() and the fact that it is called before we've determined the socket endpoint (at least in the datagram case), I don't think that will work.
I'm also not overly excited about converting security_unix_may_send() into a per-msg/skb hook for both stream and datagram sends, that has the potential for increasing the overhead for LSMs that really only care about the datagram sends and the establishment of a stream connection.
I'm open to suggestions, thoughts, etc. but I think modifying security_unix_may_send() to take a unix_skb_params pointer, e.g. 'security_unix_may_send(sk, other, UNIXCB(skb))', and moving the SEQ_PACKET restriction out of unix_dgram_sendmsg() and into the existing LSM callbacks is okay. However, instead of adding security_unix_may_send() to unix_stream_sendmsg(), I would suggest the creation of a new hook, security_unix_send_scm(sk, other, UNIXCB(skb)) (feel free to suggest a different name), that would handle the per-skb access control for streams.
Of course there is an issue with unix_skb_params not being defined outside of net/unix, but from a practical perspective that is going to be a challenge regardless of which, unix_skb_params or the full skb, is passed to the LSM hook. You'll need to solve this with all the relevant stakeholders regardless.
As a FYI, we've documented our policy/guidance on both modifying existing LSM hooks as well as adding new hooks, see the link below.
https://github.com/LinuxSecurityModule/kernel/blob/main/README.md#new-lsm-hooks
Quoted 2 lines
BTW, I was about to send v3, what target tree should be specified in subject, bpf-next or something else ?
I wouldn't worry too much about the subject, prefix, etc. as the To/CC line tends to be more important, at least from a LSM subsystem perspective. I would ensure that you send it to the LSM list and any related lists, MAINTAINERS and/or scripts/get_maintainer.pl is your friend here. Since this is almost surely LSM framework tree material, my general rule is that I'll handle any merge conflicts so long as your patch is based on the lsm/dev branch or Linus' default branch. Of course, you can base your patch against any tree you like, and I'll grab it, but if there are significant merge conflicts you'll likely get a nasty email back about that ;)
As far as a v3 is concerned, while I generally don't like to tell people *not* to post patches, I'll leave that up to you, I do think we could benefit from some additional discussion here before you post a v3. Reviewer time is precious and I would hate to see it spent on an approach that still has open questions, just in case we all decide to go in a different direction.
Signature · 1 line
paul-moore.com