conditional.c 14.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/* Authors: Karl MacMillan <kmacmillan@tresys.com>
2
 *	    Frank Mayer <mayerf@tresys.com>
L
Linus Torvalds 已提交
3 4 5
 *
 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
 *	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
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17
 *	the Free Software Foundation, version 2.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/slab.h>

#include "security.h"
#include "conditional.h"
18
#include "services.h"
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32

/*
 * 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(struct policydb *p, struct cond_expr *expr)
{

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

33
	for (cur = expr; cur; cur = cur->next) {
L
Linus Torvalds 已提交
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
		switch (cur->expr_type) {
		case COND_BOOL:
			if (sp == (COND_EXPR_MAXDEPTH - 1))
				return -1;
			sp++;
			s[sp] = p->bool_val_to_struct[cur->bool - 1]->state;
			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];
}

/*
 * evaluate_cond_node evaluates the conditional stored in
 * a struct cond_node and if the result is different than the
 * current state of the node it sets the rules in the true/false
 * list appropriately. If the result of the expression is undefined
 * all of the rules are disabled for safety.
 */
int evaluate_cond_node(struct policydb *p, struct cond_node *node)
{
	int new_state;
93
	struct cond_av_list *cur;
L
Linus Torvalds 已提交
94 95 96 97 98

	new_state = cond_evaluate_expr(p, node->expr);
	if (new_state != node->cur_state) {
		node->cur_state = new_state;
		if (new_state == -1)
J
James Morris 已提交
99
			printk(KERN_ERR "SELinux: expression result was undefined - disabling all rules.\n");
L
Linus Torvalds 已提交
100
		/* turn the rules on or off */
101
		for (cur = node->true_list; cur; cur = cur->next) {
102
			if (new_state <= 0)
103
				cur->node->key.specified &= ~AVTAB_ENABLED;
104
			else
105
				cur->node->key.specified |= AVTAB_ENABLED;
L
Linus Torvalds 已提交
106 107
		}

108
		for (cur = node->false_list; cur; cur = cur->next) {
L
Linus Torvalds 已提交
109
			/* -1 or 1 */
110
			if (new_state)
111
				cur->node->key.specified &= ~AVTAB_ENABLED;
112
			else
113
				cur->node->key.specified |= AVTAB_ENABLED;
L
Linus Torvalds 已提交
114 115 116 117 118 119 120
		}
	}
	return 0;
}

int cond_policydb_init(struct policydb *p)
{
121 122
	int rc;

L
Linus Torvalds 已提交
123 124
	p->bool_val_to_struct = NULL;
	p->cond_list = NULL;
125 126 127 128

	rc = avtab_init(&p->te_cond_avtab);
	if (rc)
		return rc;
L
Linus Torvalds 已提交
129 130 131 132 133 134 135

	return 0;
}

static void cond_av_list_destroy(struct cond_av_list *list)
{
	struct cond_av_list *cur, *next;
136
	for (cur = list; cur; cur = next) {
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146
		next = cur->next;
		/* the avtab_ptr_t node is destroy by the avtab */
		kfree(cur);
	}
}

static void cond_node_destroy(struct cond_node *node)
{
	struct cond_expr *cur_expr, *next_expr;

147
	for (cur_expr = node->expr; cur_expr; cur_expr = next_expr) {
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		next_expr = cur_expr->next;
		kfree(cur_expr);
	}
	cond_av_list_destroy(node->true_list);
	cond_av_list_destroy(node->false_list);
	kfree(node);
}

static void cond_list_destroy(struct cond_node *list)
{
	struct cond_node *next, *cur;

	if (list == NULL)
		return;

163
	for (cur = list; cur; cur = next) {
L
Linus Torvalds 已提交
164 165 166 167 168 169 170
		next = cur->next;
		cond_node_destroy(cur);
	}
}

void cond_policydb_destroy(struct policydb *p)
{
J
Jesper Juhl 已提交
171
	kfree(p->bool_val_to_struct);
L
Linus Torvalds 已提交
172 173 174 175 176 177
	avtab_destroy(&p->te_cond_avtab);
	cond_list_destroy(p->cond_list);
}

