blob: 5b92c0219207521715e33b063baf59366d1aed91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the policy database.
3 *
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
6
7/*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 *
10 * Support for enhanced MLS infrastructure.
11 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
Eric Paris2ced3df2008-04-17 13:37:12 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Paul Moore3bb56b22008-01-29 08:38:19 -050016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for the policy capability bitmap
19 *
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
Eric Paris2ced3df2008-04-17 13:37:12 -040024 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the Free Software Foundation, version 2.
26 */
27
28#include <linux/kernel.h>
Eric Paris9dc99782007-06-04 17:41:22 -040029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/string.h>
32#include <linux/errno.h>
KaiGai Koheid9250de2008-08-28 16:35:57 +090033#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "security.h"
35
36#include "policydb.h"
37#include "conditional.h"
38#include "mls.h"
39
40#define _DEBUG_HASHES
41
42#ifdef DEBUG_HASHES
43static char *symtab_name[SYM_NUM] = {
44 "common prefixes",
45 "classes",
46 "roles",
47 "types",
48 "users",
49 "bools",
50 "levels",
51 "categories",
52};
53#endif
54
Eric Paris2ced3df2008-04-17 13:37:12 -040055int selinux_mls_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static unsigned int symtab_sizes[SYM_NUM] = {
58 2,
59 32,
60 16,
61 512,
62 128,
63 16,
64 16,
65 16,
66};
67
68struct policydb_compat_info {
69 int version;
70 int sym_num;
71 int ocon_num;
72};
73
74/* These need to be updated if SYM_NUM or OCON_NUM changes */
75static struct policydb_compat_info policydb_compat[] = {
76 {
Eric Paris2ced3df2008-04-17 13:37:12 -040077 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 },
81 {
Eric Paris2ced3df2008-04-17 13:37:12 -040082 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 },
86 {
Eric Paris2ced3df2008-04-17 13:37:12 -040087 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 },
91 {
Eric Paris2ced3df2008-04-17 13:37:12 -040092 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 },
96 {
Eric Paris2ced3df2008-04-17 13:37:12 -040097 .version = POLICYDB_VERSION_MLS,
98 .sym_num = SYM_NUM,
99 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 },
Stephen Smalley782ebb92005-09-03 15:55:16 -0700101 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400102 .version = POLICYDB_VERSION_AVTAB,
103 .sym_num = SYM_NUM,
104 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700105 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700106 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400107 .version = POLICYDB_VERSION_RANGETRANS,
108 .sym_num = SYM_NUM,
109 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700110 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500111 {
112 .version = POLICYDB_VERSION_POLCAP,
113 .sym_num = SYM_NUM,
114 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100115 },
116 {
117 .version = POLICYDB_VERSION_PERMISSIVE,
118 .sym_num = SYM_NUM,
119 .ocon_num = OCON_NUM,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900120 },
121 {
122 .version = POLICYDB_VERSION_BOUNDARY,
123 .sym_num = SYM_NUM,
124 .ocon_num = OCON_NUM,
125 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static struct policydb_compat_info *policydb_lookup_compat(int version)
129{
130 int i;
131 struct policydb_compat_info *info = NULL;
132
Tobias Klauser32725ad2006-01-06 00:11:23 -0800133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (policydb_compat[i].version == version) {
135 info = &policydb_compat[i];
136 break;
137 }
138 }
139 return info;
140}
141
142/*
143 * Initialize the role table.
144 */
145static int roles_init(struct policydb *p)
146{
147 char *key = NULL;
148 int rc;
149 struct role_datum *role;
150
James Morris89d155e2005-10-30 14:59:21 -0800151 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!role) {
153 rc = -ENOMEM;
154 goto out;
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 role->value = ++p->p_roles.nprim;
157 if (role->value != OBJECT_R_VAL) {
158 rc = -EINVAL;
159 goto out_free_role;
160 }
Eric Paris2ced3df2008-04-17 13:37:12 -0400161 key = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!key) {
163 rc = -ENOMEM;
164 goto out_free_role;
165 }
166 strcpy(key, OBJECT_R);
167 rc = hashtab_insert(p->p_roles.table, key, role);
168 if (rc)
169 goto out_free_key;
170out:
171 return rc;
172
173out_free_key:
174 kfree(key);
175out_free_role:
176 kfree(role);
177 goto out;
178}
179
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500180static u32 rangetr_hash(struct hashtab *h, const void *k)
181{
182 const struct range_trans *key = k;
183 return (key->source_type + (key->target_type << 3) +
184 (key->target_class << 5)) & (h->size - 1);
185}
186
187static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
188{
189 const struct range_trans *key1 = k1, *key2 = k2;
190 return (key1->source_type != key2->source_type ||
191 key1->target_type != key2->target_type ||
192 key1->target_class != key2->target_class);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * Initialize a policy database structure.
197 */
198static int policydb_init(struct policydb *p)
199{
200 int i, rc;
201
202 memset(p, 0, sizeof(*p));
203
204 for (i = 0; i < SYM_NUM; i++) {
205 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
206 if (rc)
207 goto out_free_symtab;
208 }
209
210 rc = avtab_init(&p->te_avtab);
211 if (rc)
212 goto out_free_symtab;
213
214 rc = roles_init(p);
215 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900216 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 rc = cond_policydb_init(p);
219 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900220 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500222 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
223 if (!p->range_tr)
224 goto out_free_symtab;
225
Paul Moore3bb56b22008-01-29 08:38:19 -0500226 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100227 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out:
230 return rc;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out_free_symtab:
233 for (i = 0; i < SYM_NUM; i++)
234 hashtab_destroy(p->symtab[i].table);
235 goto out;
236}
237
238/*
239 * The following *_index functions are used to
240 * define the val_to_name and val_to_struct arrays
241 * in a policy database structure. The val_to_name
242 * arrays are used when converting security context
243 * structures into string representations. The
244 * val_to_struct arrays are used when the attributes
245 * of a class, role, or user are needed.
246 */
247
248static int common_index(void *key, void *datum, void *datap)
249{
250 struct policydb *p;
251 struct common_datum *comdatum;
252
253 comdatum = datum;
254 p = datap;
255 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
256 return -EINVAL;
257 p->p_common_val_to_name[comdatum->value - 1] = key;
258 return 0;
259}
260
261static int class_index(void *key, void *datum, void *datap)
262{
263 struct policydb *p;
264 struct class_datum *cladatum;
265
266 cladatum = datum;
267 p = datap;
268 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
269 return -EINVAL;
270 p->p_class_val_to_name[cladatum->value - 1] = key;
271 p->class_val_to_struct[cladatum->value - 1] = cladatum;
272 return 0;
273}
274
275static int role_index(void *key, void *datum, void *datap)
276{
277 struct policydb *p;
278 struct role_datum *role;
279
280 role = datum;
281 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900282 if (!role->value
283 || role->value > p->p_roles.nprim
284 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EINVAL;
286 p->p_role_val_to_name[role->value - 1] = key;
287 p->role_val_to_struct[role->value - 1] = role;
288 return 0;
289}
290
291static int type_index(void *key, void *datum, void *datap)
292{
293 struct policydb *p;
294 struct type_datum *typdatum;
295
296 typdatum = datum;
297 p = datap;
298
299 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900300 if (!typdatum->value
301 || typdatum->value > p->p_types.nprim
302 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -EINVAL;
304 p->p_type_val_to_name[typdatum->value - 1] = key;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900305 p->type_val_to_struct[typdatum->value - 1] = typdatum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 return 0;
309}
310
311static int user_index(void *key, void *datum, void *datap)
312{
313 struct policydb *p;
314 struct user_datum *usrdatum;
315
316 usrdatum = datum;
317 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900318 if (!usrdatum->value
319 || usrdatum->value > p->p_users.nprim
320 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322 p->p_user_val_to_name[usrdatum->value - 1] = key;
323 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
324 return 0;
325}
326
327static int sens_index(void *key, void *datum, void *datap)
328{
329 struct policydb *p;
330 struct level_datum *levdatum;
331
332 levdatum = datum;
333 p = datap;
334
335 if (!levdatum->isalias) {
336 if (!levdatum->level->sens ||
337 levdatum->level->sens > p->p_levels.nprim)
338 return -EINVAL;
339 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
340 }
341
342 return 0;
343}
344
345static int cat_index(void *key, void *datum, void *datap)
346{
347 struct policydb *p;
348 struct cat_datum *catdatum;
349
350 catdatum = datum;
351 p = datap;
352
353 if (!catdatum->isalias) {
354 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
355 return -EINVAL;
356 p->p_cat_val_to_name[catdatum->value - 1] = key;
357 }
358
359 return 0;
360}
361
362static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
363{
364 common_index,
365 class_index,
366 role_index,
367 type_index,
368 user_index,
369 cond_index_bool,
370 sens_index,
371 cat_index,
372};
373
374/*
375 * Define the common val_to_name array and the class
376 * val_to_name and val_to_struct arrays in a policy
377 * database structure.
378 *
379 * Caller must clean up upon failure.
380 */
381static int policydb_index_classes(struct policydb *p)
382{
383 int rc;
384
385 p->p_common_val_to_name =
386 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
387 if (!p->p_common_val_to_name) {
388 rc = -ENOMEM;
389 goto out;
390 }
391
392 rc = hashtab_map(p->p_commons.table, common_index, p);
393 if (rc)
394 goto out;
395
396 p->class_val_to_struct =
397 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
398 if (!p->class_val_to_struct) {
399 rc = -ENOMEM;
400 goto out;
401 }
402
403 p->p_class_val_to_name =
404 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
405 if (!p->p_class_val_to_name) {
406 rc = -ENOMEM;
407 goto out;
408 }
409
410 rc = hashtab_map(p->p_classes.table, class_index, p);
411out:
412 return rc;
413}
414
415#ifdef DEBUG_HASHES
416static void symtab_hash_eval(struct symtab *s)
417{
418 int i;
419
420 for (i = 0; i < SYM_NUM; i++) {
421 struct hashtab *h = s[i].table;
422 struct hashtab_info info;
423
424 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400425 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 "longest chain length %d\n", symtab_name[i], h->nel,
427 info.slots_used, h->size, info.max_chain_len);
428 }
429}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500430
431static void rangetr_hash_eval(struct hashtab *h)
432{
433 struct hashtab_info info;
434
435 hashtab_stat(h, &info);
436 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
437 "longest chain length %d\n", h->nel,
438 info.slots_used, h->size, info.max_chain_len);
439}
440#else
441static inline void rangetr_hash_eval(struct hashtab *h)
442{
443}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445
446/*
447 * Define the other val_to_name and val_to_struct arrays
448 * in a policy database structure.
449 *
450 * Caller must clean up on failure.
451 */
452static int policydb_index_others(struct policydb *p)
453{
454 int i, rc = 0;
455
James Morris454d9722008-02-26 20:42:02 +1100456 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
458 if (selinux_mls_enabled)
459 printk(", %d sens, %d cats", p->p_levels.nprim,
460 p->p_cats.nprim);
461 printk("\n");
462
James Morris454d9722008-02-26 20:42:02 +1100463 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 p->p_classes.nprim, p->te_avtab.nel);
465
466#ifdef DEBUG_HASHES
467 avtab_hash_eval(&p->te_avtab, "rules");
468 symtab_hash_eval(p->symtab);
469#endif
470
471 p->role_val_to_struct =
472 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400473 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!p->role_val_to_struct) {
475 rc = -ENOMEM;
476 goto out;
477 }
478
479 p->user_val_to_struct =
480 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400481 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!p->user_val_to_struct) {
483 rc = -ENOMEM;
484 goto out;
485 }
486
KaiGai Koheid9250de2008-08-28 16:35:57 +0900487 p->type_val_to_struct =
488 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
489 GFP_KERNEL);
490 if (!p->type_val_to_struct) {
491 rc = -ENOMEM;
492 goto out;
493 }
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (cond_init_bool_indexes(p)) {
496 rc = -ENOMEM;
497 goto out;
498 }
499
500 for (i = SYM_ROLES; i < SYM_NUM; i++) {
501 p->sym_val_to_name[i] =
502 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
503 if (!p->sym_val_to_name[i]) {
504 rc = -ENOMEM;
505 goto out;
506 }
507 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
508 if (rc)
509 goto out;
510 }
511
512out:
513 return rc;
514}
515
516/*
517 * The following *_destroy functions are used to
518 * free any memory allocated for each kind of
519 * symbol data in the policy database.
520 */
521
522static int perm_destroy(void *key, void *datum, void *p)
523{
524 kfree(key);
525 kfree(datum);
526 return 0;
527}
528
529static int common_destroy(void *key, void *datum, void *p)
530{
531 struct common_datum *comdatum;
532
533 kfree(key);
534 comdatum = datum;
535 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
536 hashtab_destroy(comdatum->permissions.table);
537 kfree(datum);
538 return 0;
539}
540
James Morris6cbda6b2006-11-29 16:50:27 -0500541static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct class_datum *cladatum;
544 struct constraint_node *constraint, *ctemp;
545 struct constraint_expr *e, *etmp;
546
547 kfree(key);
548 cladatum = datum;
549 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
550 hashtab_destroy(cladatum->permissions.table);
551 constraint = cladatum->constraints;
552 while (constraint) {
553 e = constraint->expr;
554 while (e) {
555 ebitmap_destroy(&e->names);
556 etmp = e;
557 e = e->next;
558 kfree(etmp);
559 }
560 ctemp = constraint;
561 constraint = constraint->next;
562 kfree(ctemp);
563 }
564
565 constraint = cladatum->validatetrans;
566 while (constraint) {
567 e = constraint->expr;
568 while (e) {
569 ebitmap_destroy(&e->names);
570 etmp = e;
571 e = e->next;
572 kfree(etmp);
573 }
574 ctemp = constraint;
575 constraint = constraint->next;
576 kfree(ctemp);
577 }
578
579 kfree(cladatum->comkey);
580 kfree(datum);
581 return 0;
582}
583
584static int role_destroy(void *key, void *datum, void *p)
585{
586 struct role_datum *role;
587
588 kfree(key);
589 role = datum;
590 ebitmap_destroy(&role->dominates);
591 ebitmap_destroy(&role->types);
592 kfree(datum);
593 return 0;
594}
595
596static int type_destroy(void *key, void *datum, void *p)
597{
598 kfree(key);
599 kfree(datum);
600 return 0;
601}
602
603static int user_destroy(void *key, void *datum, void *p)
604{
605 struct user_datum *usrdatum;
606
607 kfree(key);
608 usrdatum = datum;
609 ebitmap_destroy(&usrdatum->roles);
610 ebitmap_destroy(&usrdatum->range.level[0].cat);
611 ebitmap_destroy(&usrdatum->range.level[1].cat);
612 ebitmap_destroy(&usrdatum->dfltlevel.cat);
613 kfree(datum);
614 return 0;
615}
616
617static int sens_destroy(void *key, void *datum, void *p)
618{
619 struct level_datum *levdatum;
620
621 kfree(key);
622 levdatum = datum;
623 ebitmap_destroy(&levdatum->level->cat);
624 kfree(levdatum->level);
625 kfree(datum);
626 return 0;
627}
628
629static int cat_destroy(void *key, void *datum, void *p)
630{
631 kfree(key);
632 kfree(datum);
633 return 0;
634}
635
636static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
637{
638 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500639 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 role_destroy,
641 type_destroy,
642 user_destroy,
643 cond_destroy_bool,
644 sens_destroy,
645 cat_destroy,
646};
647
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500648static int range_tr_destroy(void *key, void *datum, void *p)
649{
650 struct mls_range *rt = datum;
651 kfree(key);
652 ebitmap_destroy(&rt->level[0].cat);
653 ebitmap_destroy(&rt->level[1].cat);
654 kfree(datum);
655 cond_resched();
656 return 0;
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static void ocontext_destroy(struct ocontext *c, int i)
660{
661 context_destroy(&c->context[0]);
662 context_destroy(&c->context[1]);
663 if (i == OCON_ISID || i == OCON_FS ||
664 i == OCON_NETIF || i == OCON_FSUSE)
665 kfree(c->u.name);
666 kfree(c);
667}
668
669/*
670 * Free any memory allocated by a policy database structure.
671 */
672void policydb_destroy(struct policydb *p)
673{
674 struct ocontext *c, *ctmp;
675 struct genfs *g, *gtmp;
676 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700677 struct role_allow *ra, *lra = NULL;
678 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400681 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
683 hashtab_destroy(p->symtab[i].table);
684 }
685
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700686 for (i = 0; i < SYM_NUM; i++)
687 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700689 kfree(p->class_val_to_struct);
690 kfree(p->role_val_to_struct);
691 kfree(p->user_val_to_struct);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900692 kfree(p->type_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 avtab_destroy(&p->te_avtab);
695
696 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400697 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 c = p->ocontexts[i];
699 while (c) {
700 ctmp = c;
701 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400702 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400704 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 g = p->genfs;
708 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400709 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 kfree(g->fstype);
711 c = g->head;
712 while (c) {
713 ctmp = c;
714 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400715 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 gtmp = g;
718 g = g->next;
719 kfree(gtmp);
720 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400721 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 cond_policydb_destroy(p);
724
Stephen Smalley782ebb92005-09-03 15:55:16 -0700725 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400726 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800727 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700728 ltr = tr;
729 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800730 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700731
Eric Paris2ced3df2008-04-17 13:37:12 -0400732 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400733 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800734 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700735 lra = ra;
736 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800737 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700738
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500739 hashtab_map(p->range_tr, range_tr_destroy, NULL);
740 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700741
Stephen Smalley282c1f52005-10-23 12:57:15 -0700742 if (p->type_attr_map) {
743 for (i = 0; i < p->p_types.nprim; i++)
744 ebitmap_destroy(&p->type_attr_map[i]);
745 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700746 kfree(p->type_attr_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500747 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100748 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751}
752
753/*
754 * Load the initial SIDs specified in a policy database
755 * structure into a SID table.
756 */
757int policydb_load_isids(struct policydb *p, struct sidtab *s)
758{
759 struct ocontext *head, *c;
760 int rc;
761
762 rc = sidtab_init(s);
763 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100764 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 }
767
768 head = p->ocontexts[OCON_ISID];
769 for (c = head; c; c = c->next) {
770 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100771 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 "defined.\n", c->u.name);
773 rc = -EINVAL;
774 goto out;
775 }
776 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100777 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 "SID %s.\n", c->u.name);
779 rc = -EINVAL;
780 goto out;
781 }
782 }
783out:
784 return rc;
785}
786
Stephen Smalley45e54212007-11-07 10:08:00 -0500787int policydb_class_isvalid(struct policydb *p, unsigned int class)
788{
789 if (!class || class > p->p_classes.nprim)
790 return 0;
791 return 1;
792}
793
794int policydb_role_isvalid(struct policydb *p, unsigned int role)
795{
796 if (!role || role > p->p_roles.nprim)
797 return 0;
798 return 1;
799}
800
801int policydb_type_isvalid(struct policydb *p, unsigned int type)
802{
803 if (!type || type > p->p_types.nprim)
804 return 0;
805 return 1;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * Return 1 if the fields in the security context
810 * structure `c' are valid. Return 0 otherwise.
811 */
812int policydb_context_isvalid(struct policydb *p, struct context *c)
813{
814 struct role_datum *role;
815 struct user_datum *usrdatum;
816
817 if (!c->role || c->role > p->p_roles.nprim)
818 return 0;
819
820 if (!c->user || c->user > p->p_users.nprim)
821 return 0;
822
823 if (!c->type || c->type > p->p_types.nprim)
824 return 0;
825
826 if (c->role != OBJECT_R_VAL) {
827 /*
828 * Role must be authorized for the type.
829 */
830 role = p->role_val_to_struct[c->role - 1];
831 if (!ebitmap_get_bit(&role->types,
832 c->type - 1))
833 /* role may not be associated with type */
834 return 0;
835
836 /*
837 * User must be authorized for the role.
838 */
839 usrdatum = p->user_val_to_struct[c->user - 1];
840 if (!usrdatum)
841 return 0;
842
843 if (!ebitmap_get_bit(&usrdatum->roles,
844 c->role - 1))
845 /* user may not be associated with role */
846 return 0;
847 }
848
849 if (!mls_context_isvalid(p, c))
850 return 0;
851
852 return 1;
853}
854
855/*
856 * Read a MLS range structure from a policydb binary
857 * representation file.
858 */
859static int mls_read_range_helper(struct mls_range *r, void *fp)
860{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700861 __le32 buf[2];
862 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int rc;
864
865 rc = next_entry(buf, fp, sizeof(u32));
866 if (rc < 0)
867 goto out;
868
869 items = le32_to_cpu(buf[0]);
870 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100871 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rc = -EINVAL;
873 goto out;
874 }
875 rc = next_entry(buf, fp, sizeof(u32) * items);
876 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100877 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto out;
879 }
880 r->level[0].sens = le32_to_cpu(buf[0]);
881 if (items > 1)
882 r->level[1].sens = le32_to_cpu(buf[1]);
883 else
884 r->level[1].sens = r->level[0].sens;
885
886 rc = ebitmap_read(&r->level[0].cat, fp);
887 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100888 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 "categories\n");
890 goto out;
891 }
892 if (items > 1) {
893 rc = ebitmap_read(&r->level[1].cat, fp);
894 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100895 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 "categories\n");
897 goto bad_high;
898 }
899 } else {
900 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
901 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100902 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto bad_high;
904 }
905 }
906
907 rc = 0;
908out:
909 return rc;
910bad_high:
911 ebitmap_destroy(&r->level[0].cat);
912 goto out;
913}
914
915/*
916 * Read and validate a security context structure
917 * from a policydb binary representation file.
918 */
919static int context_read_and_validate(struct context *c,
920 struct policydb *p,
921 void *fp)
922{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700923 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int rc;
925
926 rc = next_entry(buf, fp, sizeof buf);
927 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100928 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930 }
931 c->user = le32_to_cpu(buf[0]);
932 c->role = le32_to_cpu(buf[1]);
933 c->type = le32_to_cpu(buf[2]);
934 if (p->policyvers >= POLICYDB_VERSION_MLS) {
935 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100936 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 "context\n");
938 rc = -EINVAL;
939 goto out;
940 }
941 }
942
943 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100944 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 context_destroy(c);
946 rc = -EINVAL;
947 }
948out:
949 return rc;
950}
951
952/*
953 * The following *_read functions are used to
954 * read the symbol data from a policy database
955 * binary representation file.
956 */
957
958static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
959{
960 char *key = NULL;
961 struct perm_datum *perdatum;
962 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700963 __le32 buf[2];
964 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
James Morris89d155e2005-10-30 14:59:21 -0800966 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!perdatum) {
968 rc = -ENOMEM;
969 goto out;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 rc = next_entry(buf, fp, sizeof buf);
973 if (rc < 0)
974 goto bad;
975
976 len = le32_to_cpu(buf[0]);
977 perdatum->value = le32_to_cpu(buf[1]);
978
Eric Paris2ced3df2008-04-17 13:37:12 -0400979 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!key) {
981 rc = -ENOMEM;
982 goto bad;
983 }
984 rc = next_entry(key, fp, len);
985 if (rc < 0)
986 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +0300987 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 rc = hashtab_insert(h, key, perdatum);
990 if (rc)
991 goto bad;
992out:
993 return rc;
994bad:
995 perm_destroy(key, perdatum, NULL);
996 goto out;
997}
998
999static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1000{
1001 char *key = NULL;
1002 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001003 __le32 buf[4];
1004 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int i, rc;
1006
James Morris89d155e2005-10-30 14:59:21 -08001007 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!comdatum) {
1009 rc = -ENOMEM;
1010 goto out;
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 rc = next_entry(buf, fp, sizeof buf);
1014 if (rc < 0)
1015 goto bad;
1016
1017 len = le32_to_cpu(buf[0]);
1018 comdatum->value = le32_to_cpu(buf[1]);
1019
1020 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1021 if (rc)
1022 goto bad;
1023 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1024 nel = le32_to_cpu(buf[3]);
1025
Eric Paris2ced3df2008-04-17 13:37:12 -04001026 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!key) {
1028 rc = -ENOMEM;
1029 goto bad;
1030 }
1031 rc = next_entry(key, fp, len);
1032 if (rc < 0)
1033 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001034 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 for (i = 0; i < nel; i++) {
1037 rc = perm_read(p, comdatum->permissions.table, fp);
1038 if (rc)
1039 goto bad;
1040 }
1041
1042 rc = hashtab_insert(h, key, comdatum);
1043 if (rc)
1044 goto bad;
1045out:
1046 return rc;
1047bad:
1048 common_destroy(key, comdatum, NULL);
1049 goto out;
1050}
1051
1052static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001053 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct constraint_node *c, *lc;
1056 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001057 __le32 buf[3];
1058 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int rc, i, j, depth;
1060
1061 lc = NULL;
1062 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001063 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (!c)
1065 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Eric Paris2ced3df2008-04-17 13:37:12 -04001067 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001069 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1073 if (rc < 0)
1074 return rc;
1075 c->permissions = le32_to_cpu(buf[0]);
1076 nexpr = le32_to_cpu(buf[1]);
1077 le = NULL;
1078 depth = -1;
1079 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001080 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!e)
1082 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Eric Paris2ced3df2008-04-17 13:37:12 -04001084 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001086 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1090 if (rc < 0)
1091 return rc;
1092 e->expr_type = le32_to_cpu(buf[0]);
1093 e->attr = le32_to_cpu(buf[1]);
1094 e->op = le32_to_cpu(buf[2]);
1095
1096 switch (e->expr_type) {
1097 case CEXPR_NOT:
1098 if (depth < 0)
1099 return -EINVAL;
1100 break;
1101 case CEXPR_AND:
1102 case CEXPR_OR:
1103 if (depth < 1)
1104 return -EINVAL;
1105 depth--;
1106 break;
1107 case CEXPR_ATTR:
1108 if (depth == (CEXPR_MAXDEPTH - 1))
1109 return -EINVAL;
1110 depth++;
1111 break;
1112 case CEXPR_NAMES:
1113 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1114 return -EINVAL;
1115 if (depth == (CEXPR_MAXDEPTH - 1))
1116 return -EINVAL;
1117 depth++;
1118 if (ebitmap_read(&e->names, fp))
1119 return -EINVAL;
1120 break;
1121 default:
1122 return -EINVAL;
1123 }
1124 le = e;
1125 }
1126 if (depth != 0)
1127 return -EINVAL;
1128 lc = c;
1129 }
1130
1131 return 0;
1132}
1133
1134static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1135{
1136 char *key = NULL;
1137 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001138 __le32 buf[6];
1139 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int i, rc;
1141
James Morris89d155e2005-10-30 14:59:21 -08001142 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!cladatum) {
1144 rc = -ENOMEM;
1145 goto out;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 rc = next_entry(buf, fp, sizeof(u32)*6);
1149 if (rc < 0)
1150 goto bad;
1151
1152 len = le32_to_cpu(buf[0]);
1153 len2 = le32_to_cpu(buf[1]);
1154 cladatum->value = le32_to_cpu(buf[2]);
1155
1156 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1157 if (rc)
1158 goto bad;
1159 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1160 nel = le32_to_cpu(buf[4]);
1161
1162 ncons = le32_to_cpu(buf[5]);
1163
Eric Paris2ced3df2008-04-17 13:37:12 -04001164 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!key) {
1166 rc = -ENOMEM;
1167 goto bad;
1168 }
1169 rc = next_entry(key, fp, len);
1170 if (rc < 0)
1171 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001172 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001175 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!cladatum->comkey) {
1177 rc = -ENOMEM;
1178 goto bad;
1179 }
1180 rc = next_entry(cladatum->comkey, fp, len2);
1181 if (rc < 0)
1182 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001183 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 cladatum->comdatum = hashtab_search(p->p_commons.table,
1186 cladatum->comkey);
1187 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001188 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cladatum->comkey);
1190 rc = -EINVAL;
1191 goto bad;
1192 }
1193 }
1194 for (i = 0; i < nel; i++) {
1195 rc = perm_read(p, cladatum->permissions.table, fp);
1196 if (rc)
1197 goto bad;
1198 }
1199
1200 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1201 if (rc)
1202 goto bad;
1203
1204 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1205 /* grab the validatetrans rules */
1206 rc = next_entry(buf, fp, sizeof(u32));
1207 if (rc < 0)
1208 goto bad;
1209 ncons = le32_to_cpu(buf[0]);
1210 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1211 if (rc)
1212 goto bad;
1213 }
1214
1215 rc = hashtab_insert(h, key, cladatum);
1216 if (rc)
1217 goto bad;
1218
1219 rc = 0;
1220out:
1221 return rc;
1222bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001223 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto out;
1225}
1226
1227static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1228{
1229 char *key = NULL;
1230 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001231 int rc, to_read = 2;
1232 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001233 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
James Morris89d155e2005-10-30 14:59:21 -08001235 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!role) {
1237 rc = -ENOMEM;
1238 goto out;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
KaiGai Koheid9250de2008-08-28 16:35:57 +09001241 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1242 to_read = 3;
1243
1244 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (rc < 0)
1246 goto bad;
1247
1248 len = le32_to_cpu(buf[0]);
1249 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001250 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1251 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Eric Paris2ced3df2008-04-17 13:37:12 -04001253 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!key) {
1255 rc = -ENOMEM;
1256 goto bad;
1257 }
1258 rc = next_entry(key, fp, len);
1259 if (rc < 0)
1260 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001261 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 rc = ebitmap_read(&role->dominates, fp);
1264 if (rc)
1265 goto bad;
1266
1267 rc = ebitmap_read(&role->types, fp);
1268 if (rc)
1269 goto bad;
1270
1271 if (strcmp(key, OBJECT_R) == 0) {
1272 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001273 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 OBJECT_R, role->value);
1275 rc = -EINVAL;
1276 goto bad;
1277 }
1278 rc = 0;
1279 goto bad;
1280 }
1281
1282 rc = hashtab_insert(h, key, role);
1283 if (rc)
1284 goto bad;
1285out:
1286 return rc;
1287bad:
1288 role_destroy(key, role, NULL);
1289 goto out;
1290}
1291
1292static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1293{
1294 char *key = NULL;
1295 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001296 int rc, to_read = 3;
1297 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001298 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Paris2ced3df2008-04-17 13:37:12 -04001300 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!typdatum) {
1302 rc = -ENOMEM;
1303 return rc;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
KaiGai Koheid9250de2008-08-28 16:35:57 +09001306 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1307 to_read = 4;
1308
1309 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (rc < 0)
1311 goto bad;
1312
1313 len = le32_to_cpu(buf[0]);
1314 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001315 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1316 u32 prop = le32_to_cpu(buf[2]);
1317
1318 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1319 typdatum->primary = 1;
1320 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1321 typdatum->attribute = 1;
1322
1323 typdatum->bounds = le32_to_cpu(buf[3]);
1324 } else {
1325 typdatum->primary = le32_to_cpu(buf[2]);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric Paris2ced3df2008-04-17 13:37:12 -04001328 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!key) {
1330 rc = -ENOMEM;
1331 goto bad;
1332 }
1333 rc = next_entry(key, fp, len);
1334 if (rc < 0)
1335 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001336 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 rc = hashtab_insert(h, key, typdatum);
1339 if (rc)
1340 goto bad;
1341out:
1342 return rc;
1343bad:
1344 type_destroy(key, typdatum, NULL);
1345 goto out;
1346}
1347
1348
1349/*
1350 * Read a MLS level structure from a policydb binary
1351 * representation file.
1352 */
1353static int mls_read_level(struct mls_level *lp, void *fp)
1354{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001355 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int rc;
1357
1358 memset(lp, 0, sizeof(*lp));
1359
1360 rc = next_entry(buf, fp, sizeof buf);
1361 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001362 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 goto bad;
1364 }
1365 lp->sens = le32_to_cpu(buf[0]);
1366
1367 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001368 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 "categories\n");
1370 goto bad;
1371 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374
1375bad:
1376 return -EINVAL;
1377}
1378
1379static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1380{
1381 char *key = NULL;
1382 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001383 int rc, to_read = 2;
1384 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001385 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
James Morris89d155e2005-10-30 14:59:21 -08001387 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!usrdatum) {
1389 rc = -ENOMEM;
1390 goto out;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
KaiGai Koheid9250de2008-08-28 16:35:57 +09001393 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1394 to_read = 3;
1395
1396 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (rc < 0)
1398 goto bad;
1399
1400 len = le32_to_cpu(buf[0]);
1401 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1403 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Eric Paris2ced3df2008-04-17 13:37:12 -04001405 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!key) {
1407 rc = -ENOMEM;
1408 goto bad;
1409 }
1410 rc = next_entry(key, fp, len);
1411 if (rc < 0)
1412 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001413 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 rc = ebitmap_read(&usrdatum->roles, fp);
1416 if (rc)
1417 goto bad;
1418
1419 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1420 rc = mls_read_range_helper(&usrdatum->range, fp);
1421 if (rc)
1422 goto bad;
1423 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1424 if (rc)
1425 goto bad;
1426 }
1427
1428 rc = hashtab_insert(h, key, usrdatum);
1429 if (rc)
1430 goto bad;
1431out:
1432 return rc;
1433bad:
1434 user_destroy(key, usrdatum, NULL);
1435 goto out;
1436}
1437
1438static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1439{
1440 char *key = NULL;
1441 struct level_datum *levdatum;
1442 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001443 __le32 buf[2];
1444 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
James Morris89d155e2005-10-30 14:59:21 -08001446 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!levdatum) {
1448 rc = -ENOMEM;
1449 goto out;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 rc = next_entry(buf, fp, sizeof buf);
1453 if (rc < 0)
1454 goto bad;
1455
1456 len = le32_to_cpu(buf[0]);
1457 levdatum->isalias = le32_to_cpu(buf[1]);
1458
Eric Paris2ced3df2008-04-17 13:37:12 -04001459 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!key) {
1461 rc = -ENOMEM;
1462 goto bad;
1463 }
1464 rc = next_entry(key, fp, len);
1465 if (rc < 0)
1466 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001467 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1470 if (!levdatum->level) {
1471 rc = -ENOMEM;
1472 goto bad;
1473 }
1474 if (mls_read_level(levdatum->level, fp)) {
1475 rc = -EINVAL;
1476 goto bad;
1477 }
1478
1479 rc = hashtab_insert(h, key, levdatum);
1480 if (rc)
1481 goto bad;
1482out:
1483 return rc;
1484bad:
1485 sens_destroy(key, levdatum, NULL);
1486 goto out;
1487}
1488
1489static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1490{
1491 char *key = NULL;
1492 struct cat_datum *catdatum;
1493 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001494 __le32 buf[3];
1495 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
James Morris89d155e2005-10-30 14:59:21 -08001497 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!catdatum) {
1499 rc = -ENOMEM;
1500 goto out;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 rc = next_entry(buf, fp, sizeof buf);
1504 if (rc < 0)
1505 goto bad;
1506
1507 len = le32_to_cpu(buf[0]);
1508 catdatum->value = le32_to_cpu(buf[1]);
1509 catdatum->isalias = le32_to_cpu(buf[2]);
1510
Eric Paris2ced3df2008-04-17 13:37:12 -04001511 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (!key) {
1513 rc = -ENOMEM;
1514 goto bad;
1515 }
1516 rc = next_entry(key, fp, len);
1517 if (rc < 0)
1518 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001519 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 rc = hashtab_insert(h, key, catdatum);
1522 if (rc)
1523 goto bad;
1524out:
1525 return rc;
1526
1527bad:
1528 cat_destroy(key, catdatum, NULL);
1529 goto out;
1530}
1531
1532static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1533{
1534 common_read,
1535 class_read,
1536 role_read,
1537 type_read,
1538 user_read,
1539 cond_read_bool,
1540 sens_read,
1541 cat_read,
1542};
1543
KaiGai Koheid9250de2008-08-28 16:35:57 +09001544static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1545{
1546 struct user_datum *upper, *user;
1547 struct policydb *p = datap;
1548 int depth = 0;
1549
1550 upper = user = datum;
1551 while (upper->bounds) {
1552 struct ebitmap_node *node;
1553 unsigned long bit;
1554
1555 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1556 printk(KERN_ERR "SELinux: user %s: "
1557 "too deep or looped boundary",
1558 (char *) key);
1559 return -EINVAL;
1560 }
1561
1562 upper = p->user_val_to_struct[upper->bounds - 1];
1563 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1564 if (ebitmap_get_bit(&upper->roles, bit))
1565 continue;
1566
1567 printk(KERN_ERR
1568 "SELinux: boundary violated policy: "
1569 "user=%s role=%s bounds=%s\n",
1570 p->p_user_val_to_name[user->value - 1],
1571 p->p_role_val_to_name[bit],
1572 p->p_user_val_to_name[upper->value - 1]);
1573
1574 return -EINVAL;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
1581static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1582{
1583 struct role_datum *upper, *role;
1584 struct policydb *p = datap;
1585 int depth = 0;
1586
1587 upper = role = datum;
1588 while (upper->bounds) {
1589 struct ebitmap_node *node;
1590 unsigned long bit;
1591
1592 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1593 printk(KERN_ERR "SELinux: role %s: "
1594 "too deep or looped bounds\n",
1595 (char *) key);
1596 return -EINVAL;
1597 }
1598
1599 upper = p->role_val_to_struct[upper->bounds - 1];
1600 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1601 if (ebitmap_get_bit(&upper->types, bit))
1602 continue;
1603
1604 printk(KERN_ERR
1605 "SELinux: boundary violated policy: "
1606 "role=%s type=%s bounds=%s\n",
1607 p->p_role_val_to_name[role->value - 1],
1608 p->p_type_val_to_name[bit],
1609 p->p_role_val_to_name[upper->value - 1]);
1610
1611 return -EINVAL;
1612 }
1613 }
1614
1615 return 0;
1616}
1617
1618static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1619{
1620 struct type_datum *upper, *type;
1621 struct policydb *p = datap;
1622 int depth = 0;
1623
1624 upper = type = datum;
1625 while (upper->bounds) {
1626 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1627 printk(KERN_ERR "SELinux: type %s: "
1628 "too deep or looped boundary\n",
1629 (char *) key);
1630 return -EINVAL;
1631 }
1632
1633 upper = p->type_val_to_struct[upper->bounds - 1];
1634 if (upper->attribute) {
1635 printk(KERN_ERR "SELinux: type %s: "
1636 "bounded by attribute %s",
1637 (char *) key,
1638 p->p_type_val_to_name[upper->value - 1]);
1639 return -EINVAL;
1640 }
1641 }
1642
1643 return 0;
1644}
1645
1646static int policydb_bounds_sanity_check(struct policydb *p)
1647{
1648 int rc;
1649
1650 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1651 return 0;
1652
1653 rc = hashtab_map(p->p_users.table,
1654 user_bounds_sanity_check, p);
1655 if (rc)
1656 return rc;
1657
1658 rc = hashtab_map(p->p_roles.table,
1659 role_bounds_sanity_check, p);
1660 if (rc)
1661 return rc;
1662
1663 rc = hashtab_map(p->p_types.table,
1664 type_bounds_sanity_check, p);
1665 if (rc)
1666 return rc;
1667
1668 return 0;
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671extern int ss_initialized;
1672
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001673u16 string_to_security_class(struct policydb *p, const char *name)
1674{
1675 struct class_datum *cladatum;
1676
1677 cladatum = hashtab_search(p->p_classes.table, name);
1678 if (!cladatum)
1679 return 0;
1680
1681 return cladatum->value;
1682}
1683
1684u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1685{
1686 struct class_datum *cladatum;
1687 struct perm_datum *perdatum = NULL;
1688 struct common_datum *comdatum;
1689
1690 if (!tclass || tclass > p->p_classes.nprim)
1691 return 0;
1692
1693 cladatum = p->class_val_to_struct[tclass-1];
1694 comdatum = cladatum->comdatum;
1695 if (comdatum)
1696 perdatum = hashtab_search(comdatum->permissions.table,
1697 name);
1698 if (!perdatum)
1699 perdatum = hashtab_search(cladatum->permissions.table,
1700 name);
1701 if (!perdatum)
1702 return 0;
1703
1704 return 1U << (perdatum->value-1);
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * Read the configuration data from a policy database binary
1709 * representation file into a policy database structure.
1710 */
1711int policydb_read(struct policydb *p, void *fp)
1712{
1713 struct role_allow *ra, *lra;
1714 struct role_trans *tr, *ltr;
1715 struct ocontext *l, *c, *newc;
1716 struct genfs *genfs_p, *genfs, *newgenfs;
1717 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001718 __le32 buf[4];
1719 u32 nodebuf[8];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001720 u32 len, len2, config, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 char *policydb_str;
1722 struct policydb_compat_info *info;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05001723 struct range_trans *rt;
1724 struct mls_range *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 config = 0;
1727
1728 rc = policydb_init(p);
1729 if (rc)
1730 goto out;
1731
1732 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001733 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (rc < 0)
1735 goto bad;
1736
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001737 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001738 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001740 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto bad;
1742 }
1743
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001744 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001746 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 "match expected length %Zu\n",
1748 len, strlen(POLICYDB_STRING));
1749 goto bad;
1750 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001751 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001753 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 "string of length %d\n", len);
1755 rc = -ENOMEM;
1756 goto bad;
1757 }
1758 rc = next_entry(policydb_str, fp, len);
1759 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001760 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 kfree(policydb_str);
1762 goto bad;
1763 }
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001764 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001766 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 "my string %s\n", policydb_str, POLICYDB_STRING);
1768 kfree(policydb_str);
1769 goto bad;
1770 }
1771 /* Done with policydb_str. */
1772 kfree(policydb_str);
1773 policydb_str = NULL;
1774
1775 /* Read the version, config, and table sizes. */
1776 rc = next_entry(buf, fp, sizeof(u32)*4);
1777 if (rc < 0)
1778 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001780 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (p->policyvers < POLICYDB_VERSION_MIN ||
1782 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001783 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001784 "my version range %d-%d\n",
1785 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1786 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001789 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (ss_initialized && !selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001791 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1792 " and MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto bad;
1794 }
1795 selinux_mls_enabled = 1;
1796 config |= POLICYDB_CONFIG_MLS;
1797
1798 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001799 printk(KERN_ERR "SELinux: security policydb version %d "
1800 "(MLS) not backwards compatible\n",
1801 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto bad;
1803 }
1804 } else {
1805 if (ss_initialized && selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001806 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1807 " non-MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto bad;
1809 }
1810 }
Eric Paris3f120702007-09-21 14:37:10 -04001811 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1812 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Paul Moore3bb56b22008-01-29 08:38:19 -05001814 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1815 ebitmap_read(&p->policycaps, fp) != 0)
1816 goto bad;
1817
Eric Paris64dbf072008-03-31 12:17:33 +11001818 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1819 ebitmap_read(&p->permissive_map, fp) != 0)
1820 goto bad;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 info = policydb_lookup_compat(p->policyvers);
1823 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11001824 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 "for version %d\n", p->policyvers);
1826 goto bad;
1827 }
1828
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001829 if (le32_to_cpu(buf[2]) != info->sym_num ||
1830 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11001831 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001832 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1833 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 info->sym_num, info->ocon_num);
1835 goto bad;
1836 }
1837
1838 for (i = 0; i < info->sym_num; i++) {
1839 rc = next_entry(buf, fp, sizeof(u32)*2);
1840 if (rc < 0)
1841 goto bad;
1842 nprim = le32_to_cpu(buf[0]);
1843 nel = le32_to_cpu(buf[1]);
1844 for (j = 0; j < nel; j++) {
1845 rc = read_f[i](p, p->symtab[i].table, fp);
1846 if (rc)
1847 goto bad;
1848 }
1849
1850 p->symtab[i].nprim = nprim;
1851 }
1852
Stephen Smalley45e54212007-11-07 10:08:00 -05001853 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (rc)
1855 goto bad;
1856
1857 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1858 rc = cond_read_list(p, fp);
1859 if (rc)
1860 goto bad;
1861 }
1862
1863 rc = next_entry(buf, fp, sizeof(u32));
1864 if (rc < 0)
1865 goto bad;
1866 nel = le32_to_cpu(buf[0]);
1867 ltr = NULL;
1868 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001869 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (!tr) {
1871 rc = -ENOMEM;
1872 goto bad;
1873 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001874 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04001876 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rc = next_entry(buf, fp, sizeof(u32)*3);
1879 if (rc < 0)
1880 goto bad;
1881 tr->role = le32_to_cpu(buf[0]);
1882 tr->type = le32_to_cpu(buf[1]);
1883 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001884 if (!policydb_role_isvalid(p, tr->role) ||
1885 !policydb_type_isvalid(p, tr->type) ||
1886 !policydb_role_isvalid(p, tr->new_role)) {
1887 rc = -EINVAL;
1888 goto bad;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 ltr = tr;
1891 }
1892
1893 rc = next_entry(buf, fp, sizeof(u32));
1894 if (rc < 0)
1895 goto bad;
1896 nel = le32_to_cpu(buf[0]);
1897 lra = NULL;
1898 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001899 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!ra) {
1901 rc = -ENOMEM;
1902 goto bad;
1903 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001904 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04001906 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 rc = next_entry(buf, fp, sizeof(u32)*2);
1909 if (rc < 0)
1910 goto bad;
1911 ra->role = le32_to_cpu(buf[0]);
1912 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001913 if (!policydb_role_isvalid(p, ra->role) ||
1914 !policydb_role_isvalid(p, ra->new_role)) {
1915 rc = -EINVAL;
1916 goto bad;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 lra = ra;
1919 }
1920
1921 rc = policydb_index_classes(p);
1922 if (rc)
1923 goto bad;
1924
1925 rc = policydb_index_others(p);
1926 if (rc)
1927 goto bad;
1928
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001929 p->process_class = string_to_security_class(p, "process");
1930 if (!p->process_class)
1931 goto bad;
1932 p->process_trans_perms = string_to_av_perm(p, p->process_class,
1933 "transition");
1934 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
1935 "dyntransition");
1936 if (!p->process_trans_perms)
1937 goto bad;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 for (i = 0; i < info->ocon_num; i++) {
1940 rc = next_entry(buf, fp, sizeof(u32));
1941 if (rc < 0)
1942 goto bad;
1943 nel = le32_to_cpu(buf[0]);
1944 l = NULL;
1945 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001946 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (!c) {
1948 rc = -ENOMEM;
1949 goto bad;
1950 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001951 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001953 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 l = c;
1956 rc = -EINVAL;
1957 switch (i) {
1958 case OCON_ISID:
1959 rc = next_entry(buf, fp, sizeof(u32));
1960 if (rc < 0)
1961 goto bad;
1962 c->sid[0] = le32_to_cpu(buf[0]);
1963 rc = context_read_and_validate(&c->context[0], p, fp);
1964 if (rc)
1965 goto bad;
1966 break;
1967 case OCON_FS:
1968 case OCON_NETIF:
1969 rc = next_entry(buf, fp, sizeof(u32));
1970 if (rc < 0)
1971 goto bad;
1972 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001973 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!c->u.name) {
1975 rc = -ENOMEM;
1976 goto bad;
1977 }
1978 rc = next_entry(c->u.name, fp, len);
1979 if (rc < 0)
1980 goto bad;
1981 c->u.name[len] = 0;
1982 rc = context_read_and_validate(&c->context[0], p, fp);
1983 if (rc)
1984 goto bad;
1985 rc = context_read_and_validate(&c->context[1], p, fp);
1986 if (rc)
1987 goto bad;
1988 break;
1989 case OCON_PORT:
1990 rc = next_entry(buf, fp, sizeof(u32)*3);
1991 if (rc < 0)
1992 goto bad;
1993 c->u.port.protocol = le32_to_cpu(buf[0]);
1994 c->u.port.low_port = le32_to_cpu(buf[1]);
1995 c->u.port.high_port = le32_to_cpu(buf[2]);
1996 rc = context_read_and_validate(&c->context[0], p, fp);
1997 if (rc)
1998 goto bad;
1999 break;
2000 case OCON_NODE:
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002001 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (rc < 0)
2003 goto bad;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002004 c->u.node.addr = nodebuf[0]; /* network order */
2005 c->u.node.mask = nodebuf[1]; /* network order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 rc = context_read_and_validate(&c->context[0], p, fp);
2007 if (rc)
2008 goto bad;
2009 break;
2010 case OCON_FSUSE:
2011 rc = next_entry(buf, fp, sizeof(u32)*2);
2012 if (rc < 0)
2013 goto bad;
2014 c->v.behavior = le32_to_cpu(buf[0]);
2015 if (c->v.behavior > SECURITY_FS_USE_NONE)
2016 goto bad;
2017 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04002018 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (!c->u.name) {
2020 rc = -ENOMEM;
2021 goto bad;
2022 }
2023 rc = next_entry(c->u.name, fp, len);
2024 if (rc < 0)
2025 goto bad;
2026 c->u.name[len] = 0;
2027 rc = context_read_and_validate(&c->context[0], p, fp);
2028 if (rc)
2029 goto bad;
2030 break;
2031 case OCON_NODE6: {
2032 int k;
2033
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002034 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (rc < 0)
2036 goto bad;
2037 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002038 c->u.node6.addr[k] = nodebuf[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002040 c->u.node6.mask[k] = nodebuf[k+4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (context_read_and_validate(&c->context[0], p, fp))
2042 goto bad;
2043 break;
2044 }
2045 }
2046 }
2047 }
2048
2049 rc = next_entry(buf, fp, sizeof(u32));
2050 if (rc < 0)
2051 goto bad;
2052 nel = le32_to_cpu(buf[0]);
2053 genfs_p = NULL;
2054 rc = -EINVAL;
2055 for (i = 0; i < nel; i++) {
2056 rc = next_entry(buf, fp, sizeof(u32));
2057 if (rc < 0)
2058 goto bad;
2059 len = le32_to_cpu(buf[0]);
James Morris89d155e2005-10-30 14:59:21 -08002060 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (!newgenfs) {
2062 rc = -ENOMEM;
2063 goto bad;
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Eric Paris2ced3df2008-04-17 13:37:12 -04002066 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!newgenfs->fstype) {
2068 rc = -ENOMEM;
2069 kfree(newgenfs);
2070 goto bad;
2071 }
2072 rc = next_entry(newgenfs->fstype, fp, len);
2073 if (rc < 0) {
2074 kfree(newgenfs->fstype);
2075 kfree(newgenfs);
2076 goto bad;
2077 }
2078 newgenfs->fstype[len] = 0;
2079 for (genfs_p = NULL, genfs = p->genfs; genfs;
2080 genfs_p = genfs, genfs = genfs->next) {
2081 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
James Morris454d9722008-02-26 20:42:02 +11002082 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 "fstype %s\n", newgenfs->fstype);
2084 kfree(newgenfs->fstype);
2085 kfree(newgenfs);
2086 goto bad;
2087 }
2088 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2089 break;
2090 }
2091 newgenfs->next = genfs;
2092 if (genfs_p)
2093 genfs_p->next = newgenfs;
2094 else
2095 p->genfs = newgenfs;
2096 rc = next_entry(buf, fp, sizeof(u32));
2097 if (rc < 0)
2098 goto bad;
2099 nel2 = le32_to_cpu(buf[0]);
2100 for (j = 0; j < nel2; j++) {
2101 rc = next_entry(buf, fp, sizeof(u32));
2102 if (rc < 0)
2103 goto bad;
2104 len = le32_to_cpu(buf[0]);
2105
James Morris89d155e2005-10-30 14:59:21 -08002106 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!newc) {
2108 rc = -ENOMEM;
2109 goto bad;
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Eric Paris2ced3df2008-04-17 13:37:12 -04002112 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (!newc->u.name) {
2114 rc = -ENOMEM;
2115 goto bad_newc;
2116 }
2117 rc = next_entry(newc->u.name, fp, len);
2118 if (rc < 0)
2119 goto bad_newc;
2120 newc->u.name[len] = 0;
2121 rc = next_entry(buf, fp, sizeof(u32));
2122 if (rc < 0)
2123 goto bad_newc;
2124 newc->v.sclass = le32_to_cpu(buf[0]);
2125 if (context_read_and_validate(&newc->context[0], p, fp))
2126 goto bad_newc;
2127 for (l = NULL, c = newgenfs->head; c;
2128 l = c, c = c->next) {
2129 if (!strcmp(newc->u.name, c->u.name) &&
2130 (!c->v.sclass || !newc->v.sclass ||
2131 newc->v.sclass == c->v.sclass)) {
James Morris454d9722008-02-26 20:42:02 +11002132 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 "entry (%s,%s)\n",
2134 newgenfs->fstype, c->u.name);
2135 goto bad_newc;
2136 }
2137 len = strlen(newc->u.name);
2138 len2 = strlen(c->u.name);
2139 if (len > len2)
2140 break;
2141 }
2142
2143 newc->next = c;
2144 if (l)
2145 l->next = newc;
2146 else
2147 newgenfs->head = newc;
2148 }
2149 }
2150
2151 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002152 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 rc = next_entry(buf, fp, sizeof(u32));
2154 if (rc < 0)
2155 goto bad;
2156 nel = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002158 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (!rt) {
2160 rc = -ENOMEM;
2161 goto bad;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002164 if (rc < 0) {
2165 kfree(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto bad;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002167 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002168 rt->source_type = le32_to_cpu(buf[0]);
2169 rt->target_type = le32_to_cpu(buf[1]);
2170 if (new_rangetr) {
2171 rc = next_entry(buf, fp, sizeof(u32));
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002172 if (rc < 0) {
2173 kfree(rt);
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002174 goto bad;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002175 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002176 rt->target_class = le32_to_cpu(buf[0]);
2177 } else
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002178 rt->target_class = p->process_class;
Stephen Smalley45e54212007-11-07 10:08:00 -05002179 if (!policydb_type_isvalid(p, rt->source_type) ||
2180 !policydb_type_isvalid(p, rt->target_type) ||
2181 !policydb_class_isvalid(p, rt->target_class)) {
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002182 kfree(rt);
Stephen Smalley45e54212007-11-07 10:08:00 -05002183 rc = -EINVAL;
2184 goto bad;
2185 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002186 r = kzalloc(sizeof(*r), GFP_KERNEL);
2187 if (!r) {
2188 kfree(rt);
2189 rc = -ENOMEM;
Stephen Smalley45e54212007-11-07 10:08:00 -05002190 goto bad;
2191 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002192 rc = mls_read_range_helper(r, fp);
2193 if (rc) {
2194 kfree(rt);
2195 kfree(r);
2196 goto bad;
2197 }
2198 if (!mls_range_isvalid(p, r)) {
2199 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
2200 kfree(rt);
2201 kfree(r);
2202 goto bad;
2203 }
2204 rc = hashtab_insert(p->range_tr, rt, r);
2205 if (rc) {
2206 kfree(rt);
2207 kfree(r);
2208 goto bad;
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002211 rangetr_hash_eval(p->range_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Stephen Smalley782ebb92005-09-03 15:55:16 -07002214 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2215 if (!p->type_attr_map)
2216 goto bad;
2217
2218 for (i = 0; i < p->p_types.nprim; i++) {
2219 ebitmap_init(&p->type_attr_map[i]);
2220 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2221 if (ebitmap_read(&p->type_attr_map[i], fp))
2222 goto bad;
2223 }
2224 /* add the type itself as the degenerate case */
2225 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2226 goto bad;
2227 }
2228
KaiGai Koheid9250de2008-08-28 16:35:57 +09002229 rc = policydb_bounds_sanity_check(p);
2230 if (rc)
2231 goto bad;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 rc = 0;
2234out:
2235 return rc;
2236bad_newc:
Eric Paris2ced3df2008-04-17 13:37:12 -04002237 ocontext_destroy(newc, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238bad:
2239 if (!rc)
2240 rc = -EINVAL;
2241 policydb_destroy(p);
2242 goto out;
2243}