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