tests/bpf: Add tests for SELinux BPF token access control
PATCH v6 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
policy/Makefile | 7 + policy/test_bpf_token.te | 42 +++ tests/bpf/Makefile | 6 +- tests/bpf/bpf_common.h | 10 + tests/bpf/bpf_test.c | 59 +++-- tests/bpf/test | 35 +++ tests/bpf/token_test.c | 559 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 700 insertions(+), 18 deletions(-) create mode 100644 policy/test_bpf_token.te create mode 100644 tests/bpf/token_test.c
policy/Makefile +7 -0
@@ -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 +42 -0
42 lines not on this page · raw message
tests/bpf/Makefile +4 -2
@@ -1,5 +1,5 @@ TARGETS = bpf_test -DEPS = bpf_common.c bpf_common.h +SRCS = bpf_test.c bpf_common.c token_test.c LDLIBS += -lselinux -lbpf # export so that BPF_ENABLED entries get built correctly on local build
@@ -14,4 +14,6 @@ clean: rm -f $(TARGETS) test_sock flag *_flag @set -e; for i in $(BPF_ENABLED); do $(MAKE) -C $$i clean ; done -$(TARGETS): $(DEPS) +$(TARGETS): $(SRCS) + $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) +
tests/bpf/bpf_common.h +10 -0
@@ -12,6 +12,8 @@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 );/* edited eBPF instruction library */ /* Short form of mov, dst_reg = imm32 */
@@ -32,3 +34,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 +43 -16
109 lines not on this page · raw message
tests/bpf/test +35 -0
@@ -6,12 +6,25 @@ BEGIN { $basedir =~ s|(.*)/[^/]*|$1|; $fdr_basedir = "$basedir/../fdreceive"; $binder_basedir = "$basedir/../binder"; + $test_bpf_token = 0; $test_bpf_count = 7; $test_fdreceive_count = 4; $test_count = $test_bpf_count + $test_fdreceive_count; + if ( + system( +"grep -q 1 /sys/fs/selinux/policy_capabilities/bpf_token_perms 2> /dev/null" + ) == 0 + ) + { + $test_bpf_token = 1; + + $test_bpf_token_count = 4; + $test_count += $test_bpf_token_count; + } + # allow info to be shown during tests $v = $ARGV[0]; if ($v) {
@@ -92,6 +105,28 @@ $result = system "runcon -t test_bpf_deny_prog_run_t $basedir/bpf_test -p $v 2>&1"; ok($result); +if ($test_bpf_token) { + + # BPF token - BPF_MAP_CREATE_AS, BPF_PROG_LOAD_AS + $result = system "runcon -t test_bpf_t $basedir/bpf_test -c $v"; + ok( $result eq 0 ); + + $result = system "runcon -t test_bpf_t $basedir/bpf_test -l $v"; + ok( $result eq 0 ); + + # BPF token - deny BPF_MAP_CREATE_AS + $result = + system + "runcon -t test_bpf_deny_map_create_as_t $basedir/bpf_test -c $v 2>&1"; + ok($result); + + # BPF token - deny BPF_PROG_LOAD_AS + $result = + system + "runcon -t test_bpf_deny_prog_load_as_t $basedir/bpf_test -l $v 2>&1"; + ok($result); +} + # ################ BPF Tests for fdreceive ####################### #
tests/bpf/token_test.c new file +559 -0
559 lines not on this page · raw message
Signature · 1 line
2.50.1
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.
- Successfulmap_createandprog_loadwhen 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/develas 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 "(policycapbpf_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 7 lines
When finished testing, you can semodule -r base
bpf_token_permsto undo the two module changes and restore your all_perms.spt file from the saved .orig file.Signed-off-by Eric Suen <[email protected]>Tested-by Daniel Durning <[email protected]>Reviewed-by Daniel Durning <[email protected]>
You aren't supposed to keep Acked-by, Tested-by, or Reviewed-by lines from others if you make any changes to the patch itself since they haven't necessarily reviewed or tested the updated version of the patch. That said, would recommend waiting and seeing if Daniel re-affirms his tags for you rather than re-submitting without them.
Also, one nitpick below but no need to re-submit just for it.
Quoted 54 lines
---
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
- Addedtoken_test.cwhich was missing from previous patchChanges 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 definedChanges 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 capabilitybpf_token_permsis definedChanges in v6:
- Updated commit description per review feedback
- Update comment in policy/Makefile
- Fixed missing new line in tests/bpf/Makefilepolicy/Makefile | 7 + policy/test_bpf_token.te | 42 +++ tests/bpf/Makefile | 6 +- tests/bpf/bpf_common.h | 10 + tests/bpf/bpf_test.c | 59 +++-- tests/bpf/test | 35 +++ tests/bpf/token_test.c | 559 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 700 insertions(+), 18 deletions(-) create mode 100644 policy/test_bpf_token.te create mode 100644 tests/bpf/token_test.ctests/bpf/Makefile +1
-1@@ -1,5 +1,5 @@ TARGETS = bpf_test -DEPS = bpf_common.c bpf_common.h +SRCS = bpf_test.c bpf_common.c token_test.c LDLIBS += -lselinux -lbpf # export so that BPF_ENABLED entries get built correctly on local build
tests/bpf/Makefile clean: · line 19
Quoted 7 lines
tests/bpf/Makefile +3
-1@@ -14,4 +14,6 @@ clean: rm -f $(TARGETS) test_sock flag *_flag @set -e; for i in $(BPF_ENABLED); do $(MAKE) -C $$i clean ; done -$(TARGETS): $(DEPS) +$(TARGETS): $(SRCS) + $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) +
When you added a terminating newline for the Makefile, you also added an extra empty line above, which is generally frowned upon. But as I said above, no need to re-spin just for this.
Quoted 41 lines · Stephen Smalley
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.
- Successfulmap_createandprog_loadwhen 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/develas 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 "(policycapbpf_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.sptQuoted 7 lines
When finished testing, you can semodule -r base
bpf_token_permsto undo the two module changes and restore your all_perms.spt file from the saved .orig file.Signed-off-by Eric Suen <[email protected]>Tested-by Daniel Durning <[email protected]>Reviewed-by Daniel Durning <[email protected]>
Quoted 5 lines
You aren't supposed to keep Acked-by, Tested-by, or Reviewed-by lines from others if you make any changes to the patch itself since they haven't necessarily reviewed or tested the updated version of the patch. That said, would recommend waiting and seeing if Daniel re-affirms his tags for you rather than re-submitting without them.
Everything still looks fine to me, so my tags can stay.