File: cond.c

package info (click to toggle)
setools 2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,112 kB
  • ctags: 10,502
  • sloc: ansic: 76,267; tcl: 27,222; yacc: 2,943; makefile: 993; sh: 504; lex: 244
file content (382 lines) | stat: -rw-r--r-- 8,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
 /* Copyright (C) 2003 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/* 
 * Author: mayerf@tresys.com
 */

/* cond.c */

/* Support functions for conditional policy language extensions 
 * some of this is borrowed directly from our work in conditional.c
 * for the checkpolicy extensions */


#include "util.h"
#include "cond.h"
#include "policy.h"
#include <assert.h>
#include <string.h>
#include <sys/capability.h>

int cond_free_expr(cond_expr_t *expr)
{
	cond_expr_t *cur, *next;

	for (cur = expr; cur != NULL; cur = next) {
		next = cur->next;
		free(cur);
	}
	return 0;
}

int cond_free_rules_list(cond_rule_list_t *rl)
{
	if(rl == NULL)
		return 0;
	if(rl->av_access != NULL)
		free(rl->av_access);
	if(rl->av_audit != NULL)
		free(rl->av_audit);
	if(rl->te_trans != NULL)
		free(rl->te_trans);
	free(rl);
	return 0;
}

int cond_free_expr_item(cond_expr_item_t *c)
{
	if(c == NULL)
		return 0;
	cond_free_expr(c->expr);
	cond_free_rules_list(c->true_list);
	cond_free_rules_list(c->false_list);
	return 0;
}

int cond_free_bool(cond_bool_t *b)
{
	if(b != NULL) {
		if(b->name != NULL)
			free(b->name);
	}
	return 0;
}

/*
 * cond_evaluate_expr evaluates a conditional expr
 * in reverse polish notation. It returns true (1), false (0),
 * or undefined (-1). Undefined occurs when the expression
 * exceeds the stack depth of COND_EXPR_MAXDEPTH.
 */

static int cond_evaluate_expr_helper(cond_expr_t *expr, bool_t *vals)
{

	cond_expr_t *cur;
	int s[COND_EXPR_MAXDEPTH];
	int sp = -1;

	for (cur = expr; cur != NULL; cur = cur->next) {
		switch (cur->expr_type) {
		case COND_BOOL:
			if (sp == (COND_EXPR_MAXDEPTH - 1))
				return -1;
			sp++;
			s[sp] = vals[cur->bool];
			break;
		case COND_NOT:
			if (sp < 0)
				return -1;
			s[sp] = !s[sp];
			break;
		case COND_OR:
			if (sp < 1)
				return -1;
			sp--;
			s[sp] |= s[sp + 1];
			break;
		case COND_AND:
			if (sp < 1)
				return -1;
			sp--;
			s[sp] &= s[sp + 1];
			break;
		case COND_XOR:
			if (sp < 1)
				return -1;
			sp--;
			s[sp] ^= s[sp + 1];
			break;
		case COND_EQ:
			if (sp < 1)
				return -1;
			sp--;
			s[sp] = (s[sp] == s[sp + 1]);
			break;
		case COND_NEQ:
			if (sp < 1)
				return -1;
			sp--;
			s[sp] = (s[sp] != s[sp + 1]);
			break;
		default:
			return -1;
		}
	}
	return s[0];
}

int cond_evaluate_expr(cond_expr_t *expr, policy_t *policy)
{
	bool_t *vals;
	int i, rt; 
	
	if(expr == NULL || policy == NULL)
		return -1;
	
	vals = (bool_t *)malloc(sizeof(bool_t) * policy->num_cond_bools);
	if(vals == NULL ) {
		fprintf(stderr, "out of memory\n");
		return -1;
	}
	for(i = 0; i < policy->num_cond_bools; i++) {
		vals[i] = policy->cond_bools[i].state;
	}
	rt = cond_evaluate_expr_helper(expr, vals);
	free(vals);
	return rt;
}

/* Compare 2 conditional expressions for equality. This is a very basic compare and
 * the expressions need to be exactly the same in order to match (including order).
 *
 *
 * RETURNS:
 *	TRUE or FALSE if the conditional expressions match or not.
 */
bool_t cond_exprs_equal(cond_expr_t *a, cond_expr_t *b)
{
	cond_expr_t *cur_a, *cur_b;
	
	if (!a || !b)
		return FALSE;
	
	cur_a = a;
	cur_b = b;
	
	while (1) {
		if (!cur_a && !cur_b)
			return TRUE;
		if (!cur_a || !cur_b)
			return FALSE;
		if (cur_a->expr_type != cur_b->expr_type)
			return FALSE;
		if (cur_a->expr_type == COND_BOOL)
			if (cur_a->bool != cur_b->bool)
				return FALSE;
		cur_a = cur_a->next;
		cur_b = cur_b->next;
	}
	/* can't be reached */
	return TRUE;
}

static int count_and_get_unique_bools(cond_expr_t *e, int **bools)
{
	int num = 0, rt;
	cond_expr_t *t;
	
	if(bools == NULL)
		return -1;
	*bools = NULL;
	
	for(t = e; t != NULL; t = t->next) {
		if(t->expr_type == COND_BOOL) {
			rt = find_int_in_array(t->bool, *bools, num);
			if(rt < 0) { /* means this is a new, unique bool */
				rt = add_i_to_a(t->bool, &num, bools);
				if(rt < 0)
					return -1;
			}
		}
	}
	return num;
}



/* assumes vals is of size sz (which is num of bools in policy; alloc's and returns pre computed values.
 * num is number of unique bools in expression e.  Returns in comp the pre comp and return the sz in bytes
 * of comp.  Returns -1 for error. */
