xref: /aosp_15_r20/external/selinux/libsepol/src/policydb.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 
2 /* Author : Stephen Smalley, <[email protected]> */
3 
4 /*
5  * Updated: Trusted Computer Solutions, Inc. <[email protected]>
6  *
7  *	Support for enhanced MLS infrastructure.
8  *
9  * Updated: Frank Mayer <[email protected]> and Karl MacMillan <[email protected]>
10  *
11  * 	Added conditional policy language extensions
12  *
13  * Updated: Red Hat, Inc.  James Morris <[email protected]>
14  *      Fine-grained netlink support
15  *      IPv6 support
16  *      Code cleanup
17  *
18  * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
19  * Copyright (C) 2003 - 2005 Tresys Technology, LLC
20  * Copyright (C) 2003 - 2007 Red Hat, Inc.
21  * Copyright (C) 2017 Mellanox Technologies Inc.
22  *
23  *  This library is free software; you can redistribute it and/or
24  *  modify it under the terms of the GNU Lesser General Public
25  *  License as published by the Free Software Foundation; either
26  *  version 2.1 of the License, or (at your option) any later version.
27  *
28  *  This library is distributed in the hope that it will be useful,
29  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
30  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  *  Lesser General Public License for more details.
32  *
33  *  You should have received a copy of the GNU Lesser General Public
34  *  License along with this library; if not, write to the Free Software
35  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
36  */
37 
38 /* FLASK */
39 
40 /*
41  * Implementation of the policy database.
42  */
43 
44 #include <assert.h>
45 #include <stdlib.h>
46 
47 #include <sepol/policydb/policydb.h>
48 #include <sepol/policydb/expand.h>
49 #include <sepol/policydb/conditional.h>
50 #include <sepol/policydb/avrule_block.h>
51 #include <sepol/policydb/util.h>
52 
53 #include "kernel_to_common.h"
54 #include "private.h"
55 #include "debug.h"
56 #include "mls.h"
57 #include "policydb_validate.h"
58 
59 #define POLICYDB_TARGET_SZ   ARRAY_SIZE(policydb_target_strings)
60 const char * const policydb_target_strings[] = { POLICYDB_STRING, POLICYDB_XEN_STRING };
61 
62 /* These need to be updated if SYM_NUM or OCON_NUM changes */
63 static const struct policydb_compat_info policydb_compat[] = {
64 	{
65 	 .type = POLICY_KERN,
66 	 .version = POLICYDB_VERSION_BOUNDARY,
67 	 .sym_num = SYM_NUM,
68 	 .ocon_num = OCON_XEN_PCIDEVICE + 1,
69 	 .target_platform = SEPOL_TARGET_XEN,
70 	 },
71 	{
72 	 .type = POLICY_KERN,
73 	 .version = POLICYDB_VERSION_XEN_DEVICETREE,
74 	 .sym_num = SYM_NUM,
75 	 .ocon_num = OCON_XEN_DEVICETREE + 1,
76 	 .target_platform = SEPOL_TARGET_XEN,
77 	 },
78 	{
79 	 .type = POLICY_KERN,
80 	 .version = POLICYDB_VERSION_BASE,
81 	 .sym_num = SYM_NUM - 3,
82 	 .ocon_num = OCON_FSUSE + 1,
83 	 .target_platform = SEPOL_TARGET_SELINUX,
84 	 },
85 	{
86 	 .type = POLICY_KERN,
87 	 .version = POLICYDB_VERSION_BOOL,
88 	 .sym_num = SYM_NUM - 2,
89 	 .ocon_num = OCON_FSUSE + 1,
90 	 .target_platform = SEPOL_TARGET_SELINUX,
91 	 },
92 	{
93 	 .type = POLICY_KERN,
94 	 .version = POLICYDB_VERSION_IPV6,
95 	 .sym_num = SYM_NUM - 2,
96 	 .ocon_num = OCON_NODE6 + 1,
97 	 .target_platform = SEPOL_TARGET_SELINUX,
98 	 },
99 	{
100 	 .type = POLICY_KERN,
101 	 .version = POLICYDB_VERSION_NLCLASS,
102 	 .sym_num = SYM_NUM - 2,
103 	 .ocon_num = OCON_NODE6 + 1,
104 	 .target_platform = SEPOL_TARGET_SELINUX,
105 	 },
106 	{
107 	 .type = POLICY_KERN,
108 	 .version = POLICYDB_VERSION_MLS,
109 	 .sym_num = SYM_NUM,
110 	 .ocon_num = OCON_NODE6 + 1,
111 	 .target_platform = SEPOL_TARGET_SELINUX,
112 	 },
113 	{
114 	 .type = POLICY_KERN,
115 	 .version = POLICYDB_VERSION_AVTAB,
116 	 .sym_num = SYM_NUM,
117 	 .ocon_num = OCON_NODE6 + 1,
118 	 .target_platform = SEPOL_TARGET_SELINUX,
119 	 },
120 	{
121 	 .type = POLICY_KERN,
122 	 .version = POLICYDB_VERSION_RANGETRANS,
123 	 .sym_num = SYM_NUM,
124 	 .ocon_num = OCON_NODE6 + 1,
125 	 .target_platform = SEPOL_TARGET_SELINUX,
126 	 },
127 	{
128 	 .type = POLICY_KERN,
129 	 .version = POLICYDB_VERSION_POLCAP,
130 	 .sym_num = SYM_NUM,
131 	 .ocon_num = OCON_NODE6 + 1,
132 	 .target_platform = SEPOL_TARGET_SELINUX,
133 	 },
134 	{
135 	 .type = POLICY_KERN,
136 	 .version = POLICYDB_VERSION_PERMISSIVE,
137 	 .sym_num = SYM_NUM,
138 	 .ocon_num = OCON_NODE6 + 1,
139 	 .target_platform = SEPOL_TARGET_SELINUX,
140 	 },
141         {
142 	 .type = POLICY_KERN,
143 	 .version = POLICYDB_VERSION_BOUNDARY,
144 	 .sym_num = SYM_NUM,
145 	 .ocon_num = OCON_NODE6 + 1,
146 	 .target_platform = SEPOL_TARGET_SELINUX,
147 	},
148 	{
149 	 .type = POLICY_KERN,
150 	 .version = POLICYDB_VERSION_FILENAME_TRANS,
151 	 .sym_num = SYM_NUM,
152 	 .ocon_num = OCON_NODE6 + 1,
153 	 .target_platform = SEPOL_TARGET_SELINUX,
154 	},
155 	{
156 	 .type = POLICY_KERN,
157 	 .version = POLICYDB_VERSION_ROLETRANS,
158 	 .sym_num = SYM_NUM,
159 	 .ocon_num = OCON_NODE6 + 1,
160 	 .target_platform = SEPOL_TARGET_SELINUX,
161 	},
162 	{
163 	 .type = POLICY_KERN,
164 	 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
165 	 .sym_num = SYM_NUM,
166 	 .ocon_num = OCON_NODE6 + 1,
167 	 .target_platform = SEPOL_TARGET_SELINUX,
168 	},
169 	{
170 	 .type = POLICY_KERN,
171 	 .version = POLICYDB_VERSION_DEFAULT_TYPE,
172 	 .sym_num = SYM_NUM,
173 	 .ocon_num = OCON_NODE6 + 1,
174 	 .target_platform = SEPOL_TARGET_SELINUX,
175 	},
176 	{
177 	 .type = POLICY_KERN,
178 	 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
179 	 .sym_num = SYM_NUM,
180 	 .ocon_num = OCON_NODE6 + 1,
181 	 .target_platform = SEPOL_TARGET_SELINUX,
182 	},
183 	{
184 	 .type = POLICY_KERN,
185 	 .version = POLICYDB_VERSION_XPERMS_IOCTL,
186 	 .sym_num = SYM_NUM,
187 	 .ocon_num = OCON_NODE6 + 1,
188 	 .target_platform = SEPOL_TARGET_SELINUX,
189 	},
190 	{
191 	 .type = POLICY_KERN,
192 	 .version = POLICYDB_VERSION_INFINIBAND,
193 	 .sym_num = SYM_NUM,
194 	 .ocon_num = OCON_IBENDPORT + 1,
195 	 .target_platform = SEPOL_TARGET_SELINUX,
196 	},
197 	{
198 	 .type = POLICY_KERN,
199 	 .version = POLICYDB_VERSION_GLBLUB,
200 	 .sym_num = SYM_NUM,
201 	 .ocon_num = OCON_IBENDPORT + 1,
202 	 .target_platform = SEPOL_TARGET_SELINUX,
203 	},
204 	{
205 	 .type = POLICY_KERN,
206 	 .version = POLICYDB_VERSION_COMP_FTRANS,
207 	 .sym_num = SYM_NUM,
208 	 .ocon_num = OCON_IBENDPORT + 1,
209 	 .target_platform = SEPOL_TARGET_SELINUX,
210 	},
211 	{
212 	 .type = POLICY_BASE,
213 	 .version = MOD_POLICYDB_VERSION_BASE,
214 	 .sym_num = SYM_NUM,
215 	 .ocon_num = OCON_NODE6 + 1,
216 	 .target_platform = SEPOL_TARGET_SELINUX,
217 	 },
218 	{
219 	 .type = POLICY_BASE,
220 	 .version = MOD_POLICYDB_VERSION_MLS,
221 	 .sym_num = SYM_NUM,
222 	 .ocon_num = OCON_NODE6 + 1,
223 	 .target_platform = SEPOL_TARGET_SELINUX,
224 	 },
225 	{
226 	 .type = POLICY_BASE,
227 	 .version = MOD_POLICYDB_VERSION_MLS_USERS,
228 	 .sym_num = SYM_NUM,
229 	 .ocon_num = OCON_NODE6 + 1,
230 	 .target_platform = SEPOL_TARGET_SELINUX,
231 	 },
232 	{
233 	 .type = POLICY_BASE,
234 	 .version = MOD_POLICYDB_VERSION_POLCAP,
235 	 .sym_num = SYM_NUM,
236 	 .ocon_num = OCON_NODE6 + 1,
237 	 .target_platform = SEPOL_TARGET_SELINUX,
238 	 },
239 	{
240 	 .type = POLICY_BASE,
241 	 .version = MOD_POLICYDB_VERSION_PERMISSIVE,
242 	 .sym_num = SYM_NUM,
243 	 .ocon_num = OCON_NODE6 + 1,
244 	 .target_platform = SEPOL_TARGET_SELINUX,
245 	 },
246 	{
247 	 .type = POLICY_BASE,
248 	 .version = MOD_POLICYDB_VERSION_BOUNDARY,
249 	 .sym_num = SYM_NUM,
250 	 .ocon_num = OCON_NODE6 + 1,
251 	 .target_platform = SEPOL_TARGET_SELINUX,
252 	},
253 	{
254 	 .type = POLICY_BASE,
255 	 .version = MOD_POLICYDB_VERSION_BOUNDARY_ALIAS,
256 	 .sym_num = SYM_NUM,
257 	 .ocon_num = OCON_NODE6 + 1,
258 	 .target_platform = SEPOL_TARGET_SELINUX,
259 	},
260 	{
261 	 .type = POLICY_BASE,
262 	 .version = MOD_POLICYDB_VERSION_FILENAME_TRANS,
263 	 .sym_num = SYM_NUM,
264 	 .ocon_num = OCON_NODE6 + 1,
265 	 .target_platform = SEPOL_TARGET_SELINUX,
266 	},
267 	{
268 	 .type = POLICY_BASE,
269 	 .version = MOD_POLICYDB_VERSION_ROLETRANS,
270 	 .sym_num = SYM_NUM,
271 	 .ocon_num = OCON_NODE6 + 1,
272 	 .target_platform = SEPOL_TARGET_SELINUX,
273 	},
274 	{
275 	 .type = POLICY_BASE,
276 	 .version = MOD_POLICYDB_VERSION_ROLEATTRIB,
277 	 .sym_num = SYM_NUM,
278 	 .ocon_num = OCON_NODE6 + 1,
279 	 .target_platform = SEPOL_TARGET_SELINUX,
280 	},
281 	{
282 	 .type = POLICY_BASE,
283 	 .version = MOD_POLICYDB_VERSION_TUNABLE_SEP,
284 	 .sym_num = SYM_NUM,
285 	 .ocon_num = OCON_NODE6 + 1,
286 	 .target_platform = SEPOL_TARGET_SELINUX,
287 	},
288 	{
289 	 .type = POLICY_BASE,
290 	 .version = MOD_POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
291 	 .sym_num = SYM_NUM,
292 	 .ocon_num = OCON_NODE6 + 1,
293 	 .target_platform = SEPOL_TARGET_SELINUX,
294 	},
295 	{
296 	 .type = POLICY_BASE,
297 	 .version = MOD_POLICYDB_VERSION_DEFAULT_TYPE,
298 	 .sym_num = SYM_NUM,
299 	 .ocon_num = OCON_NODE6 + 1,
300 	 .target_platform = SEPOL_TARGET_SELINUX,
301 	},
302 	{
303 	 .type = POLICY_BASE,
304 	 .version = MOD_POLICYDB_VERSION_CONSTRAINT_NAMES,
305 	 .sym_num = SYM_NUM,
306 	 .ocon_num = OCON_NODE6 + 1,
307 	 .target_platform = SEPOL_TARGET_SELINUX,
308 	},
309 	{
310 	 .type = POLICY_BASE,
311 	 .version = MOD_POLICYDB_VERSION_XPERMS_IOCTL,
312 	 .sym_num = SYM_NUM,
313 	 .ocon_num = OCON_NODE6 + 1,
314 	 .target_platform = SEPOL_TARGET_SELINUX,
315 	},
316 	{
317 	 .type = POLICY_BASE,
318 	 .version = MOD_POLICYDB_VERSION_INFINIBAND,
319 	 .sym_num = SYM_NUM,
320 	 .ocon_num = OCON_IBENDPORT + 1,
321 	 .target_platform = SEPOL_TARGET_SELINUX,
322 	},
323 	{
324 	 .type = POLICY_BASE,
325 	 .version = MOD_POLICYDB_VERSION_GLBLUB,
326 	 .sym_num = SYM_NUM,
327 	 .ocon_num = OCON_IBENDPORT + 1,
328 	 .target_platform = SEPOL_TARGET_SELINUX,
329 	},
330 	{
331 	 .type = POLICY_BASE,
332 	 .version = MOD_POLICYDB_VERSION_SELF_TYPETRANS,
333 	 .sym_num = SYM_NUM,
334 	 .ocon_num = OCON_IBENDPORT + 1,
335 	 .target_platform = SEPOL_TARGET_SELINUX,
336 	},
337 	{
338 	 .type = POLICY_MOD,
339 	 .version = MOD_POLICYDB_VERSION_BASE,
340 	 .sym_num = SYM_NUM,
341 	 .ocon_num = 0,
342 	 .target_platform = SEPOL_TARGET_SELINUX,
343 	 },
344 	{
345 	 .type = POLICY_MOD,
346 	 .version = MOD_POLICYDB_VERSION_MLS,
347 	 .sym_num = SYM_NUM,
348 	 .ocon_num = 0,
349 	 .target_platform = SEPOL_TARGET_SELINUX,
350 	 },
351 	{
352 	 .type = POLICY_MOD,
353 	 .version = MOD_POLICYDB_VERSION_MLS_USERS,
354 	 .sym_num = SYM_NUM,
355 	 .ocon_num = 0,
356 	 .target_platform = SEPOL_TARGET_SELINUX,
357 	 },
358 	{
359 	 .type = POLICY_MOD,
360 	 .version = MOD_POLICYDB_VERSION_POLCAP,
361 	 .sym_num = SYM_NUM,
362 	 .ocon_num = 0,
363 	 .target_platform = SEPOL_TARGET_SELINUX,
364 	 },
365 	{
366 	 .type = POLICY_MOD,
367 	 .version = MOD_POLICYDB_VERSION_PERMISSIVE,
368 	 .sym_num = SYM_NUM,
369 	 .ocon_num = 0,
370 	 .target_platform = SEPOL_TARGET_SELINUX,
371 	 },
372 	{
373 	 .type = POLICY_MOD,
374 	 .version = MOD_POLICYDB_VERSION_BOUNDARY,
375 	 .sym_num = SYM_NUM,
376 	 .ocon_num = 0,
377 	 .target_platform = SEPOL_TARGET_SELINUX,
378 	},
379 	{
380 	 .type = POLICY_MOD,
381 	 .version = MOD_POLICYDB_VERSION_BOUNDARY_ALIAS,
382 	 .sym_num = SYM_NUM,
383 	 .ocon_num = 0,
384 	 .target_platform = SEPOL_TARGET_SELINUX,
385 	},
386 	{
387 	 .type = POLICY_MOD,
388 	 .version = MOD_POLICYDB_VERSION_FILENAME_TRANS,
389 	 .sym_num = SYM_NUM,
390 	 .ocon_num = 0,
391 	 .target_platform = SEPOL_TARGET_SELINUX,
392 	},
393 	{
394 	 .type = POLICY_MOD,
395 	 .version = MOD_POLICYDB_VERSION_ROLETRANS,
396 	 .sym_num = SYM_NUM,
397 	 .ocon_num = 0,
398 	 .target_platform = SEPOL_TARGET_SELINUX,
399 	},
400 	{
401 	 .type = POLICY_MOD,
402 	 .version = MOD_POLICYDB_VERSION_ROLEATTRIB,
403 	 .sym_num = SYM_NUM,
404 	 .ocon_num = 0,
405 	 .target_platform = SEPOL_TARGET_SELINUX,
406 	},
407 	{
408 	 .type = POLICY_MOD,
409 	 .version = MOD_POLICYDB_VERSION_TUNABLE_SEP,
410 	 .sym_num = SYM_NUM,
411 	 .ocon_num = 0,
412 	 .target_platform = SEPOL_TARGET_SELINUX,
413 	},
414 	{
415 	 .type = POLICY_MOD,
416 	 .version = MOD_POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
417 	 .sym_num = SYM_NUM,
418 	 .ocon_num = 0,
419 	 .target_platform = SEPOL_TARGET_SELINUX,
420 	},
421 	{
422 	 .type = POLICY_MOD,
423 	 .version = MOD_POLICYDB_VERSION_DEFAULT_TYPE,
424 	 .sym_num = SYM_NUM,
425 	 .ocon_num = 0,
426 	 .target_platform = SEPOL_TARGET_SELINUX,
427 	},
428 	{
429 	 .type = POLICY_MOD,
430 	 .version = MOD_POLICYDB_VERSION_CONSTRAINT_NAMES,
431 	 .sym_num = SYM_NUM,
432 	 .ocon_num = 0,
433 	 .target_platform = SEPOL_TARGET_SELINUX,
434 	},
435 	{
436 	 .type = POLICY_MOD,
437 	 .version = MOD_POLICYDB_VERSION_XPERMS_IOCTL,
438 	 .sym_num = SYM_NUM,
439 	 .ocon_num = 0,
440 	 .target_platform = SEPOL_TARGET_SELINUX,
441 	},
442 	{
443 	 .type = POLICY_MOD,
444 	 .version = MOD_POLICYDB_VERSION_INFINIBAND,
445 	 .sym_num = SYM_NUM,
446 	 .ocon_num = 0,
447 	 .target_platform = SEPOL_TARGET_SELINUX,
448 	},
449 	{
450 	 .type = POLICY_MOD,
451 	 .version = MOD_POLICYDB_VERSION_GLBLUB,
452 	 .sym_num = SYM_NUM,
453 	 .ocon_num = 0,
454 	 .target_platform = SEPOL_TARGET_SELINUX,
455 	},
456 	{
457 	 .type = POLICY_MOD,
458 	 .version = MOD_POLICYDB_VERSION_SELF_TYPETRANS,
459 	 .sym_num = SYM_NUM,
460 	 .ocon_num = 0,
461 	 .target_platform = SEPOL_TARGET_SELINUX,
462 	},
463 };
464 
465 #if 0
466 static char *symtab_name[SYM_NUM] = {
467 	"common prefixes",
468 	"classes",
469 	"roles",
470 	"types",
471 	"users",
472 	"bools" mls_symtab_names cond_symtab_names
473 };
474 #endif
475 
476 static const unsigned int symtab_sizes[SYM_NUM] = {
477 	2,
478 	32,
479 	16,
480 	512,
481 	128,
482 	16,
483 	16,
484 	16,
485 };
486 
policydb_lookup_compat(unsigned int version,unsigned int type,unsigned int target_platform)487 const struct policydb_compat_info *policydb_lookup_compat(unsigned int version,
488 						          unsigned int type,
489 						          unsigned int target_platform)
490 {
491 	unsigned int i;
492 	const struct policydb_compat_info *info = NULL;
493 
494 	for (i = 0; i < sizeof(policydb_compat) / sizeof(*info); i++) {
495 		if (policydb_compat[i].version == version &&
496 		    policydb_compat[i].type == type &&
497 		    policydb_compat[i].target_platform == target_platform) {
498 			info = &policydb_compat[i];
499 			break;
500 		}
501 	}
502 	return info;
503 }
504 
type_set_init(type_set_t * x)505 void type_set_init(type_set_t * x)
506 {
507 	memset(x, 0, sizeof(type_set_t));
508 	ebitmap_init(&x->types);
509 	ebitmap_init(&x->negset);
510 }
511 
type_set_destroy(type_set_t * x)512 void type_set_destroy(type_set_t * x)
513 {
514 	if (x != NULL) {
515 		ebitmap_destroy(&x->types);
516 		ebitmap_destroy(&x->negset);
517 	}
518 }
519 
role_set_init(role_set_t * x)520 void role_set_init(role_set_t * x)
521 {
522 	memset(x, 0, sizeof(role_set_t));
523 	ebitmap_init(&x->roles);
524 }
525 
role_set_destroy(role_set_t * x)526 void role_set_destroy(role_set_t * x)
527 {
528 	ebitmap_destroy(&x->roles);
529 }
530 
role_datum_init(role_datum_t * x)531 void role_datum_init(role_datum_t * x)
532 {
533 	memset(x, 0, sizeof(role_datum_t));
534 	ebitmap_init(&x->dominates);
535 	type_set_init(&x->types);
536 	ebitmap_init(&x->cache);
537 	ebitmap_init(&x->roles);
538 }
539 
role_datum_destroy(role_datum_t * x)540 void role_datum_destroy(role_datum_t * x)
541 {
542 	if (x != NULL) {
543 		ebitmap_destroy(&x->dominates);
544 		type_set_destroy(&x->types);
545 		ebitmap_destroy(&x->cache);
546 		ebitmap_destroy(&x->roles);
547 	}
548 }
549 
type_datum_init(type_datum_t * x)550 void type_datum_init(type_datum_t * x)
551 {
552 	memset(x, 0, sizeof(*x));
553 	ebitmap_init(&x->types);
554 }
555 
type_datum_destroy(type_datum_t * x)556 void type_datum_destroy(type_datum_t * x)
557 {
558 	if (x != NULL) {
559 		ebitmap_destroy(&x->types);
560 	}
561 }
562 
user_datum_init(user_datum_t * x)563 void user_datum_init(user_datum_t * x)
564 {
565 	memset(x, 0, sizeof(user_datum_t));
566 	role_set_init(&x->roles);
567 	mls_semantic_range_init(&x->range);
568 	mls_semantic_level_init(&x->dfltlevel);
569 	ebitmap_init(&x->cache);
570 	mls_range_init(&x->exp_range);
571 	mls_level_init(&x->exp_dfltlevel);
572 }
573 
user_datum_destroy(user_datum_t * x)574 void user_datum_destroy(user_datum_t * x)
575 {
576 	if (x != NULL) {
577 		role_set_destroy(&x->roles);
578 		mls_semantic_range_destroy(&x->range);
579 		mls_semantic_level_destroy(&x->dfltlevel);
580 		ebitmap_destroy(&x->cache);
581 		mls_range_destroy(&x->exp_range);
582 		mls_level_destroy(&x->exp_dfltlevel);
583 	}
584 }
585 
level_datum_init(level_datum_t * x)586 void level_datum_init(level_datum_t * x)
587 {
588 	memset(x, 0, sizeof(level_datum_t));
589 }
590 
level_datum_destroy(level_datum_t * x)591 void level_datum_destroy(level_datum_t * x __attribute__ ((unused)))
592 {
593 	/* the mls_level_t referenced by the level_datum is managed
594 	 * separately for now, so there is nothing to destroy */
595 	return;
596 }
597 
cat_datum_init(cat_datum_t * x)598 void cat_datum_init(cat_datum_t * x)
599 {
600 	memset(x, 0, sizeof(cat_datum_t));
601 }
602 
cat_datum_destroy(cat_datum_t * x)603 void cat_datum_destroy(cat_datum_t * x __attribute__ ((unused)))
604 {
605 	/* it's currently a simple struct - really nothing to destroy */
606 	return;
607 }
608 
class_perm_node_init(class_perm_node_t * x)609 void class_perm_node_init(class_perm_node_t * x)
610 {
611 	memset(x, 0, sizeof(class_perm_node_t));
612 }
613 
avrule_init(avrule_t * x)614 void avrule_init(avrule_t * x)
615 {
616 	memset(x, 0, sizeof(avrule_t));
617 	type_set_init(&x->stypes);
618 	type_set_init(&x->ttypes);
619 }
620 
avrule_destroy(avrule_t * x)621 void avrule_destroy(avrule_t * x)
622 {
623 	class_perm_node_t *cur, *next;
624 
625 	if (x == NULL) {
626 		return;
627 	}
628 	type_set_destroy(&x->stypes);
629 	type_set_destroy(&x->ttypes);
630 
631 	free(x->source_filename);
632 
633 	next = x->perms;
634 	while (next) {
635 		cur = next;
636 		next = cur->next;
637 		free(cur);
638 	}
639 
640 	free(x->xperms);
641 }
642 
role_trans_rule_init(role_trans_rule_t * x)643 void role_trans_rule_init(role_trans_rule_t * x)
644 {
645 	memset(x, 0, sizeof(*x));
646 	role_set_init(&x->roles);
647 	type_set_init(&x->types);
648 	ebitmap_init(&x->classes);
649 }
650 
role_trans_rule_destroy(role_trans_rule_t * x)651 static void role_trans_rule_destroy(role_trans_rule_t * x)
652 {
653 	if (x != NULL) {
654 		role_set_destroy(&x->roles);
655 		type_set_destroy(&x->types);
656 		ebitmap_destroy(&x->classes);
657 	}
658 }
659 
role_trans_rule_list_destroy(role_trans_rule_t * x)660 void role_trans_rule_list_destroy(role_trans_rule_t * x)
661 {
662 	while (x != NULL) {
663 		role_trans_rule_t *next = x->next;
664 		role_trans_rule_destroy(x);
665 		free(x);
666 		x = next;
667 	}
668 }
669 
filename_trans_rule_init(filename_trans_rule_t * x)670 void filename_trans_rule_init(filename_trans_rule_t * x)
671 {
672 	memset(x, 0, sizeof(*x));
673 	type_set_init(&x->stypes);
674 	type_set_init(&x->ttypes);
675 }
676 
filename_trans_rule_destroy(filename_trans_rule_t * x)677 static void filename_trans_rule_destroy(filename_trans_rule_t * x)
678 {
679 	if (!x)
680 		return;
681 	type_set_destroy(&x->stypes);
682 	type_set_destroy(&x->ttypes);
683 	free(x->name);
684 }
685 
filename_trans_rule_list_destroy(filename_trans_rule_t * x)686 void filename_trans_rule_list_destroy(filename_trans_rule_t * x)
687 {
688 	filename_trans_rule_t *next;
689 	while (x) {
690 		next = x->next;
691 		filename_trans_rule_destroy(x);
692 		free(x);
693 		x = next;
694 	}
695 }
696 
role_allow_rule_init(role_allow_rule_t * x)697 void role_allow_rule_init(role_allow_rule_t * x)
698 {
699 	memset(x, 0, sizeof(role_allow_rule_t));
700 	role_set_init(&x->roles);
701 	role_set_init(&x->new_roles);
702 }
703 
role_allow_rule_destroy(role_allow_rule_t * x)704 void role_allow_rule_destroy(role_allow_rule_t * x)
705 {
706 	role_set_destroy(&x->roles);
707 	role_set_destroy(&x->new_roles);
708 }
709 
role_allow_rule_list_destroy(role_allow_rule_t * x)710 void role_allow_rule_list_destroy(role_allow_rule_t * x)
711 {
712 	while (x != NULL) {
713 		role_allow_rule_t *next = x->next;
714 		role_allow_rule_destroy(x);
715 		free(x);
716 		x = next;
717 	}
718 }
719 
range_trans_rule_init(range_trans_rule_t * x)720 void range_trans_rule_init(range_trans_rule_t * x)
721 {
722 	type_set_init(&x->stypes);
723 	type_set_init(&x->ttypes);
724 	ebitmap_init(&x->tclasses);
725 	mls_semantic_range_init(&x->trange);
726 	x->next = NULL;
727 }
728 
range_trans_rule_destroy(range_trans_rule_t * x)729 void range_trans_rule_destroy(range_trans_rule_t * x)
730 {
731 	type_set_destroy(&x->stypes);
732 	type_set_destroy(&x->ttypes);
733 	ebitmap_destroy(&x->tclasses);
734 	mls_semantic_range_destroy(&x->trange);
735 }
736 
range_trans_rule_list_destroy(range_trans_rule_t * x)737 void range_trans_rule_list_destroy(range_trans_rule_t * x)
738 {
739 	while (x != NULL) {
740 		range_trans_rule_t *next = x->next;
741 		range_trans_rule_destroy(x);
742 		free(x);
743 		x = next;
744 	}
745 }
746 
avrule_list_destroy(avrule_t * x)747 void avrule_list_destroy(avrule_t * x)
748 {
749 	avrule_t *next, *cur;
750 
751 	if (!x)
752 		return;
753 
754 	next = x;
755 	while (next) {
756 		cur = next;
757 		next = next->next;
758 		avrule_destroy(cur);
759 		free(cur);
760 	}
761 }
762 
763 /*
764  * Initialize the role table by implicitly adding role 'object_r'.  If
765  * the policy is a module, set object_r's scope to be SCOPE_REQ,
766  * otherwise set it to SCOPE_DECL.
767  */
roles_init(policydb_t * p)768 static int roles_init(policydb_t * p)
769 {
770 	char *key = 0;
771 	int rc;
772 	role_datum_t *role;
773 
774 	role = calloc(1, sizeof(role_datum_t));
775 	if (!role) {
776 		rc = -ENOMEM;
777 		goto out;
778 	}
779 	key = strdup(OBJECT_R);
780 	if (!key) {
781 		rc = -ENOMEM;
782 		goto out_free_role;
783 	}
784 	rc = symtab_insert(p, SYM_ROLES, key, role,
785 			   (p->policy_type ==
786 			    POLICY_MOD ? SCOPE_REQ : SCOPE_DECL), 1,
787 			   &role->s.value);
788 	if (rc)
789 		goto out_free_key;
790 	if (role->s.value != OBJECT_R_VAL) {
791 		rc = -EINVAL;
792 		goto out_free_role;
793 	}
794       out:
795 	return rc;
796 
797       out_free_key:
798 	free(key);
799       out_free_role:
800 	free(role);
801 	goto out;
802 }
803 
804 ignore_unsigned_overflow_
805 static inline unsigned long
partial_name_hash(unsigned long c,unsigned long prevhash)806 partial_name_hash(unsigned long c, unsigned long prevhash)
807 {
808 	return (prevhash + (c << 4) + (c >> 4)) * 11;
809 }
810 
filenametr_hash(hashtab_t h,const_hashtab_key_t k)811 static unsigned int filenametr_hash(hashtab_t h, const_hashtab_key_t k)
812 {
813 	const filename_trans_key_t *ft = (const filename_trans_key_t *)k;
814 	unsigned long hash;
815 	unsigned int byte_num;
816 	unsigned char focus;
817 
818 	hash = ft->ttype ^ ft->tclass;
819 
820 	byte_num = 0;
821 	while ((focus = ft->name[byte_num++]))
822 		hash = partial_name_hash(focus, hash);
823 	return hash & (h->size - 1);
824 }
825 
filenametr_cmp(hashtab_t h,const_hashtab_key_t k1,const_hashtab_key_t k2)826 static int filenametr_cmp(hashtab_t h __attribute__ ((unused)),
827 			  const_hashtab_key_t k1, const_hashtab_key_t k2)
828 {
829 	const filename_trans_key_t *ft1 = (const filename_trans_key_t *)k1;
830 	const filename_trans_key_t *ft2 = (const filename_trans_key_t *)k2;
831 	int v;
832 
833 	v = spaceship_cmp(ft1->ttype, ft2->ttype);
834 	if (v)
835 		return v;
836 
837 	v = spaceship_cmp(ft1->tclass, ft2->tclass);
838 	if (v)
839 		return v;
840 
841 	return strcmp(ft1->name, ft2->name);
842 
843 }
844 
rangetr_hash(hashtab_t h,const_hashtab_key_t k)845 static unsigned int rangetr_hash(hashtab_t h, const_hashtab_key_t k)
846 {
847 	const struct range_trans *key = (const struct range_trans *)k;
848 	return (key->source_type + (key->target_type << 3) +
849 		(key->target_class << 5)) & (h->size - 1);
850 }
851 
rangetr_cmp(hashtab_t h,const_hashtab_key_t k1,const_hashtab_key_t k2)852 static int rangetr_cmp(hashtab_t h __attribute__ ((unused)),
853 		       const_hashtab_key_t k1, const_hashtab_key_t k2)
854 {
855 	const struct range_trans *key1 = (const struct range_trans *)k1;
856 	const struct range_trans *key2 = (const struct range_trans *)k2;
857 	int v;
858 
859 	v = spaceship_cmp(key1->source_type, key2->source_type);
860 	if (v)
861 		return v;
862 
863 	v = spaceship_cmp(key1->target_type, key2->target_type);
864 	if (v)
865 		return v;
866 
867 	v = spaceship_cmp(key1->target_class, key2->target_class);
868 
869 	return v;
870 }
871 
872 /*
873  * Initialize a policy database structure.
874  */
policydb_init(policydb_t * p)875 int policydb_init(policydb_t * p)
876 {
877 	int i, rc;
878 
879 	memset(p, 0, sizeof(policydb_t));
880 
881 	for (i = 0; i < SYM_NUM; i++) {
882 		p->sym_val_to_name[i] = NULL;
883 		rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
884 		if (rc)
885 			goto err;
886 	}
887 
888 	/* initialize the module stuff */
889 	for (i = 0; i < SYM_NUM; i++) {
890 		if (symtab_init(&p->scope[i], symtab_sizes[i])) {
891 			goto err;
892 		}
893 	}
894 	if ((p->global = avrule_block_create()) == NULL ||
895 	    (p->global->branch_list = avrule_decl_create(1)) == NULL) {
896 		goto err;
897 	}
898 	p->decl_val_to_struct = NULL;
899 
900 	rc = avtab_init(&p->te_avtab);
901 	if (rc)
902 		goto err;
903 
904 	rc = roles_init(p);
905 	if (rc)
906 		goto err;
907 
908 	rc = cond_policydb_init(p);
909 	if (rc)
910 		goto err;
911 
912 	p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
913 	if (!p->filename_trans) {
914 		rc = -ENOMEM;
915 		goto err;
916 	}
917 
918 	p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
919 	if (!p->range_tr) {
920 		rc = -ENOMEM;
921 		goto err;
922 	}
923 
924 	ebitmap_init(&p->policycaps);
925 	ebitmap_init(&p->permissive_map);
926 
927 	return 0;
928 err:
929 	hashtab_destroy(p->filename_trans);
930 	hashtab_destroy(p->range_tr);
931 	for (i = 0; i < SYM_NUM; i++) {
932 		hashtab_destroy(p->symtab[i].table);
933 		hashtab_destroy(p->scope[i].table);
934 	}
935 	avrule_block_list_destroy(p->global);
936 	return rc;
937 }
938 
policydb_role_cache(hashtab_key_t key,hashtab_datum_t datum,void * arg)939 int policydb_role_cache(hashtab_key_t key
940 			__attribute__ ((unused)), hashtab_datum_t datum,
941 			void *arg)
942 {
943 	policydb_t *p;
944 	role_datum_t *role;
945 
946 	role = (role_datum_t *) datum;
947 	p = (policydb_t *) arg;
948 
949 	ebitmap_destroy(&role->cache);
950 	if (type_set_expand(&role->types, &role->cache, p, 1)) {
951 		return -1;
952 	}
953 
954 	return 0;
955 }
956 
policydb_user_cache(hashtab_key_t key,hashtab_datum_t datum,void * arg)957 int policydb_user_cache(hashtab_key_t key
958 			__attribute__ ((unused)), hashtab_datum_t datum,
959 			void *arg)
960 {
961 	policydb_t *p;
962 	user_datum_t *user;
963 
964 	user = (user_datum_t *) datum;
965 	p = (policydb_t *) arg;
966 
967 	ebitmap_destroy(&user->cache);
968 	if (role_set_expand(&user->roles, &user->cache, p, NULL, NULL)) {
969 		return -1;
970 	}
971 
972 	/* we do not expand user's MLS info in kernel policies because the
973 	 * semantic representation is not present and we do not expand user's
974 	 * MLS info in module policies because all of the necessary mls
975 	 * information is not present */
976 	if (p->policy_type != POLICY_KERN && p->policy_type != POLICY_MOD) {
977 		mls_range_destroy(&user->exp_range);
978 		if (mls_semantic_range_expand(&user->range,
979 					      &user->exp_range, p, NULL)) {
980 			return -1;
981 		}
982 
983 		mls_level_destroy(&user->exp_dfltlevel);
984 		if (mls_semantic_level_expand(&user->dfltlevel,
985 					      &user->exp_dfltlevel, p, NULL)) {
986 			return -1;
987 		}
988 	}
989 
990 	return 0;
991 }
992 
993 /*
994  * The following *_index functions are used to
995  * define the val_to_name and val_to_struct arrays
996  * in a policy database structure.  The val_to_name
997  * arrays are used when converting security context
998  * structures into string representations.  The
999  * val_to_struct arrays are used when the attributes
1000  * of a class, role, or user are needed.
1001  */
1002 
common_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1003 static int common_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1004 {
1005 	policydb_t *p;
1006 	common_datum_t *comdatum;
1007 
1008 	comdatum = (common_datum_t *) datum;
1009 	p = (policydb_t *) datap;
1010 	if (!value_isvalid(comdatum->s.value, p->p_commons.nprim))
1011 		return -EINVAL;
1012 	if (p->p_common_val_to_name[comdatum->s.value - 1] != NULL)
1013 		return -EINVAL;
1014 	p->p_common_val_to_name[comdatum->s.value - 1] = (char *)key;
1015 
1016 	return 0;
1017 }
1018 
class_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1019 static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1020 {
1021 	policydb_t *p;
1022 	class_datum_t *cladatum;
1023 
1024 	cladatum = (class_datum_t *) datum;
1025 	p = (policydb_t *) datap;
1026 	if (!value_isvalid(cladatum->s.value, p->p_classes.nprim))
1027 		return -EINVAL;
1028 	if (p->p_class_val_to_name[cladatum->s.value - 1] != NULL)
1029 		return -EINVAL;
1030 	p->p_class_val_to_name[cladatum->s.value - 1] = (char *)key;
1031 	p->class_val_to_struct[cladatum->s.value - 1] = cladatum;
1032 
1033 	return 0;
1034 }
1035 
role_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1036 static int role_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1037 {
1038 	policydb_t *p;
1039 	role_datum_t *role;
1040 
1041 	role = (role_datum_t *) datum;
1042 	p = (policydb_t *) datap;
1043 	if (!value_isvalid(role->s.value, p->p_roles.nprim))
1044 		return -EINVAL;
1045 	if (p->p_role_val_to_name[role->s.value - 1] != NULL)
1046 		return -EINVAL;
1047 	p->p_role_val_to_name[role->s.value - 1] = (char *)key;
1048 	p->role_val_to_struct[role->s.value - 1] = role;
1049 
1050 	return 0;
1051 }
1052 
type_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1053 static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1054 {
1055 	policydb_t *p;
1056 	type_datum_t *typdatum;
1057 
1058 	typdatum = (type_datum_t *) datum;
1059 	p = (policydb_t *) datap;
1060 
1061 	if (typdatum->primary) {
1062 		if (!value_isvalid(typdatum->s.value, p->p_types.nprim))
1063 			return -EINVAL;
1064 		if (p->p_type_val_to_name[typdatum->s.value - 1] != NULL)
1065 			return -EINVAL;
1066 		p->p_type_val_to_name[typdatum->s.value - 1] = (char *)key;
1067 		p->type_val_to_struct[typdatum->s.value - 1] = typdatum;
1068 	}
1069 
1070 	return 0;
1071 }
1072 
user_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1073 static int user_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1074 {
1075 	policydb_t *p;
1076 	user_datum_t *usrdatum;
1077 
1078 	usrdatum = (user_datum_t *) datum;
1079 	p = (policydb_t *) datap;
1080 
1081 	if (!value_isvalid(usrdatum->s.value, p->p_users.nprim))
1082 		return -EINVAL;
1083 	if (p->p_user_val_to_name[usrdatum->s.value - 1] != NULL)
1084 		return -EINVAL;
1085 	p->p_user_val_to_name[usrdatum->s.value - 1] = (char *)key;
1086 	p->user_val_to_struct[usrdatum->s.value - 1] = usrdatum;
1087 
1088 	return 0;
1089 }
1090 
sens_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1091 static int sens_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1092 {
1093 	policydb_t *p;
1094 	level_datum_t *levdatum;
1095 
1096 	levdatum = (level_datum_t *) datum;
1097 	p = (policydb_t *) datap;
1098 
1099 	if (!levdatum->isalias) {
1100 		if (!value_isvalid(levdatum->level->sens, p->p_levels.nprim))
1101 			return -EINVAL;
1102 		if (p->p_sens_val_to_name[levdatum->level->sens - 1] != NULL)
1103 			return -EINVAL;
1104 		p->p_sens_val_to_name[levdatum->level->sens - 1] = (char *)key;
1105 	}
1106 
1107 	return 0;
1108 }
1109 
cat_index(hashtab_key_t key,hashtab_datum_t datum,void * datap)1110 static int cat_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
1111 {
1112 	policydb_t *p;
1113 	cat_datum_t *catdatum;
1114 
1115 	catdatum = (cat_datum_t *) datum;
1116 	p = (policydb_t *) datap;
1117 
1118 	if (!catdatum->isalias) {
1119 		if (!value_isvalid(catdatum->s.value, p->p_cats.nprim))
1120 			return -EINVAL;
1121 		if (p->p_cat_val_to_name[catdatum->s.value - 1] != NULL)
1122 			return -EINVAL;
1123 		p->p_cat_val_to_name[catdatum->s.value - 1] = (char *)key;
1124 	}
1125 
1126 	return 0;
1127 }
1128 
1129 static int (*const index_f[SYM_NUM]) (hashtab_key_t key, hashtab_datum_t datum,
1130 				void *datap) = {
1131 common_index, class_index, role_index, type_index, user_index,
1132 	    cond_index_bool, sens_index, cat_index,};
1133 
1134 /*
1135  * Define the common val_to_name array and the class
1136  * val_to_name and val_to_struct arrays in a policy
1137  * database structure.
1138  */
policydb_index_classes(policydb_t * p)1139 int policydb_index_classes(policydb_t * p)
1140 {
1141 	free(p->p_common_val_to_name);
1142 	p->p_common_val_to_name = (char **)
1143 	    calloc(p->p_commons.nprim, sizeof(char *));
1144 	if (!p->p_common_val_to_name)
1145 		return -1;
1146 
1147 	if (hashtab_map(p->p_commons.table, common_index, p))
1148 		return -1;
1149 
1150 	free(p->class_val_to_struct);
1151 	p->class_val_to_struct = (class_datum_t **)
1152 	    calloc(p->p_classes.nprim, sizeof(class_datum_t *));
1153 	if (!p->class_val_to_struct)
1154 		return -1;
1155 
1156 	free(p->p_class_val_to_name);
1157 	p->p_class_val_to_name = (char **)
1158 	    calloc(p->p_classes.nprim, sizeof(char *));
1159 	if (!p->p_class_val_to_name)
1160 		return -1;
1161 
1162 	if (hashtab_map(p->p_classes.table, class_index, p))
1163 		return -1;
1164 
1165 	return 0;
1166 }
1167 
policydb_index_bools(policydb_t * p)1168 int policydb_index_bools(policydb_t * p)
1169 {
1170 
1171 	if (cond_init_bool_indexes(p) == -1)
1172 		return -1;
1173 	p->p_bool_val_to_name = (char **)
1174 	    calloc(p->p_bools.nprim, sizeof(char *));
1175 	if (!p->p_bool_val_to_name)
1176 		return -1;
1177 	if (hashtab_map(p->p_bools.table, cond_index_bool, p))
1178 		return -1;
1179 	return 0;
1180 }
1181 
policydb_index_decls(sepol_handle_t * handle,policydb_t * p)1182 static int policydb_index_decls(sepol_handle_t * handle, policydb_t * p)
1183 {
1184 	avrule_block_t *curblock;
1185 	avrule_decl_t *decl;
1186 	unsigned int num_decls = 0;
1187 
1188 	free(p->decl_val_to_struct);
1189 
1190 	for (curblock = p->global; curblock != NULL; curblock = curblock->next) {
1191 		for (decl = curblock->branch_list; decl != NULL;
1192 		     decl = decl->next) {
1193 			num_decls++;
1194 		}
1195 	}
1196 
1197 	p->decl_val_to_struct =
1198 	    calloc(num_decls, sizeof(*(p->decl_val_to_struct)));
1199 	if (!p->decl_val_to_struct) {
1200 		return -1;
1201 	}
1202 
1203 	for (curblock = p->global; curblock != NULL; curblock = curblock->next) {
1204 		for (decl = curblock->branch_list; decl != NULL;
1205 		     decl = decl->next) {
1206 			if (!value_isvalid(decl->decl_id, num_decls)) {
1207 				ERR(handle, "invalid decl ID %u", decl->decl_id);
1208 				return -1;
1209 			}
1210 			if (p->decl_val_to_struct[decl->decl_id - 1] != NULL) {
1211 				ERR(handle, "duplicated decl ID %u", decl->decl_id);
1212 				return -1;
1213 			}
1214 			p->decl_val_to_struct[decl->decl_id - 1] = decl;
1215 		}
1216 	}
1217 
1218 	return 0;
1219 }
1220 
1221 /*
1222  * Define the other val_to_name and val_to_struct arrays
1223  * in a policy database structure.
1224  */
policydb_index_others(sepol_handle_t * handle,policydb_t * p,unsigned verbose)1225 int policydb_index_others(sepol_handle_t * handle,
1226 			  policydb_t * p, unsigned verbose)
1227 {
1228 	int i;
1229 
1230 	if (verbose) {
1231 		INFO(handle,
1232 		     "security:  %d users, %d roles, %d types, %d bools",
1233 		     p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
1234 		     p->p_bools.nprim);
1235 
1236 		if (p->mls)
1237 			INFO(handle, "security: %d sens, %d cats",
1238 			     p->p_levels.nprim, p->p_cats.nprim);
1239 
1240 		INFO(handle, "security:  %d classes, %d rules, %d cond rules",
1241 		     p->p_classes.nprim, p->te_avtab.nel, p->te_cond_avtab.nel);
1242 	}
1243 #if 0
1244 	avtab_hash_eval(&p->te_avtab, "rules");
1245 	for (i = 0; i < SYM_NUM; i++)
1246 		hashtab_hash_eval(p->symtab[i].table, symtab_name[i]);
1247 #endif
1248 
1249 	free(p->role_val_to_struct);
1250 	p->role_val_to_struct = (role_datum_t **)
1251 	    calloc(p->p_roles.nprim, sizeof(role_datum_t *));
1252 	if (!p->role_val_to_struct)
1253 		return -1;
1254 
1255 	free(p->user_val_to_struct);
1256 	p->user_val_to_struct = (user_datum_t **)
1257 	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
1258 	if (!p->user_val_to_struct)
1259 		return -1;
1260 
1261 	free(p->type_val_to_struct);
1262 	p->type_val_to_struct = (type_datum_t **)
1263 	    calloc(p->p_types.nprim, sizeof(type_datum_t *));
1264 	if (!p->type_val_to_struct)
1265 		return -1;
1266 
1267 	if (cond_init_bool_indexes(p))
1268 		return -1;
1269 
1270 	for (i = SYM_ROLES; i < SYM_NUM; i++) {
1271 		free(p->sym_val_to_name[i]);
1272 		p->sym_val_to_name[i] = NULL;
1273 		if (p->symtab[i].nprim) {
1274 			p->sym_val_to_name[i] = (char **)
1275 			    calloc(p->symtab[i].nprim, sizeof(char *));
1276 			if (!p->sym_val_to_name[i])
1277 				return -1;
1278 			if (hashtab_map(p->symtab[i].table, index_f[i], p))
1279 				return -1;
1280 		}
1281 	}
1282 
1283 	/* This pre-expands the roles and users for context validity checking */
1284 	if (hashtab_map(p->p_roles.table, policydb_role_cache, p))
1285 		return -1;
1286 
1287 	if (hashtab_map(p->p_users.table, policydb_user_cache, p))
1288 		return -1;
1289 
1290 	return 0;
1291 }
1292 
1293 /*
1294  * The following *_destroy functions are used to
1295  * free any memory allocated for each kind of
1296  * symbol data in the policy database.
1297  */
1298 
perm_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1299 static int perm_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1300 			__attribute__ ((unused)))
1301 {
1302 	if (key)
1303 		free(key);
1304 	free(datum);
1305 	return 0;
1306 }
1307 
common_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1308 static int common_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1309 			  __attribute__ ((unused)))
1310 {
1311 	common_datum_t *comdatum;
1312 
1313 	if (key)
1314 		free(key);
1315 	comdatum = (common_datum_t *) datum;
1316 	(void)hashtab_map(comdatum->permissions.table, perm_destroy, 0);
1317 	hashtab_destroy(comdatum->permissions.table);
1318 	free(datum);
1319 	return 0;
1320 }
1321 
class_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1322 static int class_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1323 			 __attribute__ ((unused)))
1324 {
1325 	class_datum_t *cladatum;
1326 	constraint_node_t *constraint, *ctemp;
1327 
1328 	if (key)
1329 		free(key);
1330 	cladatum = (class_datum_t *) datum;
1331 	if (cladatum == NULL) {
1332 		return 0;
1333 	}
1334 	(void)hashtab_map(cladatum->permissions.table, perm_destroy, 0);
1335 	hashtab_destroy(cladatum->permissions.table);
1336 	constraint = cladatum->constraints;
1337 	while (constraint) {
1338 		constraint_expr_destroy(constraint->expr);
1339 		ctemp = constraint;
1340 		constraint = constraint->next;
1341 		free(ctemp);
1342 	}
1343 
1344 	constraint = cladatum->validatetrans;
1345 	while (constraint) {
1346 		constraint_expr_destroy(constraint->expr);
1347 		ctemp = constraint;
1348 		constraint = constraint->next;
1349 		free(ctemp);
1350 	}
1351 
1352 	if (cladatum->comkey)
1353 		free(cladatum->comkey);
1354 	free(datum);
1355 	return 0;
1356 }
1357 
role_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1358 static int role_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1359 			__attribute__ ((unused)))
1360 {
1361 	free(key);
1362 	role_datum_destroy((role_datum_t *) datum);
1363 	free(datum);
1364 	return 0;
1365 }
1366 
type_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1367 static int type_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1368 			__attribute__ ((unused)))
1369 {
1370 	free(key);
1371 	type_datum_destroy((type_datum_t *) datum);
1372 	free(datum);
1373 	return 0;
1374 }
1375 
user_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1376 static int user_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1377 			__attribute__ ((unused)))
1378 {
1379 	free(key);
1380 	user_datum_destroy((user_datum_t *) datum);
1381 	free(datum);
1382 	return 0;
1383 }
1384 
sens_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1385 static int sens_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1386 			__attribute__ ((unused)))
1387 {
1388 	level_datum_t *levdatum;
1389 
1390 	if (key)
1391 		free(key);
1392 	levdatum = (level_datum_t *) datum;
1393 	if (!levdatum->isalias || !levdatum->notdefined) {
1394 		mls_level_destroy(levdatum->level);
1395 		free(levdatum->level);
1396 	}
1397 	level_datum_destroy(levdatum);
1398 	free(levdatum);
1399 	return 0;
1400 }
1401 
cat_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1402 static int cat_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1403 		       __attribute__ ((unused)))
1404 {
1405 	if (key)
1406 		free(key);
1407 	cat_datum_destroy((cat_datum_t *) datum);
1408 	free(datum);
1409 	return 0;
1410 }
1411 
1412 static int (*const destroy_f[SYM_NUM]) (hashtab_key_t key, hashtab_datum_t datum,
1413 				  void *datap) = {
1414 common_destroy, class_destroy, role_destroy, type_destroy, user_destroy,
1415 	    cond_destroy_bool, sens_destroy, cat_destroy,};
1416 
filenametr_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1417 static int filenametr_destroy(hashtab_key_t key, hashtab_datum_t datum,
1418 			      void *p __attribute__ ((unused)))
1419 {
1420 	filename_trans_key_t *ft = (filename_trans_key_t *)key;
1421 	filename_trans_datum_t *fd = datum, *next;
1422 
1423 	free(ft->name);
1424 	free(key);
1425 	do {
1426 		next = fd->next;
1427 		ebitmap_destroy(&fd->stypes);
1428 		free(fd);
1429 		fd = next;
1430 	} while (fd);
1431 	return 0;
1432 }
1433 
range_tr_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1434 static int range_tr_destroy(hashtab_key_t key, hashtab_datum_t datum,
1435 			    void *p __attribute__ ((unused)))
1436 {
1437 	struct mls_range *rt = (struct mls_range *)datum;
1438 	free(key);
1439 	ebitmap_destroy(&rt->level[0].cat);
1440 	ebitmap_destroy(&rt->level[1].cat);
1441 	free(datum);
1442 	return 0;
1443 }
1444 
ocontext_selinux_free(ocontext_t ** ocontexts)1445 static void ocontext_selinux_free(ocontext_t **ocontexts)
1446 {
1447 	ocontext_t *c, *ctmp;
1448 	int i;
1449 
1450 	for (i = 0; i < OCON_NUM; i++) {
1451 		c = ocontexts[i];
1452 		while (c) {
1453 			ctmp = c;
1454 			c = c->next;
1455 			context_destroy(&ctmp->context[0]);
1456 			context_destroy(&ctmp->context[1]);
1457 			if (i == OCON_ISID || i == OCON_FS || i == OCON_NETIF
1458 				|| i == OCON_FSUSE)
1459 				free(ctmp->u.name);
1460 			else if (i == OCON_IBENDPORT)
1461 				free(ctmp->u.ibendport.dev_name);
1462 			free(ctmp);
1463 		}
1464 	}
1465 }
1466 
ocontext_xen_free(ocontext_t ** ocontexts)1467 static void ocontext_xen_free(ocontext_t **ocontexts)
1468 {
1469 	ocontext_t *c, *ctmp;
1470 	int i;
1471 
1472 	for (i = 0; i < OCON_NUM; i++) {
1473 		c = ocontexts[i];
1474 		while (c) {
1475 			ctmp = c;
1476 			c = c->next;
1477 			context_destroy(&ctmp->context[0]);
1478 			context_destroy(&ctmp->context[1]);
1479 			if (i == OCON_ISID || i == OCON_XEN_DEVICETREE)
1480 				free(ctmp->u.name);
1481 			free(ctmp);
1482 		}
1483 	}
1484 }
1485 
1486 /*
1487  * Free any memory allocated by a policy database structure.
1488  */
policydb_destroy(policydb_t * p)1489 void policydb_destroy(policydb_t * p)
1490 {
1491 	ocontext_t *c, *ctmp;
1492 	genfs_t *g, *gtmp;
1493 	unsigned int i;
1494 	role_allow_t *ra, *lra = NULL;
1495 	role_trans_t *tr, *ltr = NULL;
1496 
1497 	if (!p)
1498 		return;
1499 
1500 	ebitmap_destroy(&p->policycaps);
1501 
1502 	ebitmap_destroy(&p->permissive_map);
1503 
1504 	symtabs_destroy(p->symtab);
1505 
1506 	for (i = 0; i < SYM_NUM; i++) {
1507 		if (p->sym_val_to_name[i])
1508 			free(p->sym_val_to_name[i]);
1509 	}
1510 
1511 	if (p->class_val_to_struct)
1512 		free(p->class_val_to_struct);
1513 	if (p->role_val_to_struct)
1514 		free(p->role_val_to_struct);
1515 	if (p->user_val_to_struct)
1516 		free(p->user_val_to_struct);
1517 	if (p->type_val_to_struct)
1518 		free(p->type_val_to_struct);
1519 	free(p->decl_val_to_struct);
1520 
1521 	for (i = 0; i < SYM_NUM; i++) {
1522 		(void)hashtab_map(p->scope[i].table, scope_destroy, 0);
1523 		hashtab_destroy(p->scope[i].table);
1524 	}
1525 	avrule_block_list_destroy(p->global);
1526 	free(p->name);
1527 	free(p->version);
1528 
1529 	avtab_destroy(&p->te_avtab);
1530 
1531 	if (p->target_platform == SEPOL_TARGET_SELINUX)
1532 		ocontext_selinux_free(p->ocontexts);
1533 	else if (p->target_platform == SEPOL_TARGET_XEN)
1534 		ocontext_xen_free(p->ocontexts);
1535 
1536 	g = p->genfs;
1537 	while (g) {
1538 		free(g->fstype);
1539 		c = g->head;
1540 		while (c) {
1541 			ctmp = c;
1542 			c = c->next;
1543 			context_destroy(&ctmp->context[0]);
1544 			free(ctmp->u.name);
1545 			free(ctmp);
1546 		}
1547 		gtmp = g;
1548 		g = g->next;
1549 		free(gtmp);
1550 	}
1551 	cond_policydb_destroy(p);
1552 
1553 	for (tr = p->role_tr; tr; tr = tr->next) {
1554 		if (ltr)
1555 			free(ltr);
1556 		ltr = tr;
1557 	}
1558 	if (ltr)
1559 		free(ltr);
1560 
1561 	for (ra = p->role_allow; ra; ra = ra->next) {
1562 		if (lra)
1563 			free(lra);
1564 		lra = ra;
1565 	}
1566 	if (lra)
1567 		free(lra);
1568 
1569 	hashtab_map(p->filename_trans, filenametr_destroy, NULL);
1570 	hashtab_destroy(p->filename_trans);
1571 
1572 	hashtab_map(p->range_tr, range_tr_destroy, NULL);
1573 	hashtab_destroy(p->range_tr);
1574 
1575 	if (p->type_attr_map) {
1576 		for (i = 0; i < p->p_types.nprim; i++) {
1577 			ebitmap_destroy(&p->type_attr_map[i]);
1578 		}
1579 		free(p->type_attr_map);
1580 	}
1581 
1582 	if (p->attr_type_map) {
1583 		for (i = 0; i < p->p_types.nprim; i++) {
1584 			ebitmap_destroy(&p->attr_type_map[i]);
1585 		}
1586 		free(p->attr_type_map);
1587 	}
1588 
1589 	return;
1590 }
1591 
symtabs_destroy(symtab_t * symtab)1592 void symtabs_destroy(symtab_t * symtab)
1593 {
1594 	int i;
1595 	for (i = 0; i < SYM_NUM; i++) {
1596 		(void)hashtab_map(symtab[i].table, destroy_f[i], 0);
1597 		hashtab_destroy(symtab[i].table);
1598 	}
1599 }
1600 
scope_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)1601 int scope_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
1602 		  __attribute__ ((unused)))
1603 {
1604 	scope_datum_t *cur = (scope_datum_t *) datum;
1605 	free(key);
1606 	if (cur != NULL) {
1607 		free(cur->decl_ids);
1608 	}
1609 	free(cur);
1610 	return 0;
1611 }
1612 
1613 /*
1614  * Load the initial SIDs specified in a policy database
1615  * structure into a SID table.
1616  */
policydb_load_isids(policydb_t * p,sidtab_t * s)1617 int policydb_load_isids(policydb_t * p, sidtab_t * s)
1618 {
1619 	ocontext_t *head, *c;
1620 
1621 	if (sepol_sidtab_init(s)) {
1622 		ERR(NULL, "out of memory on SID table init");
1623 		return -1;
1624 	}
1625 
1626 	head = p->ocontexts[OCON_ISID];
1627 	for (c = head; c; c = c->next) {
1628 		if (sepol_sidtab_insert(s, c->sid[0], &c->context[0])) {
1629 			ERR(NULL, "unable to load initial SID %s", c->u.name);
1630 			return -1;
1631 		}
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 /* Declare a symbol for a certain avrule_block context.  Insert it
1638  * into a symbol table for a policy.  This function will handle
1639  * inserting the appropriate scope information in addition to
1640  * inserting the symbol into the hash table.
1641  *
1642  * arguments:
1643  *   policydb_t *pol       module policy to modify
1644  *   uint32_t sym          the symbole table for insertion (SYM_*)
1645  *   hashtab_key_t key     the key for the symbol - not cloned
1646  *   hashtab_datum_t data  the data for the symbol - not cloned
1647  *   scope                 scope of this symbol, either SCOPE_REQ or SCOPE_DECL
1648  *   avrule_decl_id        identifier for this symbol's encapsulating declaration
1649  *   value (out)           assigned value to the symbol (if value is not NULL)
1650  *
1651  * returns:
1652  *   0                     success
1653  *   1                     success, but symbol already existed as a requirement
1654  *                         (datum was not inserted and needs to be free()d)
1655  *   -1                    general error
1656  *   -2                    scope conflicted
1657  *   -ENOMEM               memory error
1658  *   error codes from hashtab_insert
1659  */
symtab_insert(policydb_t * pol,uint32_t sym,hashtab_key_t key,hashtab_datum_t datum,uint32_t scope,uint32_t avrule_decl_id,uint32_t * value)1660 int symtab_insert(policydb_t * pol, uint32_t sym,
1661 		  hashtab_key_t key, hashtab_datum_t datum,
1662 		  uint32_t scope, uint32_t avrule_decl_id, uint32_t * value)
1663 {
1664 	int rc, retval = 0;
1665 	unsigned int i;
1666 	scope_datum_t *scope_datum;
1667 
1668 	/* check if the symbol is already there.  multiple
1669 	 * declarations of non-roles/non-users are illegal, but
1670 	 * multiple requires are allowed. */
1671 
1672 	/* FIX ME - the failures after the hashtab_insert will leave
1673 	 * the policy in a inconsistent state. */
1674 	rc = hashtab_insert(pol->symtab[sym].table, key, datum);
1675 	if (rc == SEPOL_OK) {
1676 		/* if no value is passed in the symbol is not primary
1677 		 * (i.e. aliases) */
1678 		if (value)
1679 			*value = ++pol->symtab[sym].nprim;
1680 	} else if (rc == SEPOL_EEXIST) {
1681 		retval = 1;	/* symbol not added -- need to free() later */
1682 	} else {
1683 		return rc;
1684 	}
1685 
1686 	/* get existing scope information; if there is not one then
1687 	 * create it */
1688 	scope_datum =
1689 	    (scope_datum_t *) hashtab_search(pol->scope[sym].table, key);
1690 	if (scope_datum == NULL) {
1691 		hashtab_key_t key2 = strdup((char *)key);
1692 		if (!key2)
1693 			return -ENOMEM;
1694 		if ((scope_datum = malloc(sizeof(*scope_datum))) == NULL) {
1695 			free(key2);
1696 			return -ENOMEM;
1697 		}
1698 		scope_datum->scope = scope;
1699 		scope_datum->decl_ids = NULL;
1700 		scope_datum->decl_ids_len = 0;
1701 		if ((rc =
1702 		     hashtab_insert(pol->scope[sym].table, key2,
1703 				    scope_datum)) != 0) {
1704 			free(key2);
1705 			free(scope_datum);
1706 			return rc;
1707 		}
1708 	} else if (scope_datum->scope == SCOPE_DECL && scope == SCOPE_DECL) {
1709 		/* disallow multiple declarations for non-roles/users */
1710 		if (sym != SYM_ROLES && sym != SYM_USERS) {
1711 			return -2;
1712 		}
1713 		/* Further confine that a role attribute can't have the same
1714 		 * name as another regular role, and a role attribute can't
1715 		 * be declared more than once. */
1716 		if (sym == SYM_ROLES) {
1717 			role_datum_t *base_role;
1718 			role_datum_t *cur_role = (role_datum_t *)datum;
1719 
1720 			base_role = (role_datum_t *)
1721 					hashtab_search(pol->symtab[sym].table,
1722 						       key);
1723 			assert(base_role != NULL);
1724 
1725 			if (!((base_role->flavor == ROLE_ROLE) &&
1726 			    (cur_role->flavor == ROLE_ROLE))) {
1727 				/* Only regular roles are allowed to have
1728 				 * multiple declarations. */
1729 				return -2;
1730 			}
1731 		}
1732 	} else if (scope_datum->scope == SCOPE_REQ && scope == SCOPE_DECL) {
1733 		scope_datum->scope = SCOPE_DECL;
1734 	}
1735 
1736 	/* search through the pre-existing list to avoid adding duplicates */
1737 	for (i = 0; i < scope_datum->decl_ids_len; i++) {
1738 		if (scope_datum->decl_ids[i] == avrule_decl_id) {
1739 			/* already there, so don't modify its scope */
1740 			return retval;
1741 		}
1742 	}
1743 
1744 	if (add_i_to_a(avrule_decl_id,
1745 		       &scope_datum->decl_ids_len,
1746 		       &scope_datum->decl_ids) == -1) {
1747 		return -ENOMEM;
1748 	}
1749 
1750 	if (scope_datum->scope == SCOPE_DECL && scope == SCOPE_REQ) {
1751 		/* Need to keep the decl at the end of the list */
1752 		uint32_t len, tmp;
1753 		len = scope_datum->decl_ids_len;
1754 		if (len < 2) {
1755 			/* This should be impossible here */
1756 			return -1;
1757 		}
1758 		tmp = scope_datum->decl_ids[len-2];
1759 		scope_datum->decl_ids[len-2] = scope_datum->decl_ids[len-1];
1760 		scope_datum->decl_ids[len-1] = tmp;
1761 	}
1762 
1763 	return retval;
1764 }
1765 
type_set_or(type_set_t * dst,const type_set_t * a,const type_set_t * b)1766 static int type_set_or(type_set_t * dst, const type_set_t * a, const type_set_t * b)
1767 {
1768 	type_set_init(dst);
1769 
1770 	if (ebitmap_or(&dst->types, &a->types, &b->types)) {
1771 		return -1;
1772 	}
1773 	if (ebitmap_or(&dst->negset, &a->negset, &b->negset)) {
1774 		return -1;
1775 	}
1776 
1777 	dst->flags |= a->flags;
1778 	dst->flags |= b->flags;
1779 
1780 	return 0;
1781 }
1782 
type_set_cpy(type_set_t * dst,const type_set_t * src)1783 int type_set_cpy(type_set_t * dst, const type_set_t * src)
1784 {
1785 	type_set_init(dst);
1786 
1787 	dst->flags = src->flags;
1788 	if (ebitmap_cpy(&dst->types, &src->types))
1789 		return -1;
1790 	if (ebitmap_cpy(&dst->negset, &src->negset))
1791 		return -1;
1792 
1793 	return 0;
1794 }
1795 
type_set_or_eq(type_set_t * dst,const type_set_t * other)1796 int type_set_or_eq(type_set_t * dst, const type_set_t * other)
1797 {
1798 	int ret;
1799 	type_set_t tmp;
1800 
1801 	if (type_set_or(&tmp, dst, other))
1802 		return -1;
1803 	type_set_destroy(dst);
1804 	ret = type_set_cpy(dst, &tmp);
1805 	type_set_destroy(&tmp);
1806 
1807 	return ret;
1808 }
1809 
1810 /***********************************************************************/
1811 /* everything below is for policy reads */
1812 
1813 /* The following are read functions for module structures */
1814 
role_set_read(role_set_t * r,struct policy_file * fp)1815 static int role_set_read(role_set_t * r, struct policy_file *fp)
1816 {
1817 	uint32_t buf[1];
1818 	int rc;
1819 
1820 	if (ebitmap_read(&r->roles, fp))
1821 		return -1;
1822 	rc = next_entry(buf, fp, sizeof(uint32_t));
1823 	if (rc < 0)
1824 		return -1;
1825 	r->flags = le32_to_cpu(buf[0]);
1826 
1827 	return 0;
1828 }
1829 
type_set_read(type_set_t * t,struct policy_file * fp)1830 static int type_set_read(type_set_t * t, struct policy_file *fp)
1831 {
1832 	uint32_t buf[1];
1833 	int rc;
1834 
1835 	if (ebitmap_read(&t->types, fp))
1836 		return -1;
1837 	if (ebitmap_read(&t->negset, fp))
1838 		return -1;
1839 
1840 	rc = next_entry(buf, fp, sizeof(uint32_t));
1841 	if (rc < 0)
1842 		return -1;
1843 	t->flags = le32_to_cpu(buf[0]);
1844 
1845 	return 0;
1846 }
1847 
1848 /*
1849  * Read a MLS range structure from a policydb binary
1850  * representation file.
1851  */
mls_read_range_helper(mls_range_t * r,struct policy_file * fp)1852 static int mls_read_range_helper(mls_range_t * r, struct policy_file *fp)
1853 {
1854 	uint32_t buf[2], items;
1855 	int rc;
1856 
1857 	rc = next_entry(buf, fp, sizeof(uint32_t));
1858 	if (rc < 0)
1859 		goto out;
1860 
1861 	items = le32_to_cpu(buf[0]);
1862 	if (items > ARRAY_SIZE(buf)) {
1863 		ERR(fp->handle, "range overflow");
1864 		rc = -EINVAL;
1865 		goto out;
1866 	}
1867 	rc = next_entry(buf, fp, sizeof(uint32_t) * items);
1868 	if (rc < 0) {
1869 		ERR(fp->handle, "truncated range");
1870 		goto out;
1871 	}
1872 	r->level[0].sens = le32_to_cpu(buf[0]);
1873 	if (items > 1)
1874 		r->level[1].sens = le32_to_cpu(buf[1]);
1875 	else
1876 		r->level[1].sens = r->level[0].sens;
1877 
1878 	rc = ebitmap_read(&r->level[0].cat, fp);
1879 	if (rc) {
1880 		ERR(fp->handle, "error reading low categories");
1881 		goto out;
1882 	}
1883 	if (items > 1) {
1884 		rc = ebitmap_read(&r->level[1].cat, fp);
1885 		if (rc) {
1886 			ERR(fp->handle, "error reading high categories");
1887 			goto bad_high;
1888 		}
1889 	} else {
1890 		rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1891 		if (rc) {
1892 			ERR(fp->handle, "out of memory");
1893 			goto bad_high;
1894 		}
1895 	}
1896 
1897 	rc = 0;
1898       out:
1899 	return rc;
1900       bad_high:
1901 	ebitmap_destroy(&r->level[0].cat);
1902 	goto out;
1903 }
1904 
1905 /*
1906  * Read a semantic MLS level structure from a policydb binary
1907  * representation file.
1908  */
mls_read_semantic_level_helper(mls_semantic_level_t * l,struct policy_file * fp)1909 static int mls_read_semantic_level_helper(mls_semantic_level_t * l,
1910 					  struct policy_file *fp)
1911 {
1912 	uint32_t buf[2], ncat;
1913 	unsigned int i;
1914 	mls_semantic_cat_t *cat;
1915 	int rc;
1916 
1917 	mls_semantic_level_init(l);
1918 
1919 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
1920 	if (rc < 0) {
1921 		ERR(fp->handle, "truncated level");
1922 		goto bad;
1923 	}
1924 	l->sens = le32_to_cpu(buf[0]);
1925 
1926 	ncat = le32_to_cpu(buf[1]);
1927 	for (i = 0; i < ncat; i++) {
1928 		cat = (mls_semantic_cat_t *) malloc(sizeof(mls_semantic_cat_t));
1929 		if (!cat) {
1930 			ERR(fp->handle, "out of memory");
1931 			goto bad;
1932 		}
1933 
1934 		mls_semantic_cat_init(cat);
1935 		cat->next = l->cat;
1936 		l->cat = cat;
1937 
1938 		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
1939 		if (rc < 0) {
1940 			ERR(fp->handle, "error reading level categories");
1941 			goto bad;
1942 		}
1943 		cat->low = le32_to_cpu(buf[0]);
1944 		cat->high = le32_to_cpu(buf[1]);
1945 	}
1946 
1947 	return 0;
1948 
1949       bad:
1950 	return -EINVAL;
1951 }
1952 
1953 /*
1954  * Read a semantic MLS range structure from a policydb binary
1955  * representation file.
1956  */
mls_read_semantic_range_helper(mls_semantic_range_t * r,struct policy_file * fp)1957 static int mls_read_semantic_range_helper(mls_semantic_range_t * r,
1958 					  struct policy_file *fp)
1959 {
1960 	int rc;
1961 
1962 	rc = mls_read_semantic_level_helper(&r->level[0], fp);
1963 	if (rc)
1964 		return rc;
1965 
1966 	rc = mls_read_semantic_level_helper(&r->level[1], fp);
1967 
1968 	return rc;
1969 }
1970 
mls_level_to_semantic(mls_level_t * l,mls_semantic_level_t * sl)1971 static int mls_level_to_semantic(mls_level_t * l, mls_semantic_level_t * sl)
1972 {
1973 	unsigned int i;
1974 	ebitmap_node_t *cnode;
1975 	mls_semantic_cat_t *open_cat = NULL;
1976 
1977 	mls_semantic_level_init(sl);
1978 	sl->sens = l->sens;
1979 	ebitmap_for_each_bit(&l->cat, cnode, i) {
1980 		if (ebitmap_node_get_bit(cnode, i)) {
1981 			if (open_cat)
1982 				continue;
1983 			open_cat = (mls_semantic_cat_t *)
1984 			    malloc(sizeof(mls_semantic_cat_t));
1985 			if (!open_cat)
1986 				return -1;
1987 
1988 			mls_semantic_cat_init(open_cat);
1989 			open_cat->low = i + 1;
1990 			open_cat->next = sl->cat;
1991 			sl->cat = open_cat;
1992 		} else {
1993 			if (!open_cat)
1994 				continue;
1995 			open_cat->high = i;
1996 			open_cat = NULL;
1997 		}
1998 	}
1999 	if (open_cat)
2000 		open_cat->high = i;
2001 
2002 	return 0;
2003 }
2004 
mls_range_to_semantic(mls_range_t * r,mls_semantic_range_t * sr)2005 static int mls_range_to_semantic(mls_range_t * r, mls_semantic_range_t * sr)
2006 {
2007 	if (mls_level_to_semantic(&r->level[0], &sr->level[0]))
2008 		return -1;
2009 
2010 	if (mls_level_to_semantic(&r->level[1], &sr->level[1]))
2011 		return -1;
2012 
2013 	return 0;
2014 }
2015 
2016 /*
2017  * Read and validate a security context structure
2018  * from a policydb binary representation file.
2019  */
context_read_and_validate(context_struct_t * c,policydb_t * p,struct policy_file * fp)2020 static int context_read_and_validate(context_struct_t * c,
2021 				     policydb_t * p, struct policy_file *fp)
2022 {
2023 	uint32_t buf[3];
2024 	int rc;
2025 
2026 	rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
2027 	if (rc < 0) {
2028 		ERR(fp->handle, "context truncated");
2029 		return -1;
2030 	}
2031 	c->user = le32_to_cpu(buf[0]);
2032 	c->role = le32_to_cpu(buf[1]);
2033 	c->type = le32_to_cpu(buf[2]);
2034 	if ((p->policy_type == POLICY_KERN
2035 	     && p->policyvers >= POLICYDB_VERSION_MLS)
2036 	    || (p->policy_type == POLICY_BASE
2037 		&& p->policyvers >= MOD_POLICYDB_VERSION_MLS)) {
2038 		if (mls_read_range_helper(&c->range, fp)) {
2039 			ERR(fp->handle, "error reading MLS range "
2040 			    "of context");
2041 			return -1;
2042 		}
2043 	}
2044 
2045 	if (!policydb_context_isvalid(p, c)) {
2046 		ERR(fp->handle, "invalid security context");
2047 		context_destroy(c);
2048 		return -1;
2049 	}
2050 	return 0;
2051 }
2052 
2053 /*
2054  * The following *_read functions are used to
2055  * read the symbol data from a policy database
2056  * binary representation file.
2057  */
2058 
perm_read(policydb_t * p,hashtab_t h,struct policy_file * fp,uint32_t nprim)2059 static int perm_read(policydb_t * p
2060 		     __attribute__ ((unused)), hashtab_t h,
2061 		     struct policy_file *fp, uint32_t nprim)
2062 {
2063 	char *key = 0;
2064 	perm_datum_t *perdatum;
2065 	uint32_t buf[2];
2066 	size_t len;
2067 	int rc;
2068 
2069 	perdatum = calloc(1, sizeof(perm_datum_t));
2070 	if (!perdatum)
2071 		return -1;
2072 
2073 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
2074 	if (rc < 0)
2075 		goto bad;
2076 
2077 	len = le32_to_cpu(buf[0]);
2078 	if(str_read(&key, fp, len))
2079 		goto bad;
2080 
2081 	perdatum->s.value = le32_to_cpu(buf[1]);
2082 	if (!value_isvalid(perdatum->s.value, nprim))
2083 		goto bad;
2084 
2085 	if (hashtab_insert(h, key, perdatum))
2086 		goto bad;
2087 
2088 	return 0;
2089 
2090       bad:
2091 	perm_destroy(key, perdatum, NULL);
2092 	return -1;
2093 }
2094 
common_read(policydb_t * p,hashtab_t h,struct policy_file * fp)2095 static int common_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
2096 {
2097 	char *key = 0;
2098 	common_datum_t *comdatum;
2099 	uint32_t buf[4];
2100 	size_t len, nel;
2101 	unsigned int i;
2102 	int rc;
2103 
2104 	comdatum = calloc(1, sizeof(common_datum_t));
2105 	if (!comdatum)
2106 		return -1;
2107 
2108 	rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
2109 	if (rc < 0)
2110 		goto bad;
2111 
2112 	len = le32_to_cpu(buf[0]);
2113 	rc = str_read(&key, fp, len);
2114 	if (rc < 0)
2115 		goto bad;
2116 
2117 	comdatum->s.value = le32_to_cpu(buf[1]);
2118 
2119 	if (symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE))
2120 		goto bad;
2121 	comdatum->permissions.nprim = le32_to_cpu(buf[2]);
2122 	if (comdatum->permissions.nprim > PERM_SYMTAB_SIZE)
2123 		goto bad;
2124 	nel = le32_to_cpu(buf[3]);
2125 
2126 	for (i = 0; i < nel; i++) {
2127 		if (perm_read(p, comdatum->permissions.table, fp, comdatum->permissions.nprim))
2128 			goto bad;
2129 	}
2130 
2131 	if (hashtab_insert(h, key, comdatum))
2132 		goto bad;
2133 
2134 	return 0;
2135 
2136       bad:
2137 	common_destroy(key, comdatum, NULL);
2138 	return -1;
2139 }
2140 
read_cons_helper(policydb_t * p,constraint_node_t ** nodep,unsigned int ncons,int allowxtarget,struct policy_file * fp)2141 static int read_cons_helper(policydb_t * p, constraint_node_t ** nodep,
2142 			    unsigned int ncons,
2143 			    int allowxtarget, struct policy_file *fp)
2144 {
2145 	constraint_node_t *c, *lc;
2146 	constraint_expr_t *e, *le;
2147 	uint32_t buf[3];
2148 	size_t nexpr;
2149 	unsigned int i, j;
2150 	int rc, depth;
2151 
2152 	lc = NULL;
2153 	for (i = 0; i < ncons; i++) {
2154 		c = calloc(1, sizeof(constraint_node_t));
2155 		if (!c)
2156 			return -1;
2157 
2158 		if (lc)
2159 			lc->next = c;
2160 		else
2161 			*nodep = c;
2162 
2163 		rc = next_entry(buf, fp, (sizeof(uint32_t) * 2));
2164 		if (rc < 0)
2165 			return -1;
2166 		c->permissions = le32_to_cpu(buf[0]);
2167 		nexpr = le32_to_cpu(buf[1]);
2168 		le = NULL;
2169 		depth = -1;
2170 		for (j = 0; j < nexpr; j++) {
2171 			e = malloc(sizeof(constraint_expr_t));
2172 			if (!e)
2173 				return -1;
2174 			if (constraint_expr_init(e) == -1) {
2175 				free(e);
2176 				return -1;
2177 			}
2178 			if (le) {
2179 				le->next = e;
2180 			} else {
2181 				c->expr = e;
2182 			}
2183 
2184 			rc = next_entry(buf, fp, (sizeof(uint32_t) * 3));
2185 			if (rc < 0)
2186 				return -1;
2187 			e->expr_type = le32_to_cpu(buf[0]);
2188 			e->attr = le32_to_cpu(buf[1]);
2189 			e->op = le32_to_cpu(buf[2]);
2190 
2191 			switch (e->expr_type) {
2192 			case CEXPR_NOT:
2193 				if (depth < 0)
2194 					return -1;
2195 				break;
2196 			case CEXPR_AND:
2197 			case CEXPR_OR:
2198 				if (depth < 1)
2199 					return -1;
2200 				depth--;
2201 				break;
2202 			case CEXPR_ATTR:
2203 				if (depth == (CEXPR_MAXDEPTH - 1))
2204 					return -1;
2205 				depth++;
2206 				break;
2207 			case CEXPR_NAMES:
2208 				if (!allowxtarget && (e->attr & CEXPR_XTARGET))
2209 					return -1;
2210 				if (depth == (CEXPR_MAXDEPTH - 1))
2211 					return -1;
2212 				depth++;
2213 				if (ebitmap_read(&e->names, fp))
2214 					return -1;
2215 				if (p->policy_type != POLICY_KERN &&
2216 				    type_set_read(e->type_names, fp))
2217 					return -1;
2218 				else if (p->policy_type == POLICY_KERN &&
2219 					 p->policyvers >= POLICYDB_VERSION_CONSTRAINT_NAMES &&
2220 					 type_set_read(e->type_names, fp))
2221 					return -1;
2222 				break;
2223 			default:
2224 				return -1;
2225 			}
2226 			le = e;
2227 		}
2228 		if (depth != 0)
2229 			return -1;
2230 		lc = c;
2231 	}
2232 
2233 	return 0;
2234 }
2235 
class_read(policydb_t * p,hashtab_t h,struct policy_file * fp)2236 static int class_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
2237 {
2238 	char *key = 0;
2239 	class_datum_t *cladatum;
2240 	uint32_t buf[6];
2241 	size_t len, len2, ncons, nel;
2242 	unsigned int i;
2243 	int rc;
2244 
2245 	cladatum = (class_datum_t *) calloc(1, sizeof(class_datum_t));
2246 	if (!cladatum)
2247 		return -1;
2248 
2249 	rc = next_entry(buf, fp, sizeof(uint32_t) * 6);
2250 	if (rc < 0)
2251 		goto bad;
2252 
2253 	len = le32_to_cpu(buf[0]);
2254 	rc = str_read(&key, fp, len);
2255 	if (rc < 0)
2256 		goto bad;
2257 
2258 	len2 = le32_to_cpu(buf[1]);
2259 	cladatum->s.value = le32_to_cpu(buf[2]);
2260 	if (cladatum->s.value > UINT16_MAX)
2261 		goto bad;
2262 
2263 	if (symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE))
2264 		goto bad;
2265 	cladatum->permissions.nprim = le32_to_cpu(buf[3]);
2266 	if (cladatum->permissions.nprim > PERM_SYMTAB_SIZE)
2267 		goto bad;
2268 	nel = le32_to_cpu(buf[4]);
2269 
2270 	ncons = le32_to_cpu(buf[5]);
2271 
2272 	if (len2) {
2273 		rc = str_read(&cladatum->comkey, fp, len2);
2274 		if (rc < 0)
2275 			goto bad;
2276 
2277 		cladatum->comdatum = hashtab_search(p->p_commons.table,
2278 						    cladatum->comkey);
2279 		if (!cladatum->comdatum) {
2280 			ERR(fp->handle, "unknown common %s", cladatum->comkey);
2281 			goto bad;
2282 		}
2283 	}
2284 	for (i = 0; i < nel; i++) {
2285 		if (perm_read(p, cladatum->permissions.table, fp, cladatum->permissions.nprim))
2286 			goto bad;
2287 	}
2288 
2289 	if (read_cons_helper(p, &cladatum->constraints, ncons, 0, fp))
2290 		goto bad;
2291 
2292 	if ((p->policy_type == POLICY_KERN
2293 	     && p->policyvers >= POLICYDB_VERSION_VALIDATETRANS)
2294 	    || (p->policy_type == POLICY_BASE
2295 		&& p->policyvers >= MOD_POLICYDB_VERSION_VALIDATETRANS)) {
2296 		/* grab the validatetrans rules */
2297 		rc = next_entry(buf, fp, sizeof(uint32_t));
2298 		if (rc < 0)
2299 			goto bad;
2300 		ncons = le32_to_cpu(buf[0]);
2301 		if (read_cons_helper(p, &cladatum->validatetrans, ncons, 1, fp))
2302 			goto bad;
2303 	}
2304 
2305 	if ((p->policy_type == POLICY_KERN &&
2306 	     p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) ||
2307 	    (p->policy_type == POLICY_BASE &&
2308 	     p->policyvers >= MOD_POLICYDB_VERSION_NEW_OBJECT_DEFAULTS)) {
2309 		rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
2310 		if (rc < 0)
2311 			goto bad;
2312 		cladatum->default_user = le32_to_cpu(buf[0]);
2313 		cladatum->default_role = le32_to_cpu(buf[1]);
2314 		cladatum->default_range = le32_to_cpu(buf[2]);
2315 	}
2316 
2317 	if ((p->policy_type == POLICY_KERN &&
2318 	     p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) ||
2319 	    (p->policy_type == POLICY_BASE &&
2320 	     p->policyvers >= MOD_POLICYDB_VERSION_DEFAULT_TYPE)) {
2321 		rc = next_entry(buf, fp, sizeof(uint32_t));
2322 		if (rc < 0)
2323 			goto bad;
2324 		cladatum->default_type = le32_to_cpu(buf[0]);
2325 	}
2326 
2327 	if (hashtab_insert(h, key, cladatum))
2328 		goto bad;
2329 
2330 	return 0;
2331 
2332       bad:
2333 	class_destroy(key, cladatum, NULL);
2334 	return -1;
2335 }
2336 
role_read(policydb_t * p,hashtab_t h,struct policy_file * fp)2337 static int role_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
2338 {
2339 	char *key = 0;
2340 	role_datum_t *role;
2341 	uint32_t buf[3];
2342 	size_t len;
2343 	int rc, to_read = 2;
2344 
2345 	role = calloc(1, sizeof(role_datum_t));
2346 	if (!role)
2347 		return -1;
2348 
2349 	if (policydb_has_boundary_feature(p))
2350 		to_read = 3;
2351 
2352 	rc = next_entry(buf, fp, sizeof(uint32_t) * to_read);
2353 	if (rc < 0)
2354 		goto bad;
2355 
2356 	len = le32_to_cpu(buf[0]);
2357 	rc = str_read(&key, fp, len);
2358 	if (rc < 0)
2359 		goto bad;
2360 
2361 	role->s.value = le32_to_cpu(buf[1]);
2362 	if (policydb_has_boundary_feature(p))
2363 		role->bounds = le32_to_cpu(buf[2]);
2364 
2365 	if (ebitmap_read(&role->dominates, fp))
2366 		goto bad;
2367 
2368 	if (p->policy_type == POLICY_KERN) {
2369 		if (ebitmap_read(&role->types.types, fp))
2370 			goto bad;
2371 	} else {
2372 		if (type_set_read(&role->types, fp))
2373 			goto bad;
2374 	}
2375 
2376 	if (p->policy_type != POLICY_KERN &&
2377 	    p->policyvers >= MOD_POLICYDB_VERSION_ROLEATTRIB) {
2378 		rc = next_entry(buf, fp, sizeof(uint32_t));
2379 		if (rc < 0)
2380 			goto bad;
2381 
2382 		role->flavor = le32_to_cpu(buf[0]);
2383 
2384 		if (ebitmap_read(&role->roles, fp))
2385 			goto bad;
2386 	}
2387 
2388 	if (strcmp(key, OBJECT_R) == 0) {
2389 		if (role->s.value != OBJECT_R_VAL) {
2390 			ERR(fp->handle, "role %s has wrong value %d",
2391 			    OBJECT_R, role->s.value);
2392 			role_destroy(key, role, NULL);
2393 			return -1;
2394 		}
2395 		role_destroy(key, role, NULL);
2396 		return 0;
2397 	}
2398 
2399 	if (hashtab_insert(h, key, role))
2400 		goto bad;
2401 
2402 	return 0;
2403 
2404       bad:
2405 	role_destroy(key, role, NULL);
2406 	return -1;
2407 }
2408 
type_read(policydb_t * p,hashtab_t h,struct policy_file * fp)2409 static int type_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
2410 {
2411 	char *key = 0;
2412 	type_datum_t *typdatum;
2413 	uint32_t buf[5];
2414 	size_t len;
2415 	int rc, to_read;
2416 	int pos = 0;
2417 
2418 	typdatum = calloc(1, sizeof(type_datum_t));
2419 	if (!typdatum)
2420 		return -1;
2421 
2422 	if (policydb_has_boundary_feature(p)) {
2423 		if (p->policy_type != POLICY_KERN
2424 		    && p->policyvers >= MOD_POLICYDB_VERSION_BOUNDARY_ALIAS)
2425 			to_read = 5;
2426 		else
2427 			to_read = 4;
2428 	}
2429 	else if (p->policy_type == POLICY_KERN)
2430 		to_read = 3;
2431 	else if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
2432 		to_read = 5;
2433 	else
2434 		to_read = 4;
2435 
2436 	rc = next_entry(buf, fp, sizeof(uint32_t) * to_read);
2437 	if (rc < 0)
2438 		goto bad;
2439 
2440 	len = le32_to_cpu(buf[pos]);
2441 	typdatum->s.value = le32_to_cpu(buf[++pos]);
2442 	if (policydb_has_boundary_feature(p)) {
2443 		uint32_t properties;
2444 
2445 		if (p->policy_type != POLICY_KERN
2446 		    && p->policyvers >= MOD_POLICYDB_VERSION_BOUNDARY_ALIAS) {
2447 			typdatum->primary = le32_to_cpu(buf[++pos]);
2448 			properties = le32_to_cpu(buf[++pos]);
2449 		}
2450 		else {
2451 			properties = le32_to_cpu(buf[++pos]);
2452 
2453 			if (properties & TYPEDATUM_PROPERTY_PRIMARY)
2454 				typdatum->primary = 1;
2455 		}
2456 
2457 		if (properties & TYPEDATUM_PROPERTY_ATTRIBUTE)
2458 			typdatum->flavor = TYPE_ATTRIB;
2459 		if (properties & TYPEDATUM_PROPERTY_ALIAS
2460 		    && p->policy_type != POLICY_KERN)
2461 			typdatum->flavor = TYPE_ALIAS;
2462 		if (properties & TYPEDATUM_PROPERTY_PERMISSIVE
2463 		    && p->policy_type != POLICY_KERN)
2464 			typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
2465 
2466 		typdatum->bounds = le32_to_cpu(buf[++pos]);
2467 	} else {
2468 		typdatum->primary = le32_to_cpu(buf[++pos]);
2469 		if (p->policy_type != POLICY_KERN) {
2470 			typdatum->flavor = le32_to_cpu(buf[++pos]);
2471 			if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
2472 				typdatum->flags = le32_to_cpu(buf[++pos]);
2473 		}
2474 	}
2475 
2476 	if (p->policy_type != POLICY_KERN) {
2477 		if (ebitmap_read(&typdatum->types, fp))
2478 			goto bad;
2479 	}
2480 
2481 	rc = str_read(&key, fp, len);
2482 	if (rc < 0)
2483 		goto bad;
2484 
2485 	if (hashtab_insert(h, key, typdatum))
2486 		goto bad;
2487 
2488 	return 0;
2489 
2490       bad:
2491 	type_destroy(key, typdatum, NULL);
2492 	return -1;
2493 }
2494 
role_trans_read(policydb_t * p,struct policy_file * fp)2495 static int role_trans_read(policydb_t *p, struct policy_file *fp)
2496 {
2497 	role_trans_t **t = &p->role_tr;
2498 	unsigned int i;
2499 	uint32_t buf[3], nel;
2500 	role_trans_t *tr, *ltr;
2501 	int rc;
2502 	int new_roletr = (p->policy_type == POLICY_KERN &&
2503 			  p->policyvers >= POLICYDB_VERSION_ROLETRANS);
2504 
2505 	rc = next_entry(buf, fp, sizeof(uint32_t));
2506 	if (rc < 0)
2507 		return -1;
2508 	nel = le32_to_cpu(buf[0]);
2509 	ltr = NULL;
2510 	for (i = 0; i < nel; i++) {
2511 		tr = calloc(1, sizeof(struct role_trans));
2512 		if (!tr) {
2513 			return -1;
2514 		}
2515 		if (ltr) {
2516 			ltr->next = tr;
2517 		} else {
2518 			*t = tr;
2519 		}
2520 		rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
2521 		if (rc < 0)
2522 			return -1;
2523 		tr->role = le32_to_cpu(buf[0]);
2524 		tr->type = le32_to_cpu(buf[1]);
2525 		tr->new_role = le32_to_cpu(buf[2]);
2526 		if (new_roletr) {
2527 			rc = next_entry(buf, fp, sizeof(uint32_t));
2528 			if (rc < 0)
2529 				return -1;
2530 			tr->tclass = le32_to_cpu(buf[0]);
2531 		} else
2532 			tr->tclass = p->process_class;
2533 		ltr = tr;
2534 	}
2535 	return 0;
2536 }
2537 
role_allow_read(role_allow_t ** r,struct policy_file * fp)2538 static int role_allow_read(role_allow_t ** r, struct policy_file *fp)
2539 {
2540 	unsigned int i;
2541 	uint32_t buf[2], nel;
2542 	role_allow_t *ra, *lra;
2543 	int rc;
2544 
2545 	rc = next_entry(buf, fp, sizeof(uint32_t));
2546 	if (rc < 0)
2547 		return -1;
2548 	nel = le32_to_cpu(buf[0]);
2549 	lra = NULL;
2550 	for (i = 0; i < nel; i++) {
2551 		ra = calloc(1, sizeof(struct role_allow));
2552 		if (!ra) {
2553 			return -1;
2554 		}
2555 		if (lra) {
2556 			lra->next = ra;
2557 		} else {
2558 			*r = ra;
2559 		}
2560 		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
2561 		if (rc < 0)
2562 			return -1;
2563 		ra->role = le32_to_cpu(buf[0]);
2564 		ra->new_role = le32_to_cpu(buf[1]);
2565 		lra = ra;
2566 	}
2567 	return 0;
2568 }
2569 
policydb_filetrans_insert(policydb_t * p,uint32_t stype,uint32_t ttype,uint32_t tclass,const char * name,char ** name_alloc,uint32_t otype,uint32_t * present_otype)2570 int policydb_filetrans_insert(policydb_t *p, uint32_t stype, uint32_t ttype,
2571 			      uint32_t tclass, const char *name,
2572 			      char **name_alloc, uint32_t otype,
2573 			      uint32_t *present_otype)
2574 {
2575 	filename_trans_key_t *ft, key;
2576 	filename_trans_datum_t *datum, *last;
2577 
2578 	key.ttype = ttype;
2579 	key.tclass = tclass;
2580 	key.name = (char *)name;
2581 
2582 	last = NULL;
2583 	datum = hashtab_search(p->filename_trans, (hashtab_key_t)&key);
2584 	while (datum) {
2585 		if (ebitmap_get_bit(&datum->stypes, stype - 1)) {
2586 			if (present_otype)
2587 				*present_otype = datum->otype;
2588 			return SEPOL_EEXIST;
2589 		}
2590 		if (datum->otype == otype)
2591 			break;
2592 		last = datum;
2593 		datum = datum->next;
2594 	}
2595 	if (!datum) {
2596 		datum = malloc(sizeof(*datum));
2597 		if (!datum)
2598 			return SEPOL_ENOMEM;
2599 
2600 		ebitmap_init(&datum->stypes);
2601 		datum->otype = otype;
2602 		datum->next = NULL;
2603 
2604 		if (last) {
2605 			last->next = datum;
2606 		} else {
2607 			char *name_dup;
2608 
2609 			if (name_alloc) {
2610 				name_dup = *name_alloc;
2611 				*name_alloc = NULL;
2612 			} else {
2613 				name_dup = strdup(name);
2614 				if (!name_dup) {
2615 					free(datum);
2616 					return SEPOL_ENOMEM;
2617 				}
2618 			}
2619 
2620 			ft = malloc(sizeof(*ft));
2621 			if (!ft) {
2622 				free(name_dup);
2623 				free(datum);
2624 				return SEPOL_ENOMEM;
2625 			}
2626 
2627 			ft->ttype = ttype;
2628 			ft->tclass = tclass;
2629 			ft->name = name_dup;
2630 
2631 			if (hashtab_insert(p->filename_trans, (hashtab_key_t)ft,
2632 					   (hashtab_datum_t)datum)) {
2633 				free(name_dup);
2634 				free(datum);
2635 				free(ft);
2636 				return SEPOL_ENOMEM;
2637 			}
2638 		}
2639 	}
2640 
2641 	p->filename_trans_count++;
2642 	return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
2643 }
2644 
filename_trans_read_one_compat(policydb_t * p,struct policy_file * fp)2645 static int filename_trans_read_one_compat(policydb_t *p, struct policy_file *fp)
2646 {
2647 	uint32_t buf[4], len, stype, ttype, tclass, otype;
2648 	char *name = NULL;
2649 	int rc;
2650 
2651 	rc = next_entry(buf, fp, sizeof(uint32_t));
2652 	if (rc < 0)
2653 		return -1;
2654 	len = le32_to_cpu(buf[0]);
2655 
2656 	rc = str_read(&name, fp, len);
2657 	if (rc < 0)
2658 		goto err;
2659 
2660 	rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
2661 	if (rc < 0)
2662 		goto err;
2663 
2664 	stype = le32_to_cpu(buf[0]);
2665 	if (stype == 0)
2666 		goto err;
2667 
2668 	ttype  = le32_to_cpu(buf[1]);
2669 	tclass = le32_to_cpu(buf[2]);
2670 	otype  = le32_to_cpu(buf[3]);
2671 
2672 	rc = policydb_filetrans_insert(p, stype, ttype, tclass, name, &name,
2673 				       otype, NULL);
2674 	if (rc) {
2675 		if (rc != SEPOL_EEXIST)
2676 			goto err;
2677 		/*
2678 		 * Some old policies were wrongly generated with
2679 		 * duplicate filename transition rules.  For backward
2680 		 * compatibility, do not reject such policies, just
2681 		 * ignore the duplicate.
2682 		 */
2683 	}
2684 	free(name);
2685 	return 0;
2686 err:
2687 	free(name);
2688 	return -1;
2689 }
2690 
filename_trans_check_datum(filename_trans_datum_t * datum)2691 static int filename_trans_check_datum(filename_trans_datum_t *datum)
2692 {
2693 	ebitmap_t stypes, otypes;
2694 	int rc = -1;
2695 
2696 	ebitmap_init(&stypes);
2697 	ebitmap_init(&otypes);
2698 
2699 	while (datum) {
2700 		if (ebitmap_get_bit(&otypes, datum->otype))
2701 			goto out;
2702 
2703 		if (ebitmap_set_bit(&otypes, datum->otype, 1))
2704 			goto out;
2705 
2706 		if (ebitmap_match_any(&stypes, &datum->stypes))
2707 			goto out;
2708 
2709 		if (ebitmap_union(&stypes, &datum->stypes))
2710 			goto out;
2711 
2712 		datum = datum->next;
2713 	}
2714 	rc = 0;
2715 out:
2716 	ebitmap_destroy(&stypes);
2717 	ebitmap_destroy(&otypes);
2718 	return rc;
2719 }
2720 
filename_trans_read_one(policydb_t * p,struct policy_file * fp)2721 static int filename_trans_read_one(policydb_t *p, struct policy_file *fp)
2722 {
2723 	filename_trans_key_t *ft = NULL;
2724 	filename_trans_datum_t **dst, *datum, *first = NULL;
2725 	unsigned int i;
2726 	uint32_t buf[3], len, ttype, tclass, ndatum;
2727 	char *name = NULL;
2728 	int rc;
2729 
2730 	rc = next_entry(buf, fp, sizeof(uint32_t));
2731 	if (rc < 0)
2732 		return -1;
2733 	len = le32_to_cpu(buf[0]);
2734 
2735 	rc = str_read(&name, fp, len);
2736 	if (rc < 0)
2737 		goto err;
2738 
2739 	rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
2740 	if (rc < 0)
2741 		goto err;
2742 
2743 	ttype = le32_to_cpu(buf[0]);
2744 	tclass = le32_to_cpu(buf[1]);
2745 	ndatum = le32_to_cpu(buf[2]);
2746 	if (ndatum == 0)
2747 		goto err;
2748 
2749 	dst = &first;
2750 	for (i = 0; i < ndatum; i++) {
2751 		datum = malloc(sizeof(*datum));
2752 		if (!datum)
2753 			goto err;
2754 
2755 		datum->next = NULL;
2756 		*dst = datum;
2757 
2758 		/* ebitmap_read() will at least init the bitmap */
2759 		rc = ebitmap_read(&datum->stypes, fp);
2760 		if (rc < 0)
2761 			goto err;
2762 
2763 		rc = next_entry(buf, fp, sizeof(uint32_t));
2764 		if (rc < 0)
2765 			goto err;
2766 
2767 		datum->otype = le32_to_cpu(buf[0]);
2768 
2769 		p->filename_trans_count += ebitmap_cardinality(&datum->stypes);
2770 
2771 		dst = &datum->next;
2772 	}
2773 
2774 	if (ndatum > 1 && filename_trans_check_datum(first))
2775 		goto err;
2776 
2777 	ft = malloc(sizeof(*ft));
2778 	if (!ft)
2779 		goto err;
2780 
2781 	ft->ttype = ttype;
2782 	ft->tclass = tclass;
2783 	ft->name = name;
2784 
2785 	rc = hashtab_insert(p->filename_trans, (hashtab_key_t)ft,
2786 			    (hashtab_datum_t)first);
2787 	if (rc)
2788 		goto err;
2789 
2790 	return 0;
2791 err:
2792 	free(ft);
2793 	free(name);
2794 	while (first) {
2795 		datum = first;
2796 		first = first->next;
2797 
2798 		ebitmap_destroy(&datum->stypes);
2799 		free(datum);
2800 	}
2801 	return -1;
2802 }
2803 
filename_trans_read(policydb_t * p,struct policy_file * fp)2804 static int filename_trans_read(policydb_t *p, struct policy_file *fp)
2805 {
2806 	unsigned int i;
2807 	uint32_t buf[1], nel;
2808 	int rc;
2809 
2810 	rc = next_entry(buf, fp, sizeof(uint32_t));
2811 	if (rc < 0)
2812 		return -1;
2813 	nel = le32_to_cpu(buf[0]);
2814 
2815 	if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
2816 		for (i = 0; i < nel; i++) {
2817 			rc = filename_trans_read_one_compat(p, fp);
2818 			if (rc < 0)
2819 				return -1;
2820 		}
2821 	} else {
2822 		for (i = 0; i < nel; i++) {
2823 			rc = filename_trans_read_one(p, fp);
2824 			if (rc < 0)
2825 				return -1;
2826 		}
2827 	}
2828 	return 0;
2829 }
2830 
ocontext_read_xen(const struct policydb_compat_info * info,policydb_t * p,struct policy_file * fp)2831 static int ocontext_read_xen(const struct policydb_compat_info *info,
2832 	policydb_t *p, struct policy_file *fp)
2833 {
2834 	unsigned int i, j;
2835 	size_t nel, len;
2836 	ocontext_t *l, *c;
2837 	uint32_t buf[8];
2838 	int rc;
2839 
2840 	for (i = 0; i < info->ocon_num; i++) {
2841 		rc = next_entry(buf, fp, sizeof(uint32_t));
2842 		if (rc < 0)
2843 			return -1;
2844 		nel = le32_to_cpu(buf[0]);
2845 		l = NULL;
2846 		for (j = 0; j < nel; j++) {
2847 			c = calloc(1, sizeof(ocontext_t));
2848 			if (!c)
2849 				return -1;
2850 			if (l)
2851 				l->next = c;
2852 			else
2853 				p->ocontexts[i] = c;
2854 			l = c;
2855 			switch (i) {
2856 			case OCON_XEN_ISID:
2857 				rc = next_entry(buf, fp, sizeof(uint32_t));
2858 				if (rc < 0)
2859 					return -1;
2860 				c->sid[0] = le32_to_cpu(buf[0]);
2861 				if (is_saturated(c->sid[0]))
2862 					return -1;
2863 				if (context_read_and_validate
2864 				    (&c->context[0], p, fp))
2865 					return -1;
2866 				break;
2867 			case OCON_XEN_PIRQ:
2868 				rc = next_entry(buf, fp, sizeof(uint32_t));
2869 				if (rc < 0)
2870 					return -1;
2871 				c->u.pirq = le32_to_cpu(buf[0]);
2872 				if (context_read_and_validate
2873 				    (&c->context[0], p, fp))
2874 					return -1;
2875 				break;
2876 			case OCON_XEN_IOPORT:
2877 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
2878 				if (rc < 0)
2879 					return -1;
2880 				c->u.ioport.low_ioport = le32_to_cpu(buf[0]);
2881 				c->u.ioport.high_ioport = le32_to_cpu(buf[1]);
2882 				if (context_read_and_validate
2883 				    (&c->context[0], p, fp))
2884 					return -1;
2885 				break;
2886 			case OCON_XEN_IOMEM:
2887 				if (p->policyvers >= POLICYDB_VERSION_XEN_DEVICETREE) {
2888 					uint64_t b64[2];
2889 					rc = next_entry(b64, fp, sizeof(uint64_t) * 2);
2890 					if (rc < 0)
2891 						return -1;
2892 					c->u.iomem.low_iomem = le64_to_cpu(b64[0]);
2893 					c->u.iomem.high_iomem = le64_to_cpu(b64[1]);
2894 				} else {
2895 					rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
2896 					if (rc < 0)
2897 						return -1;
2898 					c->u.iomem.low_iomem = le32_to_cpu(buf[0]);
2899 					c->u.iomem.high_iomem = le32_to_cpu(buf[1]);
2900 				}
2901 				if (context_read_and_validate
2902 				    (&c->context[0], p, fp))
2903 					return -1;
2904 				break;
2905 			case OCON_XEN_PCIDEVICE:
2906 				rc = next_entry(buf, fp, sizeof(uint32_t));
2907 				if (rc < 0)
2908 					return -1;
2909 				c->u.device = le32_to_cpu(buf[0]);
2910 				if (context_read_and_validate
2911 				    (&c->context[0], p, fp))
2912 					return -1;
2913 				break;
2914 			case OCON_XEN_DEVICETREE:
2915 				rc = next_entry(buf, fp, sizeof(uint32_t));
2916 				if (rc < 0)
2917 					return -1;
2918 				len = le32_to_cpu(buf[0]);
2919 				rc = str_read(&c->u.name, fp, len);
2920 				if (rc < 0)
2921 					return -1;
2922 				if (context_read_and_validate
2923 				    (&c->context[0], p, fp))
2924 					return -1;
2925 				break;
2926 			default:
2927 				/* should never get here */
2928 				ERR(fp->handle, "Unknown Xen ocontext");
2929 				return -1;
2930 			}
2931 		}
2932 	}
2933 	return 0;
2934 }
ocontext_read_selinux(const struct policydb_compat_info * info,policydb_t * p,struct policy_file * fp)2935 static int ocontext_read_selinux(const struct policydb_compat_info *info,
2936 			 policydb_t * p, struct policy_file *fp)
2937 {
2938 	unsigned int i, j;
2939 	size_t nel, len;
2940 	ocontext_t *l, *c;
2941 	uint32_t buf[8];
2942 	int rc;
2943 
2944 	for (i = 0; i < info->ocon_num; i++) {
2945 		rc = next_entry(buf, fp, sizeof(uint32_t));
2946 		if (rc < 0)
2947 			return -1;
2948 		nel = le32_to_cpu(buf[0]);
2949 		l = NULL;
2950 		for (j = 0; j < nel; j++) {
2951 			c = calloc(1, sizeof(ocontext_t));
2952 			if (!c) {
2953 				return -1;
2954 			}
2955 			if (l) {
2956 				l->next = c;
2957 			} else {
2958 				p->ocontexts[i] = c;
2959 			}
2960 			l = c;
2961 			switch (i) {
2962 			case OCON_ISID:
2963 				rc = next_entry(buf, fp, sizeof(uint32_t));
2964 				if (rc < 0)
2965 					return -1;
2966 				c->sid[0] = le32_to_cpu(buf[0]);
2967 				if (is_saturated(c->sid[0]))
2968 					return -1;
2969 				if (context_read_and_validate
2970 				    (&c->context[0], p, fp))
2971 					return -1;
2972 				break;
2973 			case OCON_FS:
2974 			case OCON_NETIF:
2975 				rc = next_entry(buf, fp, sizeof(uint32_t));
2976 				if (rc < 0)
2977 					return -1;
2978 				len = le32_to_cpu(buf[0]);
2979 				if (len > 63)
2980 					return -1;
2981 
2982 				rc = str_read(&c->u.name, fp, len);
2983 				if (rc < 0)
2984 					return -1;
2985 
2986 				if (context_read_and_validate
2987 				    (&c->context[0], p, fp))
2988 					return -1;
2989 				if (context_read_and_validate
2990 				    (&c->context[1], p, fp))
2991 					return -1;
2992 				break;
2993 			case OCON_IBPKEY: {
2994 				uint32_t pkey_lo, pkey_hi;
2995 
2996 				rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
2997 				if (rc < 0)
2998 					return -1;
2999 
3000 				pkey_lo = le32_to_cpu(buf[2]);
3001 				pkey_hi = le32_to_cpu(buf[3]);
3002 
3003 				if (pkey_lo > UINT16_MAX || pkey_hi > UINT16_MAX)
3004 					return -1;
3005 
3006 				c->u.ibpkey.low_pkey  = pkey_lo;
3007 				c->u.ibpkey.high_pkey = pkey_hi;
3008 
3009 				/* we want c->u.ibpkey.subnet_prefix in network
3010 				 * (big-endian) order, just memcpy it */
3011 				memcpy(&c->u.ibpkey.subnet_prefix, buf,
3012 				       sizeof(c->u.ibpkey.subnet_prefix));
3013 
3014 				if (context_read_and_validate
3015 				    (&c->context[0], p, fp))
3016 					return -1;
3017 				break;
3018 			}
3019 			case OCON_IBENDPORT: {
3020 				uint32_t port;
3021 
3022 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3023 				if (rc < 0)
3024 					return -1;
3025 				len = le32_to_cpu(buf[0]);
3026 				if (len == 0 || len > IB_DEVICE_NAME_MAX - 1)
3027 					return -1;
3028 
3029 				port = le32_to_cpu(buf[1]);
3030 				if (port > UINT8_MAX || port == 0)
3031 					return -1;
3032 
3033 				rc = str_read(&c->u.ibendport.dev_name, fp, len);
3034 				if (rc < 0)
3035 					return -1;
3036 
3037 				c->u.ibendport.port = port;
3038 				if (context_read_and_validate
3039 				    (&c->context[0], p, fp))
3040 					return -1;
3041 				break;
3042 			}
3043 			case OCON_PORT:
3044 				rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
3045 				if (rc < 0)
3046 					return -1;
3047 				c->u.port.protocol = le32_to_cpu(buf[0]);
3048 				c->u.port.low_port = le32_to_cpu(buf[1]);
3049 				c->u.port.high_port = le32_to_cpu(buf[2]);
3050 				if (context_read_and_validate
3051 				    (&c->context[0], p, fp))
3052 					return -1;
3053 				break;
3054 			case OCON_NODE:
3055 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3056 				if (rc < 0)
3057 					return -1;
3058 				c->u.node.addr = buf[0]; /* network order */
3059 				c->u.node.mask = buf[1]; /* network order */
3060 				if (context_read_and_validate
3061 				    (&c->context[0], p, fp))
3062 					return -1;
3063 				break;
3064 			case OCON_FSUSE:
3065 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3066 				if (rc < 0)
3067 					return -1;
3068 				c->v.behavior = le32_to_cpu(buf[0]);
3069 				len = le32_to_cpu(buf[1]);
3070 
3071 				rc = str_read(&c->u.name, fp, len);
3072 				if (rc < 0)
3073 					return -1;
3074 
3075 				if (context_read_and_validate
3076 				    (&c->context[0], p, fp))
3077 					return -1;
3078 				break;
3079 			case OCON_NODE6:{
3080 				int k;
3081 
3082 				rc = next_entry(buf, fp, sizeof(uint32_t) * 8);
3083 				if (rc < 0)
3084 					return -1;
3085 				for (k = 0; k < 4; k++)
3086 					 /* network order */
3087 					c->u.node6.addr[k] = buf[k];
3088 				for (k = 0; k < 4; k++)
3089 					/* network order */
3090 					c->u.node6.mask[k] = buf[k + 4];
3091 				if (context_read_and_validate
3092 				    (&c->context[0], p, fp))
3093 					return -1;
3094 				break;
3095 				}
3096 			default:{
3097 				ERR(fp->handle, "Unknown SELinux ocontext");
3098 				return -1;
3099 				}
3100 			}
3101 		}
3102 	}
3103 	return 0;
3104 }
3105 
ocontext_read(const struct policydb_compat_info * info,policydb_t * p,struct policy_file * fp)3106 static int ocontext_read(const struct policydb_compat_info *info,
3107 	policydb_t *p, struct policy_file *fp)
3108 {
3109 	int rc = -1;
3110 	switch (p->target_platform) {
3111 	case SEPOL_TARGET_SELINUX:
3112 		rc = ocontext_read_selinux(info, p, fp);
3113 		break;
3114 	case SEPOL_TARGET_XEN:
3115 		rc = ocontext_read_xen(info, p, fp);
3116 		break;
3117 	default:
3118 		ERR(fp->handle, "Unknown target");
3119 	}
3120 	return rc;
3121 }
3122 
genfs_read(policydb_t * p,struct policy_file * fp)3123 static int genfs_read(policydb_t * p, struct policy_file *fp)
3124 {
3125 	uint32_t buf[1];
3126 	size_t nel, nel2, len, len2;
3127 	genfs_t *genfs_p, *newgenfs, *genfs;
3128 	size_t i, j;
3129 	ocontext_t *l, *c, *newc = NULL;
3130 	int rc;
3131 
3132 	rc = next_entry(buf, fp, sizeof(uint32_t));
3133 	if (rc < 0)
3134 		goto bad;
3135 	nel = le32_to_cpu(buf[0]);
3136 	genfs_p = NULL;
3137 	for (i = 0; i < nel; i++) {
3138 		rc = next_entry(buf, fp, sizeof(uint32_t));
3139 		if (rc < 0)
3140 			goto bad;
3141 		len = le32_to_cpu(buf[0]);
3142 		newgenfs = calloc(1, sizeof(genfs_t));
3143 		if (!newgenfs)
3144 			goto bad;
3145 
3146 		rc = str_read(&newgenfs->fstype, fp, len);
3147 		if (rc < 0) {
3148 			free(newgenfs->fstype);
3149 			free(newgenfs);
3150 			goto bad;
3151 		}
3152 
3153 		for (genfs_p = NULL, genfs = p->genfs; genfs;
3154 		     genfs_p = genfs, genfs = genfs->next) {
3155 			if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
3156 				ERR(fp->handle, "dup genfs fstype %s",
3157 				    newgenfs->fstype);
3158 				free(newgenfs->fstype);
3159 				free(newgenfs);
3160 				goto bad;
3161 			}
3162 			if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
3163 				break;
3164 		}
3165 		newgenfs->next = genfs;
3166 		if (genfs_p)
3167 			genfs_p->next = newgenfs;
3168 		else
3169 			p->genfs = newgenfs;
3170 		rc = next_entry(buf, fp, sizeof(uint32_t));
3171 		if (rc < 0)
3172 			goto bad;
3173 		nel2 = le32_to_cpu(buf[0]);
3174 		for (j = 0; j < nel2; j++) {
3175 			newc = calloc(1, sizeof(ocontext_t));
3176 			if (!newc) {
3177 				goto bad;
3178 			}
3179 			rc = next_entry(buf, fp, sizeof(uint32_t));
3180 			if (rc < 0)
3181 				goto bad;
3182 			len = le32_to_cpu(buf[0]);
3183 			rc = str_read(&newc->u.name, fp, len);
3184 			if (rc < 0)
3185 				goto bad;
3186 
3187 			rc = next_entry(buf, fp, sizeof(uint32_t));
3188 			if (rc < 0)
3189 				goto bad;
3190 			newc->v.sclass = le32_to_cpu(buf[0]);
3191 			if (context_read_and_validate(&newc->context[0], p, fp))
3192 				goto bad;
3193 			for (l = NULL, c = newgenfs->head; c;
3194 			     l = c, c = c->next) {
3195 				if (!strcmp(newc->u.name, c->u.name) &&
3196 				    (!c->v.sclass || !newc->v.sclass ||
3197 				     newc->v.sclass == c->v.sclass)) {
3198 					ERR(fp->handle, "dup genfs entry "
3199 					    "(%s,%s)", newgenfs->fstype,
3200 					    c->u.name);
3201 					goto bad;
3202 				}
3203 				len = strlen(newc->u.name);
3204 				len2 = strlen(c->u.name);
3205 				if (len > len2)
3206 					break;
3207 			}
3208 			newc->next = c;
3209 			if (l)
3210 				l->next = newc;
3211 			else
3212 				newgenfs->head = newc;
3213 			/* clear newc after a new owner has the pointer */
3214 			newc = NULL;
3215 		}
3216 	}
3217 
3218 	return 0;
3219 
3220       bad:
3221 	if (newc) {
3222 		context_destroy(&newc->context[0]);
3223 		context_destroy(&newc->context[1]);
3224 		free(newc->u.name);
3225 		free(newc);
3226 	}
3227 	return -1;
3228 }
3229 
3230 /*
3231  * Read a MLS level structure from a policydb binary
3232  * representation file.
3233  */
mls_read_level(mls_level_t * lp,struct policy_file * fp)3234 static int mls_read_level(mls_level_t * lp, struct policy_file *fp)
3235 {
3236 	uint32_t buf[1];
3237 	int rc;
3238 
3239 	mls_level_init(lp);
3240 
3241 	rc = next_entry(buf, fp, sizeof(uint32_t));
3242 	if (rc < 0) {
3243 		ERR(fp->handle, "truncated level");
3244 		goto bad;
3245 	}
3246 	lp->sens = le32_to_cpu(buf[0]);
3247 
3248 	if (ebitmap_read(&lp->cat, fp)) {
3249 		ERR(fp->handle, "error reading level categories");
3250 		goto bad;
3251 	}
3252 	return 0;
3253 
3254       bad:
3255 	return -EINVAL;
3256 }
3257 
user_read(policydb_t * p,hashtab_t h,struct policy_file * fp)3258 static int user_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
3259 {
3260 	char *key = 0;
3261 	user_datum_t *usrdatum;
3262 	uint32_t buf[3];
3263 	size_t len;
3264 	int rc, to_read = 2;
3265 
3266 	usrdatum = calloc(1, sizeof(user_datum_t));
3267 	if (!usrdatum)
3268 		return -1;
3269 
3270 	if (policydb_has_boundary_feature(p))
3271 		to_read = 3;
3272 
3273 	rc = next_entry(buf, fp, sizeof(uint32_t) * to_read);
3274 	if (rc < 0)
3275 		goto bad;
3276 
3277 	len = le32_to_cpu(buf[0]);
3278 	rc = str_read(&key, fp, len);
3279 	if (rc < 0)
3280 		goto bad;
3281 
3282 	usrdatum->s.value = le32_to_cpu(buf[1]);
3283 	if (policydb_has_boundary_feature(p))
3284 		usrdatum->bounds = le32_to_cpu(buf[2]);
3285 
3286 	if (p->policy_type == POLICY_KERN) {
3287 		if (ebitmap_read(&usrdatum->roles.roles, fp))
3288 			goto bad;
3289 	} else {
3290 		if (role_set_read(&usrdatum->roles, fp))
3291 			goto bad;
3292 	}
3293 
3294 	/* users were not allowed in mls modules before version
3295 	 * MOD_POLICYDB_VERSION_MLS_USERS, but they could have been
3296 	 * required - the mls fields will be empty.  user declarations in
3297 	 * non-mls modules will also have empty mls fields */
3298 	if ((p->policy_type == POLICY_KERN
3299 	     && p->policyvers >= POLICYDB_VERSION_MLS)
3300 	    || (p->policy_type == POLICY_MOD
3301 		&& p->policyvers >= MOD_POLICYDB_VERSION_MLS
3302 		&& p->policyvers < MOD_POLICYDB_VERSION_MLS_USERS)
3303 	    || (p->policy_type == POLICY_BASE
3304 		&& p->policyvers >= MOD_POLICYDB_VERSION_MLS
3305 		&& p->policyvers < MOD_POLICYDB_VERSION_MLS_USERS)) {
3306 		if (mls_read_range_helper(&usrdatum->exp_range, fp))
3307 			goto bad;
3308 		if (mls_read_level(&usrdatum->exp_dfltlevel, fp))
3309 			goto bad;
3310 		if (p->policy_type != POLICY_KERN) {
3311 			if (mls_range_to_semantic(&usrdatum->exp_range,
3312 						  &usrdatum->range))
3313 				goto bad;
3314 			if (mls_level_to_semantic(&usrdatum->exp_dfltlevel,
3315 						  &usrdatum->dfltlevel))
3316 				goto bad;
3317 		}
3318 	} else if ((p->policy_type == POLICY_MOD
3319 		    && p->policyvers >= MOD_POLICYDB_VERSION_MLS_USERS)
3320 		   || (p->policy_type == POLICY_BASE
3321 		       && p->policyvers >= MOD_POLICYDB_VERSION_MLS_USERS)) {
3322 		if (mls_read_semantic_range_helper(&usrdatum->range, fp))
3323 			goto bad;
3324 		if (mls_read_semantic_level_helper(&usrdatum->dfltlevel, fp))
3325 			goto bad;
3326 	}
3327 
3328 	if (hashtab_insert(h, key, usrdatum))
3329 		goto bad;
3330 
3331 	return 0;
3332 
3333       bad:
3334 	user_destroy(key, usrdatum, NULL);
3335 	return -1;
3336 }
3337 
sens_read(policydb_t * p,hashtab_t h,struct policy_file * fp)3338 static int sens_read(policydb_t * p
3339 		     __attribute__ ((unused)), hashtab_t h,
3340 		     struct policy_file *fp)
3341 {
3342 	char *key = 0;
3343 	level_datum_t *levdatum;
3344 	uint32_t buf[2], len;
3345 	int rc;
3346 
3347 	levdatum = malloc(sizeof(level_datum_t));
3348 	if (!levdatum)
3349 		return -1;
3350 	level_datum_init(levdatum);
3351 
3352 	rc = next_entry(buf, fp, (sizeof(uint32_t) * 2));
3353 	if (rc < 0)
3354 		goto bad;
3355 
3356 	len = le32_to_cpu(buf[0]);
3357 	rc = str_read(&key, fp, len);
3358 	if (rc < 0)
3359 		goto bad;
3360 
3361 	levdatum->isalias = le32_to_cpu(buf[1]);
3362 
3363 	levdatum->level = malloc(sizeof(mls_level_t));
3364 	if (!levdatum->level || mls_read_level(levdatum->level, fp))
3365 		goto bad;
3366 
3367 	if (hashtab_insert(h, key, levdatum))
3368 		goto bad;
3369 
3370 	return 0;
3371 
3372       bad:
3373 	sens_destroy(key, levdatum, NULL);
3374 	return -1;
3375 }
3376 
cat_read(policydb_t * p,hashtab_t h,struct policy_file * fp)3377 static int cat_read(policydb_t * p
3378 		    __attribute__ ((unused)), hashtab_t h,
3379 		    struct policy_file *fp)
3380 {
3381 	char *key = 0;
3382 	cat_datum_t *catdatum;
3383 	uint32_t buf[3], len;
3384 	int rc;
3385 
3386 	catdatum = malloc(sizeof(cat_datum_t));
3387 	if (!catdatum)
3388 		return -1;
3389 	cat_datum_init(catdatum);
3390 
3391 	rc = next_entry(buf, fp, (sizeof(uint32_t) * 3));
3392 	if (rc < 0)
3393 		goto bad;
3394 
3395 	len = le32_to_cpu(buf[0]);
3396 	rc = str_read(&key, fp, len);
3397 	if (rc < 0)
3398 		goto bad;
3399 
3400 	catdatum->s.value = le32_to_cpu(buf[1]);
3401 	catdatum->isalias = le32_to_cpu(buf[2]);
3402 
3403 	if (hashtab_insert(h, key, catdatum))
3404 		goto bad;
3405 
3406 	return 0;
3407 
3408       bad:
3409 	cat_destroy(key, catdatum, NULL);
3410 	return -1;
3411 }
3412 
3413 static int (*const read_f[SYM_NUM]) (policydb_t * p, hashtab_t h,
3414 			       struct policy_file * fp) = {
3415 common_read, class_read, role_read, type_read, user_read,
3416 	    cond_read_bool, sens_read, cat_read,};
3417 
3418 /************** module reading functions below **************/
3419 
avrule_read(policydb_t * p,struct policy_file * fp)3420 static avrule_t *avrule_read(policydb_t * p, struct policy_file *fp)
3421 {
3422 	unsigned int i;
3423 	uint32_t buf[2], len;
3424 	class_perm_node_t *cur, *tail = NULL;
3425 	avrule_t *avrule;
3426 	int rc;
3427 
3428 	avrule = (avrule_t *) malloc(sizeof(avrule_t));
3429 	if (!avrule)
3430 		return NULL;
3431 
3432 	avrule_init(avrule);
3433 
3434 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3435 	if (rc < 0)
3436 		goto bad;
3437 
3438 	avrule->specified = le32_to_cpu(buf[0]);
3439 	avrule->flags = le32_to_cpu(buf[1]);
3440 
3441 	if (type_set_read(&avrule->stypes, fp))
3442 		goto bad;
3443 
3444 	if (type_set_read(&avrule->ttypes, fp))
3445 		goto bad;
3446 
3447 	rc = next_entry(buf, fp, sizeof(uint32_t));
3448 	if (rc < 0)
3449 		goto bad;
3450 	len = le32_to_cpu(buf[0]);
3451 
3452 	for (i = 0; i < len; i++) {
3453 		cur = (class_perm_node_t *) malloc(sizeof(class_perm_node_t));
3454 		if (!cur)
3455 			goto bad;
3456 		class_perm_node_init(cur);
3457 
3458 		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3459 		if (rc < 0) {
3460 			free(cur);
3461 			goto bad;
3462 		}
3463 
3464 		cur->tclass = le32_to_cpu(buf[0]);
3465 		cur->data = le32_to_cpu(buf[1]);
3466 
3467 		if (!tail) {
3468 			avrule->perms = cur;
3469 		} else {
3470 			tail->next = cur;
3471 		}
3472 		tail = cur;
3473 	}
3474 
3475 	if (avrule->specified & AVRULE_XPERMS) {
3476 		uint8_t buf8;
3477 		size_t nel = ARRAY_SIZE(avrule->xperms->perms);
3478 		uint32_t buf32[nel];
3479 
3480 		if (p->policyvers < MOD_POLICYDB_VERSION_XPERMS_IOCTL) {
3481 			ERR(fp->handle,
3482 			    "module policy version %u does not support ioctl"
3483 			    " extended permissions rules and one was specified",
3484 			    p->policyvers);
3485 			goto bad;
3486 		}
3487 
3488 		if (p->target_platform != SEPOL_TARGET_SELINUX) {
3489 			ERR(fp->handle,
3490 			    "Target platform %s does not support ioctl"
3491 			    " extended permissions rules and one was specified",
3492 			    policydb_target_strings[p->target_platform]);
3493 			goto bad;
3494 		}
3495 
3496 		avrule->xperms = calloc(1, sizeof(*avrule->xperms));
3497 		if (!avrule->xperms)
3498 			goto bad;
3499 
3500 		rc = next_entry(&buf8, fp, sizeof(uint8_t));
3501 		if (rc < 0) {
3502 			ERR(fp->handle, "truncated entry");
3503 			goto bad;
3504 		}
3505 		avrule->xperms->specified = buf8;
3506 		rc = next_entry(&buf8, fp, sizeof(uint8_t));
3507 		if (rc < 0) {
3508 			ERR(fp->handle, "truncated entry");
3509 			goto bad;
3510 		}
3511 		avrule->xperms->driver = buf8;
3512 		rc = next_entry(buf32, fp, sizeof(uint32_t)*nel);
3513 		if (rc < 0) {
3514 			ERR(fp->handle, "truncated entry");
3515 			goto bad;
3516 		}
3517 		for (i = 0; i < nel; i++)
3518 			avrule->xperms->perms[i] = le32_to_cpu(buf32[i]);
3519 	}
3520 
3521 	return avrule;
3522       bad:
3523 	if (avrule) {
3524 		avrule_destroy(avrule);
3525 		free(avrule);
3526 	}
3527 	return NULL;
3528 }
3529 
range_read(policydb_t * p,struct policy_file * fp)3530 static int range_read(policydb_t * p, struct policy_file *fp)
3531 {
3532 	uint32_t buf[2], nel;
3533 	range_trans_t *rt = NULL;
3534 	struct mls_range *r = NULL;
3535 	range_trans_rule_t *rtr = NULL, *lrtr = NULL;
3536 	unsigned int i;
3537 	int new_rangetr = (p->policy_type == POLICY_KERN &&
3538 			   p->policyvers >= POLICYDB_VERSION_RANGETRANS);
3539 	int rc;
3540 
3541 	rc = next_entry(buf, fp, sizeof(uint32_t));
3542 	if (rc < 0)
3543 		return -1;
3544 	nel = le32_to_cpu(buf[0]);
3545 	for (i = 0; i < nel; i++) {
3546 		rt = calloc(1, sizeof(range_trans_t));
3547 		if (!rt)
3548 			return -1;
3549 		rc = next_entry(buf, fp, (sizeof(uint32_t) * 2));
3550 		if (rc < 0)
3551 			goto err;
3552 		rt->source_type = le32_to_cpu(buf[0]);
3553 		if (!value_isvalid(rt->source_type, p->p_types.nprim))
3554 			goto err;
3555 		rt->target_type = le32_to_cpu(buf[1]);
3556 		if (!value_isvalid(rt->target_type, p->p_types.nprim))
3557 			goto err;
3558 		if (new_rangetr) {
3559 			rc = next_entry(buf, fp, (sizeof(uint32_t)));
3560 			if (rc < 0)
3561 				goto err;
3562 			rt->target_class = le32_to_cpu(buf[0]);
3563 		} else
3564 			rt->target_class = p->process_class;
3565 		if (!value_isvalid(rt->target_class, p->p_classes.nprim))
3566 			goto err;
3567 		r = calloc(1, sizeof(*r));
3568 		if (!r)
3569 			goto err;
3570 		if (mls_read_range_helper(r, fp))
3571 			goto err;
3572 
3573 		if (p->policy_type == POLICY_KERN) {
3574 			rc = hashtab_insert(p->range_tr, (hashtab_key_t)rt, r);
3575 			if (rc)
3576 				goto err;
3577 			rt = NULL;
3578 			r = NULL;
3579 			continue;
3580 		}
3581 
3582 		/* Module policy: convert to range_trans_rule and discard. */
3583 		rtr = malloc(sizeof(range_trans_rule_t));
3584 		if (!rtr)
3585 			goto err;
3586 		range_trans_rule_init(rtr);
3587 
3588 		if (ebitmap_set_bit(&rtr->stypes.types, rt->source_type - 1, 1))
3589 			goto err;
3590 
3591 		if (ebitmap_set_bit(&rtr->ttypes.types, rt->target_type - 1, 1))
3592 			goto err;
3593 
3594 		if (ebitmap_set_bit(&rtr->tclasses, rt->target_class - 1, 1))
3595 			goto err;
3596 
3597 		if (mls_range_to_semantic(r, &rtr->trange))
3598 			goto err;
3599 
3600 		if (lrtr)
3601 			lrtr->next = rtr;
3602 		else
3603 			p->global->enabled->range_tr_rules = rtr;
3604 
3605 		free(rt);
3606 		rt = NULL;
3607 		free(r);
3608 		r = NULL;
3609 		lrtr = rtr;
3610 	}
3611 
3612 	return 0;
3613 err:
3614 	free(rt);
3615 	if (r) {
3616 		mls_range_destroy(r);
3617 		free(r);
3618 	}
3619 	if (rtr) {
3620 		range_trans_rule_destroy(rtr);
3621 		free(rtr);
3622 	}
3623 	return -1;
3624 }
3625 
avrule_read_list(policydb_t * p,avrule_t ** avrules,struct policy_file * fp)3626 int avrule_read_list(policydb_t * p, avrule_t ** avrules,
3627 		     struct policy_file *fp)
3628 {
3629 	unsigned int i;
3630 	avrule_t *cur, *tail;
3631 	uint32_t buf[1], len;
3632 	int rc;
3633 
3634 	*avrules = tail = NULL;
3635 
3636 	rc = next_entry(buf, fp, sizeof(uint32_t));
3637 	if (rc < 0) {
3638 		return -1;
3639 	}
3640 	len = le32_to_cpu(buf[0]);
3641 
3642 	for (i = 0; i < len; i++) {
3643 		cur = avrule_read(p, fp);
3644 		if (!cur) {
3645 			return -1;
3646 		}
3647 
3648 		if (!tail) {
3649 			*avrules = cur;
3650 		} else {
3651 			tail->next = cur;
3652 		}
3653 		tail = cur;
3654 	}
3655 
3656 	return 0;
3657 }
3658 
role_trans_rule_read(policydb_t * p,role_trans_rule_t ** r,struct policy_file * fp)3659 static int role_trans_rule_read(policydb_t *p, role_trans_rule_t ** r,
3660 				struct policy_file *fp)
3661 {
3662 	uint32_t buf[1], nel;
3663 	unsigned int i;
3664 	role_trans_rule_t *tr, *ltr;
3665 	int rc;
3666 
3667 	rc = next_entry(buf, fp, sizeof(uint32_t));
3668 	if (rc < 0)
3669 		return -1;
3670 	nel = le32_to_cpu(buf[0]);
3671 	ltr = NULL;
3672 	for (i = 0; i < nel; i++) {
3673 		tr = malloc(sizeof(role_trans_rule_t));
3674 		if (!tr) {
3675 			return -1;
3676 		}
3677 		role_trans_rule_init(tr);
3678 
3679 		if (ltr) {
3680 			ltr->next = tr;
3681 		} else {
3682 			*r = tr;
3683 		}
3684 
3685 		if (role_set_read(&tr->roles, fp))
3686 			return -1;
3687 
3688 		if (type_set_read(&tr->types, fp))
3689 			return -1;
3690 
3691 		if (p->policyvers >= MOD_POLICYDB_VERSION_ROLETRANS) {
3692 			if (ebitmap_read(&tr->classes, fp))
3693 				return -1;
3694 		} else {
3695 			if (!p->process_class)
3696 				return -1;
3697 			if (ebitmap_set_bit(&tr->classes, p->process_class - 1, 1))
3698 				return -1;
3699 		}
3700 
3701 		rc = next_entry(buf, fp, sizeof(uint32_t));
3702 		if (rc < 0)
3703 			return -1;
3704 		tr->new_role = le32_to_cpu(buf[0]);
3705 		ltr = tr;
3706 	}
3707 
3708 	return 0;
3709 }
3710 
role_allow_rule_read(role_allow_rule_t ** r,struct policy_file * fp)3711 static int role_allow_rule_read(role_allow_rule_t ** r, struct policy_file *fp)
3712 {
3713 	unsigned int i;
3714 	uint32_t buf[1], nel;
3715 	role_allow_rule_t *ra, *lra;
3716 	int rc;
3717 
3718 	rc = next_entry(buf, fp, sizeof(uint32_t));
3719 	if (rc < 0)
3720 		return -1;
3721 	nel = le32_to_cpu(buf[0]);
3722 	lra = NULL;
3723 	for (i = 0; i < nel; i++) {
3724 		ra = malloc(sizeof(role_allow_rule_t));
3725 		if (!ra) {
3726 			return -1;
3727 		}
3728 		role_allow_rule_init(ra);
3729 
3730 		if (lra) {
3731 			lra->next = ra;
3732 		} else {
3733 			*r = ra;
3734 		}
3735 
3736 		if (role_set_read(&ra->roles, fp))
3737 			return -1;
3738 
3739 		if (role_set_read(&ra->new_roles, fp))
3740 			return -1;
3741 
3742 		lra = ra;
3743 	}
3744 	return 0;
3745 }
3746 
filename_trans_rule_read(policydb_t * p,filename_trans_rule_t ** r,struct policy_file * fp)3747 static int filename_trans_rule_read(policydb_t *p, filename_trans_rule_t **r,
3748 				    struct policy_file *fp)
3749 {
3750 	uint32_t buf[3], nel, i, len;
3751 	unsigned int entries;
3752 	filename_trans_rule_t *ftr, *lftr;
3753 	int rc;
3754 
3755 	rc = next_entry(buf, fp, sizeof(uint32_t));
3756 	if (rc < 0)
3757 		return -1;
3758 	nel = le32_to_cpu(buf[0]);
3759 	lftr = NULL;
3760 	for (i = 0; i < nel; i++) {
3761 		ftr = malloc(sizeof(*ftr));
3762 		if (!ftr)
3763 			return -1;
3764 
3765 		filename_trans_rule_init(ftr);
3766 
3767 		if (lftr)
3768 			lftr->next = ftr;
3769 		else
3770 			*r = ftr;
3771 		lftr = ftr;
3772 
3773 		rc = next_entry(buf, fp, sizeof(uint32_t));
3774 		if (rc < 0)
3775 			return -1;
3776 
3777 		len = le32_to_cpu(buf[0]);
3778 
3779 		rc = str_read(&ftr->name, fp, len);
3780 		if (rc < 0)
3781 			return -1;
3782 
3783 		if (type_set_read(&ftr->stypes, fp))
3784 			return -1;
3785 
3786 		if (type_set_read(&ftr->ttypes, fp))
3787 			return -1;
3788 
3789 		if (p->policyvers >= MOD_POLICYDB_VERSION_SELF_TYPETRANS)
3790 			entries = 3;
3791 		else
3792 			entries = 2;
3793 
3794 		rc = next_entry(buf, fp, sizeof(uint32_t) * entries);
3795 		if (rc < 0)
3796 			return -1;
3797 		ftr->tclass = le32_to_cpu(buf[0]);
3798 		ftr->otype = le32_to_cpu(buf[1]);
3799 		if (p->policyvers >= MOD_POLICYDB_VERSION_SELF_TYPETRANS)
3800 			ftr->flags = le32_to_cpu(buf[2]);
3801 	}
3802 
3803 	return 0;
3804 }
3805 
range_trans_rule_read(range_trans_rule_t ** r,struct policy_file * fp)3806 static int range_trans_rule_read(range_trans_rule_t ** r,
3807 				 struct policy_file *fp)
3808 {
3809 	uint32_t buf[1], nel;
3810 	unsigned int i;
3811 	range_trans_rule_t *rt, *lrt = NULL;
3812 	int rc;
3813 
3814 	rc = next_entry(buf, fp, sizeof(uint32_t));
3815 	if (rc < 0)
3816 		return -1;
3817 	nel = le32_to_cpu(buf[0]);
3818 	for (i = 0; i < nel; i++) {
3819 		rt = malloc(sizeof(range_trans_rule_t));
3820 		if (!rt) {
3821 			return -1;
3822 		}
3823 		range_trans_rule_init(rt);
3824 
3825 		if (lrt)
3826 			lrt->next = rt;
3827 		else
3828 			*r = rt;
3829 
3830 		if (type_set_read(&rt->stypes, fp))
3831 			return -1;
3832 
3833 		if (type_set_read(&rt->ttypes, fp))
3834 			return -1;
3835 
3836 		if (ebitmap_read(&rt->tclasses, fp))
3837 			return -1;
3838 
3839 		if (mls_read_semantic_range_helper(&rt->trange, fp))
3840 			return -1;
3841 
3842 		lrt = rt;
3843 	}
3844 
3845 	return 0;
3846 }
3847 
scope_index_read(scope_index_t * scope_index,unsigned int num_scope_syms,struct policy_file * fp)3848 static int scope_index_read(scope_index_t * scope_index,
3849 			    unsigned int num_scope_syms, struct policy_file *fp)
3850 {
3851 	unsigned int i;
3852 	uint32_t buf[1];
3853 	int rc;
3854 
3855 	for (i = 0; i < num_scope_syms; i++) {
3856 		if (ebitmap_read(scope_index->scope + i, fp) < 0) {
3857 			return -1;
3858 		}
3859 	}
3860 	rc = next_entry(buf, fp, sizeof(uint32_t));
3861 	if (rc < 0)
3862 		return -1;
3863 	scope_index->class_perms_len = le32_to_cpu(buf[0]);
3864 	if (is_saturated(scope_index->class_perms_len) ||
3865 	    exceeds_available_bytes(fp, scope_index->class_perms_len, sizeof(uint32_t) * 3))
3866 		return -1;
3867 	if (scope_index->class_perms_len == 0) {
3868 		scope_index->class_perms_map = NULL;
3869 		return 0;
3870 	}
3871 	if ((scope_index->class_perms_map =
3872 	     calloc(scope_index->class_perms_len,
3873 		    sizeof(*scope_index->class_perms_map))) == NULL) {
3874 		return -1;
3875 	}
3876 	for (i = 0; i < scope_index->class_perms_len; i++) {
3877 		if (ebitmap_read(scope_index->class_perms_map + i, fp) < 0) {
3878 			return -1;
3879 		}
3880 	}
3881 	return 0;
3882 }
3883 
avrule_decl_read(policydb_t * p,avrule_decl_t * decl,unsigned int num_scope_syms,struct policy_file * fp)3884 static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,
3885 			    unsigned int num_scope_syms, struct policy_file *fp)
3886 {
3887 	uint32_t buf[2], nprim, nel;
3888 	unsigned int i, j;
3889 	int rc;
3890 
3891 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3892 	if (rc < 0)
3893 		return -1;
3894 	decl->decl_id = le32_to_cpu(buf[0]);
3895 	decl->enabled = le32_to_cpu(buf[1]);
3896 	if (cond_read_list(p, &decl->cond_list, fp) == -1 ||
3897 	    avrule_read_list(p, &decl->avrules, fp) == -1 ||
3898 	    role_trans_rule_read(p, &decl->role_tr_rules, fp) == -1 ||
3899 	    role_allow_rule_read(&decl->role_allow_rules, fp) == -1) {
3900 		return -1;
3901 	}
3902 
3903 	if (p->policyvers >= MOD_POLICYDB_VERSION_FILENAME_TRANS &&
3904 	    filename_trans_rule_read(p, &decl->filename_trans_rules, fp))
3905 		return -1;
3906 
3907 	if (p->policyvers >= MOD_POLICYDB_VERSION_RANGETRANS &&
3908 	    range_trans_rule_read(&decl->range_tr_rules, fp) == -1) {
3909 		return -1;
3910 	}
3911 	if (scope_index_read(&decl->required, num_scope_syms, fp) == -1 ||
3912 	    scope_index_read(&decl->declared, num_scope_syms, fp) == -1) {
3913 		return -1;
3914 	}
3915 
3916 	for (i = 0; i < num_scope_syms; i++) {
3917 		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
3918 		if (rc < 0)
3919 			return -1;
3920 		nprim = le32_to_cpu(buf[0]);
3921 		if (is_saturated(nprim))
3922 			return -1;
3923 		nel = le32_to_cpu(buf[1]);
3924 		for (j = 0; j < nel; j++) {
3925 			if (read_f[i] (p, decl->symtab[i].table, fp)) {
3926 				return -1;
3927 			}
3928 		}
3929 		decl->symtab[i].nprim = nprim;
3930 	}
3931 	return 0;
3932 }
3933 
avrule_block_read(policydb_t * p,avrule_block_t ** block,unsigned int num_scope_syms,struct policy_file * fp)3934 static int avrule_block_read(policydb_t * p,
3935 			     avrule_block_t ** block,
3936 			     unsigned int num_scope_syms,
3937 			     struct policy_file *fp)
3938 {
3939 	avrule_block_t *last_block = NULL, *curblock;
3940 	uint32_t buf[1], num_blocks, nel;
3941 	int rc;
3942 
3943 	assert(*block == NULL);
3944 
3945 	rc = next_entry(buf, fp, sizeof(uint32_t));
3946 	if (rc < 0)
3947 		return -1;
3948 	num_blocks = le32_to_cpu(buf[0]);
3949 	nel = num_blocks;
3950 	while (num_blocks > 0) {
3951 		avrule_decl_t *last_decl = NULL, *curdecl;
3952 		uint32_t num_decls;
3953 		if ((curblock = calloc(1, sizeof(*curblock))) == NULL) {
3954 			return -1;
3955 		}
3956 		rc = next_entry(buf, fp, sizeof(uint32_t));
3957 		if (rc < 0) {
3958 			free(curblock);
3959 			return -1;
3960 		}
3961 		/* if this is the first block its non-optional, else its optional */
3962 		if (num_blocks != nel)
3963 			curblock->flags |= AVRULE_OPTIONAL;
3964 
3965 		num_decls = le32_to_cpu(buf[0]);
3966 		while (num_decls > 0) {
3967 			if ((curdecl = avrule_decl_create(0)) == NULL) {
3968 				avrule_block_destroy(curblock);
3969 				return -1;
3970 			}
3971 			if (avrule_decl_read(p, curdecl, num_scope_syms, fp) ==
3972 			    -1) {
3973 				avrule_decl_destroy(curdecl);
3974 				avrule_block_destroy(curblock);
3975 				return -1;
3976 			}
3977 			if (curdecl->enabled) {
3978 				if (curblock->enabled != NULL) {
3979 					/* probably a corrupt file */
3980 					avrule_decl_destroy(curdecl);
3981 					avrule_block_destroy(curblock);
3982 					return -1;
3983 				}
3984 				curblock->enabled = curdecl;
3985 			}
3986 			/* one must be careful to reconstruct the
3987 			 * decl chain in its correct order */
3988 			if (curblock->branch_list == NULL) {
3989 				curblock->branch_list = curdecl;
3990 			} else {
3991 				assert(last_decl);
3992 				last_decl->next = curdecl;
3993 			}
3994 			last_decl = curdecl;
3995 			num_decls--;
3996 		}
3997 
3998 		if (*block == NULL) {
3999 			*block = curblock;
4000 		} else {
4001 			assert(last_block);
4002 			last_block->next = curblock;
4003 		}
4004 		last_block = curblock;
4005 
4006 		num_blocks--;
4007 	}
4008 
4009 	return 0;
4010 }
4011 
scope_read(policydb_t * p,int symnum,struct policy_file * fp)4012 static int scope_read(policydb_t * p, int symnum, struct policy_file *fp)
4013 {
4014 	scope_datum_t *scope = NULL;
4015 	uint32_t buf[2];
4016 	char *key = NULL;
4017 	size_t key_len;
4018 	unsigned int i;
4019 	hashtab_t h = p->scope[symnum].table;
4020 	int rc;
4021 
4022 	rc = next_entry(buf, fp, sizeof(uint32_t));
4023 	if (rc < 0)
4024 		goto cleanup;
4025 	key_len = le32_to_cpu(buf[0]);
4026 
4027 	rc = str_read(&key, fp, key_len);
4028 	if (rc < 0)
4029 		goto cleanup;
4030 
4031 	/* ensure that there already exists a symbol with this key */
4032 	if (hashtab_search(p->symtab[symnum].table, key) == NULL) {
4033 		goto cleanup;
4034 	}
4035 
4036 	if ((scope = calloc(1, sizeof(*scope))) == NULL) {
4037 		goto cleanup;
4038 	}
4039 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
4040 	if (rc < 0)
4041 		goto cleanup;
4042 	scope->scope = le32_to_cpu(buf[0]);
4043 	scope->decl_ids_len = le32_to_cpu(buf[1]);
4044 	if (zero_or_saturated(scope->decl_ids_len) ||
4045 	    exceeds_available_bytes(fp, scope->decl_ids_len, sizeof(uint32_t))) {
4046 		ERR(fp->handle, "invalid scope with no declaration");
4047 		goto cleanup;
4048 	}
4049 	if ((scope->decl_ids =
4050 	     calloc(scope->decl_ids_len, sizeof(uint32_t))) == NULL) {
4051 		goto cleanup;
4052 	}
4053 	rc = next_entry(scope->decl_ids, fp, sizeof(uint32_t) * scope->decl_ids_len);
4054 	if (rc < 0)
4055 		goto cleanup;
4056 	for (i = 0; i < scope->decl_ids_len; i++) {
4057 		scope->decl_ids[i] = le32_to_cpu(scope->decl_ids[i]);
4058 	}
4059 
4060 	if (strcmp(key, "object_r") == 0 && h == p->p_roles_scope.table) {
4061 		/* object_r was already added to this table in roles_init() */
4062 		scope_destroy(key, scope, NULL);
4063 	} else {
4064 		if (hashtab_insert(h, key, scope)) {
4065 			goto cleanup;
4066 		}
4067 	}
4068 
4069 	return 0;
4070 
4071       cleanup:
4072 	scope_destroy(key, scope, NULL);
4073 	return -1;
4074 }
4075 
policydb_string_to_security_class(struct policydb * policydb,const char * class_name)4076 static sepol_security_class_t policydb_string_to_security_class(
4077 	struct policydb *policydb,
4078 	const char *class_name)
4079 {
4080 	class_datum_t *tclass_datum;
4081 
4082 	tclass_datum = hashtab_search(policydb->p_classes.table,
4083 				      class_name);
4084 	if (!tclass_datum)
4085 		return 0;
4086 	return tclass_datum->s.value;
4087 }
4088 
policydb_string_to_av_perm(struct policydb * policydb,sepol_security_class_t tclass,const char * perm_name)4089 static sepol_access_vector_t policydb_string_to_av_perm(
4090 	struct policydb *policydb,
4091 	sepol_security_class_t tclass,
4092 	const char *perm_name)
4093 {
4094 	class_datum_t *tclass_datum;
4095 	perm_datum_t *perm_datum;
4096 
4097 	if (!tclass || tclass > policydb->p_classes.nprim)
4098 		return 0;
4099 	tclass_datum = policydb->class_val_to_struct[tclass - 1];
4100 
4101 	perm_datum = (perm_datum_t *)
4102 			hashtab_search(tclass_datum->permissions.table,
4103 			perm_name);
4104 	if (perm_datum != NULL)
4105 		return UINT32_C(1) << (perm_datum->s.value - 1);
4106 
4107 	if (tclass_datum->comdatum == NULL)
4108 		return 0;
4109 
4110 	perm_datum = (perm_datum_t *)
4111 			hashtab_search(tclass_datum->comdatum->permissions.table,
4112 			perm_name);
4113 
4114 	if (perm_datum != NULL)
4115 		return UINT32_C(1) << (perm_datum->s.value - 1);
4116 
4117 	return 0;
4118 }
4119 
4120 
4121 /*
4122  * Read the configuration data from a policy database binary
4123  * representation file into a policy database structure.
4124  */
policydb_read(policydb_t * p,struct policy_file * fp,unsigned verbose)4125 int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
4126 {
4127 
4128 	unsigned int i, j, r_policyvers;
4129 	uint32_t buf[5], nprim;
4130 	size_t len, nel;
4131 	char *policydb_str;
4132 	const struct policydb_compat_info *info;
4133 	unsigned int policy_type, bufindex;
4134 	ebitmap_node_t *tnode;
4135 	int rc;
4136 
4137 	/* Read the magic number and string length. */
4138 	rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
4139 	if (rc < 0)
4140 		return POLICYDB_ERROR;
4141 	for (i = 0; i < 2; i++)
4142 		buf[i] = le32_to_cpu(buf[i]);
4143 
4144 	if (buf[0] == POLICYDB_MAGIC) {
4145 		policy_type = POLICY_KERN;
4146 	} else if (buf[0] == POLICYDB_MOD_MAGIC) {
4147 		policy_type = POLICY_MOD;
4148 	} else {
4149 		ERR(fp->handle, "policydb magic number %#08x does not "
4150 		    "match expected magic number %#08x or %#08x",
4151 		    buf[0], POLICYDB_MAGIC, POLICYDB_MOD_MAGIC);
4152 		return POLICYDB_ERROR;
4153 	}
4154 
4155 	len = buf[1];
4156 	if (len == 0 || len > POLICYDB_STRING_MAX_LENGTH) {
4157 		ERR(fp->handle, "policydb string length %s ", len ? "too long" : "zero");
4158 		return POLICYDB_ERROR;
4159 	}
4160 
4161 	policydb_str = malloc(len + 1);
4162 	if (!policydb_str) {
4163 		ERR(fp->handle, "unable to allocate memory for policydb "
4164 		    "string of length %zu", len);
4165 		return POLICYDB_ERROR;
4166 	}
4167 	rc = next_entry(policydb_str, fp, len);
4168 	if (rc < 0) {
4169 		ERR(fp->handle, "truncated policydb string identifier");
4170 		free(policydb_str);
4171 		return POLICYDB_ERROR;
4172 	}
4173 	policydb_str[len] = 0;
4174 
4175 	if (policy_type == POLICY_KERN) {
4176 		for (i = 0; i < POLICYDB_TARGET_SZ; i++) {
4177 			if ((strcmp(policydb_str, policydb_target_strings[i])
4178 				== 0)) {
4179 				policydb_set_target_platform(p, i);
4180 				break;
4181 			}
4182 		}
4183 
4184 		if (i == POLICYDB_TARGET_SZ) {
4185 			ERR(fp->handle, "cannot find a valid target for policy "
4186 				"string %s", policydb_str);
4187 			free(policydb_str);
4188 			return POLICYDB_ERROR;
4189 		}
4190 	} else {
4191 		if (strcmp(policydb_str, POLICYDB_MOD_STRING)) {
4192 			ERR(fp->handle, "invalid string identifier %s",
4193 				policydb_str);
4194 			free(policydb_str);
4195 			return POLICYDB_ERROR;
4196 		}
4197 	}
4198 
4199 	/* Done with policydb_str. */
4200 	free(policydb_str);
4201 	policydb_str = NULL;
4202 
4203 	/* Read the version, config, and table sizes (and policy type if it's a module). */
4204 	if (policy_type == POLICY_KERN)
4205 		nel = 4;
4206 	else
4207 		nel = 5;
4208 
4209 	rc = next_entry(buf, fp, sizeof(uint32_t) * nel);
4210 	if (rc < 0)
4211 		return POLICYDB_ERROR;
4212 	for (i = 0; i < nel; i++)
4213 		buf[i] = le32_to_cpu(buf[i]);
4214 
4215 	bufindex = 0;
4216 
4217 	if (policy_type == POLICY_MOD) {
4218 		/* We know it's a module but not whether it's a base
4219 		   module or regular binary policy module.  buf[0]
4220 		   tells us which. */
4221 		policy_type = buf[bufindex];
4222 		if (policy_type != POLICY_MOD && policy_type != POLICY_BASE) {
4223 			ERR(fp->handle, "unknown module type: %#08x",
4224 			    policy_type);
4225 			return POLICYDB_ERROR;
4226 		}
4227 		bufindex++;
4228 	}
4229 
4230 	r_policyvers = buf[bufindex];
4231 	if (policy_type == POLICY_KERN) {
4232 		if (r_policyvers < POLICYDB_VERSION_MIN ||
4233 		    r_policyvers > POLICYDB_VERSION_MAX) {
4234 			ERR(fp->handle, "policydb version %d does not match "
4235 			    "my version range %d-%d", buf[bufindex],
4236 			    POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
4237 			return POLICYDB_ERROR;
4238 		}
4239 	} else if (policy_type == POLICY_BASE || policy_type == POLICY_MOD) {
4240 		if (r_policyvers < MOD_POLICYDB_VERSION_MIN ||
4241 		    r_policyvers > MOD_POLICYDB_VERSION_MAX) {
4242 			ERR(fp->handle, "policydb module version %d does "
4243 			    "not match my version range %d-%d",
4244 			    buf[bufindex], MOD_POLICYDB_VERSION_MIN,
4245 			    MOD_POLICYDB_VERSION_MAX);
4246 			return POLICYDB_ERROR;
4247 		}
4248 	} else {
4249 		assert(0);
4250 	}
4251 	bufindex++;
4252 
4253 	/* Set the policy type and version from the read values. */
4254 	p->policy_type = policy_type;
4255 	p->policyvers = r_policyvers;
4256 
4257 	if (buf[bufindex] & POLICYDB_CONFIG_MLS) {
4258 		p->mls = 1;
4259 	} else {
4260 		p->mls = 0;
4261 	}
4262 
4263 	p->handle_unknown = buf[bufindex] & POLICYDB_CONFIG_UNKNOWN_MASK;
4264 
4265 	bufindex++;
4266 
4267 	info = policydb_lookup_compat(r_policyvers, policy_type,
4268 					p->target_platform);
4269 	if (!info) {
4270 		ERR(fp->handle, "unable to find policy compat info "
4271 		    "for version %d", r_policyvers);
4272 		goto bad;
4273 	}
4274 
4275 	if (buf[bufindex] != info->sym_num
4276 	    || buf[bufindex + 1] != info->ocon_num) {
4277 		ERR(fp->handle,
4278 		    "policydb table sizes (%d,%d) do not " "match mine (%d,%d)",
4279 		    buf[bufindex], buf[bufindex + 1], info->sym_num,
4280 		    info->ocon_num);
4281 		goto bad;
4282 	}
4283 
4284 	if (p->policy_type == POLICY_MOD) {
4285 		/* Get the module name and version */
4286 		if ((rc = next_entry(buf, fp, sizeof(uint32_t))) < 0) {
4287 			goto bad;
4288 		}
4289 		len = le32_to_cpu(buf[0]);
4290 		rc = str_read(&p->name, fp, len);
4291 		if (rc < 0)
4292 			goto bad;
4293 
4294 		if ((rc = next_entry(buf, fp, sizeof(uint32_t))) < 0) {
4295 			goto bad;
4296 		}
4297 		len = le32_to_cpu(buf[0]);
4298 		rc = str_read(&p->version, fp, len);
4299 		if (rc < 0)
4300 			goto bad;
4301 	}
4302 
4303 	if ((p->policyvers >= POLICYDB_VERSION_POLCAP &&
4304 	     p->policy_type == POLICY_KERN) ||
4305 	    (p->policyvers >= MOD_POLICYDB_VERSION_POLCAP &&
4306 	     p->policy_type == POLICY_BASE) ||
4307 	    (p->policyvers >= MOD_POLICYDB_VERSION_POLCAP &&
4308 	     p->policy_type == POLICY_MOD)) {
4309 		if (ebitmap_read(&p->policycaps, fp))
4310 			goto bad;
4311 	}
4312 
4313 	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
4314 	    p->policy_type == POLICY_KERN) {
4315 		if (ebitmap_read(&p->permissive_map, fp))
4316 			goto bad;
4317 	}
4318 
4319 	for (i = 0; i < info->sym_num; i++) {
4320 		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
4321 		if (rc < 0)
4322 			goto bad;
4323 		nprim = le32_to_cpu(buf[0]);
4324 		if (is_saturated(nprim) ||
4325 		    exceeds_available_bytes(fp, nprim, sizeof(uint32_t) * 3))
4326 			goto bad;
4327 		nel = le32_to_cpu(buf[1]);
4328 		if (nel && !nprim) {
4329 			ERR(fp->handle, "unexpected items in symbol table with no symbol");
4330 			goto bad;
4331 		}
4332 		for (j = 0; j < nel; j++) {
4333 			if (read_f[i] (p, p->symtab[i].table, fp))
4334 				goto bad;
4335 		}
4336 
4337 		p->symtab[i].nprim = nprim;
4338 	}
4339 
4340 	switch (p->target_platform) {
4341 	case SEPOL_TARGET_SELINUX:
4342 		p->process_class = policydb_string_to_security_class(p, "process");
4343 		p->dir_class = policydb_string_to_security_class(p, "dir");
4344 		break;
4345 	case SEPOL_TARGET_XEN:
4346 		p->process_class = policydb_string_to_security_class(p, "domain");
4347 		break;
4348 	default:
4349 		break;
4350 	}
4351 
4352 	if (policy_type == POLICY_KERN) {
4353 		if (avtab_read(&p->te_avtab, fp, r_policyvers))
4354 			goto bad;
4355 		if (r_policyvers >= POLICYDB_VERSION_BOOL)
4356 			if (cond_read_list(p, &p->cond_list, fp))
4357 				goto bad;
4358 		if (role_trans_read(p, fp))
4359 			goto bad;
4360 		if (role_allow_read(&p->role_allow, fp))
4361 			goto bad;
4362 		if (r_policyvers >= POLICYDB_VERSION_FILENAME_TRANS &&
4363 		    filename_trans_read(p, fp))
4364 			goto bad;
4365 	} else {
4366 		/* first read the AV rule blocks, then the scope tables */
4367 		avrule_block_destroy(p->global);
4368 		p->global = NULL;
4369 		if (avrule_block_read(p, &p->global, info->sym_num, fp) == -1) {
4370 			goto bad;
4371 		}
4372 		if (p->global == NULL) {
4373 			ERR(fp->handle, "no avrule block in policy");
4374 			goto bad;
4375 		}
4376 		for (i = 0; i < info->sym_num; i++) {
4377 			if ((rc = next_entry(buf, fp, sizeof(uint32_t))) < 0) {
4378 				goto bad;
4379 			}
4380 			nel = le32_to_cpu(buf[0]);
4381 			for (j = 0; j < nel; j++) {
4382 				if (scope_read(p, i, fp))
4383 					goto bad;
4384 			}
4385 		}
4386 
4387 	}
4388 
4389 	if (policydb_index_decls(fp->handle, p))
4390 		goto bad;
4391 
4392 	if (policydb_index_classes(p))
4393 		goto bad;
4394 
4395 	switch (p->target_platform) {
4396 	case SEPOL_TARGET_SELINUX:
4397 		/* fall through */
4398 	case SEPOL_TARGET_XEN:
4399 		p->process_trans = policydb_string_to_av_perm(p, p->process_class,
4400 							      "transition");
4401 		p->process_trans_dyntrans = p->process_trans |
4402 			policydb_string_to_av_perm(p, p->process_class,
4403 						   "dyntransition");
4404 		break;
4405 	default:
4406 		break;
4407 	}
4408 
4409 	if (policydb_index_others(fp->handle, p, verbose))
4410 		goto bad;
4411 
4412 	if (ocontext_read(info, p, fp) == -1) {
4413 		goto bad;
4414 	}
4415 
4416 	if (genfs_read(p, fp) == -1) {
4417 		goto bad;
4418 	}
4419 
4420 	if ((p->policy_type == POLICY_KERN
4421 	     && p->policyvers >= POLICYDB_VERSION_MLS)
4422 	    || (p->policy_type == POLICY_BASE
4423 		&& p->policyvers >= MOD_POLICYDB_VERSION_MLS
4424 		&& p->policyvers < MOD_POLICYDB_VERSION_RANGETRANS)) {
4425 		if (range_read(p, fp)) {
4426 			goto bad;
4427 		}
4428 	}
4429 
4430 	if (policy_type == POLICY_KERN) {
4431 		p->type_attr_map = calloc(p->p_types.nprim, sizeof(ebitmap_t));
4432 		p->attr_type_map = calloc(p->p_types.nprim, sizeof(ebitmap_t));
4433 		if (!p->type_attr_map || !p->attr_type_map)
4434 			goto bad;
4435 		for (i = 0; i < p->p_types.nprim; i++) {
4436 			if (r_policyvers >= POLICYDB_VERSION_AVTAB) {
4437 				if (ebitmap_read(&p->type_attr_map[i], fp))
4438 					goto bad;
4439 				ebitmap_for_each_positive_bit(&p->type_attr_map[i],
4440 							 tnode, j) {
4441 					if (i == j)
4442 						continue;
4443 
4444 					if (j >= p->p_types.nprim)
4445 						goto bad;
4446 
4447 					if (ebitmap_set_bit
4448 					    (&p->attr_type_map[j], i, 1))
4449 						goto bad;
4450 				}
4451 			}
4452 			/* add the type itself as the degenerate case */
4453 			if (p->type_val_to_struct[i] && ebitmap_set_bit(&p->type_attr_map[i], i, 1))
4454 				goto bad;
4455 			if (p->type_val_to_struct[i] && p->type_val_to_struct[i]->flavor != TYPE_ATTRIB) {
4456 				if (ebitmap_set_bit(&p->attr_type_map[i], i, 1))
4457 					goto bad;
4458 			}
4459 		}
4460 	}
4461 
4462 	if (policydb_validate(fp->handle, p))
4463 		goto bad;
4464 
4465 	return POLICYDB_SUCCESS;
4466       bad:
4467 	return POLICYDB_ERROR;
4468 }
4469 
policydb_reindex_users(policydb_t * p)4470 int policydb_reindex_users(policydb_t * p)
4471 {
4472 	unsigned int i = SYM_USERS;
4473 
4474 	if (p->user_val_to_struct)
4475 		free(p->user_val_to_struct);
4476 	if (p->sym_val_to_name[i])
4477 		free(p->sym_val_to_name[i]);
4478 
4479 	p->user_val_to_struct = (user_datum_t **)
4480 	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
4481 	if (!p->user_val_to_struct)
4482 		return -1;
4483 
4484 	p->sym_val_to_name[i] = (char **)
4485 	    calloc(p->symtab[i].nprim, sizeof(char *));
4486 	if (!p->sym_val_to_name[i])
4487 		return -1;
4488 
4489 	if (hashtab_map(p->symtab[i].table, index_f[i], p))
4490 		return -1;
4491 
4492 	/* Expand user roles for context validity checking */
4493 	if (hashtab_map(p->p_users.table, policydb_user_cache, p))
4494 		return -1;
4495 
4496 	return 0;
4497 }
4498 
policy_file_init(policy_file_t * pf)4499 void policy_file_init(policy_file_t *pf)
4500 {
4501 	memset(pf, 0, sizeof(policy_file_t));
4502 }
4503 
policydb_set_target_platform(policydb_t * p,int platform)4504 int policydb_set_target_platform(policydb_t *p, int platform)
4505 {
4506 	if (platform == SEPOL_TARGET_SELINUX)
4507 		p->target_platform = SEPOL_TARGET_SELINUX;
4508 	else if (platform == SEPOL_TARGET_XEN)
4509 		p->target_platform = SEPOL_TARGET_XEN;
4510 	else
4511 		return -1;
4512 
4513 	return 0;
4514 }
4515 
policydb_sort_ocontexts(policydb_t * p)4516 int policydb_sort_ocontexts(policydb_t *p)
4517 {
4518 	return sort_ocontexts(p);
4519 }
4520