tests/bpf: Add tests for SELinux BPF token access control
PATCH v3 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]/
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 cp base.cil base.cil.orig
sudo sed -i "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'
--- policy/test_bpf.te | 39 +++ tests/bpf/Makefile | 5 +- tests/bpf/bpf_common.h | 10 + tests/bpf/bpf_test.c | 59 +++-- tests/bpf/test | 21 +- tests/bpf/token_test.c | 543 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 658 insertions(+), 19 deletions(-) create mode 100644 tests/bpf/token_test.c
policy/test_bpf.te +39 -0
@@ -57,3 +57,42 @@ typeattribute test_bpf_deny_prog_run_t bpfdomain; allow test_bpf_deny_prog_run_t self:process { setrlimit }; allow test_bpf_deny_prog_run_t self:capability { sys_resource sys_admin }; allow test_bpf_deny_prog_run_t self:bpf { map_create map_read map_write prog_load }; + +################### Allow map_create_as and prog_load_as ################### +fs_list_bpf_dirs(test_bpf_t) +allow test_bpf_t bpf_t:filesystem mount; +allow test_bpf_t root_t:dir mounton; +allow test_bpf_t self:bpf { map_create_as prog_load_as }; +allow test_bpf_t self:cap2_userns { bpf perfmon }; +allow test_bpf_t self:cap_userns { net_admin setgid setuid sys_admin }; +allow test_bpf_t self:user_namespace create; + +############################ Deny map_create_as ############################ +type test_bpf_deny_map_create_as_t; +testsuite_domain_type(test_bpf_deny_map_create_as_t) +typeattribute test_bpf_deny_map_create_as_t bpfdomain; +allow test_bpf_deny_map_create_as_t self:process { setrlimit }; +allow test_bpf_deny_map_create_as_t self:capability { sys_resource sys_admin }; + +fs_list_bpf_dirs(test_bpf_deny_map_create_as_t) +allow test_bpf_deny_map_create_as_t bpf_t:filesystem mount; +allow test_bpf_deny_map_create_as_t root_t:dir mounton; +allow test_bpf_deny_map_create_as_t self:bpf { map_create map_read map_write prog_load prog_load_as }; +allow test_bpf_deny_map_create_as_t self:cap2_userns { bpf }; +allow test_bpf_deny_map_create_as_t self:cap_userns { setgid setuid sys_admin }; +allow test_bpf_deny_map_create_as_t self:user_namespace create; + +############################ Deny prog_load_as ############################# +type test_bpf_deny_prog_load_as_t; +testsuite_domain_type(test_bpf_deny_prog_load_as_t) +typeattribute test_bpf_deny_prog_load_as_t bpfdomain; +allow test_bpf_deny_prog_load_as_t self:process { setrlimit }; +allow test_bpf_deny_prog_load_as_t self:capability { sys_resource sys_admin }; + +fs_list_bpf_dirs(test_bpf_deny_prog_load_as_t) +allow test_bpf_deny_prog_load_as_t bpf_t:filesystem mount; +allow test_bpf_deny_prog_load_as_t root_t:dir mounton; +allow test_bpf_deny_prog_load_as_t self:bpf { map_create map_create_as map_read map_write prog_load }; +allow test_bpf_deny_prog_load_as_t self:cap2_userns { bpf perfmon }; +allow test_bpf_deny_prog_load_as_t self:cap_userns { net_admin setgid setuid sys_admin }; +allow test_bpf_deny_prog_load_as_t self:user_namespace create;
tests/bpf/Makefile +3 -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,5 @@ 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 +20 -1
@@ -9,8 +9,10 @@ BEGIN { $test_bpf_count = 7; $test_fdreceive_count = 4; + $test_bpf_token_count = 4; - $test_count = $test_bpf_count + $test_fdreceive_count; + $test_count = + $test_bpf_count + $test_fdreceive_count + $test_bpf_token_count; # allow info to be shown during tests $v = $ARGV[0];
@@ -67,6 +69,13 @@ ok( $result eq 0 ); $result = system "runcon -t test_bpf_t $basedir/bpf_test -p $v"; ok( $result eq 0 ); +# 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 ); + # Deny map_create permission $result = system "runcon -t test_bpf_deny_map_create_t $basedir/bpf_test -m $v 2>&1";
@@ -92,6 +101,16 @@ $result = system "runcon -t test_bpf_deny_prog_run_t $basedir/bpf_test -p $v 2>&1"; ok($result); +# 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 +543 -0
543 lines not on this page · raw message
Signature · 1 line
2.50.1.windows.1
Stephen Smalley
maintainerQuoted 5 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]/
No need to re-spin just for this, but this also depends on the corresponding userspace patch for libsepol.
Quoted 42 lines
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 cp base.cil base.cil.orig
sudo sed -i "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.sptWhen 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.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'Signed-off-by Eric Suen <[email protected]>Tested-by Daniel Durning <[email protected]>
Quoted 9 lines
--- policy/test_bpf.te | 39 +++ tests/bpf/Makefile | 5 +- tests/bpf/bpf_common.h | 10 + tests/bpf/bpf_test.c | 59 +++-- tests/bpf/test | 21 +- tests/bpf/token_test.c | 543 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 658 insertions(+), 19 deletions(-) create mode 100644 tests/bpf/token_test.c
If I run the tests manually ala "sudo ./tests/bpf/test", I see the following output during the new tests:
ok 9
unexpected userns_map_create/token_create: actual -13 <= expected 0 (errno 13) test_callback failed. unexpected error: -22 (errno 13) waitpid_child failed. unexpected error: 22 (errno 2)
ok 10
unexpected userns_prog_load/token_create: actual -13 <= expected 0 (errno 13) test_callback failed. unexpected error: -22 (errno 13) waitpid_child failed. unexpected error: 22 (errno 2)
ok 11
Generally we try to avoid/minimize spurious output from the tests for legitimate errors, so ideally you would eliminate this noise if these are in fact operating as intended.
Quoted 71 lines · Stephen Smalley
Quoted 4 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]/No need to re-spin just for this, but this also depends on the corresponding userspace patch for libsepol.
Quoted 50 lines
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 cp base.cil base.cil.orig
sudo sed -i "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.sptWhen 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.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'Signed-off-by Eric Suen <[email protected]>Tested-by Daniel Durning <[email protected]>--- policy/test_bpf.te | 39 +++ tests/bpf/Makefile | 5 +- tests/bpf/bpf_common.h | 10 + tests/bpf/bpf_test.c | 59 +++-- tests/bpf/test | 21 +- tests/bpf/token_test.c | 543 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 658 insertions(+), 19 deletions(-)Quoted 1 line
create mode 100644
tests/bpf/token_test.cIf I run the tests manually ala "sudo ./tests/bpf/test", I see the following output during the new tests:
ok 9
unexpected userns_map_create/token_create: actual -13 <= expected 0 (errno 13)test_callbackfailed. unexpected error: -22 (errno 13)waitpid_childfailed. unexpected error: 22 (errno 2)
ok 10
unexpected userns_prog_load/token_create: actual -13 <= expected 0 (errno 13)test_callbackfailed. unexpected error: -22 (errno 13)waitpid_childfailed. unexpected error: 22 (errno 2)
ok 11
Quoted 3 lines
Generally we try to avoid/minimize spurious output from the tests for legitimate errors, so ideally you would eliminate this noise if these are in fact operating as intended.
Stephen - apologies for the delayed response. Yes, that's a great feedback. Let me look into it and update in next patch.