policydb.c 45.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Implementation of the policy database.
 *
 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
 */

/*
 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
 *
 *	Support for enhanced MLS infrastructure.
 *
 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
 *
14
 *	Added conditional policy language extensions
L
Linus Torvalds 已提交
15
 *
16 17 18 19 20
 * Updated: Hewlett-Packard <paul.moore@hp.com>
 *
 *      Added support for the policy capability bitmap
 *
 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
L
Linus Torvalds 已提交
21 22 23
 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
 *	This program is free software; you can redistribute it and/or modify
24
 *	it under the terms of the GNU General Public License as published by
L
Linus Torvalds 已提交
25 26 27 28
 *	the Free Software Foundation, version 2.
 */

#include <linux/kernel.h>
29
#include <linux/sched.h>
L
Linus Torvalds 已提交
30 31 32
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
33
#include <linux/audit.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42
#include "security.h"

#include "policydb.h"
#include "conditional.h"
#include "mls.h"

#define _DEBUG_HASHES

#ifdef DEBUG_HASHES
43
static const char *symtab_name[SYM_NUM] = {
L
Linus Torvalds 已提交
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
	"common prefixes",
	"classes",
	"roles",
	"types",
	"users",
	"bools",
	"levels",
	"categories",
};
#endif

static unsigned int symtab_sizes[SYM_NUM] = {
	2,
	32,
	16,
	512,
	128,
	16,
	16,
	16,
};

struct policydb_compat_info {
	int version;
	int sym_num;
	int ocon_num;
};

/* These need to be updated if SYM_NUM or OCON_NUM changes */
static struct policydb_compat_info policydb_compat[] = {
	{
75 76 77
		.version	= POLICYDB_VERSION_BASE,
		.sym_num	= SYM_NUM - 3,
		.ocon_num	= OCON_NUM - 1,
L
Linus Torvalds 已提交
78 79
	},
	{
80 81 82
		.version	= POLICYDB_VERSION_BOOL,
		.sym_num	= SYM_NUM - 2,
		.ocon_num	= OCON_NUM - 1,
L
Linus Torvalds 已提交
83 84
	},
	{
85 86 87
		.version	= POLICYDB_VERSION_IPV6,
		.sym_num	= SYM_NUM - 2,
		.ocon_num	= OCON_NUM,
L
Linus Torvalds 已提交
88 89
	},
	{
90 91 92
		.version	= POLICYDB_VERSION_NLCLASS,
		.sym_num	= SYM_NUM - 2,
		.ocon_num	= OCON_NUM,
L
Linus Torvalds 已提交
93 94
	},
	{
95 96 97
		.version	= POLICYDB_VERSION_MLS,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
L
Linus Torvalds 已提交
98
	},
99
	{
100 101 102
		.version	= POLICYDB_VERSION_AVTAB,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
103
	},
104
	{
105 106 107
		.version	= POLICYDB_VERSION_RANGETRANS,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
108
	},
109 110 111 112
	{
		.version	= POLICYDB_VERSION_POLCAP,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
E
Eric Paris 已提交
113 114 115 116 117
	},
	{
		.version	= POLICYDB_VERSION_PERMISSIVE,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
118 119 120 121 122 123
	},
	{
		.version	= POLICYDB_VERSION_BOUNDARY,
		.sym_num	= SYM_NUM,
		.ocon_num	= OCON_NUM,
	},
L
Linus Torvalds 已提交
124 125 126 127 128 129 130
};

static struct policydb_compat_info *policydb_lookup_compat(int version)
{
	int i;
	struct policydb_compat_info *info = NULL;

131
	for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		if (policydb_compat[i].version == version) {
			info = &policydb_compat[i];
			break;
		}
	}
	return info;
}

/*
 * Initialize the role table.
 */
static int roles_init(struct policydb *p)
{
	char *key = NULL;
	int rc;
	struct role_datum *role;

J
James Morris 已提交
149
	role = kzalloc(sizeof(*role), GFP_KERNEL);
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158
	if (!role) {
		rc = -ENOMEM;
		goto out;
	}
	role->value = ++p->p_roles.nprim;
	if (role->value != OBJECT_R_VAL) {
		rc = -EINVAL;
		goto out_free_role;
	}
J
Julia Lawall 已提交
159
	key = kstrdup(OBJECT_R, GFP_KERNEL);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	if (!key) {
		rc = -ENOMEM;
		goto out_free_role;
	}
	rc = hashtab_insert(p->p_roles.table, key, role);
	if (rc)
		goto out_free_key;
out:
	return rc;

out_free_key:
	kfree(key);
out_free_role:
	kfree(role);
	goto out;
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static u32 rangetr_hash(struct hashtab *h, const void *k)
{
	const struct range_trans *key = k;
	return (key->source_type + (key->target_type << 3) +
		(key->target_class << 5)) & (h->size - 1);
}

static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
{
	const struct range_trans *key1 = k1, *key2 = k2;
	return (key1->source_type != key2->source_type ||
		key1->target_type != key2->target_type ||
		key1->target_class != key2->target_class);
}

L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/*
 * Initialize a policy database structure.
 */
static int policydb_init(struct policydb *p)
{
	int i, rc;

	memset(p, 0, sizeof(*p));

	for (i = 0; i < SYM_NUM; i++) {
		rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
		if (rc)
			goto out_free_symtab;
	}

	rc = avtab_init(&p->te_avtab);
	if (rc)
		goto out_free_symtab;

	rc = roles_init(p);
	if (rc)
213
		goto out_free_symtab;
L
Linus Torvalds 已提交
214 215 216

	rc = cond_policydb_init(p);
	if (rc)
217
		goto out_free_symtab;
L
Linus Torvalds 已提交
218

219 220 221 222
	p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
	if (!p->range_tr)
		goto out_free_symtab;

223
	ebitmap_init(&p->policycaps);
E
Eric Paris 已提交
224
	ebitmap_init(&p->permissive_map);
225

L
Linus Torvalds 已提交
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
out:
	return rc;

out_free_symtab:
	for (i = 0; i < SYM_NUM; i++)
		hashtab_destroy(p->symtab[i].table);
	goto out;
}

/*
 * The following *_index functions are used to
 * define the val_to_name and val_to_struct arrays
 * in a policy database structure.  The val_to_name
 * arrays are used when converting security context
 * structures into string representations.  The
 * val_to_struct arrays are used when the attributes
 * of a class, role, or user are needed.
 */

static int common_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct common_datum *comdatum;

	comdatum = datum;
	p = datap;
	if (!comdatum->value || comdatum->value > p->p_commons.nprim)
		return -EINVAL;
	p->p_common_val_to_name[comdatum->value - 1] = key;
	return 0;
}

static int class_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct class_datum *cladatum;

	cladatum = datum;
	p = datap;
	if (!cladatum->value || cladatum->value > p->p_classes.nprim)
		return -EINVAL;
	p->p_class_val_to_name[cladatum->value - 1] = key;
	p->class_val_to_struct[cladatum->value - 1] = cladatum;
	return 0;
}

static int role_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct role_datum *role;

	role = datum;
	p = datap;
279 280 281
	if (!role->value
	    || role->value > p->p_roles.nprim
	    || role->bounds > p->p_roles.nprim)
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		return -EINVAL;
	p->p_role_val_to_name[role->value - 1] = key;
	p->role_val_to_struct[role->value - 1] = role;
	return 0;
}

static int type_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct type_datum *typdatum;

	typdatum = datum;
	p = datap;

	if (typdatum->primary) {
297 298 299
		if (!typdatum->value
		    || typdatum->value > p->p_types.nprim
		    || typdatum->bounds > p->p_types.nprim)
L
Linus Torvalds 已提交
300 301
			return -EINVAL;
		p->p_type_val_to_name[typdatum->value - 1] = key;
302
		p->type_val_to_struct[typdatum->value - 1] = typdatum;
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314
	}

	return 0;
}

