Allow type attributes to be associated with other type attributes
PATCH v3 1/1 Allow type attributes to be associated with other type attributes
Allow the following:
attribute at1;
attribute at2;
typeattribute at2 at1;
This associates the attribute at2 with the attribute at1. Any types associated with at2 will be associated with at1 as well when the policy is expanded. A loop in the associations between attributes will produce an error.
The kernel binary policy is not effected by this change because all attribute associations are expanded when the policy is expanded. The modular policy is effected because compiled modules and the linked modular policy can now have type attributes associated with other type attributes, so a new module version is needed to prevent an older policy toolchain from creating a kernel binary policy that has type attributes associated with other attributes.
Also validate that the types field of a type datum for an attribute is empty for kernel policies and does not contain an attribute for modular policies before this new version.
This was suggested by Vit Mojzis <[email protected]> who also provided an initial patch for checkpolicy.
---
V3: Fix potential NULL dereference in expand_attributes_in_attributes() and
change the language of the error message.
V2: relocated call of expand_attributes_in_attributes() because of recent commits
checkpolicy/policy_define.c | 9 +++- libsepol/include/sepol/policydb/policydb.h | 3 +- libsepol/src/expand.c | 54 ++++++++++++++++++++++ libsepol/src/policydb.c | 25 ++++++++-- libsepol/src/policydb_validate.c | 30 +++++++++++- 5 files changed, 113 insertions(+), 8 deletions(-)
checkpolicy/policy_define.c +8 -1
@@ -1440,11 +1440,18 @@ int define_typeattribute(void)return -1 ; } t =hashtab_search (policydbp->p_types.table, id); -if (!t || t->flavor ==TYPE_ATTRIB ) { +if (!t) {yyerror2 ("unknown type %s" , id);free (id);return -1 ; + }else if (t->flavor ==TYPE_ATTRIB ) { +if (policydbp->policy_type !=POLICY_KERN && policydbp->policyvers <MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS ) { +yyerror2 ("Type attributes cannot be used in a typeattribute definition in policy version %d" , policydbp->policyvers); +free (id); +return -1 ; + } } +free (id);while ((id =queue_remove (id_queue))) {
libsepol/include/sepol/policydb/policydb.h +2 -1
@@ -802,9 +802,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define MOD_POLICYDB_VERSION_SELF_TYPETRANS 21#define MOD_POLICYDB_VERSION_COND_XPERMS 22#define MOD_POLICYDB_VERSION_NEVERAUDIT 23 +#define MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS 24#define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE -#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_NEVERAUDIT +#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS#define POLICYDB_CONFIG_MLS 1
libsepol/src/expand.c +54 -0
66 lines not on this page · raw message
libsepol/src/policydb.c +21 -4
@@ -362,6 +362,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_BASE, + .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_MOD, .version = MOD_POLICYDB_VERSION_BASE,
@@ -502,6 +509,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = 0, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_MOD, + .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS, + .sym_num = SYM_NUM, + .ocon_num = 0, + .target_platform = SEPOL_TARGET_SELINUX, + }, };#if 0
@@ -4492,16 +4506,19 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)if (r_policyvers >=POLICYDB_VERSION_AVTAB ) {if (ebitmap_read (&p->type_attr_map[i], fp)) goto bad; -ebitmap_for_each_positive_bit (&p->type_attr_map[i], - tnode, j) { +ebitmap_for_each_positive_bit (&p->type_attr_map[i], tnode, j) {if (i == j)continue ;if (j >= p->p_types.nprim) goto bad; -if (ebitmap_set_bit - (&p->attr_type_map[j], i,1 )) +if (p->type_val_to_struct[i]->flavor ==TYPE_ATTRIB ) { +ERR (fp->handle,"Invalid to have type attributes associated with an attribute for a kernel policy" ); + goto bad; + } + +if (ebitmap_set_bit (&p->attr_type_map[j], i,1 )) goto bad; } }
libsepol/src/policydb_validate.c +28 -2
@@ -566,6 +566,27 @@ bad:return -1 ; } +static int validate_types_in_attribute (const ebitmap_t *map,const policydb_t *p,const validate_t flavors[SYM_NUM ]) +{ +ebitmap_node_t *node; +uint32_t i; + +if (validate_ebitmap (map, &flavors[SYM_TYPES ])) + goto bad; + +if (p->policy_type !=POLICY_KERN && p->policyvers <MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS ) { +ebitmap_for_each_positive_bit (map, node, i) { +if (validate_simpletype (i+1 , p, flavors)) + goto bad; + } + } + +return 0 ; + +bad: +return -1 ; +} +static int validate_type_datum (sepol_handle_t *handle,const type_datum_t *type,const policydb_t *p,validate_t flavors[]) {if (validate_value (type->s.value, &flavors[SYM_TYPES ]))
@@ -582,8 +603,13 @@ static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type, goto bad;break ;case TYPE_ATTRIB : -if (validate_ebitmap (&type->types, &flavors[SYM_TYPES ])) - goto bad; +if (p->policy_type ==POLICY_KERN ) { +if (!ebitmap_is_empty (&type->types)) + goto bad; + }else { +if (validate_types_in_attribute (&type->types, p, flavors)) + goto bad; + }if (type->bounds) goto bad;break ;
Signature · 1 line
2.50.0
Quoted 19 lines · James Carter
Allow the following:
attribute at1;
attribute at2;
typeattribute at2 at1;This associates the attribute at2 with the attribute at1. Any types associated with at2 will be associated with at1 as well when the policy is expanded. A loop in the associations between attributes will produce an error.
The kernel binary policy is not effected by this change because all attribute associations are expanded when the policy is expanded. The modular policy is effected because compiled modules and the linked modular policy can now have type attributes associated with other type attributes, so a new module version is needed to prevent an older policy toolchain from creating a kernel binary policy that has type attributes associated with other attributes.
Quoted 8 lines
Also validate that the types field of a type datum for an attribute is empty for kernel policies and does not contain an attribute for modular policies before this new version.
This was suggested by Vit Mojzis <[email protected]> who also provided an initial patch for checkpolicy.
Signed-off-by James Carter <[email protected]>
This patch has been merged.
Jim
Quoted 231 lines
---
V3: Fix potential NULL dereference inexpand_attributes_in_attributes()and
change the language of the error message.
V2: relocated call ofexpand_attributes_in_attributes()because of recent commitscheckpolicy/policy_define.c | 9 +++- libsepol/include/sepol/policydb/policydb.h | 3 +- libsepol/src/expand.c | 54 ++++++++++++++++++++++ libsepol/src/policydb.c | 25 ++++++++-- libsepol/src/policydb_validate.c | 30 +++++++++++- 5 files changed, 113 insertions(+), 8 deletions(-)checkpolicy/policy_define.c +8
-1@@ -1440,11 +1440,18 @@ int define_typeattribute(void)return -1 ; } t =hashtab_search (policydbp->p_types.table, id); -if (!t || t->flavor ==TYPE_ATTRIB ) { +if (!t) {yyerror2 ("unknown type %s" , id);free (id);return -1 ; + }else if (t->flavor ==TYPE_ATTRIB ) { +if (policydbp->policy_type !=POLICY_KERN && policydbp->policyvers <MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS ) { +yyerror2 ("Type attributes cannot be used in a typeattribute definition in policy version %d" , policydbp->policyvers); +free (id); +return -1 ; + } } +free (id);while ((id =queue_remove (id_queue))) {libsepol/include/sepol/policydb/policydb.h +2
-1@@ -802,9 +802,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define MOD_POLICYDB_VERSION_SELF_TYPETRANS 21#define MOD_POLICYDB_VERSION_COND_XPERMS 22#define MOD_POLICYDB_VERSION_NEVERAUDIT 23 +#define MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS 24#define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE -#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_NEVERAUDIT +#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS#define POLICYDB_CONFIG_MLS 1libsepol/src/expand.c +54
-0@@ -222,6 +222,56 @@ static int attr_convert_callback(hashtab_key_t key, hashtab_datum_t datum,return 0 ; } +static int expand_attributes_in_attributes (sepol_handle_t *handle,policydb_t *p) +{ +ebitmap_t attrs, types; +ebitmap_node_t *ni, *nj; +unsigned int i, j, reps =0 , done =0 ; +type_datum_t *td, *ad; + +ebitmap_init (&attrs); +for (i=0 ; i < p->p_types.nprim; i++) { + td = p->type_val_to_struct[i]; +if (td && td->flavor ==TYPE_ATTRIB ) +ebitmap_set_bit (&attrs, i,1 ); + } + +while (!done && reps < p->p_types.nprim) { + done =1 ; + reps++; +ebitmap_for_each_positive_bit (&attrs, ni, i) { + td = p->type_val_to_struct[i]; +if (ebitmap_match_any (&td->types, &attrs)) { + done =0 ; +ebitmap_init (&types); +ebitmap_for_each_positive_bit (&td->types, nj, j) { +if (ebitmap_get_bit (&attrs, j)) { + ad = p->type_val_to_struct[j]; +ebitmap_union (&types, &ad->types); +ebitmap_set_bit (&td->types, j,0 ); + } + } +ebitmap_union (&td->types, &types); +ebitmap_destroy (&types); +if (ebitmap_get_bit (&td->types, i)) { +ERR (handle,"Found loop in type attributes involving: %s" , p->p_type_val_to_name[i]); +ebitmap_destroy (&attrs); +return -1 ; + } + } + } + } + +ebitmap_destroy (&attrs); + +if (!done) { +ERR (handle,"Failed to expand attributes" ); +return -1 ; + } + +return 0 ; +} +static int perm_copy_callback (hashtab_key_t key,hashtab_datum_t datum,void *data) {@@ -3161,6 +3211,10 @@ int expand_module(sepol_handle_t * handle,if (hashtab_map (decl->p_types.table, type_bounds_copy_callback, &state)) goto cleanup; } +/* Expand any attributes in each attributes' types ebitmap */ +if (expand_attributes_in_attributes (state.handle, state.out)) { + goto cleanup; + }/* copy commons */ if (hashtab_maplibsepol/src/policydb.c +21
-4@@ -362,6 +362,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = OCON_IBENDPORT + 1, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_BASE, + .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS, + .sym_num = SYM_NUM, + .ocon_num = OCON_IBENDPORT + 1, + .target_platform = SEPOL_TARGET_SELINUX, + }, { .type = POLICY_MOD, .version = MOD_POLICYDB_VERSION_BASE,@@ -502,6 +509,13 @@ static const struct policydb_compat_info policydb_compat[] = { .ocon_num = 0, .target_platform = SEPOL_TARGET_SELINUX, }, + { + .type = POLICY_MOD, + .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS, + .sym_num = SYM_NUM, + .ocon_num = 0, + .target_platform = SEPOL_TARGET_SELINUX, + }, };#if 0 @@ -4492,16 +4506,19 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)if (r_policyvers >=POLICYDB_VERSION_AVTAB ) {if (ebitmap_read (&p->type_attr_map[i], fp)) goto bad; -ebitmap_for_each_positive_bit (&p->type_attr_map[i], - tnode, j) { +ebitmap_for_each_positive_bit (&p->type_attr_map[i], tnode, j) {if (i == j)continue ;if (j >= p->p_types.nprim) goto bad; -if (ebitmap_set_bit - (&p->attr_type_map[j], i,1 )) +if (p->type_val_to_struct[i]->flavor ==TYPE_ATTRIB ) { +ERR (fp->handle,"Invalid to have type attributes associated with an attribute for a kernel policy" ); + goto bad; + } + +if (ebitmap_set_bit (&p->attr_type_map[j], i,1 )) goto bad; } }libsepol/src/policydb_validate.c +28
-3@@ -566,6 +566,27 @@ bad:return -1 ; } +static int validate_types_in_attribute (const ebitmap_t *map,const policydb_t *p,const validate_t flavors[SYM_NUM ]) +{ +ebitmap_node_t *node; +uint32_t i; + +if (validate_ebitmap (map, &flavors[SYM_TYPES ])) + goto bad; + +if (p->policy_type !=POLICY_KERN && p->policyvers <MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS ) { +ebitmap_for_each_positive_bit (map, node, i) { +if (validate_simpletype (i+1 , p, flavors)) + goto bad; + } + } + +return 0 ; + +bad: +return -1 ; +} +static int validate_type_datum (sepol_handle_t *handle,const type_datum_t *type,const policydb_t *p,validate_t flavors[]) {if (validate_value (type->s.value, &flavors[SYM_TYPES ]))@@ -582,8 +603,13 @@ static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type, goto bad;break ;case TYPE_ATTRIB : -if (validate_ebitmap (&type->types, &flavors[SYM_TYPES ])) - goto bad; +if (p->policy_type ==POLICY_KERN ) { +if (!ebitmap_is_empty (&type->types)) + goto bad; + }else { +if (validate_types_in_attribute (&type->types, p, flavors)) + goto bad; + }if (type->bounds) goto bad;break ; --2.50.0