conditional.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "SELinux: 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->key.specified &= ~AVTAB_ENABLED;
  97. else
  98. cur->node->key.specified |= AVTAB_ENABLED;
  99. }
  100. for (cur = node->false_list; cur != NULL; cur = cur->next) {
  101. /* -1 or 1 */
  102. if (new_state)
  103. cur->node->key.specified &= ~AVTAB_ENABLED;
  104. else
  105. cur->node->key.specified |= AVTAB_ENABLED;
  106. }
  107. }
  108. return 0;
  109. }
  110. int cond_policydb_init(struct policydb *p)
  111. {
  112. p->bool_val_to_struct = NULL;
  113. p->cond_list = NULL;
  114. if (avtab_init(&p->te_cond_avtab))
  115. return -1;
  116. return 0;
  117. }
  118. static void cond_av_list_destroy(struct cond_av_list *list)
  119. {
  120. struct cond_av_list *cur, *next;
  121. for (cur = list; cur != NULL; cur = next) {
  122. next = cur->next;
  123. /* the avtab_ptr_t node is destroy by the avtab */
  124. kfree(cur);
  125. }
  126. }
  127. static void cond_node_destroy(struct cond_node *node)
  128. {
  129. struct cond_expr *cur_expr, *next_expr;
  130. for (cur_expr = node->expr; cur_expr != NULL; cur_expr = next_expr) {
  131. next_expr = cur_expr->next;
  132. kfree(cur_expr);
  133. }
  134. cond_av_list_destroy(node->true_list);
  135. cond_av_list_destroy(node->false_list);
  136. kfree(node);
  137. }
  138. static void cond_list_destroy(struct cond_node *list)
  139. {
  140. struct cond_node *next, *cur;
  141. if (list == NULL)
  142. return;
  143. for (cur = list; cur != NULL; cur = next) {
  144. next = cur->next;
  145. cond_node_destroy(cur);
  146. }
  147. }
  148. void cond_policydb_destroy(struct policydb *p)
  149. {
  150. kfree(p->bool_val_to_struct);
  151. avtab_destroy(&p->te_cond_avtab);
  152. cond_list_destroy(p->cond_list);
  153. }
  154. int cond_init_bool_indexes(struct policydb *p)
  155. {
  156. kfree(p->bool_val_to_struct);
  157. p->bool_val_to_struct = (struct cond_bool_datum **)
  158. kmalloc(p->p_bools.nprim * sizeof(struct cond_bool_datum *), GFP_KERNEL);
  159. if (!p->bool_val_to_struct)
  160. return -1;
  161. return 0;
  162. }
  163. int cond_destroy_bool(void *key, void *datum, void *p)
  164. {
  165. kfree(key);
  166. kfree(datum);
  167. return 0;
  168. }
  169. int cond_index_bool(void *key, void *datum, void *datap)
  170. {
  171. struct policydb *p;
  172. struct cond_bool_datum *booldatum;
  173. booldatum = datum;
  174. p = datap;
  175. if (!booldatum->value || booldatum->value > p->p_bools.nprim)
  176. return -EINVAL;
  177. p->p_bool_val_to_name[booldatum->value - 1] = key;
  178. p->bool_val_to_struct[booldatum->value - 1] = booldatum;
  179. return 0;
  180. }
  181. static int bool_isvalid(struct cond_bool_datum *b)
  182. {
  183. if (!(b->state == 0 || b->state == 1))
  184. return 0;
  185. return 1;
  186. }
  187. int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
  188. {
  189. char *key = NULL;
  190. struct cond_bool_datum *booldatum;
  191. __le32 buf[3];
  192. u32 len;
  193. int rc;
  194. booldatum = kzalloc(sizeof(struct cond_bool_datum), GFP_KERNEL);
  195. if (!booldatum)
  196. return -1;
  197. rc = next_entry(buf, fp, sizeof buf);
  198. if (rc < 0)
  199. goto err;
  200. booldatum->value = le32_to_cpu(buf[0]);
  201. booldatum->state = le32_to_cpu(buf[1]);
  202. if (!bool_isvalid(booldatum))
  203. goto err;
  204. len = le32_to_cpu(buf[2]);
  205. key = kmalloc(len + 1, GFP_KERNEL);
  206. if (!key)
  207. goto err;
  208. rc = next_entry(key, fp, len);
  209. if (rc < 0)
  210. goto err;
  211. key[len] = 0;
  212. if (hashtab_insert(h, key, booldatum))
  213. goto err;
  214. return 0;
  215. err:
  216. cond_destroy_bool(key, booldatum, NULL);
  217. return -1;
  218. }
  219. struct cond_insertf_data {
  220. struct policydb *p;
  221. struct cond_av_list *other;
  222. struct cond_av_list *head;
  223. struct cond_av_list *tail;
  224. };
  225. static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr)
  226. {
  227. struct cond_insertf_data *data = ptr;
  228. struct policydb *p = data->p;
  229. struct cond_av_list *other = data->other, *list, *cur;
  230. struct avtab_node *node_ptr;
  231. u8 found;
  232. /*
  233. * For type rules we have to make certain there aren't any
  234. * conflicting rules by searching the te_avtab and the
  235. * cond_te_avtab.
  236. */
  237. if (k->specified & AVTAB_TYPE) {
  238. if (avtab_search(&p->te_avtab, k)) {
  239. printk(KERN_ERR "SELinux: type rule already exists outside of a conditional.\n");
  240. goto err;
  241. }
  242. /*
  243. * If we are reading the false list other will be a pointer to
  244. * the true list. We can have duplicate entries if there is only
  245. * 1 other entry and it is in our true list.
  246. *
  247. * If we are reading the true list (other == NULL) there shouldn't
  248. * be any other entries.
  249. */
  250. if (other) {
  251. node_ptr = avtab_search_node(&p->te_cond_avtab, k);
  252. if (node_ptr) {
  253. if (avtab_search_node_next(node_ptr, k->specified)) {
  254. printk(KERN_ERR "SELinux: too many conflicting type rules.\n");
  255. goto err;
  256. }
  257. found = 0;
  258. for (cur = other; cur != NULL; cur = cur->next) {
  259. if (cur->node == node_ptr) {
  260. found = 1;
  261. break;
  262. }
  263. }
  264. if (!found) {
  265. printk(KERN_ERR "SELinux: conflicting type rules.\n");
  266. goto err;
  267. }
  268. }
  269. } else {
  270. if (avtab_search(&p->te_cond_avtab, k)) {
  271. printk(KERN_ERR "SELinux: conflicting type rules when adding type rule for true.\n");
  272. goto err;
  273. }
  274. }
  275. }
  276. node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
  277. if (!node_ptr) {
  278. printk(KERN_ERR "SELinux: could not insert rule.\n");
  279. goto err;
  280. }
  281. list = kzalloc(sizeof(struct cond_av_list), GFP_KERNEL);
  282. if (!list)
  283. goto err;
  284. list->node = node_ptr;
  285. if (!data->head)
  286. data->head = list;
  287. else
  288. data->tail->next = list;
  289. data->tail = list;
  290. return 0;
  291. err:
  292. cond_av_list_destroy(data->head);
  293. data->head = NULL;
  294. return -1;
  295. }
  296. static int cond_read_av_list(struct policydb *p, void *fp, struct cond_av_list **ret_list, struct cond_av_list *other)
  297. {
  298. int i, rc;
  299. __le32 buf[1];
  300. u32 len;
  301. struct cond_insertf_data data;
  302. *ret_list = NULL;
  303. len = 0;
  304. rc = next_entry(buf, fp, sizeof(u32));
  305. if (rc < 0)
  306. return -1;
  307. len = le32_to_cpu(buf[0]);
  308. if (len == 0)
  309. return 0;
  310. data.p = p;
  311. data.other = other;
  312. data.head = NULL;
  313. data.tail = NULL;
  314. for (i = 0; i < len; i++) {
  315. rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
  316. &data);
  317. if (rc)
  318. return rc;
  319. }
  320. *ret_list = data.head;
  321. return 0;
  322. }
  323. static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
  324. {
  325. if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
  326. printk(KERN_ERR "SELinux: conditional expressions uses unknown operator.\n");
  327. return 0;
  328. }
  329. if (expr->bool > p->p_bools.nprim) {
  330. printk(KERN_ERR "SELinux: conditional expressions uses unknown bool.\n");
  331. return 0;
  332. }
  333. return 1;
  334. }
  335. static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
  336. {
  337. __le32 buf[2];
  338. u32 len, i;
  339. int rc;
  340. struct cond_expr *expr = NULL, *last = NULL;
  341. rc = next_entry(buf, fp, sizeof(u32));
  342. if (rc < 0)
  343. return -1;
  344. node->cur_state = le32_to_cpu(buf[0]);
  345. len = 0;
  346. rc = next_entry(buf, fp, sizeof(u32));
  347. if (rc < 0)
  348. return -1;
  349. /* expr */
  350. len = le32_to_cpu(buf[0]);
  351. for (i = 0; i < len; i++) {
  352. rc = next_entry(buf, fp, sizeof(u32) * 2);
  353. if (rc < 0)
  354. goto err;
  355. expr = kzalloc(sizeof(struct cond_expr), GFP_KERNEL);
  356. if (!expr)
  357. goto err;
  358. expr->expr_type = le32_to_cpu(buf[0]);
  359. expr->bool = le32_to_cpu(buf[1]);
  360. if (!expr_isvalid(p, expr)) {
  361. kfree(expr);
  362. goto err;
  363. }
  364. if (i == 0)
  365. node->expr = expr;
  366. else
  367. last->next = expr;
  368. last = expr;
  369. }
  370. if (cond_read_av_list(p, fp, &node->true_list, NULL) != 0)
  371. goto err;
  372. if (cond_read_av_list(p, fp, &node->false_list, node->true_list) != 0)
  373. goto err;
  374. return 0;
  375. err:
  376. cond_node_destroy(node);
  377. return -1;
  378. }
  379. int cond_read_list(struct policydb *p, void *fp)
  380. {
  381. struct cond_node *node, *last = NULL;
  382. __le32 buf[1];
  383. u32 i, len;
  384. int rc;
  385. rc = next_entry(buf, fp, sizeof buf);
  386. if (rc < 0)
  387. return -1;
  388. len = le32_to_cpu(buf[0]);
  389. rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
  390. if (rc)
  391. goto err;
  392. for (i = 0; i < len; i++) {
  393. node = kzalloc(sizeof(struct cond_node), GFP_KERNEL);
  394. if (!node)
  395. goto err;
  396. if (cond_read_node(p, node, fp) != 0)
  397. goto err;
  398. if (i == 0)
  399. p->cond_list = node;
  400. else
  401. last->next = node;
  402. last = node;
  403. }
  404. return 0;
  405. err:
  406. cond_list_destroy(p->cond_list);
  407. p->cond_list = NULL;
  408. return -1;
  409. }
  410. /* Determine whether additional permissions are granted by the conditional
  411. * av table, and if so, add them to the result
  412. */
  413. void cond_compute_av(struct avtab *ctab, struct avtab_key *key, struct av_decision *avd)
  414. {
  415. struct avtab_node *node;
  416. if (!ctab || !key || !avd)
  417. return;
  418. for (node = avtab_search_node(ctab, key); node != NULL;
  419. node = avtab_search_node_next(node, key->specified)) {
  420. if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) ==
  421. (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)))
  422. avd->allowed |= node->datum.data;
  423. if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) ==
  424. (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)))
  425. /* Since a '0' in an auditdeny mask represents a
  426. * permission we do NOT want to audit (dontaudit), we use
  427. * the '&' operand to ensure that all '0's in the mask
  428. * are retained (much unlike the allow and auditallow cases).
  429. */
  430. avd->auditdeny &= node->datum.data;
  431. if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
  432. (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)))
  433. avd->auditallow |= node->datum.data;
  434. }
  435. return;
  436. }