static int user_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct user_datum *usrdatum;

	usrdatum = datum;
	p = datap;
315 316 317
	if (!usrdatum->value
	    || usrdatum->value > p->p_users.nprim
	    || usrdatum->bounds > p->p_users.nprim)
L
Linus Torvalds 已提交
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		return -EINVAL;
	p->p_user_val_to_name[usrdatum->value - 1] = key;
	p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
	return 0;
}

static int sens_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct level_datum *levdatum;

	levdatum = datum;
	p = datap;

	if (!levdatum->isalias) {
		if (!levdatum->level->sens ||
		    levdatum->level->sens > p->p_levels.nprim)
			return -EINVAL;
		p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
	}

	return 0;
}

static int cat_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct cat_datum *catdatum;

	catdatum = datum;
	p = datap;

	if (!catdatum->isalias) {
		if (!catdatum->value || catdatum->value > p->p_cats.nprim)
			return -EINVAL;
		p->p_cat_val_to_name[catdatum->value - 1] = key;
	}

	return 0;
}

static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
{
	common_index,
	class_index,
	role_index,
	type_index,
	user_index,
	cond_index_bool,
	sens_index,
	cat_index,
};

/*
 * Define the common val_to_name array and the class
 * val_to_name and val_to_struct arrays in a policy
 * database structure.
 *
 * Caller must clean up upon failure.
 */
static int policydb_index_classes(struct policydb *p)
{
	int rc;

	p->p_common_val_to_name =
		kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
	if (!p->p_common_val_to_name) {
		rc = -ENOMEM;
		goto out;
	}

	rc = hashtab_map(p->p_commons.table, common_index, p);
	if (rc)
		goto out;

	p->class_val_to_struct =
		kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
	if (!p->class_val_to_struct) {
		rc = -ENOMEM;
		goto out;
	}

	p->p_class_val_to_name =
		kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
	if (!p->p_class_val_to_name) {
		rc = -ENOMEM;
		goto out;
	}

	rc = hashtab_map(p->p_classes.table, class_index, p);
out:
	return rc;
}

#ifdef DEBUG_HASHES
static void symtab_hash_eval(struct symtab *s)
{
	int i;

	for (i = 0; i < SYM_NUM; i++) {
		struct hashtab *h = s[i].table;
		struct hashtab_info info;

		hashtab_stat(h, &info);
E
Eric Paris 已提交
422
		printk(KERN_DEBUG "SELinux: %s:  %d entries and %d/%d buckets used, "
L
Linus Torvalds 已提交
423 424 425 426
		       "longest chain length %d\n", symtab_name[i], h->nel,
		       info.slots_used, h->size, info.max_chain_len);
	}
}
427 428 429 430 431 432 433 434 435 436 437 438 439 440

static void rangetr_hash_eval(struct hashtab *h)
{
	struct hashtab_info info;

	hashtab_stat(h, &info);
	printk(KERN_DEBUG "SELinux: rangetr:  %d entries and %d/%d buckets used, "
	       "longest chain length %d\n", h->nel,
	       info.slots_used, h->size, info.max_chain_len);
}
#else
static inline void rangetr_hash_eval(struct hashtab *h)
{
}
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450 451 452
#endif

/*
 * Define the other val_to_name and val_to_struct arrays
 * in a policy database structure.
 *
 * Caller must clean up on failure.
 */
static int policydb_index_others(struct policydb *p)
{
	int i, rc = 0;

J
James Morris 已提交
453
	printk(KERN_DEBUG "SELinux:  %d users, %d roles, %d types, %d bools",
L
Linus Torvalds 已提交
454
	       p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
455
	if (p->mls_enabled)
L
Linus Torvalds 已提交
456 457 458 459
		printk(", %d sens, %d cats", p->p_levels.nprim,
		       p->p_cats.nprim);
	printk("\n");

J
James Morris 已提交
460
	printk(KERN_DEBUG "SELinux:  %d classes, %d rules\n",
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469
	       p->p_classes.nprim, p->te_avtab.nel);

#ifdef DEBUG_HASHES
	avtab_hash_eval(&p->te_avtab, "rules");
	symtab_hash_eval(p->symtab);
#endif

	p->role_val_to_struct =
		kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
470
			GFP_KERNEL);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477
	if (!p->role_val_to_struct) {
		rc = -ENOMEM;
		goto out;
	}

	p->user_val_to_struct =
		kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
478
			GFP_KERNEL);
L
Linus Torvalds 已提交
479 480 481 482 483
	if (!p->user_val_to_struct) {
		rc = -ENOMEM;
		goto out;
	}

484 485 486 487 488 489 490 491
	p->type_val_to_struct =
		kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
			GFP_KERNEL);
	if (!p->type_val_to_struct) {
		rc = -ENOMEM;
		goto out;
	}

L
Linus Torvalds 已提交
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
	if (cond_init_bool_indexes(p)) {
		rc = -ENOMEM;
		goto out;
	}

	for (i = SYM_ROLES; i < SYM_NUM; i++) {
		p->sym_val_to_name[i] =
			kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
		if (!p->sym_val_to_name[i]) {
			rc = -ENOMEM;
			goto out;
		}
		rc = hashtab_map(p->symtab[i].table, index_f[i], p);
		if (rc)
			goto out;
	}

out:
	return rc;
}

/*
 * The following *_destroy functions are used to
 * free any memory allocated for each kind of
 * symbol data in the policy database.
 */

static int perm_destroy(void *key, void *datum, void *p)
{
	kfree(key);
	kfree(datum);
	return 0;
}

static int common_destroy(void *key, void *datum, void *p)
{
	struct common_datum *comdatum;

	kfree(key);
	comdatum = datum;
	hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
	hashtab_destroy(comdatum->permissions.table);
	kfree(datum);
	return 0;
}

538
static int cls_destroy(void *key, void *datum, void *p)
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 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 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
	struct class_datum *cladatum;
	struct constraint_node *constraint, *ctemp;
	struct constraint_expr *e, *etmp;

	kfree(key);
	cladatum = datum;
	hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
	hashtab_destroy(cladatum->permissions.table);
	constraint = cladatum->constraints;
	while (constraint) {
		e = constraint->expr;
		while (e) {
			ebitmap_destroy(&e->names);
			etmp = e;
			e = e->next;
			kfree(etmp);
		}
		ctemp = constraint;
		constraint = constraint->next;
		kfree(ctemp);
	}

	constraint = cladatum->validatetrans;
	while (constraint) {
		e = constraint->expr;
		while (e) {
			ebitmap_destroy(&e->names);
			etmp = e;
			e = e->next;
			kfree(etmp);
		}
		ctemp = constraint;
		constraint = constraint->next;
		kfree(ctemp);
	}

	kfree(cladatum->comkey);
	kfree(datum);
	return 0;
}

static int role_destroy(void *key, void *datum, void *p)
{
	struct role_datum *role;

	kfree(key);
	role = datum;
	ebitmap_destroy(&role->dominates);
	ebitmap_destroy(&role->types);
	kfree(datum);
	return 0;
}

static int type_destroy(void *key, void *datum, void *p)
{
	kfree(key);
	kfree(datum);
	return 0;
}

static int user_destroy(void *key, void *datum, void *p)
{
	struct user_datum *usrdatum;

	kfree(key);
	usrdatum = datum;
	ebitmap_destroy(&usrdatum->roles);
	ebitmap_destroy(&usrdatum->range.level[0].cat);
	ebitmap_destroy(&usrdatum->range.level[1].cat);
	ebitmap_destroy(&usrdatum->dfltlevel.cat);
	kfree(datum);
	return 0;
}

static int sens_destroy(void *key, void *datum, void *p)
{
	struct level_datum *levdatum;

	kfree(key);
	levdatum = datum;
	ebitmap_destroy(&levdatum->level->cat);
	kfree(levdatum->level);
	kfree(datum);
	return 0;
}

