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

/*
 * 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
 * Updated: Hewlett-Packard <paul@paul-moore.com>
17 18 19
 *
 *      Added support for the policy capability bitmap
 *
20 21 22 23 24
 * Update: Mellanox Techonologies
 *
 *	Added Infiniband support
 *
 * Copyright (C) 2016 Mellanox Techonologies
25
 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
L
Linus Torvalds 已提交
26 27 28
 * 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
29
 *	it under the terms of the GNU General Public License as published by
L
Linus Torvalds 已提交
30 31 32 33
 *	the Free Software Foundation, version 2.
 */

#include <linux/kernel.h>
34
#include <linux/sched.h>
L
Linus Torvalds 已提交
35 36 37
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
38
#include <linux/audit.h>
39
#include <linux/flex_array.h>
L
Linus Torvalds 已提交
40 41 42 43 44
#include "security.h"

#include "policydb.h"
#include "conditional.h"
#include "mls.h"
45
#include "services.h"
L
Linus Torvalds 已提交
46 47 48 49

#define _DEBUG_HASHES

#ifdef DEBUG_HASHES
50
static const char *symtab_name[SYM_NUM] = {
L
Linus Torvalds 已提交
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
	"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[] = {
	{
82 83
		.version	= POLICYDB_VERSION_BASE,
		.sym_num	= SYM_NUM - 3,
84
		.ocon_num	= OCON_NUM - 3,
L
Linus Torvalds 已提交
85 86
	},
	{
87 88
		.version	= POLICYDB_VERSION_BOOL,
		.sym_num	= SYM_NUM - 2,
89
		.ocon_num	= OCON_NUM - 3,
L
Linus Torvalds 已提交
90 91
	},
	{
92 93
		.version	= POLICYDB_VERSION_IPV6,
		.sym_num	= SYM_NUM - 2,
94
		.ocon_num	= OCON_NUM - 2,
L
Linus Torvalds 已提交
95 96
	},
	{
97 98
		.version	= POLICYDB_VERSION_NLCLASS,
		.sym_num	= SYM_NUM - 2,
99
		.ocon_num	= OCON_NUM - 2,
L
Linus Torvalds 已提交
100 101
	},
	{
102 103
		.version	= POLICYDB_VERSION_MLS,
		.sym_num	= SYM_NUM,
104
		.ocon_num	= OCON_NUM - 2,
L
Linus Torvalds 已提交
105
	},
106
	{
107 108
		.version	= POLICYDB_VERSION_AVTAB,
		.sym_num	= SYM_NUM,
109
		.ocon_num	= OCON_NUM - 2,
110
	},
111
	{
112 113
		.version	= POLICYDB_VERSION_RANGETRANS,
		.sym_num	= SYM_NUM,
114
		.ocon_num	= OCON_NUM - 2,
115
	},
116 117 118
	{
		.version	= POLICYDB_VERSION_POLCAP,
		.sym_num	= SYM_NUM,
119
		.ocon_num	= OCON_NUM - 2,
E
Eric Paris 已提交
120 121 122 123
	},
	{
		.version	= POLICYDB_VERSION_PERMISSIVE,
		.sym_num	= SYM_NUM,
124
		.ocon_num	= OCON_NUM - 2,
125 126 127 128
	},
	{
		.version	= POLICYDB_VERSION_BOUNDARY,
		.sym_num	= SYM_NUM,
129
		.ocon_num	= OCON_NUM - 2,
130
	},
131 132 133
	{
		.version	= POLICYDB_VERSION_FILENAME_TRANS,
		.sym_num	= SYM_NUM,
134
		.ocon_num	= OCON_NUM - 2,
135
	},
136 137 138
	{
		.version	= POLICYDB_VERSION_ROLETRANS,
		.sym_num	= SYM_NUM,
139
		.ocon_num	= OCON_NUM - 2,
140
	},
141 142 143
	{
		.version	= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
		.sym_num	= SYM_NUM,
144
		.ocon_num	= OCON_NUM - 2,
145
	},
146 147 148
	{
		.version	= POLICYDB_VERSION_DEFAULT_TYPE,
		.sym_num	= SYM_NUM,
149
		.ocon_num	= OCON_NUM - 2,
150
	},
151 152 153
	{
		.version	= POLICYDB_VERSION_CONSTRAINT_NAMES,
		.sym_num	= SYM_NUM,
154
		.ocon_num	= OCON_NUM - 2,
155
	},
156 157 158
	{
		.version	= POLICYDB_VERSION_XPERMS_IOCTL,
		.sym_num	= SYM_NUM,
159 160 161 162 163
		.ocon_num	= OCON_NUM - 2,
	},
	{
		.version	= POLICYDB_VERSION_INFINIBAND,
		.sym_num	= SYM_NUM,
164 165
		.ocon_num	= OCON_NUM,
	},
L
Linus Torvalds 已提交
166 167 168 169 170 171 172
};

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

173
	for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		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 已提交
191
	role = kzalloc(sizeof(*role), GFP_KERNEL);
192
	if (!role)
193
		return -ENOMEM;
194 195

	rc = -EINVAL;
L
Linus Torvalds 已提交
196
	role->value = ++p->p_roles.nprim;
197 198 199 200
	if (role->value != OBJECT_R_VAL)
		goto out;

	rc = -ENOMEM;
J
Julia Lawall 已提交
201
	key = kstrdup(OBJECT_R, GFP_KERNEL);
202 203 204
	if (!key)
		goto out;

L
Linus Torvalds 已提交
205 206
	rc = hashtab_insert(p->p_roles.table, key, role);
	if (rc)
207
		goto out;
L
Linus Torvalds 已提交
208

209 210
	return 0;
out:
L
Linus Torvalds 已提交
211 212
	kfree(key);
	kfree(role);
213
	return rc;
L
Linus Torvalds 已提交
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
static u32 filenametr_hash(struct hashtab *h, const void *k)
{
	const struct filename_trans *ft = k;
	unsigned long hash;
	unsigned int byte_num;
	unsigned char focus;

	hash = ft->stype ^ ft->ttype ^ ft->tclass;

	byte_num = 0;
	while ((focus = ft->name[byte_num++]))
		hash = partial_name_hash(focus, hash);
	return hash & (h->size - 1);
}

static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
{
	const struct filename_trans *ft1 = k1;
	const struct filename_trans *ft2 = k2;
	int v;

	v = ft1->stype - ft2->stype;
	if (v)
		return v;

	v = ft1->ttype - ft2->ttype;
	if (v)
		return v;

	v = ft1->tclass - ft2->tclass;
	if (v)
		return v;

	return strcmp(ft1->name, ft2->name);

}

253 254 255 256 257 258 259 260 261 262
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;
263 264 265 266 267 268 269 270 271 272 273 274 275
	int v;

	v = key1->source_type - key2->source_type;
	if (v)
		return v;

	v = key1->target_type - key2->target_type;
	if (v)
		return v;

	v = key1->target_class - key2->target_class;

	return v;
276 277
}

L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286 287 288 289
/*
 * 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)
290
			goto out;
L
Linus Torvalds 已提交
291 292 293 294
	}

	rc = avtab_init(&p->te_avtab);
	if (rc)
295
		goto out;
L
Linus Torvalds 已提交
296 297 298

	rc = roles_init(p);
	if (rc)
299
		goto out;
L
Linus Torvalds 已提交
300 301 302

	rc = cond_policydb_init(p);
	if (rc)
303
		goto out;
L
Linus Torvalds 已提交
304

305
	p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
306 307
	if (!p->filename_trans) {
		rc = -ENOMEM;
308
		goto out;
309
	}
310

311
	p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
312 313
	if (!p->range_tr) {
		rc = -ENOMEM;
314
		goto out;
315
	}
316

317
	ebitmap_init(&p->filename_trans_ttypes);
318
	ebitmap_init(&p->policycaps);
E
Eric Paris 已提交
319
	ebitmap_init(&p->permissive_map);
320

321
	return 0;
L
Linus Torvalds 已提交
322
out:
323 324
	hashtab_destroy(p->filename_trans);
	hashtab_destroy(p->range_tr);
L
Linus Torvalds 已提交
325 326
	for (i = 0; i < SYM_NUM; i++)
		hashtab_destroy(p->symtab[i].table);
327
	return rc;
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

/*
 * 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;
344
	struct flex_array *fa;
L
Linus Torvalds 已提交
345 346 347 348 349

	comdatum = datum;
	p = datap;
	if (!comdatum->value || comdatum->value > p->p_commons.nprim)
		return -EINVAL;
350 351 352 353 354

	fa = p->sym_val_to_name[SYM_COMMONS];
	if (flex_array_put_ptr(fa, comdatum->value - 1, key,
			       GFP_KERNEL | __GFP_ZERO))
		BUG();
L
Linus Torvalds 已提交
355 356 357 358 359 360 361
	return 0;
}

static int class_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct class_datum *cladatum;
362
	struct flex_array *fa;
L
Linus Torvalds 已提交
363 364 365 366 367

	cladatum = datum;
	p = datap;
	if (!cladatum->value || cladatum->value > p->p_classes.nprim)
		return -EINVAL;
368 369 370 371
	fa = p->sym_val_to_name[SYM_CLASSES];
	if (flex_array_put_ptr(fa, cladatum->value - 1, key,
			       GFP_KERNEL | __GFP_ZERO))
		BUG();
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379
	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;
380
	struct flex_array *fa;
L
Linus Torvalds 已提交
381 382 383

	role = datum;
	p = datap;
384 385 386
	if (!role->value
	    || role->value > p->p_roles.nprim
	    || role->bounds > p->p_roles.nprim)
L
Linus Torvalds 已提交
387
		return -EINVAL;
388 389 390 391 392

	fa = p->sym_val_to_name[SYM_ROLES];
	if (flex_array_put_ptr(fa, role->value - 1, key,
			       GFP_KERNEL | __GFP_ZERO))
		BUG();
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400
	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;
401
	struct flex_array *fa;
L
Linus Torvalds 已提交
402 403 404 405 406

	typdatum = datum;
	p = datap;

	if (typdatum->primary) {
407 408 409
		if (!typdatum->value
		    || typdatum->value > p->p_types.nprim
		    || typdatum->bounds > p->p_types.nprim)
L
Linus Torvalds 已提交
410
			return -EINVAL;
411 412 413 414 415 416 417
		fa = p->sym_val_to_name[SYM_TYPES];
		if (flex_array_put_ptr(fa, typdatum->value - 1, key,
				       GFP_KERNEL | __GFP_ZERO))
			BUG();

		fa = p->type_val_to_struct_array;
		if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
418 419
				       GFP_KERNEL | __GFP_ZERO))
			BUG();
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428
	}

	return 0;
}

static int user_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct user_datum *usrdatum;
429
	struct flex_array *fa;
L
Linus Torvalds 已提交
430 431 432

	usrdatum = datum;
	p = datap;
433 434 435
	if (!usrdatum->value
	    || usrdatum->value > p->p_users.nprim
	    || usrdatum->bounds > p->p_users.nprim)
L
Linus Torvalds 已提交
436
		return -EINVAL;
437 438 439 440 441

	fa = p->sym_val_to_name[SYM_USERS];
	if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
			       GFP_KERNEL | __GFP_ZERO))
		BUG();
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449
	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;
450
	struct flex_array *fa;
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458

	levdatum = datum;
	p = datap;

	if (!levdatum->isalias) {
		if (!levdatum->level->sens ||
		    levdatum->level->sens > p->p_levels.nprim)
			return -EINVAL;
459 460 461 462
		fa = p->sym_val_to_name[SYM_LEVELS];
		if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
				       GFP_KERNEL | __GFP_ZERO))
			BUG();
L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470 471
	}

	return 0;
}

static int cat_index(void *key, void *datum, void *datap)
{
	struct policydb *p;
	struct cat_datum *catdatum;
472
	struct flex_array *fa;
L
Linus Torvalds 已提交
473 474 475 476 477 478 479

	catdatum = datum;
	p = datap;

	if (!catdatum->isalias) {
		if (!catdatum->value || catdatum->value > p->p_cats.nprim)
			return -EINVAL;
480 481 482 483
		fa = p->sym_val_to_name[SYM_CATS];
		if (flex_array_put_ptr(fa, catdatum->value - 1, key,
				       GFP_KERNEL | __GFP_ZERO))
			BUG();
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	}

	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,
};

#ifdef DEBUG_HASHES
502
static void hash_eval(struct hashtab *h, const char *hash_name)
L
Linus Torvalds 已提交
503
{
504
	struct hashtab_info info;
L
Linus Torvalds 已提交
505

506
	hashtab_stat(h, &info);
507
	pr_debug("SELinux: %s:  %d entries and %d/%d buckets used, "
508 509
	       "longest chain length %d\n", hash_name, h->nel,
	       info.slots_used, h->size, info.max_chain_len);
L
Linus Torvalds 已提交
510
}
511

512
static void symtab_hash_eval(struct symtab *s)
513
{
514
	int i;
515

516 517
	for (i = 0; i < SYM_NUM; i++)
		hash_eval(s[i].table, symtab_name[i]);
518
}
519

520
#else
521
static inline void hash_eval(struct hashtab *h, char *hash_name)
522 523
{
}
L
Linus Torvalds 已提交
524 525 526 527 528 529 530 531
#endif

/*
 * Define the other val_to_name and val_to_struct arrays
 * in a policy database structure.
 *
 * Caller must clean up on failure.
 */