int cond_init_bool_indexes(struct policydb *p)
{
J
Jesper Juhl 已提交
178
	kfree(p->bool_val_to_struct);
179
	p->bool_val_to_struct =
180
		kmalloc(p->p_bools.nprim * sizeof(struct cond_bool_datum *), GFP_KERNEL);
L
Linus Torvalds 已提交
181
	if (!p->bool_val_to_struct)
182
		return -ENOMEM;
L
Linus Torvalds 已提交
183 184 185 186 187
	return 0;
}

int cond_destroy_bool(void *key, void *datum, void *p)
{
J
Jesper Juhl 已提交
188
	kfree(key);
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196
	kfree(datum);
	return 0;
}

int cond_index_bool(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct cond_bool_datum *booldatum;
197
	struct flex_array *fa;
L
Linus Torvalds 已提交
198 199 200 201 202 203 204

	booldatum = datum;
	p = datap;

	if (!booldatum->value || booldatum->value > p->p_bools.nprim)
		return -EINVAL;

205 206 207 208
	fa = p->sym_val_to_name[SYM_BOOLS];
	if (flex_array_put_ptr(fa, booldatum->value - 1, key,
			       GFP_KERNEL | __GFP_ZERO))
		BUG();
209
	p->bool_val_to_struct[booldatum->value - 1] = booldatum;
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

	return 0;
}

static int bool_isvalid(struct cond_bool_datum *b)
{
	if (!(b->state == 0 || b->state == 1))
		return 0;
	return 1;
}

int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct cond_bool_datum *booldatum;
225 226
	__le32 buf[3];
	u32 len;
L
Linus Torvalds 已提交
227 228
	int rc;

J
James Morris 已提交
229
	booldatum = kzalloc(sizeof(struct cond_bool_datum), GFP_KERNEL);
L
Linus Torvalds 已提交
230
	if (!booldatum)
231
		return -ENOMEM;
L
Linus Torvalds 已提交
232 233

	rc = next_entry(buf, fp, sizeof buf);
234
	if (rc)
L
Linus Torvalds 已提交
235 236 237 238 239
		goto err;

	booldatum->value = le32_to_cpu(buf[0]);
	booldatum->state = le32_to_cpu(buf[1]);

240
	rc = -EINVAL;
L
Linus Torvalds 已提交
241 242 243 244 245
	if (!bool_isvalid(booldatum))
		goto err;

	len = le32_to_cpu(buf[2]);

246
	rc = -ENOMEM;
L
Linus Torvalds 已提交
247 248 249 250
	key = kmalloc(len + 1, GFP_KERNEL);
	if (!key)
		goto err;
	rc = next_entry(key, fp, len);
251
	if (rc)
L
Linus Torvalds 已提交
252
		goto err;
253
	key[len] = '\0';
254 255
	rc = hashtab_insert(h, key, booldatum);
	if (rc)
L
Linus Torvalds 已提交
256 257 258 259 260
		goto err;

	return 0;
err:
	cond_destroy_bool(key, booldatum, NULL);
261
	return rc;
L
Linus Torvalds 已提交
262 263
}

264
struct cond_insertf_data {
265 266 267 268 269 270 271 272 273 274 275
	struct policydb *p;
	struct cond_av_list *other;
	struct cond_av_list *head;
	struct cond_av_list *tail;
};

