1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include <sepol/policydb/policydb.h>
34 #include <sepol/policydb/symtab.h>
35
36 #include "cil_internal.h"
37 #include "cil_flavor.h"
38 #include "cil_log.h"
39 #include "cil_mem.h"
40 #include "cil_tree.h"
41 #include "cil_list.h"
42 #include "cil_symtab.h"
43 #include "cil_build_ast.h"
44
45 #include "cil_parser.h"
46 #include "cil_build_ast.h"
47 #include "cil_resolve_ast.h"
48 #include "cil_fqn.h"
49 #include "cil_post.h"
50 #include "cil_binary.h"
51 #include "cil_policy.h"
52 #include "cil_strpool.h"
53 #include "cil_write_ast.h"
54
55 const int cil_sym_sizes[CIL_SYM_ARRAY_NUM][CIL_SYM_NUM] = {
56 {64, 64, 64, 1 << 13, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
57 {8, 8, 8, 32, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
58 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
59 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
60 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
61 };
62
63 char *CIL_KEY_CONS_T1;
64 char *CIL_KEY_CONS_T2;
65 char *CIL_KEY_CONS_T3;
66 char *CIL_KEY_CONS_R1;
67 char *CIL_KEY_CONS_R2;
68 char *CIL_KEY_CONS_R3;
69 char *CIL_KEY_CONS_U1;
70 char *CIL_KEY_CONS_U2;
71 char *CIL_KEY_CONS_U3;
72 char *CIL_KEY_CONS_L1;
73 char *CIL_KEY_CONS_L2;
74 char *CIL_KEY_CONS_H1;
75 char *CIL_KEY_CONS_H2;
76 char *CIL_KEY_AND;
77 char *CIL_KEY_OR;
78 char *CIL_KEY_NOT;
79 char *CIL_KEY_EQ;
80 char *CIL_KEY_NEQ;
81 char *CIL_KEY_CONS_DOM;
82 char *CIL_KEY_CONS_DOMBY;
83 char *CIL_KEY_CONS_INCOMP;
84 char *CIL_KEY_CONDTRUE;
85 char *CIL_KEY_CONDFALSE;
86 char *CIL_KEY_SELF;
87 char *CIL_KEY_OBJECT_R;
88 char *CIL_KEY_STAR;
89 char *CIL_KEY_TCP;
90 char *CIL_KEY_UDP;
91 char *CIL_KEY_DCCP;
92 char *CIL_KEY_SCTP;
93 char *CIL_KEY_AUDITALLOW;
94 char *CIL_KEY_TUNABLEIF;
95 char *CIL_KEY_ALLOW;
96 char *CIL_KEY_DONTAUDIT;
97 char *CIL_KEY_TYPETRANSITION;
98 char *CIL_KEY_TYPECHANGE;
99 char *CIL_KEY_CALL;
100 char *CIL_KEY_TUNABLE;
101 char *CIL_KEY_XOR;
102 char *CIL_KEY_ALL;
103 char *CIL_KEY_RANGE;
104 char *CIL_KEY_GLOB;
105 char *CIL_KEY_FILE;
106 char *CIL_KEY_DIR;
107 char *CIL_KEY_CHAR;
108 char *CIL_KEY_BLOCK;
109 char *CIL_KEY_SOCKET;
110 char *CIL_KEY_PIPE;
111 char *CIL_KEY_SYMLINK;
112 char *CIL_KEY_ANY;
113 char *CIL_KEY_XATTR;
114 char *CIL_KEY_TASK;
115 char *CIL_KEY_TRANS;
116 char *CIL_KEY_TYPE;
117 char *CIL_KEY_ROLE;
118 char *CIL_KEY_USER;
119 char *CIL_KEY_USERATTRIBUTE;
120 char *CIL_KEY_USERATTRIBUTESET;
121 char *CIL_KEY_SENSITIVITY;
122 char *CIL_KEY_CATEGORY;
123 char *CIL_KEY_CATSET;
124 char *CIL_KEY_LEVEL;
125 char *CIL_KEY_LEVELRANGE;
126 char *CIL_KEY_CLASS;
127 char *CIL_KEY_IPADDR;
128 char *CIL_KEY_MAP_CLASS;
129 char *CIL_KEY_CLASSPERMISSION;
130 char *CIL_KEY_BOOL;
131 char *CIL_KEY_STRING;
132 char *CIL_KEY_NAME;
133 char *CIL_KEY_SOURCE;
134 char *CIL_KEY_TARGET;
135 char *CIL_KEY_LOW;
136 char *CIL_KEY_HIGH;
137 char *CIL_KEY_LOW_HIGH;
138 char *CIL_KEY_GLBLUB;
139 char *CIL_KEY_HANDLEUNKNOWN;
140 char *CIL_KEY_HANDLEUNKNOWN_ALLOW;
141 char *CIL_KEY_HANDLEUNKNOWN_DENY;
142 char *CIL_KEY_HANDLEUNKNOWN_REJECT;
143 char *CIL_KEY_MACRO;
144 char *CIL_KEY_IN;
145 char *CIL_KEY_IN_BEFORE;
146 char *CIL_KEY_IN_AFTER;
147 char *CIL_KEY_MLS;
148 char *CIL_KEY_DEFAULTRANGE;
149 char *CIL_KEY_BLOCKINHERIT;
150 char *CIL_KEY_BLOCKABSTRACT;
151 char *CIL_KEY_CLASSORDER;
152 char *CIL_KEY_CLASSMAPPING;
153 char *CIL_KEY_CLASSPERMISSIONSET;
154 char *CIL_KEY_COMMON;
155 char *CIL_KEY_CLASSCOMMON;
156 char *CIL_KEY_SID;
157 char *CIL_KEY_SIDCONTEXT;
158 char *CIL_KEY_SIDORDER;
159 char *CIL_KEY_USERLEVEL;
160 char *CIL_KEY_USERRANGE;
161 char *CIL_KEY_USERBOUNDS;
162 char *CIL_KEY_USERPREFIX;
163 char *CIL_KEY_SELINUXUSER;
164 char *CIL_KEY_SELINUXUSERDEFAULT;
165 char *CIL_KEY_TYPEATTRIBUTE;
166 char *CIL_KEY_TYPEATTRIBUTESET;
167 char *CIL_KEY_EXPANDTYPEATTRIBUTE;
168 char *CIL_KEY_TYPEALIAS;
169 char *CIL_KEY_TYPEALIASACTUAL;
170 char *CIL_KEY_TYPEBOUNDS;
171 char *CIL_KEY_TYPEPERMISSIVE;
172 char *CIL_KEY_RANGETRANSITION;
173 char *CIL_KEY_USERROLE;
174 char *CIL_KEY_ROLETYPE;
175 char *CIL_KEY_ROLETRANSITION;
176 char *CIL_KEY_ROLEALLOW;
177 char *CIL_KEY_ROLEATTRIBUTE;
178 char *CIL_KEY_ROLEATTRIBUTESET;
179 char *CIL_KEY_ROLEBOUNDS;
180 char *CIL_KEY_BOOLEANIF;
181 char *CIL_KEY_NEVERALLOW;
182 char *CIL_KEY_TYPEMEMBER;
183 char *CIL_KEY_SENSALIAS;
184 char *CIL_KEY_SENSALIASACTUAL;
185 char *CIL_KEY_CATALIAS;
186 char *CIL_KEY_CATALIASACTUAL;
187 char *CIL_KEY_CATORDER;
188 char *CIL_KEY_SENSITIVITYORDER;
189 char *CIL_KEY_SENSCAT;
190 char *CIL_KEY_CONSTRAIN;
191 char *CIL_KEY_MLSCONSTRAIN;
192 char *CIL_KEY_VALIDATETRANS;
193 char *CIL_KEY_MLSVALIDATETRANS;
194 char *CIL_KEY_CONTEXT;
195 char *CIL_KEY_FILECON;
196 char *CIL_KEY_IBPKEYCON;
197 char *CIL_KEY_IBENDPORTCON;
198 char *CIL_KEY_PORTCON;
199 char *CIL_KEY_NODECON;
200 char *CIL_KEY_GENFSCON;
201 char *CIL_KEY_NETIFCON;
202 char *CIL_KEY_PIRQCON;
203 char *CIL_KEY_IOMEMCON;
204 char *CIL_KEY_IOPORTCON;
205 char *CIL_KEY_PCIDEVICECON;
206 char *CIL_KEY_DEVICETREECON;
207 char *CIL_KEY_FSUSE;
208 char *CIL_KEY_POLICYCAP;
209 char *CIL_KEY_OPTIONAL;
210 char *CIL_KEY_DEFAULTUSER;
211 char *CIL_KEY_DEFAULTROLE;
212 char *CIL_KEY_DEFAULTTYPE;
213 char *CIL_KEY_ROOT;
214 char *CIL_KEY_NODE;
215 char *CIL_KEY_PERM;
216 char *CIL_KEY_ALLOWX;
217 char *CIL_KEY_AUDITALLOWX;
218 char *CIL_KEY_DONTAUDITX;
219 char *CIL_KEY_NEVERALLOWX;
220 char *CIL_KEY_PERMISSIONX;
221 char *CIL_KEY_IOCTL;
222 char *CIL_KEY_UNORDERED;
223 char *CIL_KEY_SRC_INFO;
224 char *CIL_KEY_SRC_CIL;
225 char *CIL_KEY_SRC_HLL_LMS;
226 char *CIL_KEY_SRC_HLL_LMX;
227 char *CIL_KEY_SRC_HLL_LME;
228
cil_init_keys(void)229 static void cil_init_keys(void)
230 {
231 /* Initialize CIL Keys into strpool */
232 CIL_KEY_CONS_T1 = cil_strpool_add("t1");
233 CIL_KEY_CONS_T2 = cil_strpool_add("t2");
234 CIL_KEY_CONS_T3 = cil_strpool_add("t3");
235 CIL_KEY_CONS_R1 = cil_strpool_add("r1");
236 CIL_KEY_CONS_R2 = cil_strpool_add("r2");
237 CIL_KEY_CONS_R3 = cil_strpool_add("r3");
238 CIL_KEY_CONS_U1 = cil_strpool_add("u1");
239 CIL_KEY_CONS_U2 = cil_strpool_add("u2");
240 CIL_KEY_CONS_U3 = cil_strpool_add("u3");
241 CIL_KEY_CONS_L1 = cil_strpool_add("l1");
242 CIL_KEY_CONS_L2 = cil_strpool_add("l2");
243 CIL_KEY_CONS_H1 = cil_strpool_add("h1");
244 CIL_KEY_CONS_H2 = cil_strpool_add("h2");
245 CIL_KEY_AND = cil_strpool_add("and");
246 CIL_KEY_OR = cil_strpool_add("or");
247 CIL_KEY_NOT = cil_strpool_add("not");
248 CIL_KEY_EQ = cil_strpool_add("eq");
249 CIL_KEY_NEQ = cil_strpool_add("neq");
250 CIL_KEY_CONS_DOM = cil_strpool_add("dom");
251 CIL_KEY_CONS_DOMBY = cil_strpool_add("domby");
252 CIL_KEY_CONS_INCOMP = cil_strpool_add("incomp");
253 CIL_KEY_CONDTRUE = cil_strpool_add("true");
254 CIL_KEY_CONDFALSE = cil_strpool_add("false");
255 CIL_KEY_SELF = cil_strpool_add("self");
256 CIL_KEY_OBJECT_R = cil_strpool_add("object_r");
257 CIL_KEY_STAR = cil_strpool_add("*");
258 CIL_KEY_UDP = cil_strpool_add("udp");
259 CIL_KEY_TCP = cil_strpool_add("tcp");
260 CIL_KEY_DCCP = cil_strpool_add("dccp");
261 CIL_KEY_SCTP = cil_strpool_add("sctp");
262 CIL_KEY_AUDITALLOW = cil_strpool_add("auditallow");
263 CIL_KEY_TUNABLEIF = cil_strpool_add("tunableif");
264 CIL_KEY_ALLOW = cil_strpool_add("allow");
265 CIL_KEY_DONTAUDIT = cil_strpool_add("dontaudit");
266 CIL_KEY_TYPETRANSITION = cil_strpool_add("typetransition");
267 CIL_KEY_TYPECHANGE = cil_strpool_add("typechange");
268 CIL_KEY_CALL = cil_strpool_add("call");
269 CIL_KEY_TUNABLE = cil_strpool_add("tunable");
270 CIL_KEY_XOR = cil_strpool_add("xor");
271 CIL_KEY_ALL = cil_strpool_add("all");
272 CIL_KEY_RANGE = cil_strpool_add("range");
273 CIL_KEY_TYPE = cil_strpool_add("type");
274 CIL_KEY_ROLE = cil_strpool_add("role");
275 CIL_KEY_USER = cil_strpool_add("user");
276 CIL_KEY_USERATTRIBUTE = cil_strpool_add("userattribute");
277 CIL_KEY_USERATTRIBUTESET = cil_strpool_add("userattributeset");
278 CIL_KEY_SENSITIVITY = cil_strpool_add("sensitivity");
279 CIL_KEY_CATEGORY = cil_strpool_add("category");
280 CIL_KEY_CATSET = cil_strpool_add("categoryset");
281 CIL_KEY_LEVEL = cil_strpool_add("level");
282 CIL_KEY_LEVELRANGE = cil_strpool_add("levelrange");
283 CIL_KEY_CLASS = cil_strpool_add("class");
284 CIL_KEY_IPADDR = cil_strpool_add("ipaddr");
285 CIL_KEY_MAP_CLASS = cil_strpool_add("classmap");
286 CIL_KEY_CLASSPERMISSION = cil_strpool_add("classpermission");
287 CIL_KEY_BOOL = cil_strpool_add("boolean");
288 CIL_KEY_STRING = cil_strpool_add("string");
289 CIL_KEY_NAME = cil_strpool_add("name");
290 CIL_KEY_HANDLEUNKNOWN = cil_strpool_add("handleunknown");
291 CIL_KEY_HANDLEUNKNOWN_ALLOW = cil_strpool_add("allow");
292 CIL_KEY_HANDLEUNKNOWN_DENY = cil_strpool_add("deny");
293 CIL_KEY_HANDLEUNKNOWN_REJECT = cil_strpool_add("reject");
294 CIL_KEY_BLOCKINHERIT = cil_strpool_add("blockinherit");
295 CIL_KEY_BLOCKABSTRACT = cil_strpool_add("blockabstract");
296 CIL_KEY_CLASSORDER = cil_strpool_add("classorder");
297 CIL_KEY_CLASSMAPPING = cil_strpool_add("classmapping");
298 CIL_KEY_CLASSPERMISSIONSET = cil_strpool_add("classpermissionset");
299 CIL_KEY_COMMON = cil_strpool_add("common");
300 CIL_KEY_CLASSCOMMON = cil_strpool_add("classcommon");
301 CIL_KEY_SID = cil_strpool_add("sid");
302 CIL_KEY_SIDCONTEXT = cil_strpool_add("sidcontext");
303 CIL_KEY_SIDORDER = cil_strpool_add("sidorder");
304 CIL_KEY_USERLEVEL = cil_strpool_add("userlevel");
305 CIL_KEY_USERRANGE = cil_strpool_add("userrange");
306 CIL_KEY_USERBOUNDS = cil_strpool_add("userbounds");
307 CIL_KEY_USERPREFIX = cil_strpool_add("userprefix");
308 CIL_KEY_SELINUXUSER = cil_strpool_add("selinuxuser");
309 CIL_KEY_SELINUXUSERDEFAULT = cil_strpool_add("selinuxuserdefault");
310 CIL_KEY_TYPEATTRIBUTE = cil_strpool_add("typeattribute");
311 CIL_KEY_TYPEATTRIBUTESET = cil_strpool_add("typeattributeset");
312 CIL_KEY_EXPANDTYPEATTRIBUTE = cil_strpool_add("expandtypeattribute");
313 CIL_KEY_TYPEALIAS = cil_strpool_add("typealias");
314 CIL_KEY_TYPEALIASACTUAL = cil_strpool_add("typealiasactual");
315 CIL_KEY_TYPEBOUNDS = cil_strpool_add("typebounds");
316 CIL_KEY_TYPEPERMISSIVE = cil_strpool_add("typepermissive");
317 CIL_KEY_RANGETRANSITION = cil_strpool_add("rangetransition");
318 CIL_KEY_USERROLE = cil_strpool_add("userrole");
319 CIL_KEY_ROLETYPE = cil_strpool_add("roletype");
320 CIL_KEY_ROLETRANSITION = cil_strpool_add("roletransition");
321 CIL_KEY_ROLEALLOW = cil_strpool_add("roleallow");
322 CIL_KEY_ROLEATTRIBUTE = cil_strpool_add("roleattribute");
323 CIL_KEY_ROLEATTRIBUTESET = cil_strpool_add("roleattributeset");
324 CIL_KEY_ROLEBOUNDS = cil_strpool_add("rolebounds");
325 CIL_KEY_BOOLEANIF = cil_strpool_add("booleanif");
326 CIL_KEY_NEVERALLOW = cil_strpool_add("neverallow");
327 CIL_KEY_TYPEMEMBER = cil_strpool_add("typemember");
328 CIL_KEY_SENSALIAS = cil_strpool_add("sensitivityalias");
329 CIL_KEY_SENSALIASACTUAL = cil_strpool_add("sensitivityaliasactual");
330 CIL_KEY_CATALIAS = cil_strpool_add("categoryalias");
331 CIL_KEY_CATALIASACTUAL = cil_strpool_add("categoryaliasactual");
332 CIL_KEY_CATORDER = cil_strpool_add("categoryorder");
333 CIL_KEY_SENSITIVITYORDER = cil_strpool_add("sensitivityorder");
334 CIL_KEY_SENSCAT = cil_strpool_add("sensitivitycategory");
335 CIL_KEY_CONSTRAIN = cil_strpool_add("constrain");
336 CIL_KEY_MLSCONSTRAIN = cil_strpool_add("mlsconstrain");
337 CIL_KEY_VALIDATETRANS = cil_strpool_add("validatetrans");
338 CIL_KEY_MLSVALIDATETRANS = cil_strpool_add("mlsvalidatetrans");
339 CIL_KEY_CONTEXT = cil_strpool_add("context");
340 CIL_KEY_FILECON = cil_strpool_add("filecon");
341 CIL_KEY_IBPKEYCON = cil_strpool_add("ibpkeycon");
342 CIL_KEY_IBENDPORTCON = cil_strpool_add("ibendportcon");
343 CIL_KEY_PORTCON = cil_strpool_add("portcon");
344 CIL_KEY_NODECON = cil_strpool_add("nodecon");
345 CIL_KEY_GENFSCON = cil_strpool_add("genfscon");
346 CIL_KEY_NETIFCON = cil_strpool_add("netifcon");
347 CIL_KEY_PIRQCON = cil_strpool_add("pirqcon");
348 CIL_KEY_IOMEMCON = cil_strpool_add("iomemcon");
349 CIL_KEY_IOPORTCON = cil_strpool_add("ioportcon");
350 CIL_KEY_PCIDEVICECON = cil_strpool_add("pcidevicecon");
351 CIL_KEY_DEVICETREECON = cil_strpool_add("devicetreecon");
352 CIL_KEY_FSUSE = cil_strpool_add("fsuse");
353 CIL_KEY_POLICYCAP = cil_strpool_add("policycap");
354 CIL_KEY_OPTIONAL = cil_strpool_add("optional");
355 CIL_KEY_DEFAULTUSER = cil_strpool_add("defaultuser");
356 CIL_KEY_DEFAULTROLE = cil_strpool_add("defaultrole");
357 CIL_KEY_DEFAULTTYPE = cil_strpool_add("defaulttype");
358 CIL_KEY_MACRO = cil_strpool_add("macro");
359 CIL_KEY_IN = cil_strpool_add("in");
360 CIL_KEY_IN_BEFORE = cil_strpool_add("before");
361 CIL_KEY_IN_AFTER = cil_strpool_add("after");
362 CIL_KEY_MLS = cil_strpool_add("mls");
363 CIL_KEY_DEFAULTRANGE = cil_strpool_add("defaultrange");
364 CIL_KEY_GLOB = cil_strpool_add("*");
365 CIL_KEY_FILE = cil_strpool_add("file");
366 CIL_KEY_DIR = cil_strpool_add("dir");
367 CIL_KEY_CHAR = cil_strpool_add("char");
368 CIL_KEY_BLOCK = cil_strpool_add("block");
369 CIL_KEY_SOCKET = cil_strpool_add("socket");
370 CIL_KEY_PIPE = cil_strpool_add("pipe");
371 CIL_KEY_SYMLINK = cil_strpool_add("symlink");
372 CIL_KEY_ANY = cil_strpool_add("any");
373 CIL_KEY_XATTR = cil_strpool_add("xattr");
374 CIL_KEY_TASK = cil_strpool_add("task");
375 CIL_KEY_TRANS = cil_strpool_add("trans");
376 CIL_KEY_SOURCE = cil_strpool_add("source");
377 CIL_KEY_TARGET = cil_strpool_add("target");
378 CIL_KEY_LOW = cil_strpool_add("low");
379 CIL_KEY_HIGH = cil_strpool_add("high");
380 CIL_KEY_LOW_HIGH = cil_strpool_add("low-high");
381 CIL_KEY_GLBLUB = cil_strpool_add("glblub");
382 CIL_KEY_ROOT = cil_strpool_add("<root>");
383 CIL_KEY_NODE = cil_strpool_add("<node>");
384 CIL_KEY_PERM = cil_strpool_add("perm");
385 CIL_KEY_ALLOWX = cil_strpool_add("allowx");
386 CIL_KEY_AUDITALLOWX = cil_strpool_add("auditallowx");
387 CIL_KEY_DONTAUDITX = cil_strpool_add("dontauditx");
388 CIL_KEY_NEVERALLOWX = cil_strpool_add("neverallowx");
389 CIL_KEY_PERMISSIONX = cil_strpool_add("permissionx");
390 CIL_KEY_IOCTL = cil_strpool_add("ioctl");
391 CIL_KEY_UNORDERED = cil_strpool_add("unordered");
392 CIL_KEY_SRC_INFO = cil_strpool_add("<src_info>");
393 CIL_KEY_SRC_CIL = cil_strpool_add("cil");
394 CIL_KEY_SRC_HLL_LMS = cil_strpool_add("lms");
395 CIL_KEY_SRC_HLL_LMX = cil_strpool_add("lmx");
396 CIL_KEY_SRC_HLL_LME = cil_strpool_add("lme");
397 }
398
cil_db_init(struct cil_db ** db)399 void cil_db_init(struct cil_db **db)
400 {
401 *db = cil_malloc(sizeof(**db));
402
403 cil_strpool_init();
404 cil_init_keys();
405
406 cil_tree_init(&(*db)->parse);
407 cil_tree_init(&(*db)->ast);
408 cil_root_init((struct cil_root **)&(*db)->ast->root->data);
409 (*db)->sidorder = NULL;
410 (*db)->classorder = NULL;
411 (*db)->catorder = NULL;
412 (*db)->sensitivityorder = NULL;
413 cil_sort_init(&(*db)->netifcon);
414 cil_sort_init(&(*db)->genfscon);
415 cil_sort_init(&(*db)->filecon);
416 cil_sort_init(&(*db)->nodecon);
417 cil_sort_init(&(*db)->ibpkeycon);
418 cil_sort_init(&(*db)->ibendportcon);
419 cil_sort_init(&(*db)->portcon);
420 cil_sort_init(&(*db)->pirqcon);
421 cil_sort_init(&(*db)->iomemcon);
422 cil_sort_init(&(*db)->ioportcon);
423 cil_sort_init(&(*db)->pcidevicecon);
424 cil_sort_init(&(*db)->devicetreecon);
425 cil_sort_init(&(*db)->fsuse);
426 cil_list_init(&(*db)->userprefixes, CIL_LIST_ITEM);
427 cil_list_init(&(*db)->selinuxusers, CIL_LIST_ITEM);
428 cil_list_init(&(*db)->names, CIL_LIST_ITEM);
429
430 cil_type_init(&(*db)->selftype);
431 (*db)->selftype->datum.name = CIL_KEY_SELF;
432 (*db)->selftype->datum.fqn = CIL_KEY_SELF;
433 (*db)->num_types_and_attrs = 0;
434 (*db)->num_classes = 0;
435 (*db)->num_types = 0;
436 (*db)->num_roles = 0;
437 (*db)->num_users = 0;
438 (*db)->num_cats = 0;
439 (*db)->val_to_type = NULL;
440 (*db)->val_to_role = NULL;
441 (*db)->val_to_user = NULL;
442
443 (*db)->disable_dontaudit = CIL_FALSE;
444 (*db)->disable_neverallow = CIL_FALSE;
445 (*db)->attrs_expand_generated = CIL_FALSE;
446 (*db)->attrs_expand_size = 1;
447 (*db)->preserve_tunables = CIL_FALSE;
448 (*db)->handle_unknown = -1;
449 (*db)->mls = -1;
450 (*db)->multiple_decls = CIL_FALSE;
451 (*db)->qualified_names = CIL_FALSE;
452 (*db)->target_platform = SEPOL_TARGET_SELINUX;
453 (*db)->policy_version = POLICYDB_VERSION_MAX;
454 }
455
cil_db_destroy(struct cil_db ** db)456 void cil_db_destroy(struct cil_db **db)
457 {
458 if (db == NULL || *db == NULL) {
459 return;
460 }
461
462 cil_tree_destroy(&(*db)->parse);
463 cil_tree_destroy(&(*db)->ast);
464 cil_list_destroy(&(*db)->sidorder, CIL_FALSE);
465 cil_list_destroy(&(*db)->classorder, CIL_FALSE);
466 cil_list_destroy(&(*db)->catorder, CIL_FALSE);
467 cil_list_destroy(&(*db)->sensitivityorder, CIL_FALSE);
468 cil_sort_destroy(&(*db)->netifcon);
469 cil_sort_destroy(&(*db)->genfscon);
470 cil_sort_destroy(&(*db)->filecon);
471 cil_sort_destroy(&(*db)->nodecon);
472 cil_sort_destroy(&(*db)->ibpkeycon);
473 cil_sort_destroy(&(*db)->ibendportcon);
474 cil_sort_destroy(&(*db)->portcon);
475 cil_sort_destroy(&(*db)->pirqcon);
476 cil_sort_destroy(&(*db)->iomemcon);
477 cil_sort_destroy(&(*db)->ioportcon);
478 cil_sort_destroy(&(*db)->pcidevicecon);
479 cil_sort_destroy(&(*db)->devicetreecon);
480 cil_sort_destroy(&(*db)->fsuse);
481 cil_list_destroy(&(*db)->userprefixes, CIL_FALSE);
482 cil_list_destroy(&(*db)->selinuxusers, CIL_FALSE);
483 cil_list_destroy(&(*db)->names, CIL_TRUE);
484
485 cil_destroy_type((*db)->selftype);
486
487 cil_strpool_destroy();
488 free((*db)->val_to_type);
489 free((*db)->val_to_role);
490 free((*db)->val_to_user);
491
492 free(*db);
493 *db = NULL;
494 }
495
cil_root_init(struct cil_root ** root)496 void cil_root_init(struct cil_root **root)
497 {
498 struct cil_root *r = cil_malloc(sizeof(*r));
499 cil_symtab_array_init(r->symtab, cil_sym_sizes[CIL_SYM_ARRAY_ROOT]);
500
501 *root = r;
502 }
503
cil_root_destroy(struct cil_root * root)504 void cil_root_destroy(struct cil_root *root)
505 {
506 if (root == NULL) {
507 return;
508 }
509 cil_symtab_array_destroy(root->symtab);
510 free(root);
511 }
512
cil_add_file(cil_db_t * db,const char * name,const char * data,size_t size)513 int cil_add_file(cil_db_t *db, const char *name, const char *data, size_t size)
514 {
515 char *buffer = NULL;
516 int rc;
517
518 cil_log(CIL_INFO, "Parsing %s\n", name);
519
520 buffer = cil_malloc(size + 2);
521 memcpy(buffer, data, size);
522 memset(buffer + size, 0, 2);
523
524 rc = cil_parser(name, buffer, size + 2, &db->parse);
525 if (rc != SEPOL_OK) {
526 cil_log(CIL_INFO, "Failed to parse %s\n", name);
527 goto exit;
528 }
529
530 free(buffer);
531 buffer = NULL;
532
533 rc = SEPOL_OK;
534
535 exit:
536 free(buffer);
537
538 return rc;
539 }
540
cil_compile(struct cil_db * db)541 int cil_compile(struct cil_db *db)
542 {
543 int rc = SEPOL_ERR;
544
545 if (db == NULL) {
546 goto exit;
547 }
548
549 cil_log(CIL_INFO, "Building AST from Parse Tree\n");
550 rc = cil_build_ast(db, db->parse->root, db->ast->root);
551 if (rc != SEPOL_OK) {
552 cil_log(CIL_ERR, "Failed to build AST\n");
553 goto exit;
554 }
555
556 cil_log(CIL_INFO, "Destroying Parse Tree\n");
557 cil_tree_destroy(&db->parse);
558
559 cil_log(CIL_INFO, "Resolving AST\n");
560 rc = cil_resolve_ast(db, db->ast->root);
561 if (rc != SEPOL_OK) {
562 cil_log(CIL_ERR, "Failed to resolve AST\n");
563 goto exit;
564 }
565
566 cil_log(CIL_INFO, "Qualifying Names\n");
567 rc = cil_fqn_qualify(db->ast->root);
568 if (rc != SEPOL_OK) {
569 cil_log(CIL_ERR, "Failed to qualify names\n");
570 goto exit;
571 }
572
573 cil_log(CIL_INFO, "Compile post process\n");
574 rc = cil_post_process(db);
575 if (rc != SEPOL_OK ) {
576 cil_log(CIL_ERR, "Post process failed\n");
577 goto exit;
578 }
579
580 exit:
581
582 return rc;
583 }
584
cil_write_parse_ast(FILE * out,cil_db_t * db)585 int cil_write_parse_ast(FILE *out, cil_db_t *db)
586 {
587 int rc = SEPOL_ERR;
588
589 if (db == NULL) {
590 goto exit;
591 }
592
593 cil_log(CIL_INFO, "Writing Parse AST\n");
594 rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_PARSE, db->parse->root);
595 if (rc != SEPOL_OK) {
596 cil_log(CIL_ERR, "Failed to write parse ast\n");
597 goto exit;
598 }
599
600 exit:
601 return rc;
602 }
603
cil_write_build_ast(FILE * out,cil_db_t * db)604 int cil_write_build_ast(FILE *out, cil_db_t *db)
605 {
606 int rc = SEPOL_ERR;
607
608 if (db == NULL) {
609 goto exit;
610 }
611
612 cil_log(CIL_INFO, "Building AST from Parse Tree\n");
613 rc = cil_build_ast(db, db->parse->root, db->ast->root);
614 if (rc != SEPOL_OK) {
615 cil_log(CIL_ERR, "Failed to build ast\n");
616 goto exit;
617 }
618
619 cil_log(CIL_INFO, "Destroying Parse Tree\n");
620 cil_tree_destroy(&db->parse);
621
622 cil_log(CIL_INFO, "Writing Build AST\n");
623 rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_BUILD, db->ast->root);
624 if (rc != SEPOL_OK) {
625 cil_log(CIL_ERR, "Failed to write build ast\n");
626 goto exit;
627 }
628
629 exit:
630 return rc;
631 }
632
cil_write_resolve_ast(FILE * out,cil_db_t * db)633 int cil_write_resolve_ast(FILE *out, cil_db_t *db)
634 {
635 int rc = SEPOL_ERR;
636
637 if (db == NULL) {
638 goto exit;
639 }
640
641 cil_log(CIL_INFO, "Building AST from Parse Tree\n");
642 rc = cil_build_ast(db, db->parse->root, db->ast->root);
643 if (rc != SEPOL_OK) {
644 cil_log(CIL_ERR, "Failed to build ast\n");
645 goto exit;
646 }
647
648 cil_log(CIL_INFO, "Destroying Parse Tree\n");
649 cil_tree_destroy(&db->parse);
650
651 cil_log(CIL_INFO, "Resolving AST\n");
652 rc = cil_resolve_ast(db, db->ast->root);
653 if (rc != SEPOL_OK) {
654 cil_log(CIL_ERR, "Failed to resolve ast\n");
655 goto exit;
656 }
657
658 cil_log(CIL_INFO, "Qualifying Names\n");
659 rc = cil_fqn_qualify(db->ast->root);
660 if (rc != SEPOL_OK) {
661 cil_log(CIL_ERR, "Failed to qualify names\n");
662 goto exit;
663 }
664
665 cil_log(CIL_INFO, "Writing Resolve AST\n");
666 rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_RESOLVE, db->ast->root);
667 if (rc != SEPOL_OK) {
668 cil_log(CIL_ERR, "Failed to write resolve ast\n");
669 goto exit;
670 }
671
672 exit:
673 return rc;
674 }
675
cil_build_policydb(cil_db_t * db,sepol_policydb_t ** sepol_db)676 int cil_build_policydb(cil_db_t *db, sepol_policydb_t **sepol_db)
677 {
678 int rc;
679
680 cil_log(CIL_INFO, "Building policy binary\n");
681 rc = cil_binary_create(db, sepol_db);
682 if (rc != SEPOL_OK) {
683 cil_log(CIL_ERR, "Failed to generate binary\n");
684 goto exit;
685 }
686
687 exit:
688 return rc;
689 }
690
cil_write_policy_conf(FILE * out,struct cil_db * db)691 void cil_write_policy_conf(FILE *out, struct cil_db *db)
692 {
693 cil_log(CIL_INFO, "Writing policy.conf file\n");
694 cil_gen_policy(out, db);
695 }
696
cil_destroy_data(void ** data,enum cil_flavor flavor)697 void cil_destroy_data(void **data, enum cil_flavor flavor)
698 {
699 if (*data == NULL) {
700 return;
701 }
702
703 switch(flavor) {
704 case CIL_NONE:
705 break;
706 case CIL_ROOT:
707 cil_root_destroy(*data);
708 break;
709 case CIL_NODE:
710 break;
711 case CIL_STRING:
712 break;
713 case CIL_DATUM:
714 break;
715 case CIL_LIST:
716 cil_list_destroy(*data, CIL_FALSE);
717 break;
718 case CIL_LIST_ITEM:
719 break;
720 case CIL_PARAM:
721 cil_destroy_param(*data);
722 break;
723 case CIL_ARGS:
724 cil_destroy_args(*data);
725 break;
726 case CIL_BLOCK:
727 cil_destroy_block(*data);
728 break;
729 case CIL_BLOCKINHERIT:
730 cil_destroy_blockinherit(*data);
731 break;
732 case CIL_BLOCKABSTRACT:
733 cil_destroy_blockabstract(*data);
734 break;
735 case CIL_IN:
736 cil_destroy_in(*data);
737 break;
738 case CIL_MACRO:
739 cil_destroy_macro(*data);
740 break;
741 case CIL_CALL:
742 cil_destroy_call(*data);
743 break;
744 case CIL_OPTIONAL:
745 cil_destroy_optional(*data);
746 break;
747 case CIL_BOOL:
748 cil_destroy_bool(*data);
749 break;
750 case CIL_BOOLEANIF:
751 cil_destroy_boolif(*data);
752 break;
753 case CIL_TUNABLE:
754 cil_destroy_tunable(*data);
755 break;
756 case CIL_TUNABLEIF:
757 cil_destroy_tunif(*data);
758 break;
759 case CIL_CONDBLOCK:
760 cil_destroy_condblock(*data);
761 break;
762 case CIL_CONDTRUE:
763 break;
764 case CIL_CONDFALSE:
765 break;
766 case CIL_PERM:
767 case CIL_MAP_PERM:
768 cil_destroy_perm(*data);
769 break;
770 case CIL_COMMON:
771 case CIL_CLASS:
772 case CIL_MAP_CLASS:
773 cil_destroy_class(*data);
774 break;
775 case CIL_CLASSORDER:
776 cil_destroy_classorder(*data);
777 break;
778 case CIL_CLASSPERMISSION:
779 cil_destroy_classpermission(*data);
780 break;
781 case CIL_CLASSCOMMON:
782 cil_destroy_classcommon(*data);
783 break;
784 case CIL_CLASSMAPPING:
785 cil_destroy_classmapping(*data);
786 break;
787 case CIL_CLASSPERMS:
788 cil_destroy_classperms(*data);
789 break;
790 case CIL_CLASSPERMS_SET:
791 cil_destroy_classperms_set(*data);
792 break;
793 case CIL_CLASSPERMISSIONSET:
794 cil_destroy_classpermissionset(*data);
795 break;
796 case CIL_USER:
797 cil_destroy_user(*data);
798 break;
799 case CIL_USERATTRIBUTE:
800 cil_destroy_userattribute(*data);
801 break;
802 case CIL_USERATTRIBUTESET:
803 cil_destroy_userattributeset(*data);
804 break;
805 case CIL_USERPREFIX:
806 cil_destroy_userprefix(*data);
807 break;
808 case CIL_USERROLE:
809 cil_destroy_userrole(*data);
810 break;
811 case CIL_USERLEVEL:
812 cil_destroy_userlevel(*data);
813 break;
814 case CIL_USERRANGE:
815 cil_destroy_userrange(*data);
816 break;
817 case CIL_USERBOUNDS:
818 cil_destroy_bounds(*data);
819 break;
820 case CIL_SELINUXUSER:
821 case CIL_SELINUXUSERDEFAULT:
822 cil_destroy_selinuxuser(*data);
823 break;
824 case CIL_ROLE:
825 cil_destroy_role(*data);
826 break;
827 case CIL_ROLEATTRIBUTE:
828 cil_destroy_roleattribute(*data);
829 break;
830 case CIL_ROLEATTRIBUTESET:
831 cil_destroy_roleattributeset(*data);
832 break;
833 case CIL_ROLETYPE:
834 cil_destroy_roletype(*data);
835 break;
836 case CIL_ROLEBOUNDS:
837 cil_destroy_bounds(*data);
838 break;
839 case CIL_TYPE:
840 cil_destroy_type(*data);
841 break;
842 case CIL_TYPEATTRIBUTE:
843 cil_destroy_typeattribute(*data);
844 break;
845 case CIL_TYPEALIAS:
846 cil_destroy_alias(*data);
847 break;
848 case CIL_TYPEATTRIBUTESET:
849 cil_destroy_typeattributeset(*data);
850 break;
851 case CIL_EXPANDTYPEATTRIBUTE:
852 cil_destroy_expandtypeattribute(*data);
853 break;
854 case CIL_TYPEALIASACTUAL:
855 cil_destroy_aliasactual(*data);
856 break;
857 case CIL_TYPEBOUNDS:
858 cil_destroy_bounds(*data);
859 break;
860 case CIL_TYPEPERMISSIVE:
861 cil_destroy_typepermissive(*data);
862 break;
863 case CIL_SENS:
864 cil_destroy_sensitivity(*data);
865 break;
866 case CIL_SENSALIAS:
867 cil_destroy_alias(*data);
868 break;
869 case CIL_SENSALIASACTUAL:
870 cil_destroy_aliasactual(*data);
871 break;
872 case CIL_SENSITIVITYORDER:
873 cil_destroy_sensitivityorder(*data);
874 break;
875 case CIL_SENSCAT:
876 cil_destroy_senscat(*data);
877 break;
878 case CIL_CAT:
879 cil_destroy_category(*data);
880 break;
881 case CIL_CATSET:
882 cil_destroy_catset(*data);
883 break;
884 case CIL_CATALIAS:
885 cil_destroy_alias(*data);
886 break;
887 case CIL_CATALIASACTUAL:
888 cil_destroy_aliasactual(*data);
889 break;
890 case CIL_CATORDER:
891 cil_destroy_catorder(*data);
892 break;
893 case CIL_LEVEL:
894 cil_destroy_level(*data);
895 break;
896 case CIL_LEVELRANGE:
897 cil_destroy_levelrange(*data);
898 break;
899 case CIL_SID:
900 cil_destroy_sid(*data);
901 break;
902 case CIL_SIDORDER:
903 cil_destroy_sidorder(*data);
904 break;
905 case CIL_NAME:
906 cil_destroy_name(*data);
907 break;
908 case CIL_ROLEALLOW:
909 cil_destroy_roleallow(*data);
910 break;
911 case CIL_AVRULE:
912 case CIL_AVRULEX:
913 cil_destroy_avrule(*data);
914 break;
915 case CIL_PERMISSIONX:
916 cil_destroy_permissionx(*data);
917 break;
918 case CIL_ROLETRANSITION:
919 cil_destroy_roletransition(*data);
920 break;
921 case CIL_TYPE_RULE:
922 cil_destroy_type_rule(*data);
923 break;
924 case CIL_NAMETYPETRANSITION:
925 cil_destroy_typetransition(*data);
926 break;
927 case CIL_RANGETRANSITION:
928 cil_destroy_rangetransition(*data);
929 break;
930 case CIL_CONSTRAIN:
931 cil_destroy_constrain(*data);
932 break;
933 case CIL_MLSCONSTRAIN:
934 cil_destroy_constrain(*data);
935 break;
936 case CIL_VALIDATETRANS:
937 case CIL_MLSVALIDATETRANS:
938 cil_destroy_validatetrans(*data);
939 break;
940 case CIL_CONTEXT:
941 cil_destroy_context(*data);
942 break;
943 case CIL_IPADDR:
944 cil_destroy_ipaddr(*data);
945 break;
946 case CIL_SIDCONTEXT:
947 cil_destroy_sidcontext(*data);
948 break;
949 case CIL_FSUSE:
950 cil_destroy_fsuse(*data);
951 break;
952 case CIL_FILECON:
953 cil_destroy_filecon(*data);
954 break;
955 case CIL_IBPKEYCON:
956 cil_destroy_ibpkeycon(*data);
957 break;
958 case CIL_PORTCON:
959 cil_destroy_portcon(*data);
960 break;
961 case CIL_IBENDPORTCON:
962 cil_destroy_ibendportcon(*data);
963 break;
964 case CIL_NODECON:
965 cil_destroy_nodecon(*data);
966 break;
967 case CIL_GENFSCON:
968 cil_destroy_genfscon(*data);
969 break;
970 case CIL_NETIFCON:
971 cil_destroy_netifcon(*data);
972 break;
973 case CIL_PIRQCON:
974 cil_destroy_pirqcon(*data);
975 break;
976 case CIL_IOMEMCON:
977 cil_destroy_iomemcon(*data);
978 break;
979 case CIL_IOPORTCON:
980 cil_destroy_ioportcon(*data);
981 break;
982 case CIL_PCIDEVICECON:
983 cil_destroy_pcidevicecon(*data);
984 break;
985 case CIL_DEVICETREECON:
986 cil_destroy_devicetreecon(*data);
987 break;
988 case CIL_POLICYCAP:
989 cil_destroy_policycap(*data);
990 break;
991 case CIL_DEFAULTUSER:
992 case CIL_DEFAULTROLE:
993 case CIL_DEFAULTTYPE:
994 cil_destroy_default(*data);
995 break;
996 case CIL_DEFAULTRANGE:
997 cil_destroy_defaultrange(*data);
998 break;
999 case CIL_HANDLEUNKNOWN:
1000 cil_destroy_handleunknown(*data);
1001 break;
1002 case CIL_MLS:
1003 cil_destroy_mls(*data);
1004 break;
1005 case CIL_SRC_INFO:
1006 cil_destroy_src_info(*data);
1007 break;
1008 case CIL_OP:
1009 case CIL_CONS_OPERAND:
1010 break;
1011 default:
1012 cil_log(CIL_INFO, "Unknown data flavor: %d\n", flavor);
1013 break;
1014 }
1015
1016 *data = NULL;
1017 }
1018
cil_flavor_to_symtab_index(enum cil_flavor flavor,enum cil_sym_index * sym_index)1019 int cil_flavor_to_symtab_index(enum cil_flavor flavor, enum cil_sym_index *sym_index)
1020 {
1021 if (flavor < CIL_MIN_DECLARATIVE) {
1022 return SEPOL_ERR;
1023 }
1024
1025 switch(flavor) {
1026 case CIL_BLOCK:
1027 *sym_index = CIL_SYM_BLOCKS;
1028 break;
1029 case CIL_MACRO:
1030 *sym_index = CIL_SYM_BLOCKS;
1031 break;
1032 case CIL_OPTIONAL:
1033 *sym_index = CIL_SYM_BLOCKS;
1034 break;
1035 case CIL_BOOL:
1036 *sym_index = CIL_SYM_BOOLS;
1037 break;
1038 case CIL_TUNABLE:
1039 *sym_index = CIL_SYM_TUNABLES;
1040 break;
1041 case CIL_PERM:
1042 case CIL_MAP_PERM:
1043 *sym_index = CIL_SYM_PERMS;
1044 break;
1045 case CIL_COMMON:
1046 *sym_index = CIL_SYM_COMMONS;
1047 break;
1048 case CIL_CLASS:
1049 case CIL_MAP_CLASS:
1050 *sym_index = CIL_SYM_CLASSES;
1051 break;
1052 case CIL_CLASSPERMISSION:
1053 case CIL_CLASSPERMISSIONSET:
1054 *sym_index = CIL_SYM_CLASSPERMSETS;
1055 break;
1056 case CIL_USER:
1057 case CIL_USERATTRIBUTE:
1058 *sym_index = CIL_SYM_USERS;
1059 break;
1060 case CIL_ROLE:
1061 case CIL_ROLEATTRIBUTE:
1062 *sym_index = CIL_SYM_ROLES;
1063 break;
1064 case CIL_TYPE:
1065 case CIL_TYPEALIAS:
1066 case CIL_TYPEATTRIBUTE:
1067 *sym_index = CIL_SYM_TYPES;
1068 break;
1069 case CIL_SENS:
1070 case CIL_SENSALIAS:
1071 *sym_index = CIL_SYM_SENS;
1072 break;
1073 case CIL_CAT:
1074 case CIL_CATSET:
1075 case CIL_CATALIAS:
1076 *sym_index = CIL_SYM_CATS;
1077 break;
1078 case CIL_LEVEL:
1079 *sym_index = CIL_SYM_LEVELS;
1080 break;
1081 case CIL_LEVELRANGE:
1082 *sym_index = CIL_SYM_LEVELRANGES;
1083 break;
1084 case CIL_SID:
1085 *sym_index = CIL_SYM_SIDS;
1086 break;
1087 case CIL_NAME:
1088 *sym_index = CIL_SYM_NAMES;
1089 break;
1090 case CIL_CONTEXT:
1091 *sym_index = CIL_SYM_CONTEXTS;
1092 break;
1093 case CIL_IPADDR:
1094 *sym_index = CIL_SYM_IPADDRS;
1095 break;
1096 case CIL_POLICYCAP:
1097 *sym_index = CIL_SYM_POLICYCAPS;
1098 break;
1099 case CIL_PERMISSIONX:
1100 *sym_index = CIL_SYM_PERMX;
1101 break;
1102 default:
1103 *sym_index = CIL_SYM_UNKNOWN;
1104 cil_log(CIL_INFO, "Failed to find flavor: %d\n", flavor);
1105 return SEPOL_ERR;
1106 }
1107
1108 return SEPOL_OK;
1109 }
1110
cil_node_to_string(struct cil_tree_node * node)1111 const char * cil_node_to_string(struct cil_tree_node *node)
1112 {
1113 switch (node->flavor) {
1114 case CIL_NONE:
1115 return "<none>";
1116 case CIL_ROOT:
1117 return CIL_KEY_ROOT;
1118 case CIL_NODE:
1119 return CIL_KEY_NODE;
1120 case CIL_STRING:
1121 return "string";
1122 case CIL_DATUM:
1123 return "<datum>";
1124 case CIL_LIST:
1125 return "<list>";
1126 case CIL_LIST_ITEM:
1127 return "<list_item>";
1128 case CIL_PARAM:
1129 return "<param>";
1130 case CIL_ARGS:
1131 return "<args>";
1132 case CIL_BLOCK:
1133 return CIL_KEY_BLOCK;
1134 case CIL_BLOCKINHERIT:
1135 return CIL_KEY_BLOCKINHERIT;
1136 case CIL_BLOCKABSTRACT:
1137 return CIL_KEY_BLOCKABSTRACT;
1138 case CIL_IN:
1139 return CIL_KEY_IN;
1140 case CIL_MACRO:
1141 return CIL_KEY_MACRO;
1142 case CIL_CALL:
1143 return CIL_KEY_CALL;
1144 case CIL_OPTIONAL:
1145 return CIL_KEY_OPTIONAL;
1146 case CIL_BOOL:
1147 return CIL_KEY_BOOL;
1148 case CIL_BOOLEANIF:
1149 return CIL_KEY_BOOLEANIF;
1150 case CIL_TUNABLE:
1151 return CIL_KEY_TUNABLE;
1152 case CIL_TUNABLEIF:
1153 return CIL_KEY_TUNABLEIF;
1154 case CIL_CONDBLOCK:
1155 switch (((struct cil_condblock*)node->data)->flavor) {
1156 case CIL_CONDTRUE:
1157 return CIL_KEY_CONDTRUE;
1158 case CIL_CONDFALSE:
1159 return CIL_KEY_CONDFALSE;
1160 default:
1161 break;
1162 }
1163 break;
1164 case CIL_CONDTRUE:
1165 return CIL_KEY_CONDTRUE;
1166 case CIL_CONDFALSE:
1167 return CIL_KEY_CONDFALSE;
1168 case CIL_PERM:
1169 return CIL_KEY_PERM;
1170 case CIL_COMMON:
1171 return CIL_KEY_COMMON;
1172 case CIL_CLASS:
1173 return CIL_KEY_CLASS;
1174 case CIL_CLASSORDER:
1175 return CIL_KEY_CLASSORDER;
1176 case CIL_MAP_CLASS:
1177 return CIL_KEY_MAP_CLASS;
1178 case CIL_CLASSPERMISSION:
1179 return CIL_KEY_CLASSPERMISSION;
1180 case CIL_CLASSCOMMON:
1181 return CIL_KEY_CLASSCOMMON;
1182 case CIL_CLASSMAPPING:
1183 return CIL_KEY_CLASSMAPPING;
1184 case CIL_CLASSPERMISSIONSET:
1185 return CIL_KEY_CLASSPERMISSIONSET;
1186 case CIL_USER:
1187 return CIL_KEY_USER;
1188 case CIL_USERATTRIBUTE:
1189 return CIL_KEY_USERATTRIBUTE;
1190 case CIL_USERATTRIBUTESET:
1191 return CIL_KEY_USERATTRIBUTESET;
1192 case CIL_USERPREFIX:
1193 return CIL_KEY_USERPREFIX;
1194 case CIL_USERROLE:
1195 return CIL_KEY_USERROLE;
1196 case CIL_USERLEVEL:
1197 return CIL_KEY_USERLEVEL;
1198 case CIL_USERRANGE:
1199 return CIL_KEY_USERRANGE;
1200 case CIL_USERBOUNDS:
1201 return CIL_KEY_USERBOUNDS;
1202 case CIL_SELINUXUSER:
1203 return CIL_KEY_SELINUXUSER;
1204 case CIL_SELINUXUSERDEFAULT:
1205 return CIL_KEY_SELINUXUSERDEFAULT;
1206 case CIL_ROLE:
1207 return CIL_KEY_ROLE;
1208 case CIL_ROLEATTRIBUTE:
1209 return CIL_KEY_ROLEATTRIBUTE;
1210 case CIL_ROLEATTRIBUTESET:
1211 return CIL_KEY_ROLEATTRIBUTESET;
1212 case CIL_ROLETYPE:
1213 return CIL_KEY_ROLETYPE;
1214 case CIL_ROLEBOUNDS:
1215 return CIL_KEY_ROLEBOUNDS;
1216 case CIL_TYPE:
1217 return CIL_KEY_TYPE;
1218 case CIL_TYPEATTRIBUTE:
1219 return CIL_KEY_TYPEATTRIBUTE;
1220 case CIL_TYPEALIAS:
1221 return CIL_KEY_TYPEALIAS;
1222 case CIL_TYPEATTRIBUTESET:
1223 return CIL_KEY_TYPEATTRIBUTESET;
1224 case CIL_EXPANDTYPEATTRIBUTE:
1225 return CIL_KEY_EXPANDTYPEATTRIBUTE;
1226 case CIL_TYPEALIASACTUAL:
1227 return CIL_KEY_TYPEALIASACTUAL;
1228 case CIL_TYPEBOUNDS:
1229 return CIL_KEY_TYPEBOUNDS;
1230 case CIL_TYPEPERMISSIVE:
1231 return CIL_KEY_TYPEPERMISSIVE;
1232 case CIL_SENS:
1233 return CIL_KEY_SENSITIVITY;
1234 case CIL_SENSALIAS:
1235 return CIL_KEY_SENSALIAS;
1236 case CIL_SENSALIASACTUAL:
1237 return CIL_KEY_SENSALIASACTUAL;
1238 case CIL_SENSITIVITYORDER:
1239 return CIL_KEY_SENSITIVITYORDER;
1240 case CIL_SENSCAT:
1241 return CIL_KEY_SENSCAT;
1242 case CIL_CAT:
1243 return CIL_KEY_CATEGORY;
1244 case CIL_CATSET:
1245 return CIL_KEY_CATSET;
1246 case CIL_CATALIAS:
1247 return CIL_KEY_CATALIAS;
1248 case CIL_CATALIASACTUAL:
1249 return CIL_KEY_CATALIASACTUAL;
1250 case CIL_CATORDER:
1251 return CIL_KEY_CATORDER;
1252 case CIL_LEVEL:
1253 return CIL_KEY_LEVEL;
1254 case CIL_LEVELRANGE:
1255 return CIL_KEY_LEVELRANGE;
1256 case CIL_SID:
1257 return CIL_KEY_SID;
1258 case CIL_SIDORDER:
1259 return CIL_KEY_SIDORDER;
1260 case CIL_NAME:
1261 return CIL_KEY_NAME;
1262 case CIL_ROLEALLOW:
1263 return CIL_KEY_ROLEALLOW;
1264 case CIL_AVRULE:
1265 switch (((struct cil_avrule *)node->data)->rule_kind) {
1266 case CIL_AVRULE_ALLOWED:
1267 return CIL_KEY_ALLOW;
1268 case CIL_AVRULE_AUDITALLOW:
1269 return CIL_KEY_AUDITALLOW;
1270 case CIL_AVRULE_DONTAUDIT:
1271 return CIL_KEY_DONTAUDIT;
1272 case CIL_AVRULE_NEVERALLOW:
1273 return CIL_KEY_NEVERALLOW;
1274 default:
1275 break;
1276 }
1277 break;
1278 case CIL_AVRULEX:
1279 switch (((struct cil_avrule *)node->data)->rule_kind) {
1280 case CIL_AVRULE_ALLOWED:
1281 return CIL_KEY_ALLOWX;
1282 case CIL_AVRULE_AUDITALLOW:
1283 return CIL_KEY_AUDITALLOWX;
1284 case CIL_AVRULE_DONTAUDIT:
1285 return CIL_KEY_DONTAUDITX;
1286 case CIL_AVRULE_NEVERALLOW:
1287 return CIL_KEY_NEVERALLOWX;
1288 default:
1289 break;
1290 }
1291 break;
1292 case CIL_PERMISSIONX:
1293 return CIL_KEY_PERMISSIONX;
1294 case CIL_ROLETRANSITION:
1295 return CIL_KEY_ROLETRANSITION;
1296 case CIL_TYPE_RULE:
1297 switch (((struct cil_type_rule *)node->data)->rule_kind) {
1298 case CIL_TYPE_TRANSITION:
1299 return CIL_KEY_TYPETRANSITION;
1300 case CIL_TYPE_MEMBER:
1301 return CIL_KEY_TYPEMEMBER;
1302 case CIL_TYPE_CHANGE:
1303 return CIL_KEY_TYPECHANGE;
1304 default:
1305 break;
1306 }
1307 break;
1308 case CIL_NAMETYPETRANSITION:
1309 return CIL_KEY_TYPETRANSITION;
1310 case CIL_RANGETRANSITION:
1311 return CIL_KEY_RANGETRANSITION;
1312 case CIL_CONSTRAIN:
1313 return CIL_KEY_CONSTRAIN;
1314 case CIL_MLSCONSTRAIN:
1315 return CIL_KEY_MLSCONSTRAIN;
1316 case CIL_VALIDATETRANS:
1317 return CIL_KEY_VALIDATETRANS;
1318 case CIL_MLSVALIDATETRANS:
1319 return CIL_KEY_MLSVALIDATETRANS;
1320 case CIL_CONTEXT:
1321 return CIL_KEY_CONTEXT;
1322 case CIL_IPADDR:
1323 return CIL_KEY_IPADDR;
1324 case CIL_SIDCONTEXT:
1325 return CIL_KEY_SIDCONTEXT;
1326 case CIL_FSUSE:
1327 return CIL_KEY_FSUSE;
1328 case CIL_FILECON:
1329 return CIL_KEY_FILECON;
1330 case CIL_IBPKEYCON:
1331 return CIL_KEY_IBPKEYCON;
1332 case CIL_IBENDPORTCON:
1333 return CIL_KEY_IBENDPORTCON;
1334 case CIL_PORTCON:
1335 return CIL_KEY_PORTCON;
1336 case CIL_NODECON:
1337 return CIL_KEY_NODECON;
1338 case CIL_GENFSCON:
1339 return CIL_KEY_GENFSCON;
1340 case CIL_NETIFCON:
1341 return CIL_KEY_NETIFCON;
1342 case CIL_PIRQCON:
1343 return CIL_KEY_PIRQCON;
1344 case CIL_IOMEMCON:
1345 return CIL_KEY_IOMEMCON;
1346 case CIL_IOPORTCON:
1347 return CIL_KEY_IOPORTCON;
1348 case CIL_PCIDEVICECON:
1349 return CIL_KEY_PCIDEVICECON;
1350 case CIL_DEVICETREECON:
1351 return CIL_KEY_DEVICETREECON;
1352 case CIL_POLICYCAP:
1353 return CIL_KEY_POLICYCAP;
1354 case CIL_DEFAULTUSER:
1355 return CIL_KEY_DEFAULTUSER;
1356 case CIL_DEFAULTROLE:
1357 return CIL_KEY_DEFAULTROLE;
1358 case CIL_DEFAULTTYPE:
1359 return CIL_KEY_DEFAULTTYPE;
1360 case CIL_DEFAULTRANGE:
1361 return CIL_KEY_DEFAULTRANGE;
1362 case CIL_HANDLEUNKNOWN:
1363 return CIL_KEY_HANDLEUNKNOWN;
1364 case CIL_MLS:
1365 return CIL_KEY_MLS;
1366 case CIL_SRC_INFO:
1367 return CIL_KEY_SRC_INFO;
1368 case CIL_ALL:
1369 return CIL_KEY_ALL;
1370 case CIL_RANGE:
1371 return CIL_KEY_RANGE;
1372 case CIL_AND:
1373 return CIL_KEY_AND;
1374 case CIL_OR:
1375 return CIL_KEY_OR;
1376 case CIL_XOR:
1377 return CIL_KEY_XOR;
1378 case CIL_NOT:
1379 return CIL_KEY_NOT;
1380 case CIL_EQ:
1381 return CIL_KEY_EQ;
1382 case CIL_NEQ:
1383 return CIL_KEY_NEQ;
1384 case CIL_CONS_DOM:
1385 return CIL_KEY_CONS_DOM;
1386 case CIL_CONS_DOMBY:
1387 return CIL_KEY_CONS_DOMBY;
1388 case CIL_CONS_INCOMP:
1389 return CIL_KEY_CONS_INCOMP;
1390 case CIL_CONS_U1:
1391 return CIL_KEY_CONS_U1;
1392 case CIL_CONS_U2:
1393 return CIL_KEY_CONS_U2;
1394 case CIL_CONS_U3:
1395 return CIL_KEY_CONS_U3;
1396 case CIL_CONS_T1:
1397 return CIL_KEY_CONS_T1;
1398 case CIL_CONS_T2:
1399 return CIL_KEY_CONS_T2;
1400 case CIL_CONS_T3:
1401 return CIL_KEY_CONS_T3;
1402 case CIL_CONS_R1:
1403 return CIL_KEY_CONS_R1;
1404 case CIL_CONS_R2:
1405 return CIL_KEY_CONS_R2;
1406 case CIL_CONS_R3:
1407 return CIL_KEY_CONS_R3;
1408 case CIL_CONS_L1:
1409 return CIL_KEY_CONS_L1;
1410 case CIL_CONS_L2:
1411 return CIL_KEY_CONS_L2;
1412 case CIL_CONS_H1:
1413 return CIL_KEY_CONS_H1;
1414 case CIL_CONS_H2:
1415 return CIL_KEY_CONS_H2;
1416
1417 default:
1418 break;
1419 }
1420
1421 return "<unknown>";
1422 }
1423
cil_userprefixes_to_string(struct cil_db * db,char ** out,size_t * size)1424 int cil_userprefixes_to_string(struct cil_db *db, char **out, size_t *size)
1425 {
1426 int rc = SEPOL_ERR;
1427 size_t str_len = 0;
1428 int buf_pos = 0;
1429 char *str_tmp = NULL;
1430 struct cil_list_item *curr;
1431 struct cil_userprefix *userprefix = NULL;
1432 struct cil_user *user = NULL;
1433
1434 *out = NULL;
1435
1436 if (db->userprefixes->head == NULL) {
1437 rc = SEPOL_OK;
1438 *size = 0;
1439 goto exit;
1440 }
1441
1442 cil_list_for_each(curr, db->userprefixes) {
1443 userprefix = curr->data;
1444 user = userprefix->user;
1445 str_len += strlen("user ") + strlen(user->datum.fqn) + strlen(" prefix ") + strlen(userprefix->prefix_str) + 2;
1446 }
1447
1448 *size = str_len * sizeof(char);
1449 str_len++;
1450 str_tmp = cil_malloc(str_len * sizeof(char));
1451 *out = str_tmp;
1452
1453 cil_list_for_each(curr, db->userprefixes) {
1454 userprefix = curr->data;
1455 user = userprefix->user;
1456
1457 buf_pos = snprintf(str_tmp, str_len, "user %s prefix %s;\n", user->datum.fqn,
1458 userprefix->prefix_str);
1459 str_len -= buf_pos;
1460 str_tmp += buf_pos;
1461 }
1462
1463 rc = SEPOL_OK;
1464 exit:
1465 return rc;
1466
1467 }
1468
cil_cats_to_ebitmap(struct cil_cats * cats,struct ebitmap * cats_ebitmap)1469 static int cil_cats_to_ebitmap(struct cil_cats *cats, struct ebitmap* cats_ebitmap)
1470 {
1471 int rc = SEPOL_ERR;
1472 struct cil_list_item *i;
1473 struct cil_list_item *j;
1474 struct cil_cat* cat;
1475 struct cil_catset *cs;
1476 struct cil_tree_node *node;
1477
1478 if (cats == NULL) {
1479 rc = SEPOL_OK;
1480 goto exit;
1481 }
1482
1483 cil_list_for_each(i, cats->datum_expr) {
1484 node = NODE(i->data);
1485 if (node->flavor == CIL_CATSET) {
1486 cs = (struct cil_catset*)i->data;
1487 cil_list_for_each(j, cs->cats->datum_expr) {
1488 cat = (struct cil_cat*)j->data;
1489 rc = ebitmap_set_bit(cats_ebitmap, cat->value, 1);
1490 if (rc != SEPOL_OK) {
1491 goto exit;
1492 }
1493 }
1494 } else {
1495 cat = (struct cil_cat*)i->data;
1496 rc = ebitmap_set_bit(cats_ebitmap, cat->value, 1);
1497 if (rc != SEPOL_OK) {
1498 goto exit;
1499 }
1500 }
1501 }
1502
1503 return SEPOL_OK;
1504
1505 exit:
1506 return rc;
1507 }
1508
cil_level_equals(struct cil_level * low,struct cil_level * high)1509 static int cil_level_equals(struct cil_level *low, struct cil_level *high)
1510 {
1511 int rc;
1512 struct ebitmap elow;
1513 struct ebitmap ehigh;
1514
1515 if (strcmp(low->sens->datum.fqn, high->sens->datum.fqn)) {
1516 rc = 0;
1517 goto exit;
1518 }
1519
1520 ebitmap_init(&elow);
1521 ebitmap_init(&ehigh);
1522
1523 rc = cil_cats_to_ebitmap(low->cats, &elow);
1524 if (rc != SEPOL_OK) {
1525 goto exit;
1526 }
1527
1528 rc = cil_cats_to_ebitmap(high->cats, &ehigh);
1529 if (rc != SEPOL_OK) {
1530 goto exit;
1531 }
1532
1533 rc = ebitmap_cmp(&elow, &ehigh);
1534 ebitmap_destroy(&elow);
1535 ebitmap_destroy(&ehigh);
1536
1537 exit:
1538 return rc;
1539 }
1540
__cil_level_strlen(struct cil_level * lvl)1541 static int __cil_level_strlen(struct cil_level *lvl)
1542 {
1543 struct cil_list_item *item;
1544 struct cil_cats *cats = lvl->cats;
1545 int str_len = 0;
1546 char *str1 = NULL;
1547 char *str2 = NULL;
1548 int first = -1;
1549 int last = -1;
1550
1551 str_len += strlen(lvl->sens->datum.fqn);
1552
1553 if (cats && cats->datum_expr != NULL) {
1554 str_len++; /* initial ":" */
1555 cil_list_for_each(item, cats->datum_expr) {
1556 struct cil_cat *cat = item->data;
1557 if (first == -1) {
1558 str1 = cat->datum.fqn;
1559 first = cat->value;
1560 last = first;
1561 } else if (cat->value == last + 1) {
1562 last++;
1563 str2 = cat->datum.fqn;
1564 } else {
1565 if (first == last) {
1566 str_len += strlen(str1) + strlen(cat->datum.fqn) + 1;
1567 } else if (last == first + 1) {
1568 str_len += strlen(str1) + strlen(str2) + strlen(cat->datum.fqn) + 2;
1569 } else {
1570 str_len += strlen(str1) + strlen(str2) + strlen(cat->datum.fqn) + 2;
1571 }
1572 first = -1;
1573 last = -1;
1574 if (item->next != NULL) {
1575 str_len++; /* space for "," after */
1576 }
1577 }
1578 }
1579 if (first != -1) {
1580 if (first == last) {
1581 str_len += strlen(str1);
1582 } else if (last == first + 1) {
1583 str_len += strlen(str1) + strlen(str2) + 1;
1584 } else {
1585 str_len += strlen(str1) + strlen(str2) + 1;
1586 }
1587 }
1588 }
1589
1590 return str_len;
1591 }
1592
__cil_level_to_string(struct cil_level * lvl,char * out)1593 static int __cil_level_to_string(struct cil_level *lvl, char *out)
1594 {
1595 struct cil_list_item *item;
1596 struct cil_cats *cats = lvl->cats;
1597 int buf_pos = 0;
1598 char *str_tmp = out;
1599 char *str1 = NULL;
1600 char *str2 = NULL;
1601 int first = -1;
1602 int last = -1;
1603
1604 buf_pos = sprintf(str_tmp, "%s", lvl->sens->datum.fqn);
1605 str_tmp += buf_pos;
1606
1607 if (cats && cats->datum_expr != NULL) {
1608 buf_pos = sprintf(str_tmp, ":");
1609 str_tmp += buf_pos;
1610
1611 cil_list_for_each(item, cats->datum_expr) {
1612 struct cil_cat *cat = item->data;
1613 if (first == -1) {
1614 str1 = cat->datum.fqn;
1615 first = cat->value;
1616 last = first;
1617 } else if (cat->value == last + 1) {
1618 last++;
1619 str2 = cat->datum.fqn;
1620 } else {
1621 if (first == last) {
1622 buf_pos = sprintf(str_tmp, "%s,%s", str1, cat->datum.fqn);
1623 str_tmp += buf_pos;
1624 } else if (last == first + 1) {
1625 buf_pos = sprintf(str_tmp, "%s,%s,%s", str1, str2, cat->datum.fqn);
1626 str_tmp += buf_pos;
1627 } else {
1628 buf_pos = sprintf(str_tmp, "%s.%s,%s",str1, str2, cat->datum.fqn);
1629 str_tmp += buf_pos;
1630 }
1631 first = -1;
1632 last = -1;
1633 if (item->next != NULL) {
1634 buf_pos = sprintf(str_tmp, ",");
1635 str_tmp += buf_pos;
1636 }
1637 }
1638 }
1639 if (first != -1) {
1640 if (first == last) {
1641 buf_pos = sprintf(str_tmp, "%s", str1);
1642 str_tmp += buf_pos;
1643 } else if (last == first + 1) {
1644 buf_pos = sprintf(str_tmp, "%s,%s", str1, str2);
1645 str_tmp += buf_pos;
1646 } else {
1647 buf_pos = sprintf(str_tmp, "%s.%s",str1, str2);
1648 str_tmp += buf_pos;
1649 }
1650 }
1651 }
1652
1653 return str_tmp - out;
1654 }
1655
cil_selinuxusers_to_string(struct cil_db * db,char ** out,size_t * size)1656 int cil_selinuxusers_to_string(struct cil_db *db, char **out, size_t *size)
1657 {
1658 size_t str_len = 0;
1659 int buf_pos = 0;
1660 char *str_tmp = NULL;
1661 struct cil_list_item *curr;
1662
1663 if (db->selinuxusers->head == NULL) {
1664 *size = 0;
1665 *out = NULL;
1666 return SEPOL_OK;
1667 }
1668
1669 cil_list_for_each(curr, db->selinuxusers) {
1670 struct cil_selinuxuser *selinuxuser = curr->data;
1671 struct cil_user *user = selinuxuser->user;
1672
1673 str_len += strlen(selinuxuser->name_str) + strlen(user->datum.fqn) + 1;
1674
1675 if (db->mls == CIL_TRUE) {
1676 struct cil_levelrange *range = selinuxuser->range;
1677 str_len += __cil_level_strlen(range->low) + __cil_level_strlen(range->high) + 2;
1678 }
1679
1680 str_len++;
1681 }
1682
1683 *size = str_len * sizeof(char);
1684 str_tmp = cil_malloc(*size+1);
1685 *out = str_tmp;
1686
1687 for(curr = db->selinuxusers->head; curr != NULL; curr = curr->next) {
1688 struct cil_selinuxuser *selinuxuser = curr->data;
1689 struct cil_user *user = selinuxuser->user;
1690
1691 buf_pos = sprintf(str_tmp, "%s:%s", selinuxuser->name_str, user->datum.fqn);
1692 str_tmp += buf_pos;
1693
1694 if (db->mls == CIL_TRUE) {
1695 struct cil_levelrange *range = selinuxuser->range;
1696 buf_pos = sprintf(str_tmp, ":");
1697 str_tmp += buf_pos;
1698 buf_pos = __cil_level_to_string(range->low, str_tmp);
1699 str_tmp += buf_pos;
1700 buf_pos = sprintf(str_tmp, "-");
1701 str_tmp += buf_pos;
1702 buf_pos = __cil_level_to_string(range->high, str_tmp);
1703 str_tmp += buf_pos;
1704 }
1705
1706 buf_pos = sprintf(str_tmp, "\n");
1707 str_tmp += buf_pos;
1708 }
1709
1710 return SEPOL_OK;
1711 }
1712
cil_filecons_to_string(struct cil_db * db,char ** out,size_t * size)1713 int cil_filecons_to_string(struct cil_db *db, char **out, size_t *size)
1714 {
1715 uint32_t i = 0;
1716 int buf_pos = 0;
1717 size_t str_len = 0;
1718 char *str_tmp = NULL;
1719 struct cil_sort *filecons = db->filecon;
1720
1721 for (i = 0; i < filecons->count; i++) {
1722 struct cil_filecon *filecon = filecons->array[i];
1723 struct cil_context *ctx = filecon->context;
1724
1725 str_len += strlen(filecon->path_str);
1726
1727 if (filecon->type != CIL_FILECON_ANY) {
1728 /* If a type is specified,
1729 +2 for type string, +1 for tab */
1730 str_len += 3;
1731 }
1732
1733 if (ctx != NULL) {
1734 struct cil_user *user = ctx->user;
1735 struct cil_role *role = ctx->role;
1736 struct cil_type *type = ctx->type;
1737
1738 str_len += (strlen(user->datum.fqn) + strlen(role->datum.fqn) + strlen(type->datum.fqn) + 3);
1739
1740 if (db->mls == CIL_TRUE) {
1741 struct cil_levelrange *range = ctx->range;
1742 if (cil_level_equals(range->low, range->high)) {
1743 str_len += __cil_level_strlen(range->low) + 1;
1744 } else {
1745 str_len += __cil_level_strlen(range->low) + __cil_level_strlen(range->high) + 2;
1746 }
1747 }
1748 } else {
1749 str_len += strlen("\t<<none>>");
1750 }
1751
1752 str_len++;
1753 }
1754
1755 *size = str_len * sizeof(char);
1756 str_tmp = cil_malloc(*size+1);
1757 *out = str_tmp;
1758
1759 for (i = 0; i < filecons->count; i++) {
1760 struct cil_filecon *filecon = filecons->array[i];
1761 struct cil_context *ctx = filecon->context;
1762 const char *str_type = NULL;
1763
1764 buf_pos = sprintf(str_tmp, "%s", filecon->path_str);
1765 str_tmp += buf_pos;
1766
1767 switch(filecon->type) {
1768 case CIL_FILECON_FILE:
1769 str_type = "\t--";
1770 break;
1771 case CIL_FILECON_DIR:
1772 str_type = "\t-d";
1773 break;
1774 case CIL_FILECON_CHAR:
1775 str_type = "\t-c";
1776 break;
1777 case CIL_FILECON_BLOCK:
1778 str_type = "\t-b";
1779 break;
1780 case CIL_FILECON_SOCKET:
1781 str_type = "\t-s";
1782 break;
1783 case CIL_FILECON_PIPE:
1784 str_type = "\t-p";
1785 break;
1786 case CIL_FILECON_SYMLINK:
1787 str_type = "\t-l";
1788 break;
1789 default:
1790 str_type = "";
1791 break;
1792 }
1793 buf_pos = sprintf(str_tmp, "%s", str_type);
1794 str_tmp += buf_pos;
1795
1796 if (ctx != NULL) {
1797 struct cil_user *user = ctx->user;
1798 struct cil_role *role = ctx->role;
1799 struct cil_type *type = ctx->type;
1800
1801 buf_pos = sprintf(str_tmp, "\t%s:%s:%s", user->datum.fqn, role->datum.fqn,
1802 type->datum.fqn);
1803 str_tmp += buf_pos;
1804
1805 if (db->mls == CIL_TRUE) {
1806 struct cil_levelrange *range = ctx->range;
1807 buf_pos = sprintf(str_tmp, ":");
1808 str_tmp += buf_pos;
1809 buf_pos = __cil_level_to_string(range->low, str_tmp);
1810 str_tmp += buf_pos;
1811
1812 if (!cil_level_equals(range->low, range->high)) {
1813 buf_pos = sprintf(str_tmp, "-");
1814 str_tmp += buf_pos;
1815 buf_pos = __cil_level_to_string(range->high, str_tmp);
1816 str_tmp += buf_pos;
1817 }
1818 }
1819 } else {
1820 buf_pos = sprintf(str_tmp, "\t<<none>>");
1821 str_tmp += buf_pos;
1822 }
1823
1824 buf_pos = sprintf(str_tmp, "\n");
1825 str_tmp += buf_pos;
1826 }
1827
1828 return SEPOL_OK;
1829 }
1830
cil_set_disable_dontaudit(struct cil_db * db,int disable_dontaudit)1831 void cil_set_disable_dontaudit(struct cil_db *db, int disable_dontaudit)
1832 {
1833 db->disable_dontaudit = disable_dontaudit;
1834 }
1835
cil_set_disable_neverallow(struct cil_db * db,int disable_neverallow)1836 void cil_set_disable_neverallow(struct cil_db *db, int disable_neverallow)
1837 {
1838 db->disable_neverallow = disable_neverallow;
1839 }
1840
cil_set_attrs_expand_generated(struct cil_db * db,int attrs_expand_generated)1841 void cil_set_attrs_expand_generated(struct cil_db *db, int attrs_expand_generated)
1842 {
1843 db->attrs_expand_generated = attrs_expand_generated;
1844 }
1845
cil_set_attrs_expand_size(struct cil_db * db,unsigned attrs_expand_size)1846 void cil_set_attrs_expand_size(struct cil_db *db, unsigned attrs_expand_size)
1847 {
1848 db->attrs_expand_size = attrs_expand_size;
1849 }
1850
cil_set_preserve_tunables(struct cil_db * db,int preserve_tunables)1851 void cil_set_preserve_tunables(struct cil_db *db, int preserve_tunables)
1852 {
1853 db->preserve_tunables = preserve_tunables;
1854 }
1855
cil_set_handle_unknown(struct cil_db * db,int handle_unknown)1856 int cil_set_handle_unknown(struct cil_db *db, int handle_unknown)
1857 {
1858 int rc = 0;
1859
1860 switch (handle_unknown) {
1861 case SEPOL_DENY_UNKNOWN:
1862 case SEPOL_REJECT_UNKNOWN:
1863 case SEPOL_ALLOW_UNKNOWN:
1864 db->handle_unknown = handle_unknown;
1865 break;
1866 default:
1867 cil_log(CIL_ERR, "Unknown value for handle-unknown: %i\n", handle_unknown);
1868 rc = -1;
1869 }
1870
1871 return rc;
1872 }
1873
cil_set_mls(struct cil_db * db,int mls)1874 void cil_set_mls(struct cil_db *db, int mls)
1875 {
1876 db->mls = mls;
1877 }
1878
cil_set_multiple_decls(struct cil_db * db,int multiple_decls)1879 void cil_set_multiple_decls(struct cil_db *db, int multiple_decls)
1880 {
1881 db->multiple_decls = multiple_decls;
1882 }
1883
cil_set_qualified_names(struct cil_db * db,int qualified_names)1884 void cil_set_qualified_names(struct cil_db *db, int qualified_names)
1885 {
1886 db->qualified_names = qualified_names;
1887 }
1888
cil_set_target_platform(struct cil_db * db,int target_platform)1889 void cil_set_target_platform(struct cil_db *db, int target_platform)
1890 {
1891 db->target_platform = target_platform;
1892 }
1893
cil_set_policy_version(struct cil_db * db,int policy_version)1894 void cil_set_policy_version(struct cil_db *db, int policy_version)
1895 {
1896 db->policy_version = policy_version;
1897 }
1898
cil_symtab_array_init(symtab_t symtab[],const int symtab_sizes[CIL_SYM_NUM])1899 void cil_symtab_array_init(symtab_t symtab[], const int symtab_sizes[CIL_SYM_NUM])
1900 {
1901 uint32_t i = 0;
1902 for (i = 0; i < CIL_SYM_NUM; i++) {
1903 cil_symtab_init(&symtab[i], symtab_sizes[i]);
1904 }
1905 }
1906
cil_symtab_array_destroy(symtab_t symtab[])1907 void cil_symtab_array_destroy(symtab_t symtab[])
1908 {
1909 int i = 0;
1910 for (i = 0; i < CIL_SYM_NUM; i++) {
1911 cil_symtab_destroy(&symtab[i]);
1912 }
1913 }
1914
cil_destroy_ast_symtabs(struct cil_tree_node * current)1915 void cil_destroy_ast_symtabs(struct cil_tree_node *current)
1916 {
1917 while (current) {
1918 switch (current->flavor) {
1919 case CIL_BLOCK:
1920 cil_symtab_array_destroy(((struct cil_block*)current->data)->symtab);
1921 break;
1922 case CIL_IN:
1923 cil_symtab_array_destroy(((struct cil_in*)current->data)->symtab);
1924 break;
1925 case CIL_CLASS:
1926 case CIL_COMMON:
1927 case CIL_MAP_CLASS:
1928 cil_symtab_destroy(&((struct cil_class*)current->data)->perms);
1929 break;
1930 case CIL_MACRO:
1931 cil_symtab_array_destroy(((struct cil_macro*)current->data)->symtab);
1932 break;
1933 case CIL_CONDBLOCK:
1934 cil_symtab_array_destroy(((struct cil_condblock*)current->data)->symtab);
1935 break;
1936 default:
1937 break;
1938 }
1939
1940 if (current->cl_head) {
1941 cil_destroy_ast_symtabs(current->cl_head);
1942 }
1943
1944 current = current->next;
1945 }
1946 }
1947
cil_get_symtab(struct cil_tree_node * ast_node,symtab_t ** symtab,enum cil_sym_index sym_index)1948 int cil_get_symtab(struct cil_tree_node *ast_node, symtab_t **symtab, enum cil_sym_index sym_index)
1949 {
1950 struct cil_tree_node *node = ast_node;
1951 *symtab = NULL;
1952
1953 if (sym_index == CIL_SYM_PERMS) {
1954 /* Class statements are not blocks, so the passed node should be the class */
1955 if (node->flavor == CIL_CLASS || node->flavor == CIL_MAP_CLASS ||
1956 node->flavor == CIL_COMMON) {
1957 *symtab = &((struct cil_class*)node->data)->perms;
1958 return SEPOL_OK;
1959 }
1960 goto exit;
1961 }
1962
1963 if (sym_index < CIL_SYM_BLOCKS || sym_index >= CIL_SYM_NUM) {
1964 cil_log(CIL_ERR, "Invalid symtab type\n");
1965 goto exit;
1966 }
1967
1968 while (node != NULL && *symtab == NULL) {
1969 switch (node->flavor) {
1970 case CIL_ROOT:
1971 *symtab = &((struct cil_root *)node->data)->symtab[sym_index];
1972 break;
1973 case CIL_BLOCK:
1974 *symtab = &((struct cil_block*)node->data)->symtab[sym_index];
1975 break;
1976 case CIL_MACRO:
1977 *symtab = &((struct cil_macro*)node->data)->symtab[sym_index];
1978 break;
1979 case CIL_IN:
1980 /* In blocks only exist before resolving the AST */
1981 *symtab = &((struct cil_in*)node->data)->symtab[sym_index];
1982 break;
1983 case CIL_CONDBLOCK: {
1984 if (node->parent->flavor == CIL_TUNABLEIF) {
1985 /* Cond blocks only exist before resolving the AST */
1986 *symtab = &((struct cil_condblock*)node->data)->symtab[sym_index];
1987 } else if (node->parent->flavor == CIL_BOOLEANIF) {
1988 node = node->parent->parent;
1989 }
1990 break;
1991 }
1992 default:
1993 node = node->parent;
1994 }
1995 }
1996
1997 if (*symtab == NULL) {
1998 goto exit;
1999 }
2000
2001 return SEPOL_OK;
2002
2003 exit:
2004 cil_tree_log(ast_node, CIL_ERR, "Failed to get symtab from node");
2005 return SEPOL_ERR;
2006 }
2007
cil_string_to_uint32(const char * string,uint32_t * value,int base)2008 int cil_string_to_uint32(const char *string, uint32_t *value, int base)
2009 {
2010 unsigned long val;
2011 char *end = NULL;
2012 int rc = SEPOL_ERR;
2013
2014 if (string == NULL || value == NULL) {
2015 goto exit;
2016 }
2017
2018 errno = 0;
2019 val = strtoul(string, &end, base);
2020 if (errno != 0 || end == string || *end != '\0') {
2021 rc = SEPOL_ERR;
2022 goto exit;
2023 }
2024
2025 /* Ensure that the value fits a 32-bit integer without triggering -Wtype-limits */
2026 #if ULONG_MAX > UINT32_MAX
2027 if (val > UINT32_MAX) {
2028 rc = SEPOL_ERR;
2029 goto exit;
2030 }
2031 #endif
2032
2033 *value = val;
2034
2035 return SEPOL_OK;
2036
2037 exit:
2038 cil_log(CIL_ERR, "Failed to create uint32_t from string\n");
2039 return rc;
2040 }
2041
cil_string_to_uint64(const char * string,uint64_t * value,int base)2042 int cil_string_to_uint64(const char *string, uint64_t *value, int base)
2043 {
2044 char *end = NULL;
2045 int rc = SEPOL_ERR;
2046
2047 if (string == NULL || value == NULL) {
2048 goto exit;
2049 }
2050
2051 errno = 0;
2052 *value = strtoull(string, &end, base);
2053 if (errno != 0 || end == string || *end != '\0') {
2054 rc = SEPOL_ERR;
2055 goto exit;
2056 }
2057
2058 return SEPOL_OK;
2059
2060 exit:
2061 cil_log(CIL_ERR, "Failed to create uint64_t from string\n");
2062 return rc;
2063 }
2064
cil_sort_init(struct cil_sort ** sort)2065 void cil_sort_init(struct cil_sort **sort)
2066 {
2067 *sort = cil_malloc(sizeof(**sort));
2068
2069 (*sort)->flavor = CIL_NONE;
2070 (*sort)->count = 0;
2071 (*sort)->index = 0;
2072 (*sort)->array = NULL;
2073 }
2074
cil_sort_destroy(struct cil_sort ** sort)2075 void cil_sort_destroy(struct cil_sort **sort)
2076 {
2077 (*sort)->flavor = CIL_NONE;
2078 (*sort)->count = 0;
2079 (*sort)->index = 0;
2080 if ((*sort)->array != NULL) {
2081 free((*sort)->array);
2082 }
2083 (*sort)->array = NULL;
2084
2085 free(*sort);
2086 *sort = NULL;
2087 }
2088
cil_netifcon_init(struct cil_netifcon ** netifcon)2089 void cil_netifcon_init(struct cil_netifcon **netifcon)
2090 {
2091 *netifcon = cil_malloc(sizeof(**netifcon));
2092
2093 (*netifcon)->interface_str = NULL;
2094 (*netifcon)->if_context_str = NULL;
2095 (*netifcon)->if_context = NULL;
2096 (*netifcon)->packet_context_str = NULL;
2097 (*netifcon)->packet_context = NULL;
2098 (*netifcon)->context_str = NULL;
2099 }
2100
cil_ibendportcon_init(struct cil_ibendportcon ** ibendportcon)2101 void cil_ibendportcon_init(struct cil_ibendportcon **ibendportcon)
2102 {
2103 *ibendportcon = cil_malloc(sizeof(**ibendportcon));
2104
2105 (*ibendportcon)->dev_name_str = NULL;
2106 (*ibendportcon)->port = 0;
2107 (*ibendportcon)->context_str = NULL;
2108 (*ibendportcon)->context = NULL;
2109 }
2110
cil_context_init(struct cil_context ** context)2111 void cil_context_init(struct cil_context **context)
2112 {
2113 *context = cil_malloc(sizeof(**context));
2114
2115 cil_symtab_datum_init(&(*context)->datum);
2116 (*context)->user_str = NULL;
2117 (*context)->user = NULL;
2118 (*context)->role_str = NULL;
2119 (*context)->role = NULL;
2120 (*context)->type_str = NULL;
2121 (*context)->type = NULL;
2122 (*context)->range_str = NULL;
2123 (*context)->range = NULL;
2124 }
2125
cil_level_init(struct cil_level ** level)2126 void cil_level_init(struct cil_level **level)
2127 {
2128 *level = cil_malloc(sizeof(**level));
2129
2130 cil_symtab_datum_init(&(*level)->datum);
2131 (*level)->sens_str = NULL;
2132 (*level)->sens = NULL;
2133 (*level)->cats = NULL;
2134 }
2135
cil_levelrange_init(struct cil_levelrange ** range)2136 void cil_levelrange_init(struct cil_levelrange **range)
2137 {
2138 *range = cil_malloc(sizeof(**range));
2139
2140 cil_symtab_datum_init(&(*range)->datum);
2141 (*range)->low_str = NULL;
2142 (*range)->low = NULL;
2143 (*range)->high_str = NULL;
2144 (*range)->high = NULL;
2145 }
2146
cil_sens_init(struct cil_sens ** sens)2147 void cil_sens_init(struct cil_sens **sens)
2148 {
2149 *sens = cil_malloc(sizeof(**sens));
2150
2151 cil_symtab_datum_init(&(*sens)->datum);
2152
2153 (*sens)->cats_list = NULL;
2154
2155 (*sens)->ordered = CIL_FALSE;
2156 }
2157
cil_block_init(struct cil_block ** block)2158 void cil_block_init(struct cil_block **block)
2159 {
2160 *block = cil_malloc(sizeof(**block));
2161
2162 cil_symtab_datum_init(&(*block)->datum);
2163
2164 cil_symtab_array_init((*block)->symtab, cil_sym_sizes[CIL_SYM_ARRAY_BLOCK]);
2165
2166 (*block)->is_abstract = CIL_FALSE;
2167
2168 (*block)->bi_nodes = NULL;
2169 }
2170
cil_blockinherit_init(struct cil_blockinherit ** inherit)2171 void cil_blockinherit_init(struct cil_blockinherit **inherit)
2172 {
2173 *inherit = cil_malloc(sizeof(**inherit));
2174 (*inherit)->block_str = NULL;
2175 (*inherit)->block = NULL;
2176 }
2177
cil_blockabstract_init(struct cil_blockabstract ** abstract)2178 void cil_blockabstract_init(struct cil_blockabstract **abstract)
2179 {
2180 *abstract = cil_malloc(sizeof(**abstract));
2181 (*abstract)->block_str = NULL;
2182 }
2183
cil_in_init(struct cil_in ** in)2184 void cil_in_init(struct cil_in **in)
2185 {
2186 *in = cil_malloc(sizeof(**in));
2187
2188 cil_symtab_array_init((*in)->symtab, cil_sym_sizes[CIL_SYM_ARRAY_IN]);
2189 (*in)->is_after = CIL_FALSE;
2190 (*in)->block_str = NULL;
2191 }
2192
cil_class_init(struct cil_class ** class)2193 void cil_class_init(struct cil_class **class)
2194 {
2195 *class = cil_malloc(sizeof(**class));
2196
2197 cil_symtab_datum_init(&(*class)->datum);
2198
2199 cil_symtab_init(&(*class)->perms, CIL_CLASS_SYM_SIZE);
2200
2201 (*class)->num_perms = 0;
2202 (*class)->common = NULL;
2203 (*class)->ordered = CIL_FALSE;
2204 }
2205
cil_classorder_init(struct cil_classorder ** classorder)2206 void cil_classorder_init(struct cil_classorder **classorder)
2207 {
2208 *classorder = cil_malloc(sizeof(**classorder));
2209
2210 (*classorder)->class_list_str = NULL;
2211 }
2212
cil_classcommon_init(struct cil_classcommon ** classcommon)2213 void cil_classcommon_init(struct cil_classcommon **classcommon)
2214 {
2215 *classcommon = cil_malloc(sizeof(**classcommon));
2216
2217 (*classcommon)->class_str = NULL;
2218 (*classcommon)->common_str = NULL;
2219 }
2220
cil_sid_init(struct cil_sid ** sid)2221 void cil_sid_init(struct cil_sid **sid)
2222 {
2223 *sid = cil_malloc(sizeof(**sid));
2224
2225 cil_symtab_datum_init(&(*sid)->datum);
2226
2227 (*sid)->ordered = CIL_FALSE;
2228 (*sid)->context = NULL;
2229 }
2230
cil_sidcontext_init(struct cil_sidcontext ** sidcontext)2231 void cil_sidcontext_init(struct cil_sidcontext **sidcontext)
2232 {
2233 *sidcontext = cil_malloc(sizeof(**sidcontext));
2234
2235 (*sidcontext)->sid_str = NULL;
2236 (*sidcontext)->context_str = NULL;
2237 (*sidcontext)->context = NULL;
2238 }
2239
cil_sidorder_init(struct cil_sidorder ** sidorder)2240 void cil_sidorder_init(struct cil_sidorder **sidorder)
2241 {
2242 *sidorder = cil_malloc(sizeof(**sidorder));
2243
2244 (*sidorder)->sid_list_str = NULL;
2245 }
2246
cil_userrole_init(struct cil_userrole ** userrole)2247 void cil_userrole_init(struct cil_userrole **userrole)
2248 {
2249 *userrole = cil_malloc(sizeof(**userrole));
2250
2251 (*userrole)->user_str = NULL;
2252 (*userrole)->user = NULL;
2253 (*userrole)->role_str = NULL;
2254 (*userrole)->role = NULL;
2255 }
2256
cil_userprefix_init(struct cil_userprefix ** userprefix)2257 void cil_userprefix_init(struct cil_userprefix **userprefix)
2258 {
2259 *userprefix = cil_malloc(sizeof(**userprefix));
2260
2261 (*userprefix)->user_str = NULL;
2262 (*userprefix)->user = NULL;
2263 (*userprefix)->prefix_str = NULL;
2264 }
2265
cil_selinuxuser_init(struct cil_selinuxuser ** selinuxuser)2266 void cil_selinuxuser_init(struct cil_selinuxuser **selinuxuser)
2267 {
2268 *selinuxuser = cil_malloc(sizeof(**selinuxuser));
2269
2270 (*selinuxuser)->name_str = NULL;
2271 (*selinuxuser)->user_str = NULL;
2272 (*selinuxuser)->user = NULL;
2273 (*selinuxuser)->range_str = NULL;
2274 (*selinuxuser)->range = NULL;
2275 }
2276
cil_roletype_init(struct cil_roletype ** roletype)2277 void cil_roletype_init(struct cil_roletype **roletype)
2278 {
2279 *roletype = cil_malloc(sizeof(**roletype));
2280
2281 (*roletype)->role_str = NULL;
2282 (*roletype)->role = NULL;
2283 (*roletype)->type_str = NULL;
2284 (*roletype)->type = NULL;
2285 }
2286
cil_roleattribute_init(struct cil_roleattribute ** attr)2287 void cil_roleattribute_init(struct cil_roleattribute **attr)
2288 {
2289 *attr = cil_malloc(sizeof(**attr));
2290
2291 cil_symtab_datum_init(&(*attr)->datum);
2292
2293 (*attr)->expr_list = NULL;
2294 (*attr)->roles = NULL;
2295 }
2296
cil_roleattributeset_init(struct cil_roleattributeset ** attrset)2297 void cil_roleattributeset_init(struct cil_roleattributeset **attrset)
2298 {
2299 *attrset = cil_malloc(sizeof(**attrset));
2300
2301 (*attrset)->attr_str = NULL;
2302 (*attrset)->str_expr = NULL;
2303 (*attrset)->datum_expr = NULL;
2304 }
2305
cil_typeattribute_init(struct cil_typeattribute ** attr)2306 void cil_typeattribute_init(struct cil_typeattribute **attr)
2307 {
2308 *attr = cil_malloc(sizeof(**attr));
2309
2310 cil_symtab_datum_init(&(*attr)->datum);
2311
2312 (*attr)->expr_list = NULL;
2313 (*attr)->types = NULL;
2314 (*attr)->used = CIL_FALSE;
2315 (*attr)->keep = CIL_FALSE;
2316 }
2317
cil_typeattributeset_init(struct cil_typeattributeset ** attrset)2318 void cil_typeattributeset_init(struct cil_typeattributeset **attrset)
2319 {
2320 *attrset = cil_malloc(sizeof(**attrset));
2321
2322 (*attrset)->attr_str = NULL;
2323 (*attrset)->str_expr = NULL;
2324 (*attrset)->datum_expr = NULL;
2325 }
2326
cil_expandtypeattribute_init(struct cil_expandtypeattribute ** expandattr)2327 void cil_expandtypeattribute_init(struct cil_expandtypeattribute **expandattr)
2328 {
2329 *expandattr = cil_malloc(sizeof(**expandattr));
2330
2331 (*expandattr)->attr_strs = NULL;
2332 (*expandattr)->attr_datums = NULL;
2333 (*expandattr)->expand = 0;
2334 }
2335
cil_alias_init(struct cil_alias ** alias)2336 void cil_alias_init(struct cil_alias **alias)
2337 {
2338 *alias = cil_malloc(sizeof(**alias));
2339
2340 (*alias)->actual = NULL;
2341
2342 cil_symtab_datum_init(&(*alias)->datum);
2343 }
2344
cil_aliasactual_init(struct cil_aliasactual ** aliasactual)2345 void cil_aliasactual_init(struct cil_aliasactual **aliasactual)
2346 {
2347 *aliasactual = cil_malloc(sizeof(**aliasactual));
2348
2349 (*aliasactual)->alias_str = NULL;
2350 (*aliasactual)->actual_str = NULL;
2351 }
2352
cil_typepermissive_init(struct cil_typepermissive ** typeperm)2353 void cil_typepermissive_init(struct cil_typepermissive **typeperm)
2354 {
2355 *typeperm = cil_malloc(sizeof(**typeperm));
2356
2357 (*typeperm)->type_str = NULL;
2358 (*typeperm)->type = NULL;
2359 }
2360
cil_name_init(struct cil_name ** name)2361 void cil_name_init(struct cil_name **name)
2362 {
2363 *name = cil_malloc(sizeof(**name));
2364
2365 cil_symtab_datum_init(&(*name)->datum);
2366 (*name)->name_str = NULL;
2367 }
2368
cil_nametypetransition_init(struct cil_nametypetransition ** nametypetrans)2369 void cil_nametypetransition_init(struct cil_nametypetransition **nametypetrans)
2370 {
2371 *nametypetrans = cil_malloc(sizeof(**nametypetrans));
2372
2373 (*nametypetrans)->src_str = NULL;
2374 (*nametypetrans)->src = NULL;
2375 (*nametypetrans)->tgt_str = NULL;
2376 (*nametypetrans)->tgt = NULL;
2377 (*nametypetrans)->obj_str = NULL;
2378 (*nametypetrans)->obj = NULL;
2379 (*nametypetrans)->name_str = NULL;
2380 (*nametypetrans)->name = NULL;
2381 (*nametypetrans)->result_str = NULL;
2382 (*nametypetrans)->result = NULL;
2383 }
2384
cil_rangetransition_init(struct cil_rangetransition ** rangetrans)2385 void cil_rangetransition_init(struct cil_rangetransition **rangetrans)
2386 {
2387 *rangetrans = cil_malloc(sizeof(**rangetrans));
2388
2389 (*rangetrans)->src_str = NULL;
2390 (*rangetrans)->src = NULL;
2391 (*rangetrans)->exec_str = NULL;
2392 (*rangetrans)->exec = NULL;
2393 (*rangetrans)->obj_str = NULL;
2394 (*rangetrans)->obj = NULL;
2395 (*rangetrans)->range_str = NULL;
2396 (*rangetrans)->range = NULL;
2397 }
2398
cil_bool_init(struct cil_bool ** cilbool)2399 void cil_bool_init(struct cil_bool **cilbool)
2400 {
2401 *cilbool = cil_malloc(sizeof(**cilbool));
2402
2403 cil_symtab_datum_init(&(*cilbool)->datum);
2404 (*cilbool)->value = 0;
2405 }
2406
cil_tunable_init(struct cil_tunable ** ciltun)2407 void cil_tunable_init(struct cil_tunable **ciltun)
2408 {
2409 *ciltun = cil_malloc(sizeof(**ciltun));
2410
2411 cil_symtab_datum_init(&(*ciltun)->datum);
2412 (*ciltun)->value = 0;
2413 }
2414
cil_condblock_init(struct cil_condblock ** cb)2415 void cil_condblock_init(struct cil_condblock **cb)
2416 {
2417 *cb = cil_malloc(sizeof(**cb));
2418
2419 (*cb)->flavor = CIL_NONE;
2420 cil_symtab_array_init((*cb)->symtab, cil_sym_sizes[CIL_SYM_ARRAY_CONDBLOCK]);
2421 }
2422
cil_boolif_init(struct cil_booleanif ** bif)2423 void cil_boolif_init(struct cil_booleanif **bif)
2424 {
2425 *bif = cil_malloc(sizeof(**bif));
2426
2427 (*bif)->str_expr = NULL;
2428 (*bif)->datum_expr = NULL;
2429 }
2430
cil_tunif_init(struct cil_tunableif ** tif)2431 void cil_tunif_init(struct cil_tunableif **tif)
2432 {
2433 *tif = cil_malloc(sizeof(**tif));
2434
2435 (*tif)->str_expr = NULL;
2436 (*tif)->datum_expr = NULL;
2437 }
2438
cil_avrule_init(struct cil_avrule ** avrule)2439 void cil_avrule_init(struct cil_avrule **avrule)
2440 {
2441 *avrule = cil_malloc(sizeof(**avrule));
2442
2443 (*avrule)->is_extended = 0;
2444 (*avrule)->rule_kind = CIL_NONE;
2445 (*avrule)->src_str = NULL;
2446 (*avrule)->src = NULL;
2447 (*avrule)->tgt_str = NULL;
2448 (*avrule)->tgt = NULL;
2449 memset(&((*avrule)->perms), 0, sizeof((*avrule)->perms));
2450 }
2451
cil_permissionx_init(struct cil_permissionx ** permx)2452 void cil_permissionx_init(struct cil_permissionx **permx)
2453 {
2454 *permx = cil_malloc(sizeof(**permx));
2455
2456 cil_symtab_datum_init(&(*permx)->datum);
2457 (*permx)->kind = CIL_NONE;
2458 (*permx)->obj_str = NULL;
2459 (*permx)->obj = NULL;
2460 (*permx)->expr_str = NULL;
2461 (*permx)->perms = NULL;
2462 }
2463
cil_type_rule_init(struct cil_type_rule ** type_rule)2464 void cil_type_rule_init(struct cil_type_rule **type_rule)
2465 {
2466 *type_rule = cil_malloc(sizeof(**type_rule));
2467
2468 (*type_rule)->rule_kind = CIL_NONE;
2469 (*type_rule)->src_str = NULL;
2470 (*type_rule)->src = NULL;
2471 (*type_rule)->tgt_str = NULL;
2472 (*type_rule)->tgt = NULL;
2473 (*type_rule)->obj_str = NULL;
2474 (*type_rule)->obj = NULL;
2475 (*type_rule)->result_str = NULL;
2476 (*type_rule)->result = NULL;
2477 }
2478
cil_roletransition_init(struct cil_roletransition ** role_trans)2479 void cil_roletransition_init(struct cil_roletransition **role_trans)
2480 {
2481 *role_trans = cil_malloc(sizeof(**role_trans));
2482
2483 (*role_trans)->src_str = NULL;
2484 (*role_trans)->src = NULL;
2485 (*role_trans)->tgt_str = NULL;
2486 (*role_trans)->tgt = NULL;
2487 (*role_trans)->obj_str = NULL;
2488 (*role_trans)->obj = NULL;
2489 (*role_trans)->result_str = NULL;
2490 (*role_trans)->result = NULL;
2491 }
2492
cil_roleallow_init(struct cil_roleallow ** roleallow)2493 void cil_roleallow_init(struct cil_roleallow **roleallow)
2494 {
2495 *roleallow = cil_malloc(sizeof(**roleallow));
2496
2497 (*roleallow)->src_str = NULL;
2498 (*roleallow)->src = NULL;
2499 (*roleallow)->tgt_str = NULL;
2500 (*roleallow)->tgt = NULL;
2501 }
2502
cil_catset_init(struct cil_catset ** catset)2503 void cil_catset_init(struct cil_catset **catset)
2504 {
2505 *catset = cil_malloc(sizeof(**catset));
2506
2507 cil_symtab_datum_init(&(*catset)->datum);
2508 (*catset)->cats = NULL;
2509 }
2510
cil_senscat_init(struct cil_senscat ** senscat)2511 void cil_senscat_init(struct cil_senscat **senscat)
2512 {
2513 *senscat = cil_malloc(sizeof(**senscat));
2514
2515 (*senscat)->sens_str = NULL;
2516 (*senscat)->cats = NULL;
2517 }
2518
cil_cats_init(struct cil_cats ** cats)2519 void cil_cats_init(struct cil_cats **cats)
2520 {
2521 *cats = cil_malloc(sizeof(**cats));
2522
2523 (*cats)->evaluated = CIL_FALSE;
2524 (*cats)->str_expr = NULL;
2525 (*cats)->datum_expr = NULL;
2526 }
2527
cil_filecon_init(struct cil_filecon ** filecon)2528 void cil_filecon_init(struct cil_filecon **filecon)
2529 {
2530 *filecon = cil_malloc(sizeof(**filecon));
2531
2532 (*filecon)->path_str = NULL;
2533 (*filecon)->type = 0;
2534 (*filecon)->context_str = NULL;
2535 (*filecon)->context = NULL;
2536 }
2537
cil_ibpkeycon_init(struct cil_ibpkeycon ** ibpkeycon)2538 void cil_ibpkeycon_init(struct cil_ibpkeycon **ibpkeycon)
2539 {
2540 *ibpkeycon = cil_malloc(sizeof(**ibpkeycon));
2541
2542 (*ibpkeycon)->subnet_prefix_str = NULL;
2543 (*ibpkeycon)->pkey_low = 0;
2544 (*ibpkeycon)->pkey_high = 0;
2545 (*ibpkeycon)->context_str = NULL;
2546 (*ibpkeycon)->context = NULL;
2547 }
2548
cil_portcon_init(struct cil_portcon ** portcon)2549 void cil_portcon_init(struct cil_portcon **portcon)
2550 {
2551 *portcon = cil_malloc(sizeof(**portcon));
2552 (*portcon)->proto = 0;
2553 (*portcon)->port_low = 0;
2554 (*portcon)->port_high = 0;
2555 (*portcon)->context_str = NULL;
2556 (*portcon)->context = NULL;
2557 }
2558
cil_nodecon_init(struct cil_nodecon ** nodecon)2559 void cil_nodecon_init(struct cil_nodecon **nodecon)
2560 {
2561 *nodecon = cil_malloc(sizeof(**nodecon));
2562
2563 (*nodecon)->addr_str = NULL;
2564 (*nodecon)->addr = NULL;
2565 (*nodecon)->mask_str = NULL;
2566 (*nodecon)->mask = NULL;
2567 (*nodecon)->context_str = NULL;
2568 (*nodecon)->context = NULL;
2569 }
2570
cil_genfscon_init(struct cil_genfscon ** genfscon)2571 void cil_genfscon_init(struct cil_genfscon **genfscon)
2572 {
2573 *genfscon = cil_malloc(sizeof(**genfscon));
2574
2575 (*genfscon)->fs_str = NULL;
2576 (*genfscon)->path_str = NULL;
2577 (*genfscon)->context_str = NULL;
2578 (*genfscon)->context = NULL;
2579 }
2580
cil_pirqcon_init(struct cil_pirqcon ** pirqcon)2581 void cil_pirqcon_init(struct cil_pirqcon **pirqcon)
2582 {
2583 *pirqcon = cil_malloc(sizeof(**pirqcon));
2584
2585 (*pirqcon)->pirq = 0;
2586 (*pirqcon)->context_str = NULL;
2587 (*pirqcon)->context = NULL;
2588 }
2589
cil_iomemcon_init(struct cil_iomemcon ** iomemcon)2590 void cil_iomemcon_init(struct cil_iomemcon **iomemcon)
2591 {
2592 *iomemcon = cil_malloc(sizeof(**iomemcon));
2593
2594 (*iomemcon)->iomem_low = 0;
2595 (*iomemcon)->iomem_high = 0;
2596 (*iomemcon)->context_str = NULL;
2597 (*iomemcon)->context = NULL;
2598 }
2599
cil_ioportcon_init(struct cil_ioportcon ** ioportcon)2600 void cil_ioportcon_init(struct cil_ioportcon **ioportcon)
2601 {
2602 *ioportcon = cil_malloc(sizeof(**ioportcon));
2603
2604 (*ioportcon)->context_str = NULL;
2605 (*ioportcon)->context = NULL;
2606 }
2607
cil_pcidevicecon_init(struct cil_pcidevicecon ** pcidevicecon)2608 void cil_pcidevicecon_init(struct cil_pcidevicecon **pcidevicecon)
2609 {
2610 *pcidevicecon = cil_malloc(sizeof(**pcidevicecon));
2611
2612 (*pcidevicecon)->dev = 0;
2613 (*pcidevicecon)->context_str = NULL;
2614 (*pcidevicecon)->context = NULL;
2615 }
2616
cil_devicetreecon_init(struct cil_devicetreecon ** dtcon)2617 void cil_devicetreecon_init(struct cil_devicetreecon **dtcon)
2618 {
2619 *dtcon = cil_malloc(sizeof(**dtcon));
2620
2621 (*dtcon)->path = NULL;
2622 (*dtcon)->context_str = NULL;
2623 (*dtcon)->context = NULL;
2624 }
2625
cil_fsuse_init(struct cil_fsuse ** fsuse)2626 void cil_fsuse_init(struct cil_fsuse **fsuse)
2627 {
2628 *fsuse = cil_malloc(sizeof(**fsuse));
2629
2630 (*fsuse)->type = 0;
2631 (*fsuse)->fs_str = NULL;
2632 (*fsuse)->context_str = NULL;
2633 (*fsuse)->context = NULL;
2634 }
2635
cil_constrain_init(struct cil_constrain ** constrain)2636 void cil_constrain_init(struct cil_constrain **constrain)
2637 {
2638 *constrain = cil_malloc(sizeof(**constrain));
2639
2640 (*constrain)->classperms = NULL;
2641 (*constrain)->str_expr = NULL;
2642 (*constrain)->datum_expr = NULL;
2643 }
2644
cil_validatetrans_init(struct cil_validatetrans ** validtrans)2645 void cil_validatetrans_init(struct cil_validatetrans **validtrans)
2646 {
2647 *validtrans = cil_malloc(sizeof(**validtrans));
2648
2649 (*validtrans)->class_str = NULL;
2650 (*validtrans)->class = NULL;
2651 (*validtrans)->str_expr = NULL;
2652 (*validtrans)->datum_expr = NULL;
2653 }
2654
cil_ipaddr_init(struct cil_ipaddr ** ipaddr)2655 void cil_ipaddr_init(struct cil_ipaddr **ipaddr)
2656 {
2657 *ipaddr = cil_malloc(sizeof(**ipaddr));
2658
2659 cil_symtab_datum_init(&(*ipaddr)->datum);
2660 memset(&(*ipaddr)->ip, 0, sizeof((*ipaddr)->ip));
2661 }
2662
cil_perm_init(struct cil_perm ** perm)2663 void cil_perm_init(struct cil_perm **perm)
2664 {
2665 *perm = cil_malloc(sizeof(**perm));
2666
2667 cil_symtab_datum_init(&(*perm)->datum);
2668 (*perm)->value = 0;
2669 (*perm)->classperms = NULL;
2670 }
2671
cil_classpermission_init(struct cil_classpermission ** cp)2672 void cil_classpermission_init(struct cil_classpermission **cp)
2673 {
2674 *cp = cil_malloc(sizeof(**cp));
2675
2676 cil_symtab_datum_init(&(*cp)->datum);
2677 (*cp)->classperms = NULL;
2678 }
2679
cil_classpermissionset_init(struct cil_classpermissionset ** cps)2680 void cil_classpermissionset_init(struct cil_classpermissionset **cps)
2681 {
2682 *cps = cil_malloc(sizeof(**cps));
2683
2684 (*cps)->set_str = NULL;
2685 (*cps)->classperms = NULL;
2686 }
2687
cil_classperms_set_init(struct cil_classperms_set ** cp_set)2688 void cil_classperms_set_init(struct cil_classperms_set **cp_set)
2689 {
2690 *cp_set = cil_malloc(sizeof(**cp_set));
2691 (*cp_set)->set_str = NULL;
2692 (*cp_set)->set = NULL;
2693 }
2694
cil_classperms_init(struct cil_classperms ** cp)2695 void cil_classperms_init(struct cil_classperms **cp)
2696 {
2697 *cp = cil_malloc(sizeof(**cp));
2698 (*cp)->class_str = NULL;
2699 (*cp)->class = NULL;
2700 (*cp)->perm_strs = NULL;
2701 (*cp)->perms = NULL;
2702 }
2703
cil_classmapping_init(struct cil_classmapping ** mapping)2704 void cil_classmapping_init(struct cil_classmapping **mapping)
2705 {
2706 *mapping = cil_malloc(sizeof(**mapping));
2707
2708 (*mapping)->map_class_str = NULL;
2709 (*mapping)->map_perm_str = NULL;
2710 (*mapping)->classperms = NULL;
2711 }
2712
cil_user_init(struct cil_user ** user)2713 void cil_user_init(struct cil_user **user)
2714 {
2715 *user = cil_malloc(sizeof(**user));
2716
2717 cil_symtab_datum_init(&(*user)->datum);
2718 (*user)->bounds = NULL;
2719 (*user)->roles = NULL;
2720 (*user)->dftlevel = NULL;
2721 (*user)->range = NULL;
2722 (*user)->value = 0;
2723 }
2724
cil_userattribute_init(struct cil_userattribute ** attr)2725 void cil_userattribute_init(struct cil_userattribute **attr)
2726 {
2727 *attr = cil_malloc(sizeof(**attr));
2728
2729 cil_symtab_datum_init(&(*attr)->datum);
2730
2731 (*attr)->expr_list = NULL;
2732 (*attr)->users = NULL;
2733 }
2734
cil_userattributeset_init(struct cil_userattributeset ** attrset)2735 void cil_userattributeset_init(struct cil_userattributeset **attrset)
2736 {
2737 *attrset = cil_malloc(sizeof(**attrset));
2738
2739 (*attrset)->attr_str = NULL;
2740 (*attrset)->str_expr = NULL;
2741 (*attrset)->datum_expr = NULL;
2742 }
2743
cil_userlevel_init(struct cil_userlevel ** usrlvl)2744 void cil_userlevel_init(struct cil_userlevel **usrlvl)
2745 {
2746 *usrlvl = cil_malloc(sizeof(**usrlvl));
2747
2748 (*usrlvl)->user_str = NULL;
2749 (*usrlvl)->level_str = NULL;
2750 (*usrlvl)->level = NULL;
2751 }
2752
cil_userrange_init(struct cil_userrange ** userrange)2753 void cil_userrange_init(struct cil_userrange **userrange)
2754 {
2755 *userrange = cil_malloc(sizeof(**userrange));
2756
2757 (*userrange)->user_str = NULL;
2758 (*userrange)->range_str = NULL;
2759 (*userrange)->range = NULL;
2760 }
2761
cil_role_init(struct cil_role ** role)2762 void cil_role_init(struct cil_role **role)
2763 {
2764 *role = cil_malloc(sizeof(**role));
2765
2766 cil_symtab_datum_init(&(*role)->datum);
2767 (*role)->bounds = NULL;
2768 (*role)->types = NULL;
2769 (*role)->value = 0;
2770 }
2771
cil_type_init(struct cil_type ** type)2772 void cil_type_init(struct cil_type **type)
2773 {
2774 *type = cil_malloc(sizeof(**type));
2775
2776 cil_symtab_datum_init(&(*type)->datum);
2777 (*type)->bounds = NULL;
2778 (*type)->value = 0;
2779 }
2780
cil_cat_init(struct cil_cat ** cat)2781 void cil_cat_init(struct cil_cat **cat)
2782 {
2783 *cat = cil_malloc(sizeof(**cat));
2784
2785 cil_symtab_datum_init(&(*cat)->datum);
2786 (*cat)->ordered = CIL_FALSE;
2787 (*cat)->value = 0;
2788 }
2789
cil_catorder_init(struct cil_catorder ** catorder)2790 void cil_catorder_init(struct cil_catorder **catorder)
2791 {
2792 *catorder = cil_malloc(sizeof(**catorder));
2793
2794 (*catorder)->cat_list_str = NULL;
2795 }
2796
cil_sensorder_init(struct cil_sensorder ** sensorder)2797 void cil_sensorder_init(struct cil_sensorder **sensorder)
2798 {
2799 *sensorder = cil_malloc(sizeof(**sensorder));
2800
2801 (*sensorder)->sens_list_str = NULL;
2802 }
2803
cil_args_init(struct cil_args ** args)2804 void cil_args_init(struct cil_args **args)
2805 {
2806 *args = cil_malloc(sizeof(**args));
2807 (*args)->arg_str = NULL;
2808 (*args)->arg = NULL;
2809 (*args)->param_str = NULL;
2810 (*args)->flavor = CIL_NONE;
2811 }
2812
cil_call_init(struct cil_call ** call)2813 void cil_call_init(struct cil_call **call)
2814 {
2815 *call = cil_malloc(sizeof(**call));
2816
2817 (*call)->macro_str = NULL;
2818 (*call)->macro = NULL;
2819 (*call)->args_tree = NULL;
2820 (*call)->args = NULL;
2821 (*call)->copied = 0;
2822 }
2823
cil_optional_init(struct cil_optional ** optional)2824 void cil_optional_init(struct cil_optional **optional)
2825 {
2826 *optional = cil_malloc(sizeof(**optional));
2827 cil_symtab_datum_init(&(*optional)->datum);
2828 }
2829
cil_param_init(struct cil_param ** param)2830 void cil_param_init(struct cil_param **param)
2831 {
2832 *param = cil_malloc(sizeof(**param));
2833
2834 (*param)->str = NULL;
2835 (*param)->flavor = CIL_NONE;
2836 }
2837
cil_macro_init(struct cil_macro ** macro)2838 void cil_macro_init(struct cil_macro **macro)
2839 {
2840 *macro = cil_malloc(sizeof(**macro));
2841
2842 cil_symtab_datum_init(&(*macro)->datum);
2843 cil_symtab_array_init((*macro)->symtab, cil_sym_sizes[CIL_SYM_ARRAY_MACRO]);
2844 (*macro)->params = NULL;
2845 }
2846
cil_policycap_init(struct cil_policycap ** policycap)2847 void cil_policycap_init(struct cil_policycap **policycap)
2848 {
2849 *policycap = cil_malloc(sizeof(**policycap));
2850
2851 cil_symtab_datum_init(&(*policycap)->datum);
2852 }
2853
cil_bounds_init(struct cil_bounds ** bounds)2854 void cil_bounds_init(struct cil_bounds **bounds)
2855 {
2856 *bounds = cil_malloc(sizeof(**bounds));
2857
2858 (*bounds)->parent_str = NULL;
2859 (*bounds)->child_str = NULL;
2860 }
2861
cil_default_init(struct cil_default ** def)2862 void cil_default_init(struct cil_default **def)
2863 {
2864 *def = cil_malloc(sizeof(**def));
2865
2866 (*def)->flavor = CIL_NONE;
2867 (*def)->class_strs = NULL;
2868 (*def)->class_datums = NULL;
2869 }
2870
cil_defaultrange_init(struct cil_defaultrange ** def)2871 void cil_defaultrange_init(struct cil_defaultrange **def)
2872 {
2873 *def = cil_malloc(sizeof(**def));
2874
2875 (*def)->class_strs = NULL;
2876 (*def)->class_datums = NULL;
2877 }
2878
cil_handleunknown_init(struct cil_handleunknown ** unk)2879 void cil_handleunknown_init(struct cil_handleunknown **unk)
2880 {
2881 *unk = cil_malloc(sizeof(**unk));
2882 }
2883
cil_mls_init(struct cil_mls ** mls)2884 void cil_mls_init(struct cil_mls **mls)
2885 {
2886 *mls = cil_malloc(sizeof(**mls));
2887 (*mls)->value = 0;
2888 }
2889
cil_src_info_init(struct cil_src_info ** info)2890 void cil_src_info_init(struct cil_src_info **info)
2891 {
2892 *info = cil_malloc(sizeof(**info));
2893 (*info)->kind = NULL;
2894 (*info)->hll_line = 0;
2895 (*info)->path = NULL;
2896 }
2897