libsemanage: optionally sort file_contexts.local by specificity
Stephen Smalley
maintainerPATCH 1/1 libsemanage: optionally sort file_contexts.local by specificity
The policy-supplied file_contexts is emitted by the CIL compiler in least-to-most-specific order (cil_post_filecon_compare()) so that the libselinux label_file backend, which returns last matching spec, picks the most specific match. file_contexts.local is written by libsemanage in the order the entriers were added with semanage fcontext -a, so a broad entry added after a narrow one overrides it, and callers cannot rely on "most specific wins" for local customizations.
Add a semanage.conf sort-local-fcontexts boolean. When set to true, the copy of file_contexts.local installed to the policy directory is run through semanage_fc_sort(), which uses the same heuristics as cil_post_filecon_compare(), with equally-specific entries retaining their insertion order. The module store copy is left in insertion order, so semanage fcontext -l -C still shows entries as they were added.
--- libsemanage/man/man5/semanage.conf.5 | 13 ++++++ libsemanage/src/conf-parse.y | 15 +++++- libsemanage/src/conf-scan.l | 1 + libsemanage/src/direct_api.c | 16 +++++-- libsemanage/src/semanage_conf.h | 1 + libsemanage/src/semanage_store.c | 70 ++++++++++++++++++++++++++++ libsemanage/src/semanage_store.h | 2 + 7 files changed, 113 insertions(+), 5 deletions(-)
libsemanage/man/man5/semanage.conf.5 +13 -0
@@ -131,6 +131,19 @@ It can be set to either "true" or "false" and by default it is set to "true". When set to "true", duplicate type, type attribute, and role declarations will be allowed. It can be set to either "true" or "false" and by default it is set to "true". +.TP +.B sort-local-fcontexts +When set to "true", the installed +.I file_contexts.local +file is sorted from least to most specific using the same heuristic as the policy-supplied +.IR file_contexts , +so the most specific local entry wins regardless of the order the entries were added with +.BR "semanage fcontext \-a" . +The module store copy (and thus +.BR "semanage fcontext \-l \-C" ) +is unaffected. +It can be set to either "true" or "false" and by default it is set to "false", preserving insertion-order matching. + .RE .PP For certain tasks the SELinux Management library resorts to running
libsemanage/src/conf-parse.y +14 -1
@@ -62,7 +62,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 OPTIMIZE_POLICY MULTIPLE_DECLS +%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY MULTIPLE_DECLS SORT_LOCAL_FCONTEXTS %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS %token BZIP_BLOCKSIZE BZIP_SMALL RELABEL_STORE REMOVE_HLL %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END
@@ -101,6 +101,7 @@ single_opt: module_store | relabel_store | optimize_policy | multiple_decls + | sort_local_fcontexts ; module_store: MODULE_STORE '=' ARG {
@@ -329,6 +330,17 @@ multiple_decls: MULTIPLE_DECLS '=' ARG { free($3); } +sort_local_fcontexts: SORT_LOCAL_FCONTEXTS '=' ARG { + if (strcasecmp($3, "false") == 0) { + current_conf->sort_local_fcontexts = false; + } else if (strcasecmp($3, "true") == 0) { + current_conf->sort_local_fcontexts = true; + } else { + yyerror("sort-local-fcontexts can only be 'true' or 'false'"); + } + free($3); +} + command_block: command_start external_opts BLOCK_END { if (new_external->path == NULL) {
@@ -425,6 +437,7 @@ static int semanage_conf_init(semanage_conf_t * conf) conf->relabel_store = true; conf->optimize_policy = true; conf->multiple_decls = true; + conf->sort_local_fcontexts = false; conf->save_previous = false; conf->save_linked = false;
libsemanage/src/conf-scan.l +1 -0
@@ -57,6 +57,7 @@ remove-hll return REMOVE_HLL; relabel_store return RELABEL_STORE; optimize-policy return OPTIMIZE_POLICY; multiple-decls return MULTIPLE_DECLS; +sort-local-fcontexts return SORT_LOCAL_FCONTEXTS; "[load_policy]" return LOAD_POLICY_START; "[setfiles]" return SETFILES_START; "[sefcontext_compile]" return SEFCONTEXT_COMPILE_START;
libsemanage/src/direct_api.c +12 -4
@@ -1675,10 +1675,18 @@ static int semanage_direct_commit(semanage_handle_t *sh) goto cleanup; } - retval =copy_file_if_exists ( - sh,semanage_path (SEMANAGE_TMP ,SEMANAGE_STORE_FC_LOCAL ), -semanage_final_path (SEMANAGE_FINAL_TMP ,SEMANAGE_FC_LOCAL ), - sh->conf->file_mode); +if (sh->conf->sort_local_fcontexts) { + retval =semanage_sort_fc_file (sh, +semanage_path (SEMANAGE_TMP ,SEMANAGE_STORE_FC_LOCAL ), +semanage_final_path (SEMANAGE_FINAL_TMP , +SEMANAGE_FC_LOCAL ), + sh->conf->file_mode); + }else { + retval =copy_file_if_exists ( + sh,semanage_path (SEMANAGE_TMP ,SEMANAGE_STORE_FC_LOCAL ), +semanage_final_path (SEMANAGE_FINAL_TMP ,SEMANAGE_FC_LOCAL ), + sh->conf->file_mode); + }if (retval <0 ) { goto cleanup; }
libsemanage/src/semanage_conf.h +1 -0
@@ -51,6 +51,7 @@ typedef struct semanage_conf {bool optimize_policy;bool multiple_decls;bool relabel_store; +bool sort_local_fcontexts;char *ignoredirs;/* ";" separated of list for genhomedircon to ignore */ struct external_prog *load_policy;struct external_prog *setfiles;
libsemanage/src/semanage_store.c +70 -0
76 lines not on this page · raw message
libsemanage/src/semanage_store.h +2 -0
@@ -156,6 +156,8 @@ int semanage_split_fc(semanage_handle_t *sh);/* sort file context routines */ int semanage_fc_sort (semanage_handle_t *sh,const char *buf,size_t buf_len,char **sorted_buf,size_t *sorted_buf_len); +int semanage_sort_fc_file (semanage_handle_t *sh,const char *src, +const char *dst,mode_t mode);/* sort netfilter context routines */ int semanage_nc_sort(semanage_handle_t *sh, const char *buf, size_t buf_len,
Signature · 1 line
2.55.0