static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr)
{
	struct cond_insertf_data *data = ptr;
	struct policydb *p = data->p;
	struct cond_av_list *other = data->other, *list, *cur;
L
Linus Torvalds 已提交
276 277
	struct avtab_node *node_ptr;
	u8 found;
278
	int rc = -EINVAL;
L
Linus Torvalds 已提交
279

280 281 282 283 284 285 286
	/*
	 * For type rules we have to make certain there aren't any
	 * conflicting rules by searching the te_avtab and the
	 * cond_te_avtab.
	 */
	if (k->specified & AVTAB_TYPE) {
		if (avtab_search(&p->te_avtab, k)) {
E
Eric Paris 已提交
287
			printk(KERN_ERR "SELinux: type rule already exists outside of a conditional.\n");
L
Linus Torvalds 已提交
288
			goto err;
289
		}
L
Linus Torvalds 已提交
290
		/*
291 292 293 294 295 296
		 * If we are reading the false list other will be a pointer to
		 * the true list. We can have duplicate entries if there is only
		 * 1 other entry and it is in our true list.
		 *
		 * If we are reading the true list (other == NULL) there shouldn't
		 * be any other entries.
L
Linus Torvalds 已提交
297
		 */
298 299 300 301
		if (other) {
			node_ptr = avtab_search_node(&p->te_cond_avtab, k);
			if (node_ptr) {
				if (avtab_search_node_next(node_ptr, k->specified)) {
E
Eric Paris 已提交
302
					printk(KERN_ERR "SELinux: too many conflicting type rules.\n");
303 304 305
					goto err;
				}
				found = 0;
306
				for (cur = other; cur; cur = cur->next) {
307 308 309
					if (cur->node == node_ptr) {
						found = 1;
						break;
L
Linus Torvalds 已提交
310 311
					}
				}
312
				if (!found) {
E
Eric Paris 已提交
313
					printk(KERN_ERR "SELinux: conflicting type rules.\n");
L
Linus Torvalds 已提交
314 315 316
					goto err;
				}
			}
317 318
		} else {
			if (avtab_search(&p->te_cond_avtab, k)) {
E
Eric Paris 已提交
319
				printk(KERN_ERR "SELinux: conflicting type rules when adding type rule for true.\n");
320 321
				goto err;
			}
L
Linus Torvalds 已提交
322
		}
323
	}
L
Linus Torvalds 已提交
324

325 326
	node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
	if (!node_ptr) {
E
Eric Paris 已提交
327
		printk(KERN_ERR "SELinux: could not insert rule.\n");
328
		rc = -ENOMEM;
329
		goto err;
L
Linus Torvalds 已提交
330 331
	}

J
James Morris 已提交
332
	list = kzalloc(sizeof(struct cond_av_list), GFP_KERNEL);
333 334
	if (!list) {
		rc = -ENOMEM;
335
		goto err;
336
	}
337 338 339 340 341 342 343

	list->node = node_ptr;
	if (!data->head)
		data->head = list;
	else
		data->tail->next = list;
	data->tail = list;
L
Linus Torvalds 已提交
344
	return 0;
345

L
Linus Torvalds 已提交
346
err:
347 348
	cond_av_list_destroy(data->head);
	data->head = NULL;
349
	return rc;
L
Linus Torvalds 已提交
350 351
}

352 353 354
static int cond_read_av_list(struct policydb *p, void *fp, struct cond_av_list **ret_list, struct cond_av_list *other)
{
	int i, rc;
355 356
	__le32 buf[1];
	u32 len;
357 358 359 360 361 362
	struct cond_insertf_data data;

	*ret_list = NULL;

	len = 0;
	rc = next_entry(buf, fp, sizeof(u32));
363 364
	if (rc)
		return rc;
365 366

	len = le32_to_cpu(buf[0]);
367
	if (len == 0)
368 369 370 371 372 373 374
		return 0;

	data.p = p;
	data.other = other;
	data.head = NULL;
	data.tail = NULL;
	for (i = 0; i < len; i++) {
375 376
		rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
				     &data);
377 378 379 380 381 382 383 384
		if (rc)
			return rc;
	}

	*ret_list = data.head;
	return 0;
}

L
Linus Torvalds 已提交
385 386 387
static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
{
	if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
E
Eric Paris 已提交
388
		printk(KERN_ERR "SELinux: conditional expressions uses unknown operator.\n");
L
Linus Torvalds 已提交
389 390 391 392
		return 0;
	}

	if (expr->bool > p->p_bools.nprim) {
E
Eric Paris 已提交
393
		printk(KERN_ERR "SELinux: conditional expressions uses unknown bool.\n");
L
Linus Torvalds 已提交
394 395 396 397 398 399 400
		return 0;
	}
	return 1;
}

