1 /* Authors: Karl MacMillan <kmacmillan@tresys.com>
2 * Frank Mayer <mayerf@tresys.com>
3 *
4 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2.
8 */
9
10 /* Ported to Xen 3.0, George Coker, <gscoker@alpha.ncsc.mil> */
11
12 #include <asm/byteorder.h>
13 #include <xen/lib.h>
14 #include <xen/types.h>
15 #include <xen/errno.h>
16 #include <xen/string.h>
17 #include <xen/spinlock.h>
18 #include <xen/xmalloc.h>
19
20 #include "security.h"
21 #include "conditional.h"
22
23 /*
24 * cond_evaluate_expr evaluates a conditional expr
25 * in reverse polish notation. It returns true (1), false (0),
26 * or undefined (-1). Undefined occurs when the expression
27 * exceeds the stack depth of COND_EXPR_MAXDEPTH.
28 */
cond_evaluate_expr(struct policydb * p,struct cond_expr * expr)29 static int cond_evaluate_expr(struct policydb *p, struct cond_expr *expr)
30 {
31 struct cond_expr *cur;
32 int s[COND_EXPR_MAXDEPTH];
33 int sp = -1;
34
35 for ( cur = expr; cur != NULL; cur = cur->next )
36 {
37 switch ( cur->expr_type )
38 {
39 case COND_BOOL:
40 if ( sp == (COND_EXPR_MAXDEPTH - 1) )
41 return -1;
42 sp++;
43 s[sp] = p->bool_val_to_struct[cur->bool_val - 1]->state;
44 break;
45 case COND_NOT:
46 if ( sp < 0 )
47 return -1;
48 s[sp] = !s[sp];
49 break;
50 case COND_OR:
51 if ( sp < 1 )
52 return -1;
53 sp--;
54 s[sp] |= s[sp + 1];
55 break;
56 case COND_AND:
57 if ( sp < 1 )
58 return -1;
59 sp--;
60 s[sp] &= s[sp + 1];
61 break;
62 case COND_XOR:
63 if ( sp < 1 )
64 return -1;
65 sp--;
66 s[sp] ^= s[sp + 1];
67 break;
68 case COND_EQ:
69 if ( sp < 1 )
70 return -1;
71 sp--;
72 s[sp] = (s[sp] == s[sp + 1]);
73 break;
74 case COND_NEQ:
75 if ( sp < 1 )
76 return -1;
77 sp--;
78 s[sp] = (s[sp] != s[sp + 1]);
79 break;
80 default:
81 return -1;
82 }
83 }
84 return s[0];
85 }
86
87 /*
88 * evaluate_cond_node evaluates the conditional stored in
89 * a struct cond_node and if the result is different than the
90 * current state of the node it sets the rules in the true/false
91 * list appropriately. If the result of the expression is undefined
92 * all of the rules are disabled for safety.
93 */
evaluate_cond_node(struct policydb * p,struct cond_node * node)94 int evaluate_cond_node(struct policydb *p, struct cond_node *node)
95 {
96 int new_state;
97 struct cond_av_list* cur;
98
99 new_state = cond_evaluate_expr(p, node->expr);
100 if ( new_state != node->cur_state )
101 {
102 node->cur_state = new_state;
103 if ( new_state == -1 )
104 printk(KERN_ERR "Flask: expression result was undefined - disabling all rules.\n");
105 /* turn the rules on or off */
106 for ( cur = node->true_list; cur != NULL; cur = cur->next )
107 {
108 if ( new_state <= 0 )
109 cur->node->key.specified &= ~AVTAB_ENABLED;
110 else
111 cur->node->key.specified |= AVTAB_ENABLED;
112 }
113
114 for ( cur = node->false_list; cur != NULL; cur = cur->next )
115 {
116 /* -1 or 1 */
117 if ( new_state )
118 cur->node->key.specified &= ~AVTAB_ENABLED;
119 else
120 cur->node->key.specified |= AVTAB_ENABLED;
121 }
122 }
123 return 0;
124 }
125
cond_policydb_init(struct policydb * p)126 int cond_policydb_init(struct policydb *p)
127 {
128 p->bool_val_to_struct = NULL;
129 p->cond_list = NULL;
130 if ( avtab_init(&p->te_cond_avtab) )
131 return -1;
132
133 return 0;
134 }
135
cond_av_list_destroy(struct cond_av_list * list)136 static void cond_av_list_destroy(struct cond_av_list *list)
137 {
138 struct cond_av_list *cur, *next;
139 for ( cur = list; cur != NULL; cur = next )
140 {
141 next = cur->next;
142 /* the avtab_ptr_t node is destroy by the avtab */
143 xfree(cur);
144 }
145 }
146
cond_node_destroy(struct cond_node * node)147 static void cond_node_destroy(struct cond_node *node)
148 {
149 struct cond_expr *cur_expr, *next_expr;
150
151 for ( cur_expr = node->expr; cur_expr != NULL; cur_expr = next_expr )
152 {
153 next_expr = cur_expr->next;
154 xfree(cur_expr);
155 }
156 cond_av_list_destroy(node->true_list);
157 cond_av_list_destroy(node->false_list);
158 xfree(node);
159 }
160
cond_list_destroy(struct cond_node * list)161 static void cond_list_destroy(struct cond_node *list)
162 {
163 struct cond_node *next, *cur;
164
165 if ( list == NULL )
166 return;
167
168 for ( cur = list; cur != NULL; cur = next )
169 {
170 next = cur->next;
171 cond_node_destroy(cur);
172 }
173 }
174
cond_policydb_destroy(struct policydb * p)175 void cond_policydb_destroy(struct policydb *p)
176 {
177 xfree(p->bool_val_to_struct);
178 avtab_destroy(&p->te_cond_avtab);
179 cond_list_destroy(p->cond_list);
180 }
181
cond_init_bool_indexes(struct policydb * p)182 int cond_init_bool_indexes(struct policydb *p)
183 {
184 xfree(p->bool_val_to_struct);
185 p->bool_val_to_struct = (struct cond_bool_datum**)
186 xmalloc_array(struct cond_bool_datum*, p->p_bools.nprim);
187 if ( !p->bool_val_to_struct )
188 return -1;
189 return 0;
190 }
191
cond_destroy_bool(void * key,void * datum,void * p)192 int cond_destroy_bool(void *key, void *datum, void *p)
193 {
194 xfree(key);
195 xfree(datum);
196 return 0;
197 }
198
cond_index_bool(void * key,void * datum,void * datap)199 int cond_index_bool(void *key, void *datum, void *datap)
200 {
201 struct policydb *p;
202 struct cond_bool_datum *booldatum;
203
204 booldatum = datum;
205 p = datap;
206
207 if ( !booldatum->value || booldatum->value > p->p_bools.nprim )
208 return -EINVAL;
209
210 p->p_bool_val_to_name[booldatum->value - 1] = key;
211 p->bool_val_to_struct[booldatum->value -1] = booldatum;
212
213 return 0;
214 }
215
bool_isvalid(struct cond_bool_datum * b)216 static int bool_isvalid(struct cond_bool_datum *b)
217 {
218 if ( !(b->state == 0 || b->state == 1) )
219 return 0;
220 return 1;
221 }
222
cond_read_bool(struct policydb * p,struct hashtab * h,void * fp)223 int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
224 {
225 char *key = NULL;
226 struct cond_bool_datum *booldatum;
227 __le32 buf[3];
228 u32 len;
229 int rc;
230
231 booldatum = xzalloc(struct cond_bool_datum);
232 if ( !booldatum )
233 return -1;
234
235 rc = next_entry(buf, fp, sizeof buf);
236 if ( rc < 0 )
237 goto err;
238
239 booldatum->value = le32_to_cpu(buf[0]);
240 booldatum->state = le32_to_cpu(buf[1]);
241
242 if ( !bool_isvalid(booldatum) )
243 goto err;
244
245 len = le32_to_cpu(buf[2]);
246
247 key = xmalloc_array(char, len + 1);
248 if ( !key )
249 goto err;
250 rc = next_entry(key, fp, len);
251 if ( rc < 0 )
252 goto err;
253 key[len] = 0;
254 if ( hashtab_insert(h, key, booldatum) )
255 goto err;
256
257 return 0;
258 err:
259 cond_destroy_bool(key, booldatum, NULL);
260 return -1;
261 }
262
263 struct cond_insertf_data
264 {
265 struct policydb *p;
266 struct cond_av_list *other;
267 struct cond_av_list *head;
268 struct cond_av_list *tail;
269 };
270
cond_insertf(struct avtab * a,struct avtab_key * k,struct avtab_datum * d,void * ptr)271 static int cond_insertf(struct avtab *a, struct avtab_key *k,
272 struct avtab_datum *d, void *ptr)
273 {
274 struct cond_insertf_data *data = ptr;
275 struct policydb *p = data->p;
276 struct cond_av_list *other = data->other, *list, *cur;
277 struct avtab_node *node_ptr;
278 u8 found;
279
280 /*
281 * For type rules we have to make certain there aren't any
282 * conflicting rules by searching the te_avtab and the
283 * cond_te_avtab.
284 */
285 if ( k->specified & AVTAB_TYPE )
286 {
287 if ( avtab_search(&p->te_avtab, k) )
288 {
289 printk("Flask: type rule already exists outside of a "
290 "conditional.");
291 goto err;
292 }
293 /*
294 * If we are reading the false list other will be a pointer to
295 * the true list. We can have duplicate entries if there is only
296 * 1 other entry and it is in our true list.
297 *
298 * If we are reading the true list (other == NULL) there shouldn't
299 * be any other entries.
300 */
301 if ( other )
302 {
303 node_ptr = avtab_search_node(&p->te_cond_avtab, k);
304 if ( node_ptr )
305 {
306 if ( avtab_search_node_next(node_ptr, k->specified) )
307 {
308 printk("Flask: too many conflicting type rules.");
309 goto err;
310 }
311 found = 0;
312 for ( cur = other; cur != NULL; cur = cur->next )
313 {
314 if ( cur->node == node_ptr )
315 {
316 found = 1;
317 break;
318 }
319 }
320 if ( !found )
321 {
322 printk("Flask: conflicting type rules.\n");
323 goto err;
324 }
325 }
326 }
327 else
328 {
329 if ( avtab_search(&p->te_cond_avtab, k) )
330 {
331 printk("Flask: conflicting type rules when adding type rule "
332 "for true.\n");
333 goto err;
334 }
335 }
336 }
337
338 node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
339 if ( !node_ptr )
340 {
341 printk("Flask: could not insert rule.");
342 goto err;
343 }
344
345 list = xzalloc(struct cond_av_list);
346 if ( !list )
347 goto err;
348
349 list->node = node_ptr;
350 if ( !data->head )
351 data->head = list;
352 else
353 data->tail->next = list;
354 data->tail = list;
355 return 0;
356
357 err:
358 cond_av_list_destroy(data->head);
359 data->head = NULL;
360 return -1;
361 }
362
cond_read_av_list(struct policydb * p,void * fp,struct cond_av_list ** ret_list,struct cond_av_list * other)363 static int cond_read_av_list(struct policydb *p, void *fp,
364 struct cond_av_list **ret_list, struct cond_av_list *other)
365 {
366 int i, rc;
367 __le32 buf[1];
368 u32 len;
369 struct cond_insertf_data data;
370
371 *ret_list = NULL;
372
373 len = 0;
374 rc = next_entry(buf, fp, sizeof(u32));
375 if ( rc < 0 )
376 return -1;
377
378 len = le32_to_cpu(buf[0]);
379 if ( len == 0 )
380 {
381 return 0;
382 }
383
384 data.p = p;
385 data.other = other;
386 data.head = NULL;
387 data.tail = NULL;
388 for ( i = 0; i < len; i++ )
389 {
390 rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf, &data);
391 if ( rc )
392 return rc;
393 }
394
395 *ret_list = data.head;
396 return 0;
397 }
398
expr_isvalid(struct policydb * p,struct cond_expr * expr)399 static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
400 {
401 if ( expr->expr_type <= 0 || expr->expr_type > COND_LAST )
402 {
403 printk("Flask: conditional expressions uses unknown operator.\n");
404 return 0;
405 }
406
407 if ( expr->bool_val > p->p_bools.nprim )
408 {
409 printk("Flask: conditional expressions uses unknown bool.\n");
410 return 0;
411 }
412 return 1;
413 }
414
cond_read_node(struct policydb * p,struct cond_node * node,void * fp)415 static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
416 {
417 __le32 buf[2];
418 u32 len, i;
419 int rc;
420 struct cond_expr *expr = NULL, *last = NULL;
421
422 rc = next_entry(buf, fp, sizeof(u32));
423 if ( rc < 0 )
424 return -1;
425
426 node->cur_state = le32_to_cpu(buf[0]);
427
428 len = 0;
429 rc = next_entry(buf, fp, sizeof(u32));
430 if ( rc < 0 )
431 return -1;
432
433 /* expr */
434 len = le32_to_cpu(buf[0]);
435
436 for ( i = 0; i < len; i++ )
437 {
438 rc = next_entry(buf, fp, sizeof(u32) * 2);
439 if ( rc < 0 )
440 goto err;
441
442 expr = xzalloc(struct cond_expr);
443 if ( !expr )
444 goto err;
445
446 expr->expr_type = le32_to_cpu(buf[0]);
447 expr->bool_val = le32_to_cpu(buf[1]);
448
449 if ( !expr_isvalid(p, expr) )
450 {
451 xfree(expr);
452 goto err;
453 }
454
455 if ( i == 0 )
456 node->expr = expr;
457 else
458 last->next = expr;
459
460 last = expr;
461 }
462
463 if ( cond_read_av_list(p, fp, &node->true_list, NULL) != 0 )
464 goto err;
465 if ( cond_read_av_list(p, fp, &node->false_list, node->true_list) != 0 )
466 goto err;
467 return 0;
468 err:
469 cond_node_destroy(node);
470 return -1;
471 }
472
cond_read_list(struct policydb * p,void * fp)473 int cond_read_list(struct policydb *p, void *fp)
474 {
475 struct cond_node *node, *last = NULL;
476 __le32 buf[1];
477 u32 i, len;
478 int rc;
479
480 rc = next_entry(buf, fp, sizeof buf);
481 if ( rc < 0 )
482 return -1;
483
484 len = le32_to_cpu(buf[0]);
485
486 rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
487 if ( rc )
488 goto err;
489
490 for ( i = 0; i < len; i++ )
491 {
492 node = xzalloc(struct cond_node);
493 if ( !node )
494 goto err;
495
496 if ( cond_read_node(p, node, fp) != 0 )
497 goto err;
498
499 if ( i == 0 )
500 p->cond_list = node;
501 else
502 last->next = node;
503
504 last = node;
505 }
506 return 0;
507 err:
508 cond_list_destroy(p->cond_list);
509 p->cond_list = NULL;
510 return -1;
511 }
512
513 /* Determine whether additional permissions are granted by the conditional
514 * av table, and if so, add them to the result
515 */
cond_compute_av(struct avtab * ctab,struct avtab_key * key,struct av_decision * avd)516 void cond_compute_av(struct avtab *ctab, struct avtab_key *key,
517 struct av_decision *avd)
518 {
519 struct avtab_node *node;
520
521 if( !ctab || !key || !avd )
522 return;
523
524 for( node = avtab_search_node(ctab, key); node != NULL;
525 node = avtab_search_node_next(node, key->specified) )
526 {
527 if ( (u16) (AVTAB_ALLOWED|AVTAB_ENABLED) ==
528 (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)) )
529 avd->allowed |= node->datum.data;
530 if ( (u16) (AVTAB_AUDITDENY|AVTAB_ENABLED) ==
531 (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)) )
532 /* Since a '0' in an auditdeny mask represents a
533 * permission we do NOT want to audit (dontaudit), we use
534 * the '&' operand to ensure that all '0's in the mask
535 * are retained (much unlike the allow and auditallow cases).
536 */
537 avd->auditdeny &= node->datum.data;
538 if ( (u16) (AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
539 (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)) )
540 avd->auditallow |= node->datum.data;
541 }
542 return;
543 }
544