1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3*2d543d20SAndroid Build Coastguard Worker *
4*2d543d20SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
5*2d543d20SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
6*2d543d20SAndroid Build Coastguard Worker *
7*2d543d20SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright notice,
8*2d543d20SAndroid Build Coastguard Worker * this list of conditions and the following disclaimer.
9*2d543d20SAndroid Build Coastguard Worker *
10*2d543d20SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright notice,
11*2d543d20SAndroid Build Coastguard Worker * this list of conditions and the following disclaimer in the documentation
12*2d543d20SAndroid Build Coastguard Worker * and/or other materials provided with the distribution.
13*2d543d20SAndroid Build Coastguard Worker *
14*2d543d20SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15*2d543d20SAndroid Build Coastguard Worker * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16*2d543d20SAndroid Build Coastguard Worker * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17*2d543d20SAndroid Build Coastguard Worker * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18*2d543d20SAndroid Build Coastguard Worker * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19*2d543d20SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*2d543d20SAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21*2d543d20SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22*2d543d20SAndroid Build Coastguard Worker * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23*2d543d20SAndroid Build Coastguard Worker * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*2d543d20SAndroid Build Coastguard Worker *
25*2d543d20SAndroid Build Coastguard Worker * The views and conclusions contained in the software and documentation are those
26*2d543d20SAndroid Build Coastguard Worker * of the authors and should not be interpreted as representing official policies,
27*2d543d20SAndroid Build Coastguard Worker * either expressed or implied, of Tresys Technology, LLC.
28*2d543d20SAndroid Build Coastguard Worker */
29*2d543d20SAndroid Build Coastguard Worker
30*2d543d20SAndroid Build Coastguard Worker #include <stddef.h>
31*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
32*2d543d20SAndroid Build Coastguard Worker #include <stdio.h>
33*2d543d20SAndroid Build Coastguard Worker #include <string.h>
34*2d543d20SAndroid Build Coastguard Worker #include <stdint.h>
35*2d543d20SAndroid Build Coastguard Worker #include <unistd.h>
36*2d543d20SAndroid Build Coastguard Worker
37*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/conditional.h>
38*2d543d20SAndroid Build Coastguard Worker #include <sepol/errcodes.h>
39*2d543d20SAndroid Build Coastguard Worker
40*2d543d20SAndroid Build Coastguard Worker #include "cil_internal.h"
41*2d543d20SAndroid Build Coastguard Worker #include "cil_flavor.h"
42*2d543d20SAndroid Build Coastguard Worker #include "cil_log.h"
43*2d543d20SAndroid Build Coastguard Worker #include "cil_mem.h"
44*2d543d20SAndroid Build Coastguard Worker #include "cil_tree.h"
45*2d543d20SAndroid Build Coastguard Worker #include "cil_list.h"
46*2d543d20SAndroid Build Coastguard Worker #include "cil_post.h"
47*2d543d20SAndroid Build Coastguard Worker #include "cil_policy.h"
48*2d543d20SAndroid Build Coastguard Worker #include "cil_verify.h"
49*2d543d20SAndroid Build Coastguard Worker #include "cil_symtab.h"
50*2d543d20SAndroid Build Coastguard Worker #include "cil_deny.h"
51*2d543d20SAndroid Build Coastguard Worker
52*2d543d20SAndroid Build Coastguard Worker #define GEN_REQUIRE_ATTR "cil_gen_require" /* Also in libsepol/src/module_to_cil.c */
53*2d543d20SAndroid Build Coastguard Worker #define TYPEATTR_INFIX "_typeattr_" /* Also in libsepol/src/module_to_cil.c */
54*2d543d20SAndroid Build Coastguard Worker
55*2d543d20SAndroid Build Coastguard Worker #define spaceship_cmp(a, b) (((a) > (b)) - ((a) < (b)))
56*2d543d20SAndroid Build Coastguard Worker
57*2d543d20SAndroid Build Coastguard Worker struct fc_data {
58*2d543d20SAndroid Build Coastguard Worker unsigned int meta;
59*2d543d20SAndroid Build Coastguard Worker size_t stem_len;
60*2d543d20SAndroid Build Coastguard Worker size_t str_len;
61*2d543d20SAndroid Build Coastguard Worker };
62*2d543d20SAndroid Build Coastguard Worker
63*2d543d20SAndroid Build Coastguard Worker static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db);
64*2d543d20SAndroid Build Coastguard Worker static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db);
65*2d543d20SAndroid Build Coastguard Worker
cats_compare(struct cil_cats * a,struct cil_cats * b)66*2d543d20SAndroid Build Coastguard Worker static int cats_compare(struct cil_cats *a, struct cil_cats *b)
67*2d543d20SAndroid Build Coastguard Worker {
68*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *i, *j;
69*2d543d20SAndroid Build Coastguard Worker int rc;
70*2d543d20SAndroid Build Coastguard Worker
71*2d543d20SAndroid Build Coastguard Worker if (a == b) return 0;
72*2d543d20SAndroid Build Coastguard Worker if (!a) return -1;
73*2d543d20SAndroid Build Coastguard Worker if (!b) return 1;
74*2d543d20SAndroid Build Coastguard Worker
75*2d543d20SAndroid Build Coastguard Worker /* Expects cat expression to have been evaluated */
76*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(i, a->datum_expr) {
77*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(j, b->datum_expr) {
78*2d543d20SAndroid Build Coastguard Worker rc = strcmp(DATUM(i->data)->fqn, DATUM(j->data)->fqn);
79*2d543d20SAndroid Build Coastguard Worker if (!rc) return rc;
80*2d543d20SAndroid Build Coastguard Worker }
81*2d543d20SAndroid Build Coastguard Worker }
82*2d543d20SAndroid Build Coastguard Worker return 0;
83*2d543d20SAndroid Build Coastguard Worker }
84*2d543d20SAndroid Build Coastguard Worker
level_compare(struct cil_level * a,struct cil_level * b)85*2d543d20SAndroid Build Coastguard Worker static int level_compare(struct cil_level *a, struct cil_level *b)
86*2d543d20SAndroid Build Coastguard Worker {
87*2d543d20SAndroid Build Coastguard Worker int rc;
88*2d543d20SAndroid Build Coastguard Worker
89*2d543d20SAndroid Build Coastguard Worker if (a == b) return 0;
90*2d543d20SAndroid Build Coastguard Worker if (!a) return -1;
91*2d543d20SAndroid Build Coastguard Worker if (!b) return 1;
92*2d543d20SAndroid Build Coastguard Worker
93*2d543d20SAndroid Build Coastguard Worker if (a->sens != b->sens) {
94*2d543d20SAndroid Build Coastguard Worker rc = strcmp(DATUM(a->sens)->fqn, DATUM(b->sens)->fqn);
95*2d543d20SAndroid Build Coastguard Worker if (rc != 0) return rc;
96*2d543d20SAndroid Build Coastguard Worker }
97*2d543d20SAndroid Build Coastguard Worker if (a->cats != b->cats) {
98*2d543d20SAndroid Build Coastguard Worker return cats_compare(a->cats, b->cats);
99*2d543d20SAndroid Build Coastguard Worker }
100*2d543d20SAndroid Build Coastguard Worker return 0;
101*2d543d20SAndroid Build Coastguard Worker }
102*2d543d20SAndroid Build Coastguard Worker
range_compare(struct cil_levelrange * a,struct cil_levelrange * b)103*2d543d20SAndroid Build Coastguard Worker static int range_compare(struct cil_levelrange *a, struct cil_levelrange *b)
104*2d543d20SAndroid Build Coastguard Worker {
105*2d543d20SAndroid Build Coastguard Worker int rc;
106*2d543d20SAndroid Build Coastguard Worker
107*2d543d20SAndroid Build Coastguard Worker if (a == b) return 0;
108*2d543d20SAndroid Build Coastguard Worker if (!a) return -1;
109*2d543d20SAndroid Build Coastguard Worker if (!b) return 1;
110*2d543d20SAndroid Build Coastguard Worker
111*2d543d20SAndroid Build Coastguard Worker if (a->low != b->low) {
112*2d543d20SAndroid Build Coastguard Worker rc = level_compare(a->low, b->low);
113*2d543d20SAndroid Build Coastguard Worker if (rc != 0) return rc;
114*2d543d20SAndroid Build Coastguard Worker }
115*2d543d20SAndroid Build Coastguard Worker if (a->high != b->high) {
116*2d543d20SAndroid Build Coastguard Worker return level_compare(a->high, b->high);
117*2d543d20SAndroid Build Coastguard Worker }
118*2d543d20SAndroid Build Coastguard Worker return 0;
119*2d543d20SAndroid Build Coastguard Worker }
120*2d543d20SAndroid Build Coastguard Worker
context_compare(struct cil_context * a,struct cil_context * b)121*2d543d20SAndroid Build Coastguard Worker static int context_compare(struct cil_context *a, struct cil_context *b)
122*2d543d20SAndroid Build Coastguard Worker {
123*2d543d20SAndroid Build Coastguard Worker int rc;
124*2d543d20SAndroid Build Coastguard Worker
125*2d543d20SAndroid Build Coastguard Worker if (a->user != b->user) {
126*2d543d20SAndroid Build Coastguard Worker rc = strcmp(DATUM(a->user)->fqn, DATUM(b->user)->fqn);
127*2d543d20SAndroid Build Coastguard Worker if (rc != 0) return rc;
128*2d543d20SAndroid Build Coastguard Worker }
129*2d543d20SAndroid Build Coastguard Worker if (a->role != b->role) {
130*2d543d20SAndroid Build Coastguard Worker rc = strcmp(DATUM(a->role)->fqn, DATUM(b->role)->fqn);
131*2d543d20SAndroid Build Coastguard Worker if (rc != 0) return rc;
132*2d543d20SAndroid Build Coastguard Worker }
133*2d543d20SAndroid Build Coastguard Worker if (a->type != b->type) {
134*2d543d20SAndroid Build Coastguard Worker rc = strcmp(DATUM(a->type)->fqn, DATUM(b->type)->fqn);
135*2d543d20SAndroid Build Coastguard Worker if (rc != 0) return rc;
136*2d543d20SAndroid Build Coastguard Worker }
137*2d543d20SAndroid Build Coastguard Worker if (a->range != b->range) {
138*2d543d20SAndroid Build Coastguard Worker return range_compare(a->range, b->range);
139*2d543d20SAndroid Build Coastguard Worker }
140*2d543d20SAndroid Build Coastguard Worker return 0;
141*2d543d20SAndroid Build Coastguard Worker }
142*2d543d20SAndroid Build Coastguard Worker
cil_verify_is_list(struct cil_list * list,enum cil_flavor flavor)143*2d543d20SAndroid Build Coastguard Worker static int cil_verify_is_list(struct cil_list *list, enum cil_flavor flavor)
144*2d543d20SAndroid Build Coastguard Worker {
145*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
146*2d543d20SAndroid Build Coastguard Worker
147*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(curr, list) {
148*2d543d20SAndroid Build Coastguard Worker switch (curr->flavor) {
149*2d543d20SAndroid Build Coastguard Worker case CIL_LIST:
150*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
151*2d543d20SAndroid Build Coastguard Worker break;
152*2d543d20SAndroid Build Coastguard Worker case CIL_OP:
153*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
154*2d543d20SAndroid Build Coastguard Worker break;
155*2d543d20SAndroid Build Coastguard Worker default:
156*2d543d20SAndroid Build Coastguard Worker if (flavor == CIL_CAT) {
157*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *d = curr->data;
158*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *n = d->nodes->head->data;
159*2d543d20SAndroid Build Coastguard Worker if (n->flavor == CIL_CATSET) {
160*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
161*2d543d20SAndroid Build Coastguard Worker }
162*2d543d20SAndroid Build Coastguard Worker }
163*2d543d20SAndroid Build Coastguard Worker break;
164*2d543d20SAndroid Build Coastguard Worker }
165*2d543d20SAndroid Build Coastguard Worker }
166*2d543d20SAndroid Build Coastguard Worker return CIL_TRUE;
167*2d543d20SAndroid Build Coastguard Worker }
168*2d543d20SAndroid Build Coastguard Worker
cil_post_fc_fill_data(struct fc_data * fc,const char * path)169*2d543d20SAndroid Build Coastguard Worker static void cil_post_fc_fill_data(struct fc_data *fc, const char *path)
170*2d543d20SAndroid Build Coastguard Worker {
171*2d543d20SAndroid Build Coastguard Worker size_t c = 0;
172*2d543d20SAndroid Build Coastguard Worker fc->meta = 0;
173*2d543d20SAndroid Build Coastguard Worker fc->stem_len = 0;
174*2d543d20SAndroid Build Coastguard Worker fc->str_len = 0;
175*2d543d20SAndroid Build Coastguard Worker
176*2d543d20SAndroid Build Coastguard Worker while (path[c] != '\0') {
177*2d543d20SAndroid Build Coastguard Worker switch (path[c]) {
178*2d543d20SAndroid Build Coastguard Worker case '.':
179*2d543d20SAndroid Build Coastguard Worker case '^':
180*2d543d20SAndroid Build Coastguard Worker case '$':
181*2d543d20SAndroid Build Coastguard Worker case '?':
182*2d543d20SAndroid Build Coastguard Worker case '*':
183*2d543d20SAndroid Build Coastguard Worker case '+':
184*2d543d20SAndroid Build Coastguard Worker case '|':
185*2d543d20SAndroid Build Coastguard Worker case '[':
186*2d543d20SAndroid Build Coastguard Worker case '(':
187*2d543d20SAndroid Build Coastguard Worker case '{':
188*2d543d20SAndroid Build Coastguard Worker fc->meta = 1;
189*2d543d20SAndroid Build Coastguard Worker break;
190*2d543d20SAndroid Build Coastguard Worker case '\\':
191*2d543d20SAndroid Build Coastguard Worker c++;
192*2d543d20SAndroid Build Coastguard Worker if (path[c] == '\0') {
193*2d543d20SAndroid Build Coastguard Worker if (!fc->meta) {
194*2d543d20SAndroid Build Coastguard Worker fc->stem_len++;
195*2d543d20SAndroid Build Coastguard Worker }
196*2d543d20SAndroid Build Coastguard Worker fc->str_len++;
197*2d543d20SAndroid Build Coastguard Worker return;
198*2d543d20SAndroid Build Coastguard Worker }
199*2d543d20SAndroid Build Coastguard Worker /* FALLTHRU */
200*2d543d20SAndroid Build Coastguard Worker default:
201*2d543d20SAndroid Build Coastguard Worker if (!fc->meta) {
202*2d543d20SAndroid Build Coastguard Worker fc->stem_len++;
203*2d543d20SAndroid Build Coastguard Worker }
204*2d543d20SAndroid Build Coastguard Worker break;
205*2d543d20SAndroid Build Coastguard Worker }
206*2d543d20SAndroid Build Coastguard Worker fc->str_len++;
207*2d543d20SAndroid Build Coastguard Worker c++;
208*2d543d20SAndroid Build Coastguard Worker }
209*2d543d20SAndroid Build Coastguard Worker }
210*2d543d20SAndroid Build Coastguard Worker
cil_post_filecon_compare(const void * a,const void * b)211*2d543d20SAndroid Build Coastguard Worker int cil_post_filecon_compare(const void *a, const void *b)
212*2d543d20SAndroid Build Coastguard Worker {
213*2d543d20SAndroid Build Coastguard Worker int rc = 0;
214*2d543d20SAndroid Build Coastguard Worker struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
215*2d543d20SAndroid Build Coastguard Worker struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
216*2d543d20SAndroid Build Coastguard Worker struct fc_data *a_data = cil_malloc(sizeof(*a_data));
217*2d543d20SAndroid Build Coastguard Worker struct fc_data *b_data = cil_malloc(sizeof(*b_data));
218*2d543d20SAndroid Build Coastguard Worker char *a_path_str, *a_path, *b_path_str, *b_path;
219*2d543d20SAndroid Build Coastguard Worker
220*2d543d20SAndroid Build Coastguard Worker a_path_str = a_filecon->path ? DATUM(a_filecon->path)->fqn : a_filecon->path_str;
221*2d543d20SAndroid Build Coastguard Worker b_path_str = b_filecon->path ? DATUM(b_filecon->path)->fqn : b_filecon->path_str;
222*2d543d20SAndroid Build Coastguard Worker a_path = cil_malloc(strlen(a_path_str) + 1);
223*2d543d20SAndroid Build Coastguard Worker b_path = cil_malloc(strlen(b_path_str) + 1);
224*2d543d20SAndroid Build Coastguard Worker a_path[0] = '\0';
225*2d543d20SAndroid Build Coastguard Worker b_path[0] = '\0';
226*2d543d20SAndroid Build Coastguard Worker strcat(a_path, a_path_str);
227*2d543d20SAndroid Build Coastguard Worker strcat(b_path, b_path_str);
228*2d543d20SAndroid Build Coastguard Worker cil_post_fc_fill_data(a_data, a_path);
229*2d543d20SAndroid Build Coastguard Worker cil_post_fc_fill_data(b_data, b_path);
230*2d543d20SAndroid Build Coastguard Worker if (a_data->meta && !b_data->meta) {
231*2d543d20SAndroid Build Coastguard Worker rc = -1;
232*2d543d20SAndroid Build Coastguard Worker } else if (b_data->meta && !a_data->meta) {
233*2d543d20SAndroid Build Coastguard Worker rc = 1;
234*2d543d20SAndroid Build Coastguard Worker } else if (a_data->stem_len < b_data->stem_len) {
235*2d543d20SAndroid Build Coastguard Worker rc = -1;
236*2d543d20SAndroid Build Coastguard Worker } else if (b_data->stem_len < a_data->stem_len) {
237*2d543d20SAndroid Build Coastguard Worker rc = 1;
238*2d543d20SAndroid Build Coastguard Worker } else if (a_data->str_len < b_data->str_len) {
239*2d543d20SAndroid Build Coastguard Worker rc = -1;
240*2d543d20SAndroid Build Coastguard Worker } else if (b_data->str_len < a_data->str_len) {
241*2d543d20SAndroid Build Coastguard Worker rc = 1;
242*2d543d20SAndroid Build Coastguard Worker } else if (a_filecon->type < b_filecon->type) {
243*2d543d20SAndroid Build Coastguard Worker rc = -1;
244*2d543d20SAndroid Build Coastguard Worker } else if (b_filecon->type < a_filecon->type) {
245*2d543d20SAndroid Build Coastguard Worker rc = 1;
246*2d543d20SAndroid Build Coastguard Worker } else {
247*2d543d20SAndroid Build Coastguard Worker rc = strcmp(a_path_str, b_path_str);
248*2d543d20SAndroid Build Coastguard Worker }
249*2d543d20SAndroid Build Coastguard Worker
250*2d543d20SAndroid Build Coastguard Worker free(a_path);
251*2d543d20SAndroid Build Coastguard Worker free(b_path);
252*2d543d20SAndroid Build Coastguard Worker free(a_data);
253*2d543d20SAndroid Build Coastguard Worker free(b_data);
254*2d543d20SAndroid Build Coastguard Worker
255*2d543d20SAndroid Build Coastguard Worker return rc;
256*2d543d20SAndroid Build Coastguard Worker }
257*2d543d20SAndroid Build Coastguard Worker
cil_post_ibpkeycon_compare(const void * a,const void * b)258*2d543d20SAndroid Build Coastguard Worker int cil_post_ibpkeycon_compare(const void *a, const void *b)
259*2d543d20SAndroid Build Coastguard Worker {
260*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
261*2d543d20SAndroid Build Coastguard Worker struct cil_ibpkeycon *aibpkeycon = *(struct cil_ibpkeycon **)a;
262*2d543d20SAndroid Build Coastguard Worker struct cil_ibpkeycon *bibpkeycon = *(struct cil_ibpkeycon **)b;
263*2d543d20SAndroid Build Coastguard Worker
264*2d543d20SAndroid Build Coastguard Worker rc = strcmp(aibpkeycon->subnet_prefix_str, bibpkeycon->subnet_prefix_str);
265*2d543d20SAndroid Build Coastguard Worker if (rc)
266*2d543d20SAndroid Build Coastguard Worker return rc;
267*2d543d20SAndroid Build Coastguard Worker
268*2d543d20SAndroid Build Coastguard Worker rc = spaceship_cmp(aibpkeycon->pkey_high - aibpkeycon->pkey_low,
269*2d543d20SAndroid Build Coastguard Worker bibpkeycon->pkey_high - bibpkeycon->pkey_low);
270*2d543d20SAndroid Build Coastguard Worker if (rc == 0) {
271*2d543d20SAndroid Build Coastguard Worker if (aibpkeycon->pkey_low < bibpkeycon->pkey_low)
272*2d543d20SAndroid Build Coastguard Worker rc = -1;
273*2d543d20SAndroid Build Coastguard Worker else if (bibpkeycon->pkey_low < aibpkeycon->pkey_low)
274*2d543d20SAndroid Build Coastguard Worker rc = 1;
275*2d543d20SAndroid Build Coastguard Worker }
276*2d543d20SAndroid Build Coastguard Worker
277*2d543d20SAndroid Build Coastguard Worker return rc;
278*2d543d20SAndroid Build Coastguard Worker }
279*2d543d20SAndroid Build Coastguard Worker
cil_post_portcon_compare(const void * a,const void * b)280*2d543d20SAndroid Build Coastguard Worker int cil_post_portcon_compare(const void *a, const void *b)
281*2d543d20SAndroid Build Coastguard Worker {
282*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
283*2d543d20SAndroid Build Coastguard Worker struct cil_portcon *aportcon = *(struct cil_portcon**)a;
284*2d543d20SAndroid Build Coastguard Worker struct cil_portcon *bportcon = *(struct cil_portcon**)b;
285*2d543d20SAndroid Build Coastguard Worker
286*2d543d20SAndroid Build Coastguard Worker rc = spaceship_cmp(aportcon->port_high - aportcon->port_low,
287*2d543d20SAndroid Build Coastguard Worker bportcon->port_high - bportcon->port_low);
288*2d543d20SAndroid Build Coastguard Worker if (rc == 0) {
289*2d543d20SAndroid Build Coastguard Worker if (aportcon->port_low < bportcon->port_low) {
290*2d543d20SAndroid Build Coastguard Worker rc = -1;
291*2d543d20SAndroid Build Coastguard Worker } else if (bportcon->port_low < aportcon->port_low) {
292*2d543d20SAndroid Build Coastguard Worker rc = 1;
293*2d543d20SAndroid Build Coastguard Worker } else if (aportcon->proto < bportcon->proto) {
294*2d543d20SAndroid Build Coastguard Worker rc = -1;
295*2d543d20SAndroid Build Coastguard Worker } else if (aportcon->proto > bportcon->proto) {
296*2d543d20SAndroid Build Coastguard Worker rc = 1;
297*2d543d20SAndroid Build Coastguard Worker }
298*2d543d20SAndroid Build Coastguard Worker }
299*2d543d20SAndroid Build Coastguard Worker
300*2d543d20SAndroid Build Coastguard Worker return rc;
301*2d543d20SAndroid Build Coastguard Worker }
302*2d543d20SAndroid Build Coastguard Worker
cil_post_genfscon_compare(const void * a,const void * b)303*2d543d20SAndroid Build Coastguard Worker int cil_post_genfscon_compare(const void *a, const void *b)
304*2d543d20SAndroid Build Coastguard Worker {
305*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
306*2d543d20SAndroid Build Coastguard Worker struct cil_genfscon *agenfscon = *(struct cil_genfscon**)a;
307*2d543d20SAndroid Build Coastguard Worker struct cil_genfscon *bgenfscon = *(struct cil_genfscon**)b;
308*2d543d20SAndroid Build Coastguard Worker
309*2d543d20SAndroid Build Coastguard Worker rc = strcmp(agenfscon->fs_str, bgenfscon->fs_str);
310*2d543d20SAndroid Build Coastguard Worker if (rc == 0) {
311*2d543d20SAndroid Build Coastguard Worker rc = strcmp(agenfscon->path_str, bgenfscon->path_str);
312*2d543d20SAndroid Build Coastguard Worker }
313*2d543d20SAndroid Build Coastguard Worker
314*2d543d20SAndroid Build Coastguard Worker return rc;
315*2d543d20SAndroid Build Coastguard Worker }
316*2d543d20SAndroid Build Coastguard Worker
cil_post_netifcon_compare(const void * a,const void * b)317*2d543d20SAndroid Build Coastguard Worker int cil_post_netifcon_compare(const void *a, const void *b)
318*2d543d20SAndroid Build Coastguard Worker {
319*2d543d20SAndroid Build Coastguard Worker struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
320*2d543d20SAndroid Build Coastguard Worker struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
321*2d543d20SAndroid Build Coastguard Worker
322*2d543d20SAndroid Build Coastguard Worker return strcmp(anetifcon->interface_str, bnetifcon->interface_str);
323*2d543d20SAndroid Build Coastguard Worker }
324*2d543d20SAndroid Build Coastguard Worker
cil_post_ibendportcon_compare(const void * a,const void * b)325*2d543d20SAndroid Build Coastguard Worker int cil_post_ibendportcon_compare(const void *a, const void *b)
326*2d543d20SAndroid Build Coastguard Worker {
327*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
328*2d543d20SAndroid Build Coastguard Worker
329*2d543d20SAndroid Build Coastguard Worker struct cil_ibendportcon *aibendportcon = *(struct cil_ibendportcon **)a;
330*2d543d20SAndroid Build Coastguard Worker struct cil_ibendportcon *bibendportcon = *(struct cil_ibendportcon **)b;
331*2d543d20SAndroid Build Coastguard Worker
332*2d543d20SAndroid Build Coastguard Worker rc = strcmp(aibendportcon->dev_name_str, bibendportcon->dev_name_str);
333*2d543d20SAndroid Build Coastguard Worker if (rc)
334*2d543d20SAndroid Build Coastguard Worker return rc;
335*2d543d20SAndroid Build Coastguard Worker
336*2d543d20SAndroid Build Coastguard Worker if (aibendportcon->port < bibendportcon->port)
337*2d543d20SAndroid Build Coastguard Worker return -1;
338*2d543d20SAndroid Build Coastguard Worker else if (bibendportcon->port < aibendportcon->port)
339*2d543d20SAndroid Build Coastguard Worker return 1;
340*2d543d20SAndroid Build Coastguard Worker
341*2d543d20SAndroid Build Coastguard Worker return rc;
342*2d543d20SAndroid Build Coastguard Worker }
343*2d543d20SAndroid Build Coastguard Worker
cil_post_nodecon_compare(const void * a,const void * b)344*2d543d20SAndroid Build Coastguard Worker int cil_post_nodecon_compare(const void *a, const void *b)
345*2d543d20SAndroid Build Coastguard Worker {
346*2d543d20SAndroid Build Coastguard Worker struct cil_nodecon *anodecon;
347*2d543d20SAndroid Build Coastguard Worker struct cil_nodecon *bnodecon;
348*2d543d20SAndroid Build Coastguard Worker anodecon = *(struct cil_nodecon**)a;
349*2d543d20SAndroid Build Coastguard Worker bnodecon = *(struct cil_nodecon**)b;
350*2d543d20SAndroid Build Coastguard Worker
351*2d543d20SAndroid Build Coastguard Worker /* sort ipv4 before ipv6 */
352*2d543d20SAndroid Build Coastguard Worker if (anodecon->addr->family != bnodecon->addr->family) {
353*2d543d20SAndroid Build Coastguard Worker if (anodecon->addr->family == AF_INET) {
354*2d543d20SAndroid Build Coastguard Worker return -1;
355*2d543d20SAndroid Build Coastguard Worker } else {
356*2d543d20SAndroid Build Coastguard Worker return 1;
357*2d543d20SAndroid Build Coastguard Worker }
358*2d543d20SAndroid Build Coastguard Worker }
359*2d543d20SAndroid Build Coastguard Worker
360*2d543d20SAndroid Build Coastguard Worker /* most specific netmask goes first, then order by ip addr */
361*2d543d20SAndroid Build Coastguard Worker if (anodecon->addr->family == AF_INET) {
362*2d543d20SAndroid Build Coastguard Worker int rc = memcmp(&anodecon->mask->ip.v4, &bnodecon->mask->ip.v4, sizeof(anodecon->mask->ip.v4));
363*2d543d20SAndroid Build Coastguard Worker if (rc != 0) {
364*2d543d20SAndroid Build Coastguard Worker return -1 * rc;
365*2d543d20SAndroid Build Coastguard Worker }
366*2d543d20SAndroid Build Coastguard Worker return memcmp(&anodecon->addr->ip.v4, &bnodecon->addr->ip.v4, sizeof(anodecon->addr->ip.v4));
367*2d543d20SAndroid Build Coastguard Worker } else {
368*2d543d20SAndroid Build Coastguard Worker int rc = memcmp(&anodecon->mask->ip.v6, &bnodecon->mask->ip.v6, sizeof(anodecon->mask->ip.v6));
369*2d543d20SAndroid Build Coastguard Worker if (rc != 0) {
370*2d543d20SAndroid Build Coastguard Worker return -1 * rc;
371*2d543d20SAndroid Build Coastguard Worker }
372*2d543d20SAndroid Build Coastguard Worker return memcmp(&anodecon->addr->ip.v6, &bnodecon->addr->ip.v6, sizeof(anodecon->addr->ip.v6));
373*2d543d20SAndroid Build Coastguard Worker }
374*2d543d20SAndroid Build Coastguard Worker }
375*2d543d20SAndroid Build Coastguard Worker
cil_post_pirqcon_compare(const void * a,const void * b)376*2d543d20SAndroid Build Coastguard Worker static int cil_post_pirqcon_compare(const void *a, const void *b)
377*2d543d20SAndroid Build Coastguard Worker {
378*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
379*2d543d20SAndroid Build Coastguard Worker struct cil_pirqcon *apirqcon = *(struct cil_pirqcon**)a;
380*2d543d20SAndroid Build Coastguard Worker struct cil_pirqcon *bpirqcon = *(struct cil_pirqcon**)b;
381*2d543d20SAndroid Build Coastguard Worker
382*2d543d20SAndroid Build Coastguard Worker if (apirqcon->pirq < bpirqcon->pirq) {
383*2d543d20SAndroid Build Coastguard Worker rc = -1;
384*2d543d20SAndroid Build Coastguard Worker } else if (bpirqcon->pirq < apirqcon->pirq) {
385*2d543d20SAndroid Build Coastguard Worker rc = 1;
386*2d543d20SAndroid Build Coastguard Worker } else {
387*2d543d20SAndroid Build Coastguard Worker rc = 0;
388*2d543d20SAndroid Build Coastguard Worker }
389*2d543d20SAndroid Build Coastguard Worker
390*2d543d20SAndroid Build Coastguard Worker return rc;
391*2d543d20SAndroid Build Coastguard Worker }
392*2d543d20SAndroid Build Coastguard Worker
cil_post_iomemcon_compare(const void * a,const void * b)393*2d543d20SAndroid Build Coastguard Worker static int cil_post_iomemcon_compare(const void *a, const void *b)
394*2d543d20SAndroid Build Coastguard Worker {
395*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
396*2d543d20SAndroid Build Coastguard Worker struct cil_iomemcon *aiomemcon = *(struct cil_iomemcon**)a;
397*2d543d20SAndroid Build Coastguard Worker struct cil_iomemcon *biomemcon = *(struct cil_iomemcon**)b;
398*2d543d20SAndroid Build Coastguard Worker
399*2d543d20SAndroid Build Coastguard Worker rc = spaceship_cmp(aiomemcon->iomem_high - aiomemcon->iomem_low,
400*2d543d20SAndroid Build Coastguard Worker biomemcon->iomem_high - biomemcon->iomem_low);
401*2d543d20SAndroid Build Coastguard Worker if (rc == 0) {
402*2d543d20SAndroid Build Coastguard Worker if (aiomemcon->iomem_low < biomemcon->iomem_low) {
403*2d543d20SAndroid Build Coastguard Worker rc = -1;
404*2d543d20SAndroid Build Coastguard Worker } else if (biomemcon->iomem_low < aiomemcon->iomem_low) {
405*2d543d20SAndroid Build Coastguard Worker rc = 1;
406*2d543d20SAndroid Build Coastguard Worker }
407*2d543d20SAndroid Build Coastguard Worker }
408*2d543d20SAndroid Build Coastguard Worker
409*2d543d20SAndroid Build Coastguard Worker return rc;
410*2d543d20SAndroid Build Coastguard Worker }
411*2d543d20SAndroid Build Coastguard Worker
cil_post_ioportcon_compare(const void * a,const void * b)412*2d543d20SAndroid Build Coastguard Worker static int cil_post_ioportcon_compare(const void *a, const void *b)
413*2d543d20SAndroid Build Coastguard Worker {
414*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
415*2d543d20SAndroid Build Coastguard Worker struct cil_ioportcon *aioportcon = *(struct cil_ioportcon**)a;
416*2d543d20SAndroid Build Coastguard Worker struct cil_ioportcon *bioportcon = *(struct cil_ioportcon**)b;
417*2d543d20SAndroid Build Coastguard Worker
418*2d543d20SAndroid Build Coastguard Worker rc = spaceship_cmp(aioportcon->ioport_high - aioportcon->ioport_low,
419*2d543d20SAndroid Build Coastguard Worker bioportcon->ioport_high - bioportcon->ioport_low);
420*2d543d20SAndroid Build Coastguard Worker if (rc == 0) {
421*2d543d20SAndroid Build Coastguard Worker if (aioportcon->ioport_low < bioportcon->ioport_low) {
422*2d543d20SAndroid Build Coastguard Worker rc = -1;
423*2d543d20SAndroid Build Coastguard Worker } else if (bioportcon->ioport_low < aioportcon->ioport_low) {
424*2d543d20SAndroid Build Coastguard Worker rc = 1;
425*2d543d20SAndroid Build Coastguard Worker }
426*2d543d20SAndroid Build Coastguard Worker }
427*2d543d20SAndroid Build Coastguard Worker
428*2d543d20SAndroid Build Coastguard Worker return rc;
429*2d543d20SAndroid Build Coastguard Worker }
430*2d543d20SAndroid Build Coastguard Worker
cil_post_pcidevicecon_compare(const void * a,const void * b)431*2d543d20SAndroid Build Coastguard Worker static int cil_post_pcidevicecon_compare(const void *a, const void *b)
432*2d543d20SAndroid Build Coastguard Worker {
433*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
434*2d543d20SAndroid Build Coastguard Worker struct cil_pcidevicecon *apcidevicecon = *(struct cil_pcidevicecon**)a;
435*2d543d20SAndroid Build Coastguard Worker struct cil_pcidevicecon *bpcidevicecon = *(struct cil_pcidevicecon**)b;
436*2d543d20SAndroid Build Coastguard Worker
437*2d543d20SAndroid Build Coastguard Worker if (apcidevicecon->dev < bpcidevicecon->dev) {
438*2d543d20SAndroid Build Coastguard Worker rc = -1;
439*2d543d20SAndroid Build Coastguard Worker } else if (bpcidevicecon->dev < apcidevicecon->dev) {
440*2d543d20SAndroid Build Coastguard Worker rc = 1;
441*2d543d20SAndroid Build Coastguard Worker } else {
442*2d543d20SAndroid Build Coastguard Worker rc = 0;
443*2d543d20SAndroid Build Coastguard Worker }
444*2d543d20SAndroid Build Coastguard Worker
445*2d543d20SAndroid Build Coastguard Worker return rc;
446*2d543d20SAndroid Build Coastguard Worker }
447*2d543d20SAndroid Build Coastguard Worker
cil_post_devicetreecon_compare(const void * a,const void * b)448*2d543d20SAndroid Build Coastguard Worker static int cil_post_devicetreecon_compare(const void *a, const void *b)
449*2d543d20SAndroid Build Coastguard Worker {
450*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
451*2d543d20SAndroid Build Coastguard Worker struct cil_devicetreecon *adevicetreecon = *(struct cil_devicetreecon**)a;
452*2d543d20SAndroid Build Coastguard Worker struct cil_devicetreecon *bdevicetreecon = *(struct cil_devicetreecon**)b;
453*2d543d20SAndroid Build Coastguard Worker
454*2d543d20SAndroid Build Coastguard Worker rc = strcmp(adevicetreecon->path, bdevicetreecon->path);
455*2d543d20SAndroid Build Coastguard Worker
456*2d543d20SAndroid Build Coastguard Worker return rc;
457*2d543d20SAndroid Build Coastguard Worker }
458*2d543d20SAndroid Build Coastguard Worker
cil_post_fsuse_compare(const void * a,const void * b)459*2d543d20SAndroid Build Coastguard Worker int cil_post_fsuse_compare(const void *a, const void *b)
460*2d543d20SAndroid Build Coastguard Worker {
461*2d543d20SAndroid Build Coastguard Worker int rc;
462*2d543d20SAndroid Build Coastguard Worker struct cil_fsuse *afsuse;
463*2d543d20SAndroid Build Coastguard Worker struct cil_fsuse *bfsuse;
464*2d543d20SAndroid Build Coastguard Worker afsuse = *(struct cil_fsuse**)a;
465*2d543d20SAndroid Build Coastguard Worker bfsuse = *(struct cil_fsuse**)b;
466*2d543d20SAndroid Build Coastguard Worker if (afsuse->type < bfsuse->type) {
467*2d543d20SAndroid Build Coastguard Worker rc = -1;
468*2d543d20SAndroid Build Coastguard Worker } else if (bfsuse->type < afsuse->type) {
469*2d543d20SAndroid Build Coastguard Worker rc = 1;
470*2d543d20SAndroid Build Coastguard Worker } else {
471*2d543d20SAndroid Build Coastguard Worker rc = strcmp(afsuse->fs_str, bfsuse->fs_str);
472*2d543d20SAndroid Build Coastguard Worker }
473*2d543d20SAndroid Build Coastguard Worker return rc;
474*2d543d20SAndroid Build Coastguard Worker }
475*2d543d20SAndroid Build Coastguard Worker
cil_post_filecon_context_compare(const void * a,const void * b)476*2d543d20SAndroid Build Coastguard Worker static int cil_post_filecon_context_compare(const void *a, const void *b)
477*2d543d20SAndroid Build Coastguard Worker {
478*2d543d20SAndroid Build Coastguard Worker struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
479*2d543d20SAndroid Build Coastguard Worker struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
480*2d543d20SAndroid Build Coastguard Worker return context_compare(a_filecon->context, b_filecon->context);
481*2d543d20SAndroid Build Coastguard Worker }
482*2d543d20SAndroid Build Coastguard Worker
cil_post_ibpkeycon_context_compare(const void * a,const void * b)483*2d543d20SAndroid Build Coastguard Worker static int cil_post_ibpkeycon_context_compare(const void *a, const void *b)
484*2d543d20SAndroid Build Coastguard Worker {
485*2d543d20SAndroid Build Coastguard Worker struct cil_ibpkeycon *a_ibpkeycon = *(struct cil_ibpkeycon **)a;
486*2d543d20SAndroid Build Coastguard Worker struct cil_ibpkeycon *b_ibpkeycon = *(struct cil_ibpkeycon **)b;
487*2d543d20SAndroid Build Coastguard Worker return context_compare(a_ibpkeycon->context, b_ibpkeycon->context);
488*2d543d20SAndroid Build Coastguard Worker }
489*2d543d20SAndroid Build Coastguard Worker
cil_post_portcon_context_compare(const void * a,const void * b)490*2d543d20SAndroid Build Coastguard Worker static int cil_post_portcon_context_compare(const void *a, const void *b)
491*2d543d20SAndroid Build Coastguard Worker {
492*2d543d20SAndroid Build Coastguard Worker struct cil_portcon *a_portcon = *(struct cil_portcon**)a;
493*2d543d20SAndroid Build Coastguard Worker struct cil_portcon *b_portcon = *(struct cil_portcon**)b;
494*2d543d20SAndroid Build Coastguard Worker return context_compare(a_portcon->context, b_portcon->context);
495*2d543d20SAndroid Build Coastguard Worker }
496*2d543d20SAndroid Build Coastguard Worker
cil_post_genfscon_context_compare(const void * a,const void * b)497*2d543d20SAndroid Build Coastguard Worker static int cil_post_genfscon_context_compare(const void *a, const void *b)
498*2d543d20SAndroid Build Coastguard Worker {
499*2d543d20SAndroid Build Coastguard Worker struct cil_genfscon *a_genfscon = *(struct cil_genfscon**)a;
500*2d543d20SAndroid Build Coastguard Worker struct cil_genfscon *b_genfscon = *(struct cil_genfscon**)b;
501*2d543d20SAndroid Build Coastguard Worker return context_compare(a_genfscon->context, b_genfscon->context);
502*2d543d20SAndroid Build Coastguard Worker }
503*2d543d20SAndroid Build Coastguard Worker
cil_post_netifcon_context_compare(const void * a,const void * b)504*2d543d20SAndroid Build Coastguard Worker static int cil_post_netifcon_context_compare(const void *a, const void *b)
505*2d543d20SAndroid Build Coastguard Worker {
506*2d543d20SAndroid Build Coastguard Worker int rc;
507*2d543d20SAndroid Build Coastguard Worker struct cil_netifcon *a_netifcon = *(struct cil_netifcon**)a;
508*2d543d20SAndroid Build Coastguard Worker struct cil_netifcon *b_netifcon = *(struct cil_netifcon**)b;
509*2d543d20SAndroid Build Coastguard Worker rc = context_compare(a_netifcon->if_context, b_netifcon->if_context);
510*2d543d20SAndroid Build Coastguard Worker if (rc != 0) {
511*2d543d20SAndroid Build Coastguard Worker return rc;
512*2d543d20SAndroid Build Coastguard Worker }
513*2d543d20SAndroid Build Coastguard Worker return context_compare(a_netifcon->packet_context, b_netifcon->packet_context);
514*2d543d20SAndroid Build Coastguard Worker }
515*2d543d20SAndroid Build Coastguard Worker
cil_post_ibendportcon_context_compare(const void * a,const void * b)516*2d543d20SAndroid Build Coastguard Worker static int cil_post_ibendportcon_context_compare(const void *a, const void *b)
517*2d543d20SAndroid Build Coastguard Worker {
518*2d543d20SAndroid Build Coastguard Worker struct cil_ibendportcon *a_ibendportcon = *(struct cil_ibendportcon **)a;
519*2d543d20SAndroid Build Coastguard Worker struct cil_ibendportcon *b_ibendportcon = *(struct cil_ibendportcon **)b;
520*2d543d20SAndroid Build Coastguard Worker return context_compare(a_ibendportcon->context, b_ibendportcon->context);
521*2d543d20SAndroid Build Coastguard Worker }
522*2d543d20SAndroid Build Coastguard Worker
cil_post_nodecon_context_compare(const void * a,const void * b)523*2d543d20SAndroid Build Coastguard Worker static int cil_post_nodecon_context_compare(const void *a, const void *b)
524*2d543d20SAndroid Build Coastguard Worker {
525*2d543d20SAndroid Build Coastguard Worker struct cil_nodecon *a_nodecon = *(struct cil_nodecon **)a;
526*2d543d20SAndroid Build Coastguard Worker struct cil_nodecon *b_nodecon = *(struct cil_nodecon **)b;
527*2d543d20SAndroid Build Coastguard Worker return context_compare(a_nodecon->context, b_nodecon->context);
528*2d543d20SAndroid Build Coastguard Worker }
529*2d543d20SAndroid Build Coastguard Worker
cil_post_pirqcon_context_compare(const void * a,const void * b)530*2d543d20SAndroid Build Coastguard Worker static int cil_post_pirqcon_context_compare(const void *a, const void *b)
531*2d543d20SAndroid Build Coastguard Worker {
532*2d543d20SAndroid Build Coastguard Worker struct cil_pirqcon *a_pirqcon = *(struct cil_pirqcon**)a;
533*2d543d20SAndroid Build Coastguard Worker struct cil_pirqcon *b_pirqcon = *(struct cil_pirqcon**)b;
534*2d543d20SAndroid Build Coastguard Worker return context_compare(a_pirqcon->context, b_pirqcon->context);
535*2d543d20SAndroid Build Coastguard Worker }
536*2d543d20SAndroid Build Coastguard Worker
cil_post_iomemcon_context_compare(const void * a,const void * b)537*2d543d20SAndroid Build Coastguard Worker static int cil_post_iomemcon_context_compare(const void *a, const void *b)
538*2d543d20SAndroid Build Coastguard Worker {
539*2d543d20SAndroid Build Coastguard Worker struct cil_iomemcon *a_iomemcon = *(struct cil_iomemcon**)a;
540*2d543d20SAndroid Build Coastguard Worker struct cil_iomemcon *b_iomemcon = *(struct cil_iomemcon**)b;
541*2d543d20SAndroid Build Coastguard Worker return context_compare(a_iomemcon->context, b_iomemcon->context);
542*2d543d20SAndroid Build Coastguard Worker }
543*2d543d20SAndroid Build Coastguard Worker
cil_post_ioportcon_context_compare(const void * a,const void * b)544*2d543d20SAndroid Build Coastguard Worker static int cil_post_ioportcon_context_compare(const void *a, const void *b)
545*2d543d20SAndroid Build Coastguard Worker {
546*2d543d20SAndroid Build Coastguard Worker struct cil_ioportcon *a_ioportcon = *(struct cil_ioportcon**)a;
547*2d543d20SAndroid Build Coastguard Worker struct cil_ioportcon *b_ioportcon = *(struct cil_ioportcon**)b;
548*2d543d20SAndroid Build Coastguard Worker return context_compare(a_ioportcon->context, b_ioportcon->context);
549*2d543d20SAndroid Build Coastguard Worker }
550*2d543d20SAndroid Build Coastguard Worker
cil_post_pcidevicecon_context_compare(const void * a,const void * b)551*2d543d20SAndroid Build Coastguard Worker static int cil_post_pcidevicecon_context_compare(const void *a, const void *b)
552*2d543d20SAndroid Build Coastguard Worker {
553*2d543d20SAndroid Build Coastguard Worker struct cil_pcidevicecon *a_pcidevicecon = *(struct cil_pcidevicecon**)a;
554*2d543d20SAndroid Build Coastguard Worker struct cil_pcidevicecon *b_pcidevicecon = *(struct cil_pcidevicecon**)b;
555*2d543d20SAndroid Build Coastguard Worker return context_compare(a_pcidevicecon->context, b_pcidevicecon->context);
556*2d543d20SAndroid Build Coastguard Worker }
557*2d543d20SAndroid Build Coastguard Worker
cil_post_devicetreecon_context_compare(const void * a,const void * b)558*2d543d20SAndroid Build Coastguard Worker static int cil_post_devicetreecon_context_compare(const void *a, const void *b)
559*2d543d20SAndroid Build Coastguard Worker {
560*2d543d20SAndroid Build Coastguard Worker struct cil_devicetreecon *a_devicetreecon = *(struct cil_devicetreecon**)a;
561*2d543d20SAndroid Build Coastguard Worker struct cil_devicetreecon *b_devicetreecon = *(struct cil_devicetreecon**)b;
562*2d543d20SAndroid Build Coastguard Worker return context_compare(a_devicetreecon->context, b_devicetreecon->context);
563*2d543d20SAndroid Build Coastguard Worker }
564*2d543d20SAndroid Build Coastguard Worker
cil_post_fsuse_context_compare(const void * a,const void * b)565*2d543d20SAndroid Build Coastguard Worker static int cil_post_fsuse_context_compare(const void *a, const void *b)
566*2d543d20SAndroid Build Coastguard Worker {
567*2d543d20SAndroid Build Coastguard Worker struct cil_fsuse *a_fsuse = *(struct cil_fsuse**)a;
568*2d543d20SAndroid Build Coastguard Worker struct cil_fsuse *b_fsuse = *(struct cil_fsuse**)b;
569*2d543d20SAndroid Build Coastguard Worker return context_compare(a_fsuse->context, b_fsuse->context);
570*2d543d20SAndroid Build Coastguard Worker }
571*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_count_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)572*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_count_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
573*2d543d20SAndroid Build Coastguard Worker {
574*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
575*2d543d20SAndroid Build Coastguard Worker
576*2d543d20SAndroid Build Coastguard Worker switch(node->flavor) {
577*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
578*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
579*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
580*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
581*2d543d20SAndroid Build Coastguard Worker }
582*2d543d20SAndroid Build Coastguard Worker break;
583*2d543d20SAndroid Build Coastguard Worker }
584*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO:
585*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
586*2d543d20SAndroid Build Coastguard Worker break;
587*2d543d20SAndroid Build Coastguard Worker case CIL_CLASS: {
588*2d543d20SAndroid Build Coastguard Worker struct cil_class *class = node->data;
589*2d543d20SAndroid Build Coastguard Worker if (class->datum.nodes->head->data == node) {
590*2d543d20SAndroid Build Coastguard Worker // Multiple nodes can point to the same datum. Only count once.
591*2d543d20SAndroid Build Coastguard Worker db->num_classes++;
592*2d543d20SAndroid Build Coastguard Worker }
593*2d543d20SAndroid Build Coastguard Worker break;
594*2d543d20SAndroid Build Coastguard Worker }
595*2d543d20SAndroid Build Coastguard Worker case CIL_TYPE: {
596*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = node->data;
597*2d543d20SAndroid Build Coastguard Worker if (type->datum.nodes->head->data == node) {
598*2d543d20SAndroid Build Coastguard Worker // Multiple nodes can point to the same datum. Only count once.
599*2d543d20SAndroid Build Coastguard Worker type->value = db->num_types;
600*2d543d20SAndroid Build Coastguard Worker db->num_types++;
601*2d543d20SAndroid Build Coastguard Worker db->num_types_and_attrs++;
602*2d543d20SAndroid Build Coastguard Worker }
603*2d543d20SAndroid Build Coastguard Worker break;
604*2d543d20SAndroid Build Coastguard Worker }
605*2d543d20SAndroid Build Coastguard Worker case CIL_TYPEATTRIBUTE: {
606*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = node->data;
607*2d543d20SAndroid Build Coastguard Worker if (attr->datum.nodes->head->data == node) {
608*2d543d20SAndroid Build Coastguard Worker // Multiple nodes can point to the same datum. Only count once.
609*2d543d20SAndroid Build Coastguard Worker db->num_types_and_attrs++;
610*2d543d20SAndroid Build Coastguard Worker }
611*2d543d20SAndroid Build Coastguard Worker break;
612*2d543d20SAndroid Build Coastguard Worker }
613*2d543d20SAndroid Build Coastguard Worker
614*2d543d20SAndroid Build Coastguard Worker case CIL_ROLE: {
615*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = node->data;
616*2d543d20SAndroid Build Coastguard Worker if (role->datum.nodes->head->data == node) {
617*2d543d20SAndroid Build Coastguard Worker // Multiple nodes can point to the same datum. Only count once.
618*2d543d20SAndroid Build Coastguard Worker role->value = db->num_roles;
619*2d543d20SAndroid Build Coastguard Worker db->num_roles++;
620*2d543d20SAndroid Build Coastguard Worker }
621*2d543d20SAndroid Build Coastguard Worker break;
622*2d543d20SAndroid Build Coastguard Worker }
623*2d543d20SAndroid Build Coastguard Worker case CIL_USER: {
624*2d543d20SAndroid Build Coastguard Worker struct cil_user *user = node->data;
625*2d543d20SAndroid Build Coastguard Worker if (user->datum.nodes->head->data == node) {
626*2d543d20SAndroid Build Coastguard Worker // multiple AST nodes can point to the same cil_user data (like if
627*2d543d20SAndroid Build Coastguard Worker // copied from a macro). This check ensures we only count the
628*2d543d20SAndroid Build Coastguard Worker // duplicates once
629*2d543d20SAndroid Build Coastguard Worker user->value = db->num_users;
630*2d543d20SAndroid Build Coastguard Worker db->num_users++;
631*2d543d20SAndroid Build Coastguard Worker }
632*2d543d20SAndroid Build Coastguard Worker break;
633*2d543d20SAndroid Build Coastguard Worker }
634*2d543d20SAndroid Build Coastguard Worker case CIL_NETIFCON:
635*2d543d20SAndroid Build Coastguard Worker db->netifcon->count++;
636*2d543d20SAndroid Build Coastguard Worker break;
637*2d543d20SAndroid Build Coastguard Worker case CIL_GENFSCON:
638*2d543d20SAndroid Build Coastguard Worker db->genfscon->count++;
639*2d543d20SAndroid Build Coastguard Worker break;
640*2d543d20SAndroid Build Coastguard Worker case CIL_FILECON:
641*2d543d20SAndroid Build Coastguard Worker db->filecon->count++;
642*2d543d20SAndroid Build Coastguard Worker break;
643*2d543d20SAndroid Build Coastguard Worker case CIL_NODECON:
644*2d543d20SAndroid Build Coastguard Worker db->nodecon->count++;
645*2d543d20SAndroid Build Coastguard Worker break;
646*2d543d20SAndroid Build Coastguard Worker case CIL_IBPKEYCON:
647*2d543d20SAndroid Build Coastguard Worker db->ibpkeycon->count++;
648*2d543d20SAndroid Build Coastguard Worker break;
649*2d543d20SAndroid Build Coastguard Worker case CIL_IBENDPORTCON:
650*2d543d20SAndroid Build Coastguard Worker db->ibendportcon->count++;
651*2d543d20SAndroid Build Coastguard Worker break;
652*2d543d20SAndroid Build Coastguard Worker case CIL_PORTCON:
653*2d543d20SAndroid Build Coastguard Worker db->portcon->count++;
654*2d543d20SAndroid Build Coastguard Worker break;
655*2d543d20SAndroid Build Coastguard Worker case CIL_PIRQCON:
656*2d543d20SAndroid Build Coastguard Worker db->pirqcon->count++;
657*2d543d20SAndroid Build Coastguard Worker break;
658*2d543d20SAndroid Build Coastguard Worker case CIL_IOMEMCON:
659*2d543d20SAndroid Build Coastguard Worker db->iomemcon->count++;
660*2d543d20SAndroid Build Coastguard Worker break;
661*2d543d20SAndroid Build Coastguard Worker case CIL_IOPORTCON:
662*2d543d20SAndroid Build Coastguard Worker db->ioportcon->count++;
663*2d543d20SAndroid Build Coastguard Worker break;
664*2d543d20SAndroid Build Coastguard Worker case CIL_PCIDEVICECON:
665*2d543d20SAndroid Build Coastguard Worker db->pcidevicecon->count++;
666*2d543d20SAndroid Build Coastguard Worker break;
667*2d543d20SAndroid Build Coastguard Worker case CIL_DEVICETREECON:
668*2d543d20SAndroid Build Coastguard Worker db->devicetreecon->count++;
669*2d543d20SAndroid Build Coastguard Worker break;
670*2d543d20SAndroid Build Coastguard Worker case CIL_FSUSE:
671*2d543d20SAndroid Build Coastguard Worker db->fsuse->count++;
672*2d543d20SAndroid Build Coastguard Worker break;
673*2d543d20SAndroid Build Coastguard Worker default:
674*2d543d20SAndroid Build Coastguard Worker break;
675*2d543d20SAndroid Build Coastguard Worker }
676*2d543d20SAndroid Build Coastguard Worker
677*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
678*2d543d20SAndroid Build Coastguard Worker }
679*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_array_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)680*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_array_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
681*2d543d20SAndroid Build Coastguard Worker {
682*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
683*2d543d20SAndroid Build Coastguard Worker
684*2d543d20SAndroid Build Coastguard Worker switch(node->flavor) {
685*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
686*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
687*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
688*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
689*2d543d20SAndroid Build Coastguard Worker }
690*2d543d20SAndroid Build Coastguard Worker break;
691*2d543d20SAndroid Build Coastguard Worker }
692*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO:
693*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
694*2d543d20SAndroid Build Coastguard Worker break;
695*2d543d20SAndroid Build Coastguard Worker case CIL_TYPE: {
696*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = node->data;
697*2d543d20SAndroid Build Coastguard Worker if (db->val_to_type == NULL) {
698*2d543d20SAndroid Build Coastguard Worker db->val_to_type = cil_malloc(sizeof(*db->val_to_type) * db->num_types);
699*2d543d20SAndroid Build Coastguard Worker }
700*2d543d20SAndroid Build Coastguard Worker db->val_to_type[type->value] = type;
701*2d543d20SAndroid Build Coastguard Worker break;
702*2d543d20SAndroid Build Coastguard Worker }
703*2d543d20SAndroid Build Coastguard Worker case CIL_ROLE: {
704*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = node->data;
705*2d543d20SAndroid Build Coastguard Worker if (db->val_to_role == NULL) {
706*2d543d20SAndroid Build Coastguard Worker db->val_to_role = cil_malloc(sizeof(*db->val_to_role) * db->num_roles);
707*2d543d20SAndroid Build Coastguard Worker }
708*2d543d20SAndroid Build Coastguard Worker db->val_to_role[role->value] = role;
709*2d543d20SAndroid Build Coastguard Worker break;
710*2d543d20SAndroid Build Coastguard Worker }
711*2d543d20SAndroid Build Coastguard Worker case CIL_USER: {
712*2d543d20SAndroid Build Coastguard Worker struct cil_user *user= node->data;
713*2d543d20SAndroid Build Coastguard Worker if (db->val_to_user == NULL) {
714*2d543d20SAndroid Build Coastguard Worker db->val_to_user = cil_malloc(sizeof(*db->val_to_user) * db->num_users);
715*2d543d20SAndroid Build Coastguard Worker }
716*2d543d20SAndroid Build Coastguard Worker db->val_to_user[user->value] = user;
717*2d543d20SAndroid Build Coastguard Worker break;
718*2d543d20SAndroid Build Coastguard Worker }
719*2d543d20SAndroid Build Coastguard Worker case CIL_USERPREFIX: {
720*2d543d20SAndroid Build Coastguard Worker cil_list_append(db->userprefixes, CIL_USERPREFIX, node->data);
721*2d543d20SAndroid Build Coastguard Worker break;
722*2d543d20SAndroid Build Coastguard Worker }
723*2d543d20SAndroid Build Coastguard Worker case CIL_SELINUXUSER: {
724*2d543d20SAndroid Build Coastguard Worker cil_list_prepend(db->selinuxusers, CIL_SELINUXUSER, node->data);
725*2d543d20SAndroid Build Coastguard Worker break;
726*2d543d20SAndroid Build Coastguard Worker }
727*2d543d20SAndroid Build Coastguard Worker case CIL_SELINUXUSERDEFAULT: {
728*2d543d20SAndroid Build Coastguard Worker cil_list_append(db->selinuxusers, CIL_SELINUXUSERDEFAULT, node->data);
729*2d543d20SAndroid Build Coastguard Worker break;
730*2d543d20SAndroid Build Coastguard Worker }
731*2d543d20SAndroid Build Coastguard Worker case CIL_NETIFCON: {
732*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->netifcon;
733*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
734*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
735*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
736*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
737*2d543d20SAndroid Build Coastguard Worker }
738*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
739*2d543d20SAndroid Build Coastguard Worker sort->index++;
740*2d543d20SAndroid Build Coastguard Worker break;
741*2d543d20SAndroid Build Coastguard Worker }
742*2d543d20SAndroid Build Coastguard Worker case CIL_IBENDPORTCON: {
743*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->ibendportcon;
744*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
745*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
746*2d543d20SAndroid Build Coastguard Worker
747*2d543d20SAndroid Build Coastguard Worker if (!sort->array)
748*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array) * count);
749*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
750*2d543d20SAndroid Build Coastguard Worker sort->index++;
751*2d543d20SAndroid Build Coastguard Worker break;
752*2d543d20SAndroid Build Coastguard Worker }
753*2d543d20SAndroid Build Coastguard Worker case CIL_FSUSE: {
754*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->fsuse;
755*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
756*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
757*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
758*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
759*2d543d20SAndroid Build Coastguard Worker }
760*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
761*2d543d20SAndroid Build Coastguard Worker sort->index++;
762*2d543d20SAndroid Build Coastguard Worker break;
763*2d543d20SAndroid Build Coastguard Worker }
764*2d543d20SAndroid Build Coastguard Worker case CIL_GENFSCON: {
765*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->genfscon;
766*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
767*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
768*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
769*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
770*2d543d20SAndroid Build Coastguard Worker }
771*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
772*2d543d20SAndroid Build Coastguard Worker sort->index++;
773*2d543d20SAndroid Build Coastguard Worker break;
774*2d543d20SAndroid Build Coastguard Worker }
775*2d543d20SAndroid Build Coastguard Worker case CIL_FILECON: {
776*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->filecon;
777*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
778*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
779*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
780*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
781*2d543d20SAndroid Build Coastguard Worker }
782*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
783*2d543d20SAndroid Build Coastguard Worker sort->index++;
784*2d543d20SAndroid Build Coastguard Worker break;
785*2d543d20SAndroid Build Coastguard Worker }
786*2d543d20SAndroid Build Coastguard Worker case CIL_NODECON: {
787*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->nodecon;
788*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
789*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
790*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
791*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
792*2d543d20SAndroid Build Coastguard Worker }
793*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
794*2d543d20SAndroid Build Coastguard Worker sort->index++;
795*2d543d20SAndroid Build Coastguard Worker break;
796*2d543d20SAndroid Build Coastguard Worker }
797*2d543d20SAndroid Build Coastguard Worker case CIL_IBPKEYCON: {
798*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->ibpkeycon;
799*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
800*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
801*2d543d20SAndroid Build Coastguard Worker
802*2d543d20SAndroid Build Coastguard Worker if (!sort->array)
803*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array) * count);
804*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
805*2d543d20SAndroid Build Coastguard Worker sort->index++;
806*2d543d20SAndroid Build Coastguard Worker break;
807*2d543d20SAndroid Build Coastguard Worker }
808*2d543d20SAndroid Build Coastguard Worker case CIL_PORTCON: {
809*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->portcon;
810*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
811*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
812*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
813*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
814*2d543d20SAndroid Build Coastguard Worker }
815*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
816*2d543d20SAndroid Build Coastguard Worker sort->index++;
817*2d543d20SAndroid Build Coastguard Worker break;
818*2d543d20SAndroid Build Coastguard Worker }
819*2d543d20SAndroid Build Coastguard Worker case CIL_PIRQCON: {
820*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->pirqcon;
821*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
822*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
823*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
824*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
825*2d543d20SAndroid Build Coastguard Worker }
826*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
827*2d543d20SAndroid Build Coastguard Worker sort->index++;
828*2d543d20SAndroid Build Coastguard Worker break;
829*2d543d20SAndroid Build Coastguard Worker }
830*2d543d20SAndroid Build Coastguard Worker case CIL_IOMEMCON: {
831*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->iomemcon;
832*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
833*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
834*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
835*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
836*2d543d20SAndroid Build Coastguard Worker }
837*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
838*2d543d20SAndroid Build Coastguard Worker sort->index++;
839*2d543d20SAndroid Build Coastguard Worker break;
840*2d543d20SAndroid Build Coastguard Worker }
841*2d543d20SAndroid Build Coastguard Worker case CIL_IOPORTCON: {
842*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->ioportcon;
843*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
844*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
845*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
846*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
847*2d543d20SAndroid Build Coastguard Worker }
848*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
849*2d543d20SAndroid Build Coastguard Worker sort->index++;
850*2d543d20SAndroid Build Coastguard Worker break;
851*2d543d20SAndroid Build Coastguard Worker }
852*2d543d20SAndroid Build Coastguard Worker case CIL_PCIDEVICECON: {
853*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->pcidevicecon;
854*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
855*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
856*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
857*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
858*2d543d20SAndroid Build Coastguard Worker }
859*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
860*2d543d20SAndroid Build Coastguard Worker sort->index++;
861*2d543d20SAndroid Build Coastguard Worker break;
862*2d543d20SAndroid Build Coastguard Worker }
863*2d543d20SAndroid Build Coastguard Worker case CIL_DEVICETREECON: {
864*2d543d20SAndroid Build Coastguard Worker struct cil_sort *sort = db->devicetreecon;
865*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
866*2d543d20SAndroid Build Coastguard Worker uint32_t i = sort->index;
867*2d543d20SAndroid Build Coastguard Worker if (sort->array == NULL) {
868*2d543d20SAndroid Build Coastguard Worker sort->array = cil_malloc(sizeof(*sort->array)*count);
869*2d543d20SAndroid Build Coastguard Worker }
870*2d543d20SAndroid Build Coastguard Worker sort->array[i] = node->data;
871*2d543d20SAndroid Build Coastguard Worker sort->index++;
872*2d543d20SAndroid Build Coastguard Worker break;
873*2d543d20SAndroid Build Coastguard Worker }
874*2d543d20SAndroid Build Coastguard Worker default:
875*2d543d20SAndroid Build Coastguard Worker break;
876*2d543d20SAndroid Build Coastguard Worker }
877*2d543d20SAndroid Build Coastguard Worker
878*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
879*2d543d20SAndroid Build Coastguard Worker }
880*2d543d20SAndroid Build Coastguard Worker
__evaluate_type_expression(struct cil_typeattribute * attr,struct cil_db * db)881*2d543d20SAndroid Build Coastguard Worker static int __evaluate_type_expression(struct cil_typeattribute *attr, struct cil_db *db)
882*2d543d20SAndroid Build Coastguard Worker {
883*2d543d20SAndroid Build Coastguard Worker int rc;
884*2d543d20SAndroid Build Coastguard Worker
885*2d543d20SAndroid Build Coastguard Worker attr->types = cil_malloc(sizeof(*attr->types));
886*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->types, db->num_types, db);
887*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
888*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to expand type attribute to bitmap\n");
889*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(attr->types);
890*2d543d20SAndroid Build Coastguard Worker free(attr->types);
891*2d543d20SAndroid Build Coastguard Worker attr->types = NULL;
892*2d543d20SAndroid Build Coastguard Worker }
893*2d543d20SAndroid Build Coastguard Worker return rc;
894*2d543d20SAndroid Build Coastguard Worker }
895*2d543d20SAndroid Build Coastguard Worker
__cil_type_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)896*2d543d20SAndroid Build Coastguard Worker static int __cil_type_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
897*2d543d20SAndroid Build Coastguard Worker {
898*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
899*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
900*2d543d20SAndroid Build Coastguard Worker
901*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
902*2d543d20SAndroid Build Coastguard Worker
903*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_TYPEATTRIBUTE) {
904*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
905*2d543d20SAndroid Build Coastguard Worker if (attr->types == NULL) {
906*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_type_expression(attr, db);
907*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
908*2d543d20SAndroid Build Coastguard Worker }
909*2d543d20SAndroid Build Coastguard Worker ebitmap_union(bitmap, attr->types);
910*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_TYPEALIAS) {
911*2d543d20SAndroid Build Coastguard Worker struct cil_alias *alias = (struct cil_alias *)datum;
912*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = alias->actual;
913*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, type->value, 1)) {
914*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set type bit\n");
915*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
916*2d543d20SAndroid Build Coastguard Worker goto exit;
917*2d543d20SAndroid Build Coastguard Worker }
918*2d543d20SAndroid Build Coastguard Worker } else {
919*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = (struct cil_type *)datum;
920*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, type->value, 1)) {
921*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set type bit\n");
922*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
923*2d543d20SAndroid Build Coastguard Worker goto exit;
924*2d543d20SAndroid Build Coastguard Worker }
925*2d543d20SAndroid Build Coastguard Worker }
926*2d543d20SAndroid Build Coastguard Worker
927*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
928*2d543d20SAndroid Build Coastguard Worker
929*2d543d20SAndroid Build Coastguard Worker exit:
930*2d543d20SAndroid Build Coastguard Worker return rc;
931*2d543d20SAndroid Build Coastguard Worker }
932*2d543d20SAndroid Build Coastguard Worker
__evaluate_user_expression(struct cil_userattribute * attr,struct cil_db * db)933*2d543d20SAndroid Build Coastguard Worker static int __evaluate_user_expression(struct cil_userattribute *attr, struct cil_db *db)
934*2d543d20SAndroid Build Coastguard Worker {
935*2d543d20SAndroid Build Coastguard Worker int rc;
936*2d543d20SAndroid Build Coastguard Worker
937*2d543d20SAndroid Build Coastguard Worker attr->users = cil_malloc(sizeof(*attr->users));
938*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->users, db->num_users, db);
939*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
940*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to expand user attribute to bitmap\n");
941*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(attr->users);
942*2d543d20SAndroid Build Coastguard Worker free(attr->users);
943*2d543d20SAndroid Build Coastguard Worker attr->users = NULL;
944*2d543d20SAndroid Build Coastguard Worker }
945*2d543d20SAndroid Build Coastguard Worker return rc;
946*2d543d20SAndroid Build Coastguard Worker }
947*2d543d20SAndroid Build Coastguard Worker
__cil_user_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)948*2d543d20SAndroid Build Coastguard Worker static int __cil_user_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
949*2d543d20SAndroid Build Coastguard Worker {
950*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
951*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
952*2d543d20SAndroid Build Coastguard Worker struct cil_userattribute *attr = NULL;
953*2d543d20SAndroid Build Coastguard Worker struct cil_user *user = NULL;
954*2d543d20SAndroid Build Coastguard Worker
955*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
956*2d543d20SAndroid Build Coastguard Worker
957*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_USERATTRIBUTE) {
958*2d543d20SAndroid Build Coastguard Worker attr = (struct cil_userattribute *)datum;
959*2d543d20SAndroid Build Coastguard Worker if (attr->users == NULL) {
960*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_user_expression(attr, db);
961*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
962*2d543d20SAndroid Build Coastguard Worker goto exit;
963*2d543d20SAndroid Build Coastguard Worker }
964*2d543d20SAndroid Build Coastguard Worker }
965*2d543d20SAndroid Build Coastguard Worker ebitmap_union(bitmap, attr->users);
966*2d543d20SAndroid Build Coastguard Worker } else {
967*2d543d20SAndroid Build Coastguard Worker user = (struct cil_user *)datum;
968*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, user->value, 1)) {
969*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set user bit\n");
970*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
971*2d543d20SAndroid Build Coastguard Worker goto exit;
972*2d543d20SAndroid Build Coastguard Worker }
973*2d543d20SAndroid Build Coastguard Worker }
974*2d543d20SAndroid Build Coastguard Worker
975*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
976*2d543d20SAndroid Build Coastguard Worker
977*2d543d20SAndroid Build Coastguard Worker exit:
978*2d543d20SAndroid Build Coastguard Worker return rc;
979*2d543d20SAndroid Build Coastguard Worker }
980*2d543d20SAndroid Build Coastguard Worker
__evaluate_role_expression(struct cil_roleattribute * attr,struct cil_db * db)981*2d543d20SAndroid Build Coastguard Worker static int __evaluate_role_expression(struct cil_roleattribute *attr, struct cil_db *db)
982*2d543d20SAndroid Build Coastguard Worker {
983*2d543d20SAndroid Build Coastguard Worker int rc;
984*2d543d20SAndroid Build Coastguard Worker
985*2d543d20SAndroid Build Coastguard Worker attr->roles = cil_malloc(sizeof(*attr->roles));
986*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->roles, db->num_roles, db);
987*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
988*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to expand role attribute to bitmap\n");
989*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(attr->roles);
990*2d543d20SAndroid Build Coastguard Worker free(attr->roles);
991*2d543d20SAndroid Build Coastguard Worker attr->roles = NULL;
992*2d543d20SAndroid Build Coastguard Worker }
993*2d543d20SAndroid Build Coastguard Worker return rc;
994*2d543d20SAndroid Build Coastguard Worker }
995*2d543d20SAndroid Build Coastguard Worker
__cil_role_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)996*2d543d20SAndroid Build Coastguard Worker static int __cil_role_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
997*2d543d20SAndroid Build Coastguard Worker {
998*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
999*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
1000*2d543d20SAndroid Build Coastguard Worker
1001*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
1002*2d543d20SAndroid Build Coastguard Worker
1003*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_ROLEATTRIBUTE) {
1004*2d543d20SAndroid Build Coastguard Worker struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
1005*2d543d20SAndroid Build Coastguard Worker if (attr->roles == NULL) {
1006*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_role_expression(attr, db);
1007*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1008*2d543d20SAndroid Build Coastguard Worker }
1009*2d543d20SAndroid Build Coastguard Worker ebitmap_union(bitmap, attr->roles);
1010*2d543d20SAndroid Build Coastguard Worker } else {
1011*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = (struct cil_role *)datum;
1012*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, role->value, 1)) {
1013*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set role bit\n");
1014*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1015*2d543d20SAndroid Build Coastguard Worker goto exit;
1016*2d543d20SAndroid Build Coastguard Worker }
1017*2d543d20SAndroid Build Coastguard Worker }
1018*2d543d20SAndroid Build Coastguard Worker
1019*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1020*2d543d20SAndroid Build Coastguard Worker
1021*2d543d20SAndroid Build Coastguard Worker exit:
1022*2d543d20SAndroid Build Coastguard Worker return rc;
1023*2d543d20SAndroid Build Coastguard Worker }
1024*2d543d20SAndroid Build Coastguard Worker
__evaluate_permissionx_expression(struct cil_permissionx * permx,struct cil_db * db)1025*2d543d20SAndroid Build Coastguard Worker static int __evaluate_permissionx_expression(struct cil_permissionx *permx, struct cil_db *db)
1026*2d543d20SAndroid Build Coastguard Worker {
1027*2d543d20SAndroid Build Coastguard Worker int rc;
1028*2d543d20SAndroid Build Coastguard Worker
1029*2d543d20SAndroid Build Coastguard Worker permx->perms = cil_malloc(sizeof(*permx->perms));
1030*2d543d20SAndroid Build Coastguard Worker ebitmap_init(permx->perms);
1031*2d543d20SAndroid Build Coastguard Worker
1032*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap(permx->expr_str, permx->perms, 0x10000, db); // max is one more than 0xFFFF
1033*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1034*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to expand permissionx expression\n");
1035*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(permx->perms);
1036*2d543d20SAndroid Build Coastguard Worker free(permx->perms);
1037*2d543d20SAndroid Build Coastguard Worker permx->perms = NULL;
1038*2d543d20SAndroid Build Coastguard Worker }
1039*2d543d20SAndroid Build Coastguard Worker
1040*2d543d20SAndroid Build Coastguard Worker return rc;
1041*2d543d20SAndroid Build Coastguard Worker }
1042*2d543d20SAndroid Build Coastguard Worker
__cil_permx_str_to_int(char * permx_str,uint16_t * val)1043*2d543d20SAndroid Build Coastguard Worker static int __cil_permx_str_to_int(char *permx_str, uint16_t *val)
1044*2d543d20SAndroid Build Coastguard Worker {
1045*2d543d20SAndroid Build Coastguard Worker char *endptr = NULL;
1046*2d543d20SAndroid Build Coastguard Worker long lval = strtol(permx_str, &endptr, 0);
1047*2d543d20SAndroid Build Coastguard Worker
1048*2d543d20SAndroid Build Coastguard Worker if (*endptr != '\0') {
1049*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "permissionx value %s not valid number\n", permx_str);
1050*2d543d20SAndroid Build Coastguard Worker goto exit;
1051*2d543d20SAndroid Build Coastguard Worker }
1052*2d543d20SAndroid Build Coastguard Worker if (lval < 0x0000 || lval > 0xFFFF) {
1053*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "permissionx value %s must be between 0x0000 and 0xFFFF\n", permx_str);
1054*2d543d20SAndroid Build Coastguard Worker goto exit;
1055*2d543d20SAndroid Build Coastguard Worker }
1056*2d543d20SAndroid Build Coastguard Worker
1057*2d543d20SAndroid Build Coastguard Worker *val = (uint16_t)lval;
1058*2d543d20SAndroid Build Coastguard Worker
1059*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1060*2d543d20SAndroid Build Coastguard Worker
1061*2d543d20SAndroid Build Coastguard Worker exit:
1062*2d543d20SAndroid Build Coastguard Worker return SEPOL_ERR;
1063*2d543d20SAndroid Build Coastguard Worker }
1064*2d543d20SAndroid Build Coastguard Worker
__cil_permx_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1065*2d543d20SAndroid Build Coastguard Worker static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1066*2d543d20SAndroid Build Coastguard Worker {
1067*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1068*2d543d20SAndroid Build Coastguard Worker uint16_t val;
1069*2d543d20SAndroid Build Coastguard Worker
1070*2d543d20SAndroid Build Coastguard Worker rc = __cil_permx_str_to_int((char*)datum, &val);
1071*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1072*2d543d20SAndroid Build Coastguard Worker goto exit;
1073*2d543d20SAndroid Build Coastguard Worker }
1074*2d543d20SAndroid Build Coastguard Worker
1075*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
1076*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
1077*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set permissionx bit\n");
1078*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1079*2d543d20SAndroid Build Coastguard Worker goto exit;
1080*2d543d20SAndroid Build Coastguard Worker }
1081*2d543d20SAndroid Build Coastguard Worker
1082*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1083*2d543d20SAndroid Build Coastguard Worker
1084*2d543d20SAndroid Build Coastguard Worker exit:
1085*2d543d20SAndroid Build Coastguard Worker return rc;
1086*2d543d20SAndroid Build Coastguard Worker }
1087*2d543d20SAndroid Build Coastguard Worker
__cil_perm_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1088*2d543d20SAndroid Build Coastguard Worker static int __cil_perm_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1089*2d543d20SAndroid Build Coastguard Worker {
1090*2d543d20SAndroid Build Coastguard Worker struct cil_perm *perm = (struct cil_perm *)datum;
1091*2d543d20SAndroid Build Coastguard Worker unsigned int value = perm->value;
1092*2d543d20SAndroid Build Coastguard Worker
1093*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
1094*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, value, 1)) {
1095*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to set perm bit\n");
1096*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1097*2d543d20SAndroid Build Coastguard Worker return SEPOL_ERR;
1098*2d543d20SAndroid Build Coastguard Worker }
1099*2d543d20SAndroid Build Coastguard Worker
1100*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1101*2d543d20SAndroid Build Coastguard Worker }
1102*2d543d20SAndroid Build Coastguard Worker
__evaluate_cat_expression(struct cil_cats * cats,struct cil_db * db)1103*2d543d20SAndroid Build Coastguard Worker static int __evaluate_cat_expression(struct cil_cats *cats, struct cil_db *db)
1104*2d543d20SAndroid Build Coastguard Worker {
1105*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1106*2d543d20SAndroid Build Coastguard Worker ebitmap_t bitmap;
1107*2d543d20SAndroid Build Coastguard Worker struct cil_list *new;
1108*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
1109*2d543d20SAndroid Build Coastguard Worker
1110*2d543d20SAndroid Build Coastguard Worker if (cats->evaluated == CIL_TRUE) {
1111*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1112*2d543d20SAndroid Build Coastguard Worker }
1113*2d543d20SAndroid Build Coastguard Worker
1114*2d543d20SAndroid Build Coastguard Worker if (cil_verify_is_list(cats->datum_expr, CIL_CAT)) {
1115*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1116*2d543d20SAndroid Build Coastguard Worker }
1117*2d543d20SAndroid Build Coastguard Worker
1118*2d543d20SAndroid Build Coastguard Worker ebitmap_init(&bitmap);
1119*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap(cats->datum_expr, &bitmap, db->num_cats, db);
1120*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1121*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to expand category expression to bitmap\n");
1122*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
1123*2d543d20SAndroid Build Coastguard Worker goto exit;
1124*2d543d20SAndroid Build Coastguard Worker }
1125*2d543d20SAndroid Build Coastguard Worker
1126*2d543d20SAndroid Build Coastguard Worker cil_list_init(&new, CIL_CAT);
1127*2d543d20SAndroid Build Coastguard Worker
1128*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(curr, db->catorder) {
1129*2d543d20SAndroid Build Coastguard Worker struct cil_cat *cat = curr->data;
1130*2d543d20SAndroid Build Coastguard Worker if (ebitmap_get_bit(&bitmap, cat->value)) {
1131*2d543d20SAndroid Build Coastguard Worker cil_list_append(new, CIL_DATUM, cat);
1132*2d543d20SAndroid Build Coastguard Worker }
1133*2d543d20SAndroid Build Coastguard Worker }
1134*2d543d20SAndroid Build Coastguard Worker
1135*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
1136*2d543d20SAndroid Build Coastguard Worker cil_list_destroy(&cats->datum_expr, CIL_FALSE);
1137*2d543d20SAndroid Build Coastguard Worker cats->datum_expr = new;
1138*2d543d20SAndroid Build Coastguard Worker
1139*2d543d20SAndroid Build Coastguard Worker cats->evaluated = CIL_TRUE;
1140*2d543d20SAndroid Build Coastguard Worker
1141*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1142*2d543d20SAndroid Build Coastguard Worker
1143*2d543d20SAndroid Build Coastguard Worker exit:
1144*2d543d20SAndroid Build Coastguard Worker return rc;
1145*2d543d20SAndroid Build Coastguard Worker }
1146*2d543d20SAndroid Build Coastguard Worker
__cil_cat_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1147*2d543d20SAndroid Build Coastguard Worker static int __cil_cat_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
1148*2d543d20SAndroid Build Coastguard Worker {
1149*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1150*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
1151*2d543d20SAndroid Build Coastguard Worker
1152*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
1153*2d543d20SAndroid Build Coastguard Worker
1154*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_CATSET) {
1155*2d543d20SAndroid Build Coastguard Worker struct cil_catset *catset = (struct cil_catset *)datum;
1156*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
1157*2d543d20SAndroid Build Coastguard Worker if (catset->cats->evaluated == CIL_FALSE) {
1158*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_cat_expression(catset->cats, db);
1159*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1160*2d543d20SAndroid Build Coastguard Worker }
1161*2d543d20SAndroid Build Coastguard Worker for (curr = catset->cats->datum_expr->head; curr; curr = curr->next) {
1162*2d543d20SAndroid Build Coastguard Worker struct cil_cat *cat = (struct cil_cat *)curr->data;
1163*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1164*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set cat bit\n");
1165*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1166*2d543d20SAndroid Build Coastguard Worker goto exit;
1167*2d543d20SAndroid Build Coastguard Worker }
1168*2d543d20SAndroid Build Coastguard Worker }
1169*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_CATALIAS) {
1170*2d543d20SAndroid Build Coastguard Worker struct cil_alias *alias = (struct cil_alias *)datum;
1171*2d543d20SAndroid Build Coastguard Worker struct cil_cat *cat = alias->actual;
1172*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1173*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set cat bit\n");
1174*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1175*2d543d20SAndroid Build Coastguard Worker goto exit;
1176*2d543d20SAndroid Build Coastguard Worker }
1177*2d543d20SAndroid Build Coastguard Worker } else {
1178*2d543d20SAndroid Build Coastguard Worker struct cil_cat *cat = (struct cil_cat *)datum;
1179*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1180*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set cat bit\n");
1181*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1182*2d543d20SAndroid Build Coastguard Worker goto exit;
1183*2d543d20SAndroid Build Coastguard Worker }
1184*2d543d20SAndroid Build Coastguard Worker }
1185*2d543d20SAndroid Build Coastguard Worker
1186*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1187*2d543d20SAndroid Build Coastguard Worker
1188*2d543d20SAndroid Build Coastguard Worker exit:
1189*2d543d20SAndroid Build Coastguard Worker return rc;
1190*2d543d20SAndroid Build Coastguard Worker }
1191*2d543d20SAndroid Build Coastguard Worker
__cil_cat_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)1192*2d543d20SAndroid Build Coastguard Worker static int __cil_cat_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1193*2d543d20SAndroid Build Coastguard Worker {
1194*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1195*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *d1 = i1->data;
1196*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *d2 = i2->data;
1197*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *n1 = d1->nodes->head->data;
1198*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *n2 = d2->nodes->head->data;
1199*2d543d20SAndroid Build Coastguard Worker struct cil_cat *c1 = (struct cil_cat *)d1;
1200*2d543d20SAndroid Build Coastguard Worker struct cil_cat *c2 = (struct cil_cat *)d2;
1201*2d543d20SAndroid Build Coastguard Worker
1202*2d543d20SAndroid Build Coastguard Worker if (n1->flavor == CIL_CATSET || n2->flavor == CIL_CATSET) {
1203*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Category sets cannot be used in a category range\n");
1204*2d543d20SAndroid Build Coastguard Worker goto exit;
1205*2d543d20SAndroid Build Coastguard Worker }
1206*2d543d20SAndroid Build Coastguard Worker
1207*2d543d20SAndroid Build Coastguard Worker if (n1->flavor == CIL_CATALIAS) {
1208*2d543d20SAndroid Build Coastguard Worker struct cil_alias *alias = (struct cil_alias *)d1;
1209*2d543d20SAndroid Build Coastguard Worker c1 = alias->actual;
1210*2d543d20SAndroid Build Coastguard Worker }
1211*2d543d20SAndroid Build Coastguard Worker
1212*2d543d20SAndroid Build Coastguard Worker if (n2->flavor == CIL_CATALIAS) {
1213*2d543d20SAndroid Build Coastguard Worker struct cil_alias *alias = (struct cil_alias *)d2;
1214*2d543d20SAndroid Build Coastguard Worker c2 = alias->actual;
1215*2d543d20SAndroid Build Coastguard Worker }
1216*2d543d20SAndroid Build Coastguard Worker
1217*2d543d20SAndroid Build Coastguard Worker if (c1->value > c2->value) {
1218*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Invalid category range\n");
1219*2d543d20SAndroid Build Coastguard Worker goto exit;
1220*2d543d20SAndroid Build Coastguard Worker }
1221*2d543d20SAndroid Build Coastguard Worker
1222*2d543d20SAndroid Build Coastguard Worker if (ebitmap_init_range(bitmap, c1->value, c2->value)) {
1223*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set cat bit\n");
1224*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1225*2d543d20SAndroid Build Coastguard Worker goto exit;
1226*2d543d20SAndroid Build Coastguard Worker }
1227*2d543d20SAndroid Build Coastguard Worker
1228*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1229*2d543d20SAndroid Build Coastguard Worker
1230*2d543d20SAndroid Build Coastguard Worker exit:
1231*2d543d20SAndroid Build Coastguard Worker return rc;
1232*2d543d20SAndroid Build Coastguard Worker }
1233*2d543d20SAndroid Build Coastguard Worker
__cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)1234*2d543d20SAndroid Build Coastguard Worker static int __cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1235*2d543d20SAndroid Build Coastguard Worker {
1236*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1237*2d543d20SAndroid Build Coastguard Worker char *p1 = i1->data;
1238*2d543d20SAndroid Build Coastguard Worker char *p2 = i2->data;
1239*2d543d20SAndroid Build Coastguard Worker uint16_t v1;
1240*2d543d20SAndroid Build Coastguard Worker uint16_t v2;
1241*2d543d20SAndroid Build Coastguard Worker
1242*2d543d20SAndroid Build Coastguard Worker rc = __cil_permx_str_to_int(p1, &v1);
1243*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1244*2d543d20SAndroid Build Coastguard Worker goto exit;
1245*2d543d20SAndroid Build Coastguard Worker }
1246*2d543d20SAndroid Build Coastguard Worker
1247*2d543d20SAndroid Build Coastguard Worker rc = __cil_permx_str_to_int(p2, &v2);
1248*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1249*2d543d20SAndroid Build Coastguard Worker goto exit;
1250*2d543d20SAndroid Build Coastguard Worker }
1251*2d543d20SAndroid Build Coastguard Worker
1252*2d543d20SAndroid Build Coastguard Worker if (ebitmap_init_range(bitmap, v1, v2)) {
1253*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to set permissionx bits\n");
1254*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1255*2d543d20SAndroid Build Coastguard Worker goto exit;
1256*2d543d20SAndroid Build Coastguard Worker }
1257*2d543d20SAndroid Build Coastguard Worker
1258*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1259*2d543d20SAndroid Build Coastguard Worker
1260*2d543d20SAndroid Build Coastguard Worker exit:
1261*2d543d20SAndroid Build Coastguard Worker return rc;
1262*2d543d20SAndroid Build Coastguard Worker }
1263*2d543d20SAndroid Build Coastguard Worker
__cil_expr_to_bitmap_helper(struct cil_list_item * curr,enum cil_flavor flavor,ebitmap_t * bitmap,int max,struct cil_db * db)1264*2d543d20SAndroid Build Coastguard Worker static int __cil_expr_to_bitmap_helper(struct cil_list_item *curr, enum cil_flavor flavor, ebitmap_t *bitmap, int max, struct cil_db *db)
1265*2d543d20SAndroid Build Coastguard Worker {
1266*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1267*2d543d20SAndroid Build Coastguard Worker
1268*2d543d20SAndroid Build Coastguard Worker if (curr->flavor == CIL_DATUM) {
1269*2d543d20SAndroid Build Coastguard Worker switch (flavor) {
1270*2d543d20SAndroid Build Coastguard Worker case CIL_TYPE:
1271*2d543d20SAndroid Build Coastguard Worker rc = __cil_type_to_bitmap(curr->data, bitmap, db);
1272*2d543d20SAndroid Build Coastguard Worker break;
1273*2d543d20SAndroid Build Coastguard Worker case CIL_ROLE:
1274*2d543d20SAndroid Build Coastguard Worker rc = __cil_role_to_bitmap(curr->data, bitmap, db);
1275*2d543d20SAndroid Build Coastguard Worker break;
1276*2d543d20SAndroid Build Coastguard Worker case CIL_USER:
1277*2d543d20SAndroid Build Coastguard Worker rc = __cil_user_to_bitmap(curr->data, bitmap, db);
1278*2d543d20SAndroid Build Coastguard Worker break;
1279*2d543d20SAndroid Build Coastguard Worker case CIL_PERM:
1280*2d543d20SAndroid Build Coastguard Worker rc = __cil_perm_to_bitmap(curr->data, bitmap, db);
1281*2d543d20SAndroid Build Coastguard Worker break;
1282*2d543d20SAndroid Build Coastguard Worker case CIL_CAT:
1283*2d543d20SAndroid Build Coastguard Worker rc = __cil_cat_to_bitmap(curr->data, bitmap, db);
1284*2d543d20SAndroid Build Coastguard Worker break;
1285*2d543d20SAndroid Build Coastguard Worker default:
1286*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
1287*2d543d20SAndroid Build Coastguard Worker }
1288*2d543d20SAndroid Build Coastguard Worker } else if (curr->flavor == CIL_LIST) {
1289*2d543d20SAndroid Build Coastguard Worker struct cil_list *l = curr->data;
1290*2d543d20SAndroid Build Coastguard Worker ebitmap_init(bitmap);
1291*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap(l, bitmap, max, db);
1292*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1293*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(bitmap);
1294*2d543d20SAndroid Build Coastguard Worker }
1295*2d543d20SAndroid Build Coastguard Worker } else if (flavor == CIL_PERMISSIONX) {
1296*2d543d20SAndroid Build Coastguard Worker // permissionx expressions aren't resolved into anything, so curr->flavor
1297*2d543d20SAndroid Build Coastguard Worker // is just a CIL_STRING, not a CIL_DATUM, so just check on flavor for those
1298*2d543d20SAndroid Build Coastguard Worker rc = __cil_permx_to_bitmap(curr->data, bitmap, db);
1299*2d543d20SAndroid Build Coastguard Worker }
1300*2d543d20SAndroid Build Coastguard Worker
1301*2d543d20SAndroid Build Coastguard Worker return rc;
1302*2d543d20SAndroid Build Coastguard Worker }
1303*2d543d20SAndroid Build Coastguard Worker
__cil_expr_to_bitmap(struct cil_list * expr,ebitmap_t * out,int max,struct cil_db * db)1304*2d543d20SAndroid Build Coastguard Worker static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db)
1305*2d543d20SAndroid Build Coastguard Worker {
1306*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1307*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
1308*2d543d20SAndroid Build Coastguard Worker enum cil_flavor flavor;
1309*2d543d20SAndroid Build Coastguard Worker ebitmap_t tmp, b1, b2;
1310*2d543d20SAndroid Build Coastguard Worker
1311*2d543d20SAndroid Build Coastguard Worker if (expr == NULL || expr->head == NULL) {
1312*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1313*2d543d20SAndroid Build Coastguard Worker }
1314*2d543d20SAndroid Build Coastguard Worker
1315*2d543d20SAndroid Build Coastguard Worker curr = expr->head;
1316*2d543d20SAndroid Build Coastguard Worker flavor = expr->flavor;
1317*2d543d20SAndroid Build Coastguard Worker
1318*2d543d20SAndroid Build Coastguard Worker ebitmap_init(&tmp);
1319*2d543d20SAndroid Build Coastguard Worker
1320*2d543d20SAndroid Build Coastguard Worker if (curr->flavor == CIL_OP) {
1321*2d543d20SAndroid Build Coastguard Worker enum cil_flavor op = (enum cil_flavor)(uintptr_t)curr->data;
1322*2d543d20SAndroid Build Coastguard Worker
1323*2d543d20SAndroid Build Coastguard Worker if (op == CIL_ALL) {
1324*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_init_range(&tmp, 0, max - 1);
1325*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1326*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to expand 'all' operator\n");
1327*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1328*2d543d20SAndroid Build Coastguard Worker goto exit;
1329*2d543d20SAndroid Build Coastguard Worker }
1330*2d543d20SAndroid Build Coastguard Worker } else if (op == CIL_RANGE) {
1331*2d543d20SAndroid Build Coastguard Worker if (flavor == CIL_CAT) {
1332*2d543d20SAndroid Build Coastguard Worker rc = __cil_cat_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1333*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1334*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to expand category range\n");
1335*2d543d20SAndroid Build Coastguard Worker goto exit;
1336*2d543d20SAndroid Build Coastguard Worker }
1337*2d543d20SAndroid Build Coastguard Worker } else if (flavor == CIL_PERMISSIONX) {
1338*2d543d20SAndroid Build Coastguard Worker rc = __cil_permissionx_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1339*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1340*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to expand category range\n");
1341*2d543d20SAndroid Build Coastguard Worker goto exit;
1342*2d543d20SAndroid Build Coastguard Worker }
1343*2d543d20SAndroid Build Coastguard Worker } else {
1344*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Range operation only supported for categories permissionx\n");
1345*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
1346*2d543d20SAndroid Build Coastguard Worker goto exit;
1347*2d543d20SAndroid Build Coastguard Worker }
1348*2d543d20SAndroid Build Coastguard Worker } else {
1349*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap_helper(curr->next, flavor, &b1, max, db);
1350*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1351*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to get first operand bitmap\n");
1352*2d543d20SAndroid Build Coastguard Worker goto exit;
1353*2d543d20SAndroid Build Coastguard Worker }
1354*2d543d20SAndroid Build Coastguard Worker
1355*2d543d20SAndroid Build Coastguard Worker if (op == CIL_NOT) {
1356*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_not(&tmp, &b1, max);
1357*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b1);
1358*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1359*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to NOT bitmap\n");
1360*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1361*2d543d20SAndroid Build Coastguard Worker goto exit;
1362*2d543d20SAndroid Build Coastguard Worker }
1363*2d543d20SAndroid Build Coastguard Worker } else {
1364*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap_helper(curr->next->next, flavor, &b2, max, db);
1365*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1366*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to get second operand bitmap\n");
1367*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b1);
1368*2d543d20SAndroid Build Coastguard Worker goto exit;
1369*2d543d20SAndroid Build Coastguard Worker }
1370*2d543d20SAndroid Build Coastguard Worker
1371*2d543d20SAndroid Build Coastguard Worker if (op == CIL_OR) {
1372*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_or(&tmp, &b1, &b2);
1373*2d543d20SAndroid Build Coastguard Worker } else if (op == CIL_AND) {
1374*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_and(&tmp, &b1, &b2);
1375*2d543d20SAndroid Build Coastguard Worker } else if (op == CIL_XOR) {
1376*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_xor(&tmp, &b1, &b2);
1377*2d543d20SAndroid Build Coastguard Worker } else {
1378*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
1379*2d543d20SAndroid Build Coastguard Worker }
1380*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b1);
1381*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b2);
1382*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1383*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to apply operator to bitmaps\n");
1384*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1385*2d543d20SAndroid Build Coastguard Worker goto exit;
1386*2d543d20SAndroid Build Coastguard Worker }
1387*2d543d20SAndroid Build Coastguard Worker }
1388*2d543d20SAndroid Build Coastguard Worker }
1389*2d543d20SAndroid Build Coastguard Worker } else {
1390*2d543d20SAndroid Build Coastguard Worker ebitmap_init(&tmp);
1391*2d543d20SAndroid Build Coastguard Worker for (;curr; curr = curr->next) {
1392*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap_helper(curr, flavor, &b2, max, db);
1393*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1394*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to get operand in list\n");
1395*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1396*2d543d20SAndroid Build Coastguard Worker goto exit;
1397*2d543d20SAndroid Build Coastguard Worker }
1398*2d543d20SAndroid Build Coastguard Worker b1 = tmp;
1399*2d543d20SAndroid Build Coastguard Worker rc = ebitmap_or(&tmp, &b1, &b2);
1400*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b1);
1401*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&b2);
1402*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1403*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to OR operands in list\n");
1404*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1405*2d543d20SAndroid Build Coastguard Worker goto exit;
1406*2d543d20SAndroid Build Coastguard Worker }
1407*2d543d20SAndroid Build Coastguard Worker
1408*2d543d20SAndroid Build Coastguard Worker }
1409*2d543d20SAndroid Build Coastguard Worker }
1410*2d543d20SAndroid Build Coastguard Worker
1411*2d543d20SAndroid Build Coastguard Worker ebitmap_union(out, &tmp);
1412*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&tmp);
1413*2d543d20SAndroid Build Coastguard Worker
1414*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1415*2d543d20SAndroid Build Coastguard Worker
1416*2d543d20SAndroid Build Coastguard Worker exit:
1417*2d543d20SAndroid Build Coastguard Worker return rc;
1418*2d543d20SAndroid Build Coastguard Worker }
1419*2d543d20SAndroid Build Coastguard Worker
__cil_expr_list_to_bitmap(struct cil_list * expr_list,ebitmap_t * out,int max,struct cil_db * db)1420*2d543d20SAndroid Build Coastguard Worker static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db)
1421*2d543d20SAndroid Build Coastguard Worker {
1422*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1423*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *expr;
1424*2d543d20SAndroid Build Coastguard Worker
1425*2d543d20SAndroid Build Coastguard Worker ebitmap_init(out);
1426*2d543d20SAndroid Build Coastguard Worker
1427*2d543d20SAndroid Build Coastguard Worker if (expr_list == NULL) {
1428*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1429*2d543d20SAndroid Build Coastguard Worker }
1430*2d543d20SAndroid Build Coastguard Worker
1431*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(expr, expr_list) {
1432*2d543d20SAndroid Build Coastguard Worker ebitmap_t bitmap;
1433*2d543d20SAndroid Build Coastguard Worker struct cil_list *l = (struct cil_list *)expr->data;
1434*2d543d20SAndroid Build Coastguard Worker ebitmap_init(&bitmap);
1435*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap(l, &bitmap, max, db);
1436*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1437*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to expand expression list to bitmap\n");
1438*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
1439*2d543d20SAndroid Build Coastguard Worker goto exit;
1440*2d543d20SAndroid Build Coastguard Worker }
1441*2d543d20SAndroid Build Coastguard Worker ebitmap_union(out, &bitmap);
1442*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
1443*2d543d20SAndroid Build Coastguard Worker }
1444*2d543d20SAndroid Build Coastguard Worker
1445*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1446*2d543d20SAndroid Build Coastguard Worker
1447*2d543d20SAndroid Build Coastguard Worker exit:
1448*2d543d20SAndroid Build Coastguard Worker return SEPOL_ERR;
1449*2d543d20SAndroid Build Coastguard Worker }
1450*2d543d20SAndroid Build Coastguard Worker
cil_typeattribute_used(struct cil_typeattribute * attr,struct cil_db * db)1451*2d543d20SAndroid Build Coastguard Worker static int cil_typeattribute_used(struct cil_typeattribute *attr, struct cil_db *db)
1452*2d543d20SAndroid Build Coastguard Worker {
1453*2d543d20SAndroid Build Coastguard Worker if (!attr->used) {
1454*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
1455*2d543d20SAndroid Build Coastguard Worker }
1456*2d543d20SAndroid Build Coastguard Worker
1457*2d543d20SAndroid Build Coastguard Worker if (attr->used & CIL_ATTR_EXPAND_FALSE) {
1458*2d543d20SAndroid Build Coastguard Worker return CIL_TRUE;
1459*2d543d20SAndroid Build Coastguard Worker }
1460*2d543d20SAndroid Build Coastguard Worker
1461*2d543d20SAndroid Build Coastguard Worker if (attr->used & CIL_ATTR_EXPAND_TRUE) {
1462*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
1463*2d543d20SAndroid Build Coastguard Worker }
1464*2d543d20SAndroid Build Coastguard Worker
1465*2d543d20SAndroid Build Coastguard Worker if (attr->used & CIL_ATTR_CONSTRAINT) {
1466*2d543d20SAndroid Build Coastguard Worker return CIL_TRUE;
1467*2d543d20SAndroid Build Coastguard Worker }
1468*2d543d20SAndroid Build Coastguard Worker
1469*2d543d20SAndroid Build Coastguard Worker if (db->attrs_expand_generated || attr->used == CIL_ATTR_NEVERALLOW) {
1470*2d543d20SAndroid Build Coastguard Worker if (strcmp(DATUM(attr)->name, GEN_REQUIRE_ATTR) == 0) {
1471*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
1472*2d543d20SAndroid Build Coastguard Worker } else if (strstr(DATUM(attr)->name, TYPEATTR_INFIX) != NULL) {
1473*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
1474*2d543d20SAndroid Build Coastguard Worker }
1475*2d543d20SAndroid Build Coastguard Worker
1476*2d543d20SAndroid Build Coastguard Worker if (attr->used == CIL_ATTR_NEVERALLOW) {
1477*2d543d20SAndroid Build Coastguard Worker return CIL_TRUE;
1478*2d543d20SAndroid Build Coastguard Worker }
1479*2d543d20SAndroid Build Coastguard Worker }
1480*2d543d20SAndroid Build Coastguard Worker
1481*2d543d20SAndroid Build Coastguard Worker if (attr->used == CIL_ATTR_AVRULE) {
1482*2d543d20SAndroid Build Coastguard Worker if (ebitmap_cardinality(attr->types) < db->attrs_expand_size) {
1483*2d543d20SAndroid Build Coastguard Worker return CIL_FALSE;
1484*2d543d20SAndroid Build Coastguard Worker }
1485*2d543d20SAndroid Build Coastguard Worker }
1486*2d543d20SAndroid Build Coastguard Worker
1487*2d543d20SAndroid Build Coastguard Worker return CIL_TRUE;
1488*2d543d20SAndroid Build Coastguard Worker }
1489*2d543d20SAndroid Build Coastguard Worker
__mark_neverallow_attrs(struct cil_list * expr_list)1490*2d543d20SAndroid Build Coastguard Worker static void __mark_neverallow_attrs(struct cil_list *expr_list)
1491*2d543d20SAndroid Build Coastguard Worker {
1492*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
1493*2d543d20SAndroid Build Coastguard Worker
1494*2d543d20SAndroid Build Coastguard Worker if (!expr_list) {
1495*2d543d20SAndroid Build Coastguard Worker return;
1496*2d543d20SAndroid Build Coastguard Worker }
1497*2d543d20SAndroid Build Coastguard Worker
1498*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(curr, expr_list) {
1499*2d543d20SAndroid Build Coastguard Worker if (curr->flavor == CIL_DATUM) {
1500*2d543d20SAndroid Build Coastguard Worker if (FLAVOR(curr->data) == CIL_TYPEATTRIBUTE) {
1501*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = curr->data;
1502*2d543d20SAndroid Build Coastguard Worker if (strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1503*2d543d20SAndroid Build Coastguard Worker __mark_neverallow_attrs(attr->expr_list);
1504*2d543d20SAndroid Build Coastguard Worker } else {
1505*2d543d20SAndroid Build Coastguard Worker attr->used |= CIL_ATTR_NEVERALLOW;
1506*2d543d20SAndroid Build Coastguard Worker }
1507*2d543d20SAndroid Build Coastguard Worker }
1508*2d543d20SAndroid Build Coastguard Worker } else if (curr->flavor == CIL_LIST) {
1509*2d543d20SAndroid Build Coastguard Worker __mark_neverallow_attrs(curr->data);
1510*2d543d20SAndroid Build Coastguard Worker }
1511*2d543d20SAndroid Build Coastguard Worker }
1512*2d543d20SAndroid Build Coastguard Worker }
1513*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_neverallow_attr_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1514*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_neverallow_attr_helper(struct cil_tree_node *node, uint32_t *finished, __attribute__((unused)) void *extra_args)
1515*2d543d20SAndroid Build Coastguard Worker {
1516*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
1517*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
1518*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
1519*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
1520*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1521*2d543d20SAndroid Build Coastguard Worker }
1522*2d543d20SAndroid Build Coastguard Worker break;
1523*2d543d20SAndroid Build Coastguard Worker }
1524*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO: {
1525*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1526*2d543d20SAndroid Build Coastguard Worker break;
1527*2d543d20SAndroid Build Coastguard Worker }
1528*2d543d20SAndroid Build Coastguard Worker case CIL_TYPEATTRIBUTE: {
1529*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = node->data;
1530*2d543d20SAndroid Build Coastguard Worker if ((attr->used & CIL_ATTR_NEVERALLOW) &&
1531*2d543d20SAndroid Build Coastguard Worker strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1532*2d543d20SAndroid Build Coastguard Worker __mark_neverallow_attrs(attr->expr_list);
1533*2d543d20SAndroid Build Coastguard Worker }
1534*2d543d20SAndroid Build Coastguard Worker break;
1535*2d543d20SAndroid Build Coastguard Worker }
1536*2d543d20SAndroid Build Coastguard Worker default:
1537*2d543d20SAndroid Build Coastguard Worker break;
1538*2d543d20SAndroid Build Coastguard Worker }
1539*2d543d20SAndroid Build Coastguard Worker
1540*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1541*2d543d20SAndroid Build Coastguard Worker }
1542*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_attr_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1543*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_attr_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1544*2d543d20SAndroid Build Coastguard Worker {
1545*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1546*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
1547*2d543d20SAndroid Build Coastguard Worker
1548*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
1549*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
1550*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
1551*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
1552*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1553*2d543d20SAndroid Build Coastguard Worker }
1554*2d543d20SAndroid Build Coastguard Worker break;
1555*2d543d20SAndroid Build Coastguard Worker }
1556*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO: {
1557*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1558*2d543d20SAndroid Build Coastguard Worker break;
1559*2d543d20SAndroid Build Coastguard Worker }
1560*2d543d20SAndroid Build Coastguard Worker case CIL_TYPEATTRIBUTE: {
1561*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = node->data;
1562*2d543d20SAndroid Build Coastguard Worker if (attr->types == NULL) {
1563*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_type_expression(attr, db);
1564*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1565*2d543d20SAndroid Build Coastguard Worker }
1566*2d543d20SAndroid Build Coastguard Worker attr->keep = cil_typeattribute_used(attr, db);
1567*2d543d20SAndroid Build Coastguard Worker break;
1568*2d543d20SAndroid Build Coastguard Worker }
1569*2d543d20SAndroid Build Coastguard Worker case CIL_ROLEATTRIBUTE: {
1570*2d543d20SAndroid Build Coastguard Worker struct cil_roleattribute *attr = node->data;
1571*2d543d20SAndroid Build Coastguard Worker if (attr->roles == NULL) {
1572*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_role_expression(attr, db);
1573*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1574*2d543d20SAndroid Build Coastguard Worker }
1575*2d543d20SAndroid Build Coastguard Worker break;
1576*2d543d20SAndroid Build Coastguard Worker }
1577*2d543d20SAndroid Build Coastguard Worker case CIL_AVRULEX: {
1578*2d543d20SAndroid Build Coastguard Worker struct cil_avrule *rule = node->data;
1579*2d543d20SAndroid Build Coastguard Worker if (rule->perms.x.permx_str == NULL) {
1580*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_permissionx_expression(rule->perms.x.permx, db);
1581*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1582*2d543d20SAndroid Build Coastguard Worker }
1583*2d543d20SAndroid Build Coastguard Worker break;
1584*2d543d20SAndroid Build Coastguard Worker }
1585*2d543d20SAndroid Build Coastguard Worker case CIL_PERMISSIONX: {
1586*2d543d20SAndroid Build Coastguard Worker struct cil_permissionx *permx = node->data;
1587*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_permissionx_expression(permx, db);
1588*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) goto exit;
1589*2d543d20SAndroid Build Coastguard Worker break;
1590*2d543d20SAndroid Build Coastguard Worker }
1591*2d543d20SAndroid Build Coastguard Worker case CIL_USERATTRIBUTE: {
1592*2d543d20SAndroid Build Coastguard Worker struct cil_userattribute *attr = node->data;
1593*2d543d20SAndroid Build Coastguard Worker if (attr->users == NULL) {
1594*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_user_expression(attr, db);
1595*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1596*2d543d20SAndroid Build Coastguard Worker goto exit;
1597*2d543d20SAndroid Build Coastguard Worker }
1598*2d543d20SAndroid Build Coastguard Worker }
1599*2d543d20SAndroid Build Coastguard Worker break;
1600*2d543d20SAndroid Build Coastguard Worker }
1601*2d543d20SAndroid Build Coastguard Worker default:
1602*2d543d20SAndroid Build Coastguard Worker break;
1603*2d543d20SAndroid Build Coastguard Worker }
1604*2d543d20SAndroid Build Coastguard Worker
1605*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1606*2d543d20SAndroid Build Coastguard Worker
1607*2d543d20SAndroid Build Coastguard Worker exit:
1608*2d543d20SAndroid Build Coastguard Worker return rc;
1609*2d543d20SAndroid Build Coastguard Worker }
1610*2d543d20SAndroid Build Coastguard Worker
__cil_role_assign_types(struct cil_role * role,struct cil_symtab_datum * datum)1611*2d543d20SAndroid Build Coastguard Worker static int __cil_role_assign_types(struct cil_role *role, struct cil_symtab_datum *datum)
1612*2d543d20SAndroid Build Coastguard Worker {
1613*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
1614*2d543d20SAndroid Build Coastguard Worker
1615*2d543d20SAndroid Build Coastguard Worker if (role->types == NULL) {
1616*2d543d20SAndroid Build Coastguard Worker role->types = cil_malloc(sizeof(*role->types));
1617*2d543d20SAndroid Build Coastguard Worker ebitmap_init(role->types);
1618*2d543d20SAndroid Build Coastguard Worker }
1619*2d543d20SAndroid Build Coastguard Worker
1620*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_TYPE) {
1621*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = (struct cil_type *)datum;
1622*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(role->types, type->value, 1)) {
1623*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1624*2d543d20SAndroid Build Coastguard Worker goto exit;
1625*2d543d20SAndroid Build Coastguard Worker }
1626*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_TYPEALIAS) {
1627*2d543d20SAndroid Build Coastguard Worker struct cil_alias *alias = (struct cil_alias *)datum;
1628*2d543d20SAndroid Build Coastguard Worker struct cil_type *type = alias->actual;
1629*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(role->types, type->value, 1)) {
1630*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1631*2d543d20SAndroid Build Coastguard Worker goto exit;
1632*2d543d20SAndroid Build Coastguard Worker }
1633*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_TYPEATTRIBUTE) {
1634*2d543d20SAndroid Build Coastguard Worker struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
1635*2d543d20SAndroid Build Coastguard Worker ebitmap_union(role->types, attr->types);
1636*2d543d20SAndroid Build Coastguard Worker }
1637*2d543d20SAndroid Build Coastguard Worker
1638*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1639*2d543d20SAndroid Build Coastguard Worker
1640*2d543d20SAndroid Build Coastguard Worker exit:
1641*2d543d20SAndroid Build Coastguard Worker return SEPOL_ERR;
1642*2d543d20SAndroid Build Coastguard Worker }
1643*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_roletype_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1644*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_roletype_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1645*2d543d20SAndroid Build Coastguard Worker {
1646*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1647*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
1648*2d543d20SAndroid Build Coastguard Worker
1649*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
1650*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
1651*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
1652*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
1653*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1654*2d543d20SAndroid Build Coastguard Worker }
1655*2d543d20SAndroid Build Coastguard Worker break;
1656*2d543d20SAndroid Build Coastguard Worker }
1657*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO: {
1658*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1659*2d543d20SAndroid Build Coastguard Worker break;
1660*2d543d20SAndroid Build Coastguard Worker }
1661*2d543d20SAndroid Build Coastguard Worker case CIL_ROLETYPE: {
1662*2d543d20SAndroid Build Coastguard Worker struct cil_roletype *roletype = node->data;
1663*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *role_datum = roletype->role;
1664*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *type_datum = roletype->type;
1665*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *role_node = role_datum->nodes->head->data;
1666*2d543d20SAndroid Build Coastguard Worker
1667*2d543d20SAndroid Build Coastguard Worker if (role_node->flavor == CIL_ROLEATTRIBUTE) {
1668*2d543d20SAndroid Build Coastguard Worker struct cil_roleattribute *attr = roletype->role;
1669*2d543d20SAndroid Build Coastguard Worker ebitmap_node_t *rnode;
1670*2d543d20SAndroid Build Coastguard Worker unsigned int i;
1671*2d543d20SAndroid Build Coastguard Worker
1672*2d543d20SAndroid Build Coastguard Worker ebitmap_for_each_positive_bit(attr->roles, rnode, i) {
1673*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = NULL;
1674*2d543d20SAndroid Build Coastguard Worker
1675*2d543d20SAndroid Build Coastguard Worker role = db->val_to_role[i];
1676*2d543d20SAndroid Build Coastguard Worker
1677*2d543d20SAndroid Build Coastguard Worker rc = __cil_role_assign_types(role, type_datum);
1678*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1679*2d543d20SAndroid Build Coastguard Worker goto exit;
1680*2d543d20SAndroid Build Coastguard Worker }
1681*2d543d20SAndroid Build Coastguard Worker }
1682*2d543d20SAndroid Build Coastguard Worker } else {
1683*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = roletype->role;
1684*2d543d20SAndroid Build Coastguard Worker
1685*2d543d20SAndroid Build Coastguard Worker rc = __cil_role_assign_types(role, type_datum);
1686*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1687*2d543d20SAndroid Build Coastguard Worker goto exit;
1688*2d543d20SAndroid Build Coastguard Worker }
1689*2d543d20SAndroid Build Coastguard Worker }
1690*2d543d20SAndroid Build Coastguard Worker break;
1691*2d543d20SAndroid Build Coastguard Worker }
1692*2d543d20SAndroid Build Coastguard Worker default:
1693*2d543d20SAndroid Build Coastguard Worker break;
1694*2d543d20SAndroid Build Coastguard Worker }
1695*2d543d20SAndroid Build Coastguard Worker
1696*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1697*2d543d20SAndroid Build Coastguard Worker exit:
1698*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "cil_post_db_roletype_helper failed\n");
1699*2d543d20SAndroid Build Coastguard Worker return rc;
1700*2d543d20SAndroid Build Coastguard Worker }
1701*2d543d20SAndroid Build Coastguard Worker
__cil_user_assign_roles(struct cil_user * user,struct cil_symtab_datum * datum)1702*2d543d20SAndroid Build Coastguard Worker static int __cil_user_assign_roles(struct cil_user *user, struct cil_symtab_datum *datum)
1703*2d543d20SAndroid Build Coastguard Worker {
1704*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *node = datum->nodes->head->data;
1705*2d543d20SAndroid Build Coastguard Worker struct cil_role *role = NULL;
1706*2d543d20SAndroid Build Coastguard Worker struct cil_roleattribute *attr = NULL;
1707*2d543d20SAndroid Build Coastguard Worker
1708*2d543d20SAndroid Build Coastguard Worker if (user->roles == NULL) {
1709*2d543d20SAndroid Build Coastguard Worker user->roles = cil_malloc(sizeof(*user->roles));
1710*2d543d20SAndroid Build Coastguard Worker ebitmap_init(user->roles);
1711*2d543d20SAndroid Build Coastguard Worker }
1712*2d543d20SAndroid Build Coastguard Worker
1713*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_ROLE) {
1714*2d543d20SAndroid Build Coastguard Worker role = (struct cil_role *)datum;
1715*2d543d20SAndroid Build Coastguard Worker if (ebitmap_set_bit(user->roles, role->value, 1)) {
1716*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to set bit in user roles bitmap\n");
1717*2d543d20SAndroid Build Coastguard Worker goto exit;
1718*2d543d20SAndroid Build Coastguard Worker }
1719*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_ROLEATTRIBUTE) {
1720*2d543d20SAndroid Build Coastguard Worker attr = (struct cil_roleattribute *)datum;
1721*2d543d20SAndroid Build Coastguard Worker ebitmap_union(user->roles, attr->roles);
1722*2d543d20SAndroid Build Coastguard Worker }
1723*2d543d20SAndroid Build Coastguard Worker
1724*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1725*2d543d20SAndroid Build Coastguard Worker
1726*2d543d20SAndroid Build Coastguard Worker exit:
1727*2d543d20SAndroid Build Coastguard Worker return SEPOL_ERR;
1728*2d543d20SAndroid Build Coastguard Worker }
1729*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_userrole_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1730*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_userrole_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1731*2d543d20SAndroid Build Coastguard Worker {
1732*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1733*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
1734*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = NULL;
1735*2d543d20SAndroid Build Coastguard Worker struct cil_userrole *userrole = NULL;
1736*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *user_datum = NULL;
1737*2d543d20SAndroid Build Coastguard Worker struct cil_symtab_datum *role_datum = NULL;
1738*2d543d20SAndroid Build Coastguard Worker struct cil_tree_node *user_node = NULL;
1739*2d543d20SAndroid Build Coastguard Worker struct cil_userattribute *u_attr = NULL;
1740*2d543d20SAndroid Build Coastguard Worker unsigned int i;
1741*2d543d20SAndroid Build Coastguard Worker struct cil_user *user = NULL;
1742*2d543d20SAndroid Build Coastguard Worker ebitmap_node_t *unode = NULL;
1743*2d543d20SAndroid Build Coastguard Worker
1744*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
1745*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
1746*2d543d20SAndroid Build Coastguard Worker blk = node->data;
1747*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
1748*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1749*2d543d20SAndroid Build Coastguard Worker }
1750*2d543d20SAndroid Build Coastguard Worker break;
1751*2d543d20SAndroid Build Coastguard Worker }
1752*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO: {
1753*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1754*2d543d20SAndroid Build Coastguard Worker break;
1755*2d543d20SAndroid Build Coastguard Worker }
1756*2d543d20SAndroid Build Coastguard Worker case CIL_USERROLE: {
1757*2d543d20SAndroid Build Coastguard Worker userrole = node->data;
1758*2d543d20SAndroid Build Coastguard Worker user_datum = userrole->user;
1759*2d543d20SAndroid Build Coastguard Worker role_datum = userrole->role;
1760*2d543d20SAndroid Build Coastguard Worker user_node = user_datum->nodes->head->data;
1761*2d543d20SAndroid Build Coastguard Worker
1762*2d543d20SAndroid Build Coastguard Worker if (user_node->flavor == CIL_USERATTRIBUTE) {
1763*2d543d20SAndroid Build Coastguard Worker u_attr = userrole->user;
1764*2d543d20SAndroid Build Coastguard Worker
1765*2d543d20SAndroid Build Coastguard Worker ebitmap_for_each_positive_bit(u_attr->users, unode, i) {
1766*2d543d20SAndroid Build Coastguard Worker user = db->val_to_user[i];
1767*2d543d20SAndroid Build Coastguard Worker
1768*2d543d20SAndroid Build Coastguard Worker rc = __cil_user_assign_roles(user, role_datum);
1769*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1770*2d543d20SAndroid Build Coastguard Worker goto exit;
1771*2d543d20SAndroid Build Coastguard Worker }
1772*2d543d20SAndroid Build Coastguard Worker }
1773*2d543d20SAndroid Build Coastguard Worker } else {
1774*2d543d20SAndroid Build Coastguard Worker user = userrole->user;
1775*2d543d20SAndroid Build Coastguard Worker
1776*2d543d20SAndroid Build Coastguard Worker rc = __cil_user_assign_roles(user, role_datum);
1777*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1778*2d543d20SAndroid Build Coastguard Worker goto exit;
1779*2d543d20SAndroid Build Coastguard Worker }
1780*2d543d20SAndroid Build Coastguard Worker }
1781*2d543d20SAndroid Build Coastguard Worker
1782*2d543d20SAndroid Build Coastguard Worker break;
1783*2d543d20SAndroid Build Coastguard Worker }
1784*2d543d20SAndroid Build Coastguard Worker default:
1785*2d543d20SAndroid Build Coastguard Worker break;
1786*2d543d20SAndroid Build Coastguard Worker }
1787*2d543d20SAndroid Build Coastguard Worker
1788*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1789*2d543d20SAndroid Build Coastguard Worker exit:
1790*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "cil_post_db_userrole_helper failed\n");
1791*2d543d20SAndroid Build Coastguard Worker return rc;
1792*2d543d20SAndroid Build Coastguard Worker }
1793*2d543d20SAndroid Build Coastguard Worker
__evaluate_level_expression(struct cil_level * level,struct cil_db * db)1794*2d543d20SAndroid Build Coastguard Worker static int __evaluate_level_expression(struct cil_level *level, struct cil_db *db)
1795*2d543d20SAndroid Build Coastguard Worker {
1796*2d543d20SAndroid Build Coastguard Worker if (level->cats != NULL) {
1797*2d543d20SAndroid Build Coastguard Worker return __evaluate_cat_expression(level->cats, db);
1798*2d543d20SAndroid Build Coastguard Worker }
1799*2d543d20SAndroid Build Coastguard Worker
1800*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
1801*2d543d20SAndroid Build Coastguard Worker }
1802*2d543d20SAndroid Build Coastguard Worker
__evaluate_levelrange_expression(struct cil_levelrange * levelrange,struct cil_db * db)1803*2d543d20SAndroid Build Coastguard Worker static int __evaluate_levelrange_expression(struct cil_levelrange *levelrange, struct cil_db *db)
1804*2d543d20SAndroid Build Coastguard Worker {
1805*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_OK;
1806*2d543d20SAndroid Build Coastguard Worker
1807*2d543d20SAndroid Build Coastguard Worker if (levelrange->low != NULL && levelrange->low->cats != NULL) {
1808*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_cat_expression(levelrange->low->cats, db);
1809*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1810*2d543d20SAndroid Build Coastguard Worker goto exit;
1811*2d543d20SAndroid Build Coastguard Worker }
1812*2d543d20SAndroid Build Coastguard Worker }
1813*2d543d20SAndroid Build Coastguard Worker if (levelrange->high != NULL && levelrange->high->cats != NULL) {
1814*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_cat_expression(levelrange->high->cats, db);
1815*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1816*2d543d20SAndroid Build Coastguard Worker goto exit;
1817*2d543d20SAndroid Build Coastguard Worker }
1818*2d543d20SAndroid Build Coastguard Worker }
1819*2d543d20SAndroid Build Coastguard Worker
1820*2d543d20SAndroid Build Coastguard Worker exit:
1821*2d543d20SAndroid Build Coastguard Worker return rc;
1822*2d543d20SAndroid Build Coastguard Worker }
1823*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_cat_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1824*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_cat_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1825*2d543d20SAndroid Build Coastguard Worker {
1826*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
1827*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
1828*2d543d20SAndroid Build Coastguard Worker
1829*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
1830*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
1831*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
1832*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
1833*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1834*2d543d20SAndroid Build Coastguard Worker }
1835*2d543d20SAndroid Build Coastguard Worker break;
1836*2d543d20SAndroid Build Coastguard Worker }
1837*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO: {
1838*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
1839*2d543d20SAndroid Build Coastguard Worker break;
1840*2d543d20SAndroid Build Coastguard Worker }
1841*2d543d20SAndroid Build Coastguard Worker case CIL_CATSET: {
1842*2d543d20SAndroid Build Coastguard Worker struct cil_catset *catset = node->data;
1843*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_cat_expression(catset->cats, db);
1844*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1845*2d543d20SAndroid Build Coastguard Worker goto exit;
1846*2d543d20SAndroid Build Coastguard Worker }
1847*2d543d20SAndroid Build Coastguard Worker break;
1848*2d543d20SAndroid Build Coastguard Worker }
1849*2d543d20SAndroid Build Coastguard Worker case CIL_SENSCAT: {
1850*2d543d20SAndroid Build Coastguard Worker struct cil_senscat *senscat = node->data;
1851*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_cat_expression(senscat->cats, db);
1852*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1853*2d543d20SAndroid Build Coastguard Worker goto exit;
1854*2d543d20SAndroid Build Coastguard Worker }
1855*2d543d20SAndroid Build Coastguard Worker break;
1856*2d543d20SAndroid Build Coastguard Worker }
1857*2d543d20SAndroid Build Coastguard Worker case CIL_LEVEL: {
1858*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_level_expression(node->data, db);
1859*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1860*2d543d20SAndroid Build Coastguard Worker goto exit;
1861*2d543d20SAndroid Build Coastguard Worker }
1862*2d543d20SAndroid Build Coastguard Worker break;
1863*2d543d20SAndroid Build Coastguard Worker }
1864*2d543d20SAndroid Build Coastguard Worker case CIL_LEVELRANGE: {
1865*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(node->data, db);
1866*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1867*2d543d20SAndroid Build Coastguard Worker goto exit;
1868*2d543d20SAndroid Build Coastguard Worker }
1869*2d543d20SAndroid Build Coastguard Worker break;
1870*2d543d20SAndroid Build Coastguard Worker }
1871*2d543d20SAndroid Build Coastguard Worker case CIL_USER: {
1872*2d543d20SAndroid Build Coastguard Worker struct cil_user *user = node->data;
1873*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_level_expression(user->dftlevel, db);
1874*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1875*2d543d20SAndroid Build Coastguard Worker goto exit;
1876*2d543d20SAndroid Build Coastguard Worker }
1877*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(user->range, db);
1878*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1879*2d543d20SAndroid Build Coastguard Worker goto exit;
1880*2d543d20SAndroid Build Coastguard Worker }
1881*2d543d20SAndroid Build Coastguard Worker break;
1882*2d543d20SAndroid Build Coastguard Worker }
1883*2d543d20SAndroid Build Coastguard Worker case CIL_SELINUXUSERDEFAULT:
1884*2d543d20SAndroid Build Coastguard Worker case CIL_SELINUXUSER: {
1885*2d543d20SAndroid Build Coastguard Worker struct cil_selinuxuser *selinuxuser = node->data;
1886*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(selinuxuser->range, db);
1887*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1888*2d543d20SAndroid Build Coastguard Worker goto exit;
1889*2d543d20SAndroid Build Coastguard Worker }
1890*2d543d20SAndroid Build Coastguard Worker break;
1891*2d543d20SAndroid Build Coastguard Worker }
1892*2d543d20SAndroid Build Coastguard Worker case CIL_RANGETRANSITION: {
1893*2d543d20SAndroid Build Coastguard Worker struct cil_rangetransition *rangetrans = node->data;
1894*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(rangetrans->range, db);
1895*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1896*2d543d20SAndroid Build Coastguard Worker goto exit;
1897*2d543d20SAndroid Build Coastguard Worker }
1898*2d543d20SAndroid Build Coastguard Worker break;
1899*2d543d20SAndroid Build Coastguard Worker }
1900*2d543d20SAndroid Build Coastguard Worker case CIL_CONTEXT: {
1901*2d543d20SAndroid Build Coastguard Worker struct cil_context *context = node->data;
1902*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(context->range, db);
1903*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1904*2d543d20SAndroid Build Coastguard Worker goto exit;
1905*2d543d20SAndroid Build Coastguard Worker }
1906*2d543d20SAndroid Build Coastguard Worker break;
1907*2d543d20SAndroid Build Coastguard Worker }
1908*2d543d20SAndroid Build Coastguard Worker case CIL_SIDCONTEXT: {
1909*2d543d20SAndroid Build Coastguard Worker struct cil_sidcontext *sidcontext = node->data;
1910*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(sidcontext->context->range, db);
1911*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1912*2d543d20SAndroid Build Coastguard Worker goto exit;
1913*2d543d20SAndroid Build Coastguard Worker }
1914*2d543d20SAndroid Build Coastguard Worker break;
1915*2d543d20SAndroid Build Coastguard Worker }
1916*2d543d20SAndroid Build Coastguard Worker case CIL_FILECON: {
1917*2d543d20SAndroid Build Coastguard Worker struct cil_filecon *filecon = node->data;
1918*2d543d20SAndroid Build Coastguard Worker if (filecon->context) {
1919*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(filecon->context->range, db);
1920*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1921*2d543d20SAndroid Build Coastguard Worker goto exit;
1922*2d543d20SAndroid Build Coastguard Worker }
1923*2d543d20SAndroid Build Coastguard Worker }
1924*2d543d20SAndroid Build Coastguard Worker break;
1925*2d543d20SAndroid Build Coastguard Worker }
1926*2d543d20SAndroid Build Coastguard Worker case CIL_IBPKEYCON: {
1927*2d543d20SAndroid Build Coastguard Worker struct cil_ibpkeycon *ibpkeycon = node->data;
1928*2d543d20SAndroid Build Coastguard Worker
1929*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(ibpkeycon->context->range, db);
1930*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK)
1931*2d543d20SAndroid Build Coastguard Worker goto exit;
1932*2d543d20SAndroid Build Coastguard Worker break;
1933*2d543d20SAndroid Build Coastguard Worker }
1934*2d543d20SAndroid Build Coastguard Worker case CIL_IBENDPORTCON: {
1935*2d543d20SAndroid Build Coastguard Worker struct cil_ibendportcon *ibendportcon = node->data;
1936*2d543d20SAndroid Build Coastguard Worker
1937*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(ibendportcon->context->range, db);
1938*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK)
1939*2d543d20SAndroid Build Coastguard Worker goto exit;
1940*2d543d20SAndroid Build Coastguard Worker break;
1941*2d543d20SAndroid Build Coastguard Worker }
1942*2d543d20SAndroid Build Coastguard Worker case CIL_PORTCON: {
1943*2d543d20SAndroid Build Coastguard Worker struct cil_portcon *portcon = node->data;
1944*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(portcon->context->range, db);
1945*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1946*2d543d20SAndroid Build Coastguard Worker goto exit;
1947*2d543d20SAndroid Build Coastguard Worker }
1948*2d543d20SAndroid Build Coastguard Worker break;
1949*2d543d20SAndroid Build Coastguard Worker }
1950*2d543d20SAndroid Build Coastguard Worker case CIL_NODECON: {
1951*2d543d20SAndroid Build Coastguard Worker struct cil_nodecon *nodecon = node->data;
1952*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(nodecon->context->range, db);
1953*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1954*2d543d20SAndroid Build Coastguard Worker goto exit;
1955*2d543d20SAndroid Build Coastguard Worker }
1956*2d543d20SAndroid Build Coastguard Worker break;
1957*2d543d20SAndroid Build Coastguard Worker }
1958*2d543d20SAndroid Build Coastguard Worker case CIL_GENFSCON: {
1959*2d543d20SAndroid Build Coastguard Worker struct cil_genfscon *genfscon = node->data;
1960*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(genfscon->context->range, db);
1961*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1962*2d543d20SAndroid Build Coastguard Worker goto exit;
1963*2d543d20SAndroid Build Coastguard Worker }
1964*2d543d20SAndroid Build Coastguard Worker break;
1965*2d543d20SAndroid Build Coastguard Worker }
1966*2d543d20SAndroid Build Coastguard Worker case CIL_NETIFCON: {
1967*2d543d20SAndroid Build Coastguard Worker struct cil_netifcon *netifcon = node->data;
1968*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(netifcon->if_context->range, db);
1969*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1970*2d543d20SAndroid Build Coastguard Worker goto exit;
1971*2d543d20SAndroid Build Coastguard Worker }
1972*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(netifcon->packet_context->range, db);
1973*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1974*2d543d20SAndroid Build Coastguard Worker goto exit;
1975*2d543d20SAndroid Build Coastguard Worker }
1976*2d543d20SAndroid Build Coastguard Worker break;
1977*2d543d20SAndroid Build Coastguard Worker }
1978*2d543d20SAndroid Build Coastguard Worker case CIL_PIRQCON: {
1979*2d543d20SAndroid Build Coastguard Worker struct cil_pirqcon *pirqcon = node->data;
1980*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(pirqcon->context->range, db);
1981*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1982*2d543d20SAndroid Build Coastguard Worker goto exit;
1983*2d543d20SAndroid Build Coastguard Worker }
1984*2d543d20SAndroid Build Coastguard Worker break;
1985*2d543d20SAndroid Build Coastguard Worker }
1986*2d543d20SAndroid Build Coastguard Worker case CIL_IOMEMCON: {
1987*2d543d20SAndroid Build Coastguard Worker struct cil_iomemcon *iomemcon = node->data;
1988*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(iomemcon->context->range, db);
1989*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1990*2d543d20SAndroid Build Coastguard Worker goto exit;
1991*2d543d20SAndroid Build Coastguard Worker }
1992*2d543d20SAndroid Build Coastguard Worker break;
1993*2d543d20SAndroid Build Coastguard Worker }
1994*2d543d20SAndroid Build Coastguard Worker case CIL_IOPORTCON: {
1995*2d543d20SAndroid Build Coastguard Worker struct cil_ioportcon *ioportcon = node->data;
1996*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(ioportcon->context->range, db);
1997*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
1998*2d543d20SAndroid Build Coastguard Worker goto exit;
1999*2d543d20SAndroid Build Coastguard Worker }
2000*2d543d20SAndroid Build Coastguard Worker break;
2001*2d543d20SAndroid Build Coastguard Worker }
2002*2d543d20SAndroid Build Coastguard Worker case CIL_PCIDEVICECON: {
2003*2d543d20SAndroid Build Coastguard Worker struct cil_pcidevicecon *pcidevicecon = node->data;
2004*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(pcidevicecon->context->range, db);
2005*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2006*2d543d20SAndroid Build Coastguard Worker goto exit;
2007*2d543d20SAndroid Build Coastguard Worker }
2008*2d543d20SAndroid Build Coastguard Worker break;
2009*2d543d20SAndroid Build Coastguard Worker }
2010*2d543d20SAndroid Build Coastguard Worker case CIL_DEVICETREECON: {
2011*2d543d20SAndroid Build Coastguard Worker struct cil_devicetreecon *devicetreecon = node->data;
2012*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(devicetreecon->context->range, db);
2013*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2014*2d543d20SAndroid Build Coastguard Worker goto exit;
2015*2d543d20SAndroid Build Coastguard Worker }
2016*2d543d20SAndroid Build Coastguard Worker break;
2017*2d543d20SAndroid Build Coastguard Worker }
2018*2d543d20SAndroid Build Coastguard Worker case CIL_FSUSE: {
2019*2d543d20SAndroid Build Coastguard Worker struct cil_fsuse *fsuse = node->data;
2020*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_levelrange_expression(fsuse->context->range, db);
2021*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2022*2d543d20SAndroid Build Coastguard Worker goto exit;
2023*2d543d20SAndroid Build Coastguard Worker }
2024*2d543d20SAndroid Build Coastguard Worker break;
2025*2d543d20SAndroid Build Coastguard Worker }
2026*2d543d20SAndroid Build Coastguard Worker default:
2027*2d543d20SAndroid Build Coastguard Worker break;
2028*2d543d20SAndroid Build Coastguard Worker }
2029*2d543d20SAndroid Build Coastguard Worker
2030*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2031*2d543d20SAndroid Build Coastguard Worker
2032*2d543d20SAndroid Build Coastguard Worker exit:
2033*2d543d20SAndroid Build Coastguard Worker return rc;
2034*2d543d20SAndroid Build Coastguard Worker }
2035*2d543d20SAndroid Build Coastguard Worker
2036*2d543d20SAndroid Build Coastguard Worker struct perm_to_list {
2037*2d543d20SAndroid Build Coastguard Worker enum cil_flavor flavor;
2038*2d543d20SAndroid Build Coastguard Worker ebitmap_t *perms;
2039*2d543d20SAndroid Build Coastguard Worker struct cil_list *new_list;
2040*2d543d20SAndroid Build Coastguard Worker };
2041*2d543d20SAndroid Build Coastguard Worker
__perm_bits_to_list(hashtab_key_t k,hashtab_datum_t d,void * args)2042*2d543d20SAndroid Build Coastguard Worker static int __perm_bits_to_list(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2043*2d543d20SAndroid Build Coastguard Worker {
2044*2d543d20SAndroid Build Coastguard Worker struct perm_to_list *perm_args = (struct perm_to_list *)args;
2045*2d543d20SAndroid Build Coastguard Worker ebitmap_t *perms = perm_args->perms;
2046*2d543d20SAndroid Build Coastguard Worker struct cil_list *new_list = perm_args->new_list;
2047*2d543d20SAndroid Build Coastguard Worker struct cil_perm *perm = (struct cil_perm *)d;
2048*2d543d20SAndroid Build Coastguard Worker unsigned int value = perm->value;
2049*2d543d20SAndroid Build Coastguard Worker
2050*2d543d20SAndroid Build Coastguard Worker if (!ebitmap_get_bit(perms, value)) {
2051*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2052*2d543d20SAndroid Build Coastguard Worker }
2053*2d543d20SAndroid Build Coastguard Worker
2054*2d543d20SAndroid Build Coastguard Worker cil_list_append(new_list, CIL_DATUM, d);
2055*2d543d20SAndroid Build Coastguard Worker
2056*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2057*2d543d20SAndroid Build Coastguard Worker }
2058*2d543d20SAndroid Build Coastguard Worker
__evaluate_perm_expression(struct cil_list * perms,enum cil_flavor flavor,symtab_t * class_symtab,symtab_t * common_symtab,unsigned int num_perms,struct cil_list ** new_list,struct cil_db * db)2059*2d543d20SAndroid Build Coastguard Worker static int __evaluate_perm_expression(struct cil_list *perms, enum cil_flavor flavor, symtab_t *class_symtab, symtab_t *common_symtab, unsigned int num_perms, struct cil_list **new_list, struct cil_db *db)
2060*2d543d20SAndroid Build Coastguard Worker {
2061*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2062*2d543d20SAndroid Build Coastguard Worker struct perm_to_list args;
2063*2d543d20SAndroid Build Coastguard Worker ebitmap_t bitmap;
2064*2d543d20SAndroid Build Coastguard Worker
2065*2d543d20SAndroid Build Coastguard Worker if (cil_verify_is_list(perms, CIL_PERM)) {
2066*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2067*2d543d20SAndroid Build Coastguard Worker }
2068*2d543d20SAndroid Build Coastguard Worker
2069*2d543d20SAndroid Build Coastguard Worker ebitmap_init(&bitmap);
2070*2d543d20SAndroid Build Coastguard Worker rc = __cil_expr_to_bitmap(perms, &bitmap, num_perms, db);
2071*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2072*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
2073*2d543d20SAndroid Build Coastguard Worker goto exit;
2074*2d543d20SAndroid Build Coastguard Worker }
2075*2d543d20SAndroid Build Coastguard Worker
2076*2d543d20SAndroid Build Coastguard Worker cil_list_init(new_list, flavor);
2077*2d543d20SAndroid Build Coastguard Worker
2078*2d543d20SAndroid Build Coastguard Worker args.flavor = flavor;
2079*2d543d20SAndroid Build Coastguard Worker args.perms = &bitmap;
2080*2d543d20SAndroid Build Coastguard Worker args.new_list = *new_list;
2081*2d543d20SAndroid Build Coastguard Worker
2082*2d543d20SAndroid Build Coastguard Worker cil_symtab_map(class_symtab, __perm_bits_to_list, &args);
2083*2d543d20SAndroid Build Coastguard Worker
2084*2d543d20SAndroid Build Coastguard Worker if (common_symtab != NULL) {
2085*2d543d20SAndroid Build Coastguard Worker cil_symtab_map(common_symtab, __perm_bits_to_list, &args);
2086*2d543d20SAndroid Build Coastguard Worker }
2087*2d543d20SAndroid Build Coastguard Worker
2088*2d543d20SAndroid Build Coastguard Worker ebitmap_destroy(&bitmap);
2089*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2090*2d543d20SAndroid Build Coastguard Worker
2091*2d543d20SAndroid Build Coastguard Worker exit:
2092*2d543d20SAndroid Build Coastguard Worker return rc;
2093*2d543d20SAndroid Build Coastguard Worker }
2094*2d543d20SAndroid Build Coastguard Worker
__evaluate_classperms(struct cil_classperms * cp,struct cil_db * db)2095*2d543d20SAndroid Build Coastguard Worker static int __evaluate_classperms(struct cil_classperms *cp, struct cil_db *db)
2096*2d543d20SAndroid Build Coastguard Worker {
2097*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2098*2d543d20SAndroid Build Coastguard Worker struct cil_class *class = cp->class;
2099*2d543d20SAndroid Build Coastguard Worker struct cil_class *common = class->common;
2100*2d543d20SAndroid Build Coastguard Worker symtab_t *common_symtab = NULL;
2101*2d543d20SAndroid Build Coastguard Worker struct cil_list *new_list = NULL;
2102*2d543d20SAndroid Build Coastguard Worker
2103*2d543d20SAndroid Build Coastguard Worker if (common) {
2104*2d543d20SAndroid Build Coastguard Worker common_symtab = &common->perms;
2105*2d543d20SAndroid Build Coastguard Worker }
2106*2d543d20SAndroid Build Coastguard Worker
2107*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_perm_expression(cp->perms, CIL_PERM, &class->perms, common_symtab, class->num_perms, &new_list, db);
2108*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2109*2d543d20SAndroid Build Coastguard Worker goto exit;
2110*2d543d20SAndroid Build Coastguard Worker }
2111*2d543d20SAndroid Build Coastguard Worker
2112*2d543d20SAndroid Build Coastguard Worker if (new_list == NULL) {
2113*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2114*2d543d20SAndroid Build Coastguard Worker }
2115*2d543d20SAndroid Build Coastguard Worker
2116*2d543d20SAndroid Build Coastguard Worker cil_list_destroy(&cp->perms, CIL_FALSE);
2117*2d543d20SAndroid Build Coastguard Worker
2118*2d543d20SAndroid Build Coastguard Worker cp->perms = new_list;
2119*2d543d20SAndroid Build Coastguard Worker
2120*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2121*2d543d20SAndroid Build Coastguard Worker
2122*2d543d20SAndroid Build Coastguard Worker exit:
2123*2d543d20SAndroid Build Coastguard Worker return rc;
2124*2d543d20SAndroid Build Coastguard Worker }
2125*2d543d20SAndroid Build Coastguard Worker
__evaluate_classperms_list(struct cil_list * classperms,struct cil_db * db)2126*2d543d20SAndroid Build Coastguard Worker static int __evaluate_classperms_list(struct cil_list *classperms, struct cil_db *db)
2127*2d543d20SAndroid Build Coastguard Worker {
2128*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2129*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *curr;
2130*2d543d20SAndroid Build Coastguard Worker
2131*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(curr, classperms) {
2132*2d543d20SAndroid Build Coastguard Worker if (curr->flavor == CIL_CLASSPERMS) {
2133*2d543d20SAndroid Build Coastguard Worker struct cil_classperms *cp = curr->data;
2134*2d543d20SAndroid Build Coastguard Worker if (FLAVOR(cp->class) == CIL_CLASS) {
2135*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms(cp, db);
2136*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2137*2d543d20SAndroid Build Coastguard Worker goto exit;
2138*2d543d20SAndroid Build Coastguard Worker }
2139*2d543d20SAndroid Build Coastguard Worker } else { /* MAP */
2140*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *i = NULL;
2141*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms(cp, db);
2142*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2143*2d543d20SAndroid Build Coastguard Worker goto exit;
2144*2d543d20SAndroid Build Coastguard Worker }
2145*2d543d20SAndroid Build Coastguard Worker cil_list_for_each(i, cp->perms) {
2146*2d543d20SAndroid Build Coastguard Worker struct cil_perm *cmp = i->data;
2147*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms_list(cmp->classperms, db);
2148*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2149*2d543d20SAndroid Build Coastguard Worker goto exit;
2150*2d543d20SAndroid Build Coastguard Worker }
2151*2d543d20SAndroid Build Coastguard Worker }
2152*2d543d20SAndroid Build Coastguard Worker }
2153*2d543d20SAndroid Build Coastguard Worker } else { /* SET */
2154*2d543d20SAndroid Build Coastguard Worker struct cil_classperms_set *cp_set = curr->data;
2155*2d543d20SAndroid Build Coastguard Worker struct cil_classpermission *cp = cp_set->set;
2156*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms_list(cp->classperms, db);
2157*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2158*2d543d20SAndroid Build Coastguard Worker goto exit;
2159*2d543d20SAndroid Build Coastguard Worker }
2160*2d543d20SAndroid Build Coastguard Worker }
2161*2d543d20SAndroid Build Coastguard Worker }
2162*2d543d20SAndroid Build Coastguard Worker
2163*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2164*2d543d20SAndroid Build Coastguard Worker
2165*2d543d20SAndroid Build Coastguard Worker exit:
2166*2d543d20SAndroid Build Coastguard Worker return rc;
2167*2d543d20SAndroid Build Coastguard Worker }
2168*2d543d20SAndroid Build Coastguard Worker
2169*2d543d20SAndroid Build Coastguard Worker struct class_map_args {
2170*2d543d20SAndroid Build Coastguard Worker struct cil_db *db;
2171*2d543d20SAndroid Build Coastguard Worker int rc;
2172*2d543d20SAndroid Build Coastguard Worker };
2173*2d543d20SAndroid Build Coastguard Worker
__evaluate_map_perm_classperms(hashtab_key_t k,hashtab_datum_t d,void * args)2174*2d543d20SAndroid Build Coastguard Worker static int __evaluate_map_perm_classperms(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2175*2d543d20SAndroid Build Coastguard Worker {
2176*2d543d20SAndroid Build Coastguard Worker struct class_map_args *map_args = args;
2177*2d543d20SAndroid Build Coastguard Worker struct cil_perm *cmp = (struct cil_perm *)d;
2178*2d543d20SAndroid Build Coastguard Worker
2179*2d543d20SAndroid Build Coastguard Worker int rc = __evaluate_classperms_list(cmp->classperms, map_args->db);
2180*2d543d20SAndroid Build Coastguard Worker
2181*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2182*2d543d20SAndroid Build Coastguard Worker map_args->rc = rc;
2183*2d543d20SAndroid Build Coastguard Worker }
2184*2d543d20SAndroid Build Coastguard Worker
2185*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2186*2d543d20SAndroid Build Coastguard Worker }
2187*2d543d20SAndroid Build Coastguard Worker
__evaluate_map_class(struct cil_class * mc,struct cil_db * db)2188*2d543d20SAndroid Build Coastguard Worker static int __evaluate_map_class(struct cil_class *mc, struct cil_db *db)
2189*2d543d20SAndroid Build Coastguard Worker {
2190*2d543d20SAndroid Build Coastguard Worker struct class_map_args map_args;
2191*2d543d20SAndroid Build Coastguard Worker
2192*2d543d20SAndroid Build Coastguard Worker map_args.db = db;
2193*2d543d20SAndroid Build Coastguard Worker map_args.rc = SEPOL_OK;
2194*2d543d20SAndroid Build Coastguard Worker cil_symtab_map(&mc->perms, __evaluate_map_perm_classperms, &map_args);
2195*2d543d20SAndroid Build Coastguard Worker
2196*2d543d20SAndroid Build Coastguard Worker return map_args.rc;
2197*2d543d20SAndroid Build Coastguard Worker }
2198*2d543d20SAndroid Build Coastguard Worker
__cil_post_db_classperms_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)2199*2d543d20SAndroid Build Coastguard Worker static int __cil_post_db_classperms_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2200*2d543d20SAndroid Build Coastguard Worker {
2201*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2202*2d543d20SAndroid Build Coastguard Worker struct cil_db *db = extra_args;
2203*2d543d20SAndroid Build Coastguard Worker
2204*2d543d20SAndroid Build Coastguard Worker switch (node->flavor) {
2205*2d543d20SAndroid Build Coastguard Worker case CIL_BLOCK: {
2206*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
2207*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
2208*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
2209*2d543d20SAndroid Build Coastguard Worker }
2210*2d543d20SAndroid Build Coastguard Worker break;
2211*2d543d20SAndroid Build Coastguard Worker }
2212*2d543d20SAndroid Build Coastguard Worker case CIL_MACRO:
2213*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
2214*2d543d20SAndroid Build Coastguard Worker break;
2215*2d543d20SAndroid Build Coastguard Worker case CIL_MAP_CLASS: {
2216*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_map_class(node->data, db);
2217*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2218*2d543d20SAndroid Build Coastguard Worker goto exit;
2219*2d543d20SAndroid Build Coastguard Worker }
2220*2d543d20SAndroid Build Coastguard Worker break;
2221*2d543d20SAndroid Build Coastguard Worker }
2222*2d543d20SAndroid Build Coastguard Worker case CIL_CLASSPERMISSION: {
2223*2d543d20SAndroid Build Coastguard Worker struct cil_classpermission *cp = node->data;
2224*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms_list(cp->classperms, db);
2225*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2226*2d543d20SAndroid Build Coastguard Worker goto exit;
2227*2d543d20SAndroid Build Coastguard Worker }
2228*2d543d20SAndroid Build Coastguard Worker break;
2229*2d543d20SAndroid Build Coastguard Worker }
2230*2d543d20SAndroid Build Coastguard Worker case CIL_AVRULE: {
2231*2d543d20SAndroid Build Coastguard Worker struct cil_avrule *avrule = node->data;
2232*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms_list(avrule->perms.classperms, db);
2233*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2234*2d543d20SAndroid Build Coastguard Worker goto exit;
2235*2d543d20SAndroid Build Coastguard Worker }
2236*2d543d20SAndroid Build Coastguard Worker break;
2237*2d543d20SAndroid Build Coastguard Worker }
2238*2d543d20SAndroid Build Coastguard Worker case CIL_CONSTRAIN:
2239*2d543d20SAndroid Build Coastguard Worker case CIL_MLSCONSTRAIN: {
2240*2d543d20SAndroid Build Coastguard Worker struct cil_constrain *constrain = node->data;
2241*2d543d20SAndroid Build Coastguard Worker rc = __evaluate_classperms_list(constrain->classperms, db);
2242*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2243*2d543d20SAndroid Build Coastguard Worker goto exit;
2244*2d543d20SAndroid Build Coastguard Worker }
2245*2d543d20SAndroid Build Coastguard Worker break;
2246*2d543d20SAndroid Build Coastguard Worker }
2247*2d543d20SAndroid Build Coastguard Worker default:
2248*2d543d20SAndroid Build Coastguard Worker break;
2249*2d543d20SAndroid Build Coastguard Worker }
2250*2d543d20SAndroid Build Coastguard Worker
2251*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2252*2d543d20SAndroid Build Coastguard Worker
2253*2d543d20SAndroid Build Coastguard Worker exit:
2254*2d543d20SAndroid Build Coastguard Worker return rc;
2255*2d543d20SAndroid Build Coastguard Worker }
2256*2d543d20SAndroid Build Coastguard Worker
__cil_post_report_conflict(struct cil_tree_node * node,uint32_t * finished,void * extra_args)2257*2d543d20SAndroid Build Coastguard Worker static int __cil_post_report_conflict(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2258*2d543d20SAndroid Build Coastguard Worker {
2259*2d543d20SAndroid Build Coastguard Worker struct cil_list_item *li = extra_args;
2260*2d543d20SAndroid Build Coastguard Worker
2261*2d543d20SAndroid Build Coastguard Worker if (node->flavor == CIL_BLOCK) {
2262*2d543d20SAndroid Build Coastguard Worker struct cil_block *blk = node->data;
2263*2d543d20SAndroid Build Coastguard Worker if (blk->is_abstract == CIL_TRUE) {
2264*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
2265*2d543d20SAndroid Build Coastguard Worker }
2266*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == CIL_MACRO) {
2267*2d543d20SAndroid Build Coastguard Worker *finished = CIL_TREE_SKIP_HEAD;
2268*2d543d20SAndroid Build Coastguard Worker } else if (node->flavor == li->flavor) {
2269*2d543d20SAndroid Build Coastguard Worker if (node->data == li->data) {
2270*2d543d20SAndroid Build Coastguard Worker char *path = cil_tree_get_cil_path(node);
2271*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_WARN, " at %s:%d\n", path, node->line);
2272*2d543d20SAndroid Build Coastguard Worker }
2273*2d543d20SAndroid Build Coastguard Worker }
2274*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2275*2d543d20SAndroid Build Coastguard Worker }
2276*2d543d20SAndroid Build Coastguard Worker
__cil_post_process_context_rules(struct cil_sort * sort,int (* compar)(const void *,const void *),int (* concompar)(const void *,const void *),struct cil_db * db,enum cil_flavor flavor,const char * flavor_str)2277*2d543d20SAndroid Build Coastguard Worker static int __cil_post_process_context_rules(struct cil_sort *sort, int (*compar)(const void *, const void *), int (*concompar)(const void *, const void *), struct cil_db *db, enum cil_flavor flavor, const char *flavor_str)
2278*2d543d20SAndroid Build Coastguard Worker {
2279*2d543d20SAndroid Build Coastguard Worker uint32_t count = sort->count;
2280*2d543d20SAndroid Build Coastguard Worker uint32_t i = 0, j, removed = 0;
2281*2d543d20SAndroid Build Coastguard Worker int conflicting = 0;
2282*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_OK;
2283*2d543d20SAndroid Build Coastguard Worker enum cil_log_level log_level = cil_get_log_level();
2284*2d543d20SAndroid Build Coastguard Worker
2285*2d543d20SAndroid Build Coastguard Worker if (count < 2) {
2286*2d543d20SAndroid Build Coastguard Worker return SEPOL_OK;
2287*2d543d20SAndroid Build Coastguard Worker }
2288*2d543d20SAndroid Build Coastguard Worker
2289*2d543d20SAndroid Build Coastguard Worker qsort(sort->array, sort->count, sizeof(sort->array), compar);
2290*2d543d20SAndroid Build Coastguard Worker
2291*2d543d20SAndroid Build Coastguard Worker for (j=1; j<count; j++) {
2292*2d543d20SAndroid Build Coastguard Worker if (compar(&sort->array[i], &sort->array[j]) != 0) {
2293*2d543d20SAndroid Build Coastguard Worker i++;
2294*2d543d20SAndroid Build Coastguard Worker if (conflicting >= 4) {
2295*2d543d20SAndroid Build Coastguard Worker /* 2 rules were written when conflicting == 1 */
2296*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_WARN, " Only first 4 of %d conflicting rules shown\n", conflicting);
2297*2d543d20SAndroid Build Coastguard Worker }
2298*2d543d20SAndroid Build Coastguard Worker conflicting = 0;
2299*2d543d20SAndroid Build Coastguard Worker } else {
2300*2d543d20SAndroid Build Coastguard Worker removed++;
2301*2d543d20SAndroid Build Coastguard Worker if (!db->multiple_decls || concompar(&sort->array[i], &sort->array[j]) != 0) {
2302*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
2303*2d543d20SAndroid Build Coastguard Worker conflicting++;
2304*2d543d20SAndroid Build Coastguard Worker if (log_level >= CIL_WARN) {
2305*2d543d20SAndroid Build Coastguard Worker struct cil_list_item li;
2306*2d543d20SAndroid Build Coastguard Worker int rc2;
2307*2d543d20SAndroid Build Coastguard Worker li.flavor = flavor;
2308*2d543d20SAndroid Build Coastguard Worker if (conflicting == 1) {
2309*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_WARN, "Found conflicting %s rules\n", flavor_str);
2310*2d543d20SAndroid Build Coastguard Worker li.data = sort->array[i];
2311*2d543d20SAndroid Build Coastguard Worker rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2312*2d543d20SAndroid Build Coastguard Worker NULL, NULL, &li);
2313*2d543d20SAndroid Build Coastguard Worker if (rc2 != SEPOL_OK) goto exit;
2314*2d543d20SAndroid Build Coastguard Worker }
2315*2d543d20SAndroid Build Coastguard Worker if (conflicting < 4 || log_level > CIL_WARN) {
2316*2d543d20SAndroid Build Coastguard Worker li.data = sort->array[j];
2317*2d543d20SAndroid Build Coastguard Worker rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2318*2d543d20SAndroid Build Coastguard Worker NULL, NULL, &li);
2319*2d543d20SAndroid Build Coastguard Worker if (rc2 != SEPOL_OK) goto exit;
2320*2d543d20SAndroid Build Coastguard Worker }
2321*2d543d20SAndroid Build Coastguard Worker }
2322*2d543d20SAndroid Build Coastguard Worker }
2323*2d543d20SAndroid Build Coastguard Worker }
2324*2d543d20SAndroid Build Coastguard Worker if (i != j && !conflicting) {
2325*2d543d20SAndroid Build Coastguard Worker sort->array[i] = sort->array[j];
2326*2d543d20SAndroid Build Coastguard Worker }
2327*2d543d20SAndroid Build Coastguard Worker }
2328*2d543d20SAndroid Build Coastguard Worker sort->count = count - removed;
2329*2d543d20SAndroid Build Coastguard Worker
2330*2d543d20SAndroid Build Coastguard Worker exit:
2331*2d543d20SAndroid Build Coastguard Worker return rc;
2332*2d543d20SAndroid Build Coastguard Worker }
2333*2d543d20SAndroid Build Coastguard Worker
cil_post_db(struct cil_db * db)2334*2d543d20SAndroid Build Coastguard Worker static int cil_post_db(struct cil_db *db)
2335*2d543d20SAndroid Build Coastguard Worker {
2336*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2337*2d543d20SAndroid Build Coastguard Worker
2338*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_count_helper, NULL, NULL, db);
2339*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2340*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failure during cil database count helper\n");
2341*2d543d20SAndroid Build Coastguard Worker goto exit;
2342*2d543d20SAndroid Build Coastguard Worker }
2343*2d543d20SAndroid Build Coastguard Worker
2344*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_array_helper, NULL, NULL, db);
2345*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2346*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failure during cil database array helper\n");
2347*2d543d20SAndroid Build Coastguard Worker goto exit;
2348*2d543d20SAndroid Build Coastguard Worker }
2349*2d543d20SAndroid Build Coastguard Worker
2350*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_neverallow_attr_helper, NULL, NULL, db);
2351*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2352*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to mark attributes used by generated attributes used in neverallow rules\n");
2353*2d543d20SAndroid Build Coastguard Worker goto exit;
2354*2d543d20SAndroid Build Coastguard Worker }
2355*2d543d20SAndroid Build Coastguard Worker
2356*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_attr_helper, NULL, NULL, db);
2357*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2358*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to create attribute bitmaps\n");
2359*2d543d20SAndroid Build Coastguard Worker goto exit;
2360*2d543d20SAndroid Build Coastguard Worker }
2361*2d543d20SAndroid Build Coastguard Worker
2362*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_roletype_helper, NULL, NULL, db);
2363*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2364*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed during roletype association\n");
2365*2d543d20SAndroid Build Coastguard Worker goto exit;
2366*2d543d20SAndroid Build Coastguard Worker }
2367*2d543d20SAndroid Build Coastguard Worker
2368*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_userrole_helper, NULL, NULL, db);
2369*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2370*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed during userrole association\n");
2371*2d543d20SAndroid Build Coastguard Worker goto exit;
2372*2d543d20SAndroid Build Coastguard Worker }
2373*2d543d20SAndroid Build Coastguard Worker
2374*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_classperms_helper, NULL, NULL, db);
2375*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2376*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to evaluate class mapping permissions expressions\n");
2377*2d543d20SAndroid Build Coastguard Worker goto exit;
2378*2d543d20SAndroid Build Coastguard Worker }
2379*2d543d20SAndroid Build Coastguard Worker
2380*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_post_db_cat_helper, NULL, NULL, db);
2381*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2382*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_INFO, "Failed to evaluate category expressions\n");
2383*2d543d20SAndroid Build Coastguard Worker goto exit;
2384*2d543d20SAndroid Build Coastguard Worker }
2385*2d543d20SAndroid Build Coastguard Worker
2386*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->netifcon, cil_post_netifcon_compare, cil_post_netifcon_context_compare, db, CIL_NETIFCON, CIL_KEY_NETIFCON);
2387*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2388*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing netifcon rules\n");
2389*2d543d20SAndroid Build Coastguard Worker goto exit;
2390*2d543d20SAndroid Build Coastguard Worker }
2391*2d543d20SAndroid Build Coastguard Worker
2392*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->genfscon, cil_post_genfscon_compare, cil_post_genfscon_context_compare, db, CIL_GENFSCON, CIL_KEY_GENFSCON);
2393*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2394*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing genfscon rules\n");
2395*2d543d20SAndroid Build Coastguard Worker goto exit;
2396*2d543d20SAndroid Build Coastguard Worker }
2397*2d543d20SAndroid Build Coastguard Worker
2398*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->ibpkeycon, cil_post_ibpkeycon_compare, cil_post_ibpkeycon_context_compare, db, CIL_IBPKEYCON, CIL_KEY_IBPKEYCON);
2399*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2400*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing ibpkeycon rules\n");
2401*2d543d20SAndroid Build Coastguard Worker goto exit;
2402*2d543d20SAndroid Build Coastguard Worker }
2403*2d543d20SAndroid Build Coastguard Worker
2404*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->ibendportcon, cil_post_ibendportcon_compare, cil_post_ibendportcon_context_compare, db, CIL_IBENDPORTCON, CIL_KEY_IBENDPORTCON);
2405*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2406*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing ibendportcon rules\n");
2407*2d543d20SAndroid Build Coastguard Worker goto exit;
2408*2d543d20SAndroid Build Coastguard Worker }
2409*2d543d20SAndroid Build Coastguard Worker
2410*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->portcon, cil_post_portcon_compare, cil_post_portcon_context_compare, db, CIL_PORTCON, CIL_KEY_PORTCON);
2411*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2412*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing portcon rules\n");
2413*2d543d20SAndroid Build Coastguard Worker goto exit;
2414*2d543d20SAndroid Build Coastguard Worker }
2415*2d543d20SAndroid Build Coastguard Worker
2416*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->nodecon, cil_post_nodecon_compare, cil_post_nodecon_context_compare, db, CIL_NODECON, CIL_KEY_NODECON);
2417*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2418*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing nodecon rules\n");
2419*2d543d20SAndroid Build Coastguard Worker goto exit;
2420*2d543d20SAndroid Build Coastguard Worker }
2421*2d543d20SAndroid Build Coastguard Worker
2422*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->fsuse, cil_post_fsuse_compare, cil_post_fsuse_context_compare, db, CIL_FSUSE, CIL_KEY_FSUSE);
2423*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2424*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing fsuse rules\n");
2425*2d543d20SAndroid Build Coastguard Worker goto exit;
2426*2d543d20SAndroid Build Coastguard Worker }
2427*2d543d20SAndroid Build Coastguard Worker
2428*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->filecon, cil_post_filecon_compare, cil_post_filecon_context_compare, db, CIL_FILECON, CIL_KEY_FILECON);
2429*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2430*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing filecon rules\n");
2431*2d543d20SAndroid Build Coastguard Worker goto exit;
2432*2d543d20SAndroid Build Coastguard Worker }
2433*2d543d20SAndroid Build Coastguard Worker
2434*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->pirqcon, cil_post_pirqcon_compare, cil_post_pirqcon_context_compare, db, CIL_PIRQCON, CIL_KEY_IOMEMCON);
2435*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2436*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing pirqcon rules\n");
2437*2d543d20SAndroid Build Coastguard Worker goto exit;
2438*2d543d20SAndroid Build Coastguard Worker }
2439*2d543d20SAndroid Build Coastguard Worker
2440*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->iomemcon, cil_post_iomemcon_compare, cil_post_iomemcon_context_compare, db, CIL_IOMEMCON, CIL_KEY_IOMEMCON);
2441*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2442*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing iomemcon rules\n");
2443*2d543d20SAndroid Build Coastguard Worker goto exit;
2444*2d543d20SAndroid Build Coastguard Worker }
2445*2d543d20SAndroid Build Coastguard Worker
2446*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->ioportcon, cil_post_ioportcon_compare, cil_post_ioportcon_context_compare, db, CIL_IOPORTCON, CIL_KEY_IOPORTCON);
2447*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2448*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing ioportcon rules\n");
2449*2d543d20SAndroid Build Coastguard Worker goto exit;
2450*2d543d20SAndroid Build Coastguard Worker }
2451*2d543d20SAndroid Build Coastguard Worker
2452*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->pcidevicecon, cil_post_pcidevicecon_compare, cil_post_pcidevicecon_context_compare, db, CIL_PCIDEVICECON, CIL_KEY_PCIDEVICECON);
2453*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2454*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing pcidevicecon rules\n");
2455*2d543d20SAndroid Build Coastguard Worker goto exit;
2456*2d543d20SAndroid Build Coastguard Worker }
2457*2d543d20SAndroid Build Coastguard Worker
2458*2d543d20SAndroid Build Coastguard Worker rc = __cil_post_process_context_rules(db->devicetreecon, cil_post_devicetreecon_compare, cil_post_devicetreecon_context_compare, db, CIL_DEVICETREECON, CIL_KEY_DEVICETREECON);
2459*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2460*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Problems processing devicetreecon rules\n");
2461*2d543d20SAndroid Build Coastguard Worker goto exit;
2462*2d543d20SAndroid Build Coastguard Worker }
2463*2d543d20SAndroid Build Coastguard Worker
2464*2d543d20SAndroid Build Coastguard Worker exit:
2465*2d543d20SAndroid Build Coastguard Worker return rc;
2466*2d543d20SAndroid Build Coastguard Worker }
2467*2d543d20SAndroid Build Coastguard Worker
cil_post_verify(struct cil_db * db)2468*2d543d20SAndroid Build Coastguard Worker static int cil_post_verify(struct cil_db *db)
2469*2d543d20SAndroid Build Coastguard Worker {
2470*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2471*2d543d20SAndroid Build Coastguard Worker int avrule_cnt = 0;
2472*2d543d20SAndroid Build Coastguard Worker int handleunknown = -1;
2473*2d543d20SAndroid Build Coastguard Worker int mls = -1;
2474*2d543d20SAndroid Build Coastguard Worker int nseuserdflt = 0;
2475*2d543d20SAndroid Build Coastguard Worker int pass = 0;
2476*2d543d20SAndroid Build Coastguard Worker struct cil_args_verify extra_args;
2477*2d543d20SAndroid Build Coastguard Worker struct cil_complex_symtab csymtab;
2478*2d543d20SAndroid Build Coastguard Worker
2479*2d543d20SAndroid Build Coastguard Worker cil_complex_symtab_init(&csymtab, CIL_CLASS_SYM_SIZE);
2480*2d543d20SAndroid Build Coastguard Worker
2481*2d543d20SAndroid Build Coastguard Worker extra_args.db = db;
2482*2d543d20SAndroid Build Coastguard Worker extra_args.csymtab = &csymtab;
2483*2d543d20SAndroid Build Coastguard Worker extra_args.avrule_cnt = &avrule_cnt;
2484*2d543d20SAndroid Build Coastguard Worker extra_args.handleunknown = &handleunknown;
2485*2d543d20SAndroid Build Coastguard Worker extra_args.mls = &mls;
2486*2d543d20SAndroid Build Coastguard Worker extra_args.nseuserdflt = &nseuserdflt;
2487*2d543d20SAndroid Build Coastguard Worker extra_args.pass = &pass;
2488*2d543d20SAndroid Build Coastguard Worker
2489*2d543d20SAndroid Build Coastguard Worker for (pass = 0; pass < 2; pass++) {
2490*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_verify_helper, NULL, NULL, &extra_args);
2491*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2492*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to verify cil database\n");
2493*2d543d20SAndroid Build Coastguard Worker goto exit;
2494*2d543d20SAndroid Build Coastguard Worker }
2495*2d543d20SAndroid Build Coastguard Worker }
2496*2d543d20SAndroid Build Coastguard Worker
2497*2d543d20SAndroid Build Coastguard Worker if (db->handle_unknown == -1) {
2498*2d543d20SAndroid Build Coastguard Worker if (handleunknown == -1) {
2499*2d543d20SAndroid Build Coastguard Worker db->handle_unknown = SEPOL_DENY_UNKNOWN;
2500*2d543d20SAndroid Build Coastguard Worker } else {
2501*2d543d20SAndroid Build Coastguard Worker db->handle_unknown = handleunknown;
2502*2d543d20SAndroid Build Coastguard Worker }
2503*2d543d20SAndroid Build Coastguard Worker }
2504*2d543d20SAndroid Build Coastguard Worker
2505*2d543d20SAndroid Build Coastguard Worker if (db->mls == -1) {
2506*2d543d20SAndroid Build Coastguard Worker if (mls == -1) {
2507*2d543d20SAndroid Build Coastguard Worker db->mls = CIL_FALSE;
2508*2d543d20SAndroid Build Coastguard Worker } else {
2509*2d543d20SAndroid Build Coastguard Worker db->mls = mls;
2510*2d543d20SAndroid Build Coastguard Worker }
2511*2d543d20SAndroid Build Coastguard Worker }
2512*2d543d20SAndroid Build Coastguard Worker
2513*2d543d20SAndroid Build Coastguard Worker if (avrule_cnt == 0) {
2514*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Policy must include at least one avrule\n");
2515*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
2516*2d543d20SAndroid Build Coastguard Worker goto exit;
2517*2d543d20SAndroid Build Coastguard Worker }
2518*2d543d20SAndroid Build Coastguard Worker
2519*2d543d20SAndroid Build Coastguard Worker if (nseuserdflt > 1) {
2520*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
2521*2d543d20SAndroid Build Coastguard Worker rc = SEPOL_ERR;
2522*2d543d20SAndroid Build Coastguard Worker goto exit;
2523*2d543d20SAndroid Build Coastguard Worker }
2524*2d543d20SAndroid Build Coastguard Worker
2525*2d543d20SAndroid Build Coastguard Worker exit:
2526*2d543d20SAndroid Build Coastguard Worker cil_complex_symtab_destroy(&csymtab);
2527*2d543d20SAndroid Build Coastguard Worker return rc;
2528*2d543d20SAndroid Build Coastguard Worker }
2529*2d543d20SAndroid Build Coastguard Worker
cil_pre_verify(struct cil_db * db)2530*2d543d20SAndroid Build Coastguard Worker static int cil_pre_verify(struct cil_db *db)
2531*2d543d20SAndroid Build Coastguard Worker {
2532*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2533*2d543d20SAndroid Build Coastguard Worker struct cil_args_verify extra_args;
2534*2d543d20SAndroid Build Coastguard Worker
2535*2d543d20SAndroid Build Coastguard Worker extra_args.db = db;
2536*2d543d20SAndroid Build Coastguard Worker
2537*2d543d20SAndroid Build Coastguard Worker rc = cil_tree_walk(db->ast->root, __cil_pre_verify_helper, NULL, NULL, &extra_args);
2538*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2539*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to verify cil database\n");
2540*2d543d20SAndroid Build Coastguard Worker goto exit;
2541*2d543d20SAndroid Build Coastguard Worker }
2542*2d543d20SAndroid Build Coastguard Worker
2543*2d543d20SAndroid Build Coastguard Worker exit:
2544*2d543d20SAndroid Build Coastguard Worker return rc;
2545*2d543d20SAndroid Build Coastguard Worker }
2546*2d543d20SAndroid Build Coastguard Worker
cil_post_process(struct cil_db * db)2547*2d543d20SAndroid Build Coastguard Worker int cil_post_process(struct cil_db *db)
2548*2d543d20SAndroid Build Coastguard Worker {
2549*2d543d20SAndroid Build Coastguard Worker int rc = SEPOL_ERR;
2550*2d543d20SAndroid Build Coastguard Worker
2551*2d543d20SAndroid Build Coastguard Worker rc = cil_pre_verify(db);
2552*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2553*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to verify cil database\n");
2554*2d543d20SAndroid Build Coastguard Worker goto exit;
2555*2d543d20SAndroid Build Coastguard Worker }
2556*2d543d20SAndroid Build Coastguard Worker
2557*2d543d20SAndroid Build Coastguard Worker rc = cil_post_db(db);
2558*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2559*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed post db handling\n");
2560*2d543d20SAndroid Build Coastguard Worker goto exit;
2561*2d543d20SAndroid Build Coastguard Worker }
2562*2d543d20SAndroid Build Coastguard Worker
2563*2d543d20SAndroid Build Coastguard Worker rc = cil_process_deny_rules_in_ast(db);
2564*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2565*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to process deny rules\n");
2566*2d543d20SAndroid Build Coastguard Worker goto exit;
2567*2d543d20SAndroid Build Coastguard Worker }
2568*2d543d20SAndroid Build Coastguard Worker
2569*2d543d20SAndroid Build Coastguard Worker rc = cil_post_verify(db);
2570*2d543d20SAndroid Build Coastguard Worker if (rc != SEPOL_OK) {
2571*2d543d20SAndroid Build Coastguard Worker cil_log(CIL_ERR, "Failed to verify cil database\n");
2572*2d543d20SAndroid Build Coastguard Worker goto exit;
2573*2d543d20SAndroid Build Coastguard Worker }
2574*2d543d20SAndroid Build Coastguard Worker
2575*2d543d20SAndroid Build Coastguard Worker exit:
2576*2d543d20SAndroid Build Coastguard Worker return rc;
2577*2d543d20SAndroid Build Coastguard Worker
2578*2d543d20SAndroid Build Coastguard Worker }
2579