static int cat_destroy(void *key, void *datum, void *p)
{
	kfree(key);
	kfree(datum);
	return 0;
}

static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
{
	common_destroy,
636
	cls_destroy,
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644
	role_destroy,
	type_destroy,
	user_destroy,
	cond_destroy_bool,
	sens_destroy,
	cat_destroy,
};

645 646 647 648 649 650 651 652 653 654 655
static int range_tr_destroy(void *key, void *datum, void *p)
{
	struct mls_range *rt = datum;
	kfree(key);
	ebitmap_destroy(&rt->level[0].cat);
	ebitmap_destroy(&rt->level[1].cat);
	kfree(datum);
	cond_resched();
	return 0;
}

L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static void ocontext_destroy(struct ocontext *c, int i)
{
	context_destroy(&c->context[0]);
	context_destroy(&c->context[1]);
	if (i == OCON_ISID || i == OCON_FS ||
	    i == OCON_NETIF || i == OCON_FSUSE)
		kfree(c->u.name);
	kfree(c);
}

/*
 * Free any memory allocated by a policy database structure.
 */
void policydb_destroy(struct policydb *p)
{
	struct ocontext *c, *ctmp;
	struct genfs *g, *gtmp;
	int i;
674 675
	struct role_allow *ra, *lra = NULL;
	struct role_trans *tr, *ltr = NULL;
L
Linus Torvalds 已提交
676 677

	for (i = 0; i < SYM_NUM; i++) {
678
		cond_resched();
L
Linus Torvalds 已提交
679 680 681 682
		hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
		hashtab_destroy(p->symtab[i].table);
	}

J
Jesper Juhl 已提交
683 684
	for (i = 0; i < SYM_NUM; i++)
		kfree(p->sym_val_to_name[i]);
L
Linus Torvalds 已提交
685

J
Jesper Juhl 已提交
686 687 688
	kfree(p->class_val_to_struct);
	kfree(p->role_val_to_struct);
	kfree(p->user_val_to_struct);
689
	kfree(p->type_val_to_struct);
L
Linus Torvalds 已提交
690 691 692 693

	avtab_destroy(&p->te_avtab);

	for (i = 0; i < OCON_NUM; i++) {
694
		cond_resched();
L
Linus Torvalds 已提交
695 696 697 698
		c = p->ocontexts[i];
		while (c) {
			ctmp = c;
			c = c->next;
699
			ocontext_destroy(ctmp, i);
L
Linus Torvalds 已提交
700
		}
701
		p->ocontexts[i] = NULL;
L
Linus Torvalds 已提交
702 703 704 705
	}

	g = p->genfs;
	while (g) {
706
		cond_resched();
L
Linus Torvalds 已提交
707 708 709 710 711
		kfree(g->fstype);
		c = g->head;
		while (c) {
			ctmp = c;
			c = c->next;
712
			ocontext_destroy(ctmp, OCON_FSUSE);
L
Linus Torvalds 已提交
713 714 715 716 717
		}
		gtmp = g;
		g = g->next;
		kfree(gtmp);
	}
718
	p->genfs = NULL;
L
Linus Torvalds 已提交
719 720 721

	cond_policydb_destroy(p);

722
	for (tr = p->role_tr; tr; tr = tr->next) {
723
		cond_resched();
J
Jesper Juhl 已提交
724
		kfree(ltr);
725 726
		ltr = tr;
	}
J
Jesper Juhl 已提交
727
	kfree(ltr);
728

729
	for (ra = p->role_allow; ra; ra = ra->next) {
730
		cond_resched();
J
Jesper Juhl 已提交
731
		kfree(lra);
732 733
		lra = ra;
	}
J
Jesper Juhl 已提交
734
	kfree(lra);
735

736 737
	hashtab_map(p->range_tr, range_tr_destroy, NULL);
	hashtab_destroy(p->range_tr);
738

739 740 741 742
	if (p->type_attr_map) {
		for (i = 0; i < p->p_types.nprim; i++)
			ebitmap_destroy(&p->type_attr_map[i]);
	}
743
	kfree(p->type_attr_map);
744
	ebitmap_destroy(&p->policycaps);
E
Eric Paris 已提交
745
	ebitmap_destroy(&p->permissive_map);
746

L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760
	return;
}

/*
 * Load the initial SIDs specified in a policy database
 * structure into a SID table.
 */
int policydb_load_isids(struct policydb *p, struct sidtab *s)
{
	struct ocontext *head, *c;
	int rc;

	rc = sidtab_init(s);
	if (rc) {
J
James Morris 已提交
761
		printk(KERN_ERR "SELinux:  out of memory on SID table init\n");
L
Linus Torvalds 已提交
762 763 764 765 766 767
		goto out;
	}

	head = p->ocontexts[OCON_ISID];
	for (c = head; c; c = c->next) {
		if (!c->context[0].user) {
J
James Morris 已提交
768
			printk(KERN_ERR "SELinux:  SID %s was never "
L
Linus Torvalds 已提交
769 770 771 772 773
			       "defined.\n", c->u.name);
			rc = -EINVAL;
			goto out;
		}
		if (sidtab_insert(s, c->sid[0], &c->context[0])) {
J
James Morris 已提交
774
			printk(KERN_ERR "SELinux:  unable to load initial "
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783
			       "SID %s.\n", c->u.name);
			rc = -EINVAL;
			goto out;
		}
	}
out:
	return rc;
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
int policydb_class_isvalid(struct policydb *p, unsigned int class)
{
	if (!class || class > p->p_classes.nprim)
		return 0;
	return 1;
}

int policydb_role_isvalid(struct policydb *p, unsigned int role)
{
	if (!role || role > p->p_roles.nprim)
		return 0;
	return 1;
}

int policydb_type_isvalid(struct policydb *p, unsigned int type)
{
	if (!type || type > p->p_types.nprim)
		return 0;
	return 1;
}

L
Linus Torvalds 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
/*
 * Return 1 if the fields in the security context
 * structure `c' are valid.  Return 0 otherwise.
 */
int policydb_context_isvalid(struct policydb *p, struct context *c)
{
	struct role_datum *role;
	struct user_datum *usrdatum;

	if (!c->role || c->role > p->p_roles.nprim)
		return 0;

	if (!c->user || c->user > p->p_users.nprim)
		return 0;

	if (!c->type || c->type > p->p_types.nprim)
		return 0;

	if (c->role != OBJECT_R_VAL) {
		/*
		 * Role must be authorized for the type.
		 */
		role = p->role_val_to_struct[c->role - 1];
		if (!ebitmap_get_bit(&role->types,
				     c->type - 1))
			/* role may not be associated with type */
			return 0;

		/*
		 * User must be authorized for the role.
		 */
		usrdatum = p->user_val_to_struct[c->user - 1];
		if (!usrdatum)
			return 0;

		if (!ebitmap_get_bit(&usrdatum->roles,
				     c->role - 1))
			/* user may not be associated with role */
			return 0;
	}

	if (!mls_context_isvalid(p, c))
		return 0;

	return 1;
}

/*
 * Read a MLS range structure from a policydb binary
 * representation file.
 */
static int mls_read_range_helper(struct mls_range *r, void *fp)
{
858 859
	__le32 buf[2];
	u32 items;
L
Linus Torvalds 已提交
860 861 862 863 864 865 866 867
	int rc;

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0)
		goto out;

	items = le32_to_cpu(buf[0]);
	if (items > ARRAY_SIZE(buf)) {
J
James Morris 已提交
868
		printk(KERN_ERR "SELinux: mls:  range overflow\n");
L
Linus Torvalds 已提交
869 870 871 872 873
		rc = -EINVAL;
		goto out;
	}
	rc = next_entry(buf, fp, sizeof(u32) * items);
	if (rc < 0) {
J
James Morris 已提交
874
		printk(KERN_ERR "SELinux: mls:  truncated range\n");
L
Linus Torvalds 已提交
875 876 877 878 879 880 881 882 883 884
		goto out;
	}
	r->level[0].sens = le32_to_cpu(buf[0]);
	if (items > 1)
		r->level[1].sens = le32_to_cpu(buf[1]);
	else
		r->level[1].sens = r->level[0].sens;

	rc = ebitmap_read(&r->level[0].cat, fp);
	if (rc) {
J
James Morris 已提交
885
		printk(KERN_ERR "SELinux: mls:  error reading low "
L
Linus Torvalds 已提交
886 887 888 889 890 891
		       "categories\n");
		goto out;
	}
	if (items > 1) {
		rc = ebitmap_read(&r->level[1].cat, fp);
		if (rc) {
J
James Morris 已提交
892
			printk(KERN_ERR "SELinux: mls:  error reading high "
L
Linus Torvalds 已提交
893 894 895 896 897 898
			       "categories\n");
			goto bad_high;
		}
	} else {
		rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
		if (rc) {
J
James Morris 已提交
899
			printk(KERN_ERR "SELinux: mls:  out of memory\n");
L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
			goto bad_high;
		}
	}

	rc = 0;
out:
	return rc;
bad_high:
	ebitmap_destroy(&r->level[0].cat);
	goto out;
}

