From: Paul Houssel <paulhoussel2@gmail.com>
To: paul.houssel@orange.com, "Andrii Nakryiko" <andrii@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Paul Houssel" <paulhoussel2@gmail.com>,
"KP Singh" <kpsingh@kernel.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Song Liu" <song@kernel.org>,
"Martin KaFai Lau" <martin.lau@kernel.org>,
"Christian König" <christian.koenig@amd.com>,
"Florian Westphal" <fw@strlen.de>,
"T.J. Mercier" <tjmercier@google.com>,
"Li RongQing" <lirongqing@baidu.com>,
"Paul Chaignon" <paul.chaignon@gmail.com>,
"D. Wythe" <alibuda@linux.alibaba.com>,
"Jakub Kicinski" <kuba@kernel.org>
Cc: "Stanislav Fomichev" <sdf@fomichev.me>, bpf@vger.kernel.org
Subject: [PATCH v3 2/2] selftests/bpf: add tests to verify the enforcement of CONFIG_CGROUP_LSM_NUM
Date: Wed, 6 May 2026 17:05:47 +0200 [thread overview]
Message-ID: <20260506150547.767315-3-paulhoussel2@gmail.com> (raw)
In-Reply-To: <20260506150547.767315-1-paulhoussel2@gmail.com>
Add a selftest that verifies the kernel correctly enforces
CONFIG_CGROUP_LSM_NUM as the maximum number of concurrently attachable
per-cgroup LSM hook slots.
The BPF program side (progs/cgroup_lsm_num.c) defines 12 lsm_cgroup
programs, each attached to a distinct LSM hook. The test side
(prog_tests/cgroup_lsm_num.c) attempts to attach all 12 programs one by
one to a cgroup, and verifies that exactly 10 succeed and 2 are rejected,
matching the value of CONFIG_CGROUP_LSM_NUM set to 10 in the selftest
Kconfig fragment.
Signed-off-by: Paul Houssel <paulhoussel2@gmail.com>
---
tools/testing/selftests/bpf/config | 1 +
.../selftests/bpf/prog_tests/cgroup_lsm_num.c | 60 +++++++++++++++++++
.../selftests/bpf/progs/cgroup_lsm_num.c | 46 ++++++++++++++
3 files changed, 107 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_lsm_num.c
create mode 100644 tools/testing/selftests/bpf/progs/cgroup_lsm_num.c
diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index 24855381290d..e4c5dd86c640 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -11,6 +11,7 @@ CONFIG_BPF_STREAM_PARSER=y
CONFIG_BPF_SYSCALL=y
# CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set
CONFIG_CGROUP_BPF=y
+CONFIG_CGROUP_LSM_NUM=10
CONFIG_CRYPTO_HMAC=y
CONFIG_CRYPTO_SHA256=y
CONFIG_CRYPTO_USER_API=y
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_lsm_num.c b/tools/testing/selftests/bpf/prog_tests/cgroup_lsm_num.c
new file mode 100644
index 000000000000..1c5825c6c3d0
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_lsm_num.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Orange */
+
+/*
+ * Test that the kernel enforces CONFIG_CGROUP_LSM_NUM as the maximum
+ * number of concurrently used per-cgroup LSM hook slots.
+ *
+ * - load a BPF object with 12 programs each on a distinct lsm_cgroup hook
+ * - attach them one by one via bpf_program__attach_cgroup()
+ * - at some point the slots are exhausted and attachment fails
+ * - verify that 10 succeed attachment and 2 fail
+ */
+
+#include <test_progs.h>
+#include <bpf/bpf.h>
+
+#include "cgroup_lsm_num.skel.h"
+#include "cgroup_helpers.h"
+
+void test_cgroup_lsm_num(void)
+{
+ struct cgroup_lsm_num *skel = NULL;
+ struct bpf_program *prog;
+ int cgroup_fd = -1;
+ int attached = 0;
+ int failed = 0;
+
+ cgroup_fd = test__join_cgroup("/cgroup_lsm_num");
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ skel = cgroup_lsm_num__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto out;
+
+ bpf_object__for_each_program(prog, skel->obj) {
+ struct bpf_link *link;
+
+ link = bpf_program__attach_cgroup(prog, cgroup_fd);
+ if (!link) {
+ if (errno == EOPNOTSUPP) {
+ test__skip();
+ goto out;
+ }
+ failed++;
+ } else {
+ attached++;
+ }
+ }
+
+ // CONFIG_CGROUP_LSM_NUM set to 10
+ // -> 10 programs shall be attached
+ ASSERT_EQ(attached, 10, "at least one attached");
+ // -> 2 programs shall be rejected
+ ASSERT_EQ(failed, 2, "limit was enforced");
+
+out:
+ close(cgroup_fd);
+ cgroup_lsm_num__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/cgroup_lsm_num.c b/tools/testing/selftests/bpf/progs/cgroup_lsm_num.c
new file mode 100644
index 000000000000..662aee2283c2
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/cgroup_lsm_num.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Orange */
+
+/*
+ * 12 LSM programs with lsm_cgroup attachment type, each on a distinct LSM
+ * hook. Used by prog_tests/cgroup_lsm_num.c to verify that the kernel
+ * enforces the CONFIG_CGROUP_LSM_NUM limit on unique per-cgroup LSM hook
+ * slots. With CONFIG_CGROUP_LSM_NUM set to 10, 10 shall be attached and 2
+ * rejected.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define LSM_CGROUP_HOOK(name, hook) \
+ SEC("lsm_cgroup/" #hook) \
+ int BPF_PROG(name) { return 1; }
+
+
+LSM_CGROUP_HOOK(hook0, socket_create)
+
+LSM_CGROUP_HOOK(hook1, socket_post_create)
+
+LSM_CGROUP_HOOK(hook2, socket_socketpair)
+
+LSM_CGROUP_HOOK(hook3, socket_bind)
+
+LSM_CGROUP_HOOK(hook4, socket_connect)
+
+LSM_CGROUP_HOOK(hook5, socket_listen)
+
+LSM_CGROUP_HOOK(hook6, socket_accept)
+
+LSM_CGROUP_HOOK(hook7, socket_sendmsg)
+
+LSM_CGROUP_HOOK(hook8, socket_recvmsg)
+
+LSM_CGROUP_HOOK(hook9, socket_getsockname)
+
+LSM_CGROUP_HOOK(hook10, socket_getpeername)
+
+LSM_CGROUP_HOOK(hook11, socket_shutdown)
+
--
2.54.0
next prev parent reply other threads:[~2026-05-06 15:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 15:05 [PATCH v3 0/2] Introduce CONFIG_CGROUP_LSM_NUM to render BPF_LSM_CGROUP attachment limit configurable Paul Houssel
2026-05-06 15:05 ` [PATCH v3 1/2] bpf: render CGROUP_LSM_NUM configurable as a KConfig Paul Houssel
2026-05-06 15:52 ` bot+bpf-ci
2026-05-06 16:11 ` Paul Chaignon
2026-05-06 21:08 ` sashiko-bot
2026-05-07 16:39 ` Paul Houssel
2026-05-08 22:16 ` Alexei Starovoitov
2026-05-06 15:05 ` Paul Houssel [this message]
2026-05-06 16:05 ` [PATCH v3 2/2] selftests/bpf: add tests to verify the enforcement of CONFIG_CGROUP_LSM_NUM Paul Chaignon
2026-05-06 21:24 ` sashiko-bot
2026-05-06 16:13 ` [PATCH v3 0/2] Introduce CONFIG_CGROUP_LSM_NUM to render BPF_LSM_CGROUP attachment limit configurable Paul Chaignon
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=20260506150547.767315-3-paulhoussel2@gmail.com \
--to=paulhoussel2@gmail.com \
--cc=alibuda@linux.alibaba.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=christian.koenig@amd.com \
--cc=fw@strlen.de \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=lirongqing@baidu.com \
--cc=martin.lau@kernel.org \
--cc=paul.chaignon@gmail.com \
--cc=paul.houssel@orange.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=tjmercier@google.com \
--cc=yonghong.song@linux.dev \
/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