Skip to content

Commit de7ca16

Browse files
committed
checkpolicy: add front-end support for segregate attributes
Support specifying segregate attributes. The following two blocks are equivalent: segregate_attributes attr1, attr2, attr3; segregate_attributes attr1, attr2; segregate_attributes attr1, attr3; segregate_attributes attr2, attr3; Signed-off-by: Christian Göttsche <[email protected]>
1 parent d37be9d commit de7ca16

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

checkpolicy/policy_define.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,72 @@ int expand_attrib(void)
12191219
return rc;
12201220
}
12211221

1222+
int define_segregate_attributes(void)
1223+
{
1224+
char *id = NULL;
1225+
segregate_attributes_rule_t *sattr = NULL;
1226+
int rc = -1;
1227+
1228+
if (pass == 1) {
1229+
while ((id = queue_remove(id_queue)))
1230+
free(id);
1231+
return 0;
1232+
}
1233+
1234+
sattr = malloc(sizeof(segregate_attributes_rule_t));
1235+
if (!sattr) {
1236+
yyerror("Out of memory!");
1237+
goto exit;
1238+
}
1239+
1240+
ebitmap_init(&sattr->attrs);
1241+
1242+
while ((id = queue_remove(id_queue))) {
1243+
const type_datum_t *attr;
1244+
1245+
if (!is_id_in_scope(SYM_TYPES, id)) {
1246+
yyerror2("attribute %s is not within scope", id);
1247+
goto exit;
1248+
}
1249+
1250+
attr = hashtab_search(policydbp->p_types.table, id);
1251+
if (!attr) {
1252+
yyerror2("attribute %s is not declared", id);
1253+
goto exit;
1254+
}
1255+
1256+
if (attr->flavor != TYPE_ATTRIB) {
1257+
yyerror2("%s is a type, not an attribute", id);
1258+
goto exit;
1259+
}
1260+
1261+
if (ebitmap_get_bit(&sattr->attrs, attr->s.value - 1)) {
1262+
yyerror2("attribute %s used multiple times", id);
1263+
goto exit;
1264+
}
1265+
1266+
if (ebitmap_set_bit(&sattr->attrs, attr->s.value - 1, TRUE)) {
1267+
yyerror("Out of memory!");
1268+
goto exit;
1269+
}
1270+
1271+
free(id);
1272+
}
1273+
1274+
sattr->next = policydbp->segregate_attributes;
1275+
policydbp->segregate_attributes = sattr;
1276+
1277+
sattr = NULL;
1278+
rc = 0;
1279+
exit:
1280+
if (sattr) {
1281+
ebitmap_destroy(&sattr->attrs);
1282+
free(sattr);
1283+
}
1284+
free(id);
1285+
return rc;
1286+
}
1287+
12221288
static int add_aliases_to_type(type_datum_t * type)
12231289
{
12241290
char *id;

checkpolicy/policy_define.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ int define_type(int alias);
6868
int define_user(void);
6969
int define_validatetrans(constraint_expr_t *expr);
7070
int expand_attrib(void);
71+
int define_segregate_attributes(void);
7172
int insert_id(const char *id,int push);
7273
int insert_separator(int push);
7374
role_datum_t *define_role_dom(role_datum_t *r);

checkpolicy/policy_parse.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ typedef int (* require_func_t)(int pass);
104104
%token ALIAS
105105
%token ATTRIBUTE
106106
%token EXPANDATTRIBUTE
107+
%token SEGREGATEATTRIBUTES
107108
%token BOOL
108109
%token TUNABLE
109110
%token IF
@@ -320,6 +321,7 @@ rbac_decl : attribute_role_def
320321
;
321322
te_decl : attribute_def
322323
| expandattribute_def
324+
| segregateattributes_def
323325
| type_def
324326
| typealias_def
325327
| typeattribute_def
@@ -337,6 +339,9 @@ attribute_def : ATTRIBUTE identifier ';'
337339
expandattribute_def : EXPANDATTRIBUTE names bool_val ';'
338340
{ if (expand_attrib()) return -1;}
339341
;
342+
segregateattributes_def : SEGREGATEATTRIBUTES identifier ',' id_comma_list ';'
343+
{ if (define_segregate_attributes()) return -1;}
344+
;
340345
type_def : TYPE identifier alias_def opt_attr_list ';'
341346
{if (define_type(1)) return -1;}
342347
| TYPE identifier opt_attr_list ';'

checkpolicy/policy_scan.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ ATTRIBUTE |
123123
attribute { return(ATTRIBUTE); }
124124
EXPANDATTRIBUTE |
125125
expandattribute { return(EXPANDATTRIBUTE); }
126+
SEGREGATE_ATTRIBUTES |
127+
segregate_attributes { return(SEGREGATEATTRIBUTES); }
126128
TYPE_TRANSITION |
127129
type_transition { return(TYPE_TRANSITION); }
128130
TYPE_MEMBER |

0 commit comments

Comments
 (0)