/*
 * Read and validate a security context structure
 * from a policydb binary representation file.
 */
static int context_read_and_validate(struct context *c,
				     struct policydb *p,
				     void *fp)
{
920
	__le32 buf[3];
L
Linus Torvalds 已提交
921 922 923 924
	int rc;

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0) {
J
James Morris 已提交
925
		printk(KERN_ERR "SELinux: context truncated\n");
L
Linus Torvalds 已提交
926 927 928 929 930 931 932
		goto out;
	}
	c->user = le32_to_cpu(buf[0]);
	c->role = le32_to_cpu(buf[1]);
	c->type = le32_to_cpu(buf[2]);
	if (p->policyvers >= POLICYDB_VERSION_MLS) {
		if (mls_read_range_helper(&c->range, fp)) {
J
James Morris 已提交
933
			printk(KERN_ERR "SELinux: error reading MLS range of "
L
Linus Torvalds 已提交
934 935 936 937 938 939 940
			       "context\n");
			rc = -EINVAL;
			goto out;
		}
	}

	if (!policydb_context_isvalid(p, c)) {
J
James Morris 已提交
941
		printk(KERN_ERR "SELinux:  invalid security context\n");
L
Linus Torvalds 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
		context_destroy(c);
		rc = -EINVAL;
	}
out:
	return rc;
}

/*
 * The following *_read functions are used to
 * read the symbol data from a policy database
 * binary representation file.
 */

static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct perm_datum *perdatum;
	int rc;
960 961
	__le32 buf[2];
	u32 len;
L
Linus Torvalds 已提交
962

J
James Morris 已提交
963
	perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
L
Linus Torvalds 已提交
964 965 966 967 968 969 970 971 972 973 974 975
	if (!perdatum) {
		rc = -ENOMEM;
		goto out;
	}

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	perdatum->value = le32_to_cpu(buf[1]);

976
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
977 978 979 980 981 982 983
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
984
	key[len] = '\0';
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

	rc = hashtab_insert(h, key, perdatum);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	perm_destroy(key, perdatum, NULL);
	goto out;
}

static int common_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct common_datum *comdatum;
1000 1001
	__le32 buf[4];
	u32 len, nel;
L
Linus Torvalds 已提交
1002 1003
	int i, rc;

J
James Morris 已提交
1004
	comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	if (!comdatum) {
		rc = -ENOMEM;
		goto out;
	}

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	comdatum->value = le32_to_cpu(buf[1]);

	rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
	if (rc)
		goto bad;
	comdatum->permissions.nprim = le32_to_cpu(buf[2]);
	nel = le32_to_cpu(buf[3]);

1023
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1031
	key[len] = '\0';
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

	for (i = 0; i < nel; i++) {
		rc = perm_read(p, comdatum->permissions.table, fp);
		if (rc)
			goto bad;
	}

	rc = hashtab_insert(h, key, comdatum);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	common_destroy(key, comdatum, NULL);
	goto out;
}

static int read_cons_helper(struct constraint_node **nodep, int ncons,
1050
			    int allowxtarget, void *fp)
L
Linus Torvalds 已提交
1051 1052 1053
{
	struct constraint_node *c, *lc;
	struct constraint_expr *e, *le;
1054 1055
	__le32 buf[3];
	u32 nexpr;
L
Linus Torvalds 已提交
1056 1057 1058 1059
	int rc, i, j, depth;

	lc = NULL;
	for (i = 0; i < ncons; i++) {
J
James Morris 已提交
1060
		c = kzalloc(sizeof(*c), GFP_KERNEL);
L
Linus Torvalds 已提交
1061 1062 1063
		if (!c)
			return -ENOMEM;

1064
		if (lc)
L
Linus Torvalds 已提交
1065
			lc->next = c;
1066
		else
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
			*nodep = c;

		rc = next_entry(buf, fp, (sizeof(u32) * 2));
		if (rc < 0)
			return rc;
		c->permissions = le32_to_cpu(buf[0]);
		nexpr = le32_to_cpu(buf[1]);
		le = NULL;
		depth = -1;
		for (j = 0; j < nexpr; j++) {
J
James Morris 已提交
1077
			e = kzalloc(sizeof(*e), GFP_KERNEL);
L
Linus Torvalds 已提交
1078 1079 1080
			if (!e)
				return -ENOMEM;

1081
			if (le)
L
Linus Torvalds 已提交
1082
				le->next = e;
1083
			else
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
				c->expr = e;

			rc = next_entry(buf, fp, (sizeof(u32) * 3));
			if (rc < 0)
				return rc;
			e->expr_type = le32_to_cpu(buf[0]);
			e->attr = le32_to_cpu(buf[1]);
			e->op = le32_to_cpu(buf[2]);

			switch (e->expr_type) {
			case CEXPR_NOT:
				if (depth < 0)
					return -EINVAL;
				break;
			case CEXPR_AND:
			case CEXPR_OR:
				if (depth < 1)
					return -EINVAL;
				depth--;
				break;
			case CEXPR_ATTR:
				if (depth == (CEXPR_MAXDEPTH - 1))
					return -EINVAL;
				depth++;
				break;
			case CEXPR_NAMES:
				if (!allowxtarget && (e->attr & CEXPR_XTARGET))
					return -EINVAL;
				if (depth == (CEXPR_MAXDEPTH - 1))
					return -EINVAL;
				depth++;
				if (ebitmap_read(&e->names, fp))
					return -EINVAL;
				break;
			default:
				return -EINVAL;
			}
			le = e;
		}
		if (depth != 0)
			return -EINVAL;
		lc = c;
	}

	return 0;
}

static int class_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct class_datum *cladatum;
1135 1136
	__le32 buf[6];
	u32 len, len2, ncons, nel;
L
Linus Torvalds 已提交
1137 1138
	int i, rc;

J
James Morris 已提交
1139
	cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	if (!cladatum) {
		rc = -ENOMEM;
		goto out;
	}

	rc = next_entry(buf, fp, sizeof(u32)*6);
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	len2 = le32_to_cpu(buf[1]);
	cladatum->value = le32_to_cpu(buf[2]);

	rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
	if (rc)
		goto bad;
	cladatum->permissions.nprim = le32_to_cpu(buf[3]);
	nel = le32_to_cpu(buf[4]);

	ncons = le32_to_cpu(buf[5]);

1161
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166 1167 1168
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1169
	key[len] = '\0';
