blob: c57802a164d5d3b840a287a06a15e58793610c4a [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
Stephen Hemminger634a5392010-03-04 21:59:03 -080043static const char *symtab_name[SYM_NUM] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 "common prefixes",
45 "classes",
46 "roles",
47 "types",
48 "users",
49 "bools",
50 "levels",
51 "categories",
52};
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static unsigned int symtab_sizes[SYM_NUM] = {
56 2,
57 32,
58 16,
59 512,
60 128,
61 16,
62 16,
63 16,
64};
65
66struct policydb_compat_info {
67 int version;
68 int sym_num;
69 int ocon_num;
70};
71
72/* These need to be updated if SYM_NUM or OCON_NUM changes */
73static struct policydb_compat_info policydb_compat[] = {
74 {
Eric Paris2ced3df2008-04-17 13:37:12 -040075 .version = POLICYDB_VERSION_BASE,
76 .sym_num = SYM_NUM - 3,
77 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 },
79 {
Eric Paris2ced3df2008-04-17 13:37:12 -040080 .version = POLICYDB_VERSION_BOOL,
81 .sym_num = SYM_NUM - 2,
82 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 },
84 {
Eric Paris2ced3df2008-04-17 13:37:12 -040085 .version = POLICYDB_VERSION_IPV6,
86 .sym_num = SYM_NUM - 2,
87 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 },
89 {
Eric Paris2ced3df2008-04-17 13:37:12 -040090 .version = POLICYDB_VERSION_NLCLASS,
91 .sym_num = SYM_NUM - 2,
92 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 },
94 {
Eric Paris2ced3df2008-04-17 13:37:12 -040095 .version = POLICYDB_VERSION_MLS,
96 .sym_num = SYM_NUM,
97 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 },
Stephen Smalley782ebb92005-09-03 15:55:16 -070099 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400100 .version = POLICYDB_VERSION_AVTAB,
101 .sym_num = SYM_NUM,
102 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700103 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700104 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400105 .version = POLICYDB_VERSION_RANGETRANS,
106 .sym_num = SYM_NUM,
107 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700108 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500109 {
110 .version = POLICYDB_VERSION_POLCAP,
111 .sym_num = SYM_NUM,
112 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100113 },
114 {
115 .version = POLICYDB_VERSION_PERMISSIVE,
116 .sym_num = SYM_NUM,
117 .ocon_num = OCON_NUM,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900118 },
119 {
120 .version = POLICYDB_VERSION_BOUNDARY,
121 .sym_num = SYM_NUM,
122 .ocon_num = OCON_NUM,
123 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126static struct policydb_compat_info *policydb_lookup_compat(int version)
127{
128 int i;
129 struct policydb_compat_info *info = NULL;
130
Tobias Klauser32725ad2006-01-06 00:11:23 -0800131 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (policydb_compat[i].version == version) {
133 info = &policydb_compat[i];
134 break;
135 }
136 }
137 return info;
138}
139
140/*
141 * Initialize the role table.
142 */
143static int roles_init(struct policydb *p)
144{
145 char *key = NULL;
146 int rc;
147 struct role_datum *role;
148
James Morris89d155e2005-10-30 14:59:21 -0800149 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!role) {
151 rc = -ENOMEM;
152 goto out;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 role->value = ++p->p_roles.nprim;
155 if (role->value != OBJECT_R_VAL) {
156 rc = -EINVAL;
157 goto out_free_role;
158 }
Julia Lawallb3139bb2010-05-14 21:30:30 +0200159 key = kstrdup(OBJECT_R, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!key) {
161 rc = -ENOMEM;
162 goto out_free_role;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 rc = hashtab_insert(p->p_roles.table, key, role);
165 if (rc)
166 goto out_free_key;
167out:
168 return rc;
169
170out_free_key:
171 kfree(key);
172out_free_role:
173 kfree(role);
174 goto out;
175}
176
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500177static u32 rangetr_hash(struct hashtab *h, const void *k)
178{
179 const struct range_trans *key = k;
180 return (key->source_type + (key->target_type << 3) +
181 (key->target_class << 5)) & (h->size - 1);
182}
183
184static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
185{
186 const struct range_trans *key1 = k1, *key2 = k2;
187 return (key1->source_type != key2->source_type ||
188 key1->target_type != key2->target_type ||
189 key1->target_class != key2->target_class);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Initialize a policy database structure.
194 */
195static int policydb_init(struct policydb *p)
196{
197 int i, rc;
198
199 memset(p, 0, sizeof(*p));
200
201 for (i = 0; i < SYM_NUM; i++) {
202 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
203 if (rc)
204 goto out_free_symtab;
205 }
206
207 rc = avtab_init(&p->te_avtab);
208 if (rc)
209 goto out_free_symtab;
210
211 rc = roles_init(p);
212 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900213 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 rc = cond_policydb_init(p);
216 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900217 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500219 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
220 if (!p->range_tr)
221 goto out_free_symtab;
222
Paul Moore3bb56b22008-01-29 08:38:19 -0500223 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100224 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226out:
227 return rc;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out_free_symtab:
230 for (i = 0; i < SYM_NUM; i++)
231 hashtab_destroy(p->symtab[i].table);
232 goto out;
233}
234
235/*
236 * The following *_index functions are used to
237 * define the val_to_name and val_to_struct arrays
238 * in a policy database structure. The val_to_name
239 * arrays are used when converting security context
240 * structures into string representations. The
241 * val_to_struct arrays are used when the attributes
242 * of a class, role, or user are needed.
243 */
244
245static int common_index(void *key, void *datum, void *datap)
246{
247 struct policydb *p;
248 struct common_datum *comdatum;
249
250 comdatum = datum;
251 p = datap;
252 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
253 return -EINVAL;
254 p->p_common_val_to_name[comdatum->value - 1] = key;
255 return 0;
256}
257
258static int class_index(void *key, void *datum, void *datap)
259{
260 struct policydb *p;
261 struct class_datum *cladatum;
262
263 cladatum = datum;
264 p = datap;
265 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
266 return -EINVAL;
267 p->p_class_val_to_name[cladatum->value - 1] = key;
268 p->class_val_to_struct[cladatum->value - 1] = cladatum;
269 return 0;
270}
271
272static int role_index(void *key, void *datum, void *datap)
273{
274 struct policydb *p;
275 struct role_datum *role;
276
277 role = datum;
278 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900279 if (!role->value
280 || role->value > p->p_roles.nprim
281 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EINVAL;
283 p->p_role_val_to_name[role->value - 1] = key;
284 p->role_val_to_struct[role->value - 1] = role;
285 return 0;
286}
287
288static int type_index(void *key, void *datum, void *datap)
289{
290 struct policydb *p;
291 struct type_datum *typdatum;
292
293 typdatum = datum;
294 p = datap;
295
296 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900297 if (!typdatum->value
298 || typdatum->value > p->p_types.nprim
299 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EINVAL;
301 p->p_type_val_to_name[typdatum->value - 1] = key;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900302 p->type_val_to_struct[typdatum->value - 1] = typdatum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 return 0;
306}
307
308static int user_index(void *key, void *datum, void *datap)
309{
310 struct policydb *p;
311 struct user_datum *usrdatum;
312
313 usrdatum = datum;
314 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900315 if (!usrdatum->value
316 || usrdatum->value > p->p_users.nprim
317 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
319 p->p_user_val_to_name[usrdatum->value - 1] = key;
320 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
321 return 0;
322}
323
324static int sens_index(void *key, void *datum, void *datap)
325{
326 struct policydb *p;
327 struct level_datum *levdatum;
328
329 levdatum = datum;
330 p = datap;
331
332 if (!levdatum->isalias) {
333 if (!levdatum->level->sens ||
334 levdatum->level->sens > p->p_levels.nprim)
335 return -EINVAL;
336 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
337 }
338
339 return 0;
340}
341
342static int cat_index(void *key, void *datum, void *datap)
343{
344 struct policydb *p;
345 struct cat_datum *catdatum;
346
347 catdatum = datum;
348 p = datap;
349
350 if (!catdatum->isalias) {
351 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
352 return -EINVAL;
353 p->p_cat_val_to_name[catdatum->value - 1] = key;
354 }
355
356 return 0;
357}
358
359static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
360{
361 common_index,
362 class_index,
363 role_index,
364 type_index,
365 user_index,
366 cond_index_bool,
367 sens_index,
368 cat_index,
369};
370
371/*
372 * Define the common val_to_name array and the class
373 * val_to_name and val_to_struct arrays in a policy
374 * database structure.
375 *
376 * Caller must clean up upon failure.
377 */
378static int policydb_index_classes(struct policydb *p)
379{
380 int rc;
381
382 p->p_common_val_to_name =
383 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
384 if (!p->p_common_val_to_name) {
385 rc = -ENOMEM;
386 goto out;
387 }
388
389 rc = hashtab_map(p->p_commons.table, common_index, p);
390 if (rc)
391 goto out;
392
393 p->class_val_to_struct =
394 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
395 if (!p->class_val_to_struct) {
396 rc = -ENOMEM;
397 goto out;
398 }
399
400 p->p_class_val_to_name =
401 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
402 if (!p->p_class_val_to_name) {
403 rc = -ENOMEM;
404 goto out;
405 }
406
407 rc = hashtab_map(p->p_classes.table, class_index, p);
408out:
409 return rc;
410}
411
412#ifdef DEBUG_HASHES
413static void symtab_hash_eval(struct symtab *s)
414{
415 int i;
416
417 for (i = 0; i < SYM_NUM; i++) {
418 struct hashtab *h = s[i].table;
419 struct hashtab_info info;
420
421 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400422 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 "longest chain length %d\n", symtab_name[i], h->nel,
424 info.slots_used, h->size, info.max_chain_len);
425 }
426}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500427
428static void rangetr_hash_eval(struct hashtab *h)
429{
430 struct hashtab_info info;
431
432 hashtab_stat(h, &info);
433 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
434 "longest chain length %d\n", h->nel,
435 info.slots_used, h->size, info.max_chain_len);
436}
437#else
438static inline void rangetr_hash_eval(struct hashtab *h)
439{
440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#endif
442
443/*
444 * Define the other val_to_name and val_to_struct arrays
445 * in a policy database structure.
446 *
447 * Caller must clean up on failure.
448 */
449static int policydb_index_others(struct policydb *p)
450{
451 int i, rc = 0;
452
James Morris454d9722008-02-26 20:42:02 +1100453 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf52010-02-03 16:40:20 +0100455 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk(", %d sens, %d cats", p->p_levels.nprim,
457 p->p_cats.nprim);
458 printk("\n");
459
James Morris454d9722008-02-26 20:42:02 +1100460 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p->p_classes.nprim, p->te_avtab.nel);
462
463#ifdef DEBUG_HASHES
464 avtab_hash_eval(&p->te_avtab, "rules");
465 symtab_hash_eval(p->symtab);
466#endif
467
468 p->role_val_to_struct =
469 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400470 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!p->role_val_to_struct) {
472 rc = -ENOMEM;
473 goto out;
474 }
475
476 p->user_val_to_struct =
477 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400478 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (!p->user_val_to_struct) {
480 rc = -ENOMEM;
481 goto out;
482 }
483
KaiGai Koheid9250de2008-08-28 16:35:57 +0900484 p->type_val_to_struct =
485 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
486 GFP_KERNEL);
487 if (!p->type_val_to_struct) {
488 rc = -ENOMEM;
489 goto out;
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (cond_init_bool_indexes(p)) {
493 rc = -ENOMEM;
494 goto out;
495 }
496
497 for (i = SYM_ROLES; i < SYM_NUM; i++) {
498 p->sym_val_to_name[i] =
499 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
500 if (!p->sym_val_to_name[i]) {
501 rc = -ENOMEM;
502 goto out;
503 }
504 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
505 if (rc)
506 goto out;
507 }
508
509out:
510 return rc;
511}
512
513/*
514 * The following *_destroy functions are used to
515 * free any memory allocated for each kind of
516 * symbol data in the policy database.
517 */
518
519static int perm_destroy(void *key, void *datum, void *p)
520{
521 kfree(key);
522 kfree(datum);
523 return 0;
524}
525
526static int common_destroy(void *key, void *datum, void *p)
527{
528 struct common_datum *comdatum;
529
530 kfree(key);
531 comdatum = datum;
532 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
533 hashtab_destroy(comdatum->permissions.table);
534 kfree(datum);
535 return 0;
536}
537
James Morris6cbda6b2006-11-29 16:50:27 -0500538static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct class_datum *cladatum;
541 struct constraint_node *constraint, *ctemp;
542 struct constraint_expr *e, *etmp;
543
544 kfree(key);
545 cladatum = datum;
546 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
547 hashtab_destroy(cladatum->permissions.table);
548 constraint = cladatum->constraints;
549 while (constraint) {
550 e = constraint->expr;
551 while (e) {
552 ebitmap_destroy(&e->names);
553 etmp = e;
554 e = e->next;
555 kfree(etmp);
556 }
557 ctemp = constraint;
558 constraint = constraint->next;
559 kfree(ctemp);
560 }
561
562 constraint = cladatum->validatetrans;
563 while (constraint) {
564 e = constraint->expr;
565 while (e) {
566 ebitmap_destroy(&e->names);
567 etmp = e;
568 e = e->next;
569 kfree(etmp);
570 }
571 ctemp = constraint;
572 constraint = constraint->next;
573 kfree(ctemp);
574 }
575
576 kfree(cladatum->comkey);
577 kfree(datum);
578 return 0;
579}
580
581static int role_destroy(void *key, void *datum, void *p)
582{
583 struct role_datum *role;
584
585 kfree(key);
586 role = datum;
587 ebitmap_destroy(&role->dominates);
588 ebitmap_destroy(&role->types);
589 kfree(datum);
590 return 0;
591}
592
593static int type_destroy(void *key, void *datum, void *p)
594{
595 kfree(key);
596 kfree(datum);
597 return 0;
598}
599
600static int user_destroy(void *key, void *datum, void *p)
601{
602 struct user_datum *usrdatum;
603
604 kfree(key);
605 usrdatum = datum;
606 ebitmap_destroy(&usrdatum->roles);
607 ebitmap_destroy(&usrdatum->range.level[0].cat);
608 ebitmap_destroy(&usrdatum->range.level[1].cat);
609 ebitmap_destroy(&usrdatum->dfltlevel.cat);
610 kfree(datum);
611 return 0;
612}
613
614static int sens_destroy(void *key, void *datum, void *p)
615{
616 struct level_datum *levdatum;
617
618 kfree(key);
619 levdatum = datum;
620 ebitmap_destroy(&levdatum->level->cat);
621 kfree(levdatum->level);
622 kfree(datum);
623 return 0;
624}
625
626static int cat_destroy(void *key, void *datum, void *p)
627{
628 kfree(key);
629 kfree(datum);
630 return 0;
631}
632
633static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
634{
635 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500636 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 role_destroy,
638 type_destroy,
639 user_destroy,
640 cond_destroy_bool,
641 sens_destroy,
642 cat_destroy,
643};
644
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500645static int range_tr_destroy(void *key, void *datum, void *p)
646{
647 struct mls_range *rt = datum;
648 kfree(key);
649 ebitmap_destroy(&rt->level[0].cat);
650 ebitmap_destroy(&rt->level[1].cat);
651 kfree(datum);
652 cond_resched();
653 return 0;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static void ocontext_destroy(struct ocontext *c, int i)
657{
658 context_destroy(&c->context[0]);
659 context_destroy(&c->context[1]);
660 if (i == OCON_ISID || i == OCON_FS ||
661 i == OCON_NETIF || i == OCON_FSUSE)
662 kfree(c->u.name);
663 kfree(c);
664}
665
666/*
667 * Free any memory allocated by a policy database structure.
668 */
669void policydb_destroy(struct policydb *p)
670{
671 struct ocontext *c, *ctmp;
672 struct genfs *g, *gtmp;
673 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700674 struct role_allow *ra, *lra = NULL;
675 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400678 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
680 hashtab_destroy(p->symtab[i].table);
681 }
682
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700683 for (i = 0; i < SYM_NUM; i++)
684 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700686 kfree(p->class_val_to_struct);
687 kfree(p->role_val_to_struct);
688 kfree(p->user_val_to_struct);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900689 kfree(p->type_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 avtab_destroy(&p->te_avtab);
692
693 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400694 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 c = p->ocontexts[i];
696 while (c) {
697 ctmp = c;
698 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400699 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400701 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
704 g = p->genfs;
705 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400706 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(g->fstype);
708 c = g->head;
709 while (c) {
710 ctmp = c;
711 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400712 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 gtmp = g;
715 g = g->next;
716 kfree(gtmp);
717 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400718 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 cond_policydb_destroy(p);
721
Stephen Smalley782ebb92005-09-03 15:55:16 -0700722 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400723 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800724 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700725 ltr = tr;
726 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800727 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700728
Eric Paris2ced3df2008-04-17 13:37:12 -0400729 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400730 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800731 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700732 lra = ra;
733 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800734 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700735
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500736 hashtab_map(p->range_tr, range_tr_destroy, NULL);
737 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700738
Stephen Smalley282c1f52005-10-23 12:57:15 -0700739 if (p->type_attr_map) {
740 for (i = 0; i < p->p_types.nprim; i++)
741 ebitmap_destroy(&p->type_attr_map[i]);
742 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700743 kfree(p->type_attr_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500744 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100745 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return;
748}
749
750/*
751 * Load the initial SIDs specified in a policy database
752 * structure into a SID table.
753 */
754int policydb_load_isids(struct policydb *p, struct sidtab *s)
755{
756 struct ocontext *head, *c;
757 int rc;
758
759 rc = sidtab_init(s);
760 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100761 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto out;
763 }
764
765 head = p->ocontexts[OCON_ISID];
766 for (c = head; c; c = c->next) {
767 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100768 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 "defined.\n", c->u.name);
770 rc = -EINVAL;
771 goto out;
772 }
773 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100774 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 "SID %s.\n", c->u.name);
776 rc = -EINVAL;
777 goto out;
778 }
779 }
780out:
781 return rc;
782}
783
Stephen Smalley45e54212007-11-07 10:08:00 -0500784int policydb_class_isvalid(struct policydb *p, unsigned int class)
785{
786 if (!class || class > p->p_classes.nprim)
787 return 0;
788 return 1;
789}
790
791int policydb_role_isvalid(struct policydb *p, unsigned int role)
792{
793 if (!role || role > p->p_roles.nprim)
794 return 0;
795 return 1;
796}
797
798int policydb_type_isvalid(struct policydb *p, unsigned int type)
799{
800 if (!type || type > p->p_types.nprim)
801 return 0;
802 return 1;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * Return 1 if the fields in the security context
807 * structure `c' are valid. Return 0 otherwise.
808 */
809int policydb_context_isvalid(struct policydb *p, struct context *c)
810{
811 struct role_datum *role;
812 struct user_datum *usrdatum;
813
814 if (!c->role || c->role > p->p_roles.nprim)
815 return 0;
816
817 if (!c->user || c->user > p->p_users.nprim)
818 return 0;
819
820 if (!c->type || c->type > p->p_types.nprim)
821 return 0;
822
823 if (c->role != OBJECT_R_VAL) {
824 /*
825 * Role must be authorized for the type.
826 */
827 role = p->role_val_to_struct[c->role - 1];
828 if (!ebitmap_get_bit(&role->types,
829 c->type - 1))
830 /* role may not be associated with type */
831 return 0;
832
833 /*
834 * User must be authorized for the role.
835 */
836 usrdatum = p->user_val_to_struct[c->user - 1];
837 if (!usrdatum)
838 return 0;
839
840 if (!ebitmap_get_bit(&usrdatum->roles,
841 c->role - 1))
842 /* user may not be associated with role */
843 return 0;
844 }
845
846 if (!mls_context_isvalid(p, c))
847 return 0;
848
849 return 1;
850}
851
852/*
853 * Read a MLS range structure from a policydb binary
854 * representation file.
855 */
856static int mls_read_range_helper(struct mls_range *r, void *fp)
857{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700858 __le32 buf[2];
859 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int rc;
861
862 rc = next_entry(buf, fp, sizeof(u32));
863 if (rc < 0)
864 goto out;
865
866 items = le32_to_cpu(buf[0]);
867 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100868 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 rc = -EINVAL;
870 goto out;
871 }
872 rc = next_entry(buf, fp, sizeof(u32) * items);
873 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100874 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto out;
876 }
877 r->level[0].sens = le32_to_cpu(buf[0]);
878 if (items > 1)
879 r->level[1].sens = le32_to_cpu(buf[1]);
880 else
881 r->level[1].sens = r->level[0].sens;
882
883 rc = ebitmap_read(&r->level[0].cat, fp);
884 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100885 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 "categories\n");
887 goto out;
888 }
889 if (items > 1) {
890 rc = ebitmap_read(&r->level[1].cat, fp);
891 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100892 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 "categories\n");
894 goto bad_high;
895 }
896 } else {
897 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
898 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100899 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto bad_high;
901 }
902 }
903
904 rc = 0;
905out:
906 return rc;
907bad_high:
908 ebitmap_destroy(&r->level[0].cat);
909 goto out;
910}
911
912/*
913 * Read and validate a security context structure
914 * from a policydb binary representation file.
915 */
916static int context_read_and_validate(struct context *c,
917 struct policydb *p,
918 void *fp)
919{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700920 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int rc;
922
923 rc = next_entry(buf, fp, sizeof buf);
924 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100925 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
927 }
928 c->user = le32_to_cpu(buf[0]);
929 c->role = le32_to_cpu(buf[1]);
930 c->type = le32_to_cpu(buf[2]);
931 if (p->policyvers >= POLICYDB_VERSION_MLS) {
932 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100933 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 "context\n");
935 rc = -EINVAL;
936 goto out;
937 }
938 }
939
940 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100941 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 context_destroy(c);
943 rc = -EINVAL;
944 }
945out:
946 return rc;
947}
948
949/*
950 * The following *_read functions are used to
951 * read the symbol data from a policy database
952 * binary representation file.
953 */
954
955static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
956{
957 char *key = NULL;
958 struct perm_datum *perdatum;
959 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700960 __le32 buf[2];
961 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
James Morris89d155e2005-10-30 14:59:21 -0800963 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!perdatum) {
965 rc = -ENOMEM;
966 goto out;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 rc = next_entry(buf, fp, sizeof buf);
970 if (rc < 0)
971 goto bad;
972
973 len = le32_to_cpu(buf[0]);
974 perdatum->value = le32_to_cpu(buf[1]);
975
Eric Paris2ced3df2008-04-17 13:37:12 -0400976 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!key) {
978 rc = -ENOMEM;
979 goto bad;
980 }
981 rc = next_entry(key, fp, len);
982 if (rc < 0)
983 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +0300984 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 rc = hashtab_insert(h, key, perdatum);
987 if (rc)
988 goto bad;
989out:
990 return rc;
991bad:
992 perm_destroy(key, perdatum, NULL);
993 goto out;
994}
995
996static int common_read(struct policydb *p, struct hashtab *h, void *fp)
997{
998 char *key = NULL;
999 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001000 __le32 buf[4];
1001 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int i, rc;
1003
James Morris89d155e2005-10-30 14:59:21 -08001004 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!comdatum) {
1006 rc = -ENOMEM;
1007 goto out;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 rc = next_entry(buf, fp, sizeof buf);
1011 if (rc < 0)
1012 goto bad;
1013
1014 len = le32_to_cpu(buf[0]);
1015 comdatum->value = le32_to_cpu(buf[1]);
1016
1017 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1018 if (rc)
1019 goto bad;
1020 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1021 nel = le32_to_cpu(buf[3]);
1022
Eric Paris2ced3df2008-04-17 13:37:12 -04001023 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!key) {
1025 rc = -ENOMEM;
1026 goto bad;
1027 }
1028 rc = next_entry(key, fp, len);
1029 if (rc < 0)
1030 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001031 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 for (i = 0; i < nel; i++) {
1034 rc = perm_read(p, comdatum->permissions.table, fp);
1035 if (rc)
1036 goto bad;
1037 }
1038
1039 rc = hashtab_insert(h, key, comdatum);
1040 if (rc)
1041 goto bad;
1042out:
1043 return rc;
1044bad:
1045 common_destroy(key, comdatum, NULL);
1046 goto out;
1047}
1048
1049static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001050 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct constraint_node *c, *lc;
1053 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001054 __le32 buf[3];
1055 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int rc, i, j, depth;
1057
1058 lc = NULL;
1059 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001060 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!c)
1062 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Eric Paris2ced3df2008-04-17 13:37:12 -04001064 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1070 if (rc < 0)
1071 return rc;
1072 c->permissions = le32_to_cpu(buf[0]);
1073 nexpr = le32_to_cpu(buf[1]);
1074 le = NULL;
1075 depth = -1;
1076 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001077 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!e)
1079 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Paris2ced3df2008-04-17 13:37:12 -04001081 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001083 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1087 if (rc < 0)
1088 return rc;
1089 e->expr_type = le32_to_cpu(buf[0]);
1090 e->attr = le32_to_cpu(buf[1]);
1091 e->op = le32_to_cpu(buf[2]);
1092
1093 switch (e->expr_type) {
1094 case CEXPR_NOT:
1095 if (depth < 0)
1096 return -EINVAL;
1097 break;
1098 case CEXPR_AND:
1099 case CEXPR_OR:
1100 if (depth < 1)
1101 return -EINVAL;
1102 depth--;
1103 break;
1104 case CEXPR_ATTR:
1105 if (depth == (CEXPR_MAXDEPTH - 1))
1106 return -EINVAL;
1107 depth++;
1108 break;
1109 case CEXPR_NAMES:
1110 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1111 return -EINVAL;
1112 if (depth == (CEXPR_MAXDEPTH - 1))
1113 return -EINVAL;
1114 depth++;
1115 if (ebitmap_read(&e->names, fp))
1116 return -EINVAL;
1117 break;
1118 default:
1119 return -EINVAL;
1120 }
1121 le = e;
1122 }
1123 if (depth != 0)
1124 return -EINVAL;
1125 lc = c;
1126 }
1127
1128 return 0;
1129}
1130
1131static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1132{
1133 char *key = NULL;
1134 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001135 __le32 buf[6];
1136 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int i, rc;
1138
James Morris89d155e2005-10-30 14:59:21 -08001139 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!cladatum) {
1141 rc = -ENOMEM;
1142 goto out;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 rc = next_entry(buf, fp, sizeof(u32)*6);
1146 if (rc < 0)
1147 goto bad;
1148
1149 len = le32_to_cpu(buf[0]);
1150 len2 = le32_to_cpu(buf[1]);
1151 cladatum->value = le32_to_cpu(buf[2]);
1152
1153 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1154 if (rc)
1155 goto bad;
1156 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1157 nel = le32_to_cpu(buf[4]);
1158
1159 ncons = le32_to_cpu(buf[5]);
1160
Eric Paris2ced3df2008-04-17 13:37:12 -04001161 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!key) {
1163 rc = -ENOMEM;
1164 goto bad;
1165 }
1166 rc = next_entry(key, fp, len);
1167 if (rc < 0)
1168 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001169 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001172 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!cladatum->comkey) {
1174 rc = -ENOMEM;
1175 goto bad;
1176 }
1177 rc = next_entry(cladatum->comkey, fp, len2);
1178 if (rc < 0)
1179 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001180 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 cladatum->comdatum = hashtab_search(p->p_commons.table,
1183 cladatum->comkey);
1184 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001185 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 cladatum->comkey);
1187 rc = -EINVAL;
1188 goto bad;
1189 }
1190 }
1191 for (i = 0; i < nel; i++) {
1192 rc = perm_read(p, cladatum->permissions.table, fp);
1193 if (rc)
1194 goto bad;
1195 }
1196
1197 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1198 if (rc)
1199 goto bad;
1200
1201 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1202 /* grab the validatetrans rules */
1203 rc = next_entry(buf, fp, sizeof(u32));
1204 if (rc < 0)
1205 goto bad;
1206 ncons = le32_to_cpu(buf[0]);
1207 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1208 if (rc)
1209 goto bad;
1210 }
1211
1212 rc = hashtab_insert(h, key, cladatum);
1213 if (rc)
1214 goto bad;
1215
1216 rc = 0;
1217out:
1218 return rc;
1219bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001220 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto out;
1222}
1223
1224static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1225{
1226 char *key = NULL;
1227 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001228 int rc, to_read = 2;
1229 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001230 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
James Morris89d155e2005-10-30 14:59:21 -08001232 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!role) {
1234 rc = -ENOMEM;
1235 goto out;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
KaiGai Koheid9250de2008-08-28 16:35:57 +09001238 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1239 to_read = 3;
1240
1241 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (rc < 0)
1243 goto bad;
1244
1245 len = le32_to_cpu(buf[0]);
1246 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001247 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1248 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Eric Paris2ced3df2008-04-17 13:37:12 -04001250 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (!key) {
1252 rc = -ENOMEM;
1253 goto bad;
1254 }
1255 rc = next_entry(key, fp, len);
1256 if (rc < 0)
1257 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001258 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 rc = ebitmap_read(&role->dominates, fp);
1261 if (rc)
1262 goto bad;
1263
1264 rc = ebitmap_read(&role->types, fp);
1265 if (rc)
1266 goto bad;
1267
1268 if (strcmp(key, OBJECT_R) == 0) {
1269 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001270 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 OBJECT_R, role->value);
1272 rc = -EINVAL;
1273 goto bad;
1274 }
1275 rc = 0;
1276 goto bad;
1277 }
1278
1279 rc = hashtab_insert(h, key, role);
1280 if (rc)
1281 goto bad;
1282out:
1283 return rc;
1284bad:
1285 role_destroy(key, role, NULL);
1286 goto out;
1287}
1288
1289static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1290{
1291 char *key = NULL;
1292 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001293 int rc, to_read = 3;
1294 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001295 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Eric Paris2ced3df2008-04-17 13:37:12 -04001297 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!typdatum) {
1299 rc = -ENOMEM;
1300 return rc;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
KaiGai Koheid9250de2008-08-28 16:35:57 +09001303 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1304 to_read = 4;
1305
1306 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (rc < 0)
1308 goto bad;
1309
1310 len = le32_to_cpu(buf[0]);
1311 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001312 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1313 u32 prop = le32_to_cpu(buf[2]);
1314
1315 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1316 typdatum->primary = 1;
1317 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1318 typdatum->attribute = 1;
1319
1320 typdatum->bounds = le32_to_cpu(buf[3]);
1321 } else {
1322 typdatum->primary = le32_to_cpu(buf[2]);
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Eric Paris2ced3df2008-04-17 13:37:12 -04001325 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!key) {
1327 rc = -ENOMEM;
1328 goto bad;
1329 }
1330 rc = next_entry(key, fp, len);
1331 if (rc < 0)
1332 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001333 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 rc = hashtab_insert(h, key, typdatum);
1336 if (rc)
1337 goto bad;
1338out:
1339 return rc;
1340bad:
1341 type_destroy(key, typdatum, NULL);
1342 goto out;
1343}
1344
1345
1346/*
1347 * Read a MLS level structure from a policydb binary
1348 * representation file.
1349 */
1350static int mls_read_level(struct mls_level *lp, void *fp)
1351{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001352 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int rc;
1354
1355 memset(lp, 0, sizeof(*lp));
1356
1357 rc = next_entry(buf, fp, sizeof buf);
1358 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001359 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto bad;
1361 }
1362 lp->sens = le32_to_cpu(buf[0]);
1363
1364 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001365 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 "categories\n");
1367 goto bad;
1368 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
1371
1372bad:
1373 return -EINVAL;
1374}
1375
1376static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1377{
1378 char *key = NULL;
1379 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001380 int rc, to_read = 2;
1381 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001382 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
James Morris89d155e2005-10-30 14:59:21 -08001384 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!usrdatum) {
1386 rc = -ENOMEM;
1387 goto out;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
KaiGai Koheid9250de2008-08-28 16:35:57 +09001390 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1391 to_read = 3;
1392
1393 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (rc < 0)
1395 goto bad;
1396
1397 len = le32_to_cpu(buf[0]);
1398 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001399 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1400 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Eric Paris2ced3df2008-04-17 13:37:12 -04001402 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!key) {
1404 rc = -ENOMEM;
1405 goto bad;
1406 }
1407 rc = next_entry(key, fp, len);
1408 if (rc < 0)
1409 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001410 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 rc = ebitmap_read(&usrdatum->roles, fp);
1413 if (rc)
1414 goto bad;
1415
1416 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1417 rc = mls_read_range_helper(&usrdatum->range, fp);
1418 if (rc)
1419 goto bad;
1420 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1421 if (rc)
1422 goto bad;
1423 }
1424
1425 rc = hashtab_insert(h, key, usrdatum);
1426 if (rc)
1427 goto bad;
1428out:
1429 return rc;
1430bad:
1431 user_destroy(key, usrdatum, NULL);
1432 goto out;
1433}
1434
1435static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1436{
1437 char *key = NULL;
1438 struct level_datum *levdatum;
1439 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001440 __le32 buf[2];
1441 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
James Morris89d155e2005-10-30 14:59:21 -08001443 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!levdatum) {
1445 rc = -ENOMEM;
1446 goto out;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 rc = next_entry(buf, fp, sizeof buf);
1450 if (rc < 0)
1451 goto bad;
1452
1453 len = le32_to_cpu(buf[0]);
1454 levdatum->isalias = le32_to_cpu(buf[1]);
1455
Eric Paris2ced3df2008-04-17 13:37:12 -04001456 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (!key) {
1458 rc = -ENOMEM;
1459 goto bad;
1460 }
1461 rc = next_entry(key, fp, len);
1462 if (rc < 0)
1463 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001464 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1467 if (!levdatum->level) {
1468 rc = -ENOMEM;
1469 goto bad;
1470 }
1471 if (mls_read_level(levdatum->level, fp)) {
1472 rc = -EINVAL;
1473 goto bad;
1474 }
1475
1476 rc = hashtab_insert(h, key, levdatum);
1477 if (rc)
1478 goto bad;
1479out:
1480 return rc;
1481bad:
1482 sens_destroy(key, levdatum, NULL);
1483 goto out;
1484}
1485
1486static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1487{
1488 char *key = NULL;
1489 struct cat_datum *catdatum;
1490 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001491 __le32 buf[3];
1492 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
James Morris89d155e2005-10-30 14:59:21 -08001494 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!catdatum) {
1496 rc = -ENOMEM;
1497 goto out;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 rc = next_entry(buf, fp, sizeof buf);
1501 if (rc < 0)
1502 goto bad;
1503
1504 len = le32_to_cpu(buf[0]);
1505 catdatum->value = le32_to_cpu(buf[1]);
1506 catdatum->isalias = le32_to_cpu(buf[2]);
1507
Eric Paris2ced3df2008-04-17 13:37:12 -04001508 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (!key) {
1510 rc = -ENOMEM;
1511 goto bad;
1512 }
1513 rc = next_entry(key, fp, len);
1514 if (rc < 0)
1515 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001516 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 rc = hashtab_insert(h, key, catdatum);
1519 if (rc)
1520 goto bad;
1521out:
1522 return rc;
1523
1524bad:
1525 cat_destroy(key, catdatum, NULL);
1526 goto out;
1527}
1528
1529static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1530{
1531 common_read,
1532 class_read,
1533 role_read,
1534 type_read,
1535 user_read,
1536 cond_read_bool,
1537 sens_read,
1538 cat_read,
1539};
1540
KaiGai Koheid9250de2008-08-28 16:35:57 +09001541static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1542{
1543 struct user_datum *upper, *user;
1544 struct policydb *p = datap;
1545 int depth = 0;
1546
1547 upper = user = datum;
1548 while (upper->bounds) {
1549 struct ebitmap_node *node;
1550 unsigned long bit;
1551
1552 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1553 printk(KERN_ERR "SELinux: user %s: "
1554 "too deep or looped boundary",
1555 (char *) key);
1556 return -EINVAL;
1557 }
1558
1559 upper = p->user_val_to_struct[upper->bounds - 1];
1560 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1561 if (ebitmap_get_bit(&upper->roles, bit))
1562 continue;
1563
1564 printk(KERN_ERR
1565 "SELinux: boundary violated policy: "
1566 "user=%s role=%s bounds=%s\n",
1567 p->p_user_val_to_name[user->value - 1],
1568 p->p_role_val_to_name[bit],
1569 p->p_user_val_to_name[upper->value - 1]);
1570
1571 return -EINVAL;
1572 }
1573 }
1574
1575 return 0;
1576}
1577
1578static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1579{
1580 struct role_datum *upper, *role;
1581 struct policydb *p = datap;
1582 int depth = 0;
1583
1584 upper = role = datum;
1585 while (upper->bounds) {
1586 struct ebitmap_node *node;
1587 unsigned long bit;
1588
1589 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1590 printk(KERN_ERR "SELinux: role %s: "
1591 "too deep or looped bounds\n",
1592 (char *) key);
1593 return -EINVAL;
1594 }
1595
1596 upper = p->role_val_to_struct[upper->bounds - 1];
1597 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1598 if (ebitmap_get_bit(&upper->types, bit))
1599 continue;
1600
1601 printk(KERN_ERR
1602 "SELinux: boundary violated policy: "
1603 "role=%s type=%s bounds=%s\n",
1604 p->p_role_val_to_name[role->value - 1],
1605 p->p_type_val_to_name[bit],
1606 p->p_role_val_to_name[upper->value - 1]);
1607
1608 return -EINVAL;
1609 }
1610 }
1611
1612 return 0;
1613}
1614
1615static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1616{
1617 struct type_datum *upper, *type;
1618 struct policydb *p = datap;
1619 int depth = 0;
1620
1621 upper = type = datum;
1622 while (upper->bounds) {
1623 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1624 printk(KERN_ERR "SELinux: type %s: "
1625 "too deep or looped boundary\n",
1626 (char *) key);
1627 return -EINVAL;
1628 }
1629
1630 upper = p->type_val_to_struct[upper->bounds - 1];
1631 if (upper->attribute) {
1632 printk(KERN_ERR "SELinux: type %s: "
1633 "bounded by attribute %s",
1634 (char *) key,
1635 p->p_type_val_to_name[upper->value - 1]);
1636 return -EINVAL;
1637 }
1638 }
1639
1640 return 0;
1641}
1642
1643static int policydb_bounds_sanity_check(struct policydb *p)
1644{
1645 int rc;
1646
1647 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1648 return 0;
1649
1650 rc = hashtab_map(p->p_users.table,
1651 user_bounds_sanity_check, p);
1652 if (rc)
1653 return rc;
1654
1655 rc = hashtab_map(p->p_roles.table,
1656 role_bounds_sanity_check, p);
1657 if (rc)
1658 return rc;
1659
1660 rc = hashtab_map(p->p_types.table,
1661 type_bounds_sanity_check, p);
1662 if (rc)
1663 return rc;
1664
1665 return 0;
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668extern int ss_initialized;
1669
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001670u16 string_to_security_class(struct policydb *p, const char *name)
1671{
1672 struct class_datum *cladatum;
1673
1674 cladatum = hashtab_search(p->p_classes.table, name);
1675 if (!cladatum)
1676 return 0;
1677
1678 return cladatum->value;
1679}
1680
1681u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1682{
1683 struct class_datum *cladatum;
1684 struct perm_datum *perdatum = NULL;
1685 struct common_datum *comdatum;
1686
1687 if (!tclass || tclass > p->p_classes.nprim)
1688 return 0;
1689
1690 cladatum = p->class_val_to_struct[tclass-1];
1691 comdatum = cladatum->comdatum;
1692 if (comdatum)
1693 perdatum = hashtab_search(comdatum->permissions.table,
1694 name);
1695 if (!perdatum)
1696 perdatum = hashtab_search(cladatum->permissions.table,
1697 name);
1698 if (!perdatum)
1699 return 0;
1700
1701 return 1U << (perdatum->value-1);
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/*
1705 * Read the configuration data from a policy database binary
1706 * representation file into a policy database structure.
1707 */
1708int policydb_read(struct policydb *p, void *fp)
1709{
1710 struct role_allow *ra, *lra;
1711 struct role_trans *tr, *ltr;
1712 struct ocontext *l, *c, *newc;
1713 struct genfs *genfs_p, *genfs, *newgenfs;
1714 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001715 __le32 buf[4];
1716 u32 nodebuf[8];
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01001717 u32 len, len2, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 char *policydb_str;
1719 struct policydb_compat_info *info;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05001720 struct range_trans *rt;
1721 struct mls_range *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 rc = policydb_init(p);
1724 if (rc)
1725 goto out;
1726
1727 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001728 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (rc < 0)
1730 goto bad;
1731
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001732 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001733 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001735 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto bad;
1737 }
1738
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001739 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001741 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 "match expected length %Zu\n",
1743 len, strlen(POLICYDB_STRING));
1744 goto bad;
1745 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001746 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001748 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 "string of length %d\n", len);
1750 rc = -ENOMEM;
1751 goto bad;
1752 }
1753 rc = next_entry(policydb_str, fp, len);
1754 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001755 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 kfree(policydb_str);
1757 goto bad;
1758 }
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001759 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001761 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 "my string %s\n", policydb_str, POLICYDB_STRING);
1763 kfree(policydb_str);
1764 goto bad;
1765 }
1766 /* Done with policydb_str. */
1767 kfree(policydb_str);
1768 policydb_str = NULL;
1769
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01001770 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rc = next_entry(buf, fp, sizeof(u32)*4);
1772 if (rc < 0)
1773 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001775 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (p->policyvers < POLICYDB_VERSION_MIN ||
1777 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001778 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001779 "my version range %d-%d\n",
1780 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1781 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001784 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01001785 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001788 printk(KERN_ERR "SELinux: security policydb version %d "
1789 "(MLS) not backwards compatible\n",
1790 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 goto bad;
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Eric Paris3f120702007-09-21 14:37:10 -04001794 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1795 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Paul Moore3bb56b22008-01-29 08:38:19 -05001797 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1798 ebitmap_read(&p->policycaps, fp) != 0)
1799 goto bad;
1800
Eric Paris64dbf072008-03-31 12:17:33 +11001801 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1802 ebitmap_read(&p->permissive_map, fp) != 0)
1803 goto bad;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 info = policydb_lookup_compat(p->policyvers);
1806 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11001807 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 "for version %d\n", p->policyvers);
1809 goto bad;
1810 }
1811
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001812 if (le32_to_cpu(buf[2]) != info->sym_num ||
1813 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11001814 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001815 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1816 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 info->sym_num, info->ocon_num);
1818 goto bad;
1819 }
1820
1821 for (i = 0; i < info->sym_num; i++) {
1822 rc = next_entry(buf, fp, sizeof(u32)*2);
1823 if (rc < 0)
1824 goto bad;
1825 nprim = le32_to_cpu(buf[0]);
1826 nel = le32_to_cpu(buf[1]);
1827 for (j = 0; j < nel; j++) {
1828 rc = read_f[i](p, p->symtab[i].table, fp);
1829 if (rc)
1830 goto bad;
1831 }
1832
1833 p->symtab[i].nprim = nprim;
1834 }
1835
Stephen Smalley45e54212007-11-07 10:08:00 -05001836 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (rc)
1838 goto bad;
1839
1840 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1841 rc = cond_read_list(p, fp);
1842 if (rc)
1843 goto bad;
1844 }
1845
1846 rc = next_entry(buf, fp, sizeof(u32));
1847 if (rc < 0)
1848 goto bad;
1849 nel = le32_to_cpu(buf[0]);
1850 ltr = NULL;
1851 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001852 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (!tr) {
1854 rc = -ENOMEM;
1855 goto bad;
1856 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001857 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04001859 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 rc = next_entry(buf, fp, sizeof(u32)*3);
1862 if (rc < 0)
1863 goto bad;
1864 tr->role = le32_to_cpu(buf[0]);
1865 tr->type = le32_to_cpu(buf[1]);
1866 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001867 if (!policydb_role_isvalid(p, tr->role) ||
1868 !policydb_type_isvalid(p, tr->type) ||
1869 !policydb_role_isvalid(p, tr->new_role)) {
1870 rc = -EINVAL;
1871 goto bad;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 ltr = tr;
1874 }
1875
1876 rc = next_entry(buf, fp, sizeof(u32));
1877 if (rc < 0)
1878 goto bad;
1879 nel = le32_to_cpu(buf[0]);
1880 lra = NULL;
1881 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001882 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (!ra) {
1884 rc = -ENOMEM;
1885 goto bad;
1886 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001887 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04001889 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rc = next_entry(buf, fp, sizeof(u32)*2);
1892 if (rc < 0)
1893 goto bad;
1894 ra->role = le32_to_cpu(buf[0]);
1895 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001896 if (!policydb_role_isvalid(p, ra->role) ||
1897 !policydb_role_isvalid(p, ra->new_role)) {
1898 rc = -EINVAL;
1899 goto bad;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 lra = ra;
1902 }
1903
1904 rc = policydb_index_classes(p);
1905 if (rc)
1906 goto bad;
1907
1908 rc = policydb_index_others(p);
1909 if (rc)
1910 goto bad;
1911
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001912 p->process_class = string_to_security_class(p, "process");
1913 if (!p->process_class)
1914 goto bad;
1915 p->process_trans_perms = string_to_av_perm(p, p->process_class,
1916 "transition");
1917 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
1918 "dyntransition");
1919 if (!p->process_trans_perms)
1920 goto bad;
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 for (i = 0; i < info->ocon_num; i++) {
1923 rc = next_entry(buf, fp, sizeof(u32));
1924 if (rc < 0)
1925 goto bad;
1926 nel = le32_to_cpu(buf[0]);
1927 l = NULL;
1928 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001929 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (!c) {
1931 rc = -ENOMEM;
1932 goto bad;
1933 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001934 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001936 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 l = c;
1939 rc = -EINVAL;
1940 switch (i) {
1941 case OCON_ISID:
1942 rc = next_entry(buf, fp, sizeof(u32));
1943 if (rc < 0)
1944 goto bad;
1945 c->sid[0] = le32_to_cpu(buf[0]);
1946 rc = context_read_and_validate(&c->context[0], p, fp);
1947 if (rc)
1948 goto bad;
1949 break;
1950 case OCON_FS:
1951 case OCON_NETIF:
1952 rc = next_entry(buf, fp, sizeof(u32));
1953 if (rc < 0)
1954 goto bad;
1955 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001956 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (!c->u.name) {
1958 rc = -ENOMEM;
1959 goto bad;
1960 }
1961 rc = next_entry(c->u.name, fp, len);
1962 if (rc < 0)
1963 goto bad;
1964 c->u.name[len] = 0;
1965 rc = context_read_and_validate(&c->context[0], p, fp);
1966 if (rc)
1967 goto bad;
1968 rc = context_read_and_validate(&c->context[1], p, fp);
1969 if (rc)
1970 goto bad;
1971 break;
1972 case OCON_PORT:
1973 rc = next_entry(buf, fp, sizeof(u32)*3);
1974 if (rc < 0)
1975 goto bad;
1976 c->u.port.protocol = le32_to_cpu(buf[0]);
1977 c->u.port.low_port = le32_to_cpu(buf[1]);
1978 c->u.port.high_port = le32_to_cpu(buf[2]);
1979 rc = context_read_and_validate(&c->context[0], p, fp);
1980 if (rc)
1981 goto bad;
1982 break;
1983 case OCON_NODE:
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001984 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (rc < 0)
1986 goto bad;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001987 c->u.node.addr = nodebuf[0]; /* network order */
1988 c->u.node.mask = nodebuf[1]; /* network order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 rc = context_read_and_validate(&c->context[0], p, fp);
1990 if (rc)
1991 goto bad;
1992 break;
1993 case OCON_FSUSE:
1994 rc = next_entry(buf, fp, sizeof(u32)*2);
1995 if (rc < 0)
1996 goto bad;
1997 c->v.behavior = le32_to_cpu(buf[0]);
1998 if (c->v.behavior > SECURITY_FS_USE_NONE)
1999 goto bad;
2000 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04002001 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (!c->u.name) {
2003 rc = -ENOMEM;
2004 goto bad;
2005 }
2006 rc = next_entry(c->u.name, fp, len);
2007 if (rc < 0)
2008 goto bad;
2009 c->u.name[len] = 0;
2010 rc = context_read_and_validate(&c->context[0], p, fp);
2011 if (rc)
2012 goto bad;
2013 break;
2014 case OCON_NODE6: {
2015 int k;
2016
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002017 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (rc < 0)
2019 goto bad;
2020 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002021 c->u.node6.addr[k] = nodebuf[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002023 c->u.node6.mask[k] = nodebuf[k+4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (context_read_and_validate(&c->context[0], p, fp))
2025 goto bad;
2026 break;
2027 }
2028 }
2029 }
2030 }
2031
2032 rc = next_entry(buf, fp, sizeof(u32));
2033 if (rc < 0)
2034 goto bad;
2035 nel = le32_to_cpu(buf[0]);
2036 genfs_p = NULL;
2037 rc = -EINVAL;
2038 for (i = 0; i < nel; i++) {
2039 rc = next_entry(buf, fp, sizeof(u32));
2040 if (rc < 0)
2041 goto bad;
2042 len = le32_to_cpu(buf[0]);
James Morris89d155e2005-10-30 14:59:21 -08002043 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (!newgenfs) {
2045 rc = -ENOMEM;
2046 goto bad;
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Eric Paris2ced3df2008-04-17 13:37:12 -04002049 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (!newgenfs->fstype) {
2051 rc = -ENOMEM;
2052 kfree(newgenfs);
2053 goto bad;
2054 }
2055 rc = next_entry(newgenfs->fstype, fp, len);
2056 if (rc < 0) {
2057 kfree(newgenfs->fstype);
2058 kfree(newgenfs);
2059 goto bad;
2060 }
2061 newgenfs->fstype[len] = 0;
2062 for (genfs_p = NULL, genfs = p->genfs; genfs;
2063 genfs_p = genfs, genfs = genfs->next) {
2064 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
James Morris454d9722008-02-26 20:42:02 +11002065 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 "fstype %s\n", newgenfs->fstype);
2067 kfree(newgenfs->fstype);
2068 kfree(newgenfs);
2069 goto bad;
2070 }
2071 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2072 break;
2073 }
2074 newgenfs->next = genfs;
2075 if (genfs_p)
2076 genfs_p->next = newgenfs;
2077 else
2078 p->genfs = newgenfs;
2079 rc = next_entry(buf, fp, sizeof(u32));
2080 if (rc < 0)
2081 goto bad;
2082 nel2 = le32_to_cpu(buf[0]);
2083 for (j = 0; j < nel2; j++) {
2084 rc = next_entry(buf, fp, sizeof(u32));
2085 if (rc < 0)
2086 goto bad;
2087 len = le32_to_cpu(buf[0]);
2088
James Morris89d155e2005-10-30 14:59:21 -08002089 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (!newc) {
2091 rc = -ENOMEM;
2092 goto bad;
2093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric Paris2ced3df2008-04-17 13:37:12 -04002095 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (!newc->u.name) {
2097 rc = -ENOMEM;
2098 goto bad_newc;
2099 }
2100 rc = next_entry(newc->u.name, fp, len);
2101 if (rc < 0)
2102 goto bad_newc;
2103 newc->u.name[len] = 0;
2104 rc = next_entry(buf, fp, sizeof(u32));
2105 if (rc < 0)
2106 goto bad_newc;
2107 newc->v.sclass = le32_to_cpu(buf[0]);
2108 if (context_read_and_validate(&newc->context[0], p, fp))
2109 goto bad_newc;
2110 for (l = NULL, c = newgenfs->head; c;
2111 l = c, c = c->next) {
2112 if (!strcmp(newc->u.name, c->u.name) &&
2113 (!c->v.sclass || !newc->v.sclass ||
2114 newc->v.sclass == c->v.sclass)) {
James Morris454d9722008-02-26 20:42:02 +11002115 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 "entry (%s,%s)\n",
2117 newgenfs->fstype, c->u.name);
2118 goto bad_newc;
2119 }
2120 len = strlen(newc->u.name);
2121 len2 = strlen(c->u.name);
2122 if (len > len2)
2123 break;
2124 }
2125
2126 newc->next = c;
2127 if (l)
2128 l->next = newc;
2129 else
2130 newgenfs->head = newc;
2131 }
2132 }
2133
2134 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002135 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 rc = next_entry(buf, fp, sizeof(u32));
2137 if (rc < 0)
2138 goto bad;
2139 nel = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002141 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (!rt) {
2143 rc = -ENOMEM;
2144 goto bad;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002147 if (rc < 0) {
2148 kfree(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto bad;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002150 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002151 rt->source_type = le32_to_cpu(buf[0]);
2152 rt->target_type = le32_to_cpu(buf[1]);
2153 if (new_rangetr) {
2154 rc = next_entry(buf, fp, sizeof(u32));
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002155 if (rc < 0) {
2156 kfree(rt);
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002157 goto bad;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002158 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07002159 rt->target_class = le32_to_cpu(buf[0]);
2160 } else
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002161 rt->target_class = p->process_class;
Stephen Smalley45e54212007-11-07 10:08:00 -05002162 if (!policydb_type_isvalid(p, rt->source_type) ||
2163 !policydb_type_isvalid(p, rt->target_type) ||
2164 !policydb_class_isvalid(p, rt->target_class)) {
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002165 kfree(rt);
Stephen Smalley45e54212007-11-07 10:08:00 -05002166 rc = -EINVAL;
2167 goto bad;
2168 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002169 r = kzalloc(sizeof(*r), GFP_KERNEL);
2170 if (!r) {
2171 kfree(rt);
2172 rc = -ENOMEM;
Stephen Smalley45e54212007-11-07 10:08:00 -05002173 goto bad;
2174 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002175 rc = mls_read_range_helper(r, fp);
2176 if (rc) {
2177 kfree(rt);
2178 kfree(r);
2179 goto bad;
2180 }
2181 if (!mls_range_isvalid(p, r)) {
2182 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
2183 kfree(rt);
2184 kfree(r);
2185 goto bad;
2186 }
2187 rc = hashtab_insert(p->range_tr, rt, r);
2188 if (rc) {
2189 kfree(rt);
2190 kfree(r);
2191 goto bad;
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -05002194 rangetr_hash_eval(p->range_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002197 p->type_attr_map = kmalloc(p->p_types.nprim * sizeof(struct ebitmap), GFP_KERNEL);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002198 if (!p->type_attr_map)
2199 goto bad;
2200
2201 for (i = 0; i < p->p_types.nprim; i++) {
2202 ebitmap_init(&p->type_attr_map[i]);
2203 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2204 if (ebitmap_read(&p->type_attr_map[i], fp))
2205 goto bad;
2206 }
2207 /* add the type itself as the degenerate case */
2208 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2209 goto bad;
2210 }
2211
KaiGai Koheid9250de2008-08-28 16:35:57 +09002212 rc = policydb_bounds_sanity_check(p);
2213 if (rc)
2214 goto bad;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 rc = 0;
2217out:
2218 return rc;
2219bad_newc:
Eric Paris2ced3df2008-04-17 13:37:12 -04002220 ocontext_destroy(newc, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221bad:
2222 if (!rc)
2223 rc = -EINVAL;
2224 policydb_destroy(p);
2225 goto out;
2226}