libsepol/cil: Add function to check CIL neverallows against binary policy
PATCH v4 1/3 libsepol/cil: Add function to check CIL neverallows against binary policy
Add the function cil_check_neverallows_against_pdb() that will extract the neverallows from a CIL db and check them against a kernel policydb.
Android has the requirement of validating policies from partners against the standard AOSP assertions. While this is done at build time, the requirement is also enforced during certification via the Compatibility Test Suite.
Currently, this validation is done by manually parsing the neverallow assertions which is brittle and does not support neverallowx assertions.
This function will allow a CIL db with the neverallow and neverallowxperm rules to be used to check a kernel policydb for violations as part of the Compatibility Test Suite.
---
v2: Add more context to commit msg
Add error messages for when type or class-permission look-ups fail Set violation to CIL_FALSE initially to ensure it gets set Add more description in cil_binary.h for the function
libsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 48 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 15 +++++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 69 insertions(+)
libsepol/cil/include/cil/cil.h +1 -0
@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,
libsepol/cil/src/cil_binary.c +48 -0
54 lines not on this page · raw message
libsepol/cil/src/cil_binary.h +15 -0
@@ -36,6 +36,21 @@#include "cil_tree.h" #include "cil_list.h" + +/** +* Check neverallows from a CIL database against a policy database. +* +* The types, classes, and permissions used in the CIL neverallow rules must +* be declared in the policy database. +* +* @param[in] db The cil database containing the neverallows to be used. +* @param[in] pdb The policy database to be checked for violations. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. *
libsepol/src/libsepol.map.in +5 -0
@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6;
Signature · 1 line
2.54.0
PATCH v4 2/3 libsepol: Add a function to output CIL declarations from a binary policy
Add the function sepol_kernel_policydb_decls_to_cil() that takes a binary policy as input and outputs the declarations in CIL format.
Also move the checks for a supported policy to a new common function and use that function in kernel_to_conf and kernel_to_cil.
In the near-term this will be used get the declarations from a policydb so they can be used along with neverallows written in CIL to check for neverallow and neverallowx violations in the binary policy.
---
v4: Don't delete the Python swig exception stuff like an idiot
v3: Split out less common functionality to keep the same order for the rules
to prevent breaking existing tests (even though CIL is not dependent on order.)
v2: Added more explanation to the commit msg
Split out common functionality
libsepol/include/sepol/kernel_to_cil.h | 3 +- libsepol/src/kernel_to_cil.c | 97 +++++++++++++++++++------- libsepol/src/kernel_to_common.c | 26 +++++++ libsepol/src/kernel_to_common.h | 2 + libsepol/src/kernel_to_conf.c | 29 ++------ libsepol/src/libsepol.map.in | 1 + 6 files changed, 109 insertions(+), 49 deletions(-)
libsepol/include/sepol/kernel_to_cil.h +2 -1
@@ -2,4 +2,5 @@#include <sepol/policydb/policydb.h> -int sepol_kernel_policydb_to_cil (FILE *fp,struct policydb *pdb); +int sepol_kernel_policydb_to_cil (FILE *out,struct policydb *pdb); +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb);
libsepol/src/kernel_to_cil.c +73 -24
112 lines not on this page · raw message
libsepol/src/kernel_to_common.c +26 -0
@@ -751,3 +751,29 @@ exit:return rc; } + +int check_for_supported_policy (struct policydb *pdb) +{ +if (pdb ==NULL ) { +ERR (NULL ,"No policy" ); +return -1 ; + } + +if (pdb->policy_type !=SEPOL_POLICY_KERN ) { +ERR (NULL ,"Policy is not a kernel policy" ); +return -1 ; + } + +if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { +/* +* For policy versions between 20 and 23, attributes exist in the policy, +* but only in the type_attr_map. This means that there are gaps in both +* the type_val_to_struct and p_type_val_to_name arrays and policy rules +* can refer to those gaps. +*/ +ERR (NULL ,"Writing out CIL or policy.conf from policy versions between 20 and 23 is not supported" ); +return -1 ; + } + +return 0 ; +}
libsepol/src/kernel_to_common.h +2 -0
@@ -117,3 +117,5 @@ int strs_stack_empty(const struct strs *stack);struct strs *isids_to_strs (const char *const *sid_to_str,unsigned num_sids,struct ocontext *isids);int sort_ocontexts (struct policydb *pdb); + +int check_for_supported_policy (struct policydb *pdb);
libsepol/src/kernel_to_conf.c +5 -24
@@ -3116,6 +3116,11 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb)struct strs *non_mls_validatetrans =NULL ;int rc =0 ; + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + rc =strs_init (&mls_constraints,32 );if (rc !=0 ) { goto exit;
@@ -3136,30 +3141,6 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb) goto exit; } -if (pdb ==NULL ) { -ERR (NULL ,"No policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policy_type !=SEPOL_POLICY_KERN ) { -ERR (NULL ,"Policy is not a kernel policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { -/* -* For policy versions between 20 and 23, attributes exist in the policy, -* but only in the type_attr_map. This means that there are gaps in both -* the type_val_to_struct and p_type_val_to_name arrays and policy rules -* can refer to those gaps. -*/ -ERR (NULL ,"Writing policy versions between 20 and 23 as a policy.conf is not supported" ); - rc =-1 ; - goto exit; - } - rc =constraint_rules_to_strs (pdb, mls_constraints, non_mls_constraints);if (rc !=0 ) { goto exit;
libsepol/src/libsepol.map.in +1 -0
@@ -297,5 +297,6 @@ LIBSEPOL_3.6 { LIBSEPOL_3.11 { global: + sepol_kernel_policydb_decls_to_cil; cil_check_neverallows_against_pdb; } LIBSEPOL_3.6;
Signature · 1 line
2.54.0
PATCH v4 3/3 secilc: Add program that checks CIL neverallows against a binary policy
In reply to James CarterAdd the program secilcheck that takes a binary policy and one or more CIL files and checks for violations in the binary policy using the neverallow and neverallowx rules from the CIL policy.
ex/
secilcheck policy.bin neverallows.cil
The CIL neverallow and neverallowx rules must only use types, classes, and permissions that are declared in the binary policy.
This is planned on being used in Android to validate policies from partners against the standard AOSP assertions as part of the Compatibility Test Suite.
---
v2: Added more explanation to the commit msg
Added the header unistd.h
secilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 302 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.c
secilc/.gitignore +2 -0
@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/html
secilc/Makefile +18 -2
@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff
@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml
@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs
@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs clean
secilc/secilcheck.8.xml new file +81 -0
81 lines not on this page · raw message
secilc/secilcheck.c new file +302 -0
302 lines not on this page · raw message
Signature · 1 line
2.54.0
Stephen Smalley
maintainerQuoted 12 lines · James Carter
Add the function
cil_check_neverallows_against_pdb()that will extract the neverallows from a CIL db and check them against a kernel policydb.Android has the requirement of validating policies from partners against the standard AOSP assertions. While this is done at build time, the requirement is also enforced during certification via the Compatibility Test Suite.
Currently, this validation is done by manually parsing the neverallow assertions which is brittle and does not support neverallowx assertions.
Quoted 6 lines
This function will allow a CIL db with the neverallow and neverallowxperm rules to be used to check a kernel policydb for violations as part of the Compatibility Test Suite.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Acked-by; Stephen Smalley <[email protected]>
Quoted 125 lines
---
v2: Add more context to commit msg
Add error messages for when type or class-permission look-ups fail Set violation toCIL_FALSEinitially to ensure it gets set Add more description incil_binary.hfor the functionlibsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 48 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 15 +++++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 69 insertions(+)libsepol/cil/include/cil/cil.h +1
-0@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,libsepol/cil/src/cil_binary.c +48
-0@@ -5143,6 +5143,54 @@ exit:return rc; } +static int __cil_collect_neverallows_helper (struct cil_tree_node *node,uint32_t *finished,void *extra_args) +{ +if (node->flavor ==CIL_BLOCK ) { +struct cil_block *blk = node->data; +if (blk->is_abstract ==CIL_TRUE ) { + *finished =CIL_TREE_SKIP_HEAD ; + } + }else if (node->flavor ==CIL_MACRO ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_BOOLEANIF ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_AVRULE || node->flavor ==CIL_AVRULEX ) { +struct cil_avrule *rule = node->data; +if (rule->rule_kind ==CIL_AVRULE_NEVERALLOW ) { +struct cil_list *neverallows = extra_args; +cil_list_prepend (neverallows,CIL_LIST_ITEM , node); + } + } + +return SEPOL_OK ; +} + +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation) +{ +int rc =SEPOL_OK ; +struct cil_list *neverallows; + + *violation =CIL_FALSE ; + +if (db ==NULL || pdb ==NULL ) { +cil_log (CIL_ERR ,"CIL db and/or Policy db is NULL\n" ); +return SEPOL_ERR ; + } + +cil_list_init (&neverallows,CIL_LIST_ITEM ); + rc =cil_tree_walk (db->ast->root, __cil_collect_neverallows_helper,NULL ,NULL , neverallows); +if (rc !=SEPOL_OK ) { +cil_log (CIL_INFO ,"Failure collecting neverallows\n" ); + goto exit; + } + + rc =cil_check_neverallows (db, pdb, neverallows, violation); + +exit: +cil_list_destroy (&neverallows,CIL_FALSE ); +return rc; +} +static struct cil_list *cil_classperms_from_sepol (policydb_t *pdb,uint16_t class,uint32_t data,struct cil_class *class_value_to_cil[],struct cil_perm **perm_value_to_cil[]) {struct cil_classperms *cp;libsepol/cil/src/cil_binary.h +15
-0@@ -36,6 +36,21 @@#include "cil_tree.h" #include "cil_list.h" + +/** +* Check neverallows from a CIL database against a policy database. +* +* The types, classes, and permissions used in the CIL neverallow rules must +* be declared in the policy database. +* +* @param[in] db The cil database containing the neverallows to be used. +* @param[in] pdb The policy database to be checked for violations. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. * libsepol/src/libsepol.map.in +5
-1@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6; --2.54.0
Stephen Smalley
maintainerRe: [PATCH 2/3 v4] libsepol: Add a function to output CIL declarations from a binary policy
In reply to James CarterQuoted 7 lines · James Carter
Add the function
sepol_kernel_policydb_decls_to_cil()that takes a binary policy as input and outputs the declarations in CIL format.Also move the checks for a supported policy to a new common function and use that function in
kernel_to_confandkernel_to_cil.
Quoted 6 lines
In the near-term this will be used get the declarations from a policydb so they can be used along with neverallows written in CIL to check for neverallow and neverallowx violations in the binary policy.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Acked-by; Stephen Smalley <[email protected]>
Quoted 250 lines
---
v4: Don't delete the Python swig exception stuff like an idiot
v3: Split out less common functionality to keep the same order for the rules
to prevent breaking existing tests (even though CIL is not dependent on order.)
v2: Added more explanation to the commit msg
Split out common functionalitylibsepol/include/sepol/kernel_to_cil.h | 3 +- libsepol/src/kernel_to_cil.c | 97 +++++++++++++++++++------- libsepol/src/kernel_to_common.c | 26 +++++++ libsepol/src/kernel_to_common.h | 2 + libsepol/src/kernel_to_conf.c | 29 ++------ libsepol/src/libsepol.map.in | 1 + 6 files changed, 109 insertions(+), 49 deletions(-)libsepol/include/sepol/kernel_to_cil.h +2
-1@@ -2,4 +2,5 @@#include <sepol/policydb/policydb.h> -int sepol_kernel_policydb_to_cil (FILE *fp,struct policydb *pdb); +int sepol_kernel_policydb_to_cil (FILE *out,struct policydb *pdb); +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb);libsepol/src/kernel_to_cil.c +73
-24@@ -3262,6 +3262,11 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb)struct strs *non_mls_validatetrans =NULL ;int rc =0 ; + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + rc =strs_init (&mls_constraints,32 );if (rc !=0 ) { goto exit;@@ -3282,30 +3287,6 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb) goto exit; } -if (pdb ==NULL ) { -ERR (NULL ,"No policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policy_type !=SEPOL_POLICY_KERN ) { -ERR (NULL ,"Policy is not a kernel policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { -/* -* For policy versions between 20 and 23, attributes exist in the policy, -* but only in the type_attr_map. This means that there are gaps in both -* the type_val_to_struct and p_type_val_to_name arrays and policy rules -* can refer to those gaps. -*/ -ERR (NULL ,"Writing policy versions between 20 and 23 as CIL is not supported" ); - rc =-1 ; - goto exit; - } - rc =constraint_rules_to_strs (pdb, mls_constraints, non_mls_constraints);if (rc !=0 ) { goto exit;@@ -3533,3 +3514,71 @@ exit:return rc; } + +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb) +{ +int rc =0 ; + + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + + rc =write_sid_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_class_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_mls_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_boolean_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_alias_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attribute_sets_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_user_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + +exit: +return rc; +}libsepol/src/kernel_to_common.c +26
-0@@ -751,3 +751,29 @@ exit:return rc; } + +int check_for_supported_policy (struct policydb *pdb) +{ +if (pdb ==NULL ) { +ERR (NULL ,"No policy" ); +return -1 ; + } + +if (pdb->policy_type !=SEPOL_POLICY_KERN ) { +ERR (NULL ,"Policy is not a kernel policy" ); +return -1 ; + } + +if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { +/* +* For policy versions between 20 and 23, attributes exist in the policy, +* but only in the type_attr_map. This means that there are gaps in both +* the type_val_to_struct and p_type_val_to_name arrays and policy rules +* can refer to those gaps. +*/ +ERR (NULL ,"Writing out CIL or policy.conf from policy versions between 20 and 23 is not supported" ); +return -1 ; + } + +return 0 ; +}libsepol/src/kernel_to_common.h +2
-0@@ -117,3 +117,5 @@ int strs_stack_empty(const struct strs *stack);struct strs *isids_to_strs (const char *const *sid_to_str,unsigned num_sids,struct ocontext *isids);int sort_ocontexts (struct policydb *pdb); + +int check_for_supported_policy (struct policydb *pdb);libsepol/src/kernel_to_conf.c +5
-24@@ -3116,6 +3116,11 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb)struct strs *non_mls_validatetrans =NULL ;int rc =0 ; + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + rc =strs_init (&mls_constraints,32 );if (rc !=0 ) { goto exit;@@ -3136,30 +3141,6 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb) goto exit; } -if (pdb ==NULL ) { -ERR (NULL ,"No policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policy_type !=SEPOL_POLICY_KERN ) { -ERR (NULL ,"Policy is not a kernel policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { -/* -* For policy versions between 20 and 23, attributes exist in the policy, -* but only in the type_attr_map. This means that there are gaps in both -* the type_val_to_struct and p_type_val_to_name arrays and policy rules -* can refer to those gaps. -*/ -ERR (NULL ,"Writing policy versions between 20 and 23 as a policy.conf is not supported" ); - rc =-1 ; - goto exit; - } - rc =constraint_rules_to_strs (pdb, mls_constraints, non_mls_constraints);if (rc !=0 ) { goto exit;libsepol/src/libsepol.map.in +1
-1@@ -297,5 +297,6 @@ LIBSEPOL_3.6 { LIBSEPOL_3.11 { global: + sepol_kernel_policydb_decls_to_cil; cil_check_neverallows_against_pdb; } LIBSEPOL_3.6; --2.54.0
Stephen Smalley
maintainerRe: [PATCH 3/3 v4] secilc: Add program that checks CIL neverallows against a binary policy
In reply to James CarterQuoted 11 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks for violations in the binary policy using the neverallow and neverallowx rules from the CIL policy.
ex/
secilcheck policy.bin neverallows.cilThe CIL neverallow and neverallowx rules must only use types, classes, and permissions that are declared in the binary policy.
Quoted 6 lines
This is planned on being used in Android to validate policies from partners against the standard AOSP assertions as part of the Compatibility Test Suite.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Acked-by; Stephen Smalley <[email protected]>
Quoted 499 lines
---
v2: Added more explanation to the commit msg
Added the headerunistd.hsecilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 302 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">29 May 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +302
-1@@ -0,0 +1,302 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS +* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those +* of the authors and should not be interpreted as representing official policies, +* either expressed or implied, of Tresys Technology, LLC. +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <stdint.h> +#include <string.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> + +#ifdef ANDROID +#include <cil/cil.h> +#else +#include <sepol/cil/cil.h> +#endif +#include <sepol/policydb.h> +#include <sepol/kernel_to_cil.h> + +static int get_binary_policy_db (const char *filename,sepol_policydb_t *pdb) +{ +int fd; +struct stat sb; +void *map =NULL ; +sepol_policy_file_t *pf =NULL ; +int rc =SEPOL_ERR ; + +/* Read binary policy */ + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Could not map file: %s\n" , filename); + goto exit; + } + +if (sepol_policy_file_create (&pf) <0 ) { +fprintf (stderr,"Out of memory" ); + goto exit; + } +sepol_policy_file_set_mem (pf, map, sb.st_size); + +if (sepol_policydb_read (pdb, pf) <0 ) { +fprintf (stderr,"Error reading binary policy: %s\n" , filename); +sepol_policydb_free (pdb); + rc =SEPOL_ERR ; + goto exit; + } + + rc =SEPOL_OK ; + +exit: +if (fd >=0 ) +close (fd); +if (map) +munmap (map, sb.st_size); +sepol_policy_file_free (pf); +return rc; +} + +static int add_decls_to_cil (const char *filename,sepol_policydb_t *pdb,struct cil_db *db) +{ +FILE *cfp =NULL ; +char *cptr =NULL ; +size_t csize; +int rc =SEPOL_ERR ; + + cfp =open_memstream (&cptr, &csize); +if (!cfp) { +fprintf (stderr,"Failed to open dynamic memory buffer stream\n" ); + goto exit; + } + +if (sepol_kernel_policydb_decls_to_cil (cfp, &pdb->p) <0 ) { +fprintf (stderr,"Failed to convert binary policy to CIL\n" ); + goto exit; + } + +if (fflush (cfp) <0 ) { +fprintf (stderr,"Failed to flush CIL memory buffer stream\n" ); + goto exit; + } + +/* Add to CIL db */ +if (cil_add_file (db, filename, cptr, csize) <0 ) { +fprintf (stderr,"Failed to add binary policy file: %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (cfp) +fclose (cfp); +if (cptr) +free (cptr); +return rc; +} + +static int add_cil_file (const char *filename,struct cil_db *cdb) +{ +FILE *file; +struct stat filedata; +char *buffer =NULL ; +size_t file_size; +int rc =-1 ; + + file =fopen (filename,"r" ); +if (!file) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (stat (filename, &filedata) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + file_size = filedata.st_size; + +if (!file_size) { +fclose (file); + rc =0 ; + goto exit; + } + + buffer =malloc (file_size); +if (!buffer) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } + +if (fread (buffer, file_size,1 , file) !=1 ) { +fprintf (stderr,"Failure reading file: %s\n" , filename); + goto exit; + } + +if (cil_add_file (cdb, filename, buffer, file_size) <0 ) { +fprintf (stderr,"Failure adding %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (file) +fclose (file); +if (buffer) +free (buffer); +return rc; +} + +static __attribute__((__noreturn__))void usage (const char *prog) +{ +printf ("Usage: %s [OPTION]... BIN_POLICY CIL_NEVERALLOW_FILE1...\n" , prog); +printf ("\n" ); +printf ("Options:\n" ); +printf (" -Q, --qualified-names Allow names containing dots (qualified names).\n" ); +printf (" Blocks, blockinherits, blockabstracts, and\n" ); +printf (" in-statements will not be allowed.\n" ); +printf (" -m, --multiple-decls allow some statements to be re-declared\n" ); +printf (" -v, --verbose increment verbosity level\n" ); +printf (" -h, --help display usage information\n" ); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ +int rc =SEPOL_ERR ; +sepol_policydb_t *pdb =NULL ; +int violation =0 ; +struct cil_db *db =NULL ; +int multiple_decls =0 ; +int qualified_names =0 ; +int opt_char; +int opt_index =0 ; +enum cil_log_level log_level =CIL_ERR ; +static struct option long_opts[] = { + {"help" , no_argument,0 ,'h' }, + {"verbose" , no_argument,0 ,'v' }, + {"multiple-decls" , no_argument,0 ,'m' }, + {"qualified-names" , no_argument,0 ,'Q' }, + {0 ,0 ,0 ,0 } + }; + +while (1 ) { + opt_char =getopt_long (argc, argv,"hvQm" , long_opts, &opt_index); +if (opt_char ==-1 ) { +break ; + } +switch (opt_char) { +case 'v' : + log_level++; +break ; +case 'm' : + multiple_decls =1 ; +break ; +case 'Q' : + qualified_names =1 ; +break ; +case 'h' : +usage (argv[0 ]); +case '?' : +break ; +default : +fprintf (stderr,"Unsupported option: %s\n" , optarg); +usage (argv[0 ]); + } + } + +if (argc < optind +2 ) { +fprintf (stderr,"Not enough files specified\n" ); +usage (argv[0 ]); + } + +cil_set_log_level (log_level); + +cil_db_init (&db); +cil_set_multiple_decls (db, multiple_decls); +cil_set_qualified_names (db, qualified_names); + +if (sepol_policydb_create (&pdb) <0 ) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } +if (get_binary_policy_db (argv[optind], pdb) <0 ) { +fprintf (stderr,"Failed to get binary policy db\n" ); + rc =SEPOL_ERR ; + goto exit; + } +if (add_decls_to_cil (argv[optind], pdb, db) <0 ) { +fprintf (stderr,"Failed to convert decls to CIL\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + +do { +if (add_cil_file (argv[optind], db) <0 ) { +fprintf (stderr,"Failed to add cil file to CIL files\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + }while (optind < argc); + + rc =cil_compile (db); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to compile cildb: %d\n" , rc); + goto exit; + } + + rc =cil_check_neverallows_against_pdb (db, &pdb->p, &violation); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to complete neverallow checking\n" ); + goto exit; + } +if (violation ==1 ) { +fprintf (stderr,"There was a neverallow violation\n" ); + rc =SEPOL_ERR ; + } + + rc =SEPOL_OK ; + +exit: +cil_db_destroy (&db); +sepol_policydb_free (pdb); +return rc; +} --2.54.0
Quoted 12 lines · James Carter
Add the function
cil_check_neverallows_against_pdb()that will extract the neverallows from a CIL db and check them against a kernel policydb.Android has the requirement of validating policies from partners against the standard AOSP assertions. While this is done at build time, the requirement is also enforced during certification via the Compatibility Test Suite.
Currently, this validation is done by manually parsing the neverallow assertions which is brittle and does not support neverallowx assertions.
Quoted 6 lines
This function will allow a CIL db with the neverallow and neverallowxperm rules to be used to check a kernel policydb for violations as part of the Compatibility Test Suite.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Quoted 125 lines
---
v2: Add more context to commit msg
Add error messages for when type or class-permission look-ups fail Set violation toCIL_FALSEinitially to ensure it gets set Add more description incil_binary.hfor the functionlibsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 48 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 15 +++++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 69 insertions(+)libsepol/cil/include/cil/cil.h +1
-0@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,libsepol/cil/src/cil_binary.c +48
-0@@ -5143,6 +5143,54 @@ exit:return rc; } +static int __cil_collect_neverallows_helper (struct cil_tree_node *node,uint32_t *finished,void *extra_args) +{ +if (node->flavor ==CIL_BLOCK ) { +struct cil_block *blk = node->data; +if (blk->is_abstract ==CIL_TRUE ) { + *finished =CIL_TREE_SKIP_HEAD ; + } + }else if (node->flavor ==CIL_MACRO ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_BOOLEANIF ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_AVRULE || node->flavor ==CIL_AVRULEX ) { +struct cil_avrule *rule = node->data; +if (rule->rule_kind ==CIL_AVRULE_NEVERALLOW ) { +struct cil_list *neverallows = extra_args; +cil_list_prepend (neverallows,CIL_LIST_ITEM , node); + } + } + +return SEPOL_OK ; +} + +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation) +{ +int rc =SEPOL_OK ; +struct cil_list *neverallows; + + *violation =CIL_FALSE ; + +if (db ==NULL || pdb ==NULL ) { +cil_log (CIL_ERR ,"CIL db and/or Policy db is NULL\n" ); +return SEPOL_ERR ; + } + +cil_list_init (&neverallows,CIL_LIST_ITEM ); + rc =cil_tree_walk (db->ast->root, __cil_collect_neverallows_helper,NULL ,NULL , neverallows); +if (rc !=SEPOL_OK ) { +cil_log (CIL_INFO ,"Failure collecting neverallows\n" ); + goto exit; + } + + rc =cil_check_neverallows (db, pdb, neverallows, violation); + +exit: +cil_list_destroy (&neverallows,CIL_FALSE ); +return rc; +} +static struct cil_list *cil_classperms_from_sepol (policydb_t *pdb,uint16_t class,uint32_t data,struct cil_class *class_value_to_cil[],struct cil_perm **perm_value_to_cil[]) {struct cil_classperms *cp;libsepol/cil/src/cil_binary.h +15
-0@@ -36,6 +36,21 @@#include "cil_tree.h" #include "cil_list.h" + +/** +* Check neverallows from a CIL database against a policy database. +* +* The types, classes, and permissions used in the CIL neverallow rules must +* be declared in the policy database. +* +* @param[in] db The cil database containing the neverallows to be used. +* @param[in] pdb The policy database to be checked for violations. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. * libsepol/src/libsepol.map.in +5
-1@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6; --2.54.0
Re: [PATCH 2/3 v4] libsepol: Add a function to output CIL declarations from a binary policy
In reply to James CarterQuoted 7 lines · James Carter
Add the function
sepol_kernel_policydb_decls_to_cil()that takes a binary policy as input and outputs the declarations in CIL format.Also move the checks for a supported policy to a new common function and use that function in
kernel_to_confandkernel_to_cil.
Quoted 6 lines
In the near-term this will be used get the declarations from a policydb so they can be used along with neverallows written in CIL to check for neverallow and neverallowx violations in the binary policy.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Quoted 250 lines
---
v4: Don't delete the Python swig exception stuff like an idiot
v3: Split out less common functionality to keep the same order for the rules
to prevent breaking existing tests (even though CIL is not dependent on order.)
v2: Added more explanation to the commit msg
Split out common functionalitylibsepol/include/sepol/kernel_to_cil.h | 3 +- libsepol/src/kernel_to_cil.c | 97 +++++++++++++++++++------- libsepol/src/kernel_to_common.c | 26 +++++++ libsepol/src/kernel_to_common.h | 2 + libsepol/src/kernel_to_conf.c | 29 ++------ libsepol/src/libsepol.map.in | 1 + 6 files changed, 109 insertions(+), 49 deletions(-)libsepol/include/sepol/kernel_to_cil.h +2
-1@@ -2,4 +2,5 @@#include <sepol/policydb/policydb.h> -int sepol_kernel_policydb_to_cil (FILE *fp,struct policydb *pdb); +int sepol_kernel_policydb_to_cil (FILE *out,struct policydb *pdb); +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb);libsepol/src/kernel_to_cil.c +73
-24@@ -3262,6 +3262,11 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb)struct strs *non_mls_validatetrans =NULL ;int rc =0 ; + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + rc =strs_init (&mls_constraints,32 );if (rc !=0 ) { goto exit;@@ -3282,30 +3287,6 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb) goto exit; } -if (pdb ==NULL ) { -ERR (NULL ,"No policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policy_type !=SEPOL_POLICY_KERN ) { -ERR (NULL ,"Policy is not a kernel policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { -/* -* For policy versions between 20 and 23, attributes exist in the policy, -* but only in the type_attr_map. This means that there are gaps in both -* the type_val_to_struct and p_type_val_to_name arrays and policy rules -* can refer to those gaps. -*/ -ERR (NULL ,"Writing policy versions between 20 and 23 as CIL is not supported" ); - rc =-1 ; - goto exit; - } - rc =constraint_rules_to_strs (pdb, mls_constraints, non_mls_constraints);if (rc !=0 ) { goto exit;@@ -3533,3 +3514,71 @@ exit:return rc; } + +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb) +{ +int rc =0 ; + + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + + rc =write_sid_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_class_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_mls_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_boolean_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_alias_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attribute_sets_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_user_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + +exit: +return rc; +}libsepol/src/kernel_to_common.c +26
-0@@ -751,3 +751,29 @@ exit:return rc; } + +int check_for_supported_policy (struct policydb *pdb) +{ +if (pdb ==NULL ) { +ERR (NULL ,"No policy" ); +return -1 ; + } + +if (pdb->policy_type !=SEPOL_POLICY_KERN ) { +ERR (NULL ,"Policy is not a kernel policy" ); +return -1 ; + } + +if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { +/* +* For policy versions between 20 and 23, attributes exist in the policy, +* but only in the type_attr_map. This means that there are gaps in both +* the type_val_to_struct and p_type_val_to_name arrays and policy rules +* can refer to those gaps. +*/ +ERR (NULL ,"Writing out CIL or policy.conf from policy versions between 20 and 23 is not supported" ); +return -1 ; + } + +return 0 ; +}libsepol/src/kernel_to_common.h +2
-0@@ -117,3 +117,5 @@ int strs_stack_empty(const struct strs *stack);struct strs *isids_to_strs (const char *const *sid_to_str,unsigned num_sids,struct ocontext *isids);int sort_ocontexts (struct policydb *pdb); + +int check_for_supported_policy (struct policydb *pdb);libsepol/src/kernel_to_conf.c +5
-24@@ -3116,6 +3116,11 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb)struct strs *non_mls_validatetrans =NULL ;int rc =0 ; + rc =check_for_supported_policy (pdb); +if (rc !=0 ) { +return rc; + } + rc =strs_init (&mls_constraints,32 );if (rc !=0 ) { goto exit;@@ -3136,30 +3141,6 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb) goto exit; } -if (pdb ==NULL ) { -ERR (NULL ,"No policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policy_type !=SEPOL_POLICY_KERN ) { -ERR (NULL ,"Policy is not a kernel policy" ); - rc =-1 ; - goto exit; - } - -if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { -/* -* For policy versions between 20 and 23, attributes exist in the policy, -* but only in the type_attr_map. This means that there are gaps in both -* the type_val_to_struct and p_type_val_to_name arrays and policy rules -* can refer to those gaps. -*/ -ERR (NULL ,"Writing policy versions between 20 and 23 as a policy.conf is not supported" ); - rc =-1 ; - goto exit; - } - rc =constraint_rules_to_strs (pdb, mls_constraints, non_mls_constraints);if (rc !=0 ) { goto exit;libsepol/src/libsepol.map.in +1
-1@@ -297,5 +297,6 @@ LIBSEPOL_3.6 { LIBSEPOL_3.11 { global: + sepol_kernel_policydb_decls_to_cil; cil_check_neverallows_against_pdb; } LIBSEPOL_3.6; --2.54.0
Re: [PATCH 3/3 v4] secilc: Add program that checks CIL neverallows against a binary policy
In reply to James CarterQuoted 11 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks for violations in the binary policy using the neverallow and neverallowx rules from the CIL policy.
ex/
secilcheck policy.bin neverallows.cilThe CIL neverallow and neverallowx rules must only use types, classes, and permissions that are declared in the binary policy.
Quoted 6 lines
This is planned on being used in Android to validate policies from partners against the standard AOSP assertions as part of the Compatibility Test Suite.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>
Quoted 499 lines
---
v2: Added more explanation to the commit msg
Added the headerunistd.hsecilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 302 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">29 May 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +302
-1@@ -0,0 +1,302 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS +* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those +* of the authors and should not be interpreted as representing official policies, +* either expressed or implied, of Tresys Technology, LLC. +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <stdint.h> +#include <string.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> + +#ifdef ANDROID +#include <cil/cil.h> +#else +#include <sepol/cil/cil.h> +#endif +#include <sepol/policydb.h> +#include <sepol/kernel_to_cil.h> + +static int get_binary_policy_db (const char *filename,sepol_policydb_t *pdb) +{ +int fd; +struct stat sb; +void *map =NULL ; +sepol_policy_file_t *pf =NULL ; +int rc =SEPOL_ERR ; + +/* Read binary policy */ + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Could not map file: %s\n" , filename); + goto exit; + } + +if (sepol_policy_file_create (&pf) <0 ) { +fprintf (stderr,"Out of memory" ); + goto exit; + } +sepol_policy_file_set_mem (pf, map, sb.st_size); + +if (sepol_policydb_read (pdb, pf) <0 ) { +fprintf (stderr,"Error reading binary policy: %s\n" , filename); +sepol_policydb_free (pdb); + rc =SEPOL_ERR ; + goto exit; + } + + rc =SEPOL_OK ; + +exit: +if (fd >=0 ) +close (fd); +if (map) +munmap (map, sb.st_size); +sepol_policy_file_free (pf); +return rc; +} + +static int add_decls_to_cil (const char *filename,sepol_policydb_t *pdb,struct cil_db *db) +{ +FILE *cfp =NULL ; +char *cptr =NULL ; +size_t csize; +int rc =SEPOL_ERR ; + + cfp =open_memstream (&cptr, &csize); +if (!cfp) { +fprintf (stderr,"Failed to open dynamic memory buffer stream\n" ); + goto exit; + } + +if (sepol_kernel_policydb_decls_to_cil (cfp, &pdb->p) <0 ) { +fprintf (stderr,"Failed to convert binary policy to CIL\n" ); + goto exit; + } + +if (fflush (cfp) <0 ) { +fprintf (stderr,"Failed to flush CIL memory buffer stream\n" ); + goto exit; + } + +/* Add to CIL db */ +if (cil_add_file (db, filename, cptr, csize) <0 ) { +fprintf (stderr,"Failed to add binary policy file: %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (cfp) +fclose (cfp); +if (cptr) +free (cptr); +return rc; +} + +static int add_cil_file (const char *filename,struct cil_db *cdb) +{ +FILE *file; +struct stat filedata; +char *buffer =NULL ; +size_t file_size; +int rc =-1 ; + + file =fopen (filename,"r" ); +if (!file) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (stat (filename, &filedata) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + file_size = filedata.st_size; + +if (!file_size) { +fclose (file); + rc =0 ; + goto exit; + } + + buffer =malloc (file_size); +if (!buffer) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } + +if (fread (buffer, file_size,1 , file) !=1 ) { +fprintf (stderr,"Failure reading file: %s\n" , filename); + goto exit; + } + +if (cil_add_file (cdb, filename, buffer, file_size) <0 ) { +fprintf (stderr,"Failure adding %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (file) +fclose (file); +if (buffer) +free (buffer); +return rc; +} + +static __attribute__((__noreturn__))void usage (const char *prog) +{ +printf ("Usage: %s [OPTION]... BIN_POLICY CIL_NEVERALLOW_FILE1...\n" , prog); +printf ("\n" ); +printf ("Options:\n" ); +printf (" -Q, --qualified-names Allow names containing dots (qualified names).\n" ); +printf (" Blocks, blockinherits, blockabstracts, and\n" ); +printf (" in-statements will not be allowed.\n" ); +printf (" -m, --multiple-decls allow some statements to be re-declared\n" ); +printf (" -v, --verbose increment verbosity level\n" ); +printf (" -h, --help display usage information\n" ); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ +int rc =SEPOL_ERR ; +sepol_policydb_t *pdb =NULL ; +int violation =0 ; +struct cil_db *db =NULL ; +int multiple_decls =0 ; +int qualified_names =0 ; +int opt_char; +int opt_index =0 ; +enum cil_log_level log_level =CIL_ERR ; +static struct option long_opts[] = { + {"help" , no_argument,0 ,'h' }, + {"verbose" , no_argument,0 ,'v' }, + {"multiple-decls" , no_argument,0 ,'m' }, + {"qualified-names" , no_argument,0 ,'Q' }, + {0 ,0 ,0 ,0 } + }; + +while (1 ) { + opt_char =getopt_long (argc, argv,"hvQm" , long_opts, &opt_index); +if (opt_char ==-1 ) { +break ; + } +switch (opt_char) { +case 'v' : + log_level++; +break ; +case 'm' : + multiple_decls =1 ; +break ; +case 'Q' : + qualified_names =1 ; +break ; +case 'h' : +usage (argv[0 ]); +case '?' : +break ; +default : +fprintf (stderr,"Unsupported option: %s\n" , optarg); +usage (argv[0 ]); + } + } + +if (argc < optind +2 ) { +fprintf (stderr,"Not enough files specified\n" ); +usage (argv[0 ]); + } + +cil_set_log_level (log_level); + +cil_db_init (&db); +cil_set_multiple_decls (db, multiple_decls); +cil_set_qualified_names (db, qualified_names); + +if (sepol_policydb_create (&pdb) <0 ) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } +if (get_binary_policy_db (argv[optind], pdb) <0 ) { +fprintf (stderr,"Failed to get binary policy db\n" ); + rc =SEPOL_ERR ; + goto exit; + } +if (add_decls_to_cil (argv[optind], pdb, db) <0 ) { +fprintf (stderr,"Failed to convert decls to CIL\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + +do { +if (add_cil_file (argv[optind], db) <0 ) { +fprintf (stderr,"Failed to add cil file to CIL files\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + }while (optind < argc); + + rc =cil_compile (db); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to compile cildb: %d\n" , rc); + goto exit; + } + + rc =cil_check_neverallows_against_pdb (db, &pdb->p, &violation); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to complete neverallow checking\n" ); + goto exit; + } +if (violation ==1 ) { +fprintf (stderr,"There was a neverallow violation\n" ); + rc =SEPOL_ERR ; + } + + rc =SEPOL_OK ; + +exit: +cil_db_destroy (&db); +sepol_policydb_free (pdb); +return rc; +} --2.54.0
Stephen Smalley
maintainerRe: [PATCH 3/3 v4] secilc: Add program that checks CIL neverallows against a binary policy
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks for violations in the binary policy using the neverallow and neverallowx rules from the CIL policy.
ex/
secilcheck policy.bin neverallows.cilThe CIL neverallow and neverallowx rules must only use types, classes, and permissions that are declared in the binary policy.
Quoted 8 lines
Quoted 6 lines
This is planned on being used in Android to validate policies from partners against the standard AOSP assertions as part of the Compatibility Test Suite.
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>Acked-by Stephen Smalley <[email protected]>
All three merged.
Quoted 500 lines
Quoted 500 lines
---
v2: Added more explanation to the commit msg
Added the headerunistd.hsecilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 302 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">29 May 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +302
-1@@ -0,0 +1,302 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS +* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those +* of the authors and should not be interpreted as representing official policies, +* either expressed or implied, of Tresys Technology, LLC. +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <stdint.h> +#include <string.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> + +#ifdef ANDROID +#include <cil/cil.h> +#else +#include <sepol/cil/cil.h> +#endif +#include <sepol/policydb.h> +#include <sepol/kernel_to_cil.h> + +static int get_binary_policy_db (const char *filename,sepol_policydb_t *pdb) +{ +int fd; +struct stat sb; +void *map =NULL ; +sepol_policy_file_t *pf =NULL ; +int rc =SEPOL_ERR ; + +/* Read binary policy */ + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Could not map file: %s\n" , filename); + goto exit; + } + +if (sepol_policy_file_create (&pf) <0 ) { +fprintf (stderr,"Out of memory" ); + goto exit; + } +sepol_policy_file_set_mem (pf, map, sb.st_size); + +if (sepol_policydb_read (pdb, pf) <0 ) { +fprintf (stderr,"Error reading binary policy: %s\n" , filename); +sepol_policydb_free (pdb); + rc =SEPOL_ERR ; + goto exit; + } + + rc =SEPOL_OK ; + +exit: +if (fd >=0 ) +close (fd); +if (map) +munmap (map, sb.st_size); +sepol_policy_file_free (pf); +return rc; +} + +static int add_decls_to_cil (const char *filename,sepol_policydb_t *pdb,struct cil_db *db) +{ +FILE *cfp =NULL ; +char *cptr =NULL ; +size_t csize; +int rc =SEPOL_ERR ; + + cfp =open_memstream (&cptr, &csize); +if (!cfp) { +fprintf (stderr,"Failed to open dynamic memory buffer stream\n" ); + goto exit; + } + +if (sepol_kernel_policydb_decls_to_cil (cfp, &pdb->p) <0 ) { +fprintf (stderr,"Failed to convert binary policy to CIL\n" ); + goto exit; + } + +if (fflush (cfp) <0 ) { +fprintf (stderr,"Failed to flush CIL memory buffer stream\n" ); + goto exit; + } + +/* Add to CIL db */ +if (cil_add_file (db, filename, cptr, csize) <0 ) { +fprintf (stderr,"Failed to add binary policy file: %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (cfp) +fclose (cfp); +if (cptr) +free (cptr); +return rc; +} + +static int add_cil_file (const char *filename,struct cil_db *cdb) +{ +FILE *file; +struct stat filedata; +char *buffer =NULL ; +size_t file_size; +int rc =-1 ; + + file =fopen (filename,"r" ); +if (!file) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (stat (filename, &filedata) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + file_size = filedata.st_size; + +if (!file_size) { +fclose (file); + rc =0 ; + goto exit; + } + + buffer =malloc (file_size); +if (!buffer) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } + +if (fread (buffer, file_size,1 , file) !=1 ) { +fprintf (stderr,"Failure reading file: %s\n" , filename); + goto exit; + } + +if (cil_add_file (cdb, filename, buffer, file_size) <0 ) { +fprintf (stderr,"Failure adding %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (file) +fclose (file); +if (buffer) +free (buffer); +return rc; +} + +static __attribute__((__noreturn__))void usage (const char *prog) +{ +printf ("Usage: %s [OPTION]... BIN_POLICY CIL_NEVERALLOW_FILE1...\n" , prog); +printf ("\n" ); +printf ("Options:\n" ); +printf (" -Q, --qualified-names Allow names containing dots (qualified names).\n" ); +printf (" Blocks, blockinherits, blockabstracts, and\n" ); +printf (" in-statements will not be allowed.\n" ); +printf (" -m, --multiple-decls allow some statements to be re-declared\n" ); +printf (" -v, --verbose increment verbosity level\n" ); +printf (" -h, --help display usage information\n" ); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ +int rc =SEPOL_ERR ; +sepol_policydb_t *pdb =NULL ; +int violation =0 ; +struct cil_db *db =NULL ; +int multiple_decls =0 ; +int qualified_names =0 ; +int opt_char; +int opt_index =0 ; +enum cil_log_level log_level =CIL_ERR ; +static struct option long_opts[] = { + {"help" , no_argument,0 ,'h' }, + {"verbose" , no_argument,0 ,'v' }, + {"multiple-decls" , no_argument,0 ,'m' }, + {"qualified-names" , no_argument,0 ,'Q' }, + {0 ,0 ,0 ,0 } + }; + +while (1 ) { + opt_char =getopt_long (argc, argv,"hvQm" , long_opts, &opt_index); +if (opt_char ==-1 ) { +break ; + } +switch (opt_char) { +case 'v' : + log_level++; +break ; +case 'm' : + multiple_decls =1 ; +break ; +case 'Q' : + qualified_names =1 ; +break ; +case 'h' : +usage (argv[0 ]); +case '?' : +break ; +default : +fprintf (stderr,"Unsupported option: %s\n" , optarg); +usage (argv[0 ]); + } + } + +if (argc < optind +2 ) { +fprintf (stderr,"Not enough files specified\n" ); +usage (argv[0 ]); + } + +cil_set_log_level (log_level); + +cil_db_init (&db); +cil_set_multiple_decls (db, multiple_decls); +cil_set_qualified_names (db, qualified_names); + +if (sepol_policydb_create (&pdb) <0 ) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } +if (get_binary_policy_db (argv[optind], pdb) <0 ) { +fprintf (stderr,"Failed to get binary policy db\n" ); + rc =SEPOL_ERR ; + goto exit; + } +if (add_decls_to_cil (argv[optind], pdb, db) <0 ) { +fprintf (stderr,"Failed to convert decls to CIL\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + +do { +if (add_cil_file (argv[optind], db) <0 ) { +fprintf (stderr,"Failed to add cil file to CIL files\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + }while (optind < argc); + + rc =cil_compile (db); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to compile cildb: %d\n" , rc); + goto exit; + } + + rc =cil_check_neverallows_against_pdb (db, &pdb->p, &violation); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to complete neverallow checking\n" ); + goto exit; + } +if (violation ==1 ) { +fprintf (stderr,"There was a neverallow violation\n" ); + rc =SEPOL_ERR ; + } + + rc =SEPOL_OK ; + +exit: +cil_db_destroy (&db); +sepol_policydb_free (pdb); +return rc; +} --2.54.0
Thiébaud Weksteen
Tested-byRe: [PATCH 3/3 v4] secilc: Add program that checks CIL neverallows against a binary policy
Quoted 20 lines · Stephen Smalley
Quoted 17 lines · Stephen Smalley
Quoted 15 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks for violations in the binary policy using the neverallow and neverallowx rules from the CIL policy.
ex/
secilcheck policy.bin neverallows.cilThe CIL neverallow and neverallowx rules must only use types, classes, and permissions that are declared in the binary policy.
This is planned on being used in Android to validate policies from partners against the standard AOSP assertions as part of the Compatibility Test Suite.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Suggested-by Thiébaud Weksteen <[email protected]>Signed-off-by James Carter <[email protected]>Acked-by Stephen Smalley <[email protected]>All three merged.
Thanks again James.
I know they have been merged already, but for the whole set: