Enable direct compare of policy file with kernel policy
cover Enable direct compare of policy file with kernel policy
To date we have been unable to directly compare a policy file, e.g. /etc/selinux/targeted/policy/policy.30, with the kernel policy, i.e. /sys/fs/selinux/policy, due to non-semantic differences in the two policy images (e.g. ordering issues). sediff can be used to compare the two, but sediff is incomplete in its coverage and is necessarily less efficient than a simple cmp of two files. These two commits make it possible to directly compare a policy file with a kernel policy after normalizing them both through checkpolicy, ala:
checkpolicy -M -b /etc/selinux/targeted/policy/policy.30 -o policy.1 checkpolicy -M -b /sys/fs/selinux/policy -o policy.2
cmp policy.1 policy.2
Caveat: Un-tested with Android policy; might still differ on xperms ordering issues.
Stephen Smalley (2):
libsepol: do not write object_r types to policy file libsepol,checkpolicy: convert rangetrans and filenametrans to hashtabs
checkpolicy/policy_define.c | 57 +++--- checkpolicy/test/dispol.c | 36 +++- libsepol/cil/src/cil_binary.c | 79 +++++--- libsepol/include/sepol/policydb/context.h | 40 ++++ libsepol/include/sepol/policydb/policydb.h | 18 +- libsepol/src/expand.c | 152 ++++++++------- libsepol/src/mls.c | 52 +++-- libsepol/src/policydb.c | 293 +++++++++++++++++++---------- libsepol/src/write.c | 205 +++++++++++++------- 9 files changed, 607 insertions(+), 325 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/2 libsepol: do not write object_r types to policy file
Originally object_r's types bitmap was empty since we exempt object_r from the normal user-role and role-type checks. CIL however sets object_r's types to all types to avoid special case logic. However, the kernel does not load object_r types from the policy file; it predefines object_r and merely validates that the object_r definition in the policy has the expected value. Thus, the actual policy file and the /sys/fs/selinux/policy file were differing in their object_r entry. Fix this by not writing object_r's types to the policy file, since they are ignored by the kernel anyway.
--- libsepol/src/write.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
libsepol/src/write.c +19 -2
@@ -1078,8 +1078,25 @@ static int role_write(hashtab_key_t key, hashtab_datum_t datum, void *ptr)if (ebitmap_write (&role->dominates, fp))return POLICYDB_ERROR ;if (p->policy_type ==POLICY_KERN ) { -if (ebitmap_write (&role->types.types, fp)) -return POLICYDB_ERROR ; +if (role->s.value ==OBJECT_R_VAL ) { +/* +* CIL populates object_r's types map +* rather than handling it as a special case. +* However, this creates an inconsistency with +* the kernel policy read from /sys/fs/selinux/policy +* because the kernel ignores everything except for +* object_r's value from the policy file. +* Make them consistent by writing an empty +* ebitmap instead. +*/ +ebitmap_t empty; +ebitmap_init (&empty); +if (ebitmap_write (&empty, fp)) +return POLICYDB_ERROR ; + }else { +if (ebitmap_write (&role->types.types, fp)) +return POLICYDB_ERROR ; + } }else {if (type_set_write (&role->types, fp))return POLICYDB_ERROR ;
Signature · 1 line
2.7.4
PATCH 2/2 libsepol, checkpolicy: convert rangetrans and filenametrans to hashtabs
In reply to Stephen Smalleyrange transition and name-based type transition rules were originally simple unordered lists. They were converted to hashtabs in the kernel by commit 2f3e82d694d3d7a2db019db1bb63385fbc1066f3 ("selinux: convert range transition list to a hashtab") and by commit 2463c26d50adc282d19317013ba0ff473823ca47 ("SELinux: put name based create rules in a hashtable"), but left unchanged in libsepol and checkpolicy. Convert libsepol and checkpolicy to use the same hashtabs as the kernel for the range transitions and name-based type transitions.
With this change and the preceding one, it is possible to directly compare a policy file generated by libsepol/checkpolicy and the kernel-generated /sys/fs/selinux/policy pseudo file after normalizing them both through checkpolicy. To do so, you can run the following sequence of commands:
checkpolicy -M -b /etc/selinux/targeted/policy/policy.30 -o policy.1 checkpolicy -M -b /sys/fs/selinux/policy -o policy.2
cmp policy.1 policy.2
Normalizing the two files via checkpolicy is still necessary to ensure consistent ordering of the avtab entries. There may still be potential for other areas of difference, e.g. xperms entries may lack a well-defined order.
--- checkpolicy/policy_define.c | 57 +++--- checkpolicy/test/dispol.c | 36 +++- libsepol/cil/src/cil_binary.c | 79 +++++--- libsepol/include/sepol/policydb/context.h | 40 ++++ libsepol/include/sepol/policydb/policydb.h | 18 +- libsepol/src/expand.c | 152 ++++++++------- libsepol/src/mls.c | 52 +++-- libsepol/src/policydb.c | 293 +++++++++++++++++++---------- libsepol/src/write.c | 184 ++++++++++++------ 9 files changed, 588 insertions(+), 323 deletions(-)
checkpolicy/policy_define.c +31 -26
@@ -3220,11 +3220,12 @@ int define_filename_trans(void)ebitmap_t e_tclasses;ebitmap_node_t *snode, *tnode, *cnode;filename_trans_t *ft; +filename_trans_datum_t *ftdatum;filename_trans_rule_t *ftr;type_datum_t *typdatum;uint32_t otype;unsigned int c, s, t; -int add; +int add, rc;if (pass ==1 ) {/* stype */
@@ -3308,40 +3309,44 @@ int define_filename_trans(void)ebitmap_for_each_bit (&e_ttypes, tnode, t) {if (!ebitmap_node_get_bit (tnode, t))continue ; - -for (ft = policydbp->filename_trans; ft; ft = ft->next) { -if (ft->stype == (s +1 ) && - ft->ttype == (t +1 ) && - ft->tclass == (c +1 ) && - !strcmp (ft->name, name)) { -yyerror2 ("duplicate filename transition for: filename_trans %s %s %s:%s" , - name, - policydbp->p_type_val_to_name[s], - policydbp->p_type_val_to_name[t], - policydbp->p_class_val_to_name[c]); - goto bad; - } - } - - ft =malloc (sizeof (*ft)); + + ft =calloc (1 ,sizeof (*ft));if (!ft) {yyerror ("out of memory" ); goto bad; } -memset (ft,0 ,sizeof (*ft)); - - ft->next = policydbp->filename_trans; - policydbp->filename_trans = ft; - + ft->stype = s+1 ; + ft->ttype = t+1 ; + ft->tclass = c+1 ; ft->name =strdup (name);if (!ft->name) {yyerror ("out of memory" ); goto bad; } - ft->stype = s +1 ; - ft->ttype = t +1 ; - ft->tclass = c +1 ; - ft->otype = otype; + + ftdatum =hashtab_search (policydbp->filename_trans, + (hashtab_key_t )ft); +if (ftdatum) { +yyerror2 ("duplicate filename transition for: filename_trans %s %s %s:%s" , + name, + policydbp->p_type_val_to_name[s], + policydbp->p_type_val_to_name[t], + policydbp->p_class_val_to_name[c]); + goto bad; + } + + ftdatum =calloc (1 ,sizeof (*ftdatum)); +if (!ftdatum) { +yyerror ("out of memory" ); + goto bad; + } + rc =hashtab_insert (policydbp->filename_trans, + (hashtab_key_t )ft, + ftdatum); +if (rc) { +yyerror ("out of memory" ); + goto bad; + } } }
checkpolicy/test/dispol.c +28 -8
@@ -330,18 +330,38 @@ static void display_role_trans(policydb_t *p, FILE *fp) } } +struct filenametr_display_args { +policydb_t *p; +FILE *fp; +}; + +static int filenametr_display (hashtab_key_t key, +hashtab_datum_t datum, +void *ptr) +{ +struct filename_trans *ft = (struct filename_trans *)key; +struct filename_trans_datum *ftdatum = datum; +struct filenametr_display_args *args = ptr; +policydb_t *p = args->p; +FILE *fp = args->fp; + +display_id (p, fp,SYM_TYPES , ft->stype -1 ,"" ); +display_id (p, fp,SYM_TYPES , ft->ttype -1 ,"" ); +display_id (p, fp,SYM_CLASSES , ft->tclass -1 ,":" ); +display_id (p, fp,SYM_TYPES , ftdatum->otype -1 ,"" ); +fprintf (fp," %s\n" , ft->name); +return 0 ; +} + +static void display_filename_trans (policydb_t *p,FILE *fp) { -filename_trans_t *ft; +struct filenametr_display_args args;fprintf (fp,"filename_trans rules:\n" ); -for (ft = p->filename_trans; ft; ft = ft->next) { -display_id (p, fp,SYM_TYPES , ft->stype -1 ,"" ); -display_id (p, fp,SYM_TYPES , ft->ttype -1 ,"" ); -display_id (p, fp,SYM_CLASSES , ft->tclass -1 ,":" ); -display_id (p, fp,SYM_TYPES , ft->otype -1 ,"" ); -fprintf (fp," %s\n" , ft->name); - } + args.p = p; + args.fp = fp; +hashtab_map (p->filename_trans, filenametr_display, &args); }int menu (void )
libsepol/cil/src/cil_binary.c +48 -31
134 lines not on this page · raw message
libsepol/include/sepol/policydb/context.h +40 -0
@@ -50,6 +50,46 @@ static inline int mls_context_cpy(context_struct_t * dst,return 0 ; } +/* +* Sets both levels in the MLS range of 'dst' to the low level of 'src'. +*/ +static inline int mls_context_cpy_low (context_struct_t *dst,context_struct_t *src) +{ +int rc; + + dst->range.level[0 ].sens = src->range.level[0 ].sens; + rc =ebitmap_cpy (&dst->range.level[0 ].cat, &src->range.level[0 ].cat); +if (rc) + goto out; + + dst->range.level[1 ].sens = src->range.level[0 ].sens; + rc =ebitmap_cpy (&dst->range.level[1 ].cat, &src->range.level[0 ].cat); +if (rc) +ebitmap_destroy (&dst->range.level[0 ].cat); +out: +return rc; +} + +/* +* Sets both levels in the MLS range of 'dst' to the high level of 'src'. +*/ +static inline int mls_context_cpy_high (context_struct_t *dst,context_struct_t *src) +{ +int rc; + + dst->range.level[0 ].sens = src->range.level[1 ].sens; + rc =ebitmap_cpy (&dst->range.level[0 ].cat, &src->range.level[1 ].cat); +if (rc) + goto out; + + dst->range.level[1 ].sens = src->range.level[1 ].sens; + rc =ebitmap_cpy (&dst->range.level[1 ].cat, &src->range.level[1 ].cat); +if (rc) +ebitmap_destroy (&dst->range.level[0 ].cat); +out: +return rc; +} +static inline int mls_context_cmp (context_struct_t * c1,context_struct_t * c2) { return (mls_level_eq(&c1->range.level[0], &c2->range.level[0]) &&
libsepol/include/sepol/policydb/policydb.h +9 -9
@@ -162,10 +162,12 @@ typedef struct filename_trans {uint32_t ttype;uint32_t tclass;char *name; -uint32_t otype; -struct filename_trans *next; } filename_trans_t; +typedef struct filename_trans_datum { +uint32_t otype;/* expected of new object */ +}filename_trans_datum_t ; +/* Type attributes */ typedef struct type_datum { symtab_datum_t s;
@@ -218,8 +220,6 @@ typedef struct range_trans {uint32_t source_type;uint32_t target_type;uint32_t target_class; -mls_range_t target_range; -struct range_trans *next; } range_trans_t;/* Boolean data type */
@@ -555,9 +555,6 @@ typedef struct policydb {/* role transitions */ role_trans_t *role_tr; -/* type transition rules with a 'name' component */ -filename_trans_t *filename_trans; -/* role allows */ role_allow_t *role_allow;
@@ -570,8 +567,11 @@ typedef struct policydb {fixed labeling behavior. */ genfs_t *genfs; -/* range transitions */ -range_trans_t *range_tr; +/* range transitions table (range_trans_key -> mls_range) */ +hashtab_t range_tr; + +/* file transitions with the last path component */ +hashtab_t filename_trans;ebitmap_t *type_attr_map;
libsepol/src/expand.c +82 -70
202 lines not on this page · raw message
libsepol/src/mls.c +34 -18
71 lines not on this page · raw message
libsepol/src/policydb.c +194 -99
448 lines not on this page · raw message
libsepol/src/write.c +122 -62
220 lines not on this page · raw message
Signature · 1 line
2.7.4