L
Linus Torvalds 已提交
1170 1171

	if (len2) {
1172
		cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1173 1174 1175 1176 1177 1178 1179
		if (!cladatum->comkey) {
			rc = -ENOMEM;
			goto bad;
		}
		rc = next_entry(cladatum->comkey, fp, len2);
		if (rc < 0)
			goto bad;
1180
		cladatum->comkey[len2] = '\0';
L
Linus Torvalds 已提交
1181 1182 1183 1184

		cladatum->comdatum = hashtab_search(p->p_commons.table,
						    cladatum->comkey);
		if (!cladatum->comdatum) {
J
James Morris 已提交
1185
			printk(KERN_ERR "SELinux:  unknown common %s\n",
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
			       cladatum->comkey);
			rc = -EINVAL;
			goto bad;
		}
	}
	for (i = 0; i < nel; i++) {
		rc = perm_read(p, cladatum->permissions.table, fp);
		if (rc)
			goto bad;
	}

	rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
	if (rc)
		goto bad;

	if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
		/* grab the validatetrans rules */
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc < 0)
			goto bad;
		ncons = le32_to_cpu(buf[0]);
		rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
		if (rc)
			goto bad;
	}

	rc = hashtab_insert(h, key, cladatum);
	if (rc)
		goto bad;

	rc = 0;
out:
	return rc;
bad:
1220
	cls_destroy(key, cladatum, NULL);
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226 1227
	goto out;
}

static int role_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct role_datum *role;
1228 1229
	int rc, to_read = 2;
	__le32 buf[3];
1230
	u32 len;
L
Linus Torvalds 已提交
1231

J
James Morris 已提交
1232
	role = kzalloc(sizeof(*role), GFP_KERNEL);
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237
	if (!role) {
		rc = -ENOMEM;
		goto out;
	}

1238 1239 1240 1241
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 3;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	role->value = le32_to_cpu(buf[1]);
1247 1248
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		role->bounds = le32_to_cpu(buf[2]);
L
Linus Torvalds 已提交
1249

1250
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1251 1252 1253 1254 1255 1256 1257
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1258
	key[len] = '\0';
L
Linus Torvalds 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

	rc = ebitmap_read(&role->dominates, fp);
	if (rc)
		goto bad;

	rc = ebitmap_read(&role->types, fp);
	if (rc)
		goto bad;

	if (strcmp(key, OBJECT_R) == 0) {
		if (role->value != OBJECT_R_VAL) {
E
Eric Paris 已提交
1270
			printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
L
Linus Torvalds 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
			       OBJECT_R, role->value);
			rc = -EINVAL;
			goto bad;
		}
		rc = 0;
		goto bad;
	}

	rc = hashtab_insert(h, key, role);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	role_destroy(key, role, NULL);
	goto out;
}

static int type_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct type_datum *typdatum;
1293 1294
	int rc, to_read = 3;
	__le32 buf[4];
1295
	u32 len;
L
Linus Torvalds 已提交
1296

1297
	typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302
	if (!typdatum) {
		rc = -ENOMEM;
		return rc;
	}

1303 1304 1305 1306
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 4;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
L
Linus Torvalds 已提交
1307 1308 1309 1310 1311
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	typdatum->value = le32_to_cpu(buf[1]);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
		u32 prop = le32_to_cpu(buf[2]);

		if (prop & TYPEDATUM_PROPERTY_PRIMARY)
			typdatum->primary = 1;
		if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
			typdatum->attribute = 1;

		typdatum->bounds = le32_to_cpu(buf[3]);
	} else {
		typdatum->primary = le32_to_cpu(buf[2]);
	}
L
Linus Torvalds 已提交
1324

1325
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331 1332
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1333
	key[len] = '\0';
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

	rc = hashtab_insert(h, key, typdatum);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	type_destroy(key, typdatum, NULL);
	goto out;
}


/*
 * Read a MLS level structure from a policydb binary
 * representation file.
 */
static int mls_read_level(struct mls_level *lp, void *fp)
{
1352
	__le32 buf[1];
L
Linus Torvalds 已提交
1353 1354 1355 1356 1357 1358
	int rc;

	memset(lp, 0, sizeof(*lp));

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0) {
J
James Morris 已提交
1359
		printk(KERN_ERR "SELinux: mls: truncated level\n");
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364
		goto bad;
	}
	lp->sens = le32_to_cpu(buf[0]);

	if (ebitmap_read(&lp->cat, fp)) {
J
James Morris 已提交
1365
		printk(KERN_ERR "SELinux: mls:  error reading level "
L
Linus Torvalds 已提交
1366 1367 1368
		       "categories\n");
		goto bad;
	}
1369

L
Linus Torvalds 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	return 0;

bad:
	return -EINVAL;
}

static int user_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct user_datum *usrdatum;
1380 1381
	int rc, to_read = 2;
	__le32 buf[3];
1382
	u32 len;
L
Linus Torvalds 已提交
1383

J
James Morris 已提交
1384
	usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
L
Linus Torvalds 已提交
1385 1386 1387 1388 1389
	if (!usrdatum) {
		rc = -ENOMEM;
		goto out;
	}

1390 1391 1392 1393
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 3;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	usrdatum->value = le32_to_cpu(buf[1]);
1399 1400
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		usrdatum->bounds = le32_to_cpu(buf[2]);
L
Linus Torvalds 已提交
1401

1402
	key = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1403 1404 1405 1406 1407 1408 1409
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1410
	key[len] = '\0';
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

	rc = ebitmap_read(&usrdatum->roles, fp);
	if (rc)
		goto bad;

	if (p->policyvers >= POLICYDB_VERSION_MLS) {
		rc = mls_read_range_helper(&usrdatum->range, fp);
		if (rc)
			goto bad;
		rc = mls_read_level(&usrdatum->dfltlevel, fp);
		if (rc)
			goto bad;
	}

	rc = hashtab_insert(h, key, usrdatum);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	user_destroy(key, usrdatum, NULL);
	goto out;
}

static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct level_datum *levdatum;
	int rc;
1440 1441
	__le32 buf[2];
	u32 len;
L
Linus Torvalds 已提交
1442

J
James Morris 已提交
1443
	levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
	if (!levdatum) {
		rc = -ENOMEM;
		goto out;
	}

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	levdatum->isalias = le32_to_cpu(buf[1]);

1456
	key = kmalloc(len + 1, GFP_ATOMIC);
L
Linus Torvalds 已提交
1457 1458 1459 1460 1461 1462 1463
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1464
	key[len] = '\0';
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

	levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
	if (!levdatum->level) {
		rc = -ENOMEM;
		goto bad;
	}
	if (mls_read_level(levdatum->level, fp)) {
		rc = -EINVAL;
		goto bad;
	}

	rc = hashtab_insert(h, key, levdatum);
	if (rc)
		goto bad;
out:
	return rc;
bad:
	sens_destroy(key, levdatum, NULL);
	goto out;
}

static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct cat_datum *catdatum;
	int rc;
1491 1492
	__le32 buf[3];
	u32 len;
L
Linus Torvalds 已提交
1493

J
James Morris 已提交
1494
	catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	if (!catdatum) {
		rc = -ENOMEM;
		goto out;
	}

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0)
		goto bad;

	len = le32_to_cpu(buf[0]);
	catdatum->value = le32_to_cpu(buf[1]);
	catdatum->isalias = le32_to_cpu(buf[2]);

1508
	key = kmalloc(len + 1, GFP_ATOMIC);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515
	if (!key) {
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(key, fp, len);
	if (rc < 0)
		goto bad;
1516
	key[len] = '\0';
L
Linus Torvalds 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

	rc = hashtab_insert(h, key, catdatum);
	if (rc)
		goto bad;
out:
	return rc;

bad:
	cat_destroy(key, catdatum, NULL);
	goto out;
}