532
static int policydb_index(struct policydb *p)
L
Linus Torvalds 已提交
533
{
534
	int i, rc;
L
Linus Torvalds 已提交
535

536
	if (p->mls_enabled)
537 538 539 540 541 542 543
		pr_debug("SELinux:  %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
			 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
			 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
	else
		pr_debug("SELinux:  %d users, %d roles, %d types, %d bools\n",
			 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
			 p->p_bools.nprim);
L
Linus Torvalds 已提交
544

545 546
	pr_debug("SELinux:  %d classes, %d rules\n",
		 p->p_classes.nprim, p->te_avtab.nel);
L
Linus Torvalds 已提交
547 548 549 550 551 552

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

553 554 555
	p->class_val_to_struct = kcalloc(p->p_classes.nprim,
					 sizeof(*p->class_val_to_struct),
					 GFP_KERNEL);
556
	if (!p->class_val_to_struct)
557
		return -ENOMEM;
558

559 560 561
	p->role_val_to_struct = kcalloc(p->p_roles.nprim,
					sizeof(*p->role_val_to_struct),
					GFP_KERNEL);
562
	if (!p->role_val_to_struct)
563
		return -ENOMEM;
L
Linus Torvalds 已提交
564

565 566 567
	p->user_val_to_struct = kcalloc(p->p_users.nprim,
					sizeof(*p->user_val_to_struct),
					GFP_KERNEL);
568
	if (!p->user_val_to_struct)
569
		return -ENOMEM;
L
Linus Torvalds 已提交
570

571 572 573 574 575
	/* Yes, I want the sizeof the pointer, not the structure */
	p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
						       p->p_types.nprim,
						       GFP_KERNEL | __GFP_ZERO);
	if (!p->type_val_to_struct_array)
576
		return -ENOMEM;
577 578

	rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
579
				 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
580
	if (rc)
581 582
		goto out;

583 584
	rc = cond_init_bool_indexes(p);
	if (rc)
L
Linus Torvalds 已提交
585 586
		goto out;

587
	for (i = 0; i < SYM_NUM; i++) {
588 589 590
		p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
							 p->symtab[i].nprim,
							 GFP_KERNEL | __GFP_ZERO);
591
		if (!p->sym_val_to_name[i])
592
			return -ENOMEM;
593 594

		rc = flex_array_prealloc(p->sym_val_to_name[i],
595
					 0, p->symtab[i].nprim,
596 597 598 599
					 GFP_KERNEL | __GFP_ZERO);
		if (rc)
			goto out;

L
Linus Torvalds 已提交
600 601 602 603
		rc = hashtab_map(p->symtab[i].table, index_f[i], p);
		if (rc)
			goto out;
	}
604
	rc = 0;
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
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);
627 628 629 630 631
	if (datum) {
		comdatum = datum;
		hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
		hashtab_destroy(comdatum->permissions.table);
	}
L
Linus Torvalds 已提交
632 633 634 635
	kfree(datum);
	return 0;
}

636 637 638 639 640 641 642 643 644 645 646 647 648
static void constraint_expr_destroy(struct constraint_expr *expr)
{
	if (expr) {
		ebitmap_destroy(&expr->names);
		if (expr->type_names) {
			ebitmap_destroy(&expr->type_names->types);
			ebitmap_destroy(&expr->type_names->negset);
			kfree(expr->type_names);
		}
		kfree(expr);
	}
}

649
static int cls_destroy(void *key, void *datum, void *p)
L
Linus Torvalds 已提交
650 651 652 653 654 655
{
	struct class_datum *cladatum;
	struct constraint_node *constraint, *ctemp;
	struct constraint_expr *e, *etmp;

	kfree(key);
656 657 658 659 660 661 662 663 664 665
	if (datum) {
		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) {
				etmp = e;
				e = e->next;
666
				constraint_expr_destroy(etmp);
667 668 669 670
			}
			ctemp = constraint;
			constraint = constraint->next;
			kfree(ctemp);
L
Linus Torvalds 已提交
671
		}
672 673 674 675 676 677 678

		constraint = cladatum->validatetrans;
		while (constraint) {
			e = constraint->expr;
			while (e) {
				etmp = e;
				e = e->next;
679
				constraint_expr_destroy(etmp);
680 681 682 683
			}
			ctemp = constraint;
			constraint = constraint->next;
			kfree(ctemp);
L
Linus Torvalds 已提交
684
		}
685 686
		kfree(cladatum->comkey);
	}
L
Linus Torvalds 已提交
687 688 689 690 691 692 693 694 695
	kfree(datum);
	return 0;
}

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

	kfree(key);
696 697 698 699 700
	if (datum) {
		role = datum;
		ebitmap_destroy(&role->dominates);
		ebitmap_destroy(&role->types);
	}
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	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);
717 718 719 720 721 722 723
	if (datum) {
		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);
	}
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732
	kfree(datum);
	return 0;
}

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

	kfree(key);
733 734 735 736 737
	if (datum) {
		levdatum = datum;
		ebitmap_destroy(&levdatum->level->cat);
		kfree(levdatum->level);
	}
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751
	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,
752
	cls_destroy,
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760
	role_destroy,
	type_destroy,
	user_destroy,
	cond_destroy_bool,
	sens_destroy,
	cat_destroy,
};

761 762 763 764 765 766 767 768 769 770
static int filenametr_destroy(void *key, void *datum, void *p)
{
	struct filename_trans *ft = key;
	kfree(ft->name);
	kfree(key);
	kfree(datum);
	cond_resched();
	return 0;
}

771 772 773 774 775 776 777 778 779 780 781
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 已提交
782 783
static void ocontext_destroy(struct ocontext *c, int i)
{
784 785 786
	if (!c)
		return;

L
Linus Torvalds 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	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;
803 804
	struct role_allow *ra, *lra = NULL;
	struct role_trans *tr, *ltr = NULL;
L
Linus Torvalds 已提交
805 806

	for (i = 0; i < SYM_NUM; i++) {
807
		cond_resched();
L
Linus Torvalds 已提交
808 809 810 811
		hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
		hashtab_destroy(p->symtab[i].table);
	}

812 813 814 815
	for (i = 0; i < SYM_NUM; i++) {
		if (p->sym_val_to_name[i])
			flex_array_free(p->sym_val_to_name[i]);
	}
L
Linus Torvalds 已提交
816

J
Jesper Juhl 已提交
817 818 819
	kfree(p->class_val_to_struct);
	kfree(p->role_val_to_struct);
	kfree(p->user_val_to_struct);
820 821
	if (p->type_val_to_struct_array)
		flex_array_free(p->type_val_to_struct_array);
L
Linus Torvalds 已提交
822 823 824 825

	avtab_destroy(&p->te_avtab);

	for (i = 0; i < OCON_NUM; i++) {
826
		cond_resched();
L
Linus Torvalds 已提交
827 828 829 830
		c = p->ocontexts[i];
		while (c) {
			ctmp = c;
			c = c->next;
831
			ocontext_destroy(ctmp, i);
L
Linus Torvalds 已提交
832
		}
833
		p->ocontexts[i] = NULL;
L
Linus Torvalds 已提交
834 835 836 837
	}

	g = p->genfs;
	while (g) {
838
		cond_resched();
L
Linus Torvalds 已提交
839 840 841 842 843
		kfree(g->fstype);
		c = g->head;
		while (c) {
			ctmp = c;
			c = c->next;
844
			ocontext_destroy(ctmp, OCON_FSUSE);
L
Linus Torvalds 已提交
845 846 847 848 849
		}
		gtmp = g;
		g = g->next;
		kfree(gtmp);
	}
850
	p->genfs = NULL;
L
Linus Torvalds 已提交
851 852 853

	cond_policydb_destroy(p);

854
	for (tr = p->role_tr; tr; tr = tr->next) {
855
		cond_resched();
J
Jesper Juhl 已提交
856
		kfree(ltr);
857 858
		ltr = tr;
	}
J
Jesper Juhl 已提交
859
	kfree(ltr);
860

861
	for (ra = p->role_allow; ra; ra = ra->next) {
862
		cond_resched();
J
Jesper Juhl 已提交
863
		kfree(lra);
864 865
		lra = ra;
	}
J
Jesper Juhl 已提交
866
	kfree(lra);
867

868 869 870
	hashtab_map(p->filename_trans, filenametr_destroy, NULL);
	hashtab_destroy(p->filename_trans);

871 872
	hashtab_map(p->range_tr, range_tr_destroy, NULL);
	hashtab_destroy(p->range_tr);
873

874 875 876 877 878 879 880 881 882 883
	if (p->type_attr_map_array) {
		for (i = 0; i < p->p_types.nprim; i++) {
			struct ebitmap *e;

			e = flex_array_get(p->type_attr_map_array, i);
			if (!e)
				continue;
			ebitmap_destroy(e);
		}
		flex_array_free(p->type_attr_map_array);
884
	}
885

886
	ebitmap_destroy(&p->filename_trans_ttypes);
887
	ebitmap_destroy(&p->policycaps);
E
Eric Paris 已提交
888
	ebitmap_destroy(&p->permissive_map);
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901
}

