blob: 91814cb58f54aef7c13eebd802f44acf42471a12 [file] [log] [blame]
Joshua Brindle13cd4c82008-08-19 15:30:36 -04001/* 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
21#ifndef _SEPOL_POLICYDB_CONDITIONAL_H_
22#define _SEPOL_POLICYDB_CONDITIONAL_H_
23
24#include <sepol/policydb/flask_types.h>
25#include <sepol/policydb/avtab.h>
26#include <sepol/policydb/symtab.h>
27#include <sepol/policydb/policydb.h>
28
29#define COND_EXPR_MAXDEPTH 10
30
31/* this is the max unique bools in a conditional expression
32 * for which we precompute all outcomes for the expression.
33 *
34 * NOTE - do _NOT_ use value greater than 5 because
35 * cond_node_t->expr_pre_comp can only hold at most 32 values
36 */
37#define COND_MAX_BOOLS 5
38
39/*
40 * A conditional expression is a list of operators and operands
41 * in reverse polish notation.
42 */
43typedef struct cond_expr {
44#define COND_BOOL 1 /* plain bool */
45#define COND_NOT 2 /* !bool */
46#define COND_OR 3 /* bool || bool */
47#define COND_AND 4 /* bool && bool */
48#define COND_XOR 5 /* bool ^ bool */
49#define COND_EQ 6 /* bool == bool */
50#define COND_NEQ 7 /* bool != bool */
51#define COND_LAST 8
52 uint32_t expr_type;
53 uint32_t bool;
54 struct cond_expr *next;
55} cond_expr_t;
56
57/*
58 * Each cond_node_t contains a list of rules to be enabled/disabled
59 * depending on the current value of the conditional expression. This
60 * struct is for that list.
61 */
62typedef struct cond_av_list {
63 avtab_ptr_t node;
64 struct cond_av_list *next;
65} cond_av_list_t;
66
67/*
68 * A cond node represents a conditional block in a policy. It
69 * contains a conditional expression, the current state of the expression,
70 * two lists of rules to enable/disable depending on the value of the
71 * expression (the true list corresponds to if and the false list corresponds
72 * to else)..
73 */
74typedef struct cond_node {
75 int cur_state;
76 cond_expr_t *expr;
77 /* these true/false lists point into te_avtab when that is used */
78 cond_av_list_t *true_list;
79 cond_av_list_t *false_list;
80 /* and these are using during parsing and for modules */
81 avrule_t *avtrue_list;
82 avrule_t *avfalse_list;
83 /* these fields are not written to binary policy */
84 unsigned int nbools;
85 uint32_t bool_ids[COND_MAX_BOOLS];
86 uint32_t expr_pre_comp;
87 /* */
88 struct cond_node *next;
89} cond_node_t;
90
91extern int cond_evaluate_expr(policydb_t * p, cond_expr_t * expr);
92extern cond_expr_t *cond_copy_expr(cond_expr_t * expr);
93
94extern int cond_expr_equal(cond_node_t * a, cond_node_t * b);
95extern int cond_normalize_expr(policydb_t * p, cond_node_t * cn);
96extern void cond_node_destroy(cond_node_t * node);
97extern void cond_expr_destroy(cond_expr_t * expr);
98
99extern cond_node_t *cond_node_find(policydb_t * p,
100 cond_node_t * needle, cond_node_t * haystack,
101 int *was_created);
102
103extern cond_node_t *cond_node_create(policydb_t * p, cond_node_t * node);
104
105extern cond_node_t *cond_node_search(policydb_t * p, cond_node_t * list,
106 cond_node_t * cn);
107
108extern int evaluate_conds(policydb_t * p);
109
110extern avtab_datum_t *cond_av_list_search(avtab_key_t * key,
111 cond_av_list_t * cond_list);
112
113extern void cond_av_list_destroy(cond_av_list_t * list);
114
115extern void cond_optimize_lists(cond_list_t * cl);
116
117extern int cond_policydb_init(policydb_t * p);
118extern void cond_policydb_destroy(policydb_t * p);
119extern void cond_list_destroy(cond_list_t * list);
120
121extern int cond_init_bool_indexes(policydb_t * p);
122extern int cond_destroy_bool(hashtab_key_t key, hashtab_datum_t datum, void *p);
123
124extern int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum,
125 void *datap);
126
127extern int cond_read_bool(policydb_t * p, hashtab_t h, struct policy_file *fp);
128
129extern int cond_read_list(policydb_t * p, cond_list_t ** list, void *fp);
130
131extern void cond_compute_av(avtab_t * ctab, avtab_key_t * key,
132 struct sepol_av_decision *avd);
133
134#endif /* _CONDITIONAL_H_ */