static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
{
	common_read,
	class_read,
	role_read,
	type_read,
	user_read,
	cond_read_bool,
	sens_read,
	cat_read,
};

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
static int user_bounds_sanity_check(void *key, void *datum, void *datap)
{
	struct user_datum *upper, *user;
	struct policydb *p = datap;
	int depth = 0;

	upper = user = datum;
	while (upper->bounds) {
		struct ebitmap_node *node;
		unsigned long bit;

		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
			printk(KERN_ERR "SELinux: user %s: "
			       "too deep or looped boundary",
			       (char *) key);
			return -EINVAL;
		}

		upper = p->user_val_to_struct[upper->bounds - 1];
		ebitmap_for_each_positive_bit(&user->roles, node, bit) {
			if (ebitmap_get_bit(&upper->roles, bit))
				continue;

			printk(KERN_ERR
			       "SELinux: boundary violated policy: "
			       "user=%s role=%s bounds=%s\n",
			       p->p_user_val_to_name[user->value - 1],
			       p->p_role_val_to_name[bit],
			       p->p_user_val_to_name[upper->value - 1]);

			return -EINVAL;
		}
	}

	return 0;
}

static int role_bounds_sanity_check(void *key, void *datum, void *datap)
{
	struct role_datum *upper, *role;
	struct policydb *p = datap;
	int depth = 0;

	upper = role = datum;
	while (upper->bounds) {
		struct ebitmap_node *node;
		unsigned long bit;

		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
			printk(KERN_ERR "SELinux: role %s: "
			       "too deep or looped bounds\n",
			       (char *) key);
			return -EINVAL;
		}

		upper = p->role_val_to_struct[upper->bounds - 1];
		ebitmap_for_each_positive_bit(&role->types, node, bit) {
			if (ebitmap_get_bit(&upper->types, bit))
				continue;

			printk(KERN_ERR
			       "SELinux: boundary violated policy: "
			       "role=%s type=%s bounds=%s\n",
			       p->p_role_val_to_name[role->value - 1],
			       p->p_type_val_to_name[bit],
			       p->p_role_val_to_name[upper->value - 1]);

			return -EINVAL;
		}
	}

	return 0;
}

static int type_bounds_sanity_check(void *key, void *datum, void *datap)
{
	struct type_datum *upper, *type;
	struct policydb *p = datap;
	int depth = 0;

	upper = type = datum;
	while (upper->bounds) {
		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
			printk(KERN_ERR "SELinux: type %s: "
			       "too deep or looped boundary\n",
			       (char *) key);
			return -EINVAL;
		}

		upper = p->type_val_to_struct[upper->bounds - 1];
		if (upper->attribute) {
			printk(KERN_ERR "SELinux: type %s: "
			       "bounded by attribute %s",
			       (char *) key,
			       p->p_type_val_to_name[upper->value - 1]);
			return -EINVAL;
		}
	}

	return 0;
}

static int policydb_bounds_sanity_check(struct policydb *p)
{
	int rc;

	if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
		return 0;

	rc = hashtab_map(p->p_users.table,
			 user_bounds_sanity_check, p);
	if (rc)
		return rc;

	rc = hashtab_map(p->p_roles.table,
			 role_bounds_sanity_check, p);
	if (rc)
		return rc;

	rc = hashtab_map(p->p_types.table,
			 type_bounds_sanity_check, p);
	if (rc)
		return rc;

	return 0;
}

L
Linus Torvalds 已提交
1668 1669
extern int ss_initialized;

1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
u16 string_to_security_class(struct policydb *p, const char *name)
{
	struct class_datum *cladatum;

	cladatum = hashtab_search(p->p_classes.table, name);
	if (!cladatum)
		return 0;

	return cladatum->value;
}

u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
{
	struct class_datum *cladatum;
	struct perm_datum *perdatum = NULL;
	struct common_datum *comdatum;

	if (!tclass || tclass > p->p_classes.nprim)
		return 0;

	cladatum = p->class_val_to_struct[tclass-1];
	comdatum = cladatum->comdatum;
	if (comdatum)
		perdatum = hashtab_search(comdatum->permissions.table,
					  name);
	if (!perdatum)
		perdatum = hashtab_search(cladatum->permissions.table,
					  name);
	if (!perdatum)
		return 0;

	return 1U << (perdatum->value-1);
}

L
Linus Torvalds 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
/*
 * Read the configuration data from a policy database binary
 * representation file into a policy database structure.
 */
int policydb_read(struct policydb *p, void *fp)
{
	struct role_allow *ra, *lra;
	struct role_trans *tr, *ltr;
	struct ocontext *l, *c, *newc;
	struct genfs *genfs_p, *genfs, *newgenfs;
	int i, j, rc;
1715 1716
	__le32 buf[4];
	u32 nodebuf[8];
1717
	u32 len, len2, nprim, nel, nel2;
L
Linus Torvalds 已提交
1718 1719
	char *policydb_str;
	struct policydb_compat_info *info;
1720 1721
	struct range_trans *rt;
	struct mls_range *r;
L
Linus Torvalds 已提交
1722 1723 1724 1725 1726 1727

	rc = policydb_init(p);
	if (rc)
		goto out;

	/* Read the magic number and string length. */
1728
	rc = next_entry(buf, fp, sizeof(u32) * 2);
L
Linus Torvalds 已提交
1729 1730 1731
	if (rc < 0)
		goto bad;

1732
	if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
J
James Morris 已提交
1733
		printk(KERN_ERR "SELinux:  policydb magic number 0x%x does "
L
Linus Torvalds 已提交
1734
		       "not match expected magic number 0x%x\n",
1735
		       le32_to_cpu(buf[0]), POLICYDB_MAGIC);
L
Linus Torvalds 已提交
1736 1737 1738
		goto bad;
	}

1739
	len = le32_to_cpu(buf[1]);
L
Linus Torvalds 已提交
1740
	if (len != strlen(POLICYDB_STRING)) {
J
James Morris 已提交
1741
		printk(KERN_ERR "SELinux:  policydb string length %d does not "
L
Linus Torvalds 已提交
1742 1743 1744 1745
		       "match expected length %Zu\n",
		       len, strlen(POLICYDB_STRING));
		goto bad;
	}
1746
	policydb_str = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1747
	if (!policydb_str) {
J
James Morris 已提交
1748
		printk(KERN_ERR "SELinux:  unable to allocate memory for policydb "
L
Linus Torvalds 已提交
1749 1750 1751 1752 1753 1754
		       "string of length %d\n", len);
		rc = -ENOMEM;
		goto bad;
	}
	rc = next_entry(policydb_str, fp, len);
	if (rc < 0) {
J
James Morris 已提交
1755
		printk(KERN_ERR "SELinux:  truncated policydb string identifier\n");
L
Linus Torvalds 已提交
1756 1757 1758
		kfree(policydb_str);
		goto bad;
	}
1759
	policydb_str[len] = '\0';
L
Linus Torvalds 已提交
1760
	if (strcmp(policydb_str, POLICYDB_STRING)) {
J
James Morris 已提交
1761
		printk(KERN_ERR "SELinux:  policydb string %s does not match "
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769
		       "my string %s\n", policydb_str, POLICYDB_STRING);
		kfree(policydb_str);
		goto bad;
	}
	/* Done with policydb_str. */
	kfree(policydb_str);
	policydb_str = NULL;

1770
	/* Read the version and table sizes. */
L
Linus Torvalds 已提交
1771 1772 1773 1774
	rc = next_entry(buf, fp, sizeof(u32)*4);
	if (rc < 0)
		goto bad;

1775
	p->policyvers = le32_to_cpu(buf[0]);
