From: Mahe Tardy <mahe.tardy@gmail.com>
To: bpf@vger.kernel.org
Cc: andrew+netdev@lunn.ch, andrii@kernel.org, ast@kernel.org,
daniel@iogearbox.net, davem@davemloft.net, eddyz87@gmail.com,
edumazet@google.com, john.fastabend@gmail.com, kuba@kernel.org,
liamwisehart@meta.com, martin.lau@linux.dev, pabeni@redhat.com,
song@kernel.org, netdev@vger.kernel.org, sdf.kernel@gmail.com,
ameryhung@gmail.com, kuniyu@google.com, memxor@gmail.com,
Mahe Tardy <mahe.tardy@gmail.com>
Subject: [PATCH bpf-next v4 4/5] selftests/bpf: Test forbidden bpf_ksock_send() LSM attach
Date: Thu, 6 Aug 2026 18:22:33 +0000 [thread overview]
Message-ID: <20260806182234.380833-5-mahe.tardy@gmail.com> (raw)
In-Reply-To: <20260806182234.380833-1-mahe.tardy@gmail.com>
The bpf_ksock_send() kfunc eventually calls security_socket_sendmsg(),
thus creating a possible recursion if a program calling the kfunc is
attached on that specific hook. A filter is added on the kfunc
registration to prevent that at load time from the verifier. This test
exercises that the verifier will reject such program on that attach
point.
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
.../testing/selftests/bpf/prog_tests/ksock.c | 6 ++++
.../selftests/bpf/progs/ksock_lsm_verifier.c | 36 +++++++++++++++++++
2 files changed, 42 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c
diff --git a/tools/testing/selftests/bpf/prog_tests/ksock.c b/tools/testing/selftests/bpf/prog_tests/ksock.c
index 30e89d34d388..fb1a332eb3da 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksock.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksock.c
@@ -6,6 +6,7 @@
#include "test_progs.h"
#include "network_helpers.h"
#include "ksock_lsm.skel.h"
+#include "ksock_lsm_verifier.skel.h"
#define NS_TEST "ksock_lsm_ns"
#define RECV_PORT 7777
@@ -125,3 +126,8 @@ void test_ksock_lsm(void)
SYS_NOFAIL("ip netns del %s >/dev/null 2>&1", NS_TEST);
ksock_lsm__destroy(skel);
}
+
+void test_ksock_lsm_verifier(void)
+{
+ RUN_TESTS(ksock_lsm_verifier);
+}
diff --git a/tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c b/tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c
new file mode 100644
index 000000000000..5b969e03b6d6
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Isovalent */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_tracing_net.h"
+#include "ksock_common.h"
+
+char send_data[11] = "dummy data";
+
+SEC("lsm.s/socket_sendmsg")
+__description("bpf_ksock_send is rejected from socket_sendmsg LSM hook")
+__failure __msg("calling kernel function bpf_ksock_send is not allowed")
+int BPF_PROG(ksock_socket_sendmsg, struct socket *sock, struct msghdr *msg,
+ int size, int ret)
+{
+ struct __ksock_ctx_value *v;
+ struct bpf_ksock *ks;
+
+ v = ksock_ctx_value_lookup();
+ if (!v)
+ return ret;
+
+ ks = bpf_kptr_xchg(&v->ctx, NULL);
+ if (!ks)
+ return ret;
+
+ bpf_ksock_send(ks, send_data, sizeof(send_data));
+ bpf_ksock_release(ks);
+
+ return ret;
+}
+
+char __license[] SEC("license") = "GPL";
--
2.34.1
next prev parent reply other threads:[~2026-08-06 18:22 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-06 18:22 [PATCH bpf-next v4 0/5] Introduce bpf_ksock Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 1/5] net: Add connect_socket() helper Mahe Tardy
2026-08-06 20:09 ` Song Liu
2026-08-07 1:53 ` Jiayuan Chen
2026-08-07 9:51 ` Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 2/5] bpf: Add ksock kfuncs Mahe Tardy
2026-08-07 3:08 ` Jiayuan Chen
2026-08-07 9:56 ` Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 3/5] selftests/bpf: Add ksock kfunc test Mahe Tardy
2026-08-07 9:12 ` Jiayuan Chen
2026-08-07 10:11 ` Mahe Tardy
2026-08-06 18:22 ` Mahe Tardy [this message]
2026-08-06 18:22 ` [PATCH bpf-next v4 5/5] selftests/bpf: Add ksock test for async callback guard Mahe Tardy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260806182234.380833-5-mahe.tardy@gmail.com \
--to=mahe.tardy@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=liamwisehart@meta.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf.kernel@gmail.com \
--cc=song@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox