/src/selinux/libsepol/src/hierarchy.c
Line | Count | Source |
1 | | /* Authors: Joshua Brindle <jbrindle@tresys.com> |
2 | | * Jason Tang <jtang@tresys.com> |
3 | | * |
4 | | * Updates: KaiGai Kohei <kaigai@ak.jp.nec.com> |
5 | | * adds checks based on newer boundary facility. |
6 | | * |
7 | | * A set of utility functions that aid policy decision when dealing |
8 | | * with hierarchal namespaces. |
9 | | * |
10 | | * Copyright (C) 2005 Tresys Technology, LLC |
11 | | * |
12 | | * Copyright (c) 2008 NEC Corporation |
13 | | * |
14 | | * This library is free software; you can redistribute it and/or |
15 | | * modify it under the terms of the GNU Lesser General Public |
16 | | * License as published by the Free Software Foundation; either |
17 | | * version 2.1 of the License, or (at your option) any later version. |
18 | | * |
19 | | * This library is distributed in the hope that it will be useful, |
20 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 | | * Lesser General Public License for more details. |
23 | | * |
24 | | * You should have received a copy of the GNU Lesser General Public |
25 | | * License along with this library; if not, write to the Free Software |
26 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
27 | | */ |
28 | | |
29 | | #include <string.h> |
30 | | #include <stdlib.h> |
31 | | #include <assert.h> |
32 | | #include <sepol/policydb/policydb.h> |
33 | | #include <sepol/policydb/conditional.h> |
34 | | #include <sepol/policydb/hierarchy.h> |
35 | | #include <sepol/policydb/expand.h> |
36 | | #include <sepol/policydb/util.h> |
37 | | |
38 | | #include "debug.h" |
39 | | |
40 | 46 | #define BOUNDS_AVTAB_SIZE 1024 |
41 | | |
42 | | static int bounds_insert_helper(sepol_handle_t *handle, avtab_t *avtab, |
43 | | avtab_key_t *avtab_key, avtab_datum_t *datum) |
44 | 33 | { |
45 | 33 | int rc = avtab_insert(avtab, avtab_key, datum); |
46 | 33 | if (rc) { |
47 | 0 | if (rc == SEPOL_ENOMEM) |
48 | 0 | ERR(handle, "Insufficient memory"); |
49 | 0 | else |
50 | 0 | ERR(handle, "Unexpected error (%d)", rc); |
51 | 0 | } |
52 | 33 | return rc; |
53 | 33 | } |
54 | | |
55 | | |
56 | | static int bounds_insert_rule(sepol_handle_t *handle, avtab_t *avtab, |
57 | | avtab_t *global, avtab_t *other, |
58 | | avtab_key_t *avtab_key, avtab_datum_t *datum) |
59 | 351 | { |
60 | 351 | int rc = 0; |
61 | 351 | avtab_datum_t *dup = avtab_search(avtab, avtab_key); |
62 | | |
63 | 351 | if (!dup) { |
64 | 33 | rc = bounds_insert_helper(handle, avtab, avtab_key, datum); |
65 | 33 | if (rc) goto exit; |
66 | 318 | } else { |
67 | 318 | dup->data |= datum->data; |
68 | 318 | } |
69 | | |
70 | 351 | if (other) { |
71 | | /* Search the other conditional avtab for the key and |
72 | | * add any common permissions to the global avtab |
73 | | */ |
74 | 163 | uint32_t data = 0; |
75 | 163 | dup = avtab_search(other, avtab_key); |
76 | 163 | if (dup) { |
77 | 163 | data = dup->data & datum->data; |
78 | 163 | if (data) { |
79 | 0 | dup = avtab_search(global, avtab_key); |
80 | 0 | if (!dup) { |
81 | 0 | avtab_datum_t d; |
82 | 0 | d.data = data; |
83 | 0 | rc = bounds_insert_helper(handle, global, |
84 | 0 | avtab_key, &d); |
85 | 0 | if (rc) goto exit; |
86 | 0 | } else { |
87 | 0 | dup->data |= data; |
88 | 0 | } |
89 | 0 | } |
90 | 163 | } |
91 | 163 | } |
92 | | |
93 | 351 | exit: |
94 | 351 | return rc; |
95 | 351 | } |
96 | | |
97 | | static int bounds_expand_rule(sepol_handle_t *handle, policydb_t *p, |
98 | | avtab_t *avtab, avtab_t *global, avtab_t *other, |
99 | | uint32_t parent, uint32_t src, uint32_t tgt, |
100 | | uint32_t class, uint32_t data) |
101 | 351 | { |
102 | 351 | int rc = 0; |
103 | 351 | avtab_key_t avtab_key; |
104 | 351 | avtab_datum_t datum; |
105 | 351 | ebitmap_node_t *tnode; |
106 | 351 | unsigned int i; |
107 | | |
108 | 351 | avtab_key.specified = AVTAB_ALLOWED; |
109 | 351 | avtab_key.target_class = class; |
110 | 351 | datum.data = data; |
111 | | |
112 | 351 | if (ebitmap_get_bit(&p->attr_type_map[src - 1], parent - 1)) { |
113 | 351 | avtab_key.source_type = parent; |
114 | 22.4k | ebitmap_for_each_positive_bit(&p->attr_type_map[tgt - 1], tnode, i) { |
115 | 351 | avtab_key.target_type = i + 1; |
116 | 351 | rc = bounds_insert_rule(handle, avtab, global, other, |
117 | 351 | &avtab_key, &datum); |
118 | 351 | if (rc) goto exit; |
119 | 351 | } |
120 | 351 | } |
121 | | |
122 | 351 | exit: |
123 | 351 | return rc; |
124 | 351 | } |
125 | | |
126 | | static int bounds_expand_cond_rules(sepol_handle_t *handle, policydb_t *p, |
127 | | cond_av_list_t *cur, avtab_t *avtab, |
128 | | avtab_t *global, avtab_t *other, |
129 | | uint32_t parent) |
130 | 21 | { |
131 | 21 | int rc = 0; |
132 | | |
133 | 360 | for (; cur; cur = cur->next) { |
134 | 339 | avtab_ptr_t n = cur->node; |
135 | 339 | rc = bounds_expand_rule(handle, p, avtab, global, other, parent, |
136 | 339 | n->key.source_type, n->key.target_type, |
137 | 339 | n->key.target_class, n->datum.data); |
138 | 339 | if (rc) goto exit; |
139 | 339 | } |
140 | | |
141 | 21 | exit: |
142 | 21 | return rc; |
143 | 21 | } |
144 | | |
145 | | struct bounds_expand_args { |
146 | | sepol_handle_t *handle; |
147 | | policydb_t *p; |
148 | | avtab_t *avtab; |
149 | | uint32_t parent; |
150 | | }; |
151 | | |
152 | | static int bounds_expand_rule_callback(avtab_key_t *k, avtab_datum_t *d, |
153 | | void *args) |
154 | 43 | { |
155 | 43 | struct bounds_expand_args *a = (struct bounds_expand_args *)args; |
156 | | |
157 | 43 | if (!(k->specified & AVTAB_ALLOWED)) |
158 | 31 | return 0; |
159 | | |
160 | 12 | return bounds_expand_rule(a->handle, a->p, a->avtab, NULL, NULL, |
161 | 12 | a->parent, k->source_type, k->target_type, |
162 | 12 | k->target_class, d->data); |
163 | 43 | } |
164 | | |
165 | | struct bounds_cond_info { |
166 | | avtab_t true_avtab; |
167 | | avtab_t false_avtab; |
168 | | cond_list_t *cond_list; |
169 | | struct bounds_cond_info *next; |
170 | | }; |
171 | | |
172 | | static void bounds_destroy_cond_info(struct bounds_cond_info *cur) |
173 | 25 | { |
174 | 25 | struct bounds_cond_info *next; |
175 | | |
176 | 36 | for (; cur; cur = next) { |
177 | 11 | next = cur->next; |
178 | 11 | avtab_destroy(&cur->true_avtab); |
179 | 11 | avtab_destroy(&cur->false_avtab); |
180 | 11 | cur->next = NULL; |
181 | 11 | free(cur); |
182 | 11 | } |
183 | 25 | } |
184 | | |
185 | | static int bounds_expand_parent_rules(sepol_handle_t *handle, policydb_t *p, |
186 | | avtab_t *global_avtab, |
187 | | struct bounds_cond_info **cond_info, |
188 | | uint32_t parent) |
189 | 25 | { |
190 | 25 | int rc = 0; |
191 | 25 | struct bounds_expand_args args; |
192 | 25 | cond_list_t *cur; |
193 | | |
194 | 25 | avtab_init(global_avtab); |
195 | 25 | rc = avtab_alloc(global_avtab, BOUNDS_AVTAB_SIZE); |
196 | 25 | if (rc) goto oom; |
197 | | |
198 | 25 | args.handle = handle; |
199 | 25 | args.p = p; |
200 | 25 | args.avtab = global_avtab; |
201 | 25 | args.parent = parent; |
202 | 25 | rc = avtab_map(&p->te_avtab, bounds_expand_rule_callback, &args); |
203 | 25 | if (rc) goto exit; |
204 | | |
205 | 25 | *cond_info = NULL; |
206 | 36 | for (cur = p->cond_list; cur; cur = cur->next) { |
207 | 11 | struct bounds_cond_info *ci; |
208 | 11 | ci = malloc(sizeof(struct bounds_cond_info)); |
209 | 11 | if (!ci) goto oom; |
210 | 11 | avtab_init(&ci->true_avtab); |
211 | 11 | avtab_init(&ci->false_avtab); |
212 | 11 | ci->cond_list = cur; |
213 | 11 | ci->next = *cond_info; |
214 | 11 | *cond_info = ci; |
215 | 11 | if (cur->true_list) { |
216 | 11 | rc = avtab_alloc(&ci->true_avtab, BOUNDS_AVTAB_SIZE); |
217 | 11 | if (rc) goto oom; |
218 | 11 | rc = bounds_expand_cond_rules(handle, p, cur->true_list, |
219 | 11 | &ci->true_avtab, NULL, |
220 | 11 | NULL, parent); |
221 | 11 | if (rc) goto exit; |
222 | 11 | } |
223 | 11 | if (cur->false_list) { |
224 | 10 | rc = avtab_alloc(&ci->false_avtab, BOUNDS_AVTAB_SIZE); |
225 | 10 | if (rc) goto oom; |
226 | 10 | rc = bounds_expand_cond_rules(handle, p, cur->false_list, |
227 | 10 | &ci->false_avtab, |
228 | 10 | global_avtab, |
229 | 10 | &ci->true_avtab, parent); |
230 | 10 | if (rc) goto exit; |
231 | 10 | } |
232 | 11 | } |
233 | | |
234 | 25 | return 0; |
235 | | |
236 | 0 | oom: |
237 | 0 | ERR(handle, "Insufficient memory"); |
238 | |
|
239 | 0 | exit: |
240 | 0 | ERR(handle,"Failed to expand parent rules"); |
241 | 0 | avtab_destroy(global_avtab); |
242 | 0 | bounds_destroy_cond_info(*cond_info); |
243 | 0 | *cond_info = NULL; |
244 | 0 | return rc; |
245 | 0 | } |
246 | | |
247 | | static int bounds_not_covered(avtab_t *global_avtab, avtab_t *cur_avtab, |
248 | | avtab_key_t *avtab_key, uint32_t data) |
249 | 12 | { |
250 | 12 | avtab_datum_t *datum = avtab_search(cur_avtab, avtab_key); |
251 | 12 | if (datum) |
252 | 12 | data &= ~datum->data; |
253 | 12 | if (global_avtab && data) { |
254 | 0 | datum = avtab_search(global_avtab, avtab_key); |
255 | 0 | if (datum) |
256 | 0 | data &= ~datum->data; |
257 | 0 | } |
258 | | |
259 | 12 | return data; |
260 | 12 | } |
261 | | |
262 | | static int bounds_add_bad(sepol_handle_t *handle, uint32_t src, uint32_t tgt, |
263 | | uint32_t class, uint32_t data, avtab_ptr_t *bad) |
264 | 0 | { |
265 | 0 | struct avtab_node *new = malloc(sizeof(struct avtab_node)); |
266 | 0 | if (new == NULL) { |
267 | 0 | ERR(handle, "Insufficient memory"); |
268 | 0 | return SEPOL_ENOMEM; |
269 | 0 | } |
270 | 0 | memset(new, 0, sizeof(struct avtab_node)); |
271 | 0 | new->key.source_type = src; |
272 | 0 | new->key.target_type = tgt; |
273 | 0 | new->key.target_class = class; |
274 | 0 | new->datum.data = data; |
275 | 0 | new->next = *bad; |
276 | 0 | *bad = new; |
277 | |
|
278 | 0 | return 0; |
279 | 0 | } |
280 | | |
281 | | static int bounds_check_rule(sepol_handle_t *handle, policydb_t *p, |
282 | | avtab_t *global_avtab, avtab_t *cur_avtab, |
283 | | uint32_t child, uint32_t parent, uint32_t src, |
284 | | uint32_t tgt, uint32_t class, uint32_t data, |
285 | | avtab_ptr_t *bad, int *numbad) |
286 | 12 | { |
287 | 12 | int rc = 0; |
288 | 12 | avtab_key_t avtab_key; |
289 | 12 | type_datum_t *td; |
290 | 12 | ebitmap_node_t *tnode; |
291 | 12 | unsigned int i; |
292 | 12 | uint32_t d; |
293 | | |
294 | 12 | avtab_key.specified = AVTAB_ALLOWED; |
295 | 12 | avtab_key.target_class = class; |
296 | | |
297 | 12 | if (ebitmap_get_bit(&p->attr_type_map[src - 1], child - 1)) { |
298 | 12 | avtab_key.source_type = parent; |
299 | 768 | ebitmap_for_each_positive_bit(&p->attr_type_map[tgt - 1], tnode, i) { |
300 | 12 | td = p->type_val_to_struct[i]; |
301 | 12 | if (td && td->bounds) { |
302 | 10 | avtab_key.target_type = td->bounds; |
303 | 10 | d = bounds_not_covered(global_avtab, cur_avtab, |
304 | 10 | &avtab_key, data); |
305 | 10 | } else { |
306 | 2 | avtab_key.target_type = i + 1; |
307 | 2 | d = bounds_not_covered(global_avtab, cur_avtab, |
308 | 2 | &avtab_key, data); |
309 | 2 | } |
310 | 12 | if (d) { |
311 | 0 | (*numbad)++; |
312 | 0 | rc = bounds_add_bad(handle, child, i+1, class, d, bad); |
313 | 0 | if (rc) goto exit; |
314 | 0 | } |
315 | 12 | } |
316 | 12 | } |
317 | | |
318 | 12 | exit: |
319 | 12 | return rc; |
320 | 12 | } |
321 | | |
322 | | static int bounds_check_cond_rules(sepol_handle_t *handle, policydb_t *p, |
323 | | avtab_t *global_avtab, avtab_t *cond_avtab, |
324 | | cond_av_list_t *rules, uint32_t child, |
325 | | uint32_t parent, avtab_ptr_t *bad, |
326 | | int *numbad) |
327 | 22 | { |
328 | 22 | int rc = 0; |
329 | 22 | cond_av_list_t *cur; |
330 | | |
331 | 361 | for (cur = rules; cur; cur = cur->next) { |
332 | 339 | avtab_ptr_t ap = cur->node; |
333 | 339 | avtab_key_t *key = &ap->key; |
334 | 339 | avtab_datum_t *datum = &ap->datum; |
335 | 339 | if (!(key->specified & AVTAB_ALLOWED)) |
336 | 339 | continue; |
337 | 0 | rc = bounds_check_rule(handle, p, global_avtab, cond_avtab, |
338 | 0 | child, parent, key->source_type, |
339 | 0 | key->target_type, key->target_class, |
340 | 0 | datum->data, bad, numbad); |
341 | 0 | if (rc) goto exit; |
342 | 0 | } |
343 | | |
344 | 22 | exit: |
345 | 22 | return rc; |
346 | 22 | } |
347 | | |
348 | | struct bounds_check_args { |
349 | | sepol_handle_t *handle; |
350 | | policydb_t *p; |
351 | | avtab_t *cur_avtab; |
352 | | uint32_t child; |
353 | | uint32_t parent; |
354 | | avtab_ptr_t bad; |
355 | | int numbad; |
356 | | }; |
357 | | |
358 | | static int bounds_check_rule_callback(avtab_key_t *k, avtab_datum_t *d, |
359 | | void *args) |
360 | 43 | { |
361 | 43 | struct bounds_check_args *a = (struct bounds_check_args *)args; |
362 | | |
363 | 43 | if (!(k->specified & AVTAB_ALLOWED)) |
364 | 31 | return 0; |
365 | | |
366 | 12 | return bounds_check_rule(a->handle, a->p, NULL, a->cur_avtab, a->child, |
367 | 12 | a->parent, k->source_type, k->target_type, |
368 | 12 | k->target_class, d->data, &a->bad, &a->numbad); |
369 | 43 | } |
370 | | |
371 | | static int bounds_check_child_rules(sepol_handle_t *handle, policydb_t *p, |
372 | | avtab_t *global_avtab, |
373 | | struct bounds_cond_info *cond_info, |
374 | | uint32_t child, uint32_t parent, |
375 | | avtab_ptr_t *bad, int *numbad) |
376 | 25 | { |
377 | 25 | int rc; |
378 | 25 | struct bounds_check_args args; |
379 | 25 | struct bounds_cond_info *cur; |
380 | | |
381 | 25 | args.handle = handle; |
382 | 25 | args.p = p; |
383 | 25 | args.cur_avtab = global_avtab; |
384 | 25 | args.child = child; |
385 | 25 | args.parent = parent; |
386 | 25 | args.bad = NULL; |
387 | 25 | args.numbad = 0; |
388 | 25 | rc = avtab_map(&p->te_avtab, bounds_check_rule_callback, &args); |
389 | 25 | if (rc) goto exit; |
390 | | |
391 | 36 | for (cur = cond_info; cur; cur = cur->next) { |
392 | 11 | cond_list_t *node = cur->cond_list; |
393 | 11 | rc = bounds_check_cond_rules(handle, p, global_avtab, |
394 | 11 | &cur->true_avtab, |
395 | 11 | node->true_list, child, parent, |
396 | 11 | &args.bad, &args.numbad); |
397 | 11 | if (rc) goto exit; |
398 | | |
399 | 11 | rc = bounds_check_cond_rules(handle, p, global_avtab, |
400 | 11 | &cur->false_avtab, |
401 | 11 | node->false_list, child, parent, |
402 | 11 | &args.bad, &args.numbad); |
403 | 11 | if (rc) goto exit; |
404 | 11 | } |
405 | | |
406 | 25 | *numbad += args.numbad; |
407 | 25 | *bad = args.bad; |
408 | | |
409 | 25 | exit: |
410 | 25 | return rc; |
411 | 25 | } |
412 | | |
413 | | int bounds_check_type(sepol_handle_t *handle, policydb_t *p, uint32_t child, |
414 | | uint32_t parent, avtab_ptr_t *bad, int *numbad) |
415 | 25 | { |
416 | 25 | int rc = 0; |
417 | 25 | avtab_t global_avtab; |
418 | 25 | struct bounds_cond_info *cond_info = NULL; |
419 | | |
420 | 25 | rc = bounds_expand_parent_rules(handle, p, &global_avtab, &cond_info, parent); |
421 | 25 | if (rc) goto exit; |
422 | | |
423 | 25 | rc = bounds_check_child_rules(handle, p, &global_avtab, cond_info, |
424 | 25 | child, parent, bad, numbad); |
425 | | |
426 | 25 | bounds_destroy_cond_info(cond_info); |
427 | 25 | avtab_destroy(&global_avtab); |
428 | | |
429 | 25 | exit: |
430 | 25 | return rc; |
431 | 25 | } |
432 | | |
433 | | struct bounds_args { |
434 | | sepol_handle_t *handle; |
435 | | policydb_t *p; |
436 | | int numbad; |
437 | | }; |
438 | | |
439 | | static void bounds_report(sepol_handle_t *handle, policydb_t *p, uint32_t child, |
440 | | uint32_t parent, avtab_ptr_t cur) |
441 | 0 | { |
442 | 0 | ERR(handle, "Child type %s exceeds bounds of parent %s in the following rules:", |
443 | 0 | p->p_type_val_to_name[child - 1], |
444 | 0 | p->p_type_val_to_name[parent - 1]); |
445 | 0 | for (; cur; cur = cur->next) { |
446 | 0 | char *permstr = sepol_av_to_string(p, cur->key.target_class, cur->datum.data); |
447 | |
|
448 | 0 | ERR(handle, " %s %s : %s { %s }", |
449 | 0 | p->p_type_val_to_name[cur->key.source_type - 1], |
450 | 0 | p->p_type_val_to_name[cur->key.target_type - 1], |
451 | 0 | p->p_class_val_to_name[cur->key.target_class - 1], |
452 | 0 | permstr ?: "<format-failure>"); |
453 | |
|
454 | 0 | free(permstr); |
455 | 0 | } |
456 | 0 | } |
457 | | |
458 | | void bounds_destroy_bad(avtab_ptr_t cur) |
459 | 0 | { |
460 | 0 | avtab_ptr_t next; |
461 | |
|
462 | 0 | for (; cur; cur = next) { |
463 | 0 | next = cur->next; |
464 | 0 | cur->next = NULL; |
465 | 0 | free(cur); |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | | static int bounds_check_type_callback(hashtab_key_t k __attribute__ ((unused)), |
470 | | hashtab_datum_t d, void *args) |
471 | 72 | { |
472 | 72 | int rc = 0; |
473 | 72 | struct bounds_args *a = (struct bounds_args *)args; |
474 | 72 | type_datum_t *t = (type_datum_t *)d; |
475 | 72 | avtab_ptr_t bad = NULL; |
476 | | |
477 | 72 | if (t->bounds) { |
478 | 25 | rc = bounds_check_type(a->handle, a->p, t->s.value, t->bounds, |
479 | 25 | &bad, &a->numbad); |
480 | 25 | if (bad) { |
481 | 0 | bounds_report(a->handle, a->p, t->s.value, t->bounds, |
482 | 0 | bad); |
483 | 0 | bounds_destroy_bad(bad); |
484 | 0 | } |
485 | 25 | } |
486 | | |
487 | 72 | return rc; |
488 | 72 | } |
489 | | |
490 | | int bounds_check_types(sepol_handle_t *handle, policydb_t *p) |
491 | 46 | { |
492 | 46 | int rc; |
493 | 46 | struct bounds_args args; |
494 | | |
495 | 46 | args.handle = handle; |
496 | 46 | args.p = p; |
497 | 46 | args.numbad = 0; |
498 | | |
499 | 46 | rc = hashtab_map(p->p_types.table, bounds_check_type_callback, &args); |
500 | 46 | if (rc) goto exit; |
501 | | |
502 | 46 | if (args.numbad > 0) { |
503 | 0 | ERR(handle, "%d errors found during type bounds check", |
504 | 0 | args.numbad); |
505 | 0 | rc = SEPOL_ERR; |
506 | 0 | } |
507 | | |
508 | 46 | exit: |
509 | 46 | return rc; |
510 | 46 | } |
511 | | |
512 | | /* The role bounds is defined as: a child role cannot have a type that |
513 | | * its parent doesn't have. |
514 | | */ |
515 | | static int bounds_check_role_callback(hashtab_key_t k, |
516 | | hashtab_datum_t d, void *args) |
517 | 55 | { |
518 | 55 | struct bounds_args *a = (struct bounds_args *)args; |
519 | 55 | role_datum_t *r = (role_datum_t *) d; |
520 | 55 | role_datum_t *rp = NULL; |
521 | | |
522 | 55 | if (!r->bounds) |
523 | 54 | return 0; |
524 | | |
525 | 1 | rp = a->p->role_val_to_struct[r->bounds - 1]; |
526 | | |
527 | 1 | if (rp && !ebitmap_contains(&rp->types.types, &r->types.types)) { |
528 | 1 | ERR(a->handle, "Role bounds violation, %s exceeds %s", |
529 | 1 | (char *)k, a->p->p_role_val_to_name[rp->s.value - 1]); |
530 | 1 | a->numbad++; |
531 | 1 | } |
532 | | |
533 | 1 | return 0; |
534 | 55 | } |
535 | | |
536 | | int bounds_check_roles(sepol_handle_t *handle, policydb_t *p) |
537 | 46 | { |
538 | 46 | struct bounds_args args; |
539 | | |
540 | 46 | args.handle = handle; |
541 | 46 | args.p = p; |
542 | 46 | args.numbad = 0; |
543 | | |
544 | 46 | hashtab_map(p->p_roles.table, bounds_check_role_callback, &args); |
545 | | |
546 | 46 | if (args.numbad > 0) { |
547 | 1 | ERR(handle, "%d errors found during role bounds check", |
548 | 1 | args.numbad); |
549 | 1 | return SEPOL_ERR; |
550 | 1 | } |
551 | | |
552 | 45 | return 0; |
553 | 46 | } |
554 | | |
555 | | /* The user bounds is defined as: a child user cannot have a role that |
556 | | * its parent doesn't have. |
557 | | */ |
558 | | static int bounds_check_user_callback(hashtab_key_t k, |
559 | | hashtab_datum_t d, void *args) |
560 | 12 | { |
561 | 12 | struct bounds_args *a = (struct bounds_args *)args; |
562 | 12 | user_datum_t *u = (user_datum_t *) d; |
563 | 12 | user_datum_t *up = NULL; |
564 | | |
565 | 12 | if (!u->bounds) |
566 | 9 | return 0; |
567 | | |
568 | 3 | up = a->p->user_val_to_struct[u->bounds - 1]; |
569 | | |
570 | 3 | if (up && !ebitmap_contains(&up->roles.roles, &u->roles.roles)) { |
571 | 0 | ERR(a->handle, "User bounds violation, %s exceeds %s", |
572 | 0 | (char *) k, a->p->p_user_val_to_name[up->s.value - 1]); |
573 | 0 | a->numbad++; |
574 | 0 | } |
575 | | |
576 | 3 | return 0; |
577 | 12 | } |
578 | | |
579 | | int bounds_check_users(sepol_handle_t *handle, policydb_t *p) |
580 | 46 | { |
581 | 46 | struct bounds_args args; |
582 | | |
583 | 46 | args.handle = handle; |
584 | 46 | args.p = p; |
585 | 46 | args.numbad = 0; |
586 | | |
587 | 46 | hashtab_map(p->p_users.table, bounds_check_user_callback, &args); |
588 | | |
589 | 46 | if (args.numbad > 0) { |
590 | 0 | ERR(handle, "%d errors found during user bounds check", |
591 | 0 | args.numbad); |
592 | 0 | return SEPOL_ERR; |
593 | 0 | } |
594 | | |
595 | 46 | return 0; |
596 | 46 | } |
597 | | |
598 | | #define add_hierarchy_callback_template(prefix) \ |
599 | | int hierarchy_add_##prefix##_callback(hashtab_key_t k __attribute__ ((unused)), \ |
600 | 153 | hashtab_datum_t d, void *args) \ |
601 | 153 | { \ |
602 | 153 | struct bounds_args *a = (struct bounds_args *)args; \ |
603 | 153 | sepol_handle_t *handle = a->handle; \ |
604 | 153 | policydb_t *p = a->p; \ |
605 | 153 | prefix##_datum_t *datum = (prefix##_datum_t *)d; \ |
606 | 153 | prefix##_datum_t *parent; \ |
607 | 153 | char *parent_name, *datum_name, *tmp; \ |
608 | 153 | \ |
609 | 153 | if (!datum->bounds) { \ |
610 | 122 | datum_name = p->p_##prefix##_val_to_name[datum->s.value - 1]; \ |
611 | 122 | \ |
612 | 122 | tmp = strrchr(datum_name, '.'); \ |
613 | 122 | /* no '.' means it has no parent */ \ |
614 | 122 | if (!tmp) return 0; \ |
615 | 122 | \ |
616 | 122 | parent_name = strdup(datum_name); \ |
617 | 6 | if (!parent_name) { \ |
618 | 0 | ERR(handle, "Insufficient memory"); \ |
619 | 0 | return SEPOL_ENOMEM; \ |
620 | 0 | } \ |
621 | 6 | parent_name[tmp - datum_name] = '\0'; \ |
622 | 6 | \ |
623 | 6 | parent = hashtab_search(p->p_##prefix##s.table, parent_name); \ |
624 | 6 | if (!parent) { \ |
625 | 6 | /* Orphan type/role/user */ \ |
626 | 6 | ERR(handle, "%s doesn't exist, %s is an orphan",\ |
627 | 6 | parent_name, \ |
628 | 6 | p->p_##prefix##_val_to_name[datum->s.value - 1]); \ |
629 | 6 | free(parent_name); \ |
630 | 6 | a->numbad++; \ |
631 | 6 | return 0; \ |
632 | 6 | } \ |
633 | 6 | datum->bounds = parent->s.value; \ |
634 | 0 | free(parent_name); \ |
635 | 0 | } \ |
636 | 153 | \ |
637 | 153 | return 0; \ |
638 | 153 | } \ |
639 | | |
640 | 80 | static add_hierarchy_callback_template(type) |
641 | 60 | static add_hierarchy_callback_template(role) |
642 | 13 | static add_hierarchy_callback_template(user) |
643 | | |
644 | | int hierarchy_add_bounds(sepol_handle_t *handle, policydb_t *p) |
645 | 50 | { |
646 | 50 | int rc = 0; |
647 | 50 | struct bounds_args args; |
648 | | |
649 | 50 | args.handle = handle; |
650 | 50 | args.p = p; |
651 | 50 | args.numbad = 0; |
652 | | |
653 | 50 | rc = hashtab_map(p->p_users.table, hierarchy_add_user_callback, &args); |
654 | 50 | if (rc) goto exit; |
655 | | |
656 | 50 | rc = hashtab_map(p->p_roles.table, hierarchy_add_role_callback, &args); |
657 | 50 | if (rc) goto exit; |
658 | | |
659 | 50 | rc = hashtab_map(p->p_types.table, hierarchy_add_type_callback, &args); |
660 | 50 | if (rc) goto exit; |
661 | | |
662 | 50 | if (args.numbad > 0) { |
663 | 4 | ERR(handle, "%d errors found while adding hierarchies", |
664 | 4 | args.numbad); |
665 | 4 | rc = SEPOL_ERR; |
666 | 4 | } |
667 | | |
668 | 50 | exit: |
669 | 50 | return rc; |
670 | 50 | } |
671 | | |
672 | | int hierarchy_check_constraints(sepol_handle_t * handle, policydb_t * p) |
673 | 50 | { |
674 | 50 | int rc = 0; |
675 | 50 | int violation = 0; |
676 | | |
677 | 50 | rc = hierarchy_add_bounds(handle, p); |
678 | 50 | if (rc) goto exit; |
679 | | |
680 | 46 | rc = bounds_check_users(handle, p); |
681 | 46 | if (rc) |
682 | 0 | violation = 1; |
683 | | |
684 | 46 | rc = bounds_check_roles(handle, p); |
685 | 46 | if (rc) |
686 | 1 | violation = 1; |
687 | | |
688 | 46 | rc = bounds_check_types(handle, p); |
689 | 46 | if (rc) { |
690 | 0 | if (rc == SEPOL_ERR) |
691 | 0 | violation = 1; |
692 | 0 | else |
693 | 0 | goto exit; |
694 | 0 | } |
695 | | |
696 | 46 | if (violation) |
697 | 1 | rc = SEPOL_ERR; |
698 | | |
699 | 50 | exit: |
700 | 50 | return rc; |
701 | 46 | } |