/src/selinux/checkpolicy/policy_define.c
Line | Count | Source |
1 | | /* |
2 | | * Author : Stephen Smalley, <stephen.smalley.work@gmail.com> |
3 | | */ |
4 | | |
5 | | /* |
6 | | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> |
7 | | * |
8 | | * Support for enhanced MLS infrastructure. |
9 | | * |
10 | | * Updated: David Caplan, <dac@tresys.com> |
11 | | * |
12 | | * Added conditional policy language extensions |
13 | | * |
14 | | * Updated: Joshua Brindle <jbrindle@tresys.com> |
15 | | * Karl MacMillan <kmacmillan@mentalrootkit.com> |
16 | | * Jason Tang <jtang@tresys.com> |
17 | | * |
18 | | * Added support for binary policy modules |
19 | | * |
20 | | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
21 | | * Copyright (C) 2003 - 2008 Tresys Technology, LLC |
22 | | * Copyright (C) 2007 Red Hat Inc. |
23 | | * Copyright (C) 2017 Mellanox Techonologies Inc. |
24 | | * This program is free software; you can redistribute it and/or modify |
25 | | * it under the terms of the GNU General Public License as published by |
26 | | * the Free Software Foundation, version 2. |
27 | | */ |
28 | | |
29 | | /* FLASK */ |
30 | | |
31 | | #include <sys/types.h> |
32 | | #include <assert.h> |
33 | | #include <stdarg.h> |
34 | | #include <stdint.h> |
35 | | #include <stdio.h> |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | #include <sys/socket.h> |
39 | | #include <netinet/in.h> |
40 | | #ifndef IPPROTO_DCCP |
41 | | #define IPPROTO_DCCP 33 |
42 | | #endif |
43 | | #ifndef IPPROTO_SCTP |
44 | | #define IPPROTO_SCTP 132 |
45 | | #endif |
46 | | #include <arpa/inet.h> |
47 | | #include <limits.h> |
48 | | #include <inttypes.h> |
49 | | #include <ctype.h> |
50 | | |
51 | | #include <sepol/policydb/expand.h> |
52 | | #include <sepol/policydb/policydb.h> |
53 | | #include <sepol/policydb/services.h> |
54 | | #include <sepol/policydb/conditional.h> |
55 | | #include <sepol/policydb/hierarchy.h> |
56 | | #include <sepol/policydb/polcaps.h> |
57 | | #include "queue.h" |
58 | | #include "module_compiler.h" |
59 | | #include "policy_define.h" |
60 | | |
61 | | extern void init_parser(int pass_number, const char *input_name); |
62 | | __attribute__ ((format(printf, 1, 2))) |
63 | | extern void yyerror2(const char *fmt, ...); |
64 | | |
65 | | policydb_t *policydbp; |
66 | | queue_t id_queue = 0; |
67 | | unsigned int pass; |
68 | | int mlspol = 0; |
69 | | |
70 | | extern unsigned long policydb_lineno; |
71 | | extern unsigned long source_lineno; |
72 | | extern unsigned int policydb_errors; |
73 | | extern char source_file[PATH_MAX]; |
74 | | extern void set_source_file(const char *name); |
75 | | |
76 | | extern int yywarn(const char *msg); |
77 | | extern int yyerror(const char *msg); |
78 | | |
79 | | /* initialize all of the state variables for the scanner/parser */ |
80 | | void init_parser(int pass_number, const char *input_name) |
81 | 3.48k | { |
82 | 3.48k | policydb_lineno = 1; |
83 | 3.48k | source_lineno = 1; |
84 | 3.48k | policydb_errors = 0; |
85 | 3.48k | pass = pass_number; |
86 | 3.48k | set_source_file(input_name); |
87 | 3.48k | queue_clear(id_queue); |
88 | 3.48k | } |
89 | | |
90 | | void yyerror2(const char *fmt, ...) |
91 | 12.6k | { |
92 | 12.6k | char errormsg[256]; |
93 | 12.6k | va_list ap; |
94 | 12.6k | va_start(ap, fmt); |
95 | 12.6k | vsnprintf(errormsg, sizeof(errormsg), fmt, ap); |
96 | 12.6k | yyerror(errormsg); |
97 | 12.6k | va_end(ap); |
98 | 12.6k | } |
99 | | |
100 | | __attribute__ ((format(printf, 1, 2))) |
101 | | static void yywarn2(const char *fmt, ...) |
102 | 0 | { |
103 | 0 | char warnmsg[256]; |
104 | 0 | va_list ap; |
105 | 0 | va_start(ap, fmt); |
106 | 0 | vsnprintf(warnmsg, sizeof(warnmsg), fmt, ap); |
107 | 0 | yywarn(warnmsg); |
108 | 0 | va_end(ap); |
109 | 0 | } |
110 | | |
111 | | int insert_separator(int push) |
112 | 432k | { |
113 | 432k | int error; |
114 | | |
115 | 432k | if (push) |
116 | 47.9k | error = queue_push(id_queue, 0); |
117 | 384k | else |
118 | 384k | error = queue_insert(id_queue, 0); |
119 | | |
120 | 432k | if (error) { |
121 | 0 | yyerror("queue overflow"); |
122 | 0 | return -1; |
123 | 0 | } |
124 | 432k | return 0; |
125 | 432k | } |
126 | | |
127 | | int insert_id(const char *id, int push) |
128 | 1.49M | { |
129 | 1.49M | char *newid = 0; |
130 | 1.49M | int error; |
131 | | |
132 | 1.49M | newid = strdup(id); |
133 | 1.49M | if (!newid) { |
134 | 0 | yyerror("out of memory"); |
135 | 0 | return -1; |
136 | 0 | } |
137 | 1.49M | if (push) |
138 | 48.4k | error = queue_push(id_queue, (queue_element_t) newid); |
139 | 1.44M | else |
140 | 1.44M | error = queue_insert(id_queue, (queue_element_t) newid); |
141 | | |
142 | 1.49M | if (error) { |
143 | 0 | yyerror("queue overflow"); |
144 | 0 | free(newid); |
145 | 0 | return -1; |
146 | 0 | } |
147 | 1.49M | return 0; |
148 | 1.49M | } |
149 | | |
150 | | /* If the identifier has a dot within it and that its first character |
151 | | is not a dot then return 1, else return 0. */ |
152 | | static int id_has_dot(const char *id) |
153 | 6.40k | { |
154 | 6.40k | if (strchr(id, '.') >= id + 1) { |
155 | 3 | return 1; |
156 | 3 | } |
157 | 6.40k | return 0; |
158 | 6.40k | } |
159 | | |
160 | | int define_class(void) |
161 | 7.26k | { |
162 | 7.26k | char *id = NULL; |
163 | 7.26k | class_datum_t *datum = NULL; |
164 | 7.26k | int ret = -1; |
165 | 7.26k | uint32_t value; |
166 | | |
167 | 7.26k | if (pass == 2) { |
168 | 3.35k | id = queue_remove(id_queue); |
169 | 3.35k | free(id); |
170 | 3.35k | return 0; |
171 | 3.35k | } |
172 | | |
173 | 3.91k | id = (char *)queue_remove(id_queue); |
174 | 3.91k | if (!id) { |
175 | 0 | yyerror("no class name for class definition?"); |
176 | 0 | return -1; |
177 | 0 | } |
178 | 3.91k | datum = (class_datum_t *) malloc(sizeof(class_datum_t)); |
179 | 3.91k | if (!datum) { |
180 | 0 | yyerror("out of memory"); |
181 | 0 | goto cleanup; |
182 | 0 | } |
183 | 3.91k | memset(datum, 0, sizeof(class_datum_t)); |
184 | 3.91k | ret = declare_symbol(SYM_CLASSES, id, datum, &value, &value); |
185 | 3.91k | switch (ret) { |
186 | 0 | case -3:{ |
187 | 0 | yyerror("Out of memory!"); |
188 | 0 | goto cleanup; |
189 | 0 | } |
190 | 1 | case -2:{ |
191 | 1 | yyerror2("duplicate declaration of class %s", id); |
192 | 1 | goto cleanup; |
193 | 0 | } |
194 | 0 | case -1:{ |
195 | 0 | yyerror("could not declare class here"); |
196 | 0 | goto cleanup; |
197 | 0 | } |
198 | 3.90k | case 0: { |
199 | 3.90k | break; |
200 | 0 | } |
201 | 0 | case 1:{ |
202 | 0 | goto cleanup; |
203 | 0 | } |
204 | 0 | default:{ |
205 | 0 | assert(0); /* should never get here */ |
206 | 0 | } |
207 | 3.91k | } |
208 | 3.90k | datum->s.value = value; |
209 | 3.90k | return 0; |
210 | | |
211 | 1 | cleanup: |
212 | 1 | free(id); |
213 | 1 | free(datum); |
214 | 1 | return ret == 1 ? 0 : -1; |
215 | 3.91k | } |
216 | | |
217 | | int define_permissive(void) |
218 | 449 | { |
219 | 449 | char *type = NULL; |
220 | 449 | struct type_datum *t; |
221 | 449 | int rc = 0; |
222 | | |
223 | 449 | type = queue_remove(id_queue); |
224 | | |
225 | 449 | if (!type) { |
226 | 1 | yyerror2("forgot to include type in permissive definition?"); |
227 | 1 | rc = -1; |
228 | 1 | goto out; |
229 | 1 | } |
230 | | |
231 | 448 | if (pass == 1) |
232 | 224 | goto out; |
233 | | |
234 | 224 | if (!is_id_in_scope(SYM_TYPES, type)) { |
235 | 0 | yyerror2("type %s is not within scope", type); |
236 | 0 | rc = -1; |
237 | 0 | goto out; |
238 | 0 | } |
239 | | |
240 | 224 | t = hashtab_search(policydbp->p_types.table, type); |
241 | 224 | if (!t) { |
242 | 0 | yyerror2("type is not defined: %s", type); |
243 | 0 | rc = -1; |
244 | 0 | goto out; |
245 | 0 | } |
246 | | |
247 | 224 | if (t->flavor == TYPE_ATTRIB) { |
248 | 0 | yyerror2("attributes may not be permissive: %s", type); |
249 | 0 | rc = -1; |
250 | 0 | goto out; |
251 | 0 | } |
252 | | |
253 | 224 | t->flags |= TYPE_FLAGS_PERMISSIVE; |
254 | | |
255 | 449 | out: |
256 | 449 | free(type); |
257 | 449 | return rc; |
258 | 224 | } |
259 | | |
260 | | int define_neveraudit(void) |
261 | 1 | { |
262 | 1 | char *type = NULL; |
263 | 1 | struct type_datum *t; |
264 | 1 | int rc = 0; |
265 | | |
266 | 1 | type = queue_remove(id_queue); |
267 | | |
268 | 1 | if (!type) { |
269 | 1 | yyerror2("forgot to include type in neveraudit definition?"); |
270 | 1 | rc = -1; |
271 | 1 | goto out; |
272 | 1 | } |
273 | | |
274 | 0 | if (pass == 1) |
275 | 0 | goto out; |
276 | | |
277 | 0 | if (!is_id_in_scope(SYM_TYPES, type)) { |
278 | 0 | yyerror2("type %s is not within scope", type); |
279 | 0 | rc = -1; |
280 | 0 | goto out; |
281 | 0 | } |
282 | | |
283 | 0 | t = hashtab_search(policydbp->p_types.table, type); |
284 | 0 | if (!t) { |
285 | 0 | yyerror2("type is not defined: %s", type); |
286 | 0 | rc = -1; |
287 | 0 | goto out; |
288 | 0 | } |
289 | | |
290 | 0 | if (t->flavor == TYPE_ATTRIB) { |
291 | 0 | yyerror2("attributes may not be neveraudit: %s", type); |
292 | 0 | rc = -1; |
293 | 0 | goto out; |
294 | 0 | } |
295 | | |
296 | 0 | t->flags |= TYPE_FLAGS_NEVERAUDIT; |
297 | |
|
298 | 1 | out: |
299 | 1 | free(type); |
300 | 1 | return rc; |
301 | 0 | } |
302 | | |
303 | | int define_polcap(void) |
304 | 500 | { |
305 | 500 | char *id = 0; |
306 | 500 | int capnum; |
307 | | |
308 | 500 | if (pass == 2) { |
309 | 241 | id = queue_remove(id_queue); |
310 | 241 | free(id); |
311 | 241 | return 0; |
312 | 241 | } |
313 | | |
314 | 259 | id = (char *)queue_remove(id_queue); |
315 | 259 | if (!id) { |
316 | 0 | yyerror("no capability name for policycap definition?"); |
317 | 0 | goto bad; |
318 | 0 | } |
319 | | |
320 | | /* Check for valid cap name -> number mapping */ |
321 | 259 | capnum = sepol_polcap_getnum(id); |
322 | 259 | if (capnum < 0) { |
323 | 1 | yyerror2("invalid policy capability name %s", id); |
324 | 1 | goto bad; |
325 | 1 | } |
326 | | |
327 | | /* Store it */ |
328 | 258 | if (ebitmap_set_bit(&policydbp->policycaps, capnum, TRUE)) { |
329 | 0 | yyerror("out of memory"); |
330 | 0 | goto bad; |
331 | 0 | } |
332 | | |
333 | 258 | free(id); |
334 | 258 | return 0; |
335 | | |
336 | 1 | bad: |
337 | 1 | free(id); |
338 | 1 | return -1; |
339 | 258 | } |
340 | | |
341 | | int define_initial_sid(void) |
342 | 5.58k | { |
343 | 5.58k | char *id = 0; |
344 | 5.58k | ocontext_t *newc = 0, *c, *head; |
345 | | |
346 | 5.58k | if (pass == 2) { |
347 | 2.55k | id = queue_remove(id_queue); |
348 | 2.55k | free(id); |
349 | 2.55k | return 0; |
350 | 2.55k | } |
351 | | |
352 | 3.02k | id = (char *)queue_remove(id_queue); |
353 | 3.02k | if (!id) { |
354 | 0 | yyerror("no sid name for SID definition?"); |
355 | 0 | return -1; |
356 | 0 | } |
357 | 3.02k | newc = (ocontext_t *) malloc(sizeof(ocontext_t)); |
358 | 3.02k | if (!newc) { |
359 | 0 | yyerror("out of memory"); |
360 | 0 | goto bad; |
361 | 0 | } |
362 | 3.02k | memset(newc, 0, sizeof(ocontext_t)); |
363 | 3.02k | newc->u.name = id; |
364 | 3.02k | context_init(&newc->context[0]); |
365 | 3.02k | head = policydbp->ocontexts[OCON_ISID]; |
366 | | |
367 | 4.37k | for (c = head; c; c = c->next) { |
368 | 1.34k | if (!strcmp(newc->u.name, c->u.name)) { |
369 | 0 | yyerror2("duplicate initial SID %s", id); |
370 | 0 | goto bad; |
371 | 0 | } |
372 | 1.34k | } |
373 | | |
374 | 3.02k | if (head) { |
375 | 1.09k | newc->sid[0] = head->sid[0] + 1; |
376 | 1.93k | } else { |
377 | 1.93k | newc->sid[0] = 1; |
378 | 1.93k | } |
379 | 3.02k | newc->next = head; |
380 | 3.02k | policydbp->ocontexts[OCON_ISID] = newc; |
381 | | |
382 | 3.02k | return 0; |
383 | | |
384 | 0 | bad: |
385 | 0 | if (id) |
386 | 0 | free(id); |
387 | 0 | if (newc) |
388 | 0 | free(newc); |
389 | 0 | return -1; |
390 | 3.02k | } |
391 | | |
392 | | static int read_classes(ebitmap_t *e_classes) |
393 | 14.6k | { |
394 | 14.6k | char *id; |
395 | 14.6k | class_datum_t *cladatum; |
396 | | |
397 | 29.2k | while ((id = queue_remove(id_queue))) { |
398 | 14.6k | if (!is_id_in_scope(SYM_CLASSES, id)) { |
399 | 0 | yyerror2("class %s is not within scope", id); |
400 | 0 | free(id); |
401 | 0 | return -1; |
402 | 0 | } |
403 | 14.6k | cladatum = hashtab_search(policydbp->p_classes.table, id); |
404 | 14.6k | if (!cladatum) { |
405 | 22 | yyerror2("unknown class %s", id); |
406 | 22 | free(id); |
407 | 22 | return -1; |
408 | 22 | } |
409 | 14.6k | free(id); |
410 | 14.6k | if (ebitmap_set_bit(e_classes, cladatum->s.value - 1, TRUE)) { |
411 | 0 | yyerror("Out of memory"); |
412 | 0 | return -1; |
413 | 0 | } |
414 | 14.6k | } |
415 | 14.6k | return 0; |
416 | 14.6k | } |
417 | | |
418 | | int define_default_user(int which) |
419 | 891 | { |
420 | 891 | char *id; |
421 | 891 | class_datum_t *cladatum; |
422 | | |
423 | 891 | if (pass == 1) { |
424 | 1.34k | while ((id = queue_remove(id_queue))) |
425 | 793 | free(id); |
426 | 551 | return 0; |
427 | 551 | } |
428 | | |
429 | 690 | while ((id = queue_remove(id_queue))) { |
430 | 353 | if (!is_id_in_scope(SYM_CLASSES, id)) { |
431 | 0 | yyerror2("class %s is not within scope", id); |
432 | 0 | free(id); |
433 | 0 | return -1; |
434 | 0 | } |
435 | 353 | cladatum = hashtab_search(policydbp->p_classes.table, id); |
436 | 353 | if (!cladatum) { |
437 | 3 | yyerror2("unknown class %s", id); |
438 | 3 | free(id); |
439 | 3 | return -1; |
440 | 3 | } |
441 | 350 | if (cladatum->default_user && cladatum->default_user != which) { |
442 | 0 | yyerror2("conflicting default user information for class %s", id); |
443 | 0 | free(id); |
444 | 0 | return -1; |
445 | 0 | } |
446 | 350 | cladatum->default_user = which; |
447 | 350 | free(id); |
448 | 350 | } |
449 | | |
450 | 337 | return 0; |
451 | 340 | } |
452 | | |
453 | | int define_default_role(int which) |
454 | 2.07k | { |
455 | 2.07k | char *id; |
456 | 2.07k | class_datum_t *cladatum; |
457 | | |
458 | 2.07k | if (pass == 1) { |
459 | 2.50k | while ((id = queue_remove(id_queue))) |
460 | 1.36k | free(id); |
461 | 1.13k | return 0; |
462 | 1.13k | } |
463 | | |
464 | 1.91k | while ((id = queue_remove(id_queue))) { |
465 | 982 | if (!is_id_in_scope(SYM_CLASSES, id)) { |
466 | 0 | yyerror2("class %s is not within scope", id); |
467 | 0 | free(id); |
468 | 0 | return -1; |
469 | 0 | } |
470 | 982 | cladatum = hashtab_search(policydbp->p_classes.table, id); |
471 | 982 | if (!cladatum) { |
472 | 7 | yyerror2("unknown class %s", id); |
473 | 7 | free(id); |
474 | 7 | return -1; |
475 | 7 | } |
476 | 975 | if (cladatum->default_role && cladatum->default_role != which) { |
477 | 0 | yyerror2("conflicting default role information for class %s", id); |
478 | 0 | free(id); |
479 | 0 | return -1; |
480 | 0 | } |
481 | 975 | cladatum->default_role = which; |
482 | 975 | free(id); |
483 | 975 | } |
484 | | |
485 | 931 | return 0; |
486 | 938 | } |
487 | | |
488 | | int define_default_type(int which) |
489 | 342 | { |
490 | 342 | char *id; |
491 | 342 | class_datum_t *cladatum; |
492 | | |
493 | 342 | if (pass == 1) { |
494 | 735 | while ((id = queue_remove(id_queue))) |
495 | 467 | free(id); |
496 | 268 | return 0; |
497 | 268 | } |
498 | | |
499 | 161 | while ((id = queue_remove(id_queue))) { |
500 | 92 | if (!is_id_in_scope(SYM_CLASSES, id)) { |
501 | 0 | yyerror2("class %s is not within scope", id); |
502 | 0 | free(id); |
503 | 0 | return -1; |
504 | 0 | } |
505 | 92 | cladatum = hashtab_search(policydbp->p_classes.table, id); |
506 | 92 | if (!cladatum) { |
507 | 5 | yyerror2("unknown class %s", id); |
508 | 5 | free(id); |
509 | 5 | return -1; |
510 | 5 | } |
511 | 87 | if (cladatum->default_type && cladatum->default_type != which) { |
512 | 0 | yyerror2("conflicting default type information for class %s", id); |
513 | 0 | free(id); |
514 | 0 | return -1; |
515 | 0 | } |
516 | 87 | cladatum->default_type = which; |
517 | 87 | free(id); |
518 | 87 | } |
519 | | |
520 | 69 | return 0; |
521 | 74 | } |
522 | | |
523 | | int define_default_range(int which) |
524 | 66 | { |
525 | 66 | char *id; |
526 | 66 | class_datum_t *cladatum; |
527 | | |
528 | 66 | if (pass == 1) { |
529 | 507 | while ((id = queue_remove(id_queue))) |
530 | 441 | free(id); |
531 | 66 | return 0; |
532 | 66 | } |
533 | | |
534 | 0 | while ((id = queue_remove(id_queue))) { |
535 | 0 | if (!is_id_in_scope(SYM_CLASSES, id)) { |
536 | 0 | yyerror2("class %s is not within scope", id); |
537 | 0 | free(id); |
538 | 0 | return -1; |
539 | 0 | } |
540 | 0 | cladatum = hashtab_search(policydbp->p_classes.table, id); |
541 | 0 | if (!cladatum) { |
542 | 0 | yyerror2("unknown class %s", id); |
543 | 0 | free(id); |
544 | 0 | return -1; |
545 | 0 | } |
546 | 0 | if (cladatum->default_range && cladatum->default_range != which) { |
547 | 0 | yyerror2("conflicting default range information for class %s", id); |
548 | 0 | free(id); |
549 | 0 | return -1; |
550 | 0 | } |
551 | 0 | cladatum->default_range = which; |
552 | 0 | free(id); |
553 | 0 | } |
554 | | |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | | int define_common_perms(void) |
559 | 30 | { |
560 | 30 | char *id = 0, *perm = 0; |
561 | 30 | common_datum_t *comdatum = 0; |
562 | 30 | perm_datum_t *perdatum = 0; |
563 | 30 | int ret; |
564 | | |
565 | 30 | if (pass == 2) { |
566 | 30 | while ((id = queue_remove(id_queue))) |
567 | 20 | free(id); |
568 | 10 | return 0; |
569 | 10 | } |
570 | | |
571 | 20 | id = (char *)queue_remove(id_queue); |
572 | 20 | if (!id) { |
573 | 0 | yyerror("no common name for common perm definition?"); |
574 | 0 | return -1; |
575 | 0 | } |
576 | 20 | comdatum = hashtab_search(policydbp->p_commons.table, id); |
577 | 20 | if (comdatum) { |
578 | 0 | yyerror2("duplicate declaration for common %s", id); |
579 | 0 | free(id); |
580 | 0 | return -1; |
581 | 0 | } |
582 | 20 | comdatum = (common_datum_t *) malloc(sizeof(common_datum_t)); |
583 | 20 | if (!comdatum) { |
584 | 0 | yyerror("out of memory"); |
585 | 0 | goto bad; |
586 | 0 | } |
587 | 20 | memset(comdatum, 0, sizeof(common_datum_t)); |
588 | 20 | ret = hashtab_insert(policydbp->p_commons.table, |
589 | 20 | (hashtab_key_t) id, (hashtab_datum_t) comdatum); |
590 | | |
591 | 20 | if (ret == SEPOL_EEXIST) { |
592 | 0 | yyerror("duplicate common definition"); |
593 | 0 | goto bad; |
594 | 0 | } |
595 | 20 | if (ret == SEPOL_ENOMEM) { |
596 | 0 | yyerror("hash table overflow"); |
597 | 0 | goto bad; |
598 | 0 | } |
599 | 20 | comdatum->s.value = policydbp->p_commons.nprim + 1; |
600 | 20 | if (symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE)) { |
601 | 0 | yyerror("out of memory"); |
602 | 0 | goto bad; |
603 | 0 | } |
604 | 20 | policydbp->p_commons.nprim++; |
605 | 40 | while ((perm = queue_remove(id_queue))) { |
606 | 20 | perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t)); |
607 | 20 | if (!perdatum) { |
608 | 0 | yyerror("out of memory"); |
609 | 0 | goto bad_perm; |
610 | 0 | } |
611 | 20 | memset(perdatum, 0, sizeof(perm_datum_t)); |
612 | 20 | perdatum->s.value = comdatum->permissions.nprim + 1; |
613 | | |
614 | 20 | if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) { |
615 | 0 | yyerror2 |
616 | 0 | ("too many permissions (%d) to fit in an access vector", perdatum->s.value); |
617 | 0 | goto bad_perm; |
618 | 0 | } |
619 | 20 | ret = hashtab_insert(comdatum->permissions.table, |
620 | 20 | (hashtab_key_t) perm, |
621 | 20 | (hashtab_datum_t) perdatum); |
622 | | |
623 | 20 | if (ret == SEPOL_EEXIST) { |
624 | 0 | yyerror2("duplicate permission %s in common %s", perm, |
625 | 0 | id); |
626 | 0 | goto bad_perm; |
627 | 0 | } |
628 | 20 | if (ret == SEPOL_ENOMEM) { |
629 | 0 | yyerror("hash table overflow"); |
630 | 0 | goto bad_perm; |
631 | 0 | } |
632 | 20 | comdatum->permissions.nprim++; |
633 | 20 | } |
634 | | |
635 | 20 | return 0; |
636 | | |
637 | 0 | bad: |
638 | 0 | if (id) |
639 | 0 | free(id); |
640 | 0 | if (comdatum) |
641 | 0 | free(comdatum); |
642 | 0 | return -1; |
643 | | |
644 | 0 | bad_perm: |
645 | 0 | if (perm) |
646 | 0 | free(perm); |
647 | 0 | if (perdatum) |
648 | 0 | free(perdatum); |
649 | 0 | return -1; |
650 | 20 | } |
651 | | |
652 | | int define_av_perms(int inherits) |
653 | 3.44k | { |
654 | 3.44k | char *id; |
655 | 3.44k | class_datum_t *cladatum; |
656 | 3.44k | common_datum_t *comdatum; |
657 | 3.44k | perm_datum_t *perdatum = 0, *perdatum2 = 0; |
658 | 3.44k | int ret; |
659 | | |
660 | 3.44k | if (pass == 2) { |
661 | 7.14k | while ((id = queue_remove(id_queue))) |
662 | 5.62k | free(id); |
663 | 1.51k | return 0; |
664 | 1.51k | } |
665 | | |
666 | 1.93k | id = (char *)queue_remove(id_queue); |
667 | 1.93k | if (!id) { |
668 | 0 | yyerror("no tclass name for av perm definition?"); |
669 | 0 | return -1; |
670 | 0 | } |
671 | 1.93k | cladatum = (class_datum_t *) hashtab_search(policydbp->p_classes.table, |
672 | 1.93k | (hashtab_key_t) id); |
673 | 1.93k | if (!cladatum) { |
674 | 0 | yyerror2("class %s is not defined", id); |
675 | 0 | goto bad; |
676 | 0 | } |
677 | | |
678 | 1.93k | if (cladatum->comdatum || cladatum->permissions.nprim) { |
679 | 0 | yyerror2("duplicate access vector definition for class %s", id); |
680 | 0 | goto bad; |
681 | 0 | } |
682 | | |
683 | 1.93k | free(id); |
684 | 1.93k | id = NULL; |
685 | | |
686 | 1.93k | if (symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE)) { |
687 | 0 | yyerror("out of memory"); |
688 | 0 | return -1; |
689 | 0 | } |
690 | 1.93k | if (inherits) { |
691 | 0 | id = (char *)queue_remove(id_queue); |
692 | 0 | if (!id) { |
693 | 0 | yyerror |
694 | 0 | ("no inherits name for access vector definition?"); |
695 | 0 | return -1; |
696 | 0 | } |
697 | 0 | comdatum = |
698 | 0 | (common_datum_t *) hashtab_search(policydbp->p_commons. |
699 | 0 | table, |
700 | 0 | (hashtab_key_t) id); |
701 | |
|
702 | 0 | if (!comdatum) { |
703 | 0 | yyerror2("common %s is not defined", id); |
704 | 0 | goto bad; |
705 | 0 | } |
706 | 0 | cladatum->comkey = id; |
707 | 0 | cladatum->comdatum = comdatum; |
708 | | |
709 | | /* |
710 | | * Class-specific permissions start with values |
711 | | * after the last common permission. |
712 | | */ |
713 | 0 | cladatum->permissions.nprim += comdatum->permissions.nprim; |
714 | 0 | } |
715 | 6.53k | while ((id = queue_remove(id_queue))) { |
716 | 4.60k | perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t)); |
717 | 4.60k | if (!perdatum) { |
718 | 0 | yyerror("out of memory"); |
719 | 0 | goto bad; |
720 | 0 | } |
721 | 4.60k | memset(perdatum, 0, sizeof(perm_datum_t)); |
722 | 4.60k | perdatum->s.value = ++cladatum->permissions.nprim; |
723 | | |
724 | 4.60k | if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) { |
725 | 0 | yyerror2 |
726 | 0 | ("too many permissions (%d) to fit in an access vector", perdatum->s.value); |
727 | 0 | goto bad; |
728 | 0 | } |
729 | 4.60k | if (inherits) { |
730 | | /* |
731 | | * Class-specific permissions and |
732 | | * common permissions exist in the same |
733 | | * name space. |
734 | | */ |
735 | 0 | perdatum2 = |
736 | 0 | (perm_datum_t *) hashtab_search(cladatum->comdatum-> |
737 | 0 | permissions.table, |
738 | 0 | (hashtab_key_t) id); |
739 | 0 | if (perdatum2) { |
740 | 0 | yyerror2("permission %s conflicts with an " |
741 | 0 | "inherited permission", id); |
742 | 0 | goto bad; |
743 | 0 | } |
744 | 0 | } |
745 | 4.60k | ret = hashtab_insert(cladatum->permissions.table, |
746 | 4.60k | (hashtab_key_t) id, |
747 | 4.60k | (hashtab_datum_t) perdatum); |
748 | | |
749 | 4.60k | if (ret == SEPOL_EEXIST) { |
750 | 0 | yyerror2("duplicate permission %s", id); |
751 | 0 | goto bad; |
752 | 0 | } |
753 | 4.60k | if (ret == SEPOL_ENOMEM) { |
754 | 0 | yyerror("hash table overflow"); |
755 | 0 | goto bad; |
756 | 0 | } |
757 | 4.60k | if (add_perm_to_class(perdatum->s.value, cladatum->s.value)) { |
758 | 0 | yyerror("out of memory"); |
759 | 0 | goto bad; |
760 | 0 | } |
761 | 4.60k | } |
762 | | |
763 | 1.93k | return 0; |
764 | | |
765 | 0 | bad: |
766 | 0 | if (id) |
767 | 0 | free(id); |
768 | 0 | if (perdatum) |
769 | 0 | free(perdatum); |
770 | 0 | return -1; |
771 | 1.93k | } |
772 | | |
773 | | int define_sens(void) |
774 | 1.98k | { |
775 | 1.98k | char *id; |
776 | 1.98k | mls_level_t *level = 0; |
777 | 1.98k | level_datum_t *datum = 0, *aliasdatum = 0; |
778 | 1.98k | int ret; |
779 | 1.98k | uint32_t value; /* dummy variable -- its value is never used */ |
780 | | |
781 | 1.98k | if (!mlspol) { |
782 | 0 | yyerror("sensitivity definition in non-MLS configuration"); |
783 | 0 | return -1; |
784 | 0 | } |
785 | | |
786 | 1.98k | if (pass == 2) { |
787 | 1.93k | while ((id = queue_remove(id_queue))) |
788 | 965 | free(id); |
789 | 965 | return 0; |
790 | 965 | } |
791 | | |
792 | 1.02k | id = (char *)queue_remove(id_queue); |
793 | 1.02k | if (!id) { |
794 | 0 | yyerror("no sensitivity name for sensitivity definition?"); |
795 | 0 | return -1; |
796 | 0 | } |
797 | 1.02k | if (id_has_dot(id)) { |
798 | 0 | yyerror2("sensitivity identifier %s may not contain periods", id); |
799 | 0 | goto bad; |
800 | 0 | } |
801 | 1.02k | level = (mls_level_t *) malloc(sizeof(mls_level_t)); |
802 | 1.02k | if (!level) { |
803 | 0 | yyerror("out of memory"); |
804 | 0 | goto bad; |
805 | 0 | } |
806 | 1.02k | mls_level_init(level); |
807 | 1.02k | level->sens = 0; /* actual value set in define_dominance */ |
808 | 1.02k | ebitmap_init(&level->cat); /* actual value set in define_level */ |
809 | | |
810 | 1.02k | datum = (level_datum_t *) malloc(sizeof(level_datum_t)); |
811 | 1.02k | if (!datum) { |
812 | 0 | yyerror("out of memory"); |
813 | 0 | goto bad; |
814 | 0 | } |
815 | 1.02k | level_datum_init(datum); |
816 | 1.02k | datum->isalias = FALSE; |
817 | 1.02k | datum->level = level; |
818 | 1.02k | datum->notdefined = TRUE; |
819 | | |
820 | 1.02k | ret = declare_symbol(SYM_LEVELS, id, datum, &value, &value); |
821 | 1.02k | switch (ret) { |
822 | 0 | case -3:{ |
823 | 0 | yyerror("Out of memory!"); |
824 | 0 | goto bad; |
825 | 0 | } |
826 | 0 | case -2:{ |
827 | 0 | yyerror2("duplicate declaration of sensitivity level %s", id); |
828 | 0 | goto bad; |
829 | 0 | } |
830 | 0 | case -1:{ |
831 | 0 | yyerror2("could not declare sensitivity level %s here", id); |
832 | 0 | goto bad; |
833 | 0 | } |
834 | 1.02k | case 0: { |
835 | 1.02k | break; |
836 | 0 | } |
837 | 0 | case 1:{ |
838 | 0 | level_datum_destroy(datum); |
839 | 0 | free(datum); |
840 | 0 | free(id); |
841 | 0 | break; |
842 | 0 | } |
843 | 0 | default:{ |
844 | 0 | assert(0); /* should never get here */ |
845 | 0 | } |
846 | 1.02k | } |
847 | | |
848 | 1.02k | while ((id = queue_remove(id_queue))) { |
849 | 2 | if (id_has_dot(id)) { |
850 | 0 | yyerror2("sensitivity alias %s may not contain periods", id); |
851 | 0 | free(id); |
852 | 0 | return -1; |
853 | 0 | } |
854 | 2 | aliasdatum = (level_datum_t *) malloc(sizeof(level_datum_t)); |
855 | 2 | if (!aliasdatum) { |
856 | 0 | yyerror("out of memory"); |
857 | 0 | free(id); |
858 | 0 | return -1; |
859 | 0 | } |
860 | 2 | level_datum_init(aliasdatum); |
861 | 2 | aliasdatum->isalias = TRUE; |
862 | 2 | aliasdatum->level = level; |
863 | 2 | aliasdatum->notdefined = TRUE; |
864 | | |
865 | 2 | ret = declare_symbol(SYM_LEVELS, id, aliasdatum, NULL, &value); |
866 | 2 | switch (ret) { |
867 | 0 | case -3:{ |
868 | 0 | yyerror("Out of memory!"); |
869 | 0 | goto bad_alias; |
870 | 0 | } |
871 | 0 | case -2:{ |
872 | 0 | yyerror2 |
873 | 0 | ("duplicate declaration of sensitivity alias %s", id); |
874 | 0 | goto bad_alias; |
875 | 0 | } |
876 | 0 | case -1:{ |
877 | 0 | yyerror2 |
878 | 0 | ("could not declare sensitivity alias %s here", id); |
879 | 0 | goto bad_alias; |
880 | 0 | } |
881 | 2 | case 0: { |
882 | 2 | break; |
883 | 0 | } |
884 | 0 | case 1:{ |
885 | 0 | level_datum_destroy(aliasdatum); |
886 | 0 | free(aliasdatum); |
887 | 0 | free(id); |
888 | 0 | break; |
889 | 0 | } |
890 | 0 | default:{ |
891 | 0 | assert(0); /* should never get here */ |
892 | 0 | } |
893 | 2 | } |
894 | 2 | } |
895 | | |
896 | 1.02k | return 0; |
897 | | |
898 | 0 | bad: |
899 | 0 | if (id) |
900 | 0 | free(id); |
901 | 0 | if (level) |
902 | 0 | free(level); |
903 | 0 | if (datum) { |
904 | 0 | level_datum_destroy(datum); |
905 | 0 | free(datum); |
906 | 0 | } |
907 | 0 | return -1; |
908 | | |
909 | 0 | bad_alias: |
910 | 0 | if (id) |
911 | 0 | free(id); |
912 | 0 | if (aliasdatum) { |
913 | 0 | level_datum_destroy(aliasdatum); |
914 | 0 | free(aliasdatum); |
915 | 0 | } |
916 | 0 | return -1; |
917 | 1.02k | } |
918 | | |
919 | | int define_dominance(void) |
920 | 1.98k | { |
921 | 1.98k | level_datum_t *datum; |
922 | 1.98k | uint32_t order; |
923 | 1.98k | char *id; |
924 | | |
925 | 1.98k | if (!mlspol) { |
926 | 0 | yyerror("dominance definition in non-MLS configuration"); |
927 | 0 | return -1; |
928 | 0 | } |
929 | | |
930 | 1.98k | if (pass == 2) { |
931 | 1.93k | while ((id = queue_remove(id_queue))) |
932 | 965 | free(id); |
933 | 965 | return 0; |
934 | 965 | } |
935 | | |
936 | 1.02k | order = 0; |
937 | 2.04k | while ((id = (char *)queue_remove(id_queue))) { |
938 | 1.02k | datum = |
939 | 1.02k | (level_datum_t *) hashtab_search(policydbp->p_levels.table, |
940 | 1.02k | (hashtab_key_t) id); |
941 | 1.02k | if (!datum) { |
942 | 0 | yyerror2("unknown sensitivity %s used in dominance " |
943 | 0 | "definition", id); |
944 | 0 | free(id); |
945 | 0 | return -1; |
946 | 0 | } |
947 | 1.02k | if (datum->level->sens != 0) { |
948 | 0 | yyerror2("sensitivity %s occurs multiply in dominance " |
949 | 0 | "definition", id); |
950 | 0 | free(id); |
951 | 0 | return -1; |
952 | 0 | } |
953 | 1.02k | datum->level->sens = ++order; |
954 | | |
955 | | /* no need to keep sensitivity name */ |
956 | 1.02k | free(id); |
957 | 1.02k | } |
958 | | |
959 | 1.02k | if (order != policydbp->p_levels.nprim) { |
960 | 0 | yyerror |
961 | 0 | ("all sensitivities must be specified in dominance definition"); |
962 | 0 | return -1; |
963 | 0 | } |
964 | 1.02k | return 0; |
965 | 1.02k | } |
966 | | |
967 | | int define_category(void) |
968 | 2.01k | { |
969 | 2.01k | char *id; |
970 | 2.01k | cat_datum_t *datum = 0, *aliasdatum = 0; |
971 | 2.01k | int ret; |
972 | 2.01k | uint32_t value; |
973 | | |
974 | 2.01k | if (!mlspol) { |
975 | 0 | yyerror("category definition in non-MLS configuration"); |
976 | 0 | return -1; |
977 | 0 | } |
978 | | |
979 | 2.01k | if (pass == 2) { |
980 | 1.97k | while ((id = queue_remove(id_queue))) |
981 | 991 | free(id); |
982 | 980 | return 0; |
983 | 980 | } |
984 | | |
985 | 1.03k | id = (char *)queue_remove(id_queue); |
986 | 1.03k | if (!id) { |
987 | 0 | yyerror("no category name for category definition?"); |
988 | 0 | return -1; |
989 | 0 | } |
990 | 1.03k | if (id_has_dot(id)) { |
991 | 0 | yyerror2("category identifier %s may not contain periods", id); |
992 | 0 | goto bad; |
993 | 0 | } |
994 | 1.03k | datum = (cat_datum_t *) malloc(sizeof(cat_datum_t)); |
995 | 1.03k | if (!datum) { |
996 | 0 | yyerror("out of memory"); |
997 | 0 | goto bad; |
998 | 0 | } |
999 | 1.03k | cat_datum_init(datum); |
1000 | 1.03k | datum->isalias = FALSE; |
1001 | | |
1002 | 1.03k | ret = declare_symbol(SYM_CATS, id, datum, &value, &value); |
1003 | 1.03k | switch (ret) { |
1004 | 0 | case -3:{ |
1005 | 0 | yyerror("Out of memory!"); |
1006 | 0 | goto bad; |
1007 | 0 | } |
1008 | 0 | case -2:{ |
1009 | 0 | yyerror2("duplicate declaration of category %s", id); |
1010 | 0 | goto bad; |
1011 | 0 | } |
1012 | 0 | case -1:{ |
1013 | 0 | yyerror2("could not declare category %s here", id); |
1014 | 0 | goto bad; |
1015 | 0 | } |
1016 | 1.03k | case 0:{ |
1017 | 1.03k | datum->s.value = value; |
1018 | 1.03k | break; |
1019 | 0 | } |
1020 | 0 | case 1:{ |
1021 | 0 | cat_datum_destroy(datum); |
1022 | 0 | free(datum); |
1023 | 0 | free(id); |
1024 | 0 | break; |
1025 | 0 | } |
1026 | 0 | default:{ |
1027 | 0 | assert(0); /* should never get here */ |
1028 | 0 | } |
1029 | 1.03k | } |
1030 | | |
1031 | 1.04k | while ((id = queue_remove(id_queue))) { |
1032 | 11 | if (id_has_dot(id)) { |
1033 | 0 | yyerror2("category alias %s may not contain periods", id); |
1034 | 0 | free(id); |
1035 | 0 | return -1; |
1036 | 0 | } |
1037 | 11 | aliasdatum = (cat_datum_t *) malloc(sizeof(cat_datum_t)); |
1038 | 11 | if (!aliasdatum) { |
1039 | 0 | yyerror("out of memory"); |
1040 | 0 | free(id); |
1041 | 0 | return -1; |
1042 | 0 | } |
1043 | 11 | cat_datum_init(aliasdatum); |
1044 | 11 | aliasdatum->isalias = TRUE; |
1045 | 11 | aliasdatum->s.value = value; |
1046 | | |
1047 | 11 | ret = |
1048 | 11 | declare_symbol(SYM_CATS, id, aliasdatum, NULL, |
1049 | 11 | &value); |
1050 | 11 | switch (ret) { |
1051 | 0 | case -3:{ |
1052 | 0 | yyerror("Out of memory!"); |
1053 | 0 | goto bad_alias; |
1054 | 0 | } |
1055 | 0 | case -2:{ |
1056 | 0 | yyerror2 |
1057 | 0 | ("duplicate declaration of category alias %s", id); |
1058 | 0 | goto bad_alias; |
1059 | 0 | } |
1060 | 0 | case -1:{ |
1061 | 0 | yyerror2 |
1062 | 0 | ("could not declare category alias %s here", id); |
1063 | 0 | goto bad_alias; |
1064 | 0 | } |
1065 | 11 | case 0:{ |
1066 | 11 | break; |
1067 | 0 | } |
1068 | 0 | case 1:{ |
1069 | 0 | cat_datum_destroy(aliasdatum); |
1070 | 0 | free(aliasdatum); |
1071 | 0 | free(id); |
1072 | 0 | break; |
1073 | 0 | } |
1074 | 0 | default:{ |
1075 | 0 | assert(0); /* should never get here */ |
1076 | 0 | } |
1077 | 11 | } |
1078 | 11 | } |
1079 | | |
1080 | 1.03k | return 0; |
1081 | | |
1082 | 0 | bad: |
1083 | 0 | if (id) |
1084 | 0 | free(id); |
1085 | 0 | if (datum) { |
1086 | 0 | cat_datum_destroy(datum); |
1087 | 0 | free(datum); |
1088 | 0 | } |
1089 | 0 | return -1; |
1090 | | |
1091 | 0 | bad_alias: |
1092 | 0 | if (id) |
1093 | 0 | free(id); |
1094 | 0 | if (aliasdatum) { |
1095 | 0 | cat_datum_destroy(aliasdatum); |
1096 | 0 | free(aliasdatum); |
1097 | 0 | } |
1098 | 0 | return -1; |
1099 | 1.03k | } |
1100 | | |
1101 | | static int clone_level(hashtab_key_t key __attribute__ ((unused)), hashtab_datum_t datum, void *arg) |
1102 | 1.33k | { |
1103 | 1.33k | level_datum_t *levdatum = (level_datum_t *) datum; |
1104 | 1.33k | mls_level_t *level = (mls_level_t *) arg, *newlevel; |
1105 | | |
1106 | 1.33k | if (levdatum->notdefined && levdatum->level == level) { |
1107 | 1.02k | if (!levdatum->isalias) { |
1108 | 1.02k | levdatum->notdefined = FALSE; |
1109 | 1.02k | return 0; |
1110 | 1.02k | } |
1111 | 2 | newlevel = (mls_level_t *) malloc(sizeof(mls_level_t)); |
1112 | 2 | if (!newlevel) |
1113 | 0 | return -1; |
1114 | 2 | if (mls_level_cpy(newlevel, level)) { |
1115 | 0 | free(newlevel); |
1116 | 0 | return -1; |
1117 | 0 | } |
1118 | 2 | levdatum->level = newlevel; |
1119 | 2 | levdatum->notdefined = FALSE; |
1120 | 2 | } |
1121 | 313 | return 0; |
1122 | 1.33k | } |
1123 | | |
1124 | | int define_level(void) |
1125 | 2.23k | { |
1126 | 2.23k | char *id; |
1127 | 2.23k | level_datum_t *levdatum; |
1128 | | |
1129 | 2.23k | if (!mlspol) { |
1130 | 0 | yyerror("level definition in non-MLS configuration"); |
1131 | 0 | return -1; |
1132 | 0 | } |
1133 | | |
1134 | 2.23k | if (pass == 2) { |
1135 | 2.89k | while ((id = queue_remove(id_queue))) |
1136 | 1.91k | free(id); |
1137 | 984 | return 0; |
1138 | 984 | } |
1139 | | |
1140 | 1.24k | id = (char *)queue_remove(id_queue); |
1141 | 1.24k | if (!id) { |
1142 | 0 | yyerror("no level name for level definition?"); |
1143 | 0 | return -1; |
1144 | 0 | } |
1145 | 1.24k | levdatum = (level_datum_t *) hashtab_search(policydbp->p_levels.table, |
1146 | 1.24k | (hashtab_key_t) id); |
1147 | 1.24k | if (!levdatum) { |
1148 | 0 | yyerror2("unknown sensitivity %s used in level definition", id); |
1149 | 0 | free(id); |
1150 | 0 | return -1; |
1151 | 0 | } |
1152 | 1.24k | if (ebitmap_length(&levdatum->level->cat)) { |
1153 | 0 | yyerror2("sensitivity %s used in multiple level definitions", |
1154 | 0 | id); |
1155 | 0 | free(id); |
1156 | 0 | return -1; |
1157 | 0 | } |
1158 | 1.24k | free(id); |
1159 | | |
1160 | 2.23k | while ((id = queue_remove(id_queue))) { |
1161 | 984 | cat_datum_t *cdatum; |
1162 | 984 | uint32_t range_start, range_end, i; |
1163 | | |
1164 | 984 | if (id_has_dot(id)) { |
1165 | 0 | char *id_start = id; |
1166 | 0 | char *id_end = strchr(id, '.'); |
1167 | |
|
1168 | 0 | *(id_end++) = '\0'; |
1169 | |
|
1170 | 0 | cdatum = |
1171 | 0 | (cat_datum_t *) hashtab_search(policydbp->p_cats. |
1172 | 0 | table, |
1173 | 0 | (hashtab_key_t) |
1174 | 0 | id_start); |
1175 | 0 | if (!cdatum) { |
1176 | 0 | yyerror2("unknown category %s", id_start); |
1177 | 0 | free(id); |
1178 | 0 | return -1; |
1179 | 0 | } |
1180 | 0 | range_start = cdatum->s.value - 1; |
1181 | 0 | cdatum = |
1182 | 0 | (cat_datum_t *) hashtab_search(policydbp->p_cats. |
1183 | 0 | table, |
1184 | 0 | (hashtab_key_t) |
1185 | 0 | id_end); |
1186 | 0 | if (!cdatum) { |
1187 | 0 | yyerror2("unknown category %s", id_end); |
1188 | 0 | free(id); |
1189 | 0 | return -1; |
1190 | 0 | } |
1191 | 0 | range_end = cdatum->s.value - 1; |
1192 | |
|
1193 | 0 | if (range_end < range_start) { |
1194 | 0 | yyerror2("category range %d-%d is invalid", range_start, range_end); |
1195 | 0 | free(id); |
1196 | 0 | return -1; |
1197 | 0 | } |
1198 | 984 | } else { |
1199 | 984 | cdatum = |
1200 | 984 | (cat_datum_t *) hashtab_search(policydbp->p_cats. |
1201 | 984 | table, |
1202 | 984 | (hashtab_key_t) id); |
1203 | 984 | if (!cdatum) { |
1204 | 0 | yyerror2("unknown category %s", id); |
1205 | 0 | free(id); |
1206 | 0 | return -1; |
1207 | 0 | } |
1208 | 984 | range_start = range_end = cdatum->s.value - 1; |
1209 | 984 | } |
1210 | | |
1211 | 1.96k | for (i = range_start; i <= range_end; i++) { |
1212 | 984 | if (ebitmap_set_bit(&levdatum->level->cat, i, TRUE)) { |
1213 | 0 | yyerror("out of memory"); |
1214 | 0 | free(id); |
1215 | 0 | return -1; |
1216 | 0 | } |
1217 | 984 | } |
1218 | | |
1219 | 984 | free(id); |
1220 | 984 | } |
1221 | | |
1222 | 1.24k | if (hashtab_map |
1223 | 1.24k | (policydbp->p_levels.table, clone_level, levdatum->level)) { |
1224 | 0 | yyerror("out of memory"); |
1225 | 0 | return -1; |
1226 | 0 | } |
1227 | | |
1228 | 1.24k | return 0; |
1229 | 1.24k | } |
1230 | | |
1231 | | int define_attrib(void) |
1232 | 273 | { |
1233 | 273 | if (pass == 2) { |
1234 | 135 | free(queue_remove(id_queue)); |
1235 | 135 | return 0; |
1236 | 135 | } |
1237 | | |
1238 | 138 | if (declare_type(TRUE, TRUE) == NULL) { |
1239 | 0 | return -1; |
1240 | 0 | } |
1241 | 138 | return 0; |
1242 | 138 | } |
1243 | | |
1244 | | int expand_attrib(void) |
1245 | 110 | { |
1246 | 110 | char *id; |
1247 | 110 | ebitmap_t attrs; |
1248 | 110 | type_datum_t *attr; |
1249 | 110 | ebitmap_node_t *node; |
1250 | 110 | const char *name; |
1251 | 110 | uint32_t i; |
1252 | 110 | int rc = -1; |
1253 | 110 | int flags = 0; |
1254 | | |
1255 | 110 | if (pass == 1) { |
1256 | 321 | for (i = 0; i < 2; i++) { |
1257 | 532 | while ((id = queue_remove(id_queue))) { |
1258 | 318 | free(id); |
1259 | 318 | } |
1260 | 214 | } |
1261 | 107 | return 0; |
1262 | 107 | } |
1263 | | |
1264 | 3 | ebitmap_init(&attrs); |
1265 | 6 | while ((id = queue_remove(id_queue))) { |
1266 | 3 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1267 | 0 | yyerror2("attribute %s is not within scope", id); |
1268 | 0 | goto exit; |
1269 | 0 | } |
1270 | | |
1271 | 3 | attr = hashtab_search(policydbp->p_types.table, id); |
1272 | 3 | if (!attr) { |
1273 | 0 | yyerror2("attribute %s is not declared", id); |
1274 | 0 | goto exit; |
1275 | 0 | } |
1276 | | |
1277 | 3 | if (attr->flavor != TYPE_ATTRIB) { |
1278 | 0 | yyerror2("%s is a type, not an attribute", id); |
1279 | 0 | goto exit; |
1280 | 0 | } |
1281 | | |
1282 | 3 | if (ebitmap_set_bit(&attrs, attr->s.value - 1, TRUE)) { |
1283 | 0 | yyerror("Out of memory!"); |
1284 | 0 | goto exit; |
1285 | 0 | } |
1286 | | |
1287 | 3 | free(id); |
1288 | 3 | } |
1289 | | |
1290 | 3 | id = (char *) queue_remove(id_queue); |
1291 | 3 | if (!id) { |
1292 | 0 | yyerror("No option specified for attribute expansion."); |
1293 | 0 | goto exit; |
1294 | 0 | } |
1295 | | |
1296 | 3 | if (!strcmp(id, "T")) { |
1297 | 0 | flags = TYPE_FLAGS_EXPAND_ATTR_TRUE; |
1298 | 3 | } else { |
1299 | 3 | flags = TYPE_FLAGS_EXPAND_ATTR_FALSE; |
1300 | 3 | } |
1301 | | |
1302 | 192 | ebitmap_for_each_positive_bit(&attrs, node, i) { |
1303 | 3 | name = policydbp->sym_val_to_name[SYM_TYPES][i]; |
1304 | 3 | attr = hashtab_search(policydbp->p_types.table, name); |
1305 | 3 | attr->flags |= flags; |
1306 | 3 | if ((attr->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE) && |
1307 | 0 | (attr->flags & TYPE_FLAGS_EXPAND_ATTR_FALSE)) { |
1308 | 0 | yywarn2("Expandattribute option of attribute %s was set to both true and false; " |
1309 | 0 | "Resolving to false.", name); |
1310 | 0 | attr->flags &= ~TYPE_FLAGS_EXPAND_ATTR_TRUE; |
1311 | 0 | } |
1312 | 3 | } |
1313 | | |
1314 | 3 | rc = 0; |
1315 | 3 | exit: |
1316 | 3 | ebitmap_destroy(&attrs); |
1317 | 3 | free(id); |
1318 | 3 | return rc; |
1319 | 3 | } |
1320 | | |
1321 | | static int add_aliases_to_type(type_datum_t * type) |
1322 | 85 | { |
1323 | 85 | char *id; |
1324 | 85 | type_datum_t *aliasdatum = NULL; |
1325 | 85 | int ret; |
1326 | 316 | while ((id = queue_remove(id_queue))) { |
1327 | 231 | if (id_has_dot(id)) { |
1328 | 0 | yyerror2 |
1329 | 0 | ("type alias identifier %s may not contain periods", id); |
1330 | 0 | free(id); |
1331 | 0 | return -1; |
1332 | 0 | } |
1333 | 231 | aliasdatum = (type_datum_t *) malloc(sizeof(type_datum_t)); |
1334 | 231 | if (!aliasdatum) { |
1335 | 0 | free(id); |
1336 | 0 | yyerror("Out of memory!"); |
1337 | 0 | return -1; |
1338 | 0 | } |
1339 | 231 | memset(aliasdatum, 0, sizeof(type_datum_t)); |
1340 | 231 | aliasdatum->s.value = type->s.value; |
1341 | | |
1342 | 231 | ret = declare_symbol(SYM_TYPES, id, aliasdatum, |
1343 | 231 | NULL, &aliasdatum->s.value); |
1344 | 231 | switch (ret) { |
1345 | 0 | case -3:{ |
1346 | 0 | yyerror("Out of memory!"); |
1347 | 0 | goto cleanup; |
1348 | 0 | } |
1349 | 0 | case -2:{ |
1350 | 0 | yyerror2("duplicate declaration of alias %s", |
1351 | 0 | id); |
1352 | 0 | goto cleanup; |
1353 | 0 | } |
1354 | 0 | case -1:{ |
1355 | 0 | yyerror2("could not declare alias %s here", id); |
1356 | 0 | goto cleanup; |
1357 | 0 | } |
1358 | 231 | case 0: break; |
1359 | 0 | case 1:{ |
1360 | | /* ret == 1 means the alias was required and therefore already |
1361 | | * has a value. Set it up as an alias with a different primary. */ |
1362 | 0 | type_datum_destroy(aliasdatum); |
1363 | 0 | free(aliasdatum); |
1364 | |
|
1365 | 0 | aliasdatum = hashtab_search(policydbp->symtab[SYM_TYPES].table, id); |
1366 | 0 | assert(aliasdatum); |
1367 | |
|
1368 | 0 | aliasdatum->primary = type->s.value; |
1369 | 0 | aliasdatum->flavor = TYPE_ALIAS; |
1370 | |
|
1371 | 0 | free(id); |
1372 | 0 | break; |
1373 | 0 | } |
1374 | 0 | default:{ |
1375 | 0 | assert(0); /* should never get here */ |
1376 | 0 | } |
1377 | 231 | } |
1378 | 231 | } |
1379 | 85 | return 0; |
1380 | 0 | cleanup: |
1381 | 0 | free(id); |
1382 | 0 | type_datum_destroy(aliasdatum); |
1383 | 0 | free(aliasdatum); |
1384 | 0 | return -1; |
1385 | 85 | } |
1386 | | |
1387 | | int define_typealias(void) |
1388 | 154 | { |
1389 | 154 | char *id; |
1390 | 154 | type_datum_t *t; |
1391 | | |
1392 | 154 | if (pass == 2) { |
1393 | 357 | while ((id = queue_remove(id_queue))) |
1394 | 283 | free(id); |
1395 | 74 | return 0; |
1396 | 74 | } |
1397 | | |
1398 | 80 | id = (char *)queue_remove(id_queue); |
1399 | 80 | if (!id) { |
1400 | 0 | yyerror("no type name for typealias definition?"); |
1401 | 0 | return -1; |
1402 | 0 | } |
1403 | | |
1404 | 80 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1405 | 0 | yyerror2("type %s is not within scope", id); |
1406 | 0 | free(id); |
1407 | 0 | return -1; |
1408 | 0 | } |
1409 | 80 | t = hashtab_search(policydbp->p_types.table, id); |
1410 | 80 | if (!t || t->flavor == TYPE_ATTRIB) { |
1411 | 0 | yyerror2("unknown type %s, or it was already declared as an " |
1412 | 0 | "attribute", id); |
1413 | 0 | free(id); |
1414 | 0 | return -1; |
1415 | 0 | } |
1416 | 80 | free(id); |
1417 | 80 | return add_aliases_to_type(t); |
1418 | 80 | } |
1419 | | |
1420 | | int define_typeattribute(void) |
1421 | 63 | { |
1422 | 63 | char *id; |
1423 | 63 | type_datum_t *t, *attr; |
1424 | | |
1425 | 63 | if (pass == 2) { |
1426 | 0 | while ((id = queue_remove(id_queue))) |
1427 | 0 | free(id); |
1428 | 0 | return 0; |
1429 | 0 | } |
1430 | | |
1431 | 63 | id = (char *)queue_remove(id_queue); |
1432 | 63 | if (!id) { |
1433 | 0 | yyerror("no type name for typeattribute definition?"); |
1434 | 0 | return -1; |
1435 | 0 | } |
1436 | | |
1437 | 63 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1438 | 0 | yyerror2("type %s is not within scope", id); |
1439 | 0 | free(id); |
1440 | 0 | return -1; |
1441 | 0 | } |
1442 | 63 | t = hashtab_search(policydbp->p_types.table, id); |
1443 | 63 | if (!t) { |
1444 | 0 | yyerror2("unknown type %s", id); |
1445 | 0 | free(id); |
1446 | 0 | return -1; |
1447 | 63 | } else if (t->flavor == TYPE_ATTRIB) { |
1448 | 0 | if (policydbp->policy_type != POLICY_KERN && policydbp->policyvers < MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS) { |
1449 | 0 | yyerror2("Type attributes cannot be used in a typeattribute definition in policy version %d", policydbp->policyvers); |
1450 | 0 | free(id); |
1451 | 0 | return -1; |
1452 | 0 | } |
1453 | 0 | } |
1454 | | |
1455 | 63 | free(id); |
1456 | | |
1457 | 126 | while ((id = queue_remove(id_queue))) { |
1458 | 63 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1459 | 0 | yyerror2("attribute %s is not within scope", id); |
1460 | 0 | free(id); |
1461 | 0 | return -1; |
1462 | 0 | } |
1463 | 63 | attr = hashtab_search(policydbp->p_types.table, id); |
1464 | 63 | if (!attr) { |
1465 | | /* treat it as a fatal error */ |
1466 | 0 | yyerror2("attribute %s is not declared", id); |
1467 | 0 | free(id); |
1468 | 0 | return -1; |
1469 | 0 | } |
1470 | | |
1471 | 63 | if (attr->flavor != TYPE_ATTRIB) { |
1472 | 0 | yyerror2("%s is a type, not an attribute", id); |
1473 | 0 | free(id); |
1474 | 0 | return -1; |
1475 | 0 | } |
1476 | | |
1477 | 63 | if ((attr = get_local_type(id, attr->s.value, 1)) == NULL) { |
1478 | 0 | yyerror("Out of memory!"); |
1479 | 0 | return -1; |
1480 | 0 | } |
1481 | | |
1482 | 63 | if (ebitmap_set_bit(&attr->types, (t->s.value - 1), TRUE)) { |
1483 | 0 | yyerror("out of memory"); |
1484 | 0 | return -1; |
1485 | 0 | } |
1486 | 63 | } |
1487 | | |
1488 | 63 | return 0; |
1489 | 63 | } |
1490 | | |
1491 | | static int define_typebounds_helper(const char *bounds_id, const char *type_id) |
1492 | 1.75k | { |
1493 | 1.75k | type_datum_t *bounds, *type; |
1494 | 1.75k | char *id; |
1495 | 1.75k | uint32_t value; |
1496 | | |
1497 | 1.75k | if (!is_id_in_scope(SYM_TYPES, bounds_id)) { |
1498 | 2 | yyerror2("type %s is not within scope", bounds_id); |
1499 | 2 | return -1; |
1500 | 2 | } |
1501 | | |
1502 | 1.75k | bounds = hashtab_search(policydbp->p_types.table, bounds_id); |
1503 | 1.75k | if (!bounds || bounds->flavor == TYPE_ATTRIB) { |
1504 | 0 | yyerror2("type %s is not declared", bounds_id); |
1505 | 0 | return -1; |
1506 | 0 | } |
1507 | | |
1508 | 1.75k | if (!is_id_in_scope(SYM_TYPES, type_id)) { |
1509 | 1 | yyerror2("type %s is not within scope", type_id); |
1510 | 1 | return -1; |
1511 | 1 | } |
1512 | | |
1513 | 1.75k | type = hashtab_search(policydbp->p_types.table, type_id); |
1514 | 1.75k | if (!type || type->flavor == TYPE_ATTRIB) { |
1515 | 0 | yyerror2("type %s is not declared", type_id); |
1516 | 0 | return -1; |
1517 | 0 | } |
1518 | | |
1519 | 1.75k | id = strdup(type_id); |
1520 | 1.75k | value = (type->flavor != TYPE_ALIAS) ? type->s.value : type->primary; |
1521 | 1.75k | type = get_local_type(id, value, 0); |
1522 | 1.75k | if (!type) { |
1523 | 0 | yyerror("Out of memory!"); |
1524 | 0 | } |
1525 | | |
1526 | 1.75k | if (!type->bounds) |
1527 | 1.67k | type->bounds = bounds->s.value; |
1528 | 76 | else if (type->bounds != bounds->s.value) { |
1529 | 0 | yyerror2("type %s has inconsistent bounds %s/%s", |
1530 | 0 | type_id, |
1531 | 0 | policydbp->p_type_val_to_name[type->bounds - 1], |
1532 | 0 | policydbp->p_type_val_to_name[bounds->s.value - 1]); |
1533 | 0 | return -1; |
1534 | 0 | } |
1535 | | |
1536 | 1.75k | return 0; |
1537 | 1.75k | } |
1538 | | |
1539 | | int define_typebounds(void) |
1540 | 2.15k | { |
1541 | 2.15k | char *bounds, *id; |
1542 | | |
1543 | 2.15k | if (pass == 1) { |
1544 | 6.14k | while ((id = queue_remove(id_queue))) |
1545 | 4.56k | free(id); |
1546 | 1.57k | return 0; |
1547 | 1.57k | } |
1548 | | |
1549 | 576 | bounds = (char *) queue_remove(id_queue); |
1550 | 576 | if (!bounds) { |
1551 | 0 | yyerror("no type name for typebounds definition?"); |
1552 | 0 | return -1; |
1553 | 0 | } |
1554 | | |
1555 | 2.33k | while ((id = queue_remove(id_queue))) { |
1556 | 1.75k | if (define_typebounds_helper(bounds, id)) { |
1557 | 3 | free(bounds); |
1558 | 3 | free(id); |
1559 | 3 | return -1; |
1560 | 3 | } |
1561 | | |
1562 | 1.75k | free(id); |
1563 | 1.75k | } |
1564 | 573 | free(bounds); |
1565 | | |
1566 | 573 | return 0; |
1567 | 576 | } |
1568 | | |
1569 | | int define_type(int alias) |
1570 | 2.06k | { |
1571 | 2.06k | char *id; |
1572 | 2.06k | type_datum_t *datum, *attr; |
1573 | | |
1574 | 2.06k | if (pass == 2) { |
1575 | | /* |
1576 | | * If type name contains ".", we have to define boundary |
1577 | | * relationship implicitly to keep compatibility with |
1578 | | * old name based hierarchy. |
1579 | | */ |
1580 | 1.00k | if ((id = queue_remove(id_queue))) { |
1581 | 1.00k | const char *delim; |
1582 | | |
1583 | 1.00k | if ((delim = strrchr(id, '.'))) { |
1584 | 0 | int ret; |
1585 | 0 | char *bounds = strdup(id); |
1586 | 0 | if (!bounds) { |
1587 | 0 | yyerror("out of memory"); |
1588 | 0 | free(id); |
1589 | 0 | return -1; |
1590 | 0 | } |
1591 | | |
1592 | 0 | bounds[(size_t)(delim - id)] = '\0'; |
1593 | |
|
1594 | 0 | ret = define_typebounds_helper(bounds, id); |
1595 | 0 | free(bounds); |
1596 | 0 | if (ret) { |
1597 | 0 | free(id); |
1598 | 0 | return -1; |
1599 | 0 | } |
1600 | |
|
1601 | 0 | } |
1602 | 1.00k | free(id); |
1603 | 1.00k | } |
1604 | | |
1605 | 1.00k | if (alias) { |
1606 | 17 | while ((id = queue_remove(id_queue))) |
1607 | 12 | free(id); |
1608 | 5 | } |
1609 | | |
1610 | 1.13k | while ((id = queue_remove(id_queue))) |
1611 | 130 | free(id); |
1612 | 1.00k | return 0; |
1613 | 1.00k | } |
1614 | | |
1615 | 1.05k | if ((datum = declare_type(TRUE, FALSE)) == NULL) { |
1616 | 0 | return -1; |
1617 | 0 | } |
1618 | | |
1619 | 1.05k | if (alias) { |
1620 | 5 | if (add_aliases_to_type(datum) == -1) { |
1621 | 0 | return -1; |
1622 | 0 | } |
1623 | 5 | } |
1624 | | |
1625 | 1.32k | while ((id = queue_remove(id_queue))) { |
1626 | 264 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1627 | 0 | yyerror2("attribute %s is not within scope", id); |
1628 | 0 | free(id); |
1629 | 0 | return -1; |
1630 | 0 | } |
1631 | 264 | attr = hashtab_search(policydbp->p_types.table, id); |
1632 | 264 | if (!attr) { |
1633 | | /* treat it as a fatal error */ |
1634 | 0 | yyerror2("attribute %s is not declared", id); |
1635 | 0 | free(id); |
1636 | 0 | return -1; |
1637 | 0 | } |
1638 | | |
1639 | 264 | if (attr->flavor != TYPE_ATTRIB) { |
1640 | 0 | yyerror2("%s is a type, not an attribute", id); |
1641 | 0 | free(id); |
1642 | 0 | return -1; |
1643 | 0 | } |
1644 | | |
1645 | 264 | if ((attr = get_local_type(id, attr->s.value, 1)) == NULL) { |
1646 | 0 | yyerror("Out of memory!"); |
1647 | 0 | return -1; |
1648 | 0 | } |
1649 | | |
1650 | 264 | if (ebitmap_set_bit(&attr->types, datum->s.value - 1, TRUE)) { |
1651 | 0 | yyerror("Out of memory"); |
1652 | 0 | return -1; |
1653 | 0 | } |
1654 | 264 | } |
1655 | | |
1656 | 1.05k | return 0; |
1657 | 1.05k | } |
1658 | | |
1659 | | struct val_to_name { |
1660 | | unsigned int val; |
1661 | | char *name; |
1662 | | }; |
1663 | | |
1664 | | /* Adds a type, given by its textual name, to a typeset. If *add is |
1665 | | 0, then add the type to the negative set; otherwise if *add is 1 |
1666 | | then add it to the positive side. |
1667 | | The identifier `id` is always consumed. */ |
1668 | | static int set_types(type_set_t * set, char *id, int *add, char starallowed) |
1669 | 48.5k | { |
1670 | 48.5k | type_datum_t *t; |
1671 | | |
1672 | 48.5k | if (strcmp(id, "*") == 0) { |
1673 | 172 | free(id); |
1674 | 172 | if (!starallowed) { |
1675 | 0 | yyerror("* not allowed in this type of rule"); |
1676 | 0 | return -1; |
1677 | 0 | } |
1678 | | /* set TYPE_STAR flag */ |
1679 | 172 | set->flags = TYPE_STAR; |
1680 | 172 | *add = 1; |
1681 | 172 | return 0; |
1682 | 172 | } |
1683 | | |
1684 | 48.3k | if (strcmp(id, "~") == 0) { |
1685 | 948 | free(id); |
1686 | 948 | if (!starallowed) { |
1687 | 0 | yyerror("~ not allowed in this type of rule"); |
1688 | 0 | return -1; |
1689 | 0 | } |
1690 | | /* complement the set */ |
1691 | 948 | set->flags = TYPE_COMP; |
1692 | 948 | *add = 1; |
1693 | 948 | return 0; |
1694 | 948 | } |
1695 | | |
1696 | 47.3k | if (strcmp(id, "-") == 0) { |
1697 | 7.57k | *add = 0; |
1698 | 7.57k | free(id); |
1699 | 7.57k | return 0; |
1700 | 7.57k | } |
1701 | | |
1702 | 39.8k | if (!is_id_in_scope(SYM_TYPES, id)) { |
1703 | 4 | yyerror2("type %s is not within scope", id); |
1704 | 4 | free(id); |
1705 | 4 | return -1; |
1706 | 4 | } |
1707 | 39.8k | t = hashtab_search(policydbp->p_types.table, id); |
1708 | 39.8k | if (!t) { |
1709 | 65 | yyerror2("unknown type %s", id); |
1710 | 65 | free(id); |
1711 | 65 | return -1; |
1712 | 65 | } |
1713 | | |
1714 | 39.7k | if (*add == 0) { |
1715 | 7.56k | if (ebitmap_set_bit(&set->negset, t->s.value - 1, TRUE)) |
1716 | 0 | goto oom; |
1717 | 32.1k | } else { |
1718 | 32.1k | if (ebitmap_set_bit(&set->types, t->s.value - 1, TRUE)) |
1719 | 0 | goto oom; |
1720 | 32.1k | } |
1721 | 39.7k | free(id); |
1722 | 39.7k | *add = 1; |
1723 | 39.7k | return 0; |
1724 | 0 | oom: |
1725 | 0 | yyerror("Out of memory"); |
1726 | 0 | free(id); |
1727 | 0 | return -1; |
1728 | 39.7k | } |
1729 | | |
1730 | | static int define_compute_type_helper(int which, avrule_t ** rule) |
1731 | 473 | { |
1732 | 473 | char *id; |
1733 | 473 | type_datum_t *datum; |
1734 | 473 | ebitmap_t tclasses; |
1735 | 473 | ebitmap_node_t *node; |
1736 | 473 | avrule_t *avrule; |
1737 | 473 | class_perm_node_t *perm; |
1738 | 473 | uint32_t i; |
1739 | 473 | int add = 1; |
1740 | | |
1741 | 473 | avrule = malloc(sizeof(avrule_t)); |
1742 | 473 | if (!avrule) { |
1743 | 0 | yyerror("out of memory"); |
1744 | 0 | return -1; |
1745 | 0 | } |
1746 | 473 | avrule_init(avrule); |
1747 | 473 | avrule->specified = which; |
1748 | 473 | avrule->line = policydb_lineno; |
1749 | 473 | avrule->source_line = source_lineno; |
1750 | 473 | avrule->source_filename = strdup(source_file); |
1751 | 473 | if (!avrule->source_filename) { |
1752 | 0 | yyerror("out of memory"); |
1753 | 0 | return -1; |
1754 | 0 | } |
1755 | | |
1756 | 473 | ebitmap_init(&tclasses); |
1757 | | |
1758 | 946 | while ((id = queue_remove(id_queue))) { |
1759 | 473 | if (set_types(&avrule->stypes, id, &add, 0)) |
1760 | 0 | goto bad; |
1761 | 473 | } |
1762 | 473 | add = 1; |
1763 | 1.31k | while ((id = queue_remove(id_queue))) { |
1764 | 843 | if (strcmp(id, "self") == 0) { |
1765 | 241 | free(id); |
1766 | 241 | if (add == 0) { |
1767 | 1 | yyerror("-self is not supported"); |
1768 | 1 | goto bad; |
1769 | 1 | } |
1770 | 240 | avrule->flags |= RULE_SELF; |
1771 | 240 | continue; |
1772 | 241 | } |
1773 | 602 | if (set_types(&avrule->ttypes, id, &add, 0)) |
1774 | 4 | goto bad; |
1775 | 602 | } |
1776 | | |
1777 | 468 | if (read_classes(&tclasses)) |
1778 | 1 | goto bad; |
1779 | | |
1780 | 467 | id = (char *)queue_remove(id_queue); |
1781 | 467 | if (!id) { |
1782 | 0 | yyerror("no newtype?"); |
1783 | 0 | goto bad; |
1784 | 0 | } |
1785 | 467 | if (!is_id_in_scope(SYM_TYPES, id)) { |
1786 | 0 | yyerror2("type %s is not within scope", id); |
1787 | 0 | free(id); |
1788 | 0 | goto bad; |
1789 | 0 | } |
1790 | 467 | datum = (type_datum_t *) hashtab_search(policydbp->p_types.table, |
1791 | 467 | (hashtab_key_t) id); |
1792 | 467 | if (!datum || datum->flavor == TYPE_ATTRIB) { |
1793 | 2 | yyerror2("unknown type %s", id); |
1794 | 2 | free(id); |
1795 | 2 | goto bad; |
1796 | 2 | } |
1797 | 465 | free(id); |
1798 | | |
1799 | 29.7k | ebitmap_for_each_positive_bit(&tclasses, node, i) { |
1800 | 465 | perm = malloc(sizeof(class_perm_node_t)); |
1801 | 465 | if (!perm) { |
1802 | 0 | yyerror("out of memory"); |
1803 | 0 | goto bad; |
1804 | 0 | } |
1805 | 465 | class_perm_node_init(perm); |
1806 | 465 | perm->tclass = i + 1; |
1807 | 465 | perm->data = datum->s.value; |
1808 | 465 | perm->next = avrule->perms; |
1809 | 465 | avrule->perms = perm; |
1810 | 465 | } |
1811 | 465 | ebitmap_destroy(&tclasses); |
1812 | | |
1813 | 465 | *rule = avrule; |
1814 | 465 | return 0; |
1815 | | |
1816 | 8 | bad: |
1817 | 8 | ebitmap_destroy(&tclasses); |
1818 | 8 | avrule_destroy(avrule); |
1819 | 8 | free(avrule); |
1820 | 8 | return -1; |
1821 | 465 | } |
1822 | | |
1823 | | int define_compute_type(int which) |
1824 | 3.55k | { |
1825 | 3.55k | char *id; |
1826 | 3.55k | avrule_t *avrule; |
1827 | | |
1828 | 3.55k | if (pass == 1) { |
1829 | 6.32k | while ((id = queue_remove(id_queue))) |
1830 | 3.22k | free(id); |
1831 | 7.34k | while ((id = queue_remove(id_queue))) |
1832 | 4.24k | free(id); |
1833 | 7.33k | while ((id = queue_remove(id_queue))) |
1834 | 4.23k | free(id); |
1835 | 3.10k | id = queue_remove(id_queue); |
1836 | 3.10k | free(id); |
1837 | 3.10k | return 0; |
1838 | 3.10k | } |
1839 | | |
1840 | 455 | if (define_compute_type_helper(which, &avrule)) |
1841 | 7 | return -1; |
1842 | | |
1843 | 448 | append_avrule(avrule); |
1844 | 448 | return 0; |
1845 | 455 | } |
1846 | | |
1847 | | avrule_t *define_cond_compute_type(int which) |
1848 | 3.30k | { |
1849 | 3.30k | char *id; |
1850 | 3.30k | avrule_t *avrule; |
1851 | | |
1852 | 3.30k | if (pass == 1) { |
1853 | 5.92k | while ((id = queue_remove(id_queue))) |
1854 | 2.64k | free(id); |
1855 | 7.31k | while ((id = queue_remove(id_queue))) |
1856 | 4.03k | free(id); |
1857 | 7.85k | while ((id = queue_remove(id_queue))) |
1858 | 4.57k | free(id); |
1859 | 3.28k | id = queue_remove(id_queue); |
1860 | 3.28k | free(id); |
1861 | 3.28k | return (avrule_t *) 1; |
1862 | 3.28k | } |
1863 | | |
1864 | 18 | if (define_compute_type_helper(which, &avrule)) |
1865 | 1 | return COND_ERR; |
1866 | | |
1867 | 17 | return avrule; |
1868 | 18 | } |
1869 | | |
1870 | | int define_bool_tunable(int is_tunable) |
1871 | 20 | { |
1872 | 20 | char *id, *bool_value; |
1873 | 20 | cond_bool_datum_t *datum; |
1874 | 20 | int ret; |
1875 | 20 | uint32_t value; |
1876 | | |
1877 | 20 | if (pass == 2) { |
1878 | 30 | while ((id = queue_remove(id_queue))) |
1879 | 20 | free(id); |
1880 | 10 | return 0; |
1881 | 10 | } |
1882 | | |
1883 | 10 | id = (char *)queue_remove(id_queue); |
1884 | 10 | if (!id) { |
1885 | 0 | yyerror("no identifier for bool definition?"); |
1886 | 0 | return -1; |
1887 | 0 | } |
1888 | 10 | if (id_has_dot(id)) { |
1889 | 0 | yyerror2("boolean identifier %s may not contain periods", id); |
1890 | 0 | free(id); |
1891 | 0 | return -1; |
1892 | 0 | } |
1893 | 10 | datum = (cond_bool_datum_t *) malloc(sizeof(cond_bool_datum_t)); |
1894 | 10 | if (!datum) { |
1895 | 0 | yyerror("out of memory"); |
1896 | 0 | free(id); |
1897 | 0 | return -1; |
1898 | 0 | } |
1899 | 10 | memset(datum, 0, sizeof(cond_bool_datum_t)); |
1900 | 10 | if (is_tunable) |
1901 | 0 | datum->flags |= COND_BOOL_FLAGS_TUNABLE; |
1902 | 10 | ret = declare_symbol(SYM_BOOLS, id, datum, &value, &value); |
1903 | 10 | switch (ret) { |
1904 | 0 | case -3:{ |
1905 | 0 | yyerror("Out of memory!"); |
1906 | 0 | goto cleanup; |
1907 | 0 | } |
1908 | 0 | case -2:{ |
1909 | 0 | yyerror2("duplicate declaration of boolean %s", id); |
1910 | 0 | goto cleanup; |
1911 | 0 | } |
1912 | 0 | case -1:{ |
1913 | 0 | yyerror2("could not declare boolean %s here", id); |
1914 | 0 | goto cleanup; |
1915 | 0 | } |
1916 | 10 | case 0:{ |
1917 | 10 | break; |
1918 | 0 | } |
1919 | 0 | case 1:{ |
1920 | 0 | goto cleanup; |
1921 | 0 | } |
1922 | 0 | default:{ |
1923 | 0 | assert(0); /* should never get here */ |
1924 | 0 | } |
1925 | 10 | } |
1926 | 10 | datum->s.value = value; |
1927 | | |
1928 | 10 | bool_value = (char *)queue_remove(id_queue); |
1929 | 10 | if (!bool_value) { |
1930 | 0 | yyerror("no default value for bool definition?"); |
1931 | 0 | return -1; |
1932 | 0 | } |
1933 | | |
1934 | 10 | datum->state = (bool_value[0] == 'T') ? 1 : 0; |
1935 | 10 | free(bool_value); |
1936 | 10 | return 0; |
1937 | 0 | cleanup: |
1938 | 0 | cond_destroy_bool(id, datum, NULL); |
1939 | 0 | return ret == 1 ? 0 : -1; |
1940 | 10 | } |
1941 | | |
1942 | | avrule_t *define_cond_pol_list(avrule_t * avlist, avrule_t * sl) |
1943 | 15.0k | { |
1944 | 15.0k | avrule_t *last; |
1945 | | |
1946 | 15.0k | if (pass == 1) { |
1947 | | /* return something so we get through pass 1 */ |
1948 | 10.8k | return (avrule_t *) 1; |
1949 | 10.8k | } |
1950 | | |
1951 | 4.17k | if (sl == NULL) { |
1952 | | /* This is a require block, return previous list */ |
1953 | 281 | return avlist; |
1954 | 281 | } |
1955 | | |
1956 | | /* Prepend the new avlist to the pre-existing one. |
1957 | | * An extended permission statement might consist of multiple av |
1958 | | * rules. */ |
1959 | 5.57k | for (last = sl; last->next; last = last->next) |
1960 | 1.68k | ; |
1961 | 3.89k | last->next = avlist; |
1962 | 3.89k | return sl; |
1963 | 4.17k | } |
1964 | | |
1965 | | typedef struct av_xperm_range { |
1966 | | uint16_t low; |
1967 | | uint16_t high; |
1968 | | } av_xperm_range_t; |
1969 | | |
1970 | | struct av_xperm_range_list { |
1971 | | uint8_t omit; |
1972 | | av_xperm_range_t range; |
1973 | | struct av_xperm_range_list *next; |
1974 | | }; |
1975 | | |
1976 | | static int avrule_sort_xperms(struct av_xperm_range_list **rangehead) |
1977 | 6.33k | { |
1978 | 6.33k | struct av_xperm_range_list *r, *r2, *sorted, *sortedhead = NULL; |
1979 | | |
1980 | | /* order list by range.low */ |
1981 | 12.6k | for (r = *rangehead; r != NULL; r = r->next) { |
1982 | 6.33k | sorted = malloc(sizeof(struct av_xperm_range_list)); |
1983 | 6.33k | if (sorted == NULL) |
1984 | 0 | goto error; |
1985 | 6.33k | memcpy(sorted, r, sizeof(struct av_xperm_range_list)); |
1986 | 6.33k | sorted->next = NULL; |
1987 | 6.33k | if (sortedhead == NULL) { |
1988 | 6.33k | sortedhead = sorted; |
1989 | 6.33k | continue; |
1990 | 6.33k | } |
1991 | 0 | for (r2 = sortedhead; r2 != NULL; r2 = r2->next) { |
1992 | 0 | if (sorted->range.low < r2->range.low) { |
1993 | | /* range is the new head */ |
1994 | 0 | sorted->next = r2; |
1995 | 0 | sortedhead = sorted; |
1996 | 0 | break; |
1997 | 0 | } else if ((r2 ->next != NULL) && |
1998 | 0 | (r->range.low < r2->next->range.low)) { |
1999 | | /* insert range between elements */ |
2000 | 0 | sorted->next = r2->next; |
2001 | 0 | r2->next = sorted; |
2002 | 0 | break; |
2003 | 0 | } else if (r2->next == NULL) { |
2004 | | /* range is the new tail*/ |
2005 | 0 | r2->next = sorted; |
2006 | 0 | break; |
2007 | 0 | } |
2008 | 0 | } |
2009 | 0 | } |
2010 | | |
2011 | 6.33k | r = *rangehead; |
2012 | 12.6k | while (r != NULL) { |
2013 | 6.33k | r2 = r; |
2014 | 6.33k | r = r->next; |
2015 | 6.33k | free(r2); |
2016 | 6.33k | } |
2017 | 6.33k | *rangehead = sortedhead; |
2018 | 6.33k | return 0; |
2019 | 0 | error: |
2020 | 0 | yyerror("out of memory"); |
2021 | 0 | return -1; |
2022 | 6.33k | } |
2023 | | |
2024 | | static void avrule_merge_xperms(struct av_xperm_range_list **rangehead) |
2025 | 6.33k | { |
2026 | 6.33k | struct av_xperm_range_list *r, *tmp; |
2027 | 6.33k | r = *rangehead; |
2028 | 6.33k | while (r != NULL && r->next != NULL) { |
2029 | | /* merge */ |
2030 | 0 | if ((r->range.high + 1) >= r->next->range.low) { |
2031 | | /* keep the higher of the two */ |
2032 | 0 | if (r->range.high < r->next->range.high) |
2033 | 0 | r->range.high = r->next->range.high; |
2034 | 0 | tmp = r->next; |
2035 | 0 | r->next = r->next->next; |
2036 | 0 | free(tmp); |
2037 | 0 | continue; |
2038 | 0 | } |
2039 | 0 | r = r->next; |
2040 | 0 | } |
2041 | 6.33k | } |
2042 | | |
2043 | | static int avrule_read_xperm_ranges(struct av_xperm_range_list **rangehead) |
2044 | 6.33k | { |
2045 | 6.33k | char *id; |
2046 | 6.33k | struct av_xperm_range_list *rnew, *r = NULL; |
2047 | 6.33k | uint8_t omit = 0; |
2048 | | |
2049 | 6.33k | *rangehead = NULL; |
2050 | | |
2051 | | /* read in all the ioctl/netlink commands */ |
2052 | 16.9k | while ((id = queue_remove(id_queue))) { |
2053 | 10.5k | if (strcmp(id,"~") == 0) { |
2054 | | /* these are values to be omitted */ |
2055 | 4.23k | free(id); |
2056 | 4.23k | omit = 1; |
2057 | 6.33k | } else if (strcmp(id,"-") == 0) { |
2058 | | /* high value of range */ |
2059 | 0 | free(id); |
2060 | 0 | id = queue_remove(id_queue); |
2061 | 0 | r->range.high = (uint16_t) strtoul(id,NULL,0); |
2062 | 0 | if (r->range.high < r->range.low) { |
2063 | 0 | yyerror2("extended permission range %#x-%#x must be in ascending order.", |
2064 | 0 | r->range.low, r->range.high); |
2065 | 0 | return -1; |
2066 | 0 | } |
2067 | 0 | free(id); |
2068 | 6.33k | } else { |
2069 | | /* read in new low value */ |
2070 | 6.33k | rnew = malloc(sizeof(struct av_xperm_range_list)); |
2071 | 6.33k | if (rnew == NULL) |
2072 | 0 | goto error; |
2073 | 6.33k | rnew->next = NULL; |
2074 | 6.33k | if (*rangehead == NULL) { |
2075 | 6.33k | *rangehead = rnew; |
2076 | 6.33k | r = *rangehead; |
2077 | 6.33k | } else { |
2078 | 0 | r->next = rnew; |
2079 | 0 | r = r->next; |
2080 | 0 | } |
2081 | 6.33k | rnew->range.low = (uint16_t) strtoul(id,NULL,0); |
2082 | 6.33k | rnew->range.high = rnew->range.low; |
2083 | 6.33k | free(id); |
2084 | 6.33k | } |
2085 | 10.5k | } |
2086 | 6.33k | r = *rangehead; |
2087 | 6.33k | if (r) { |
2088 | 6.33k | r->omit = omit; |
2089 | 6.33k | } |
2090 | 6.33k | return 0; |
2091 | 0 | error: |
2092 | 0 | yyerror("out of memory"); |
2093 | 0 | return -1; |
2094 | 6.33k | } |
2095 | | |
2096 | | /* flip to included ranges */ |
2097 | | static int avrule_omit_xperms(struct av_xperm_range_list **rangehead) |
2098 | 4.23k | { |
2099 | 4.23k | struct av_xperm_range_list *rnew, *r, *newhead, *r2; |
2100 | | |
2101 | 4.23k | rnew = calloc(1, sizeof(struct av_xperm_range_list)); |
2102 | 4.23k | if (!rnew) |
2103 | 0 | goto error; |
2104 | | |
2105 | 4.23k | newhead = rnew; |
2106 | | |
2107 | 4.23k | r = *rangehead; |
2108 | 4.23k | r2 = newhead; |
2109 | | |
2110 | 4.23k | if (r->range.low == 0) { |
2111 | 1.98k | r2->range.low = r->range.high + 1; |
2112 | 1.98k | r = r->next; |
2113 | 2.24k | } else { |
2114 | 2.24k | r2->range.low = 0; |
2115 | 2.24k | } |
2116 | | |
2117 | 6.48k | while (r) { |
2118 | 2.24k | r2->range.high = r->range.low - 1; |
2119 | 2.24k | rnew = calloc(1, sizeof(struct av_xperm_range_list)); |
2120 | 2.24k | if (!rnew) |
2121 | 0 | goto error; |
2122 | 2.24k | r2->next = rnew; |
2123 | 2.24k | r2 = r2->next; |
2124 | | |
2125 | 2.24k | r2->range.low = r->range.high + 1; |
2126 | 2.24k | if (!r->next) |
2127 | 2.24k | r2->range.high = 0xffff; |
2128 | 2.24k | r = r->next; |
2129 | 2.24k | } |
2130 | | |
2131 | 4.23k | r = *rangehead; |
2132 | 8.47k | while (r != NULL) { |
2133 | 4.23k | r2 = r; |
2134 | 4.23k | r = r->next; |
2135 | 4.23k | free(r2); |
2136 | 4.23k | } |
2137 | 4.23k | *rangehead = newhead; |
2138 | 4.23k | return 0; |
2139 | | |
2140 | 0 | error: |
2141 | 0 | yyerror("out of memory"); |
2142 | 0 | return -1; |
2143 | 4.23k | } |
2144 | | |
2145 | | static int avrule_xperm_ranges(struct av_xperm_range_list **rangelist) |
2146 | 6.33k | { |
2147 | 6.33k | struct av_xperm_range_list *rangehead; |
2148 | 6.33k | uint8_t omit; |
2149 | | |
2150 | | /* read in ranges to include and omit */ |
2151 | 6.33k | if (avrule_read_xperm_ranges(&rangehead)) |
2152 | 0 | return -1; |
2153 | 6.33k | if (rangehead == NULL) { |
2154 | 0 | yyerror("error processing ioctl/netlink commands"); |
2155 | 0 | return -1; |
2156 | 0 | } |
2157 | 6.33k | omit = rangehead->omit; |
2158 | | /* sort and merge the input ranges */ |
2159 | 6.33k | if (avrule_sort_xperms(&rangehead)) |
2160 | 0 | return -1; |
2161 | 6.33k | avrule_merge_xperms(&rangehead); |
2162 | | /* flip ranges if these are omitted */ |
2163 | 6.33k | if (omit) { |
2164 | 4.23k | if (avrule_omit_xperms(&rangehead)) |
2165 | 0 | return -1; |
2166 | 4.23k | } |
2167 | | |
2168 | 6.33k | *rangelist = rangehead; |
2169 | 6.33k | return 0; |
2170 | 6.33k | } |
2171 | | |
2172 | | static int define_te_avtab_xperms_helper(int which, avrule_t ** rule) |
2173 | 6.40k | { |
2174 | 6.40k | char *id; |
2175 | 6.40k | class_perm_node_t *perms, *tail = NULL, *cur_perms = NULL; |
2176 | 6.40k | const class_datum_t *cladatum; |
2177 | 6.40k | const perm_datum_t *perdatum; |
2178 | 6.40k | ebitmap_t tclasses; |
2179 | 6.40k | ebitmap_node_t *node; |
2180 | 6.40k | avrule_t *avrule; |
2181 | 6.40k | unsigned int i; |
2182 | 6.40k | int add = 1, ret; |
2183 | | |
2184 | 6.40k | avrule = (avrule_t *) malloc(sizeof(avrule_t)); |
2185 | 6.40k | if (!avrule) { |
2186 | 0 | yyerror("out of memory"); |
2187 | 0 | goto out; |
2188 | 0 | } |
2189 | 6.40k | avrule_init(avrule); |
2190 | 6.40k | avrule->specified = which; |
2191 | 6.40k | avrule->line = policydb_lineno; |
2192 | 6.40k | avrule->source_line = source_lineno; |
2193 | 6.40k | avrule->source_filename = strdup(source_file); |
2194 | 6.40k | avrule->xperms = NULL; |
2195 | 6.40k | if (!avrule->source_filename) { |
2196 | 0 | yyerror("out of memory"); |
2197 | 0 | goto out; |
2198 | 0 | } |
2199 | | |
2200 | 12.8k | while ((id = queue_remove(id_queue))) { |
2201 | 6.42k | if (set_types |
2202 | 6.42k | (&avrule->stypes, id, &add, |
2203 | 6.42k | which == AVRULE_XPERMS_NEVERALLOW ? 1 : 0)) { |
2204 | 10 | goto out; |
2205 | 10 | } |
2206 | 6.42k | } |
2207 | 6.39k | add = 1; |
2208 | 13.0k | while ((id = queue_remove(id_queue))) { |
2209 | 6.71k | if (strcmp(id, "self") == 0) { |
2210 | 2.04k | free(id); |
2211 | 2.04k | if (add == 0 && which != AVRULE_XPERMS_NEVERALLOW) { |
2212 | 0 | yyerror("-self is only supported in neverallow and neverallowxperm rules"); |
2213 | 0 | goto out; |
2214 | 0 | } |
2215 | 2.04k | avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF); |
2216 | 2.04k | if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) { |
2217 | 0 | yyerror("self and -self are mutual exclusive"); |
2218 | 0 | goto out; |
2219 | 0 | } |
2220 | 2.04k | continue; |
2221 | 2.04k | } |
2222 | 4.66k | if (set_types |
2223 | 4.66k | (&avrule->ttypes, id, &add, |
2224 | 4.66k | which == AVRULE_XPERMS_NEVERALLOW ? 1 : 0)) { |
2225 | 8 | goto out; |
2226 | 8 | } |
2227 | 4.66k | } |
2228 | | |
2229 | 6.38k | if ((avrule->ttypes.flags & TYPE_COMP)) { |
2230 | 318 | if (avrule->flags & RULE_NOTSELF) { |
2231 | 0 | yyerror("-self is not supported in complements"); |
2232 | 0 | goto out; |
2233 | 0 | } |
2234 | 318 | if (avrule->flags & RULE_SELF) { |
2235 | 49 | avrule->flags &= ~RULE_SELF; |
2236 | 49 | avrule->flags |= RULE_NOTSELF; |
2237 | 49 | } |
2238 | 318 | } |
2239 | | |
2240 | 6.38k | ebitmap_init(&tclasses); |
2241 | 6.38k | ret = read_classes(&tclasses); |
2242 | 6.38k | if (ret) |
2243 | 7 | goto out2; |
2244 | | |
2245 | 6.37k | perms = NULL; |
2246 | 6.37k | id = queue_head(id_queue); |
2247 | 408k | ebitmap_for_each_positive_bit(&tclasses, node, i) { |
2248 | 6.37k | cur_perms = |
2249 | 6.37k | (class_perm_node_t *) malloc(sizeof(class_perm_node_t)); |
2250 | 6.37k | if (!cur_perms) { |
2251 | 0 | yyerror("out of memory"); |
2252 | 0 | goto out2; |
2253 | 0 | } |
2254 | 6.37k | class_perm_node_init(cur_perms); |
2255 | 6.37k | cur_perms->tclass = i + 1; |
2256 | 6.37k | if (!perms) |
2257 | 6.37k | perms = cur_perms; |
2258 | 6.37k | if (tail) |
2259 | 0 | tail->next = cur_perms; |
2260 | 6.37k | tail = cur_perms; |
2261 | | |
2262 | 6.37k | cladatum = policydbp->class_val_to_struct[i]; |
2263 | 6.37k | perdatum = hashtab_search(cladatum->permissions.table, id); |
2264 | 6.37k | if (!perdatum) { |
2265 | 6.25k | if (cladatum->comdatum) { |
2266 | 0 | perdatum = hashtab_search(cladatum->comdatum-> |
2267 | 0 | permissions.table, |
2268 | 0 | id); |
2269 | 0 | } |
2270 | 6.25k | } |
2271 | 6.37k | if (!perdatum) { |
2272 | 6.25k | yyerror2("permission %s is not defined" |
2273 | 6.25k | " for class %s", id, |
2274 | 6.25k | policydbp->p_class_val_to_name[i]); |
2275 | 6.25k | continue; |
2276 | 6.25k | } else if (!is_perm_in_scope (id, policydbp->p_class_val_to_name[i])) { |
2277 | 0 | yyerror2("permission %s of class %s is" |
2278 | 0 | " not within scope", id, |
2279 | 0 | policydbp->p_class_val_to_name[i]); |
2280 | 0 | continue; |
2281 | 118 | } else { |
2282 | 118 | cur_perms->data |= UINT32_C(1) << (perdatum->s.value - 1); |
2283 | 118 | } |
2284 | 6.37k | } |
2285 | | |
2286 | 6.37k | ebitmap_destroy(&tclasses); |
2287 | | |
2288 | 6.37k | avrule->perms = perms; |
2289 | 6.37k | *rule = avrule; |
2290 | 6.37k | return 0; |
2291 | | |
2292 | 7 | out2: |
2293 | 7 | ebitmap_destroy(&tclasses); |
2294 | 25 | out: |
2295 | 25 | avrule_destroy(avrule); |
2296 | 25 | free(avrule); |
2297 | 25 | return -1; |
2298 | 7 | } |
2299 | | |
2300 | | /* index of the u32 containing the permission */ |
2301 | 67.5k | #define XPERM_IDX(x) ((x) >> 5) |
2302 | | /* set bits 0 through x-1 within the u32 */ |
2303 | 14.1k | #define XPERM_SETBITS(x) ((UINT32_C(1) << ((x) & 0x1f)) - 1) |
2304 | | /* low value for this u32 */ |
2305 | 65.7k | #define XPERM_LOW(x) ((x) << 5) |
2306 | | /* high value for this u32 */ |
2307 | 52.3k | #define XPERM_HIGH(x) ((((x) + 1) << 5) - 1) |
2308 | | static void avrule_xperm_setrangebits(uint16_t low, uint16_t high, |
2309 | | av_extended_perms_t *xperms) |
2310 | 11.3k | { |
2311 | 11.3k | unsigned int i; |
2312 | 11.3k | uint16_t h = high + 1; |
2313 | | /* for each u32 that this low-high range touches, set driver permissions */ |
2314 | 56.2k | for (i = XPERM_IDX(low); i <= XPERM_IDX(high); i++) { |
2315 | | /* set all bits in u32 */ |
2316 | 44.9k | if ((low <= XPERM_LOW(i)) && (high >= XPERM_HIGH(i))) |
2317 | 34.8k | xperms->perms[i] |= ~0U; |
2318 | | /* set low bits */ |
2319 | 10.0k | else if ((low <= XPERM_LOW(i)) && (high < XPERM_HIGH(i))) |
2320 | 3.34k | xperms->perms[i] |= XPERM_SETBITS(h); |
2321 | | /* set high bits */ |
2322 | 6.73k | else if ((low > XPERM_LOW(i)) && (high >= XPERM_HIGH(i))) |
2323 | 2.70k | xperms->perms[i] |= ~0U - XPERM_SETBITS(low); |
2324 | | /* set middle bits */ |
2325 | 4.03k | else if ((low > XPERM_LOW(i)) && (high <= XPERM_HIGH(i))) |
2326 | 4.03k | xperms->perms[i] |= XPERM_SETBITS(h) - XPERM_SETBITS(low); |
2327 | 44.9k | } |
2328 | 11.3k | } |
2329 | | |
2330 | | static int avrule_xperms_used(const av_extended_perms_t *xperms) |
2331 | 22.8k | { |
2332 | 22.8k | unsigned int i; |
2333 | | |
2334 | 70.8k | for (i = 0; i < sizeof(xperms->perms)/sizeof(xperms->perms[0]); i++) { |
2335 | 66.2k | if (xperms->perms[i]) |
2336 | 18.3k | return 1; |
2337 | 66.2k | } |
2338 | 4.54k | return 0; |
2339 | 22.8k | } |
2340 | | |
2341 | | /* |
2342 | | * using definitions found in kernel document ioctl-number.txt |
2343 | | * The kernel components of an ioctl command are: |
2344 | | * dir, size, driver, and function. Only the driver and function fields |
2345 | | * are considered here |
2346 | | */ |
2347 | 48.8k | #define IOC_DRIV(x) ((x) >> 8) |
2348 | 22.4k | #define IOC_FUNC(x) ((x) & 0xff) |
2349 | 11.3k | #define IOC_CMD(driver, func) (((driver) << 8) + (func)) |
2350 | | static int avrule_xperm_partialdriver(struct av_xperm_range_list *rangelist, |
2351 | | av_extended_perms_t *complete_driver, |
2352 | | av_extended_perms_t **extended_perms) |
2353 | 6.33k | { |
2354 | 6.33k | struct av_xperm_range_list *r; |
2355 | 6.33k | av_extended_perms_t *xperms; |
2356 | 6.33k | uint8_t low, high; |
2357 | | |
2358 | 6.33k | xperms = calloc(1, sizeof(av_extended_perms_t)); |
2359 | 6.33k | if (!xperms) { |
2360 | 0 | yyerror("out of memory"); |
2361 | 0 | return -1; |
2362 | 0 | } |
2363 | | |
2364 | 6.33k | r = rangelist; |
2365 | 14.9k | while(r) { |
2366 | 8.58k | low = IOC_DRIV(r->range.low); |
2367 | 8.58k | high = IOC_DRIV(r->range.high); |
2368 | 8.58k | if (complete_driver) { |
2369 | 3.94k | if (!xperm_test(low, complete_driver->perms)) |
2370 | 1.89k | xperm_set(low, xperms->perms); |
2371 | 3.94k | if (!xperm_test(high, complete_driver->perms)) |
2372 | 1.95k | xperm_set(high, xperms->perms); |
2373 | 4.63k | } else { |
2374 | 4.63k | xperm_set(low, xperms->perms); |
2375 | 4.63k | xperm_set(high, xperms->perms); |
2376 | 4.63k | } |
2377 | 8.58k | r = r->next; |
2378 | 8.58k | } |
2379 | 6.33k | if (avrule_xperms_used(xperms)) { |
2380 | 5.90k | *extended_perms = xperms; |
2381 | 5.90k | } else { |
2382 | 432 | free(xperms); |
2383 | 432 | *extended_perms = NULL; |
2384 | 432 | } |
2385 | 6.33k | return 0; |
2386 | | |
2387 | 6.33k | } |
2388 | | |
2389 | | static int avrule_ioctl_completedriver(struct av_xperm_range_list *rangelist, |
2390 | | av_extended_perms_t **extended_perms) |
2391 | 4.10k | { |
2392 | 4.10k | struct av_xperm_range_list *r; |
2393 | 4.10k | av_extended_perms_t *xperms; |
2394 | 4.10k | uint16_t low, high; |
2395 | 4.10k | xperms = calloc(1, sizeof(av_extended_perms_t)); |
2396 | 4.10k | if (!xperms) { |
2397 | 0 | yyerror("out of memory"); |
2398 | 0 | return -1; |
2399 | 0 | } |
2400 | | |
2401 | 4.10k | r = rangelist; |
2402 | 10.1k | while(r) { |
2403 | | /* |
2404 | | * Any driver code that has sequence 0x00 - 0xff is a complete code, |
2405 | | * |
2406 | | * if command number = 0xff, then round high up to next code, |
2407 | | * else 0x00 - 0xfe keep current code |
2408 | | * of this range. temporarily u32 for the + 1 |
2409 | | * to account for possible rollover before right shift |
2410 | | */ |
2411 | 6.07k | high = IOC_DRIV((uint32_t) (r->range.high + 1)); |
2412 | | /* if 0x00 keep current driver code else 0x01 - 0xff round up to next code*/ |
2413 | 6.07k | low = IOC_DRIV(r->range.low); |
2414 | 6.07k | if (IOC_FUNC(r->range.low)) |
2415 | 3.63k | low++; |
2416 | 6.07k | if (high > low) |
2417 | 3.15k | avrule_xperm_setrangebits(low, high - 1, xperms); |
2418 | 6.07k | r = r->next; |
2419 | 6.07k | } |
2420 | 4.10k | if (avrule_xperms_used(xperms)) { |
2421 | 1.97k | xperms->driver = 0x00; |
2422 | 1.97k | xperms->specified = AVRULE_XPERMS_IOCTLDRIVER; |
2423 | 1.97k | *extended_perms = xperms; |
2424 | 2.13k | } else { |
2425 | 2.13k | free(xperms); |
2426 | 2.13k | *extended_perms = NULL; |
2427 | 2.13k | } |
2428 | 4.10k | return 0; |
2429 | 4.10k | } |
2430 | | |
2431 | | static int avrule_xperm_func(struct av_xperm_range_list *rangelist, |
2432 | | av_extended_perms_t **extended_perms, unsigned int driver, uint8_t specified) |
2433 | 6.51k | { |
2434 | 6.51k | struct av_xperm_range_list *r; |
2435 | 6.51k | av_extended_perms_t *xperms; |
2436 | 6.51k | uint16_t low, high; |
2437 | | |
2438 | 6.51k | *extended_perms = NULL; |
2439 | 6.51k | xperms = calloc(1, sizeof(av_extended_perms_t)); |
2440 | 6.51k | if (!xperms) { |
2441 | 0 | yyerror("out of memory"); |
2442 | 0 | return -1; |
2443 | 0 | } |
2444 | | |
2445 | 6.51k | r = rangelist; |
2446 | | /* for the passed in driver code, find the ranges that apply */ |
2447 | 15.4k | while (r) { |
2448 | 8.93k | low = r->range.low; |
2449 | 8.93k | high = r->range.high; |
2450 | 8.93k | if ((driver != IOC_DRIV(low)) && (driver != IOC_DRIV(high))) { |
2451 | 764 | r = r->next; |
2452 | 764 | continue; |
2453 | 764 | } |
2454 | | |
2455 | 8.17k | if (driver == IOC_DRIV(low)) { |
2456 | 6.53k | if (high > IOC_CMD(driver, 0xff)) |
2457 | 1.58k | high = IOC_CMD(driver, 0xff); |
2458 | | |
2459 | 6.53k | } else { |
2460 | 1.63k | if (low < IOC_CMD(driver, 0)) |
2461 | 1.63k | low = IOC_CMD(driver, 0); |
2462 | 1.63k | } |
2463 | | |
2464 | 8.17k | low = IOC_FUNC(low); |
2465 | 8.17k | high = IOC_FUNC(high); |
2466 | 8.17k | avrule_xperm_setrangebits(low, high, xperms); |
2467 | 8.17k | xperms->driver = driver; |
2468 | 8.17k | xperms->specified = specified; |
2469 | 8.17k | r = r->next; |
2470 | 8.17k | } |
2471 | | |
2472 | 6.51k | if (avrule_xperms_used(xperms)) { |
2473 | 4.52k | *extended_perms = xperms; |
2474 | 4.52k | } else { |
2475 | 1.98k | free(xperms); |
2476 | 1.98k | *extended_perms = NULL; |
2477 | 1.98k | } |
2478 | 6.51k | return 0; |
2479 | 6.51k | } |
2480 | | |
2481 | | static unsigned int xperms_for_each_bit(unsigned int *bit, av_extended_perms_t *xperms) |
2482 | 12.4k | { |
2483 | 12.4k | unsigned int i; |
2484 | 1.52M | for (i = *bit; i < sizeof(xperms->perms)*8; i++) { |
2485 | 1.51M | if (xperm_test(i,xperms->perms)) { |
2486 | 6.51k | xperm_clear(i, xperms->perms); |
2487 | 6.51k | *bit = i; |
2488 | 6.51k | return 1; |
2489 | 6.51k | } |
2490 | 1.51M | } |
2491 | 5.90k | return 0; |
2492 | 12.4k | } |
2493 | | |
2494 | | static int avrule_cpy(avrule_t *dest, const avrule_t *src) |
2495 | 6.49k | { |
2496 | 6.49k | class_perm_node_t *src_perms; |
2497 | 6.49k | class_perm_node_t *dest_perms, *dest_tail; |
2498 | 6.49k | dest_tail = NULL; |
2499 | | |
2500 | 6.49k | avrule_init(dest); |
2501 | 6.49k | dest->specified = src->specified; |
2502 | 6.49k | dest->flags = src->flags; |
2503 | 6.49k | if (type_set_cpy(&dest->stypes, &src->stypes)) { |
2504 | 0 | yyerror("out of memory"); |
2505 | 0 | return -1; |
2506 | 0 | } |
2507 | 6.49k | if (type_set_cpy(&dest->ttypes, &src->ttypes)) { |
2508 | 0 | yyerror("out of memory"); |
2509 | 0 | return -1; |
2510 | 0 | } |
2511 | 6.49k | dest->line = src->line; |
2512 | 6.49k | dest->source_filename = strdup(source_file); |
2513 | 6.49k | if (!dest->source_filename) { |
2514 | 0 | yyerror("out of memory"); |
2515 | 0 | return -1; |
2516 | 0 | } |
2517 | 6.49k | dest->source_line = src->source_line; |
2518 | | |
2519 | | /* increment through the class perms and copy over */ |
2520 | 6.49k | src_perms = src->perms; |
2521 | 12.9k | while (src_perms) { |
2522 | 6.49k | dest_perms = (class_perm_node_t *) calloc(1, sizeof(class_perm_node_t)); |
2523 | 6.49k | if (!dest_perms) { |
2524 | 0 | yyerror("out of memory"); |
2525 | 0 | return -1; |
2526 | 0 | } |
2527 | 6.49k | class_perm_node_init(dest_perms); |
2528 | | |
2529 | 6.49k | if (!dest->perms) |
2530 | 6.49k | dest->perms = dest_perms; |
2531 | 0 | else |
2532 | 0 | dest_tail->next = dest_perms; |
2533 | | |
2534 | 6.49k | dest_perms->tclass = src_perms->tclass; |
2535 | 6.49k | dest_perms->data = src_perms->data; |
2536 | 6.49k | dest_perms->next = NULL; |
2537 | 6.49k | dest_tail = dest_perms; |
2538 | 6.49k | src_perms = src_perms->next; |
2539 | 6.49k | } |
2540 | 6.49k | return 0; |
2541 | 6.49k | } |
2542 | | |
2543 | | static int define_te_avtab_ioctl(const avrule_t *avrule_template, avrule_t **ret_avrules) |
2544 | 4.10k | { |
2545 | 4.10k | avrule_t *avrule, *ret = NULL, **last = &ret; |
2546 | 4.10k | struct av_xperm_range_list *rangelist, *r; |
2547 | 4.10k | av_extended_perms_t *complete_driver, *partial_driver, *xperms; |
2548 | 4.10k | unsigned int i; |
2549 | | |
2550 | | |
2551 | | /* organize ioctl ranges */ |
2552 | 4.10k | if (avrule_xperm_ranges(&rangelist)) |
2553 | 0 | return -1; |
2554 | | |
2555 | | /* create rule for ioctl driver types that are entirely enabled */ |
2556 | 4.10k | if (avrule_ioctl_completedriver(rangelist, &complete_driver)) |
2557 | 0 | return -1; |
2558 | 4.10k | if (complete_driver) { |
2559 | 1.97k | avrule = (avrule_t *) calloc(1, sizeof(avrule_t)); |
2560 | 1.97k | if (!avrule) { |
2561 | 0 | yyerror("out of memory"); |
2562 | 0 | return -1; |
2563 | 0 | } |
2564 | 1.97k | if (avrule_cpy(avrule, avrule_template)) |
2565 | 0 | return -1; |
2566 | 1.97k | avrule->xperms = complete_driver; |
2567 | | |
2568 | 1.97k | if (ret_avrules) { |
2569 | 1.58k | *last = avrule; |
2570 | 1.58k | last = &(avrule->next); |
2571 | 1.58k | } else { |
2572 | 387 | append_avrule(avrule); |
2573 | 387 | } |
2574 | 1.97k | } |
2575 | | |
2576 | | /* flag ioctl driver codes that are partially enabled */ |
2577 | 4.10k | if (avrule_xperm_partialdriver(rangelist, complete_driver, &partial_driver)) |
2578 | 0 | return -1; |
2579 | | |
2580 | 4.10k | if (!partial_driver || !avrule_xperms_used(partial_driver)) |
2581 | 432 | goto done; |
2582 | | |
2583 | | /* |
2584 | | * create rule for each partially used driver codes |
2585 | | * "partially used" meaning that the code number e.g. socket 0x89 |
2586 | | * has some permission bits set and others not set. |
2587 | | */ |
2588 | 3.67k | i = 0; |
2589 | 7.34k | while (xperms_for_each_bit(&i, partial_driver)) { |
2590 | 3.67k | if (avrule_xperm_func(rangelist, &xperms, i, AVRULE_XPERMS_IOCTLFUNCTION)) |
2591 | 0 | return -1; |
2592 | | |
2593 | 3.67k | if (xperms) { |
2594 | 1.91k | avrule = (avrule_t *) calloc(1, sizeof(avrule_t)); |
2595 | 1.91k | if (!avrule) { |
2596 | 0 | yyerror("out of memory"); |
2597 | 0 | return -1; |
2598 | 0 | } |
2599 | 1.91k | if (avrule_cpy(avrule, avrule_template)) |
2600 | 0 | return -1; |
2601 | 1.91k | avrule->xperms = xperms; |
2602 | | |
2603 | 1.91k | if (ret_avrules) { |
2604 | 1.15k | *last = avrule; |
2605 | 1.15k | last = &(avrule->next); |
2606 | 1.15k | } else { |
2607 | 761 | append_avrule(avrule); |
2608 | 761 | } |
2609 | 1.91k | } |
2610 | 3.67k | } |
2611 | | |
2612 | 4.10k | done: |
2613 | 4.10k | if (partial_driver) |
2614 | 3.67k | free(partial_driver); |
2615 | | |
2616 | 10.1k | while (rangelist != NULL) { |
2617 | 6.07k | r = rangelist; |
2618 | 6.07k | rangelist = rangelist->next; |
2619 | 6.07k | free(r); |
2620 | 6.07k | } |
2621 | | |
2622 | 4.10k | if (ret_avrules) |
2623 | 1.64k | *ret_avrules = ret; |
2624 | | |
2625 | 4.10k | return 0; |
2626 | 3.67k | } |
2627 | | |
2628 | | static int define_te_avtab_netlink(const avrule_t *avrule_template, avrule_t **ret_avrules) |
2629 | 2.23k | { |
2630 | 2.23k | avrule_t *avrule, *ret = NULL, **last = &ret; |
2631 | 2.23k | struct av_xperm_range_list *rangelist, *r; |
2632 | 2.23k | av_extended_perms_t *partial_driver, *xperms; |
2633 | 2.23k | unsigned int i; |
2634 | | |
2635 | | /* organize ranges */ |
2636 | 2.23k | if (avrule_xperm_ranges(&rangelist)) |
2637 | 0 | return -1; |
2638 | | |
2639 | | /* flag driver codes that are partially enabled */ |
2640 | 2.23k | if (avrule_xperm_partialdriver(rangelist, NULL, &partial_driver)) |
2641 | 0 | return -1; |
2642 | | |
2643 | 2.23k | if (!partial_driver || !avrule_xperms_used(partial_driver)) |
2644 | 0 | goto done; |
2645 | | |
2646 | | /* |
2647 | | * create rule for each partially used driver codes |
2648 | | * "partially used" meaning that the code number e.g. socket 0x89 |
2649 | | * has some permission bits set and others not set. |
2650 | | */ |
2651 | 2.23k | i = 0; |
2652 | 5.07k | while (xperms_for_each_bit(&i, partial_driver)) { |
2653 | 2.83k | if (avrule_xperm_func(rangelist, &xperms, i, AVRULE_XPERMS_NLMSG)) |
2654 | 0 | return -1; |
2655 | | |
2656 | 2.83k | if (xperms) { |
2657 | 2.60k | avrule = (avrule_t *) calloc(1, sizeof(avrule_t)); |
2658 | 2.60k | if (!avrule) { |
2659 | 0 | yyerror("out of memory"); |
2660 | 0 | return -1; |
2661 | 0 | } |
2662 | 2.60k | if (avrule_cpy(avrule, avrule_template)) |
2663 | 0 | return -1; |
2664 | 2.60k | avrule->xperms = xperms; |
2665 | | |
2666 | 2.60k | if (ret_avrules) { |
2667 | 769 | *last = avrule; |
2668 | 769 | last = &(avrule->next); |
2669 | 1.83k | } else { |
2670 | 1.83k | append_avrule(avrule); |
2671 | 1.83k | } |
2672 | 2.60k | } |
2673 | 2.83k | } |
2674 | | |
2675 | 2.23k | done: |
2676 | 2.23k | if (partial_driver) |
2677 | 2.23k | free(partial_driver); |
2678 | | |
2679 | 4.73k | while (rangelist != NULL) { |
2680 | 2.50k | r = rangelist; |
2681 | 2.50k | rangelist = rangelist->next; |
2682 | 2.50k | free(r); |
2683 | 2.50k | } |
2684 | | |
2685 | 2.23k | if (ret_avrules) |
2686 | 439 | *ret_avrules = ret; |
2687 | | |
2688 | 2.23k | return 0; |
2689 | 2.23k | } |
2690 | | |
2691 | | avrule_t *define_cond_te_avtab_extended_perms(int which) |
2692 | 5.54k | { |
2693 | 5.54k | char *id; |
2694 | 5.54k | unsigned int i; |
2695 | 5.54k | avrule_t *avrule_template, *rules = NULL; |
2696 | 5.54k | int rc = 0; |
2697 | | |
2698 | 5.54k | if (policydbp->policy_type == POLICY_KERN && policydbp->policyvers < POLICYDB_VERSION_COND_XPERMS) { |
2699 | 0 | yyerror2("extended permissions in conditional policies are only supported since policy version %d, found policy version %d", |
2700 | 0 | POLICYDB_VERSION_COND_XPERMS, policydbp->policyvers); |
2701 | 0 | return COND_ERR; |
2702 | 0 | } |
2703 | 5.54k | if (policydbp->policy_type != POLICY_KERN && policydbp->policyvers < MOD_POLICYDB_VERSION_COND_XPERMS) { |
2704 | 0 | yyerror2("extended permissions in conditional policies are only supported since module policy version %d, found module policy version %d", |
2705 | 0 | MOD_POLICYDB_VERSION_COND_XPERMS, policydbp->policyvers); |
2706 | 0 | return COND_ERR; |
2707 | 0 | } |
2708 | | |
2709 | 5.54k | if (pass == 1) { |
2710 | 17.1k | for (i = 0; i < 4; i++) { |
2711 | 34.4k | while ((id = queue_remove(id_queue))) |
2712 | 20.6k | free(id); |
2713 | 13.7k | } |
2714 | 3.43k | return (avrule_t *) 1; /* any non-NULL value */ |
2715 | 3.43k | } |
2716 | | |
2717 | | /* populate avrule template with source/target/tclass */ |
2718 | 2.11k | if (define_te_avtab_xperms_helper(which, &avrule_template)) |
2719 | 17 | return COND_ERR; |
2720 | | |
2721 | 2.09k | id = queue_remove(id_queue); |
2722 | 2.09k | if (strcmp(id, "ioctl") == 0) { |
2723 | 1.64k | rc = define_te_avtab_ioctl(avrule_template, &rules); |
2724 | 1.64k | } else if (strcmp(id, "nlmsg") == 0) { |
2725 | 439 | rc = define_te_avtab_netlink(avrule_template, &rules); |
2726 | 439 | } else { |
2727 | 12 | yyerror2("only ioctl and nlmsg extended permissions are supported, found %s", id); |
2728 | 12 | rc = -1; |
2729 | 12 | } |
2730 | | |
2731 | 2.09k | free(id); |
2732 | 2.09k | avrule_destroy(avrule_template); |
2733 | 2.09k | free(avrule_template); |
2734 | | |
2735 | 2.09k | if (rc) { |
2736 | 12 | avrule_destroy(rules); |
2737 | 12 | return NULL; |
2738 | 12 | } |
2739 | | |
2740 | 2.08k | return rules; |
2741 | 2.09k | } |
2742 | | |
2743 | | int define_te_avtab_extended_perms(int which) |
2744 | 9.73k | { |
2745 | 9.73k | char *id; |
2746 | 9.73k | unsigned int i; |
2747 | 9.73k | avrule_t *avrule_template; |
2748 | 9.73k | int rc = 0; |
2749 | | |
2750 | 9.73k | if (pass == 1) { |
2751 | 27.2k | for (i = 0; i < 4; i++) { |
2752 | 52.0k | while ((id = queue_remove(id_queue))) |
2753 | 30.2k | free(id); |
2754 | 21.8k | } |
2755 | 5.45k | return 0; |
2756 | 5.45k | } |
2757 | | |
2758 | | /* populate avrule template with source/target/tclass */ |
2759 | 4.28k | if (define_te_avtab_xperms_helper(which, &avrule_template)) |
2760 | 8 | return -1; |
2761 | | |
2762 | 4.28k | id = queue_remove(id_queue); |
2763 | 4.28k | if (strcmp(id,"ioctl") == 0) { |
2764 | 2.45k | rc = define_te_avtab_ioctl(avrule_template, NULL); |
2765 | 2.45k | } else if (strcmp(id,"nlmsg") == 0) { |
2766 | 1.79k | rc = define_te_avtab_netlink(avrule_template, NULL); |
2767 | 1.79k | } else { |
2768 | 30 | yyerror2("only ioctl and nlmsg extended permissions are supported, found %s", id); |
2769 | 30 | rc = -1; |
2770 | 30 | } |
2771 | | |
2772 | 4.28k | free(id); |
2773 | 4.28k | avrule_destroy(avrule_template); |
2774 | 4.28k | free(avrule_template); |
2775 | | |
2776 | 4.28k | return rc; |
2777 | 4.28k | } |
2778 | | |
2779 | 4.76k | #define PERMISSION_MASK(nprim) ((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : ((UINT32_C(1) << (nprim)) - 1)) |
2780 | | |
2781 | | static int define_te_avtab_helper(int which, avrule_t ** rule) |
2782 | 5.37k | { |
2783 | 5.37k | char *id; |
2784 | 5.37k | class_datum_t *cladatum; |
2785 | 5.37k | perm_datum_t *perdatum = NULL; |
2786 | 5.37k | class_perm_node_t *perms, *tail = NULL, *cur_perms = NULL; |
2787 | 5.37k | ebitmap_t tclasses; |
2788 | 5.37k | ebitmap_node_t *node; |
2789 | 5.37k | avrule_t *avrule; |
2790 | 5.37k | unsigned int i; |
2791 | 5.37k | int add = 1, ret = 0; |
2792 | 5.37k | int suppress = 0; |
2793 | | |
2794 | 5.37k | ebitmap_init(&tclasses); |
2795 | | |
2796 | 5.37k | avrule = (avrule_t *) malloc(sizeof(avrule_t)); |
2797 | 5.37k | if (!avrule) { |
2798 | 0 | yyerror("memory error"); |
2799 | 0 | ret = -1; |
2800 | 0 | goto out; |
2801 | 0 | } |
2802 | 5.37k | avrule_init(avrule); |
2803 | 5.37k | avrule->specified = which; |
2804 | 5.37k | avrule->line = policydb_lineno; |
2805 | 5.37k | avrule->source_line = source_lineno; |
2806 | 5.37k | avrule->source_filename = strdup(source_file); |
2807 | 5.37k | avrule->xperms = NULL; |
2808 | 5.37k | if (!avrule->source_filename) { |
2809 | 0 | yyerror("out of memory"); |
2810 | 0 | return -1; |
2811 | 0 | } |
2812 | | |
2813 | | |
2814 | 10.9k | while ((id = queue_remove(id_queue))) { |
2815 | 5.55k | if (set_types |
2816 | 5.55k | (&avrule->stypes, id, &add, |
2817 | 5.55k | which == AVRULE_NEVERALLOW ? 1 : 0)) { |
2818 | 10 | ret = -1; |
2819 | 10 | goto out; |
2820 | 10 | } |
2821 | 5.55k | } |
2822 | 5.36k | add = 1; |
2823 | 11.4k | while ((id = queue_remove(id_queue))) { |
2824 | 6.07k | if (strcmp(id, "self") == 0) { |
2825 | 634 | free(id); |
2826 | 634 | if (add == 0 && which != AVRULE_NEVERALLOW) { |
2827 | 0 | yyerror("-self is only supported in neverallow and neverallowxperm rules"); |
2828 | 0 | ret = -1; |
2829 | 0 | goto out; |
2830 | 0 | } |
2831 | 634 | avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF); |
2832 | 634 | if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) { |
2833 | 0 | yyerror("self and -self are mutual exclusive"); |
2834 | 0 | ret = -1; |
2835 | 0 | goto out; |
2836 | 0 | } |
2837 | 634 | continue; |
2838 | 634 | } |
2839 | 5.43k | if (set_types |
2840 | 5.43k | (&avrule->ttypes, id, &add, |
2841 | 5.43k | which == AVRULE_NEVERALLOW ? 1 : 0)) { |
2842 | 6 | ret = -1; |
2843 | 6 | goto out; |
2844 | 6 | } |
2845 | 5.43k | } |
2846 | | |
2847 | 5.35k | if ((avrule->ttypes.flags & TYPE_COMP)) { |
2848 | 449 | if (avrule->flags & RULE_NOTSELF) { |
2849 | 0 | yyerror("-self is not supported in complements"); |
2850 | 0 | ret = -1; |
2851 | 0 | goto out; |
2852 | 0 | } |
2853 | 449 | if (avrule->flags & RULE_SELF) { |
2854 | 15 | avrule->flags &= ~RULE_SELF; |
2855 | 15 | avrule->flags |= RULE_NOTSELF; |
2856 | 15 | } |
2857 | 449 | } |
2858 | | |
2859 | 5.35k | ret = read_classes(&tclasses); |
2860 | 5.35k | if (ret) |
2861 | 9 | goto out; |
2862 | | |
2863 | 5.34k | perms = NULL; |
2864 | 342k | ebitmap_for_each_positive_bit(&tclasses, node, i) { |
2865 | 5.34k | cur_perms = |
2866 | 5.34k | (class_perm_node_t *) malloc(sizeof(class_perm_node_t)); |
2867 | 5.34k | if (!cur_perms) { |
2868 | 0 | yyerror("out of memory"); |
2869 | 0 | ret = -1; |
2870 | 0 | goto out; |
2871 | 0 | } |
2872 | 5.34k | class_perm_node_init(cur_perms); |
2873 | 5.34k | cur_perms->tclass = i + 1; |
2874 | 5.34k | if (!perms) |
2875 | 5.34k | perms = cur_perms; |
2876 | 5.34k | if (tail) |
2877 | 0 | tail->next = cur_perms; |
2878 | 5.34k | tail = cur_perms; |
2879 | 5.34k | } |
2880 | | |
2881 | 16.5k | while ((id = queue_remove(id_queue))) { |
2882 | 11.1k | cur_perms = perms; |
2883 | 714k | ebitmap_for_each_positive_bit(&tclasses, node, i) { |
2884 | 11.1k | cladatum = policydbp->class_val_to_struct[i]; |
2885 | | |
2886 | 11.1k | if (strcmp(id, "*") == 0) { |
2887 | | /* set all declared permissions in the class */ |
2888 | 224 | cur_perms->data = PERMISSION_MASK(cladatum->permissions.nprim); |
2889 | 224 | goto next; |
2890 | 224 | } |
2891 | | |
2892 | 10.9k | if (strcmp(id, "~") == 0) { |
2893 | | /* complement the set */ |
2894 | 3.92k | if (which == AVRULE_DONTAUDIT) |
2895 | 786 | yywarn("dontaudit rule with a ~?"); |
2896 | 3.92k | cur_perms->data = ~cur_perms->data & PERMISSION_MASK(cladatum->permissions.nprim); |
2897 | 3.92k | if (cur_perms->data == 0) { |
2898 | 402 | class_perm_node_t *tmp = cur_perms; |
2899 | 402 | yywarn("omitting avrule with no permission set"); |
2900 | 402 | if (perms == cur_perms) |
2901 | 402 | perms = cur_perms->next; |
2902 | 402 | cur_perms = cur_perms->next; |
2903 | 402 | free(tmp); |
2904 | 402 | continue; |
2905 | 402 | } |
2906 | 3.52k | goto next; |
2907 | 3.92k | } |
2908 | | |
2909 | 7.01k | perdatum = |
2910 | 7.01k | hashtab_search(cladatum->permissions.table, id); |
2911 | 7.01k | if (!perdatum) { |
2912 | 5.62k | if (cladatum->comdatum) { |
2913 | 0 | perdatum = |
2914 | 0 | hashtab_search(cladatum->comdatum-> |
2915 | 0 | permissions.table, |
2916 | 0 | id); |
2917 | 0 | } |
2918 | 5.62k | } |
2919 | 7.01k | if (!perdatum) { |
2920 | 5.62k | if (!suppress) |
2921 | 5.62k | yyerror2("permission %s is not defined" |
2922 | 5.62k | " for class %s", id, |
2923 | 5.62k | policydbp->p_class_val_to_name[i]); |
2924 | 5.62k | continue; |
2925 | 5.62k | } else |
2926 | 1.38k | if (!is_perm_in_scope |
2927 | 1.38k | (id, policydbp->p_class_val_to_name[i])) { |
2928 | 0 | if (!suppress) { |
2929 | 0 | yyerror2("permission %s of class %s is" |
2930 | 0 | " not within scope", id, |
2931 | 0 | policydbp->p_class_val_to_name[i]); |
2932 | 0 | } |
2933 | 0 | continue; |
2934 | 1.38k | } else { |
2935 | 1.38k | cur_perms->data |= UINT32_C(1) << (perdatum->s.value - 1); |
2936 | 1.38k | } |
2937 | 5.13k | next: |
2938 | 5.13k | cur_perms = cur_perms->next; |
2939 | 5.13k | } |
2940 | | |
2941 | 11.1k | free(id); |
2942 | 11.1k | } |
2943 | | |
2944 | 5.34k | avrule->perms = perms; |
2945 | 5.34k | *rule = avrule; |
2946 | | |
2947 | 5.37k | out: |
2948 | 5.37k | if (ret) { |
2949 | 25 | avrule_destroy(avrule); |
2950 | 25 | free(avrule); |
2951 | 25 | } |
2952 | | |
2953 | 5.37k | ebitmap_destroy(&tclasses); |
2954 | | |
2955 | 5.37k | return ret; |
2956 | | |
2957 | 5.34k | } |
2958 | | |
2959 | | avrule_t *define_cond_te_avtab(int which) |
2960 | 6.12k | { |
2961 | 6.12k | char *id; |
2962 | 6.12k | avrule_t *avrule; |
2963 | 6.12k | int i; |
2964 | | |
2965 | 6.12k | if (pass == 1) { |
2966 | 20.3k | for (i = 0; i < 4; i++) { |
2967 | 36.2k | while ((id = queue_remove(id_queue))) |
2968 | 19.9k | free(id); |
2969 | 16.2k | } |
2970 | 4.07k | return (avrule_t *) 1; /* any non-NULL value */ |
2971 | 4.07k | } |
2972 | | |
2973 | 2.05k | if (define_te_avtab_helper(which, &avrule)) |
2974 | 8 | return COND_ERR; |
2975 | | |
2976 | 2.04k | return avrule; |
2977 | 2.05k | } |
2978 | | |
2979 | | int define_te_avtab(int which) |
2980 | 9.33k | { |
2981 | 9.33k | char *id; |
2982 | 9.33k | avrule_t *avrule; |
2983 | 9.33k | int i; |
2984 | | |
2985 | 9.33k | if (pass == 1) { |
2986 | 30.0k | for (i = 0; i < 4; i++) { |
2987 | 54.0k | while ((id = queue_remove(id_queue))) |
2988 | 29.9k | free(id); |
2989 | 24.0k | } |
2990 | 6.01k | return 0; |
2991 | 6.01k | } |
2992 | | |
2993 | 3.31k | if (define_te_avtab_helper(which, &avrule)) |
2994 | 17 | return -1; |
2995 | | |
2996 | | /* append this avrule to the end of the current rules list */ |
2997 | 3.30k | append_avrule(avrule); |
2998 | 3.30k | return 0; |
2999 | 3.31k | } |
3000 | | |
3001 | | /* The role-types rule is no longer used to declare regular role or |
3002 | | * role attribute, but solely aimed for declaring role-types associations. |
3003 | | */ |
3004 | | int define_role_types(void) |
3005 | 7.94k | { |
3006 | 7.94k | role_datum_t *role; |
3007 | 7.94k | char *id; |
3008 | 7.94k | int add = 1; |
3009 | | |
3010 | 7.94k | if (pass == 1) { |
3011 | 14.9k | while ((id = queue_remove(id_queue))) |
3012 | 9.31k | free(id); |
3013 | 5.66k | return 0; |
3014 | 5.66k | } |
3015 | | |
3016 | 2.28k | id = (char *)queue_remove(id_queue); |
3017 | 2.28k | if (!id) { |
3018 | 0 | yyerror("no role name for role-types rule?"); |
3019 | 0 | return -1; |
3020 | 0 | } |
3021 | | |
3022 | 2.28k | if (!is_id_in_scope(SYM_ROLES, id)) { |
3023 | 0 | yyerror2("role %s is not within scope", id); |
3024 | 0 | free(id); |
3025 | 0 | return -1; |
3026 | 0 | } |
3027 | | |
3028 | 2.28k | role = hashtab_search(policydbp->p_roles.table, id); |
3029 | 2.28k | if (!role) { |
3030 | 12 | yyerror2("unknown role %s", id); |
3031 | 12 | free(id); |
3032 | 12 | return -1; |
3033 | 12 | } |
3034 | 2.27k | role = get_local_role(id, role->s.value, (role->flavor == ROLE_ATTRIB)); |
3035 | | |
3036 | 4.88k | while ((id = queue_remove(id_queue))) { |
3037 | 2.60k | if (set_types(&role->types, id, &add, 0)) |
3038 | 0 | return -1; |
3039 | 2.60k | } |
3040 | | |
3041 | 2.27k | return 0; |
3042 | 2.27k | } |
3043 | | |
3044 | | int define_attrib_role(void) |
3045 | 1.12k | { |
3046 | 1.12k | if (pass == 2) { |
3047 | 546 | free(queue_remove(id_queue)); |
3048 | 546 | return 0; |
3049 | 546 | } |
3050 | | |
3051 | | /* Declare a role attribute */ |
3052 | 583 | if (declare_role(TRUE) == NULL) |
3053 | 0 | return -1; |
3054 | | |
3055 | 583 | return 0; |
3056 | 583 | } |
3057 | | |
3058 | | int define_role_attr(void) |
3059 | 134k | { |
3060 | 134k | char *id; |
3061 | 134k | role_datum_t *r, *attr; |
3062 | | |
3063 | 134k | if (pass == 2) { |
3064 | 142k | while ((id = queue_remove(id_queue))) |
3065 | 79.5k | free(id); |
3066 | 63.3k | return 0; |
3067 | 63.3k | } |
3068 | | |
3069 | | /* Declare a regular role */ |
3070 | 71.1k | if ((r = declare_role(FALSE)) == NULL) |
3071 | 0 | return -1; |
3072 | | |
3073 | 89.4k | while ((id = queue_remove(id_queue))) { |
3074 | 18.3k | if (!is_id_in_scope(SYM_ROLES, id)) { |
3075 | 1 | yyerror2("attribute %s is not within scope", id); |
3076 | 1 | free(id); |
3077 | 1 | return -1; |
3078 | 1 | } |
3079 | 18.3k | attr = hashtab_search(policydbp->p_roles.table, id); |
3080 | 18.3k | if (!attr) { |
3081 | | /* treat it as a fatal error */ |
3082 | 3 | yyerror2("role attribute %s is not declared", id); |
3083 | 3 | free(id); |
3084 | 3 | return -1; |
3085 | 3 | } |
3086 | | |
3087 | 18.3k | if (attr->flavor != ROLE_ATTRIB) { |
3088 | 4 | yyerror2("%s is a regular role, not an attribute", id); |
3089 | 4 | free(id); |
3090 | 4 | return -1; |
3091 | 4 | } |
3092 | | |
3093 | 18.2k | if ((attr = get_local_role(id, attr->s.value, 1)) == NULL) { |
3094 | 0 | yyerror("Out of memory!"); |
3095 | 0 | return -1; |
3096 | 0 | } |
3097 | | |
3098 | 18.2k | if (ebitmap_set_bit(&attr->roles, (r->s.value - 1), TRUE)) { |
3099 | 0 | yyerror("out of memory"); |
3100 | 0 | return -1; |
3101 | 0 | } |
3102 | 18.2k | } |
3103 | | |
3104 | 71.1k | return 0; |
3105 | 71.1k | } |
3106 | | |
3107 | | int define_roleattribute(void) |
3108 | 525 | { |
3109 | 525 | char *id; |
3110 | 525 | role_datum_t *r, *attr; |
3111 | | |
3112 | 525 | if (pass == 2) { |
3113 | 590 | while ((id = queue_remove(id_queue))) |
3114 | 410 | free(id); |
3115 | 180 | return 0; |
3116 | 180 | } |
3117 | | |
3118 | 345 | id = (char *)queue_remove(id_queue); |
3119 | 345 | if (!id) { |
3120 | 0 | yyerror("no role name for roleattribute definition?"); |
3121 | 0 | return -1; |
3122 | 0 | } |
3123 | | |
3124 | 345 | if (!is_id_in_scope(SYM_ROLES, id)) { |
3125 | 0 | yyerror2("role %s is not within scope", id); |
3126 | 0 | free(id); |
3127 | 0 | return -1; |
3128 | 0 | } |
3129 | 345 | r = hashtab_search(policydbp->p_roles.table, id); |
3130 | | /* We support adding one role attribute into another */ |
3131 | 345 | if (!r) { |
3132 | 0 | yyerror2("unknown role %s", id); |
3133 | 0 | free(id); |
3134 | 0 | return -1; |
3135 | 0 | } |
3136 | 345 | free(id); |
3137 | | |
3138 | 740 | while ((id = queue_remove(id_queue))) { |
3139 | 395 | if (!is_id_in_scope(SYM_ROLES, id)) { |
3140 | 0 | yyerror2("attribute %s is not within scope", id); |
3141 | 0 | free(id); |
3142 | 0 | return -1; |
3143 | 0 | } |
3144 | 395 | attr = hashtab_search(policydbp->p_roles.table, id); |
3145 | 395 | if (!attr) { |
3146 | | /* treat it as a fatal error */ |
3147 | 0 | yyerror2("role attribute %s is not declared", id); |
3148 | 0 | free(id); |
3149 | 0 | return -1; |
3150 | 0 | } |
3151 | | |
3152 | 395 | if (attr->flavor != ROLE_ATTRIB) { |
3153 | 0 | yyerror2("%s is a regular role, not an attribute", id); |
3154 | 0 | free(id); |
3155 | 0 | return -1; |
3156 | 0 | } |
3157 | | |
3158 | 395 | if ((attr = get_local_role(id, attr->s.value, 1)) == NULL) { |
3159 | 0 | yyerror("Out of memory!"); |
3160 | 0 | return -1; |
3161 | 0 | } |
3162 | | |
3163 | 395 | if (ebitmap_set_bit(&attr->roles, (r->s.value - 1), TRUE)) { |
3164 | 0 | yyerror("out of memory"); |
3165 | 0 | return -1; |
3166 | 0 | } |
3167 | 395 | } |
3168 | | |
3169 | 345 | return 0; |
3170 | 345 | } |
3171 | | |
3172 | | static int role_val_to_name_helper(hashtab_key_t key, hashtab_datum_t datum, |
3173 | | void *p) |
3174 | 326 | { |
3175 | 326 | struct val_to_name *v = p; |
3176 | 326 | role_datum_t *roldatum; |
3177 | | |
3178 | 326 | roldatum = (role_datum_t *) datum; |
3179 | | |
3180 | 326 | if (v->val == roldatum->s.value) { |
3181 | 11 | v->name = key; |
3182 | 11 | return 1; |
3183 | 11 | } |
3184 | | |
3185 | 315 | return 0; |
3186 | 326 | } |
3187 | | |
3188 | | static char *role_val_to_name(unsigned int val) |
3189 | 11 | { |
3190 | 11 | struct val_to_name v; |
3191 | 11 | int rc; |
3192 | | |
3193 | 11 | v.val = val; |
3194 | 11 | rc = hashtab_map(policydbp->p_roles.table, role_val_to_name_helper, &v); |
3195 | 11 | if (rc) |
3196 | 11 | return v.name; |
3197 | 0 | return NULL; |
3198 | 11 | } |
3199 | | |
3200 | | static int set_roles(role_set_t * set, char *id) |
3201 | 11.6k | { |
3202 | 11.6k | role_datum_t *r; |
3203 | | |
3204 | 11.6k | if (strcmp(id, "*") == 0) { |
3205 | 0 | free(id); |
3206 | 0 | yyerror("* is not allowed for role sets"); |
3207 | 0 | return -1; |
3208 | 0 | } |
3209 | | |
3210 | 11.6k | if (strcmp(id, "~") == 0) { |
3211 | 0 | free(id); |
3212 | 0 | yyerror("~ is not allowed for role sets"); |
3213 | 0 | return -1; |
3214 | 0 | } |
3215 | 11.6k | if (!is_id_in_scope(SYM_ROLES, id)) { |
3216 | 2 | yyerror2("role %s is not within scope", id); |
3217 | 2 | free(id); |
3218 | 2 | return -1; |
3219 | 2 | } |
3220 | 11.6k | r = hashtab_search(policydbp->p_roles.table, id); |
3221 | 11.6k | if (!r) { |
3222 | 6 | yyerror2("unknown role %s", id); |
3223 | 6 | free(id); |
3224 | 6 | return -1; |
3225 | 6 | } |
3226 | | |
3227 | 11.6k | if (ebitmap_set_bit(&set->roles, r->s.value - 1, TRUE)) { |
3228 | 0 | yyerror("out of memory"); |
3229 | 0 | free(id); |
3230 | 0 | return -1; |
3231 | 0 | } |
3232 | 11.6k | free(id); |
3233 | 11.6k | return 0; |
3234 | 11.6k | } |
3235 | | |
3236 | | int define_role_trans(int class_specified) |
3237 | 4.74k | { |
3238 | 4.74k | char *id; |
3239 | 4.74k | const role_datum_t *role; |
3240 | 4.74k | role_set_t roles; |
3241 | 4.74k | type_set_t types; |
3242 | 4.74k | const class_datum_t *cladatum; |
3243 | 4.74k | ebitmap_t e_types, e_roles, e_classes; |
3244 | 4.74k | ebitmap_node_t *tnode, *rnode, *cnode; |
3245 | 4.74k | struct role_trans *tr = NULL; |
3246 | 4.74k | struct role_trans_rule *rule = NULL; |
3247 | 4.74k | unsigned int i, j, k; |
3248 | 4.74k | int add = 1; |
3249 | | |
3250 | 4.74k | if (pass == 1) { |
3251 | 9.27k | while ((id = queue_remove(id_queue))) |
3252 | 5.06k | free(id); |
3253 | 15.0k | while ((id = queue_remove(id_queue))) |
3254 | 10.8k | free(id); |
3255 | 4.21k | if (class_specified) |
3256 | 8.62k | while ((id = queue_remove(id_queue))) |
3257 | 5.26k | free(id); |
3258 | 4.21k | id = queue_remove(id_queue); |
3259 | 4.21k | free(id); |
3260 | 4.21k | return 0; |
3261 | 4.21k | } |
3262 | | |
3263 | 536 | role_set_init(&roles); |
3264 | 536 | ebitmap_init(&e_roles); |
3265 | 536 | type_set_init(&types); |
3266 | 536 | ebitmap_init(&e_types); |
3267 | 536 | ebitmap_init(&e_classes); |
3268 | | |
3269 | 1.06k | while ((id = queue_remove(id_queue))) { |
3270 | 536 | if (set_roles(&roles, id)) |
3271 | 6 | goto bad; |
3272 | 536 | } |
3273 | 530 | add = 1; |
3274 | 5.32k | while ((id = queue_remove(id_queue))) { |
3275 | 4.79k | if (set_types(&types, id, &add, 0)) |
3276 | 5 | goto bad; |
3277 | 4.79k | } |
3278 | | |
3279 | 525 | if (class_specified) { |
3280 | 510 | if (read_classes(&e_classes)) |
3281 | 1 | goto bad; |
3282 | 510 | } else { |
3283 | 15 | cladatum = hashtab_search(policydbp->p_classes.table, |
3284 | 15 | "process"); |
3285 | 15 | if (!cladatum) { |
3286 | 4 | yyerror2("could not find process class for " |
3287 | 4 | "legacy role_transition statement"); |
3288 | 4 | goto bad; |
3289 | 4 | } |
3290 | | |
3291 | 11 | if (ebitmap_set_bit(&e_classes, cladatum->s.value - 1, TRUE)) { |
3292 | 0 | yyerror("out of memory"); |
3293 | 0 | goto bad; |
3294 | 0 | } |
3295 | 11 | } |
3296 | | |
3297 | 520 | id = (char *)queue_remove(id_queue); |
3298 | 520 | if (!id) { |
3299 | 0 | yyerror("no new role in transition definition?"); |
3300 | 0 | goto bad; |
3301 | 0 | } |
3302 | 520 | if (!is_id_in_scope(SYM_ROLES, id)) { |
3303 | 0 | yyerror2("role %s is not within scope", id); |
3304 | 0 | free(id); |
3305 | 0 | goto bad; |
3306 | 0 | } |
3307 | 520 | role = hashtab_search(policydbp->p_roles.table, id); |
3308 | 520 | if (!role) { |
3309 | 3 | yyerror2("unknown role %s used in transition definition", id); |
3310 | 3 | free(id); |
3311 | 3 | goto bad; |
3312 | 3 | } |
3313 | | |
3314 | 517 | if (role->flavor != ROLE_ROLE) { |
3315 | 0 | yyerror2("the new role %s must be a regular role", id); |
3316 | 0 | free(id); |
3317 | 0 | goto bad; |
3318 | 0 | } |
3319 | 517 | free(id); |
3320 | | |
3321 | | /* This ebitmap business is just to ensure that there are not conflicting role_trans rules */ |
3322 | 517 | if (role_set_expand(&roles, &e_roles, policydbp, NULL, NULL)) |
3323 | 0 | goto bad; |
3324 | | |
3325 | 517 | if (type_set_expand(&types, &e_types, policydbp, 1)) |
3326 | 0 | goto bad; |
3327 | | |
3328 | 32.6k | ebitmap_for_each_positive_bit(&e_roles, rnode, i) { |
3329 | 7.04k | ebitmap_for_each_positive_bit(&e_types, tnode, j) { |
3330 | 11.2k | ebitmap_for_each_positive_bit(&e_classes, cnode, k) { |
3331 | 897 | for (tr = policydbp->role_tr; tr; |
3332 | 721 | tr = tr->next) { |
3333 | 721 | if (tr->role == (i + 1) && |
3334 | 221 | tr->type == (j + 1) && |
3335 | 11 | tr->tclass == (k + 1)) { |
3336 | 11 | yyerror2("duplicate role " |
3337 | 11 | "transition for " |
3338 | 11 | "(%s,%s,%s)", |
3339 | 11 | role_val_to_name(i+1), |
3340 | 11 | policydbp->p_type_val_to_name[j], |
3341 | 11 | policydbp->p_class_val_to_name[k]); |
3342 | 11 | goto bad; |
3343 | 11 | } |
3344 | 721 | } |
3345 | | |
3346 | 176 | tr = malloc(sizeof(struct role_trans)); |
3347 | 176 | if (!tr) { |
3348 | 0 | yyerror("out of memory"); |
3349 | 0 | goto bad; |
3350 | 0 | } |
3351 | 176 | memset(tr, 0, sizeof(struct role_trans)); |
3352 | 176 | tr->role = i + 1; |
3353 | 176 | tr->type = j + 1; |
3354 | 176 | tr->tclass = k + 1; |
3355 | 176 | tr->new_role = role->s.value; |
3356 | 176 | tr->next = policydbp->role_tr; |
3357 | 176 | policydbp->role_tr = tr; |
3358 | 176 | } |
3359 | 187 | } |
3360 | 517 | } |
3361 | | /* Now add the real rule */ |
3362 | 506 | rule = malloc(sizeof(struct role_trans_rule)); |
3363 | 506 | if (!rule) { |
3364 | 0 | yyerror("out of memory"); |
3365 | 0 | goto bad; |
3366 | 0 | } |
3367 | 506 | memset(rule, 0, sizeof(struct role_trans_rule)); |
3368 | 506 | rule->roles = roles; |
3369 | 506 | rule->types = types; |
3370 | 506 | rule->classes = e_classes; |
3371 | 506 | rule->new_role = role->s.value; |
3372 | | |
3373 | 506 | append_role_trans(rule); |
3374 | | |
3375 | 506 | ebitmap_destroy(&e_roles); |
3376 | 506 | ebitmap_destroy(&e_types); |
3377 | | |
3378 | 506 | return 0; |
3379 | | |
3380 | 30 | bad: |
3381 | 30 | role_set_destroy(&roles); |
3382 | 30 | type_set_destroy(&types); |
3383 | 30 | ebitmap_destroy(&e_roles); |
3384 | 30 | ebitmap_destroy(&e_types); |
3385 | 30 | ebitmap_destroy(&e_classes); |
3386 | 30 | return -1; |
3387 | 506 | } |
3388 | | |
3389 | | int define_role_allow(void) |
3390 | 12.2k | { |
3391 | 12.2k | char *id; |
3392 | 12.2k | struct role_allow_rule *ra = 0; |
3393 | | |
3394 | 12.2k | if (pass == 1) { |
3395 | 13.6k | while ((id = queue_remove(id_queue))) |
3396 | 6.93k | free(id); |
3397 | 13.6k | while ((id = queue_remove(id_queue))) |
3398 | 6.98k | free(id); |
3399 | 6.68k | return 0; |
3400 | 6.68k | } |
3401 | | |
3402 | 5.56k | ra = malloc(sizeof(role_allow_rule_t)); |
3403 | 5.56k | if (!ra) { |
3404 | 0 | yyerror("out of memory"); |
3405 | 0 | return -1; |
3406 | 0 | } |
3407 | 5.56k | role_allow_rule_init(ra); |
3408 | | |
3409 | 11.1k | while ((id = queue_remove(id_queue))) { |
3410 | 5.56k | if (set_roles(&ra->roles, id)) { |
3411 | 1 | role_allow_rule_destroy(ra); |
3412 | 1 | free(ra); |
3413 | 1 | return -1; |
3414 | 1 | } |
3415 | 5.56k | } |
3416 | | |
3417 | 11.1k | while ((id = queue_remove(id_queue))) { |
3418 | 5.56k | if (set_roles(&ra->new_roles, id)) { |
3419 | 1 | role_allow_rule_destroy(ra); |
3420 | 1 | free(ra); |
3421 | 1 | return -1; |
3422 | 1 | } |
3423 | 5.56k | } |
3424 | | |
3425 | 5.56k | append_role_allow(ra); |
3426 | 5.56k | return 0; |
3427 | 5.56k | } |
3428 | | |
3429 | | avrule_t *define_cond_filename_trans(void) |
3430 | 0 | { |
3431 | 0 | yyerror("type transitions with a filename not allowed inside " |
3432 | 0 | "conditionals"); |
3433 | 0 | return COND_ERR; |
3434 | 0 | } |
3435 | | |
3436 | | int define_filename_trans(void) |
3437 | 4.92k | { |
3438 | 4.92k | char *id, *name = NULL; |
3439 | 4.92k | type_set_t stypes, ttypes; |
3440 | 4.92k | ebitmap_t e_stypes, e_ttypes; |
3441 | 4.92k | ebitmap_t e_tclasses; |
3442 | 4.92k | ebitmap_node_t *snode, *tnode, *cnode; |
3443 | 4.92k | filename_trans_rule_t *ftr; |
3444 | 4.92k | type_datum_t *typdatum; |
3445 | 4.92k | uint32_t otype; |
3446 | 4.92k | unsigned int c, s, t; |
3447 | 4.92k | int add, self, rc; |
3448 | | |
3449 | 4.92k | if (pass == 1) { |
3450 | | /* stype */ |
3451 | 6.59k | while ((id = queue_remove(id_queue))) |
3452 | 3.60k | free(id); |
3453 | | /* ttype */ |
3454 | 18.0k | while ((id = queue_remove(id_queue))) |
3455 | 15.0k | free(id); |
3456 | | /* tclass */ |
3457 | 5.23k | while ((id = queue_remove(id_queue))) |
3458 | 2.23k | free(id); |
3459 | | /* otype */ |
3460 | 2.99k | id = queue_remove(id_queue); |
3461 | 2.99k | free(id); |
3462 | | /* name */ |
3463 | 2.99k | id = queue_remove(id_queue); |
3464 | 2.99k | free(id); |
3465 | 2.99k | return 0; |
3466 | 2.99k | } |
3467 | | |
3468 | 1.92k | type_set_init(&stypes); |
3469 | 1.92k | type_set_init(&ttypes); |
3470 | 1.92k | ebitmap_init(&e_stypes); |
3471 | 1.92k | ebitmap_init(&e_ttypes); |
3472 | 1.92k | ebitmap_init(&e_tclasses); |
3473 | | |
3474 | 1.92k | add = 1; |
3475 | 4.27k | while ((id = queue_remove(id_queue))) { |
3476 | 2.35k | if (set_types(&stypes, id, &add, 0)) |
3477 | 6 | goto bad; |
3478 | 2.35k | } |
3479 | | |
3480 | 1.92k | self = 0; |
3481 | 1.92k | add = 1; |
3482 | 14.2k | while ((id = queue_remove(id_queue))) { |
3483 | 12.3k | if (strcmp(id, "self") == 0) { |
3484 | 134 | free(id); |
3485 | 134 | if (add == 0) { |
3486 | 0 | yyerror("-self is not supported"); |
3487 | 0 | goto bad; |
3488 | 0 | } |
3489 | 134 | self = 1; |
3490 | 134 | continue; |
3491 | 134 | } |
3492 | 12.2k | if (set_types(&ttypes, id, &add, 0)) |
3493 | 12 | goto bad; |
3494 | 12.2k | } |
3495 | | |
3496 | 1.90k | if (read_classes(&e_tclasses)) |
3497 | 4 | goto bad; |
3498 | | |
3499 | 1.90k | id = (char *)queue_remove(id_queue); |
3500 | 1.90k | if (!id) { |
3501 | 0 | yyerror("no otype in transition definition?"); |
3502 | 0 | goto bad; |
3503 | 0 | } |
3504 | 1.90k | if (!is_id_in_scope(SYM_TYPES, id)) { |
3505 | 0 | yyerror2("type %s is not within scope", id); |
3506 | 0 | free(id); |
3507 | 0 | goto bad; |
3508 | 0 | } |
3509 | 1.90k | typdatum = hashtab_search(policydbp->p_types.table, id); |
3510 | 1.90k | if (!typdatum) { |
3511 | 2 | yyerror2("unknown type %s used in transition definition", id); |
3512 | 2 | free(id); |
3513 | 2 | goto bad; |
3514 | 2 | } |
3515 | 1.90k | free(id); |
3516 | 1.90k | otype = typdatum->s.value; |
3517 | | |
3518 | 1.90k | name = queue_remove(id_queue); |
3519 | 1.90k | if (!name) { |
3520 | 0 | yyerror("no pathname specified in filename_trans definition?"); |
3521 | 0 | goto bad; |
3522 | 0 | } |
3523 | | |
3524 | | /* We expand the class set into separate rules. We expand the types |
3525 | | * just to make sure there are not duplicates. They will get turned |
3526 | | * into separate rules later */ |
3527 | 1.90k | if (type_set_expand(&stypes, &e_stypes, policydbp, 1)) |
3528 | 0 | goto bad; |
3529 | | |
3530 | 1.90k | if (type_set_expand(&ttypes, &e_ttypes, policydbp, 1)) |
3531 | 0 | goto bad; |
3532 | | |
3533 | 121k | ebitmap_for_each_positive_bit(&e_tclasses, cnode, c) { |
3534 | 61.4k | ebitmap_for_each_positive_bit(&e_stypes, snode, s) { |
3535 | 27.7k | ebitmap_for_each_positive_bit(&e_ttypes, tnode, t) { |
3536 | 546 | rc = policydb_filetrans_insert( |
3537 | 546 | policydbp, s+1, t+1, c+1, name, |
3538 | 546 | NULL, otype, NULL |
3539 | 546 | ); |
3540 | 546 | if (rc != SEPOL_OK) { |
3541 | 12 | if (rc == SEPOL_EEXIST) { |
3542 | 12 | yyerror2("duplicate filename transition for: filename_trans %s %s %s:%s", |
3543 | 12 | name, |
3544 | 12 | policydbp->p_type_val_to_name[s], |
3545 | 12 | policydbp->p_type_val_to_name[t], |
3546 | 12 | policydbp->p_class_val_to_name[c]); |
3547 | 12 | goto bad; |
3548 | 12 | } |
3549 | 0 | yyerror("out of memory"); |
3550 | 0 | goto bad; |
3551 | 12 | } |
3552 | 546 | } |
3553 | 1.02k | if (self) { |
3554 | 0 | rc = policydb_filetrans_insert( |
3555 | 0 | policydbp, s+1, s+1, c+1, name, |
3556 | 0 | NULL, otype, NULL |
3557 | 0 | ); |
3558 | 0 | if (rc != SEPOL_OK) { |
3559 | 0 | if (rc == SEPOL_EEXIST) { |
3560 | 0 | yyerror2("duplicate filename transition for: filename_trans %s %s %s:%s", |
3561 | 0 | name, |
3562 | 0 | policydbp->p_type_val_to_name[s], |
3563 | 0 | policydbp->p_type_val_to_name[s], |
3564 | 0 | policydbp->p_class_val_to_name[c]); |
3565 | 0 | goto bad; |
3566 | 0 | } |
3567 | 0 | yyerror("out of memory"); |
3568 | 0 | goto bad; |
3569 | 0 | } |
3570 | 0 | } |
3571 | 1.02k | } |
3572 | | |
3573 | | /* Now add the real rule since we didn't find any duplicates */ |
3574 | 1.89k | ftr = malloc(sizeof(*ftr)); |
3575 | 1.89k | if (!ftr) { |
3576 | 0 | yyerror("out of memory"); |
3577 | 0 | goto bad; |
3578 | 0 | } |
3579 | 1.89k | filename_trans_rule_init(ftr); |
3580 | 1.89k | append_filename_trans(ftr); |
3581 | | |
3582 | 1.89k | ftr->name = strdup(name); |
3583 | 1.89k | if (type_set_cpy(&ftr->stypes, &stypes)) { |
3584 | 0 | yyerror("out of memory"); |
3585 | 0 | goto bad; |
3586 | 0 | } |
3587 | 1.89k | if (type_set_cpy(&ftr->ttypes, &ttypes)) { |
3588 | 0 | yyerror("out of memory"); |
3589 | 0 | goto bad; |
3590 | 0 | } |
3591 | 1.89k | ftr->tclass = c + 1; |
3592 | 1.89k | ftr->otype = otype; |
3593 | 1.89k | ftr->flags = self ? RULE_SELF : 0; |
3594 | 1.89k | } |
3595 | | |
3596 | 1.89k | free(name); |
3597 | 1.89k | ebitmap_destroy(&e_stypes); |
3598 | 1.89k | ebitmap_destroy(&e_ttypes); |
3599 | 1.89k | ebitmap_destroy(&e_tclasses); |
3600 | 1.89k | type_set_destroy(&stypes); |
3601 | 1.89k | type_set_destroy(&ttypes); |
3602 | | |
3603 | 1.89k | return 0; |
3604 | | |
3605 | 36 | bad: |
3606 | 36 | free(name); |
3607 | 36 | ebitmap_destroy(&e_stypes); |
3608 | 36 | ebitmap_destroy(&e_ttypes); |
3609 | 36 | ebitmap_destroy(&e_tclasses); |
3610 | 36 | type_set_destroy(&stypes); |
3611 | 36 | type_set_destroy(&ttypes); |
3612 | 36 | return -1; |
3613 | 1.90k | } |
3614 | | |
3615 | | static constraint_expr_t *constraint_expr_clone(const constraint_expr_t * expr) |
3616 | 0 | { |
3617 | 0 | constraint_expr_t *h = NULL, *l = NULL, *newe; |
3618 | 0 | const constraint_expr_t *e; |
3619 | 0 | for (e = expr; e; e = e->next) { |
3620 | 0 | newe = malloc(sizeof(*newe)); |
3621 | 0 | if (!newe) |
3622 | 0 | goto oom; |
3623 | 0 | if (constraint_expr_init(newe) == -1) { |
3624 | 0 | free(newe); |
3625 | 0 | goto oom; |
3626 | 0 | } |
3627 | 0 | if (l) |
3628 | 0 | l->next = newe; |
3629 | 0 | else |
3630 | 0 | h = newe; |
3631 | 0 | l = newe; |
3632 | 0 | newe->expr_type = e->expr_type; |
3633 | 0 | newe->attr = e->attr; |
3634 | 0 | newe->op = e->op; |
3635 | 0 | if (newe->expr_type == CEXPR_NAMES) { |
3636 | 0 | if (newe->attr & CEXPR_TYPE) { |
3637 | 0 | if (type_set_cpy |
3638 | 0 | (newe->type_names, e->type_names)) |
3639 | 0 | goto oom; |
3640 | 0 | } else { |
3641 | 0 | if (ebitmap_cpy(&newe->names, &e->names)) |
3642 | 0 | goto oom; |
3643 | 0 | } |
3644 | 0 | } |
3645 | 0 | } |
3646 | | |
3647 | 0 | return h; |
3648 | 0 | oom: |
3649 | 0 | constraint_expr_destroy(h); |
3650 | 0 | return NULL; |
3651 | 0 | } |
3652 | | |
3653 | | int define_constraint(constraint_expr_t * expr) |
3654 | 10.6k | { |
3655 | 10.6k | struct constraint_node *node; |
3656 | 10.6k | char *id; |
3657 | 10.6k | class_datum_t *cladatum; |
3658 | 10.6k | perm_datum_t *perdatum; |
3659 | 10.6k | ebitmap_t classmap; |
3660 | 10.6k | ebitmap_node_t *enode; |
3661 | 10.6k | constraint_expr_t *e; |
3662 | 10.6k | unsigned int i; |
3663 | 10.6k | int depth; |
3664 | 10.6k | unsigned char useexpr = 1; |
3665 | | |
3666 | 10.6k | if (pass == 1) { |
3667 | 11.1k | while ((id = queue_remove(id_queue))) |
3668 | 5.63k | free(id); |
3669 | 11.6k | while ((id = queue_remove(id_queue))) |
3670 | 6.12k | free(id); |
3671 | 5.48k | return 0; |
3672 | 5.48k | } |
3673 | | |
3674 | 5.16k | ebitmap_init(&classmap); |
3675 | | |
3676 | 5.16k | depth = -1; |
3677 | 367k | for (e = expr; e; e = e->next) { |
3678 | 362k | switch (e->expr_type) { |
3679 | 326k | case CEXPR_NOT: |
3680 | 326k | if (depth < 0) { |
3681 | 0 | yyerror("illegal constraint expression"); |
3682 | 0 | goto bad; |
3683 | 0 | } |
3684 | 326k | break; |
3685 | 326k | case CEXPR_AND: |
3686 | 15.2k | case CEXPR_OR: |
3687 | 15.2k | if (depth < 1) { |
3688 | 0 | yyerror("illegal constraint expression"); |
3689 | 0 | goto bad; |
3690 | 0 | } |
3691 | 15.2k | depth--; |
3692 | 15.2k | break; |
3693 | 11.2k | case CEXPR_ATTR: |
3694 | 20.4k | case CEXPR_NAMES: |
3695 | 20.4k | if (e->attr & CEXPR_XTARGET) { |
3696 | 16 | yyerror("illegal constraint expression"); |
3697 | 16 | goto bad; /* only for validatetrans rules */ |
3698 | 16 | } |
3699 | 20.4k | if (depth == (CEXPR_MAXDEPTH - 1)) { |
3700 | 0 | yyerror("constraint expression is too deep"); |
3701 | 0 | goto bad; |
3702 | 0 | } |
3703 | 20.4k | depth++; |
3704 | 20.4k | break; |
3705 | 0 | default: |
3706 | 0 | yyerror("illegal constraint expression"); |
3707 | 0 | goto bad; |
3708 | 362k | } |
3709 | 362k | } |
3710 | 5.15k | if (depth != 0) { |
3711 | 0 | yyerror("illegal constraint expression"); |
3712 | 0 | goto bad; |
3713 | 0 | } |
3714 | | |
3715 | 10.2k | while ((id = queue_remove(id_queue))) { |
3716 | 5.15k | if (!is_id_in_scope(SYM_CLASSES, id)) { |
3717 | 0 | yyerror2("class %s is not within scope", id); |
3718 | 0 | free(id); |
3719 | 0 | goto bad; |
3720 | 0 | } |
3721 | 5.15k | cladatum = |
3722 | 5.15k | (class_datum_t *) hashtab_search(policydbp->p_classes.table, |
3723 | 5.15k | (hashtab_key_t) id); |
3724 | 5.15k | if (!cladatum) { |
3725 | 5 | yyerror2("class %s is not defined", id); |
3726 | 5 | free(id); |
3727 | 5 | goto bad; |
3728 | 5 | } |
3729 | 5.14k | if (ebitmap_set_bit(&classmap, cladatum->s.value - 1, TRUE)) { |
3730 | 0 | yyerror("out of memory"); |
3731 | 0 | free(id); |
3732 | 0 | goto bad; |
3733 | 0 | } |
3734 | 5.14k | node = malloc(sizeof(struct constraint_node)); |
3735 | 5.14k | if (!node) { |
3736 | 0 | yyerror("out of memory"); |
3737 | 0 | free(node); |
3738 | 0 | goto bad; |
3739 | 0 | } |
3740 | 5.14k | memset(node, 0, sizeof(constraint_node_t)); |
3741 | 5.14k | if (useexpr) { |
3742 | 5.14k | node->expr = expr; |
3743 | 5.14k | useexpr = 0; |
3744 | 5.14k | } else { |
3745 | 0 | node->expr = constraint_expr_clone(expr); |
3746 | 0 | } |
3747 | 5.14k | if (!node->expr) { |
3748 | 0 | yyerror("out of memory"); |
3749 | 0 | free(node); |
3750 | 0 | goto bad; |
3751 | 0 | } |
3752 | 5.14k | node->permissions = 0; |
3753 | | |
3754 | 5.14k | node->next = cladatum->constraints; |
3755 | 5.14k | cladatum->constraints = node; |
3756 | | |
3757 | 5.14k | free(id); |
3758 | 5.14k | } |
3759 | | |
3760 | 10.9k | while ((id = queue_remove(id_queue))) { |
3761 | 368k | ebitmap_for_each_positive_bit(&classmap, enode, i) { |
3762 | 5.76k | cladatum = policydbp->class_val_to_struct[i]; |
3763 | 5.76k | node = cladatum->constraints; |
3764 | | |
3765 | 5.76k | if (strcmp(id, "*") == 0) { |
3766 | 0 | node->permissions = PERMISSION_MASK(cladatum->permissions.nprim); |
3767 | 0 | continue; |
3768 | 0 | } |
3769 | | |
3770 | 5.76k | if (strcmp(id, "~") == 0) { |
3771 | 618 | node->permissions = ~node->permissions & PERMISSION_MASK(cladatum->permissions.nprim); |
3772 | 618 | if (node->permissions == 0) { |
3773 | 73 | yywarn("omitting constraint with no permission set"); |
3774 | 73 | cladatum->constraints = node->next; |
3775 | 73 | constraint_expr_destroy(node->expr); |
3776 | 73 | free(node); |
3777 | 73 | } |
3778 | 618 | continue; |
3779 | 618 | } |
3780 | | |
3781 | 5.14k | perdatum = |
3782 | 5.14k | (perm_datum_t *) hashtab_search(cladatum-> |
3783 | 5.14k | permissions. |
3784 | 5.14k | table, |
3785 | 5.14k | (hashtab_key_t) |
3786 | 5.14k | id); |
3787 | 5.14k | if (!perdatum) { |
3788 | 4 | if (cladatum->comdatum) { |
3789 | 0 | perdatum = |
3790 | 0 | (perm_datum_t *) |
3791 | 0 | hashtab_search(cladatum-> |
3792 | 0 | comdatum-> |
3793 | 0 | permissions. |
3794 | 0 | table, |
3795 | 0 | (hashtab_key_t) |
3796 | 0 | id); |
3797 | 0 | } |
3798 | 4 | if (!perdatum) { |
3799 | 4 | yyerror2("permission %s is not" |
3800 | 4 | " defined for class %s", id, policydbp->p_class_val_to_name[i]); |
3801 | 4 | free(id); |
3802 | 4 | goto bad; |
3803 | 4 | } |
3804 | 4 | } |
3805 | 5.14k | node->permissions |= (UINT32_C(1) << (perdatum->s.value - 1)); |
3806 | 5.14k | } |
3807 | 5.76k | free(id); |
3808 | 5.76k | } |
3809 | | |
3810 | 5.14k | ebitmap_destroy(&classmap); |
3811 | | |
3812 | 5.14k | return 0; |
3813 | | |
3814 | 25 | bad: |
3815 | 25 | ebitmap_destroy(&classmap); |
3816 | 25 | if (useexpr) |
3817 | 21 | constraint_expr_destroy(expr); |
3818 | | |
3819 | 25 | return -1; |
3820 | 5.14k | } |
3821 | | |
3822 | | int define_validatetrans(constraint_expr_t * expr) |
3823 | 44.4k | { |
3824 | 44.4k | struct constraint_node *node; |
3825 | 44.4k | char *id; |
3826 | 44.4k | class_datum_t *cladatum; |
3827 | 44.4k | ebitmap_t classmap; |
3828 | 44.4k | constraint_expr_t *e; |
3829 | 44.4k | int depth; |
3830 | 44.4k | unsigned char useexpr = 1; |
3831 | | |
3832 | 44.4k | if (pass == 1) { |
3833 | 51.2k | while ((id = queue_remove(id_queue))) |
3834 | 25.7k | free(id); |
3835 | 25.5k | return 0; |
3836 | 25.5k | } |
3837 | | |
3838 | 18.8k | ebitmap_init(&classmap); |
3839 | | |
3840 | 18.8k | depth = -1; |
3841 | 851k | for (e = expr; e; e = e->next) { |
3842 | 832k | switch (e->expr_type) { |
3843 | 766k | case CEXPR_NOT: |
3844 | 766k | if (depth < 0) { |
3845 | 0 | yyerror("illegal validatetrans expression"); |
3846 | 0 | goto bad; |
3847 | 0 | } |
3848 | 766k | break; |
3849 | 766k | case CEXPR_AND: |
3850 | 23.6k | case CEXPR_OR: |
3851 | 23.6k | if (depth < 1) { |
3852 | 0 | yyerror("illegal validatetrans expression"); |
3853 | 0 | goto bad; |
3854 | 0 | } |
3855 | 23.6k | depth--; |
3856 | 23.6k | break; |
3857 | 30.3k | case CEXPR_ATTR: |
3858 | 42.5k | case CEXPR_NAMES: |
3859 | 42.5k | if (depth == (CEXPR_MAXDEPTH - 1)) { |
3860 | 2 | yyerror("validatetrans expression is too deep"); |
3861 | 2 | goto bad; |
3862 | 2 | } |
3863 | 42.5k | depth++; |
3864 | 42.5k | break; |
3865 | 0 | default: |
3866 | 0 | yyerror("illegal validatetrans expression"); |
3867 | 0 | goto bad; |
3868 | 832k | } |
3869 | 832k | } |
3870 | 18.8k | if (depth != 0) { |
3871 | 0 | yyerror("illegal validatetrans expression"); |
3872 | 0 | goto bad; |
3873 | 0 | } |
3874 | | |
3875 | 37.6k | while ((id = queue_remove(id_queue))) { |
3876 | 18.8k | if (!is_id_in_scope(SYM_CLASSES, id)) { |
3877 | 0 | yyerror2("class %s is not within scope", id); |
3878 | 0 | free(id); |
3879 | 0 | goto bad; |
3880 | 0 | } |
3881 | 18.8k | cladatum = |
3882 | 18.8k | (class_datum_t *) hashtab_search(policydbp->p_classes.table, |
3883 | 18.8k | (hashtab_key_t) id); |
3884 | 18.8k | if (!cladatum) { |
3885 | 8 | yyerror2("class %s is not defined", id); |
3886 | 8 | free(id); |
3887 | 8 | goto bad; |
3888 | 8 | } |
3889 | 18.8k | if (ebitmap_set_bit(&classmap, (cladatum->s.value - 1), TRUE)) { |
3890 | 0 | yyerror("out of memory"); |
3891 | 0 | free(id); |
3892 | 0 | goto bad; |
3893 | 0 | } |
3894 | | |
3895 | 18.8k | node = malloc(sizeof(struct constraint_node)); |
3896 | 18.8k | if (!node) { |
3897 | 0 | yyerror("out of memory"); |
3898 | 0 | free(id); |
3899 | 0 | goto bad; |
3900 | 0 | } |
3901 | 18.8k | memset(node, 0, sizeof(constraint_node_t)); |
3902 | 18.8k | if (useexpr) { |
3903 | 18.8k | node->expr = expr; |
3904 | 18.8k | useexpr = 0; |
3905 | 18.8k | } else { |
3906 | 0 | node->expr = constraint_expr_clone(expr); |
3907 | 0 | } |
3908 | 18.8k | node->permissions = 0; |
3909 | | |
3910 | 18.8k | node->next = cladatum->validatetrans; |
3911 | 18.8k | cladatum->validatetrans = node; |
3912 | | |
3913 | 18.8k | free(id); |
3914 | 18.8k | } |
3915 | | |
3916 | 18.8k | ebitmap_destroy(&classmap); |
3917 | | |
3918 | 18.8k | return 0; |
3919 | | |
3920 | 10 | bad: |
3921 | 10 | ebitmap_destroy(&classmap); |
3922 | 10 | if (useexpr) |
3923 | 9 | constraint_expr_destroy(expr); |
3924 | | |
3925 | 10 | return -1; |
3926 | 18.8k | } |
3927 | | |
3928 | | uintptr_t define_cexpr(uint32_t expr_type, uintptr_t arg1, uintptr_t arg2) |
3929 | 2.46M | { |
3930 | 2.46M | struct constraint_expr *expr, *e1 = NULL, *e2; |
3931 | 2.46M | user_datum_t *user; |
3932 | 2.46M | role_datum_t *role; |
3933 | 2.46M | ebitmap_t negset; |
3934 | 2.46M | char *id; |
3935 | 2.46M | uint32_t val; |
3936 | 2.46M | int add = 1; |
3937 | | |
3938 | 2.46M | if (pass == 1) { |
3939 | 1.26M | if (expr_type == CEXPR_NAMES) { |
3940 | 51.6k | while ((id = queue_remove(id_queue))) |
3941 | 26.1k | free(id); |
3942 | 25.5k | } |
3943 | 1.26M | return 1; /* any non-NULL value */ |
3944 | 1.26M | } |
3945 | | |
3946 | 1.20M | if ((expr = malloc(sizeof(*expr))) == NULL || |
3947 | 1.20M | constraint_expr_init(expr) == -1) { |
3948 | 0 | yyerror("out of memory"); |
3949 | 0 | free(expr); |
3950 | 0 | return 0; |
3951 | 0 | } |
3952 | 1.20M | expr->expr_type = expr_type; |
3953 | | |
3954 | 1.20M | switch (expr_type) { |
3955 | 1.09M | case CEXPR_NOT: |
3956 | 1.09M | e1 = NULL; |
3957 | 1.09M | e2 = (struct constraint_expr *)arg1; |
3958 | 44.5M | while (e2) { |
3959 | 43.4M | e1 = e2; |
3960 | 43.4M | e2 = e2->next; |
3961 | 43.4M | } |
3962 | 1.09M | if (!e1 || e1->next) { |
3963 | 0 | yyerror("illegal constraint expression"); |
3964 | 0 | constraint_expr_destroy(expr); |
3965 | 0 | return 0; |
3966 | 0 | } |
3967 | 1.09M | e1->next = expr; |
3968 | 1.09M | return arg1; |
3969 | 25.0k | case CEXPR_AND: |
3970 | 39.8k | case CEXPR_OR: |
3971 | 39.8k | e1 = NULL; |
3972 | 39.8k | e2 = (struct constraint_expr *)arg1; |
3973 | 105M | while (e2) { |
3974 | 105M | e1 = e2; |
3975 | 105M | e2 = e2->next; |
3976 | 105M | } |
3977 | 39.8k | if (!e1 || e1->next) { |
3978 | 0 | yyerror("illegal constraint expression"); |
3979 | 0 | constraint_expr_destroy(expr); |
3980 | 0 | return 0; |
3981 | 0 | } |
3982 | 39.8k | e1->next = (struct constraint_expr *)arg2; |
3983 | | |
3984 | 39.8k | e1 = NULL; |
3985 | 39.8k | e2 = (struct constraint_expr *)arg2; |
3986 | 1.73M | while (e2) { |
3987 | 1.69M | e1 = e2; |
3988 | 1.69M | e2 = e2->next; |
3989 | 1.69M | } |
3990 | 39.8k | if (!e1 || e1->next) { |
3991 | 0 | yyerror("illegal constraint expression"); |
3992 | 0 | constraint_expr_destroy(expr); |
3993 | 0 | return 0; |
3994 | 0 | } |
3995 | 39.8k | e1->next = expr; |
3996 | 39.8k | return arg1; |
3997 | 41.5k | case CEXPR_ATTR: |
3998 | 41.5k | expr->attr = arg1; |
3999 | 41.5k | expr->op = arg2; |
4000 | 41.5k | return (uintptr_t) expr; |
4001 | 22.3k | case CEXPR_NAMES: |
4002 | 22.3k | add = 1; |
4003 | 22.3k | expr->attr = arg1; |
4004 | 22.3k | expr->op = arg2; |
4005 | 22.3k | ebitmap_init(&negset); |
4006 | 44.6k | while ((id = (char *)queue_remove(id_queue))) { |
4007 | 22.3k | if (expr->attr & CEXPR_USER) { |
4008 | 749 | if (!is_id_in_scope(SYM_USERS, id)) { |
4009 | 0 | yyerror2("user %s is not within scope", |
4010 | 0 | id); |
4011 | 0 | free(id); |
4012 | 0 | constraint_expr_destroy(expr); |
4013 | 0 | return 0; |
4014 | 0 | } |
4015 | 749 | user = |
4016 | 749 | (user_datum_t *) hashtab_search(policydbp-> |
4017 | 749 | p_users. |
4018 | 749 | table, |
4019 | 749 | (hashtab_key_t) |
4020 | 749 | id); |
4021 | 749 | if (!user) { |
4022 | 3 | yyerror2("unknown user %s", id); |
4023 | 3 | free(id); |
4024 | 3 | constraint_expr_destroy(expr); |
4025 | 3 | return 0; |
4026 | 3 | } |
4027 | 746 | val = user->s.value; |
4028 | 21.6k | } else if (expr->attr & CEXPR_ROLE) { |
4029 | 18.5k | if (!is_id_in_scope(SYM_ROLES, id)) { |
4030 | 0 | yyerror2("role %s is not within scope", |
4031 | 0 | id); |
4032 | 0 | constraint_expr_destroy(expr); |
4033 | 0 | free(id); |
4034 | 0 | return 0; |
4035 | 0 | } |
4036 | 18.5k | role = |
4037 | 18.5k | (role_datum_t *) hashtab_search(policydbp-> |
4038 | 18.5k | p_roles. |
4039 | 18.5k | table, |
4040 | 18.5k | (hashtab_key_t) |
4041 | 18.5k | id); |
4042 | 18.5k | if (!role) { |
4043 | 3 | yyerror2("unknown role %s", id); |
4044 | 3 | constraint_expr_destroy(expr); |
4045 | 3 | free(id); |
4046 | 3 | return 0; |
4047 | 3 | } |
4048 | 18.5k | val = role->s.value; |
4049 | 18.5k | } else if (expr->attr & CEXPR_TYPE) { |
4050 | 3.01k | if (set_types(expr->type_names, id, &add, 0)) { |
4051 | 4 | constraint_expr_destroy(expr); |
4052 | 4 | return 0; |
4053 | 4 | } |
4054 | 3.01k | continue; |
4055 | 3.01k | } else { |
4056 | 0 | yyerror("invalid constraint expression"); |
4057 | 0 | constraint_expr_destroy(expr); |
4058 | 0 | free(id); |
4059 | 0 | return 0; |
4060 | 0 | } |
4061 | 19.3k | if (ebitmap_set_bit(&expr->names, val - 1, TRUE)) { |
4062 | 0 | yyerror("out of memory"); |
4063 | 0 | ebitmap_destroy(&expr->names); |
4064 | 0 | free(id); |
4065 | 0 | constraint_expr_destroy(expr); |
4066 | 0 | return 0; |
4067 | 0 | } |
4068 | 19.3k | free(id); |
4069 | 19.3k | } |
4070 | 22.3k | ebitmap_destroy(&negset); |
4071 | 22.3k | return (uintptr_t) expr; |
4072 | 0 | default: |
4073 | 0 | break; |
4074 | 1.20M | } |
4075 | | |
4076 | 0 | yyerror("invalid constraint expression"); |
4077 | 0 | constraint_expr_destroy(expr); |
4078 | 0 | return 0; |
4079 | 1.20M | } |
4080 | | |
4081 | | int define_conditional(cond_expr_t * expr, avrule_t * t_list, avrule_t * f_list) |
4082 | 2.37k | { |
4083 | 2.37k | cond_expr_t *e; |
4084 | 2.37k | int depth, booleans, tunables; |
4085 | 2.37k | cond_node_t cn, *cn_old; |
4086 | 2.37k | const cond_bool_datum_t *bool_var; |
4087 | | |
4088 | | /* expression cannot be NULL */ |
4089 | 2.37k | if (!expr) { |
4090 | 7 | yyerror("illegal conditional expression"); |
4091 | 7 | return -1; |
4092 | 7 | } |
4093 | 2.36k | if (!t_list) { |
4094 | 188 | if (!f_list) { |
4095 | | /* empty is fine, destroy expression and return */ |
4096 | 141 | cond_expr_destroy(expr); |
4097 | 141 | return 0; |
4098 | 141 | } |
4099 | | /* Invert */ |
4100 | 47 | t_list = f_list; |
4101 | 47 | f_list = NULL; |
4102 | 47 | expr = define_cond_expr(COND_NOT, expr, 0); |
4103 | 47 | if (!expr) { |
4104 | 0 | yyerror("unable to invert conditional expression"); |
4105 | 0 | return -1; |
4106 | 0 | } |
4107 | 47 | } |
4108 | | |
4109 | | /* verify expression */ |
4110 | 2.22k | depth = -1; |
4111 | 2.22k | booleans = 0; |
4112 | 2.22k | tunables = 0; |
4113 | 252k | for (e = expr; e; e = e->next) { |
4114 | 249k | switch (e->expr_type) { |
4115 | 46.8k | case COND_NOT: |
4116 | 46.8k | if (depth < 0) { |
4117 | 0 | yyerror |
4118 | 0 | ("illegal conditional expression; Bad NOT"); |
4119 | 0 | return -1; |
4120 | 0 | } |
4121 | 46.8k | break; |
4122 | 46.8k | case COND_AND: |
4123 | 1.22k | case COND_OR: |
4124 | 93.0k | case COND_XOR: |
4125 | 94.4k | case COND_EQ: |
4126 | 100k | case COND_NEQ: |
4127 | 100k | if (depth < 1) { |
4128 | 0 | yyerror |
4129 | 0 | ("illegal conditional expression; Bad binary op"); |
4130 | 0 | return -1; |
4131 | 0 | } |
4132 | 100k | depth--; |
4133 | 100k | break; |
4134 | 102k | case COND_BOOL: |
4135 | 102k | if (depth == (COND_EXPR_MAXDEPTH - 1)) { |
4136 | 0 | yyerror |
4137 | 0 | ("conditional expression is like totally too deep"); |
4138 | 0 | return -1; |
4139 | 0 | } |
4140 | 102k | depth++; |
4141 | | |
4142 | 102k | bool_var = policydbp->bool_val_to_struct[e->boolean - 1]; |
4143 | 102k | if (bool_var->flags & COND_BOOL_FLAGS_TUNABLE) { |
4144 | 259 | tunables = 1; |
4145 | 102k | } else { |
4146 | 102k | booleans = 1; |
4147 | 102k | } |
4148 | | |
4149 | 102k | break; |
4150 | 0 | default: |
4151 | 0 | yyerror("illegal conditional expression"); |
4152 | 0 | return -1; |
4153 | 249k | } |
4154 | 249k | } |
4155 | 2.22k | if (depth != 0) { |
4156 | 0 | yyerror("illegal conditional expression"); |
4157 | 0 | return -1; |
4158 | 0 | } |
4159 | 2.22k | if (booleans && tunables) { |
4160 | 0 | yyerror("illegal conditional expression; Contains boolean and tunable"); |
4161 | 0 | return -1; |
4162 | 0 | } |
4163 | | |
4164 | | /* use tmp conditional node to partially build new node */ |
4165 | 2.22k | memset(&cn, 0, sizeof(cn)); |
4166 | 2.22k | cn.expr = expr; |
4167 | 2.22k | cn.avtrue_list = t_list; |
4168 | 2.22k | cn.avfalse_list = f_list; |
4169 | | |
4170 | | /* normalize/precompute expression */ |
4171 | 2.22k | if (cond_normalize_expr(policydbp, &cn) < 0) { |
4172 | 0 | yyerror("problem normalizing conditional expression"); |
4173 | 0 | return -1; |
4174 | 0 | } |
4175 | | |
4176 | | /* get the existing conditional node, or create a new one */ |
4177 | 2.22k | cn_old = get_current_cond_list(&cn); |
4178 | 2.22k | if (!cn_old) { |
4179 | 0 | return -1; |
4180 | 0 | } |
4181 | | |
4182 | 2.22k | append_cond_list(&cn); |
4183 | | |
4184 | | /* note that there is no check here for duplicate rules, nor |
4185 | | * check that rule already exists in base -- that will be |
4186 | | * handled during conditional expansion, in expand.c */ |
4187 | | |
4188 | 2.22k | cn.avtrue_list = NULL; |
4189 | 2.22k | cn.avfalse_list = NULL; |
4190 | 2.22k | cond_node_destroy(&cn); |
4191 | | |
4192 | 2.22k | return 0; |
4193 | 2.22k | } |
4194 | | |
4195 | | cond_expr_t *define_cond_expr(uint32_t expr_type, void *arg1, void *arg2) |
4196 | 526k | { |
4197 | 526k | struct cond_expr *expr, *e1 = NULL, *e2; |
4198 | 526k | cond_bool_datum_t *bool_var; |
4199 | 526k | char *id; |
4200 | | |
4201 | | /* expressions are handled in the second pass */ |
4202 | 526k | if (pass == 1) { |
4203 | 273k | if (expr_type == COND_BOOL) { |
4204 | 213k | while ((id = queue_remove(id_queue))) { |
4205 | 106k | free(id); |
4206 | 106k | } |
4207 | 106k | } |
4208 | 273k | return (cond_expr_t *) 1; /* any non-NULL value */ |
4209 | 273k | } |
4210 | | |
4211 | | /* create a new expression struct */ |
4212 | 252k | expr = malloc(sizeof(struct cond_expr)); |
4213 | 252k | if (!expr) { |
4214 | 0 | yyerror("out of memory"); |
4215 | 0 | return NULL; |
4216 | 0 | } |
4217 | 252k | memset(expr, 0, sizeof(cond_expr_t)); |
4218 | 252k | expr->expr_type = expr_type; |
4219 | | |
4220 | | /* create the type asked for */ |
4221 | 252k | switch (expr_type) { |
4222 | 48.2k | case COND_NOT: |
4223 | 48.2k | e1 = NULL; |
4224 | 48.2k | e2 = (struct cond_expr *)arg1; |
4225 | 584k | while (e2) { |
4226 | 536k | e1 = e2; |
4227 | 536k | e2 = e2->next; |
4228 | 536k | } |
4229 | 48.2k | if (!e1 || e1->next) { |
4230 | 12 | yyerror("illegal conditional NOT expression"); |
4231 | 12 | free(expr); |
4232 | 12 | return NULL; |
4233 | 12 | } |
4234 | 48.2k | e1->next = expr; |
4235 | 48.2k | return (struct cond_expr *)arg1; |
4236 | 992 | case COND_AND: |
4237 | 1.49k | case COND_OR: |
4238 | 93.3k | case COND_XOR: |
4239 | 94.8k | case COND_EQ: |
4240 | 100k | case COND_NEQ: |
4241 | 100k | e1 = NULL; |
4242 | 100k | e2 = (struct cond_expr *)arg1; |
4243 | 1.24G | while (e2) { |
4244 | 1.24G | e1 = e2; |
4245 | 1.24G | e2 = e2->next; |
4246 | 1.24G | } |
4247 | 100k | if (!e1 || e1->next) { |
4248 | 2 | yyerror |
4249 | 2 | ("illegal left side of conditional binary op expression"); |
4250 | 2 | free(expr); |
4251 | 2 | return NULL; |
4252 | 2 | } |
4253 | 100k | e1->next = (struct cond_expr *)arg2; |
4254 | | |
4255 | 100k | e1 = NULL; |
4256 | 100k | e2 = (struct cond_expr *)arg2; |
4257 | 292k | while (e2) { |
4258 | 191k | e1 = e2; |
4259 | 191k | e2 = e2->next; |
4260 | 191k | } |
4261 | 100k | if (!e1 || e1->next) { |
4262 | 9 | yyerror |
4263 | 9 | ("illegal right side of conditional binary op expression"); |
4264 | 9 | cond_expr_destroy(arg1); |
4265 | 9 | free(expr); |
4266 | 9 | return NULL; |
4267 | 9 | } |
4268 | 100k | e1->next = expr; |
4269 | 100k | return (struct cond_expr *)arg1; |
4270 | 103k | case COND_BOOL: |
4271 | 103k | id = (char *)queue_remove(id_queue); |
4272 | 103k | if (!id) { |
4273 | 0 | yyerror("bad conditional; expected boolean id"); |
4274 | 0 | free(id); |
4275 | 0 | free(expr); |
4276 | 0 | return NULL; |
4277 | 0 | } |
4278 | 103k | if (!is_id_in_scope(SYM_BOOLS, id)) { |
4279 | 2 | yyerror2("boolean %s is not within scope", id); |
4280 | 2 | free(id); |
4281 | 2 | free(expr); |
4282 | 2 | return NULL; |
4283 | 2 | } |
4284 | 103k | bool_var = |
4285 | 103k | (cond_bool_datum_t *) hashtab_search(policydbp->p_bools. |
4286 | 103k | table, |
4287 | 103k | (hashtab_key_t) id); |
4288 | 103k | if (!bool_var) { |
4289 | 33 | yyerror2("unknown boolean %s in conditional expression", |
4290 | 33 | id); |
4291 | 33 | free(expr); |
4292 | 33 | free(id); |
4293 | 33 | return NULL; |
4294 | 33 | } |
4295 | 103k | expr->boolean = bool_var->s.value; |
4296 | 103k | free(id); |
4297 | 103k | return expr; |
4298 | 0 | default: |
4299 | 0 | yyerror("illegal conditional expression"); |
4300 | 0 | free(expr); |
4301 | 0 | return NULL; |
4302 | 252k | } |
4303 | 252k | } |
4304 | | |
4305 | | static int set_user_roles(role_set_t * set, char *id) |
4306 | 4.41k | { |
4307 | 4.41k | role_datum_t *r; |
4308 | | |
4309 | 4.41k | if (strcmp(id, "*") == 0) { |
4310 | 2 | free(id); |
4311 | 2 | yyerror("* is not allowed in user declarations"); |
4312 | 2 | return -1; |
4313 | 2 | } |
4314 | | |
4315 | 4.41k | if (strcmp(id, "~") == 0) { |
4316 | 0 | free(id); |
4317 | 0 | yyerror("~ is not allowed in user declarations"); |
4318 | 0 | return -1; |
4319 | 0 | } |
4320 | | |
4321 | 4.41k | if (!is_id_in_scope(SYM_ROLES, id)) { |
4322 | 12 | yyerror2("role %s is not within scope", id); |
4323 | 12 | free(id); |
4324 | 12 | return -1; |
4325 | 12 | } |
4326 | 4.39k | r = hashtab_search(policydbp->p_roles.table, id); |
4327 | 4.39k | if (!r) { |
4328 | 253 | yyerror2("unknown role %s", id); |
4329 | 253 | free(id); |
4330 | 253 | return -1; |
4331 | 253 | } |
4332 | | |
4333 | 4.14k | free(id); |
4334 | 4.14k | if (ebitmap_set_bit(&set->roles, r->s.value - 1, TRUE)) |
4335 | 0 | goto oom; |
4336 | 4.14k | return 0; |
4337 | 0 | oom: |
4338 | 0 | yyerror("out of memory"); |
4339 | 0 | return -1; |
4340 | 4.14k | } |
4341 | | |
4342 | | static int parse_categories(char *id, level_datum_t * levdatum, ebitmap_t * cats) |
4343 | 6 | { |
4344 | 6 | cat_datum_t *cdatum; |
4345 | 6 | uint32_t range_start, range_end, i; |
4346 | | |
4347 | 6 | if (id_has_dot(id)) { |
4348 | 2 | char *id_start = id; |
4349 | 2 | char *id_end = strchr(id, '.'); |
4350 | | |
4351 | 2 | *(id_end++) = '\0'; |
4352 | | |
4353 | 2 | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4354 | 2 | (hashtab_key_t) |
4355 | 2 | id_start); |
4356 | 2 | if (!cdatum) { |
4357 | 1 | yyerror2("unknown category %s", id_start); |
4358 | 1 | return -1; |
4359 | 1 | } |
4360 | 1 | range_start = cdatum->s.value - 1; |
4361 | 1 | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4362 | 1 | (hashtab_key_t) id_end); |
4363 | 1 | if (!cdatum) { |
4364 | 1 | yyerror2("unknown category %s", id_end); |
4365 | 1 | return -1; |
4366 | 1 | } |
4367 | 0 | range_end = cdatum->s.value - 1; |
4368 | |
|
4369 | 0 | if (range_end < range_start) { |
4370 | 0 | yyerror2("category range %d-%d is invalid", range_start, range_end); |
4371 | 0 | return -1; |
4372 | 0 | } |
4373 | 4 | } else { |
4374 | 4 | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4375 | 4 | (hashtab_key_t) id); |
4376 | 4 | if (!cdatum) { |
4377 | 0 | yyerror2("unknown category %s", id); |
4378 | 0 | return -1; |
4379 | 0 | } |
4380 | 4 | range_start = range_end = cdatum->s.value - 1; |
4381 | 4 | } |
4382 | | |
4383 | 7 | for (i = range_start; i <= range_end; i++) { |
4384 | 4 | if (!ebitmap_get_bit(&levdatum->level->cat, i)) { |
4385 | 1 | uint32_t level_value = levdatum->level->sens - 1; |
4386 | 1 | policydb_index_others(NULL, policydbp, 0); |
4387 | 1 | yyerror2("category %s can not be associated " |
4388 | 1 | "with level %s", |
4389 | 1 | policydbp->p_cat_val_to_name[i], |
4390 | 1 | policydbp->p_sens_val_to_name[level_value]); |
4391 | 1 | return -1; |
4392 | 1 | } |
4393 | 3 | if (ebitmap_set_bit(cats, i, TRUE)) { |
4394 | 0 | yyerror("out of memory"); |
4395 | 0 | return -1; |
4396 | 0 | } |
4397 | 3 | } |
4398 | | |
4399 | 3 | return 0; |
4400 | 4 | } |
4401 | | |
4402 | | static int mls_semantic_cats_merge(mls_semantic_cat_t ** dst, |
4403 | | const mls_semantic_cat_t * src) |
4404 | 4.66k | { |
4405 | 4.66k | mls_semantic_cat_t *new; |
4406 | | |
4407 | 20.6k | while (src) { |
4408 | 15.9k | new = (mls_semantic_cat_t *) malloc(sizeof(mls_semantic_cat_t)); |
4409 | 15.9k | if (!new) |
4410 | 0 | return -1; |
4411 | | |
4412 | 15.9k | mls_semantic_cat_init(new); |
4413 | 15.9k | new->low = src->low; |
4414 | 15.9k | new->high = src->high; |
4415 | 15.9k | new->next = *dst; |
4416 | 15.9k | *dst = new; |
4417 | | |
4418 | 15.9k | src = src->next; |
4419 | 15.9k | } |
4420 | | |
4421 | 4.66k | return 0; |
4422 | 4.66k | } |
4423 | | |
4424 | | static int mls_add_or_check_level(mls_semantic_level_t *dst, const mls_semantic_level_t *src) |
4425 | 6.49k | { |
4426 | 6.49k | if (!dst->sens) { |
4427 | 1.82k | if (mls_semantic_level_cpy(dst, src) < 0) { |
4428 | 0 | yyerror("out of memory"); |
4429 | 0 | return -1; |
4430 | 0 | } |
4431 | 4.66k | } else { |
4432 | 4.66k | if (dst->sens != src->sens) { |
4433 | 1 | return -1; |
4434 | 1 | } |
4435 | | /* Duplicate cats won't cause problems, but different cats will |
4436 | | * result in an error during expansion */ |
4437 | 4.66k | if (mls_semantic_cats_merge(&dst->cat, src->cat) < 0) { |
4438 | 0 | yyerror("out of memory"); |
4439 | 0 | return -1; |
4440 | 0 | } |
4441 | 4.66k | } |
4442 | | |
4443 | 6.49k | return 0; |
4444 | 6.49k | } |
4445 | | |
4446 | | static int parse_semantic_categories(char *id, level_datum_t * levdatum __attribute__ ((unused)), |
4447 | | mls_semantic_cat_t ** cats) |
4448 | 3.10k | { |
4449 | 3.10k | cat_datum_t *cdatum; |
4450 | 3.10k | mls_semantic_cat_t *newcat; |
4451 | 3.10k | unsigned int range_start, range_end; |
4452 | | |
4453 | 3.10k | if (id_has_dot(id)) { |
4454 | 1 | char *id_start = id; |
4455 | 1 | char *id_end = strchr(id, '.'); |
4456 | | |
4457 | 1 | *(id_end++) = '\0'; |
4458 | | |
4459 | 1 | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4460 | 1 | (hashtab_key_t) |
4461 | 1 | id_start); |
4462 | 1 | if (!cdatum) { |
4463 | 0 | yyerror2("unknown category %s", id_start); |
4464 | 0 | return -1; |
4465 | 0 | } |
4466 | 1 | range_start = cdatum->s.value; |
4467 | | |
4468 | 1 | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4469 | 1 | (hashtab_key_t) id_end); |
4470 | 1 | if (!cdatum) { |
4471 | 0 | yyerror2("unknown category %s", id_end); |
4472 | 0 | return -1; |
4473 | 0 | } |
4474 | 1 | range_end = cdatum->s.value; |
4475 | 3.10k | } else { |
4476 | 3.10k | cdatum = (cat_datum_t *) hashtab_search(policydbp->p_cats.table, |
4477 | 3.10k | (hashtab_key_t) id); |
4478 | 3.10k | if (!cdatum) { |
4479 | 10 | yyerror2("unknown category %s", id); |
4480 | 10 | return -1; |
4481 | 10 | } |
4482 | 3.09k | range_start = range_end = cdatum->s.value; |
4483 | 3.09k | } |
4484 | | |
4485 | 3.09k | newcat = (mls_semantic_cat_t *) malloc(sizeof(mls_semantic_cat_t)); |
4486 | 3.09k | if (!newcat) { |
4487 | 0 | yyerror("out of memory"); |
4488 | 0 | return -1; |
4489 | 0 | } |
4490 | | |
4491 | 3.09k | mls_semantic_cat_init(newcat); |
4492 | 3.09k | newcat->next = *cats; |
4493 | 3.09k | newcat->low = range_start; |
4494 | 3.09k | newcat->high = range_end; |
4495 | | |
4496 | 3.09k | *cats = newcat; |
4497 | | |
4498 | 3.09k | return 0; |
4499 | 3.09k | } |
4500 | | |
4501 | | int define_user(void) |
4502 | 28.9k | { |
4503 | 28.9k | const char *username; |
4504 | 28.9k | char *id; |
4505 | 28.9k | user_datum_t *usrdatum, *usr_global; |
4506 | 28.9k | level_datum_t *levdatum; |
4507 | 28.9k | int l; |
4508 | | |
4509 | 28.9k | if (pass == 1) { |
4510 | 69.2k | while ((id = queue_remove(id_queue))) |
4511 | 44.6k | free(id); |
4512 | 24.5k | if (mlspol) { |
4513 | 8.47k | while ((id = queue_remove(id_queue))) |
4514 | 4.09k | free(id); |
4515 | 4.38k | id = queue_remove(id_queue); |
4516 | 4.38k | free(id); |
4517 | 7.54k | for (l = 0; l < 2; l++) { |
4518 | 11.3k | while ((id = queue_remove(id_queue))) { |
4519 | 3.76k | free(id); |
4520 | 3.76k | } |
4521 | 7.54k | id = queue_remove(id_queue); |
4522 | 7.54k | if (!id) |
4523 | 4.38k | break; |
4524 | 3.15k | free(id); |
4525 | 3.15k | } |
4526 | 4.38k | } |
4527 | 24.5k | return 0; |
4528 | 24.5k | } |
4529 | | |
4530 | 4.41k | username = queue_head(id_queue); |
4531 | 4.41k | if (!username) { |
4532 | 0 | yyerror("no user name"); |
4533 | 0 | return -1; |
4534 | 0 | } |
4535 | | |
4536 | 4.41k | id = strdup(username); |
4537 | | |
4538 | 4.41k | if ((usrdatum = declare_user()) == NULL) { |
4539 | 2 | free(id); |
4540 | 2 | return -1; |
4541 | 2 | } |
4542 | | |
4543 | 4.41k | usr_global = hashtab_search(policydbp->p_users.table, (hashtab_key_t) id); |
4544 | 4.41k | free(id); |
4545 | | |
4546 | 8.55k | while ((id = queue_remove(id_queue))) { |
4547 | 4.41k | if (set_user_roles(&usrdatum->roles, id)) |
4548 | 267 | return -1; |
4549 | 4.41k | } |
4550 | | |
4551 | 4.14k | if (mlspol) { |
4552 | 3.31k | id = queue_remove(id_queue); |
4553 | 3.31k | if (!id) { |
4554 | 1 | yyerror("no default level specified for user"); |
4555 | 1 | return -1; |
4556 | 1 | } |
4557 | | |
4558 | 3.31k | levdatum = (level_datum_t *) |
4559 | 3.31k | hashtab_search(policydbp->p_levels.table, |
4560 | 3.31k | (hashtab_key_t) id); |
4561 | 3.31k | if (!levdatum) { |
4562 | 10 | yyerror2("unknown sensitivity %s used in user" |
4563 | 10 | " level definition", id); |
4564 | 10 | free(id); |
4565 | 10 | return -1; |
4566 | 10 | } |
4567 | 3.30k | free(id); |
4568 | | |
4569 | 3.30k | usrdatum->dfltlevel.sens = levdatum->level->sens; |
4570 | | |
4571 | 3.44k | while ((id = queue_remove(id_queue))) { |
4572 | | /* This will add to any already existing categories */ |
4573 | 135 | if (parse_semantic_categories(id, levdatum, |
4574 | 135 | &usrdatum->dfltlevel.cat)) { |
4575 | 2 | free(id); |
4576 | 2 | return -1; |
4577 | 2 | } |
4578 | 133 | free(id); |
4579 | 133 | } |
4580 | | |
4581 | 3.30k | id = queue_remove(id_queue); |
4582 | | |
4583 | 5.87k | for (l = 0; l < 2; l++) { |
4584 | 5.87k | levdatum = (level_datum_t *) |
4585 | 5.87k | hashtab_search(policydbp->p_levels.table, |
4586 | 5.87k | (hashtab_key_t) id); |
4587 | 5.87k | if (!levdatum) { |
4588 | 5 | yyerror2("unknown sensitivity %s used in user" |
4589 | 5 | " range definition", id); |
4590 | 5 | free(id); |
4591 | 5 | return -1; |
4592 | 5 | } |
4593 | 5.86k | free(id); |
4594 | | |
4595 | 5.86k | usrdatum->range.level[l].sens = levdatum->level->sens; |
4596 | | |
4597 | 8.83k | while ((id = queue_remove(id_queue))) { |
4598 | | /* This will add to any already existing categories */ |
4599 | 2.97k | if (parse_semantic_categories(id, levdatum, |
4600 | 2.97k | &usrdatum->range.level[l].cat)) { |
4601 | 8 | free(id); |
4602 | 8 | return -1; |
4603 | 8 | } |
4604 | 2.96k | free(id); |
4605 | 2.96k | } |
4606 | | |
4607 | 5.85k | id = queue_remove(id_queue); |
4608 | 5.85k | if (!id) |
4609 | 3.29k | break; |
4610 | 5.85k | } |
4611 | | |
4612 | 3.29k | if (l == 0) { |
4613 | 735 | if (mls_semantic_level_cpy(&usrdatum->range.level[1], |
4614 | 735 | &usrdatum->range.level[0])) { |
4615 | 0 | yyerror("out of memory"); |
4616 | 0 | return -1; |
4617 | 0 | } |
4618 | 735 | } |
4619 | | |
4620 | 3.29k | if (usr_global && usr_global != usrdatum) { |
4621 | 2.16k | if (mls_add_or_check_level(&usr_global->dfltlevel, |
4622 | 2.16k | &usrdatum->dfltlevel)) { |
4623 | 0 | yyerror("Problem with user default level"); |
4624 | 0 | return -1; |
4625 | 0 | } |
4626 | 2.16k | if (mls_add_or_check_level(&usr_global->range.level[0], |
4627 | 2.16k | &usrdatum->range.level[0])) { |
4628 | 1 | yyerror("Problem with user low level"); |
4629 | 1 | return -1; |
4630 | 1 | } |
4631 | 2.16k | if (mls_add_or_check_level(&usr_global->range.level[1], |
4632 | 2.16k | &usrdatum->range.level[1])) { |
4633 | 0 | yyerror("Problem with user high level"); |
4634 | 0 | return -1; |
4635 | 0 | } |
4636 | 2.16k | } |
4637 | 3.29k | } |
4638 | 4.11k | return 0; |
4639 | 4.14k | } |
4640 | | |
4641 | | static int parse_security_context(context_struct_t * c) |
4642 | 6.75k | { |
4643 | 6.75k | char *id; |
4644 | 6.75k | role_datum_t *role; |
4645 | 6.75k | type_datum_t *typdatum; |
4646 | 6.75k | user_datum_t *usrdatum; |
4647 | 6.75k | level_datum_t *levdatum; |
4648 | 6.75k | int l; |
4649 | | |
4650 | 6.75k | if (pass == 1) { |
4651 | 5.24k | id = queue_remove(id_queue); |
4652 | 5.24k | free(id); /* user */ |
4653 | 5.24k | id = queue_remove(id_queue); |
4654 | 5.24k | free(id); /* role */ |
4655 | 5.24k | id = queue_remove(id_queue); |
4656 | 5.24k | free(id); /* type */ |
4657 | 5.24k | if (mlspol) { |
4658 | 3.65k | id = queue_remove(id_queue); |
4659 | 3.65k | free(id); |
4660 | 3.78k | for (l = 0; l < 2; l++) { |
4661 | 4.56k | while ((id = queue_remove(id_queue))) { |
4662 | 777 | free(id); |
4663 | 777 | } |
4664 | 3.78k | id = queue_remove(id_queue); |
4665 | 3.78k | if (!id) |
4666 | 3.65k | break; |
4667 | 131 | free(id); |
4668 | 131 | } |
4669 | 3.65k | } |
4670 | 5.24k | return 0; |
4671 | 5.24k | } |
4672 | | |
4673 | | /* check context c to make sure ok to dereference c later */ |
4674 | 1.50k | if (c == NULL) { |
4675 | 0 | yyerror("null context pointer!"); |
4676 | 0 | return -1; |
4677 | 0 | } |
4678 | | |
4679 | 1.50k | context_init(c); |
4680 | | |
4681 | | /* extract the user */ |
4682 | 1.50k | id = queue_remove(id_queue); |
4683 | 1.50k | if (!id) { |
4684 | 0 | yyerror("no effective user?"); |
4685 | 0 | goto bad; |
4686 | 0 | } |
4687 | 1.50k | if (!is_id_in_scope(SYM_USERS, id)) { |
4688 | 0 | yyerror2("user %s is not within scope", id); |
4689 | 0 | free(id); |
4690 | 0 | goto bad; |
4691 | 0 | } |
4692 | 1.50k | usrdatum = (user_datum_t *) hashtab_search(policydbp->p_users.table, |
4693 | 1.50k | (hashtab_key_t) id); |
4694 | 1.50k | if (!usrdatum) { |
4695 | 23 | yyerror2("user %s is not defined", id); |
4696 | 23 | free(id); |
4697 | 23 | goto bad; |
4698 | 23 | } |
4699 | 1.47k | c->user = usrdatum->s.value; |
4700 | | |
4701 | | /* no need to keep the user name */ |
4702 | 1.47k | free(id); |
4703 | | |
4704 | | /* extract the role */ |
4705 | 1.47k | id = (char *)queue_remove(id_queue); |
4706 | 1.47k | if (!id) { |
4707 | 0 | yyerror("no role name for sid context definition?"); |
4708 | 0 | return -1; |
4709 | 0 | } |
4710 | 1.47k | if (!is_id_in_scope(SYM_ROLES, id)) { |
4711 | 1 | yyerror2("role %s is not within scope", id); |
4712 | 1 | free(id); |
4713 | 1 | return -1; |
4714 | 1 | } |
4715 | 1.47k | role = (role_datum_t *) hashtab_search(policydbp->p_roles.table, |
4716 | 1.47k | (hashtab_key_t) id); |
4717 | 1.47k | if (!role) { |
4718 | 15 | yyerror2("role %s is not defined", id); |
4719 | 15 | free(id); |
4720 | 15 | return -1; |
4721 | 15 | } |
4722 | 1.46k | c->role = role->s.value; |
4723 | | |
4724 | | /* no need to keep the role name */ |
4725 | 1.46k | free(id); |
4726 | | |
4727 | | /* extract the type */ |
4728 | 1.46k | id = (char *)queue_remove(id_queue); |
4729 | 1.46k | if (!id) { |
4730 | 0 | yyerror("no type name for sid context definition?"); |
4731 | 0 | return -1; |
4732 | 0 | } |
4733 | 1.46k | if (!is_id_in_scope(SYM_TYPES, id)) { |
4734 | 0 | yyerror2("type %s is not within scope", id); |
4735 | 0 | free(id); |
4736 | 0 | return -1; |
4737 | 0 | } |
4738 | 1.46k | typdatum = (type_datum_t *) hashtab_search(policydbp->p_types.table, |
4739 | 1.46k | (hashtab_key_t) id); |
4740 | 1.46k | if (!typdatum || typdatum->flavor == TYPE_ATTRIB) { |
4741 | 19 | yyerror2("type %s is not defined or is an attribute", id); |
4742 | 19 | free(id); |
4743 | 19 | return -1; |
4744 | 19 | } |
4745 | 1.44k | c->type = typdatum->s.value; |
4746 | | |
4747 | | /* no need to keep the type name */ |
4748 | 1.44k | free(id); |
4749 | | |
4750 | 1.44k | if (mlspol) { |
4751 | | /* extract the low sensitivity */ |
4752 | 1.43k | id = (char *)queue_remove(id_queue); |
4753 | 1.43k | if (!id) { |
4754 | 1 | yyerror("no sensitivity name for sid context" |
4755 | 1 | " definition?"); |
4756 | 1 | return -1; |
4757 | 1 | } |
4758 | | |
4759 | 1.43k | for (l = 0; l < 2; l++) { |
4760 | 1.43k | levdatum = (level_datum_t *) |
4761 | 1.43k | hashtab_search(policydbp->p_levels.table, |
4762 | 1.43k | (hashtab_key_t) id); |
4763 | 1.43k | if (!levdatum) { |
4764 | 5 | yyerror2("Sensitivity %s is not defined", id); |
4765 | 5 | free(id); |
4766 | 5 | return -1; |
4767 | 5 | } |
4768 | 1.42k | free(id); |
4769 | 1.42k | c->range.level[l].sens = levdatum->level->sens; |
4770 | | |
4771 | | /* extract low category set */ |
4772 | 1.43k | while ((id = queue_remove(id_queue))) { |
4773 | 6 | if (parse_categories(id, levdatum, |
4774 | 6 | &c->range.level[l].cat)) { |
4775 | 3 | free(id); |
4776 | 3 | return -1; |
4777 | 3 | } |
4778 | 3 | free(id); |
4779 | 3 | } |
4780 | | |
4781 | | /* extract high sensitivity */ |
4782 | 1.42k | id = (char *)queue_remove(id_queue); |
4783 | 1.42k | if (!id) |
4784 | 1.42k | break; |
4785 | 1.42k | } |
4786 | | |
4787 | 1.42k | if (l == 0) { |
4788 | 1.42k | c->range.level[1].sens = c->range.level[0].sens; |
4789 | 1.42k | if (ebitmap_cpy(&c->range.level[1].cat, |
4790 | 1.42k | &c->range.level[0].cat)) { |
4791 | |
|
4792 | 0 | yyerror("out of memory"); |
4793 | 0 | goto bad; |
4794 | 0 | } |
4795 | 1.42k | } |
4796 | 1.42k | } |
4797 | | |
4798 | 1.43k | if (!policydb_context_isvalid(policydbp, c)) { |
4799 | 9 | yyerror("invalid security context"); |
4800 | 9 | goto bad; |
4801 | 9 | } |
4802 | 1.42k | return 0; |
4803 | | |
4804 | 32 | bad: |
4805 | 32 | context_destroy(c); |
4806 | | |
4807 | 32 | return -1; |
4808 | 1.43k | } |
4809 | | |
4810 | | int define_initial_sid_context(void) |
4811 | 2.91k | { |
4812 | 2.91k | char *id; |
4813 | 2.91k | ocontext_t *c, *head; |
4814 | | |
4815 | 2.91k | if (pass == 1) { |
4816 | 1.93k | id = (char *)queue_remove(id_queue); |
4817 | 1.93k | free(id); |
4818 | 1.93k | parse_security_context(NULL); |
4819 | 1.93k | return 0; |
4820 | 1.93k | } |
4821 | | |
4822 | 973 | id = (char *)queue_remove(id_queue); |
4823 | 973 | if (!id) { |
4824 | 0 | yyerror("no sid name for SID context definition?"); |
4825 | 0 | return -1; |
4826 | 0 | } |
4827 | 973 | head = policydbp->ocontexts[OCON_ISID]; |
4828 | 1.84k | for (c = head; c; c = c->next) { |
4829 | 1.79k | if (!strcmp(id, c->u.name)) |
4830 | 920 | break; |
4831 | 1.79k | } |
4832 | | |
4833 | 973 | if (!c) { |
4834 | 53 | yyerror2("SID %s is not defined", id); |
4835 | 53 | free(id); |
4836 | 53 | return -1; |
4837 | 53 | } |
4838 | 920 | if (c->context[0].user) { |
4839 | 4 | yyerror2("The context for SID %s is multiply defined", id); |
4840 | 4 | free(id); |
4841 | 4 | return -1; |
4842 | 4 | } |
4843 | | /* no need to keep the sid name */ |
4844 | 916 | free(id); |
4845 | | |
4846 | 916 | if (parse_security_context(&c->context[0])) |
4847 | 21 | return -1; |
4848 | | |
4849 | 895 | return 0; |
4850 | 916 | } |
4851 | | |
4852 | | int define_fs_context(unsigned int major, unsigned int minor) |
4853 | 0 | { |
4854 | 0 | ocontext_t *newc, *c, *head; |
4855 | |
|
4856 | 0 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
4857 | 0 | yyerror("fscon not supported for target"); |
4858 | 0 | return -1; |
4859 | 0 | } |
4860 | | |
4861 | 0 | if (pass == 1) { |
4862 | 0 | parse_security_context(NULL); |
4863 | 0 | parse_security_context(NULL); |
4864 | 0 | return 0; |
4865 | 0 | } |
4866 | | |
4867 | 0 | newc = (ocontext_t *) malloc(sizeof(ocontext_t)); |
4868 | 0 | if (!newc) { |
4869 | 0 | yyerror("out of memory"); |
4870 | 0 | return -1; |
4871 | 0 | } |
4872 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
4873 | |
|
4874 | 0 | newc->u.name = (char *)malloc(6); |
4875 | 0 | if (!newc->u.name) { |
4876 | 0 | yyerror("out of memory"); |
4877 | 0 | free(newc); |
4878 | 0 | return -1; |
4879 | 0 | } |
4880 | 0 | sprintf(newc->u.name, "%02x:%02x", major, minor); |
4881 | |
|
4882 | 0 | if (parse_security_context(&newc->context[0])) { |
4883 | 0 | free(newc->u.name); |
4884 | 0 | free(newc); |
4885 | 0 | return -1; |
4886 | 0 | } |
4887 | 0 | if (parse_security_context(&newc->context[1])) { |
4888 | 0 | context_destroy(&newc->context[0]); |
4889 | 0 | free(newc->u.name); |
4890 | 0 | free(newc); |
4891 | 0 | return -1; |
4892 | 0 | } |
4893 | 0 | head = policydbp->ocontexts[OCON_FS]; |
4894 | |
|
4895 | 0 | for (c = head; c; c = c->next) { |
4896 | 0 | if (!strcmp(newc->u.name, c->u.name)) { |
4897 | 0 | yyerror2("duplicate entry for file system %s", |
4898 | 0 | newc->u.name); |
4899 | 0 | context_destroy(&newc->context[0]); |
4900 | 0 | context_destroy(&newc->context[1]); |
4901 | 0 | free(newc->u.name); |
4902 | 0 | free(newc); |
4903 | 0 | return -1; |
4904 | 0 | } |
4905 | 0 | } |
4906 | | |
4907 | 0 | newc->next = head; |
4908 | 0 | policydbp->ocontexts[OCON_FS] = newc; |
4909 | |
|
4910 | 0 | return 0; |
4911 | 0 | } |
4912 | | |
4913 | | int define_pirq_context(unsigned int pirq) |
4914 | 0 | { |
4915 | 0 | ocontext_t *newc, *c, *l, *head; |
4916 | |
|
4917 | 0 | if (policydbp->target_platform != SEPOL_TARGET_XEN) { |
4918 | 0 | yyerror("pirqcon not supported for target"); |
4919 | 0 | return -1; |
4920 | 0 | } |
4921 | | |
4922 | 0 | if (pass == 1) { |
4923 | 0 | parse_security_context(NULL); |
4924 | 0 | return 0; |
4925 | 0 | } |
4926 | | |
4927 | 0 | newc = malloc(sizeof(ocontext_t)); |
4928 | 0 | if (!newc) { |
4929 | 0 | yyerror("out of memory"); |
4930 | 0 | return -1; |
4931 | 0 | } |
4932 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
4933 | |
|
4934 | 0 | newc->u.pirq = pirq; |
4935 | |
|
4936 | 0 | if (parse_security_context(&newc->context[0])) { |
4937 | 0 | free(newc); |
4938 | 0 | return -1; |
4939 | 0 | } |
4940 | | |
4941 | 0 | head = policydbp->ocontexts[OCON_XEN_PIRQ]; |
4942 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
4943 | 0 | unsigned int pirq2; |
4944 | |
|
4945 | 0 | pirq2 = c->u.pirq; |
4946 | 0 | if (pirq == pirq2) { |
4947 | 0 | yyerror2("duplicate pirqcon entry for %d ", pirq); |
4948 | 0 | goto bad; |
4949 | 0 | } |
4950 | 0 | } |
4951 | | |
4952 | 0 | if (l) |
4953 | 0 | l->next = newc; |
4954 | 0 | else |
4955 | 0 | policydbp->ocontexts[OCON_XEN_PIRQ] = newc; |
4956 | |
|
4957 | 0 | return 0; |
4958 | | |
4959 | 0 | bad: |
4960 | 0 | free(newc); |
4961 | 0 | return -1; |
4962 | 0 | } |
4963 | | |
4964 | | int define_iomem_context(uint64_t low, uint64_t high) |
4965 | 722 | { |
4966 | 722 | ocontext_t *newc, *c, *l, *head; |
4967 | | |
4968 | 722 | if (policydbp->target_platform != SEPOL_TARGET_XEN) { |
4969 | 0 | yyerror("iomemcon not supported for target"); |
4970 | 0 | return -1; |
4971 | 0 | } |
4972 | | |
4973 | 722 | if (pass == 1) { |
4974 | 722 | parse_security_context(NULL); |
4975 | 722 | return 0; |
4976 | 722 | } |
4977 | | |
4978 | 0 | newc = malloc(sizeof(ocontext_t)); |
4979 | 0 | if (!newc) { |
4980 | 0 | yyerror("out of memory"); |
4981 | 0 | return -1; |
4982 | 0 | } |
4983 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
4984 | |
|
4985 | 0 | newc->u.iomem.low_iomem = low; |
4986 | 0 | newc->u.iomem.high_iomem = high; |
4987 | |
|
4988 | 0 | if (low > high) { |
4989 | 0 | yyerror2("low memory 0x%"PRIx64" exceeds high memory 0x%"PRIx64"", low, high); |
4990 | 0 | free(newc); |
4991 | 0 | return -1; |
4992 | 0 | } |
4993 | | |
4994 | 0 | if (parse_security_context(&newc->context[0])) { |
4995 | 0 | free(newc); |
4996 | 0 | return -1; |
4997 | 0 | } |
4998 | | |
4999 | 0 | head = policydbp->ocontexts[OCON_XEN_IOMEM]; |
5000 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5001 | 0 | uint64_t low2, high2; |
5002 | |
|
5003 | 0 | low2 = c->u.iomem.low_iomem; |
5004 | 0 | high2 = c->u.iomem.high_iomem; |
5005 | 0 | if (low <= high2 && low2 <= high) { |
5006 | 0 | yyerror2("iomemcon entry for 0x%"PRIx64"-0x%"PRIx64" overlaps with " |
5007 | 0 | "earlier entry 0x%"PRIx64"-0x%"PRIx64"", low, high, |
5008 | 0 | low2, high2); |
5009 | 0 | goto bad; |
5010 | 0 | } |
5011 | 0 | } |
5012 | | |
5013 | 0 | if (l) |
5014 | 0 | l->next = newc; |
5015 | 0 | else |
5016 | 0 | policydbp->ocontexts[OCON_XEN_IOMEM] = newc; |
5017 | |
|
5018 | 0 | return 0; |
5019 | | |
5020 | 0 | bad: |
5021 | 0 | free(newc); |
5022 | 0 | return -1; |
5023 | 0 | } |
5024 | | |
5025 | | int define_ioport_context(unsigned long low, unsigned long high) |
5026 | 32 | { |
5027 | 32 | ocontext_t *newc, *c, *l, *head; |
5028 | | |
5029 | 32 | if (policydbp->target_platform != SEPOL_TARGET_XEN) { |
5030 | 0 | yyerror("ioportcon not supported for target"); |
5031 | 0 | return -1; |
5032 | 0 | } |
5033 | | |
5034 | 32 | if (pass == 1) { |
5035 | 32 | parse_security_context(NULL); |
5036 | 32 | return 0; |
5037 | 32 | } |
5038 | | |
5039 | 0 | newc = malloc(sizeof(ocontext_t)); |
5040 | 0 | if (!newc) { |
5041 | 0 | yyerror("out of memory"); |
5042 | 0 | return -1; |
5043 | 0 | } |
5044 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
5045 | |
|
5046 | 0 | newc->u.ioport.low_ioport = low; |
5047 | 0 | newc->u.ioport.high_ioport = high; |
5048 | |
|
5049 | 0 | if (low > high) { |
5050 | 0 | yyerror2("low ioport 0x%lx exceeds high ioport 0x%lx", low, high); |
5051 | 0 | free(newc); |
5052 | 0 | return -1; |
5053 | 0 | } |
5054 | | |
5055 | 0 | if (parse_security_context(&newc->context[0])) { |
5056 | 0 | free(newc); |
5057 | 0 | return -1; |
5058 | 0 | } |
5059 | | |
5060 | 0 | head = policydbp->ocontexts[OCON_XEN_IOPORT]; |
5061 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5062 | 0 | uint32_t low2, high2; |
5063 | |
|
5064 | 0 | low2 = c->u.ioport.low_ioport; |
5065 | 0 | high2 = c->u.ioport.high_ioport; |
5066 | 0 | if (low <= high2 && low2 <= high) { |
5067 | 0 | yyerror2("ioportcon entry for 0x%lx-0x%lx overlaps with" |
5068 | 0 | "earlier entry 0x%x-0x%x", low, high, |
5069 | 0 | low2, high2); |
5070 | 0 | goto bad; |
5071 | 0 | } |
5072 | 0 | } |
5073 | | |
5074 | 0 | if (l) |
5075 | 0 | l->next = newc; |
5076 | 0 | else |
5077 | 0 | policydbp->ocontexts[OCON_XEN_IOPORT] = newc; |
5078 | |
|
5079 | 0 | return 0; |
5080 | | |
5081 | 0 | bad: |
5082 | 0 | free(newc); |
5083 | 0 | return -1; |
5084 | 0 | } |
5085 | | |
5086 | | int define_pcidevice_context(unsigned long device) |
5087 | 30 | { |
5088 | 30 | ocontext_t *newc, *c, *l, *head; |
5089 | | |
5090 | 30 | if (policydbp->target_platform != SEPOL_TARGET_XEN) { |
5091 | 0 | yyerror("pcidevicecon not supported for target"); |
5092 | 0 | return -1; |
5093 | 0 | } |
5094 | | |
5095 | 30 | if (pass == 1) { |
5096 | 30 | parse_security_context(NULL); |
5097 | 30 | return 0; |
5098 | 30 | } |
5099 | | |
5100 | 0 | newc = malloc(sizeof(ocontext_t)); |
5101 | 0 | if (!newc) { |
5102 | 0 | yyerror("out of memory"); |
5103 | 0 | return -1; |
5104 | 0 | } |
5105 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
5106 | |
|
5107 | 0 | newc->u.device = device; |
5108 | |
|
5109 | 0 | if (parse_security_context(&newc->context[0])) { |
5110 | 0 | free(newc); |
5111 | 0 | return -1; |
5112 | 0 | } |
5113 | | |
5114 | 0 | head = policydbp->ocontexts[OCON_XEN_PCIDEVICE]; |
5115 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5116 | 0 | unsigned int device2; |
5117 | |
|
5118 | 0 | device2 = c->u.device; |
5119 | 0 | if (device == device2) { |
5120 | 0 | yyerror2("duplicate pcidevicecon entry for 0x%lx", |
5121 | 0 | device); |
5122 | 0 | goto bad; |
5123 | 0 | } |
5124 | 0 | } |
5125 | | |
5126 | 0 | if (l) |
5127 | 0 | l->next = newc; |
5128 | 0 | else |
5129 | 0 | policydbp->ocontexts[OCON_XEN_PCIDEVICE] = newc; |
5130 | |
|
5131 | 0 | return 0; |
5132 | | |
5133 | 0 | bad: |
5134 | 0 | free(newc); |
5135 | 0 | return -1; |
5136 | 0 | } |
5137 | | |
5138 | | int define_devicetree_context(void) |
5139 | 48 | { |
5140 | 48 | ocontext_t *newc, *c, *l, *head; |
5141 | | |
5142 | 48 | if (policydbp->target_platform != SEPOL_TARGET_XEN) { |
5143 | 0 | yyerror("devicetreecon not supported for target"); |
5144 | 0 | return -1; |
5145 | 0 | } |
5146 | | |
5147 | 48 | if (pass == 1) { |
5148 | 48 | free(queue_remove(id_queue)); |
5149 | 48 | parse_security_context(NULL); |
5150 | 48 | return 0; |
5151 | 48 | } |
5152 | | |
5153 | 0 | newc = malloc(sizeof(ocontext_t)); |
5154 | 0 | if (!newc) { |
5155 | 0 | yyerror("out of memory"); |
5156 | 0 | return -1; |
5157 | 0 | } |
5158 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
5159 | |
|
5160 | 0 | newc->u.name = (char *)queue_remove(id_queue); |
5161 | 0 | if (!newc->u.name) { |
5162 | 0 | free(newc); |
5163 | 0 | return -1; |
5164 | 0 | } |
5165 | | |
5166 | 0 | if (parse_security_context(&newc->context[0])) { |
5167 | 0 | free(newc->u.name); |
5168 | 0 | free(newc); |
5169 | 0 | return -1; |
5170 | 0 | } |
5171 | | |
5172 | 0 | head = policydbp->ocontexts[OCON_XEN_DEVICETREE]; |
5173 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5174 | 0 | if (strcmp(newc->u.name, c->u.name) == 0) { |
5175 | 0 | yyerror2("duplicate devicetree entry for '%s'", newc->u.name); |
5176 | 0 | goto bad; |
5177 | 0 | } |
5178 | 0 | } |
5179 | | |
5180 | 0 | if (l) |
5181 | 0 | l->next = newc; |
5182 | 0 | else |
5183 | 0 | policydbp->ocontexts[OCON_XEN_DEVICETREE] = newc; |
5184 | |
|
5185 | 0 | return 0; |
5186 | | |
5187 | 0 | bad: |
5188 | 0 | free(newc->u.name); |
5189 | 0 | free(newc); |
5190 | 0 | return -1; |
5191 | 0 | } |
5192 | | |
5193 | | int define_port_context(unsigned int low, unsigned int high) |
5194 | 122 | { |
5195 | 122 | ocontext_t *newc, *c, *l, *head; |
5196 | 122 | unsigned int protocol; |
5197 | 122 | char *id; |
5198 | | |
5199 | 122 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5200 | 0 | yyerror("portcon not supported for target"); |
5201 | 0 | return -1; |
5202 | 0 | } |
5203 | | |
5204 | 122 | if (pass == 1) { |
5205 | 107 | id = (char *)queue_remove(id_queue); |
5206 | 107 | free(id); |
5207 | 107 | parse_security_context(NULL); |
5208 | 107 | return 0; |
5209 | 107 | } |
5210 | | |
5211 | 15 | newc = malloc(sizeof(ocontext_t)); |
5212 | 15 | if (!newc) { |
5213 | 0 | yyerror("out of memory"); |
5214 | 0 | return -1; |
5215 | 0 | } |
5216 | 15 | memset(newc, 0, sizeof(ocontext_t)); |
5217 | | |
5218 | 15 | id = (char *)queue_remove(id_queue); |
5219 | 15 | if (!id) { |
5220 | 0 | free(newc); |
5221 | 0 | return -1; |
5222 | 0 | } |
5223 | 15 | if ((strcmp(id, "tcp") == 0) || (strcmp(id, "TCP") == 0)) { |
5224 | 0 | protocol = IPPROTO_TCP; |
5225 | 15 | } else if ((strcmp(id, "udp") == 0) || (strcmp(id, "UDP") == 0)) { |
5226 | 0 | protocol = IPPROTO_UDP; |
5227 | 15 | } else if ((strcmp(id, "dccp") == 0) || (strcmp(id, "DCCP") == 0)) { |
5228 | 0 | protocol = IPPROTO_DCCP; |
5229 | 15 | } else if ((strcmp(id, "sctp") == 0) || (strcmp(id, "SCTP") == 0)) { |
5230 | 0 | protocol = IPPROTO_SCTP; |
5231 | 15 | } else { |
5232 | 15 | yyerror2("unrecognized protocol %s", id); |
5233 | 15 | goto bad; |
5234 | 15 | } |
5235 | | |
5236 | 0 | newc->u.port.protocol = protocol; |
5237 | 0 | newc->u.port.low_port = low; |
5238 | 0 | newc->u.port.high_port = high; |
5239 | |
|
5240 | 0 | if (low > high) { |
5241 | 0 | yyerror2("low port %d exceeds high port %d", low, high); |
5242 | 0 | goto bad; |
5243 | 0 | } |
5244 | | |
5245 | 0 | if (parse_security_context(&newc->context[0])) { |
5246 | 0 | goto bad; |
5247 | 0 | } |
5248 | | |
5249 | | /* Preserve the matching order specified in the configuration. */ |
5250 | 0 | head = policydbp->ocontexts[OCON_PORT]; |
5251 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5252 | 0 | unsigned int prot2, low2, high2; |
5253 | |
|
5254 | 0 | prot2 = c->u.port.protocol; |
5255 | 0 | low2 = c->u.port.low_port; |
5256 | 0 | high2 = c->u.port.high_port; |
5257 | 0 | if (protocol != prot2) |
5258 | 0 | continue; |
5259 | 0 | if (low == low2 && high == high2) { |
5260 | 0 | yyerror2("duplicate portcon entry for %s %d-%d ", id, |
5261 | 0 | low, high); |
5262 | 0 | goto bad; |
5263 | 0 | } |
5264 | 0 | if (low2 <= low && high2 >= high) { |
5265 | 0 | yyerror2("portcon entry for %s %d-%d hidden by earlier " |
5266 | 0 | "entry for %d-%d", id, low, high, low2, high2); |
5267 | 0 | goto bad; |
5268 | 0 | } |
5269 | 0 | } |
5270 | | |
5271 | 0 | if (l) |
5272 | 0 | l->next = newc; |
5273 | 0 | else |
5274 | 0 | policydbp->ocontexts[OCON_PORT] = newc; |
5275 | |
|
5276 | 0 | free(id); |
5277 | 0 | return 0; |
5278 | | |
5279 | 15 | bad: |
5280 | 15 | free(id); |
5281 | 15 | free(newc); |
5282 | 15 | return -1; |
5283 | 0 | } |
5284 | | |
5285 | | int define_ibpkey_context(unsigned int low, unsigned int high) |
5286 | 333 | { |
5287 | 333 | ocontext_t *newc, *c, *l, *head; |
5288 | 333 | struct in6_addr subnet_prefix; |
5289 | 333 | char *id; |
5290 | 333 | int rc = 0; |
5291 | | |
5292 | 333 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5293 | 0 | yyerror("ibpkeycon not supported for target"); |
5294 | 0 | return -1; |
5295 | 0 | } |
5296 | | |
5297 | 333 | if (pass == 1) { |
5298 | 325 | id = (char *)queue_remove(id_queue); |
5299 | 325 | free(id); |
5300 | 325 | parse_security_context(NULL); |
5301 | 325 | return 0; |
5302 | 325 | } |
5303 | | |
5304 | 8 | newc = malloc(sizeof(*newc)); |
5305 | 8 | if (!newc) { |
5306 | 0 | yyerror("out of memory"); |
5307 | 0 | return -1; |
5308 | 0 | } |
5309 | 8 | memset(newc, 0, sizeof(*newc)); |
5310 | | |
5311 | 8 | id = queue_remove(id_queue); |
5312 | 8 | if (!id) { |
5313 | 0 | yyerror("failed to read the subnet prefix"); |
5314 | 0 | rc = -1; |
5315 | 0 | goto out; |
5316 | 0 | } |
5317 | | |
5318 | 8 | rc = inet_pton(AF_INET6, id, &subnet_prefix); |
5319 | 8 | free(id); |
5320 | 8 | if (rc < 1) { |
5321 | 1 | yyerror("failed to parse the subnet prefix"); |
5322 | 1 | if (rc == 0) |
5323 | 1 | rc = -1; |
5324 | 1 | goto out; |
5325 | 1 | } |
5326 | | |
5327 | 7 | if (subnet_prefix.s6_addr32[2] || subnet_prefix.s6_addr32[3]) { |
5328 | 2 | yyerror("subnet prefix should be 0's in the low order 64 bits."); |
5329 | 2 | rc = -1; |
5330 | 2 | goto out; |
5331 | 2 | } |
5332 | | |
5333 | 5 | if (low > 0xffff || high > 0xffff) { |
5334 | 0 | yyerror("pkey value too large, pkeys are 16 bits."); |
5335 | 0 | rc = -1; |
5336 | 0 | goto out; |
5337 | 0 | } |
5338 | | |
5339 | 5 | memcpy(&newc->u.ibpkey.subnet_prefix, &subnet_prefix.s6_addr[0], |
5340 | 5 | sizeof(newc->u.ibpkey.subnet_prefix)); |
5341 | | |
5342 | 5 | newc->u.ibpkey.low_pkey = low; |
5343 | 5 | newc->u.ibpkey.high_pkey = high; |
5344 | | |
5345 | 5 | if (low > high) { |
5346 | 1 | yyerror2("low pkey %d exceeds high pkey %d", low, high); |
5347 | 1 | rc = -1; |
5348 | 1 | goto out; |
5349 | 1 | } |
5350 | | |
5351 | 4 | rc = parse_security_context(&newc->context[0]); |
5352 | 4 | if (rc) |
5353 | 4 | goto out; |
5354 | | |
5355 | | /* Preserve the matching order specified in the configuration. */ |
5356 | 0 | head = policydbp->ocontexts[OCON_IBPKEY]; |
5357 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5358 | 0 | unsigned int low2, high2; |
5359 | |
|
5360 | 0 | low2 = c->u.ibpkey.low_pkey; |
5361 | 0 | high2 = c->u.ibpkey.high_pkey; |
5362 | |
|
5363 | 0 | if (low == low2 && high == high2 && |
5364 | 0 | c->u.ibpkey.subnet_prefix == newc->u.ibpkey.subnet_prefix) { |
5365 | 0 | yyerror2("duplicate ibpkeycon entry for %d-%d ", |
5366 | 0 | low, high); |
5367 | 0 | rc = -1; |
5368 | 0 | goto out; |
5369 | 0 | } |
5370 | 0 | if (low2 <= low && high2 >= high && |
5371 | 0 | c->u.ibpkey.subnet_prefix == newc->u.ibpkey.subnet_prefix) { |
5372 | 0 | yyerror2("ibpkeycon entry for %d-%d hidden by earlier entry for %d-%d", |
5373 | 0 | low, high, low2, high2); |
5374 | 0 | rc = -1; |
5375 | 0 | goto out; |
5376 | 0 | } |
5377 | 0 | } |
5378 | | |
5379 | 0 | if (l) |
5380 | 0 | l->next = newc; |
5381 | 0 | else |
5382 | 0 | policydbp->ocontexts[OCON_IBPKEY] = newc; |
5383 | |
|
5384 | 0 | return 0; |
5385 | | |
5386 | 8 | out: |
5387 | 8 | free(newc); |
5388 | 8 | return rc; |
5389 | 0 | } |
5390 | | |
5391 | | int define_ibendport_context(unsigned int port) |
5392 | 35 | { |
5393 | 35 | ocontext_t *newc, *c, *l, *head; |
5394 | 35 | char *id; |
5395 | 35 | int rc = 0; |
5396 | | |
5397 | 35 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5398 | 0 | yyerror("ibendportcon not supported for target"); |
5399 | 0 | return -1; |
5400 | 0 | } |
5401 | | |
5402 | 35 | if (pass == 1) { |
5403 | 35 | id = (char *)queue_remove(id_queue); |
5404 | 35 | free(id); |
5405 | 35 | parse_security_context(NULL); |
5406 | 35 | return 0; |
5407 | 35 | } |
5408 | | |
5409 | 0 | if (port > 0xff || port == 0) { |
5410 | 0 | yyerror2("Invalid ibendport port number %d, should be 0 < port < 256", port); |
5411 | 0 | return -1; |
5412 | 0 | } |
5413 | | |
5414 | 0 | newc = malloc(sizeof(*newc)); |
5415 | 0 | if (!newc) { |
5416 | 0 | yyerror("out of memory"); |
5417 | 0 | return -1; |
5418 | 0 | } |
5419 | 0 | memset(newc, 0, sizeof(*newc)); |
5420 | |
|
5421 | 0 | newc->u.ibendport.dev_name = queue_remove(id_queue); |
5422 | 0 | if (!newc->u.ibendport.dev_name) { |
5423 | 0 | yyerror("failed to read infiniband device name."); |
5424 | 0 | rc = -1; |
5425 | 0 | goto out; |
5426 | 0 | } |
5427 | | |
5428 | 0 | if (strlen(newc->u.ibendport.dev_name) > IB_DEVICE_NAME_MAX - 1) { |
5429 | 0 | yyerror2("infiniband device name %s exceeds max length of 63.", newc->u.ibendport.dev_name); |
5430 | 0 | rc = -1; |
5431 | 0 | goto out; |
5432 | 0 | } |
5433 | | |
5434 | 0 | newc->u.ibendport.port = port; |
5435 | |
|
5436 | 0 | if (parse_security_context(&newc->context[0])) { |
5437 | 0 | free(newc); |
5438 | 0 | return -1; |
5439 | 0 | } |
5440 | | |
5441 | | /* Preserve the matching order specified in the configuration. */ |
5442 | 0 | head = policydbp->ocontexts[OCON_IBENDPORT]; |
5443 | 0 | for (l = NULL, c = head; c; l = c, c = c->next) { |
5444 | 0 | unsigned int port2; |
5445 | |
|
5446 | 0 | port2 = c->u.ibendport.port; |
5447 | |
|
5448 | 0 | if (port == port2 && |
5449 | 0 | !strcmp(c->u.ibendport.dev_name, |
5450 | 0 | newc->u.ibendport.dev_name)) { |
5451 | 0 | yyerror2("duplicate ibendportcon entry for %s port %u", |
5452 | 0 | newc->u.ibendport.dev_name, port); |
5453 | 0 | rc = -1; |
5454 | 0 | goto out; |
5455 | 0 | } |
5456 | 0 | } |
5457 | | |
5458 | 0 | if (l) |
5459 | 0 | l->next = newc; |
5460 | 0 | else |
5461 | 0 | policydbp->ocontexts[OCON_IBENDPORT] = newc; |
5462 | |
|
5463 | 0 | return 0; |
5464 | | |
5465 | 0 | out: |
5466 | 0 | free(newc->u.ibendport.dev_name); |
5467 | 0 | free(newc); |
5468 | 0 | return rc; |
5469 | 0 | } |
5470 | | |
5471 | | int define_netif_context(void) |
5472 | 219 | { |
5473 | 219 | ocontext_t *newc, *c, *head; |
5474 | | |
5475 | 219 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5476 | 0 | yyerror("netifcon not supported for target"); |
5477 | 0 | return -1; |
5478 | 0 | } |
5479 | | |
5480 | 219 | if (pass == 1) { |
5481 | 219 | free(queue_remove(id_queue)); |
5482 | 219 | parse_security_context(NULL); |
5483 | 219 | parse_security_context(NULL); |
5484 | 219 | return 0; |
5485 | 219 | } |
5486 | | |
5487 | 0 | newc = (ocontext_t *) malloc(sizeof(ocontext_t)); |
5488 | 0 | if (!newc) { |
5489 | 0 | yyerror("out of memory"); |
5490 | 0 | return -1; |
5491 | 0 | } |
5492 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
5493 | |
|
5494 | 0 | newc->u.name = (char *)queue_remove(id_queue); |
5495 | 0 | if (!newc->u.name) { |
5496 | 0 | free(newc); |
5497 | 0 | return -1; |
5498 | 0 | } |
5499 | 0 | if (parse_security_context(&newc->context[0])) { |
5500 | 0 | free(newc->u.name); |
5501 | 0 | free(newc); |
5502 | 0 | return -1; |
5503 | 0 | } |
5504 | 0 | if (parse_security_context(&newc->context[1])) { |
5505 | 0 | context_destroy(&newc->context[0]); |
5506 | 0 | free(newc->u.name); |
5507 | 0 | free(newc); |
5508 | 0 | return -1; |
5509 | 0 | } |
5510 | 0 | head = policydbp->ocontexts[OCON_NETIF]; |
5511 | |
|
5512 | 0 | for (c = head; c; c = c->next) { |
5513 | 0 | if (!strcmp(newc->u.name, c->u.name)) { |
5514 | 0 | yyerror2("duplicate entry for network interface %s", |
5515 | 0 | newc->u.name); |
5516 | 0 | context_destroy(&newc->context[0]); |
5517 | 0 | context_destroy(&newc->context[1]); |
5518 | 0 | free(newc->u.name); |
5519 | 0 | free(newc); |
5520 | 0 | return -1; |
5521 | 0 | } |
5522 | 0 | } |
5523 | | |
5524 | 0 | newc->next = head; |
5525 | 0 | policydbp->ocontexts[OCON_NETIF] = newc; |
5526 | 0 | return 0; |
5527 | 0 | } |
5528 | | |
5529 | | static int insert_ipv4_node(ocontext_t *newc) |
5530 | 223 | { |
5531 | 223 | ocontext_t *c, *l; |
5532 | 223 | char addr[INET_ADDRSTRLEN]; |
5533 | 223 | char mask[INET_ADDRSTRLEN]; |
5534 | | |
5535 | | /* Create order of most specific to least retaining |
5536 | | the order specified in the configuration. */ |
5537 | 500 | for (l = NULL, c = policydbp->ocontexts[OCON_NODE]; c; l = c, c = c->next) { |
5538 | 346 | if (newc->u.node.mask == c->u.node.mask && |
5539 | 179 | newc->u.node.addr == c->u.node.addr) { |
5540 | 5 | yyerror2("duplicate entry for network node %s %s", |
5541 | 5 | inet_ntop(AF_INET, &newc->u.node.addr, addr, INET_ADDRSTRLEN) ?: "<invalid>", |
5542 | 5 | inet_ntop(AF_INET, &newc->u.node.mask, mask, INET_ADDRSTRLEN) ?: "<invalid>"); |
5543 | 5 | context_destroy(&newc->context[0]); |
5544 | 5 | free(newc); |
5545 | 5 | return -1; |
5546 | 5 | } |
5547 | | |
5548 | 341 | if (newc->u.node.mask > c->u.node.mask) |
5549 | 64 | break; |
5550 | 341 | } |
5551 | | |
5552 | 218 | newc->next = c; |
5553 | | |
5554 | 218 | if (l) |
5555 | 113 | l->next = newc; |
5556 | 105 | else |
5557 | 105 | policydbp->ocontexts[OCON_NODE] = newc; |
5558 | | |
5559 | 218 | return 0; |
5560 | 223 | } |
5561 | | |
5562 | | int define_ipv4_node_context(void) |
5563 | 547 | { |
5564 | 547 | char *id; |
5565 | 547 | int rc = 0; |
5566 | 547 | struct in_addr addr, mask; |
5567 | 547 | ocontext_t *newc; |
5568 | | |
5569 | 547 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5570 | 0 | yyerror("nodecon not supported for target"); |
5571 | 0 | return -1; |
5572 | 0 | } |
5573 | | |
5574 | 547 | if (pass == 1) { |
5575 | 322 | free(queue_remove(id_queue)); |
5576 | 322 | free(queue_remove(id_queue)); |
5577 | 322 | parse_security_context(NULL); |
5578 | 322 | return 0; |
5579 | 322 | } |
5580 | | |
5581 | 225 | id = queue_remove(id_queue); |
5582 | 225 | if (!id) { |
5583 | 0 | yyerror("failed to read ipv4 address"); |
5584 | 0 | return -1; |
5585 | 0 | } |
5586 | | |
5587 | 225 | rc = inet_pton(AF_INET, id, &addr); |
5588 | 225 | if (rc < 1) { |
5589 | 1 | yyerror2("failed to parse ipv4 address %s", id); |
5590 | 1 | free(id); |
5591 | 1 | return -1; |
5592 | 1 | } |
5593 | 224 | free(id); |
5594 | | |
5595 | 224 | id = queue_remove(id_queue); |
5596 | 224 | if (!id) { |
5597 | 0 | yyerror("failed to read ipv4 address"); |
5598 | 0 | return -1; |
5599 | 0 | } |
5600 | | |
5601 | 224 | rc = inet_pton(AF_INET, id, &mask); |
5602 | 224 | if (rc < 1) { |
5603 | 0 | yyerror2("failed to parse ipv4 mask %s", id); |
5604 | 0 | free(id); |
5605 | 0 | return -1; |
5606 | 0 | } |
5607 | | |
5608 | 224 | free(id); |
5609 | | |
5610 | 224 | if (mask.s_addr != 0 && ((~be32toh(mask.s_addr) + 1) & ~be32toh(mask.s_addr)) != 0) { |
5611 | 224 | yywarn("ipv4 mask is not contiguous"); |
5612 | 224 | } |
5613 | | |
5614 | 224 | if ((~mask.s_addr & addr.s_addr) != 0) { |
5615 | 186 | yywarn("host bits in ipv4 address set"); |
5616 | 186 | } |
5617 | | |
5618 | 224 | newc = malloc(sizeof(ocontext_t)); |
5619 | 224 | if (!newc) { |
5620 | 0 | yyerror("out of memory"); |
5621 | 0 | return -1; |
5622 | 0 | } |
5623 | | |
5624 | 224 | memset(newc, 0, sizeof(ocontext_t)); |
5625 | 224 | newc->u.node.addr = addr.s_addr; |
5626 | 224 | newc->u.node.mask = mask.s_addr; |
5627 | | |
5628 | 224 | if (parse_security_context(&newc->context[0])) { |
5629 | 20 | free(newc); |
5630 | 20 | return -1; |
5631 | 20 | } |
5632 | | |
5633 | 204 | return insert_ipv4_node(newc); |
5634 | 224 | } |
5635 | | |
5636 | | int define_ipv4_cidr_node_context(void) |
5637 | 337 | { |
5638 | 337 | char *endptr, *id, *split; |
5639 | 337 | unsigned long mask_bits; |
5640 | 337 | uint32_t mask; |
5641 | 337 | struct in_addr addr; |
5642 | 337 | ocontext_t *newc; |
5643 | 337 | int rc; |
5644 | | |
5645 | 337 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5646 | 0 | yyerror("nodecon not supported for target"); |
5647 | 0 | return -1; |
5648 | 0 | } |
5649 | | |
5650 | 337 | if (pass == 1) { |
5651 | 306 | free(queue_remove(id_queue)); |
5652 | 306 | parse_security_context(NULL); |
5653 | 306 | return 0; |
5654 | 306 | } |
5655 | | |
5656 | 31 | id = queue_remove(id_queue); |
5657 | 31 | if (!id) { |
5658 | 0 | yyerror("failed to read IPv4 address"); |
5659 | 0 | return -1; |
5660 | 0 | } |
5661 | | |
5662 | 31 | split = strchr(id, '/'); |
5663 | 31 | if (!split) { |
5664 | 0 | yyerror2("invalid IPv4 CIDR notation: %s", id); |
5665 | 0 | free(id); |
5666 | 0 | return -1; |
5667 | 0 | } |
5668 | 31 | *split = '\0'; |
5669 | | |
5670 | 31 | rc = inet_pton(AF_INET, id, &addr); |
5671 | 31 | if (rc < 1) { |
5672 | 1 | yyerror2("failed to parse IPv4 address %s", id); |
5673 | 1 | free(id); |
5674 | 1 | return -1; |
5675 | 1 | } |
5676 | | |
5677 | 31 | errno = 0; |
5678 | 30 | mask_bits = strtoul(split + 1, &endptr, 10); |
5679 | 30 | if (errno || *endptr != '\0' || mask_bits > 32) { |
5680 | 0 | yyerror2("invalid mask in IPv4 CIDR notation: %s", split + 1); |
5681 | 0 | free(id); |
5682 | 0 | return -1; |
5683 | 0 | } |
5684 | | |
5685 | 30 | free(id); |
5686 | | |
5687 | 30 | if (mask_bits == 0) { |
5688 | 3 | yywarn("IPv4 CIDR mask of 0, matching all IPs"); |
5689 | 3 | mask = 0; |
5690 | 27 | } else { |
5691 | 27 | mask = ~((UINT32_C(1) << (32 - mask_bits)) - 1); |
5692 | 27 | mask = htobe32(mask); |
5693 | 27 | } |
5694 | | |
5695 | 30 | if ((~mask & addr.s_addr) != 0) |
5696 | 20 | yywarn("host bits in IPv4 address set"); |
5697 | | |
5698 | 30 | newc = calloc(1, sizeof(ocontext_t)); |
5699 | 30 | if (!newc) { |
5700 | 0 | yyerror("out of memory"); |
5701 | 0 | return -1; |
5702 | 0 | } |
5703 | | |
5704 | 30 | newc->u.node.addr = addr.s_addr & mask; |
5705 | 30 | newc->u.node.mask = mask; |
5706 | | |
5707 | 30 | if (parse_security_context(&newc->context[0])) { |
5708 | 11 | free(newc); |
5709 | 11 | return -1; |
5710 | 11 | } |
5711 | | |
5712 | 19 | return insert_ipv4_node(newc); |
5713 | 30 | } |
5714 | | |
5715 | | static int ipv6_is_mask_contiguous(const struct in6_addr *mask) |
5716 | 39 | { |
5717 | 39 | int filled = 1; |
5718 | 39 | unsigned i; |
5719 | | |
5720 | 557 | for (i = 0; i < 16; i++) { |
5721 | 526 | if ((((~mask->s6_addr[i] & 0xFF) + 1) & (~mask->s6_addr[i] & 0xFF)) != 0) { |
5722 | 8 | return 0; |
5723 | 8 | } |
5724 | 518 | if (!filled && mask->s6_addr[i] != 0) { |
5725 | 0 | return 0; |
5726 | 0 | } |
5727 | | |
5728 | 518 | if (filled && mask->s6_addr[i] != 0xFF) { |
5729 | 39 | filled = 0; |
5730 | 39 | } |
5731 | 518 | } |
5732 | | |
5733 | 31 | return 1; |
5734 | 39 | } |
5735 | | |
5736 | | static int ipv6_has_host_bits_set(const struct in6_addr *addr, const struct in6_addr *mask) |
5737 | 328 | { |
5738 | 328 | unsigned i; |
5739 | | |
5740 | 5.52k | for (i = 0; i < 16; i++) { |
5741 | 5.24k | if ((addr->s6_addr[i] & ~mask->s6_addr[i]) != 0) { |
5742 | 47 | return 1; |
5743 | 47 | } |
5744 | 5.24k | } |
5745 | | |
5746 | 281 | return 0; |
5747 | 328 | } |
5748 | | |
5749 | | static void ipv6_cidr_bits_to_mask(unsigned long cidr_bits, struct in6_addr *mask) |
5750 | 289 | { |
5751 | 289 | unsigned i; |
5752 | | |
5753 | 1.44k | for (i = 0; i < 4; i++) { |
5754 | 1.15k | if (cidr_bits == 0) { |
5755 | 527 | mask->s6_addr32[i] = 0; |
5756 | 629 | } else if (cidr_bits >= 32) { |
5757 | 359 | mask->s6_addr32[i] = ~UINT32_C(0); |
5758 | 359 | } else { |
5759 | 270 | mask->s6_addr32[i] = htobe32(~((UINT32_C(1) << (32 - cidr_bits)) - 1)); |
5760 | 270 | } |
5761 | | |
5762 | 1.15k | if (cidr_bits >= 32) |
5763 | 359 | cidr_bits -= 32; |
5764 | 797 | else |
5765 | 797 | cidr_bits = 0; |
5766 | 1.15k | } |
5767 | 289 | } |
5768 | | |
5769 | | static void ipv6_apply_mask(struct in6_addr *restrict addr, const struct in6_addr *restrict mask) |
5770 | 289 | { |
5771 | 289 | unsigned i; |
5772 | | |
5773 | 1.44k | for (i = 0; i < 4; i++) |
5774 | 1.15k | addr->s6_addr32[i] &= mask->s6_addr32[i]; |
5775 | 289 | } |
5776 | | |
5777 | | static int insert_ipv6_node(ocontext_t *newc) |
5778 | 308 | { |
5779 | 308 | ocontext_t *c, *l; |
5780 | 308 | char addr[INET6_ADDRSTRLEN]; |
5781 | 308 | char mask[INET6_ADDRSTRLEN]; |
5782 | | |
5783 | | /* Create order of most specific to least retaining |
5784 | | the order specified in the configuration. */ |
5785 | 864 | for (l = NULL, c = policydbp->ocontexts[OCON_NODE6]; c; l = c, c = c->next) { |
5786 | 698 | if (memcmp(&newc->u.node6.mask, &c->u.node6.mask, 16) == 0 && |
5787 | 31 | memcmp(&newc->u.node6.addr, &c->u.node6.addr, 16) == 0) { |
5788 | 6 | yyerror2("duplicate entry for network node %s %s", |
5789 | 6 | inet_ntop(AF_INET6, &newc->u.node6.addr, addr, INET6_ADDRSTRLEN) ?: "<invalid>", |
5790 | 6 | inet_ntop(AF_INET6, &newc->u.node6.mask, mask, INET6_ADDRSTRLEN) ?: "<invalid>"); |
5791 | 6 | context_destroy(&newc->context[0]); |
5792 | 6 | free(newc); |
5793 | 6 | return -1; |
5794 | 6 | } |
5795 | | |
5796 | 692 | if (memcmp(&newc->u.node6.mask, &c->u.node6.mask, 16) > 0) |
5797 | 136 | break; |
5798 | 692 | } |
5799 | | |
5800 | 302 | newc->next = c; |
5801 | | |
5802 | 302 | if (l) |
5803 | 180 | l->next = newc; |
5804 | 122 | else |
5805 | 122 | policydbp->ocontexts[OCON_NODE6] = newc; |
5806 | | |
5807 | 302 | return 0; |
5808 | 308 | } |
5809 | | |
5810 | | int define_ipv6_node_context(void) |
5811 | 107 | { |
5812 | 107 | char *id; |
5813 | 107 | int rc = 0; |
5814 | 107 | struct in6_addr addr, mask; |
5815 | 107 | ocontext_t *newc; |
5816 | | |
5817 | 107 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5818 | 0 | yyerror("nodecon not supported for target"); |
5819 | 0 | return -1; |
5820 | 0 | } |
5821 | | |
5822 | 107 | if (pass == 1) { |
5823 | 66 | free(queue_remove(id_queue)); |
5824 | 66 | free(queue_remove(id_queue)); |
5825 | 66 | parse_security_context(NULL); |
5826 | 66 | return 0; |
5827 | 66 | } |
5828 | | |
5829 | 41 | id = queue_remove(id_queue); |
5830 | 41 | if (!id) { |
5831 | 0 | yyerror("failed to read ipv6 address"); |
5832 | 0 | return -1; |
5833 | 0 | } |
5834 | | |
5835 | 41 | rc = inet_pton(AF_INET6, id, &addr); |
5836 | 41 | if (rc < 1) { |
5837 | 1 | yyerror2("failed to parse ipv6 address %s", id); |
5838 | 1 | free(id); |
5839 | 1 | return -1; |
5840 | 1 | } |
5841 | | |
5842 | 40 | free(id); |
5843 | | |
5844 | 40 | id = queue_remove(id_queue); |
5845 | 40 | if (!id) { |
5846 | 0 | yyerror("failed to read ipv6 address"); |
5847 | 0 | return -1; |
5848 | 0 | } |
5849 | | |
5850 | 40 | rc = inet_pton(AF_INET6, id, &mask); |
5851 | 40 | if (rc < 1) { |
5852 | 1 | yyerror2("failed to parse ipv6 mask %s", id); |
5853 | 1 | free(id); |
5854 | 1 | return -1; |
5855 | 1 | } |
5856 | | |
5857 | 39 | free(id); |
5858 | | |
5859 | 39 | if (!ipv6_is_mask_contiguous(&mask)) { |
5860 | 8 | yywarn("ipv6 mask is not contiguous"); |
5861 | 8 | } |
5862 | | |
5863 | 39 | if (ipv6_has_host_bits_set(&addr, &mask)) { |
5864 | 30 | yywarn("host bits in ipv6 address set"); |
5865 | 30 | } |
5866 | | |
5867 | 39 | newc = malloc(sizeof(ocontext_t)); |
5868 | 39 | if (!newc) { |
5869 | 0 | yyerror("out of memory"); |
5870 | 0 | return -1; |
5871 | 0 | } |
5872 | | |
5873 | 39 | memset(newc, 0, sizeof(ocontext_t)); |
5874 | 39 | memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16); |
5875 | 39 | memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16); |
5876 | | |
5877 | 39 | if (parse_security_context(&newc->context[0])) { |
5878 | 8 | free(newc); |
5879 | 8 | return -1; |
5880 | 8 | } |
5881 | | |
5882 | 31 | return insert_ipv6_node(newc); |
5883 | 39 | } |
5884 | | |
5885 | | int define_ipv6_cidr_node_context(void) |
5886 | 638 | { |
5887 | 638 | char *endptr, *id, *split; |
5888 | 638 | unsigned long mask_bits; |
5889 | 638 | int rc; |
5890 | 638 | struct in6_addr addr, mask; |
5891 | 638 | ocontext_t *newc; |
5892 | | |
5893 | 638 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5894 | 0 | yyerror("nodecon not supported for target"); |
5895 | 0 | return -1; |
5896 | 0 | } |
5897 | | |
5898 | 638 | if (pass == 1) { |
5899 | 348 | free(queue_remove(id_queue)); |
5900 | 348 | parse_security_context(NULL); |
5901 | 348 | return 0; |
5902 | 348 | } |
5903 | | |
5904 | 290 | id = queue_remove(id_queue); |
5905 | 290 | if (!id) { |
5906 | 0 | yyerror("failed to read IPv6 address"); |
5907 | 0 | return -1; |
5908 | 0 | } |
5909 | | |
5910 | 290 | split = strchr(id, '/'); |
5911 | 290 | if (!split) { |
5912 | 0 | yyerror2("invalid IPv6 CIDR notation: %s", id); |
5913 | 0 | free(id); |
5914 | 0 | return -1; |
5915 | 0 | } |
5916 | 290 | *split = '\0'; |
5917 | | |
5918 | 290 | rc = inet_pton(AF_INET6, id, &addr); |
5919 | 290 | if (rc < 1) { |
5920 | 1 | yyerror2("failed to parse IPv6 address %s", id); |
5921 | 1 | free(id); |
5922 | 1 | return -1; |
5923 | 1 | } |
5924 | | |
5925 | 290 | errno = 0; |
5926 | 289 | mask_bits = strtoul(split + 1, &endptr, 10); |
5927 | 289 | if (errno || *endptr != '\0' || mask_bits > 128) { |
5928 | 0 | yyerror2("invalid mask in IPv6 CIDR notation: %s", split + 1); |
5929 | 0 | free(id); |
5930 | 0 | return -1; |
5931 | 0 | } |
5932 | | |
5933 | 289 | if (mask_bits == 0) { |
5934 | 6 | yywarn("IPv6 CIDR mask of 0, matching all IPs"); |
5935 | 6 | } |
5936 | | |
5937 | 289 | ipv6_cidr_bits_to_mask(mask_bits, &mask); |
5938 | | |
5939 | 289 | if (ipv6_has_host_bits_set(&addr, &mask)) { |
5940 | 17 | yywarn("host bits in ipv6 address set"); |
5941 | 17 | } |
5942 | | |
5943 | 289 | free(id); |
5944 | | |
5945 | 289 | newc = calloc(1, sizeof(ocontext_t)); |
5946 | 289 | if (!newc) { |
5947 | 0 | yyerror("out of memory"); |
5948 | 0 | return -1; |
5949 | 0 | } |
5950 | | |
5951 | 289 | ipv6_apply_mask(&addr, &mask); |
5952 | 289 | memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16); |
5953 | 289 | memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16); |
5954 | | |
5955 | 289 | if (parse_security_context(&newc->context[0])) { |
5956 | 12 | free(newc); |
5957 | 12 | return -1; |
5958 | 12 | } |
5959 | | |
5960 | 277 | return insert_ipv6_node(newc); |
5961 | 289 | } |
5962 | | |
5963 | | int define_fs_use(int behavior) |
5964 | 31 | { |
5965 | 31 | ocontext_t *newc, *c, *head; |
5966 | | |
5967 | 31 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
5968 | 0 | yyerror("fsuse not supported for target"); |
5969 | 0 | return -1; |
5970 | 0 | } |
5971 | | |
5972 | 31 | if (pass == 1) { |
5973 | 31 | free(queue_remove(id_queue)); |
5974 | 31 | parse_security_context(NULL); |
5975 | 31 | return 0; |
5976 | 31 | } |
5977 | | |
5978 | 0 | newc = (ocontext_t *) malloc(sizeof(ocontext_t)); |
5979 | 0 | if (!newc) { |
5980 | 0 | yyerror("out of memory"); |
5981 | 0 | return -1; |
5982 | 0 | } |
5983 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
5984 | |
|
5985 | 0 | newc->u.name = (char *)queue_remove(id_queue); |
5986 | 0 | if (!newc->u.name) { |
5987 | 0 | free(newc); |
5988 | 0 | return -1; |
5989 | 0 | } |
5990 | 0 | newc->v.behavior = behavior; |
5991 | 0 | if (parse_security_context(&newc->context[0])) { |
5992 | 0 | free(newc->u.name); |
5993 | 0 | free(newc); |
5994 | 0 | return -1; |
5995 | 0 | } |
5996 | | |
5997 | 0 | head = policydbp->ocontexts[OCON_FSUSE]; |
5998 | |
|
5999 | 0 | for (c = head; c; c = c->next) { |
6000 | 0 | if (!strcmp(newc->u.name, c->u.name)) { |
6001 | 0 | yyerror2("duplicate fs_use entry for filesystem type %s", |
6002 | 0 | newc->u.name); |
6003 | 0 | context_destroy(&newc->context[0]); |
6004 | 0 | free(newc->u.name); |
6005 | 0 | free(newc); |
6006 | 0 | return -1; |
6007 | 0 | } |
6008 | 0 | } |
6009 | | |
6010 | 0 | newc->next = head; |
6011 | 0 | policydbp->ocontexts[OCON_FSUSE] = newc; |
6012 | 0 | return 0; |
6013 | 0 | } |
6014 | | |
6015 | | static int define_genfs_context_helper(char *fstype, int has_type) |
6016 | 500 | { |
6017 | 500 | struct genfs *genfs_p, *genfs, *newgenfs; |
6018 | 500 | ocontext_t *newc, *c, *head, *p; |
6019 | 500 | class_datum_t *cladatum; |
6020 | 500 | char *type = NULL; |
6021 | 500 | const char *sclass; |
6022 | 500 | size_t len, len2; |
6023 | 500 | int wildcard = ebitmap_get_bit(&policydbp->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD); |
6024 | | |
6025 | 500 | if (policydbp->target_platform != SEPOL_TARGET_SELINUX) { |
6026 | 0 | yyerror("genfs not supported for target"); |
6027 | 0 | return -1; |
6028 | 0 | } |
6029 | | |
6030 | 500 | if (pass == 1) { |
6031 | 500 | free(fstype); |
6032 | 500 | free(queue_remove(id_queue)); |
6033 | 500 | if (has_type) |
6034 | 64 | free(queue_remove(id_queue)); |
6035 | 500 | parse_security_context(NULL); |
6036 | 500 | return 0; |
6037 | 500 | } |
6038 | | |
6039 | 0 | for (genfs_p = NULL, genfs = policydbp->genfs; |
6040 | 0 | genfs; genfs_p = genfs, genfs = genfs->next) { |
6041 | 0 | if (strcmp(fstype, genfs->fstype) <= 0) |
6042 | 0 | break; |
6043 | 0 | } |
6044 | |
|
6045 | 0 | if (!genfs || strcmp(fstype, genfs->fstype)) { |
6046 | 0 | newgenfs = malloc(sizeof(struct genfs)); |
6047 | 0 | if (!newgenfs) { |
6048 | 0 | yyerror("out of memory"); |
6049 | 0 | return -1; |
6050 | 0 | } |
6051 | 0 | memset(newgenfs, 0, sizeof(struct genfs)); |
6052 | 0 | newgenfs->fstype = fstype; |
6053 | 0 | newgenfs->next = genfs; |
6054 | 0 | if (genfs_p) |
6055 | 0 | genfs_p->next = newgenfs; |
6056 | 0 | else |
6057 | 0 | policydbp->genfs = newgenfs; |
6058 | 0 | genfs = newgenfs; |
6059 | 0 | } else { |
6060 | 0 | free(fstype); |
6061 | 0 | fstype = NULL; |
6062 | 0 | } |
6063 | | |
6064 | 0 | newc = (ocontext_t *) malloc(sizeof(ocontext_t)); |
6065 | 0 | if (!newc) { |
6066 | 0 | yyerror("out of memory"); |
6067 | 0 | return -1; |
6068 | 0 | } |
6069 | 0 | memset(newc, 0, sizeof(ocontext_t)); |
6070 | |
|
6071 | 0 | newc->u.name = (char *)queue_remove(id_queue); |
6072 | 0 | if (!newc->u.name) |
6073 | 0 | goto fail; |
6074 | | |
6075 | 0 | if (wildcard) { |
6076 | 0 | size_t name_len = strlen(newc->u.name); |
6077 | 0 | newc->u.name = realloc(newc->u.name, name_len + 2); |
6078 | 0 | if (newc->u.name == NULL) { |
6079 | 0 | yyerror("out of memory"); |
6080 | 0 | return -1; |
6081 | 0 | } |
6082 | | |
6083 | 0 | newc->u.name[name_len] = '*'; |
6084 | 0 | newc->u.name[name_len + 1] = '\0'; |
6085 | 0 | } |
6086 | | |
6087 | 0 | if (has_type) { |
6088 | 0 | type = (char *)queue_remove(id_queue); |
6089 | 0 | if (!type) |
6090 | 0 | goto fail; |
6091 | 0 | if (type[1] != 0) { |
6092 | 0 | yyerror2("invalid type %s", type); |
6093 | 0 | goto fail; |
6094 | 0 | } |
6095 | 0 | switch (type[0]) { |
6096 | 0 | case 'b': |
6097 | 0 | sclass = "blk_file"; |
6098 | 0 | break; |
6099 | 0 | case 'c': |
6100 | 0 | sclass = "chr_file"; |
6101 | 0 | break; |
6102 | 0 | case 'd': |
6103 | 0 | sclass = "dir"; |
6104 | 0 | break; |
6105 | 0 | case 'p': |
6106 | 0 | sclass = "fifo_file"; |
6107 | 0 | break; |
6108 | 0 | case 'l': |
6109 | 0 | sclass = "lnk_file"; |
6110 | 0 | break; |
6111 | 0 | case 's': |
6112 | 0 | sclass = "sock_file"; |
6113 | 0 | break; |
6114 | 0 | case '-': |
6115 | 0 | sclass = "file"; |
6116 | 0 | break; |
6117 | 0 | default: |
6118 | 0 | yyerror2("invalid type %s", type); |
6119 | 0 | goto fail; |
6120 | 0 | } |
6121 | | |
6122 | 0 | cladatum = hashtab_search(policydbp->p_classes.table, |
6123 | 0 | sclass); |
6124 | 0 | if (!cladatum) { |
6125 | 0 | yyerror2("could not find class %s for " |
6126 | 0 | "genfscon statement", sclass); |
6127 | 0 | goto fail; |
6128 | 0 | } |
6129 | 0 | newc->v.sclass = cladatum->s.value; |
6130 | 0 | } |
6131 | 0 | if (parse_security_context(&newc->context[0])) |
6132 | 0 | goto fail; |
6133 | | |
6134 | 0 | head = genfs->head; |
6135 | |
|
6136 | 0 | for (p = NULL, c = head; c; p = c, c = c->next) { |
6137 | 0 | if (!strcmp(newc->u.name, c->u.name) && |
6138 | 0 | (!newc->v.sclass || !c->v.sclass |
6139 | 0 | || newc->v.sclass == c->v.sclass)) { |
6140 | 0 | yyerror2("duplicate entry for genfs entry (%s, %s)", |
6141 | 0 | genfs->fstype, newc->u.name); |
6142 | 0 | goto fail; |
6143 | 0 | } |
6144 | 0 | len = strlen(newc->u.name); |
6145 | 0 | len2 = strlen(c->u.name); |
6146 | 0 | if (len > len2) |
6147 | 0 | break; |
6148 | 0 | } |
6149 | | |
6150 | 0 | newc->next = c; |
6151 | 0 | if (p) |
6152 | 0 | p->next = newc; |
6153 | 0 | else |
6154 | 0 | genfs->head = newc; |
6155 | 0 | free(type); |
6156 | 0 | return 0; |
6157 | 0 | fail: |
6158 | 0 | if (type) |
6159 | 0 | free(type); |
6160 | 0 | context_destroy(&newc->context[0]); |
6161 | 0 | if (fstype) |
6162 | 0 | free(fstype); |
6163 | 0 | if (newc->u.name) |
6164 | 0 | free(newc->u.name); |
6165 | 0 | free(newc); |
6166 | 0 | return -1; |
6167 | 0 | } |
6168 | | |
6169 | | int define_genfs_context(int has_type) |
6170 | 500 | { |
6171 | 500 | return define_genfs_context_helper(queue_remove(id_queue), has_type); |
6172 | 500 | } |
6173 | | |
6174 | | int define_range_trans(int class_specified) |
6175 | 867 | { |
6176 | 867 | char *id; |
6177 | 867 | level_datum_t *levdatum = 0; |
6178 | 867 | class_datum_t *cladatum; |
6179 | 867 | range_trans_rule_t *rule; |
6180 | 867 | int l, add = 1; |
6181 | | |
6182 | 867 | if (!mlspol) { |
6183 | 0 | yyerror("range_transition rule in non-MLS configuration"); |
6184 | 0 | return -1; |
6185 | 0 | } |
6186 | | |
6187 | 867 | if (pass == 1) { |
6188 | 2.06k | while ((id = queue_remove(id_queue))) |
6189 | 1.20k | free(id); |
6190 | 2.15k | while ((id = queue_remove(id_queue))) |
6191 | 1.28k | free(id); |
6192 | 862 | if (class_specified) |
6193 | 2.41k | while ((id = queue_remove(id_queue))) |
6194 | 1.80k | free(id); |
6195 | 862 | id = queue_remove(id_queue); |
6196 | 862 | free(id); |
6197 | 1.37k | for (l = 0; l < 2; l++) { |
6198 | 1.64k | while ((id = queue_remove(id_queue))) { |
6199 | 267 | free(id); |
6200 | 267 | } |
6201 | 1.37k | id = queue_remove(id_queue); |
6202 | 1.37k | if (!id) |
6203 | 862 | break; |
6204 | 516 | free(id); |
6205 | 516 | } |
6206 | 862 | return 0; |
6207 | 862 | } |
6208 | | |
6209 | 5 | rule = malloc(sizeof(struct range_trans_rule)); |
6210 | 5 | if (!rule) { |
6211 | 0 | yyerror("out of memory"); |
6212 | 0 | return -1; |
6213 | 0 | } |
6214 | 5 | range_trans_rule_init(rule); |
6215 | | |
6216 | 8 | while ((id = queue_remove(id_queue))) { |
6217 | 5 | if (set_types(&rule->stypes, id, &add, 0)) |
6218 | 2 | goto out; |
6219 | 5 | } |
6220 | 3 | add = 1; |
6221 | 322 | while ((id = queue_remove(id_queue))) { |
6222 | 321 | if (set_types(&rule->ttypes, id, &add, 0)) |
6223 | 2 | goto out; |
6224 | 321 | } |
6225 | | |
6226 | 1 | if (class_specified) { |
6227 | 0 | if (read_classes(&rule->tclasses)) |
6228 | 0 | goto out; |
6229 | 1 | } else { |
6230 | 1 | cladatum = hashtab_search(policydbp->p_classes.table, |
6231 | 1 | "process"); |
6232 | 1 | if (!cladatum) { |
6233 | 1 | yyerror2("could not find process class for " |
6234 | 1 | "legacy range_transition statement"); |
6235 | 1 | goto out; |
6236 | 1 | } |
6237 | | |
6238 | 0 | if (ebitmap_set_bit(&rule->tclasses, cladatum->s.value - 1, TRUE)) { |
6239 | 0 | yyerror("out of memory"); |
6240 | 0 | goto out; |
6241 | 0 | } |
6242 | 0 | } |
6243 | | |
6244 | 0 | id = (char *)queue_remove(id_queue); |
6245 | 0 | if (!id) { |
6246 | 0 | yyerror("no range in range_transition definition?"); |
6247 | 0 | goto out; |
6248 | 0 | } |
6249 | 0 | for (l = 0; l < 2; l++) { |
6250 | 0 | levdatum = hashtab_search(policydbp->p_levels.table, id); |
6251 | 0 | if (!levdatum) { |
6252 | 0 | yyerror2("unknown level %s used in range_transition " |
6253 | 0 | "definition", id); |
6254 | 0 | free(id); |
6255 | 0 | goto out; |
6256 | 0 | } |
6257 | 0 | free(id); |
6258 | |
|
6259 | 0 | rule->trange.level[l].sens = levdatum->level->sens; |
6260 | |
|
6261 | 0 | while ((id = queue_remove(id_queue))) { |
6262 | 0 | if (parse_semantic_categories(id, levdatum, |
6263 | 0 | &rule->trange.level[l].cat)) { |
6264 | 0 | free(id); |
6265 | 0 | goto out; |
6266 | 0 | } |
6267 | 0 | free(id); |
6268 | 0 | } |
6269 | | |
6270 | 0 | id = (char *)queue_remove(id_queue); |
6271 | 0 | if (!id) |
6272 | 0 | break; |
6273 | 0 | } |
6274 | 0 | if (l == 0) { |
6275 | 0 | if (mls_semantic_level_cpy(&rule->trange.level[1], |
6276 | 0 | &rule->trange.level[0])) { |
6277 | 0 | yyerror("out of memory"); |
6278 | 0 | goto out; |
6279 | 0 | } |
6280 | 0 | } |
6281 | | |
6282 | 0 | append_range_trans(rule); |
6283 | 0 | return 0; |
6284 | | |
6285 | 5 | out: |
6286 | 5 | range_trans_rule_destroy(rule); |
6287 | 5 | free(rule); |
6288 | 5 | return -1; |
6289 | 0 | } |
6290 | | |
6291 | | /* FLASK */ |