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