Skip to content

Commit b26a942

Browse files
committed
checkpolicy: add front-end support for disjoint attributes
Support specifying disjoint attributes rules. The following two blocks are equivalent and prevent at compile time that any type can be associated with more than one of the listed attributes: disjoint_attributes attr1, attr2, attr3; disjoint_attributes attr1, attr2; disjoint_attributes attr1, attr3; disjoint_attributes attr2, attr3; Accept more than two attributes with a rule to avoid quadratic growth of necessary statements for a group of attributes. Signed-off-by: Christian Göttsche <[email protected]> --- v4: rename to disjoint_attributes
1 parent 054e921 commit b26a942

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

checkpolicy/policy_define.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,72 @@ int expand_attrib(void)
12531253
return rc;
12541254
}
12551255

1256+
int define_disjoint_attributes(void)
1257+
{
1258+
char *id = NULL;
1259+
disjoint_attributes_rule_t *dattr = NULL;
1260+
int rc = -1;
1261+
1262+
if (pass == 1) {
1263+
while ((id = queue_remove(id_queue)))
1264+
free(id);
1265+
return 0;
1266+
}
1267+
1268+
dattr = malloc(sizeof(disjoint_attributes_rule_t));
1269+
if (!dattr) {
1270+
yyerror("Out of memory!");
1271+
goto exit;
1272+
}
1273+
1274+
ebitmap_init(&dattr->attrs);
1275+
1276+
while ((id = queue_remove(id_queue))) {
1277+
const type_datum_t *attr;
1278+
1279+
if (!is_id_in_scope(SYM_TYPES, id)) {
1280+
yyerror2("attribute %s is not within scope", id);
1281+
goto exit;
1282+
}
1283+
1284+
attr = hashtab_search(policydbp->p_types.table, id);
1285+
if (!attr) {
1286+
yyerror2("attribute %s is not declared", id);
1287+
goto exit;
1288+
}
1289+
1290+
if (attr->flavor != TYPE_ATTRIB) {
1291+
yyerror2("%s is a type, not an attribute", id);
1292+
goto exit;
1293+
}
1294+
1295+
if (ebitmap_get_bit(&dattr->attrs, attr->s.value - 1)) {
1296+
yyerror2("attribute %s used multiple times", id);
1297+
goto exit;
1298+
}
1299+
1300+
if (ebitmap_set_bit(&dattr->attrs, attr->s.value - 1, TRUE)) {
1301+
yyerror("Out of memory!");
1302+
goto exit;
1303+
}
1304+
1305+
free(id);
1306+
}
1307+
1308+
dattr->next = policydbp->disjoint_attributes;
1309+
policydbp->disjoint_attributes = dattr;
1310+
1311+
dattr = NULL;
1312+
rc = 0;
1313+
exit:
1314+
if (dattr) {
1315+
ebitmap_destroy(&dattr->attrs);
1316+
free(dattr);
1317+
}
1318+
free(id);
1319+
return rc;
1320+
}
1321+
12561322
static int add_aliases_to_type(type_datum_t * type)
12571323
{
12581324
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_disjoint_attributes(void);
7172
int insert_id(const char *id,int push);
7273
int insert_separator(int push);
7374
uintptr_t define_cexpr(uint32_t expr_type, uintptr_t arg1, uintptr_t arg2);

checkpolicy/policy_parse.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef int (* require_func_t)(int pass);
102102
%token ALIAS
103103
%token ATTRIBUTE
104104
%token EXPANDATTRIBUTE
105+
%token DISJOINTATTRIBUTES
105106
%token BOOL
106107
%token TUNABLE
107108
%token IF
@@ -318,6 +319,7 @@ rbac_decl : attribute_role_def
318319
;
319320
te_decl : attribute_def
320321
| expandattribute_def
322+
| disjointattributes_def
321323
| type_def
322324
| typealias_def
323325
| typeattribute_def
@@ -335,6 +337,9 @@ attribute_def : ATTRIBUTE identifier ';'
335337
expandattribute_def : EXPANDATTRIBUTE names bool_val ';'
336338
{ if (expand_attrib()) YYABORT;}
337339
;
340+
disjointattributes_def : DISJOINTATTRIBUTES identifier ',' id_comma_list ';'
341+
{ if (define_disjoint_attributes()) return -1;}
342+
;
338343
type_def : TYPE identifier alias_def opt_attr_list ';'
339344
{if (define_type(1)) YYABORT;}
340345
| TYPE identifier opt_attr_list ';'

checkpolicy/policy_scan.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ ATTRIBUTE |
137137
attribute { return(ATTRIBUTE); }
138138
EXPANDATTRIBUTE |
139139
expandattribute { return(EXPANDATTRIBUTE); }
140+
DISJOINT_ATTRIBUTES |
141+
disjoint_attributes { return(DISJOINTATTRIBUTES); }
140142
TYPE_TRANSITION |
141143
type_transition { return(TYPE_TRANSITION); }
142144
TYPE_MEMBER |

checkpolicy/tests/policy_allonce.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ attribute ATTR1;
1818
attribute ATTR2;
1919
expandattribute ATTR1 true;
2020
expandattribute ATTR2 false;
21+
disjoint_attributes ATTR1, ATTR2;
2122
type TYPE1;
2223
type TYPE2, ATTR1;
2324
type TYPE3 alias { TYPEALIAS3A TYPEALIAS3B };

0 commit comments

Comments
 (0)