Skip to contentSkip to conversation

tests/bpf: Add tests for SELinux BPF token access control

PATCH In review Eric Suen 4 participants 4 messages Dec 5, 2025 – Feb 11 SELinux security module

lore
Share & tools
lore

Eric Suen

author
#0 lore raw

PATCH v7 1/1 tests/bpf: Add tests for SELinux BPF token access control

This patch adds new tests to verify the SELinux support for BPF token access control, as introduced in the corresponding kernel patch:
https://lore.kernel.org/selinux/[email protected]/

Note that new tests require changes in libsepol which is covered in
https://lore.kernel.org/selinux/[email protected]/

Four new tests are added to cover both positive and negative scenarios, ensuring that the SELinux policy enforcement on BPF token usage behaves as expected.
- Successful map_create and prog_load when SELinux permissions are granted.
- Enforcement of SELinux policy restrictions when access is denied.

For testing purposes, you can update the base policy by manually modifying your base module and tweaking /usr/share/selinux/devel as follows:
sudo semodule -c -E base
sudo sed -i.orig "s/map_create/map_create map_create_as/" base.cil sudo sed -i "s/prog_load/prog_load prog_load_as/" base.cil sudo semodule -i base.cil
echo "(policycap bpf_token_perms)" > bpf_token_perms.cil sudo semodule -i bpf_token_perms.cil
sudo cp /usr/share/selinux/devel/include/support/all_perms.spt \
/usr/share/selinux/devel/include/support/all_perms.spt.orig
sudo sed -i "s/map_create/map_create map_create_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt
sudo sed -i "s/prog_load/prog_load prog_load_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt

When finished testing, you can semodule -r base bpf_token_perms to undo the two module changes and restore your all_perms.spt file from the saved .orig file.

---
Changes in v2:
- Removed allow rule for 'kernel_t' in test_bpf.te which was added due to a bug in the kernel
- Cleaned up other unnecessary rules in test_bpf.te
- Added token_test.c which was missing from previous patch

Changes in v3:
- Added original license in 'token_test.c'
- Updated patch description to
- replace 'base.sil' with 'base.cil'
- Remove extra quotation mark in 'sudo 'sed -i "s/"map_create'

Changes in v4:
- Updated 'token_test.c' to write error messages only when DEBUG is defined

Changes in v5:
- Created test_bpf_token.te which gets loaded when required bpf permissions (i.e. map_create_as, prog_load_as) are available, and policy capability (i.e. bpf_token_perms) is defined
- Added condition in tests/bpf/test to run new tests when policy capability bpf_token_perms is defined

Changes in v6:
- Updated commit description per review feedback
- Update comment in policy/Makefile
- Fixed missing new line in tests/bpf/Makefile

Changes in v7:
- Added new tests to verify cross domain scenarios in selinux_bpf_token_capable

 policy/Makefile                |   7 +
 policy/test_bpf_token.te       |  74 ++++++
 tests/bpf/Makefile             |  11 +-
 tests/bpf/bpf_common.h         |  13 ++
 tests/bpf/bpf_test.c           |  84 +++++--
 tests/bpf/test                 |  45 ++++
 tests/bpf/token_child_helper.c |  95 ++++++++
 tests/bpf/token_test.c         | 410 +++++++++++++++++++++++++++++++++
 tests/bpf/token_test_common.c  | 181 +++++++++++++++
 tests/bpf/token_test_common.h  |  96 ++++++++
 10 files changed, 997 insertions(+), 19 deletions(-)
 create mode 100644 policy/test_bpf_token.te
 create mode 100644 tests/bpf/token_child_helper.c
 create mode 100644 tests/bpf/token_test.c
 create mode 100644 tests/bpf/token_test_common.c
 create mode 100644 tests/bpf/token_test_common.h
