blob: 84f8cc73c7db64c9b358784a5f85e4185b6ac2e4 [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>
33#include "security.h"
34
35#include "policydb.h"
36#include "conditional.h"
37#include "mls.h"
38
39#define _DEBUG_HASHES
40
41#ifdef DEBUG_HASHES
42static char *symtab_name[SYM_NUM] = {
43 "common prefixes",
44 "classes",
45 "roles",
46 "types",
47 "users",
48 "bools",
49 "levels",
50 "categories",
51};
52#endif
53
Eric Paris2ced3df2008-04-17 13:37:12 -040054int selinux_mls_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static unsigned int symtab_sizes[SYM_NUM] = {
57 2,
58 32,
59 16,
60 512,
61 128,
62 16,
63 16,
64 16,
65};
66
67struct policydb_compat_info {
68 int version;
69 int sym_num;
70 int ocon_num;
71};
72
73/* These need to be updated if SYM_NUM or OCON_NUM changes */
74static struct policydb_compat_info policydb_compat[] = {
75 {
Eric Paris2ced3df2008-04-17 13:37:12 -040076 .version = POLICYDB_VERSION_BASE,
77 .sym_num = SYM_NUM - 3,
78 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 },
80 {
Eric Paris2ced3df2008-04-17 13:37:12 -040081 .version = POLICYDB_VERSION_BOOL,
82 .sym_num = SYM_NUM - 2,
83 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 },
85 {
Eric Paris2ced3df2008-04-17 13:37:12 -040086 .version = POLICYDB_VERSION_IPV6,
87 .sym_num = SYM_NUM - 2,
88 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 },
90 {
Eric Paris2ced3df2008-04-17 13:37:12 -040091 .version = POLICYDB_VERSION_NLCLASS,
92 .sym_num = SYM_NUM - 2,
93 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 },
95 {
Eric Paris2ced3df2008-04-17 13:37:12 -040096 .version = POLICYDB_VERSION_MLS,
97 .sym_num = SYM_NUM,
98 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 },
Stephen Smalley782ebb92005-09-03 15:55:16 -0700100 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400101 .version = POLICYDB_VERSION_AVTAB,
102 .sym_num = SYM_NUM,
103 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700104 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700105 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400106 .version = POLICYDB_VERSION_RANGETRANS,
107 .sym_num = SYM_NUM,
108 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700109 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500110 {
111 .version = POLICYDB_VERSION_POLCAP,
112 .sym_num = SYM_NUM,
113 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100114 },
115 {
116 .version = POLICYDB_VERSION_PERMISSIVE,
117 .sym_num = SYM_NUM,
118 .ocon_num = OCON_NUM,
Paul Moore3bb56b22008-01-29 08:38:19 -0500119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122static struct policydb_compat_info *policydb_lookup_compat(int version)
123{
124 int i;
125 struct policydb_compat_info *info = NULL;
126
Tobias Klauser32725ad2006-01-06 00:11:23 -0800127 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (policydb_compat[i].version == version) {
129 info = &policydb_compat[i];
130 break;
131 }
132 }
133 return info;
134}
135
136/*
137 * Initialize the role table.
138 */
139static int roles_init(struct policydb *p)
140{
141 char *key = NULL;
142 int rc;
143 struct role_datum *role;
144
James Morris89d155e2005-10-30 14:59:21 -0800145 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!role) {
147 rc = -ENOMEM;
148 goto out;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 role->value = ++p->p_roles.nprim;
151 if (role->value != OBJECT_R_VAL) {
152 rc = -EINVAL;
153 goto out_free_role;
154 }
Eric Paris2ced3df2008-04-17 13:37:12 -0400155 key = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!key) {
157 rc = -ENOMEM;
158 goto out_free_role;
159 }
160 strcpy(key, OBJECT_R);
161 rc = hashtab_insert(p->p_roles.table, key, role);
162 if (rc)
163 goto out_free_key;
164out:
165 return rc;
166
167out_free_key:
168 kfree(key);
169out_free_role:
170 kfree(role);
171 goto out;
172}
173
174/*
175 * Initialize a policy database structure.
176 */
177static int policydb_init(struct policydb *p)
178{
179 int i, rc;
180
181 memset(p, 0, sizeof(*p));
182
183 for (i = 0; i < SYM_NUM; i++) {
184 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
185 if (rc)
186 goto out_free_symtab;
187 }
188
189 rc = avtab_init(&p->te_avtab);
190 if (rc)
191 goto out_free_symtab;
192
193 rc = roles_init(p);
194 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900195 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 rc = cond_policydb_init(p);
198 if (rc)
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900199 goto out_free_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Paul Moore3bb56b22008-01-29 08:38:19 -0500201 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100202 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204out:
205 return rc;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207out_free_symtab:
208 for (i = 0; i < SYM_NUM; i++)
209 hashtab_destroy(p->symtab[i].table);
210 goto out;
211}
212
213/*
214 * The following *_index functions are used to
215 * define the val_to_name and val_to_struct arrays
216 * in a policy database structure. The val_to_name
217 * arrays are used when converting security context
218 * structures into string representations. The
219 * val_to_struct arrays are used when the attributes
220 * of a class, role, or user are needed.
221 */
222
223static int common_index(void *key, void *datum, void *datap)
224{
225 struct policydb *p;
226 struct common_datum *comdatum;
227
228 comdatum = datum;
229 p = datap;
230 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
231 return -EINVAL;
232 p->p_common_val_to_name[comdatum->value - 1] = key;
233 return 0;
234}
235
236static int class_index(void *key, void *datum, void *datap)
237{
238 struct policydb *p;
239 struct class_datum *cladatum;
240
241 cladatum = datum;
242 p = datap;
243 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
244 return -EINVAL;
245 p->p_class_val_to_name[cladatum->value - 1] = key;
246 p->class_val_to_struct[cladatum->value - 1] = cladatum;
247 return 0;
248}
249
250static int role_index(void *key, void *datum, void *datap)
251{
252 struct policydb *p;
253 struct role_datum *role;
254
255 role = datum;
256 p = datap;
257 if (!role->value || role->value > p->p_roles.nprim)
258 return -EINVAL;
259 p->p_role_val_to_name[role->value - 1] = key;
260 p->role_val_to_struct[role->value - 1] = role;
261 return 0;
262}
263
264static int type_index(void *key, void *datum, void *datap)
265{
266 struct policydb *p;
267 struct type_datum *typdatum;
268
269 typdatum = datum;
270 p = datap;
271
272 if (typdatum->primary) {
273 if (!typdatum->value || typdatum->value > p->p_types.nprim)
274 return -EINVAL;
275 p->p_type_val_to_name[typdatum->value - 1] = key;
276 }
277
278 return 0;
279}
280
281static int user_index(void *key, void *datum, void *datap)
282{
283 struct policydb *p;
284 struct user_datum *usrdatum;
285
286 usrdatum = datum;
287 p = datap;
288 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
289 return -EINVAL;
290 p->p_user_val_to_name[usrdatum->value - 1] = key;
291 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
292 return 0;
293}
294
295static int sens_index(void *key, void *datum, void *datap)
296{
297 struct policydb *p;
298 struct level_datum *levdatum;
299
300 levdatum = datum;
301 p = datap;
302
303 if (!levdatum->isalias) {
304 if (!levdatum->level->sens ||
305 levdatum->level->sens > p->p_levels.nprim)
306 return -EINVAL;
307 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
308 }
309
310 return 0;
311}
312
313static int cat_index(void *key, void *datum, void *datap)
314{
315 struct policydb *p;
316 struct cat_datum *catdatum;
317
318 catdatum = datum;
319 p = datap;
320
321 if (!catdatum->isalias) {
322 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
323 return -EINVAL;
324 p->p_cat_val_to_name[catdatum->value - 1] = key;
325 }
326
327 return 0;
328}
329
330static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
331{
332 common_index,
333 class_index,
334 role_index,
335 type_index,
336 user_index,
337 cond_index_bool,
338 sens_index,
339 cat_index,
340};
341
342/*
343 * Define the common val_to_name array and the class
344 * val_to_name and val_to_struct arrays in a policy
345 * database structure.
346 *
347 * Caller must clean up upon failure.
348 */
349static int policydb_index_classes(struct policydb *p)
350{
351 int rc;
352
353 p->p_common_val_to_name =
354 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
355 if (!p->p_common_val_to_name) {
356 rc = -ENOMEM;
357 goto out;
358 }
359
360 rc = hashtab_map(p->p_commons.table, common_index, p);
361 if (rc)
362 goto out;
363
364 p->class_val_to_struct =
365 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
366 if (!p->class_val_to_struct) {
367 rc = -ENOMEM;
368 goto out;
369 }
370
371 p->p_class_val_to_name =
372 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
373 if (!p->p_class_val_to_name) {
374 rc = -ENOMEM;
375 goto out;
376 }
377
378 rc = hashtab_map(p->p_classes.table, class_index, p);
379out:
380 return rc;
381}
382
383#ifdef DEBUG_HASHES
384static void symtab_hash_eval(struct symtab *s)
385{
386 int i;
387
388 for (i = 0; i < SYM_NUM; i++) {
389 struct hashtab *h = s[i].table;
390 struct hashtab_info info;
391
392 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400393 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 "longest chain length %d\n", symtab_name[i], h->nel,
395 info.slots_used, h->size, info.max_chain_len);
396 }
397}
398#endif
399
400/*
401 * Define the other val_to_name and val_to_struct arrays
402 * in a policy database structure.
403 *
404 * Caller must clean up on failure.
405 */
406static int policydb_index_others(struct policydb *p)
407{
408 int i, rc = 0;
409
James Morris454d9722008-02-26 20:42:02 +1100410 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
412 if (selinux_mls_enabled)
413 printk(", %d sens, %d cats", p->p_levels.nprim,
414 p->p_cats.nprim);
415 printk("\n");
416
James Morris454d9722008-02-26 20:42:02 +1100417 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 p->p_classes.nprim, p->te_avtab.nel);
419
420#ifdef DEBUG_HASHES
421 avtab_hash_eval(&p->te_avtab, "rules");
422 symtab_hash_eval(p->symtab);
423#endif
424
425 p->role_val_to_struct =
426 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400427 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!p->role_val_to_struct) {
429 rc = -ENOMEM;
430 goto out;
431 }
432
433 p->user_val_to_struct =
434 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400435 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!p->user_val_to_struct) {
437 rc = -ENOMEM;
438 goto out;
439 }
440
441 if (cond_init_bool_indexes(p)) {
442 rc = -ENOMEM;
443 goto out;
444 }
445
446 for (i = SYM_ROLES; i < SYM_NUM; i++) {
447 p->sym_val_to_name[i] =
448 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
449 if (!p->sym_val_to_name[i]) {
450 rc = -ENOMEM;
451 goto out;
452 }
453 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
454 if (rc)
455 goto out;
456 }
457
458out:
459 return rc;
460}
461
462/*
463 * The following *_destroy functions are used to
464 * free any memory allocated for each kind of
465 * symbol data in the policy database.
466 */
467
468static int perm_destroy(void *key, void *datum, void *p)
469{
470 kfree(key);
471 kfree(datum);
472 return 0;
473}
474
475static int common_destroy(void *key, void *datum, void *p)
476{
477 struct common_datum *comdatum;
478
479 kfree(key);
480 comdatum = datum;
481 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
482 hashtab_destroy(comdatum->permissions.table);
483 kfree(datum);
484 return 0;
485}
486
James Morris6cbda6b2006-11-29 16:50:27 -0500487static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct class_datum *cladatum;
490 struct constraint_node *constraint, *ctemp;
491 struct constraint_expr *e, *etmp;
492
493 kfree(key);
494 cladatum = datum;
495 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
496 hashtab_destroy(cladatum->permissions.table);
497 constraint = cladatum->constraints;
498 while (constraint) {
499 e = constraint->expr;
500 while (e) {
501 ebitmap_destroy(&e->names);
502 etmp = e;
503 e = e->next;
504 kfree(etmp);
505 }
506 ctemp = constraint;
507 constraint = constraint->next;
508 kfree(ctemp);
509 }
510
511 constraint = cladatum->validatetrans;
512 while (constraint) {
513 e = constraint->expr;
514 while (e) {
515 ebitmap_destroy(&e->names);
516 etmp = e;
517 e = e->next;
518 kfree(etmp);
519 }
520 ctemp = constraint;
521 constraint = constraint->next;
522 kfree(ctemp);
523 }
524
525 kfree(cladatum->comkey);
526 kfree(datum);
527 return 0;
528}
529
530static int role_destroy(void *key, void *datum, void *p)
531{
532 struct role_datum *role;
533
534 kfree(key);
535 role = datum;
536 ebitmap_destroy(&role->dominates);
537 ebitmap_destroy(&role->types);
538 kfree(datum);
539 return 0;
540}
541
542static int type_destroy(void *key, void *datum, void *p)
543{
544 kfree(key);
545 kfree(datum);
546 return 0;
547}
548
549static int user_destroy(void *key, void *datum, void *p)
550{
551 struct user_datum *usrdatum;
552
553 kfree(key);
554 usrdatum = datum;
555 ebitmap_destroy(&usrdatum->roles);
556 ebitmap_destroy(&usrdatum->range.level[0].cat);
557 ebitmap_destroy(&usrdatum->range.level[1].cat);
558 ebitmap_destroy(&usrdatum->dfltlevel.cat);
559 kfree(datum);
560 return 0;
561}
562
563static int sens_destroy(void *key, void *datum, void *p)
564{
565 struct level_datum *levdatum;
566
567 kfree(key);
568 levdatum = datum;
569 ebitmap_destroy(&levdatum->level->cat);
570 kfree(levdatum->level);
571 kfree(datum);
572 return 0;
573}
574
575static int cat_destroy(void *key, void *datum, void *p)
576{
577 kfree(key);
578 kfree(datum);
579 return 0;
580}
581
582static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
583{
584 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500585 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 role_destroy,
587 type_destroy,
588 user_destroy,
589 cond_destroy_bool,
590 sens_destroy,
591 cat_destroy,
592};
593
594static void ocontext_destroy(struct ocontext *c, int i)
595{
596 context_destroy(&c->context[0]);
597 context_destroy(&c->context[1]);
598 if (i == OCON_ISID || i == OCON_FS ||
599 i == OCON_NETIF || i == OCON_FSUSE)
600 kfree(c->u.name);
601 kfree(c);
602}
603
604/*
605 * Free any memory allocated by a policy database structure.
606 */
607void policydb_destroy(struct policydb *p)
608{
609 struct ocontext *c, *ctmp;
610 struct genfs *g, *gtmp;
611 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700612 struct role_allow *ra, *lra = NULL;
613 struct role_trans *tr, *ltr = NULL;
614 struct range_trans *rt, *lrt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400617 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
619 hashtab_destroy(p->symtab[i].table);
620 }
621
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700622 for (i = 0; i < SYM_NUM; i++)
623 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700625 kfree(p->class_val_to_struct);
626 kfree(p->role_val_to_struct);
627 kfree(p->user_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 avtab_destroy(&p->te_avtab);
630
631 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400632 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 c = p->ocontexts[i];
634 while (c) {
635 ctmp = c;
636 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400637 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400639 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 g = p->genfs;
643 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400644 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 kfree(g->fstype);
646 c = g->head;
647 while (c) {
648 ctmp = c;
649 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400650 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 gtmp = g;
653 g = g->next;
654 kfree(gtmp);
655 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400656 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 cond_policydb_destroy(p);
659
Stephen Smalley782ebb92005-09-03 15:55:16 -0700660 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400661 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800662 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700663 ltr = tr;
664 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800665 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700666
Eric Paris2ced3df2008-04-17 13:37:12 -0400667 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400668 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800669 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700670 lra = ra;
671 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800672 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700673
Eric Paris2ced3df2008-04-17 13:37:12 -0400674 for (rt = p->range_tr; rt; rt = rt->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400675 cond_resched();
Darrel Goeddelddccef32006-07-30 03:03:17 -0700676 if (lrt) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700677 ebitmap_destroy(&lrt->target_range.level[0].cat);
678 ebitmap_destroy(&lrt->target_range.level[1].cat);
Darrel Goeddelddccef32006-07-30 03:03:17 -0700679 kfree(lrt);
680 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700681 lrt = rt;
682 }
Darrel Goeddelddccef32006-07-30 03:03:17 -0700683 if (lrt) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700684 ebitmap_destroy(&lrt->target_range.level[0].cat);
685 ebitmap_destroy(&lrt->target_range.level[1].cat);
Darrel Goeddelddccef32006-07-30 03:03:17 -0700686 kfree(lrt);
687 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700688
Stephen Smalley282c1f52005-10-23 12:57:15 -0700689 if (p->type_attr_map) {
690 for (i = 0; i < p->p_types.nprim; i++)
691 ebitmap_destroy(&p->type_attr_map[i]);
692 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700693 kfree(p->type_attr_map);
Eric Paris3f120702007-09-21 14:37:10 -0400694 kfree(p->undefined_perms);
Paul Moore3bb56b22008-01-29 08:38:19 -0500695 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100696 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699}
700
701/*
702 * Load the initial SIDs specified in a policy database
703 * structure into a SID table.
704 */
705int policydb_load_isids(struct policydb *p, struct sidtab *s)
706{
707 struct ocontext *head, *c;
708 int rc;
709
710 rc = sidtab_init(s);
711 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100712 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto out;
714 }
715
716 head = p->ocontexts[OCON_ISID];
717 for (c = head; c; c = c->next) {
718 if (!c->context[0].user) {
James Morris454d9722008-02-26 20:42:02 +1100719 printk(KERN_ERR "SELinux: SID %s was never "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 "defined.\n", c->u.name);
721 rc = -EINVAL;
722 goto out;
723 }
724 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
James Morris454d9722008-02-26 20:42:02 +1100725 printk(KERN_ERR "SELinux: unable to load initial "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 "SID %s.\n", c->u.name);
727 rc = -EINVAL;
728 goto out;
729 }
730 }
731out:
732 return rc;
733}
734
Stephen Smalley45e54212007-11-07 10:08:00 -0500735int policydb_class_isvalid(struct policydb *p, unsigned int class)
736{
737 if (!class || class > p->p_classes.nprim)
738 return 0;
739 return 1;
740}
741
742int policydb_role_isvalid(struct policydb *p, unsigned int role)
743{
744 if (!role || role > p->p_roles.nprim)
745 return 0;
746 return 1;
747}
748
749int policydb_type_isvalid(struct policydb *p, unsigned int type)
750{
751 if (!type || type > p->p_types.nprim)
752 return 0;
753 return 1;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Return 1 if the fields in the security context
758 * structure `c' are valid. Return 0 otherwise.
759 */
760int policydb_context_isvalid(struct policydb *p, struct context *c)
761{
762 struct role_datum *role;
763 struct user_datum *usrdatum;
764
765 if (!c->role || c->role > p->p_roles.nprim)
766 return 0;
767
768 if (!c->user || c->user > p->p_users.nprim)
769 return 0;
770
771 if (!c->type || c->type > p->p_types.nprim)
772 return 0;
773
774 if (c->role != OBJECT_R_VAL) {
775 /*
776 * Role must be authorized for the type.
777 */
778 role = p->role_val_to_struct[c->role - 1];
779 if (!ebitmap_get_bit(&role->types,
780 c->type - 1))
781 /* role may not be associated with type */
782 return 0;
783
784 /*
785 * User must be authorized for the role.
786 */
787 usrdatum = p->user_val_to_struct[c->user - 1];
788 if (!usrdatum)
789 return 0;
790
791 if (!ebitmap_get_bit(&usrdatum->roles,
792 c->role - 1))
793 /* user may not be associated with role */
794 return 0;
795 }
796
797 if (!mls_context_isvalid(p, c))
798 return 0;
799
800 return 1;
801}
802
803/*
804 * Read a MLS range structure from a policydb binary
805 * representation file.
806 */
807static int mls_read_range_helper(struct mls_range *r, void *fp)
808{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700809 __le32 buf[2];
810 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int rc;
812
813 rc = next_entry(buf, fp, sizeof(u32));
814 if (rc < 0)
815 goto out;
816
817 items = le32_to_cpu(buf[0]);
818 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100819 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 rc = -EINVAL;
821 goto out;
822 }
823 rc = next_entry(buf, fp, sizeof(u32) * items);
824 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100825 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto out;
827 }
828 r->level[0].sens = le32_to_cpu(buf[0]);
829 if (items > 1)
830 r->level[1].sens = le32_to_cpu(buf[1]);
831 else
832 r->level[1].sens = r->level[0].sens;
833
834 rc = ebitmap_read(&r->level[0].cat, fp);
835 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100836 printk(KERN_ERR "SELinux: mls: error reading low "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 "categories\n");
838 goto out;
839 }
840 if (items > 1) {
841 rc = ebitmap_read(&r->level[1].cat, fp);
842 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100843 printk(KERN_ERR "SELinux: mls: error reading high "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 "categories\n");
845 goto bad_high;
846 }
847 } else {
848 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
849 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100850 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto bad_high;
852 }
853 }
854
855 rc = 0;
856out:
857 return rc;
858bad_high:
859 ebitmap_destroy(&r->level[0].cat);
860 goto out;
861}
862
863/*
864 * Read and validate a security context structure
865 * from a policydb binary representation file.
866 */
867static int context_read_and_validate(struct context *c,
868 struct policydb *p,
869 void *fp)
870{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700871 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int rc;
873
874 rc = next_entry(buf, fp, sizeof buf);
875 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100876 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out;
878 }
879 c->user = le32_to_cpu(buf[0]);
880 c->role = le32_to_cpu(buf[1]);
881 c->type = le32_to_cpu(buf[2]);
882 if (p->policyvers >= POLICYDB_VERSION_MLS) {
883 if (mls_read_range_helper(&c->range, fp)) {
James Morris454d9722008-02-26 20:42:02 +1100884 printk(KERN_ERR "SELinux: error reading MLS range of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 "context\n");
886 rc = -EINVAL;
887 goto out;
888 }
889 }
890
891 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100892 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 context_destroy(c);
894 rc = -EINVAL;
895 }
896out:
897 return rc;
898}
899
900/*
901 * The following *_read functions are used to
902 * read the symbol data from a policy database
903 * binary representation file.
904 */
905
906static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
907{
908 char *key = NULL;
909 struct perm_datum *perdatum;
910 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700911 __le32 buf[2];
912 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
James Morris89d155e2005-10-30 14:59:21 -0800914 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!perdatum) {
916 rc = -ENOMEM;
917 goto out;
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 rc = next_entry(buf, fp, sizeof buf);
921 if (rc < 0)
922 goto bad;
923
924 len = le32_to_cpu(buf[0]);
925 perdatum->value = le32_to_cpu(buf[1]);
926
Eric Paris2ced3df2008-04-17 13:37:12 -0400927 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!key) {
929 rc = -ENOMEM;
930 goto bad;
931 }
932 rc = next_entry(key, fp, len);
933 if (rc < 0)
934 goto bad;
935 key[len] = 0;
936
937 rc = hashtab_insert(h, key, perdatum);
938 if (rc)
939 goto bad;
940out:
941 return rc;
942bad:
943 perm_destroy(key, perdatum, NULL);
944 goto out;
945}
946
947static int common_read(struct policydb *p, struct hashtab *h, void *fp)
948{
949 char *key = NULL;
950 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700951 __le32 buf[4];
952 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int i, rc;
954
James Morris89d155e2005-10-30 14:59:21 -0800955 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!comdatum) {
957 rc = -ENOMEM;
958 goto out;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 rc = next_entry(buf, fp, sizeof buf);
962 if (rc < 0)
963 goto bad;
964
965 len = le32_to_cpu(buf[0]);
966 comdatum->value = le32_to_cpu(buf[1]);
967
968 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
969 if (rc)
970 goto bad;
971 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
972 nel = le32_to_cpu(buf[3]);
973
Eric Paris2ced3df2008-04-17 13:37:12 -0400974 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!key) {
976 rc = -ENOMEM;
977 goto bad;
978 }
979 rc = next_entry(key, fp, len);
980 if (rc < 0)
981 goto bad;
982 key[len] = 0;
983
984 for (i = 0; i < nel; i++) {
985 rc = perm_read(p, comdatum->permissions.table, fp);
986 if (rc)
987 goto bad;
988 }
989
990 rc = hashtab_insert(h, key, comdatum);
991 if (rc)
992 goto bad;
993out:
994 return rc;
995bad:
996 common_destroy(key, comdatum, NULL);
997 goto out;
998}
999
1000static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001001 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct constraint_node *c, *lc;
1004 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001005 __le32 buf[3];
1006 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int rc, i, j, depth;
1008
1009 lc = NULL;
1010 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001011 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!c)
1013 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Paris2ced3df2008-04-17 13:37:12 -04001015 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001017 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1021 if (rc < 0)
1022 return rc;
1023 c->permissions = le32_to_cpu(buf[0]);
1024 nexpr = le32_to_cpu(buf[1]);
1025 le = NULL;
1026 depth = -1;
1027 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001028 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!e)
1030 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Eric Paris2ced3df2008-04-17 13:37:12 -04001032 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001034 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1038 if (rc < 0)
1039 return rc;
1040 e->expr_type = le32_to_cpu(buf[0]);
1041 e->attr = le32_to_cpu(buf[1]);
1042 e->op = le32_to_cpu(buf[2]);
1043
1044 switch (e->expr_type) {
1045 case CEXPR_NOT:
1046 if (depth < 0)
1047 return -EINVAL;
1048 break;
1049 case CEXPR_AND:
1050 case CEXPR_OR:
1051 if (depth < 1)
1052 return -EINVAL;
1053 depth--;
1054 break;
1055 case CEXPR_ATTR:
1056 if (depth == (CEXPR_MAXDEPTH - 1))
1057 return -EINVAL;
1058 depth++;
1059 break;
1060 case CEXPR_NAMES:
1061 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1062 return -EINVAL;
1063 if (depth == (CEXPR_MAXDEPTH - 1))
1064 return -EINVAL;
1065 depth++;
1066 if (ebitmap_read(&e->names, fp))
1067 return -EINVAL;
1068 break;
1069 default:
1070 return -EINVAL;
1071 }
1072 le = e;
1073 }
1074 if (depth != 0)
1075 return -EINVAL;
1076 lc = c;
1077 }
1078
1079 return 0;
1080}
1081
1082static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1083{
1084 char *key = NULL;
1085 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001086 __le32 buf[6];
1087 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int i, rc;
1089
James Morris89d155e2005-10-30 14:59:21 -08001090 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!cladatum) {
1092 rc = -ENOMEM;
1093 goto out;
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 rc = next_entry(buf, fp, sizeof(u32)*6);
1097 if (rc < 0)
1098 goto bad;
1099
1100 len = le32_to_cpu(buf[0]);
1101 len2 = le32_to_cpu(buf[1]);
1102 cladatum->value = le32_to_cpu(buf[2]);
1103
1104 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1105 if (rc)
1106 goto bad;
1107 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1108 nel = le32_to_cpu(buf[4]);
1109
1110 ncons = le32_to_cpu(buf[5]);
1111
Eric Paris2ced3df2008-04-17 13:37:12 -04001112 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!key) {
1114 rc = -ENOMEM;
1115 goto bad;
1116 }
1117 rc = next_entry(key, fp, len);
1118 if (rc < 0)
1119 goto bad;
1120 key[len] = 0;
1121
1122 if (len2) {
Eric Paris2ced3df2008-04-17 13:37:12 -04001123 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!cladatum->comkey) {
1125 rc = -ENOMEM;
1126 goto bad;
1127 }
1128 rc = next_entry(cladatum->comkey, fp, len2);
1129 if (rc < 0)
1130 goto bad;
1131 cladatum->comkey[len2] = 0;
1132
1133 cladatum->comdatum = hashtab_search(p->p_commons.table,
1134 cladatum->comkey);
1135 if (!cladatum->comdatum) {
James Morris454d9722008-02-26 20:42:02 +11001136 printk(KERN_ERR "SELinux: unknown common %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 cladatum->comkey);
1138 rc = -EINVAL;
1139 goto bad;
1140 }
1141 }
1142 for (i = 0; i < nel; i++) {
1143 rc = perm_read(p, cladatum->permissions.table, fp);
1144 if (rc)
1145 goto bad;
1146 }
1147
1148 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1149 if (rc)
1150 goto bad;
1151
1152 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1153 /* grab the validatetrans rules */
1154 rc = next_entry(buf, fp, sizeof(u32));
1155 if (rc < 0)
1156 goto bad;
1157 ncons = le32_to_cpu(buf[0]);
1158 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1159 if (rc)
1160 goto bad;
1161 }
1162
1163 rc = hashtab_insert(h, key, cladatum);
1164 if (rc)
1165 goto bad;
1166
1167 rc = 0;
1168out:
1169 return rc;
1170bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001171 cls_destroy(key, cladatum, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto out;
1173}
1174
1175static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1176{
1177 char *key = NULL;
1178 struct role_datum *role;
1179 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001180 __le32 buf[2];
1181 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
James Morris89d155e2005-10-30 14:59:21 -08001183 role = kzalloc(sizeof(*role), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!role) {
1185 rc = -ENOMEM;
1186 goto out;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 rc = next_entry(buf, fp, sizeof buf);
1190 if (rc < 0)
1191 goto bad;
1192
1193 len = le32_to_cpu(buf[0]);
1194 role->value = le32_to_cpu(buf[1]);
1195
Eric Paris2ced3df2008-04-17 13:37:12 -04001196 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!key) {
1198 rc = -ENOMEM;
1199 goto bad;
1200 }
1201 rc = next_entry(key, fp, len);
1202 if (rc < 0)
1203 goto bad;
1204 key[len] = 0;
1205
1206 rc = ebitmap_read(&role->dominates, fp);
1207 if (rc)
1208 goto bad;
1209
1210 rc = ebitmap_read(&role->types, fp);
1211 if (rc)
1212 goto bad;
1213
1214 if (strcmp(key, OBJECT_R) == 0) {
1215 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001216 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 OBJECT_R, role->value);
1218 rc = -EINVAL;
1219 goto bad;
1220 }
1221 rc = 0;
1222 goto bad;
1223 }
1224
1225 rc = hashtab_insert(h, key, role);
1226 if (rc)
1227 goto bad;
1228out:
1229 return rc;
1230bad:
1231 role_destroy(key, role, NULL);
1232 goto out;
1233}
1234
1235static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1236{
1237 char *key = NULL;
1238 struct type_datum *typdatum;
1239 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001240 __le32 buf[3];
1241 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Eric Paris2ced3df2008-04-17 13:37:12 -04001243 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!typdatum) {
1245 rc = -ENOMEM;
1246 return rc;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 rc = next_entry(buf, fp, sizeof buf);
1250 if (rc < 0)
1251 goto bad;
1252
1253 len = le32_to_cpu(buf[0]);
1254 typdatum->value = le32_to_cpu(buf[1]);
1255 typdatum->primary = le32_to_cpu(buf[2]);
1256
Eric Paris2ced3df2008-04-17 13:37:12 -04001257 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!key) {
1259 rc = -ENOMEM;
1260 goto bad;
1261 }
1262 rc = next_entry(key, fp, len);
1263 if (rc < 0)
1264 goto bad;
1265 key[len] = 0;
1266
1267 rc = hashtab_insert(h, key, typdatum);
1268 if (rc)
1269 goto bad;
1270out:
1271 return rc;
1272bad:
1273 type_destroy(key, typdatum, NULL);
1274 goto out;
1275}
1276
1277
1278/*
1279 * Read a MLS level structure from a policydb binary
1280 * representation file.
1281 */
1282static int mls_read_level(struct mls_level *lp, void *fp)
1283{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001284 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int rc;
1286
1287 memset(lp, 0, sizeof(*lp));
1288
1289 rc = next_entry(buf, fp, sizeof buf);
1290 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001291 printk(KERN_ERR "SELinux: mls: truncated level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto bad;
1293 }
1294 lp->sens = le32_to_cpu(buf[0]);
1295
1296 if (ebitmap_read(&lp->cat, fp)) {
James Morris454d9722008-02-26 20:42:02 +11001297 printk(KERN_ERR "SELinux: mls: error reading level "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 "categories\n");
1299 goto bad;
1300 }
Stephen Smalley45e54212007-11-07 10:08:00 -05001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303
1304bad:
1305 return -EINVAL;
1306}
1307
1308static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1309{
1310 char *key = NULL;
1311 struct user_datum *usrdatum;
1312 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001313 __le32 buf[2];
1314 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
James Morris89d155e2005-10-30 14:59:21 -08001316 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!usrdatum) {
1318 rc = -ENOMEM;
1319 goto out;
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 rc = next_entry(buf, fp, sizeof buf);
1323 if (rc < 0)
1324 goto bad;
1325
1326 len = le32_to_cpu(buf[0]);
1327 usrdatum->value = le32_to_cpu(buf[1]);
1328
Eric Paris2ced3df2008-04-17 13:37:12 -04001329 key = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!key) {
1331 rc = -ENOMEM;
1332 goto bad;
1333 }
1334 rc = next_entry(key, fp, len);
1335 if (rc < 0)
1336 goto bad;
1337 key[len] = 0;
1338
1339 rc = ebitmap_read(&usrdatum->roles, fp);
1340 if (rc)
1341 goto bad;
1342
1343 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1344 rc = mls_read_range_helper(&usrdatum->range, fp);
1345 if (rc)
1346 goto bad;
1347 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1348 if (rc)
1349 goto bad;
1350 }
1351
1352 rc = hashtab_insert(h, key, usrdatum);
1353 if (rc)
1354 goto bad;
1355out:
1356 return rc;
1357bad:
1358 user_destroy(key, usrdatum, NULL);
1359 goto out;
1360}
1361
1362static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1363{
1364 char *key = NULL;
1365 struct level_datum *levdatum;
1366 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001367 __le32 buf[2];
1368 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
James Morris89d155e2005-10-30 14:59:21 -08001370 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!levdatum) {
1372 rc = -ENOMEM;
1373 goto out;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 rc = next_entry(buf, fp, sizeof buf);
1377 if (rc < 0)
1378 goto bad;
1379
1380 len = le32_to_cpu(buf[0]);
1381 levdatum->isalias = le32_to_cpu(buf[1]);
1382
Eric Paris2ced3df2008-04-17 13:37:12 -04001383 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!key) {
1385 rc = -ENOMEM;
1386 goto bad;
1387 }
1388 rc = next_entry(key, fp, len);
1389 if (rc < 0)
1390 goto bad;
1391 key[len] = 0;
1392
1393 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1394 if (!levdatum->level) {
1395 rc = -ENOMEM;
1396 goto bad;
1397 }
1398 if (mls_read_level(levdatum->level, fp)) {
1399 rc = -EINVAL;
1400 goto bad;
1401 }
1402
1403 rc = hashtab_insert(h, key, levdatum);
1404 if (rc)
1405 goto bad;
1406out:
1407 return rc;
1408bad:
1409 sens_destroy(key, levdatum, NULL);
1410 goto out;
1411}
1412
1413static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1414{
1415 char *key = NULL;
1416 struct cat_datum *catdatum;
1417 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001418 __le32 buf[3];
1419 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
James Morris89d155e2005-10-30 14:59:21 -08001421 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!catdatum) {
1423 rc = -ENOMEM;
1424 goto out;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 rc = next_entry(buf, fp, sizeof buf);
1428 if (rc < 0)
1429 goto bad;
1430
1431 len = le32_to_cpu(buf[0]);
1432 catdatum->value = le32_to_cpu(buf[1]);
1433 catdatum->isalias = le32_to_cpu(buf[2]);
1434
Eric Paris2ced3df2008-04-17 13:37:12 -04001435 key = kmalloc(len + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!key) {
1437 rc = -ENOMEM;
1438 goto bad;
1439 }
1440 rc = next_entry(key, fp, len);
1441 if (rc < 0)
1442 goto bad;
1443 key[len] = 0;
1444
1445 rc = hashtab_insert(h, key, catdatum);
1446 if (rc)
1447 goto bad;
1448out:
1449 return rc;
1450
1451bad:
1452 cat_destroy(key, catdatum, NULL);
1453 goto out;
1454}
1455
1456static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1457{
1458 common_read,
1459 class_read,
1460 role_read,
1461 type_read,
1462 user_read,
1463 cond_read_bool,
1464 sens_read,
1465 cat_read,
1466};
1467
1468extern int ss_initialized;
1469
1470/*
1471 * Read the configuration data from a policy database binary
1472 * representation file into a policy database structure.
1473 */
1474int policydb_read(struct policydb *p, void *fp)
1475{
1476 struct role_allow *ra, *lra;
1477 struct role_trans *tr, *ltr;
1478 struct ocontext *l, *c, *newc;
1479 struct genfs *genfs_p, *genfs, *newgenfs;
1480 int i, j, rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001481 __le32 buf[8];
1482 u32 len, len2, config, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 char *policydb_str;
1484 struct policydb_compat_info *info;
1485 struct range_trans *rt, *lrt;
1486
1487 config = 0;
1488
1489 rc = policydb_init(p);
1490 if (rc)
1491 goto out;
1492
1493 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04001494 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (rc < 0)
1496 goto bad;
1497
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001498 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11001499 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001501 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto bad;
1503 }
1504
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001505 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001507 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 "match expected length %Zu\n",
1509 len, strlen(POLICYDB_STRING));
1510 goto bad;
1511 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001512 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11001514 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 "string of length %d\n", len);
1516 rc = -ENOMEM;
1517 goto bad;
1518 }
1519 rc = next_entry(policydb_str, fp, len);
1520 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +11001521 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 kfree(policydb_str);
1523 goto bad;
1524 }
1525 policydb_str[len] = 0;
1526 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11001527 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 "my string %s\n", policydb_str, POLICYDB_STRING);
1529 kfree(policydb_str);
1530 goto bad;
1531 }
1532 /* Done with policydb_str. */
1533 kfree(policydb_str);
1534 policydb_str = NULL;
1535
1536 /* Read the version, config, and table sizes. */
1537 rc = next_entry(buf, fp, sizeof(u32)*4);
1538 if (rc < 0)
1539 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001541 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (p->policyvers < POLICYDB_VERSION_MIN ||
1543 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11001544 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04001545 "my version range %d-%d\n",
1546 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1547 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001550 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (ss_initialized && !selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001552 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1553 " and MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 goto bad;
1555 }
1556 selinux_mls_enabled = 1;
1557 config |= POLICYDB_CONFIG_MLS;
1558
1559 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04001560 printk(KERN_ERR "SELinux: security policydb version %d "
1561 "(MLS) not backwards compatible\n",
1562 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 goto bad;
1564 }
1565 } else {
1566 if (ss_initialized && selinux_mls_enabled) {
Eric Paris744ba352008-04-17 11:52:44 -04001567 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1568 " non-MLS policies\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 goto bad;
1570 }
1571 }
Eric Paris3f120702007-09-21 14:37:10 -04001572 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1573 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Paul Moore3bb56b22008-01-29 08:38:19 -05001575 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1576 ebitmap_read(&p->policycaps, fp) != 0)
1577 goto bad;
1578
Eric Paris64dbf072008-03-31 12:17:33 +11001579 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1580 ebitmap_read(&p->permissive_map, fp) != 0)
1581 goto bad;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 info = policydb_lookup_compat(p->policyvers);
1584 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11001585 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 "for version %d\n", p->policyvers);
1587 goto bad;
1588 }
1589
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001590 if (le32_to_cpu(buf[2]) != info->sym_num ||
1591 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11001592 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001593 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1594 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 info->sym_num, info->ocon_num);
1596 goto bad;
1597 }
1598
1599 for (i = 0; i < info->sym_num; i++) {
1600 rc = next_entry(buf, fp, sizeof(u32)*2);
1601 if (rc < 0)
1602 goto bad;
1603 nprim = le32_to_cpu(buf[0]);
1604 nel = le32_to_cpu(buf[1]);
1605 for (j = 0; j < nel; j++) {
1606 rc = read_f[i](p, p->symtab[i].table, fp);
1607 if (rc)
1608 goto bad;
1609 }
1610
1611 p->symtab[i].nprim = nprim;
1612 }
1613
Stephen Smalley45e54212007-11-07 10:08:00 -05001614 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (rc)
1616 goto bad;
1617
1618 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1619 rc = cond_read_list(p, fp);
1620 if (rc)
1621 goto bad;
1622 }
1623
1624 rc = next_entry(buf, fp, sizeof(u32));
1625 if (rc < 0)
1626 goto bad;
1627 nel = le32_to_cpu(buf[0]);
1628 ltr = NULL;
1629 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001630 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!tr) {
1632 rc = -ENOMEM;
1633 goto bad;
1634 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001635 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04001637 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 rc = next_entry(buf, fp, sizeof(u32)*3);
1640 if (rc < 0)
1641 goto bad;
1642 tr->role = le32_to_cpu(buf[0]);
1643 tr->type = le32_to_cpu(buf[1]);
1644 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001645 if (!policydb_role_isvalid(p, tr->role) ||
1646 !policydb_type_isvalid(p, tr->type) ||
1647 !policydb_role_isvalid(p, tr->new_role)) {
1648 rc = -EINVAL;
1649 goto bad;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ltr = tr;
1652 }
1653
1654 rc = next_entry(buf, fp, sizeof(u32));
1655 if (rc < 0)
1656 goto bad;
1657 nel = le32_to_cpu(buf[0]);
1658 lra = NULL;
1659 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001660 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!ra) {
1662 rc = -ENOMEM;
1663 goto bad;
1664 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001665 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04001667 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rc = next_entry(buf, fp, sizeof(u32)*2);
1670 if (rc < 0)
1671 goto bad;
1672 ra->role = le32_to_cpu(buf[0]);
1673 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05001674 if (!policydb_role_isvalid(p, ra->role) ||
1675 !policydb_role_isvalid(p, ra->new_role)) {
1676 rc = -EINVAL;
1677 goto bad;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 lra = ra;
1680 }
1681
1682 rc = policydb_index_classes(p);
1683 if (rc)
1684 goto bad;
1685
1686 rc = policydb_index_others(p);
1687 if (rc)
1688 goto bad;
1689
1690 for (i = 0; i < info->ocon_num; i++) {
1691 rc = next_entry(buf, fp, sizeof(u32));
1692 if (rc < 0)
1693 goto bad;
1694 nel = le32_to_cpu(buf[0]);
1695 l = NULL;
1696 for (j = 0; j < nel; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001697 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (!c) {
1699 rc = -ENOMEM;
1700 goto bad;
1701 }
Eric Paris2ced3df2008-04-17 13:37:12 -04001702 if (l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 l->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001704 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 p->ocontexts[i] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 l = c;
1707 rc = -EINVAL;
1708 switch (i) {
1709 case OCON_ISID:
1710 rc = next_entry(buf, fp, sizeof(u32));
1711 if (rc < 0)
1712 goto bad;
1713 c->sid[0] = le32_to_cpu(buf[0]);
1714 rc = context_read_and_validate(&c->context[0], p, fp);
1715 if (rc)
1716 goto bad;
1717 break;
1718 case OCON_FS:
1719 case OCON_NETIF:
1720 rc = next_entry(buf, fp, sizeof(u32));
1721 if (rc < 0)
1722 goto bad;
1723 len = le32_to_cpu(buf[0]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001724 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (!c->u.name) {
1726 rc = -ENOMEM;
1727 goto bad;
1728 }
1729 rc = next_entry(c->u.name, fp, len);
1730 if (rc < 0)
1731 goto bad;
1732 c->u.name[len] = 0;
1733 rc = context_read_and_validate(&c->context[0], p, fp);
1734 if (rc)
1735 goto bad;
1736 rc = context_read_and_validate(&c->context[1], p, fp);
1737 if (rc)
1738 goto bad;
1739 break;
1740 case OCON_PORT:
1741 rc = next_entry(buf, fp, sizeof(u32)*3);
1742 if (rc < 0)
1743 goto bad;
1744 c->u.port.protocol = le32_to_cpu(buf[0]);
1745 c->u.port.low_port = le32_to_cpu(buf[1]);
1746 c->u.port.high_port = le32_to_cpu(buf[2]);
1747 rc = context_read_and_validate(&c->context[0], p, fp);
1748 if (rc)
1749 goto bad;
1750 break;
1751 case OCON_NODE:
Eric Paris2ced3df2008-04-17 13:37:12 -04001752 rc = next_entry(buf, fp, sizeof(u32) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (rc < 0)
1754 goto bad;
1755 c->u.node.addr = le32_to_cpu(buf[0]);
1756 c->u.node.mask = le32_to_cpu(buf[1]);
1757 rc = context_read_and_validate(&c->context[0], p, fp);
1758 if (rc)
1759 goto bad;
1760 break;
1761 case OCON_FSUSE:
1762 rc = next_entry(buf, fp, sizeof(u32)*2);
1763 if (rc < 0)
1764 goto bad;
1765 c->v.behavior = le32_to_cpu(buf[0]);
1766 if (c->v.behavior > SECURITY_FS_USE_NONE)
1767 goto bad;
1768 len = le32_to_cpu(buf[1]);
Eric Paris2ced3df2008-04-17 13:37:12 -04001769 c->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!c->u.name) {
1771 rc = -ENOMEM;
1772 goto bad;
1773 }
1774 rc = next_entry(c->u.name, fp, len);
1775 if (rc < 0)
1776 goto bad;
1777 c->u.name[len] = 0;
1778 rc = context_read_and_validate(&c->context[0], p, fp);
1779 if (rc)
1780 goto bad;
1781 break;
1782 case OCON_NODE6: {
1783 int k;
1784
1785 rc = next_entry(buf, fp, sizeof(u32) * 8);
1786 if (rc < 0)
1787 goto bad;
1788 for (k = 0; k < 4; k++)
1789 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1790 for (k = 0; k < 4; k++)
1791 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1792 if (context_read_and_validate(&c->context[0], p, fp))
1793 goto bad;
1794 break;
1795 }
1796 }
1797 }
1798 }
1799
1800 rc = next_entry(buf, fp, sizeof(u32));
1801 if (rc < 0)
1802 goto bad;
1803 nel = le32_to_cpu(buf[0]);
1804 genfs_p = NULL;
1805 rc = -EINVAL;
1806 for (i = 0; i < nel; i++) {
1807 rc = next_entry(buf, fp, sizeof(u32));
1808 if (rc < 0)
1809 goto bad;
1810 len = le32_to_cpu(buf[0]);
James Morris89d155e2005-10-30 14:59:21 -08001811 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (!newgenfs) {
1813 rc = -ENOMEM;
1814 goto bad;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Eric Paris2ced3df2008-04-17 13:37:12 -04001817 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (!newgenfs->fstype) {
1819 rc = -ENOMEM;
1820 kfree(newgenfs);
1821 goto bad;
1822 }
1823 rc = next_entry(newgenfs->fstype, fp, len);
1824 if (rc < 0) {
1825 kfree(newgenfs->fstype);
1826 kfree(newgenfs);
1827 goto bad;
1828 }
1829 newgenfs->fstype[len] = 0;
1830 for (genfs_p = NULL, genfs = p->genfs; genfs;
1831 genfs_p = genfs, genfs = genfs->next) {
1832 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
James Morris454d9722008-02-26 20:42:02 +11001833 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 "fstype %s\n", newgenfs->fstype);
1835 kfree(newgenfs->fstype);
1836 kfree(newgenfs);
1837 goto bad;
1838 }
1839 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1840 break;
1841 }
1842 newgenfs->next = genfs;
1843 if (genfs_p)
1844 genfs_p->next = newgenfs;
1845 else
1846 p->genfs = newgenfs;
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc < 0)
1849 goto bad;
1850 nel2 = le32_to_cpu(buf[0]);
1851 for (j = 0; j < nel2; j++) {
1852 rc = next_entry(buf, fp, sizeof(u32));
1853 if (rc < 0)
1854 goto bad;
1855 len = le32_to_cpu(buf[0]);
1856
James Morris89d155e2005-10-30 14:59:21 -08001857 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!newc) {
1859 rc = -ENOMEM;
1860 goto bad;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Eric Paris2ced3df2008-04-17 13:37:12 -04001863 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (!newc->u.name) {
1865 rc = -ENOMEM;
1866 goto bad_newc;
1867 }
1868 rc = next_entry(newc->u.name, fp, len);
1869 if (rc < 0)
1870 goto bad_newc;
1871 newc->u.name[len] = 0;
1872 rc = next_entry(buf, fp, sizeof(u32));
1873 if (rc < 0)
1874 goto bad_newc;
1875 newc->v.sclass = le32_to_cpu(buf[0]);
1876 if (context_read_and_validate(&newc->context[0], p, fp))
1877 goto bad_newc;
1878 for (l = NULL, c = newgenfs->head; c;
1879 l = c, c = c->next) {
1880 if (!strcmp(newc->u.name, c->u.name) &&
1881 (!c->v.sclass || !newc->v.sclass ||
1882 newc->v.sclass == c->v.sclass)) {
James Morris454d9722008-02-26 20:42:02 +11001883 printk(KERN_ERR "SELinux: dup genfs "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 "entry (%s,%s)\n",
1885 newgenfs->fstype, c->u.name);
1886 goto bad_newc;
1887 }
1888 len = strlen(newc->u.name);
1889 len2 = strlen(c->u.name);
1890 if (len > len2)
1891 break;
1892 }
1893
1894 newc->next = c;
1895 if (l)
1896 l->next = newc;
1897 else
1898 newgenfs->head = newc;
1899 }
1900 }
1901
1902 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Darrel Goeddelf3f87712006-09-25 23:31:59 -07001903 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rc = next_entry(buf, fp, sizeof(u32));
1905 if (rc < 0)
1906 goto bad;
1907 nel = le32_to_cpu(buf[0]);
1908 lrt = NULL;
1909 for (i = 0; i < nel; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001910 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!rt) {
1912 rc = -ENOMEM;
1913 goto bad;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (lrt)
1916 lrt->next = rt;
1917 else
1918 p->range_tr = rt;
1919 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1920 if (rc < 0)
1921 goto bad;
Darrel Goeddelf3f87712006-09-25 23:31:59 -07001922 rt->source_type = le32_to_cpu(buf[0]);
1923 rt->target_type = le32_to_cpu(buf[1]);
1924 if (new_rangetr) {
1925 rc = next_entry(buf, fp, sizeof(u32));
1926 if (rc < 0)
1927 goto bad;
1928 rt->target_class = le32_to_cpu(buf[0]);
1929 } else
1930 rt->target_class = SECCLASS_PROCESS;
Stephen Smalley45e54212007-11-07 10:08:00 -05001931 if (!policydb_type_isvalid(p, rt->source_type) ||
1932 !policydb_type_isvalid(p, rt->target_type) ||
1933 !policydb_class_isvalid(p, rt->target_class)) {
1934 rc = -EINVAL;
1935 goto bad;
1936 }
Darrel Goeddelf3f87712006-09-25 23:31:59 -07001937 rc = mls_read_range_helper(&rt->target_range, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (rc)
1939 goto bad;
Stephen Smalley45e54212007-11-07 10:08:00 -05001940 if (!mls_range_isvalid(p, &rt->target_range)) {
James Morris454d9722008-02-26 20:42:02 +11001941 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
Stephen Smalley45e54212007-11-07 10:08:00 -05001942 goto bad;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 lrt = rt;
1945 }
1946 }
1947
Stephen Smalley782ebb92005-09-03 15:55:16 -07001948 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1949 if (!p->type_attr_map)
1950 goto bad;
1951
1952 for (i = 0; i < p->p_types.nprim; i++) {
1953 ebitmap_init(&p->type_attr_map[i]);
1954 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1955 if (ebitmap_read(&p->type_attr_map[i], fp))
1956 goto bad;
1957 }
1958 /* add the type itself as the degenerate case */
1959 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1960 goto bad;
1961 }
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 rc = 0;
1964out:
1965 return rc;
1966bad_newc:
Eric Paris2ced3df2008-04-17 13:37:12 -04001967 ocontext_destroy(newc, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968bad:
1969 if (!rc)
1970 rc = -EINVAL;
1971 policydb_destroy(p);
1972 goto out;
1973}