blob: 4ea073eaf8bfa91a6dc2e0d2dfc662f6dca8e768 [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 Trentalancia0719aaf2010-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{
Eric Parisd1b43542010-07-21 12:50:57 -0400658 if (!c)
659 return;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 context_destroy(&c->context[0]);
662 context_destroy(&c->context[1]);
663 if (i == OCON_ISID || i == OCON_FS ||
664 i == OCON_NETIF || i == OCON_FSUSE)
665 kfree(c->u.name);
666 kfree(c);
667}
668
669/*
670 * Free any memory allocated by a policy database structure.
671 */
672void policydb_destroy(struct policydb *p)
673{
674 struct ocontext *c, *ctmp;
675 struct genfs *g, *gtmp;
676 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700677 struct role_allow *ra, *lra = NULL;
678 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400681 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
683 hashtab_destroy(p->symtab[i].table);
684 }
685
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700686 for (i = 0; i < SYM_NUM; i++)
687 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700689 kfree(p->class_val_to_struct);
690 kfree(p->role_val_to_struct);
691 kfree(p->user_val_to_struct);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900692 kfree(p->type_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 avtab_destroy(&p->te_avtab);
695
696 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400697 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 c = p->ocontexts[i];
699 while (c) {
700 ctmp = c;
701 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400702 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400704 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 g = p->genfs;
708 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400709 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 kfree(g->fstype);
711 c = g->head;
712 while (c) {
713 ctmp = c;
714 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400715 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 gtmp = g;
718 g = g->next;
719 kfree(gtmp);
720 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400721 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 cond_policydb_destroy(p);
724
Stephen Smalley782ebb92005-09-03 15:55:16 -0700725 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400726 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800727 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700728 ltr = tr;
729 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800730 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700731
Eric Paris2ced3df2008-04-17 13:37:12 -0400732 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400733 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800734 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700735 lra = ra;
736 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800737 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700738
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500739 hashtab_map(p->range_tr, range_tr_destroy, NULL);
740 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700741
Stephen Smalley282c1f52005-10-23 12:57:15 -0700742 if (p->type_attr_map) {
743 for (i = 0; i < p->p_types.nprim; i++)
744 ebitmap_destroy(&p->type_attr_map[i]);
745 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700746 kfree(p->type_attr_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500747 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100748 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751}
752
753/*
754 * Load the initial SIDs specified in a policy database
755 * structure into a SID table.
756 */
757int policydb_load_isids(struct policydb *p, struct sidtab *s)
758{
759 struct ocontext *head, *c;
760 int rc;
761
762 rc = sidtab_init(s);
763 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100764 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 }
767
768 head = p->ocontexts[OCON_ISID];
769 for (c = head; c; c = c->next) {
770 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100771 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 "defined.\n", c->u.name);
773 rc = -EINVAL;
774 goto out;
775 }
776 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100777 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 "SID %s.\n", c->u.name);
779 rc = -EINVAL;
780 goto out;
781 }
782 }
783out:
784 return rc;
785}
786
Stephen Smalley45e54212007-11-07 10:08:00 -0500787int policydb_class_isvalid(struct policydb *p, unsigned int class)
788{
789 if (!class || class > p->p_classes.nprim)
790 return 0;
791 return 1;
792}
793
794int policydb_role_isvalid(struct policydb *p, unsigned int role)
795{
796 if (!role || role > p->p_roles.nprim)
797 return 0;
798 return 1;
799}
800
801int policydb_type_isvalid(struct policydb *p, unsigned int type)
802{
803 if (!type || type > p->p_types.nprim)
804 return 0;
805 return 1;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * Return 1 if the fields in the security context
810 * structure `c' are valid. Return 0 otherwise.
811 */
812int policydb_context_isvalid(struct policydb *p, struct context *c)
813{
814 struct role_datum *role;
815 struct user_datum *usrdatum;
816
817 if (!c->role || c->role > p->p_roles.nprim)
818 return 0;
819
820 if (!c->user || c->user > p->p_users.nprim)
821 return 0;
822
823 if (!c->type || c->type > p->p_types.nprim)
824 return 0;
825
826 if (c->role != OBJECT_R_VAL) {
827 /*
828 * Role must be authorized for the type.
829 */
830 role = p->role_val_to_struct[c->role - 1];
831 if (!ebitmap_get_bit(&role->types,
832 c->type - 1))
833 /* role may not be associated with type */
834 return 0;
835
836 /*
837 * User must be authorized for the role.
838 */
839 usrdatum = p->user_val_to_struct[c->user - 1];
840 if (!usrdatum)
841 return 0;
842
843 if (!ebitmap_get_bit(&usrdatum->roles,
844 c->role - 1))
845 /* user may not be associated with role */
846 return 0;
847 }
848
849 if (!mls_context_isvalid(p, c))
850 return 0;
851
852 return 1;
853}
854
855/*
856 * Read a MLS range structure from a policydb binary
857 * representation file.
858 */
859static int mls_read_range_helper(struct mls_range *r, void *fp)
860{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700861 __le32 buf[2];
862 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int rc;
864
865 rc = next_entry(buf, fp, sizeof(u32));
866 if (rc < 0)
867 goto out;
868
869 items = le32_to_cpu(buf[0]);
870 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100871 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rc = -EINVAL;
873 goto out;
874 }
875 rc = next_entry(buf, fp, sizeof(u32) * items);
876 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100877 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto out;
879 }
880 r->level[0].sens = le32_to_cpu(buf[0]);
881 if (items > 1)
882 r->level[1].sens = le32_to_cpu(buf[1]);
883 else
884 r->level[1].sens = r->level[0].sens;
885
886 rc = ebitmap_read(&r->level[0].cat, fp);
887 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100888 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 "categories\n");
890 goto out;
891 }
892 if (items > 1) {
893 rc = ebitmap_read(&r->level[1].cat, fp);
894 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100895 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 "categories\n");
897 goto bad_high;
898 }
899 } else {
900 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
901 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100902 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto bad_high;
904 }
905 }
906
907 rc = 0;
908out:
909 return rc;
910bad_high:
911 ebitmap_destroy(&r->level[0].cat);
912 goto out;
913}
914
915/*
916 * Read and validate a security context structure
917 * from a policydb binary representation file.
918 */
919static int context_read_and_validate(struct context *c,
920 struct policydb *p,
921 void *fp)
922{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700923 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int rc;
925
926 rc = next_entry(buf, fp, sizeof buf);
927 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100928 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930 }
931 c->user = le32_to_cpu(buf[0]);
932 c->role = le32_to_cpu(buf[1]);
933 c->type = le32_to_cpu(buf[2]);
934 if (p->policyvers >= POLICYDB_VERSION_MLS) {
935 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100936 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 "context\n");
938 rc = -EINVAL;
939 goto out;
940 }
941 }
942
943 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100944 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 context_destroy(c);
946 rc = -EINVAL;
947 }
948out:
949 return rc;
950}
951
952/*
953 * The following *_read functions are used to
954 * read the symbol data from a policy database
955 * binary representation file.
956 */
957
958static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
959{
960 char *key = NULL;
961 struct perm_datum *perdatum;
962 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700963 __le32 buf[2];
964 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
James Morris89d155e2005-10-30 14:59:21 -0800966 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!perdatum) {
968 rc = -ENOMEM;
969 goto out;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 rc = next_entry(buf, fp, sizeof buf);
973 if (rc < 0)
974 goto bad;
975
976 len = le32_to_cpu(buf[0]);
977 perdatum->value = le32_to_cpu(buf[1]);
978
Eric Paris2ced3df2008-04-17 13:37:12 -0400979 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!key) {
981 rc = -ENOMEM;
982 goto bad;
983 }
984 rc = next_entry(key, fp, len);
985 if (rc < 0)
986 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +0300987 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 rc = hashtab_insert(h, key, perdatum);
990 if (rc)
991 goto bad;
992out:
993 return rc;
994bad:
995 perm_destroy(key, perdatum, NULL);
996 goto out;
997}
998
999static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1000{
1001 char *key = NULL;
1002 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001003 __le32 buf[4];
1004 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int i, rc;
1006
James Morris89d155e2005-10-30 14:59:21 -08001007 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!comdatum) {
1009 rc = -ENOMEM;
1010 goto out;
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 rc = next_entry(buf, fp, sizeof buf);
1014 if (rc < 0)
1015 goto bad;
1016
1017 len = le32_to_cpu(buf[0]);
1018 comdatum->value = le32_to_cpu(buf[1]);
1019
1020 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1021 if (rc)
1022 goto bad;
1023 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1024 nel = le32_to_cpu(buf[3]);
1025
Eric Paris2ced3df2008-04-17 13:37:12 -04001026 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!key) {
1028 rc = -ENOMEM;
1029 goto bad;
1030 }
1031 rc = next_entry(key, fp, len);
1032 if (rc < 0)
1033 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001034 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 for (i = 0; i < nel; i++) {
1037 rc = perm_read(p, comdatum->permissions.table, fp);
1038 if (rc)
1039 goto bad;
1040 }
1041
1042 rc = hashtab_insert(h, key, comdatum);
1043 if (rc)
1044 goto bad;
1045out:
1046 return rc;
1047bad:
1048 common_destroy(key, comdatum, NULL);
1049 goto out;
1050}
1051
1052static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001053 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct constraint_node *c, *lc;
1056 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001057 __le32 buf[3];
1058 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int rc, i, j, depth;
1060
1061 lc = NULL;
1062 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001063 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (!c)
1065 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Eric Paris2ced3df2008-04-17 13:37:12 -04001067 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001069 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1073 if (rc < 0)
1074 return rc;
1075 c->permissions = le32_to_cpu(buf[0]);
1076 nexpr = le32_to_cpu(buf[1]);
1077 le = NULL;
1078 depth = -1;
1079 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001080 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!e)
1082 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Eric Paris2ced3df2008-04-17 13:37:12 -04001084 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001086 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1090 if (rc < 0)
1091 return rc;
1092 e->expr_type = le32_to_cpu(buf[0]);
1093 e->attr = le32_to_cpu(buf[1]);
1094 e->op = le32_to_cpu(buf[2]);
1095
1096 switch (e->expr_type) {
1097 case CEXPR_NOT:
1098 if (depth < 0)
1099 return -EINVAL;
1100 break;
1101 case CEXPR_AND:
1102 case CEXPR_OR:
1103 if (depth < 1)
1104 return -EINVAL;
1105 depth--;
1106 break;
1107 case CEXPR_ATTR:
1108 if (depth == (CEXPR_MAXDEPTH - 1))
1109 return -EINVAL;
1110 depth++;
1111 break;
1112 case CEXPR_NAMES:
1113 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1114 return -EINVAL;
1115 if (depth == (CEXPR_MAXDEPTH - 1))
1116 return -EINVAL;
1117 depth++;
1118 if (ebitmap_read(&e->names, fp))
1119 return -EINVAL;
1120 break;
1121 default:
1122 return -EINVAL;
1123 }
1124 le = e;
1125 }
1126 if (depth != 0)
1127 return -EINVAL;
1128 lc = c;
1129 }
1130
1131 return 0;
1132}
1133
1134static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1135{
1136 char *key = NULL;
1137 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001138 __le32 buf[6];
1139 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int i, rc;
1141
James Morris89d155e2005-10-30 14:59:21 -08001142 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!cladatum) {
1144 rc = -ENOMEM;
1145 goto out;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 rc = next_entry(buf, fp, sizeof(u32)*6);
1149 if (rc < 0)
1150 goto bad;
1151
1152 len = le32_to_cpu(buf[0]);
1153 len2 = le32_to_cpu(buf[1]);
1154 cladatum->value = le32_to_cpu(buf[2]);
1155
1156 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1157 if (rc)
1158 goto bad;
1159 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1160 nel = le32_to_cpu(buf[4]);
1161
1162 ncons = le32_to_cpu(buf[5]);
1163
Eric Paris2ced3df2008-04-17 13:37:12 -04001164 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!key) {
1166 rc = -ENOMEM;
1167 goto bad;
1168 }
1169 rc = next_entry(key, fp, len);
1170 if (rc < 0)
1171 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001172 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001175 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!cladatum->comkey) {
1177 rc = -ENOMEM;
1178 goto bad;
1179 }
1180 rc = next_entry(cladatum->comkey, fp, len2);
1181 if (rc < 0)
1182 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001183 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 cladatum->comdatum = hashtab_search(p->p_commons.table,
1186 cladatum->comkey);
1187 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001188 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cladatum->comkey);
1190 rc = -EINVAL;
1191 goto bad;
1192 }
1193 }
1194 for (i = 0; i < nel; i++) {
1195 rc = perm_read(p, cladatum->permissions.table, fp);
1196 if (rc)
1197 goto bad;
1198 }
1199
1200 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1201 if (rc)
1202 goto bad;
1203
1204 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1205 /* grab the validatetrans rules */
1206 rc = next_entry(buf, fp, sizeof(u32));
1207 if (rc < 0)
1208 goto bad;
1209 ncons = le32_to_cpu(buf[0]);
1210 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1211 if (rc)
1212 goto bad;
1213 }
1214
1215 rc = hashtab_insert(h, key, cladatum);
1216 if (rc)
1217 goto bad;
1218
1219 rc = 0;
1220out:
1221 return rc;
1222bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001223 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto out;
1225}
1226
1227static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1228{
1229 char *key = NULL;
1230 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001231 int rc, to_read = 2;
1232 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001233 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
James Morris89d155e2005-10-30 14:59:21 -08001235 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!role) {
1237 rc = -ENOMEM;
1238 goto out;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
KaiGai Koheid9250de2008-08-28 16:35:57 +09001241 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1242 to_read = 3;
1243
1244 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (rc < 0)
1246 goto bad;
1247
1248 len = le32_to_cpu(buf[0]);
1249 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001250 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1251 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Eric Paris2ced3df2008-04-17 13:37:12 -04001253 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!key) {
1255 rc = -ENOMEM;
1256 goto bad;
1257 }
1258 rc = next_entry(key, fp, len);
1259 if (rc < 0)
1260 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001261 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 rc = ebitmap_read(&role->dominates, fp);
1264 if (rc)
1265 goto bad;
1266
1267 rc = ebitmap_read(&role->types, fp);
1268 if (rc)
1269 goto bad;
1270
1271 if (strcmp(key, OBJECT_R) == 0) {
1272 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001273 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 OBJECT_R, role->value);
1275 rc = -EINVAL;
1276 goto bad;
1277 }
1278 rc = 0;
1279 goto bad;
1280 }
1281
1282 rc = hashtab_insert(h, key, role);
1283 if (rc)
1284 goto bad;
1285out:
1286 return rc;
1287bad:
1288 role_destroy(key, role, NULL);
1289 goto out;
1290}
1291
1292static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1293{
1294 char *key = NULL;
1295 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001296 int rc, to_read = 3;
1297 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001298 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Paris2ced3df2008-04-17 13:37:12 -04001300 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!typdatum) {
1302 rc = -ENOMEM;
1303 return rc;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
KaiGai Koheid9250de2008-08-28 16:35:57 +09001306 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1307 to_read = 4;
1308
1309 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (rc < 0)
1311 goto bad;
1312
1313 len = le32_to_cpu(buf[0]);
1314 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001315 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1316 u32 prop = le32_to_cpu(buf[2]);
1317
1318 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1319 typdatum->primary = 1;
1320 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1321 typdatum->attribute = 1;
1322
1323 typdatum->bounds = le32_to_cpu(buf[3]);
1324 } else {
1325 typdatum->primary = le32_to_cpu(buf[2]);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric Paris2ced3df2008-04-17 13:37:12 -04001328 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!key) {
1330 rc = -ENOMEM;
1331 goto bad;
1332 }
1333 rc = next_entry(key, fp, len);
1334 if (rc < 0)
1335 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001336 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 rc = hashtab_insert(h, key, typdatum);
1339 if (rc)
1340 goto bad;
1341out:
1342 return rc;
1343bad:
1344 type_destroy(key, typdatum, NULL);
1345 goto out;
1346}
1347
1348
1349/*
1350 * Read a MLS level structure from a policydb binary
1351 * representation file.
1352 */
1353static int mls_read_level(struct mls_level *lp, void *fp)
1354{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001355 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int rc;
1357
1358 memset(lp, 0, sizeof(*lp));
1359
1360 rc = next_entry(buf, fp, sizeof buf);
1361 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001362 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 goto bad;
1364 }
1365 lp->sens = le32_to_cpu(buf[0]);
1366
1367 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001368 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 "categories\n");
1370 goto bad;
1371 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374
1375bad:
1376 return -EINVAL;
1377}
1378
1379static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1380{
1381 char *key = NULL;
1382 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001383 int rc, to_read = 2;
1384 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001385 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
James Morris89d155e2005-10-30 14:59:21 -08001387 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!usrdatum) {
1389 rc = -ENOMEM;
1390 goto out;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
KaiGai Koheid9250de2008-08-28 16:35:57 +09001393 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1394 to_read = 3;
1395
1396 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (rc < 0)
1398 goto bad;
1399
1400 len = le32_to_cpu(buf[0]);
1401 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1403 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Eric Paris2ced3df2008-04-17 13:37:12 -04001405 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!key) {
1407 rc = -ENOMEM;
1408 goto bad;
1409 }
1410 rc = next_entry(key, fp, len);
1411 if (rc < 0)
1412 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001413 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 rc = ebitmap_read(&usrdatum->roles, fp);
1416 if (rc)
1417 goto bad;
1418
1419 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1420 rc = mls_read_range_helper(&usrdatum->range, fp);
1421 if (rc)
1422 goto bad;
1423 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1424 if (rc)
1425 goto bad;
1426 }
1427
1428 rc = hashtab_insert(h, key, usrdatum);
1429 if (rc)
1430 goto bad;
1431out:
1432 return rc;
1433bad:
1434 user_destroy(key, usrdatum, NULL);
1435 goto out;
1436}
1437
1438static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1439{
1440 char *key = NULL;
1441 struct level_datum *levdatum;
1442 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001443 __le32 buf[2];
1444 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
James Morris89d155e2005-10-30 14:59:21 -08001446 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!levdatum) {
1448 rc = -ENOMEM;
1449 goto out;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 rc = next_entry(buf, fp, sizeof buf);
1453 if (rc < 0)
1454 goto bad;
1455
1456 len = le32_to_cpu(buf[0]);
1457 levdatum->isalias = le32_to_cpu(buf[1]);
1458
Eric Paris2ced3df2008-04-17 13:37:12 -04001459 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!key) {
1461 rc = -ENOMEM;
1462 goto bad;
1463 }
1464 rc = next_entry(key, fp, len);
1465 if (rc < 0)
1466 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001467 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1470 if (!levdatum->level) {
1471 rc = -ENOMEM;
1472 goto bad;
1473 }
1474 if (mls_read_level(levdatum->level, fp)) {
1475 rc = -EINVAL;
1476 goto bad;
1477 }
1478
1479 rc = hashtab_insert(h, key, levdatum);
1480 if (rc)
1481 goto bad;
1482out:
1483 return rc;
1484bad:
1485 sens_destroy(key, levdatum, NULL);
1486 goto out;
1487}
1488
1489static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1490{
1491 char *key = NULL;
1492 struct cat_datum *catdatum;
1493 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001494 __le32 buf[3];
1495 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
James Morris89d155e2005-10-30 14:59:21 -08001497 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!catdatum) {
1499 rc = -ENOMEM;
1500 goto out;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 rc = next_entry(buf, fp, sizeof buf);
1504 if (rc < 0)
1505 goto bad;
1506
1507 len = le32_to_cpu(buf[0]);
1508 catdatum->value = le32_to_cpu(buf[1]);
1509 catdatum->isalias = le32_to_cpu(buf[2]);
1510
Eric Paris2ced3df2008-04-17 13:37:12 -04001511 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (!key) {
1513 rc = -ENOMEM;
1514 goto bad;
1515 }
1516 rc = next_entry(key, fp, len);
1517 if (rc < 0)
1518 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001519 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 rc = hashtab_insert(h, key, catdatum);
1522 if (rc)
1523 goto bad;
1524out:
1525 return rc;
1526
1527bad:
1528 cat_destroy(key, catdatum, NULL);
1529 goto out;
1530}
1531
1532static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1533{
1534 common_read,
1535 class_read,
1536 role_read,
1537 type_read,
1538 user_read,
1539 cond_read_bool,
1540 sens_read,
1541 cat_read,
1542};
1543
KaiGai Koheid9250de2008-08-28 16:35:57 +09001544static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1545{
1546 struct user_datum *upper, *user;
1547 struct policydb *p = datap;
1548 int depth = 0;
1549
1550 upper = user = datum;
1551 while (upper->bounds) {
1552 struct ebitmap_node *node;
1553 unsigned long bit;
1554
1555 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1556 printk(KERN_ERR "SELinux: user %s: "
1557 "too deep or looped boundary",
1558 (char *) key);
1559 return -EINVAL;
1560 }
1561
1562 upper = p->user_val_to_struct[upper->bounds - 1];
1563 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1564 if (ebitmap_get_bit(&upper->roles, bit))
1565 continue;
1566
1567 printk(KERN_ERR
1568 "SELinux: boundary violated policy: "
1569 "user=%s role=%s bounds=%s\n",
1570 p->p_user_val_to_name[user->value - 1],
1571 p->p_role_val_to_name[bit],
1572 p->p_user_val_to_name[upper->value - 1]);
1573
1574 return -EINVAL;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
1581static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1582{
1583 struct role_datum *upper, *role;
1584 struct policydb *p = datap;
1585 int depth = 0;
1586
1587 upper = role = datum;
1588 while (upper->bounds) {
1589 struct ebitmap_node *node;
1590 unsigned long bit;
1591
1592 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1593 printk(KERN_ERR "SELinux: role %s: "
1594 "too deep or looped bounds\n",
1595 (char *) key);
1596 return -EINVAL;
1597 }
1598
1599 upper = p->role_val_to_struct[upper->bounds - 1];
1600 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1601 if (ebitmap_get_bit(&upper->types, bit))
1602 continue;
1603
1604 printk(KERN_ERR
1605 "SELinux: boundary violated policy: "
1606 "role=%s type=%s bounds=%s\n",
1607 p->p_role_val_to_name[role->value - 1],
1608 p->p_type_val_to_name[bit],
1609 p->p_role_val_to_name[upper->value - 1]);
1610
1611 return -EINVAL;
1612 }
1613 }
1614
1615 return 0;
1616}
1617
1618static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1619{
1620 struct type_datum *upper, *type;
1621 struct policydb *p = datap;
1622 int depth = 0;
1623
1624 upper = type = datum;
1625 while (upper->bounds) {
1626 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1627 printk(KERN_ERR "SELinux: type %s: "
1628 "too deep or looped boundary\n",
1629 (char *) key);
1630 return -EINVAL;
1631 }
1632
1633 upper = p->type_val_to_struct[upper->bounds - 1];
1634 if (upper->attribute) {
1635 printk(KERN_ERR "SELinux: type %s: "
1636 "bounded by attribute %s",
1637 (char *) key,
1638 p->p_type_val_to_name[upper->value - 1]);
1639 return -EINVAL;
1640 }
1641 }
1642
1643 return 0;
1644}
1645
1646static int policydb_bounds_sanity_check(struct policydb *p)
1647{
1648 int rc;
1649
1650 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1651 return 0;
1652
1653 rc = hashtab_map(p->p_users.table,
1654 user_bounds_sanity_check, p);
1655 if (rc)
1656 return rc;
1657
1658 rc = hashtab_map(p->p_roles.table,
1659 role_bounds_sanity_check, p);
1660 if (rc)
1661 return rc;
1662
1663 rc = hashtab_map(p->p_types.table,
1664 type_bounds_sanity_check, p);
1665 if (rc)
1666 return rc;
1667
1668 return 0;
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671extern int ss_initialized;
1672
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001673u16 string_to_security_class(struct policydb *p, const char *name)
1674{
1675 struct class_datum *cladatum;
1676
1677 cladatum = hashtab_search(p->p_classes.table, name);
1678 if (!cladatum)
1679 return 0;
1680
1681 return cladatum->value;
1682}
1683
1684u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1685{
1686 struct class_datum *cladatum;
1687 struct perm_datum *perdatum = NULL;
1688 struct common_datum *comdatum;
1689
1690 if (!tclass || tclass > p->p_classes.nprim)
1691 return 0;
1692
1693 cladatum = p->class_val_to_struct[tclass-1];
1694 comdatum = cladatum->comdatum;
1695 if (comdatum)
1696 perdatum = hashtab_search(comdatum->permissions.table,
1697 name);
1698 if (!perdatum)
1699 perdatum = hashtab_search(cladatum->permissions.table,
1700 name);
1701 if (!perdatum)
1702 return 0;
1703
1704 return 1U << (perdatum->value-1);
1705}
1706
Eric Paris9ee0c822010-06-11 12:37:05 -04001707static int range_read(struct policydb *p, void *fp)
1708{
1709 struct range_trans *rt = NULL;
1710 struct mls_range *r = NULL;
1711 int i, rc;
1712 __le32 buf[2];
1713 u32 nel;
1714
1715 if (p->policyvers < POLICYDB_VERSION_MLS)
1716 return 0;
1717
1718 rc = next_entry(buf, fp, sizeof(u32));
1719 if (rc)
1720 goto out;
1721
1722 nel = le32_to_cpu(buf[0]);
1723 for (i = 0; i < nel; i++) {
1724 rc = -ENOMEM;
1725 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1726 if (!rt)
1727 goto out;
1728
1729 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1730 if (rc)
1731 goto out;
1732
1733 rt->source_type = le32_to_cpu(buf[0]);
1734 rt->target_type = le32_to_cpu(buf[1]);
1735 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1736 rc = next_entry(buf, fp, sizeof(u32));
1737 if (rc)
1738 goto out;
1739 rt->target_class = le32_to_cpu(buf[0]);
1740 } else
1741 rt->target_class = p->process_class;
1742
1743 rc = -EINVAL;
1744 if (!policydb_type_isvalid(p, rt->source_type) ||
1745 !policydb_type_isvalid(p, rt->target_type) ||
1746 !policydb_class_isvalid(p, rt->target_class))
1747 goto out;
1748
1749 rc = -ENOMEM;
1750 r = kzalloc(sizeof(*r), GFP_KERNEL);
1751 if (!r)
1752 goto out;
1753
1754 rc = mls_read_range_helper(r, fp);
1755 if (rc)
1756 goto out;
1757
1758 rc = -EINVAL;
1759 if (!mls_range_isvalid(p, r)) {
1760 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1761 goto out;
1762 }
1763
1764 rc = hashtab_insert(p->range_tr, rt, r);
1765 if (rc)
1766 goto out;
1767
1768 rt = NULL;
1769 r = NULL;
1770 }
1771 rangetr_hash_eval(p->range_tr);
1772 rc = 0;
1773out:
1774 kfree(rt);
1775 kfree(r);
1776 return rc;
1777}
1778
Eric Parisd1b43542010-07-21 12:50:57 -04001779static int genfs_read(struct policydb *p, void *fp)
1780{
1781 int i, j, rc;
1782 u32 nel, nel2, len, len2;
1783 __le32 buf[1];
1784 struct ocontext *l, *c;
1785 struct ocontext *newc = NULL;
1786 struct genfs *genfs_p, *genfs;
1787 struct genfs *newgenfs = NULL;
1788
1789 rc = next_entry(buf, fp, sizeof(u32));
1790 if (rc)
1791 goto out;
1792 nel = le32_to_cpu(buf[0]);
1793
1794 for (i = 0; i < nel; i++) {
1795 rc = next_entry(buf, fp, sizeof(u32));
1796 if (rc)
1797 goto out;
1798 len = le32_to_cpu(buf[0]);
1799
1800 rc = -ENOMEM;
1801 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1802 if (!newgenfs)
1803 goto out;
1804
1805 rc = -ENOMEM;
1806 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1807 if (!newgenfs->fstype)
1808 goto out;
1809
1810 rc = next_entry(newgenfs->fstype, fp, len);
1811 if (rc)
1812 goto out;
1813
1814 newgenfs->fstype[len] = 0;
1815
1816 for (genfs_p = NULL, genfs = p->genfs; genfs;
1817 genfs_p = genfs, genfs = genfs->next) {
1818 rc = -EINVAL;
1819 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1820 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1821 newgenfs->fstype);
1822 goto out;
1823 }
1824 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1825 break;
1826 }
1827 newgenfs->next = genfs;
1828 if (genfs_p)
1829 genfs_p->next = newgenfs;
1830 else
1831 p->genfs = newgenfs;
1832 genfs = newgenfs;
1833 newgenfs = NULL;
1834
1835 rc = next_entry(buf, fp, sizeof(u32));
1836 if (rc)
1837 goto out;
1838
1839 nel2 = le32_to_cpu(buf[0]);
1840 for (j = 0; j < nel2; j++) {
1841 rc = next_entry(buf, fp, sizeof(u32));
1842 if (rc)
1843 goto out;
1844 len = le32_to_cpu(buf[0]);
1845
1846 rc = -ENOMEM;
1847 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1848 if (!newc)
1849 goto out;
1850
1851 rc = -ENOMEM;
1852 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1853 if (!newc->u.name)
1854 goto out;
1855
1856 rc = next_entry(newc->u.name, fp, len);
1857 if (rc)
1858 goto out;
1859 newc->u.name[len] = 0;
1860
1861 rc = next_entry(buf, fp, sizeof(u32));
1862 if (rc)
1863 goto out;
1864
1865 newc->v.sclass = le32_to_cpu(buf[0]);
1866 rc = context_read_and_validate(&newc->context[0], p, fp);
1867 if (rc)
1868 goto out;
1869
1870 for (l = NULL, c = genfs->head; c;
1871 l = c, c = c->next) {
1872 rc = -EINVAL;
1873 if (!strcmp(newc->u.name, c->u.name) &&
1874 (!c->v.sclass || !newc->v.sclass ||
1875 newc->v.sclass == c->v.sclass)) {
1876 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1877 genfs->fstype, c->u.name);
1878 goto out;
1879 }
1880 len = strlen(newc->u.name);
1881 len2 = strlen(c->u.name);
1882 if (len > len2)
1883 break;
1884 }
1885
1886 newc->next = c;
1887 if (l)
1888 l->next = newc;
1889 else
1890 genfs->head = newc;
1891 newc = NULL;
1892 }
1893 }
1894 rc = 0;
1895out:
1896 if (newgenfs)
1897 kfree(newgenfs->fstype);
1898 kfree(newgenfs);
1899 ocontext_destroy(newc, OCON_FSUSE);
1900
1901 return rc;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/*
1905 * Read the configuration data from a policy database binary
1906 * representation file into a policy database structure.
1907 */
1908int policydb_read(struct policydb *p, void *fp)
1909{
1910 struct role_allow *ra, *lra;
1911 struct role_trans *tr, *ltr;
Eric Parisd1b43542010-07-21 12:50:57 -04001912 struct ocontext *l, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04001914 __le32 buf[4];
1915 u32 nodebuf[8];
Eric Parisd1b43542010-07-21 12:50:57 -04001916 u32 len, nprim, nel;
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 char *policydb_str;
1919 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rc = policydb_init(p);
1922 if (rc)
1923 goto out;
1924
1925 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001926 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (rc < 0)
1928 goto bad;
1929
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001930 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001931 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001933 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto bad;
1935 }
1936
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001937 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001939 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 "match expected length %Zu\n",
1941 len, strlen(POLICYDB_STRING));
1942 goto bad;
1943 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001944 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001946 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 "string of length %d\n", len);
1948 rc = -ENOMEM;
1949 goto bad;
1950 }
1951 rc = next_entry(policydb_str, fp, len);
1952 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001953 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 kfree(policydb_str);
1955 goto bad;
1956 }
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001957 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001959 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 "my string %s\n", policydb_str, POLICYDB_STRING);
1961 kfree(policydb_str);
1962 goto bad;
1963 }
1964 /* Done with policydb_str. */
1965 kfree(policydb_str);
1966 policydb_str = NULL;
1967
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001968 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rc = next_entry(buf, fp, sizeof(u32)*4);
1970 if (rc < 0)
1971 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001973 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (p->policyvers < POLICYDB_VERSION_MIN ||
1975 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001976 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001977 "my version range %d-%d\n",
1978 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1979 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001982 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001983 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001986 printk(KERN_ERR "SELinux: security policydb version %d "
1987 "(MLS) not backwards compatible\n",
1988 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto bad;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Eric Paris3f120702007-09-21 14:37:10 -04001992 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1993 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Paul Moore3bb56b22008-01-29 08:38:19 -05001995 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1996 ebitmap_read(&p->policycaps, fp) != 0)
1997 goto bad;
1998
Eric Paris64dbf072008-03-31 12:17:33 +11001999 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
2000 ebitmap_read(&p->permissive_map, fp) != 0)
2001 goto bad;
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 info = policydb_lookup_compat(p->policyvers);
2004 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002005 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 "for version %d\n", p->policyvers);
2007 goto bad;
2008 }
2009
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002010 if (le32_to_cpu(buf[2]) != info->sym_num ||
2011 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002012 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002013 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2014 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 info->sym_num, info->ocon_num);
2016 goto bad;
2017 }
2018
2019 for (i = 0; i < info->sym_num; i++) {
2020 rc = next_entry(buf, fp, sizeof(u32)*2);
2021 if (rc < 0)
2022 goto bad;
2023 nprim = le32_to_cpu(buf[0]);
2024 nel = le32_to_cpu(buf[1]);
2025 for (j = 0; j < nel; j++) {
2026 rc = read_f[i](p, p->symtab[i].table, fp);
2027 if (rc)
2028 goto bad;
2029 }
2030
2031 p->symtab[i].nprim = nprim;
2032 }
2033
Stephen Smalley45e54212007-11-07 10:08:00 -05002034 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (rc)
2036 goto bad;
2037
2038 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2039 rc = cond_read_list(p, fp);
2040 if (rc)
2041 goto bad;
2042 }
2043
2044 rc = next_entry(buf, fp, sizeof(u32));
2045 if (rc < 0)
2046 goto bad;
2047 nel = le32_to_cpu(buf[0]);
2048 ltr = NULL;
2049 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002050 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (!tr) {
2052 rc = -ENOMEM;
2053 goto bad;
2054 }
Eric Paris2ced3df2008-04-17 13:37:12 -04002055 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002057 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 rc = next_entry(buf, fp, sizeof(u32)*3);
2060 if (rc < 0)
2061 goto bad;
2062 tr->role = le32_to_cpu(buf[0]);
2063 tr->type = le32_to_cpu(buf[1]);
2064 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002065 if (!policydb_role_isvalid(p, tr->role) ||
2066 !policydb_type_isvalid(p, tr->type) ||
2067 !policydb_role_isvalid(p, tr->new_role)) {
2068 rc = -EINVAL;
2069 goto bad;
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 ltr = tr;
2072 }
2073
2074 rc = next_entry(buf, fp, sizeof(u32));
2075 if (rc < 0)
2076 goto bad;
2077 nel = le32_to_cpu(buf[0]);
2078 lra = NULL;
2079 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08002080 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (!ra) {
2082 rc = -ENOMEM;
2083 goto bad;
2084 }
Eric Paris2ced3df2008-04-17 13:37:12 -04002085 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002087 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 rc = next_entry(buf, fp, sizeof(u32)*2);
2090 if (rc < 0)
2091 goto bad;
2092 ra->role = le32_to_cpu(buf[0]);
2093 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002094 if (!policydb_role_isvalid(p, ra->role) ||
2095 !policydb_role_isvalid(p, ra->new_role)) {
2096 rc = -EINVAL;
2097 goto bad;
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 lra = ra;
2100 }
2101
2102 rc = policydb_index_classes(p);
2103 if (rc)
2104 goto bad;
2105
2106 rc = policydb_index_others(p);
2107 if (rc)
2108 goto bad;
2109
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002110 p->process_class = string_to_security_class(p, "process");
2111 if (!p->process_class)
2112 goto bad;
2113 p->process_trans_perms = string_to_av_perm(p, p->process_class,
2114 "transition");
2115 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
2116 "dyntransition");
2117 if (!p->process_trans_perms)
2118 goto bad;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 for (i = 0; i < info->ocon_num; i++) {
2121 rc = next_entry(buf, fp, sizeof(u32));
2122 if (rc < 0)
2123 goto bad;
2124 nel = le32_to_cpu(buf[0]);
2125 l = NULL;
2126 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08002127 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (!c) {
2129 rc = -ENOMEM;
2130 goto bad;
2131 }
Eric Paris2ced3df2008-04-17 13:37:12 -04002132 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04002134 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 l = c;
2137 rc = -EINVAL;
2138 switch (i) {
2139 case OCON_ISID:
2140 rc = next_entry(buf, fp, sizeof(u32));
2141 if (rc < 0)
2142 goto bad;
2143 c->sid[0] = le32_to_cpu(buf[0]);
2144 rc = context_read_and_validate(&c->context[0], p, fp);
2145 if (rc)
2146 goto bad;
2147 break;
2148 case OCON_FS:
2149 case OCON_NETIF:
2150 rc = next_entry(buf, fp, sizeof(u32));
2151 if (rc < 0)
2152 goto bad;
2153 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04002154 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (!c->u.name) {
2156 rc = -ENOMEM;
2157 goto bad;
2158 }
2159 rc = next_entry(c->u.name, fp, len);
2160 if (rc < 0)
2161 goto bad;
2162 c->u.name[len] = 0;
2163 rc = context_read_and_validate(&c->context[0], p, fp);
2164 if (rc)
2165 goto bad;
2166 rc = context_read_and_validate(&c->context[1], p, fp);
2167 if (rc)
2168 goto bad;
2169 break;
2170 case OCON_PORT:
2171 rc = next_entry(buf, fp, sizeof(u32)*3);
2172 if (rc < 0)
2173 goto bad;
2174 c->u.port.protocol = le32_to_cpu(buf[0]);
2175 c->u.port.low_port = le32_to_cpu(buf[1]);
2176 c->u.port.high_port = le32_to_cpu(buf[2]);
2177 rc = context_read_and_validate(&c->context[0], p, fp);
2178 if (rc)
2179 goto bad;
2180 break;
2181 case OCON_NODE:
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002182 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (rc < 0)
2184 goto bad;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002185 c->u.node.addr = nodebuf[0]; /* network order */
2186 c->u.node.mask = nodebuf[1]; /* network order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 rc = context_read_and_validate(&c->context[0], p, fp);
2188 if (rc)
2189 goto bad;
2190 break;
2191 case OCON_FSUSE:
2192 rc = next_entry(buf, fp, sizeof(u32)*2);
2193 if (rc < 0)
2194 goto bad;
2195 c->v.behavior = le32_to_cpu(buf[0]);
2196 if (c->v.behavior > SECURITY_FS_USE_NONE)
2197 goto bad;
2198 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04002199 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (!c->u.name) {
2201 rc = -ENOMEM;
2202 goto bad;
2203 }
2204 rc = next_entry(c->u.name, fp, len);
2205 if (rc < 0)
2206 goto bad;
2207 c->u.name[len] = 0;
2208 rc = context_read_and_validate(&c->context[0], p, fp);
2209 if (rc)
2210 goto bad;
2211 break;
2212 case OCON_NODE6: {
2213 int k;
2214
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002215 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (rc < 0)
2217 goto bad;
2218 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002219 c->u.node6.addr[k] = nodebuf[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 for (k = 0; k < 4; k++)
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002221 c->u.node6.mask[k] = nodebuf[k+4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (context_read_and_validate(&c->context[0], p, fp))
2223 goto bad;
2224 break;
2225 }
2226 }
2227 }
2228 }
2229
Eric Parisd1b43542010-07-21 12:50:57 -04002230 rc = genfs_read(p, fp);
2231 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Eric Paris9ee0c822010-06-11 12:37:05 -04002234 rc = range_read(p, fp);
2235 if (rc)
2236 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002238 p->type_attr_map = kmalloc(p->p_types.nprim * sizeof(struct ebitmap), GFP_KERNEL);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002239 if (!p->type_attr_map)
2240 goto bad;
2241
2242 for (i = 0; i < p->p_types.nprim; i++) {
2243 ebitmap_init(&p->type_attr_map[i]);
2244 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2245 if (ebitmap_read(&p->type_attr_map[i], fp))
2246 goto bad;
2247 }
2248 /* add the type itself as the degenerate case */
2249 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2250 goto bad;
2251 }
2252
KaiGai Koheid9250de2008-08-28 16:35:57 +09002253 rc = policydb_bounds_sanity_check(p);
2254 if (rc)
2255 goto bad;
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 rc = 0;
2258out:
2259 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260bad:
2261 if (!rc)
2262 rc = -EINVAL;
2263 policydb_destroy(p);
2264 goto out;
2265}