blob: 72e4a54973aae503c9c9378aa392750f7f4adb20 [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
180/*
181 * Initialize a policy database structure.
182 */
183static int policydb_init(struct policydb *p)
184{
185 int i, rc;
186
187 memset(p, 0, sizeof(*p));
188
189 for (i = 0; i < SYM_NUM; i++) {
190 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
191 if (rc)
192 goto out_free_symtab;
193 }
194
195 rc = avtab_init(&p->te_avtab);
196 if (rc)
197 goto out_free_symtab;
198
199 rc = roles_init(p);
200 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900201 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 rc = cond_policydb_init(p);
204 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900205 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Paul Moore3bb56b22008-01-29 08:38:19 -0500207 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100208 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210out:
211 return rc;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213out_free_symtab:
214 for (i = 0; i < SYM_NUM; i++)
215 hashtab_destroy(p->symtab[i].table);
216 goto out;
217}
218
219/*
220 * The following *_index functions are used to
221 * define the val_to_name and val_to_struct arrays
222 * in a policy database structure. The val_to_name
223 * arrays are used when converting security context
224 * structures into string representations. The
225 * val_to_struct arrays are used when the attributes
226 * of a class, role, or user are needed.
227 */
228
229static int common_index(void *key, void *datum, void *datap)
230{
231 struct policydb *p;
232 struct common_datum *comdatum;
233
234 comdatum = datum;
235 p = datap;
236 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
237 return -EINVAL;
238 p->p_common_val_to_name[comdatum->value - 1] = key;
239 return 0;
240}
241
242static int class_index(void *key, void *datum, void *datap)
243{
244 struct policydb *p;
245 struct class_datum *cladatum;
246
247 cladatum = datum;
248 p = datap;
249 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
250 return -EINVAL;
251 p->p_class_val_to_name[cladatum->value - 1] = key;
252 p->class_val_to_struct[cladatum->value - 1] = cladatum;
253 return 0;
254}
255
256static int role_index(void *key, void *datum, void *datap)
257{
258 struct policydb *p;
259 struct role_datum *role;
260
261 role = datum;
262 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900263 if (!role->value
264 || role->value > p->p_roles.nprim
265 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EINVAL;
267 p->p_role_val_to_name[role->value - 1] = key;
268 p->role_val_to_struct[role->value - 1] = role;
269 return 0;
270}
271
272static int type_index(void *key, void *datum, void *datap)
273{
274 struct policydb *p;
275 struct type_datum *typdatum;
276
277 typdatum = datum;
278 p = datap;
279
280 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900281 if (!typdatum->value
282 || typdatum->value > p->p_types.nprim
283 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -EINVAL;
285 p->p_type_val_to_name[typdatum->value - 1] = key;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900286 p->type_val_to_struct[typdatum->value - 1] = typdatum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 return 0;
290}
291
292static int user_index(void *key, void *datum, void *datap)
293{
294 struct policydb *p;
295 struct user_datum *usrdatum;
296
297 usrdatum = datum;
298 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900299 if (!usrdatum->value
300 || usrdatum->value > p->p_users.nprim
301 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EINVAL;
303 p->p_user_val_to_name[usrdatum->value - 1] = key;
304 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
305 return 0;
306}
307
308static int sens_index(void *key, void *datum, void *datap)
309{
310 struct policydb *p;
311 struct level_datum *levdatum;
312
313 levdatum = datum;
314 p = datap;
315
316 if (!levdatum->isalias) {
317 if (!levdatum->level->sens ||
318 levdatum->level->sens > p->p_levels.nprim)
319 return -EINVAL;
320 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
321 }
322
323 return 0;
324}
325
326static int cat_index(void *key, void *datum, void *datap)
327{
328 struct policydb *p;
329 struct cat_datum *catdatum;
330
331 catdatum = datum;
332 p = datap;
333
334 if (!catdatum->isalias) {
335 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
336 return -EINVAL;
337 p->p_cat_val_to_name[catdatum->value - 1] = key;
338 }
339
340 return 0;
341}
342
343static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
344{
345 common_index,
346 class_index,
347 role_index,
348 type_index,
349 user_index,
350 cond_index_bool,
351 sens_index,
352 cat_index,
353};
354
355/*
356 * Define the common val_to_name array and the class
357 * val_to_name and val_to_struct arrays in a policy
358 * database structure.
359 *
360 * Caller must clean up upon failure.
361 */
362static int policydb_index_classes(struct policydb *p)
363{
364 int rc;
365
366 p->p_common_val_to_name =
367 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
368 if (!p->p_common_val_to_name) {
369 rc = -ENOMEM;
370 goto out;
371 }
372
373 rc = hashtab_map(p->p_commons.table, common_index, p);
374 if (rc)
375 goto out;
376
377 p->class_val_to_struct =
378 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
379 if (!p->class_val_to_struct) {
380 rc = -ENOMEM;
381 goto out;
382 }
383
384 p->p_class_val_to_name =
385 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
386 if (!p->p_class_val_to_name) {
387 rc = -ENOMEM;
388 goto out;
389 }
390
391 rc = hashtab_map(p->p_classes.table, class_index, p);
392out:
393 return rc;
394}
395
396#ifdef DEBUG_HASHES
397static void symtab_hash_eval(struct symtab *s)
398{
399 int i;
400
401 for (i = 0; i < SYM_NUM; i++) {
402 struct hashtab *h = s[i].table;
403 struct hashtab_info info;
404
405 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400406 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 "longest chain length %d\n", symtab_name[i], h->nel,
408 info.slots_used, h->size, info.max_chain_len);
409 }
410}
411#endif
412
413/*
414 * Define the other val_to_name and val_to_struct arrays
415 * in a policy database structure.
416 *
417 * Caller must clean up on failure.
418 */
419static int policydb_index_others(struct policydb *p)
420{
421 int i, rc = 0;
422
James Morris454d9722008-02-26 20:42:02 +1100423 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
425 if (selinux_mls_enabled)
426 printk(", %d sens, %d cats", p->p_levels.nprim,
427 p->p_cats.nprim);
428 printk("\n");
429
James Morris454d9722008-02-26 20:42:02 +1100430 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 p->p_classes.nprim, p->te_avtab.nel);
432
433#ifdef DEBUG_HASHES
434 avtab_hash_eval(&p->te_avtab, "rules");
435 symtab_hash_eval(p->symtab);
436#endif
437
438 p->role_val_to_struct =
439 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400440 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!p->role_val_to_struct) {
442 rc = -ENOMEM;
443 goto out;
444 }
445
446 p->user_val_to_struct =
447 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400448 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!p->user_val_to_struct) {
450 rc = -ENOMEM;
451 goto out;
452 }
453
KaiGai Koheid9250de2008-08-28 16:35:57 +0900454 p->type_val_to_struct =
455 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
456 GFP_KERNEL);
457 if (!p->type_val_to_struct) {
458 rc = -ENOMEM;
459 goto out;
460 }
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (cond_init_bool_indexes(p)) {
463 rc = -ENOMEM;
464 goto out;
465 }
466
467 for (i = SYM_ROLES; i < SYM_NUM; i++) {
468 p->sym_val_to_name[i] =
469 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
470 if (!p->sym_val_to_name[i]) {
471 rc = -ENOMEM;
472 goto out;
473 }
474 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
475 if (rc)
476 goto out;
477 }
478
479out:
480 return rc;
481}
482
483/*
484 * The following *_destroy functions are used to
485 * free any memory allocated for each kind of
486 * symbol data in the policy database.
487 */
488
489static int perm_destroy(void *key, void *datum, void *p)
490{
491 kfree(key);
492 kfree(datum);
493 return 0;
494}
495
496static int common_destroy(void *key, void *datum, void *p)
497{
498 struct common_datum *comdatum;
499
500 kfree(key);
501 comdatum = datum;
502 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
503 hashtab_destroy(comdatum->permissions.table);
504 kfree(datum);
505 return 0;
506}
507
James Morris6cbda6b2006-11-29 16:50:27 -0500508static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct class_datum *cladatum;
511 struct constraint_node *constraint, *ctemp;
512 struct constraint_expr *e, *etmp;
513
514 kfree(key);
515 cladatum = datum;
516 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
517 hashtab_destroy(cladatum->permissions.table);
518 constraint = cladatum->constraints;
519 while (constraint) {
520 e = constraint->expr;
521 while (e) {
522 ebitmap_destroy(&e->names);
523 etmp = e;
524 e = e->next;
525 kfree(etmp);
526 }
527 ctemp = constraint;
528 constraint = constraint->next;
529 kfree(ctemp);
530 }
531
532 constraint = cladatum->validatetrans;
533 while (constraint) {
534 e = constraint->expr;
535 while (e) {
536 ebitmap_destroy(&e->names);
537 etmp = e;
538 e = e->next;
539 kfree(etmp);
540 }
541 ctemp = constraint;
542 constraint = constraint->next;
543 kfree(ctemp);
544 }
545
546 kfree(cladatum->comkey);
547 kfree(datum);
548 return 0;
549}
550
551static int role_destroy(void *key, void *datum, void *p)
552{
553 struct role_datum *role;
554
555 kfree(key);
556 role = datum;
557 ebitmap_destroy(&role->dominates);
558 ebitmap_destroy(&role->types);
559 kfree(datum);
560 return 0;
561}
562
563static int type_destroy(void *key, void *datum, void *p)
564{
565 kfree(key);
566 kfree(datum);
567 return 0;
568}
569
570static int user_destroy(void *key, void *datum, void *p)
571{
572 struct user_datum *usrdatum;
573
574 kfree(key);
575 usrdatum = datum;
576 ebitmap_destroy(&usrdatum->roles);
577 ebitmap_destroy(&usrdatum->range.level[0].cat);
578 ebitmap_destroy(&usrdatum->range.level[1].cat);
579 ebitmap_destroy(&usrdatum->dfltlevel.cat);
580 kfree(datum);
581 return 0;
582}
583
584static int sens_destroy(void *key, void *datum, void *p)
585{
586 struct level_datum *levdatum;
587
588 kfree(key);
589 levdatum = datum;
590 ebitmap_destroy(&levdatum->level->cat);
591 kfree(levdatum->level);
592 kfree(datum);
593 return 0;
594}
595
596static int cat_destroy(void *key, void *datum, void *p)
597{
598 kfree(key);
599 kfree(datum);
600 return 0;
601}
602
603static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
604{
605 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500606 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 role_destroy,
608 type_destroy,
609 user_destroy,
610 cond_destroy_bool,
611 sens_destroy,
612 cat_destroy,
613};
614
615static void ocontext_destroy(struct ocontext *c, int i)
616{
617 context_destroy(&c->context[0]);
618 context_destroy(&c->context[1]);
619 if (i == OCON_ISID || i == OCON_FS ||
620 i == OCON_NETIF || i == OCON_FSUSE)
621 kfree(c->u.name);
622 kfree(c);
623}
624
625/*
626 * Free any memory allocated by a policy database structure.
627 */
628void policydb_destroy(struct policydb *p)
629{
630 struct ocontext *c, *ctmp;
631 struct genfs *g, *gtmp;
632 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700633 struct role_allow *ra, *lra = NULL;
634 struct role_trans *tr, *ltr = NULL;
635 struct range_trans *rt, *lrt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400638 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
640 hashtab_destroy(p->symtab[i].table);
641 }
642
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700643 for (i = 0; i < SYM_NUM; i++)
644 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700646 kfree(p->class_val_to_struct);
647 kfree(p->role_val_to_struct);
648 kfree(p->user_val_to_struct);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900649 kfree(p->type_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 avtab_destroy(&p->te_avtab);
652
653 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400654 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 c = p->ocontexts[i];
656 while (c) {
657 ctmp = c;
658 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400659 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400661 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 g = p->genfs;
665 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400666 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 kfree(g->fstype);
668 c = g->head;
669 while (c) {
670 ctmp = c;
671 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400672 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 gtmp = g;
675 g = g->next;
676 kfree(gtmp);
677 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400678 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 cond_policydb_destroy(p);
681
Stephen Smalley782ebb92005-09-03 15:55:16 -0700682 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400683 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800684 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700685 ltr = tr;
686 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800687 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700688
Eric Paris2ced3df2008-04-17 13:37:12 -0400689 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400690 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800691 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700692 lra = ra;
693 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800694 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700695
Eric Paris2ced3df2008-04-17 13:37:12 -0400696 for (rt = p->range_tr; rt; rt = rt->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400697 cond_resched();
Darrel Goeddelddccef32006-07-30 03:03:17 -0700698 if (lrt) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700699 ebitmap_destroy(&lrt->target_range.level[0].cat);
700 ebitmap_destroy(&lrt->target_range.level[1].cat);
Darrel Goeddelddccef32006-07-30 03:03:17 -0700701 kfree(lrt);
702 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700703 lrt = rt;
704 }
Darrel Goeddelddccef32006-07-30 03:03:17 -0700705 if (lrt) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700706 ebitmap_destroy(&lrt->target_range.level[0].cat);
707 ebitmap_destroy(&lrt->target_range.level[1].cat);
Darrel Goeddelddccef32006-07-30 03:03:17 -0700708 kfree(lrt);
709 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700710
Stephen Smalley282c1f52005-10-23 12:57:15 -0700711 if (p->type_attr_map) {
712 for (i = 0; i < p->p_types.nprim; i++)
713 ebitmap_destroy(&p->type_attr_map[i]);
714 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700715 kfree(p->type_attr_map);
Eric Paris3f120702007-09-21 14:37:10 -0400716 kfree(p->undefined_perms);
Paul Moore3bb56b22008-01-29 08:38:19 -0500717 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100718 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return;
721}
722
723/*
724 * Load the initial SIDs specified in a policy database
725 * structure into a SID table.
726 */
727int policydb_load_isids(struct policydb *p, struct sidtab *s)
728{
729 struct ocontext *head, *c;
730 int rc;
731
732 rc = sidtab_init(s);
733 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100734 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto out;
736 }
737
738 head = p->ocontexts[OCON_ISID];
739 for (c = head; c; c = c->next) {
740 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100741 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 "defined.\n", c->u.name);
743 rc = -EINVAL;
744 goto out;
745 }
746 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100747 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 "SID %s.\n", c->u.name);
749 rc = -EINVAL;
750 goto out;
751 }
752 }
753out:
754 return rc;
755}
756
Stephen Smalley45e54212007-11-07 10:08:00 -0500757int policydb_class_isvalid(struct policydb *p, unsigned int class)
758{
759 if (!class || class > p->p_classes.nprim)
760 return 0;
761 return 1;
762}
763
764int policydb_role_isvalid(struct policydb *p, unsigned int role)
765{
766 if (!role || role > p->p_roles.nprim)
767 return 0;
768 return 1;
769}
770
771int policydb_type_isvalid(struct policydb *p, unsigned int type)
772{
773 if (!type || type > p->p_types.nprim)
774 return 0;
775 return 1;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * Return 1 if the fields in the security context
780 * structure `c' are valid. Return 0 otherwise.
781 */
782int policydb_context_isvalid(struct policydb *p, struct context *c)
783{
784 struct role_datum *role;
785 struct user_datum *usrdatum;
786
787 if (!c->role || c->role > p->p_roles.nprim)
788 return 0;
789
790 if (!c->user || c->user > p->p_users.nprim)
791 return 0;
792
793 if (!c->type || c->type > p->p_types.nprim)
794 return 0;
795
796 if (c->role != OBJECT_R_VAL) {
797 /*
798 * Role must be authorized for the type.
799 */
800 role = p->role_val_to_struct[c->role - 1];
801 if (!ebitmap_get_bit(&role->types,
802 c->type - 1))
803 /* role may not be associated with type */
804 return 0;
805
806 /*
807 * User must be authorized for the role.
808 */
809 usrdatum = p->user_val_to_struct[c->user - 1];
810 if (!usrdatum)
811 return 0;
812
813 if (!ebitmap_get_bit(&usrdatum->roles,
814 c->role - 1))
815 /* user may not be associated with role */
816 return 0;
817 }
818
819 if (!mls_context_isvalid(p, c))
820 return 0;
821
822 return 1;
823}
824
825/*
826 * Read a MLS range structure from a policydb binary
827 * representation file.
828 */
829static int mls_read_range_helper(struct mls_range *r, void *fp)
830{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700831 __le32 buf[2];
832 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int rc;
834
835 rc = next_entry(buf, fp, sizeof(u32));
836 if (rc < 0)
837 goto out;
838
839 items = le32_to_cpu(buf[0]);
840 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100841 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 rc = -EINVAL;
843 goto out;
844 }
845 rc = next_entry(buf, fp, sizeof(u32) * items);
846 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100847 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 }
850 r->level[0].sens = le32_to_cpu(buf[0]);
851 if (items > 1)
852 r->level[1].sens = le32_to_cpu(buf[1]);
853 else
854 r->level[1].sens = r->level[0].sens;
855
856 rc = ebitmap_read(&r->level[0].cat, fp);
857 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100858 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 "categories\n");
860 goto out;
861 }
862 if (items > 1) {
863 rc = ebitmap_read(&r->level[1].cat, fp);
864 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100865 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 "categories\n");
867 goto bad_high;
868 }
869 } else {
870 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
871 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100872 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto bad_high;
874 }
875 }
876
877 rc = 0;
878out:
879 return rc;
880bad_high:
881 ebitmap_destroy(&r->level[0].cat);
882 goto out;
883}
884
885/*
886 * Read and validate a security context structure
887 * from a policydb binary representation file.
888 */
889static int context_read_and_validate(struct context *c,
890 struct policydb *p,
891 void *fp)
892{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700893 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int rc;
895
896 rc = next_entry(buf, fp, sizeof buf);
897 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100898 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
901 c->user = le32_to_cpu(buf[0]);
902 c->role = le32_to_cpu(buf[1]);
903 c->type = le32_to_cpu(buf[2]);
904 if (p->policyvers >= POLICYDB_VERSION_MLS) {
905 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100906 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 "context\n");
908 rc = -EINVAL;
909 goto out;
910 }
911 }
912
913 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100914 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 context_destroy(c);
916 rc = -EINVAL;
917 }
918out:
919 return rc;
920}
921
922/*
923 * The following *_read functions are used to
924 * read the symbol data from a policy database
925 * binary representation file.
926 */
927
928static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
929{
930 char *key = NULL;
931 struct perm_datum *perdatum;
932 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700933 __le32 buf[2];
934 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
James Morris89d155e2005-10-30 14:59:21 -0800936 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!perdatum) {
938 rc = -ENOMEM;
939 goto out;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 rc = next_entry(buf, fp, sizeof buf);
943 if (rc < 0)
944 goto bad;
945
946 len = le32_to_cpu(buf[0]);
947 perdatum->value = le32_to_cpu(buf[1]);
948
Eric Paris2ced3df2008-04-17 13:37:12 -0400949 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!key) {
951 rc = -ENOMEM;
952 goto bad;
953 }
954 rc = next_entry(key, fp, len);
955 if (rc < 0)
956 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +0300957 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 rc = hashtab_insert(h, key, perdatum);
960 if (rc)
961 goto bad;
962out:
963 return rc;
964bad:
965 perm_destroy(key, perdatum, NULL);
966 goto out;
967}
968
969static int common_read(struct policydb *p, struct hashtab *h, void *fp)
970{
971 char *key = NULL;
972 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700973 __le32 buf[4];
974 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int i, rc;
976
James Morris89d155e2005-10-30 14:59:21 -0800977 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!comdatum) {
979 rc = -ENOMEM;
980 goto out;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 rc = next_entry(buf, fp, sizeof buf);
984 if (rc < 0)
985 goto bad;
986
987 len = le32_to_cpu(buf[0]);
988 comdatum->value = le32_to_cpu(buf[1]);
989
990 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
991 if (rc)
992 goto bad;
993 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
994 nel = le32_to_cpu(buf[3]);
995
Eric Paris2ced3df2008-04-17 13:37:12 -0400996 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!key) {
998 rc = -ENOMEM;
999 goto bad;
1000 }
1001 rc = next_entry(key, fp, len);
1002 if (rc < 0)
1003 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001004 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 for (i = 0; i < nel; i++) {
1007 rc = perm_read(p, comdatum->permissions.table, fp);
1008 if (rc)
1009 goto bad;
1010 }
1011
1012 rc = hashtab_insert(h, key, comdatum);
1013 if (rc)
1014 goto bad;
1015out:
1016 return rc;
1017bad:
1018 common_destroy(key, comdatum, NULL);
1019 goto out;
1020}
1021
1022static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001023 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct constraint_node *c, *lc;
1026 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001027 __le32 buf[3];
1028 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int rc, i, j, depth;
1030
1031 lc = NULL;
1032 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001033 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!c)
1035 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Eric Paris2ced3df2008-04-17 13:37:12 -04001037 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001039 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1043 if (rc < 0)
1044 return rc;
1045 c->permissions = le32_to_cpu(buf[0]);
1046 nexpr = le32_to_cpu(buf[1]);
1047 le = NULL;
1048 depth = -1;
1049 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001050 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!e)
1052 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric Paris2ced3df2008-04-17 13:37:12 -04001054 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1060 if (rc < 0)
1061 return rc;
1062 e->expr_type = le32_to_cpu(buf[0]);
1063 e->attr = le32_to_cpu(buf[1]);
1064 e->op = le32_to_cpu(buf[2]);
1065
1066 switch (e->expr_type) {
1067 case CEXPR_NOT:
1068 if (depth < 0)
1069 return -EINVAL;
1070 break;
1071 case CEXPR_AND:
1072 case CEXPR_OR:
1073 if (depth < 1)
1074 return -EINVAL;
1075 depth--;
1076 break;
1077 case CEXPR_ATTR:
1078 if (depth == (CEXPR_MAXDEPTH - 1))
1079 return -EINVAL;
1080 depth++;
1081 break;
1082 case CEXPR_NAMES:
1083 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1084 return -EINVAL;
1085 if (depth == (CEXPR_MAXDEPTH - 1))
1086 return -EINVAL;
1087 depth++;
1088 if (ebitmap_read(&e->names, fp))
1089 return -EINVAL;
1090 break;
1091 default:
1092 return -EINVAL;
1093 }
1094 le = e;
1095 }
1096 if (depth != 0)
1097 return -EINVAL;
1098 lc = c;
1099 }
1100
1101 return 0;
1102}
1103
1104static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1105{
1106 char *key = NULL;
1107 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001108 __le32 buf[6];
1109 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int i, rc;
1111
James Morris89d155e2005-10-30 14:59:21 -08001112 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!cladatum) {
1114 rc = -ENOMEM;
1115 goto out;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 rc = next_entry(buf, fp, sizeof(u32)*6);
1119 if (rc < 0)
1120 goto bad;
1121
1122 len = le32_to_cpu(buf[0]);
1123 len2 = le32_to_cpu(buf[1]);
1124 cladatum->value = le32_to_cpu(buf[2]);
1125
1126 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1127 if (rc)
1128 goto bad;
1129 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1130 nel = le32_to_cpu(buf[4]);
1131
1132 ncons = le32_to_cpu(buf[5]);
1133
Eric Paris2ced3df2008-04-17 13:37:12 -04001134 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!key) {
1136 rc = -ENOMEM;
1137 goto bad;
1138 }
1139 rc = next_entry(key, fp, len);
1140 if (rc < 0)
1141 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001142 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001145 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!cladatum->comkey) {
1147 rc = -ENOMEM;
1148 goto bad;
1149 }
1150 rc = next_entry(cladatum->comkey, fp, len2);
1151 if (rc < 0)
1152 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001153 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 cladatum->comdatum = hashtab_search(p->p_commons.table,
1156 cladatum->comkey);
1157 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001158 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 cladatum->comkey);
1160 rc = -EINVAL;
1161 goto bad;
1162 }
1163 }
1164 for (i = 0; i < nel; i++) {
1165 rc = perm_read(p, cladatum->permissions.table, fp);
1166 if (rc)
1167 goto bad;
1168 }
1169
1170 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1171 if (rc)
1172 goto bad;
1173
1174 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1175 /* grab the validatetrans rules */
1176 rc = next_entry(buf, fp, sizeof(u32));
1177 if (rc < 0)
1178 goto bad;
1179 ncons = le32_to_cpu(buf[0]);
1180 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1181 if (rc)
1182 goto bad;
1183 }
1184
1185 rc = hashtab_insert(h, key, cladatum);
1186 if (rc)
1187 goto bad;
1188
1189 rc = 0;
1190out:
1191 return rc;
1192bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001193 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
1195}
1196
1197static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1198{
1199 char *key = NULL;
1200 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001201 int rc, to_read = 2;
1202 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001203 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
James Morris89d155e2005-10-30 14:59:21 -08001205 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (!role) {
1207 rc = -ENOMEM;
1208 goto out;
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
KaiGai Koheid9250de2008-08-28 16:35:57 +09001211 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1212 to_read = 3;
1213
1214 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (rc < 0)
1216 goto bad;
1217
1218 len = le32_to_cpu(buf[0]);
1219 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001220 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1221 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Eric Paris2ced3df2008-04-17 13:37:12 -04001223 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!key) {
1225 rc = -ENOMEM;
1226 goto bad;
1227 }
1228 rc = next_entry(key, fp, len);
1229 if (rc < 0)
1230 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001231 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 rc = ebitmap_read(&role->dominates, fp);
1234 if (rc)
1235 goto bad;
1236
1237 rc = ebitmap_read(&role->types, fp);
1238 if (rc)
1239 goto bad;
1240
1241 if (strcmp(key, OBJECT_R) == 0) {
1242 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001243 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 OBJECT_R, role->value);
1245 rc = -EINVAL;
1246 goto bad;
1247 }
1248 rc = 0;
1249 goto bad;
1250 }
1251
1252 rc = hashtab_insert(h, key, role);
1253 if (rc)
1254 goto bad;
1255out:
1256 return rc;
1257bad:
1258 role_destroy(key, role, NULL);
1259 goto out;
1260}
1261
1262static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1263{
1264 char *key = NULL;
1265 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001266 int rc, to_read = 3;
1267 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001268 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Eric Paris2ced3df2008-04-17 13:37:12 -04001270 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!typdatum) {
1272 rc = -ENOMEM;
1273 return rc;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
KaiGai Koheid9250de2008-08-28 16:35:57 +09001276 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1277 to_read = 4;
1278
1279 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (rc < 0)
1281 goto bad;
1282
1283 len = le32_to_cpu(buf[0]);
1284 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001285 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1286 u32 prop = le32_to_cpu(buf[2]);
1287
1288 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1289 typdatum->primary = 1;
1290 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1291 typdatum->attribute = 1;
1292
1293 typdatum->bounds = le32_to_cpu(buf[3]);
1294 } else {
1295 typdatum->primary = le32_to_cpu(buf[2]);
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Eric Paris2ced3df2008-04-17 13:37:12 -04001298 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!key) {
1300 rc = -ENOMEM;
1301 goto bad;
1302 }
1303 rc = next_entry(key, fp, len);
1304 if (rc < 0)
1305 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001306 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 rc = hashtab_insert(h, key, typdatum);
1309 if (rc)
1310 goto bad;
1311out:
1312 return rc;
1313bad:
1314 type_destroy(key, typdatum, NULL);
1315 goto out;
1316}
1317
1318
1319/*
1320 * Read a MLS level structure from a policydb binary
1321 * representation file.
1322 */
1323static int mls_read_level(struct mls_level *lp, void *fp)
1324{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001325 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int rc;
1327
1328 memset(lp, 0, sizeof(*lp));
1329
1330 rc = next_entry(buf, fp, sizeof buf);
1331 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001332 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto bad;
1334 }
1335 lp->sens = le32_to_cpu(buf[0]);
1336
1337 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001338 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 "categories\n");
1340 goto bad;
1341 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344
1345bad:
1346 return -EINVAL;
1347}
1348
1349static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1350{
1351 char *key = NULL;
1352 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001353 int rc, to_read = 2;
1354 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001355 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
James Morris89d155e2005-10-30 14:59:21 -08001357 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!usrdatum) {
1359 rc = -ENOMEM;
1360 goto out;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
KaiGai Koheid9250de2008-08-28 16:35:57 +09001363 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1364 to_read = 3;
1365
1366 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (rc < 0)
1368 goto bad;
1369
1370 len = le32_to_cpu(buf[0]);
1371 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001372 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1373 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Eric Paris2ced3df2008-04-17 13:37:12 -04001375 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (!key) {
1377 rc = -ENOMEM;
1378 goto bad;
1379 }
1380 rc = next_entry(key, fp, len);
1381 if (rc < 0)
1382 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001383 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 rc = ebitmap_read(&usrdatum->roles, fp);
1386 if (rc)
1387 goto bad;
1388
1389 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1390 rc = mls_read_range_helper(&usrdatum->range, fp);
1391 if (rc)
1392 goto bad;
1393 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1394 if (rc)
1395 goto bad;
1396 }
1397
1398 rc = hashtab_insert(h, key, usrdatum);
1399 if (rc)
1400 goto bad;
1401out:
1402 return rc;
1403bad:
1404 user_destroy(key, usrdatum, NULL);
1405 goto out;
1406}
1407
1408static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1409{
1410 char *key = NULL;
1411 struct level_datum *levdatum;
1412 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001413 __le32 buf[2];
1414 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
James Morris89d155e2005-10-30 14:59:21 -08001416 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!levdatum) {
1418 rc = -ENOMEM;
1419 goto out;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 rc = next_entry(buf, fp, sizeof buf);
1423 if (rc < 0)
1424 goto bad;
1425
1426 len = le32_to_cpu(buf[0]);
1427 levdatum->isalias = le32_to_cpu(buf[1]);
1428
Eric Paris2ced3df2008-04-17 13:37:12 -04001429 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (!key) {
1431 rc = -ENOMEM;
1432 goto bad;
1433 }
1434 rc = next_entry(key, fp, len);
1435 if (rc < 0)
1436 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001437 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1440 if (!levdatum->level) {
1441 rc = -ENOMEM;
1442 goto bad;
1443 }
1444 if (mls_read_level(levdatum->level, fp)) {
1445 rc = -EINVAL;
1446 goto bad;
1447 }
1448
1449 rc = hashtab_insert(h, key, levdatum);
1450 if (rc)
1451 goto bad;
1452out:
1453 return rc;
1454bad:
1455 sens_destroy(key, levdatum, NULL);
1456 goto out;
1457}
1458
1459static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1460{
1461 char *key = NULL;
1462 struct cat_datum *catdatum;
1463 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001464 __le32 buf[3];
1465 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
James Morris89d155e2005-10-30 14:59:21 -08001467 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!catdatum) {
1469 rc = -ENOMEM;
1470 goto out;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 rc = next_entry(buf, fp, sizeof buf);
1474 if (rc < 0)
1475 goto bad;
1476
1477 len = le32_to_cpu(buf[0]);
1478 catdatum->value = le32_to_cpu(buf[1]);
1479 catdatum->isalias = le32_to_cpu(buf[2]);
1480
Eric Paris2ced3df2008-04-17 13:37:12 -04001481 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (!key) {
1483 rc = -ENOMEM;
1484 goto bad;
1485 }
1486 rc = next_entry(key, fp, len);
1487 if (rc < 0)
1488 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001489 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 rc = hashtab_insert(h, key, catdatum);
1492 if (rc)
1493 goto bad;
1494out:
1495 return rc;
1496
1497bad:
1498 cat_destroy(key, catdatum, NULL);
1499 goto out;
1500}
1501
1502static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1503{
1504 common_read,
1505 class_read,
1506 role_read,
1507 type_read,
1508 user_read,
1509 cond_read_bool,
1510 sens_read,
1511 cat_read,
1512};
1513
KaiGai Koheid9250de2008-08-28 16:35:57 +09001514static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1515{
1516 struct user_datum *upper, *user;
1517 struct policydb *p = datap;
1518 int depth = 0;
1519
1520 upper = user = datum;
1521 while (upper->bounds) {
1522 struct ebitmap_node *node;
1523 unsigned long bit;
1524
1525 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1526 printk(KERN_ERR "SELinux: user %s: "
1527 "too deep or looped boundary",
1528 (char *) key);
1529 return -EINVAL;
1530 }
1531
1532 upper = p->user_val_to_struct[upper->bounds - 1];
1533 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1534 if (ebitmap_get_bit(&upper->roles, bit))
1535 continue;
1536
1537 printk(KERN_ERR
1538 "SELinux: boundary violated policy: "
1539 "user=%s role=%s bounds=%s\n",
1540 p->p_user_val_to_name[user->value - 1],
1541 p->p_role_val_to_name[bit],
1542 p->p_user_val_to_name[upper->value - 1]);
1543
1544 return -EINVAL;
1545 }
1546 }
1547
1548 return 0;
1549}
1550
1551static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1552{
1553 struct role_datum *upper, *role;
1554 struct policydb *p = datap;
1555 int depth = 0;
1556
1557 upper = role = datum;
1558 while (upper->bounds) {
1559 struct ebitmap_node *node;
1560 unsigned long bit;
1561
1562 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1563 printk(KERN_ERR "SELinux: role %s: "
1564 "too deep or looped bounds\n",
1565 (char *) key);
1566 return -EINVAL;
1567 }
1568
1569 upper = p->role_val_to_struct[upper->bounds - 1];
1570 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1571 if (ebitmap_get_bit(&upper->types, bit))
1572 continue;
1573
1574 printk(KERN_ERR
1575 "SELinux: boundary violated policy: "
1576 "role=%s type=%s bounds=%s\n",
1577 p->p_role_val_to_name[role->value - 1],
1578 p->p_type_val_to_name[bit],
1579 p->p_role_val_to_name[upper->value - 1]);
1580
1581 return -EINVAL;
1582 }
1583 }
1584
1585 return 0;
1586}
1587
1588static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1589{
1590 struct type_datum *upper, *type;
1591 struct policydb *p = datap;
1592 int depth = 0;
1593
1594 upper = type = datum;
1595 while (upper->bounds) {
1596 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1597 printk(KERN_ERR "SELinux: type %s: "
1598 "too deep or looped boundary\n",
1599 (char *) key);
1600 return -EINVAL;
1601 }
1602
1603 upper = p->type_val_to_struct[upper->bounds - 1];
1604 if (upper->attribute) {
1605 printk(KERN_ERR "SELinux: type %s: "
1606 "bounded by attribute %s",
1607 (char *) key,
1608 p->p_type_val_to_name[upper->value - 1]);
1609 return -EINVAL;
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616static int policydb_bounds_sanity_check(struct policydb *p)
1617{
1618 int rc;
1619
1620 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1621 return 0;
1622
1623 rc = hashtab_map(p->p_users.table,
1624 user_bounds_sanity_check, p);
1625 if (rc)
1626 return rc;
1627
1628 rc = hashtab_map(p->p_roles.table,
1629 role_bounds_sanity_check, p);
1630 if (rc)
1631 return rc;
1632
1633 rc = hashtab_map(p->p_types.table,
1634 type_bounds_sanity_check, p);
1635 if (rc)
1636 return rc;
1637
1638 return 0;
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641extern int ss_initialized;
1642
1643/*
1644 * Read the configuration data from a policy database binary
1645 * representation file into a policy database structure.
1646 */
1647int policydb_read(struct policydb *p, void *fp)
1648{
1649 struct role_allow *ra, *lra;
1650 struct role_trans *tr, *ltr;
1651 struct ocontext *l, *c, *newc;
1652 struct genfs *genfs_p, *genfs, *newgenfs;
1653 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001654 __le32 buf[4];
1655 u32 nodebuf[8];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001656 u32 len, len2, config, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 char *policydb_str;
1658 struct policydb_compat_info *info;
1659 struct range_trans *rt, *lrt;
1660
1661 config = 0;
1662
1663 rc = policydb_init(p);
1664 if (rc)
1665 goto out;
1666
1667 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001668 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (rc < 0)
1670 goto bad;
1671
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001672 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001673 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001675 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto bad;
1677 }
1678
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001679 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001681 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 "match expected length %Zu\n",
1683 len, strlen(POLICYDB_STRING));
1684 goto bad;
1685 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001686 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001688 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 "string of length %d\n", len);
1690 rc = -ENOMEM;
1691 goto bad;
1692 }
1693 rc = next_entry(policydb_str, fp, len);
1694 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001695 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 kfree(policydb_str);
1697 goto bad;
1698 }
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001699 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001701 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 "my string %s\n", policydb_str, POLICYDB_STRING);
1703 kfree(policydb_str);
1704 goto bad;
1705 }
1706 /* Done with policydb_str. */
1707 kfree(policydb_str);
1708 policydb_str = NULL;
1709
1710 /* Read the version, config, and table sizes. */
1711 rc = next_entry(buf, fp, sizeof(u32)*4);
1712 if (rc < 0)
1713 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001715 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (p->policyvers < POLICYDB_VERSION_MIN ||
1717 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001718 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001719 "my version range %d-%d\n",
1720 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1721 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001724 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (ss_initialized && !selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001726 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1727 " and MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto bad;
1729 }
1730 selinux_mls_enabled = 1;
1731 config |= POLICYDB_CONFIG_MLS;
1732
1733 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001734 printk(KERN_ERR "SELinux: security policydb version %d "
1735 "(MLS) not backwards compatible\n",
1736 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto bad;
1738 }
1739 } else {
1740 if (ss_initialized && selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001741 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1742 " non-MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto bad;
1744 }
1745 }
Eric Paris3f120702007-09-21 14:37:10 -04001746 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1747 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Paul Moore3bb56b22008-01-29 08:38:19 -05001749 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1750 ebitmap_read(&p->policycaps, fp) != 0)
1751 goto bad;
1752
Eric Paris64dbf072008-03-31 12:17:33 +11001753 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1754 ebitmap_read(&p->permissive_map, fp) != 0)
1755 goto bad;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 info = policydb_lookup_compat(p->policyvers);
1758 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11001759 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 "for version %d\n", p->policyvers);
1761 goto bad;
1762 }
1763
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001764 if (le32_to_cpu(buf[2]) != info->sym_num ||
1765 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11001766 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001767 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1768 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 info->sym_num, info->ocon_num);
1770 goto bad;
1771 }
1772
1773 for (i = 0; i < info->sym_num; i++) {
1774 rc = next_entry(buf, fp, sizeof(u32)*2);
1775 if (rc < 0)
1776 goto bad;
1777 nprim = le32_to_cpu(buf[0]);
1778 nel = le32_to_cpu(buf[1]);
1779 for (j = 0; j < nel; j++) {
1780 rc = read_f[i](p, p->symtab[i].table, fp);
1781 if (rc)
1782 goto bad;
1783 }
1784
1785 p->symtab[i].nprim = nprim;
1786 }
1787
Stephen Smalley45e54212007-11-07 10:08:00 -05001788 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (rc)
1790 goto bad;
1791
1792 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1793 rc = cond_read_list(p, fp);
1794 if (rc)
1795 goto bad;
1796 }
1797
1798 rc = next_entry(buf, fp, sizeof(u32));
1799 if (rc < 0)
1800 goto bad;
1801 nel = le32_to_cpu(buf[0]);
1802 ltr = NULL;
1803 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001804 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!tr) {
1806 rc = -ENOMEM;
1807 goto bad;
1808 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001809 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04001811 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 rc = next_entry(buf, fp, sizeof(u32)*3);
1814 if (rc < 0)
1815 goto bad;
1816 tr->role = le32_to_cpu(buf[0]);
1817 tr->type = le32_to_cpu(buf[1]);
1818 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001819 if (!policydb_role_isvalid(p, tr->role) ||
1820 !policydb_type_isvalid(p, tr->type) ||
1821 !policydb_role_isvalid(p, tr->new_role)) {
1822 rc = -EINVAL;
1823 goto bad;
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ltr = tr;
1826 }
1827
1828 rc = next_entry(buf, fp, sizeof(u32));
1829 if (rc < 0)
1830 goto bad;
1831 nel = le32_to_cpu(buf[0]);
1832 lra = NULL;
1833 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001834 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (!ra) {
1836 rc = -ENOMEM;
1837 goto bad;
1838 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001839 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04001841 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 rc = next_entry(buf, fp, sizeof(u32)*2);
1844 if (rc < 0)
1845 goto bad;
1846 ra->role = le32_to_cpu(buf[0]);
1847 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001848 if (!policydb_role_isvalid(p, ra->role) ||
1849 !policydb_role_isvalid(p, ra->new_role)) {
1850 rc = -EINVAL;
1851 goto bad;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 lra = ra;
1854 }
1855
1856 rc = policydb_index_classes(p);
1857 if (rc)
1858 goto bad;
1859
1860 rc = policydb_index_others(p);
1861 if (rc)
1862 goto bad;
1863
1864 for (i = 0; i < info->ocon_num; i++) {
1865 rc = next_entry(buf, fp, sizeof(u32));
1866 if (rc < 0)
1867 goto bad;
1868 nel = le32_to_cpu(buf[0]);
1869 l = NULL;
1870 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001871 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (!c) {
1873 rc = -ENOMEM;
1874 goto bad;
1875 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001876 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001878 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 l = c;
1881 rc = -EINVAL;
1882 switch (i) {
1883 case OCON_ISID:
1884 rc = next_entry(buf, fp, sizeof(u32));
1885 if (rc < 0)
1886 goto bad;
1887 c->sid[0] = le32_to_cpu(buf[0]);
1888 rc = context_read_and_validate(&c->context[0], p, fp);
1889 if (rc)
1890 goto bad;
1891 break;
1892 case OCON_FS:
1893 case OCON_NETIF:
1894 rc = next_entry(buf, fp, sizeof(u32));
1895 if (rc < 0)
1896 goto bad;
1897 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001898 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (!c->u.name) {
1900 rc = -ENOMEM;
1901 goto bad;
1902 }
1903 rc = next_entry(c->u.name, fp, len);
1904 if (rc < 0)
1905 goto bad;
1906 c->u.name[len] = 0;
1907 rc = context_read_and_validate(&c->context[0], p, fp);
1908 if (rc)
1909 goto bad;
1910 rc = context_read_and_validate(&c->context[1], p, fp);
1911 if (rc)
1912 goto bad;
1913 break;
1914 case OCON_PORT:
1915 rc = next_entry(buf, fp, sizeof(u32)*3);
1916 if (rc < 0)
1917 goto bad;
1918 c->u.port.protocol = le32_to_cpu(buf[0]);
1919 c->u.port.low_port = le32_to_cpu(buf[1]);
1920 c->u.port.high_port = le32_to_cpu(buf[2]);
1921 rc = context_read_and_validate(&c->context[0], p, fp);
1922 if (rc)
1923 goto bad;
1924 break;
1925 case OCON_NODE:
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001926 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (rc < 0)
1928 goto bad;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001929 c->u.node.addr = nodebuf[0]; /* network order */
1930 c->u.node.mask = nodebuf[1]; /* network order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 rc = context_read_and_validate(&c->context[0], p, fp);
1932 if (rc)
1933 goto bad;
1934 break;
1935 case OCON_FSUSE:
1936 rc = next_entry(buf, fp, sizeof(u32)*2);
1937 if (rc < 0)
1938 goto bad;
1939 c->v.behavior = le32_to_cpu(buf[0]);
1940 if (c->v.behavior > SECURITY_FS_USE_NONE)
1941 goto bad;
1942 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001943 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!c->u.name) {
1945 rc = -ENOMEM;
1946 goto bad;
1947 }
1948 rc = next_entry(c->u.name, fp, len);
1949 if (rc < 0)
1950 goto bad;
1951 c->u.name[len] = 0;
1952 rc = context_read_and_validate(&c->context[0], p, fp);
1953 if (rc)
1954 goto bad;
1955 break;
1956 case OCON_NODE6: {
1957 int k;
1958
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001959 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (rc < 0)
1961 goto bad;
1962 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001963 c->u.node6.addr[k] = nodebuf[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001965 c->u.node6.mask[k] = nodebuf[k+4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (context_read_and_validate(&c->context[0], p, fp))
1967 goto bad;
1968 break;
1969 }
1970 }
1971 }
1972 }
1973
1974 rc = next_entry(buf, fp, sizeof(u32));
1975 if (rc < 0)
1976 goto bad;
1977 nel = le32_to_cpu(buf[0]);
1978 genfs_p = NULL;
1979 rc = -EINVAL;
1980 for (i = 0; i < nel; i++) {
1981 rc = next_entry(buf, fp, sizeof(u32));
1982 if (rc < 0)
1983 goto bad;
1984 len = le32_to_cpu(buf[0]);
James Morris89d155e2005-10-30 14:59:21 -08001985 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (!newgenfs) {
1987 rc = -ENOMEM;
1988 goto bad;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Eric Paris2ced3df2008-04-17 13:37:12 -04001991 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!newgenfs->fstype) {
1993 rc = -ENOMEM;
1994 kfree(newgenfs);
1995 goto bad;
1996 }
1997 rc = next_entry(newgenfs->fstype, fp, len);
1998 if (rc < 0) {
1999 kfree(newgenfs->fstype);
2000 kfree(newgenfs);
2001 goto bad;
2002 }
2003 newgenfs->fstype[len] = 0;
2004 for (genfs_p = NULL, genfs = p->genfs; genfs;
2005 genfs_p = genfs, genfs = genfs->next) {
2006 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
James Morris454d9722008-02-26 20:42:02 +11002007 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 "fstype %s\n", newgenfs->fstype);
2009 kfree(newgenfs->fstype);
2010 kfree(newgenfs);
2011 goto bad;
2012 }
2013 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2014 break;
2015 }
2016 newgenfs->next = genfs;
2017 if (genfs_p)
2018 genfs_p->next = newgenfs;
2019 else
2020 p->genfs = newgenfs;
2021 rc = next_entry(buf, fp, sizeof(u32));
2022 if (rc < 0)
2023 goto bad;
2024 nel2 = le32_to_cpu(buf[0]);
2025 for (j = 0; j < nel2; j++) {
2026 rc = next_entry(buf, fp, sizeof(u32));
2027 if (rc < 0)
2028 goto bad;
2029 len = le32_to_cpu(buf[0]);
2030
James Morris89d155e2005-10-30 14:59:21 -08002031 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!newc) {
2033 rc = -ENOMEM;
2034 goto bad;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Eric Paris2ced3df2008-04-17 13:37:12 -04002037 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (!newc->u.name) {
2039 rc = -ENOMEM;
2040 goto bad_newc;
2041 }
2042 rc = next_entry(newc->u.name, fp, len);
2043 if (rc < 0)
2044 goto bad_newc;
2045 newc->u.name[len] = 0;
2046 rc = next_entry(buf, fp, sizeof(u32));
2047 if (rc < 0)
2048 goto bad_newc;
2049 newc->v.sclass = le32_to_cpu(buf[0]);
2050 if (context_read_and_validate(&newc->context[0], p, fp))
2051 goto bad_newc;
2052 for (l = NULL, c = newgenfs->head; c;
2053 l = c, c = c->next) {
2054 if (!strcmp(newc->u.name, c->u.name) &&
2055 (!c->v.sclass || !newc->v.sclass ||
2056 newc->v.sclass == c->v.sclass)) {
James Morris454d9722008-02-26 20:42:02 +11002057 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 "entry (%s,%s)\n",
2059 newgenfs->fstype, c->u.name);
2060 goto bad_newc;
2061 }
2062 len = strlen(newc->u.name);
2063 len2 = strlen(c->u.name);
2064 if (len > len2)
2065 break;
2066 }
2067
2068 newc->next = c;
2069 if (l)
2070 l->next = newc;
2071 else
2072 newgenfs->head = newc;
2073 }
2074 }
2075
2076 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002077 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 rc = next_entry(buf, fp, sizeof(u32));
2079 if (rc < 0)
2080 goto bad;
2081 nel = le32_to_cpu(buf[0]);
2082 lrt = NULL;
2083 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002084 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (!rt) {
2086 rc = -ENOMEM;
2087 goto bad;
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (lrt)
2090 lrt->next = rt;
2091 else
2092 p->range_tr = rt;
2093 rc = next_entry(buf, fp, (sizeof(u32) * 2));
2094 if (rc < 0)
2095 goto bad;
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002096 rt->source_type = le32_to_cpu(buf[0]);
2097 rt->target_type = le32_to_cpu(buf[1]);
2098 if (new_rangetr) {
2099 rc = next_entry(buf, fp, sizeof(u32));
2100 if (rc < 0)
2101 goto bad;
2102 rt->target_class = le32_to_cpu(buf[0]);
2103 } else
2104 rt->target_class = SECCLASS_PROCESS;
Stephen Smalley45e54212007-11-07 10:08:00 -05002105 if (!policydb_type_isvalid(p, rt->source_type) ||
2106 !policydb_type_isvalid(p, rt->target_type) ||
2107 !policydb_class_isvalid(p, rt->target_class)) {
2108 rc = -EINVAL;
2109 goto bad;
2110 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002111 rc = mls_read_range_helper(&rt->target_range, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (rc)
2113 goto bad;
Stephen Smalley45e54212007-11-07 10:08:00 -05002114 if (!mls_range_isvalid(p, &rt->target_range)) {
James Morris454d9722008-02-26 20:42:02 +11002115 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
Stephen Smalley45e54212007-11-07 10:08:00 -05002116 goto bad;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 lrt = rt;
2119 }
2120 }
2121
Stephen Smalley782ebb92005-09-03 15:55:16 -07002122 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2123 if (!p->type_attr_map)
2124 goto bad;
2125
2126 for (i = 0; i < p->p_types.nprim; i++) {
2127 ebitmap_init(&p->type_attr_map[i]);
2128 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2129 if (ebitmap_read(&p->type_attr_map[i], fp))
2130 goto bad;
2131 }
2132 /* add the type itself as the degenerate case */
2133 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2134 goto bad;
2135 }
2136
KaiGai Koheid9250de2008-08-28 16:35:57 +09002137 rc = policydb_bounds_sanity_check(p);
2138 if (rc)
2139 goto bad;
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 rc = 0;
2142out:
2143 return rc;
2144bad_newc:
Eric Paris2ced3df2008-04-17 13:37:12 -04002145 ocontext_destroy(newc, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146bad:
2147 if (!rc)
2148 rc = -EINVAL;
2149 policydb_destroy(p);
2150 goto out;
2151}