Remove redundant rules when building policydb
v4 cover Remove redundant rules when building policydb
Changes in v4:
* fix deallocation in error path in build_type_map()
* fix leaked cond nodes
* also update the man pages
* use UINT32_C for 0xFFFFFFFF constant
* squash in the RFC patches & drop the semodule patch
v3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
* fix bad patch squashing
* rename secilc --optimize-policy option to --optimize (to be simpler and consistent with checkpolicy and semodule)
v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
* fix handling of dontaudit (AVTAB_DENY) rules
* switch optimization from opt-out to opt-in everywhere
* add a patch from jwcart2 that adds optimization support to checkpolicy as well
* add two RFC modifications (see log messages for details):
* one improves the optimization to detect also rules covered by the union of two or more other rules (on permission set level)
* the other one drops libsemanage/semodule run-time enabling/ disabling of optimization in favor of a global config option
v1: https://lore.kernel.org/selinux/[email protected]/T/
This series implements an optional optimization step when building a policydb via semodule or secilc, which identifies and removes rules that are redundant -- i.e. they are already covered by a more general rule based on the type attribute hierarchy.
Since the optimization might not always be useful (e.g. when care is taken to not have redundant rules or when the attributes are aggressively expanded) and might even drastically increase policy build times under some cicumstances (for example with the DSSP standard policy [1]), the optimization is applied only when requested explictly.
The optimization routine eliminates:
* all allow/dontaudit/auditallow(/neverallow) rules (including xperm variants) that are covered by another more general rule (or by a union of other rules),
* all conditional versions of the above rules that are covered by a more general rule either in the unconditional table or in the same branch of the same conditional.
The optimization doesn't process other rules, since they currently do not support attributes. There is some room left for more precise optimization of conditional rules, but it would likely bring only little additional benefit.
Travis build passed: https://travis-ci.org/WOnder93/selinux/builds/545184071
Tested:
* live on my Fedora 29 devel machine under normal use (no unusual AVCs observed with the optimized policy loaded)
* using: https://gitlab.com/omos/selinux-misc/blob/master/opt_test.sh
* tests also xperm rules
* doesn't test conditionals
[1] https://github.com/DefenSec/dssp2-standard
James Carter (1):
checkpolicy: add flag to enable policy optimization
Ondrej Mosnacek (3):
libsepol: add a function to optimize kernel policy
libsemanage: optionally optimize policy on rebuild
secilc: add flag to enable policy optimization
checkpolicy/checkpolicy.8 | 3 + checkpolicy/checkpolicy.c | 16 +- libsemanage/man/man5/semanage.conf.5 | 5 + libsemanage/src/conf-parse.y | 15 +- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 + libsemanage/src/semanage_conf.h | 1 + libsepol/include/sepol/policydb.h | 5 + libsepol/include/sepol/policydb/policydb.h | 2 + libsepol/src/libsepol.map.in | 5 + libsepol/src/optimize.c | 378 +++++++++++++++++++++ libsepol/src/policydb_public.c | 5 + secilc/secilc.8.xml | 5 + secilc/secilc.c | 16 +- 14 files changed, 460 insertions(+), 4 deletions(-) create mode 100644 libsepol/src/optimize.c
Signature · 1 line
2.20.1
PATCH v4 1/4 libsepol: add a function to optimize kernel policy
Add sepol_policydb_optimize(), which checks a kernel policy for redundant rules (i.e. those that are covered by an existing more general rule) and removes them.
Results on Fedora 29 policy:
WITHOUT OPTIMIZATION:
# time semodule -B
real 0m21,280s
user 0m18,636s
sys 0m2,525s $ wc -c /sys/fs/selinux/policy
8692158 /sys/fs/selinux/policy $ seinfo (edited)
Allow: 113159
Dontaudit: 10297
Total: 123156WITH OPTIMIZATION ENABLED:
# time semodule -B
real 0m22,825s
user 0m20,178s
sys 0m2,520s $ wc -c /sys/fs/selinux/policy
8096158 /sys/fs/selinux/policy $ seinfo (edited)
Allow: 66334
Dontaudit: 7480
Total: 73814--- libsepol/include/sepol/policydb.h | 5 + libsepol/include/sepol/policydb/policydb.h | 2 + libsepol/src/libsepol.map.in | 5 + libsepol/src/optimize.c | 378 +++++++++++++++++++++ libsepol/src/policydb_public.c | 5 + 5 files changed, 395 insertions(+) create mode 100644 libsepol/src/optimize.c
libsepol/include/sepol/policydb.h +5 -0
@@ -100,6 +100,11 @@ extern int sepol_policydb_set_handle_unknown(sepol_policydb_t * p,extern int sepol_policydb_set_target_platform (sepol_policydb_t * p,int target_platform); +/* +* Optimize the policy by removing redundant rules. +*/ +extern int sepol_policydb_optimize (sepol_policydb_t * p); +/* * Read a policydb from a policy file. * This automatically sets the type and version based on the
libsepol/include/sepol/policydb/policydb.h +2 -0
@@ -636,6 +636,8 @@ extern int policydb_user_cache(hashtab_key_t key,extern int policydb_reindex_users (policydb_t * p); +extern int policydb_optimize (policydb_t * p); +extern void policydb_destroy (policydb_t * p);extern int policydb_load_isids (policydb_t * p,sidtab_t * s);
libsepol/src/libsepol.map.in +5 -0
@@ -59,3 +59,8 @@ LIBSEPOL_1.1 { sepol_polcap_getnum; sepol_polcap_getname; } LIBSEPOL_1.0; + +LIBSEPOL_1.2 { + global: + sepol_optimize_policy; +} LIBSEPOL_1.1;
libsepol/src/optimize.c new file +378 -0
378 lines not on this page · raw message
libsepol/src/policydb_public.c +5 -0
@@ -169,6 +169,11 @@ int sepol_policydb_set_target_platform(sepol_policydb_t * sp,return 0 ; } +int sepol_policydb_optimize (sepol_policydb_t * p) +{ +return policydb_optimize (&p->p); +} +int sepol_policydb_read (sepol_policydb_t * p,sepol_policy_file_t * pf) {return policydb_read (&p->p, &pf->pf,0 );
Signature · 1 line
2.20.1
PATCH v4 2/4 libsemanage: optionally optimize policy on rebuild
In reply to Ondrej MosnacekWhen building binary policy, optionally run it through sepol_policydb_optimize() just before writing it out.
Add an optimize-policy variable to semanage.conf(5) that controls whether optimization will be applied during libsemanage operations.
--- libsemanage/man/man5/semanage.conf.5 | 5 +++++ libsemanage/src/conf-parse.y | 15 ++++++++++++++- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 +++++++ libsemanage/src/semanage_conf.h | 1 + 5 files changed, 28 insertions(+), 1 deletion(-)
libsemanage/man/man5/semanage.conf.5 +5 -0
@@ -121,6 +121,11 @@ and by default it is set to "false". Please note that since this option deletes all HLL files, an updated HLL compiler will not be able to recompile the original HLL file into CIL. In order to compile the original HLL file into CIL, the same HLL file will need to be reinstalled. +.TP +.B optimize-policy +When set to "true", the kernel policy will be optimized upon rebuilds. +It can be set to either "true" or "false" and by default it is set to "false". + .SH "SEE ALSO" .TP semanage(8)
libsemanage/src/conf-parse.y +14 -1
@@ -59,7 +59,7 @@ static int parse_errors; char *s; } -%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT +%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS %token BZIP_BLOCKSIZE BZIP_SMALL REMOVE_HLL %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END
@@ -95,6 +95,7 @@ single_opt: module_store | bzip_blocksize | bzip_small | remove_hll + | optimize_policy ; module_store: MODULE_STORE '=' ARG {
@@ -268,6 +269,17 @@ remove_hll: REMOVE_HLL'=' ARG { free($3); } +optimize_policy: OPTIMIZE_POLICY '=' ARG { + if (strcasecmp($3, "false") == 0) { + current_conf->optimize_policy = 0; + } else if (strcasecmp($3, "true") == 0) { + current_conf->optimize_policy = 1; + } else { + yyerror("optimize-policy can only be 'true' or 'false'"); + } + free($3); +} + command_block: command_start external_opts BLOCK_END { if (new_external->path == NULL) {
@@ -352,6 +364,7 @@ static int semanage_conf_init(semanage_conf_t * conf) conf->bzip_small = 0; conf->ignore_module_cache = 0; conf->remove_hll = 0; + conf->optimize_policy = 0; conf->save_previous = 0; conf->save_linked = 0;
libsemanage/src/conf-scan.l +1 -0
@@ -54,6 +54,7 @@ handle-unknown return HANDLE_UNKNOWN; bzip-blocksize return BZIP_BLOCKSIZE; bzip-small return BZIP_SMALL; remove-hll return REMOVE_HLL; +optimize-policy return OPTIMIZE_POLICY; "[load_policy]" return LOAD_POLICY_START; "[setfiles]" return SETFILES_START; "[sefcontext_compile]" return SEFCONTEXT_COMPILE_START;
libsemanage/src/direct_api.c +7 -0
@@ -1461,6 +1461,13 @@ rebuild:cil_db_destroy (&cildb); +/* Remove redundancies in binary policy if requested. */ +if (sh->conf->optimize_policy) { + retval =sepol_policydb_optimize (out); +if (retval <0 ) + goto cleanup; + } +/* Write the linked policy before merging local changes. */ retval =semanage_write_policydb (sh, out,SEMANAGE_LINKED );
libsemanage/src/semanage_conf.h +1 -0
@@ -47,6 +47,7 @@ typedef struct semanage_conf {int bzip_small;int remove_hll;int ignore_module_cache; +int optimize_policy;char *ignoredirs;/* ";" separated of list for genhomedircon to ignore */ struct external_prog *load_policy;struct external_prog *setfiles;
Signature · 1 line
2.20.1
Re: [Non-DoD Source] [PATCH userspace v4 2/4] libsemanage: optionally optimize policy on rebuild
Quoted 4 lines · Ondrej Mosnacek
When building binary policy, optionally run it through
sepol_policydb_optimize()just before writing it out.Add an optimize-policy variable to
semanage.conf(5)that controls
Sorry I didn't notice this in v3, but why not use "optimize" instead of "optimize-policy"?
Jim
Quoted 117 lines
whether optimization will be applied during libsemanage operations.
Signed-off-by Ondrej Mosnacek <[email protected]>--- libsemanage/man/man5/semanage.conf.5 | 5 +++++ libsemanage/src/conf-parse.y | 15 ++++++++++++++- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 +++++++ libsemanage/src/semanage_conf.h | 1 + 5 files changed, 28 insertions(+), 1 deletion(-)libsemanage/man/man5/semanage.conf.5 +5
-0@@ -121,6 +121,11 @@ and by default it is set to "false". Please note that since this option deletes all HLL files, an updated HLL compiler will not be able to recompile the original HLL file into CIL. In order to compile the original HLL file into CIL, the same HLL file will need to be reinstalled. +.TP +.B optimize-policy +When set to "true", the kernel policy will be optimized upon rebuilds. +It can be set to either "true" or "false" and by default it is set to "false". + .SH "SEE ALSO" .TP semanage(8)libsemanage/src/conf-parse.y +14
-1@@ -59,7 +59,7 @@ static int parse_errors; char *s; } -%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT +%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS %token BZIP_BLOCKSIZE BZIP_SMALL REMOVE_HLL %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END@@ -95,6 +95,7 @@ single_opt: module_store | bzip_blocksize | bzip_small | remove_hll + | optimize_policy ; module_store: MODULE_STORE '=' ARG {@@ -268,6 +269,17 @@ remove_hll: REMOVE_HLL'=' ARG { free($3); } +optimize_policy: OPTIMIZE_POLICY '=' ARG { + if (strcasecmp($3, "false") == 0) { + current_conf->optimize_policy = 0; + } else if (strcasecmp($3, "true") == 0) { + current_conf->optimize_policy = 1; + } else { + yyerror("optimize-policy can only be 'true' or 'false'"); + } + free($3); +} + command_block: command_start external_opts BLOCK_END { if (new_external->path == NULL) {@@ -352,6 +364,7 @@ static int semanage_conf_init(semanage_conf_t * conf) conf->bzip_small = 0; conf->ignore_module_cache = 0; conf->remove_hll = 0; + conf->optimize_policy = 0; conf->save_previous = 0; conf->save_linked = 0;libsemanage/src/conf-scan.l +1
-0@@ -54,6 +54,7 @@ handle-unknown return HANDLE_UNKNOWN; bzip-blocksize return BZIP_BLOCKSIZE; bzip-small return BZIP_SMALL; remove-hll return REMOVE_HLL; +optimize-policy return OPTIMIZE_POLICY; "[load_policy]" return LOAD_POLICY_START; "[setfiles]" return SETFILES_START; "[sefcontext_compile]" return SEFCONTEXT_COMPILE_START;libsemanage/src/direct_api.c +7
-0@@ -1461,6 +1461,13 @@ rebuild:cil_db_destroy (&cildb); +/* Remove redundancies in binary policy if requested. */ +if (sh->conf->optimize_policy) { + retval =sepol_policydb_optimize (out); +if (retval <0 ) + goto cleanup; + } +/* Write the linked policy before merging local changes. */ retval =semanage_write_policydb (sh, out,SEMANAGE_LINKED );libsemanage/src/semanage_conf.h +1
-0@@ -47,6 +47,7 @@ typedef struct semanage_conf {int bzip_small;int remove_hll;int ignore_module_cache; +int optimize_policy;char *ignoredirs;/* ";" separated of list for genhomedircon to ignore */ struct external_prog *load_policy;struct external_prog *setfiles;
Signature · 2 lines
James Carter <[email protected]> National Security Agency
Re: [Non-DoD Source] [PATCH userspace v4 2/4] libsemanage: optionally optimize policy on rebuild
Quoted 8 lines · On Thu, Jun 13, 2019 at 9:51 PM jwcart2 <[email protected]> wrote:
Quoted 4 lines · Ondrej Mosnacek
When building binary policy, optionally run it through
sepol_policydb_optimize()just before writing it out.Add an optimize-policy variable to
semanage.conf(5)that controlsSorry I didn't notice this in v3, but why not use "optimize" instead of "optimize-policy"?
Since this is in a global libsemanage config file, I thought it would be better if the name described also what is being optimized. In secilc or checkpolicy it is more or less obvious; in the config file it can't hurt to be more specific, IMHO.
Quoted 117 lines
Jim
Quoted 114 lines
whether optimization will be applied during libsemanage operations.
Signed-off-by Ondrej Mosnacek <[email protected]>--- libsemanage/man/man5/semanage.conf.5 | 5 +++++ libsemanage/src/conf-parse.y | 15 ++++++++++++++- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 +++++++ libsemanage/src/semanage_conf.h | 1 + 5 files changed, 28 insertions(+), 1 deletion(-)libsemanage/man/man5/semanage.conf.5 +5
-0@@ -121,6 +121,11 @@ and by default it is set to "false". Please note that since this option deletes all HLL files, an updated HLL compiler will not be able to recompile the original HLL file into CIL. In order to compile the original HLL file into CIL, the same HLL file will need to be reinstalled. +.TP +.B optimize-policy +When set to "true", the kernel policy will be optimized upon rebuilds. +It can be set to either "true" or "false" and by default it is set to "false". + .SH "SEE ALSO" .TP semanage(8)libsemanage/src/conf-parse.y +14
-1@@ -59,7 +59,7 @@ static int parse_errors; char *s; } -%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT +%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS %token BZIP_BLOCKSIZE BZIP_SMALL REMOVE_HLL %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END@@ -95,6 +95,7 @@ single_opt: module_store | bzip_blocksize | bzip_small | remove_hll + | optimize_policy ; module_store: MODULE_STORE '=' ARG {@@ -268,6 +269,17 @@ remove_hll: REMOVE_HLL'=' ARG { free($3); } +optimize_policy: OPTIMIZE_POLICY '=' ARG { + if (strcasecmp($3, "false") == 0) { + current_conf->optimize_policy = 0; + } else if (strcasecmp($3, "true") == 0) { + current_conf->optimize_policy = 1; + } else { + yyerror("optimize-policy can only be 'true' or 'false'"); + } + free($3); +} + command_block: command_start external_opts BLOCK_END { if (new_external->path == NULL) {@@ -352,6 +364,7 @@ static int semanage_conf_init(semanage_conf_t * conf) conf->bzip_small = 0; conf->ignore_module_cache = 0; conf->remove_hll = 0; + conf->optimize_policy = 0; conf->save_previous = 0; conf->save_linked = 0;libsemanage/src/conf-scan.l +1
-0@@ -54,6 +54,7 @@ handle-unknown return HANDLE_UNKNOWN; bzip-blocksize return BZIP_BLOCKSIZE; bzip-small return BZIP_SMALL; remove-hll return REMOVE_HLL; +optimize-policy return OPTIMIZE_POLICY; "[load_policy]" return LOAD_POLICY_START; "[setfiles]" return SETFILES_START; "[sefcontext_compile]" return SEFCONTEXT_COMPILE_START;libsemanage/src/direct_api.c +7
-0@@ -1461,6 +1461,13 @@ rebuild:cil_db_destroy (&cildb); +/* Remove redundancies in binary policy if requested. */ +if (sh->conf->optimize_policy) { + retval =sepol_policydb_optimize (out); +if (retval <0 ) + goto cleanup; + } +/* Write the linked policy before merging local changes. */ retval =semanage_write_policydb (sh, out,SEMANAGE_LINKED );libsemanage/src/semanage_conf.h +1
-0@@ -47,6 +47,7 @@ typedef struct semanage_conf {int bzip_small;int remove_hll;int ignore_module_cache; +int optimize_policy;char *ignoredirs;/* ";" separated of list for genhomedircon to ignore */
Quoted 8 lines
libsemanage/src/semanage_conf.h typedef struct semanage_conf { · line 53
Quoted 3 lines
libsemanage/src/semanage_conf.h +0
-0struct external_prog *load_policy;struct external_prog *setfiles;--
James Carter <[email protected]>
National Security Agency
--
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
Re: [Non-DoD Source] [PATCH userspace v4 2/4] libsemanage: optionally optimize policy on rebuild
Quoted 7 lines · Ondrej Mosnacek
Quoted 6 lines · On Thu, Jun 13, 2019 at 9:51 PM jwcart2 <[email protected]> wrote:
Quoted 4 lines · Ondrej Mosnacek
When building binary policy, optionally run it through
sepol_policydb_optimize()just before writing it out.Add an optimize-policy variable to
semanage.conf(5)that controls
Quoted 8 lines
Quoted 2 lines
Sorry I didn't notice this in v3, but why not use "optimize" instead of "optimize-policy"?
Since this is in a global libsemanage config file, I thought it would be better if the name described also what is being optimized. In secilc or checkpolicy it is more or less obvious; in the config file it can't hurt to be more specific, IMHO.
Ok. I just wanted to make sure we had thought about it.
Jim
Quoted 133 lines
Quoted 117 lines
Jim
Quoted 114 lines
whether optimization will be applied during libsemanage operations.
Signed-off-by Ondrej Mosnacek <[email protected]>--- libsemanage/man/man5/semanage.conf.5 | 5 +++++ libsemanage/src/conf-parse.y | 15 ++++++++++++++- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 +++++++ libsemanage/src/semanage_conf.h | 1 + 5 files changed, 28 insertions(+), 1 deletion(-)libsemanage/man/man5/semanage.conf.5 +5
-0@@ -121,6 +121,11 @@ and by default it is set to "false". Please note that since this option deletes all HLL files, an updated HLL compiler will not be able to recompile the original HLL file into CIL. In order to compile the original HLL file into CIL, the same HLL file will need to be reinstalled. +.TP +.B optimize-policy +When set to "true", the kernel policy will be optimized upon rebuilds. +It can be set to either "true" or "false" and by default it is set to "false". + .SH "SEE ALSO" .TP semanage(8)libsemanage/src/conf-parse.y +14
-1@@ -59,7 +59,7 @@ static int parse_errors; char *s; } -%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT +%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS %token BZIP_BLOCKSIZE BZIP_SMALL REMOVE_HLL %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END@@ -95,6 +95,7 @@ single_opt: module_store | bzip_blocksize | bzip_small | remove_hll + | optimize_policy ; module_store: MODULE_STORE '=' ARG {@@ -268,6 +269,17 @@ remove_hll: REMOVE_HLL'=' ARG { free($3); } +optimize_policy: OPTIMIZE_POLICY '=' ARG { + if (strcasecmp($3, "false") == 0) { + current_conf->optimize_policy = 0; + } else if (strcasecmp($3, "true") == 0) { + current_conf->optimize_policy = 1; + } else { + yyerror("optimize-policy can only be 'true' or 'false'"); + } + free($3); +} + command_block: command_start external_opts BLOCK_END { if (new_external->path == NULL) {@@ -352,6 +364,7 @@ static int semanage_conf_init(semanage_conf_t * conf) conf->bzip_small = 0; conf->ignore_module_cache = 0; conf->remove_hll = 0; + conf->optimize_policy = 0; conf->save_previous = 0; conf->save_linked = 0;libsemanage/src/conf-scan.l +1
-0@@ -54,6 +54,7 @@ handle-unknown return HANDLE_UNKNOWN; bzip-blocksize return BZIP_BLOCKSIZE; bzip-small return BZIP_SMALL; remove-hll return REMOVE_HLL; +optimize-policy return OPTIMIZE_POLICY; "[load_policy]" return LOAD_POLICY_START; "[setfiles]" return SETFILES_START; "[sefcontext_compile]" return SEFCONTEXT_COMPILE_START;libsemanage/src/direct_api.c +7
-0@@ -1461,6 +1461,13 @@ rebuild:cil_db_destroy (&cildb); +/* Remove redundancies in binary policy if requested. */ +if (sh->conf->optimize_policy) { + retval =sepol_policydb_optimize (out); +if (retval <0 ) + goto cleanup; + } +/* Write the linked policy before merging local changes. */ retval =semanage_write_policydb (sh, out,SEMANAGE_LINKED );libsemanage/src/semanage_conf.h +1
-0@@ -47,6 +47,7 @@ typedef struct semanage_conf {int bzip_small;int remove_hll;int ignore_module_cache; +int optimize_policy;char *ignoredirs;/* ";" separated of list for genhomedircon to ignore */ Quoted 8 lines
libsemanage/src/semanage_conf.h typedef struct semanage_conf { · line 53
Quoted 3 lines
libsemanage/src/semanage_conf.h +0
-0struct external_prog *load_policy;struct external_prog *setfiles;--
James Carter <[email protected]>
National Security Agency--
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
Signature · 2 lines
James Carter <[email protected]> National Security Agency
PATCH v4 3/4 secilc: add flag to enable policy optimization
In reply to Ondrej MosnacekAdd a command-line option -O/--optimize to optimize the final policydb using sepol_policydb_optimize() before writing it out.
--- secilc/secilc.8.xml | 5 +++++ secilc/secilc.c | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-)
secilc/secilc.8.xml +5 -0
@@ -95,6 +95,11 @@ <listitem><para>Expand type attributes with fewer than <emphasis role="bold"><SIZE></emphasis> members.</para></listitem> </varlistentry> + <varlistentry> + <term><option>-O, --optimize</option></term> + <listitem><para>Optimize final policy (remove redundant rules).</para></listitem> + </varlistentry> + <varlistentry> <term><option>-v, --verbose</option></term> <listitem><para>Increment verbosity level.</para></listitem>
secilc/secilc.c +15 -1
@@ -68,6 +68,7 @@ static __attribute__((__noreturn__)) void usage(const char *prog)printf (" -G, --expand-generated Expand and remove auto-generated attributes\n" );printf (" -X, --expand-size <SIZE> Expand type attributes with fewer than <SIZE>\n" );printf (" members.\n" ); +printf (" -O, --optimize optimize final policy\n" );printf (" -v, --verbose increment verbosity level\n" );printf (" -h, --help display usage information\n" );exit (1 );
@@ -97,6 +98,7 @@ int main(int argc, char *argv[])int policyvers =POLICYDB_VERSION_MAX ;int attrs_expand_generated =0 ;int attrs_expand_size =-1 ; +int optimize =0 ;int opt_char;int opt_index =0 ;char *fc_buf =NULL ;
@@ -117,12 +119,13 @@ int main(int argc, char *argv[]) {"filecontexts", required_argument, 0, 'f'}, {"expand-generated", no_argument, 0, 'G'}, {"expand-size", required_argument, 0, 'X'}, + {"optimize", no_argument, 0, 'O'}, {0, 0, 0, 0} };int i;while (1 ) { - opt_char =getopt_long (argc, argv,"o:f:U:hvt:M:PDmNc:GX:" , long_opts, &opt_index); + opt_char =getopt_long (argc, argv,"o:f:U:hvt:M:PDmNOc:GX:n" , long_opts, &opt_index);if (opt_char ==-1 ) {break ; }
@@ -211,6 +214,9 @@ int main(int argc, char *argv[]) }break ; } +case 'O' : + optimize =1 ; +break ;case 'h' :usage (argv[0 ]);case '?' :
@@ -294,6 +300,14 @@ int main(int argc, char *argv[]) goto exit; } +if (optimize) { + rc =sepol_policydb_optimize (pdb); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to optimize policydb\n" ); + goto exit; + } + } +if (output ==NULL ) {int size =snprintf (NULL ,0 ,"policy.%d" , policyvers); output =malloc ((size +1 ) *sizeof (char ));
Signature · 1 line
2.20.1
PATCH v4 4/4 checkpolicy: add flag to enable policy optimization
In reply to Ondrej MosnacekFrom: James Carter <[email protected]>
Add the command-line option 'O' to checkpolicy to cause kernel policies to be optimized by calling policydb_optimize() before being written out.
This option can be used on conf files and binary kernel policies, but not when converting a conf file to CIL.
[omosnace: directly use policydb_optimize() as also the rest of code already uses
other policydb_*() functions...]
--- checkpolicy/checkpolicy.8 | 3 +++ checkpolicy/checkpolicy.c | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-)
checkpolicy/checkpolicy.8 +3 -0
@@ -48,6 +48,9 @@ Sort ocontexts before writing out the binary policy. This option makes output of .B \-t,\-\-target Specify the target platform (selinux or xen). .TP +.B \-O,\-\-optimize +Optimize the final kernel policy (remove redundant rules). +.TP .B \-V,\-\-version Show version information. .TP
checkpolicy/checkpolicy.c +14 -2
@@ -394,7 +394,7 @@ int main(int argc, char **argv)size_t scontext_len, pathlen;unsigned int i;unsigned int protocol, port; -unsigned int binary =0 , debug =0 , sort =0 , cil =0 , conf =0 ; +unsigned int binary =0 , debug =0 , sort =0 , cil =0 , conf =0 , optimize =0 ;struct val_to_name v;int ret, ch, fd, target =SEPOL_TARGET_SELINUX ;unsigned int nel, uret;
@@ -419,11 +419,12 @@ int main(int argc, char **argv) {"cil", no_argument, NULL, 'C'}, {"conf",no_argument, NULL, 'F'}, {"sort", no_argument, NULL, 'S'}, + {"optimize", no_argument, NULL, 'O'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; -while ((ch =getopt_long (argc, argv,"o:t:dbU:MCFSVc:h" , long_options,NULL )) !=-1 ) { +while ((ch =getopt_long (argc, argv,"o:t:dbU:MCFSVc:Oh" , long_options,NULL )) !=-1 ) {switch (ch) {case 'o' : outfile = optarg;
@@ -466,6 +467,9 @@ int main(int argc, char **argv)case 'S' : sort =1 ;break ; +case 'O' : + optimize =1 ; +break ;case 'M' : mlspol =1 ;break ;
@@ -625,6 +629,14 @@ int main(int argc, char **argv)if (policydb_load_isids (&policydb, &sidtab))exit (1 ); +if (optimize && policydbp->policy_type ==POLICY_KERN ) { + ret =policydb_optimize (policydbp); +if (ret) { +fprintf (stderr,"%s: error optimizing policy\n" , argv[0 ]); +exit (1 ); + } + } +if (outfile) { outfp =fopen (outfile,"w" );if (!outfp) {
Signature · 1 line
2.20.1
Quoted 84 lines · Ondrej Mosnacek
Changes in v4:
* fix deallocation in error path inbuild_type_map()
* fix leaked cond nodes
* also update the man pages
* useUINT32_Cfor0xFFFFFFFFconstant
* squash in the RFC patches & drop the semodule patch
v3: https://lore.kernel.org/selinux/[email protected]/T/Changes in v3:
* fix bad patch squashing
* rename secilc --optimize-policy option to --optimize (to be simpler and consistent with checkpolicy and semodule)
v2: https://lore.kernel.org/selinux/[email protected]/T/Changes in v2:
* fix handling of dontaudit (AVTAB_DENY) rules
* switch optimization from opt-out to opt-in everywhere
* add a patch from jwcart2 that adds optimization support to checkpolicy as well
* add two RFC modifications (see log messages for details):
* one improves the optimization to detect also rules covered by the union of two or more other rules (on permission set level)
* the other one drops libsemanage/semodule run-time enabling/ disabling of optimization in favor of a global config option
v1: https://lore.kernel.org/selinux/[email protected]/T/This series implements an optional optimization step when building a policydb via semodule or secilc, which identifies and removes rules that are redundant -- i.e. they are already covered by a more general rule based on the type attribute hierarchy.
Since the optimization might not always be useful (e.g. when care is taken to not have redundant rules or when the attributes are aggressively expanded) and might even drastically increase policy build times under some cicumstances (for example with the DSSP standard policy [1]), the optimization is applied only when requested explictly.
The optimization routine eliminates:
* all allow/dontaudit/auditallow(/neverallow) rules (including xperm variants) that are covered by another more general rule (or by a union of other rules),
* all conditional versions of the above rules that are covered by a more general rule either in the unconditional table or in the same branch of the same conditional.The optimization doesn't process other rules, since they currently do not support attributes. There is some room left for more precise optimization of conditional rules, but it would likely bring only little additional benefit.
Travis build passed: https://travis-ci.org/WOnder93/selinux/builds/545184071
Tested:
* live on my Fedora 29 devel machine under normal use (no unusual AVCs observed with the optimized policy loaded)
* using: https://gitlab.com/omos/selinux-misc/blob/master/opt_test.sh
* tests also xperm rules
* doesn't test conditionals[1] https://github.com/DefenSec/dssp2-standard
James Carter (1):
checkpolicy: add flag to enable policy optimizationOndrej Mosnacek (3):
libsepol: add a function to optimize kernel policy
libsemanage: optionally optimize policy on rebuild
secilc: add flag to enable policy optimizationcheckpolicy/checkpolicy.8 | 3 + checkpolicy/checkpolicy.c | 16 +- libsemanage/man/man5/semanage.conf.5 | 5 + libsemanage/src/conf-parse.y | 15 +- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 + libsemanage/src/semanage_conf.h | 1 + libsepol/include/sepol/policydb.h | 5 + libsepol/include/sepol/policydb/policydb.h | 2 + libsepol/src/libsepol.map.in | 5 + libsepol/src/optimize.c | 378 +++++++++++++++++++++ libsepol/src/policydb_public.c | 5 + secilc/secilc.8.xml | 5 + secilc/secilc.c | 16 +- 14 files changed, 460 insertions(+), 4 deletions(-)
Quoted 2 lines
create mode 100644
libsepol/src/optimize.c
staged: https://github.com/SELinuxProject/selinux/pull/168
Signature · 2 lines
James Carter <[email protected]> National Security Agency
Quoted 88 lines · On 6/21/19 10:11 AM, jwcart2 wrote:
Quoted 83 lines · Ondrej Mosnacek
Changes in v4:
* fix deallocation in error path inbuild_type_map()
* fix leaked cond nodes
* also update the man pages
* useUINT32_Cfor0xFFFFFFFFconstant
* squash in the RFC patches & drop the semodule patch
v3: https://lore.kernel.org/selinux/[email protected]/T/Changes in v3:
* fix bad patch squashing
* rename secilc --optimize-policy option to --optimize (to be simpler and consistent with checkpolicy and semodule)
v2: https://lore.kernel.org/selinux/[email protected]/T/Changes in v2:
* fix handling of dontaudit (AVTAB_DENY) rules
* switch optimization from opt-out to opt-in everywhere
* add a patch from jwcart2 that adds optimization support to checkpolicy as well
* add two RFC modifications (see log messages for details):
* one improves the optimization to detect also rules covered by the union of two or more other rules (on permission set level)
* the other one drops libsemanage/semodule run-time enabling/ disabling of optimization in favor of a global config option
v1: https://lore.kernel.org/selinux/[email protected]/T/This series implements an optional optimization step when building a policydb via semodule or secilc, which identifies and removes rules that are redundant -- i.e. they are already covered by a more general rule based on the type attribute hierarchy.
Since the optimization might not always be useful (e.g. when care is taken to not have redundant rules or when the attributes are aggressively expanded) and might even drastically increase policy build times under some cicumstances (for example with the DSSP standard policy [1]), the optimization is applied only when requested explictly.
The optimization routine eliminates:
* all allow/dontaudit/auditallow(/neverallow) rules (including xperm variants) that are covered by another more general rule (or by a union of other rules),
* all conditional versions of the above rules that are covered by a more general rule either in the unconditional table or in the same branch of the same conditional.The optimization doesn't process other rules, since they currently do not support attributes. There is some room left for more precise optimization of conditional rules, but it would likely bring only little additional benefit.
Travis build passed: https://travis-ci.org/WOnder93/selinux/builds/545184071
Tested:
* live on my Fedora 29 devel machine under normal use (no unusual AVCs observed with the optimized policy loaded)
* using: https://gitlab.com/omos/selinux-misc/blob/master/opt_test.sh
* tests also xperm rules
* doesn't test conditionals[1] https://github.com/DefenSec/dssp2-standard
James Carter (1):
checkpolicy: add flag to enable policy optimizationOndrej Mosnacek (3):
libsepol: add a function to optimize kernel policy
libsemanage: optionally optimize policy on rebuild
secilc: add flag to enable policy optimizationcheckpolicy/checkpolicy.8 | 3 + checkpolicy/checkpolicy.c | 16 +- libsemanage/man/man5/semanage.conf.5 | 5 + libsemanage/src/conf-parse.y | 15 +- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 7 + libsemanage/src/semanage_conf.h | 1 + libsepol/include/sepol/policydb.h | 5 +
libsepol/include/sepol/policydb/policydb.h| 2 + libsepol/src/libsepol.map.in | 5 + libsepol/src/optimize.c | 378 +++++++++++++++++++++ libsepol/src/policydb_public.c | 5 + secilc/secilc.8.xml | 5 + secilc/secilc.c | 16 +-Quoted 3 lines
14 files changed, 460 insertions(+), 4 deletions(-)create mode 100644
libsepol/src/optimize.c
Quoted 4 lines
Acked-by James Carter <[email protected]>
Merged.
Signature · 2 lines
James Carter <[email protected]> National Security Agency