[PATCH v1 bpf-next 5/5] selftest: bpf: Add test for bpf_unix_scrub_fds().
Kuniyuki Iwashima
kuniyu at amazon.com
Mon May 5 21:56:50 UTC 2025
This test performs the following for all AF_UNIX socket types
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 scrubs SCM_RIGHTS fds
5. Send the receiver's fd from the sender to the receiver
6. Check if the fd was scrubbed
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
...
#175/1 lsm_unix_may_send/SOCK_STREAM:OK
#175/2 lsm_unix_may_send/SOCK_DGRAM:OK
#175/3 lsm_unix_may_send/SOCK_SEQPACKET:OK
#175 lsm_unix_may_send:OK
Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED
Signed-off-by: Kuniyuki Iwashima <kuniyu at amazon.com>
---
.../bpf/prog_tests/lsm_unix_may_send.c | 160 ++++++++++++++++++
.../selftests/bpf/progs/lsm_unix_may_send.c | 30 ++++
2 files changed, 190 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
diff --git a/tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c b/tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c
new file mode 100644
index 000000000000..50b2547e63cf
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/lsm_unix_may_send.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Amazon.com Inc. or its affiliates. */
+
+#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)
+{
+ 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;
+
+ ret = sendmsg(sender_fd, &msg, 0);
+ if (!ASSERT_EQ(ret, MSG_LEN, "sendmsg(Hello)"))
+ return -EINVAL;
+
+ 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);
+
+ 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 (lsm_attached) {
+ if (!ASSERT_ERR_PTR(CMSG_FIRSTHDR(&msg), "cmsg filtered"))
+ return -EINVAL;
+ } else {
+ 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;
+
+ receiver_fd = cmsg.fd;
+ }
+
+ memset(buf, 0, sizeof(buf));
+
+ 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]);
+ if (err)
+ goto close;
+
+ err = recv_fd(socket_fds[1], false);
+ if (err)
+ goto close;
+
+ link = bpf_program__attach_lsm(skel->progs.unix_scrub_scm_rights);
+ if (!ASSERT_OK_PTR(link, "attach lsm"))
+ goto close;
+
+ err = send_fd(socket_fds[0], socket_fds[1]);
+ if (err)
+ goto close;
+
+ err = recv_fd(socket_fds[1], true);
+ if (err)
+ goto close;
+
+ err = bpf_link__destroy(link);
+ ASSERT_EQ(err, 0, "destroy 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);
+}
diff --git a/tools/testing/selftests/bpf/progs/lsm_unix_may_send.c b/tools/testing/selftests/bpf/progs/lsm_unix_may_send.c
new file mode 100644
index 000000000000..c2459ba2c33d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/lsm_unix_may_send.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Amazon.com Inc. or its affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_tracing.h>
+
+#ifndef EPERM
+#define EPERM 1
+#endif
+
+SEC("lsm/unix_may_send")
+int BPF_PROG(unix_scrub_scm_rights,
+ struct socket *sock, struct socket *other, struct sk_buff *skb)
+{
+ struct unix_skb_parms *cb;
+
+ if (!skb)
+ return 0;
+
+ cb = (struct unix_skb_parms *)skb->cb;
+ if (!cb->fp)
+ return 0;
+
+ if (bpf_unix_scrub_fds(skb))
+ return -EPERM;
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.49.0
More information about the Linux-security-module-archive
mailing list