blob: 3a464c75d0479567a35bec0d6ac644226f95d74f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Authors: Karl MacMillan <kmacmillan@tresys.com>
Eric Paris7c2b2402008-04-18 17:38:29 -04002 * Frank Mayer <mayerf@tresys.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
5 * This program is free software; you can redistribute it and/or modify
Eric Paris7c2b2402008-04-18 17:38:29 -04006 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 <asm/semaphore.h>
15#include <linux/slab.h>
16
17#include "security.h"
18#include "conditional.h"
19
20/*
21 * cond_evaluate_expr evaluates a conditional expr
22 * in reverse polish notation. It returns true (1), false (0),
23 * or undefined (-1). Undefined occurs when the expression
24 * exceeds the stack depth of COND_EXPR_MAXDEPTH.
25 */
26static int cond_evaluate_expr(struct policydb *p, struct cond_expr *expr)
27{
28
29 struct cond_expr *cur;
30 int s[COND_EXPR_MAXDEPTH];
31 int sp = -1;
32
33 for (cur = expr; cur != NULL; cur = cur->next) {
34 switch (cur->expr_type) {
35 case COND_BOOL:
36 if (sp == (COND_EXPR_MAXDEPTH - 1))
37 return -1;
38 sp++;
39 s[sp] = p->bool_val_to_struct[cur->bool - 1]->state;
40 break;
41 case COND_NOT:
42 if (sp < 0)
43 return -1;
44 s[sp] = !s[sp];
45 break;
46 case COND_OR:
47 if (sp < 1)
48 return -1;
49 sp--;
50 s[sp] |= s[sp + 1];
51 break;
52 case COND_AND:
53 if (sp < 1)
54 return -1;
55 sp--;
56 s[sp] &= s[sp + 1];
57 break;
58 case COND_XOR:
59 if (sp < 1)
60 return -1;
61 sp--;
62 s[sp] ^= s[sp + 1];
63 break;
64 case COND_EQ:
65 if (sp < 1)
66 return -1;
67 sp--;
68 s[sp] = (s[sp] == s[sp + 1]);
69 break;
70 case COND_NEQ:
71 if (sp < 1)
72 return -1;
73 sp--;
74 s[sp] = (s[sp] != s[sp + 1]);
75 break;
76 default:
77 return -1;
78 }
79 }
80 return s[0];
81}
82
83/*
84 * evaluate_cond_node evaluates the conditional stored in
85 * a struct cond_node and if the result is different than the
86 * current state of the node it sets the rules in the true/false
87 * list appropriately. If the result of the expression is undefined
88 * all of the rules are disabled for safety.
89 */
90int evaluate_cond_node(struct policydb *p, struct cond_node *node)
91{
92 int new_state;
Eric Paris7c2b2402008-04-18 17:38:29 -040093 struct cond_av_list *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 new_state = cond_evaluate_expr(p, node->expr);
96 if (new_state != node->cur_state) {
97 node->cur_state = new_state;
98 if (new_state == -1)
James Morris454d9722008-02-26 20:42:02 +110099 printk(KERN_ERR "SELinux: expression result was undefined - disabling all rules.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* turn the rules on or off */
101 for (cur = node->true_list; cur != NULL; cur = cur->next) {
Eric Paris7c2b2402008-04-18 17:38:29 -0400102 if (new_state <= 0)
Stephen Smalley782ebb92005-09-03 15:55:16 -0700103 cur->node->key.specified &= ~AVTAB_ENABLED;
Eric Paris7c2b2402008-04-18 17:38:29 -0400104 else
Stephen Smalley782ebb92005-09-03 15:55:16 -0700105 cur->node->key.specified |= AVTAB_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
108 for (cur = node->false_list; cur != NULL; cur = cur->next) {
109 /* -1 or 1 */
Eric Paris7c2b2402008-04-18 17:38:29 -0400110 if (new_state)
Stephen Smalley782ebb92005-09-03 15:55:16 -0700111 cur->node->key.specified &= ~AVTAB_ENABLED;
Eric Paris7c2b2402008-04-18 17:38:29 -0400112 else
Stephen Smalley782ebb92005-09-03 15:55:16 -0700113 cur->node->key.specified |= AVTAB_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 }
116 return 0;
117}
118
119int cond_policydb_init(struct policydb *p)
120{
121 p->bool_val_to_struct = NULL;
122 p->cond_list = NULL;
123 if (avtab_init(&p->te_cond_avtab))
124 return -1;
125
126 return 0;
127}
128
129static void cond_av_list_destroy(struct cond_av_list *list)
130{
131 struct cond_av_list *cur, *next;
132 for (cur = list; cur != NULL; cur = next) {
133 next = cur->next;
134 /* the avtab_ptr_t node is destroy by the avtab */
135 kfree(cur);
136 }
137}
138
139static void cond_node_destroy(struct cond_node *node)
140{
141 struct cond_expr *cur_expr, *next_expr;
142
143 for (cur_expr = node->expr; cur_expr != NULL; cur_expr = next_expr) {
144 next_expr = cur_expr->next;
145 kfree(cur_expr);
146 }
147 cond_av_list_destroy(node->true_list);
148 cond_av_list_destroy(node->false_list);
149 kfree(node);
150}
151
152static void cond_list_destroy(struct cond_node *list)
153{
154 struct cond_node *next, *cur;
155
156 if (list == NULL)
157 return;
158
159 for (cur = list; cur != NULL; cur = next) {
160 next = cur->next;
161 cond_node_destroy(cur);
162 }
163}
164
165void cond_policydb_destroy(struct policydb *p)
166{
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700167 kfree(p->bool_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 avtab_destroy(&p->te_cond_avtab);
169 cond_list_destroy(p->cond_list);
170}
171
172int cond_init_bool_indexes(struct policydb *p)
173{
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700174 kfree(p->bool_val_to_struct);
Eric Paris7c2b2402008-04-18 17:38:29 -0400175 p->bool_val_to_struct = (struct cond_bool_datum **)
176 kmalloc(p->p_bools.nprim * sizeof(struct cond_bool_datum *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!p->bool_val_to_struct)
178 return -1;
179 return 0;
180}
181
182int cond_destroy_bool(void *key, void *datum, void *p)
183{
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700184 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kfree(datum);
186 return 0;
187}
188
189int cond_index_bool(void *key, void *datum, void *datap)
190{
191 struct policydb *p;
192 struct cond_bool_datum *booldatum;
193
194 booldatum = datum;
195 p = datap;
196
197 if (!booldatum->value || booldatum->value > p->p_bools.nprim)
198 return -EINVAL;
199
200 p->p_bool_val_to_name[booldatum->value - 1] = key;
Eric Paris7c2b2402008-04-18 17:38:29 -0400201 p->bool_val_to_struct[booldatum->value - 1] = booldatum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return 0;
204}
205
206static int bool_isvalid(struct cond_bool_datum *b)
207{
208 if (!(b->state == 0 || b->state == 1))
209 return 0;
210 return 1;
211}
212
213int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
214{
215 char *key = NULL;
216 struct cond_bool_datum *booldatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700217 __le32 buf[3];
218 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int rc;
220
James Morris89d155e2005-10-30 14:59:21 -0800221 booldatum = kzalloc(sizeof(struct cond_bool_datum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!booldatum)
223 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 rc = next_entry(buf, fp, sizeof buf);
226 if (rc < 0)
227 goto err;
228
229 booldatum->value = le32_to_cpu(buf[0]);
230 booldatum->state = le32_to_cpu(buf[1]);
231
232 if (!bool_isvalid(booldatum))
233 goto err;
234
235 len = le32_to_cpu(buf[2]);
236
237 key = kmalloc(len + 1, GFP_KERNEL);
238 if (!key)
239 goto err;
240 rc = next_entry(key, fp, len);
241 if (rc < 0)
242 goto err;
243 key[len] = 0;
244 if (hashtab_insert(h, key, booldatum))
245 goto err;
246
247 return 0;
248err:
249 cond_destroy_bool(key, booldatum, NULL);
250 return -1;
251}
252
Eric Paris7c2b2402008-04-18 17:38:29 -0400253struct cond_insertf_data {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700254 struct policydb *p;
255 struct cond_av_list *other;
256 struct cond_av_list *head;
257 struct cond_av_list *tail;
258};
259
260static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr)
261{
262 struct cond_insertf_data *data = ptr;
263 struct policydb *p = data->p;
264 struct cond_av_list *other = data->other, *list, *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct avtab_node *node_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 u8 found;
267
Stephen Smalley782ebb92005-09-03 15:55:16 -0700268
269 /*
270 * For type rules we have to make certain there aren't any
271 * conflicting rules by searching the te_avtab and the
272 * cond_te_avtab.
273 */
274 if (k->specified & AVTAB_TYPE) {
275 if (avtab_search(&p->te_avtab, k)) {
Eric Paris744ba352008-04-17 11:52:44 -0400276 printk(KERN_ERR "SELinux: type rule already exists outside of a conditional.\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700277 goto err;
278 }
279 /*
280 * If we are reading the false list other will be a pointer to
281 * the true list. We can have duplicate entries if there is only
282 * 1 other entry and it is in our true list.
283 *
284 * If we are reading the true list (other == NULL) there shouldn't
285 * be any other entries.
286 */
287 if (other) {
288 node_ptr = avtab_search_node(&p->te_cond_avtab, k);
289 if (node_ptr) {
290 if (avtab_search_node_next(node_ptr, k->specified)) {
Eric Paris744ba352008-04-17 11:52:44 -0400291 printk(KERN_ERR "SELinux: too many conflicting type rules.\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700292 goto err;
293 }
294 found = 0;
295 for (cur = other; cur != NULL; cur = cur->next) {
296 if (cur->node == node_ptr) {
297 found = 1;
298 break;
299 }
300 }
301 if (!found) {
Eric Paris744ba352008-04-17 11:52:44 -0400302 printk(KERN_ERR "SELinux: conflicting type rules.\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700303 goto err;
304 }
305 }
306 } else {
307 if (avtab_search(&p->te_cond_avtab, k)) {
Eric Paris744ba352008-04-17 11:52:44 -0400308 printk(KERN_ERR "SELinux: conflicting type rules when adding type rule for true.\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700309 goto err;
310 }
311 }
312 }
313
314 node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
315 if (!node_ptr) {
Eric Paris744ba352008-04-17 11:52:44 -0400316 printk(KERN_ERR "SELinux: could not insert rule.\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700317 goto err;
318 }
319
James Morris89d155e2005-10-30 14:59:21 -0800320 list = kzalloc(sizeof(struct cond_av_list), GFP_KERNEL);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700321 if (!list)
322 goto err;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700323
324 list->node = node_ptr;
325 if (!data->head)
326 data->head = list;
327 else
328 data->tail->next = list;
329 data->tail = list;
330 return 0;
331
332err:
333 cond_av_list_destroy(data->head);
334 data->head = NULL;
335 return -1;
336}
337
338static int cond_read_av_list(struct policydb *p, void *fp, struct cond_av_list **ret_list, struct cond_av_list *other)
339{
340 int i, rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700341 __le32 buf[1];
342 u32 len;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700343 struct cond_insertf_data data;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *ret_list = NULL;
346
347 len = 0;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700348 rc = next_entry(buf, fp, sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (rc < 0)
350 return -1;
351
352 len = le32_to_cpu(buf[0]);
Eric Paris7c2b2402008-04-18 17:38:29 -0400353 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Stephen Smalley782ebb92005-09-03 15:55:16 -0700356 data.p = p;
357 data.other = other;
358 data.head = NULL;
359 data.tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 for (i = 0; i < len; i++) {
Stephen Smalley45e54212007-11-07 10:08:00 -0500361 rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
362 &data);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700363 if (rc)
364 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 }
367
Stephen Smalley782ebb92005-09-03 15:55:16 -0700368 *ret_list = data.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
373{
374 if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
Eric Paris744ba352008-04-17 11:52:44 -0400375 printk(KERN_ERR "SELinux: conditional expressions uses unknown operator.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377 }
378
379 if (expr->bool > p->p_bools.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400380 printk(KERN_ERR "SELinux: conditional expressions uses unknown bool.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
382 }
383 return 1;
384}
385
386static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
387{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700388 __le32 buf[2];
389 u32 len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int rc;
391 struct cond_expr *expr = NULL, *last = NULL;
392
393 rc = next_entry(buf, fp, sizeof(u32));
394 if (rc < 0)
395 return -1;
396
397 node->cur_state = le32_to_cpu(buf[0]);
398
399 len = 0;
400 rc = next_entry(buf, fp, sizeof(u32));
401 if (rc < 0)
402 return -1;
403
404 /* expr */
405 len = le32_to_cpu(buf[0]);
406
Eric Paris7c2b2402008-04-18 17:38:29 -0400407 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rc = next_entry(buf, fp, sizeof(u32) * 2);
409 if (rc < 0)
410 goto err;
411
James Morris89d155e2005-10-30 14:59:21 -0800412 expr = kzalloc(sizeof(struct cond_expr), GFP_KERNEL);
Eric Paris7c2b2402008-04-18 17:38:29 -0400413 if (!expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 expr->expr_type = le32_to_cpu(buf[0]);
417 expr->bool = le32_to_cpu(buf[1]);
418
419 if (!expr_isvalid(p, expr)) {
420 kfree(expr);
421 goto err;
422 }
423
Eric Paris7c2b2402008-04-18 17:38:29 -0400424 if (i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 node->expr = expr;
Eric Paris7c2b2402008-04-18 17:38:29 -0400426 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 last->next = expr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 last = expr;
429 }
430
431 if (cond_read_av_list(p, fp, &node->true_list, NULL) != 0)
432 goto err;
433 if (cond_read_av_list(p, fp, &node->false_list, node->true_list) != 0)
434 goto err;
435 return 0;
436err:
437 cond_node_destroy(node);
438 return -1;
439}
440
441int cond_read_list(struct policydb *p, void *fp)
442{
443 struct cond_node *node, *last = NULL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700444 __le32 buf[1];
445 u32 i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int rc;
447
448 rc = next_entry(buf, fp, sizeof buf);
449 if (rc < 0)
450 return -1;
451
452 len = le32_to_cpu(buf[0]);
453
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900454 rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
455 if (rc)
456 goto err;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 for (i = 0; i < len; i++) {
James Morris89d155e2005-10-30 14:59:21 -0800459 node = kzalloc(sizeof(struct cond_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!node)
461 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (cond_read_node(p, node, fp) != 0)
464 goto err;
465
Eric Paris7c2b2402008-04-18 17:38:29 -0400466 if (i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 p->cond_list = node;
Eric Paris7c2b2402008-04-18 17:38:29 -0400468 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 last->next = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 last = node;
471 }
472 return 0;
473err:
474 cond_list_destroy(p->cond_list);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700475 p->cond_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -1;
477}
478
479/* Determine whether additional permissions are granted by the conditional
480 * av table, and if so, add them to the result
481 */
482void cond_compute_av(struct avtab *ctab, struct avtab_key *key, struct av_decision *avd)
483{
484 struct avtab_node *node;
485
Eric Paris7c2b2402008-04-18 17:38:29 -0400486 if (!ctab || !key || !avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return;
488
Eric Paris7c2b2402008-04-18 17:38:29 -0400489 for (node = avtab_search_node(ctab, key); node != NULL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700490 node = avtab_search_node_next(node, key->specified)) {
Eric Paris7c2b2402008-04-18 17:38:29 -0400491 if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) ==
492 (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)))
Stephen Smalley782ebb92005-09-03 15:55:16 -0700493 avd->allowed |= node->datum.data;
Eric Paris7c2b2402008-04-18 17:38:29 -0400494 if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) ==
495 (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Since a '0' in an auditdeny mask represents a
497 * permission we do NOT want to audit (dontaudit), we use
498 * the '&' operand to ensure that all '0's in the mask
499 * are retained (much unlike the allow and auditallow cases).
500 */
Stephen Smalley782ebb92005-09-03 15:55:16 -0700501 avd->auditdeny &= node->datum.data;
Eric Paris7c2b2402008-04-18 17:38:29 -0400502 if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
503 (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)))
Stephen Smalley782ebb92005-09-03 15:55:16 -0700504 avd->auditallow |= node->datum.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 return;
507}