checkpolicy,libsepol: add prefix/suffix matching to filename type transitions
v2 cover checkpolicy,libsepol: add prefix/suffix matching to filename type transitions
Currently, filename transitions are stored separately from other type enforcement rules and only support exact name matching. However, in practice, the names contain variable parts. This leads to many duplicated rules in the policy that differ only in the part of the name, or it is even impossible to cover all possible combinations.
This series implements equivalent changes made by this kernel patch series [1].
First, this series of patches moves the filename transitions to be part of the avtab and avrule structures. This not only makes the implementation of prefix/suffix matching and future enhancements easier, but also reduces the technical debt regarding the filename transitions. Next, the last three patches implement the support for prefix/suffix name matching itself by extending the structures added in previous patches in this series and adding the support to CIL in the last of the triple.
Even though, moving everything to avtab increases the memory usage and the size of the binary policy itself and thus the loading time, the ability to match the prefix or suffix of the name will reduce the overall number of rules in the policy which should mitigate this issue.
[1]: https://lore.kernel.org/selinux/20230601170302.nrhuay2wh44g6sh4@jmarcin-t14s-01/
Changelog:
v2:
- rebased to the latest upstream main
- added syntax examples to commit messages (suggested by Jim)
- removed the "match_" prefix from keywords and dropped the "match_exact" keyword (suggested by Jim, discussed with Ondrej)
- fixed the bug Jim found [2] where the output to conf and CIL did not work
- reworked avtab entry skipping when writing older versions of the policy
- fixed bug where expand_terule_helper() skipped next entries after filename transition entry
- fixed memory leak in filenametr_destroy() found by Christian in the kernel version of the code [3]
[2]: https://lore.kernel.org/selinux/CAP+JOzS1nhxF8EttikRtYBW2QtW=ck+P4mC2dovgP4kp-g9WQQ@mail.gmail.com/
[3]: https://lore.kernel.org/selinux/CAJ2a_DecaSa1Z7HiRs4sGUGx3AyKwhFA41EhAo+BViv-qA9ndg@mail.gmail.com/
Juraj Marcin (8):
checkpolicy,libsepol: move transition to separate structure in avtab checkpolicy,libsepol: move filename transitions to avtab checkpolicy,libsepol: move filename transition rules to avrule
libsepol: implement new kernel binary format for avtab
libsepol: implement new module binary format of avrule checkpolicy,libsepol: add prefix/suffix support to kernel policy checkpolicy,libsepol: add prefix/suffix support to module policy libsepol/cil: add support for prefix/suffix filename transtions to CIL
checkpolicy/checkmodule.c | 9 + checkpolicy/module_compiler.c | 12 - checkpolicy/module_compiler.h | 1 - checkpolicy/policy_define.c | 211 +----- checkpolicy/policy_define.h | 3 +- checkpolicy/policy_parse.y | 13 +- checkpolicy/policy_scan.l | 4 + checkpolicy/test/dismod.c | 39 +- checkpolicy/test/dispol.c | 106 +-- libsepol/cil/src/cil.c | 6 + libsepol/cil/src/cil_binary.c | 63 +- libsepol/cil/src/cil_build_ast.c | 26 +- libsepol/cil/src/cil_copy_ast.c | 1 + libsepol/cil/src/cil_internal.h | 4 + libsepol/cil/src/cil_policy.c | 17 +- libsepol/cil/src/cil_resolve_ast.c | 10 + libsepol/cil/src/cil_write_ast.c | 2 + libsepol/include/sepol/policydb/avtab.h | 19 +- libsepol/include/sepol/policydb/hashtab.h | 8 + libsepol/include/sepol/policydb/policydb.h | 50 +- libsepol/src/avrule_block.c | 1 - libsepol/src/avtab.c | 338 +++++++++- libsepol/src/conditional.c | 6 +- libsepol/src/expand.c | 153 ++--- libsepol/src/kernel_to_cil.c | 182 ++--- libsepol/src/kernel_to_common.h | 10 + libsepol/src/kernel_to_conf.c | 178 ++--- libsepol/src/link.c | 57 +- libsepol/src/module_to_cil.c | 86 +-- libsepol/src/optimize.c | 8 + libsepol/src/policydb.c | 479 +++----------- libsepol/src/policydb_validate.c | 101 +-- libsepol/src/services.c | 5 +- libsepol/src/write.c | 735 ++++++++++++++++----- 34 files changed, 1553 insertions(+), 1390 deletions(-)
Signature · 1 line
2.40.0
PATCH v2 1/8 checkpolicy,libsepol: move transition to separate structure in avtab
To move filename transitions to be part of avtab, we need to create space for it in the avtab_datum structure which holds the rule for a certain combination of stype, ttype and tclass.
As only type transitions have a special variant that uses a filename, it would be suboptimal to add a (mostly empty) pointer to some structure to all avtab rules.
Therefore, this patch adds a new structure to the avtab_datum and moves the otype of the transition to this structure. In the next patch, this structure will also hold filename transitions for the combination of stype, ttype and tclass.
--- checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 26 +++++++++++++++----- libsepol/include/sepol/policydb/avtab.h | 7 +++++- libsepol/src/avtab.c | 32 ++++++++++++++++++++++++- libsepol/src/expand.c | 8 +++++-- libsepol/src/kernel_to_cil.c | 3 ++- libsepol/src/kernel_to_conf.c | 3 ++- libsepol/src/optimize.c | 4 ++++ libsepol/src/policydb_validate.c | 4 +++- libsepol/src/services.c | 5 +++- libsepol/src/write.c | 17 ++++++++++--- 11 files changed, 93 insertions(+), 18 deletions(-)
checkpolicy/test/dispol.c +1 -1
@@ -180,7 +180,7 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t whaif (key->specified &AVTAB_TRANSITION ) {fprintf (fp,"type_transition " );render_key (key, p, fp); -render_type (datum->data, p, fp); +render_type (datum->trans->otype, p, fp);fprintf (fp,";\n" ); }if (key->specified &AVTAB_MEMBER ) {
libsepol/cil/src/cil_binary.c +20 -6
@@ -975,28 +975,34 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,int rc =SEPOL_OK ;avtab_key_t avtab_key;avtab_datum_t avtab_datum; +avtab_trans_t trans;avtab_ptr_t existing; avtab_key.source_type = src; avtab_key.target_type = tgt; avtab_key.target_class = obj; +memset (&avtab_datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t)); +switch (kind) {case CIL_TYPE_TRANSITION : avtab_key.specified =AVTAB_TRANSITION ; + trans.otype = res; + avtab_datum.trans = &trans;break ;case CIL_TYPE_CHANGE : avtab_key.specified =AVTAB_CHANGE ; + avtab_datum.data = res;break ;case CIL_TYPE_MEMBER : avtab_key.specified =AVTAB_MEMBER ; + avtab_datum.data = res;break ;default : rc =SEPOL_ERR ; goto exit; } - - avtab_datum.data = res; existing =avtab_search_node (&pdb->te_avtab, &avtab_key);if (existing) {
@@ -1004,13 +1010,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,* A warning should have been previously given if there is a * non-duplicate rule using the same key. */ -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;
@@ -1037,13 +1047,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, search_datum =cil_cond_av_list_search (&avtab_key, other_list);if (search_datum ==NULL ) { -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;
libsepol/include/sepol/policydb/avtab.h +6 -1
@@ -70,6 +70,10 @@ typedef struct avtab_key {uint16_t specified;/* what fields are specified */ } avtab_key_t; +typedef struct avtab_trans { +uint32_t otype;/* resulting type of the new object */ +}avtab_trans_t ; + typedef struct avtab_extended_perms { #define AVTAB_XPERMS_IOCTLFUNCTION 0x01
@@ -81,7 +85,8 @@ typedef struct avtab_extended_perms { } avtab_extended_perms_t;typedef struct avtab_datum { -uint32_t data;/* access vector or type */ +uint32_t data;/* access vector, member or change value */ +avtab_trans_t *trans;/* transition value */ avtab_extended_perms_t *xperms; }avtab_datum_t ;
libsepol/src/avtab.c +31 -1
@@ -94,6 +94,7 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, avtab_datum_t * datum) {avtab_ptr_t newnode; +avtab_trans_t *trans;avtab_extended_perms_t *xperms; newnode = (avtab_ptr_t )malloc (sizeof (struct avtab_node ));
@@ -117,6 +118,16 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,* So copy data so it is set in the avtab */ newnode->datum.data = datum->data; + }else if (key->specified &AVTAB_TRANSITION ) { + trans =calloc (1 ,sizeof (*trans)); +if (trans ==NULL ) { +free (newnode); +return NULL ; + } +if (datum->trans)/* else caller populates transition */ + *trans = *(datum->trans); + + newnode->datum.trans = trans; }else { newnode->datum = *datum; }
@@ -317,6 +328,8 @@ void avtab_destroy(avtab_t * h)while (cur !=NULL ) {if (cur->key.specified &AVTAB_XPERMS ) {free (cur->datum.xperms); + }else if (cur->key.specified &AVTAB_TRANSITION ) { +free (cur->datum.trans); } temp = cur; cur = cur->next;
@@ -440,6 +453,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,uint32_t buf32[8 ], items, items2, val;avtab_key_t key;avtab_datum_t datum; +avtab_trans_t trans;avtab_extended_perms_t xperms;unsigned set;unsigned int i;
@@ -447,6 +461,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,memset (&key,0 ,sizeof (avtab_key_t));memset (&datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t));memset (&xperms,0 ,sizeof (avtab_extended_perms_t));if (vers <POLICYDB_VERSION_AVTAB ) {
@@ -509,7 +524,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,return -1 ; } key.specified = spec_order[i] | enabled; - datum.data =le32_to_cpu (buf32[items++]); +if (key.specified &AVTAB_TRANSITION ) { + trans.otype = +le32_to_cpu (buf32[items++]); + datum.trans = &trans; + }else { + datum.data = +le32_to_cpu (buf32[items++]); + } rc =insertf (a, &key, &datum, p);if (rc)return rc;
@@ -571,6 +593,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,for (i =0 ; i <ARRAY_SIZE (xperms.perms); i++) xperms.perms[i] =le32_to_cpu (buf32[i]); datum.xperms = &xperms; + }else if (key.specified &AVTAB_TRANSITION ) { + rc =next_entry (buf32, fp,sizeof (uint32_t )); +if (rc <0 ) { +ERR (fp->handle,"truncated entry" ); +return -1 ; + } + trans.otype =le32_to_cpu (*buf32); + datum.trans = &trans; }else { rc =next_entry (buf32, fp,sizeof (uint32_t ));if (rc <0 ) {
libsepol/src/expand.c +6 -2
@@ -1746,7 +1746,7 @@ static int expand_terule_helper(sepol_handle_t * handle,if (conflict) { avdatump = &node->datum;if (specified &AVRULE_TRANSITION ) { - oldtype = avdatump->data; + oldtype = avdatump->trans->otype; }else if (specified &AVRULE_MEMBER ) { oldtype = avdatump->data; }else if (specified &AVRULE_CHANGE ) {
@@ -1789,7 +1789,11 @@ static int expand_terule_helper(sepol_handle_t * handle, } avdatump = &node->datum; - avdatump->data = remapped_data; +if (specified &AVRULE_TRANSITION ) { + avdatump->trans->otype = remapped_data; + }else { + avdatump->data = remapped_data; + } cur = cur->next; }
libsepol/src/kernel_to_cil.c +2 -1
@@ -1704,7 +1704,8 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *tgt;char *src, *class, *perms, *new;
libsepol/src/kernel_to_conf.c +2 -1
@@ -1682,7 +1682,8 @@ exit:static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *src, *tgt, *class, *perms, *new;char *rule =NULL ;
libsepol/src/optimize.c +4 -0
@@ -308,6 +308,8 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;
@@ -427,6 +429,8 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;
libsepol/src/policydb_validate.c +3 -1
@@ -836,7 +836,9 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *if (validate_avtab_key (k,0 , margs->policy, margs->flavors))return -1 ; -if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) +uint32_t otype = k->specified &AVTAB_TRANSITION + ? d->trans->otype : d->data; +if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (otype, margs->policy, margs->flavors))return -1 ;if ((k->specified &AVTAB_XPERMS ) &&validate_xperms (d->xperms))
libsepol/src/services.c +4 -1
@@ -1423,7 +1423,10 @@ static int sepol_compute_sid(sepol_security_id_t ssid,if (avdatum) {/* Use the type from the type transition/member/change rule. */ - newcontext.type = avdatum->data; +if (specified &AVTAB_TRANSITION ) + newcontext.type = avdatum->trans->otype; +else + newcontext.type = avdatum->data; }/* Check for class-specific changes. */
libsepol/src/write.c +14 -3
@@ -190,14 +190,20 @@ static int avtab_write_item(policydb_t * p,ERR (fp->handle,"missing node" );return POLICYDB_ERROR ; } - buf32[items++] = -cpu_to_le32 (node->datum.data); +uint32_t data = + node->key.specified &AVTAB_TRANSITION + ? node->datum.trans->otype + : node->datum.data; + buf32[items++] =cpu_to_le32 (data); set--; node->merged =1 ; } } }else { - buf32[items++] =cpu_to_le32 (cur->datum.data); +uint32_t data = cur->key.specified &AVTAB_TRANSITION + ? cur->datum.trans->otype + : cur->datum.data; + buf32[items++] =cpu_to_le32 (data); cur->merged =1 ; set--; }
@@ -256,6 +262,11 @@ static int avtab_write_item(policydb_t * p, items =put_entry (buf32,sizeof (uint32_t ),8 ,fp);if (items !=8 )return POLICYDB_ERROR ; + }else if (cur->key.specified &AVTAB_TRANSITION ) { + buf32[0 ] =cpu_to_le32 (cur->datum.trans->otype); + items =put_entry (buf32,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; }else { buf32[0 ] =cpu_to_le32 (cur->datum.data); items =put_entry (buf32,sizeof (uint32_t ),1 , fp);
Signature · 1 line
2.40.0
PATCH v2 2/8 checkpolicy,libsepol: move filename transitions to avtab
In reply to Juraj MarcinCurrently, filename transitions are stored separately from other type enforcement rules. This leads to possibly sub-optimal performance and makes further improvements cumbersome.
This patch adds a symbol table with filename transitions to the transition structure added to avtab in the previous patch. It also implements functions required for reading and writing filename transitions (either binary or source formats) and updates the code for expanding attributes. Last but not least, it updates the conflict check in the conditional avtab to account for empty transitions in the non-conditional avtab.
These changes are expected to cause higher memory usage, as now there needs to be a filename transition structure for every stype. This patch effectively undoes most of the commit 42ae834a ("libsepol,checkpolicy:
optimize storage of filename transitions"), but this will be mitigated by providing support for matching prefix/suffix of the filename for filename transitions in future patches which will reduce to need to have so many of them.
--- checkpolicy/checkmodule.c | 9 + checkpolicy/checkpolicy.c | 9 + checkpolicy/policy_define.c | 21 +- checkpolicy/test/dispol.c | 83 ++-- libsepol/cil/src/cil_binary.c | 27 +- libsepol/include/sepol/policydb/avtab.h | 9 + libsepol/include/sepol/policydb/hashtab.h | 8 + libsepol/include/sepol/policydb/policydb.h | 22 - libsepol/src/avtab.c | 199 +++++++++ libsepol/src/conditional.c | 6 +- libsepol/src/expand.c | 27 +- libsepol/src/kernel_to_cil.c | 159 ++----- libsepol/src/kernel_to_common.h | 9 + libsepol/src/kernel_to_conf.c | 155 ++----- libsepol/src/optimize.c | 8 +- libsepol/src/policydb.c | 343 +-------------- libsepol/src/policydb_validate.c | 69 ++- libsepol/src/write.c | 463 +++++++++++++++------ 18 files changed, 804 insertions(+), 822 deletions(-)
checkpolicy/checkmodule.c +9 -0
@@ -275,6 +275,15 @@ int main(int argc, char **argv) modpolicydb.mls = mlspol; modpolicydb.handle_unknown = handle_unknown; +/* +* Init and alloc te_avtab for filename transition duplicate +* checking +*/ +if (avtab_init (&modpolicydb.te_avtab)) +exit (1 ); +if (avtab_alloc (&modpolicydb.te_avtab,1 <<11 )) +exit (1 ); +if (read_source_policy (&modpolicydb, file, argv[0 ]) ==-1 ) {exit (1 ); }
checkpolicy/checkpolicy.c +9 -0
@@ -611,6 +611,15 @@ int main(int argc, char **argv) parse_policy.mls = mlspol; parse_policy.handle_unknown = handle_unknown; +/* +* Init and alloc te_avtab for filename transition duplicate +* checking +*/ +if (avtab_init (&parse_policy.te_avtab)) +exit (1 ); +if (avtab_alloc (&parse_policy.te_avtab,1 <<11 )) +exit (1 ); + policydbp = &parse_policy;if (read_source_policy (policydbp, file,"checkpolicy" ) <0 )
checkpolicy/policy_define.c +15 -6
@@ -3352,6 +3352,7 @@ int define_filename_trans(void)ebitmap_node_t *snode, *tnode, *cnode;filename_trans_rule_t *ftr;type_datum_t *typdatum; +avtab_key_t avt_key;uint32_t otype;unsigned int c, s, t;int add, self, rc;
@@ -3443,9 +3444,13 @@ int define_filename_trans(void)ebitmap_for_each_positive_bit (&e_tclasses, cnode, c) {ebitmap_for_each_positive_bit (&e_stypes, snode, s) {ebitmap_for_each_positive_bit (&e_ttypes, tnode, t) { - rc =policydb_filetrans_insert ( - policydbp, s+1 , t+1 , c+1 , name, -NULL , otype,NULL + avt_key.specified =AVTAB_TRANSITION ; + avt_key.source_type = s +1 ; + avt_key.target_type = t +1 ; + avt_key.target_class = c +1 ; + rc =avtab_insert_filename_trans ( + &policydbp->te_avtab, &avt_key, otype, + name,NULL );if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) {
@@ -3461,9 +3466,13 @@ int define_filename_trans(void) } }if (self) { - rc =policydb_filetrans_insert ( - policydbp, s+1 , s+1 , c+1 , name, -NULL , otype,NULL + avt_key.specified =AVTAB_TRANSITION ; + avt_key.source_type = s +1 ; + avt_key.target_type = t +1 ; + avt_key.target_class = c +1 ; + rc =avtab_insert_filename_trans ( + &policydbp->te_avtab, &avt_key, otype, + name,NULL );if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) {
checkpolicy/test/dispol.c +33 -50
113 lines not on this page · raw message
libsepol/cil/src/cil_binary.c +20 -7
@@ -1005,7 +1005,12 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, } existing =avtab_search_node (&pdb->te_avtab, &avtab_key); -if (existing) { +/* +* There might be empty transition node containing filename transitions +* only. That is okay, we can merge them later. +*/ +if (existing && !(existing->key.specified &AVTAB_TRANSITION && + !existing->datum.trans->otype)) {/* Don't add duplicate type rule and warn if they conflict. * A warning should have been previously given if there is a * non-duplicate rule using the same key.
@@ -1029,7 +1034,13 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, }if (!cond_node) { - rc =avtab_insert (&pdb->te_avtab, &avtab_key, &avtab_datum); +/* If we have node from empty filename transition, use it */ +if (existing && existing->key.specified &AVTAB_TRANSITION && + !existing->datum.trans->otype) + existing->datum.trans->otype = avtab_datum.trans->otype; +else + rc =avtab_insert (&pdb->te_avtab, &avtab_key, + &avtab_datum); }else { existing =avtab_search_node (&pdb->te_cond_avtab, &avtab_key);if (existing) {
@@ -1189,16 +1200,18 @@ static int __cil_typetransition_to_avtab_helper(policydb_t *pdb,class_datum_t *sepol_obj =NULL ;uint32_t otype;struct cil_list_item *c; +avtab_key_t avt_key;cil_list_for_each (c, class_list) { rc =__cil_get_sepol_class_datum (pdb,DATUM (c->data), &sepol_obj);if (rc !=SEPOL_OK )return rc; - rc =policydb_filetrans_insert ( - pdb, sepol_src->s.value, sepol_tgt->s.value, - sepol_obj->s.value, name,NULL , - sepol_result->s.value, &otype - ); + avt_key.specified =AVTAB_TRANSITION ; + avt_key.source_type = sepol_src->s.value; + avt_key.target_type = sepol_tgt->s.value; + avt_key.target_class = sepol_obj->s.value; + rc =avtab_insert_filename_trans (&pdb->te_avtab, &avt_key, + sepol_result->s.value, name, &otype);if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) {if (sepol_result->s.value!= otype) {
libsepol/include/sepol/policydb/avtab.h +9 -0
@@ -42,6 +42,7 @@#include <sys/types.h> #include <stdint.h> +#include <sepol/policydb/symtab.h> #ifdef __cplusplusextern "C" {
@@ -72,6 +73,7 @@ typedef struct avtab_key {typedef struct avtab_trans {uint32_t otype;/* resulting type of the new object */ +symtab_t name_trans;/* filename transitions */ }avtab_trans_t ; typedef struct avtab_extended_perms {
@@ -115,6 +117,8 @@ extern int avtab_insert(avtab_t * h, avtab_key_t * k, avtab_datum_t * d);extern avtab_datum_t *avtab_search (avtab_t * h,avtab_key_t * k); +extern void avtab_trans_destroy (avtab_trans_t *trans); +extern void avtab_destroy (avtab_t * h); extern int avtab_map(const avtab_t * h,
@@ -148,6 +152,11 @@ extern avtab_ptr_t avtab_search_node_next(avtab_ptr_t node, int specified);/* avtab_alloc uses one bucket per 2-4 elements, so adjust to get maximum buckets */ #define MAX_AVTAB_SIZE (MAX_AVTAB_HASH_BUCKETS << 1) +extern int avtab_insert_filename_trans (avtab_t *a,avtab_key_t *key, +uint32_t otype,const char *name, +uint32_t *present_otype); +extern int avtab_filename_trans_read (void *fp,uint32_t vers,avtab_t *a); + #ifdef __cplusplus }#endif
libsepol/include/sepol/policydb/hashtab.h +8 -0
@@ -110,6 +110,14 @@ extern int hashtab_map(hashtab_t h,extern void hashtab_hash_eval (hashtab_t h,char *tag); +/* Returns number of elements in the hashtab h or 0 is h is NULL */ +static inline uint32_t hashtab_nel (hashtab_t h) +{ +if (!h) +return 0 ; +return h->nel; +} + #ifdef __cplusplus }#endif
libsepol/include/sepol/policydb/policydb.h +0 -22
@@ -161,19 +161,6 @@ typedef struct role_allow {struct role_allow *next; } role_allow_t; -/* filename_trans rules */ -typedef struct filename_trans_key { -uint32_t ttype; -uint32_t tclass; -char *name; -}filename_trans_key_t ; - -typedef struct filename_trans_datum { -ebitmap_t stypes; -uint32_t otype; -struct filename_trans_datum *next; -}filename_trans_datum_t ; -/* Type attributes */ typedef struct type_datum { symtab_datum_t s;
@@ -592,10 +579,6 @@ typedef struct policydb {/* range transitions table (range_trans_key -> mls_range) */ hashtab_t range_tr; -/* file transitions with the last path component */ -hashtab_t filename_trans; -uint32_t filename_trans_count; -ebitmap_t *type_attr_map;ebitmap_t *attr_type_map;/* not saved in the binary policy */
@@ -654,11 +637,6 @@ extern int policydb_load_isids(policydb_t * p, sidtab_t * s);extern int policydb_sort_ocontexts (policydb_t *p); -extern int policydb_filetrans_insert (policydb_t *p,uint32_t stype, -uint32_t ttype,uint32_t tclass, -const char *name,char **name_alloc, -uint32_t otype,uint32_t *present_otype); -/* Deprecated */ extern int policydb_context_isvalid (const policydb_t * p,const context_struct_t * c);
libsepol/src/avtab.c +199 -0
214 lines not on this page · raw message
libsepol/src/conditional.c +5 -1
@@ -624,6 +624,7 @@ static int cond_insertf(avtab_t * astruct policydb *p = data->p;cond_av_list_t *other = data->other, *list, *cur;avtab_ptr_t node_ptr; +avtab_datum_t *existing;uint8_t found;/*
@@ -632,7 +633,10 @@ static int cond_insertf(avtab_t * a* cond_te_avtab. */ if (k->specified &AVTAB_TYPE ) { -if (avtab_search (&p->te_avtab, k)) { + existing =avtab_search (&p->te_avtab, k); +/* empty transition rule is not a conflict */ +if (existing && !(k->specified &AVTAB_TRANSITION + && !existing->trans->otype)) {WARN (NULL ,"security: type rule already exists outside of a conditional." );return -1 ; }
libsepol/src/expand.c +22 -5
@@ -1413,14 +1413,17 @@ static int expand_filename_trans_helper(expand_state_t *state, {uint32_t mapped_otype, present_otype;int rc; +avtab_key_t avt_key; mapped_otype = state->typemap[rule->otype -1 ]; - rc =policydb_filetrans_insert ( - state->out, s +1 , t +1 , - rule->tclass, rule->name, -NULL , mapped_otype, &present_otype - ); + avt_key.specified =AVTAB_TRANSITION ; + avt_key.source_type = s +1 ; + avt_key.target_type = t +1 ; + avt_key.target_class = rule->tclass; + + rc =avtab_insert_filename_trans (&state->out->te_avtab, &avt_key, + mapped_otype, rule->name, &present_otype);if (rc ==SEPOL_EEXIST ) {/* duplicate rule, ignore */ if (present_otype == mapped_otype)
@@ -1734,6 +1737,16 @@ static int expand_terule_helper(sepol_handle_t * handle,* either in the global scope or in another * conditional AV tab */ node =avtab_search_node (&p->te_avtab, &avkey); + +/* +* if node does not already contain transition, it is not a +* conflict and transition otype will be set to node found by +* find_avtab_node() +*/ +if (specified &AVRULE_TRANSITION && node && + !node->datum.trans->otype) + node =NULL ; +if (node) { conflict =1 ; }else {
@@ -1741,6 +1754,10 @@ static int expand_terule_helper(sepol_handle_t * handle,if (node && node->parse_context != other) { conflict =2 ; } +/* +* conditional avtab does not contain filename +* transitions, no need to check for otype == 0 +*/ }if (conflict) {
libsepol/src/kernel_to_cil.c +43 -116
229 lines not on this page · raw message
libsepol/src/kernel_to_common.h +9 -0
@@ -83,6 +83,15 @@ struct strs {size_t size; }; +typedef struct { +policydb_t *pdb; +struct strs *strs; +const char *flavor; +const char *src; +const char *tgt; +const char *class; +}name_trans_to_strs_args_t ; +void sepol_indent (FILE *out,int indent); __attribute__ ((format (printf,2 ,3 )))void sepol_printf (FILE *out,const char *fmt, ...);
libsepol/src/kernel_to_conf.c +43 -112
223 lines not on this page · raw message
libsepol/src/optimize.c +6 -2
@@ -308,8 +308,10 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); -if (tmp->key.specified &AVTAB_TRANSITION ) +if (tmp->key.specified &AVTAB_TRANSITION ) { +avtab_trans_destroy (tmp->datum.trans);free (tmp->datum.trans); + }free (tmp); tab->nel--;
@@ -429,8 +431,10 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); -if (tmp->key.specified &AVTAB_TRANSITION ) +if (tmp->key.specified &AVTAB_TRANSITION ) { +avtab_trans_destroy (tmp->datum.trans);free (tmp->datum.trans); + }free (tmp); tab->nel--;
libsepol/src/policydb.c +1 -342
385 lines not on this page · raw message
libsepol/src/policydb_validate.c +28 -41
94 lines not on this page · raw message
libsepol/src/write.c +345 -118
500 lines not on this page · raw message
Signature · 1 line
2.40.0
PATCH v2 3/8 checkpolicy,libsepol: move filename transition rules to avrule
In reply to Juraj MarcinSimilarly to the previous patch, filename transition rules are stored and parsed separately from other type enforcement rules. Moving them to avrule makes it consistent with the filename transitions in avtab and makes future improvements easier to implement.
This patch adds an optional object name attribute to the avrule structure and uses this new attribute to move filename transition rules to avrule. It also updates functions for parsing type enforcement rules to accept rules with a filename as their last argument (filename transition rules), separate functions for parsing filename transitions are therefore no longer needed.
--- checkpolicy/checkpolicy.c | 9 - checkpolicy/module_compiler.c | 12 -- checkpolicy/module_compiler.h | 1 - checkpolicy/policy_define.c | 215 +++------------------ checkpolicy/policy_define.h | 3 +- checkpolicy/policy_parse.y | 8 +- checkpolicy/test/dismod.c | 25 +-- libsepol/cil/src/cil_binary.c | 2 + libsepol/include/sepol/policydb/policydb.h | 16 +- libsepol/src/avrule_block.c | 1 - libsepol/src/expand.c | 132 ++++--------- libsepol/src/link.c | 56 +----- libsepol/src/module_to_cil.c | 71 +------ libsepol/src/policydb.c | 70 ++----- libsepol/src/policydb_validate.c | 27 --- libsepol/src/write.c | 86 +++++---- 16 files changed, 154 insertions(+), 580 deletions(-)
checkpolicy/checkpolicy.c +0 -9
@@ -611,15 +611,6 @@ int main(int argc, char **argv) parse_policy.mls = mlspol; parse_policy.handle_unknown = handle_unknown; -/* -* Init and alloc te_avtab for filename transition duplicate -* checking -*/ -if (avtab_init (&parse_policy.te_avtab)) -exit (1 ); -if (avtab_alloc (&parse_policy.te_avtab,1 <<11 )) -exit (1 ); - policydbp = &parse_policy;if (read_source_policy (policydbp, file,"checkpolicy" ) <0 )
checkpolicy/module_compiler.c +0 -12
@@ -1278,18 +1278,6 @@ void append_role_allow(role_allow_rule_t * role_allow_rules) decl->role_allow_rules = role_allow_rules; } -/* this doesn't actually append, but really prepends it */ -void append_filename_trans (filename_trans_rule_t * filename_trans_rules) -{ -avrule_decl_t *decl = stack_top->decl; - -/* filename transitions are not allowed within conditionals */ -assert (stack_top->type ==1 ); - - filename_trans_rules->next = decl->filename_trans_rules; - decl->filename_trans_rules = filename_trans_rules; -} -/* this doesn't actually append, but really prepends it */ void append_range_trans (range_trans_rule_t * range_tr_rules) {
checkpolicy/module_compiler.h +0 -1
@@ -83,7 +83,6 @@ void append_avrule(avrule_t * avrule);void append_role_trans (role_trans_rule_t * role_tr_rules);void append_role_allow (role_allow_rule_t * role_allow_rules);void append_range_trans (range_trans_rule_t * range_tr_rules); -void append_filename_trans (filename_trans_rule_t * filename_trans_rules);/* Create a new optional block and add it to the global policy. * During the second pass resolve the block's requirements. Return 0
checkpolicy/policy_define.c +23 -192
260 lines not on this page · raw message
checkpolicy/policy_define.h +1 -2
@@ -28,7 +28,7 @@ int define_default_role(int which);int define_default_type (int which);int define_default_range (int which);int define_common_perms (void ); -int define_compute_type (int which); +int define_compute_type (int which,int has_filename);int define_conditional (cond_expr_t *expr,avrule_t *t_list,avrule_t *f_list );int define_constraint (constraint_expr_t *expr);int define_dominance (void );
@@ -57,7 +57,6 @@ int define_role_trans(int class_specified);int define_role_types (void );int define_role_attr (void );int define_roleattribute (void ); -int define_filename_trans (void );int define_sens (void );int define_te_avtab (int which);int define_te_avtab_extended_perms (int which);
checkpolicy/policy_parse.y +4 -4
@@ -451,13 +451,13 @@ cond_dontaudit_def : DONTAUDIT names names ':' names names ';' ; ; transition_def : TYPE_TRANSITION names names ':' names identifier filename ';' - {if (define_filename_trans()) return -1; } + {if (define_compute_type(AVRULE_TRANSITION, 1)) return -1; } | TYPE_TRANSITION names names ':' names identifier ';' - {if (define_compute_type(AVRULE_TRANSITION)) return -1;} + {if (define_compute_type(AVRULE_TRANSITION, 0)) return -1;} | TYPE_MEMBER names names ':' names identifier ';' - {if (define_compute_type(AVRULE_MEMBER)) return -1;} + {if (define_compute_type(AVRULE_MEMBER, 0)) return -1;} | TYPE_CHANGE names names ':' names identifier ';' - {if (define_compute_type(AVRULE_CHANGE)) return -1;} + {if (define_compute_type(AVRULE_CHANGE, 0)) return -1;} ; range_trans_def : RANGE_TRANSITION names names mls_range_def ';' { if (define_range_trans(0)) return -1; }
checkpolicy/test/dismod.c +2 -23
@@ -50,7 +50,6 @@#define DISPLAY_AVBLOCK_ROLE_ALLOW 4#define DISPLAY_AVBLOCK_REQUIRES 5#define DISPLAY_AVBLOCK_DECLARES 6 -#define DISPLAY_AVBLOCK_FILENAME_TRANS 7static policydb_t policydb;extern unsigned int ss_initialized;
@@ -90,7 +89,6 @@ static struct command { {CMD, 'c', "Display policy capabilities"}, {CMD|NOOPT, 'l', "Link in a module"}, {CMD, 'u', "Display the unknown handling setting"}, - {CMD, 'F', "Display filename_trans rules"}, {HEADER, 0, ""}, {CMD|NOOPT, 'f', "set output file"}, {CMD|NOOPT, 'm', "display menu"},
@@ -339,6 +337,8 @@ static int display_avrule(avrule_t * avrule, policydb_t * policy, policy, fp); }else if (avrule->specified &AVRULE_TYPE ) {display_id (policy, fp,SYM_TYPES , avrule->perms->data -1 ,"" ); +if (avrule->object_name) +fprintf (fp," \"%s\"" , avrule->object_name); }else if (avrule->specified &AVRULE_XPERMS ) {avtab_extended_perms_t xperms;int i;
@@ -556,18 +556,6 @@ static void display_role_allow(role_allow_rule_t * ra, policydb_t * p, FILE * fp } } -static void display_filename_trans (filename_trans_rule_t * tr,policydb_t * p,FILE * fp) -{ -fprintf (fp,"filename transition" ); -for (; tr; tr = tr->next) { -display_type_set (&tr->stypes,0 , p, fp); -display_type_set (&tr->ttypes,0 , p, fp); -display_id (p, fp,SYM_CLASSES , tr->tclass -1 ,":" ); -display_id (p, fp,SYM_TYPES , tr->otype -1 ,"" ); -fprintf (fp," %s\n" , tr->name); - } -} -static int role_display_callback (hashtab_key_t key __attribute__((unused)),hashtab_datum_t datum,void *data) {
@@ -732,10 +720,6 @@ static int display_avdecl(avrule_decl_t * decl, int field, }break ; } -case DISPLAY_AVBLOCK_FILENAME_TRANS : -display_filename_trans (decl->filename_trans_rules, policy, - out_fp); -break ;default :{assert (0 ); }
@@ -1061,11 +1045,6 @@ int main(int argc, char **argv)if (out_fp != stdout)printf ("\nOutput to file: %s\n" , OutfileName);break ; -case 'F' : -fprintf (out_fp,"filename_trans rules:\n" ); -display_avblock (DISPLAY_AVBLOCK_FILENAME_TRANS , - &policydb, out_fp); -break ;case 'l' :link_module (&policydb, out_fp);break ;
libsepol/cil/src/cil_binary.c +2 -0
@@ -4650,6 +4650,7 @@ static avrule_t *__cil_init_sepol_avrule(uint32_t kind, struct cil_tree_node *no__cil_init_sepol_type_set (&avrule->stypes);__cil_init_sepol_type_set (&avrule->ttypes); avrule->perms =NULL ; + avrule->object_name =NULL ; avrule->line = node->line; avrule->source_filename =NULL ;
@@ -4676,6 +4677,7 @@ static void __cil_destroy_sepol_avrules(avrule_t *curr)ebitmap_destroy (&curr->stypes.negset);ebitmap_destroy (&curr->ttypes.types);ebitmap_destroy (&curr->ttypes.negset); +free (curr->object_name);__cil_destroy_sepol_class_perms (curr->perms);free (curr); curr = next;
libsepol/include/sepol/policydb/policydb.h +1 -15
@@ -278,6 +278,7 @@ typedef struct avrule {type_set_t stypes;type_set_t ttypes;class_perm_node_t *perms; +char *object_name;/* optional object name */ av_extended_perms_t *xperms;unsigned long line;/* line number from policy.conf where * this rule originated */
@@ -301,16 +302,6 @@ typedef struct role_allow_rule {struct role_allow_rule *next; } role_allow_rule_t; -typedef struct filename_trans_rule { -uint32_t flags;/* may have RULE_SELF set */ -type_set_t stypes; -type_set_t ttypes; -uint32_t tclass; -char *name; -uint32_t otype;/* new type */ -struct filename_trans_rule *next; -}filename_trans_rule_t ; - typedef struct range_trans_rule { type_set_t stypes; type_set_t ttypes;
@@ -451,9 +442,6 @@ typedef struct avrule_decl {scope_index_t required;/* symbols needed to activate this block */ scope_index_t declared;/* symbols declared within this block */ -/* type transition rules with a 'name' component */ -filename_trans_rule_t *filename_trans_rules; -/* for additive statements (type attribute, roles, and users) */ symtab_t symtab[SYM_NUM ];
@@ -656,8 +644,6 @@ extern void avrule_destroy(avrule_t * x);extern void avrule_list_destroy (avrule_t * x);extern void role_trans_rule_init (role_trans_rule_t * x);extern void role_trans_rule_list_destroy (role_trans_rule_t * x); -extern void filename_trans_rule_init (filename_trans_rule_t * x); -extern void filename_trans_rule_list_destroy (filename_trans_rule_t * x);extern void role_datum_init (role_datum_t * x);extern void role_datum_destroy (role_datum_t * x);
libsepol/src/avrule_block.c +0 -1
@@ -99,7 +99,6 @@ void avrule_decl_destroy(avrule_decl_t * x)cond_list_destroy (x->cond_list);avrule_list_destroy (x->avrules);role_trans_rule_list_destroy (x->role_tr_rules); -filename_trans_rule_list_destroy (x->filename_trans_rules);role_allow_rule_list_destroy (x->role_allow_rules);range_trans_rule_list_destroy (x->range_tr_rules);scope_index_destroy (&x->required);
libsepol/src/expand.c +34 -98
174 lines not on this page · raw message
libsepol/src/link.c +6 -50
74 lines not on this page · raw message
libsepol/src/module_to_cil.c +9 -62
107 lines not on this page · raw message
libsepol/src/policydb.c +20 -50
126 lines not on this page · raw message
libsepol/src/policydb_validate.c +0 -27
@@ -1313,31 +1313,6 @@ bad:return -1 ; } - -static int validate_filename_trans_rules (sepol_handle_t *handle,const filename_trans_rule_t *filename_trans,const policydb_t *p,validate_t flavors[]) -{ -for (; filename_trans; filename_trans = filename_trans->next) { -if (validate_type_set (&filename_trans->stypes, &flavors[SYM_TYPES ])) - goto bad; -if (validate_type_set (&filename_trans->ttypes, &flavors[SYM_TYPES ])) - goto bad; -if (validate_value (filename_trans->tclass,&flavors[SYM_CLASSES ] )) - goto bad; -if (validate_simpletype (filename_trans->otype, p, flavors)) - goto bad; - -/* currently only the RULE_SELF flag can be set */ -if ((filename_trans->flags & ~RULE_SELF ) !=0 ) - goto bad; - } - -return 0 ; - -bad: -ERR (handle,"Invalid filename trans rule list" ); -return -1 ; -} -static int validate_symtabs (sepol_handle_t *handle,const symtab_t symtabs[],validate_t flavors[]) {unsigned int i;
@@ -1372,8 +1347,6 @@ static int validate_avrule_blocks(sepol_handle_t *handle, const avrule_block_t * goto bad;if (validate_scope_index (handle, &decl->declared, flavors)) goto bad; -if (validate_filename_trans_rules (handle, decl->filename_trans_rules, p, flavors)) - goto bad;if (validate_symtabs (handle, decl->symtab, flavors)) goto bad; }
libsepol/src/write.c +52 -34
127 lines not on this page · raw message
Signature · 1 line
2.40.0
PATCH v2 4/8 libsepol: implement new kernel binary format for avtab
In reply to Juraj MarcinImplement a new binary policy format that closely matches the new internal representation introduced in the previous patch.
This patch bumps the maximum kernel policy version and implements reading/writing functions such that kernel binary policy structure matches internal representation.
These changes can cause the binary policy to grow in size due to effectively undoing the benefits of the commit 8206b8cb ("libsepol:
implement POLICYDB_VERSION_COMP_FTRANS "), but this will be mitigated by adding the prefix/suffix support as described in the previous patch.
--- libsepol/include/sepol/policydb/policydb.h | 3 +- libsepol/src/avtab.c | 88 ++++++++++++++++++++-- libsepol/src/policydb.c | 8 ++ libsepol/src/write.c | 86 +++++++++++++++++---- 4 files changed, 164 insertions(+), 21 deletions(-)
libsepol/include/sepol/policydb/policydb.h +2 -1
@@ -722,10 +722,11 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define POLICYDB_VERSION_INFINIBAND 31/* Linux-specific */ #define POLICYDB_VERSION_GLBLUB 32#define POLICYDB_VERSION_COMP_FTRANS 33/* compressed filename transitions */ +#define POLICYDB_VERSION_AVTAB_FTRANS 34/* filename transitions moved to avtab */ /* Range of policy versions we understand*/ #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE -#define POLICYDB_VERSION_MAX POLICYDB_VERSION_COMP_FTRANS +#define POLICYDB_VERSION_MAX POLICYDB_VERSION_AVTAB_FTRANS/* Module versions and specific changes*/ #define MOD_POLICYDB_VERSION_BASE 4
libsepol/src/avtab.c +83 -5
101 lines not on this page · raw message
libsepol/src/policydb.c +8 -0
@@ -208,6 +208,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_KERN, + .version = POLICYDB_VERSION_AVTAB_FTRANS, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_BASE, .version = MOD_POLICYDB_VERSION_BASE,
@@ -4099,6 +4106,7 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)if (role_allow_read (&p->role_allow, fp)) goto bad;if (r_policyvers >=POLICYDB_VERSION_FILENAME_TRANS && + r_policyvers <POLICYDB_VERSION_AVTAB_FTRANS &&avtab_filename_trans_read (fp, r_policyvers, &p->te_avtab)) goto bad; }else {
libsepol/src/write.c +71 -15
116 lines not on this page · raw message
Signature · 1 line
2.40.0
PATCH v2 5/8 libsepol: implement new module binary format of avrule
In reply to Juraj MarcinImplement a new module policy format that closely matches the new internal representation of avrule introduced in the previous patch.
This patch bumps the maximum module policy version and implements reading/writing functions such that the module binary policy structure matches its internal representation, namely, the object name attribute used for the filename transition rules.
These changes have no significant effect on the size of the module policy file (tested with Fedora policy).
--- libsepol/include/sepol/policydb/policydb.h | 3 ++- libsepol/src/policydb.c | 28 ++++++++++++++++++++++ libsepol/src/write.c | 24 ++++++++++++++++--- 3 files changed, 51 insertions(+), 4 deletions(-)
libsepol/include/sepol/policydb/policydb.h +2 -1
@@ -749,9 +749,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define MOD_POLICYDB_VERSION_INFINIBAND 19#define MOD_POLICYDB_VERSION_GLBLUB 20#define MOD_POLICYDB_VERSION_SELF_TYPETRANS 21 +#define MOD_POLICYDB_VERSION_AVRULE_FTRANS 22#define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE -#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_SELF_TYPETRANS +#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_AVRULE_FTRANS#define POLICYDB_CONFIG_MLS 1
libsepol/src/policydb.c +28 -0
@@ -341,6 +341,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_BASE, + .version = MOD_POLICYDB_VERSION_AVRULE_FTRANS, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_MOD, .version = MOD_POLICYDB_VERSION_BASE,
@@ -467,6 +474,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = 0, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_MOD, + .version = MOD_POLICYDB_VERSION_AVRULE_FTRANS, + .sym_num = SYM_NUM, + .ocon_num = 0, + .target_platform = SEPOL_TARGET_SELINUX, + }, };#if 0
@@ -3202,6 +3216,19 @@ static avrule_t *avrule_read(policydb_t * p, struct policy_file *fp) tail = cur; } +if (p->policyvers >=MOD_POLICYDB_VERSION_AVRULE_FTRANS && + avrule->specified &AVRULE_TRANSITION ) { + rc =next_entry (buf, fp,sizeof (uint32_t )); +if (rc <0 ) + goto bad; + len =le32_to_cpu (*buf); +if (len) { + rc =str_read (&avrule->object_name, fp, len); +if (rc <0 ) + goto bad; + } + } +if (avrule->specified &AVRULE_XPERMS ) {uint8_t buf8;size_t nel =ARRAY_SIZE (avrule->xperms->perms);
@@ -3633,6 +3660,7 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl, }if (p->policyvers >=MOD_POLICYDB_VERSION_FILENAME_TRANS && + p->policyvers <MOD_POLICYDB_VERSION_AVRULE_FTRANS &&filename_trans_rule_read (p, &decl->avrules, fp))return -1 ;
libsepol/src/write.c +21 -3
@@ -2025,8 +2025,9 @@ static int avrule_write(policydb_t *p, avrule_t * avrule,uint32_t buf[32 ], len;class_perm_node_t *cur; -/* skip filename transitions for now */ -if (avrule->specified &AVRULE_TRANSITION && avrule->object_name) +/* skip filename transitions if writing older version without name */ +if (p->policyvers <MOD_POLICYDB_VERSION_AVRULE_FTRANS && + avrule->specified &AVRULE_TRANSITION && avrule->object_name)return POLICYDB_SUCCESS ; if (p->policyvers < MOD_POLICYDB_VERSION_SELF_TYPETRANS &&
@@ -2073,6 +2074,21 @@ static int avrule_write(policydb_t *p, avrule_t * avrule, cur = cur->next; } +if (p->policyvers >=MOD_POLICYDB_VERSION_AVRULE_FTRANS && + avrule->specified &AVRULE_TRANSITION ) { + len = avrule->object_name ?strlen (avrule->object_name) :0 ; + *buf =cpu_to_le32 (len); + items =put_entry (buf,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; +if (avrule->object_name) { + items =put_entry (avrule->object_name,sizeof (char ), + len, fp); +if (items != len) +return POLICYDB_ERROR ; + } + } +if (avrule->specified &AVRULE_XPERMS ) {size_t nel =ARRAY_SIZE (avrule->xperms->perms);uint32_t buf32[nel];
@@ -2122,7 +2138,8 @@ static int avrule_write_list(policydb_t *p, avrule_t * avrules, avrule = avrules; len =0 ;while (avrule) { -if (!(avrule->specified &AVRULE_TRANSITION && +if (p->policyvers >=MOD_POLICYDB_VERSION_AVRULE_FTRANS || + !(avrule->specified &AVRULE_TRANSITION && avrule->object_name)) len++; avrule = avrule->next;
@@ -2357,6 +2374,7 @@ static int avrule_decl_write(avrule_decl_t * decl, int num_scope_syms, }if (p->policyvers >=MOD_POLICYDB_VERSION_FILENAME_TRANS && + p->policyvers <MOD_POLICYDB_VERSION_AVRULE_FTRANS &&filename_trans_rule_write (p, decl->avrules, fp))return POLICYDB_ERROR ;
Signature · 1 line
2.40.0
PATCH v2 6/8 checkpolicy,libsepol: add prefix/suffix support to kernel policy
In reply to Juraj MarcinCurrently, filename type transitions support only exact name matching. However, in practice, the names contain variable parts. This leads to many duplicated rules in the policy that differ only in the part of the name, or it is even impossible to cover all possible combinations.
This patch extends the filename type transitions structures to include new types of filename transitions - prefix and suffix filename transitions. It also implements the reading and writing of those rules in the kernel binary policy format together with increasing its version.
--- checkpolicy/test/dispol.c | 25 ++++- libsepol/include/sepol/policydb/avtab.h | 2 + libsepol/include/sepol/policydb/policydb.h | 9 +- libsepol/src/avtab.c | 13 +++ libsepol/src/kernel_to_cil.c | 30 +++++- libsepol/src/kernel_to_common.h | 1 + libsepol/src/kernel_to_conf.c | 30 +++++- libsepol/src/policydb.c | 7 ++ libsepol/src/policydb_validate.c | 11 +- libsepol/src/write.c | 113 +++++++++++++++++---- 10 files changed, 211 insertions(+), 30 deletions(-)
checkpolicy/test/dispol.c +24 -1
@@ -129,6 +129,7 @@ typedef struct {avtab_key_t *key;policydb_t *p;FILE *fp; +name_trans_match_t match; } render_name_trans_args_t;static int render_name_trans_helper (hashtab_key_t k,hashtab_datum_t d,void *a)
@@ -140,7 +141,22 @@ static int render_name_trans_helper(hashtab_key_t k, hashtab_datum_t d, void *a)fprintf (args->fp,"type_transition " );render_key (args->key, args->p, args->fp);render_type (*otype, args->p, args->fp); -fprintf (args->fp," \"%s\";\n" , name); +const char *match_str ="" ; +switch (args->match) { +case NAME_TRANS_MATCH_EXACT : + match_str ="" ; +break ; +case NAME_TRANS_MATCH_PREFIX : + match_str =" PREFIX" ; +break ; +case NAME_TRANS_MATCH_SUFFIX : + match_str =" SUFFIX" ; +break ; +default : +fprintf (args->fp," ERROR: no valid name match type specified\n" ); +return -1 ; + } +fprintf (args->fp," \"%s\"%s;\n" , name, match_str);return 0 ; }
@@ -207,9 +223,16 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t wha .key = key, .p = p, .fp = fp, + .match = NAME_TRANS_MATCH_EXACT, };hashtab_map (datum->trans->name_trans.table, render_name_trans_helper, &args); + args.match =NAME_TRANS_MATCH_PREFIX ; +hashtab_map (datum->trans->prefix_trans.table, + render_name_trans_helper, &args); + args.match =NAME_TRANS_MATCH_SUFFIX ; +hashtab_map (datum->trans->suffix_trans.table, + render_name_trans_helper, &args); }if (key->specified &AVTAB_MEMBER ) {fprintf (fp,"type_member " );
libsepol/include/sepol/policydb/avtab.h +2 -0
@@ -74,6 +74,8 @@ typedef struct avtab_key {typedef struct avtab_trans {uint32_t otype;/* resulting type of the new object */ symtab_t name_trans;/* filename transitions */ +symtab_t prefix_trans;/* prefix filename transitions */ +symtab_t suffix_trans;/* prefix filename transitions */ }avtab_trans_t ; typedef struct avtab_extended_perms {
libsepol/include/sepol/policydb/policydb.h +8 -1
@@ -252,6 +252,12 @@ typedef struct av_extended_perms {uint32_t perms[EXTENDED_PERMS_LEN ]; } av_extended_perms_t; +typedef enum name_trans_match { +NAME_TRANS_MATCH_EXACT , +NAME_TRANS_MATCH_PREFIX , +NAME_TRANS_MATCH_SUFFIX , +}name_trans_match_t ; + typedef struct avrule { /* these typedefs are almost exactly the same as those in avtab.h - they are * here because of the need to include neverallow and dontaudit messages */
@@ -723,10 +729,11 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define POLICYDB_VERSION_GLBLUB 32#define POLICYDB_VERSION_COMP_FTRANS 33/* compressed filename transitions */ #define POLICYDB_VERSION_AVTAB_FTRANS 34/* filename transitions moved to avtab */ +#define POLICYDB_VERSION_PREFIX_SUFFIX 35/* prefix/suffix support for filename transitions */ /* Range of policy versions we understand*/ #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE -#define POLICYDB_VERSION_MAX POLICYDB_VERSION_AVTAB_FTRANS +#define POLICYDB_VERSION_MAX POLICYDB_VERSION_PREFIX_SUFFIX/* Module versions and specific changes*/ #define MOD_POLICYDB_VERSION_BASE 4
libsepol/src/avtab.c +13 -0
@@ -327,6 +327,10 @@ void avtab_trans_destroy(avtab_trans_t *trans) {hashtab_map (trans->name_trans.table, avtab_trans_destroy_helper,NULL );symtab_destroy (&trans->name_trans); +hashtab_map (trans->prefix_trans.table, avtab_trans_destroy_helper,NULL ); +symtab_destroy (&trans->prefix_trans); +hashtab_map (trans->suffix_trans.table, avtab_trans_destroy_helper,NULL ); +symtab_destroy (&trans->suffix_trans); }void avtab_destroy (avtab_t * h)
@@ -520,6 +524,15 @@ static int avtab_trans_read(policy_file_t *fp, uint32_t vers,if (rc <0 ) goto bad; +if (vers >=POLICYDB_VERSION_PREFIX_SUFFIX ) { + rc =avtab_read_name_trans (fp, &trans->prefix_trans); +if (rc <0 ) + goto bad; + rc =avtab_read_name_trans (fp, &trans->suffix_trans); +if (rc <0 ) + goto bad; + } +return SEPOL_OK ; bad:
libsepol/src/kernel_to_cil.c +28 -2
@@ -1707,9 +1707,24 @@ static int name_trans_to_strs_helper(hashtab_key_t k, hashtab_datum_t d, void *achar *name = k;uint32_t *otype = d;name_trans_to_strs_args_t *args = a; -return strs_create_and_add (args->strs,"(%s %s %s %s \"%s\" %s)" ,6 , +const char *match_str ="" ; +switch (args->match) { +case NAME_TRANS_MATCH_EXACT : + match_str ="" ; +break ; +case NAME_TRANS_MATCH_PREFIX : + match_str =" prefix" ; +break ; +case NAME_TRANS_MATCH_SUFFIX : + match_str =" suffix" ; +break ; +default : +ERR (NULL ,"Unknown name match type: %" PRIu8, args->match); +return SEPOL_ERR ; + } +return strs_create_and_add (args->strs,"(%s %s %s %s \"%s\"%s %s)" ,7 , args->flavor, args->src, args->tgt, - args->class, name, + args->class, name, match_str, args->pdb->p_type_val_to_name[*otype -1 ]); }
@@ -1797,9 +1812,20 @@ static int avtab_node_to_strs(struct policydb *pdb, avtab_key_t *key, avtab_datu .src = src, .tgt = tgt, .class = class, + .match = NAME_TRANS_MATCH_EXACT, }; rc =hashtab_map (datum->trans->name_trans.table, name_trans_to_strs_helper, &args); +if (rc <0 ) +return rc; + args.match =NAME_TRANS_MATCH_PREFIX ; + rc =hashtab_map (datum->trans->prefix_trans.table, + name_trans_to_strs_helper, &args); +if (rc <0 ) +return rc; + args.match =NAME_TRANS_MATCH_SUFFIX ; + rc =hashtab_map (datum->trans->suffix_trans.table, + name_trans_to_strs_helper, &args); }else { new = pdb->p_type_val_to_name[data -1 ];
libsepol/src/kernel_to_common.h +1 -0
@@ -90,6 +90,7 @@ typedef struct {const char *src;const char *tgt;const char *class; +name_trans_match_t match; } name_trans_to_strs_args_t;void sepol_indent (FILE *out,int indent);
libsepol/src/kernel_to_conf.c +28 -2
@@ -1685,11 +1685,26 @@ static int name_trans_to_strs_helper(hashtab_key_t k, hashtab_datum_t d, void *achar *name = k;uint32_t *otype = d;name_trans_to_strs_args_t *args = a; -return strs_create_and_add (args->strs,"%s %s %s:%s %s \"%s\";" ,6 , +const char *match_str ="" ; +switch (args->match) { +case NAME_TRANS_MATCH_EXACT : + match_str ="" ; +break ; +case NAME_TRANS_MATCH_PREFIX : + match_str =" PREFIX" ; +break ; +case NAME_TRANS_MATCH_SUFFIX : + match_str =" SUFFIX" ; +break ; +default : +ERR (NULL ,"Unknown name match type: %" PRIu8, args->match); +return SEPOL_ERR ; + } +return strs_create_and_add (args->strs,"%s %s %s:%s %s \"%s\"%s;" ,7 , args->flavor, args->src, args->tgt, args->class, args->pdb->p_type_val_to_name[*otype -1 ], - name); + name, match_str); }static int avtab_node_to_strs (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum,struct strs *strs)
@@ -1773,9 +1788,20 @@ static int avtab_node_to_strs(struct policydb *pdb, avtab_key_t *key, avtab_datu .src = src, .tgt = tgt, .class = class, + .match = NAME_TRANS_MATCH_EXACT, }; rc =hashtab_map (datum->trans->name_trans.table, name_trans_to_strs_helper, &args); +if (rc <0 ) +return rc; + args.match =NAME_TRANS_MATCH_PREFIX ; + rc =hashtab_map (datum->trans->prefix_trans.table, + name_trans_to_strs_helper, &args); +if (rc <0 ) +return rc; + args.match =NAME_TRANS_MATCH_SUFFIX ; + rc =hashtab_map (datum->trans->suffix_trans.table, + name_trans_to_strs_helper, &args); }else { new = pdb->p_type_val_to_name[data -1 ];
libsepol/src/policydb.c +7 -0
@@ -215,6 +215,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_KERN, + .version = POLICYDB_VERSION_PREFIX_SUFFIX, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_BASE, .version = MOD_POLICYDB_VERSION_BASE,
libsepol/src/policydb_validate.c +9 -2
@@ -855,11 +855,18 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void */* also each transition must be non empty */ if (!d->trans->otype && - !hashtab_nel (d->trans->name_trans.table)) + !hashtab_nel (d->trans->name_trans.table) && + !hashtab_nel (d->trans->name_trans.table) && + !hashtab_nel (d->trans->prefix_trans.table) && + !hashtab_nel (d->trans->suffix_trans.table))return -1 ; -/* and each filename transition must be also valid */ +/* and each name transition must be also valid */ if (hashtab_map (d->trans->name_trans.table, + validate_name_trans_helper, margs) || +hashtab_map (d->trans->prefix_trans.table, + validate_name_trans_helper, margs) || +hashtab_map (d->trans->suffix_trans.table, validate_name_trans_helper, margs))return -1 ; }else if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) {
libsepol/src/write.c +91 -22
150 lines not on this page · raw message
Signature · 1 line
2.40.0
PATCH v2 7/8 checkpolicy,libsepol: add prefix/suffix support to module policy
In reply to Juraj MarcinThis patch extends the structures for module and base policy (avrule_t) to support prefix/suffix transitions. In addition to this, it implements the necessary changes to functions for reading and writing the binary policy, as well as parsing the policy conf.
Syntax of the new prefix/suffix filename transition rule:
type_transition source_type target_type : class default_type object_name match_type;
where match_type is either keyword "prefix" or "suffix"
Examples:
type_transition ta tb:CLASS01 tc "file01" prefix;
type_transition td te:CLASS01 tf "file02" suffix;--- checkpolicy/policy_define.c | 13 ++++--- checkpolicy/policy_define.h | 2 +- checkpolicy/policy_parse.y | 13 ++++--- checkpolicy/policy_scan.l | 4 +++ checkpolicy/test/dismod.c | 14 ++++++++ checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 4 ++- libsepol/include/sepol/policydb/avtab.h | 1 + libsepol/include/sepol/policydb/policydb.h | 13 ++++--- libsepol/src/avtab.c | 30 ++++++++++++---- libsepol/src/expand.c | 6 +++- libsepol/src/kernel_to_common.h | 2 +- libsepol/src/link.c | 1 + libsepol/src/module_to_cil.c | 25 +++++++++++--- libsepol/src/policydb.c | 23 ++++++++++++- libsepol/src/write.c | 40 ++++++++++++++++------ 16 files changed, 150 insertions(+), 43 deletions(-)
checkpolicy/policy_define.c +9 -4
@@ -1601,7 +1601,8 @@ static int set_types(type_set_t * set, char *id, int *add, char starallowed)return -1 ; } -static int define_compute_type_helper (int which,avrule_t ** rule,int has_filename) +static int define_compute_type_helper (int which,avrule_t ** rule, +int has_filename,uint8_t name_match) {char *id;type_datum_t *datum;
@@ -1676,6 +1677,7 @@ static int define_compute_type_helper(int which, avrule_t ** rule, int has_filen goto bad; } } + avrule->name_match = name_match;ebitmap_for_each_positive_bit (&tclasses, node, i) { perm =malloc (sizeof (class_perm_node_t));
@@ -1700,7 +1702,7 @@ static int define_compute_type_helper(int which, avrule_t ** rule, int has_filenreturn -1 ; } -int define_compute_type (int which,int has_filename) +int define_compute_type (int which,int has_filename,uint8_t name_match) {char *id;avrule_t *avrule;
@@ -1721,7 +1723,8 @@ int define_compute_type(int which, int has_filename)return 0 ; } -if (define_compute_type_helper (which, &avrule, has_filename)) +if (define_compute_type_helper (which, &avrule, has_filename, + name_match))return -1 ;append_avrule (avrule);
@@ -1745,7 +1748,8 @@ avrule_t *define_cond_compute_type(int which)return (avrule_t *)1 ; } -if (define_compute_type_helper (which, &avrule,0 )) +if (define_compute_type_helper (which, &avrule,0 , +NAME_TRANS_MATCH_EXACT ))return COND_ERR ;return avrule;
@@ -2394,6 +2398,7 @@ static int avrule_cpy(avrule_t *dest, const avrule_t *src)return -1 ; } } + dest->name_match = src->name_match; dest->line = src->line; dest->source_filename =strdup (source_file);if (!dest->source_filename) {
checkpolicy/policy_define.h +1 -1
@@ -28,7 +28,7 @@ int define_default_role(int which);int define_default_type (int which);int define_default_range (int which);int define_common_perms (void ); -int define_compute_type (int which,int has_filename); +int define_compute_type (int which,int has_filename,uint8_t name_match);int define_conditional (cond_expr_t *expr,avrule_t *t_list,avrule_t *f_list );int define_constraint (constraint_expr_t *expr);int define_dominance (void );
checkpolicy/policy_parse.y +9 -4
@@ -108,6 +108,7 @@ typedef int (* require_func_t)(int pass); %token IF %token ELSE %token TYPE_TRANSITION +%token PREFIX SUFFIX %token TYPE_MEMBER %token TYPE_CHANGE %token ROLE_TRANSITION
@@ -451,13 +452,17 @@ cond_dontaudit_def : DONTAUDIT names names ':' names names ';' ; ; transition_def : TYPE_TRANSITION names names ':' names identifier filename ';' - {if (define_compute_type(AVRULE_TRANSITION, 1)) return -1; } + {if (define_compute_type(AVRULE_TRANSITION, 1, NAME_TRANS_MATCH_EXACT)) return -1;} + | TYPE_TRANSITION names names ':' names identifier filename PREFIX ';' + {if (define_compute_type(AVRULE_TRANSITION, 1, NAME_TRANS_MATCH_PREFIX)) return -1;} + | TYPE_TRANSITION names names ':' names identifier filename SUFFIX ';' + {if (define_compute_type(AVRULE_TRANSITION, 1, NAME_TRANS_MATCH_SUFFIX)) return -1;} | TYPE_TRANSITION names names ':' names identifier ';' - {if (define_compute_type(AVRULE_TRANSITION, 0)) return -1;} + {if (define_compute_type(AVRULE_TRANSITION, 0, NAME_TRANS_MATCH_EXACT)) return -1;} | TYPE_MEMBER names names ':' names identifier ';' - {if (define_compute_type(AVRULE_MEMBER, 0)) return -1;} + {if (define_compute_type(AVRULE_MEMBER, 0, NAME_TRANS_MATCH_EXACT)) return -1;} | TYPE_CHANGE names names ':' names identifier ';' - {if (define_compute_type(AVRULE_CHANGE, 0)) return -1;} + {if (define_compute_type(AVRULE_CHANGE, 0, NAME_TRANS_MATCH_EXACT)) return -1;} ; range_trans_def : RANGE_TRANSITION names names mls_range_def ';' { if (define_range_trans(0)) return -1; }
checkpolicy/policy_scan.l +4 -0
@@ -123,6 +123,10 @@ EXPANDATTRIBUTE | expandattribute { return(EXPANDATTRIBUTE); } TYPE_TRANSITION | type_transition { return(TYPE_TRANSITION); } +PREFIX | +prefix { return(PREFIX); } +SUFFIX | +suffix { return(SUFFIX); } TYPE_MEMBER | type_member { return(TYPE_MEMBER); } TYPE_CHANGE |
checkpolicy/test/dismod.c +14 -0
@@ -339,6 +339,20 @@ static int display_avrule(avrule_t * avrule, policydb_t * policy,display_id (policy, fp,SYM_TYPES , avrule->perms->data -1 ,"" );if (avrule->object_name)fprintf (fp," \"%s\"" , avrule->object_name); +switch (avrule->name_match) { +case NAME_TRANS_MATCH_EXACT : +/* do nothing */ +break ; +case NAME_TRANS_MATCH_PREFIX : +fprintf (fp," PREFIX" ); +break ; +case NAME_TRANS_MATCH_SUFFIX : +fprintf (fp," SUFFIX" ); +break ; +default : +fprintf (fp," ERROR: no valid name match type specified\n" ); +return -1 ; + } }else if (avrule->specified &AVRULE_XPERMS ) {avtab_extended_perms_t xperms;int i;
checkpolicy/test/dispol.c +1 -1
@@ -129,7 +129,7 @@ typedef struct {avtab_key_t *key;policydb_t *p;FILE *fp; -name_trans_match_t match; +uint8_t match; } render_name_trans_args_t;static int render_name_trans_helper (hashtab_key_t k,hashtab_datum_t d,void *a)
libsepol/cil/src/cil_binary.c +3 -1
@@ -1211,7 +1211,8 @@ static int __cil_typetransition_to_avtab_helper(policydb_t *pdb, avt_key.target_type = sepol_tgt->s.value; avt_key.target_class = sepol_obj->s.value; rc =avtab_insert_filename_trans (&pdb->te_avtab, &avt_key, - sepol_result->s.value, name, &otype); + sepol_result->s.value, name,NAME_TRANS_MATCH_EXACT , + &otype);if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) {if (sepol_result->s.value!= otype) {
@@ -4651,6 +4652,7 @@ static avrule_t *__cil_init_sepol_avrule(uint32_t kind, struct cil_tree_node *no__cil_init_sepol_type_set (&avrule->ttypes); avrule->perms =NULL ; avrule->object_name =NULL ; + avrule->name_match =NAME_TRANS_MATCH_EXACT ; avrule->line = node->line; avrule->source_filename =NULL ;
libsepol/include/sepol/policydb/avtab.h +1 -0
@@ -156,6 +156,7 @@ extern avtab_ptr_t avtab_search_node_next(avtab_ptr_t node, int specified);extern int avtab_insert_filename_trans (avtab_t *a,avtab_key_t *key,uint32_t otype,const char *name, +uint8_t name_match,uint32_t *present_otype);extern int avtab_filename_trans_read (void *fp,uint32_t vers,avtab_t *a);
libsepol/include/sepol/policydb/policydb.h +6 -7
@@ -252,12 +252,6 @@ typedef struct av_extended_perms {uint32_t perms[EXTENDED_PERMS_LEN ]; } av_extended_perms_t; -typedef enum name_trans_match { -NAME_TRANS_MATCH_EXACT , -NAME_TRANS_MATCH_PREFIX , -NAME_TRANS_MATCH_SUFFIX , -}name_trans_match_t ; - typedef struct avrule { /* these typedefs are almost exactly the same as those in avtab.h - they are * here because of the need to include neverallow and dontaudit messages */
@@ -285,6 +279,10 @@ typedef struct avrule {type_set_t ttypes;class_perm_node_t *perms;char *object_name;/* optional object name */ +#define NAME_TRANS_MATCH_EXACT 0 +#define NAME_TRANS_MATCH_PREFIX 1 +#define NAME_TRANS_MATCH_SUFFIX 2 +uint8_t name_match;av_extended_perms_t *xperms;unsigned long line;/* line number from policy.conf where * this rule originated */
@@ -757,9 +755,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define MOD_POLICYDB_VERSION_GLBLUB 20#define MOD_POLICYDB_VERSION_SELF_TYPETRANS 21#define MOD_POLICYDB_VERSION_AVRULE_FTRANS 22 +#define MOD_POLICYDB_VERSION_PREFIX_SUFFIX 23/* preffix/suffix support for filename transitions */ #define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE -#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_AVRULE_FTRANS +#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_PREFIX_SUFFIX#define POLICYDB_CONFIG_MLS 1
libsepol/src/avtab.c +23 -7
@@ -771,7 +771,7 @@ int avtab_read(avtab_t * a, struct policy_file *fp, uint32_t vers)int avtab_insert_filename_trans (avtab_t *a,avtab_key_t *key,uint32_t otype,const char *name, -uint32_t *present_otype) +uint8_t name_match,uint32_t *present_otype) {int rc =SEPOL_ENOMEM ;avtab_trans_t new_trans = {0 };
@@ -780,6 +780,7 @@ int avtab_insert_filename_trans(avtab_t *a, avtab_key_t *key,avtab_ptr_t node;char *name_key =NULL ;uint32_t *otype_datum =NULL ; +symtab_t *target_symtab; datum =avtab_search (a, key);if (!datum) {
@@ -793,8 +794,22 @@ int avtab_insert_filename_trans(avtab_t *a, avtab_key_t *key, datum = &node->datum; } -if (!datum->trans->name_trans.table) { - rc =symtab_init (&datum->trans->name_trans,1 <<8 ); +switch (name_match) { +case NAME_TRANS_MATCH_EXACT : + target_symtab = &datum->trans->name_trans; +break ; +case NAME_TRANS_MATCH_PREFIX : + target_symtab = &datum->trans->prefix_trans; +break ; +case NAME_TRANS_MATCH_SUFFIX : + target_symtab = &datum->trans->suffix_trans; +break ; +default : +return SEPOL_ERR ; + } + +if (!target_symtab->table) { + rc =symtab_init (target_symtab,1 <<8 );if (rc <0 )return rc; }
@@ -810,8 +825,7 @@ int avtab_insert_filename_trans(avtab_t *a, avtab_key_t *key, goto bad; *otype_datum = otype; - rc =hashtab_insert (datum->trans->name_trans.table, name_key, - otype_datum); + rc =hashtab_insert (target_symtab->table, name_key, otype_datum);if (rc <0 ) goto bad;
@@ -856,7 +870,8 @@ static int filename_trans_read_one(avtab_t *a, void *fp) key.target_class =le32_to_cpu (buf[2 ]); otype =le32_to_cpu (buf[3 ]); - rc =avtab_insert_filename_trans (a, &key, otype, name,NULL ); + rc =avtab_insert_filename_trans (a, &key, otype, name, +NAME_TRANS_MATCH_EXACT ,NULL );if (rc) goto err;
@@ -909,7 +924,8 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp) key.source_type = bit +1 ; rc =avtab_insert_filename_trans (a, &key, otype, name, -NULL ); +NAME_TRANS_MATCH_EXACT , +NULL );if (rc <0 ) goto err_ebitmap; }
libsepol/src/expand.c +5 -1
@@ -1620,7 +1620,8 @@ static int expand_terule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond, cond_av_list_t ** other, uint32_t stype, uint32_t ttype, class_perm_node_t * perms, - char *object_name, avtab_t * avtab, int enabled) + char *object_name, uint8_t name_match, + avtab_t * avtab, int enabled) { avtab_key_t avkey;avtab_datum_t *avdatump;
@@ -1652,6 +1653,7 @@ static int expand_terule_helper(sepol_handle_t * handle,int rc =avtab_insert_filename_trans (avtab, &avkey, remapped_data, object_name, + name_match, &oldtype);if (rc ==SEPOL_EEXIST ) { ERR(handle, "conflicting filename transition %s %s:%s \"%s\": %s vs %s",
@@ -1885,6 +1887,7 @@ static int expand_rule_helper(sepol_handle_t * handle, source_rule->specified, cond, other, i, i, source_rule->perms, source_rule->object_name, + source_rule->name_match, dest_avtab, enabled);if (retval !=EXPAND_RULE_SUCCESS )return retval;
@@ -1902,6 +1905,7 @@ static int expand_rule_helper(sepol_handle_t * handle, source_rule->specified, cond, other, i, j, source_rule->perms, source_rule->object_name, + source_rule->name_match, dest_avtab, enabled);if (retval !=EXPAND_RULE_SUCCESS )return retval;
libsepol/src/kernel_to_common.h +1 -1
@@ -90,7 +90,7 @@ typedef struct {const char *src;const char *tgt;const char *class; -name_trans_match_t match; +uint8_t match; } name_trans_to_strs_args_t;void sepol_indent (FILE *out,int indent);
libsepol/src/link.c +1 -0
@@ -1254,6 +1254,7 @@ static int copy_avrule_list(avrule_t * list, avrule_t ** dst,if (!new_rule->object_name) goto cleanup; } + new_rule->name_match = cur->name_match; cur_perm = cur->perms; tail_perm =NULL ;
libsepol/src/module_to_cil.c +20 -5
@@ -547,12 +547,13 @@ static int semantic_level_to_cil(struct policydb *pdb, int sens_offset, struct mreturn 0 ; } -static int avrule_to_cil (int indent,struct policydb *pdb,uint32_t type,const char *src,const char *tgt,const char *object_name,const struct class_perm_node *classperms) +static int avrule_to_cil (int indent,struct policydb *pdb,uint32_t type,const char *src,const char *tgt,const char *object_name,uint8_t name_match,const struct class_perm_node *classperms) {int rc =-1 ;const char *rule;const struct class_perm_node *classperm;char *perms; +const char *match_str ="" ;switch (type) {case AVRULE_ALLOWED :
@@ -598,10 +599,24 @@ static int avrule_to_cil(int indent, struct policydb *pdb, uint32_t type, const pdb->p_class_val_to_name[classperm->tclass -1 ], perms + 1); }else if (object_name) { -cil_println (indent,"(%s %s %s %s \"%s\" %s)" , +switch (name_match) { +case NAME_TRANS_MATCH_EXACT : + match_str ="" ; +break ; +case NAME_TRANS_MATCH_PREFIX : + match_str =" prefix" ; +break ; +case NAME_TRANS_MATCH_SUFFIX : + match_str =" suffix" ; +break ; +default : +ERR (NULL ,"Unknown name match type: %" PRIu8, + name_match); + } +cil_println (indent,"(%s %s %s %s \"%s\"%s %s)" , rule, src, tgt, pdb->p_class_val_to_name[classperm->tclass -1 ], - object_name, + object_name, match_str, pdb->p_type_val_to_name[classperm->data -1 ]); }else { cil_println(indent, "(%s %s %s %s %s)",
@@ -1205,7 +1220,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *aif (avrule->specified &AVRULE_XPERMS ) { rc =avrulex_to_cil (indent, pdb, avrule->specified, snames[s], tnames[t], avrule->perms, avrule->xperms); }else { - rc =avrule_to_cil (indent, pdb, avrule->specified, snames[s], tnames[t], avrule->object_name, avrule->perms); + rc =avrule_to_cil (indent, pdb, avrule->specified, snames[s], tnames[t], avrule->object_name, avrule->name_match, avrule->perms); }if (rc !=0 ) { goto exit;
@@ -1216,7 +1231,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *aif (avrule->specified &AVRULE_XPERMS ) { rc =avrulex_to_cil (indent, pdb, avrule->specified, snames[s],"self" , avrule->perms, avrule->xperms); }else { - rc =avrule_to_cil (indent, pdb, avrule->specified, snames[s],"self" , avrule->object_name, avrule->perms); + rc =avrule_to_cil (indent, pdb, avrule->specified, snames[s],"self" , avrule->object_name, avrule->name_match, avrule->perms); }if (rc !=0 ) { goto exit;
libsepol/src/policydb.c +22 -1
@@ -355,6 +355,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_BASE, + .version = MOD_POLICYDB_VERSION_PREFIX_SUFFIX, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_MOD, .version = MOD_POLICYDB_VERSION_BASE,
@@ -488,6 +495,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = 0, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_MOD, + .version = MOD_POLICYDB_VERSION_PREFIX_SUFFIX, + .sym_num = SYM_NUM, + .ocon_num = 0, + .target_platform = SEPOL_TARGET_SELINUX, + }, };#if 0
@@ -3171,6 +3185,7 @@ common_read, class_read, role_read, type_read, user_read,static avrule_t *avrule_read (policydb_t * p,struct policy_file *fp) {unsigned int i; +uint8_t buf8;uint32_t buf[2 ], len;class_perm_node_t *cur, *tail =NULL ;avrule_t *avrule;
@@ -3234,10 +3249,15 @@ static avrule_t *avrule_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto bad; } +if (p->policyvers >=MOD_POLICYDB_VERSION_PREFIX_SUFFIX ) { + rc =next_entry (&buf8, fp,sizeof (uint8_t )); +if (rc <0 ) + goto bad; + avrule->name_match = buf8; + } }if (avrule->specified &AVRULE_XPERMS ) { -uint8_t buf8;size_t nel =ARRAY_SIZE (avrule->xperms->perms);uint32_t buf32[nel];
@@ -3546,6 +3566,7 @@ static int filename_trans_rule_read(policydb_t *p, avrule_t **r, rc =str_read (&cur->object_name, fp, len);if (rc)return -1 ; + cur->name_match =NAME_TRANS_MATCH_EXACT ;if (type_set_read (&cur->stypes, fp))return -1 ;
libsepol/src/write.c +30 -10
@@ -2071,6 +2071,7 @@ static int avrule_write(policydb_t *p, avrule_t * avrule, struct policy_file *fp) {size_t items, items2; +uint8_t buf8;uint32_t buf[32 ], len;class_perm_node_t *cur;
@@ -2078,6 +2079,11 @@ static int avrule_write(policydb_t *p, avrule_t * avrule,if (p->policyvers <MOD_POLICYDB_VERSION_AVRULE_FTRANS && avrule->specified &AVRULE_TRANSITION && avrule->object_name)return POLICYDB_SUCCESS ; +/* skip prefix/suffix name transition if writing older version */ +if (p->policyvers <MOD_POLICYDB_VERSION_PREFIX_SUFFIX && + avrule->specified &AVRULE_TRANSITION && + avrule->object_name && avrule->name_match !=NAME_TRANS_MATCH_EXACT ) +return POLICYDB_SUCCESS ; if (p->policyvers < MOD_POLICYDB_VERSION_SELF_TYPETRANS && (avrule->specified & AVRULE_TYPE) &&
@@ -2136,12 +2142,17 @@ static int avrule_write(policydb_t *p, avrule_t * avrule,if (items != len)return POLICYDB_ERROR ; } +if (p->policyvers >=MOD_POLICYDB_VERSION_PREFIX_SUFFIX ) { + buf8 = avrule->name_match; + items =put_entry (&buf8,sizeof (uint8_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; + } }if (avrule->specified &AVRULE_XPERMS ) {size_t nel =ARRAY_SIZE (avrule->xperms->perms);uint32_t buf32[nel]; -uint8_t buf8;unsigned int i;if (p->policyvers <MOD_POLICYDB_VERSION_XPERMS_IOCTL ) {
@@ -2186,12 +2197,17 @@ static int avrule_write_list(policydb_t *p, avrule_t * avrules, avrule = avrules; len =0 ; -while (avrule) { -if (p->policyvers >=MOD_POLICYDB_VERSION_AVRULE_FTRANS || - !(avrule->specified &AVRULE_TRANSITION && - avrule->object_name)) - len++; - avrule = avrule->next; +for (avrule = avrules; avrule; avrule = avrule->next) { +if (p->policyvers <MOD_POLICYDB_VERSION_AVRULE_FTRANS && + (avrule->specified &AVTAB_TRANSITION ) && + avrule->object_name) +continue ; +if (p->policyvers <MOD_POLICYDB_VERSION_PREFIX_SUFFIX && + (avrule->specified &AVTAB_TRANSITION ) && + avrule->object_name && + avrule->name_match !=NAME_TRANS_MATCH_EXACT ) +continue ; + len++; } buf[0 ] =cpu_to_le32 (len);
@@ -2299,7 +2315,8 @@ static int filename_trans_rule_write(policydb_t *p, avrule_t *rules,class_perm_node_t *perm;for (rule = rules; rule; rule = rule->next) { -if (rule->specified &AVRULE_TRANSITION && rule->object_name) { +if (rule->specified &AVRULE_TRANSITION && rule->object_name && + rule->name_match ==NAME_TRANS_MATCH_EXACT ) {for (perm = rule->perms; perm; perm = perm->next) { nel++; }
@@ -2312,7 +2329,9 @@ static int filename_trans_rule_write(policydb_t *p, avrule_t *rules,return POLICYDB_ERROR ;for (rule = rules; rule; rule = rule->next) { -if (!(rule->specified &AVRULE_TRANSITION && rule->object_name)) +if (!(rule->specified &AVRULE_TRANSITION && + rule->object_name && + rule->name_match ==NAME_TRANS_MATCH_EXACT ))continue ; len =strlen (rule->object_name);for (perm = rule->perms; perm; perm = perm->next) {
@@ -2751,7 +2770,8 @@ int policydb_write(policydb_t * p, struct policy_file *fp)if (p->policy_type ==POLICY_KERN ) {if (avtab_write (p, &p->te_avtab, fp))return POLICYDB_ERROR ; -if (avtab_has_prefix_suffix_filename_transitions (&p->te_avtab)) { +if (p->policyvers <POLICYDB_VERSION_PREFIX_SUFFIX && +avtab_has_prefix_suffix_filename_transitions (&p->te_avtab)) {WARN (fp->handle,"Discarding filename prefix/suffix type transition rules" ); }
Signature · 1 line
2.40.0
PATCH v2 8/8 libsepol/cil: add support for prefix/suffix filename transtions to CIL
In reply to Juraj MarcinThis patch implements the support for prefix/suffix filename transitions in the CIL structures as well as in the CIL policy parser.
Syntax of the new prefix/suffix filename transition rule:
(typetransition source_type_id target_type_id class_id object_name match_type default_type_id)
where match_type is either the keyword "prefix" or "suffix".
Examples:
(typetransition ta tb CLASS01 "file01" prefix td) (typetransition td te CLASS01 "file02" suffix tf)
--- libsepol/cil/src/cil.c | 6 ++++++ libsepol/cil/src/cil_binary.c | 8 ++++---- libsepol/cil/src/cil_build_ast.c | 26 +++++++++++++++++++++----- libsepol/cil/src/cil_copy_ast.c | 1 + libsepol/cil/src/cil_internal.h | 4 ++++ libsepol/cil/src/cil_policy.c | 17 ++++++++++++++++- libsepol/cil/src/cil_resolve_ast.c | 10 ++++++++++ libsepol/cil/src/cil_write_ast.c | 2 ++ 8 files changed, 64 insertions(+), 10 deletions(-)
libsepol/cil/src/cil.c +6 -0
@@ -95,6 +95,8 @@ char *CIL_KEY_TUNABLEIF;char *CIL_KEY_ALLOW ;char *CIL_KEY_DONTAUDIT ;char *CIL_KEY_TYPETRANSITION ; +char *CIL_KEY_PREFIX ; +char *CIL_KEY_SUFFIX ;char *CIL_KEY_TYPECHANGE ;char *CIL_KEY_CALL ;char *CIL_KEY_TUNABLE ;
@@ -264,6 +266,8 @@ static void cil_init_keys(void)CIL_KEY_ALLOW =cil_strpool_add ("allow" );CIL_KEY_DONTAUDIT =cil_strpool_add ("dontaudit" );CIL_KEY_TYPETRANSITION =cil_strpool_add ("typetransition" ); +CIL_KEY_PREFIX =cil_strpool_add ("prefix" ); +CIL_KEY_SUFFIX =cil_strpool_add ("suffix" );CIL_KEY_TYPECHANGE =cil_strpool_add ("typechange" );CIL_KEY_CALL =cil_strpool_add ("call" );CIL_KEY_TUNABLE =cil_strpool_add ("tunable" );
@@ -2387,6 +2391,8 @@ void cil_nametypetransition_init(struct cil_nametypetransition **nametypetrans) (*nametypetrans)->obj =NULL ; (*nametypetrans)->name_str =NULL ; (*nametypetrans)->name =NULL ; + (*nametypetrans)->name_match_str =NULL ; + (*nametypetrans)->name_match =NAME_TRANS_MATCH_EXACT ; (*nametypetrans)->result_str =NULL ; (*nametypetrans)->result =NULL ; }
libsepol/cil/src/cil_binary.c +4 -4
@@ -1193,7 +1193,7 @@ static int __cil_typetransition_to_avtab_helper(policydb_t *pdb, type_datum_t *sepol_src, type_datum_t *sepol_tgt, struct cil_list *class_list, - char *name, + char *name, uint8_t name_match, type_datum_t *sepol_result) {int rc;
@@ -1211,7 +1211,7 @@ static int __cil_typetransition_to_avtab_helper(policydb_t *pdb, avt_key.target_type = sepol_tgt->s.value; avt_key.target_class = sepol_obj->s.value; rc =avtab_insert_filename_trans (&pdb->te_avtab, &avt_key, - sepol_result->s.value, name,NAME_TRANS_MATCH_EXACT , + sepol_result->s.value, name, name_match, &otype);if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) {
@@ -1280,7 +1280,7 @@ static int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *d rc =__cil_typetransition_to_avtab_helper ( pdb, sepol_src, sepol_src, class_list, - name, sepol_result + name, typetrans->name_match, sepol_result );if (rc !=SEPOL_OK ) goto exit; }
@@ -1298,7 +1298,7 @@ static int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *d rc =__cil_typetransition_to_avtab_helper ( pdb, sepol_src, sepol_tgt, class_list, - name, sepol_result + name, typetrans->name_match, sepol_result );if (rc !=SEPOL_OK ) goto exit; }
libsepol/cil/src/cil_build_ast.c +21 -5
@@ -3334,10 +3334,11 @@ int cil_gen_typetransition(struct cil_db *db, struct cil_tree_node *parse_currenCIL_SYN_STRING ,CIL_SYN_STRING ,CIL_SYN_STRING |CIL_SYN_END , - CIL_SYN_END +CIL_SYN_STRING |CIL_SYN_END , + CIL_SYN_END, };size_t syntax_len =sizeof (syntax)/sizeof (*syntax); -char *s1, *s2, *s3, *s4, *s5; +char *s1, *s2, *s3, *s4, *s5, *s6;if (db ==NULL || parse_current ==NULL || ast_node ==NULL ) { goto exit;
@@ -3353,12 +3354,22 @@ int cil_gen_typetransition(struct cil_db *db, struct cil_tree_node *parse_curren s3 = parse_current->next->next->next->data; s4 = parse_current->next->next->next->next->data; s5 =NULL ; + s6 =NULL ;if (parse_current->next->next->next->next->next) {if (s4 ==CIL_KEY_STAR ) { - s4 = parse_current->next->next->next->next->next->data; +if (parse_current->next->next->next->next->next->next) { + s4 = parse_current->next->next->next->next->next->next->data; + }else { + s4 = parse_current->next->next->next->next->next->data; + } }else { - s5 = parse_current->next->next->next->next->next->data; +if (parse_current->next->next->next->next->next->next) { + s5 = parse_current->next->next->next->next->next->data; + s6 = parse_current->next->next->next->next->next->next->data; + }else { + s5 = parse_current->next->next->next->next->next->data; + } } }
@@ -3370,8 +3381,13 @@ int cil_gen_typetransition(struct cil_db *db, struct cil_tree_node *parse_curren nametypetrans->src_str = s1; nametypetrans->tgt_str = s2; nametypetrans->obj_str = s3; - nametypetrans->result_str = s5; nametypetrans->name_str = s4; +if (s6) { + nametypetrans->name_match_str = s5; + nametypetrans->result_str = s6; + }else { + nametypetrans->result_str = s5; + } ast_node->data = nametypetrans; ast_node->flavor =CIL_NAMETYPETRANSITION ;
libsepol/cil/src/cil_copy_ast.c +1 -0
@@ -726,6 +726,7 @@ int cil_copy_nametypetransition(__attribute__((unused)) struct cil_db *db, void new->tgt_str = orig->tgt_str; new->obj_str = orig->obj_str; new->name_str = orig->name_str; + new->name_match_str = orig->name_match_str; new->result_str = orig->result_str;
libsepol/cil/src/cil_internal.h +4 -0
@@ -112,6 +112,8 @@ extern char *CIL_KEY_TUNABLEIF;extern char *CIL_KEY_ALLOW ;extern char *CIL_KEY_DONTAUDIT ;extern char *CIL_KEY_TYPETRANSITION ; +extern char *CIL_KEY_PREFIX ; +extern char *CIL_KEY_SUFFIX ;extern char *CIL_KEY_TYPECHANGE ;extern char *CIL_KEY_CALL ;extern char *CIL_KEY_TUNABLE ;
@@ -575,6 +577,8 @@ struct cil_nametypetransition {struct cil_class *obj;char *name_str;struct cil_name *name; +char *name_match_str; +uint8_t name_match;char *result_str;void *result;/* type or alias */
libsepol/cil/src/cil_policy.c +16 -1
@@ -1260,6 +1260,7 @@ static void cil_nametypetransition_to_policy(FILE *out, struct cil_nametypetransstruct cil_name *name;struct cil_list *class_list;struct cil_list_item *i1; +const char *name_match_str ="" ; src = trans->src; tgt = trans->tgt;
@@ -1268,7 +1269,21 @@ static void cil_nametypetransition_to_policy(FILE *out, struct cil_nametypetrans class_list =cil_expand_class (trans->obj);cil_list_for_each (i1, class_list) { -fprintf (out,"type_transition %s %s : %s %s \"%s\";\n" , src->fqn, tgt->fqn,DATUM (i1->data)->fqn, res->fqn, name->datum.fqn); +switch (trans->name_match) { +case NAME_TRANS_MATCH_EXACT : + name_match_str ="" ; +break ; +case NAME_TRANS_MATCH_PREFIX : + name_match_str =" PREFIX" ; +break ; +case NAME_TRANS_MATCH_SUFFIX : + name_match_str =" SUFFIX" ; +break ; +default : + name_match_str ="???" ; +break ; + } +fprintf (out,"type_transition %s %s : %s %s \"%s\"%s;\n" , src->fqn, tgt->fqn,DATUM (i1->data)->fqn, res->fqn, name->datum.fqn, name_match_str); }cil_list_destroy (&class_list,CIL_FALSE ); }
libsepol/cil/src/cil_resolve_ast.c +10 -0
@@ -668,6 +668,16 @@ int cil_resolve_nametypetransition(struct cil_tree_node *current, void *extra_ar nametypetrans->name = (struct cil_name *)name_datum; } +if (nametypetrans->name_match_str ==NULL ) { + nametypetrans->name_match =NAME_TRANS_MATCH_EXACT ; + }else if (nametypetrans->name_match_str ==CIL_KEY_PREFIX ) { + nametypetrans->name_match =NAME_TRANS_MATCH_PREFIX ; + }else if (nametypetrans->name_match_str ==CIL_KEY_SUFFIX ) { + nametypetrans->name_match =NAME_TRANS_MATCH_SUFFIX ; + }else { +cil_tree_log (current,CIL_ERR ,"Invalid name match type \"%s\"" , nametypetrans->name_match_str); + } + rc =cil_resolve_name (current, nametypetrans->result_str,CIL_SYM_TYPES , extra_args, &result_datum);if (rc !=SEPOL_OK ) { goto exit;
libsepol/cil/src/cil_write_ast.c +2 -0
@@ -1168,6 +1168,8 @@ void cil_write_ast_node(FILE *out, struct cil_tree_node *node)fprintf (out,"%s " ,datum_or_str (DATUM (rule->tgt), rule->tgt_str));fprintf (out,"%s " ,datum_or_str (DATUM (rule->obj), rule->obj_str));fprintf (out,"\"%s\" " ,datum_or_str (DATUM (rule->name), rule->name_str)); +if (rule->name_match !=NAME_TRANS_MATCH_EXACT ) +fprintf (out,"%s " , rule->name_match_str);fprintf (out,"%s" ,datum_or_str (DATUM (rule->result), rule->result_str));fprintf (out,")\n" );break ;
Signature · 1 line
2.40.0
Re: [PATCH v2 1/8] checkpolicy,libsepol: move transition to separate structure in avtab
In reply to Juraj MarcinQuoted 9 lines · Juraj Marcin
To move filename transitions to be part of avtab, we need to create space for it in the
avtab_datumstructure which holds the rule for a certain combination of stype, ttype and tclass.As only type transitions have a special variant that uses a filename, it would be suboptimal to add a (mostly empty) pointer to some structure to all avtab rules.
Quoted 7 lines
Therefore, this patch adds a new structure to the
avtab_datumand moves the otype of the transition to this structure. In the next patch, this structure will also hold filename transitions for the combination of stype, ttype and tclass.Reviewed-by Ondrej Mosnacek <[email protected]>Signed-off-by Juraj Marcin <[email protected]>
I have this fear that I missed something, but I have tested compatibility as much as I can and everything seems to work fine. Even building older policy versions doesn't break anything that I can find.
Patch 3 no longer applies cleanly with some of the recent dismod, but it is minor and I can handle it when I merge this series.
If at some point we allow named type transitions in conditional policy, then CIL can handle all type transitions like named type transitions. But that is for another day.
For this series of eight patches:
Quoted 368 lines
--- checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 26 +++++++++++++++----- libsepol/include/sepol/policydb/avtab.h | 7 +++++- libsepol/src/avtab.c | 32 ++++++++++++++++++++++++- libsepol/src/expand.c | 8 +++++-- libsepol/src/kernel_to_cil.c | 3 ++- libsepol/src/kernel_to_conf.c | 3 ++- libsepol/src/optimize.c | 4 ++++ libsepol/src/policydb_validate.c | 4 +++- libsepol/src/services.c | 5 +++- libsepol/src/write.c | 17 ++++++++++--- 11 files changed, 93 insertions(+), 18 deletions(-)checkpolicy/test/dispol.c +1
-1@@ -180,7 +180,7 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t whaif (key->specified &AVTAB_TRANSITION ) {fprintf (fp,"type_transition " );render_key (key, p, fp); -render_type (datum->data, p, fp); +render_type (datum->trans->otype, p, fp);fprintf (fp,";\n" ); }if (key->specified &AVTAB_MEMBER ) {libsepol/cil/src/cil_binary.c +20
-6@@ -975,28 +975,34 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,int rc =SEPOL_OK ;avtab_key_t avtab_key;avtab_datum_t avtab_datum; +avtab_trans_t trans;avtab_ptr_t existing; avtab_key.source_type = src; avtab_key.target_type = tgt; avtab_key.target_class = obj; +memset (&avtab_datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t)); +switch (kind) {case CIL_TYPE_TRANSITION : avtab_key.specified =AVTAB_TRANSITION ; + trans.otype = res; + avtab_datum.trans = &trans;break ;case CIL_TYPE_CHANGE : avtab_key.specified =AVTAB_CHANGE ; + avtab_datum.data = res;break ;case CIL_TYPE_MEMBER : avtab_key.specified =AVTAB_MEMBER ; + avtab_datum.data = res;break ;default : rc =SEPOL_ERR ; goto exit; } - - avtab_datum.data = res; existing =avtab_search_node (&pdb->te_avtab, &avtab_key);if (existing) {@@ -1004,13 +1010,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,* A warning should have been previously given if there is a * non-duplicate rule using the same key. */ -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;@@ -1037,13 +1047,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, search_datum =cil_cond_av_list_search (&avtab_key, other_list);if (search_datum ==NULL ) { -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;libsepol/include/sepol/policydb/avtab.h +6
-1@@ -70,6 +70,10 @@ typedef struct avtab_key {uint16_t specified;/* what fields are specified */ } avtab_key_t; +typedef struct avtab_trans { +uint32_t otype;/* resulting type of the new object */ +}avtab_trans_t ; + typedef struct avtab_extended_perms { #define AVTAB_XPERMS_IOCTLFUNCTION 0x01@@ -81,7 +85,8 @@ typedef struct avtab_extended_perms { } avtab_extended_perms_t;typedef struct avtab_datum { -uint32_t data;/* access vector or type */ +uint32_t data;/* access vector, member or change value */ +avtab_trans_t *trans;/* transition value */ avtab_extended_perms_t *xperms; }avtab_datum_t ;libsepol/src/avtab.c +31
-1@@ -94,6 +94,7 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, avtab_datum_t * datum) {avtab_ptr_t newnode; +avtab_trans_t *trans;avtab_extended_perms_t *xperms; newnode = (avtab_ptr_t )malloc (sizeof (struct avtab_node ));@@ -117,6 +118,16 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,* So copy data so it is set in the avtab */ newnode->datum.data = datum->data; + }else if (key->specified &AVTAB_TRANSITION ) { + trans =calloc (1 ,sizeof (*trans)); +if (trans ==NULL ) { +free (newnode); +return NULL ; + } +if (datum->trans)/* else caller populates transition */ + *trans = *(datum->trans); + + newnode->datum.trans = trans; }else { newnode->datum = *datum; }@@ -317,6 +328,8 @@ void avtab_destroy(avtab_t * h)while (cur !=NULL ) {if (cur->key.specified &AVTAB_XPERMS ) {free (cur->datum.xperms); + }else if (cur->key.specified &AVTAB_TRANSITION ) { +free (cur->datum.trans); } temp = cur; cur = cur->next;@@ -440,6 +453,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,uint32_t buf32[8 ], items, items2, val;avtab_key_t key;avtab_datum_t datum; +avtab_trans_t trans;avtab_extended_perms_t xperms;unsigned set;unsigned int i;@@ -447,6 +461,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,memset (&key,0 ,sizeof (avtab_key_t));memset (&datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t));memset (&xperms,0 ,sizeof (avtab_extended_perms_t));if (vers <POLICYDB_VERSION_AVTAB ) {@@ -509,7 +524,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,return -1 ; } key.specified = spec_order[i] | enabled; - datum.data =le32_to_cpu (buf32[items++]); +if (key.specified &AVTAB_TRANSITION ) { + trans.otype = +le32_to_cpu (buf32[items++]); + datum.trans = &trans; + }else { + datum.data = +le32_to_cpu (buf32[items++]); + } rc =insertf (a, &key, &datum, p);if (rc)return rc;@@ -571,6 +593,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,for (i =0 ; i <ARRAY_SIZE (xperms.perms); i++) xperms.perms[i] =le32_to_cpu (buf32[i]); datum.xperms = &xperms; + }else if (key.specified &AVTAB_TRANSITION ) { + rc =next_entry (buf32, fp,sizeof (uint32_t )); +if (rc <0 ) { +ERR (fp->handle,"truncated entry" ); +return -1 ; + } + trans.otype =le32_to_cpu (*buf32); + datum.trans = &trans; }else { rc =next_entry (buf32, fp,sizeof (uint32_t ));if (rc <0 ) {libsepol/src/expand.c +6
-2@@ -1746,7 +1746,7 @@ static int expand_terule_helper(sepol_handle_t * handle,if (conflict) { avdatump = &node->datum;if (specified &AVRULE_TRANSITION ) { - oldtype = avdatump->data; + oldtype = avdatump->trans->otype; }else if (specified &AVRULE_MEMBER ) { oldtype = avdatump->data; }else if (specified &AVRULE_CHANGE ) {@@ -1789,7 +1789,11 @@ static int expand_terule_helper(sepol_handle_t * handle, } avdatump = &node->datum; - avdatump->data = remapped_data; +if (specified &AVRULE_TRANSITION ) { + avdatump->trans->otype = remapped_data; + }else { + avdatump->data = remapped_data; + } cur = cur->next; }libsepol/src/kernel_to_cil.c +2
-1@@ -1704,7 +1704,8 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *tgt;char *src, *class, *perms, *new;libsepol/src/kernel_to_conf.c +2
-1@@ -1682,7 +1682,8 @@ exit:static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *src, *tgt, *class, *perms, *new;char *rule =NULL ;libsepol/src/optimize.c +4
-0@@ -308,6 +308,8 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;@@ -427,6 +429,8 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;libsepol/src/policydb_validate.c +3
-1@@ -836,7 +836,9 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *if (validate_avtab_key (k,0 , margs->policy, margs->flavors))return -1 ; -if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) +uint32_t otype = k->specified &AVTAB_TRANSITION + ? d->trans->otype : d->data; +if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (otype, margs->policy, margs->flavors))return -1 ;if ((k->specified &AVTAB_XPERMS ) &&validate_xperms (d->xperms))libsepol/src/services.c +4
-1@@ -1423,7 +1423,10 @@ static int sepol_compute_sid(sepol_security_id_t ssid,if (avdatum) {/* Use the type from the type transition/member/change rule. */ - newcontext.type = avdatum->data; +if (specified &AVTAB_TRANSITION ) + newcontext.type = avdatum->trans->otype; +else + newcontext.type = avdatum->data; }/* Check for class-specific changes. */ libsepol/src/write.c +14
-4@@ -190,14 +190,20 @@ static int avtab_write_item(policydb_t * p,ERR (fp->handle,"missing node" );return POLICYDB_ERROR ; } - buf32[items++] = -cpu_to_le32 (node->datum.data); +uint32_t data = + node->key.specified &AVTAB_TRANSITION + ? node->datum.trans->otype + : node->datum.data; + buf32[items++] =cpu_to_le32 (data); set--; node->merged =1 ; } } }else { - buf32[items++] =cpu_to_le32 (cur->datum.data); +uint32_t data = cur->key.specified &AVTAB_TRANSITION + ? cur->datum.trans->otype + : cur->datum.data; + buf32[items++] =cpu_to_le32 (data); cur->merged =1 ; set--; }@@ -256,6 +262,11 @@ static int avtab_write_item(policydb_t * p, items =put_entry (buf32,sizeof (uint32_t ),8 ,fp);if (items !=8 )return POLICYDB_ERROR ; + }else if (cur->key.specified &AVTAB_TRANSITION ) { + buf32[0 ] =cpu_to_le32 (cur->datum.trans->otype); + items =put_entry (buf32,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; }else { buf32[0 ] =cpu_to_le32 (cur->datum.data); items =put_entry (buf32,sizeof (uint32_t ),1 , fp); --2.40.0
Re: [PATCH v2 1/8] checkpolicy,libsepol: move transition to separate structure in avtab
Quoted 26 lines · James Carter
Quoted 9 lines · Juraj Marcin
To move filename transitions to be part of avtab, we need to create space for it in the
avtab_datumstructure which holds the rule for a certain combination of stype, ttype and tclass.As only type transitions have a special variant that uses a filename, it would be suboptimal to add a (mostly empty) pointer to some structure to all avtab rules.
Quoted 7 lines
Therefore, this patch adds a new structure to the
avtab_datumand moves the otype of the transition to this structure. In the next patch, this structure will also hold filename transitions for the combination of stype, ttype and tclass.Reviewed-by Ondrej Mosnacek <[email protected]>Signed-off-by Juraj Marcin <[email protected]>I have this fear that I missed something, but I have tested compatibility as much as I can and everything seems to work fine. Even building older policy versions doesn't break anything that I can find.
Patch 3 no longer applies cleanly with some of the recent dismod, but it is minor and I can handle it when I merge this series.
Quoted 7 lines
If at some point we allow named type transitions in conditional policy, then CIL can handle all type transitions like named type transitions. But that is for another day.
For this series of eight patches:
Acked-by James Carter <[email protected]>
These eight patches have been merged.
Thanks,
Jim
Quoted 369 lines
Quoted 369 lines
--- checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 26 +++++++++++++++----- libsepol/include/sepol/policydb/avtab.h | 7 +++++- libsepol/src/avtab.c | 32 ++++++++++++++++++++++++- libsepol/src/expand.c | 8 +++++-- libsepol/src/kernel_to_cil.c | 3 ++- libsepol/src/kernel_to_conf.c | 3 ++- libsepol/src/optimize.c | 4 ++++ libsepol/src/policydb_validate.c | 4 +++- libsepol/src/services.c | 5 +++- libsepol/src/write.c | 17 ++++++++++--- 11 files changed, 93 insertions(+), 18 deletions(-)checkpolicy/test/dispol.c +1
-1@@ -180,7 +180,7 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t whaif (key->specified &AVTAB_TRANSITION ) {fprintf (fp,"type_transition " );render_key (key, p, fp); -render_type (datum->data, p, fp); +render_type (datum->trans->otype, p, fp);fprintf (fp,";\n" ); }if (key->specified &AVTAB_MEMBER ) {libsepol/cil/src/cil_binary.c +20
-6@@ -975,28 +975,34 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,int rc =SEPOL_OK ;avtab_key_t avtab_key;avtab_datum_t avtab_datum; +avtab_trans_t trans;avtab_ptr_t existing; avtab_key.source_type = src; avtab_key.target_type = tgt; avtab_key.target_class = obj; +memset (&avtab_datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t)); +switch (kind) {case CIL_TYPE_TRANSITION : avtab_key.specified =AVTAB_TRANSITION ; + trans.otype = res; + avtab_datum.trans = &trans;break ;case CIL_TYPE_CHANGE : avtab_key.specified =AVTAB_CHANGE ; + avtab_datum.data = res;break ;case CIL_TYPE_MEMBER : avtab_key.specified =AVTAB_MEMBER ; + avtab_datum.data = res;break ;default : rc =SEPOL_ERR ; goto exit; } - - avtab_datum.data = res; existing =avtab_search_node (&pdb->te_avtab, &avtab_key);if (existing) {@@ -1004,13 +1010,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,* A warning should have been previously given if there is a * non-duplicate rule using the same key. */ -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;@@ -1037,13 +1047,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, search_datum =cil_cond_av_list_search (&avtab_key, other_list);if (search_datum ==NULL ) { -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;libsepol/include/sepol/policydb/avtab.h +6
-1@@ -70,6 +70,10 @@ typedef struct avtab_key {uint16_t specified;/* what fields are specified */ } avtab_key_t; +typedef struct avtab_trans { +uint32_t otype;/* resulting type of the new object */ +}avtab_trans_t ; + typedef struct avtab_extended_perms { #define AVTAB_XPERMS_IOCTLFUNCTION 0x01@@ -81,7 +85,8 @@ typedef struct avtab_extended_perms { } avtab_extended_perms_t;typedef struct avtab_datum { -uint32_t data;/* access vector or type */ +uint32_t data;/* access vector, member or change value */ +avtab_trans_t *trans;/* transition value */ avtab_extended_perms_t *xperms; }avtab_datum_t ;libsepol/src/avtab.c +31
-1@@ -94,6 +94,7 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, avtab_datum_t * datum) {avtab_ptr_t newnode; +avtab_trans_t *trans;avtab_extended_perms_t *xperms; newnode = (avtab_ptr_t )malloc (sizeof (struct avtab_node ));@@ -117,6 +118,16 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,* So copy data so it is set in the avtab */ newnode->datum.data = datum->data; + }else if (key->specified &AVTAB_TRANSITION ) { + trans =calloc (1 ,sizeof (*trans)); +if (trans ==NULL ) { +free (newnode); +return NULL ; + } +if (datum->trans)/* else caller populates transition */ + *trans = *(datum->trans); + + newnode->datum.trans = trans; }else { newnode->datum = *datum; }@@ -317,6 +328,8 @@ void avtab_destroy(avtab_t * h)while (cur !=NULL ) {if (cur->key.specified &AVTAB_XPERMS ) {free (cur->datum.xperms); + }else if (cur->key.specified &AVTAB_TRANSITION ) { +free (cur->datum.trans); } temp = cur; cur = cur->next;@@ -440,6 +453,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,uint32_t buf32[8 ], items, items2, val;avtab_key_t key;avtab_datum_t datum; +avtab_trans_t trans;avtab_extended_perms_t xperms;unsigned set;unsigned int i;@@ -447,6 +461,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,memset (&key,0 ,sizeof (avtab_key_t));memset (&datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t));memset (&xperms,0 ,sizeof (avtab_extended_perms_t));if (vers <POLICYDB_VERSION_AVTAB ) {@@ -509,7 +524,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,return -1 ; } key.specified = spec_order[i] | enabled; - datum.data =le32_to_cpu (buf32[items++]); +if (key.specified &AVTAB_TRANSITION ) { + trans.otype = +le32_to_cpu (buf32[items++]); + datum.trans = &trans; + }else { + datum.data = +le32_to_cpu (buf32[items++]); + } rc =insertf (a, &key, &datum, p);if (rc)return rc;@@ -571,6 +593,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,for (i =0 ; i <ARRAY_SIZE (xperms.perms); i++) xperms.perms[i] =le32_to_cpu (buf32[i]); datum.xperms = &xperms; + }else if (key.specified &AVTAB_TRANSITION ) { + rc =next_entry (buf32, fp,sizeof (uint32_t )); +if (rc <0 ) { +ERR (fp->handle,"truncated entry" ); +return -1 ; + } + trans.otype =le32_to_cpu (*buf32); + datum.trans = &trans; }else { rc =next_entry (buf32, fp,sizeof (uint32_t ));if (rc <0 ) {libsepol/src/expand.c +6
-2@@ -1746,7 +1746,7 @@ static int expand_terule_helper(sepol_handle_t * handle,if (conflict) { avdatump = &node->datum;if (specified &AVRULE_TRANSITION ) { - oldtype = avdatump->data; + oldtype = avdatump->trans->otype; }else if (specified &AVRULE_MEMBER ) { oldtype = avdatump->data; }else if (specified &AVRULE_CHANGE ) {@@ -1789,7 +1789,11 @@ static int expand_terule_helper(sepol_handle_t * handle, } avdatump = &node->datum; - avdatump->data = remapped_data; +if (specified &AVRULE_TRANSITION ) { + avdatump->trans->otype = remapped_data; + }else { + avdatump->data = remapped_data; + } cur = cur->next; }libsepol/src/kernel_to_cil.c +2
-1@@ -1704,7 +1704,8 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *tgt;char *src, *class, *perms, *new;libsepol/src/kernel_to_conf.c +2
-1@@ -1682,7 +1682,8 @@ exit:static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *src, *tgt, *class, *perms, *new;char *rule =NULL ;libsepol/src/optimize.c +4
-0@@ -308,6 +308,8 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;@@ -427,6 +429,8 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;libsepol/src/policydb_validate.c +3
-1@@ -836,7 +836,9 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *if (validate_avtab_key (k,0 , margs->policy, margs->flavors))return -1 ; -if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) +uint32_t otype = k->specified &AVTAB_TRANSITION + ? d->trans->otype : d->data; +if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (otype, margs->policy, margs->flavors))return -1 ;if ((k->specified &AVTAB_XPERMS ) &&validate_xperms (d->xperms))libsepol/src/services.c +4
-1@@ -1423,7 +1423,10 @@ static int sepol_compute_sid(sepol_security_id_t ssid,if (avdatum) {/* Use the type from the type transition/member/change rule. */ - newcontext.type = avdatum->data; +if (specified &AVTAB_TRANSITION ) + newcontext.type = avdatum->trans->otype; +else + newcontext.type = avdatum->data; }/* Check for class-specific changes. */ libsepol/src/write.c +14
-4@@ -190,14 +190,20 @@ static int avtab_write_item(policydb_t * p,ERR (fp->handle,"missing node" );return POLICYDB_ERROR ; } - buf32[items++] = -cpu_to_le32 (node->datum.data); +uint32_t data = + node->key.specified &AVTAB_TRANSITION + ? node->datum.trans->otype + : node->datum.data; + buf32[items++] =cpu_to_le32 (data); set--; node->merged =1 ; } } }else { - buf32[items++] =cpu_to_le32 (cur->datum.data); +uint32_t data = cur->key.specified &AVTAB_TRANSITION + ? cur->datum.trans->otype + : cur->datum.data; + buf32[items++] =cpu_to_le32 (data); cur->merged =1 ; set--; }@@ -256,6 +262,11 @@ static int avtab_write_item(policydb_t * p, items =put_entry (buf32,sizeof (uint32_t ),8 ,fp);if (items !=8 )return POLICYDB_ERROR ; + }else if (cur->key.specified &AVTAB_TRANSITION ) { + buf32[0 ] =cpu_to_le32 (cur->datum.trans->otype); + items =put_entry (buf32,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; }else { buf32[0 ] =cpu_to_le32 (cur->datum.data); items =put_entry (buf32,sizeof (uint32_t ),1 , fp); --2.40.0
Re: [PATCH v2 1/8] checkpolicy,libsepol: move transition to separate structure in avtab
Quoted 32 lines · James Carter
Quoted 30 lines · James Carter
Quoted 9 lines · Juraj Marcin
To move filename transitions to be part of avtab, we need to create space for it in the
avtab_datumstructure which holds the rule for a certain combination of stype, ttype and tclass.As only type transitions have a special variant that uses a filename, it would be suboptimal to add a (mostly empty) pointer to some structure to all avtab rules.
Quoted 7 lines
Therefore, this patch adds a new structure to the
avtab_datumand moves the otype of the transition to this structure. In the next patch, this structure will also hold filename transitions for the combination of stype, ttype and tclass.Reviewed-by Ondrej Mosnacek <[email protected]>Signed-off-by Juraj Marcin <[email protected]>I have this fear that I missed something, but I have tested compatibility as much as I can and everything seems to work fine. Even building older policy versions doesn't break anything that I can find.
Patch 3 no longer applies cleanly with some of the recent dismod, but it is minor and I can handle it when I merge this series.
If at some point we allow named type transitions in conditional policy, then CIL can handle all type transitions like named type transitions. But that is for another day.
Quoted 7 lines
Quoted 3 lines
For this series of eight patches:
Acked-by James Carter <[email protected]>These eight patches have been merged.
Thanks,
Jim
If I remember correctly the kernel patches have not been ack'ed yet.
Quoted 371 lines
Quoted 371 lines
Quoted 371 lines
--- checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 26 +++++++++++++++----- libsepol/include/sepol/policydb/avtab.h | 7 +++++- libsepol/src/avtab.c | 32 ++++++++++++++++++++++++- libsepol/src/expand.c | 8 +++++-- libsepol/src/kernel_to_cil.c | 3 ++- libsepol/src/kernel_to_conf.c | 3 ++- libsepol/src/optimize.c | 4 ++++ libsepol/src/policydb_validate.c | 4 +++- libsepol/src/services.c | 5 +++- libsepol/src/write.c | 17 ++++++++++--- 11 files changed, 93 insertions(+), 18 deletions(-)checkpolicy/test/dispol.c +1
-1@@ -180,7 +180,7 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t whaif (key->specified &AVTAB_TRANSITION ) {fprintf (fp,"type_transition " );render_key (key, p, fp); -render_type (datum->data, p, fp); +render_type (datum->trans->otype, p, fp);fprintf (fp,";\n" ); }if (key->specified &AVTAB_MEMBER ) {libsepol/cil/src/cil_binary.c +20
-6@@ -975,28 +975,34 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,int rc =SEPOL_OK ;avtab_key_t avtab_key;avtab_datum_t avtab_datum; +avtab_trans_t trans;avtab_ptr_t existing; avtab_key.source_type = src; avtab_key.target_type = tgt; avtab_key.target_class = obj; +memset (&avtab_datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t)); +switch (kind) {case CIL_TYPE_TRANSITION : avtab_key.specified =AVTAB_TRANSITION ; + trans.otype = res; + avtab_datum.trans = &trans;break ;case CIL_TYPE_CHANGE : avtab_key.specified =AVTAB_CHANGE ; + avtab_datum.data = res;break ;case CIL_TYPE_MEMBER : avtab_key.specified =AVTAB_MEMBER ; + avtab_datum.data = res;break ;default : rc =SEPOL_ERR ; goto exit; } - - avtab_datum.data = res; existing =avtab_search_node (&pdb->te_avtab, &avtab_key);if (existing) {@@ -1004,13 +1010,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,* A warning should have been previously given if there is a * non-duplicate rule using the same key. */ -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;@@ -1037,13 +1047,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, search_datum =cil_cond_av_list_search (&avtab_key, other_list);if (search_datum ==NULL ) { -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;libsepol/include/sepol/policydb/avtab.h +6
-1@@ -70,6 +70,10 @@ typedef struct avtab_key {uint16_t specified;/* what fields are specified */ } avtab_key_t; +typedef struct avtab_trans { +uint32_t otype;/* resulting type of the new object */ +}avtab_trans_t ; + typedef struct avtab_extended_perms { #define AVTAB_XPERMS_IOCTLFUNCTION 0x01@@ -81,7 +85,8 @@ typedef struct avtab_extended_perms { } avtab_extended_perms_t;typedef struct avtab_datum { -uint32_t data;/* access vector or type */ +uint32_t data;/* access vector, member or change value */ +avtab_trans_t *trans;/* transition value */ avtab_extended_perms_t *xperms; }avtab_datum_t ;libsepol/src/avtab.c +31
-1@@ -94,6 +94,7 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, avtab_datum_t * datum) {avtab_ptr_t newnode; +avtab_trans_t *trans;avtab_extended_perms_t *xperms; newnode = (avtab_ptr_t )malloc (sizeof (struct avtab_node ));@@ -117,6 +118,16 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,* So copy data so it is set in the avtab */ newnode->datum.data = datum->data; + }else if (key->specified &AVTAB_TRANSITION ) { + trans =calloc (1 ,sizeof (*trans)); +if (trans ==NULL ) { +free (newnode); +return NULL ; + } +if (datum->trans)/* else caller populates transition */ + *trans = *(datum->trans); + + newnode->datum.trans = trans; }else { newnode->datum = *datum; }@@ -317,6 +328,8 @@ void avtab_destroy(avtab_t * h)while (cur !=NULL ) {if (cur->key.specified &AVTAB_XPERMS ) {free (cur->datum.xperms); + }else if (cur->key.specified &AVTAB_TRANSITION ) { +free (cur->datum.trans); } temp = cur; cur = cur->next;@@ -440,6 +453,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,uint32_t buf32[8 ], items, items2, val;avtab_key_t key;avtab_datum_t datum; +avtab_trans_t trans;avtab_extended_perms_t xperms;unsigned set;unsigned int i;@@ -447,6 +461,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,memset (&key,0 ,sizeof (avtab_key_t));memset (&datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t));memset (&xperms,0 ,sizeof (avtab_extended_perms_t));if (vers <POLICYDB_VERSION_AVTAB ) {@@ -509,7 +524,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,return -1 ; } key.specified = spec_order[i] | enabled; - datum.data =le32_to_cpu (buf32[items++]); +if (key.specified &AVTAB_TRANSITION ) { + trans.otype = +le32_to_cpu (buf32[items++]); + datum.trans = &trans; + }else { + datum.data = +le32_to_cpu (buf32[items++]); + } rc =insertf (a, &key, &datum, p);if (rc)return rc;@@ -571,6 +593,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,for (i =0 ; i <ARRAY_SIZE (xperms.perms); i++) xperms.perms[i] =le32_to_cpu (buf32[i]); datum.xperms = &xperms; + }else if (key.specified &AVTAB_TRANSITION ) { + rc =next_entry (buf32, fp,sizeof (uint32_t )); +if (rc <0 ) { +ERR (fp->handle,"truncated entry" ); +return -1 ; + } + trans.otype =le32_to_cpu (*buf32); + datum.trans = &trans; }else { rc =next_entry (buf32, fp,sizeof (uint32_t ));if (rc <0 ) {libsepol/src/expand.c +6
-2@@ -1746,7 +1746,7 @@ static int expand_terule_helper(sepol_handle_t * handle,if (conflict) { avdatump = &node->datum;if (specified &AVRULE_TRANSITION ) { - oldtype = avdatump->data; + oldtype = avdatump->trans->otype; }else if (specified &AVRULE_MEMBER ) { oldtype = avdatump->data; }else if (specified &AVRULE_CHANGE ) {@@ -1789,7 +1789,11 @@ static int expand_terule_helper(sepol_handle_t * handle, } avdatump = &node->datum; - avdatump->data = remapped_data; +if (specified &AVRULE_TRANSITION ) { + avdatump->trans->otype = remapped_data; + }else { + avdatump->data = remapped_data; + } cur = cur->next; }libsepol/src/kernel_to_cil.c +2
-1@@ -1704,7 +1704,8 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *tgt;char *src, *class, *perms, *new;libsepol/src/kernel_to_conf.c +2
-1@@ -1682,7 +1682,8 @@ exit:static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *src, *tgt, *class, *perms, *new;char *rule =NULL ;libsepol/src/optimize.c +4
-0@@ -308,6 +308,8 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;@@ -427,6 +429,8 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;libsepol/src/policydb_validate.c +3
-1@@ -836,7 +836,9 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *if (validate_avtab_key (k,0 , margs->policy, margs->flavors))return -1 ; -if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) +uint32_t otype = k->specified &AVTAB_TRANSITION + ? d->trans->otype : d->data; +if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (otype, margs->policy, margs->flavors))return -1 ;if ((k->specified &AVTAB_XPERMS ) &&validate_xperms (d->xperms))libsepol/src/services.c +4
-1@@ -1423,7 +1423,10 @@ static int sepol_compute_sid(sepol_security_id_t ssid,if (avdatum) {/* Use the type from the type transition/member/change rule. */ - newcontext.type = avdatum->data; +if (specified &AVTAB_TRANSITION ) + newcontext.type = avdatum->trans->otype; +else + newcontext.type = avdatum->data; }/* Check for class-specific changes. */ libsepol/src/write.c +14
-4@@ -190,14 +190,20 @@ static int avtab_write_item(policydb_t * p,ERR (fp->handle,"missing node" );return POLICYDB_ERROR ; } - buf32[items++] = -cpu_to_le32 (node->datum.data); +uint32_t data = + node->key.specified &AVTAB_TRANSITION + ? node->datum.trans->otype + : node->datum.data; + buf32[items++] =cpu_to_le32 (data); set--; node->merged =1 ; } } }else { - buf32[items++] =cpu_to_le32 (cur->datum.data); +uint32_t data = cur->key.specified &AVTAB_TRANSITION + ? cur->datum.trans->otype + : cur->datum.data; + buf32[items++] =cpu_to_le32 (data); cur->merged =1 ; set--; }@@ -256,6 +262,11 @@ static int avtab_write_item(policydb_t * p, items =put_entry (buf32,sizeof (uint32_t ),8 ,fp);if (items !=8 )return POLICYDB_ERROR ; + }else if (cur->key.specified &AVTAB_TRANSITION ) { + buf32[0 ] =cpu_to_le32 (cur->datum.trans->otype); + items =put_entry (buf32,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; }else { buf32[0 ] =cpu_to_le32 (cur->datum.data); items =put_entry (buf32,sizeof (uint32_t ),1 , fp); --2.40.0
Re: [PATCH v2 1/8] checkpolicy,libsepol: move transition to separate structure in avtab
Quoted 38 lines · Christian Göttsche
Quoted 36 lines · James Carter
Quoted 33 lines · James Carter
Quoted 9 lines · Juraj Marcin
To move filename transitions to be part of avtab, we need to create space for it in the
avtab_datumstructure which holds the rule for a certain combination of stype, ttype and tclass.As only type transitions have a special variant that uses a filename, it would be suboptimal to add a (mostly empty) pointer to some structure to all avtab rules.
Quoted 7 lines
Therefore, this patch adds a new structure to the
avtab_datumand moves the otype of the transition to this structure. In the next patch, this structure will also hold filename transitions for the combination of stype, ttype and tclass.Reviewed-by Ondrej Mosnacek <[email protected]>Signed-off-by Juraj Marcin <[email protected]>I have this fear that I missed something, but I have tested compatibility as much as I can and everything seems to work fine. Even building older policy versions doesn't break anything that I can find.
Patch 3 no longer applies cleanly with some of the recent dismod, but it is minor and I can handle it when I merge this series.
If at some point we allow named type transitions in conditional policy, then CIL can handle all type transitions like named type transitions. But that is for another day.
For this series of eight patches:
Acked-by James Carter <[email protected]>
Quoted 6 lines
Quoted 3 lines
These eight patches have been merged.
Thanks,
JimIf I remember correctly the kernel patches have not been ack'ed yet.
Yeah, you're right. I was thinking of the initial sid patches. I might have jumped the gun a bit here.
Thanks,
Jim
Quoted 371 lines
Quoted 371 lines
Quoted 371 lines
Quoted 371 lines
--- checkpolicy/test/dispol.c | 2 +- libsepol/cil/src/cil_binary.c | 26 +++++++++++++++----- libsepol/include/sepol/policydb/avtab.h | 7 +++++- libsepol/src/avtab.c | 32 ++++++++++++++++++++++++- libsepol/src/expand.c | 8 +++++-- libsepol/src/kernel_to_cil.c | 3 ++- libsepol/src/kernel_to_conf.c | 3 ++- libsepol/src/optimize.c | 4 ++++ libsepol/src/policydb_validate.c | 4 +++- libsepol/src/services.c | 5 +++- libsepol/src/write.c | 17 ++++++++++--- 11 files changed, 93 insertions(+), 18 deletions(-)checkpolicy/test/dispol.c +1
-1@@ -180,7 +180,7 @@ static int render_av_rule(avtab_key_t * key, avtab_datum_t * datum, uint32_t whaif (key->specified &AVTAB_TRANSITION ) {fprintf (fp,"type_transition " );render_key (key, p, fp); -render_type (datum->data, p, fp); +render_type (datum->trans->otype, p, fp);fprintf (fp,";\n" ); }if (key->specified &AVTAB_MEMBER ) {libsepol/cil/src/cil_binary.c +20
-6@@ -975,28 +975,34 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,int rc =SEPOL_OK ;avtab_key_t avtab_key;avtab_datum_t avtab_datum; +avtab_trans_t trans;avtab_ptr_t existing; avtab_key.source_type = src; avtab_key.target_type = tgt; avtab_key.target_class = obj; +memset (&avtab_datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t)); +switch (kind) {case CIL_TYPE_TRANSITION : avtab_key.specified =AVTAB_TRANSITION ; + trans.otype = res; + avtab_datum.trans = &trans;break ;case CIL_TYPE_CHANGE : avtab_key.specified =AVTAB_CHANGE ; + avtab_datum.data = res;break ;case CIL_TYPE_MEMBER : avtab_key.specified =AVTAB_MEMBER ; + avtab_datum.data = res;break ;default : rc =SEPOL_ERR ; goto exit; } - - avtab_datum.data = res; existing =avtab_search_node (&pdb->te_avtab, &avtab_key);if (existing) {@@ -1004,13 +1010,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src,* A warning should have been previously given if there is a * non-duplicate rule using the same key. */ -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;@@ -1037,13 +1047,17 @@ static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, search_datum =cil_cond_av_list_search (&avtab_key, other_list);if (search_datum ==NULL ) { -if (existing->datum.data != res) { +uint32_t existing_otype = + existing->key.specified &AVTAB_TRANSITION + ? existing->datum.trans->otype + : existing->datum.data; +if (existing_otype != res) {cil_log (CIL_ERR ,"Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n" , pdb->p_type_val_to_name[src -1 ], pdb->p_type_val_to_name[tgt -1 ], pdb->p_class_val_to_name[obj -1 ], pdb->p_type_val_to_name[res -1 ], - pdb->p_type_val_to_name[existing->datum.data -1 ]); + pdb->p_type_val_to_name[existing_otype -1 ]);cil_log (CIL_ERR ,"Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n" , cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str); rc =SEPOL_ERR ;libsepol/include/sepol/policydb/avtab.h +6
-1@@ -70,6 +70,10 @@ typedef struct avtab_key {uint16_t specified;/* what fields are specified */ } avtab_key_t; +typedef struct avtab_trans { +uint32_t otype;/* resulting type of the new object */ +}avtab_trans_t ; + typedef struct avtab_extended_perms { #define AVTAB_XPERMS_IOCTLFUNCTION 0x01@@ -81,7 +85,8 @@ typedef struct avtab_extended_perms { } avtab_extended_perms_t;typedef struct avtab_datum { -uint32_t data;/* access vector or type */ +uint32_t data;/* access vector, member or change value */ +avtab_trans_t *trans;/* transition value */ avtab_extended_perms_t *xperms; }avtab_datum_t ;libsepol/src/avtab.c +31
-1@@ -94,6 +94,7 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, avtab_datum_t * datum) {avtab_ptr_t newnode; +avtab_trans_t *trans;avtab_extended_perms_t *xperms; newnode = (avtab_ptr_t )malloc (sizeof (struct avtab_node ));@@ -117,6 +118,16 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,* So copy data so it is set in the avtab */ newnode->datum.data = datum->data; + }else if (key->specified &AVTAB_TRANSITION ) { + trans =calloc (1 ,sizeof (*trans)); +if (trans ==NULL ) { +free (newnode); +return NULL ; + } +if (datum->trans)/* else caller populates transition */ + *trans = *(datum->trans); + + newnode->datum.trans = trans; }else { newnode->datum = *datum; }@@ -317,6 +328,8 @@ void avtab_destroy(avtab_t * h)while (cur !=NULL ) {if (cur->key.specified &AVTAB_XPERMS ) {free (cur->datum.xperms); + }else if (cur->key.specified &AVTAB_TRANSITION ) { +free (cur->datum.trans); } temp = cur; cur = cur->next;@@ -440,6 +453,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,uint32_t buf32[8 ], items, items2, val;avtab_key_t key;avtab_datum_t datum; +avtab_trans_t trans;avtab_extended_perms_t xperms;unsigned set;unsigned int i;@@ -447,6 +461,7 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,memset (&key,0 ,sizeof (avtab_key_t));memset (&datum,0 ,sizeof (avtab_datum_t)); +memset (&trans,0 ,sizeof (avtab_trans_t));memset (&xperms,0 ,sizeof (avtab_extended_perms_t));if (vers <POLICYDB_VERSION_AVTAB ) {@@ -509,7 +524,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,return -1 ; } key.specified = spec_order[i] | enabled; - datum.data =le32_to_cpu (buf32[items++]); +if (key.specified &AVTAB_TRANSITION ) { + trans.otype = +le32_to_cpu (buf32[items++]); + datum.trans = &trans; + }else { + datum.data = +le32_to_cpu (buf32[items++]); + } rc =insertf (a, &key, &datum, p);if (rc)return rc;@@ -571,6 +593,14 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,for (i =0 ; i <ARRAY_SIZE (xperms.perms); i++) xperms.perms[i] =le32_to_cpu (buf32[i]); datum.xperms = &xperms; + }else if (key.specified &AVTAB_TRANSITION ) { + rc =next_entry (buf32, fp,sizeof (uint32_t )); +if (rc <0 ) { +ERR (fp->handle,"truncated entry" ); +return -1 ; + } + trans.otype =le32_to_cpu (*buf32); + datum.trans = &trans; }else { rc =next_entry (buf32, fp,sizeof (uint32_t ));if (rc <0 ) {libsepol/src/expand.c +6
-2@@ -1746,7 +1746,7 @@ static int expand_terule_helper(sepol_handle_t * handle,if (conflict) { avdatump = &node->datum;if (specified &AVRULE_TRANSITION ) { - oldtype = avdatump->data; + oldtype = avdatump->trans->otype; }else if (specified &AVRULE_MEMBER ) { oldtype = avdatump->data; }else if (specified &AVRULE_CHANGE ) {@@ -1789,7 +1789,11 @@ static int expand_terule_helper(sepol_handle_t * handle, } avdatump = &node->datum; - avdatump->data = remapped_data; +if (specified &AVRULE_TRANSITION ) { + avdatump->trans->otype = remapped_data; + }else { + avdatump->data = remapped_data; + } cur = cur->next; }libsepol/src/kernel_to_cil.c +2
-1@@ -1704,7 +1704,8 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *tgt;char *src, *class, *perms, *new;libsepol/src/kernel_to_conf.c +2
-1@@ -1682,7 +1682,8 @@ exit:static char *avtab_node_to_str (struct policydb *pdb,avtab_key_t *key,avtab_datum_t *datum) { -uint32_t data = datum->data; +uint32_t data = key->specified &AVTAB_TRANSITION + ? datum->trans->otype : datum->data;type_datum_t *type;const char *flavor, *src, *tgt, *class, *perms, *new;char *rule =NULL ;libsepol/src/optimize.c +4
-0@@ -308,6 +308,8 @@ static void optimize_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;@@ -427,6 +429,8 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map) *cur = tmp->next;if (tmp->key.specified &AVTAB_XPERMS )free (tmp->datum.xperms); +if (tmp->key.specified &AVTAB_TRANSITION ) +free (tmp->datum.trans);free (tmp); tab->nel--;libsepol/src/policydb_validate.c +3
-1@@ -836,7 +836,9 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *if (validate_avtab_key (k,0 , margs->policy, margs->flavors))return -1 ; -if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (d->data, margs->policy, margs->flavors)) +uint32_t otype = k->specified &AVTAB_TRANSITION + ? d->trans->otype : d->data; +if ((k->specified &AVTAB_TYPE ) &&validate_simpletype (otype, margs->policy, margs->flavors))return -1 ;if ((k->specified &AVTAB_XPERMS ) &&validate_xperms (d->xperms))libsepol/src/services.c +4
-1@@ -1423,7 +1423,10 @@ static int sepol_compute_sid(sepol_security_id_t ssid,if (avdatum) {/* Use the type from the type transition/member/change rule. */ - newcontext.type = avdatum->data; +if (specified &AVTAB_TRANSITION ) + newcontext.type = avdatum->trans->otype; +else + newcontext.type = avdatum->data; }/* Check for class-specific changes. */ libsepol/src/write.c +14
-4@@ -190,14 +190,20 @@ static int avtab_write_item(policydb_t * p,ERR (fp->handle,"missing node" );return POLICYDB_ERROR ; } - buf32[items++] = -cpu_to_le32 (node->datum.data); +uint32_t data = + node->key.specified &AVTAB_TRANSITION + ? node->datum.trans->otype + : node->datum.data; + buf32[items++] =cpu_to_le32 (data); set--; node->merged =1 ; } } }else { - buf32[items++] =cpu_to_le32 (cur->datum.data); +uint32_t data = cur->key.specified &AVTAB_TRANSITION + ? cur->datum.trans->otype + : cur->datum.data; + buf32[items++] =cpu_to_le32 (data); cur->merged =1 ; set--; }@@ -256,6 +262,11 @@ static int avtab_write_item(policydb_t * p, items =put_entry (buf32,sizeof (uint32_t ),8 ,fp);if (items !=8 )return POLICYDB_ERROR ; + }else if (cur->key.specified &AVTAB_TRANSITION ) { + buf32[0 ] =cpu_to_le32 (cur->datum.trans->otype); + items =put_entry (buf32,sizeof (uint32_t ),1 , fp); +if (items !=1 ) +return POLICYDB_ERROR ; }else { buf32[0 ] =cpu_to_le32 (cur->datum.data); items =put_entry (buf32,sizeof (uint32_t ),1 , fp); --2.40.0