conditional.c 11 KB

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