L
Linus Torvalds 已提交
1776 1777
	if (p->policyvers < POLICYDB_VERSION_MIN ||
	    p->policyvers > POLICYDB_VERSION_MAX) {
J
James Morris 已提交
1778
		printk(KERN_ERR "SELinux:  policydb version %d does not match "
1779 1780 1781
		       "my version range %d-%d\n",
		       le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
		goto bad;
L
Linus Torvalds 已提交
1782 1783
	}

1784
	if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1785
		p->mls_enabled = 1;
L
Linus Torvalds 已提交
1786 1787

		if (p->policyvers < POLICYDB_VERSION_MLS) {
E
Eric Paris 已提交
1788 1789 1790
			printk(KERN_ERR "SELinux: security policydb version %d "
				"(MLS) not backwards compatible\n",
				p->policyvers);
L
Linus Torvalds 已提交
1791 1792 1793
			goto bad;
		}
	}
1794 1795
	p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
	p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
L
Linus Torvalds 已提交
1796

1797 1798 1799 1800
	if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
	    ebitmap_read(&p->policycaps, fp) != 0)
		goto bad;

E
Eric Paris 已提交
1801 1802 1803 1804
	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
	    ebitmap_read(&p->permissive_map, fp) != 0)
		goto bad;

L
Linus Torvalds 已提交
1805 1806
	info = policydb_lookup_compat(p->policyvers);
	if (!info) {
J
James Morris 已提交
1807
		printk(KERN_ERR "SELinux:  unable to find policy compat info "
L
Linus Torvalds 已提交
1808 1809 1810 1811
		       "for version %d\n", p->policyvers);
		goto bad;
	}

1812 1813
	if (le32_to_cpu(buf[2]) != info->sym_num ||
		le32_to_cpu(buf[3]) != info->ocon_num) {
J
James Morris 已提交
1814
		printk(KERN_ERR "SELinux:  policydb table sizes (%d,%d) do "
1815 1816
		       "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
			le32_to_cpu(buf[3]),
L
Linus Torvalds 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
		       info->sym_num, info->ocon_num);
		goto bad;
	}

	for (i = 0; i < info->sym_num; i++) {
		rc = next_entry(buf, fp, sizeof(u32)*2);
		if (rc < 0)
			goto bad;
		nprim = le32_to_cpu(buf[0]);
		nel = le32_to_cpu(buf[1]);
		for (j = 0; j < nel; j++) {
			rc = read_f[i](p, p->symtab[i].table, fp);
			if (rc)
				goto bad;
		}

		p->symtab[i].nprim = nprim;
	}

1836
	rc = avtab_read(&p->te_avtab, fp, p);
L
Linus Torvalds 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
	if (rc)
		goto bad;

	if (p->policyvers >= POLICYDB_VERSION_BOOL) {
		rc = cond_read_list(p, fp);
		if (rc)
			goto bad;
	}

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0)
		goto bad;
	nel = le32_to_cpu(buf[0]);
	ltr = NULL;
	for (i = 0; i < nel; i++) {
J
James Morris 已提交
1852
		tr = kzalloc(sizeof(*tr), GFP_KERNEL);
L
Linus Torvalds 已提交
1853 1854 1855 1856
		if (!tr) {
			rc = -ENOMEM;
			goto bad;
		}
1857
		if (ltr)
L
Linus Torvalds 已提交
1858
			ltr->next = tr;
1859
		else
L
Linus Torvalds 已提交
1860 1861 1862 1863 1864 1865 1866
			p->role_tr = tr;
		rc = next_entry(buf, fp, sizeof(u32)*3);
		if (rc < 0)
			goto bad;
		tr->role = le32_to_cpu(buf[0]);
		tr->type = le32_to_cpu(buf[1]);
		tr->new_role = le32_to_cpu(buf[2]);
1867 1868 1869 1870 1871 1872
		if (!policydb_role_isvalid(p, tr->role) ||
		    !policydb_type_isvalid(p, tr->type) ||
		    !policydb_role_isvalid(p, tr->new_role)) {
			rc = -EINVAL;
			goto bad;
		}
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881
		ltr = tr;
	}

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0)
		goto bad;
	nel = le32_to_cpu(buf[0]);
	lra = NULL;
	for (i = 0; i < nel; i++) {
J
James Morris 已提交
1882
		ra = kzalloc(sizeof(*ra), GFP_KERNEL);
L
Linus Torvalds 已提交
1883 1884 1885 1886
		if (!ra) {
			rc = -ENOMEM;
			goto bad;
		}
1887
		if (lra)
L
Linus Torvalds 已提交
1888
			lra->next = ra;
1889
		else
L
Linus Torvalds 已提交
1890 1891 1892 1893 1894 1895
			p->role_allow = ra;
		rc = next_entry(buf, fp, sizeof(u32)*2);
		if (rc < 0)
			goto bad;
		ra->role = le32_to_cpu(buf[0]);
		ra->new_role = le32_to_cpu(buf[1]);
1896 1897 1898 1899 1900
		if (!policydb_role_isvalid(p, ra->role) ||
		    !policydb_role_isvalid(p, ra->new_role)) {
			rc = -EINVAL;
			goto bad;
		}
L
Linus Torvalds 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
		lra = ra;
	}

	rc = policydb_index_classes(p);
	if (rc)
		goto bad;

	rc = policydb_index_others(p);
	if (rc)
		goto bad;

1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
	p->process_class = string_to_security_class(p, "process");
	if (!p->process_class)
		goto bad;
	p->process_trans_perms = string_to_av_perm(p, p->process_class,
						   "transition");
	p->process_trans_perms |= string_to_av_perm(p, p->process_class,
						    "dyntransition");
	if (!p->process_trans_perms)
		goto bad;

L
Linus Torvalds 已提交
1922 1923 1924 1925 1926 1927 1928
	for (i = 0; i < info->ocon_num; i++) {
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc < 0)
			goto bad;
		nel = le32_to_cpu(buf[0]);
		l = NULL;
		for (j = 0; j < nel; j++) {
J
James Morris 已提交
1929
			c = kzalloc(sizeof(*c), GFP_KERNEL);
L
Linus Torvalds 已提交
1930 1931 1932 1933
			if (!c) {
				rc = -ENOMEM;
				goto bad;
			}
1934
			if (l)
L
Linus Torvalds 已提交
1935
				l->next = c;
1936
			else
L
Linus Torvalds 已提交
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
				p->ocontexts[i] = c;
			l = c;
			rc = -EINVAL;
			switch (i) {
			case OCON_ISID:
				rc = next_entry(buf, fp, sizeof(u32));
				if (rc < 0)
					goto bad;
				c->sid[0] = le32_to_cpu(buf[0]);
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto bad;
				break;
			case OCON_FS:
			case OCON_NETIF:
				rc = next_entry(buf, fp, sizeof(u32));
				if (rc < 0)
					goto bad;
				len = le32_to_cpu(buf[0]);
1956
				c->u.name = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
				if (!c->u.name) {
					rc = -ENOMEM;
					goto bad;
				}
				rc = next_entry(c->u.name, fp, len);
				if (rc < 0)
					goto bad;
				c->u.name[len] = 0;
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto bad;
				rc = context_read_and_validate(&c->context[1], p, fp);
				if (rc)
					goto bad;
				break;
			case OCON_PORT:
				rc = next_entry(buf, fp, sizeof(u32)*3);
				if (rc < 0)
					goto bad;
				c->u.port.protocol = le32_to_cpu(buf[0]);
				c->u.port.low_port = le32_to_cpu(buf[1]);
				c->u.port.high_port = le32_to_cpu(buf[2]);
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto bad;
				break;
			case OCON_NODE:
1984
				rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
L
Linus Torvalds 已提交
1985 1986
				if (rc < 0)
					goto bad;
1987 1988
				c->u.node.addr = nodebuf[0]; /* network order */
				c->u.node.mask = nodebuf[1]; /* network order */
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto bad;
				break;
			case OCON_FSUSE:
				rc = next_entry(buf, fp, sizeof(u32)*2);
				if (rc < 0)
					goto bad;
				c->v.behavior = le32_to_cpu(buf[0]);
				if (c->v.behavior > SECURITY_FS_USE_NONE)
					goto bad;
				len = le32_to_cpu(buf[1]);
2001
				c->u.name = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
				if (!c->u.name) {
					rc = -ENOMEM;
					goto bad;
				}
				rc = next_entry(c->u.name, fp, len);
				if (rc < 0)
					goto bad;
				c->u.name[len] = 0;
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto bad;
				break;
			case OCON_NODE6: {
				int k;

2017
				rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
L
Linus Torvalds 已提交
2018 2019 2020
				if (rc < 0)
					goto bad;
				for (k = 0; k < 4; k++)
2021
					c->u.node6.addr[k] = nodebuf[k];
L
Linus Torvalds 已提交
2022
				for (k = 0; k < 4; k++)
2023
					c->u.node6.mask[k] = nodebuf[k+4];
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
				if (context_read_and_validate(&c->context[0], p, fp))
					goto bad;
				break;
			}
			}
		}
	}

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0)
		goto bad;
	nel = le32_to_cpu(buf[0]);
	genfs_p = NULL;
	rc = -EINVAL;
	for (i = 0; i < nel; i++) {
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc < 0)
			goto bad;
		len = le32_to_cpu(buf[0]);
