Skip to content

Commit fafe4c2

Browse files
Dan Cashmanjwcart2
authored andcommitted
libsepol: cil: Add ability to redeclare types[attributes]
Modify cil_gen_node() to check to see if the cil_db supports multiple declarations, and if so, to check whether or not the repeated symbol is eligible to share the existing, already-stored datum. The only types considered so far are CIL_TYPE and CIL_TYPEATTRIBUTE, both of which intall empty datums during AST building, so they automatically return true. Test: Build policy with multilpe type and attribute declarations, and without. Policies are binary-identical. Signed-off-by: Dan Cashman <[email protected]> Signed-off-by: James Carter <[email protected]>
1 parent 1346746 commit fafe4c2

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

libsepol/cil/include/cil/cil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern int cil_userprefixes_to_string(cil_db_t *db, char **out, size_t *size);
5050
extern int cil_selinuxusers_to_string(cil_db_t *db, char **out, size_t *size);
5151
extern int cil_filecons_to_string(cil_db_t *db, char **out, size_t *size);
5252
extern void cil_set_disable_dontaudit(cil_db_t *db, int disable_dontaudit);
53+
extern void cil_set_multiple_decls(cil_db_t *db, int multiple_decls);
5354
extern void cil_set_disable_neverallow(cil_db_t *db, int disable_neverallow);
5455
extern void cil_set_preserve_tunables(cil_db_t *db, int preserve_tunables);
5556
extern int cil_set_handle_unknown(cil_db_t *db, int handle_unknown);

libsepol/cil/src/cil.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,11 @@ void cil_set_mls(struct cil_db *db, int mls)
16911691
db->mls = mls;
16921692
}
16931693

