conditional.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. int rc;
  112. p->bool_val_to_struct = NULL;
  113. p->cond_list = NULL;
  114. rc = avtab_init(&p->te_cond_avtab);
  115. if (rc)
  116. return rc;
  117. return 0;
  118. }
  119. static void cond_av_list_destroy(struct cond_av_list *list)
  120. {
  121. struct cond_av_list *cur, *next;
  122. for (cur = list; cur; cur = next) {
  123. next = cur->next;
  124. /* the avtab_ptr_t node is destroy by the avtab */
  125. kfree(cur);
  126. }
  127. }
  128. static void cond_node_destroy(struct cond_node *node)
  129. {
  130. struct cond_expr *cur_expr, *next_expr;
  131. for (cur_expr = node->expr; cur_expr; cur_expr = next_expr) {
  132. next_expr = cur_expr->next;
  133. kfree(cur_expr);
  134. }
  135. cond_av_list_destroy(node->true_list);
  136. cond_av_list_destroy(node->false_list);
  137. kfree(node);
  138. }
  139. static void cond_list_destroy(struct cond_node *list)
  140. {
  141. struct cond_node *next, *cur;
  142. if (list == NULL)
  143. return;
  144. for (cur = list; cur; cur = next) {
  145. next = cur->next;
  146. cond_node_destroy(cur);
  147. }
  148. }
  149. void cond_policydb_destroy(struct policydb *p)
  150. {
  151. kfree(p->bool_val_to_struct);
  152. avtab_destroy(&p->te_cond_avtab);
  153. cond_list_destroy(p->cond_list);
  154. }
  155. int cond_init_bool_indexes(struct policydb *p)
  156. {
  157. kfree(p->bool_val_to_struct);
  158. p->bool_val_to_struct =
  159. kmalloc(p->p_bools.nprim * sizeof(struct cond_bool_datum *), GFP_KERNEL);
  160. if (!p->bool_val_to_struct)
  161. return -ENOMEM;
  162. return 0;
  163. }
  164. int cond_destroy_bool(void *key, void *datum, void *p)
  165. {
  166. kfree(key);
  167. kfree(datum);
  168. return 0;
  169. }
  170. int cond_index_bool(void *key, void *datum, void *datap)
  171. {
  172. struct policydb *p;
  173. struct cond_bool_datum *booldatum;
  174. struct flex_array *fa;
  175. booldatum = datum;
  176. p = datap;
  177. if (!booldatum->value || booldatum->value > p->p_bools.nprim)
  178. return -EINVAL;
  179. fa = p->sym_val_to_name[SYM_BOOLS];
  180. if (flex_array_put_ptr(fa, booldatum->value - 1, key,
  181. GFP_KERNEL | __GFP_ZERO))
  182. BUG();
  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. __le32 buf[3];
  197. u32 len;
  198. int rc;
  199. booldatum = kzalloc(sizeof(struct cond_bool_datum), GFP_KERNEL);
  200. if (!booldatum)
  201. return -ENOMEM;
  202. rc = next_entry(buf, fp, sizeof buf);
  203. if (rc)
  204. goto err;
  205. booldatum->value = le32_to_cpu(buf[0]);
  206. booldatum->state = le32_to_cpu(buf[1]);
  207. rc = -EINVAL;
  208. if (!bool_isvalid(booldatum))
  209. goto err;
  210. len = le32_to_cpu(buf[2]);
  211. rc = -ENOMEM;
  212. key = kmalloc(len + 1, GFP_KERNEL);
  213. if (!key)
  214. goto err;
  215. rc = next_entry(key, fp, len);
  216. if (rc)
  217. goto err;
  218. key[len] = '\0';
  219. rc = hashtab_insert(h, key, booldatum);
  220. if (rc)
  221. goto err;
  222. return 0;
  223. err:
  224. cond_destroy_bool(key, booldatum, NULL);
  225. return rc;
  226. }
  227. struct cond_insertf_data {
  228. struct policydb *p;
  229. struct cond_av_list *other;
  230. struct cond_av_list *head;
  231. struct cond_av_list *tail;
  232. };
  233. static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr)
  234. {
  235. struct cond_insertf_data *data = ptr;
  236. struct policydb *p = data->p;
  237. struct cond_av_list *other = data->other, *list, *cur;
  238. struct avtab_node *node_ptr;
  239. u8 found;
  240. int rc = -EINVAL;
  241. /*
  242. * For type rules we have to make certain there aren't any
  243. * conflicting rules by searching the te_avtab and the
  244. * cond_te_avtab.
  245. */
  246. if (k->specified & AVTAB_TYPE) {
  247. if (avtab_search(&p->te_avtab, k)) {
  248. printk(KERN_ERR "SELinux: type rule already exists outside of a conditional.\n");
  249. goto err;
  250. }
  251. /*
  252. * If we are reading the false list other will be a pointer to
  253. * the true list. We can have duplicate entries if there is only
  254. * 1 other entry and it is in our true list.
  255. *
  256. * If we are reading the true list (other == NULL) there shouldn't
  257. * be any other entries.
  258. */
  259. if (other) {
  260. node_ptr = avtab_search_node(&p->te_cond_avtab, k);
  261. if (node_ptr) {
  262. if (avtab_search_node_next(node_ptr, k->specified)) {
  263. printk(KERN_ERR "SELinux: too many conflicting type rules.\n");
  264. goto err;
  265. }
  266. found = 0;
  267. for (cur = other; cur; cur = cur->next) {
  268. if (cur->node == node_ptr) {
  269. found = 1;
  270. break;
  271. }
  272. }
  273. if (!found) {
  274. printk(KERN_ERR "SELinux: conflicting type rules.\n");
  275. goto err;
  276. }
  277. }
  278. } else {
  279. if (avtab_search(&p->te_cond_avtab, k)) {
  280. printk(KERN_ERR "SELinux: conflicting type rules when adding type rule for true.\n");
  281. goto err;
  282. }
  283. }
  284. }
  285. node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
  286. if (!node_ptr) {
  287. printk(KERN_ERR "SELinux: could not insert rule.\n");
  288. rc = -ENOMEM;
  289. goto err;
  290. }
  291. list = kzalloc(sizeof(struct cond_av_list), GFP_KERNEL);
  292. if (!list) {
  293. rc = -ENOMEM;
  294. goto err;
  295. }
  296. list->node = node_ptr;
  297. if (!data->head)
  298. data->head = list;
  299. else
  300. data->tail->next = list;
  301. data->tail = list;
  302. return 0;
  303. err:
  304. cond_av_list_destroy(data->head);
  305. data->head = NULL;
  306. return rc;
  307. }
  308. static int cond_read_av_list(struct policydb *p, void *fp, struct cond_av_list **ret_list, struct cond_av_list *other)
  309. {
  310. int i, rc;
  311. __le32 buf[1];
  312. u32 len;
  313. struct cond_insertf_data data;
  314. *ret_list = NULL;
  315. len = 0;
  316. rc = next_entry(buf, fp, sizeof(u32));
  317. if (rc)
  318. return rc;
  319. len = le32_to_cpu(buf[0]);
  320. if (len == 0)
  321. return 0;
  322. data.p = p;
  323. data.other = other;
  324. data.head = NULL;
  325. data.tail = NULL;
  326. for (i = 0; i < len; i++) {
  327. rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
  328. &data);
  329. if (rc)
  330. return rc;
  331. }
  332. *ret_list = data.head;
  333. return 0;
  334. }
  335. static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
  336. {
  337. if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
  338. printk(KERN_ERR "SELinux: conditional expressions uses unknown operator.\n");
  339. return 0;
  340. }
  341. if (expr->bool > p->p_bools.nprim) {
  342. printk(KERN_ERR "SELinux: conditional expressions uses unknown bool.\n");
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
  348. {
  349. __le32 buf[2];
  350. u32 len, i;
  351. int rc;
  352. struct cond_expr *expr = NULL, *last = NULL;
  353. rc = next_entry(buf, fp, sizeof(u32) * 2);
  354. if (rc)
  355. goto err;
  356. node->cur_state = le32_to_cpu(buf[0]);
  357. /* expr */
  358. len = le32_to_cpu(buf[1]);
  359. for (i = 0; i < len; i++) {
  360. rc = next_entry(buf, fp, sizeof(u32) * 2);
  361. if (rc)
  362. goto err;
  363. rc = -ENOMEM;
  364. expr = kzalloc(sizeof(struct cond_expr), GFP_KERNEL);
  365. if (!expr)
  366. goto err;
  367. expr->expr_type = le32_to_cpu(buf[0]);
  368. expr->bool = le32_to_cpu(buf[1]);
  369. if (!expr_isvalid(p, expr)) {
  370. rc = -EINVAL;
  371. kfree(expr);
  372. goto err;
  373. }
  374. if (i == 0)
  375. node->expr = expr;
  376. else
  377. last->next = expr;
  378. last = expr;
  379. }
  380. rc = cond_read_av_list(p, fp, &node->true_list, NULL);
  381. if (rc)
  382. goto err;
  383. rc = cond_read_av_list(p, fp, &node->false_list, node->true_list);
  384. if (rc)
  385. goto err;
  386. return 0;
  387. err:
  388. cond_node_destroy(node);
  389. return rc;
  390. }
  391. int cond_read_list(struct policydb *p, void *fp)
  392. {
  393. struct cond_node *node, *last = NULL;
  394. __le32 buf[1];
  395. u32 i, len;
  396. int rc;
  397. rc = next_entry(buf, fp, sizeof buf);
  398. if (rc)
  399. return rc;
  400. len = le32_to_cpu(buf[0]);
  401. rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
  402. if (rc)
  403. goto err;
  404. for (i = 0; i < len; i++) {
  405. rc = -ENOMEM;
  406. node = kzalloc(sizeof(struct cond_node), GFP_KERNEL);
  407. if (!node)
  408. goto err;
  409. rc = cond_read_node(p, node, fp);
  410. if (rc)
  411. goto err;
  412. if (i == 0)
  413. p->cond_list = node;
  414. else
  415. last->next = node;
  416. last = node;
  417. }
  418. return 0;
  419. err:
  420. cond_list_destroy(p->cond_list);
  421. p->cond_list = NULL;
  422. return rc;
  423. }
  424. int cond_write_bool(void *vkey, void *datum, void *ptr)
  425. {
  426. char *key = vkey;
  427. struct cond_bool_datum *booldatum = datum;
  428. struct policy_data *pd = ptr;
  429. void *fp = pd->fp;
  430. __le32 buf[3];
  431. u32 len;
  432. int rc;
  433. len = strlen(key);
  434. buf[0] = cpu_to_le32(booldatum->value);
  435. buf[1] = cpu_to_le32(booldatum->state);
  436. buf[2] = cpu_to_le32(len);
  437. rc = put_entry(buf, sizeof(u32), 3, fp);
  438. if (rc)
  439. return rc;
  440. rc = put_entry(key, 1, len, fp);
  441. if (rc)
  442. return rc;
  443. return 0;
  444. }
  445. /*
  446. * cond_write_cond_av_list doesn't write out the av_list nodes.
  447. * Instead it writes out the key/value pairs from the avtab. This
  448. * is necessary because there is no way to uniquely identifying rules
  449. * in the avtab so it is not possible to associate individual rules
  450. * in the avtab with a conditional without saving them as part of
  451. * the conditional. This means that the avtab with the conditional
  452. * rules will not be saved but will be rebuilt on policy load.
  453. */
  454. static int cond_write_av_list(struct policydb *p,
  455. struct cond_av_list *list, struct policy_file *fp)
  456. {
  457. __le32 buf[1];
  458. struct cond_av_list *cur_list;
  459. u32 len;
  460. int rc;
  461. len = 0;
  462. for (cur_list = list; cur_list != NULL; cur_list = cur_list->next)
  463. len++;
  464. buf[0] = cpu_to_le32(len);
  465. rc = put_entry(buf, sizeof(u32), 1, fp);
  466. if (rc)
  467. return rc;
  468. if (len == 0)
  469. return 0;
  470. for (cur_list = list; cur_list != NULL; cur_list = cur_list->next) {
  471. rc = avtab_write_item(p, cur_list->node, fp);
  472. if (rc)
  473. return rc;
  474. }
  475. return 0;
  476. }
  477. static int cond_write_node(struct policydb *p, struct cond_node *node,
  478. struct policy_file *fp)
  479. {
  480. struct cond_expr *cur_expr;
  481. __le32 buf[2];
  482. int rc;
  483. u32 len = 0;
  484. buf[0] = cpu_to_le32(node->cur_state);
  485. rc = put_entry(buf, sizeof(u32), 1, fp);
  486. if (rc)
  487. return rc;
  488. for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next)
  489. len++;
  490. buf[0] = cpu_to_le32(len);
  491. rc = put_entry(buf, sizeof(u32), 1, fp);
  492. if (rc)
  493. return rc;
  494. for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next) {
  495. buf[0] = cpu_to_le32(cur_expr->expr_type);
  496. buf[1] = cpu_to_le32(cur_expr->bool);
  497. rc = put_entry(buf, sizeof(u32), 2, fp);
  498. if (rc)
  499. return rc;
  500. }
  501. rc = cond_write_av_list(p, node->true_list, fp);
  502. if (rc)
  503. return rc;
  504. rc = cond_write_av_list(p, node->false_list, fp);
  505. if (rc)
  506. return rc;
  507. return 0;
  508. }
  509. int cond_write_list(struct policydb *p, struct cond_node *list, void *fp)
  510. {
  511. struct cond_node *cur;
  512. u32 len;
  513. __le32 buf[1];
  514. int rc;
  515. len = 0;
  516. for (cur = list; cur != NULL; cur = cur->next)
  517. len++;
  518. buf[0] = cpu_to_le32(len);
  519. rc = put_entry(buf, sizeof(u32), 1, fp);
  520. if (rc)
  521. return rc;
  522. for (cur = list; cur != NULL; cur = cur->next) {
  523. rc = cond_write_node(p, cur, fp);
  524. if (rc)
  525. return rc;
  526. }
  527. return 0;
  528. }
  529. /* Determine whether additional permissions are granted by the conditional
  530. * av table, and if so, add them to the result
  531. */
  532. void cond_compute_av(struct avtab *ctab, struct avtab_key *key, struct av_decision *avd)
  533. {
  534. struct avtab_node *node;
  535. if (!ctab || !key || !avd)
  536. return;
  537. for (node = avtab_search_node(ctab, key); node;
  538. node = avtab_search_node_next(node, key->specified)) {
  539. if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) ==
  540. (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)))
  541. avd->allowed |= node->datum.data;
  542. if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) ==
  543. (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)))
  544. /* Since a '0' in an auditdeny mask represents a
  545. * permission we do NOT want to audit (dontaudit), we use
  546. * the '&' operand to ensure that all '0's in the mask
  547. * are retained (much unlike the allow and auditallow cases).
  548. */
  549. avd->auditdeny &= node->datum.data;
  550. if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
  551. (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)))
  552. avd->auditallow |= node->datum.data;
  553. }
  554. return;
  555. }