conditional.c 11 KB

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