conditional.c 14 KB

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