/*
 * 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) {
902
		pr_err("SELinux:  out of memory on SID table init\n");
L
Linus Torvalds 已提交
903 904 905 906 907
		goto out;
	}

	head = p->ocontexts[OCON_ISID];
	for (c = head; c; c = c->next) {
908
		rc = -EINVAL;
L
Linus Torvalds 已提交
909
		if (!c->context[0].user) {
910
			pr_err("SELinux:  SID %s was never defined.\n",
911
				c->u.name);
L
Linus Torvalds 已提交
912 913
			goto out;
		}
914 915 916

		rc = sidtab_insert(s, c->sid[0], &c->context[0]);
		if (rc) {
917
			pr_err("SELinux:  unable to load initial SID %s.\n",
918
				c->u.name);
L
Linus Torvalds 已提交
919 920 921
			goto out;
		}
	}
922
	rc = 0;
L
Linus Torvalds 已提交
923 924 925 926
out:
	return rc;
}

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
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 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
/*
 * 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];
W
William Roberts 已提交
971
		if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
L
Linus Torvalds 已提交
972 973 974 975 976 977 978 979 980 981
			/* 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;

982
		if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
			/* 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)
{
999 1000
	__le32 buf[2];
	u32 items;
L
Linus Torvalds 已提交
1001 1002 1003
	int rc;

	rc = next_entry(buf, fp, sizeof(u32));
1004
	if (rc)
L
Linus Torvalds 已提交
1005 1006
		goto out;

1007
	rc = -EINVAL;
L
Linus Torvalds 已提交
1008 1009
	items = le32_to_cpu(buf[0]);
	if (items > ARRAY_SIZE(buf)) {
1010
		pr_err("SELinux: mls:  range overflow\n");
L
Linus Torvalds 已提交
1011 1012
		goto out;
	}
1013

L
Linus Torvalds 已提交
1014
	rc = next_entry(buf, fp, sizeof(u32) * items);
1015
	if (rc) {
1016
		pr_err("SELinux: mls:  truncated range\n");
L
Linus Torvalds 已提交
1017 1018
		goto out;
	}
1019

L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027
	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) {
1028
		pr_err("SELinux: mls:  error reading low categories\n");
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033
		goto out;
	}
	if (items > 1) {
		rc = ebitmap_read(&r->level[1].cat, fp);
		if (rc) {
1034
			pr_err("SELinux: mls:  error reading high categories\n");
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039
			goto bad_high;
		}
	} else {
		rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
		if (rc) {
1040
			pr_err("SELinux: mls:  out of memory\n");
L
Linus Torvalds 已提交
1041 1042 1043 1044
			goto bad_high;
		}
	}

1045
	return 0;
L
Linus Torvalds 已提交
1046 1047
bad_high:
	ebitmap_destroy(&r->level[0].cat);
1048 1049
out:
	return rc;
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
}

/*
 * 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)
{
1060
	__le32 buf[3];
L
Linus Torvalds 已提交
1061 1062 1063
	int rc;

	rc = next_entry(buf, fp, sizeof buf);
1064
	if (rc) {
1065
		pr_err("SELinux: context truncated\n");
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071
		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) {
1072 1073
		rc = mls_read_range_helper(&c->range, fp);
		if (rc) {
1074
			pr_err("SELinux: error reading MLS range of context\n");
L
Linus Torvalds 已提交
1075 1076 1077 1078
			goto out;
		}
	}

1079
	rc = -EINVAL;
L
Linus Torvalds 已提交
1080
	if (!policydb_context_isvalid(p, c)) {
1081
		pr_err("SELinux:  invalid security context\n");
L
Linus Torvalds 已提交
1082
		context_destroy(c);
1083
		goto out;
L
Linus Torvalds 已提交
1084
	}
1085
	rc = 0;
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
out:
	return rc;
}

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

1096 1097 1098 1099 1100
static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
{
	int rc;
	char *str;

1101 1102 1103
	if ((len == 0) || (len == (u32)-1))
		return -EINVAL;

1104
	str = kmalloc(len + 1, flags | __GFP_NOWARN);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	if (!str)
		return -ENOMEM;

	/* it's expected the caller should free the str */
	*strp = str;

	rc = next_entry(str, fp, len);
	if (rc)
		return rc;

	str[len] = '\0';
	return 0;
}

L
Linus Torvalds 已提交
1119 1120 1121 1122 1123
static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
{
	char *key = NULL;
	struct perm_datum *perdatum;
	int rc;
1124 1125
	__le32 buf[2];
	u32 len;
L
Linus Torvalds 已提交
1126

J
James Morris 已提交
1127
	perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1128
	if (!perdatum)
1129
		return -ENOMEM;
L
Linus Torvalds 已提交
1130 1131

	rc = next_entry(buf, fp, sizeof buf);
1132
	if (rc)
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137
		goto bad;

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

1138
	rc = str_read(&key, GFP_KERNEL, fp, len);
1139
	if (rc)
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144
		goto bad;

	rc = hashtab_insert(h, key, perdatum);
	if (rc)
		goto bad;
1145 1146

	return 0;
L
Linus Torvalds 已提交
1147 1148
bad:
	perm_destroy(key, perdatum, NULL);
1149
	return rc;
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155
}

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

J
James Morris 已提交
1160
	comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1161
	if (!comdatum)
1162
		return -ENOMEM;
L
Linus Torvalds 已提交
1163 1164

	rc = next_entry(buf, fp, sizeof buf);
1165
	if (rc)
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
		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]);

1177
	rc = str_read(&key, GFP_KERNEL, fp, len);
1178
	if (rc)
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
		goto bad;

	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;
1190
	return 0;
L
Linus Torvalds 已提交
1191 1192
bad:
	common_destroy(key, comdatum, NULL);
1193
	return rc;
L
Linus Torvalds 已提交
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 1220 1221 1222 1223
static void type_set_init(struct type_set *t)
{
	ebitmap_init(&t->types);
	ebitmap_init(&t->negset);
}

static int type_set_read(struct type_set *t, void *fp)
{
	__le32 buf[1];
	int rc;

	if (ebitmap_read(&t->types, fp))
		return -EINVAL;
	if (ebitmap_read(&t->negset, fp))
		return -EINVAL;

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0)
		return -EINVAL;
	t->flags = le32_to_cpu(buf[0]);

	return 0;
}


static int read_cons_helper(struct policydb *p,
				struct constraint_node **nodep,
				int ncons, int allowxtarget, void *fp)
L
Linus Torvalds 已提交
1224 1225 1226
{
	struct constraint_node *c, *lc;
	struct constraint_expr *e, *le;
1227 1228
	__le32 buf[3];
	u32 nexpr;
L
Linus Torvalds 已提交
1229 1230 1231 1232
	int rc, i, j, depth;

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

1237
		if (lc)
L
Linus Torvalds 已提交
1238
			lc->next = c;
1239
		else
L
Linus Torvalds 已提交
1240 1241 1242
			*nodep = c;

		rc = next_entry(buf, fp, (sizeof(u32) * 2));
1243
		if (rc)
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248 1249
			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 已提交
1250
			e = kzalloc(sizeof(*e), GFP_KERNEL);
L
Linus Torvalds 已提交
1251 1252 1253
			if (!e)
				return -ENOMEM;

1254
			if (le)
L
Linus Torvalds 已提交
1255
				le->next = e;
1256
			else
L
Linus Torvalds 已提交
1257 1258 1259
				c->expr = e;

			rc = next_entry(buf, fp, (sizeof(u32) * 3));
1260
			if (rc)
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
				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++;
1288 1289 1290
				rc = ebitmap_read(&e->names, fp);
				if (rc)
					return rc;
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
				if (p->policyvers >=
					POLICYDB_VERSION_CONSTRAINT_NAMES) {
						e->type_names = kzalloc(sizeof
						(*e->type_names),
						GFP_KERNEL);
					if (!e->type_names)
						return -ENOMEM;
					type_set_init(e->type_names);
					rc = type_set_read(e->type_names, fp);
					if (rc)
						return rc;
				}
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
				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;
1321 1322
	__le32 buf[6];
	u32 len, len2, ncons, nel;
L
Linus Torvalds 已提交
1323 1324
	int i, rc;

J
James Morris 已提交
1325
	cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1326
	if (!cladatum)
1327
		return -ENOMEM;
L
Linus Torvalds 已提交
1328 1329

	rc = next_entry(buf, fp, sizeof(u32)*6);
1330
	if (rc)
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
		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]);

1345
	rc = str_read(&key, GFP_KERNEL, fp, len);
1346
	if (rc)
L
Linus Torvalds 已提交
1347 1348 1349
		goto bad;

	if (len2) {
1350
		rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1351
		if (rc)
L
Linus Torvalds 已提交
1352 1353
			goto bad;

1354 1355
		rc = -EINVAL;
		cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
L
Linus Torvalds 已提交
1356
		if (!cladatum->comdatum) {
1357 1358
			pr_err("SELinux:  unknown common %s\n",
			       cladatum->comkey);
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367
			goto bad;
		}
	}
	for (i = 0; i < nel; i++) {
		rc = perm_read(p, cladatum->permissions.table, fp);
		if (rc)
			goto bad;
	}

