Skip to content

Commit c0acacd

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 e5e3f69 commit c0acacd

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

checkpolicy/policy_define.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,80 @@ int expand_attrib(void)
12201220
return rc;
12211221
}
12221222

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