policy/Makefile +7 -0
index ffd774d..915555c 100644
@@ -105,6 +105,13 @@ ifeq ($(shell grep -q bpf $(POLDEV)/include/support/all_perms.spt && echo true),
 TARGETS += test_bpf.te test_fdreceive_bpf.te test_binder_bpf.te
 endif
 
+# bpf token test dependencies: bpf permission, bpf_token capability
+ifeq ($(shell [ -f /sys/fs/selinux/class/bpf/perms/map_create_as ] && [ -f /sys/fs/selinux/class/bpf/perms/prog_load_as ] && echo true),true)
+ifeq ($(shell [ -f $(SELINUXFS)/policy_capabilities/bpf_token_perms ] && grep -q 1 $(SELINUXFS)/policy_capabilities/bpf_token_perms && echo true),true)
+TARGETS += test_bpf_token.te
+endif
+endif
+
 ifeq ($(shell grep -q all_key_perms $(POLDEV)/include/support/all_perms.spt && echo true),true)
 TARGETS += test_keys.te test_watchkey.te
 endif
policy/test_bpf_token.te new file +74 -0

74 lines not on this page · raw message

tests/bpf/Makefile +8 -3
index 1ae8ce9..a24182b 100644
@@ -1,5 +1,6 @@
-TARGETS = bpf_test
-DEPS = bpf_common.c bpf_common.h
+TARGETS = bpf_test token_child_helper
+BPF_TEST_SRCS = bpf_test.c bpf_common.c token_test.c token_test_common.c
+TOKEN_CHILD_HELPER_SRCS = token_child_helper.c bpf_common.c token_test_common.c
 LDLIBS += -lselinux -lbpf
 
 # export so that BPF_ENABLED entries get built correctly on local build
@@ -14,4 +15,8 @@ clean:
 	rm -f $(TARGETS) test_sock flag *_flag
 	@set -e; for i in $(BPF_ENABLED); do $(MAKE) -C $$i clean ; done
 
-$(TARGETS): $(DEPS)
+bpf_test: $(BPF_TEST_SRCS)
+	$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)
+
+token_child_helper: $(TOKEN_CHILD_HELPER_SRCS)
+	$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)
tests/bpf/bpf_common.h +13 -0
index 44ac28f..cae36e5 100644
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <stdbool.h>
+#include <libgen.h>
 #include <selinux/selinux.h>
 #include <bpf/bpf.h>
 #include <linux/bpf.h>
@@ -12,6 +13,10 @@
 extern int create_bpf_map(void);
 extern int create_bpf_prog(void);
 extern void bpf_setrlimit(void);
+extern int test_bpf_map_create(void);
+extern int test_bpf_prog_load(void);
+extern int test_bpf_token_cap_cross_success(void);
+extern int test_bpf_token_cap_cross_failure(void);
 
 /* edited eBPF instruction library */
 /* Short form of mov, dst_reg = imm32 */
@@ -32,3 +37,11 @@ extern void bpf_setrlimit(void);
 					       .off   = 0,			\
 							.imm   = 0 })
 
+/* Raw code statement block */
+#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM)			\
+	((struct bpf_insn) {					\
+		.code  = CODE,					\
+			.dst_reg = DST,					\
+				.src_reg = SRC,					\
+					.off   = OFF,					\
+						.imm   = IMM })
tests/bpf/bpf_test.c +68 -16

134 lines not on this page · raw message

tests/bpf/test +45 -0

63 lines not on this page · raw message

tests/bpf/token_child_helper.c new file +95 -0

95 lines not on this page · raw message

tests/bpf/token_test.c new file +410 -0

410 lines not on this page · raw message

tests/bpf/token_test_common.c new file +181 -0

181 lines not on this page · raw message

tests/bpf/token_test_common.h new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.50.1
Reviewed-by Tested-by on v7 1/1
#1 lore raw
Quoted 31 lines · Eric Suen

This patch adds new tests to verify the SELinux support for BPF token access control, as introduced in the corresponding kernel patch:
https://lore.kernel.org/selinux/[email protected]/

Note that new tests require changes in libsepol which is covered in
https://lore.kernel.org/selinux/[email protected]/