1694+
void cil_set_multiple_decls(struct cil_db *db, int multiple_decls)
1695+
{
1696+
db->multiple_decls = multiple_decls;
1697+
}
1698+
16941699
void cil_set_target_platform(struct cil_db *db, int target_platform)
16951700
{
16961701
db->target_platform = target_platform;

libsepol/cil/src/cil_build_ast.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,33 @@ int cil_fill_list(struct cil_tree_node *current, enum cil_flavor flavor, struct
8282
return rc;
8383
}
8484

85+
/*
86+
* Determine whether or not multiple declarations of the same key can share a
87+
* datum, given the new datum and the one already present in a given symtab.
88+
*/
89+
int cil_is_datum_multiple_decl(__attribute__((unused)) struct cil_symtab_datum *cur,
90+
__attribute__((unused)) struct cil_symtab_datum *old,
91+
enum cil_flavor f)
92+
{
93+
int rc = CIL_FALSE;
94+
95+
switch (f) {
96+
case CIL_TYPE:
97+
case CIL_TYPEATTRIBUTE:
98+
/* type and typeattribute statements insert empty datums, ret true */
99+
rc = CIL_TRUE;
100+
break;
101+
default:
102+
break;
103+
}
104+
return rc;
105+
}
106+
85107
int cil_gen_node(__attribute__((unused)) struct cil_db *db, struct cil_tree_node *ast_node, struct cil_symtab_datum *datum, hashtab_key_t key, enum cil_sym_index sflavor, enum cil_flavor nflavor)
86108
{
87109
int rc = SEPOL_ERR;
88110
symtab_t *symtab = NULL;
111+
struct cil_symtab_datum *prev;
89112

90113
rc = __cil_verify_name((const char*)key);
91114
if (rc != SEPOL_OK) {
@@ -103,15 +126,26 @@ int cil_gen_node(__attribute__((unused)) struct cil_db *db, struct cil_tree_node
103126
if (symtab != NULL) {
104127
rc = cil_symtab_insert(symtab, (hashtab_key_t)key, datum, ast_node);
105128
if (rc == SEPOL_EEXIST) {
106-
cil_log(CIL_ERR, "Re-declaration of %s %s\n",
107-
cil_node_to_string(ast_node), key);
108-
if (cil_symtab_get_datum(symtab, key, &datum) == SEPOL_OK) {
109-
if (sflavor == CIL_SYM_BLOCKS) {
110-
struct cil_tree_node *node = datum->nodes->head->data;
111-
cil_tree_log(node, CIL_ERR, "Previous declaration");
129+
if (!db->multiple_decls ||
130+
cil_symtab_get_datum(symtab, (hashtab_key_t)key, &prev) != SEPOL_OK ||
131+
!cil_is_datum_multiple_decl(datum, prev, nflavor)) {
132+
133+
/* multiple_decls not ok, ret error */
134+
cil_log(CIL_ERR, "Re-declaration of %s %s\n",
135+
cil_node_to_string(ast_node), key);
136+
if (cil_symtab_get_datum(symtab, key, &datum) == SEPOL_OK) {
137+
if (sflavor == CIL_SYM_BLOCKS) {
138+
struct cil_tree_node *node = datum->nodes->head->data;
139+
cil_tree_log(node, CIL_ERR, "Previous declaration");
140+
}
112141
}
142+
goto exit;
113143
}
114-
goto exit;
144+
/* multiple_decls is enabled and works for this datum type, add node */
145+
cil_list_append(prev->nodes, CIL_NODE, ast_node);
146+
ast_node->data = prev;
147+
cil_symtab_datum_destroy(datum);
148+
free(datum);
115149
}
116150
}
117151

libsepol/cil/src/cil_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ struct cil_db {
316316
int preserve_tunables;
317317
int handle_unknown;
318318
int mls;
319+
int multiple_decls;
319320
int target_platform;
320321
int policy_version;
321322
};

libsepol/src/libsepol.map.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ LIBSEPOL_1.1 {
4949
cil_set_mls;
5050
cil_set_attrs_expand_generated;
5151
cil_set_attrs_expand_size;
52+
cil_set_multiple_decls;
5253
cil_write_policy_conf;
5354
sepol_ppfile_to_module_package;
5455
sepol_module_package_to_cil;

secilc/secilc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static __attribute__((__noreturn__)) void usage(const char *prog)
6363
printf(" statement if present in the policy\n");
6464
printf(" -D, --disable-dontaudit do not add dontaudit rules to the binary policy\n");
6565
printf(" -P, --preserve-tunables treat tunables as booleans\n");
66+
printf(" -m, --multiple-decls allow some statements to be re-declared\n");
6667
printf(" -N, --disable-neverallow do not check neverallow rules\n");
6768
printf(" -G, --expand-generated Expand and remove auto-generated attributes\n");
6869
printf(" -X, --expand-size <SIZE> Expand type attributes with fewer than <SIZE>\n");
@@ -89,6 +90,7 @@ int main(int argc, char *argv[])
8990
int target = SEPOL_TARGET_SELINUX;
9091
int mls = -1;
9192
int disable_dontaudit = 0;
93+
int multiple_decls = 0;
9294
int disable_neverallow = 0;
9395
int preserve_tunables = 0;
9496
int handle_unknown = -1;
@@ -108,6 +110,7 @@ int main(int argc, char *argv[])
108110
{"policyversion", required_argument, 0, 'c'},
109111
{"handle-unknown", required_argument, 0, 'U'},
110112
{"disable-dontaudit", no_argument, 0, 'D'},
113+
{"multiple-decls", no_argument, 0, 'm'},
111114
{"disable-neverallow", no_argument, 0, 'N'},
112115
{"preserve-tunables", no_argument, 0, 'P'},
113116
{"output", required_argument, 0, 'o'},
@@ -119,7 +122,7 @@ int main(int argc, char *argv[])
119122
int i;
120123

121124
while (1) {
122-
opt_char = getopt_long(argc, argv, "o:f:U:hvt:M:PDNc:GX:", long_opts, &opt_index);
125+
opt_char = getopt_long(argc, argv, "o:f:U:hvt:M:PDmNc:GX:", long_opts, &opt_index);
123126
if (opt_char == -1) {
124127
break;
125128
}
@@ -175,6 +178,9 @@ int main(int argc, char *argv[])
175178
case 'D':
176179
disable_dontaudit = 1;
177180
break;
181+
case 'm':
182+
multiple_decls = 1;
183+
break;
178184
case 'N':
179185
disable_neverallow = 1;
180186
break;
@@ -223,6 +229,7 @@ int main(int argc, char *argv[])
223229

224230
cil_db_init(&db);
225231
cil_set_disable_dontaudit(db, disable_dontaudit);
232+
cil_set_multiple_decls(db, multiple_decls);
226233
cil_set_disable_neverallow(db, disable_neverallow);
227234
cil_set_preserve_tunables(db, preserve_tunables);
228235
if (handle_unknown != -1) {

0 commit comments

Comments
 (0)