static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
{
401 402
	__le32 buf[2];
	u32 len, i;
L
Linus Torvalds 已提交
403 404 405
	int rc;
	struct cond_expr *expr = NULL, *last = NULL;

406
	rc = next_entry(buf, fp, sizeof(u32) * 2);
407
	if (rc)
408
		goto err;
L
Linus Torvalds 已提交
409 410 411 412

	node->cur_state = le32_to_cpu(buf[0]);

	/* expr */
413
	len = le32_to_cpu(buf[1]);
L
Linus Torvalds 已提交
414

415
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
416
		rc = next_entry(buf, fp, sizeof(u32) * 2);
417
		if (rc)
L
Linus Torvalds 已提交
418 419
			goto err;

420
		rc = -ENOMEM;
J
James Morris 已提交
421
		expr = kzalloc(sizeof(struct cond_expr), GFP_KERNEL);
422
		if (!expr)
L
Linus Torvalds 已提交
423 424 425 426 427 428
			goto err;

		expr->expr_type = le32_to_cpu(buf[0]);
		expr->bool = le32_to_cpu(buf[1]);

		if (!expr_isvalid(p, expr)) {
429
			rc = -EINVAL;
L
Linus Torvalds 已提交
430 431 432 433
			kfree(expr);
			goto err;
		}

434
		if (i == 0)
L
Linus Torvalds 已提交
435
			node->expr = expr;
436
		else
L
Linus Torvalds 已提交
437 438 439 440
			last->next = expr;
		last = expr;
	}

441 442
	rc = cond_read_av_list(p, fp, &node->true_list, NULL);
	if (rc)
L
Linus Torvalds 已提交
443
		goto err;
444 445
	rc = cond_read_av_list(p, fp, &node->false_list, node->true_list);
	if (rc)
L
Linus Torvalds 已提交
446 447 448 449
		goto err;
	return 0;
err:
	cond_node_destroy(node);
450
	return rc;
L
Linus Torvalds 已提交
451 452 453 454 455
}

int cond_read_list(struct policydb *p, void *fp)
{
	struct cond_node *node, *last = NULL;
456 457
	__le32 buf[1];
	u32 i, len;
L
Linus Torvalds 已提交
458 459 460
	int rc;

	rc = next_entry(buf, fp, sizeof buf);
461 462
	if (rc)
		return rc;
L
Linus Torvalds 已提交
463 464 465

	len = le32_to_cpu(buf[0]);

466 467 468 469
	rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
	if (rc)
		goto err;

L
Linus Torvalds 已提交
470
	for (i = 0; i < len; i++) {
471
		rc = -ENOMEM;
J
James Morris 已提交
472
		node = kzalloc(sizeof(struct cond_node), GFP_KERNEL);
L
Linus Torvalds 已提交
473 474 475
		if (!node)
			goto err;

476 477
		rc = cond_read_node(p, node, fp);
		if (rc)
L
Linus Torvalds 已提交
478 479
			goto err;

480
		if (i == 0)
L
Linus Torvalds 已提交
481
			p->cond_list = node;
482
		else
L
Linus Torvalds 已提交
483 484 485 486 487 488
			last->next = node;
		last = node;
	}
	return 0;
err:
	cond_list_destroy(p->cond_list);
489
	p->cond_list = NULL;
490
	return rc;
L
Linus Torvalds 已提交
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
int cond_write_bool(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct cond_bool_datum *booldatum = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	__le32 buf[3];
	u32 len;
	int rc;

	len = strlen(key);
	buf[0] = cpu_to_le32(booldatum->value);
	buf[1] = cpu_to_le32(booldatum->state);
	buf[2] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 3, fp);
	if (rc)
		return rc;
	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;
	return 0;
}