Four new tests are added to cover both positive and negative scenarios, ensuring that the SELinux policy enforcement on BPF token usage behaves as expected.
- Successful map_create and prog_load when SELinux permissions are granted.
- Enforcement of SELinux policy restrictions when access is denied.

For testing purposes, you can update the base policy by manually modifying your base module and tweaking /usr/share/selinux/devel as follows:
sudo semodule -c -E base
sudo sed -i.orig "s/map_create/map_create map_create_as/" base.cil sudo sed -i "s/prog_load/prog_load prog_load_as/" base.cil sudo semodule -i base.cil
echo "(policycap bpf_token_perms)" > bpf_token_perms.cil sudo semodule -i bpf_token_perms.cil
sudo cp /usr/share/selinux/devel/include/support/all_perms.spt \
/usr/share/selinux/devel/include/support/all_perms.spt.orig
sudo sed -i "s/map_create/map_create map_create_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt
sudo sed -i "s/prog_load/prog_load prog_load_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt

Quoted 5 lines

When finished testing, you can semodule -r base bpf_token_perms to undo the two module changes and restore your all_perms.spt file from the saved .orig file.

I tested this while on the latest dev branch of SELinux and using the patched userspace. Everything ran correctly but I noticed that token_child_helper was seemingly missing from the .gitignore.

maintainer
#2 lore raw
Quoted 37 lines · Daniel Durning
Quoted 35 lines · Eric Suen

This patch adds new tests to verify the SELinux support for BPF token access control, as introduced in the corresponding kernel patch:
https://lore.kernel.org/selinux/[email protected]/

Note that new tests require changes in libsepol which is covered in
https://lore.kernel.org/selinux/[email protected]/

Four new tests are added to cover both positive and negative scenarios, ensuring that the SELinux policy enforcement on BPF token usage behaves as expected.
- Successful map_create and prog_load when SELinux permissions are granted.
- Enforcement of SELinux policy restrictions when access is denied.

For testing purposes, you can update the base policy by manually modifying your base module and tweaking /usr/share/selinux/devel as follows:
sudo semodule -c -E base
sudo sed -i.orig "s/map_create/map_create map_create_as/" base.cil sudo sed -i "s/prog_load/prog_load prog_load_as/" base.cil sudo semodule -i base.cil
echo "(policycap bpf_token_perms)" > bpf_token_perms.cil sudo semodule -i bpf_token_perms.cil
sudo cp /usr/share/selinux/devel/include/support/all_perms.spt \
/usr/share/selinux/devel/include/support/all_perms.spt.orig
sudo sed -i "s/map_create/map_create map_create_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt
sudo sed -i "s/prog_load/prog_load prog_load_as/" \
/usr/share/selinux/devel/include/support/all_perms.spt

When finished testing, you can semodule -r base bpf_token_perms to undo the two module changes and restore your all_perms.spt file from the saved .orig file.

Quoted 8 lines
Quoted 1 line

I tested this while on the latest dev branch of SELinux and using the patched userspace. Everything ran correctly but I noticed that token_child_helper was seemingly missing from the .gitignore.

Thanks, applied. NB This won't get exercised by the automated testing until/unless someone updates the tmt configuration to apply the necessary policy changes above or until these get defined in Fedora policy.

maintainer
#3 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 1 line · Daniel Durning

On Thu, Dec 4, 2025 at 10:33 PM Eric Suen <[email protected]> wrote:

... This won't get exercised by the automated testing until/unless someone updates the tmt configuration to apply the necessary policy changes above or until these get defined in Fedora policy.

I'm thinking out loud here ... it would be nice if we had a set of Fedora Rawhide packages (COPR anyone?) for the SELinux userspace and policy that tracked the upstream development tree (somewhat similar to linux-next for the kernel) so that people could easily test these new features. I don't think this would be too difficult to automate, but it's been a bit since I've looked at it.

What do others think?

Or has this already been done and I simply missed it?

Signature · 1 line
paul-moore.com