blob: f03667213ea8d4c1d0cb94ed270c4da8e8752dea [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);
Paul Moore3bb56b22008-01-29 08:38:19 -0500716 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100717 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return;
720}
721
722/*
723 * Load the initial SIDs specified in a policy database
724 * structure into a SID table.
725 */
726int policydb_load_isids(struct policydb *p, struct sidtab *s)
727{
728 struct ocontext *head, *c;
729 int rc;
730
731 rc = sidtab_init(s);
732 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100733 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto out;
735 }
736
737 head = p->ocontexts[OCON_ISID];
738 for (c = head; c; c = c->next) {
739 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100740 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 "defined.\n", c->u.name);
742 rc = -EINVAL;
743 goto out;
744 }
745 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100746 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 "SID %s.\n", c->u.name);
748 rc = -EINVAL;
749 goto out;
750 }
751 }
752out:
753 return rc;
754}
755
Stephen Smalley45e54212007-11-07 10:08:00 -0500756int policydb_class_isvalid(struct policydb *p, unsigned int class)
757{
758 if (!class || class > p->p_classes.nprim)
759 return 0;
760 return 1;
761}
762
763int policydb_role_isvalid(struct policydb *p, unsigned int role)
764{
765 if (!role || role > p->p_roles.nprim)
766 return 0;
767 return 1;
768}
769
770int policydb_type_isvalid(struct policydb *p, unsigned int type)
771{
772 if (!type || type > p->p_types.nprim)
773 return 0;
774 return 1;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * Return 1 if the fields in the security context
779 * structure `c' are valid. Return 0 otherwise.
780 */
781int policydb_context_isvalid(struct policydb *p, struct context *c)
782{
783 struct role_datum *role;
784 struct user_datum *usrdatum;
785
786 if (!c->role || c->role > p->p_roles.nprim)
787 return 0;
788
789 if (!c->user || c->user > p->p_users.nprim)
790 return 0;
791
792 if (!c->type || c->type > p->p_types.nprim)
793 return 0;
794
795 if (c->role != OBJECT_R_VAL) {
796 /*
797 * Role must be authorized for the type.
798 */
799 role = p->role_val_to_struct[c->role - 1];
800 if (!ebitmap_get_bit(&role->types,
801 c->type - 1))
802 /* role may not be associated with type */
803 return 0;
804
805 /*
806 * User must be authorized for the role.
807 */
808 usrdatum = p->user_val_to_struct[c->user - 1];
809 if (!usrdatum)
810 return 0;
811
812 if (!ebitmap_get_bit(&usrdatum->roles,
813 c->role - 1))
814 /* user may not be associated with role */
815 return 0;
816 }
817
818 if (!mls_context_isvalid(p, c))
819 return 0;
820
821 return 1;
822}
823
824/*
825 * Read a MLS range structure from a policydb binary
826 * representation file.
827 */
828static int mls_read_range_helper(struct mls_range *r, void *fp)
829{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700830 __le32 buf[2];
831 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int rc;
833
834 rc = next_entry(buf, fp, sizeof(u32));
835 if (rc < 0)
836 goto out;
837
838 items = le32_to_cpu(buf[0]);
839 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100840 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 rc = -EINVAL;
842 goto out;
843 }
844 rc = next_entry(buf, fp, sizeof(u32) * items);
845 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100846 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
848 }
849 r->level[0].sens = le32_to_cpu(buf[0]);
850 if (items > 1)
851 r->level[1].sens = le32_to_cpu(buf[1]);
852 else
853 r->level[1].sens = r->level[0].sens;
854
855 rc = ebitmap_read(&r->level[0].cat, fp);
856 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100857 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 "categories\n");
859 goto out;
860 }
861 if (items > 1) {
862 rc = ebitmap_read(&r->level[1].cat, fp);
863 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100864 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 "categories\n");
866 goto bad_high;
867 }
868 } else {
869 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
870 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100871 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto bad_high;
873 }
874 }
875
876 rc = 0;
877out:
878 return rc;
879bad_high:
880 ebitmap_destroy(&r->level[0].cat);
881 goto out;
882}
883
884/*
885 * Read and validate a security context structure
886 * from a policydb binary representation file.
887 */
888static int context_read_and_validate(struct context *c,
889 struct policydb *p,
890 void *fp)
891{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700892 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int rc;
894
895 rc = next_entry(buf, fp, sizeof buf);
896 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100897 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto out;
899 }
900 c->user = le32_to_cpu(buf[0]);
901 c->role = le32_to_cpu(buf[1]);
902 c->type = le32_to_cpu(buf[2]);
903 if (p->policyvers >= POLICYDB_VERSION_MLS) {
904 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100905 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 "context\n");
907 rc = -EINVAL;
908 goto out;
909 }
910 }
911
912 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100913 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 context_destroy(c);
915 rc = -EINVAL;
916 }
917out:
918 return rc;
919}
920
921/*
922 * The following *_read functions are used to
923 * read the symbol data from a policy database
924 * binary representation file.
925 */
926
927static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
928{
929 char *key = NULL;
930 struct perm_datum *perdatum;
931 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700932 __le32 buf[2];
933 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
James Morris89d155e2005-10-30 14:59:21 -0800935 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!perdatum) {
937 rc = -ENOMEM;
938 goto out;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 rc = next_entry(buf, fp, sizeof buf);
942 if (rc < 0)
943 goto bad;
944
945 len = le32_to_cpu(buf[0]);
946 perdatum->value = le32_to_cpu(buf[1]);
947
Eric Paris2ced3df2008-04-17 13:37:12 -0400948 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!key) {
950 rc = -ENOMEM;
951 goto bad;
952 }
953 rc = next_entry(key, fp, len);
954 if (rc < 0)
955 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +0300956 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 rc = hashtab_insert(h, key, perdatum);
959 if (rc)
960 goto bad;
961out:
962 return rc;
963bad:
964 perm_destroy(key, perdatum, NULL);
965 goto out;
966}
967
968static int common_read(struct policydb *p, struct hashtab *h, void *fp)
969{
970 char *key = NULL;
971 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700972 __le32 buf[4];
973 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int i, rc;
975
James Morris89d155e2005-10-30 14:59:21 -0800976 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!comdatum) {
978 rc = -ENOMEM;
979 goto out;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 rc = next_entry(buf, fp, sizeof buf);
983 if (rc < 0)
984 goto bad;
985
986 len = le32_to_cpu(buf[0]);
987 comdatum->value = le32_to_cpu(buf[1]);
988
989 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
990 if (rc)
991 goto bad;
992 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
993 nel = le32_to_cpu(buf[3]);
994
Eric Paris2ced3df2008-04-17 13:37:12 -0400995 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!key) {
997 rc = -ENOMEM;
998 goto bad;
999 }
1000 rc = next_entry(key, fp, len);
1001 if (rc < 0)
1002 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001003 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 for (i = 0; i < nel; i++) {
1006 rc = perm_read(p, comdatum->permissions.table, fp);
1007 if (rc)
1008 goto bad;
1009 }
1010
1011 rc = hashtab_insert(h, key, comdatum);
1012 if (rc)
1013 goto bad;
1014out:
1015 return rc;
1016bad:
1017 common_destroy(key, comdatum, NULL);
1018 goto out;
1019}
1020
1021static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001022 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct constraint_node *c, *lc;
1025 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001026 __le32 buf[3];
1027 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 int rc, i, j, depth;
1029
1030 lc = NULL;
1031 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001032 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!c)
1034 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Eric Paris2ced3df2008-04-17 13:37:12 -04001036 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1042 if (rc < 0)
1043 return rc;
1044 c->permissions = le32_to_cpu(buf[0]);
1045 nexpr = le32_to_cpu(buf[1]);
1046 le = NULL;
1047 depth = -1;
1048 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001049 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!e)
1051 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Eric Paris2ced3df2008-04-17 13:37:12 -04001053 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001055 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1059 if (rc < 0)
1060 return rc;
1061 e->expr_type = le32_to_cpu(buf[0]);
1062 e->attr = le32_to_cpu(buf[1]);
1063 e->op = le32_to_cpu(buf[2]);
1064
1065 switch (e->expr_type) {
1066 case CEXPR_NOT:
1067 if (depth < 0)
1068 return -EINVAL;
1069 break;
1070 case CEXPR_AND:
1071 case CEXPR_OR:
1072 if (depth < 1)
1073 return -EINVAL;
1074 depth--;
1075 break;
1076 case CEXPR_ATTR:
1077 if (depth == (CEXPR_MAXDEPTH - 1))
1078 return -EINVAL;
1079 depth++;
1080 break;
1081 case CEXPR_NAMES:
1082 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1083 return -EINVAL;
1084 if (depth == (CEXPR_MAXDEPTH - 1))
1085 return -EINVAL;
1086 depth++;
1087 if (ebitmap_read(&e->names, fp))
1088 return -EINVAL;
1089 break;
1090 default:
1091 return -EINVAL;
1092 }
1093 le = e;
1094 }
1095 if (depth != 0)
1096 return -EINVAL;
1097 lc = c;
1098 }
1099
1100 return 0;
1101}
1102
1103static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1104{
1105 char *key = NULL;
1106 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001107 __le32 buf[6];
1108 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 int i, rc;
1110
James Morris89d155e2005-10-30 14:59:21 -08001111 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!cladatum) {
1113 rc = -ENOMEM;
1114 goto out;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 rc = next_entry(buf, fp, sizeof(u32)*6);
1118 if (rc < 0)
1119 goto bad;
1120
1121 len = le32_to_cpu(buf[0]);
1122 len2 = le32_to_cpu(buf[1]);
1123 cladatum->value = le32_to_cpu(buf[2]);
1124
1125 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1126 if (rc)
1127 goto bad;
1128 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1129 nel = le32_to_cpu(buf[4]);
1130
1131 ncons = le32_to_cpu(buf[5]);
1132
Eric Paris2ced3df2008-04-17 13:37:12 -04001133 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!key) {
1135 rc = -ENOMEM;
1136 goto bad;
1137 }
1138 rc = next_entry(key, fp, len);
1139 if (rc < 0)
1140 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001141 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001144 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!cladatum->comkey) {
1146 rc = -ENOMEM;
1147 goto bad;
1148 }
1149 rc = next_entry(cladatum->comkey, fp, len2);
1150 if (rc < 0)
1151 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001152 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 cladatum->comdatum = hashtab_search(p->p_commons.table,
1155 cladatum->comkey);
1156 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001157 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 cladatum->comkey);
1159 rc = -EINVAL;
1160 goto bad;
1161 }
1162 }
1163 for (i = 0; i < nel; i++) {
1164 rc = perm_read(p, cladatum->permissions.table, fp);
1165 if (rc)
1166 goto bad;
1167 }
1168
1169 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1170 if (rc)
1171 goto bad;
1172
1173 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1174 /* grab the validatetrans rules */
1175 rc = next_entry(buf, fp, sizeof(u32));
1176 if (rc < 0)
1177 goto bad;
1178 ncons = le32_to_cpu(buf[0]);
1179 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1180 if (rc)
1181 goto bad;
1182 }
1183
1184 rc = hashtab_insert(h, key, cladatum);
1185 if (rc)
1186 goto bad;
1187
1188 rc = 0;
1189out:
1190 return rc;
1191bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001192 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 goto out;
1194}
1195
1196static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1197{
1198 char *key = NULL;
1199 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001200 int rc, to_read = 2;
1201 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001202 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
James Morris89d155e2005-10-30 14:59:21 -08001204 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (!role) {
1206 rc = -ENOMEM;
1207 goto out;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
KaiGai Koheid9250de2008-08-28 16:35:57 +09001210 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1211 to_read = 3;
1212
1213 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (rc < 0)
1215 goto bad;
1216
1217 len = le32_to_cpu(buf[0]);
1218 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001219 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1220 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Eric Paris2ced3df2008-04-17 13:37:12 -04001222 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!key) {
1224 rc = -ENOMEM;
1225 goto bad;
1226 }
1227 rc = next_entry(key, fp, len);
1228 if (rc < 0)
1229 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001230 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 rc = ebitmap_read(&role->dominates, fp);
1233 if (rc)
1234 goto bad;
1235
1236 rc = ebitmap_read(&role->types, fp);
1237 if (rc)
1238 goto bad;
1239
1240 if (strcmp(key, OBJECT_R) == 0) {
1241 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001242 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 OBJECT_R, role->value);
1244 rc = -EINVAL;
1245 goto bad;
1246 }
1247 rc = 0;
1248 goto bad;
1249 }
1250
1251 rc = hashtab_insert(h, key, role);
1252 if (rc)
1253 goto bad;
1254out:
1255 return rc;
1256bad:
1257 role_destroy(key, role, NULL);
1258 goto out;
1259}
1260
1261static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1262{
1263 char *key = NULL;
1264 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001265 int rc, to_read = 3;
1266 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001267 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Eric Paris2ced3df2008-04-17 13:37:12 -04001269 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!typdatum) {
1271 rc = -ENOMEM;
1272 return rc;
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
KaiGai Koheid9250de2008-08-28 16:35:57 +09001275 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1276 to_read = 4;
1277
1278 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (rc < 0)
1280 goto bad;
1281
1282 len = le32_to_cpu(buf[0]);
1283 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001284 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1285 u32 prop = le32_to_cpu(buf[2]);
1286
1287 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1288 typdatum->primary = 1;
1289 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1290 typdatum->attribute = 1;
1291
1292 typdatum->bounds = le32_to_cpu(buf[3]);
1293 } else {
1294 typdatum->primary = le32_to_cpu(buf[2]);
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Eric Paris2ced3df2008-04-17 13:37:12 -04001297 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!key) {
1299 rc = -ENOMEM;
1300 goto bad;
1301 }
1302 rc = next_entry(key, fp, len);
1303 if (rc < 0)
1304 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001305 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 rc = hashtab_insert(h, key, typdatum);
1308 if (rc)
1309 goto bad;
1310out:
1311 return rc;
1312bad:
1313 type_destroy(key, typdatum, NULL);
1314 goto out;
1315}
1316
1317
1318/*
1319 * Read a MLS level structure from a policydb binary
1320 * representation file.
1321 */
1322static int mls_read_level(struct mls_level *lp, void *fp)
1323{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001324 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int rc;
1326
1327 memset(lp, 0, sizeof(*lp));
1328
1329 rc = next_entry(buf, fp, sizeof buf);
1330 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001331 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 goto bad;
1333 }
1334 lp->sens = le32_to_cpu(buf[0]);
1335
1336 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001337 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 "categories\n");
1339 goto bad;
1340 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343
1344bad:
1345 return -EINVAL;
1346}
1347
1348static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1349{
1350 char *key = NULL;
1351 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001352 int rc, to_read = 2;
1353 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001354 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
James Morris89d155e2005-10-30 14:59:21 -08001356 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!usrdatum) {
1358 rc = -ENOMEM;
1359 goto out;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
KaiGai Koheid9250de2008-08-28 16:35:57 +09001362 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1363 to_read = 3;
1364
1365 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (rc < 0)
1367 goto bad;
1368
1369 len = le32_to_cpu(buf[0]);
1370 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001371 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1372 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric Paris2ced3df2008-04-17 13:37:12 -04001374 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!key) {
1376 rc = -ENOMEM;
1377 goto bad;
1378 }
1379 rc = next_entry(key, fp, len);
1380 if (rc < 0)
1381 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001382 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 rc = ebitmap_read(&usrdatum->roles, fp);
1385 if (rc)
1386 goto bad;
1387
1388 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1389 rc = mls_read_range_helper(&usrdatum->range, fp);
1390 if (rc)
1391 goto bad;
1392 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1393 if (rc)
1394 goto bad;
1395 }
1396
1397 rc = hashtab_insert(h, key, usrdatum);
1398 if (rc)
1399 goto bad;
1400out:
1401 return rc;
1402bad:
1403 user_destroy(key, usrdatum, NULL);
1404 goto out;
1405}
1406
1407static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1408{
1409 char *key = NULL;
1410 struct level_datum *levdatum;
1411 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001412 __le32 buf[2];
1413 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
James Morris89d155e2005-10-30 14:59:21 -08001415 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!levdatum) {
1417 rc = -ENOMEM;
1418 goto out;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 rc = next_entry(buf, fp, sizeof buf);
1422 if (rc < 0)
1423 goto bad;
1424
1425 len = le32_to_cpu(buf[0]);
1426 levdatum->isalias = le32_to_cpu(buf[1]);
1427
Eric Paris2ced3df2008-04-17 13:37:12 -04001428 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!key) {
1430 rc = -ENOMEM;
1431 goto bad;
1432 }
1433 rc = next_entry(key, fp, len);
1434 if (rc < 0)
1435 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001436 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1439 if (!levdatum->level) {
1440 rc = -ENOMEM;
1441 goto bad;
1442 }
1443 if (mls_read_level(levdatum->level, fp)) {
1444 rc = -EINVAL;
1445 goto bad;
1446 }
1447
1448 rc = hashtab_insert(h, key, levdatum);
1449 if (rc)
1450 goto bad;
1451out:
1452 return rc;
1453bad:
1454 sens_destroy(key, levdatum, NULL);
1455 goto out;
1456}
1457
1458static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1459{
1460 char *key = NULL;
1461 struct cat_datum *catdatum;
1462 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001463 __le32 buf[3];
1464 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
James Morris89d155e2005-10-30 14:59:21 -08001466 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!catdatum) {
1468 rc = -ENOMEM;
1469 goto out;
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 rc = next_entry(buf, fp, sizeof buf);
1473 if (rc < 0)
1474 goto bad;
1475
1476 len = le32_to_cpu(buf[0]);
1477 catdatum->value = le32_to_cpu(buf[1]);
1478 catdatum->isalias = le32_to_cpu(buf[2]);
1479
Eric Paris2ced3df2008-04-17 13:37:12 -04001480 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!key) {
1482 rc = -ENOMEM;
1483 goto bad;
1484 }
1485 rc = next_entry(key, fp, len);
1486 if (rc < 0)
1487 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001488 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 rc = hashtab_insert(h, key, catdatum);
1491 if (rc)
1492 goto bad;
1493out:
1494 return rc;
1495
1496bad:
1497 cat_destroy(key, catdatum, NULL);
1498 goto out;
1499}
1500
1501static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1502{
1503 common_read,
1504 class_read,
1505 role_read,
1506 type_read,
1507 user_read,
1508 cond_read_bool,
1509 sens_read,
1510 cat_read,
1511};
1512
KaiGai Koheid9250de2008-08-28 16:35:57 +09001513static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1514{
1515 struct user_datum *upper, *user;
1516 struct policydb *p = datap;
1517 int depth = 0;
1518
1519 upper = user = datum;
1520 while (upper->bounds) {
1521 struct ebitmap_node *node;
1522 unsigned long bit;
1523
1524 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1525 printk(KERN_ERR "SELinux: user %s: "
1526 "too deep or looped boundary",
1527 (char *) key);
1528 return -EINVAL;
1529 }
1530
1531 upper = p->user_val_to_struct[upper->bounds - 1];
1532 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1533 if (ebitmap_get_bit(&upper->roles, bit))
1534 continue;
1535
1536 printk(KERN_ERR
1537 "SELinux: boundary violated policy: "
1538 "user=%s role=%s bounds=%s\n",
1539 p->p_user_val_to_name[user->value - 1],
1540 p->p_role_val_to_name[bit],
1541 p->p_user_val_to_name[upper->value - 1]);
1542
1543 return -EINVAL;
1544 }
1545 }
1546
1547 return 0;
1548}
1549
1550static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1551{
1552 struct role_datum *upper, *role;
1553 struct policydb *p = datap;
1554 int depth = 0;
1555
1556 upper = role = datum;
1557 while (upper->bounds) {
1558 struct ebitmap_node *node;
1559 unsigned long bit;
1560
1561 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1562 printk(KERN_ERR "SELinux: role %s: "
1563 "too deep or looped bounds\n",
1564 (char *) key);
1565 return -EINVAL;
1566 }
1567
1568 upper = p->role_val_to_struct[upper->bounds - 1];
1569 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1570 if (ebitmap_get_bit(&upper->types, bit))
1571 continue;
1572
1573 printk(KERN_ERR
1574 "SELinux: boundary violated policy: "
1575 "role=%s type=%s bounds=%s\n",
1576 p->p_role_val_to_name[role->value - 1],
1577 p->p_type_val_to_name[bit],
1578 p->p_role_val_to_name[upper->value - 1]);
1579
1580 return -EINVAL;
1581 }
1582 }
1583
1584 return 0;
1585}
1586
1587static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1588{
1589 struct type_datum *upper, *type;
1590 struct policydb *p = datap;
1591 int depth = 0;
1592
1593 upper = type = datum;
1594 while (upper->bounds) {
1595 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1596 printk(KERN_ERR "SELinux: type %s: "
1597 "too deep or looped boundary\n",
1598 (char *) key);
1599 return -EINVAL;
1600 }
1601
1602 upper = p->type_val_to_struct[upper->bounds - 1];
1603 if (upper->attribute) {
1604 printk(KERN_ERR "SELinux: type %s: "
1605 "bounded by attribute %s",
1606 (char *) key,
1607 p->p_type_val_to_name[upper->value - 1]);
1608 return -EINVAL;
1609 }
1610 }
1611
1612 return 0;
1613}
1614
1615static int policydb_bounds_sanity_check(struct policydb *p)
1616{
1617 int rc;
1618
1619 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1620 return 0;
1621
1622 rc = hashtab_map(p->p_users.table,
1623 user_bounds_sanity_check, p);
1624 if (rc)
1625 return rc;
1626
1627 rc = hashtab_map(p->p_roles.table,
1628 role_bounds_sanity_check, p);
1629 if (rc)
1630 return rc;
1631
1632 rc = hashtab_map(p->p_types.table,
1633 type_bounds_sanity_check, p);
1634 if (rc)
1635 return rc;
1636
1637 return 0;
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640extern int ss_initialized;
1641
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001642u16 string_to_security_class(struct policydb *p, const char *name)
1643{
1644 struct class_datum *cladatum;
1645
1646 cladatum = hashtab_search(p->p_classes.table, name);
1647 if (!cladatum)
1648 return 0;
1649
1650 return cladatum->value;
1651}
1652
1653u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1654{
1655 struct class_datum *cladatum;
1656 struct perm_datum *perdatum = NULL;
1657 struct common_datum *comdatum;
1658
1659 if (!tclass || tclass > p->p_classes.nprim)
1660 return 0;
1661
1662 cladatum = p->class_val_to_struct[tclass-1];
1663 comdatum = cladatum->comdatum;
1664 if (comdatum)
1665 perdatum = hashtab_search(comdatum->permissions.table,
1666 name);
1667 if (!perdatum)
1668 perdatum = hashtab_search(cladatum->permissions.table,
1669 name);
1670 if (!perdatum)
1671 return 0;
1672
1673 return 1U << (perdatum->value-1);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676/*
1677 * Read the configuration data from a policy database binary
1678 * representation file into a policy database structure.
1679 */
1680int policydb_read(struct policydb *p, void *fp)
1681{
1682 struct role_allow *ra, *lra;
1683 struct role_trans *tr, *ltr;
1684 struct ocontext *l, *c, *newc;
1685 struct genfs *genfs_p, *genfs, *newgenfs;
1686 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001687 __le32 buf[4];
1688 u32 nodebuf[8];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001689 u32 len, len2, config, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 char *policydb_str;
1691 struct policydb_compat_info *info;
1692 struct range_trans *rt, *lrt;
1693
1694 config = 0;
1695
1696 rc = policydb_init(p);
1697 if (rc)
1698 goto out;
1699
1700 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001701 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (rc < 0)
1703 goto bad;
1704
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001705 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001706 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001708 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto bad;
1710 }
1711
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001712 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001714 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 "match expected length %Zu\n",
1716 len, strlen(POLICYDB_STRING));
1717 goto bad;
1718 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001719 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001721 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 "string of length %d\n", len);
1723 rc = -ENOMEM;
1724 goto bad;
1725 }
1726 rc = next_entry(policydb_str, fp, len);
1727 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001728 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 kfree(policydb_str);
1730 goto bad;
1731 }
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001732 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001734 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 "my string %s\n", policydb_str, POLICYDB_STRING);
1736 kfree(policydb_str);
1737 goto bad;
1738 }
1739 /* Done with policydb_str. */
1740 kfree(policydb_str);
1741 policydb_str = NULL;
1742
1743 /* Read the version, config, and table sizes. */
1744 rc = next_entry(buf, fp, sizeof(u32)*4);
1745 if (rc < 0)
1746 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001748 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (p->policyvers < POLICYDB_VERSION_MIN ||
1750 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001751 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001752 "my version range %d-%d\n",
1753 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1754 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001757 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (ss_initialized && !selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001759 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1760 " and MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto bad;
1762 }
1763 selinux_mls_enabled = 1;
1764 config |= POLICYDB_CONFIG_MLS;
1765
1766 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001767 printk(KERN_ERR "SELinux: security policydb version %d "
1768 "(MLS) not backwards compatible\n",
1769 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto bad;
1771 }
1772 } else {
1773 if (ss_initialized && selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001774 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1775 " non-MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto bad;
1777 }
1778 }
Eric Paris3f120702007-09-21 14:37:10 -04001779 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1780 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Paul Moore3bb56b22008-01-29 08:38:19 -05001782 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1783 ebitmap_read(&p->policycaps, fp) != 0)
1784 goto bad;
1785
Eric Paris64dbf072008-03-31 12:17:33 +11001786 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1787 ebitmap_read(&p->permissive_map, fp) != 0)
1788 goto bad;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 info = policydb_lookup_compat(p->policyvers);
1791 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11001792 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 "for version %d\n", p->policyvers);
1794 goto bad;
1795 }
1796
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001797 if (le32_to_cpu(buf[2]) != info->sym_num ||
1798 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11001799 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001800 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1801 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 info->sym_num, info->ocon_num);
1803 goto bad;
1804 }
1805
1806 for (i = 0; i < info->sym_num; i++) {
1807 rc = next_entry(buf, fp, sizeof(u32)*2);
1808 if (rc < 0)
1809 goto bad;
1810 nprim = le32_to_cpu(buf[0]);
1811 nel = le32_to_cpu(buf[1]);
1812 for (j = 0; j < nel; j++) {
1813 rc = read_f[i](p, p->symtab[i].table, fp);
1814 if (rc)
1815 goto bad;
1816 }
1817
1818 p->symtab[i].nprim = nprim;
1819 }
1820
Stephen Smalley45e54212007-11-07 10:08:00 -05001821 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rc)
1823 goto bad;
1824
1825 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1826 rc = cond_read_list(p, fp);
1827 if (rc)
1828 goto bad;
1829 }
1830
1831 rc = next_entry(buf, fp, sizeof(u32));
1832 if (rc < 0)
1833 goto bad;
1834 nel = le32_to_cpu(buf[0]);
1835 ltr = NULL;
1836 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001837 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (!tr) {
1839 rc = -ENOMEM;
1840 goto bad;
1841 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001842 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04001844 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 rc = next_entry(buf, fp, sizeof(u32)*3);
1847 if (rc < 0)
1848 goto bad;
1849 tr->role = le32_to_cpu(buf[0]);
1850 tr->type = le32_to_cpu(buf[1]);
1851 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001852 if (!policydb_role_isvalid(p, tr->role) ||
1853 !policydb_type_isvalid(p, tr->type) ||
1854 !policydb_role_isvalid(p, tr->new_role)) {
1855 rc = -EINVAL;
1856 goto bad;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 ltr = tr;
1859 }
1860
1861 rc = next_entry(buf, fp, sizeof(u32));
1862 if (rc < 0)
1863 goto bad;
1864 nel = le32_to_cpu(buf[0]);
1865 lra = NULL;
1866 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001867 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (!ra) {
1869 rc = -ENOMEM;
1870 goto bad;
1871 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001872 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04001874 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 rc = next_entry(buf, fp, sizeof(u32)*2);
1877 if (rc < 0)
1878 goto bad;
1879 ra->role = le32_to_cpu(buf[0]);
1880 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001881 if (!policydb_role_isvalid(p, ra->role) ||
1882 !policydb_role_isvalid(p, ra->new_role)) {
1883 rc = -EINVAL;
1884 goto bad;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 lra = ra;
1887 }
1888
1889 rc = policydb_index_classes(p);
1890 if (rc)
1891 goto bad;
1892
1893 rc = policydb_index_others(p);
1894 if (rc)
1895 goto bad;
1896
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001897 p->process_class = string_to_security_class(p, "process");
1898 if (!p->process_class)
1899 goto bad;
1900 p->process_trans_perms = string_to_av_perm(p, p->process_class,
1901 "transition");
1902 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
1903 "dyntransition");
1904 if (!p->process_trans_perms)
1905 goto bad;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 for (i = 0; i < info->ocon_num; i++) {
1908 rc = next_entry(buf, fp, sizeof(u32));
1909 if (rc < 0)
1910 goto bad;
1911 nel = le32_to_cpu(buf[0]);
1912 l = NULL;
1913 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001914 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (!c) {
1916 rc = -ENOMEM;
1917 goto bad;
1918 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001919 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001921 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 l = c;
1924 rc = -EINVAL;
1925 switch (i) {
1926 case OCON_ISID:
1927 rc = next_entry(buf, fp, sizeof(u32));
1928 if (rc < 0)
1929 goto bad;
1930 c->sid[0] = le32_to_cpu(buf[0]);
1931 rc = context_read_and_validate(&c->context[0], p, fp);
1932 if (rc)
1933 goto bad;
1934 break;
1935 case OCON_FS:
1936 case OCON_NETIF:
1937 rc = next_entry(buf, fp, sizeof(u32));
1938 if (rc < 0)
1939 goto bad;
1940 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001941 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!c->u.name) {
1943 rc = -ENOMEM;
1944 goto bad;
1945 }
1946 rc = next_entry(c->u.name, fp, len);
1947 if (rc < 0)
1948 goto bad;
1949 c->u.name[len] = 0;
1950 rc = context_read_and_validate(&c->context[0], p, fp);
1951 if (rc)
1952 goto bad;
1953 rc = context_read_and_validate(&c->context[1], p, fp);
1954 if (rc)
1955 goto bad;
1956 break;
1957 case OCON_PORT:
1958 rc = next_entry(buf, fp, sizeof(u32)*3);
1959 if (rc < 0)
1960 goto bad;
1961 c->u.port.protocol = le32_to_cpu(buf[0]);
1962 c->u.port.low_port = le32_to_cpu(buf[1]);
1963 c->u.port.high_port = le32_to_cpu(buf[2]);
1964 rc = context_read_and_validate(&c->context[0], p, fp);
1965 if (rc)
1966 goto bad;
1967 break;
1968 case OCON_NODE:
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001969 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (rc < 0)
1971 goto bad;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001972 c->u.node.addr = nodebuf[0]; /* network order */
1973 c->u.node.mask = nodebuf[1]; /* network order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 rc = context_read_and_validate(&c->context[0], p, fp);
1975 if (rc)
1976 goto bad;
1977 break;
1978 case OCON_FSUSE:
1979 rc = next_entry(buf, fp, sizeof(u32)*2);
1980 if (rc < 0)
1981 goto bad;
1982 c->v.behavior = le32_to_cpu(buf[0]);
1983 if (c->v.behavior > SECURITY_FS_USE_NONE)
1984 goto bad;
1985 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001986 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (!c->u.name) {
1988 rc = -ENOMEM;
1989 goto bad;
1990 }
1991 rc = next_entry(c->u.name, fp, len);
1992 if (rc < 0)
1993 goto bad;
1994 c->u.name[len] = 0;
1995 rc = context_read_and_validate(&c->context[0], p, fp);
1996 if (rc)
1997 goto bad;
1998 break;
1999 case OCON_NODE6: {
2000 int k;
2001
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002002 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (rc < 0)
2004 goto bad;
2005 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002006 c->u.node6.addr[k] = nodebuf[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002008 c->u.node6.mask[k] = nodebuf[k+4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (context_read_and_validate(&c->context[0], p, fp))
2010 goto bad;
2011 break;
2012 }
2013 }
2014 }
2015 }
2016
2017 rc = next_entry(buf, fp, sizeof(u32));
2018 if (rc < 0)
2019 goto bad;
2020 nel = le32_to_cpu(buf[0]);
2021 genfs_p = NULL;
2022 rc = -EINVAL;
2023 for (i = 0; i < nel; i++) {
2024 rc = next_entry(buf, fp, sizeof(u32));
2025 if (rc < 0)
2026 goto bad;
2027 len = le32_to_cpu(buf[0]);
James Morris89d155e2005-10-30 14:59:21 -08002028 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (!newgenfs) {
2030 rc = -ENOMEM;
2031 goto bad;
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Eric Paris2ced3df2008-04-17 13:37:12 -04002034 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (!newgenfs->fstype) {
2036 rc = -ENOMEM;
2037 kfree(newgenfs);
2038 goto bad;
2039 }
2040 rc = next_entry(newgenfs->fstype, fp, len);
2041 if (rc < 0) {
2042 kfree(newgenfs->fstype);
2043 kfree(newgenfs);
2044 goto bad;
2045 }
2046 newgenfs->fstype[len] = 0;
2047 for (genfs_p = NULL, genfs = p->genfs; genfs;
2048 genfs_p = genfs, genfs = genfs->next) {
2049 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
James Morris454d9722008-02-26 20:42:02 +11002050 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "fstype %s\n", newgenfs->fstype);
2052 kfree(newgenfs->fstype);
2053 kfree(newgenfs);
2054 goto bad;
2055 }
2056 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2057 break;
2058 }
2059 newgenfs->next = genfs;
2060 if (genfs_p)
2061 genfs_p->next = newgenfs;
2062 else
2063 p->genfs = newgenfs;
2064 rc = next_entry(buf, fp, sizeof(u32));
2065 if (rc < 0)
2066 goto bad;
2067 nel2 = le32_to_cpu(buf[0]);
2068 for (j = 0; j < nel2; j++) {
2069 rc = next_entry(buf, fp, sizeof(u32));
2070 if (rc < 0)
2071 goto bad;
2072 len = le32_to_cpu(buf[0]);
2073
James Morris89d155e2005-10-30 14:59:21 -08002074 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!newc) {
2076 rc = -ENOMEM;
2077 goto bad;
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Eric Paris2ced3df2008-04-17 13:37:12 -04002080 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (!newc->u.name) {
2082 rc = -ENOMEM;
2083 goto bad_newc;
2084 }
2085 rc = next_entry(newc->u.name, fp, len);
2086 if (rc < 0)
2087 goto bad_newc;
2088 newc->u.name[len] = 0;
2089 rc = next_entry(buf, fp, sizeof(u32));
2090 if (rc < 0)
2091 goto bad_newc;
2092 newc->v.sclass = le32_to_cpu(buf[0]);
2093 if (context_read_and_validate(&newc->context[0], p, fp))
2094 goto bad_newc;
2095 for (l = NULL, c = newgenfs->head; c;
2096 l = c, c = c->next) {
2097 if (!strcmp(newc->u.name, c->u.name) &&
2098 (!c->v.sclass || !newc->v.sclass ||
2099 newc->v.sclass == c->v.sclass)) {
James Morris454d9722008-02-26 20:42:02 +11002100 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 "entry (%s,%s)\n",
2102 newgenfs->fstype, c->u.name);
2103 goto bad_newc;
2104 }
2105 len = strlen(newc->u.name);
2106 len2 = strlen(c->u.name);
2107 if (len > len2)
2108 break;
2109 }
2110
2111 newc->next = c;
2112 if (l)
2113 l->next = newc;
2114 else
2115 newgenfs->head = newc;
2116 }
2117 }
2118
2119 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002120 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 rc = next_entry(buf, fp, sizeof(u32));
2122 if (rc < 0)
2123 goto bad;
2124 nel = le32_to_cpu(buf[0]);
2125 lrt = NULL;
2126 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002127 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (!rt) {
2129 rc = -ENOMEM;
2130 goto bad;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (lrt)
2133 lrt->next = rt;
2134 else
2135 p->range_tr = rt;
2136 rc = next_entry(buf, fp, (sizeof(u32) * 2));
2137 if (rc < 0)
2138 goto bad;
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002139 rt->source_type = le32_to_cpu(buf[0]);
2140 rt->target_type = le32_to_cpu(buf[1]);
2141 if (new_rangetr) {
2142 rc = next_entry(buf, fp, sizeof(u32));
2143 if (rc < 0)
2144 goto bad;
2145 rt->target_class = le32_to_cpu(buf[0]);
2146 } else
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002147 rt->target_class = p->process_class;
Stephen Smalley45e54212007-11-07 10:08:00 -05002148 if (!policydb_type_isvalid(p, rt->source_type) ||
2149 !policydb_type_isvalid(p, rt->target_type) ||
2150 !policydb_class_isvalid(p, rt->target_class)) {
2151 rc = -EINVAL;
2152 goto bad;
2153 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002154 rc = mls_read_range_helper(&rt->target_range, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (rc)
2156 goto bad;
Stephen Smalley45e54212007-11-07 10:08:00 -05002157 if (!mls_range_isvalid(p, &rt->target_range)) {
James Morris454d9722008-02-26 20:42:02 +11002158 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
Stephen Smalley45e54212007-11-07 10:08:00 -05002159 goto bad;
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 lrt = rt;
2162 }
2163 }
2164
Stephen Smalley782ebb92005-09-03 15:55:16 -07002165 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2166 if (!p->type_attr_map)
2167 goto bad;
2168
2169 for (i = 0; i < p->p_types.nprim; i++) {
2170 ebitmap_init(&p->type_attr_map[i]);
2171 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2172 if (ebitmap_read(&p->type_attr_map[i], fp))
2173 goto bad;
2174 }
2175 /* add the type itself as the degenerate case */
2176 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2177 goto bad;
2178 }
2179
KaiGai Koheid9250de2008-08-28 16:35:57 +09002180 rc = policydb_bounds_sanity_check(p);
2181 if (rc)
2182 goto bad;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 rc = 0;
2185out:
2186 return rc;
2187bad_newc:
Eric Paris2ced3df2008-04-17 13:37:12 -04002188 ocontext_destroy(newc, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189bad:
2190 if (!rc)
2191 rc = -EINVAL;
2192 policydb_destroy(p);
2193 goto out;
2194}