/*
 * cond_write_cond_av_list doesn't write out the av_list nodes.
 * Instead it writes out the key/value pairs from the avtab. This
 * is necessary because there is no way to uniquely identifying rules
 * in the avtab so it is not possible to associate individual rules
 * in the avtab with a conditional without saving them as part of
 * the conditional. This means that the avtab with the conditional
 * rules will not be saved but will be rebuilt on policy load.
 */
static int cond_write_av_list(struct policydb *p,
			      struct cond_av_list *list, struct policy_file *fp)
{
	__le32 buf[1];
	struct cond_av_list *cur_list;
	u32 len;
	int rc;

	len = 0;
	for (cur_list = list; cur_list != NULL; cur_list = cur_list->next)
		len++;

	buf[0] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;

	if (len == 0)
		return 0;

	for (cur_list = list; cur_list != NULL; cur_list = cur_list->next) {
		rc = avtab_write_item(p, cur_list->node, fp);
		if (rc)
			return rc;
	}

	return 0;
}

554
static int cond_write_node(struct policydb *p, struct cond_node *node,
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		    struct policy_file *fp)
{
	struct cond_expr *cur_expr;
	__le32 buf[2];
	int rc;
	u32 len = 0;

	buf[0] = cpu_to_le32(node->cur_state);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;

	for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next)
		len++;

	buf[0] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;

	for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next) {
		buf[0] = cpu_to_le32(cur_expr->expr_type);
		buf[1] = cpu_to_le32(cur_expr->bool);
		rc = put_entry(buf, sizeof(u32), 2, fp);
		if (rc)
			return rc;
	}

	rc = cond_write_av_list(p, node->true_list, fp);
	if (rc)
		return rc;
	rc = cond_write_av_list(p, node->false_list, fp);
	if (rc)
		return rc;

	return 0;
}

int cond_write_list(struct policydb *p, struct cond_node *list, void *fp)
{
	struct cond_node *cur;
	u32 len;
	__le32 buf[1];
	int rc;

	len = 0;
	for (cur = list; cur != NULL; cur = cur->next)
		len++;
	buf[0] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;

	for (cur = list; cur != NULL; cur = cur->next) {
		rc = cond_write_node(p, cur, fp);
		if (rc)
			return rc;
	}

	return 0;
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

void cond_compute_xperms(struct avtab *ctab, struct avtab_key *key,
		struct extended_perms_decision *xpermd)
{
	struct avtab_node *node;

	if (!ctab || !key || !xpermd)
		return;

	for (node = avtab_search_node(ctab, key); node;
			node = avtab_search_node_next(node, key->specified)) {
		if (node->key.specified & AVTAB_ENABLED)
			services_compute_xperms_decision(xpermd, node);
	}
	return;

}
L
Linus Torvalds 已提交
633 634 635
/* Determine whether additional permissions are granted by the conditional
 * av table, and if so, add them to the result
 */
636 637
void cond_compute_av(struct avtab *ctab, struct avtab_key *key,
		struct av_decision *avd, struct extended_perms *xperms)
L
Linus Torvalds 已提交
638 639 640
{
	struct avtab_node *node;

641
	if (!ctab || !key || !avd || !xperms)
L
Linus Torvalds 已提交
642 643
		return;

644
	for (node = avtab_search_node(ctab, key); node;
645
				node = avtab_search_node_next(node, key->specified)) {
646 647
		if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) ==
		    (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)))
648
			avd->allowed |= node->datum.u.data;
649 650
		if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) ==
		    (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)))
L
Linus Torvalds 已提交
651 652 653 654 655
			/* Since a '0' in an auditdeny mask represents a
			 * permission we do NOT want to audit (dontaudit), we use
			 * the '&' operand to ensure that all '0's in the mask
			 * are retained (much unlike the allow and auditallow cases).
			 */
656
			avd->auditdeny &= node->datum.u.data;
657 658
		if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
		    (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)))
659 660 661 662
			avd->auditallow |= node->datum.u.data;
		if ((node->key.specified & AVTAB_ENABLED) &&
				(node->key.specified & AVTAB_XPERMS))
			services_compute_xperms_drivers(xperms, node);
L
Linus Torvalds 已提交
663 664 665
	}
	return;
}