conditional.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Authors: Karl MacMillan <kmacmillan@tresys.com>
  2. * Frank Mayer <mayerf@tresys.com>
  3. *
  4. * Copyright (C) 2003 - 2005 Tresys Technology, LLC
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef _SEPOL_POLICYDB_CONDITIONAL_H_
  21. #define _SEPOL_POLICYDB_CONDITIONAL_H_
  22. #include <sepol/policydb/flask_types.h>
  23. #include <sepol/policydb/avtab.h>
  24. #include <sepol/policydb/symtab.h>
  25. #include <sepol/policydb/policydb.h>
  26. #define COND_EXPR_MAXDEPTH 10
  27. /* this is the max unique bools in a conditional expression
  28. * for which we precompute all outcomes for the expression.
  29. *
  30. * NOTE - do _NOT_ use value greater than 5 because
  31. * cond_node_t->expr_pre_comp can only hold at most 32 values
  32. */
  33. #define COND_MAX_BOOLS 5
  34. /*
  35. * A conditional expression is a list of operators and operands
  36. * in reverse polish notation.
  37. */
  38. typedef struct cond_expr {
  39. #define COND_BOOL 1 /* plain bool */
  40. #define COND_NOT 2 /* !bool */
  41. #define COND_OR 3 /* bool || bool */
  42. #define COND_AND 4 /* bool && bool */
  43. #define COND_XOR 5 /* bool ^ bool */
  44. #define COND_EQ 6 /* bool == bool */
  45. #define COND_NEQ 7 /* bool != bool */
  46. #define COND_LAST COND_NEQ
  47. uint32_t expr_type;
  48. uint32_t bool;
  49. struct cond_expr *next;
  50. } cond_expr_t;
  51. /*
  52. * Each cond_node_t contains a list of rules to be enabled/disabled
  53. * depending on the current value of the conditional expression. This
  54. * struct is for that list.
  55. */
  56. typedef struct cond_av_list {
  57. avtab_ptr_t node;
  58. struct cond_av_list *next;
  59. } cond_av_list_t;
  60. /*
  61. * A cond node represents a conditional block in a policy. It
  62. * contains a conditional expression, the current state of the expression,
  63. * two lists of rules to enable/disable depending on the value of the
  64. * expression (the true list corresponds to if and the false list corresponds
  65. * to else)..
  66. */
  67. typedef struct cond_node {
  68. int cur_state;
  69. cond_expr_t *expr;
  70. /* these true/false lists point into te_avtab when that is used */
  71. cond_av_list_t *true_list;
  72. cond_av_list_t *false_list;
  73. /* and these are using during parsing and for modules */
  74. avrule_t *avtrue_list;
  75. avrule_t *avfalse_list;
  76. /* these fields are not written to binary policy */
  77. unsigned int nbools;
  78. uint32_t bool_ids[COND_MAX_BOOLS];
  79. uint32_t expr_pre_comp;
  80. /* */
  81. struct cond_node *next;
  82. } cond_node_t;
  83. extern int cond_evaluate_expr(policydb_t * p, cond_expr_t * expr);
  84. extern cond_expr_t *cond_copy_expr(cond_expr_t * expr);
  85. extern int cond_expr_equal(cond_node_t * a, cond_node_t * b);
  86. extern int cond_normalize_expr(policydb_t * p, cond_node_t * cn);
  87. extern void cond_node_destroy(cond_node_t * node);
  88. extern void cond_expr_destroy(cond_expr_t * expr);
  89. extern cond_node_t *cond_node_find(policydb_t * p,
  90. cond_node_t * needle, cond_node_t * haystack,
  91. int *was_created);
  92. extern cond_node_t *cond_node_create(policydb_t * p, cond_node_t * node);
  93. extern cond_node_t *cond_node_search(policydb_t * p, cond_node_t * list,
  94. cond_node_t * cn);
  95. extern int evaluate_conds(policydb_t * p);
  96. extern avtab_datum_t *cond_av_list_search(avtab_key_t * key,
  97. cond_av_list_t * cond_list);
  98. extern void cond_av_list_destroy(cond_av_list_t * list);
  99. extern void cond_optimize_lists(cond_list_t * cl);
  100. extern int cond_policydb_init(policydb_t * p);
  101. extern void cond_policydb_destroy(policydb_t * p);
  102. extern void cond_list_destroy(cond_list_t * list);
  103. extern int cond_init_bool_indexes(policydb_t * p);
  104. extern int cond_destroy_bool(hashtab_key_t key, hashtab_datum_t datum, void *p);
  105. extern int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum,
  106. void *datap);
  107. extern int cond_read_bool(policydb_t * p, hashtab_t h, struct policy_file *fp);
  108. extern int cond_read_list(policydb_t * p, cond_list_t ** list, void *fp);
  109. extern void cond_compute_av(avtab_t * ctab, avtab_key_t * key,
  110. struct sepol_av_decision *avd);
  111. #endif /* _CONDITIONAL_H_ */