J
James Morris 已提交
2043
		newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
L
Linus Torvalds 已提交
2044 2045 2046 2047 2048
		if (!newgenfs) {
			rc = -ENOMEM;
			goto bad;
		}

2049
		newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
		if (!newgenfs->fstype) {
			rc = -ENOMEM;
			kfree(newgenfs);
			goto bad;
		}
		rc = next_entry(newgenfs->fstype, fp, len);
		if (rc < 0) {
			kfree(newgenfs->fstype);
			kfree(newgenfs);
			goto bad;
		}
		newgenfs->fstype[len] = 0;
		for (genfs_p = NULL, genfs = p->genfs; genfs;
		     genfs_p = genfs, genfs = genfs->next) {
			if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
J
James Morris 已提交
2065
				printk(KERN_ERR "SELinux:  dup genfs "
L
Linus Torvalds 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
				       "fstype %s\n", newgenfs->fstype);
				kfree(newgenfs->fstype);
				kfree(newgenfs);
				goto bad;
			}
			if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
				break;
		}
		newgenfs->next = genfs;
		if (genfs_p)
			genfs_p->next = newgenfs;
		else
			p->genfs = newgenfs;
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc < 0)
			goto bad;
		nel2 = le32_to_cpu(buf[0]);
		for (j = 0; j < nel2; j++) {
			rc = next_entry(buf, fp, sizeof(u32));
			if (rc < 0)
				goto bad;
			len = le32_to_cpu(buf[0]);

J
James Morris 已提交
2089
			newc = kzalloc(sizeof(*newc), GFP_KERNEL);
L
Linus Torvalds 已提交
2090 2091 2092 2093 2094
			if (!newc) {
				rc = -ENOMEM;
				goto bad;
			}

2095
			newc->u.name = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
			if (!newc->u.name) {
				rc = -ENOMEM;
				goto bad_newc;
			}
			rc = next_entry(newc->u.name, fp, len);
			if (rc < 0)
				goto bad_newc;
			newc->u.name[len] = 0;
			rc = next_entry(buf, fp, sizeof(u32));
			if (rc < 0)
				goto bad_newc;
			newc->v.sclass = le32_to_cpu(buf[0]);
			if (context_read_and_validate(&newc->context[0], p, fp))
				goto bad_newc;
			for (l = NULL, c = newgenfs->head; c;
			     l = c, c = c->next) {
				if (!strcmp(newc->u.name, c->u.name) &&
				    (!c->v.sclass || !newc->v.sclass ||
				     newc->v.sclass == c->v.sclass)) {
J
James Morris 已提交
2115
					printk(KERN_ERR "SELinux:  dup genfs "
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
					       "entry (%s,%s)\n",
					       newgenfs->fstype, c->u.name);
					goto bad_newc;
				}
				len = strlen(newc->u.name);
				len2 = strlen(c->u.name);
				if (len > len2)
					break;
			}

			newc->next = c;
			if (l)
				l->next = newc;
			else
				newgenfs->head = newc;
		}
	}

	if (p->policyvers >= POLICYDB_VERSION_MLS) {
2135
		int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
L
Linus Torvalds 已提交
2136 2137 2138 2139 2140
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc < 0)
			goto bad;
		nel = le32_to_cpu(buf[0]);
		for (i = 0; i < nel; i++) {
J
James Morris 已提交
2141
			rt = kzalloc(sizeof(*rt), GFP_KERNEL);
L
Linus Torvalds 已提交
2142 2143 2144 2145 2146
			if (!rt) {
				rc = -ENOMEM;
				goto bad;
			}
			rc = next_entry(buf, fp, (sizeof(u32) * 2));
2147 2148
			if (rc < 0) {
				kfree(rt);
L
Linus Torvalds 已提交
2149
				goto bad;
2150
			}
2151 2152 2153 2154
			rt->source_type = le32_to_cpu(buf[0]);
			rt->target_type = le32_to_cpu(buf[1]);
			if (new_rangetr) {
				rc = next_entry(buf, fp, sizeof(u32));
2155 2156
				if (rc < 0) {
					kfree(rt);
2157
					goto bad;
2158
				}
2159 2160
				rt->target_class = le32_to_cpu(buf[0]);
			} else
2161
				rt->target_class = p->process_class;
2162 2163 2164
			if (!policydb_type_isvalid(p, rt->source_type) ||
			    !policydb_type_isvalid(p, rt->target_type) ||
			    !policydb_class_isvalid(p, rt->target_class)) {
2165
				kfree(rt);
2166 2167 2168
				rc = -EINVAL;
				goto bad;
			}
2169 2170 2171 2172
			r = kzalloc(sizeof(*r), GFP_KERNEL);
			if (!r) {
				kfree(rt);
				rc = -ENOMEM;
L
Linus Torvalds 已提交
2173
				goto bad;
2174 2175 2176 2177 2178 2179 2180 2181
			}
			rc = mls_read_range_helper(r, fp);
			if (rc) {
				kfree(rt);
				kfree(r);
				goto bad;
			}
			if (!mls_range_isvalid(p, r)) {
J
James Morris 已提交
2182
				printk(KERN_WARNING "SELinux:  rangetrans:  invalid range\n");
2183 2184 2185 2186 2187 2188 2189 2190
				kfree(rt);
				kfree(r);
				goto bad;
			}
			rc = hashtab_insert(p->range_tr, rt, r);
			if (rc) {
				kfree(rt);
				kfree(r);
2191 2192
				goto bad;
			}
L
Linus Torvalds 已提交
2193
		}
2194
		rangetr_hash_eval(p->range_tr);
L
Linus Torvalds 已提交
2195 2196
	}

2197
	p->type_attr_map = kmalloc(p->p_types.nprim * sizeof(struct ebitmap), GFP_KERNEL);
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
	if (!p->type_attr_map)
		goto bad;

	for (i = 0; i < p->p_types.nprim; i++) {
		ebitmap_init(&p->type_attr_map[i]);
		if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
			if (ebitmap_read(&p->type_attr_map[i], fp))
				goto bad;
		}
		/* add the type itself as the degenerate case */
		if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
				goto bad;
	}

2212 2213 2214 2215
	rc = policydb_bounds_sanity_check(p);
	if (rc)
		goto bad;

L
Linus Torvalds 已提交
2216 2217 2218 2219
	rc = 0;
out:
	return rc;
bad_newc:
2220
	ocontext_destroy(newc, OCON_FSUSE);
L
Linus Torvalds 已提交
2221 2222 2223 2224 2225 2226
bad:
	if (!rc)
		rc = -EINVAL;
	policydb_destroy(p);
	goto out;
}