1368
	rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374
	if (rc)
		goto bad;

	if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
		/* grab the validatetrans rules */
		rc = next_entry(buf, fp, sizeof(u32));
1375
		if (rc)
L
Linus Torvalds 已提交
1376 1377
			goto bad;
		ncons = le32_to_cpu(buf[0]);
1378 1379
		rc = read_cons_helper(p, &cladatum->validatetrans,
				ncons, 1, fp);
L
Linus Torvalds 已提交
1380 1381 1382 1383
		if (rc)
			goto bad;
	}

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
	if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
		rc = next_entry(buf, fp, sizeof(u32) * 3);
		if (rc)
			goto bad;

		cladatum->default_user = le32_to_cpu(buf[0]);
		cladatum->default_role = le32_to_cpu(buf[1]);
		cladatum->default_range = le32_to_cpu(buf[2]);
	}

1394 1395 1396 1397 1398 1399 1400
	if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
		rc = next_entry(buf, fp, sizeof(u32) * 1);
		if (rc)
			goto bad;
		cladatum->default_type = le32_to_cpu(buf[0]);
	}

L
Linus Torvalds 已提交
1401 1402 1403 1404
	rc = hashtab_insert(h, key, cladatum);
	if (rc)
		goto bad;

1405
	return 0;
L
Linus Torvalds 已提交
1406
bad:
1407
	cls_destroy(key, cladatum, NULL);
1408
	return rc;
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414
}

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

J
James Morris 已提交
1419
	role = kzalloc(sizeof(*role), GFP_KERNEL);
1420
	if (!role)
1421
		return -ENOMEM;
L
Linus Torvalds 已提交
1422

1423 1424 1425 1426
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 3;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1427
	if (rc)
L
Linus Torvalds 已提交
1428 1429 1430 1431
		goto bad;

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

1435
	rc = str_read(&key, GFP_KERNEL, fp, len);
1436
	if (rc)
L
Linus Torvalds 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
		goto bad;

	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) {
1448
		rc = -EINVAL;
L
Linus Torvalds 已提交
1449
		if (role->value != OBJECT_R_VAL) {
1450
			pr_err("SELinux: Role %s has wrong value %d\n",
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
			       OBJECT_R, role->value);
			goto bad;
		}
		rc = 0;
		goto bad;
	}

	rc = hashtab_insert(h, key, role);
	if (rc)
		goto bad;
1461
	return 0;
L
Linus Torvalds 已提交
1462 1463
bad:
	role_destroy(key, role, NULL);
1464
	return rc;
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470
}

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

1475
	typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1476
	if (!typdatum)
1477
		return -ENOMEM;
L
Linus Torvalds 已提交
1478

1479 1480 1481 1482
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 4;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1483
	if (rc)
L
Linus Torvalds 已提交
1484 1485 1486 1487
		goto bad;

	len = le32_to_cpu(buf[0]);
	typdatum->value = le32_to_cpu(buf[1]);
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	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 已提交
1500

1501
	rc = str_read(&key, GFP_KERNEL, fp, len);
1502
	if (rc)
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507
		goto bad;

	rc = hashtab_insert(h, key, typdatum);
	if (rc)
		goto bad;
1508
	return 0;
L
Linus Torvalds 已提交
1509 1510
bad:
	type_destroy(key, typdatum, NULL);
1511
	return rc;
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520
}


/*
 * Read a MLS level structure from a policydb binary
 * representation file.
 */
static int mls_read_level(struct mls_level *lp, void *fp)
{
1521
	__le32 buf[1];
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526
	int rc;

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

	rc = next_entry(buf, fp, sizeof buf);
1527
	if (rc) {
1528
		pr_err("SELinux: mls: truncated level\n");
1529
		return rc;
L
Linus Torvalds 已提交
1530 1531 1532
	}
	lp->sens = le32_to_cpu(buf[0]);

1533 1534
	rc = ebitmap_read(&lp->cat, fp);
	if (rc) {
1535
		pr_err("SELinux: mls:  error reading level categories\n");
1536
		return rc;
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542 1543 1544
	}
	return 0;
}

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

J
James Morris 已提交
1549
	usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1550
	if (!usrdatum)
1551
		return -ENOMEM;
L
Linus Torvalds 已提交
1552

1553 1554 1555 1556
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		to_read = 3;

	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1557
	if (rc)
L
Linus Torvalds 已提交
1558 1559 1560 1561
		goto bad;

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

1565
	rc = str_read(&key, GFP_KERNEL, fp, len);
1566
	if (rc)
L
Linus Torvalds 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
		goto bad;

	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;
1585
	return 0;
L
Linus Torvalds 已提交
1586 1587
bad:
	user_destroy(key, usrdatum, NULL);
1588
	return rc;
L
Linus Torvalds 已提交
1589 1590 1591 1592 1593 1594 1595
}

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

J
James Morris 已提交
1599
	levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1600
	if (!levdatum)
1601
		return -ENOMEM;
L
Linus Torvalds 已提交
1602 1603

	rc = next_entry(buf, fp, sizeof buf);
1604
	if (rc)
L
Linus Torvalds 已提交
1605 1606 1607 1608 1609
		goto bad;

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

1610
	rc = str_read(&key, GFP_ATOMIC, fp, len);
1611
	if (rc)
L
Linus Torvalds 已提交
1612 1613
		goto bad;

1614
	rc = -ENOMEM;
1615
	levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1616
	if (!levdatum->level)
L
Linus Torvalds 已提交
1617
		goto bad;
1618 1619 1620

	rc = mls_read_level(levdatum->level, fp);
	if (rc)
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625
		goto bad;

	rc = hashtab_insert(h, key, levdatum);
	if (rc)
		goto bad;
1626
	return 0;
L
Linus Torvalds 已提交
1627 1628
bad:
	sens_destroy(key, levdatum, NULL);
1629
	return rc;
L
Linus Torvalds 已提交
1630 1631 1632 1633 1634 1635 1636
}

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

J
James Morris 已提交
1640
	catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1641
	if (!catdatum)
1642
		return -ENOMEM;
L
Linus Torvalds 已提交
1643 1644

	rc = next_entry(buf, fp, sizeof buf);
1645
	if (rc)
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650 1651
		goto bad;

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

1652
	rc = str_read(&key, GFP_ATOMIC, fp, len);
1653
	if (rc)
L
Linus Torvalds 已提交
1654 1655 1656 1657 1658
		goto bad;

	rc = hashtab_insert(h, key, catdatum);
	if (rc)
		goto bad;
1659
	return 0;
L
Linus Torvalds 已提交
1660 1661
bad:
	cat_destroy(key, catdatum, NULL);
1662
	return rc;
L
Linus Torvalds 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
}

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,
};

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
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) {
1689
			pr_err("SELinux: user %s: "
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
			       "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;

1700
			pr_err("SELinux: boundary violated policy: "
1701
			       "user=%s role=%s bounds=%s\n",
1702 1703 1704
			       sym_name(p, SYM_USERS, user->value - 1),
			       sym_name(p, SYM_ROLES, bit),
			       sym_name(p, SYM_USERS, upper->value - 1));
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

			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) {
1725
			pr_err("SELinux: role %s: "
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
			       "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;

1736
			pr_err("SELinux: boundary violated policy: "
1737
			       "role=%s type=%s bounds=%s\n",
1738 1739 1740
			       sym_name(p, SYM_ROLES, role->value - 1),
			       sym_name(p, SYM_TYPES, bit),
			       sym_name(p, SYM_ROLES, upper->value - 1));
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

			return -EINVAL;
		}
	}

	return 0;
}

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

1755
	upper = datum;
1756 1757
	while (upper->bounds) {
		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1758
			pr_err("SELinux: type %s: "
1759 1760 1761 1762 1763
			       "too deep or looped boundary\n",
			       (char *) key);
			return -EINVAL;
		}

1764 1765 1766 1767
		upper = flex_array_get_ptr(p->type_val_to_struct_array,
					   upper->bounds - 1);
		BUG_ON(!upper);

1768
		if (upper->attribute) {
1769
			pr_err("SELinux: type %s: "
1770 1771
			       "bounded by attribute %s",
			       (char *) key,
1772
			       sym_name(p, SYM_TYPES, upper->value - 1));
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
			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;
}

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
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);
}

1839 1840
static int range_read(struct policydb *p, void *fp)
{
1841
	struct range_trans *rt = NULL;
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
	struct mls_range *r = NULL;
	int i, rc;
	__le32 buf[2];
	u32 nel;

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

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc)
1852
		return rc;
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891

	nel = le32_to_cpu(buf[0]);
	for (i = 0; i < nel; i++) {
		rc = -ENOMEM;
		rt = kzalloc(sizeof(*rt), GFP_KERNEL);
		if (!rt)
			goto out;

		rc = next_entry(buf, fp, (sizeof(u32) * 2));
		if (rc)
			goto out;

		rt->source_type = le32_to_cpu(buf[0]);
		rt->target_type = le32_to_cpu(buf[1]);
		if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
			rc = next_entry(buf, fp, sizeof(u32));
			if (rc)
				goto out;
			rt->target_class = le32_to_cpu(buf[0]);
		} else
			rt->target_class = p->process_class;

		rc = -EINVAL;
		if (!policydb_type_isvalid(p, rt->source_type) ||
		    !policydb_type_isvalid(p, rt->target_type) ||
		    !policydb_class_isvalid(p, rt->target_class))
			goto out;

		rc = -ENOMEM;
		r = kzalloc(sizeof(*r), GFP_KERNEL);
		if (!r)
			goto out;

		rc = mls_read_range_helper(r, fp);
		if (rc)
			goto out;

		rc = -EINVAL;
		if (!mls_range_isvalid(p, r)) {
1892
			pr_warn("SELinux:  rangetrans:  invalid range\n");
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
			goto out;
		}

		rc = hashtab_insert(p->range_tr, rt, r);
		if (rc)
			goto out;

		rt = NULL;
		r = NULL;
	}
1903
	hash_eval(p->range_tr, "rangetr");
1904 1905 1906 1907 1908 1909 1910
	rc = 0;
out:
	kfree(rt);
	kfree(r);
	return rc;
}

1911 1912
static int filename_trans_read(struct policydb *p, void *fp)
{
1913 1914
	struct filename_trans *ft;
	struct filename_trans_datum *otype;
1915
	char *name;
1916
	u32 nel, len;
1917 1918 1919 1920 1921 1922 1923 1924
	__le32 buf[4];
	int rc, i;

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

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc)
1925
		return rc;
