Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Authors: Karl MacMillan <kmacmillan@tresys.com> |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 2 | * Frank Mayer <mayerf@tresys.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC |
| 5 | * This program is free software; you can redistribute it and/or modify |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 6 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <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 | */ |
| 26 | static 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 | */ |
| 90 | int evaluate_cond_node(struct policydb *p, struct cond_node *node) |
| 91 | { |
| 92 | int new_state; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 93 | struct cond_av_list *cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 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 Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 99 | printk(KERN_ERR "SELinux: expression result was undefined - disabling all rules.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* turn the rules on or off */ |
| 101 | for (cur = node->true_list; cur != NULL; cur = cur->next) { |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 102 | if (new_state <= 0) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 103 | cur->node->key.specified &= ~AVTAB_ENABLED; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 104 | else |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 105 | cur->node->key.specified |= AVTAB_ENABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | for (cur = node->false_list; cur != NULL; cur = cur->next) { |
| 109 | /* -1 or 1 */ |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 110 | if (new_state) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 111 | cur->node->key.specified &= ~AVTAB_ENABLED; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 112 | else |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 113 | cur->node->key.specified |= AVTAB_ENABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int 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 | |
| 129 | static 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 | |
| 139 | static 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 | |
| 152 | static 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 | |
| 165 | void cond_policydb_destroy(struct policydb *p) |
| 166 | { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 167 | kfree(p->bool_val_to_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | avtab_destroy(&p->te_cond_avtab); |
| 169 | cond_list_destroy(p->cond_list); |
| 170 | } |
| 171 | |
| 172 | int cond_init_bool_indexes(struct policydb *p) |
| 173 | { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 174 | kfree(p->bool_val_to_struct); |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 175 | p->bool_val_to_struct = (struct cond_bool_datum **) |
| 176 | kmalloc(p->p_bools.nprim * sizeof(struct cond_bool_datum *), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (!p->bool_val_to_struct) |
| 178 | return -1; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | int cond_destroy_bool(void *key, void *datum, void *p) |
| 183 | { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 184 | kfree(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | kfree(datum); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int 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 Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 201 | p->bool_val_to_struct[booldatum->value - 1] = booldatum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static 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 | |
| 213 | int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp) |
| 214 | { |
| 215 | char *key = NULL; |
| 216 | struct cond_bool_datum *booldatum; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 217 | __le32 buf[3]; |
| 218 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | int rc; |
| 220 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 221 | booldatum = kzalloc(sizeof(struct cond_bool_datum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (!booldatum) |
| 223 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 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; |
| 248 | err: |
| 249 | cond_destroy_bool(key, booldatum, NULL); |
| 250 | return -1; |
| 251 | } |
| 252 | |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 253 | struct cond_insertf_data { |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 254 | struct policydb *p; |
| 255 | struct cond_av_list *other; |
| 256 | struct cond_av_list *head; |
| 257 | struct cond_av_list *tail; |
| 258 | }; |
| 259 | |
| 260 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct avtab_node *node_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | u8 found; |
| 267 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 268 | |
| 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)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 276 | printk("SELinux: type rule already exists outside of a conditional."); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 277 | 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)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 291 | printk("SELinux: too many conflicting type rules."); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 292 | 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) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 302 | printk("SELinux: conflicting type rules.\n"); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 303 | goto err; |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | if (avtab_search(&p->te_cond_avtab, k)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 308 | printk("SELinux: conflicting type rules when adding type rule for true.\n"); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 309 | goto err; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d); |
| 315 | if (!node_ptr) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 316 | printk("SELinux: could not insert rule."); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 317 | goto err; |
| 318 | } |
| 319 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 320 | list = kzalloc(sizeof(struct cond_av_list), GFP_KERNEL); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 321 | if (!list) |
| 322 | goto err; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 323 | |
| 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 | |
| 332 | err: |
| 333 | cond_av_list_destroy(data->head); |
| 334 | data->head = NULL; |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | static 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 Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 341 | __le32 buf[1]; |
| 342 | u32 len; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 343 | struct cond_insertf_data data; |
| 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | *ret_list = NULL; |
| 346 | |
| 347 | len = 0; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 348 | rc = next_entry(buf, fp, sizeof(u32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (rc < 0) |
| 350 | return -1; |
| 351 | |
| 352 | len = le32_to_cpu(buf[0]); |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 353 | if (len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 356 | data.p = p; |
| 357 | data.other = other; |
| 358 | data.head = NULL; |
| 359 | data.tail = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | for (i = 0; i < len; i++) { |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 361 | rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf, |
| 362 | &data); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 363 | if (rc) |
| 364 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | } |
| 367 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 368 | *ret_list = data.head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static int expr_isvalid(struct policydb *p, struct cond_expr *expr) |
| 373 | { |
| 374 | if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 375 | printk("SELinux: conditional expressions uses unknown operator.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | if (expr->bool > p->p_bools.nprim) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 380 | printk("SELinux: conditional expressions uses unknown bool.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp) |
| 387 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 388 | __le32 buf[2]; |
| 389 | u32 len, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | 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 Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 407 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | rc = next_entry(buf, fp, sizeof(u32) * 2); |
| 409 | if (rc < 0) |
| 410 | goto err; |
| 411 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 412 | expr = kzalloc(sizeof(struct cond_expr), GFP_KERNEL); |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 413 | if (!expr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 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 Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 424 | if (i == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | node->expr = expr; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 426 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | last->next = expr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | 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; |
| 436 | err: |
| 437 | cond_node_destroy(node); |
| 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | int cond_read_list(struct policydb *p, void *fp) |
| 442 | { |
| 443 | struct cond_node *node, *last = NULL; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 444 | __le32 buf[1]; |
| 445 | u32 i, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | 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 Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 454 | rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel); |
| 455 | if (rc) |
| 456 | goto err; |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | for (i = 0; i < len; i++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 459 | node = kzalloc(sizeof(struct cond_node), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (!node) |
| 461 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | if (cond_read_node(p, node, fp) != 0) |
| 464 | goto err; |
| 465 | |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 466 | if (i == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | p->cond_list = node; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 468 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | last->next = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | last = node; |
| 471 | } |
| 472 | return 0; |
| 473 | err: |
| 474 | cond_list_destroy(p->cond_list); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 475 | p->cond_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | 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 | */ |
| 482 | void cond_compute_av(struct avtab *ctab, struct avtab_key *key, struct av_decision *avd) |
| 483 | { |
| 484 | struct avtab_node *node; |
| 485 | |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 486 | if (!ctab || !key || !avd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return; |
| 488 | |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 489 | for (node = avtab_search_node(ctab, key); node != NULL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 490 | node = avtab_search_node_next(node, key->specified)) { |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 491 | if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) == |
| 492 | (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED))) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 493 | avd->allowed |= node->datum.data; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 494 | if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) == |
| 495 | (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* 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 Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 501 | avd->auditdeny &= node->datum.data; |
Eric Paris | 7c2b240 | 2008-04-18 17:38:29 -0400 | [diff] [blame] | 502 | if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) == |
| 503 | (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED))) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 504 | avd->auditallow |= node->datum.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | return; |
| 507 | } |