static int pre_comp_helper(bool_t *vals, int sz, int *bools, int num, cond_expr_t *e, unsigned char **comp)
{
	int num_reqd_bytes, i, ans;
	uint32_t test, lnum;  
	
	if(vals == NULL || e == NULL || bools == NULL || comp == NULL)
		return -1;
		
	assert(num >= 0 && num <= COND_MAX_BOOLS);
	assert(sz > 0);
	num_reqd_bytes =  (0x1 << num)/8; 
	/* always need at least 1 byte */
	if (num_reqd_bytes == 0) num_reqd_bytes++;
	
	*comp = (unsigned char *) malloc(sizeof(unsigned char) * num_reqd_bytes); 
	if(*comp == NULL ) {
		fprintf(stderr, "out of memory\n");
		return -1;
	}
	memset(*comp, 0, sizeof(unsigned char) * num_reqd_bytes);
	
	lnum = num;
	for(test = 0x0; test < (0x1 << lnum); test++) {
		/* set the boolean vals */
		for(i = 0; i < num; i++) {
			vals[bools[i]] = (test & (0x1 << i) ) ? TRUE : FALSE;
		}
		ans = cond_evaluate_expr_helper(e, vals);
		if(ans < 0) {
			free(*comp);
			return -1;
		}
		if(ans) 
			(*comp)[test/8] |= (0x1 << (test & 0x7));
	}
	return num_reqd_bytes;
}


static bool_t is_inverse_comp(int sz, unsigned char *a, unsigned char *b)
{
	int i;
	assert(a != NULL && b!= NULL);
	
	for(i = 0; i < sz; i++) {
		if(a[i] & b[i])
			return FALSE;
	}
	return TRUE;
}


/* This function assumes taht both expressions have the exact same unique booleans, and that
 * num indicates how many unique bools.  Inverse indicates that the exprs are the inverse of each
 * other in which case just logically switch the TRUE and FALSE lists for the expr  */
static bool_t semantic_equal_helper(int num, int *abools, int *bbools, cond_expr_t *a, cond_expr_t *b, policy_t *p, bool_t *inverse)
{
	bool_t *vals = NULL, ans;	
	int sza, szb, rt;
	unsigned char *a_comp = NULL, *b_comp = NULL; /* buffer for pre-computed values */

	if(num <= 0 || a == NULL || b == NULL || p == NULL || abools == NULL || bbools == NULL || inverse == NULL) {
		assert(0);
		return FALSE;
	}
	*inverse = FALSE;

	/* allocated boolean value array for testing; size of all booleans in policy */
	assert(p->num_cond_bools > 0);
	vals = (bool_t *)malloc(sizeof(bool_t) * p->num_cond_bools);
	if(vals == NULL) {
		fprintf(stderr, "out of memory\n");
		return FALSE;
	}
	memset(vals, 0, sizeof(bool_t) * p->num_cond_bools);
	
	sza = pre_comp_helper(vals,  p->num_cond_bools, abools, num, a, &a_comp);
	if(sza < 1) {
		free(vals);
		assert(0);
		return FALSE;
	}
	szb = pre_comp_helper(vals,  p->num_cond_bools, bbools, num, b, &b_comp);
	if(szb < 1) {
		free(vals);
		free(a_comp);
		assert(0);
		return FALSE;
	}
	free(vals);

	assert(a_comp != NULL);
	assert(b_comp != NULL);
	assert(sza == szb);
	rt = memcmp(a_comp, b_comp, sza);
	if(rt == 0)
		ans = TRUE;
	else {
		if(is_inverse_comp(sza, a_comp, b_comp)) {
			*inverse = TRUE; /* this is the inverse expr */
			ans = TRUE;
		}
		else {
			ans = FALSE; /* not inverse either */
		}
	}

	free(a_comp);
	free(b_comp);
	return ans;
}



/* A semantic comaprison, that will determine if two expresions are equal under
 * most cases */
bool_t cond_exprs_semantic_equal(cond_expr_t *a, cond_expr_t *b, struct policy *p, bool_t *inverse)
{
	int i, rt, anum, bnum, *abools = NULL, *bbools = NULL;
	bool_t ans;

	if(a == NULL || b == NULL || p == NULL || inverse == NULL) {
		assert(0);
		return FALSE;
	}
	*inverse = FALSE;
	
	anum = count_and_get_unique_bools(a, &abools);
	bnum = count_and_get_unique_bools(b, &bbools);
	if(anum < 0 || bnum < 0){
		assert(0);
		ans = FALSE;
		goto return_ans;
	}
	assert(abools != NULL);
	assert(bbools != NULL);
		
	/* first check the # bools heuristic */
	if(anum != bnum) {
		ans = FALSE;
		goto return_ans;
	}
	/* then attempt to check for EXACT match */
	if(cond_exprs_equal(a, b)){
		ans = TRUE;
		goto return_ans;
	}
	/* see if the exact same booleans are used; if not they can't be semantically equal */
	for(i = 0; i < anum; i++) {
		rt = find_int_in_array(abools[i], bbools, bnum);
		if(rt < 0) {
			ans = FALSE;
			goto return_ans;
		}
	}
	/* otherwise go through the brute force semantic check */
	if(p == NULL) {
		assert(0);
		ans = FALSE;
		goto  return_ans;
	}
	ans = semantic_equal_helper(anum, abools, bbools, a, b, p, inverse);
return_ans:
	if(abools != NULL) free(abools);
	if(bbools != NULL) free(bbools);
	return ans;
}