1926 1927 1928
	nel = le32_to_cpu(buf[0]);

	for (i = 0; i < nel; i++) {
1929 1930 1931
		otype = NULL;
		name = NULL;

1932 1933 1934 1935 1936
		rc = -ENOMEM;
		ft = kzalloc(sizeof(*ft), GFP_KERNEL);
		if (!ft)
			goto out;

1937 1938 1939 1940
		rc = -ENOMEM;
		otype = kmalloc(sizeof(*otype), GFP_KERNEL);
		if (!otype)
			goto out;
1941 1942 1943 1944 1945 1946 1947 1948

		/* length of the path component string */
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc)
			goto out;
		len = le32_to_cpu(buf[0]);

		/* path component string */
1949
		rc = str_read(&name, GFP_KERNEL, fp, len);
1950 1951
		if (rc)
			goto out;
1952 1953

		ft->name = name;
1954 1955 1956 1957 1958 1959 1960 1961

		rc = next_entry(buf, fp, sizeof(u32) * 4);
		if (rc)
			goto out;

		ft->stype = le32_to_cpu(buf[0]);
		ft->ttype = le32_to_cpu(buf[1]);
		ft->tclass = le32_to_cpu(buf[2]);
1962 1963

		otype->otype = le32_to_cpu(buf[3]);
1964 1965 1966 1967

		rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
		if (rc)
			goto out;
1968

1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
		rc = hashtab_insert(p->filename_trans, ft, otype);
		if (rc) {
			/*
			 * Do not return -EEXIST to the caller, or the system
			 * will not boot.
			 */
			if (rc != -EEXIST)
				goto out;
			/* But free memory to avoid memory leak. */
			kfree(ft);
			kfree(name);
			kfree(otype);
		}
1982
	}
1983 1984
	hash_eval(p->filename_trans, "filenametr");
	return 0;
1985
out:
1986 1987 1988 1989
	kfree(ft);
	kfree(name);
	kfree(otype);

1990 1991 1992
	return rc;
}

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
static int genfs_read(struct policydb *p, void *fp)
{
	int i, j, rc;
	u32 nel, nel2, len, len2;
	__le32 buf[1];
	struct ocontext *l, *c;
	struct ocontext *newc = NULL;
	struct genfs *genfs_p, *genfs;
	struct genfs *newgenfs = NULL;

	rc = next_entry(buf, fp, sizeof(u32));
	if (rc)
2005
		return rc;
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	nel = le32_to_cpu(buf[0]);

	for (i = 0; i < nel; i++) {
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc)
			goto out;
		len = le32_to_cpu(buf[0]);

		rc = -ENOMEM;
		newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
		if (!newgenfs)
			goto out;

2019
		rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2020 2021 2022 2023 2024 2025 2026
		if (rc)
			goto out;

		for (genfs_p = NULL, genfs = p->genfs; genfs;
		     genfs_p = genfs, genfs = genfs->next) {
			rc = -EINVAL;
			if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2027
				pr_err("SELinux:  dup genfs fstype %s\n",
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
				       newgenfs->fstype);
				goto out;
			}
			if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
				break;
		}
		newgenfs->next = genfs;
		if (genfs_p)
			genfs_p->next = newgenfs;
		else
			p->genfs = newgenfs;
		genfs = newgenfs;
		newgenfs = NULL;

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

		nel2 = le32_to_cpu(buf[0]);
		for (j = 0; j < nel2; j++) {
			rc = next_entry(buf, fp, sizeof(u32));
			if (rc)
				goto out;
			len = le32_to_cpu(buf[0]);

			rc = -ENOMEM;
			newc = kzalloc(sizeof(*newc), GFP_KERNEL);
			if (!newc)
				goto out;

2058
			rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
			if (rc)
				goto out;

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

			newc->v.sclass = le32_to_cpu(buf[0]);
			rc = context_read_and_validate(&newc->context[0], p, fp);
			if (rc)
				goto out;

			for (l = NULL, c = genfs->head; c;
			     l = c, c = c->next) {
				rc = -EINVAL;
				if (!strcmp(newc->u.name, c->u.name) &&
				    (!c->v.sclass || !newc->v.sclass ||
				     newc->v.sclass == c->v.sclass)) {
2077
					pr_err("SELinux:  dup genfs entry (%s,%s)\n",
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
					       genfs->fstype, c->u.name);
					goto out;
				}
				len = strlen(newc->u.name);
				len2 = strlen(c->u.name);
				if (len > len2)
					break;
			}

			newc->next = c;
			if (l)
				l->next = newc;
			else
				genfs->head = newc;
			newc = NULL;
		}
	}
	rc = 0;
out:
2097
	if (newgenfs) {
2098
		kfree(newgenfs->fstype);
2099 2100
		kfree(newgenfs);
	}
2101 2102 2103 2104 2105
	ocontext_destroy(newc, OCON_FSUSE);

	return rc;
}

2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
			 void *fp)
{
	int i, j, rc;
	u32 nel, len;
	__le32 buf[3];
	struct ocontext *l, *c;
	u32 nodebuf[8];

	for (i = 0; i < info->ocon_num; i++) {
		rc = next_entry(buf, fp, sizeof(u32));
		if (rc)
			goto out;
		nel = le32_to_cpu(buf[0]);

		l = NULL;
		for (j = 0; j < nel; j++) {
			rc = -ENOMEM;
			c = kzalloc(sizeof(*c), GFP_KERNEL);
			if (!c)
				goto out;
			if (l)
				l->next = c;
			else
				p->ocontexts[i] = c;
			l = c;

			switch (i) {
			case OCON_ISID:
				rc = next_entry(buf, fp, sizeof(u32));
				if (rc)
					goto out;

				c->sid[0] = le32_to_cpu(buf[0]);
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto out;
				break;
			case OCON_FS:
			case OCON_NETIF:
				rc = next_entry(buf, fp, sizeof(u32));
				if (rc)
					goto out;
				len = le32_to_cpu(buf[0]);

2151
				rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
				if (rc)
					goto out;

				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto out;
				rc = context_read_and_validate(&c->context[1], p, fp);
				if (rc)
					goto out;
				break;
			case OCON_PORT:
				rc = next_entry(buf, fp, sizeof(u32)*3);
				if (rc)
					goto out;
				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 out;
				break;
			case OCON_NODE:
				rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
				if (rc)
					goto out;
				c->u.node.addr = nodebuf[0]; /* network order */
				c->u.node.mask = nodebuf[1]; /* network order */
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto out;
				break;
			case OCON_FSUSE:
				rc = next_entry(buf, fp, sizeof(u32)*2);
				if (rc)
					goto out;

				rc = -EINVAL;
				c->v.behavior = le32_to_cpu(buf[0]);
2190 2191 2192 2193
				/* Determined at runtime, not in policy DB. */
				if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
					goto out;
				if (c->v.behavior > SECURITY_FS_USE_MAX)
2194 2195 2196
					goto out;

				len = le32_to_cpu(buf[1]);
2197
				rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2198 2199
				if (rc)
					goto out;
2200

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto out;
				break;
			case OCON_NODE6: {
				int k;

				rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
				if (rc)
					goto out;
				for (k = 0; k < 4; k++)
					c->u.node6.addr[k] = nodebuf[k];
				for (k = 0; k < 4; k++)
					c->u.node6.mask[k] = nodebuf[k+4];
				rc = context_read_and_validate(&c->context[0], p, fp);
				if (rc)
					goto out;
				break;
			}
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
			case OCON_IBPKEY:
				rc = next_entry(nodebuf, fp, sizeof(u32) * 4);
				if (rc)
					goto out;

				c->u.ibpkey.subnet_prefix = be64_to_cpu(*((__be64 *)nodebuf));

				if (nodebuf[2] > 0xffff ||
				    nodebuf[3] > 0xffff) {
					rc = -EINVAL;
					goto out;
				}

				c->u.ibpkey.low_pkey = le32_to_cpu(nodebuf[2]);
				c->u.ibpkey.high_pkey = le32_to_cpu(nodebuf[3]);

				rc = context_read_and_validate(&c->context[0],
							       p,
							       fp);
				if (rc)
					goto out;
				break;
			case OCON_IBENDPORT:
				rc = next_entry(buf, fp, sizeof(u32) * 2);
				if (rc)
					goto out;
				len = le32_to_cpu(buf[0]);

				rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
				if (rc)
					goto out;

				if (buf[1] > 0xff || buf[1] == 0) {
					rc = -EINVAL;
					goto out;
				}

				c->u.ibendport.port = le32_to_cpu(buf[1]);

				rc = context_read_and_validate(&c->context[0],
							       p,
							       fp);
				if (rc)
					goto out;
				break;
2265 2266 2267 2268 2269 2270 2271 2272
			}
		}
	}
	rc = 0;
out:
	return rc;
}

L
Linus Torvalds 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281
/*
 * 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;
	int i, j, rc;
2282
	__le32 buf[4];
2283 2284
	u32 len, nprim, nel;

L
Linus Torvalds 已提交
2285 2286 2287 2288 2289
	char *policydb_str;
	struct policydb_compat_info *info;

	rc = policydb_init(p);
	if (rc)
2290
		return rc;
L
Linus Torvalds 已提交
2291 2292

	/* Read the magic number and string length. */
2293
	rc = next_entry(buf, fp, sizeof(u32) * 2);
2294
	if (rc)
L
Linus Torvalds 已提交
2295 2296
		goto bad;

2297
	rc = -EINVAL;
2298
	if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2299
		pr_err("SELinux:  policydb magic number 0x%x does "
L
Linus Torvalds 已提交
2300
		       "not match expected magic number 0x%x\n",
2301
		       le32_to_cpu(buf[0]), POLICYDB_MAGIC);
L
Linus Torvalds 已提交
2302 2303 2304
		goto bad;
	}

2305
	rc = -EINVAL;
2306
	len = le32_to_cpu(buf[1]);
L
Linus Torvalds 已提交
2307
	if (len != strlen(POLICYDB_STRING)) {
2308
		pr_err("SELinux:  policydb string length %d does not "
2309
		       "match expected length %zu\n",
L
Linus Torvalds 已提交
2310 2311 2312
		       len, strlen(POLICYDB_STRING));
		goto bad;
	}
2313 2314

	rc = -ENOMEM;
