libsepol: do not write empty class definitions
PATCH 1/2 libsepol: do not write empty class definitions
Do not write class definitions for classes without any permission and any inherited common class. The classes are already declared in write_class_decl_rules_to_conf(). Skipping those empty definitions, which are equal to the corresponding class declarations, will enable to parse the generated policy conf file with checkpolicy, as checkpolicy does not accept class declarations after initial sid declarations.
This will enable simple round-trip tests with checkpolicy.
--- libsepol/src/kernel_to_conf.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
libsepol/src/kernel_to_conf.c +13 -8
@@ -591,16 +591,21 @@ static int write_class_and_common_rules_to_conf(FILE *out, struct policydb *pdb) class = pdb->class_val_to_struct[i];if (!class)continue ; name = pdb->p_class_val_to_name[i]; -sepol_printf (out,"class %s" , name); -if (class->comkey) { -sepol_printf (out," inherits %s" , class->comkey); - } perms =class_or_common_perms_to_str (&class->permissions); -if (perms) { -sepol_printf (out," { %s }" , perms); -free (perms); +/* Do not write empty classes, their declaration was alreedy +* printed in write_class_decl_rules_to_conf() */ +if (perms || class->comkey) { +sepol_printf (out,"class %s" , name); +if (class->comkey) { +sepol_printf (out," inherits %s" , class->comkey); + } + +if (perms) { +sepol_printf (out," { %s }" , perms); +free (perms); + } +sepol_printf (out,"\n" ); } -sepol_printf (out,"\n" ); } exit:
Signature · 1 line
2.39.0
PATCH 2/2 checkpolicy: add simple round-trip test
Add simple round-trip tests on a minimal standard and MLS policy.
--- checkpolicy/.gitignore | 2 + checkpolicy/Makefile | 6 +- checkpolicy/tests/polmin.conf | 81 +++++++++++++++++++++++++++ checkpolicy/tests/polmin.mls.conf | 85 +++++++++++++++++++++++++++++ checkpolicy/tests/test_roundtrip.sh | 33 +++++++++++ 5 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 checkpolicy/tests/polmin.conf create mode 100644 checkpolicy/tests/polmin.mls.conf create mode 100755 checkpolicy/tests/test_roundtrip.sh
checkpolicy/.gitignore +2 -0
@@ -3,3 +3,5 @@ checkpolicy lex.yy.c y.tab.c y.tab.h +tests/testpol.bin +tests/testpol.conf
checkpolicy/Makefile +5 -1
@@ -50,6 +50,10 @@ y.tab.c: policy_parse.y lex.yy.c: policy_scan.l y.tab.c $(LEX) policy_scan.l +.PHONY: test +test: checkpolicy + ./tests/test_roundtrip.sh + install: all -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8
@@ -68,7 +72,7 @@ relabel: install /sbin/restorecon $(DESTDIR)$(BINDIR)/checkmodule clean: - -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c + -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin $(MAKE) -C test clean indent:
checkpolicy/tests/polmin.conf new file +81 -0
81 lines not on this page · raw message
checkpolicy/tests/polmin.mls.conf new file +85 -0
85 lines not on this page · raw message
checkpolicy/tests/test_roundtrip.sh new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/sh + +set -eu + +BASEDIR=$(dirname "$0") +CHECKPOLICY="${BASEDIR}/../checkpolicy" + +check_policy() { + POLICY=$1 + MLS=$2 + + if [ "$MLS" = 'mls' ]; then + OPT='-M' + else + OPT= + fi + + echo "==== Testing ${1}" + + ${CHECKPOLICY} ${OPT} -E "${BASEDIR}/${POLICY}" -o "${BASEDIR}/testpol.bin" + ${CHECKPOLICY} ${OPT} -E -b -F "${BASEDIR}/testpol.bin" -o "${BASEDIR}/testpol.conf" + diff -u "${BASEDIR}/${POLICY}" "${BASEDIR}/testpol.conf" + + ${CHECKPOLICY} ${OPT} -S -O -E "${BASEDIR}/${POLICY}" -o "${BASEDIR}/testpol.bin" + ${CHECKPOLICY} ${OPT} -S -O -E -b -F "${BASEDIR}/testpol.bin" -o "${BASEDIR}/testpol.conf" + diff -u "${BASEDIR}/${POLICY}" "${BASEDIR}/testpol.conf" + + echo "==== ${1} success" +} + + +check_policy polmin.conf std +check_policy polmin.mls.conf mls
Signature · 1 line
2.39.0
Re: [PATCH 2/2] checkpolicy: add simple round-trip test
Quoted 55 lines · Christian Göttsche
Add simple round-trip tests on a minimal standard and MLS policy.
Signed-off-by Christian Göttsche <[email protected]>--- checkpolicy/.gitignore | 2 + checkpolicy/Makefile | 6 +- checkpolicy/tests/polmin.conf | 81 +++++++++++++++++++++++++++ checkpolicy/tests/polmin.mls.conf | 85 +++++++++++++++++++++++++++++ checkpolicy/tests/test_roundtrip.sh | 33 +++++++++++ 5 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 checkpolicy/tests/polmin.conf create mode 100644 checkpolicy/tests/polmin.mls.conf create mode 100755 checkpolicy/tests/test_roundtrip.shcheckpolicy/.gitignore +2
-0@@ -3,3 +3,5 @@ checkpolicy lex.yy.c y.tab.c y.tab.h +tests/testpol.bin +tests/testpol.confcheckpolicy/Makefile +5
-1@@ -50,6 +50,10 @@ y.tab.c: policy_parse.y lex.yy.c: policy_scan.l y.tab.c $(LEX) policy_scan.l +.PHONY: test +test: checkpolicy + ./tests/test_roundtrip.sh + install: all -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8@@ -68,7 +72,7 @@ relabel: install /sbin/restorecon $(DESTDIR)$(BINDIR)/checkmodule clean: - -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c + -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin $(MAKE) -C test clean indent:checkpolicy/tests/polmin.conf new file +0
-0
checkpolicy/tests/polmin.conf line 9
Quoted 9 lines
checkpolicy/tests/polmin.conf new file +9
-0@@ -0,0 +1,81 @@ +# handle_unknown deny +class process +class blk_file +class chr_file +class dir +class fifo_file +class file +class lnk_file +class sock_file
I am not sure why you are defining so many classes that are not being used.
Quoted 17 lines
checkpolicy/tests/polmin.conf new file +17
-0+sid kernel +sid security +sid unlabeled +sid fs +sid file +sid file_labels +sid init +sid any_socket +sid port +sid netif +sid netmsg +sid node +sid igmp_packet +sid icmp_socket +sid tcp_socket +sid sysctl_modprobe +sid sysctl
checkpolicy/tests/polmin.conf line 36
Quoted 10 lines
checkpolicy/tests/polmin.conf new file +10
-0+sid sysctl_fs +sid sysctl_kernel +sid sysctl_net +sid sysctl_net_unix +sid sysctl_vm +sid sysctl_dev +sid kmod +sid policy +sid scmp_packet +sid devnull
The policy is not being loaded into the kernel, so you don't need to have all of the sid rules.
This is the absolute minimum policy (I think):
# handle_unknown deny
class CLASS1
sid kernel
class CLASS1 { PERM1 }
type TYPE1;
allow TYPE1 self:CLASS1 { PERM1 };
role ROLE1;
role ROLE1 types { TYPE1 };
user USER1 roles ROLE1;
sid kernel USER1:ROLE1:TYPE1There would also be merit in having a very minimum policy that uses every rule.
Quoted 33 lines
checkpolicy/tests/polmin.conf new file +33
-0+class process { dyntransition transition } +default_role { blk_file } source; +default_role { chr_file } source; +default_role { dir } source; +default_role { fifo_file } source; +default_role { file } source; +default_role { lnk_file } source; +default_role { sock_file } source; +type sys_isid; +typealias sys_isid alias dpkg_script_t; +typealias sys_isid alias rpm_script_t; +allow sys_isid self:process { dyntransition transition }; +role sys_role; +role sys_role types { sys_isid }; +user sys_user roles sys_role; +constrain process { transition } u1 == u2; +sid kernel sys_user:sys_role:sys_isid +sid security sys_user:sys_role:sys_isid +sid unlabeled sys_user:sys_role:sys_isid +sid fs sys_user:sys_role:sys_isid +sid file sys_user:sys_role:sys_isid +sid file_labels sys_user:sys_role:sys_isid +sid init sys_user:sys_role:sys_isid +sid any_socket sys_user:sys_role:sys_isid +sid port sys_user:sys_role:sys_isid +sid netif sys_user:sys_role:sys_isid +sid netmsg sys_user:sys_role:sys_isid +sid node sys_user:sys_role:sys_isid +sid igmp_packet sys_user:sys_role:sys_isid +sid icmp_socket sys_user:sys_role:sys_isid +sid tcp_socket sys_user:sys_role:sys_isid +sid sysctl_modprobe sys_user:sys_role:sys_isid +sid sysctl sys_user:sys_role:sys_isid
checkpolicy/tests/polmin.conf line 79
Quoted 10 lines
checkpolicy/tests/polmin.conf new file +10
-0+sid sysctl_fs sys_user:sys_role:sys_isid +sid sysctl_kernel sys_user:sys_role:sys_isid +sid sysctl_net sys_user:sys_role:sys_isid +sid sysctl_net_unix sys_user:sys_role:sys_isid +sid sysctl_vm sys_user:sys_role:sys_isid +sid sysctl_dev sys_user:sys_role:sys_isid +sid kmod sys_user:sys_role:sys_isid +sid policy sys_user:sys_role:sys_isid +sid scmp_packet sys_user:sys_role:sys_isid +sid devnull sys_user:sys_role:sys_isid
Even if you are loading the policy into the kernel you only need to assign contexts to the sids that are going to be used (kernel, file, unlabeled, any_socket).
Eventually, we want dynamic loading of sids, so I would prefer to minimize their usage.
Thanks,
Jim
Quoted 135 lines
checkpolicy/tests/polmin.conf new file +2
-0+fs_use_trans devpts sys_user:sys_role:sys_isid; +fs_use_trans devtmpfs sys_user:sys_role:sys_isid;checkpolicy/tests/polmin.mls.conf new file +85
-0@@ -0,0 +1,85 @@ +# handle_unknown deny +class process +class blk_file +class chr_file +class dir +class fifo_file +class file +class lnk_file +class sock_file +sid kernel +sid security +sid unlabeled +sid fs +sid file +sid file_labels +sid init +sid any_socket +sid port +sid netif +sid netmsg +sid node +sid igmp_packet +sid icmp_socket +sid tcp_socket +sid sysctl_modprobe +sid sysctl +sid sysctl_fs +sid sysctl_kernel +sid sysctl_net +sid sysctl_net_unix +sid sysctl_vm +sid sysctl_dev +sid kmod +sid policy +sid scmp_packet +sid devnull +class process { dyntransition transition } +default_role { blk_file } source; +default_role { chr_file } source; +default_role { dir } source; +default_role { fifo_file } source; +default_role { file } source; +default_role { lnk_file } source; +default_role { sock_file } source; +sensitivity s0; +dominance { s0 } +category c0; +level s0:c0; +mlsconstrain process { transition } l1 == l2; +type sys_isid; +typealias sys_isid alias dpkg_script_t; +typealias sys_isid alias rpm_script_t; +allow sys_isid self:process { dyntransition transition }; +role sys_role; +role sys_role types { sys_isid }; +user sys_user roles sys_role level s0 range s0 - s0:c0; +sid kernel sys_user:sys_role:sys_isid:s0 - s0 +sid security sys_user:sys_role:sys_isid:s0 - s0 +sid unlabeled sys_user:sys_role:sys_isid:s0 - s0 +sid fs sys_user:sys_role:sys_isid:s0 - s0 +sid file sys_user:sys_role:sys_isid:s0 - s0 +sid file_labels sys_user:sys_role:sys_isid:s0 - s0 +sid init sys_user:sys_role:sys_isid:s0 - s0 +sid any_socket sys_user:sys_role:sys_isid:s0 - s0 +sid port sys_user:sys_role:sys_isid:s0 - s0 +sid netif sys_user:sys_role:sys_isid:s0 - s0 +sid netmsg sys_user:sys_role:sys_isid:s0 - s0 +sid node sys_user:sys_role:sys_isid:s0 - s0 +sid igmp_packet sys_user:sys_role:sys_isid:s0 - s0 +sid icmp_socket sys_user:sys_role:sys_isid:s0 - s0 +sid tcp_socket sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_modprobe sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_fs sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_kernel sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_net sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_net_unix sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_vm sys_user:sys_role:sys_isid:s0 - s0 +sid sysctl_dev sys_user:sys_role:sys_isid:s0 - s0 +sid kmod sys_user:sys_role:sys_isid:s0 - s0 +sid policy sys_user:sys_role:sys_isid:s0 - s0 +sid scmp_packet sys_user:sys_role:sys_isid:s0 - s0 +sid devnull sys_user:sys_role:sys_isid:s0 - s0 +fs_use_trans devpts sys_user:sys_role:sys_isid:s0 - s0; +fs_use_trans devtmpfs sys_user:sys_role:sys_isid:s0 - s0;checkpolicy/tests/test_roundtrip.sh new file +33
-1@@ -0,0 +1,33 @@ +#!/bin/sh + +set -eu + +BASEDIR=$(dirname "$0") +CHECKPOLICY="${BASEDIR}/../checkpolicy" + +check_policy() { + POLICY=$1 + MLS=$2 + + if [ "$MLS" = 'mls' ]; then + OPT='-M' + else + OPT= + fi + + echo "==== Testing ${1}" + + ${CHECKPOLICY} ${OPT} -E "${BASEDIR}/${POLICY}" -o "${BASEDIR}/testpol.bin" + ${CHECKPOLICY} ${OPT} -E -b -F "${BASEDIR}/testpol.bin" -o "${BASEDIR}/testpol.conf" + diff -u "${BASEDIR}/${POLICY}" "${BASEDIR}/testpol.conf" + + ${CHECKPOLICY} ${OPT} -S -O -E "${BASEDIR}/${POLICY}" -o "${BASEDIR}/testpol.bin" + ${CHECKPOLICY} ${OPT} -S -O -E -b -F "${BASEDIR}/testpol.bin" -o "${BASEDIR}/testpol.conf" + diff -u "${BASEDIR}/${POLICY}" "${BASEDIR}/testpol.conf" + + echo "==== ${1} success" +} + + +check_policy polmin.conf std +check_policy polmin.mls.conf mls --2.39.0
Quoted 11 lines · Christian Göttsche
Do not write class definitions for classes without any permission and any inherited common class. The classes are already declared in
write_class_decl_rules_to_conf(). Skipping those empty definitions, which are equal to the corresponding class declarations, will enable to parse the generated policy conf file with checkpolicy, as checkpolicy does not accept class declarations after initial sid declarations.This will enable simple round-trip tests with checkpolicy.
Signed-off-by Christian Göttsche <[email protected]>
Quoted 41 lines
--- libsepol/src/kernel_to_conf.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)libsepol/src/kernel_to_conf.c +13
-9@@ -591,16 +591,21 @@ static int write_class_and_common_rules_to_conf(FILE *out, struct policydb *pdb) class = pdb->class_val_to_struct[i];if (!class)continue ; name = pdb->p_class_val_to_name[i]; -sepol_printf (out,"class %s" , name); -if (class->comkey) { -sepol_printf (out," inherits %s" , class->comkey); - } perms =class_or_common_perms_to_str (&class->permissions); -if (perms) { -sepol_printf (out," { %s }" , perms); -free (perms); +/* Do not write empty classes, their declaration was alreedy +* printed in write_class_decl_rules_to_conf() */ +if (perms || class->comkey) { +sepol_printf (out,"class %s" , name); +if (class->comkey) { +sepol_printf (out," inherits %s" , class->comkey); + } + +if (perms) { +sepol_printf (out," { %s }" , perms); +free (perms); + } +sepol_printf (out,"\n" ); } -sepol_printf (out,"\n" ); } exit: --2.39.0
Quoted 11 lines · James Carter
Quoted 8 lines · Christian Göttsche
Do not write class definitions for classes without any permission and any inherited common class. The classes are already declared in
write_class_decl_rules_to_conf(). Skipping those empty definitions, which are equal to the corresponding class declarations, will enable to parse the generated policy conf file with checkpolicy, as checkpolicy does not accept class declarations after initial sid declarations.
Quoted 6 lines
Quoted 3 lines
This will enable simple round-trip tests with checkpolicy.
Signed-off-by Christian Göttsche <[email protected]>Acked-by James Carter <[email protected]>
Merged.
Thanks,
Jim
Quoted 41 lines
Quoted 41 lines
--- libsepol/src/kernel_to_conf.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)libsepol/src/kernel_to_conf.c +13
-9@@ -591,16 +591,21 @@ static int write_class_and_common_rules_to_conf(FILE *out, struct policydb *pdb) class = pdb->class_val_to_struct[i];if (!class)continue ; name = pdb->p_class_val_to_name[i]; -sepol_printf (out,"class %s" , name); -if (class->comkey) { -sepol_printf (out," inherits %s" , class->comkey); - } perms =class_or_common_perms_to_str (&class->permissions); -if (perms) { -sepol_printf (out," { %s }" , perms); -free (perms); +/* Do not write empty classes, their declaration was alreedy +* printed in write_class_decl_rules_to_conf() */ +if (perms || class->comkey) { +sepol_printf (out,"class %s" , name); +if (class->comkey) { +sepol_printf (out," inherits %s" , class->comkey); + } + +if (perms) { +sepol_printf (out," { %s }" , perms); +free (perms); + } +sepol_printf (out,"\n" ); } -sepol_printf (out,"\n" ); } exit: --2.39.0