remove modular policy support for policy rep branch
cover remove modular policy support for policy rep branch
This patchset removes module support from libsepol. This patchset is in support of the policyrep work.
Patches one and two removes the files that are solely for supporting modular policies.
Patch three and four eliminates the various structures that supported modular policies. All functional changes occur in this patch.
Patch five puts dispol in libsepol's utils directory. Dispol was not modified in this move.
This e-mail splits the original patches up so that they will go through the list-serv without being bounced.
-mdg
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 3/5 << part 1 >> This patch does all the functional work of removing the module support code.
Various unnecessary structs were removed.
At the end of this patch, libsepol is able to load policies, but does not know about modules.
dispol operates as expected when using libsepol at the end of this patch.
---
libsepol/src/policydb.c | 1512 240 + 1272 - 0 !
1 file changed, 240 insertions(+), 1272 deletions(-)
libsepol/src/policydb.c +240 -1272
@@ -44,7 +44,6 @@#include <stdlib.h> #include <sepol/policydb/policydb.h> -#include <sepol/policydb/expand.h> #include <sepol/policydb/conditional.h> #include <sepol/policydb/avrule_block.h> #include <sepol/policydb/util.h>
@@ -57,82 +56,40 @@/* These need to be updated if SYM_NUM or OCON_NUM changes */ static struct policydb_compat_info policydb_compat[] = { { - .type =POLICY_KERN , .version =POLICYDB_VERSION_BASE , .sym_num =SYM_NUM -3 , .ocon_num =OCON_FSUSE +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_BOOL , .sym_num =SYM_NUM -2 , .ocon_num =OCON_FSUSE +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_IPV6 , .sym_num =SYM_NUM -2 , .ocon_num =OCON_NODE6 +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_NLCLASS , .sym_num =SYM_NUM -2 , .ocon_num =OCON_NODE6 +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_MLS , .sym_num =SYM_NUM , .ocon_num =OCON_NODE6 +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_AVTAB , .sym_num =SYM_NUM , .ocon_num =OCON_NODE6 +1 , }, { - .type =POLICY_KERN , .version =POLICYDB_VERSION_RANGETRANS , .sym_num =SYM_NUM , .ocon_num =OCON_NODE6 +1 , }, - { - .type =POLICY_BASE , - .version =MOD_POLICYDB_VERSION_BASE , - .sym_num =SYM_NUM , - .ocon_num =OCON_NODE6 +1 , - }, - { - .type =POLICY_BASE , - .version =MOD_POLICYDB_VERSION_MLS , - .sym_num =SYM_NUM , - .ocon_num =OCON_NODE6 +1 , - }, - { - .type =POLICY_BASE , - .version =MOD_POLICYDB_VERSION_MLS_USERS , - .sym_num =SYM_NUM , - .ocon_num =OCON_NODE6 +1 , - }, - { - .type =POLICY_MOD , - .version =MOD_POLICYDB_VERSION_BASE , - .sym_num =SYM_NUM , - .ocon_num =0 , - }, - { - .type =POLICY_MOD , - .version =MOD_POLICYDB_VERSION_MLS , - .sym_num =SYM_NUM , - .ocon_num =0 , - }, - { - .type =POLICY_MOD , - .version =MOD_POLICYDB_VERSION_MLS_USERS , - .sym_num =SYM_NUM , - .ocon_num =0 }, };#if 0
@@ -157,15 +114,13 @@ static unsigned int symtab_sizes[SYM_NUM 16, }; -struct policydb_compat_info *policydb_lookup_compat (unsigned int version, -unsigned int type) +struct policydb_compat_info *policydb_lookup_compat (unsigned int version) {unsigned int i;struct policydb_compat_info *info =NULL ;for (i =0 ; i <sizeof (policydb_compat) /sizeof (*info); i++) { -if (policydb_compat[i].version == version && - policydb_compat[i].type == type) { +if (policydb_compat[i].version == version) { info = &policydb_compat[i];break ; }
@@ -173,37 +128,11 @@ struct policydb_compat_info *policydb_loreturn info; } -void type_set_init (type_set_t * x) -{ -memset (x,0 ,sizeof (type_set_t)); -ebitmap_init (&x->types); -ebitmap_init (&x->negset); -} - -void type_set_destroy (type_set_t * x) -{ -if (x !=NULL ) { -ebitmap_destroy (&x->types); -ebitmap_destroy (&x->negset); - } -} - -void role_set_init (role_set_t * x) -{ -memset (x,0 ,sizeof (role_set_t)); -ebitmap_init (&x->roles); -} - -void role_set_destroy (role_set_t * x) -{ -ebitmap_destroy (&x->roles); -} -void role_datum_init (role_datum_t * x) {memset (x,0 ,sizeof (role_datum_t));ebitmap_init (&x->dominates); -type_set_init (&x->types); +ebitmap_init (&x->types);ebitmap_init (&x->cache); }
@@ -211,7 +140,7 @@ void role_datum_destroy(role_datum_t * x {if (x !=NULL ) {ebitmap_destroy (&x->dominates); -type_set_destroy (&x->types); +ebitmap_destroy (&x->types);ebitmap_destroy (&x->cache); } }
@@ -232,7 +161,7 @@ void type_datum_destroy(type_datum_t * xvoid user_datum_init (user_datum_t * x) {memset (x,0 ,sizeof (user_datum_t)); -role_set_init (&x->roles); +ebitmap_init (&x->roles);mls_semantic_range_init (&x->range);mls_semantic_level_init (&x->dfltlevel);ebitmap_init (&x->cache);
@@ -243,7 +172,7 @@ void user_datum_init(user_datum_t * x)void user_datum_destroy (user_datum_t * x) {if (x !=NULL ) { -role_set_destroy (&x->roles); +ebitmap_destroy (&x->roles);mls_semantic_range_destroy (&x->range);mls_semantic_level_destroy (&x->dfltlevel);ebitmap_destroy (&x->cache);
@@ -275,131 +204,8 @@ void cat_datum_destroy(cat_datum_t * x _return ; } -void class_perm_node_init (class_perm_node_t * x) -{ -memset (x,0 ,sizeof (class_perm_node_t)); -} - -void avrule_init (avrule_t * x) -{ -memset (x,0 ,sizeof (avrule_t)); -type_set_init (&x->stypes); -type_set_init (&x->ttypes); -} - -void avrule_destroy (avrule_t * x) -{ -class_perm_node_t *cur, *next; - -if (x ==NULL ) { -return ; - } -type_set_destroy (&x->stypes); -type_set_destroy (&x->ttypes); - - next = x->perms; -while (next) { - cur = next; - next = cur->next; -free (cur); - } -} - -void role_trans_rule_init (role_trans_rule_t * x) -{ -memset (x,0 ,sizeof (*x)); -role_set_init (&x->roles); -type_set_init (&x->types); -} - -void role_trans_rule_destroy (role_trans_rule_t * x) -{ -if (x !=NULL ) { -role_set_destroy (&x->roles); -type_set_destroy (&x->types); - } -} - -void role_trans_rule_list_destroy (role_trans_rule_t * x) -{ -while (x !=NULL ) { -role_trans_rule_t *next = x->next; -role_trans_rule_destroy (x); -free (x); - x = next; - } -} - -void role_allow_rule_init (role_allow_rule_t * x) -{ -memset (x,0 ,sizeof (role_allow_rule_t)); -role_set_init (&x->roles); -role_set_init (&x->new_roles); -} - -void role_allow_rule_destroy (role_allow_rule_t * x) -{ -role_set_destroy (&x->roles); -role_set_destroy (&x->new_roles); -} - -void role_allow_rule_list_destroy (role_allow_rule_t * x) -{ -while (x !=NULL ) { -role_allow_rule_t *next = x->next; -role_allow_rule_destroy (x); -free (x); - x = next; - } -} - -void range_trans_rule_init (range_trans_rule_t * x) -{ -type_set_init (&x->stypes); -type_set_init (&x->ttypes); -ebitmap_init (&x->tclasses); -mls_semantic_range_init (&x->trange); - x->next =NULL ; -} - -void range_trans_rule_destroy (range_trans_rule_t * x) -{ -type_set_destroy (&x->stypes); -type_set_destroy (&x->ttypes); -ebitmap_destroy (&x->tclasses); -mls_semantic_range_destroy (&x->trange); -} - -void range_trans_rule_list_destroy (range_trans_rule_t * x) -{ -while (x !=NULL ) { -range_trans_rule_t *next = x->next; -range_trans_rule_destroy (x); -free (x); - x = next; - } -} - -void avrule_list_destroy (avrule_t * x) -{ -avrule_t *next, *cur; - -if (!x) -return ; - - next = x; -while (next) { - cur = next; - next = next->next; -avrule_destroy (cur); -free (cur); - } -} -/* -* Initialize the role table by implicitly adding role 'object_r'. If -* the policy is a module, set object_r's scope to be SCOPE_REQ, -* otherwise set it to SCOPE_DECL. +* Initialize the role table by implicitly adding role 'object_r'. */ static int roles_init (policydb_t * p) {
@@ -418,10 +224,7 @@ static int roles_init(policydb_t * p) goto out_free_role; }strcpy (key,OBJECT_R ); - rc =symtab_insert (p,SYM_ROLES , key, role, - (p->policy_type == -POLICY_MOD ?SCOPE_REQ :SCOPE_DECL ),1 , - &role->s.value); + rc =symtab_insert (p,SYM_ROLES , key, role,1 , &role->s.value);if (rc) goto out_free_key;if (role->s.value !=OBJECT_R_VAL ) {
@@ -454,18 +257,6 @@ int policydb_init(policydb_t * p) goto out_free_symtab; } -/* initialize the module stuff */ -for (i =0 ; i <SYM_NUM ; i++) { -if (symtab_init (&p->scope[i], symtab_sizes[i])) { - goto out_free_symtab; - } - } -if ((p->global =avrule_block_create ()) ==NULL || - (p->global->branch_list =avrule_decl_create (1 )) ==NULL ) { - goto out_free_symtab; - } - p->decl_val_to_struct =NULL ; - rc =avtab_init (&p->te_avtab);if (rc) goto out_free_symtab;
@@ -486,15 +277,19 @@ int policydb_init(policydb_t * p) out_free_symtab:for (i =0 ; i <SYM_NUM ; i++) {hashtab_destroy (p->symtab[i].table); -hashtab_destroy (p->scope[i].table); } -avrule_block_list_destroy (p->global); goto out; } +/* NOTE: Cacheing may or may not make sense when the new policyrep gets +* here. These functions may just go away. Currently this function only +* gets called from inside libsepol. +* - [email protected] 2007-06-21 +*/ int policydb_role_cache (hashtab_key_t key - __attribute__ ((unused)),hashtab_datum_t datum, -void *arg) + __attribute__ ((unused)),hashtab_datum_t datum + __attribute__ ((unused)),void *arg + __attribute__ ((unused))) {policydb_t *p;role_datum_t *role;
@@ -503,16 +298,23 @@ int policydb_role_cache(hashtab_key_t ke p = (policydb_t *) arg;ebitmap_destroy (&role->cache); -if (type_set_expand (&role->types, &role->cache, p,1 )) { +ebitmap_init (&role->cache); +if (ebitmap_cpy (&role->cache, &role->types)) {return -1 ; }return 0 ; } +/* NOTE: Cacheing may or may not make sense when the new policyrep gets +* here. These functions may just go away. Currently this function only +* gets called from inside libsepol. +* - [email protected] 2007-06-21 +*/ int policydb_user_cache (hashtab_key_t key - __attribute__ ((unused)),hashtab_datum_t datum, -void *arg) + __attribute__ ((unused)),hashtab_datum_t datum + __attribute__ ((unused)),void *arg + __attribute__ ((unused))) {policydb_t *p;user_datum_t *user;
@@ -521,28 +323,11 @@ int policydb_user_cache(hashtab_key_t ke p = (policydb_t *) arg;ebitmap_destroy (&user->cache); -if (role_set_expand (&user->roles, &user->cache, p)) { +ebitmap_init (&user->cache); +if (ebitmap_cpy (&user->cache, &user->roles)) {return -1 ; } -/* we do not expand user's MLS info in kernel policies because the -* semantic representation is not present and we do not expand user's -* MLS info in module policies because all of the necessary mls -* information is not present */ -if (p->policy_type !=POLICY_KERN && p->policy_type !=POLICY_MOD ) { -mls_range_destroy (&user->exp_range); -if (mls_semantic_range_expand (&user->range, - &user->exp_range, p,NULL )) { -return -1 ; - } - -mls_level_destroy (&user->exp_dfltlevel); -if (mls_semantic_level_expand (&user->dfltlevel, - &user->exp_dfltlevel, p,NULL )) { -return -1 ; - } - } -return 0 ; }
@@ -723,37 +508,6 @@ int policydb_index_bools(policydb_t * p)return 0 ; } -int policydb_index_decls (policydb_t * p) -{ -avrule_block_t *curblock; -avrule_decl_t *decl; -int num_decls =0 ; - -free (p->decl_val_to_struct); - -for (curblock = p->global; curblock !=NULL ; curblock = curblock->next) { -for (decl = curblock->branch_list; decl !=NULL ; - decl = decl->next) { - num_decls++; - } - } - - p->decl_val_to_struct = -calloc (num_decls,sizeof (*(p->decl_val_to_struct))); -if (!p->decl_val_to_struct) { -return -1 ; - } - -for (curblock = p->global; curblock !=NULL ; curblock = curblock->next) { -for (decl = curblock->branch_list; decl !=NULL ; - decl = decl->next) { - p->decl_val_to_struct[decl->decl_id -1 ] = decl; - } - } - -return 0 ; -} -/* * Define the other val_to_name and val_to_struct arrays * in a policy database structure.
@@ -986,15 +740,6 @@ void policydb_destroy(policydb_t * p)free (p->user_val_to_struct);if (p->type_val_to_struct)free (p->type_val_to_struct); -free (p->decl_val_to_struct); - -for (i =0 ; i <SYM_NUM ; i++) { -hashtab_map (p->scope[i].table, scope_destroy,0 ); -hashtab_destroy (p->scope[i].table); - } -avrule_block_list_destroy (p->global); -free (p->name); -free (p->version);avtab_destroy (&p->te_avtab);
@@ -1085,18 +830,6 @@ void symtabs_destroy(symtab_t * symtab) } } -int scope_destroy (hashtab_key_t key,hashtab_datum_t datum,void *p - __attribute__ ((unused))) -{ -scope_datum_t *cur = (scope_datum_t *) datum; -free (key); -if (cur !=NULL ) { -free (cur->decl_ids); - } -free (cur); -return 0 ; -} -hashtab_destroy_func_t get_symtab_destroy_func (int sym_num) {if (sym_num <0 || sym_num >=SYM_NUM ) {
@@ -1134,16 +867,13 @@ int policydb_load_isids(policydb_t * p, }/* Declare a symbol for a certain avrule_block context. Insert it -* into a symbol table for a policy. This function will handle -* inserting the appropriate scope information in addition to -* inserting the symbol into the hash table. -* +* into a symbol table for a policy. +* * arguments: -* policydb_t *pol module policy to modify +* policydb_t *pol policy to modify * uint32_t sym the symbole table for insertion (SYM_*) * hashtab_key_t key the key for the symbol - not cloned * hashtab_datum_t data the data for the symbol - not cloned -* scope scope of this symbol, either SCOPE_REQ or SCOPE_DECL * avrule_decl_id identifier for this symbol's encapsulating declaration * value (out) assigned value to the symbol (if value is not NULL) *
@@ -1152,17 +882,14 @@ int policydb_load_isids(policydb_t * p,* 1 success, but symbol already existed as a requirement * (datum was not inserted and needs to be free()d) * -1 general error -* -2 scope conflicted * -ENOMEM memory error * error codes from hashtab_insert */ int symtab_insert (policydb_t * pol,uint32_t sym,hashtab_key_t key,hashtab_datum_t datum, -uint32_t scope,uint32_t avrule_decl_id,uint32_t * value) +uint32_t avrule_decl_id __attribute__((unused)),uint32_t * value) {int rc, retval =0 ; -unsigned int i; -scope_datum_t *scope_datum;/* check if the symbol is already there. multiple * declarations of non-roles/non-users are illegal, but
@@ -1176,174 +903,16 @@ int symtab_insert(policydb_t * pol, uint* (i.e. aliases) */ if (value) *value = ++pol->symtab[sym].nprim; - }else if (rc ==SEPOL_EEXIST && scope ==SCOPE_REQ ) { - retval =1 ;/* symbol not added -- need to free() later */ - }else if (rc ==SEPOL_EEXIST && scope ==SCOPE_DECL ) { -if (sym ==SYM_ROLES || sym ==SYM_USERS ) { -/* allow multiple declarations for these two */ - retval =1 ; - }else { -/* duplicate declarations not allowed for all else */ -return -2 ; - } }else {return rc; } -/* get existing scope information; if there is not one then -* create it */ - scope_datum = - (scope_datum_t *)hashtab_search (pol->scope[sym].table, key); -if (scope_datum ==NULL ) { -hashtab_key_t key2 =strdup ((char *)key); -if (!key2) -return -ENOMEM ; -if ((scope_datum =malloc (sizeof (*scope_datum))) ==NULL ) { -free (key2); -return -ENOMEM ; - } - scope_datum->scope = scope; - scope_datum->decl_ids =NULL ; - scope_datum->decl_ids_len =0 ; -if ((rc = -hashtab_insert (pol->scope[sym].table, key2, - scope_datum)) !=0 ) { -free (key2); -free (scope_datum); -return rc; - } - }else if (scope_datum->scope ==SCOPE_DECL ) { -/* disallow multiple declarations for non-roles/users */ -if (sym !=SYM_ROLES && sym !=SYM_USERS ) { -return -2 ; - } - }else if (scope_datum->scope ==SCOPE_REQ && scope ==SCOPE_DECL ) { -/* appending to required symbol only allowed for roles/users */ -if (sym ==SYM_ROLES || sym ==SYM_USERS ) { - scope_datum->scope =SCOPE_DECL ; - }else { -return -2 ; - } - - }else if (scope_datum->scope != scope) { -/* scope does not match */ -return -2 ; - } - -/* search through the pre-existing list to avoid adding duplicates */ -for (i =0 ; i < scope_datum->decl_ids_len; i++) { -if (scope_datum->decl_ids[i] == avrule_decl_id) { -/* already there, so don't modify its scope */ -return retval; - } - } - -if (add_i_to_a (avrule_decl_id, - &scope_datum->decl_ids_len, - &scope_datum->decl_ids) ==-1 ) { -return -ENOMEM ; - } -return retval; } -int type_set_or (type_set_t * dst,type_set_t * a,type_set_t * b) -{ -type_set_init (dst); - -if (ebitmap_or (&dst->types, &a->types, &b->types)) { -return -1 ; - } -if (ebitmap_or (&dst->negset, &a->negset, &b->negset)) { -return -1 ; - } - - dst->flags |= a->flags; - dst->flags |= b->flags; - -return 0 ; -} - -int type_set_cpy (type_set_t * dst,type_set_t * src) -{ -type_set_init (dst); - - dst->flags = src->flags; -if (ebitmap_cpy (&dst->types, &src->types)) -return -1 ; -if (ebitmap_cpy (&dst->negset, &src->negset)) -return -1 ; - -return 0 ; -} - -int type_set_or_eq (type_set_t * dst,type_set_t * other) -{ -int ret; -type_set_t tmp; - -if (type_set_or (&tmp, dst, other)) -return -1 ; -type_set_destroy (dst); - ret =type_set_cpy (dst, &tmp); -type_set_destroy (&tmp); - -return ret; -} - -int role_set_get_role (role_set_t * x,uint32_t role) -{ -if (x->flags &ROLE_STAR ) -return 1 ; - -if (ebitmap_get_bit (&x->roles, role -1 )) { -if (x->flags &ROLE_COMP ) -return 0 ; -else -return 1 ; - }else { -if (x->flags &ROLE_COMP ) -return 1 ; -else -return 0 ; - } -} -/***********************************************************************/ /* everything below is for policy reads */ -/* The following are read functions for module structures */ - -static int role_set_read (role_set_t * r,struct policy_file *fp) -{ -uint32_t *buf; -if (ebitmap_read (&r->roles, fp)) -return -1 ; - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - r->flags =le32_to_cpu (buf[0 ]); - -return 0 ; -} - -static int type_set_read (type_set_t * t,struct policy_file *fp) -{ -uint32_t *buf; - -if (ebitmap_read (&t->types, fp)) -return -1 ; -if (ebitmap_read (&t->negset, fp)) -return -1 ; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - t->flags =le32_to_cpu (buf[0 ]); - -return 0 ; -} -/* * Read a MLS range structure from a policydb binary * representation file.
@@ -1397,155 +966,43 @@ static int mls_read_range_helper(mls_ran }/* -* Read a semantic MLS level structure from a policydb binary -* representation file. +* Read and validate a security context structure +* from a policydb binary representation file. */ -static int mls_read_semantic_level_helper (mls_semantic_level_t * l, -struct policy_file *fp) +static int context_read_and_validate (context_struct_t * c, +policydb_t * p,struct policy_file *fp) { -uint32_t *buf, ncat; -unsigned int i; -mls_semantic_cat_t *cat; - -mls_semantic_level_init (l); +uint32_t *buf; - buf =next_entry (fp,sizeof (uint32_t ) *2 ); + buf =next_entry (fp,sizeof (uint32_t ) *3 );if (!buf) { -ERR (fp->handle,"truncated level" ); - goto bad; +ERR (fp->handle,"context truncated" ); +return -1 ; } - l->sens =le32_to_cpu (buf[0 ]); - - ncat =le32_to_cpu (buf[1 ]); -for (i =0 ; i < ncat; i++) { - cat = (mls_semantic_cat_t *)malloc (sizeof (mls_semantic_cat_t)); -if (!cat) { -ERR (fp->handle,"out of memory" ); - goto bad; - } - -mls_semantic_cat_init (cat); - cat->next = l->cat; - l->cat = cat; - - buf =next_entry (fp,sizeof (uint32_t ) *2 ); -if (!buf) { -ERR (fp->handle,"error reading level categories" ); - goto bad; + c->user =le32_to_cpu (buf[0 ]); + c->role =le32_to_cpu (buf[1 ]); + c->type =le32_to_cpu (buf[2 ]); +if ((p->policyvers >=POLICYDB_VERSION_MLS ) + ) { +if (mls_read_range_helper (&c->range, fp)) { +ERR (fp->handle,"error reading MLS range " +"of context" ); +return -1 ; } - cat->low =le32_to_cpu (buf[0 ]); - cat->high =le32_to_cpu (buf[1 ]); } +if (!policydb_context_isvalid (p, c)) { +ERR (fp->handle,"invalid security context" ); +context_destroy (c); +return -1 ; + }return 0 ; - - bad: -return -EINVAL ; }/* -* Read a semantic MLS range structure from a policydb binary -* representation file. -*/ -static int mls_read_semantic_range_helper (mls_semantic_range_t * r, -struct policy_file *fp) -{ -int rc; - - rc =mls_read_semantic_level_helper (&r->level[0 ], fp); -if (rc) -return rc; - - rc =mls_read_semantic_level_helper (&r->level[1 ], fp); - -return rc; -} - -static int mls_level_to_semantic (mls_level_t * l,mls_semantic_level_t * sl) -{ -unsigned int i; -ebitmap_node_t *cnode; -mls_semantic_cat_t *open_cat =NULL ; - -mls_semantic_level_init (sl); - sl->sens = l->sens; -ebitmap_for_each_bit (&l->cat, cnode, i) { -if (ebitmap_node_get_bit (cnode, i)) { -if (open_cat) -continue ; - open_cat = (mls_semantic_cat_t *) -malloc (sizeof (mls_semantic_cat_t)); -if (!open_cat) -return -1 ; - -mls_semantic_cat_init (open_cat); - open_cat->low = i +1 ; - open_cat->next = sl->cat; - sl->cat = open_cat; - }else { -if (!open_cat) -continue ; - open_cat->high = i; - open_cat =NULL ; - } - } -if (open_cat) - open_cat->high = i; - -return 0 ; -} - -static int mls_range_to_semantic (mls_range_t * r,mls_semantic_range_t * sr) -{ -if (mls_level_to_semantic (&r->level[0 ], &sr->level[0 ])) -return -1 ; - -if (mls_level_to_semantic (&r->level[1 ], &sr->level[1 ])) -return -1 ; - -return 0 ; -} - -/* -* Read and validate a security context structure -* from a policydb binary representation file. -*/ -static int context_read_and_validate (context_struct_t * c, -policydb_t * p,struct policy_file *fp) -{ -uint32_t *buf; - - buf =next_entry (fp,sizeof (uint32_t ) *3 ); -if (!buf) { -ERR (fp->handle,"context truncated" ); -return -1 ; - } - c->user =le32_to_cpu (buf[0 ]); - c->role =le32_to_cpu (buf[1 ]); - c->type =le32_to_cpu (buf[2 ]); -if ((p->policy_type ==POLICY_KERN - && p->policyvers >=POLICYDB_VERSION_MLS ) - || (p->policy_type ==POLICY_BASE - && p->policyvers >=MOD_POLICYDB_VERSION_MLS )) { -if (mls_read_range_helper (&c->range, fp)) { -ERR (fp->handle,"error reading MLS range " -"of context" ); -return -1 ; - } - } - -if (!policydb_context_isvalid (p, c)) { -ERR (fp->handle,"invalid security context" ); -context_destroy (c); -return -1 ; - } -return 0 ; -} - -/* -* The following *_read functions are used to -* read the symbol data from a policy database -* binary representation file. +* The following *_read functions are used to +* read the symbol data from a policy database +* binary representation file. */ static int perm_read(policydb_t * p
@@ -1635,7 +1092,8 @@ static int common_read(policydb_t * p, hreturn -1 ; } -static int read_cons_helper (policydb_t * p,constraint_node_t ** nodep, +static int read_cons_helper (policydb_t * p __attribute__ ((unused)), +constraint_node_t ** nodep,unsigned int ncons,int allowxtarget,struct policy_file *fp) {
@@ -1709,9 +1167,6 @@ static int read_cons_helper(policydb_t * depth++;if (ebitmap_read (&e->names, fp))return -1 ; -if (p->policy_type !=POLICY_KERN && -type_set_read (e->type_names, fp)) -return -1 ;break ;default :return -1 ;
@@ -1787,10 +1242,8 @@ static int class_read(policydb_t * p, haif (read_cons_helper (p, &cladatum->constraints, ncons,0 , fp)) goto bad; -if ((p->policy_type ==POLICY_KERN - && p->policyvers >=POLICYDB_VERSION_VALIDATETRANS ) - || (p->policy_type ==POLICY_BASE - && p->policyvers >=MOD_POLICYDB_VERSION_VALIDATETRANS )) { +if ((p->policyvers >=POLICYDB_VERSION_VALIDATETRANS ) + ) {/* grab the validatetrans rules */ buf =next_entry (fp,sizeof (uint32_t ));if (!buf)
@@ -1842,13 +1295,8 @@ static int role_read(policydb_t * pif (ebitmap_read (&role->dominates, fp)) goto bad; -if (p->policy_type ==POLICY_KERN ) { -if (ebitmap_read (&role->types.types, fp)) - goto bad; - }else { -if (type_set_read (&role->types, fp)) - goto bad; - } +if (ebitmap_read (&role->types, fp)) + goto bad;if (strcmp (key,OBJECT_R ) ==0 ) {if (role->s.value !=OBJECT_R_VAL ) {
@@ -1884,22 +1332,13 @@ static int type_read(policydb_t * pif (!typdatum)return -1 ; -if (p->policy_type ==POLICY_KERN ) { - buf =next_entry (fp,sizeof (uint32_t ) *3 ); - }else { - buf =next_entry (fp,sizeof (uint32_t ) *4 ); - } + buf =next_entry (fp,sizeof (uint32_t ) *3 );if (!buf) goto bad; len =le32_to_cpu (buf[0 ]); typdatum->s.value =le32_to_cpu (buf[1 ]); typdatum->primary =le32_to_cpu (buf[2 ]); -if (p->policy_type !=POLICY_KERN ) { - typdatum->flavor =le32_to_cpu (buf[3 ]); -if (ebitmap_read (&typdatum->types, fp)) - goto bad; - } buf =next_entry (fp, len);if (!buf)
@@ -2274,46 +1713,15 @@ static int user_read(policydb_t * p, hasmemcpy (key, buf, len); key[len] =0 ; -if (p->policy_type ==POLICY_KERN ) { -if (ebitmap_read (&usrdatum->roles.roles, fp)) - goto bad; - }else { -if (role_set_read (&usrdatum->roles, fp)) - goto bad; - } +if (ebitmap_read (&usrdatum->roles, fp)) + goto bad; -/* users were not allowed in mls modules before version -* MOD_POLICYDB_VERSION_MLS_USERS, but they could have been -* required - the mls fields will be empty. user declarations in -* non-mls modules will also have empty mls fields */ -if ((p->policy_type ==POLICY_KERN - && p->policyvers >=POLICYDB_VERSION_MLS ) - || (p->policy_type ==POLICY_MOD - && p->policyvers >=MOD_POLICYDB_VERSION_MLS - && p->policyvers <MOD_POLICYDB_VERSION_MLS_USERS ) - || (p->policy_type ==POLICY_BASE - && p->policyvers >=MOD_POLICYDB_VERSION_MLS - && p->policyvers <MOD_POLICYDB_VERSION_MLS_USERS )) { +if ((p->policyvers >=POLICYDB_VERSION_MLS ) + ) {if (mls_read_range_helper (&usrdatum->exp_range, fp)) goto bad;if (mls_read_level (&usrdatum->exp_dfltlevel, fp)) goto bad; -if (p->policy_type !=POLICY_KERN ) { -if (mls_range_to_semantic (&usrdatum->exp_range, - &usrdatum->range)) - goto bad; -if (mls_level_to_semantic (&usrdatum->exp_dfltlevel, - &usrdatum->dfltlevel)) - goto bad; - } - }else if ((p->policy_type ==POLICY_MOD - && p->policyvers >=MOD_POLICYDB_VERSION_MLS_USERS ) - || (p->policy_type ==POLICY_BASE - && p->policyvers >=MOD_POLICYDB_VERSION_MLS_USERS )) { -if (mls_read_semantic_range_helper (&usrdatum->range, fp)) - goto bad; -if (mls_read_semantic_level_helper (&usrdatum->dfltlevel, fp)) - goto bad; }if (hashtab_insert (h, key, usrdatum))
@@ -2414,80 +1822,12 @@ static int (*read_f[SYM_NUM]) (policydb_ common_read, class_read, role_read, type_read, user_read, cond_read_bool, sens_read, cat_read,}; -/************** module reading functions below **************/ - -static avrule_t *avrule_read (policydb_t * p - __attribute__ ((unused)),struct policy_file *fp) -{ -unsigned int i; -uint32_t *buf, len; -class_perm_node_t *cur, *tail =NULL ; -avrule_t *avrule; - - avrule = (avrule_t *)malloc (sizeof (avrule_t)); -if (!avrule) -return NULL ; - -avrule_init (avrule); - - buf =next_entry (fp,sizeof (uint32_t ) *2 ); -if (!buf) - goto bad; - - (avrule)->specified =le32_to_cpu (buf[0 ]); - (avrule)->flags =le32_to_cpu (buf[1 ]); - -if (type_set_read (&avrule->stypes, fp)) - goto bad; - -if (type_set_read (&avrule->ttypes, fp)) - goto bad; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) - goto bad; - len =le32_to_cpu (buf[0 ]); - -for (i =0 ; i < len; i++) { - cur = (class_perm_node_t *)malloc (sizeof (class_perm_node_t)); -if (!cur) - goto bad; -class_perm_node_init (cur); - - buf =next_entry (fp,sizeof (uint32_t ) *2 ); -if (!buf) { -free (cur); - goto bad; - } - - cur->class =le32_to_cpu (buf[0 ]); - cur->data =le32_to_cpu (buf[1 ]); - -if (!tail) { - avrule->perms = cur; - }else { - tail->next = cur; - } - tail = cur; - } - -return avrule; - bad: -if (avrule) { -avrule_destroy (avrule); -free (avrule); - } -return NULL ; -} -static int range_read (policydb_t * p,struct policy_file *fp) {uint32_t *buf, nel;range_trans_t *rt, *lrt; -range_trans_rule_t *rtr, *lrtr =NULL ;unsigned int i; -int new_rangetr = (p->policy_type ==POLICY_KERN && - p->policyvers >=POLICYDB_VERSION_RANGETRANS ); +int new_rangetr = (p->policyvers >=POLICYDB_VERSION_RANGETRANS ); buf =next_entry (fp,sizeof (uint32_t ));if (!buf)
@@ -2519,417 +1859,9 @@ static int range_read(policydb_t * p, st lrt = rt; } -/* if this is a kernel policy, we are done - otherwise we need to -* convert these structs to range_trans_rule_ts */ -if (p->policy_type ==POLICY_KERN ) -return 0 ; - -/* create range_trans_rules_ts that correspond to the range_trans_ts -* that were just read in from an older policy */ -for (rt = p->range_tr; rt; rt = rt->next) { - rtr =malloc (sizeof (range_trans_rule_t)); -if (!rtr) { -return -1 ; - } -range_trans_rule_init (rtr); - -if (lrtr) - lrtr->next = rtr; -else - p->global->enabled->range_tr_rules = rtr; - -if (ebitmap_set_bit (&rtr->stypes.types, rt->source_type -1 ,1 )) -return -1 ; - -if (ebitmap_set_bit (&rtr->ttypes.types, rt->target_type -1 ,1 )) -return -1 ; - -if (ebitmap_set_bit (&rtr->tclasses, rt->target_class -1 ,1 )) -return -1 ; - -if (mls_range_to_semantic (&rt->target_range, &rtr->trange)) -return -1 ; - - lrtr = rtr; - } - -/* now destroy the range_trans_ts */ - lrt =NULL ; -for (rt = p->range_tr; rt; rt = rt->next) { -if (lrt) { -ebitmap_destroy (&lrt->target_range.level[0 ].cat); -ebitmap_destroy (&lrt->target_range.level[1 ].cat); -free (lrt); - } - lrt = rt; - } -if (lrt) { -ebitmap_destroy (&lrt->target_range.level[0 ].cat); -ebitmap_destroy (&lrt->target_range.level[1 ].cat); -free (lrt); - } - p->range_tr =NULL ; - -return 0 ; -} - -int avrule_read_list (policydb_t * p,avrule_t ** avrules, -struct policy_file *fp) -{ -unsigned int i; -avrule_t *cur, *tail; -uint32_t *buf, len; - - *avrules = tail =NULL ; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) { -return -1 ; - } - len =le32_to_cpu (buf[0 ]); - -for (i =0 ; i < len; i++) { - cur =avrule_read (p, fp); -if (!cur) { -return -1 ; - } - -if (!tail) { - *avrules = cur; - }else { - tail->next = cur; - } - tail = cur; - } - -return 0 ; -} - -static int role_trans_rule_read (role_trans_rule_t ** r,struct policy_file *fp) -{ -uint32_t *buf, nel; -unsigned int i; -role_trans_rule_t *tr, *ltr; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - nel =le32_to_cpu (buf[0 ]); - ltr =NULL ; -for (i =0 ; i < nel; i++) { - tr =malloc (sizeof (role_trans_rule_t)); -if (!tr) { -return -1 ; - } -role_trans_rule_init (tr); - -if (ltr) { - ltr->next = tr; - }else { - *r = tr; - } - -if (role_set_read (&tr->roles, fp)) -return -1 ; - -if (type_set_read (&tr->types, fp)) -return -1 ; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - tr->new_role =le32_to_cpu (buf[0 ]); - ltr = tr; - } -return 0 ; } -static int role_allow_rule_read (role_allow_rule_t ** r,struct policy_file *fp) -{ -unsigned int i; -uint32_t *buf, nel; -role_allow_rule_t *ra, *lra; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - nel =le32_to_cpu (buf[0 ]); - lra =NULL ; -for (i =0 ; i < nel; i++) { - ra =malloc (sizeof (role_allow_rule_t)); -if (!ra) { -return -1 ; - } -role_allow_rule_init (ra); - -if (lra) { - lra->next = ra; - }else { - *r = ra; - } - -if (role_set_read (&ra->roles, fp)) -return -1 ; - -if (role_set_read (&ra->new_roles, fp)) -return -1 ; - - lra = ra; - } -return 0 ; -} - -static int range_trans_rule_read (range_trans_rule_t ** r, -struct policy_file *fp) -{ -uint32_t *buf, nel; -unsigned int i; -range_trans_rule_t *rt, *lrt =NULL ; - - buf =next_entry (fp,sizeof (uint32_t )); -if (!buf) -return -1 ; - nel =le32_to_cpu (buf[0 ]); -for (i =0 ; i < nel; i++) { - rt =malloc (sizeof (range_trans_rule_t)); -if (!rt) { -return -1 ; - } -range_trans_rule_init (rt); - -if (lrt) - lrt->next = rt; -else - *r = rt; - -if (type_set_read (&rt->stypes, fp)) -return -1 ; - -if (type_set_read (&rt->ttypes, fp)) -return -1 ; - -if (ebitmap_read (&rt->tclasses, fp)) -return -1 ; - -if (mls_read_semantic_range_helper (&rt->trange, fp)) -return -1 ; - - lrt = rt; - } - -return 0 ; -} - -static int scope_index_read (scope_index_t * scope_index, -unsigned int num_scope_syms,struct policy_file *fp) -{ -unsigned int i; -uint32_t *buf; -for (i =0 ; i < num_scope_syms; i++) { -if (ebitmap_read (scope_index->scope + i, fp) ==-1 ) { -return -1 ; - } - } -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { -return -1 ; - } - scope_index->class_perms_len =le32_to_cpu (buf[0 ]); -if (scope_index->class_perms_len ==0 ) { - scope_index->class_perms_map =NULL ; -return 0 ; - } -if ((scope_index->class_perms_map = -calloc (scope_index->class_perms_len, -sizeof (*scope_index->class_perms_map))) ==NULL ) { -return -1 ; - } -for (i =0 ; i < scope_index->class_perms_len; i++) { -if (ebitmap_read (scope_index->class_perms_map + i, fp) ==-1 ) { -return -1 ; - } - } -return 0 ; -} - -static int avrule_decl_read (policydb_t * p,avrule_decl_t * decl, -unsigned int num_scope_syms,struct policy_file *fp) -{ -uint32_t *buf, nprim, nel; -unsigned int i, j; -if ((buf =next_entry (fp,sizeof (uint32_t ) *2 )) ==NULL ) { -return -1 ; - } - decl->decl_id =le32_to_cpu (buf[0 ]); - decl->enabled =le32_to_cpu (buf[1 ]); -if (cond_read_list (p, &decl->cond_list, fp) ==-1 || -avrule_read_list (p, &decl->avrules, fp) ==-1 || -role_trans_rule_read (&decl->role_tr_rules, fp) ==-1 || -role_allow_rule_read (&decl->role_allow_rules, fp) ==-1 ) { -return -1 ; - } -if (p->policyvers >=MOD_POLICYDB_VERSION_RANGETRANS && -range_trans_rule_read (&decl->range_tr_rules, fp) ==-1 ) { -return -1 ; - } -if (scope_index_read (&decl->required, num_scope_syms, fp) ==-1 || -scope_index_read (&decl->declared, num_scope_syms, fp) ==-1 ) { -return -1 ; - } - -for (i =0 ; i < num_scope_syms; i++) { -if ((buf =next_entry (fp,sizeof (uint32_t ) *2 )) ==NULL ) { -return -1 ; - } - nprim =le32_to_cpu (buf[0 ]); - nel =le32_to_cpu (buf[1 ]); -for (j =0 ; j < nel; j++) { -if (read_f[i] (p, decl->symtab[i].table, fp)) { -return -1 ; - } - } - decl->symtab[i].nprim = nprim; - } -return 0 ; -} - -static int avrule_block_read (policydb_t * p, -avrule_block_t ** block, -unsigned int num_scope_syms, -struct policy_file *fp) -{ -avrule_block_t *last_block =NULL , *curblock; -uint32_t *buf, num_blocks, nel; - -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { -return -1 ; - } - num_blocks =le32_to_cpu (buf[0 ]); - nel = num_blocks; -while (num_blocks >0 ) { -avrule_decl_t *last_decl =NULL , *curdecl; -uint32_t num_decls; -if ((curblock =calloc (1 ,sizeof (*curblock))) ==NULL ) { -return -1 ; - } - -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { -free (curblock); -return -1 ; - } -/* if this is the first block its non-optional, else its optional */ -if (num_blocks != nel) - curblock->flags |=AVRULE_OPTIONAL ; - - num_decls =le32_to_cpu (buf[0 ]); -while (num_decls >0 ) { -if ((curdecl =avrule_decl_create (0 )) ==NULL ) { -avrule_block_destroy (curblock); -return -1 ; - } -if (avrule_decl_read (p, curdecl, num_scope_syms, fp) == --1 ) { -avrule_decl_destroy (curdecl); -avrule_block_destroy (curblock); -return -1 ; - } -if (curdecl->enabled) { -if (curblock->enabled !=NULL ) { -/* probably a corrupt file */ -avrule_decl_destroy (curdecl); -avrule_block_destroy (curblock); -return -1 ; - } - curblock->enabled = curdecl; - } -/* one must be careful to reconstruct the -* decl chain in its correct order */ -if (curblock->branch_list ==NULL ) { - curblock->branch_list = curdecl; - }else { - last_decl->next = curdecl; - } - last_decl = curdecl; - num_decls--; - } - -if (*block ==NULL ) { - *block = curblock; - }else { - last_block->next = curblock; - } - last_block = curblock; - - num_blocks--; - } - -return 0 ; -} - -static int scope_read (policydb_t * p,int symnum,struct policy_file *fp) -{ -scope_datum_t *scope =NULL ; -uint32_t *buf; -char *key =NULL ; -size_t key_len; -unsigned int i; -hashtab_t h = p->scope[symnum].table; - -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { - goto cleanup; - } - key_len =le32_to_cpu (buf[0 ]); -if ((buf =next_entry (fp, key_len)) ==NULL ) { - goto cleanup; - } -if ((key =malloc (key_len +1 )) ==NULL ) { - goto cleanup; - } -memcpy (key, buf, key_len); - key[key_len] ='\0' ; - -/* ensure that there already exists a symbol with this key */ -if (hashtab_search (p->symtab[symnum].table, key) ==NULL ) { - goto cleanup; - } - -if ((scope =calloc (1 ,sizeof (*scope))) ==NULL ) { - goto cleanup; - } -if ((buf =next_entry (fp,sizeof (uint32_t ) *2 )) ==NULL ) { - goto cleanup; - } - scope->scope =le32_to_cpu (buf[0 ]); - scope->decl_ids_len =le32_to_cpu (buf[1 ]); -assert (scope->decl_ids_len >0 ); -if ((scope->decl_ids = -malloc (scope->decl_ids_len *sizeof (uint32_t ))) ==NULL ) { - goto cleanup; - } -if ((buf = -next_entry (fp,sizeof (uint32_t ) * scope->decl_ids_len)) ==NULL ) { - goto cleanup; - } -for (i =0 ; i < scope->decl_ids_len; i++) { - scope->decl_ids[i] =le32_to_cpu (buf[i]); - } - -if (strcmp (key,"object_r" ) ==0 && h == p->p_roles_scope.table) { -/* object_r was already added to this table in roles_init() */ -scope_destroy (key, scope,NULL ); - }else { -if (hashtab_insert (h, key, scope)) { - goto cleanup; - } - } - -return 0 ; - - cleanup: -scope_destroy (key, scope,NULL ); -return -1 ; -} -/* * Read the configuration data from a policy database binary * representation file into a policy database structure.
@@ -2942,7 +1874,7 @@ int policydb_read(policydb_t * p, structsize_t len, nprim, nel;char *policydb_str, *target_str =NULL ;struct policydb_compat_info *info; -unsigned int policy_type, bufindex; +unsigned int bufindex;ebitmap_node_t *tnode; config =0 ;
@@ -2955,15 +1887,11 @@ int policydb_read(policydb_t * p, struct buf[i] =le32_to_cpu (buf[i]);if (buf[0 ] ==POLICYDB_MAGIC ) { - policy_type =POLICY_KERN ; target_str =POLICYDB_STRING ; - }else if (buf[0 ] ==POLICYDB_MOD_MAGIC ) { - policy_type =POLICY_MOD ; - target_str =POLICYDB_MOD_STRING ; }else {ERR (fp->handle,"policydb magic number %#08x does not " -"match expected magic number %#08x or %#08x" , - buf[0 ],POLICYDB_MAGIC ,POLICYDB_MOD_MAGIC ); +"match expected magic number %#08x" , + buf[0 ],POLICYDB_MAGIC );return POLICYDB_ERROR ; }
@@ -2997,11 +1925,8 @@ int policydb_read(policydb_t * p, structfree (policydb_str); policydb_str =NULL ; -/* Read the version, config, and table sizes (and policy type if it's a module). */ -if (policy_type ==POLICY_KERN ) - nel =4 ; -else - nel =5 ; +/* Read the version, config, and table sizes. */ + nel =4 ; buf =next_entry (fp,sizeof (uint32_t ) * nel);if (!buf)
@@ -3011,44 +1936,17 @@ int policydb_read(policydb_t * p, struct bufindex =0 ; -if (policy_type ==POLICY_MOD ) { -/* We know it's a module but not whether it's a base -module or regular binary policy module. buf[0] -tells us which. */ - policy_type = buf[bufindex]; -if (policy_type !=POLICY_MOD && policy_type !=POLICY_BASE ) { -ERR (fp->handle,"unknown module type: %#08x" , - policy_type); -return POLICYDB_ERROR ; - } - bufindex++; - } - r_policyvers = buf[bufindex]; -if (policy_type ==POLICY_KERN ) { -if (r_policyvers <POLICYDB_VERSION_MIN || - r_policyvers >POLICYDB_VERSION_MAX ) { -ERR (fp->handle,"policydb version %d does not match " -"my version range %d-%d" , buf[bufindex], -POLICYDB_VERSION_MIN ,POLICYDB_VERSION_MAX ); -return POLICYDB_ERROR ; - } - }else if (policy_type ==POLICY_BASE || policy_type ==POLICY_MOD ) { -if (r_policyvers <MOD_POLICYDB_VERSION_MIN || - r_policyvers >MOD_POLICYDB_VERSION_MAX ) { -ERR (fp->handle,"policydb module version %d does " -"not match my version range %d-%d" , - buf[bufindex],MOD_POLICYDB_VERSION_MIN , -MOD_POLICYDB_VERSION_MAX ); -return POLICYDB_ERROR ; - } - }else { -assert (0 ); +if (r_policyvers <POLICYDB_VERSION_MIN || + r_policyvers >POLICYDB_VERSION_MAX ) { +ERR (fp->handle,"policydb version %d does not match " +"my version range %d-%d" , buf[bufindex], +POLICYDB_VERSION_MIN ,POLICYDB_VERSION_MAX ); +return POLICYDB_ERROR ; } bufindex++;/* Set the policy type and version from the read values. */ - p->policy_type = policy_type; p->policyvers = r_policyvers;if (buf[bufindex] &POLICYDB_CONFIG_MLS ) {
@@ -3059,7 +1957,7 @@ int policydb_read(policydb_t * p, struct bufindex++; - info =policydb_lookup_compat (r_policyvers, policy_type); + info =policydb_lookup_compat (r_policyvers);if (!info) {ERR (fp->handle,"unable to find policy compat info " "for version %d" , r_policyvers);
@@ -3075,79 +1973,31 @@ int policydb_read(policydb_t * p, struct goto bad; } -if (p->policy_type ==POLICY_MOD ) { -/* Get the module name and version */ -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { - goto bad; - } - len =le32_to_cpu (buf[0 ]); -if ((buf =next_entry (fp, len)) ==NULL ) { - goto bad; - } -if ((p->name =malloc (len +1 )) ==NULL ) { - goto bad; - } -memcpy (p->name, buf, len); - p->name[len] ='\0' ; -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { - goto bad; - } - len =le32_to_cpu (buf[0 ]); -if ((buf =next_entry (fp, len)) ==NULL ) { - goto bad; - } -if ((p->version =malloc (len +1 )) ==NULL ) { - goto bad; - } -memcpy (p->version, buf, len); - p->version[len] ='\0' ; - } -for (i =0 ; i < info->sym_num; i++) { buf =next_entry (fp,sizeof (uint32_t ) *2 ); -if (!buf) +if (!buf){ goto bad; + } nprim =le32_to_cpu (buf[0 ]); nel =le32_to_cpu (buf[1 ]);for (j =0 ; j < nel; j++) { -if (read_f[i] (p, p->symtab[i].table, fp)) +if (read_f[i] (p, p->symtab[i].table, fp)){ +fprintf (stderr,"i = %d\tj = %d\n" , i, j); goto bad; + } } p->symtab[i].nprim = nprim; } -if (policy_type ==POLICY_KERN ) { -if (avtab_read (&p->te_avtab, fp, r_policyvers)) - goto bad; -if (r_policyvers >=POLICYDB_VERSION_BOOL ) -if (cond_read_list (p, &p->cond_list, fp)) - goto bad; -if (role_trans_read (&p->role_tr, fp)) - goto bad; -if (role_allow_read (&p->role_allow, fp)) - goto bad; - }else { -/* first read the AV rule blocks, then the scope tables */ -avrule_block_destroy (p->global); - p->global =NULL ; -if (avrule_block_read (p, &p->global, info->sym_num, fp) ==-1 ) { +if (avtab_read (&p->te_avtab, fp, r_policyvers)) + goto bad; +if (r_policyvers >=POLICYDB_VERSION_BOOL ) +if (cond_read_list (p, &p->cond_list, fp)) goto bad; - } -for (i =0 ; i < info->sym_num; i++) { -if ((buf =next_entry (fp,sizeof (uint32_t ))) ==NULL ) { - goto bad; - } - nel =le32_to_cpu (buf[0 ]); -for (j =0 ; j < nel; j++) { -if (scope_read (p, i, fp)) - goto bad; - } - } - - } - -if (policydb_index_decls (p)) +if (role_trans_read (&p->role_tr, fp)) + goto bad; +if (role_allow_read (&p->role_allow, fp)) goto bad;if (policydb_index_classes (p))
@@ -3164,43 +2014,36 @@ int policydb_read(policydb_t * p, struct goto bad; } -if ((p->policy_type ==POLICY_KERN - && p->policyvers >=POLICYDB_VERSION_MLS ) - || (p->policy_type ==POLICY_BASE - && p->policyvers >=MOD_POLICYDB_VERSION_MLS - && p->policyvers <MOD_POLICYDB_VERSION_RANGETRANS )) { +if ((p->policyvers >=POLICYDB_VERSION_MLS ) + ) {if (range_read (p, fp)) { goto bad; } } -if (policy_type ==POLICY_KERN ) { - p->type_attr_map =malloc (p->p_types.nprim *sizeof (ebitmap_t)); - p->attr_type_map =malloc (p->p_types.nprim *sizeof (ebitmap_t)); -if (!p->type_attr_map || !p->attr_type_map) - goto bad; -for (i =0 ; i < p->p_types.nprim; i++) { -ebitmap_init (&p->type_attr_map[i]); -ebitmap_init (&p->attr_type_map[i]); - } -for (i =0 ; i < p->p_types.nprim; i++) { -if (r_policyvers >=POLICYDB_VERSION_AVTAB ) { -if (ebitmap_read (&p->type_attr_map[i], fp)) + p->type_attr_map =malloc (p->p_types.nprim *sizeof (ebitmap_t)); + p->attr_type_map =malloc (p->p_types.nprim *sizeof (ebitmap_t)); +if (!p->type_attr_map || !p->attr_type_map) + goto bad; +for (i =0 ; i < p->p_types.nprim; i++) { +ebitmap_init (&p->type_attr_map[i]); +ebitmap_init (&p->attr_type_map[i]); + } +for (i =0 ; i < p->p_types.nprim; i++) { +if (r_policyvers >=POLICYDB_VERSION_AVTAB ) { +if (ebitmap_read (&p->type_attr_map[i], fp)) + goto bad; +ebitmap_for_each_bit (&p->type_attr_map[i], tnode, j) { +if (!ebitmap_node_get_bit (tnode, j) + || i == j) +continue ; +if (ebitmap_set_bit (&p->attr_type_map[j], i,1 )) goto bad; -ebitmap_for_each_bit (&p->type_attr_map[i], - tnode, j) { -if (!ebitmap_node_get_bit (tnode, j) - || i == j) -continue ; -if (ebitmap_set_bit - (&p->attr_type_map[j], i,1 )) - goto bad; - } } -/* add the type itself as the degenerate case */ -if (ebitmap_set_bit (&p->type_attr_map[i], i,1 )) - goto bad; } +/* add the type itself as the degenerate case */ +if (ebitmap_set_bit (&p->type_attr_map[i], i,1 )) + goto bad; }return POLICYDB_SUCCESS ;
@@ -3236,3 +2079,128 @@ int policydb_reindex_users(policydb_t *return 0 ; } + +struct expand_avtab_data { +avtab_t *expa; +policydb_t *p; + +}; + +static int expand_avtab_insert (avtab_t * a,avtab_key_t * k,avtab_datum_t * d) +{ +avtab_ptr_t node; +avtab_datum_t *avd; +int rc; + + node =avtab_search_node (a, k); +if (!node) { + rc =avtab_insert (a, k, d); +if (rc) +fprintf (stderr,"Out of memory!" ); +return rc; + } + +if ((k->specified &AVTAB_ENABLED ) != + (node->key.specified &AVTAB_ENABLED )) { + node =avtab_insert_nonunique (a, k, d); +if (!node) { +fprintf (stderr,"Out of memory!" ); +return -1 ; + } +return 0 ; + } + + avd = &node->datum; +switch (k->specified & ~AVTAB_ENABLED ) { +case AVTAB_ALLOWED : +case AVTAB_AUDITALLOW : + avd->data |= d->data; +break ; +case AVTAB_AUDITDENY : + avd->data &= d->data; +break ; +default : +fprintf (stderr,"Type conflict!" ); +return -1 ; + } + +return 0 ; +} + +static int expand_avtab_node (avtab_key_t * k,avtab_datum_t * d,void *args) +{ +struct expand_avtab_data *ptr = args; +avtab_t *expa = ptr->expa; +policydb_t *p = ptr->p; +type_datum_t *stype = p->type_val_to_struct[k->source_type -1 ]; +type_datum_t *ttype = p->type_val_to_struct[k->target_type -1 ]; +ebitmap_t *sattr = &p->attr_type_map[k->source_type -1 ]; +ebitmap_t *tattr = &p->attr_type_map[k->target_type -1 ]; +ebitmap_node_t *snode, *tnode; +unsigned int i, j; +avtab_key_t newkey; +int rc; + + newkey.target_class = k->target_class; + newkey.specified = k->specified; + +if (stype && ttype) { +/* Both are individual types, no expansion required. */ +return expand_avtab_insert (expa, k, d); + } + +if (stype) { +/* Source is an individual type, target is an attribute. */ + newkey.source_type = k->source_type; +ebitmap_for_each_bit (tattr, tnode, j) { +if (!ebitmap_node_get_bit (tnode, j)) +continue ; + newkey.target_type = j +1 ; + rc =expand_avtab_insert (expa, &newkey, d); +if (rc) +return -1 ; + } +return 0 ; + } + +if (ttype) { +/* Target is an individual type, source is an attribute. */ + newkey.target_type = k->target_type; +ebitmap_for_each_bit (sattr, snode, i) { +if (!ebitmap_node_get_bit (snode, i)) +continue ; + newkey.source_type = i +1 ; + rc =expand_avtab_insert (expa, &newkey, d); +if (rc) +return -1 ; + } +return 0 ; + } + +/* Both source and target type are attributes. */ +ebitmap_for_each_bit (sattr, snode, i) { +if (!ebitmap_node_get_bit (snode, i)) +continue ; +ebitmap_for_each_bit (tattr, tnode, j) { +if (!ebitmap_node_get_bit (tnode, j)) +continue ; + newkey.source_type = i +1 ; + newkey.target_type = j +1 ; + rc =expand_avtab_insert (expa, &newkey, d); +if (rc) +return -1 ; + } + } + +return 0 ; +} + +int expand_avtab (policydb_t * p,avtab_t * a,avtab_t * expa) +{ +struct expand_avtab_data data; + + data.expa = expa; + data.p = p; +return avtab_map (a, expand_avtab_node, &data); +} +
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 5/5 This patch moves dispol to libsepol utilities. No functional changes to dispol. Makefile was modified to build dispol.
In reply to Mark Goldman---
libsepol/utils/Makefile | 3 3 + 0 - 0 !
libsepol/utils/dispol.c | 442 442 + 0 - 0 !
2 files changed, 445 insertions(+)
libsepol/utils/dispol.c new file +442 -0
@@ -0,0 +1,442 @@ + +/* Authors: Frank Mayer <[email protected]> and Karl MacMillan <[email protected]> +* +* Copyright (C) 2003 Tresys Technology, LLC +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2. +*/ + +/* +* displaypol.c +* +* Test program to the contents of a binary policy in text +* form. This program currently only displays the +* avtab (including conditional avtab) rules. +* +* displaypol binary_pol_file +*/ + +#include <sepol/policydb/policydb.h> +#include <sepol/policydb/avtab.h> +#include <sepol/policydb/services.h> +#include <sepol/policydb/conditional.h> +#include <sepol/policydb/util.h> +#include <getopt.h> +#include <assert.h> +#include <unistd.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/mman.h> +#include <errno.h> +#include <stdio.h> +#include <fcntl.h> + +static policydb_t policydb; + +void usage (char *progname) +{ +printf ("usage: %s binary_pol_file\n\n" , progname); +exit (1 ); +} + +int render_access_mask (uint32_t mask,avtab_key_t * key,policydb_t * p, +FILE * fp) +{ +char *perm; +fprintf (fp,"{" ); + perm =sepol_av_to_string (p, key->target_class, mask); +if (perm) +fprintf (fp,"%s " , perm); +fprintf (fp,"}" ); +return 0 ; +} + +int render_type (uint32_t type,policydb_t * p,FILE * fp) +{ +fprintf (fp,"%s" , p->p_type_val_to_name[type -1 ]); +return 0 ; +} + +int render_key (avtab_key_t * key,policydb_t * p,FILE * fp) +{ +char *stype, *ttype, *tclass; + stype = p->p_type_val_to_name[key->source_type -1 ]; + ttype = p->p_type_val_to_name[key->target_type -1 ]; + tclass = p->p_class_val_to_name[key->target_class -1 ]; +if (stype && ttype) +fprintf (fp,"%s %s : %s " , stype, ttype, tclass); +else if (stype) +fprintf (fp,"%s %u : %s " , stype, key->target_type, tclass); +else if (ttype) +fprintf (fp,"%u %s : %s " , key->source_type, ttype, tclass); +else +fprintf (fp,"%u %u : %s " , key->source_type, key->target_type, + tclass); +return 0 ; +} + +/* 'what' values for this function */ +#define RENDER_UNCONDITIONAL 0x0001/* render all regardless of enabled state */ +#define RENDER_ENABLED 0x0002 +#define RENDER_DISABLED 0x0004 +#define RENDER_CONDITIONAL (RENDER_ENABLED|RENDER_DISABLED) + +int render_av_rule (avtab_key_t * key,avtab_datum_t * datum,uint32_t what, +policydb_t * p,FILE * fp) +{ +if (!(what &RENDER_UNCONDITIONAL )) { +if (what !=RENDER_CONDITIONAL && (((what &RENDER_ENABLED ) + && !(key-> + specified & +AVTAB_ENABLED )) + || ((what &RENDER_DISABLED ) + && (key-> + specified & +AVTAB_ENABLED )))) { +return 0 ;/* doesn't match selection criteria */ + } + } + +if (!(what &RENDER_UNCONDITIONAL )) { +if (key->specified &AVTAB_ENABLED ) +fprintf (fp,"[enabled] " ); +else if (!(key->specified &AVTAB_ENABLED )) +fprintf (fp,"[disabled] " ); + } + +if (key->specified &AVTAB_AV ) { +if (key->specified &AVTAB_ALLOWED ) { +fprintf (fp,"allow " ); +render_key (key, p, fp); +render_access_mask (datum->data, key, p, fp); +fprintf (fp,";\n" ); + } +if (key->specified &AVTAB_AUDITALLOW ) { +fprintf (fp,"auditallow " ); +render_key (key, p, fp); +render_access_mask (datum->data, key, p, fp); +fprintf (fp,";\n" ); + } +if (key->specified &AVTAB_AUDITDENY ) { +fprintf (fp,"dontaudit " ); +render_key (key, p, fp); +/* We inverse the mask for dontaudit since the mask is internally stored +* as a auditdeny mask */ +render_access_mask (~datum->data, key, p, fp); +fprintf (fp,";\n" ); + } + }else if (key->specified &AVTAB_TYPE ) { +if (key->specified &AVTAB_TRANSITION ) { +fprintf (fp,"type_transition " ); +render_key (key, p, fp); +render_type (datum->data, p, fp); +fprintf (fp,";\n" ); + } +if (key->specified &AVTAB_MEMBER ) { +fprintf (fp,"type_member " ); +render_key (key, p, fp); +render_type (datum->data, p, fp); +fprintf (fp,";\n" ); + } +if (key->specified &AVTAB_CHANGE ) { +fprintf (fp,"type_change " ); +render_key (key, p, fp); +render_type (datum->data, p, fp); +fprintf (fp,";\n" ); + } + }else { +fprintf (fp," ERROR: no valid rule type specified\n" ); +return -1 ; + } +return 0 ; +} + +int display_avtab (avtab_t * a,uint32_t what,policydb_t * p,FILE * fp) +{ +int i; +avtab_ptr_t cur; +avtab_t expa; + +if (avtab_init (&expa)) + goto oom; +if (expand_avtab (p, a, &expa)) { +avtab_destroy (&expa); + goto oom; + } + +/* hmm...should have used avtab_map. */ +for (i =0 ; i <AVTAB_SIZE ; i++) { +for (cur = expa.htable[i]; cur; cur = cur->next) { +render_av_rule (&cur->key, &cur->datum, what, p, fp); + } + } +avtab_destroy (&expa); +fprintf (fp,"\n" ); +return 0 ; + oom: +fprintf (stderr,"out of memory\n" ); +return 1 ; +} + +int display_bools (policydb_t * p,FILE * fp) +{ +int i; + +for (i =0 ; i < p->p_bools.nprim; i++) { +fprintf (fp,"%s : %d\n" , p->p_bool_val_to_name[i], + p->bool_val_to_struct[i]->state); + } +return 0 ; +} + +void display_expr (policydb_t * p,cond_expr_t * exp,FILE * fp) +{ + +cond_expr_t *cur; +for (cur = exp; cur !=NULL ; cur = cur->next) { +switch (cur->expr_type) { +case COND_BOOL : +fprintf (fp,"%s " , + p->p_bool_val_to_name[cur->bool -1 ]); +break ; +case COND_NOT : +fprintf (fp,"! " ); +break ; +case COND_OR : +fprintf (fp,"|| " ); +break ; +case COND_AND : +fprintf (fp,"&& " ); +break ; +case COND_XOR : +fprintf (fp,"^ " ); +break ; +case COND_EQ : +fprintf (fp,"== " ); +break ; +case COND_NEQ : +fprintf (fp,"!= " ); +break ; +default : +fprintf (fp,"error!" ); +break ; + } + } +} + +int display_cond_expressions (policydb_t * p,FILE * fp) +{ +cond_node_t *cur; +cond_av_list_t *av_cur, *expl =NULL ; +avtab_t expa; + +for (cur = p->cond_list; cur !=NULL ; cur = cur->next) { +fprintf (fp,"expression: " ); +display_expr (p, cur->expr, fp); +fprintf (fp,"current state: %d\n" , cur->cur_state); +fprintf (fp,"True list:\n" ); +if (avtab_init (&expa)) + goto oom; +if (expand_cond_av_list (p, cur->true_list, &expl, &expa)) { +avtab_destroy (&expa); + goto oom; + } +for (av_cur = expl; av_cur !=NULL ; av_cur = av_cur->next) { +fprintf (fp,"\t" ); +render_av_rule (&av_cur->node->key, &av_cur->node->datum, +RENDER_CONDITIONAL , p, fp); + } +cond_av_list_destroy (expl); +avtab_destroy (&expa); +fprintf (fp,"False list:\n" ); +if (avtab_init (&expa)) + goto oom; +if (expand_cond_av_list (p, cur->false_list, &expl, &expa)) { +avtab_destroy (&expa); + goto oom; + } +for (av_cur = expl; av_cur !=NULL ; av_cur = av_cur->next) { +fprintf (fp,"\t" ); +render_av_rule (&av_cur->node->key, &av_cur->node->datum, +RENDER_CONDITIONAL , p, fp); + } +cond_av_list_destroy (expl); +avtab_destroy (&expa); + } +return 0 ; + + oom: +fprintf (stderr,"out of memory\n" ); +return 1 ; +} + +int change_bool (char *name,int state,policydb_t * p,FILE * fp) +{ +cond_bool_datum_t *bool; + + bool = hashtab_search(p->p_bools.table, name); +if (bool ==NULL ) { +fprintf (fp,"Could not find bool %s\n" , name); +return -1 ; + } + bool->state = state; +evaluate_conds (p); +return 0 ; +} + +int menu () +{ +printf ("\nSelect a command:\n" ); +printf ("1) display unconditional AVTAB\n" ); +printf ("2) display conditional AVTAB (entirely)\n" ); +printf ("3) display conditional AVTAG (only ENABLED rules)\n" ); +printf ("4) display conditional AVTAB (only DISABLED rules)\n" ); +printf ("5) display conditional bools\n" ); +printf ("6) display conditional expressions\n" ); +printf ("7) change a boolean value\n" ); +printf ("\n" ); +printf ("f) set output file\n" ); +printf ("m) display menu\n" ); +printf ("q) quit\n" ); +return 0 ; +} + +int main (int argc,char **argv) +{ +FILE *out_fp = stdout; +char ans[81 ], OutfileName[121 ]; +int fd, ret; +struct stat sb; +void *map; +char *name; +int state; +struct policy_file pf; + +if (argc !=2 ) +usage (argv[0 ]); + + fd =open (argv[1 ],O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Can't open '%s': %s\n" , + argv[1 ],strerror (errno)); +exit (1 ); + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Can't stat '%s': %s\n" , + argv[1 ],strerror (errno)); +exit (1 ); + } + map = +mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Can't map '%s': %s\n" , + argv[1 ],strerror (errno)); +exit (1 ); + } + +/* read the binary policy */ +fprintf (out_fp,"Reading policy...\n" ); + pf.type =PF_USE_MEMORY ; + pf.data = map; + pf.len = sb.st_size; +if (policydb_init (&policydb)) { +fprintf (stderr,"%s: Out of memory!\n" , argv[0 ]); +exit (1 ); + } + ret =policydb_read (&policydb, &pf,1 ); +if (ret) { +fprintf (stderr, +"%s: error(s) encountered while parsing configuration\n" , + argv[0 ]); +exit (1 ); + } + +fprintf (stdout,"binary policy file loaded\n\n" ); +close (fd); + +menu (); +for (;;) { +printf ("\nCommand (\'m\' for menu): " ); +fgets (ans,sizeof (ans), stdin); +switch (ans[0 ]) { + +case '1' : +display_avtab (&policydb.te_avtab,RENDER_UNCONDITIONAL , + &policydb, out_fp); +break ; +case '2' : +display_avtab (&policydb.te_cond_avtab, +RENDER_CONDITIONAL , &policydb, out_fp); +break ; +case '3' : +display_avtab (&policydb.te_cond_avtab,RENDER_ENABLED , + &policydb, out_fp); +break ; +case '4' : +display_avtab (&policydb.te_cond_avtab,RENDER_DISABLED , + &policydb, out_fp); +break ; +case '5' : +display_bools (&policydb, out_fp); +break ; +case '6' : +display_cond_expressions (&policydb, out_fp); +break ; +case '7' : +printf ("name? " ); +fgets (ans,sizeof (ans), stdin); + ans[strlen (ans) -1 ] =0 ; + + name =malloc ((strlen (ans) +1 ) *sizeof (char )); +if (name ==NULL ) { +fprintf (stderr,"couldn't malloc string.\n" ); +break ; + } +strcpy (name, ans); + +printf ("state? " ); +fgets (ans,sizeof (ans), stdin); + ans[strlen (ans) -1 ] =0 ; + +if (atoi (ans)) + state =1 ; +else + state =0 ; + +change_bool (name, state, &policydb, out_fp); +free (name); +break ; +case 'f' : +printf + ("\nFilename for output (<CR> for screen output): " ); +fgets (OutfileName,sizeof (OutfileName), stdin); + OutfileName[strlen (OutfileName) -1 ] ='\0' ;/* fix_string (remove LF) */ +if (strlen (OutfileName) ==0 ) + out_fp = stdout; +else if ((out_fp =fopen (OutfileName,"w" )) ==NULL ) { +fprintf (stderr,"Cannot open output file %s\n" , + OutfileName); + out_fp = stdout; + } +if (out_fp != stdout) +printf ("\nOutput to file: %s\n" , OutfileName); +break ; +case 'q' : +policydb_destroy (&policydb); +exit (0 ); +break ; +case 'm' : +menu (); +break ; +default : +printf ("\nInvalid choice\n" ); +menu (); +break ; + + } + } +} + +/* FLASK */
libsepol/utils/Makefile +3 -0
@@ -6,6 +6,9 @@ CFLAGS ?= -Wall override CFLAGS += -I../include LDLIBS += -L../src -lsepol +LIBDIR=$(PREFIX)/lib +LDLIBS=-lfl -lsepol -lselinux $(LIBDIR)/libsepol.a -L$(LIBDIR) + TARGETS=$(patsubst %.c,%,$(wildcard *.c)) all: $(TARGETS)
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 4/5 This patch does all the functional work of removing the module support code.
In reply to Mark GoldmanVarious unnecessary structs were removed.
At the end of this patch, libsepol is able to load policies, but does not know about modules.
dispol operates as expected when using libsepol at the end of this patch.
<< part 2 >>
---
libsepol/include/sepol/policydb.h | 7 1 + 6 - 0 ! libsepol/include/sepol/policydb/avrule_block.h | 10 0 + 10 - 0 ! libsepol/include/sepol/policydb/conditional.h | 6 3 + 3 - 0 ! libsepol/include/sepol/policydb/constraint.h | 2 1 + 1 - 0 ! libsepol/include/sepol/policydb/flask_types.h | 1 0 + 1 - 0 ! libsepol/include/sepol/policydb/policydb.h | 205 6 + 199 - 0 ! libsepol/include/sepol/users.h | 6 0 + 6 - 0 ! libsepol/src/assertion.c | 115 0 + 115 - 0 ! libsepol/src/avrule_block.c | 173 0 + 173 - 0 ! libsepol/src/conditional.c | 257 140 + 117 - 0 ! libsepol/src/constraint.c | 5 2 + 3 - 0 ! libsepol/src/genusers.c | 8 4 + 4 - 0 ! libsepol/src/hierarchy.c | 64 36 + 28 - 0 ! libsepol/src/libsepol.map | 4 3 + 1 - 0 ! libsepol/src/policydb_public.c | 29 3 + 26 - 0 ! libsepol/src/private.h | 5 2 + 3 - 0 ! libsepol/src/services.c | 7 4 + 3 - 0 ! libsepol/src/users.c | 18 8 + 10 - 0 ! libsepol/src/write.c | 570 45 + 525 - 0 ! 19 files changed, 258 insertions(+), 1234 deletions(-)
libsepol/src/assertion.c +0 -115
@@ -22,121 +22,6 @@#include <sepol/policydb/avtab.h> #include <sepol/policydb/policydb.h> -#include <sepol/policydb/expand.h> #include <sepol/policydb/util.h> #include "debug.h" - -static int check_assertion_helper (sepol_handle_t * handle, -policydb_t * p, -avtab_t * te_avtab,avtab_t * te_cond_avtab, -unsigned int stype,unsigned int ttype, -class_perm_node_t * perm,unsigned long line) -{ -avtab_key_t avkey; -avtab_ptr_t node; -class_perm_node_t *curperm; - -for (curperm = perm; curperm !=NULL ; curperm = curperm->next) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; - avkey.target_class = curperm->class; - avkey.specified =AVTAB_ALLOWED ; -for (node =avtab_search_node (te_avtab, &avkey); - node !=NULL ; - node =avtab_search_node_next (node, avkey.specified)) { -if (node->datum.data & curperm->data) - goto err; - } -for (node =avtab_search_node (te_cond_avtab, &avkey); - node !=NULL ; - node =avtab_search_node_next (node, avkey.specified)) { -if (node->datum.data & curperm->data) - goto err; - } - } - -return 0 ; - - err: -ERR (handle,"assertion on line %lu violated by allow %s %s:%s {%s };" , - line, p->p_type_val_to_name[stype], p->p_type_val_to_name[ttype], - p->p_class_val_to_name[curperm->class -1 ], -sepol_av_to_string (p, curperm->class, - node->datum.data & curperm->data)); -return -1 ; -} - -int check_assertions (sepol_handle_t * handle,policydb_t * p, -avrule_t * avrules) -{ -avrule_t *a; -avtab_t te_avtab, te_cond_avtab; -ebitmap_node_t *snode, *tnode; -unsigned int i, j; -int errors =0 ; - -if (!avrules) { -/* Since assertions are stored in avrules, if it is NULL -there won't be any to check. This also prevents an invalid -free if the avtabs are never initialized */ -return 0 ; - } - -if (avrules) { -if (avtab_init (&te_avtab)) - goto oom; -if (avtab_init (&te_cond_avtab)) { -avtab_destroy (&te_avtab); - goto oom; - } -if (expand_avtab (p, &p->te_avtab, &te_avtab) || -expand_avtab (p, &p->te_cond_avtab, &te_cond_avtab)) { -avtab_destroy (&te_avtab); -avtab_destroy (&te_cond_avtab); - goto oom; - } - } - -for (a = avrules; a !=NULL ; a = a->next) { -ebitmap_t *stypes = &a->stypes.types; -ebitmap_t *ttypes = &a->ttypes.types; - -if (!(a->specified &AVRULE_NEVERALLOW )) -continue ; - -ebitmap_for_each_bit (stypes, snode, i) { -if (!ebitmap_node_get_bit (snode, i)) -continue ; -if (a->flags &RULE_SELF ) { -if (check_assertion_helper - (handle, p, &te_avtab, &te_cond_avtab, i, i, - a->perms, a->line)) - errors++; - } -ebitmap_for_each_bit (ttypes, tnode, j) { -if (!ebitmap_node_get_bit (tnode, j)) -continue ; -if (check_assertion_helper - (handle, p, &te_avtab, &te_cond_avtab, i, j, - a->perms, a->line)) - errors++; - } - } - } - -if (errors) { -ERR (handle,"%d assertion violations occured" , errors); -avtab_destroy (&te_avtab); -avtab_destroy (&te_cond_avtab); -return -1 ; - } - -avtab_destroy (&te_avtab); -avtab_destroy (&te_cond_avtab); -return 0 ; - - oom: -ERR (handle,"Out of memory - unable to check assertions" ); -return -1 ; -}
libsepol/src/avrule_block.c +0 -173
176 lines not on this page · raw message
libsepol/src/conditional.c +140 -117
278 lines not on this page · raw message
libsepol/src/constraint.c +2 -3
@@ -19,7 +19,6 @@#include <sepol/policydb/policydb.h> #include <sepol/policydb/constraint.h> -#include <sepol/policydb/expand.h> #include <sepol/policydb/flask_types.h> #include <assert.h>
@@ -32,7 +31,7 @@ int constraint_expr_init(constraint_exprif ((expr->type_names =malloc (sizeof (*expr->type_names))) ==NULL ) {return -1 ; } -type_set_init (expr->type_names); +ebitmap_init (expr->type_names);return 0 ; }
@@ -40,7 +39,7 @@ void constraint_expr_destroy(constraint_ {if (expr !=NULL ) {ebitmap_destroy (&expr->names); -type_set_destroy (expr->type_names); +ebitmap_destroy (expr->type_names);free (expr->type_names);free (expr); }
libsepol/src/genusers.c +4 -4
@@ -72,8 +72,8 @@ static int load_users(struct policydb *p usrdatum =hashtab_search (policydb->p_users.table, q);if (usrdatum) {/* Replacing an existing user definition. */ -ebitmap_destroy (&usrdatum->roles.roles); -ebitmap_init (&usrdatum->roles.roles); +ebitmap_destroy (&usrdatum->roles); +ebitmap_init (&usrdatum->roles); }else {char *id =strdup (q);
@@ -88,7 +88,7 @@ static int load_users(struct policydb *p }memset (usrdatum,0 ,sizeof (user_datum_t)); usrdatum->s.value = ++policydb->p_users.nprim; -ebitmap_init (&usrdatum->roles.roles); +ebitmap_init (&usrdatum->roles);if (hashtab_insert (policydb->p_users.table, id, (hashtab_datum_t ) usrdatum)) {ERR (NULL ,"out of memory" );
@@ -145,7 +145,7 @@ static int load_users(struct policydb *pebitmap_for_each_bit (&roldatum->dominates, rnode, bit) {if (ebitmap_node_get_bit (rnode, bit))if (ebitmap_set_bit - (&usrdatum->roles.roles, bit,1 )) { + (&usrdatum->roles, bit,1 )) {ERR (NULL ,"out of memory" );free (buffer);fclose (fp);
libsepol/src/hierarchy.c +36 -28
125 lines not on this page · raw message
libsepol/src/policydb_public.c +3 -26
@@ -95,41 +95,18 @@ int sepol_policy_kern_vers_max(void)return POLICYDB_VERSION_MAX ; } -int sepol_policydb_set_typevers (sepol_policydb_t * sp,unsigned int type) +int sepol_policydb_set_typevers (sepol_policydb_t * sp) {struct policydb *p = &sp->p; -switch (type) { -case POLICY_KERN : - p->policyvers =POLICYDB_VERSION_MAX ; -break ; -case POLICY_BASE : -case POLICY_MOD : - p->policyvers =MOD_POLICYDB_VERSION_MAX ; -break ; -default : -return -1 ; - } - p->policy_type = type; + p->policyvers =POLICYDB_VERSION_MAX ;return 0 ; }int sepol_policydb_set_vers (sepol_policydb_t * sp,unsigned int vers) {struct policydb *p = &sp->p; -switch (p->policy_type) { -case POLICY_KERN : -if (vers <POLICYDB_VERSION_MIN || vers >POLICYDB_VERSION_MAX ) -return -1 ; -break ; -case POLICY_BASE : -case POLICY_MOD : -if (vers <MOD_POLICYDB_VERSION_MIN - || vers >MOD_POLICYDB_VERSION_MAX ) -return -1 ; -break ; -default : +if (vers <POLICYDB_VERSION_MIN || vers >POLICYDB_VERSION_MAX )return -1 ; - } p->policyvers = vers;return 0 ; }
libsepol/src/services.c +4 -3
@@ -87,7 +87,8 @@ int sepol_set_policydb_from_file(FILE *struct policy_file pf; pf.fp = fp; pf.type =PF_USE_STDIO ; -if (mypolicydb.policy_type) +// FIXME: We need to figure out a new test for destroying the db +if (0 )policydb_destroy (&mypolicydb);if (policydb_init (&mypolicydb)) {ERR (NULL ,"Out of memory!" );
@@ -1265,12 +1266,12 @@ int hidden sepol_get_user_sids(sepol_sec }memset (mysids,0 , maxnel *sizeof (sepol_security_id_t)); -ebitmap_for_each_bit (&user->roles.roles, rnode, i) { +ebitmap_for_each_bit (&user->roles, rnode, i) {if (!ebitmap_node_get_bit (rnode, i))continue ; role = policydb->role_val_to_struct[i]; usercon.role = i +1 ; -ebitmap_for_each_bit (&role->types.types, tnode, j) { +ebitmap_for_each_bit (&role->types, tnode, j) {if (!ebitmap_node_get_bit (tnode, j))continue ; usercon.type = j +1 ;
libsepol/src/users.c +8 -10
@@ -8,7 +8,6 @@#include <sepol/policydb/policydb.h> #include <sepol/policydb/hashtab.h> -#include <sepol/policydb/expand.h> #include "user_internal.h" #include "mls.h"
@@ -19,7 +18,7 @@ static int user_to_record(sepol_handle_tconst char *name = policydb->p_user_val_to_name[user_idx];user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx]; -ebitmap_t *roles = &(usrdatum->roles.roles); +ebitmap_t *roles = &(usrdatum->roles);ebitmap_node_t *rnode;unsigned bit;
@@ -169,8 +168,7 @@ int sepol_user_modify(sepol_handle_t * h/* Set the role and every role it dominates */ ebitmap_for_each_bit (&roldatum->dominates, rnode, bit) {if (ebitmap_node_get_bit (rnode, bit)) { -if (ebitmap_set_bit - (&(usrdatum->roles.roles), bit,1 )) +if (ebitmap_set_bit (&(usrdatum->roles), bit,1 )) goto omem; } }
@@ -259,11 +257,11 @@ int sepol_user_modify(sepol_handle_t * h name =NULL ;/* Expand roles */ -if (role_set_expand - (&usrdatum->roles, &usrdatum->cache, policydb)) { -ERR (handle,"unable to expand role set" ); - goto err; - } +/* NOTE: Deleted this section - it is unclear whether or not +* expansion will be required when the policyrep work is +* done. +* - [email protected] +*/ }free (roles);
@@ -278,7 +276,7 @@ int sepol_user_modify(sepol_handle_t * hfree (name);free (roles);if (new && usrdatum) { -role_set_destroy (&usrdatum->roles); +ebitmap_destroy (&usrdatum->roles);free (usrdatum); }return STATUS_ERR ;
libsepol/src/write.c +45 -525
741 lines not on this page · raw message
libsepol/include/sepol/policydb/avrule_block.h +0 -10
@@ -22,16 +22,6 @@#include <sepol/policydb/policydb.h> -extern avrule_block_t *avrule_block_create (void ); -extern void avrule_block_destroy (avrule_block_t * x); -extern avrule_decl_t *avrule_decl_create (uint32_t decl_id); -extern void avrule_decl_destroy (avrule_decl_t * x); -extern void avrule_block_list_destroy (avrule_block_t * x); -extern avrule_decl_t *get_avrule_decl (policydb_t * p,uint32_t decl_id); -extern cond_list_t *get_decl_cond_list (policydb_t * p, -avrule_decl_t * decl, -cond_list_t * cond); -extern int is_id_enabled (char *id,policydb_t * p,int symbol_table);extern int is_perm_enabled (char *class_id,char *perm_id,policydb_t * p);#endif
libsepol/include/sepol/policydb/conditional.h +3 -3
@@ -77,9 +77,6 @@ typedef struct cond_node {/* these true/false lists point into te_avtab when that is used */ cond_av_list_t *true_list;cond_av_list_t *false_list; -/* and these are using during parsing and for modules */ -avrule_t *avtrue_list; -avrule_t *avfalse_list;/* these fields are not written to binary policy */ unsigned int nbools;uint32_t bool_ids[COND_MAX_BOOLS ];
@@ -112,6 +109,9 @@ extern avtab_datum_t *cond_av_list_searcextern void cond_av_list_destroy (cond_av_list_t * list); +extern int expand_cond_av_list (policydb_t *p,cond_av_list_t *l, +cond_av_list_t ** newl,avtab_t * expa); +extern void cond_optimize_lists (cond_list_t * cl);extern int cond_policydb_init (policydb_t * p);
libsepol/include/sepol/policydb/constraint.h +1 -1
@@ -56,7 +56,7 @@ typedef struct constraint_expr {uint32_t op;/* operator */ ebitmap_t names;/* names */ -struct type_set *type_names; +ebitmap_t *type_names;struct constraint_expr *next;/* next expression */ } constraint_expr_t;
libsepol/include/sepol/policydb/flask_types.h +0 -1
@@ -46,7 +46,6 @@ typedef uint16_t sepol_security_class_t;#define SEPOL_SECCLASS_NULL 0x0000/* no class */ #define SELINUX_MAGIC 0xf97cff8c -#define SELINUX_MOD_MAGIC 0xf97cff8dtypedef uint32_t sepol_security_id_t ;#define SEPOL_SECSID_NULL 0
libsepol/include/sepol/policydb.h +1 -6
@@ -56,11 +56,6 @@ extern void sepol_policy_file_set_handleextern int sepol_policydb_create (sepol_policydb_t ** p);extern void sepol_policydb_free (sepol_policydb_t * p); -/* Legal types of policies that the policydb can represent. */ -#define SEPOL_POLICY_KERN 0 -#define SEPOL_POLICY_BASE 1 -#define SEPOL_POLICY_MOD 2 -/* * Range of policy versions for the kernel policy type supported * by this library.
@@ -74,7 +69,7 @@ extern int sepol_policy_kern_vers_max(vo* policy type. * Returns -1 if the policy type is not legal. */ -extern int sepol_policydb_set_typevers (sepol_policydb_t * p,unsigned int type); +extern int sepol_policydb_set_typevers (sepol_policydb_t * p);/* * Set the policy version to a different value.
libsepol/include/sepol/policydb/policydb.h +6 -199
288 lines not on this page · raw message
libsepol/include/sepol/users.h +0 -6
@@ -23,12 +23,6 @@ extern void sepol_set_delusers(int on);/*--------end compatibility----------*/ -/* Modify the user, or add it, if the key is not found */ -extern int sepol_user_modify (sepol_handle_t * handle, -sepol_policydb_t * policydb, -const sepol_user_key_t * key, -const sepol_user_t * data); -/* Return the number of users */ extern int sepol_user_count (sepol_handle_t * handle,const sepol_policydb_t * p,unsigned int *response);
libsepol/src/private.h +2 -3
@@ -29,14 +29,13 @@/* Policy compatibility information. */ struct policydb_compat_info { -unsigned int type;unsigned int version;unsigned int sym_num;unsigned int ocon_num; }; -extern struct policydb_compat_info *policydb_lookup_compat (unsigned int version, -unsigned int type); +extern struct policydb_compat_info *policydb_lookup_compat (unsigned int + version);/* Reading from a policy "file". */ static inline void *next_entry (struct policy_file *fp,size_t bytes)
libsepol/src/libsepol.map +3 -1
@@ -1,6 +1,6 @@ { global: - sepol_module_package_*; sepol_link_modules; sepol_expand_module; sepol_link_packages; + sepol_module_package_*; sepol_link_packages; sepol_bool_*; sepol_genbools*; sepol_context_*; sepol_mls_*; sepol_check_context; sepol_iface_*;
@@ -12,5 +12,7 @@ sepol_policydb_*; sepol_set_policydb_from_file; sepol_policy_kern_*; sepol_policy_file_*; + expand_avtab; + expand_cond_av_list; local: *; };
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 2/5 This patch removes files that existed solely to support modular policies.
In reply to Mark Goldman<< part 2 >>
---
libsepol/include/sepol/policydb/expand.h | 78 0 + 78 - 0 ! libsepol/include/sepol/policydb/link.h | 20 0 + 20 - 0 ! libsepol/include/sepol/policydb/module.h | 48 0 + 48 - 0 ! libsepol/src/link.c | 2239 0 + 2239 - 0 ! libsepol/src/module.c | 952 0 + 952 - 0 ! 5 files changed, 3337 deletions(-)
libsepol/src/link.c deleted +0 -2239
@@ -1,2239 +0,0 @@ -/* Authors: Karl MacMillan <[email protected]> -* Joshua Brindle <[email protected]> -* Jason Tang <[email protected]> -* -* Copyright (C) 2004-2005 Tresys Technology, LLC -* Copyright (C) 2007 Red Hat, Inc. -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include <sepol/policydb/policydb.h> -#include <sepol/policydb/conditional.h> -#include <sepol/policydb/hashtab.h> -#include <sepol/policydb/avrule_block.h> -#include <sepol/policydb/link.h> -#include <sepol/policydb/util.h> - -#include <stdlib.h> -#include <stdarg.h> -#include <stdio.h> -#include <string.h> -#include <assert.h> - -#include "debug.h" - -#undef min -#define min (a,b) (((a) < (b)) ? (a) : (b)) - -typedef struct policy_module { -policydb_t *policy; -uint32_t num_decls; -uint32_t *map[SYM_NUM ]; -uint32_t *avdecl_map; -uint32_t **perm_map; -uint32_t *perm_map_len; - -/* a pointer to within the base module's avrule_block chain to -* where this module's global now resides */ -avrule_block_t *base_global; -}policy_module_t ; - -typedef struct link_state { -int verbose; -policydb_t *base; -avrule_block_t *last_avrule_block, *last_base_avrule_block; -uint32_t next_decl_id, current_decl_id; - -/* temporary variables, used during hashtab_map() calls */ -policy_module_t *cur; -char *cur_mod_name; -avrule_decl_t *dest_decl; -class_datum_t *src_class, *dest_class; -char *dest_class_name; -char dest_class_req;/* flag indicating the class was not declared */ -uint32_t symbol_num; -/* used to report the name of the module if dependancy error occurs */ -policydb_t **decl_to_mod; - -/* error reporting fields */ -sepol_handle_t *handle; -}link_state_t ; - -typedef struct missing_requirement { -uint32_t symbol_type; -uint32_t symbol_value; -uint32_t perm_value; -}missing_requirement_t ; - -static const char *symtab_names[SYM_NUM ] = { -"common" ,"class" ,"role" ,"type/attribute" ,"user" , -"bool" ,"level" ,"category" -}; - -/* Deallocates all elements within a module, but NOT the policydb_t -* structure within, as well as the pointer itself. */ -static void policy_module_destroy (policy_module_t * mod) -{ -unsigned int i; -if (mod ==NULL ) { -return ; - } -for (i =0 ; i <SYM_NUM ; i++) { -free (mod->map[i]); - } -for (i =0 ; mod->perm_map !=NULL && i < mod->policy->p_classes.nprim; - i++) { -free (mod->perm_map[i]); - } -free (mod->perm_map); -free (mod->perm_map_len); -free (mod->avdecl_map); -free (mod); -} - -/***** functions that copy identifiers from a module to base *****/ - -/* Note: there is currently no scoping for permissions, which causes some -* strange side-effects. The current approach is this: -* -* a) perm is required and the class _and_ perm are declared in base: only add a mapping. -* b) perm is required and the class and perm are _not_ declared in base: simply add the permissions -* to the object class. This means that the requirements for the decl are the union of the permissions -* required for all decls, but who cares. -* c) perm is required, the class is declared in base, but the perm is not present. Nothing we can do -* here because we can't mark a single permission as required, so we bail with a requirement error -* _even_ if we are in an optional. -* -* A is correct behavior, b is wrong but not too bad, c is totall wrong for optionals. Fixing this requires -* a format change. -*/ -static int permission_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *perm_id = key, *new_id =NULL ; -perm_datum_t *perm, *new_perm =NULL , *dest_perm; -link_state_t *state = (link_state_t *) data; - -class_datum_t *src_class = state->src_class; -class_datum_t *dest_class = state->dest_class; -policy_module_t *mod = state->cur; -uint32_t sclassi = src_class->s.value -1 ; -int ret; - - perm = (perm_datum_t *) datum; - dest_perm =hashtab_search (dest_class->permissions.table, perm_id); -if (dest_perm ==NULL && dest_class->comdatum !=NULL ) { - dest_perm = -hashtab_search (dest_class->comdatum->permissions.table, - perm_id); - } - -if (dest_perm ==NULL ) { -/* If the object class was not declared in the base, add the perm -* to the object class. */ -if (state->dest_class_req) { -/* If the class was required (not declared), insert the new permission */ - new_id =strdup (perm_id); -if (new_id ==NULL ) { -ERR (state->handle,"Memory error" ); - ret =SEPOL_ERR ; - goto err; - } - new_perm = - (perm_datum_t *)calloc (1 ,sizeof (perm_datum_t)); -if (new_perm ==NULL ) { -ERR (state->handle,"Memory error" ); - ret =SEPOL_ERR ; - goto err; - } - ret =hashtab_insert (dest_class->permissions.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_perm); -if (ret) { -ERR (state->handle, -"could not insert permission into class\n" ); - goto err; - } - new_perm->s.value = dest_class->permissions.nprim +1 ; - dest_perm = new_perm; - }else { -/* this is case c from above */ -ERR (state->handle, -"Module %s depends on permission %s in class %s, not satisfied" , - state->cur_mod_name, perm_id, - state->dest_class_name); -return SEPOL_EREQ ; - } - } - -/* build the mapping for permissions encompassing this class. -* unlike symbols, the permission map translates between -* module permission bit to target permission bit. that bit -* may have originated from the class -or- it could be from -* the class's common parent.*/ -if (perm->s.value > mod->perm_map_len[sclassi]) { -uint32_t *newmap =calloc (perm->s.value,sizeof (*newmap)); -if (newmap ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } -memcpy (newmap, mod->perm_map[sclassi], - mod->perm_map_len[sclassi] *sizeof (*newmap)); -free (mod->perm_map[sclassi]); - mod->perm_map[sclassi] = newmap; - mod->perm_map_len[sclassi] = perm->s.value; - } - mod->perm_map[sclassi][perm->s.value -1 ] = dest_perm->s.value; - -return 0 ; - err: -free (new_id); -free (new_perm); -return ret; -} - -static int class_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *id = key, *new_id =NULL ; -class_datum_t *cladatum, *new_class =NULL ; -link_state_t *state = (link_state_t *) data; -scope_datum_t *scope =NULL ; -int ret; - - cladatum = (class_datum_t *) datum; - state->dest_class_req =0 ; - - new_class =hashtab_search (state->base->p_classes.table, id); -/* If there is not an object class already in the base symtab that means -* that either a) a module is trying to declare a new object class (which -* the compiler should prevent) or b) an object class was required that is -* not in the base. -*/ -if (new_class ==NULL ) { - scope = -hashtab_search (state->cur->policy->p_classes_scope.table, - id); -if (scope ==NULL ) { - ret =SEPOL_ERR ; - goto err; - } -if (scope->scope ==SCOPE_DECL ) { -/* disallow declarations in modules */ -ERR (state->handle, -"%s: Modules may not yet declare new classes." , - state->cur_mod_name); - ret =SEPOL_ENOTSUP ; - goto err; - }else { -/* It would be nice to error early here because the requirement is -* not met, but we cannot because the decl might be optional (in which -* case we should record the requirement so that it is just turned -* off). Note: this will break horribly if modules can declare object -* classes because the class numbers will be all wrong (i.e., they -* might be assigned in the order they were required rather than the -* current scheme which ensures correct numbering by ordering the -* declarations properly). This can't be fixed until some infrastructure -* for querying the object class numbers is in place. */ - state->dest_class_req =1 ; - new_class = - (class_datum_t *)calloc (1 ,sizeof (class_datum_t)); -if (new_class ==NULL ) { -ERR (state->handle,"Memory error\n" ); - ret =SEPOL_ERR ; - goto err; - } -if (symtab_init - (&new_class->permissions,PERM_SYMTAB_SIZE )) { - ret =SEPOL_ERR ; - goto err; - } - new_id =strdup (id); -if (new_id ==NULL ) { -ERR (state->handle,"Memory error\n" ); - ret =SEPOL_ERR ; - goto err; - } - ret =hashtab_insert (state->base->p_classes.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_class); -if (ret) { -ERR (state->handle, -"could not insert new class into symtab" ); - goto err; - } - new_class->s.value = ++(state->base->p_classes.nprim); - } - } - - state->cur->map[SYM_CLASSES ][cladatum->s.value -1 ] = - new_class->s.value; - -/* copy permissions */ - state->src_class = cladatum; - state->dest_class = new_class; - state->dest_class_name = (char *)key; - - ret = -hashtab_map (cladatum->permissions.table, permission_copy_callback, - state); -if (ret !=0 ) { -return ret; - } - -return 0 ; - err: -free (new_class); -free (new_id); -return ret; -} - -static int role_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -int ret; -char *id = key, *new_id =NULL ; -role_datum_t *role, *base_role, *new_role =NULL ; -link_state_t *state = (link_state_t *) data; - - role = (role_datum_t *) datum; - - base_role =hashtab_search (state->base->p_roles.table, id); -if (base_role ==NULL ) { -if (state->verbose) -INFO (state->handle,"copying role %s" , id); - -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } - -if ((new_role = - (role_datum_t *)malloc (sizeof (*new_role))) ==NULL ) { - goto cleanup; - } -role_datum_init (new_role); - -/* new_role's dominates and types field will be copied -* during role_fix_callback() */ - new_role->s.value = state->base->p_roles.nprim +1 ; - - ret =hashtab_insert (state->base->p_roles.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_role); -if (ret) { - goto cleanup; - } - state->base->p_roles.nprim++; - base_role = new_role; - } - -if (state->dest_decl) { - new_id =NULL ; -if ((new_role =malloc (sizeof (*new_role))) ==NULL ) { - goto cleanup; - } -role_datum_init (new_role); - new_role->s.value = base_role->s.value; -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } -if (hashtab_insert - (state->dest_decl->p_roles.table, new_id, new_role)) { - goto cleanup; - } - state->dest_decl->p_roles.nprim++; - } - - state->cur->map[SYM_ROLES ][role->s.value -1 ] = base_role->s.value; -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -role_datum_destroy (new_role); -free (new_id); -free (new_role); -return -1 ; -} - -/* Copy types and attributes from a module into the base module. The -* attributes are copied, but the types that make up this attribute -* are delayed type_fix_callback(). */ -static int type_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -int ret; -char *id = key, *new_id =NULL ; -type_datum_t *type, *base_type, *new_type =NULL ; -link_state_t *state = (link_state_t *) data; - - type = (type_datum_t *) datum; -if ((type->flavor ==TYPE_TYPE && !type->primary) - || type->flavor ==TYPE_ALIAS ) { -/* aliases are handled later, in alias_copy_callback() */ -return 0 ; - } - - base_type =hashtab_search (state->base->p_types.table, id); -if (base_type !=NULL ) { -/* type already exists. check that it is what this -* module expected. duplicate declarations (e.g., two -* modules both declare type foo_t) is checked during -* scope_copy_callback(). */ -if (type->flavor ==TYPE_ATTRIB - && base_type->flavor !=TYPE_ATTRIB ) { -ERR (state->handle, -"%s: Expected %s to be an attribute, but it was already declared as a type." , - state->cur_mod_name, id); -return -1 ; - }else if (type->flavor !=TYPE_ATTRIB - && base_type->flavor ==TYPE_ATTRIB ) { -ERR (state->handle, -"%s: Expected %s to be a type, but it was already declared as an attribute." , - state->cur_mod_name, id); -return -1 ; - } - }else { -if (state->verbose) -INFO (state->handle,"copying type %s" , id); - -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } - -if ((new_type = - (type_datum_t *)calloc (1 ,sizeof (*new_type))) ==NULL ) { - goto cleanup; - } - new_type->primary = type->primary; - new_type->flavor = type->flavor; -/* for attributes, the writing of new_type->types is -done in type_fix_callback() */ - - new_type->s.value = state->base->p_types.nprim +1 ; - - ret =hashtab_insert (state->base->p_types.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_type); -if (ret) { - goto cleanup; - } - state->base->p_types.nprim++; - base_type = new_type; - } - -if (state->dest_decl) { - new_id =NULL ; -if ((new_type =calloc (1 ,sizeof (*new_type))) ==NULL ) { - goto cleanup; - } - new_type->primary = type->primary; - new_type->flavor = type->flavor; - new_type->s.value = base_type->s.value; -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } -if (hashtab_insert - (state->dest_decl->p_types.table, new_id, new_type)) { - goto cleanup; - } - state->dest_decl->p_types.nprim++; - } - - state->cur->map[SYM_TYPES ][type->s.value -1 ] = base_type->s.value; -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -free (new_id); -free (new_type); -return -1 ; -} - -static int user_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -int ret; -char *id = key, *new_id =NULL ; -user_datum_t *user, *base_user, *new_user =NULL ; -link_state_t *state = (link_state_t *) data; - - user = (user_datum_t *) datum; - - base_user =hashtab_search (state->base->p_users.table, id); -if (base_user ==NULL ) { -if (state->verbose) -INFO (state->handle,"copying user %s" , id); - -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } - -if ((new_user = - (user_datum_t *)malloc (sizeof (*new_user))) ==NULL ) { - goto cleanup; - } -user_datum_init (new_user); -/* new_users's roles and MLS fields will be copied during -user_fix_callback(). */ - - new_user->s.value = state->base->p_users.nprim +1 ; - - ret =hashtab_insert (state->base->p_users.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_user); -if (ret) { - goto cleanup; - } - state->base->p_users.nprim++; - base_user = new_user; - } - -if (state->dest_decl) { - new_id =NULL ; -if ((new_user =malloc (sizeof (*new_user))) ==NULL ) { - goto cleanup; - } -user_datum_init (new_user); - new_user->s.value = base_user->s.value; -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } -if (hashtab_insert - (state->dest_decl->p_users.table, new_id, new_user)) { - goto cleanup; - } - state->dest_decl->p_users.nprim++; - } - - state->cur->map[SYM_USERS ][user->s.value -1 ] = base_user->s.value; -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -user_datum_destroy (new_user); -free (new_id); -free (new_user); -return -1 ; -} - -static int bool_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -int ret; -char *id = key, *new_id =NULL ; -cond_bool_datum_t *booldatum, *base_bool, *new_bool =NULL ; -link_state_t *state = (link_state_t *) data; - - booldatum = (cond_bool_datum_t *) datum; - - base_bool =hashtab_search (state->base->p_bools.table, id); -if (base_bool ==NULL ) { -if (state->verbose) -INFO (state->handle,"copying boolean %s" , id); - -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } - -if ((new_bool = - (cond_bool_datum_t *)malloc (sizeof (*new_bool))) ==NULL ) { - goto cleanup; - } - new_bool->state = booldatum->state; - new_bool->s.value = state->base->p_bools.nprim +1 ; - - ret =hashtab_insert (state->base->p_bools.table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_bool); -if (ret) { - goto cleanup; - } - state->base->p_bools.nprim++; - base_bool = new_bool; - - } - - state->cur->map[SYM_BOOLS ][booldatum->s.value -1 ] = base_bool->s.value; -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -cond_destroy_bool (new_id, new_bool,NULL ); -return -1 ; -} - -static int sens_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *id = key; -level_datum_t *level, *base_level; -link_state_t *state = (link_state_t *) data; -scope_datum_t *scope; - - level = (level_datum_t *) datum; - - base_level =hashtab_search (state->base->p_levels.table, id); -if (!base_level) { - scope = -hashtab_search (state->cur->policy->p_sens_scope.table, id); -if (!scope) -return SEPOL_ERR ; -if (scope->scope ==SCOPE_DECL ) { -/* disallow declarations in modules */ -ERR (state->handle, -"%s: Modules may not declare new sensitivities." , - state->cur_mod_name); -return SEPOL_ENOTSUP ; - } -if (scope->scope ==SCOPE_REQ ) { -/* unmet requirement */ -ERR (state->handle, -"%s: Sensitivity %s not declared by base." , - state->cur_mod_name, id); -return SEPOL_ENOTSUP ; - } - } - - state->cur->map[SYM_LEVELS ][level->level->sens -1 ] = - base_level->level->sens; - -return 0 ; -} - -static int cat_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *id = key; -cat_datum_t *cat, *base_cat; -link_state_t *state = (link_state_t *) data; -scope_datum_t *scope; - - cat = (cat_datum_t *) datum; - - base_cat =hashtab_search (state->base->p_cats.table, id); -if (!base_cat) { - scope = -hashtab_search (state->cur->policy->p_cat_scope.table, id); -if (!scope) -return SEPOL_ERR ; -if (scope->scope ==SCOPE_DECL ) { -/* disallow declarations in modules */ -ERR (state->handle, -"%s: Modules may not declare new categories." , - state->cur_mod_name); -return SEPOL_ENOTSUP ; - } -if (scope->scope ==SCOPE_REQ ) { -/* unmet requirement */ -ERR (state->handle, -"%s: Category %s not declared by base." , - state->cur_mod_name, id); -return SEPOL_ENOTSUP ; - } - } - - state->cur->map[SYM_CATS ][cat->s.value -1 ] = base_cat->s.value; - -return 0 ; -} - -static int (*copy_callback_f[SYM_NUM ]) (hashtab_key_t key, -hashtab_datum_t datum,void *datap) = { -NULL , class_copy_callback, role_copy_callback, type_copy_callback, - user_copy_callback, bool_copy_callback, sens_copy_callback, - cat_copy_callback}; - -/* The aliases have to be copied after the types and attributes to be -* certain that the base symbol table will have the type that the -* alias refers. Otherwise, we won't be able to find the type value -* for the alias. We can't depend on the declaration ordering because -* of the hash table. -*/ -static int alias_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *id = key, *new_id =NULL , *target_id; -type_datum_t *type, *base_type, *new_type =NULL , *target_type; -link_state_t *state = (link_state_t *) data; -policy_module_t *mod = state->cur; -int primval; - - type = (type_datum_t *) datum; -/* there are 2 kinds of aliases. Ones with their own value (TYPE_ALIAS) -* and ones with the value of their primary (TYPE_TYPE && type->primary = 0) -*/ -if (! - (type->flavor ==TYPE_ALIAS - || (type->flavor ==TYPE_TYPE && !type->primary))) { -/* ignore types and attributes -- they were handled in -* type_copy_callback() */ -return 0 ; - } - -if (type->flavor ==TYPE_ALIAS ) - primval = type->primary; -else - primval = type->s.value; - - target_id = mod->policy->p_type_val_to_name[primval -1 ]; - target_type =hashtab_search (state->base->p_types.table, target_id); -if (target_type ==NULL ) { -ERR (state->handle,"%s: Could not find type %s for alias %s." , - state->cur_mod_name, target_id, id); -return -1 ; - } - - base_type =hashtab_search (state->base->p_types.table, id); -if (base_type ==NULL ) { -if (state->verbose) -INFO (state->handle,"copying alias %s" , id); - -if ((new_type = - (type_datum_t *)calloc (1 ,sizeof (*new_type))) ==NULL ) { - goto cleanup; - } -/* the linked copy always has TYPE_ALIAS style aliases */ - new_type->primary = target_type->s.value; - new_type->flavor =TYPE_ALIAS ; - new_type->s.value = state->base->p_types.nprim +1 ; -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } -if (hashtab_insert - (state->base->p_types.table, new_id, new_type)) { - goto cleanup; - } - state->base->p_types.nprim++; - base_type = new_type; - }else { - -/* if this already exists and isn't an alias it was required by another module (or base) -* and inserted into the hashtable as a type, fix it up now */ - -if (base_type->flavor ==TYPE_ALIAS ) { -/* error checking */ -assert (base_type->primary == target_type->s.value); -assert (base_type->primary == - mod->map[SYM_TYPES ][primval -1 ]); -assert (mod->map[SYM_TYPES ][type->s.value -1 ] == - base_type->primary); -return 0 ; - } - -if (base_type->flavor ==TYPE_ATTRIB ) { -ERR (state->handle, -"%s is an alias of an attribute, not allowed" , id); -return -1 ; - } - - base_type->flavor =TYPE_ALIAS ; - base_type->primary = target_type->s.value; - - } -/* the aliases map points from its value to its primary so when this module -* references this type the value it gets back from the map is the primary */ - mod->map[SYM_TYPES ][type->s.value -1 ] = base_type->primary; - -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -free (new_id); -free (new_type); -return -1 ; -} - -/*********** callbacks that fix bitmaps ***********/ - -static int type_set_convert (type_set_t * types,type_set_t * dst, -policy_module_t * mod,link_state_t * state - __attribute__ ((unused))) -{ -unsigned int i; -ebitmap_node_t *tnode; -ebitmap_for_each_bit (&types->types, tnode, i) { -if (ebitmap_node_get_bit (tnode, i)) { -assert (mod->map[SYM_TYPES ][i]); -if (ebitmap_set_bit - (&dst->types, mod->map[SYM_TYPES ][i] -1 ,1 )) { - goto cleanup; - } - } - } -ebitmap_for_each_bit (&types->negset, tnode, i) { -if (ebitmap_node_get_bit (tnode, i)) { -assert (mod->map[SYM_TYPES ][i]); -if (ebitmap_set_bit - (&dst->negset, mod->map[SYM_TYPES ][i] -1 ,1 )) { - goto cleanup; - } - } - } - dst->flags = types->flags; -return 0 ; - - cleanup: -return -1 ; -} - -/* OR 2 typemaps together and at the same time map the src types to -* the correct values in the dst typeset. -*/ -static int type_set_or_convert (type_set_t * types,type_set_t * dst, -policy_module_t * mod,link_state_t * state) -{ -type_set_t ts_tmp; - -type_set_init (&ts_tmp); -if (type_set_convert (types, &ts_tmp, mod, state) ==-1 ) { - goto cleanup; - } -if (type_set_or_eq (dst, &ts_tmp)) { - goto cleanup; - } -type_set_destroy (&ts_tmp); -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -type_set_destroy (&ts_tmp); -return -1 ; -} - -static int role_set_or_convert (role_set_t * roles,role_set_t * dst, -policy_module_t * mod,link_state_t * state) -{ -unsigned int i; -ebitmap_t tmp; -ebitmap_node_t *rnode; - -ebitmap_init (&tmp); -ebitmap_for_each_bit (&roles->roles, rnode, i) { -if (ebitmap_node_get_bit (rnode, i)) { -assert (mod->map[SYM_ROLES ][i]); -if (ebitmap_set_bit - (&tmp, mod->map[SYM_ROLES ][i] -1 ,1 )) { - goto cleanup; - } - } - } -if (ebitmap_union (&dst->roles, &tmp)) { - goto cleanup; - } - dst->flags |= roles->flags; -ebitmap_destroy (&tmp); -return 0 ; - cleanup: -ERR (state->handle,"Out of memory!" ); -ebitmap_destroy (&tmp); -return -1 ; -} - -static int mls_level_convert (mls_semantic_level_t * src,mls_semantic_level_t * dst, -policy_module_t * mod,link_state_t * state) -{ -mls_semantic_cat_t *src_cat, *new_cat; - -if (!mod->policy->mls) -return 0 ; - -assert (mod->map[SYM_LEVELS ][src->sens -1 ]); - dst->sens = mod->map[SYM_LEVELS ][src->sens -1 ]; - -for (src_cat = src->cat; src_cat; src_cat = src_cat->next) { - new_cat = - (mls_semantic_cat_t *)malloc (sizeof (mls_semantic_cat_t)); -if (!new_cat) { -ERR (state->handle,"Out of memory" ); -return -1 ; - } -mls_semantic_cat_init (new_cat); - - new_cat->next = dst->cat; - dst->cat = new_cat; - -assert (mod->map[SYM_CATS ][src_cat->low -1 ]); - dst->cat->low = mod->map[SYM_CATS ][src_cat->low -1 ]; -assert (mod->map[SYM_CATS ][src_cat->high -1 ]); - dst->cat->high = mod->map[SYM_CATS ][src_cat->high -1 ]; - } - -return 0 ; -} - -static int mls_range_convert (mls_semantic_range_t * src,mls_semantic_range_t * dst, -policy_module_t * mod,link_state_t * state) -{ -int ret; - ret =mls_level_convert (&src->level[0 ], &dst->level[0 ], mod, state); -if (ret) -return ret; - ret =mls_level_convert (&src->level[1 ], &dst->level[1 ], mod, state); -if (ret) -return ret; -return 0 ; -} - -static int role_fix_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -unsigned int i; -char *id = key; -role_datum_t *role, *dest_role =NULL ; -link_state_t *state = (link_state_t *) data; -ebitmap_t e_tmp; -policy_module_t *mod = state->cur; -ebitmap_node_t *rnode; -hashtab_t role_tab; - - role = (role_datum_t *) datum; -if (state->dest_decl ==NULL ) - role_tab = state->base->p_roles.table; -else - role_tab = state->dest_decl->p_roles.table; - - dest_role =hashtab_search (role_tab, id); -assert (dest_role !=NULL ); - -if (state->verbose) { -INFO (state->handle,"fixing role %s" , id); - } - -ebitmap_init (&e_tmp); -ebitmap_for_each_bit (&role->dominates, rnode, i) { -if (ebitmap_node_get_bit (rnode, i)) { -assert (mod->map[SYM_ROLES ][i]); -if (ebitmap_set_bit - (&e_tmp, mod->map[SYM_ROLES ][i] -1 ,1 )) { - goto cleanup; - } - } - } -if (ebitmap_union (&dest_role->dominates, &e_tmp)) { - goto cleanup; - } -if (type_set_or_convert (&role->types, &dest_role->types, mod, state)) { - goto cleanup; - } -ebitmap_destroy (&e_tmp); -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -ebitmap_destroy (&e_tmp); -return -1 ; -} - -static int type_fix_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -unsigned int i; -char *id = key; -type_datum_t *type, *new_type =NULL ; -link_state_t *state = (link_state_t *) data; -ebitmap_t e_tmp; -policy_module_t *mod = state->cur; -ebitmap_node_t *tnode; -symtab_t *typetab; - - type = (type_datum_t *) datum; - -if (state->dest_decl ==NULL ) - typetab = &state->base->p_types; -else - typetab = &state->dest_decl->p_types; - -/* only fix attributes */ -if (type->flavor !=TYPE_ATTRIB ) { -return 0 ; - } - - new_type =hashtab_search (typetab->table, id); -assert (new_type !=NULL && new_type->flavor ==TYPE_ATTRIB ); - -if (state->verbose) { -INFO (state->handle,"fixing attribute %s" , id); - } - -ebitmap_init (&e_tmp); -ebitmap_for_each_bit (&type->types, tnode, i) { -if (ebitmap_node_get_bit (tnode, i)) { -assert (mod->map[SYM_TYPES ][i]); -if (ebitmap_set_bit - (&e_tmp, mod->map[SYM_TYPES ][i] -1 ,1 )) { - goto cleanup; - } - } - } -if (ebitmap_union (&new_type->types, &e_tmp)) { - goto cleanup; - } -ebitmap_destroy (&e_tmp); -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -ebitmap_destroy (&e_tmp); -return -1 ; -} - -static int user_fix_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -char *id = key; -user_datum_t *user, *new_user =NULL ; -link_state_t *state = (link_state_t *) data; -policy_module_t *mod = state->cur; -symtab_t *usertab; - - user = (user_datum_t *) datum; - -if (state->dest_decl ==NULL ) - usertab = &state->base->p_users; -else - usertab = &state->dest_decl->p_users; - - new_user =hashtab_search (usertab->table, id); -assert (new_user !=NULL ); - -if (state->verbose) { -INFO (state->handle,"fixing user %s" , id); - } - -if (role_set_or_convert (&user->roles, &new_user->roles, mod, state)) { - goto cleanup; - } - -if (mls_range_convert (&user->range, &new_user->range, mod, state)) - goto cleanup; - -if (mls_level_convert (&user->dfltlevel, &new_user->dfltlevel, mod, state)) - goto cleanup; - -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -return -1 ; -} - -static int (*fix_callback_f[SYM_NUM ]) (hashtab_key_t key,hashtab_datum_t datum, -void *datap) = { -NULL ,NULL , role_fix_callback, type_fix_callback, user_fix_callback, -NULL ,NULL ,NULL }; - -/*********** functions that copy AV rules ***********/ - -static int copy_avrule_list (avrule_t * list,avrule_t ** dst, -policy_module_t * module,link_state_t * state) -{ -unsigned int i; -avrule_t *cur, *new_rule =NULL , *tail; -class_perm_node_t *cur_perm, *new_perm, *tail_perm =NULL ; - - tail = *dst; -while (tail && tail->next) { - tail = tail->next; - } - - cur = list; -while (cur) { -if ((new_rule = (avrule_t *)malloc (sizeof (avrule_t))) ==NULL ) { - goto cleanup; - } -avrule_init (new_rule); - - new_rule->specified = cur->specified; - new_rule->flags = cur->flags; -if (type_set_convert - (&cur->stypes, &new_rule->stypes, module, state) ==-1 - ||type_set_convert (&cur->ttypes, &new_rule->ttypes, module, - state) ==-1 ) { - goto cleanup; - } - - cur_perm = cur->perms; - tail_perm =NULL ; -while (cur_perm) { -if ((new_perm = (class_perm_node_t *) -malloc (sizeof (class_perm_node_t))) ==NULL ) { - goto cleanup; - } -class_perm_node_init (new_perm); - - new_perm->class = - module->map[SYM_CLASSES ][cur_perm->class -1 ]; -assert (new_perm->class); - -if (new_rule->specified &AVRULE_AV ) { -for (i =0 ; - i < - module->perm_map_len[cur_perm->class -1 ]; - i++) { -if (!(cur_perm->data & (1U << i))) -continue ; - new_perm->data |= - (1U << - (module-> - perm_map[cur_perm->class -1 ][i] - -1 )); - } - }else { - new_perm->data = - module->map[SYM_TYPES ][cur_perm->data -1 ]; - } - -if (new_rule->perms ==NULL ) { - new_rule->perms = new_perm; - }else { - tail_perm->next = new_perm; - } - tail_perm = new_perm; - cur_perm = cur_perm->next; - } - new_rule->line = cur->line; - - cur = cur->next; - -if (*dst ==NULL ) { - *dst = new_rule; - }else { - tail->next = new_rule; - } - tail = new_rule; - } - -return 0 ; - cleanup: -ERR (state->handle,"Out of memory!" ); -avrule_destroy (new_rule); -free (new_rule); -return -1 ; -} - -static int copy_role_trans_list (role_trans_rule_t * list, -role_trans_rule_t ** dst, -policy_module_t * module,link_state_t * state) -{ -role_trans_rule_t *cur, *new_rule =NULL , *tail; - - cur = list; - tail = *dst; -while (tail && tail->next) { - tail = tail->next; - } -while (cur) { -if ((new_rule = - (role_trans_rule_t *)malloc (sizeof (role_trans_rule_t))) == -NULL ) { - goto cleanup; - } -role_trans_rule_init (new_rule); - -if (role_set_or_convert - (&cur->roles, &new_rule->roles, module, state) - ||type_set_or_convert (&cur->types, &new_rule->types, - module, state)) { - goto cleanup; - } - - new_rule->new_role = module->map[SYM_ROLES ][cur->new_role -1 ]; - -if (*dst ==NULL ) { - *dst = new_rule; - }else { - tail->next = new_rule; - } - tail = new_rule; - cur = cur->next; - } -return 0 ; - cleanup: -ERR (state->handle,"Out of memory!" ); -role_trans_rule_list_destroy (new_rule); -return -1 ; -} - -static int copy_role_allow_list (role_allow_rule_t * list, -role_allow_rule_t ** dst, -policy_module_t * module,link_state_t * state) -{ -role_allow_rule_t *cur, *new_rule =NULL , *tail; - - cur = list; - tail = *dst; -while (tail && tail->next) { - tail = tail->next; - } - -while (cur) { -if ((new_rule = - (role_allow_rule_t *)malloc (sizeof (role_allow_rule_t))) == -NULL ) { - goto cleanup; - } -role_allow_rule_init (new_rule); - -if (role_set_or_convert - (&cur->roles, &new_rule->roles, module, state) - ||role_set_or_convert (&cur->new_roles, - &new_rule->new_roles, module, - state)) { - goto cleanup; - } -if (*dst ==NULL ) { - *dst = new_rule; - }else { - tail->next = new_rule; - } - tail = new_rule; - cur = cur->next; - } -return 0 ; - cleanup: -ERR (state->handle,"Out of memory!" ); -role_allow_rule_list_destroy (new_rule); -return -1 ; -} - -static int copy_range_trans_list (range_trans_rule_t * rules, -range_trans_rule_t ** dst, -policy_module_t * mod,link_state_t * state) -{ -range_trans_rule_t *rule, *new_rule =NULL ; -unsigned int i; -ebitmap_node_t *cnode; - -for (rule = rules; rule; rule = rule->next) { - new_rule = - (range_trans_rule_t *)malloc (sizeof (range_trans_rule_t)); -if (!new_rule) - goto cleanup; - -range_trans_rule_init (new_rule); - - new_rule->next = *dst; - *dst = new_rule; - -if (type_set_convert (&rule->stypes, &new_rule->stypes, - mod, state)) - goto cleanup; - -if (type_set_convert (&rule->ttypes, &new_rule->ttypes, - mod, state)) - goto cleanup; - -ebitmap_for_each_bit (&rule->tclasses, cnode, i) { -if (ebitmap_node_get_bit (cnode, i)) { -assert (mod->map[SYM_CLASSES ][i]); -if (ebitmap_set_bit - (&new_rule->tclasses, - mod->map[SYM_CLASSES ][i] -1 ,1 )) { - goto cleanup; - } - } - } - -if (mls_range_convert (&rule->trange, &new_rule->trange, mod, state)) - goto cleanup; - } -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -range_trans_rule_list_destroy (new_rule); -return -1 ; -} - -static int copy_cond_list (cond_node_t * list,cond_node_t ** dst, -policy_module_t * module,link_state_t * state) -{ -unsigned i; -cond_node_t *cur, *new_node =NULL , *tail; -cond_expr_t *cur_expr; - tail = *dst; -while (tail && tail->next) - tail = tail->next; - - cur = list; -while (cur) { - new_node = (cond_node_t *)malloc (sizeof (cond_node_t)); -if (!new_node) { - goto cleanup; - } -memset (new_node,0 ,sizeof (cond_node_t)); - - new_node->cur_state = cur->cur_state; - new_node->expr =cond_copy_expr (cur->expr); -if (!new_node->expr) - goto cleanup; -/* go back through and remap the expression */ -for (cur_expr = new_node->expr; cur_expr !=NULL ; - cur_expr = cur_expr->next) { -/* expression nodes don't have a bool value of 0 - don't map them */ -if (cur_expr->expr_type !=COND_BOOL ) -continue ; -assert (module->map[SYM_BOOLS ][cur_expr->bool -1 ] !=0 ); - cur_expr->bool = - module->map[SYM_BOOLS ][cur_expr->bool -1 ]; - } - new_node->nbools = cur->nbools; -/* FIXME should COND_MAX_BOOLS be used here? */ -for (i =0 ; i <min (cur->nbools,COND_MAX_BOOLS ); i++) { -uint32_t remapped_id = - module->map[SYM_BOOLS ][cur->bool_ids[i] -1 ]; -assert (remapped_id !=0 ); - new_node->bool_ids[i] = remapped_id; - } - new_node->expr_pre_comp = cur->expr_pre_comp; - -if (copy_avrule_list - (cur->avtrue_list, &new_node->avtrue_list, module, state) - ||copy_avrule_list (cur->avfalse_list, - &new_node->avfalse_list, module, - state)) { - goto cleanup; - } - -if (*dst ==NULL ) { - *dst = new_node; - }else { - tail->next = new_node; - } - tail = new_node; - cur = cur->next; - } -return 0 ; - cleanup: -ERR (state->handle,"Out of memory!" ); -cond_node_destroy (new_node); -free (new_node); -return -1 ; - -} - -/*********** functions that copy avrule_decls from module to base ***********/ - -static int copy_identifiers (link_state_t * state,symtab_t * src_symtab, -avrule_decl_t * dest_decl) -{ -int i, ret; - - state->dest_decl = dest_decl; -for (i =0 ; i <SYM_NUM ; i++) { -if (copy_callback_f[i] !=NULL ) { - ret = -hashtab_map (src_symtab[i].table, copy_callback_f[i], - state); -if (ret) { -return ret; - } - } - } - -if (hashtab_map - (src_symtab[SYM_TYPES ].table, alias_copy_callback, state)) { -return -1 ; - } - -/* then fix bitmaps associated with those newly copied identifiers */ -for (i =0 ; i <SYM_NUM ; i++) { -if (fix_callback_f[i] !=NULL && -hashtab_map (src_symtab[i].table, fix_callback_f[i], - state)) { -return -1 ; - } - } -return 0 ; -} - -static int copy_scope_index (scope_index_t * src,scope_index_t * dest, -policy_module_t * module,link_state_t * state) -{ -unsigned int i, j; -uint32_t largest_mapped_class_value =0 ; -ebitmap_node_t *node; -/* copy the scoping information for this avrule decl block */ -for (i =0 ; i <SYM_NUM ; i++) { -ebitmap_t *srcmap = src->scope + i; -ebitmap_t *destmap = dest->scope + i; -if (copy_callback_f[i] ==NULL ) { -continue ; - } -ebitmap_for_each_bit (srcmap, node, j) { -if (ebitmap_node_get_bit (node, j)) { -assert (module->map[i][j] !=0 ); -if (ebitmap_set_bit - (destmap, module->map[i][j] -1 ,1 ) !=0 ) { - - goto cleanup; - } -if (i ==SYM_CLASSES && - largest_mapped_class_value < - module->map[SYM_CLASSES ][j]) { - largest_mapped_class_value = - module->map[SYM_CLASSES ][j]; - } - } - } - } - -/* next copy the enabled permissions data */ -if ((dest->class_perms_map =malloc (largest_mapped_class_value * -sizeof (*dest->class_perms_map))) == -NULL ) { - goto cleanup; - } -for (i =0 ; i < largest_mapped_class_value; i++) { -ebitmap_init (dest->class_perms_map + i); - } - dest->class_perms_len = largest_mapped_class_value; -for (i =0 ; i < src->class_perms_len; i++) { -ebitmap_t *srcmap = src->class_perms_map + i; -ebitmap_t *destmap = - dest->class_perms_map + module->map[SYM_CLASSES ][i] -1 ; -ebitmap_for_each_bit (srcmap, node, j) { -if (ebitmap_node_get_bit (node, j) && -ebitmap_set_bit (destmap, module->perm_map[i][j] -1 , -1 )) { - goto cleanup; - } - } - } - -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -return -1 ; -} - -static int copy_avrule_decl (link_state_t * state,policy_module_t * module, -avrule_decl_t * src_decl,avrule_decl_t * dest_decl) -{ -int ret; - -/* copy all of the RBAC and TE rules */ -if (copy_avrule_list - (src_decl->avrules, &dest_decl->avrules, module, state) ==-1 - ||copy_role_trans_list (src_decl->role_tr_rules, - &dest_decl->role_tr_rules, module, - state) ==-1 - ||copy_role_allow_list (src_decl->role_allow_rules, - &dest_decl->role_allow_rules, module, - state) ==-1 - ||copy_cond_list (src_decl->cond_list, &dest_decl->cond_list, - module, state) ==-1 ) { -return -1 ; - } - -if (copy_range_trans_list (src_decl->range_tr_rules, - &dest_decl->range_tr_rules, module, state)) -return -1 ; - -/* finally copy any identifiers local to this declaration */ - ret =copy_identifiers (state, src_decl->symtab, dest_decl); -if (ret <0 ) { -return ret; - } - -/* then copy required and declared scope indices here */ -if (copy_scope_index (&src_decl->required, &dest_decl->required, - module, state) ==-1 || -copy_scope_index (&src_decl->declared, &dest_decl->declared, - module, state) ==-1 ) { -return -1 ; - } - -return 0 ; -} - -static int copy_avrule_block (link_state_t * state,policy_module_t * module, -avrule_block_t * block) -{ -avrule_block_t *new_block =avrule_block_create (); -avrule_decl_t *decl, *last_decl =NULL ; -int ret; - -if (new_block ==NULL ) { -ERR (state->handle,"Out of memory!" ); - ret =-1 ; - goto cleanup; - } - - new_block->flags = block->flags; - -for (decl = block->branch_list; decl !=NULL ; decl = decl->next) { -avrule_decl_t *new_decl = -avrule_decl_create (state->next_decl_id); -if (new_decl ==NULL ) { -ERR (state->handle,"Out of memory!" ); - ret =-1 ; - goto cleanup; - } - -if (module->policy->name !=NULL ) { - new_decl->module_name =strdup (module->policy->name); -if (new_decl->module_name ==NULL ) { -ERR (state->handle,"Out of memory\n" ); - ret =-1 ; - goto cleanup; - } - } - -if (last_decl ==NULL ) { - new_block->branch_list = new_decl; - }else { - last_decl->next = new_decl; - } - last_decl = new_decl; - state->base->decl_val_to_struct[state->next_decl_id -1 ] = - new_decl; - state->decl_to_mod[state->next_decl_id] = module->policy; - - module->avdecl_map[decl->decl_id] = new_decl->decl_id; - - ret =copy_avrule_decl (state, module, decl, new_decl); -if (ret) { - goto cleanup; - } - - state->next_decl_id++; - } - state->last_avrule_block->next = new_block; - state->last_avrule_block = new_block; -return 0 ; - - cleanup: -avrule_block_list_destroy (new_block); -return ret; -} - -static int scope_copy_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) -{ -unsigned int i; -int ret; -char *id = key, *new_id =NULL ; -scope_datum_t *scope, *base_scope; -link_state_t *state = (link_state_t *) data; -uint32_t symbol_num = state->symbol_num; -uint32_t *avdecl_map = state->cur->avdecl_map; - - scope = (scope_datum_t *) datum; - -/* check if the base already has a scope entry */ - base_scope =hashtab_search (state->base->scope[symbol_num].table, id); -if (base_scope ==NULL ) { -scope_datum_t *new_scope; -if ((new_id =strdup (id)) ==NULL ) { - goto cleanup; - } - -if ((new_scope = - (scope_datum_t *)calloc (1 ,sizeof (*new_scope))) ==NULL ) { -free (new_id); - goto cleanup; - } - ret =hashtab_insert (state->base->scope[symbol_num].table, - (hashtab_key_t ) new_id, - (hashtab_datum_t ) new_scope); -if (ret) { -free (new_id); -free (new_scope); - goto cleanup; - } - new_scope->scope =SCOPE_REQ ;/* this is reset further down */ - base_scope = new_scope; - } -if (base_scope->scope ==SCOPE_REQ && scope->scope ==SCOPE_DECL ) { -/* this module declared symbol, so overwrite the old -* list with the new decl ids */ - base_scope->scope =SCOPE_DECL ; -free (base_scope->decl_ids); - base_scope->decl_ids =NULL ; - base_scope->decl_ids_len =0 ; -for (i =0 ; i < scope->decl_ids_len; i++) { -if (add_i_to_a (avdecl_map[scope->decl_ids[i]], - &base_scope->decl_ids_len, - &base_scope->decl_ids) ==-1 ) { - goto cleanup; - } - } - }else if (base_scope->scope ==SCOPE_DECL && scope->scope ==SCOPE_REQ ) { -/* this module depended on a symbol that now exists, -* so don't do anything */ - }else if (base_scope->scope ==SCOPE_REQ && scope->scope ==SCOPE_REQ ) { -/* symbol is still required, so add to the list */ -for (i =0 ; i < scope->decl_ids_len; i++) { -if (add_i_to_a (avdecl_map[scope->decl_ids[i]], - &base_scope->decl_ids_len, - &base_scope->decl_ids) ==-1 ) { - goto cleanup; - } - } - }else { -/* this module declared a symbol, and it was already -* declared. only roles and users may be multiply -* declared; for all others this is an error. */ -if (symbol_num !=SYM_ROLES && symbol_num !=SYM_USERS ) { -ERR (state->handle, -"%s: Duplicate declaration in module: %s %s" , - state->cur_mod_name, - symtab_names[state->symbol_num], id); -return -1 ; - } -for (i =0 ; i < scope->decl_ids_len; i++) { -if (add_i_to_a (avdecl_map[scope->decl_ids[i]], - &base_scope->decl_ids_len, - &base_scope->decl_ids) ==-1 ) { - goto cleanup; - } - } - } -return 0 ; - - cleanup: -ERR (state->handle,"Out of memory!" ); -return -1 ; -} - -/* Copy a module over to a base, remapping all values within. After -* all identifiers and rules are done, copy the scoping information. -* This is when it checks for duplicate declarations. */ -static int copy_module (link_state_t * state,policy_module_t * module) -{ -int i, ret; -avrule_block_t *cur; - state->cur = module; - state->cur_mod_name = module->policy->name; - -/* first copy all of the identifiers */ - ret =copy_identifiers (state, module->policy->symtab,NULL ); -if (ret) { -return ret; - } - -/* next copy all of the avrule blocks */ -for (cur = module->policy->global; cur !=NULL ; cur = cur->next) { - ret =copy_avrule_block (state, module, cur); -if (ret) { -return ret; - } - } - -/* then copy the scoping tables */ -for (i =0 ; i <SYM_NUM ; i++) { - state->symbol_num = i; -if (hashtab_map - (module->policy->scope[i].table, scope_copy_callback, - state)) { -return -1 ; - } - } - -return 0 ; -} - -/***** functions that check requirements and enable blocks in a module ******/ - -/* borrowed from checkpolicy.c */ - -struct find_perm_arg { -unsigned int valuep; -hashtab_key_t key; -}; - -static int find_perm (hashtab_key_t key,hashtab_datum_t datum,void *varg) -{ - -struct find_perm_arg *arg = varg; - -perm_datum_t *perdatum = (perm_datum_t *) datum; -if (arg->valuep == perdatum->s.value) { - arg->key = key; -return 1 ; - } - -return 0 ; -} - -/* Check if the requirements are met for a single declaration. If all -* are met return 1. For the first requirement found to be missing, -* if 'missing_sym_num' and 'missing_value' are both not NULL then -* write to them the symbol number and value for the missing -* declaration. Then return 0 to indicate a missing declaration. -* Note that if a declaration had no requirement at all (e.g., an ELSE -* block) this returns 1. */ -static int is_decl_requires_met (link_state_t * state, -avrule_decl_t * decl, -struct missing_requirement *req) -{ -/* (This algorithm is very unoptimized. It performs many -* redundant checks. A very obvious improvement is to cache -* which symbols have been verified, so that they do not need -* to be re-checked.) */ -unsigned int i, j; -ebitmap_t *bitmap; -char *id, *perm_id; -policydb_t *pol = state->base; -ebitmap_node_t *node; - -/* check that all symbols have been satisfied */ -for (i =0 ; i <SYM_NUM ; i++) { -if (i ==SYM_CLASSES ) { -/* classes will be checked during permissions -* checking phase below */ -continue ; - } - bitmap = &decl->required.scope[i]; -ebitmap_for_each_bit (bitmap, node, j) { -if (!ebitmap_node_get_bit (node, j)) { -continue ; - } - -/* check base's scope table */ - id = pol->sym_val_to_name[i][j]; -if (!is_id_enabled (id, state->base, i)) { -/* this symbol was not found */ -if (req !=NULL ) { - req->symbol_type = i; - req->symbol_value = j +1 ; - } -return 0 ; - } - } - } -/* check that all classes and permissions have been satisfied */ -for (i =0 ; i < decl->required.class_perms_len; i++) { - - bitmap = decl->required.class_perms_map + i; -ebitmap_for_each_bit (bitmap, node, j) { -struct find_perm_arg fparg; -class_datum_t *cladatum; -uint32_t perm_value = j +1 ; -scope_datum_t *scope; - -if (!ebitmap_node_get_bit (node, j)) { -continue ; - } - id = pol->p_class_val_to_name[i]; - cladatum = pol->class_val_to_struct[i]; - - scope = -hashtab_search (state->base->p_classes_scope.table, - id); -if (scope ==NULL ) { -ERR (state->handle, -"Could not find scope information for class %s" , - id); -return -1 ; - } -if (scope->scope ==SCOPE_REQ ) -return 0 ; - - fparg.valuep = perm_value; - fparg.key =NULL ; - -hashtab_map (cladatum->permissions.table, find_perm, - &fparg); -if (fparg.key ==NULL && cladatum->comdatum !=NULL ) -hashtab_map (cladatum->comdatum->permissions. - table, find_perm, &fparg); - perm_id = fparg.key; - -assert (perm_id !=NULL ); -if (!is_perm_enabled (id, perm_id, state->base)) { -if (req !=NULL ) { - req->symbol_type =SYM_CLASSES ; - req->symbol_value = i +1 ; - req->perm_value = perm_value; - } -return 0 ; - } - } - } - -/* all requirements have been met */ -return 1 ; -} - -static int debug_requirements (link_state_t * state,policydb_t * p) -{ -int ret; -avrule_block_t *cur; -missing_requirement_t req; - -for (cur = p->global; cur !=NULL ; cur = cur->next) { -if (cur->enabled !=NULL ) -continue ; - - ret =is_decl_requires_met (state, cur->branch_list, &req); -if (ret <0 ) { -return ret; - }else if (ret ==0 ) { -char *mod_name = cur->branch_list->module_name ? - cur->branch_list->module_name :"BASE" ; -if (req.symbol_type ==SYM_CLASSES ) { - -struct find_perm_arg fparg; - -class_datum_t *cladatum; - cladatum = - p->class_val_to_struct[req.symbol_value - -1 ]; - - fparg.valuep = req.perm_value; - fparg.key =NULL ; -hashtab_map (cladatum->permissions.table, - find_perm, &fparg); - -if (cur->flags &AVRULE_OPTIONAL ) { -ERR (state->handle, -"%s[%d]'s optional requirements were not met: class %s, permission %s" , - mod_name, cur->branch_list->decl_id, - p->p_class_val_to_name[req. - symbol_value - -1 ], - fparg.key); - }else { -ERR (state->handle, -"%s[%d]'s global requirements were not met: class %s, permission %s" , - mod_name, cur->branch_list->decl_id, - p->p_class_val_to_name[req. - symbol_value - -1 ], - fparg.key); - } - }else { -if (cur->flags &AVRULE_OPTIONAL ) { -ERR (state->handle, -"%s[%d]'s optional requirements were not met: %s %s" , - mod_name, cur->branch_list->decl_id, - symtab_names[req.symbol_type], - p->sym_val_to_name[req. - symbol_type][req. - symbol_value - - -1 ]); - }else { -ERR (state->handle, -"%s[%d]'s global requirements were not met: %s %s" , - mod_name, cur->branch_list->decl_id, - symtab_names[req.symbol_type], - p->sym_val_to_name[req. - symbol_type][req. - symbol_value - - -1 ]); - } - } - } - } -return 0 ; -} - -static void print_missing_requirements (link_state_t * state, -avrule_block_t * cur, -missing_requirement_t * req) -{ -policydb_t *p = state->base; -char *mod_name = cur->branch_list->module_name ? - cur->branch_list->module_name :"BASE" ; - -if (req->symbol_type ==SYM_CLASSES ) { - -struct find_perm_arg fparg; - -class_datum_t *cladatum; - cladatum = p->class_val_to_struct[req->symbol_value -1 ]; - - fparg.valuep = req->perm_value; - fparg.key =NULL ; -hashtab_map (cladatum->permissions.table, find_perm, &fparg); - -ERR (state->handle, -"%s's global requirements were not met: class %s, permission %s" , - mod_name, - p->p_class_val_to_name[req->symbol_value -1 ], fparg.key); - }else { -ERR (state->handle, -"%s's global requirements were not met: %s %s" , - mod_name, - symtab_names[req->symbol_type], - p->sym_val_to_name[req->symbol_type][req->symbol_value - -1 ]); - } -} - -/* Enable all of the avrule_decl blocks for the policy. This simple -* algorithm is the following: -* -* 1) Enable all of the non-else avrule_decls for all blocks. -* 2) Iterate through the non-else decls looking for decls whose requirements -* are not met. -* 2a) If the decl is non-optional, return immediately with an error. -* 2b) If the decl is optional, disable the block and mark changed = 1 -* 3) If changed == 1 goto 2. -* 4) Iterate through all blocks looking for those that have no enabled -* decl. If the block has an else decl, enable. -* -* This will correctly handle all dependencies, including mutual and -* cicular. The only downside is that it is slow. -*/ -static int enable_avrules (link_state_t * state,policydb_t * pol) -{ -int changed =1 ; -avrule_block_t *block; -avrule_decl_t *decl; -missing_requirement_t req; -int ret =0 , rc; - -if (state->verbose) { -INFO (state->handle,"Determining which avrules to enable." ); - } - -/* 1) enable all of the non-else blocks */ -for (block = pol->global; block !=NULL ; block = block->next) { - block->enabled = block->branch_list; - block->enabled->enabled =1 ; -for (decl = block->branch_list->next; decl !=NULL ; - decl = decl->next) - decl->enabled =0 ; - } - -/* 2) Iterate */ -while (changed) { - changed =0 ; -for (block = pol->global; block !=NULL ; block = block->next) { -if (block->enabled ==NULL ) { -continue ; - } - decl = block->branch_list; -if (state->verbose) { -char *mod_name = decl->module_name ? - decl->module_name :"BASE" ; -INFO (state->handle,"check module %s decl %d\n" , - mod_name, decl->decl_id); - } - rc =is_decl_requires_met (state, decl, &req); -if (rc <0 ) { - ret =SEPOL_ERR ; - goto out; - }else if (rc ==0 ) { - decl->enabled =0 ; - block->enabled =NULL ; - changed =1 ; -if (!(block->flags &AVRULE_OPTIONAL )) { -print_missing_requirements (state, block, - &req); - ret =SEPOL_EREQ ; - goto out; - } - } - } - } - -/* 4) else handling -* -* Iterate through all of the blocks skipping the first (which is the -* global block, is required to be present, and cannot have an else). -* If the block is disabled and has an else decl, enable that. -* -* This code assumes that the second block in the branch list is the else -* block. This is currently supported by the compiler. -*/ -for (block = pol->global->next; block !=NULL ; block = block->next) { -if (block->enabled ==NULL ) { -if (block->branch_list->next !=NULL ) { - block->enabled = block->branch_list->next; - block->branch_list->next->enabled =1 ; - } - } - } - - out: -if (state->verbose) -debug_requirements (state, pol); - -return ret; -} - -/*********** the main linking functions ***********/ - -/* Given a module's policy, normalize all conditional expressions -* within. Return 0 on success, -1 on error. */ -static int cond_normalize (policydb_t * p) -{ -avrule_block_t *block; -for (block = p->global; block !=NULL ; block = block->next) { -avrule_decl_t *decl; -for (decl = block->branch_list; decl !=NULL ; decl = decl->next) { -cond_list_t *cond = decl->cond_list; -while (cond) { -if (cond_normalize_expr (p, cond) <0 ) -return -1 ; - cond = cond->next; - } - } - } -return 0 ; -} - -/* Allocate space for the various remapping arrays. */ -static int prepare_module (link_state_t * state,policy_module_t * module) -{ -int i; -uint32_t items, num_decls =0 ; -avrule_block_t *cur; - -/* allocate the maps */ -for (i =0 ; i <SYM_NUM ; i++) { - items = module->policy->symtab[i].nprim; -if ((module->map[i] = - (uint32_t *)calloc (items, -sizeof (*module->map[i]))) ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - } - -/* allocate the permissions remap here */ - items = module->policy->p_classes.nprim; -if ((module->perm_map_len = -calloc (items,sizeof (*module->perm_map_len))) ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } -if ((module->perm_map = -calloc (items,sizeof (*module->perm_map))) ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - -/* allocate a map for avrule_decls */ -for (cur = module->policy->global; cur !=NULL ; cur = cur->next) { -avrule_decl_t *decl; -for (decl = cur->branch_list; decl !=NULL ; decl = decl->next) { -if (decl->decl_id > num_decls) { - num_decls = decl->decl_id; - } - } - } - num_decls++; -if ((module->avdecl_map =calloc (num_decls,sizeof (uint32_t ))) ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - module->num_decls = num_decls; - -/* normalize conditionals within */ -if (cond_normalize (module->policy) <0 ) { -ERR (state->handle, -"Error while normalizing conditionals within the module %s." , - module->policy->name); -return -1 ; - } -return 0 ; -} - -static int prepare_base (link_state_t * state,uint32_t num_mod_decls) -{ -avrule_block_t *cur = state->base->global; -assert (cur !=NULL ); - state->next_decl_id =0 ; - -/* iterate through all of the declarations in the base, to -determine what the next decl_id should be */ -while (cur !=NULL ) { -avrule_decl_t *decl; -for (decl = cur->branch_list; decl !=NULL ; decl = decl->next) { -if (decl->decl_id > state->next_decl_id) { - state->next_decl_id = decl->decl_id; - } - } - state->last_avrule_block = cur; - cur = cur->next; - } - state->last_base_avrule_block = state->last_avrule_block; - state->next_decl_id++; - -/* allocate the table mapping from base's decl_id to its -* avrule_decls and set the initial mappings */ -free (state->base->decl_val_to_struct); -if ((state->base->decl_val_to_struct = -calloc (state->next_decl_id + num_mod_decls, -sizeof (*(state->base->decl_val_to_struct)))) ==NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } -/* This allocates the decl block to module mapping used for error reporting */ -if ((state->decl_to_mod =calloc (state->next_decl_id + num_mod_decls, -sizeof (*(state->decl_to_mod)))) == -NULL ) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - cur = state->base->global; -while (cur !=NULL ) { -avrule_decl_t *decl = cur->branch_list; -while (decl !=NULL ) { - state->base->decl_val_to_struct[decl->decl_id -1 ] = - decl; - state->decl_to_mod[decl->decl_id] = state->base; - decl = decl->next; - } - cur = cur->next; - } - -/* normalize conditionals within */ -if (cond_normalize (state->base) <0 ) { -ERR (state->handle, -"Error while normalizing conditionals within the base module." ); -return -1 ; - } -return 0 ; -} - -/* Link a set of modules into a base module. This process is somewhat -* similar to an actual compiler: it requires a set of order dependent -* steps. The base and every module must have been indexed prior to -* calling this function. -*/ -int link_modules (sepol_handle_t * handle, -policydb_t * b,policydb_t ** mods,int len,int verbose) -{ -int i, ret, retval =-1 ; -policy_module_t **modules =NULL ; -link_state_t state; -uint32_t num_mod_decls =0 ; - -memset (&state,0 ,sizeof (state)); - state.base = b; - state.verbose = verbose; - state.handle = handle; - -if (b->policy_type !=POLICY_BASE ) { -ERR (state.handle,"Target of link was not a base policy." ); -return -1 ; - } - -/* first allocate some space to hold the maps from module -* symbol's value to the destination symbol value; then do -* other preparation work */ -if ((modules = - (policy_module_t **)calloc (len,sizeof (*modules))) ==NULL ) { -ERR (state.handle,"Out of memory!" ); -return -1 ; - } -for (i =0 ; i < len; i++) { -if (mods[i]->policy_type !=POLICY_MOD ) { -ERR (state.handle, -"Tried to link in a policy that was not a module." ); - goto cleanup; - } - -if (mods[i]->mls != b->mls) { -if (b->mls) -ERR (state.handle, -"Tried to link in a non-MLS module with an MLS base." ); -else -ERR (state.handle, -"Tried to link in an MLS module with a non-MLS base." ); - goto cleanup; - } - -if ((modules[i] = - (policy_module_t *)calloc (1 , -sizeof (policy_module_t))) == -NULL ) { -ERR (state.handle,"Out of memory!" ); - goto cleanup; - } - modules[i]->policy = mods[i]; -if (prepare_module (&state, modules[i]) ==-1 ) { - goto cleanup; - } - num_mod_decls += modules[i]->num_decls; - } -if (prepare_base (&state, num_mod_decls) ==-1 ) { - goto cleanup; - } - -/* copy all types, declared and required */ -for (i =0 ; i < len; i++) { - state.cur = modules[i]; - state.cur_mod_name = modules[i]->policy->name; - ret = -hashtab_map (modules[i]->policy->p_types.table, - type_copy_callback, &state); -if (ret) { - retval = ret; - goto cleanup; - } - } - -/* then copy everything else, including aliases, and fixup attributes */ -for (i =0 ; i < len; i++) { - state.cur = modules[i]; - state.cur_mod_name = modules[i]->policy->name; - ret = -copy_identifiers (&state, modules[i]->policy->symtab,NULL ); -if (ret) { - retval = ret; - goto cleanup; - } - } - -if (policydb_index_others (state.handle, state.base,0 )) { -ERR (state.handle,"Error while indexing others" ); - goto cleanup; - } - -/* copy and remap the module's data over to base */ -for (i =0 ; i < len; i++) { - state.cur = modules[i]; - ret =copy_module (&state, modules[i]); -if (ret) { - retval = ret; - goto cleanup; - } - } - -/* re-index base, for symbols were added to symbol tables */ -if (policydb_index_classes (state.base)) { -ERR (state.handle,"Error while indexing classes" ); - goto cleanup; - } -if (policydb_index_others (state.handle, state.base,0 )) { -ERR (state.handle,"Error while indexing others" ); - goto cleanup; - } - -if (enable_avrules (&state, state.base)) { - retval =SEPOL_EREQ ; - goto cleanup; - } - - retval =0 ; - cleanup: -for (i =0 ; modules !=NULL && i < len; i++) { -policy_module_destroy (modules[i]); - } -free (modules); -free (state.decl_to_mod); -return retval; -}
libsepol/src/module.c deleted +0 -952
761 lines not on this page · raw message
libsepol/include/sepol/policydb/expand.h deleted +0 -78
78 lines not on this page · raw message
libsepol/include/sepol/policydb/link.h deleted +0 -20
Lines of this file are not shown. View the raw message.
libsepol/include/sepol/policydb/module.h deleted +0 -48
48 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Part of the diff is not shown here. View the raw message.
PATCH 1/5 << Part 1 >> This patch removes files that existed solely to support modular policies.
In reply to Mark Goldman---
libsepol/include/sepol/module.h | 82 0 + 82 - 0 !
libsepol/src/expand.c | 2679 0 + 2679 - 0 !
2 files changed, 2761 deletions(-)
libsepol/include/sepol/module.h deleted +0 -82
@@ -1,82 +0,0 @@ -#ifndef _SEPOL_MODULE_H_ -#define _SEPOL_MODULE_H_ - -#include <stddef.h> -#include <stdio.h> -#include <stdint.h> - -#include <sepol/handle.h> -#include <sepol/policydb.h> - -struct sepol_module_package ; -typedef struct sepol_module_package sepol_module_package_t ; - -/* Module package public interfaces. */ - -extern int sepol_module_package_create (sepol_module_package_t ** p); - -extern void sepol_module_package_free (sepol_module_package_t * p); - -extern char *sepol_module_package_get_file_contexts (sepol_module_package_t * p); - -extern size_t sepol_module_package_get_file_contexts_len (sepol_module_package_t - * p); - -extern int sepol_module_package_set_file_contexts (sepol_module_package_t * p, -char *data,size_t len); - -extern char *sepol_module_package_get_seusers (sepol_module_package_t * p); - -extern size_t sepol_module_package_get_seusers_len (sepol_module_package_t * p); - -extern int sepol_module_package_set_seusers (sepol_module_package_t * p, -char *data,size_t len); - -extern char *sepol_module_package_get_user_extra (sepol_module_package_t * p); - -extern size_t sepol_module_package_get_user_extra_len (sepol_module_package_t * - p); - -extern int sepol_module_package_set_user_extra (sepol_module_package_t * p, -char *data,size_t len); - -extern char *sepol_module_package_get_netfilter_contexts (sepol_module_package_t - * p); - -extern size_t -sepol_module_package_get_netfilter_contexts_len (sepol_module_package_t * p); - -extern int sepol_module_package_set_netfilter_contexts (sepol_module_package_t * - p,char *data, -size_t len); - -extern sepol_policydb_t *sepol_module_package_get_policy (sepol_module_package_t - * p); - -extern int sepol_link_packages (sepol_handle_t * handle, -sepol_module_package_t * base, -sepol_module_package_t ** modules, -int num_modules,int verbose); - -extern int sepol_module_package_read (sepol_module_package_t * mod, -struct sepol_policy_file *file, -int verbose); - -extern int sepol_module_package_info (struct sepol_policy_file *file, -int *type,char **name,char **version); - -extern int sepol_module_package_write (sepol_module_package_t * p, -struct sepol_policy_file *file); - -/* Module linking/expanding public interfaces. */ - -extern int sepol_link_modules (sepol_handle_t * handle, -sepol_policydb_t * base, -sepol_policydb_t ** modules, -size_t len,int verbose); - -extern int sepol_expand_module (sepol_handle_t * handle, -sepol_policydb_t * base, -sepol_policydb_t * out,int verbose,int check); - -#endif
libsepol/src/expand.c deleted +0 -2679
2,679 lines not on this page · raw message
Signature · 4 lines
-- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Joshua Brindle
AppliedQuoted 10 lines · Mark Goldman
This patchset removes module support from libsepol. This patchset is in support of the policyrep work.
Patches one and two removes the files that are solely for supporting modular policies.
Patch three and four eliminates the various structures that supported modular policies. All functional changes occur in this patch.
Patch five puts dispol in libsepol's utils directory. Dispol was not modified in this move.
Quoted 7 lines
This e-mail splits the original patches up so that they will go through the list-serv without being bounced.
-mdg
Signed-off-by Joshua Brindle <[email protected]>
Merged into the policyrep branch with changes necessary due to the alignment patch that was applied to trunk.
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.