2315
	policydb_str = kmalloc(len + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
2316
	if (!policydb_str) {
2317
		pr_err("SELinux:  unable to allocate memory for policydb "
L
Linus Torvalds 已提交
2318 2319 2320
		       "string of length %d\n", len);
		goto bad;
	}
2321

L
Linus Torvalds 已提交
2322
	rc = next_entry(policydb_str, fp, len);
2323
	if (rc) {
2324
		pr_err("SELinux:  truncated policydb string identifier\n");
L
Linus Torvalds 已提交
2325 2326 2327
		kfree(policydb_str);
		goto bad;
	}
2328 2329

	rc = -EINVAL;
2330
	policydb_str[len] = '\0';
L
Linus Torvalds 已提交
2331
	if (strcmp(policydb_str, POLICYDB_STRING)) {
2332
		pr_err("SELinux:  policydb string %s does not match "
L
Linus Torvalds 已提交
2333 2334 2335 2336 2337 2338 2339 2340
		       "my string %s\n", policydb_str, POLICYDB_STRING);
		kfree(policydb_str);
		goto bad;
	}
	/* Done with policydb_str. */
	kfree(policydb_str);
	policydb_str = NULL;

2341
	/* Read the version and table sizes. */
L
Linus Torvalds 已提交
2342
	rc = next_entry(buf, fp, sizeof(u32)*4);
2343
	if (rc)
L
Linus Torvalds 已提交
2344 2345
		goto bad;

2346
	rc = -EINVAL;
2347
	p->policyvers = le32_to_cpu(buf[0]);
L
Linus Torvalds 已提交
2348 2349
	if (p->policyvers < POLICYDB_VERSION_MIN ||
	    p->policyvers > POLICYDB_VERSION_MAX) {
2350
		pr_err("SELinux:  policydb version %d does not match "
2351 2352 2353
		       "my version range %d-%d\n",
		       le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
		goto bad;
L
Linus Torvalds 已提交
2354 2355
	}

2356
	if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2357
		p->mls_enabled = 1;
L
Linus Torvalds 已提交
2358

2359
		rc = -EINVAL;
L
Linus Torvalds 已提交
2360
		if (p->policyvers < POLICYDB_VERSION_MLS) {
2361
			pr_err("SELinux: security policydb version %d "
E
Eric Paris 已提交
2362 2363
				"(MLS) not backwards compatible\n",
				p->policyvers);
L
Linus Torvalds 已提交
2364 2365 2366
			goto bad;
		}
	}
2367 2368
	p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
	p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
L
Linus Torvalds 已提交
2369

2370 2371 2372 2373 2374
	if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
		rc = ebitmap_read(&p->policycaps, fp);
		if (rc)
			goto bad;
	}
2375

2376 2377 2378 2379 2380
	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
		rc = ebitmap_read(&p->permissive_map, fp);
		if (rc)
			goto bad;
	}
E
Eric Paris 已提交
2381

2382
	rc = -EINVAL;
L
Linus Torvalds 已提交
2383 2384
	info = policydb_lookup_compat(p->policyvers);
	if (!info) {
2385
		pr_err("SELinux:  unable to find policy compat info "
L
Linus Torvalds 已提交
2386 2387 2388 2389
		       "for version %d\n", p->policyvers);
		goto bad;
	}

2390
	rc = -EINVAL;
2391 2392
	if (le32_to_cpu(buf[2]) != info->sym_num ||
		le32_to_cpu(buf[3]) != info->ocon_num) {
2393
		pr_err("SELinux:  policydb table sizes (%d,%d) do "
2394 2395
		       "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
			le32_to_cpu(buf[3]),
L
Linus Torvalds 已提交
2396 2397 2398 2399 2400 2401
		       info->sym_num, info->ocon_num);
		goto bad;
	}

	for (i = 0; i < info->sym_num; i++) {
		rc = next_entry(buf, fp, sizeof(u32)*2);
2402
		if (rc)
L
Linus Torvalds 已提交
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
			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;
	}

2415 2416 2417 2418 2419
	rc = -EINVAL;
	p->process_class = string_to_security_class(p, "process");
	if (!p->process_class)
		goto bad;

2420
	rc = avtab_read(&p->te_avtab, fp, p);
L
Linus Torvalds 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
	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));
2431
	if (rc)
L
Linus Torvalds 已提交
2432 2433 2434 2435
		goto bad;
	nel = le32_to_cpu(buf[0]);
	ltr = NULL;
	for (i = 0; i < nel; i++) {
2436
		rc = -ENOMEM;
J
James Morris 已提交
2437
		tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2438
		if (!tr)
L
Linus Torvalds 已提交
2439
			goto bad;
2440
		if (ltr)
L
Linus Torvalds 已提交
2441
			ltr->next = tr;
2442
		else
L
Linus Torvalds 已提交
2443 2444
			p->role_tr = tr;
		rc = next_entry(buf, fp, sizeof(u32)*3);
2445
		if (rc)
L
Linus Torvalds 已提交
2446
			goto bad;
2447 2448

		rc = -EINVAL;
L
Linus Torvalds 已提交
2449 2450 2451
		tr->role = le32_to_cpu(buf[0]);
		tr->type = le32_to_cpu(buf[1]);
		tr->new_role = le32_to_cpu(buf[2]);
2452 2453 2454 2455 2456 2457 2458 2459
		if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
			rc = next_entry(buf, fp, sizeof(u32));
			if (rc)
				goto bad;
			tr->tclass = le32_to_cpu(buf[0]);
		} else
			tr->tclass = p->process_class;

2460
		rc = -EINVAL;
2461 2462
		if (!policydb_role_isvalid(p, tr->role) ||
		    !policydb_type_isvalid(p, tr->type) ||
2463
		    !policydb_class_isvalid(p, tr->tclass) ||
2464
		    !policydb_role_isvalid(p, tr->new_role))
2465
			goto bad;
L
Linus Torvalds 已提交
2466 2467 2468 2469
		ltr = tr;
	}

	rc = next_entry(buf, fp, sizeof(u32));
2470
	if (rc)
L
Linus Torvalds 已提交
2471 2472 2473 2474
		goto bad;
	nel = le32_to_cpu(buf[0]);
	lra = NULL;
	for (i = 0; i < nel; i++) {
2475
		rc = -ENOMEM;
J
James Morris 已提交
2476
		ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2477
		if (!ra)
L
Linus Torvalds 已提交
2478
			goto bad;
2479
		if (lra)
L
Linus Torvalds 已提交
2480
			lra->next = ra;
2481
		else
L
Linus Torvalds 已提交
2482 2483
			p->role_allow = ra;
		rc = next_entry(buf, fp, sizeof(u32)*2);
2484
		if (rc)
L
Linus Torvalds 已提交
2485
			goto bad;
2486 2487

		rc = -EINVAL;
L
Linus Torvalds 已提交
2488 2489
		ra->role = le32_to_cpu(buf[0]);
		ra->new_role = le32_to_cpu(buf[1]);
2490
		if (!policydb_role_isvalid(p, ra->role) ||
2491
		    !policydb_role_isvalid(p, ra->new_role))
2492
			goto bad;
L
Linus Torvalds 已提交
2493 2494 2495
		lra = ra;
	}

2496 2497 2498 2499
	rc = filename_trans_read(p, fp);
	if (rc)
		goto bad;

2500
	rc = policydb_index(p);
L
Linus Torvalds 已提交
2501 2502 2503
	if (rc)
		goto bad;

2504 2505 2506
	rc = -EINVAL;
	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");
2507 2508 2509
	if (!p->process_trans_perms)
		goto bad;

2510 2511 2512
	rc = ocontext_read(p, info, fp);
	if (rc)
		goto bad;
L
Linus Torvalds 已提交
2513

2514 2515
	rc = genfs_read(p, fp);
	if (rc)
L
Linus Torvalds 已提交
2516 2517
		goto bad;

2518 2519 2520
	rc = range_read(p, fp);
	if (rc)
		goto bad;
L
Linus Torvalds 已提交
2521

2522 2523 2524 2525 2526 2527 2528 2529
	rc = -ENOMEM;
	p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
						  p->p_types.nprim,
						  GFP_KERNEL | __GFP_ZERO);
	if (!p->type_attr_map_array)
		goto bad;

	/* preallocate so we don't have to worry about the put ever failing */
2530
	rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2531 2532
				 GFP_KERNEL | __GFP_ZERO);
	if (rc)
2533 2534 2535
		goto bad;

	for (i = 0; i < p->p_types.nprim; i++) {
2536 2537 2538 2539
		struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);

		BUG_ON(!e);
		ebitmap_init(e);
2540
		if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2541 2542
			rc = ebitmap_read(e, fp);
			if (rc)
2543 2544 2545
				goto bad;
		}
		/* add the type itself as the degenerate case */
2546 2547 2548
		rc = ebitmap_set_bit(e, i, 1);
		if (rc)
			goto bad;
2549 2550
	}

2551 2552 2553 2554
	rc = policydb_bounds_sanity_check(p);
	if (rc)
		goto bad;

L
Linus Torvalds 已提交
2555 2556 2557 2558 2559 2560 2561
	rc = 0;
out:
	return rc;
bad:
	policydb_destroy(p);
	goto out;
}
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604

/*
 * Write a MLS level structure to a policydb binary
 * representation file.
 */
static int mls_write_level(struct mls_level *l, void *fp)
{
	__le32 buf[1];
	int rc;

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

	rc = ebitmap_write(&l->cat, fp);
	if (rc)
		return rc;

	return 0;
}

/*
 * Write a MLS range structure to a policydb binary
 * representation file.
 */
static int mls_write_range_helper(struct mls_range *r, void *fp)
{
	__le32 buf[3];
	size_t items;
	int rc, eq;

	eq = mls_level_eq(&r->level[1], &r->level[0]);

	if (eq)
		items = 2;
	else
		items = 3;
	buf[0] = cpu_to_le32(items-1);
	buf[1] = cpu_to_le32(r->level[0].sens);
	if (!eq)
		buf[2] = cpu_to_le32(r->level[1].sens);

H
Himangi Saraogi 已提交
2605
	BUG_ON(items > ARRAY_SIZE(buf));
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675

	rc = put_entry(buf, sizeof(u32), items, fp);
	if (rc)
		return rc;

	rc = ebitmap_write(&r->level[0].cat, fp);
	if (rc)
		return rc;
	if (!eq) {
		rc = ebitmap_write(&r->level[1].cat, fp);
		if (rc)
			return rc;
	}

	return 0;
}

static int sens_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct level_datum *levdatum = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	__le32 buf[2];
	size_t len;
	int rc;

	len = strlen(key);
	buf[0] = cpu_to_le32(len);
	buf[1] = cpu_to_le32(levdatum->isalias);
	rc = put_entry(buf, sizeof(u32), 2, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	rc = mls_write_level(levdatum->level, fp);
	if (rc)
		return rc;

	return 0;
}

static int cat_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct cat_datum *catdatum = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	__le32 buf[3];
	size_t len;
	int rc;

	len = strlen(key);
	buf[0] = cpu_to_le32(len);
	buf[1] = cpu_to_le32(catdatum->value);
	buf[2] = cpu_to_le32(catdatum->isalias);
	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;
}

2676
static int role_trans_write(struct policydb *p, void *fp)
2677
{
2678
	struct role_trans *r = p->role_tr;
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
	struct role_trans *tr;
	u32 buf[3];
	size_t nel;
	int rc;

	nel = 0;
	for (tr = r; tr; tr = tr->next)
		nel++;
	buf[0] = cpu_to_le32(nel);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;
	for (tr = r; tr; tr = tr->next) {
		buf[0] = cpu_to_le32(tr->role);
		buf[1] = cpu_to_le32(tr->type);
		buf[2] = cpu_to_le32(tr->new_role);
		rc = put_entry(buf, sizeof(u32), 3, fp);
		if (rc)
			return rc;
2698 2699 2700 2701 2702 2703
		if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
			buf[0] = cpu_to_le32(tr->tclass);
			rc = put_entry(buf, sizeof(u32), 1, fp);
			if (rc)
				return rc;
		}
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
	}

	return 0;
}

static int role_allow_write(struct role_allow *r, void *fp)
{
	struct role_allow *ra;
	u32 buf[2];
	size_t nel;
	int rc;

	nel = 0;
	for (ra = r; ra; ra = ra->next)
		nel++;
	buf[0] = cpu_to_le32(nel);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;
	for (ra = r; ra; ra = ra->next) {
		buf[0] = cpu_to_le32(ra->role);
		buf[1] = cpu_to_le32(ra->new_role);
		rc = put_entry(buf, sizeof(u32), 2, fp);
		if (rc)
			return rc;
	}
	return 0;
}

/*
 * Write a security context structure
 * to a policydb binary representation file.
 */
static int context_write(struct policydb *p, struct context *c,
			 void *fp)
{
	int rc;
	__le32 buf[3];

	buf[0] = cpu_to_le32(c->user);
	buf[1] = cpu_to_le32(c->role);
	buf[2] = cpu_to_le32(c->type);

	rc = put_entry(buf, sizeof(u32), 3, fp);
	if (rc)
		return rc;

	rc = mls_write_range_helper(&c->range, fp);
	if (rc)
		return rc;

	return 0;
}

/*
 * The following *_write functions are used to
 * write the symbol data to a policy database
 * binary representation file.
 */

static int perm_write(void *vkey, void *datum, void *fp)
{
	char *key = vkey;
	struct perm_datum *perdatum = datum;
	__le32 buf[2];
	size_t len;
	int rc;

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

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	return 0;
}

static int common_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct common_datum *comdatum = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	__le32 buf[4];
	size_t len;
	int rc;

	len = strlen(key);
	buf[0] = cpu_to_le32(len);
	buf[1] = cpu_to_le32(comdatum->value);
	buf[2] = cpu_to_le32(comdatum->permissions.nprim);
	buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
	rc = put_entry(buf, sizeof(u32), 4, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
	if (rc)
		return rc;

	return 0;
}

2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
static int type_set_write(struct type_set *t, void *fp)
{
	int rc;
	__le32 buf[1];

	if (ebitmap_write(&t->types, fp))
		return -EINVAL;
	if (ebitmap_write(&t->negset, fp))
		return -EINVAL;

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

	return 0;
}

2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
static int write_cons_helper(struct policydb *p, struct constraint_node *node,
			     void *fp)
{
	struct constraint_node *c;
	struct constraint_expr *e;
	__le32 buf[3];
	u32 nel;
	int rc;

	for (c = node; c; c = c->next) {
		nel = 0;
		for (e = c->expr; e; e = e->next)
			nel++;
		buf[0] = cpu_to_le32(c->permissions);
		buf[1] = cpu_to_le32(nel);
		rc = put_entry(buf, sizeof(u32), 2, fp);
		if (rc)
			return rc;
		for (e = c->expr; e; e = e->next) {
			buf[0] = cpu_to_le32(e->expr_type);
			buf[1] = cpu_to_le32(e->attr);
			buf[2] = cpu_to_le32(e->op);
			rc = put_entry(buf, sizeof(u32), 3, fp);
			if (rc)
				return rc;

			switch (e->expr_type) {
			case CEXPR_NAMES:
				rc = ebitmap_write(&e->names, fp);
				if (rc)
					return rc;
2865 2866 2867 2868 2869 2870
				if (p->policyvers >=
					POLICYDB_VERSION_CONSTRAINT_NAMES) {
					rc = type_set_write(e->type_names, fp);
					if (rc)
						return rc;
				}
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
				break;
			default:
				break;
			}
		}
	}

	return 0;
}

static int class_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct class_datum *cladatum = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	struct policydb *p = pd->p;
	struct constraint_node *c;
	__le32 buf[6];
	u32 ncons;
	size_t len, len2;
	int rc;

	len = strlen(key);
	if (cladatum->comkey)
		len2 = strlen(cladatum->comkey);
	else
		len2 = 0;

	ncons = 0;
	for (c = cladatum->constraints; c; c = c->next)
		ncons++;

	buf[0] = cpu_to_le32(len);
	buf[1] = cpu_to_le32(len2);
	buf[2] = cpu_to_le32(cladatum->value);
	buf[3] = cpu_to_le32(cladatum->permissions.nprim);
	if (cladatum->permissions.table)
		buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
	else
		buf[4] = 0;
	buf[5] = cpu_to_le32(ncons);
	rc = put_entry(buf, sizeof(u32), 6, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	if (cladatum->comkey) {
		rc = put_entry(cladatum->comkey, 1, len2, fp);
		if (rc)
			return rc;
	}

	rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
	if (rc)
		return rc;

	rc = write_cons_helper(p, cladatum->constraints, fp);
	if (rc)
		return rc;

	/* write out the validatetrans rule */
	ncons = 0;
	for (c = cladatum->validatetrans; c; c = c->next)
		ncons++;

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

	rc = write_cons_helper(p, cladatum->validatetrans, fp);
	if (rc)
		return rc;

2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
	if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
		buf[0] = cpu_to_le32(cladatum->default_user);
		buf[1] = cpu_to_le32(cladatum->default_role);
		buf[2] = cpu_to_le32(cladatum->default_range);

		rc = put_entry(buf, sizeof(uint32_t), 3, fp);
		if (rc)
			return rc;
	}

2959 2960 2961 2962 2963 2964 2965
	if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
		buf[0] = cpu_to_le32(cladatum->default_type);
		rc = put_entry(buf, sizeof(uint32_t), 1, fp);
		if (rc)
			return rc;
	}

2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
	return 0;
}

static int role_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct role_datum *role = datum;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	struct policydb *p = pd->p;
	__le32 buf[3];
	size_t items, len;
	int rc;

	len = strlen(key);
	items = 0;
	buf[items++] = cpu_to_le32(len);
	buf[items++] = cpu_to_le32(role->value);
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		buf[items++] = cpu_to_le32(role->bounds);

H
Himangi Saraogi 已提交
2987
	BUG_ON(items > ARRAY_SIZE(buf));
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036

	rc = put_entry(buf, sizeof(u32), items, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	rc = ebitmap_write(&role->dominates, fp);
	if (rc)
		return rc;

	rc = ebitmap_write(&role->types, fp);
	if (rc)
		return rc;

	return 0;
}

static int type_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct type_datum *typdatum = datum;
	struct policy_data *pd = ptr;
	struct policydb *p = pd->p;
	void *fp = pd->fp;
	__le32 buf[4];
	int rc;
	size_t items, len;

	len = strlen(key);
	items = 0;
	buf[items++] = cpu_to_le32(len);
	buf[items++] = cpu_to_le32(typdatum->value);
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
		u32 properties = 0;

		if (typdatum->primary)
			properties |= TYPEDATUM_PROPERTY_PRIMARY;

		if (typdatum->attribute)
			properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;

		buf[items++] = cpu_to_le32(properties);
		buf[items++] = cpu_to_le32(typdatum->bounds);
	} else {
		buf[items++] = cpu_to_le32(typdatum->primary);
	}
H
Himangi Saraogi 已提交
3037
	BUG_ON(items > ARRAY_SIZE(buf));
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
	rc = put_entry(buf, sizeof(u32), items, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	return 0;
}

static int user_write(void *vkey, void *datum, void *ptr)
{
	char *key = vkey;
	struct user_datum *usrdatum = datum;
	struct policy_data *pd = ptr;
	struct policydb *p = pd->p;
	void *fp = pd->fp;
	__le32 buf[3];
	size_t items, len;
	int rc;

	len = strlen(key);
	items = 0;
	buf[items++] = cpu_to_le32(len);
	buf[items++] = cpu_to_le32(usrdatum->value);
	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
		buf[items++] = cpu_to_le32(usrdatum->bounds);
H
Himangi Saraogi 已提交
3066
	BUG_ON(items > ARRAY_SIZE(buf));
3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
	rc = put_entry(buf, sizeof(u32), items, fp);
	if (rc)
		return rc;

	rc = put_entry(key, 1, len, fp);
	if (rc)
		return rc;

	rc = ebitmap_write(&usrdatum->roles, fp);
	if (rc)
		return rc;

	rc = mls_write_range_helper(&usrdatum->range, fp);
	if (rc)
		return rc;

	rc = mls_write_level(&usrdatum->dfltlevel, fp);
	if (rc)
		return rc;

	return 0;
}

static int (*write_f[SYM_NUM]) (void *key, void *datum,
				void *datap) =
{
	common_write,
	class_write,
	role_write,
	type_write,
	user_write,
	cond_write_bool,
	sens_write,
	cat_write,
};

static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
			  void *fp)
{
	unsigned int i, j, rc;
	size_t nel, len;
	__le32 buf[3];
	u32 nodebuf[8];
	struct ocontext *c;
	for (i = 0; i < info->ocon_num; i++) {
		nel = 0;
		for (c = p->ocontexts[i]; c; c = c->next)
			nel++;
		buf[0] = cpu_to_le32(nel);
		rc = put_entry(buf, sizeof(u32), 1, fp);
		if (rc)
			return rc;
		for (c = p->ocontexts[i]; c; c = c->next) {
			switch (i) {
			case OCON_ISID:
				buf[0] = cpu_to_le32(c->sid[0]);
				rc = put_entry(buf, sizeof(u32), 1, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
			case OCON_FS:
			case OCON_NETIF:
				len = strlen(c->u.name);
				buf[0] = cpu_to_le32(len);
				rc = put_entry(buf, sizeof(u32), 1, fp);
				if (rc)
					return rc;
				rc = put_entry(c->u.name, 1, len, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[1], fp);
				if (rc)
					return rc;
				break;
			case OCON_PORT:
				buf[0] = cpu_to_le32(c->u.port.protocol);
				buf[1] = cpu_to_le32(c->u.port.low_port);
				buf[2] = cpu_to_le32(c->u.port.high_port);
				rc = put_entry(buf, sizeof(u32), 3, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
			case OCON_NODE:
				nodebuf[0] = c->u.node.addr; /* network order */
				nodebuf[1] = c->u.node.mask; /* network order */
				rc = put_entry(nodebuf, sizeof(u32), 2, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
			case OCON_FSUSE:
				buf[0] = cpu_to_le32(c->v.behavior);
				len = strlen(c->u.name);
				buf[1] = cpu_to_le32(len);
				rc = put_entry(buf, sizeof(u32), 2, fp);
				if (rc)
					return rc;
				rc = put_entry(c->u.name, 1, len, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
			case OCON_NODE6:
				for (j = 0; j < 4; j++)
					nodebuf[j] = c->u.node6.addr[j]; /* network order */
				for (j = 0; j < 4; j++)
					nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
				rc = put_entry(nodebuf, sizeof(u32), 8, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
			case OCON_IBPKEY:
				*((__be64 *)nodebuf) = cpu_to_be64(c->u.ibpkey.subnet_prefix);

				nodebuf[2] = cpu_to_le32(c->u.ibpkey.low_pkey);
				nodebuf[3] = cpu_to_le32(c->u.ibpkey.high_pkey);

				rc = put_entry(nodebuf, sizeof(u32), 4, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
			case OCON_IBENDPORT:
				len = strlen(c->u.ibendport.dev_name);
				buf[0] = cpu_to_le32(len);
				buf[1] = cpu_to_le32(c->u.ibendport.port);
				rc = put_entry(buf, sizeof(u32), 2, fp);
				if (rc)
					return rc;
				rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
				if (rc)
					return rc;
				rc = context_write(p, &c->context[0], fp);
				if (rc)
					return rc;
				break;
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
			}
		}
	}
	return 0;
}

static int genfs_write(struct policydb *p, void *fp)
{
	struct genfs *genfs;
	struct ocontext *c;
	size_t len;
	__le32 buf[1];
	int rc;

	len = 0;
	for (genfs = p->genfs; genfs; genfs = genfs->next)
		len++;
	buf[0] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;
	for (genfs = p->genfs; genfs; genfs = genfs->next) {
		len = strlen(genfs->fstype);
		buf[0] = cpu_to_le32(len);
		rc = put_entry(buf, sizeof(u32), 1, fp);
		if (rc)
			return rc;
		rc = put_entry(genfs->fstype, 1, len, fp);
		if (rc)
			return rc;
		len = 0;
		for (c = genfs->head; c; c = c->next)
			len++;
		buf[0] = cpu_to_le32(len);
		rc = put_entry(buf, sizeof(u32), 1, fp);
		if (rc)
			return rc;
		for (c = genfs->head; c; c = c->next) {
			len = strlen(c->u.name);
			buf[0] = cpu_to_le32(len);
			rc = put_entry(buf, sizeof(u32), 1, fp);
			if (rc)
				return rc;
			rc = put_entry(c->u.name, 1, len, fp);
			if (rc)
				return rc;
			buf[0] = cpu_to_le32(c->v.sclass);
			rc = put_entry(buf, sizeof(u32), 1, fp);
			if (rc)
				return rc;
			rc = context_write(p, &c->context[0], fp);
			if (rc)
				return rc;
		}
	}
	return 0;
}

3279
static int hashtab_cnt(void *key, void *data, void *ptr)
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
{
	int *cnt = ptr;
	*cnt = *cnt + 1;

	return 0;
}

static int range_write_helper(void *key, void *data, void *ptr)
{
	__le32 buf[2];
	struct range_trans *rt = key;
	struct mls_range *r = data;
	struct policy_data *pd = ptr;
	void *fp = pd->fp;
	struct policydb *p = pd->p;
	int rc;

	buf[0] = cpu_to_le32(rt->source_type);
	buf[1] = cpu_to_le32(rt->target_type);
	rc = put_entry(buf, sizeof(u32), 2, fp);
	if (rc)
		return rc;
	if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
		buf[0] = cpu_to_le32(rt->target_class);
		rc = put_entry(buf, sizeof(u32), 1, fp);
		if (rc)
			return rc;
	}
	rc = mls_write_range_helper(r, fp);
	if (rc)
		return rc;

	return 0;
}

static int range_write(struct policydb *p, void *fp)
{
	__le32 buf[1];
3318
	int rc, nel;
3319 3320 3321 3322 3323 3324 3325
	struct policy_data pd;

	pd.p = p;
	pd.fp = fp;

	/* count the number of entries in the hashtab */
	nel = 0;
3326
	rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
	if (rc)
		return rc;

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

	/* actually write all of the entries */
	rc = hashtab_map(p->range_tr, range_write_helper, &pd);
	if (rc)
		return rc;

	return 0;
}

3343
static int filename_write_helper(void *key, void *data, void *ptr)
3344 3345
{
	__le32 buf[4];
3346 3347 3348
	struct filename_trans *ft = key;
	struct filename_trans_datum *otype = data;
	void *fp = ptr;
3349
	int rc;
3350
	u32 len;
3351

3352 3353
	len = strlen(ft->name);
	buf[0] = cpu_to_le32(len);
3354 3355 3356 3357
	rc = put_entry(buf, sizeof(u32), 1, fp);
	if (rc)
		return rc;

3358 3359 3360
	rc = put_entry(ft->name, sizeof(char), len, fp);
	if (rc)
		return rc;
3361

3362 3363 3364 3365
	buf[0] = cpu_to_le32(ft->stype);
	buf[1] = cpu_to_le32(ft->ttype);
	buf[2] = cpu_to_le32(ft->tclass);
	buf[3] = cpu_to_le32(otype->otype);
3366

3367 3368 3369
	rc = put_entry(buf, sizeof(u32), 4, fp);
	if (rc)
		return rc;
3370 3371 3372

	return 0;
}
3373 3374 3375 3376 3377 3378 3379

static int filename_trans_write(struct policydb *p, void *fp)
{
	u32 nel;
	__le32 buf[1];
	int rc;

3380 3381 3382
	if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
		return 0;

3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
	nel = 0;
	rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
	if (rc)
		return rc;

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

	rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
	if (rc)
		return rc;

	return 0;
}

3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
/*
 * Write the configuration data in a policy database
 * structure to a policy database binary representation
 * file.
 */
int policydb_write(struct policydb *p, void *fp)
{
	unsigned int i, num_syms;
	int rc;
	__le32 buf[4];
	u32 config;
	size_t len;
	struct policydb_compat_info *info;

	/*
	 * refuse to write policy older than compressed avtab
	 * to simplify the writer.  There are other tests dropped
	 * since we assume this throughout the writer code.  Be
	 * careful if you ever try to remove this restriction
	 */
	if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3421
		pr_err("SELinux: refusing to write policy version %d."
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
		       "  Because it is less than version %d\n", p->policyvers,
		       POLICYDB_VERSION_AVTAB);
		return -EINVAL;
	}

	config = 0;
	if (p->mls_enabled)
		config |= POLICYDB_CONFIG_MLS;

	if (p->reject_unknown)
		config |= REJECT_UNKNOWN;
	if (p->allow_unknown)
		config |= ALLOW_UNKNOWN;

	/* Write the magic number and string identifiers. */
	buf[0] = cpu_to_le32(POLICYDB_MAGIC);
	len = strlen(POLICYDB_STRING);
	buf[1] = cpu_to_le32(len);
	rc = put_entry(buf, sizeof(u32), 2, fp);
	if (rc)
		return rc;
	rc = put_entry(POLICYDB_STRING, 1, len, fp);
	if (rc)
		return rc;

	/* Write the version, config, and table sizes. */
	info = policydb_lookup_compat(p->policyvers);
	if (!info) {
3450
		pr_err("SELinux: compatibility lookup failed for policy "
3451
		    "version %d", p->policyvers);
3452
		return -EINVAL;
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501
	}

	buf[0] = cpu_to_le32(p->policyvers);
	buf[1] = cpu_to_le32(config);
	buf[2] = cpu_to_le32(info->sym_num);
	buf[3] = cpu_to_le32(info->ocon_num);

	rc = put_entry(buf, sizeof(u32), 4, fp);
	if (rc)
		return rc;

	if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
		rc = ebitmap_write(&p->policycaps, fp);
		if (rc)
			return rc;
	}

	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
		rc = ebitmap_write(&p->permissive_map, fp);
		if (rc)
			return rc;
	}

	num_syms = info->sym_num;
	for (i = 0; i < num_syms; i++) {
		struct policy_data pd;

		pd.fp = fp;
		pd.p = p;

		buf[0] = cpu_to_le32(p->symtab[i].nprim);
		buf[1] = cpu_to_le32(p->symtab[i].table->nel);

		rc = put_entry(buf, sizeof(u32), 2, fp);
		if (rc)
			return rc;
		rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
		if (rc)
			return rc;
	}

	rc = avtab_write(p, &p->te_avtab, fp);
	if (rc)
		return rc;

	rc = cond_write_list(p, p->cond_list, fp);
	if (rc)
		return rc;

3502
	rc = role_trans_write(p, fp);
3503 3504 3505 3506 3507 3508 3509
	if (rc)
		return rc;

	rc = role_allow_write(p->role_allow, fp);
	if (rc)
		return rc;

3510 3511 3512 3513
	rc = filename_trans_write(p, fp);
	if (rc)
		return rc;

3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
	rc = ocontext_write(p, info, fp);
	if (rc)
		return rc;

	rc = genfs_write(p, fp);
	if (rc)
		return rc;

	rc = range_write(p, fp);
	if (rc)
		return rc;

	for (i = 0; i < p->p_types.nprim; i++) {
		struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);

		BUG_ON(!e);
		rc = ebitmap_write(e, fp);
		if (rc)
			return rc;
	}

	return 0;
}