1 /* 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 * 14 * Added conditional policy language extensions 15 * 16 * 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. 21 * 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 24 * it under the terms of the GNU General Public License as published by 25 * the Free Software Foundation, version 2. 26 */ 27 28 #include <linux/kernel.h> 29 #include <linux/sched.h> 30 #include <linux/slab.h> 31 #include <linux/string.h> 32 #include <linux/errno.h> 33 #include <linux/audit.h> 34 #include <linux/flex_array.h> 35 #include "security.h" 36 37 #include "policydb.h" 38 #include "conditional.h" 39 #include "mls.h" 40 #include "services.h" 41 42 #define _DEBUG_HASHES 43 44 #ifdef DEBUG_HASHES 45 static const char *symtab_name[SYM_NUM] = { 46 "common prefixes", 47 "classes", 48 "roles", 49 "types", 50 "users", 51 "bools", 52 "levels", 53 "categories", 54 }; 55 #endif 56 57 static unsigned int symtab_sizes[SYM_NUM] = { 58 2, 59 32, 60 16, 61 512, 62 128, 63 16, 64 16, 65 16, 66 }; 67 68 struct policydb_compat_info { 69 int version; 70 int sym_num; 71 int ocon_num; 72 }; 73 74 /* These need to be updated if SYM_NUM or OCON_NUM changes */ 75 static struct policydb_compat_info policydb_compat[] = { 76 { 77 .version = POLICYDB_VERSION_BASE, 78 .sym_num = SYM_NUM - 3, 79 .ocon_num = OCON_NUM - 1, 80 }, 81 { 82 .version = POLICYDB_VERSION_BOOL, 83 .sym_num = SYM_NUM - 2, 84 .ocon_num = OCON_NUM - 1, 85 }, 86 { 87 .version = POLICYDB_VERSION_IPV6, 88 .sym_num = SYM_NUM - 2, 89 .ocon_num = OCON_NUM, 90 }, 91 { 92 .version = POLICYDB_VERSION_NLCLASS, 93 .sym_num = SYM_NUM - 2, 94 .ocon_num = OCON_NUM, 95 }, 96 { 97 .version = POLICYDB_VERSION_MLS, 98 .sym_num = SYM_NUM, 99 .ocon_num = OCON_NUM, 100 }, 101 { 102 .version = POLICYDB_VERSION_AVTAB, 103 .sym_num = SYM_NUM, 104 .ocon_num = OCON_NUM, 105 }, 106 { 107 .version = POLICYDB_VERSION_RANGETRANS, 108 .sym_num = SYM_NUM, 109 .ocon_num = OCON_NUM, 110 }, 111 { 112 .version = POLICYDB_VERSION_POLCAP, 113 .sym_num = SYM_NUM, 114 .ocon_num = OCON_NUM, 115 }, 116 { 117 .version = POLICYDB_VERSION_PERMISSIVE, 118 .sym_num = SYM_NUM, 119 .ocon_num = OCON_NUM, 120 }, 121 { 122 .version = POLICYDB_VERSION_BOUNDARY, 123 .sym_num = SYM_NUM, 124 .ocon_num = OCON_NUM, 125 }, 126 }; 127 128 static struct policydb_compat_info *policydb_lookup_compat(int version) 129 { 130 int i; 131 struct policydb_compat_info *info = NULL; 132 133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) { 134 if (policydb_compat[i].version == version) { 135 info = &policydb_compat[i]; 136 break; 137 } 138 } 139 return info; 140 } 141 142 /* 143 * Initialize the role table. 144 */ 145 static int roles_init(struct policydb *p) 146 { 147 char *key = NULL; 148 int rc; 149 struct role_datum *role; 150 151 rc = -ENOMEM; 152 role = kzalloc(sizeof(*role), GFP_KERNEL); 153 if (!role) 154 goto out; 155 156 rc = -EINVAL; 157 role->value = ++p->p_roles.nprim; 158 if (role->value != OBJECT_R_VAL) 159 goto out; 160 161 rc = -ENOMEM; 162 key = kstrdup(OBJECT_R, GFP_KERNEL); 163 if (!key) 164 goto out; 165 166 rc = hashtab_insert(p->p_roles.table, key, role); 167 if (rc) 168 goto out; 169 170 return 0; 171 out: 172 kfree(key); 173 kfree(role); 174 return rc; 175 } 176 177 static 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 184 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2) 185 { 186 const struct range_trans *key1 = k1, *key2 = k2; 187 int v; 188 189 v = key1->source_type - key2->source_type; 190 if (v) 191 return v; 192 193 v = key1->target_type - key2->target_type; 194 if (v) 195 return v; 196 197 v = key1->target_class - key2->target_class; 198 199 return v; 200 } 201 202 /* 203 * Initialize a policy database structure. 204 */ 205 static int policydb_init(struct policydb *p) 206 { 207 int i, rc; 208 209 memset(p, 0, sizeof(*p)); 210 211 for (i = 0; i < SYM_NUM; i++) { 212 rc = symtab_init(&p->symtab[i], symtab_sizes[i]); 213 if (rc) 214 goto out; 215 } 216 217 rc = avtab_init(&p->te_avtab); 218 if (rc) 219 goto out; 220 221 rc = roles_init(p); 222 if (rc) 223 goto out; 224 225 rc = cond_policydb_init(p); 226 if (rc) 227 goto out; 228 229 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256); 230 if (!p->range_tr) 231 goto out; 232 233 ebitmap_init(&p->policycaps); 234 ebitmap_init(&p->permissive_map); 235 236 return 0; 237 out: 238 for (i = 0; i < SYM_NUM; i++) 239 hashtab_destroy(p->symtab[i].table); 240 return rc; 241 } 242 243 /* 244 * The following *_index functions are used to 245 * define the val_to_name and val_to_struct arrays 246 * in a policy database structure. The val_to_name 247 * arrays are used when converting security context 248 * structures into string representations. The 249 * val_to_struct arrays are used when the attributes 250 * of a class, role, or user are needed. 251 */ 252 253 static int common_index(void *key, void *datum, void *datap) 254 { 255 struct policydb *p; 256 struct common_datum *comdatum; 257 struct flex_array *fa; 258 259 comdatum = datum; 260 p = datap; 261 if (!comdatum->value || comdatum->value > p->p_commons.nprim) 262 return -EINVAL; 263 264 fa = p->sym_val_to_name[SYM_COMMONS]; 265 if (flex_array_put_ptr(fa, comdatum->value - 1, key, 266 GFP_KERNEL | __GFP_ZERO)) 267 BUG(); 268 return 0; 269 } 270 271 static int class_index(void *key, void *datum, void *datap) 272 { 273 struct policydb *p; 274 struct class_datum *cladatum; 275 struct flex_array *fa; 276 277 cladatum = datum; 278 p = datap; 279 if (!cladatum->value || cladatum->value > p->p_classes.nprim) 280 return -EINVAL; 281 fa = p->sym_val_to_name[SYM_CLASSES]; 282 if (flex_array_put_ptr(fa, cladatum->value - 1, key, 283 GFP_KERNEL | __GFP_ZERO)) 284 BUG(); 285 p->class_val_to_struct[cladatum->value - 1] = cladatum; 286 return 0; 287 } 288 289 static int role_index(void *key, void *datum, void *datap) 290 { 291 struct policydb *p; 292 struct role_datum *role; 293 struct flex_array *fa; 294 295 role = datum; 296 p = datap; 297 if (!role->value 298 || role->value > p->p_roles.nprim 299 || role->bounds > p->p_roles.nprim) 300 return -EINVAL; 301 302 fa = p->sym_val_to_name[SYM_ROLES]; 303 if (flex_array_put_ptr(fa, role->value - 1, key, 304 GFP_KERNEL | __GFP_ZERO)) 305 BUG(); 306 p->role_val_to_struct[role->value - 1] = role; 307 return 0; 308 } 309 310 static int type_index(void *key, void *datum, void *datap) 311 { 312 struct policydb *p; 313 struct type_datum *typdatum; 314 struct flex_array *fa; 315 316 typdatum = datum; 317 p = datap; 318 319 if (typdatum->primary) { 320 if (!typdatum->value 321 || typdatum->value > p->p_types.nprim 322 || typdatum->bounds > p->p_types.nprim) 323 return -EINVAL; 324 fa = p->sym_val_to_name[SYM_TYPES]; 325 if (flex_array_put_ptr(fa, typdatum->value - 1, key, 326 GFP_KERNEL | __GFP_ZERO)) 327 BUG(); 328 329 fa = p->type_val_to_struct_array; 330 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum, 331 GFP_KERNEL | __GFP_ZERO)) 332 BUG(); 333 } 334 335 return 0; 336 } 337 338 static int user_index(void *key, void *datum, void *datap) 339 { 340 struct policydb *p; 341 struct user_datum *usrdatum; 342 struct flex_array *fa; 343 344 usrdatum = datum; 345 p = datap; 346 if (!usrdatum->value 347 || usrdatum->value > p->p_users.nprim 348 || usrdatum->bounds > p->p_users.nprim) 349 return -EINVAL; 350 351 fa = p->sym_val_to_name[SYM_USERS]; 352 if (flex_array_put_ptr(fa, usrdatum->value - 1, key, 353 GFP_KERNEL | __GFP_ZERO)) 354 BUG(); 355 p->user_val_to_struct[usrdatum->value - 1] = usrdatum; 356 return 0; 357 } 358 359 static int sens_index(void *key, void *datum, void *datap) 360 { 361 struct policydb *p; 362 struct level_datum *levdatum; 363 struct flex_array *fa; 364 365 levdatum = datum; 366 p = datap; 367 368 if (!levdatum->isalias) { 369 if (!levdatum->level->sens || 370 levdatum->level->sens > p->p_levels.nprim) 371 return -EINVAL; 372 fa = p->sym_val_to_name[SYM_LEVELS]; 373 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key, 374 GFP_KERNEL | __GFP_ZERO)) 375 BUG(); 376 } 377 378 return 0; 379 } 380 381 static int cat_index(void *key, void *datum, void *datap) 382 { 383 struct policydb *p; 384 struct cat_datum *catdatum; 385 struct flex_array *fa; 386 387 catdatum = datum; 388 p = datap; 389 390 if (!catdatum->isalias) { 391 if (!catdatum->value || catdatum->value > p->p_cats.nprim) 392 return -EINVAL; 393 fa = p->sym_val_to_name[SYM_CATS]; 394 if (flex_array_put_ptr(fa, catdatum->value - 1, key, 395 GFP_KERNEL | __GFP_ZERO)) 396 BUG(); 397 } 398 399 return 0; 400 } 401 402 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) = 403 { 404 common_index, 405 class_index, 406 role_index, 407 type_index, 408 user_index, 409 cond_index_bool, 410 sens_index, 411 cat_index, 412 }; 413 414 #ifdef DEBUG_HASHES 415 static void symtab_hash_eval(struct symtab *s) 416 { 417 int i; 418 419 for (i = 0; i < SYM_NUM; i++) { 420 struct hashtab *h = s[i].table; 421 struct hashtab_info info; 422 423 hashtab_stat(h, &info); 424 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, " 425 "longest chain length %d\n", symtab_name[i], h->nel, 426 info.slots_used, h->size, info.max_chain_len); 427 } 428 } 429 430 static void rangetr_hash_eval(struct hashtab *h) 431 { 432 struct hashtab_info info; 433 434 hashtab_stat(h, &info); 435 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, " 436 "longest chain length %d\n", h->nel, 437 info.slots_used, h->size, info.max_chain_len); 438 } 439 #else 440 static inline void rangetr_hash_eval(struct hashtab *h) 441 { 442 } 443 #endif 444 445 /* 446 * Define the other val_to_name and val_to_struct arrays 447 * in a policy database structure. 448 * 449 * Caller must clean up on failure. 450 */ 451 static int policydb_index(struct policydb *p) 452 { 453 int i, rc; 454 455 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools", 456 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim); 457 if (p->mls_enabled) 458 printk(", %d sens, %d cats", p->p_levels.nprim, 459 p->p_cats.nprim); 460 printk("\n"); 461 462 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n", 463 p->p_classes.nprim, p->te_avtab.nel); 464 465 #ifdef DEBUG_HASHES 466 avtab_hash_eval(&p->te_avtab, "rules"); 467 symtab_hash_eval(p->symtab); 468 #endif 469 470 rc = -ENOMEM; 471 p->class_val_to_struct = 472 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), 473 GFP_KERNEL); 474 if (!p->class_val_to_struct) 475 goto out; 476 477 rc = -ENOMEM; 478 p->role_val_to_struct = 479 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)), 480 GFP_KERNEL); 481 if (!p->role_val_to_struct) 482 goto out; 483 484 rc = -ENOMEM; 485 p->user_val_to_struct = 486 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)), 487 GFP_KERNEL); 488 if (!p->user_val_to_struct) 489 goto out; 490 491 /* Yes, I want the sizeof the pointer, not the structure */ 492 rc = -ENOMEM; 493 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *), 494 p->p_types.nprim, 495 GFP_KERNEL | __GFP_ZERO); 496 if (!p->type_val_to_struct_array) 497 goto out; 498 499 rc = flex_array_prealloc(p->type_val_to_struct_array, 0, 500 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO); 501 if (rc) 502 goto out; 503 504 rc = cond_init_bool_indexes(p); 505 if (rc) 506 goto out; 507 508 for (i = 0; i < SYM_NUM; i++) { 509 rc = -ENOMEM; 510 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *), 511 p->symtab[i].nprim, 512 GFP_KERNEL | __GFP_ZERO); 513 if (!p->sym_val_to_name[i]) 514 goto out; 515 516 rc = flex_array_prealloc(p->sym_val_to_name[i], 517 0, p->symtab[i].nprim - 1, 518 GFP_KERNEL | __GFP_ZERO); 519 if (rc) 520 goto out; 521 522 rc = hashtab_map(p->symtab[i].table, index_f[i], p); 523 if (rc) 524 goto out; 525 } 526 rc = 0; 527 out: 528 return rc; 529 } 530 531 /* 532 * The following *_destroy functions are used to 533 * free any memory allocated for each kind of 534 * symbol data in the policy database. 535 */ 536 537 static int perm_destroy(void *key, void *datum, void *p) 538 { 539 kfree(key); 540 kfree(datum); 541 return 0; 542 } 543 544 static int common_destroy(void *key, void *datum, void *p) 545 { 546 struct common_datum *comdatum; 547 548 kfree(key); 549 if (datum) { 550 comdatum = datum; 551 hashtab_map(comdatum->permissions.table, perm_destroy, NULL); 552 hashtab_destroy(comdatum->permissions.table); 553 } 554 kfree(datum); 555 return 0; 556 } 557 558 static int cls_destroy(void *key, void *datum, void *p) 559 { 560 struct class_datum *cladatum; 561 struct constraint_node *constraint, *ctemp; 562 struct constraint_expr *e, *etmp; 563 564 kfree(key); 565 if (datum) { 566 cladatum = datum; 567 hashtab_map(cladatum->permissions.table, perm_destroy, NULL); 568 hashtab_destroy(cladatum->permissions.table); 569 constraint = cladatum->constraints; 570 while (constraint) { 571 e = constraint->expr; 572 while (e) { 573 ebitmap_destroy(&e->names); 574 etmp = e; 575 e = e->next; 576 kfree(etmp); 577 } 578 ctemp = constraint; 579 constraint = constraint->next; 580 kfree(ctemp); 581 } 582 583 constraint = cladatum->validatetrans; 584 while (constraint) { 585 e = constraint->expr; 586 while (e) { 587 ebitmap_destroy(&e->names); 588 etmp = e; 589 e = e->next; 590 kfree(etmp); 591 } 592 ctemp = constraint; 593 constraint = constraint->next; 594 kfree(ctemp); 595 } 596 597 kfree(cladatum->comkey); 598 } 599 kfree(datum); 600 return 0; 601 } 602 603 static int role_destroy(void *key, void *datum, void *p) 604 { 605 struct role_datum *role; 606 607 kfree(key); 608 if (datum) { 609 role = datum; 610 ebitmap_destroy(&role->dominates); 611 ebitmap_destroy(&role->types); 612 } 613 kfree(datum); 614 return 0; 615 } 616 617 static int type_destroy(void *key, void *datum, void *p) 618 { 619 kfree(key); 620 kfree(datum); 621 return 0; 622 } 623 624 static int user_destroy(void *key, void *datum, void *p) 625 { 626 struct user_datum *usrdatum; 627 628 kfree(key); 629 if (datum) { 630 usrdatum = datum; 631 ebitmap_destroy(&usrdatum->roles); 632 ebitmap_destroy(&usrdatum->range.level[0].cat); 633 ebitmap_destroy(&usrdatum->range.level[1].cat); 634 ebitmap_destroy(&usrdatum->dfltlevel.cat); 635 } 636 kfree(datum); 637 return 0; 638 } 639 640 static int sens_destroy(void *key, void *datum, void *p) 641 { 642 struct level_datum *levdatum; 643 644 kfree(key); 645 if (datum) { 646 levdatum = datum; 647 ebitmap_destroy(&levdatum->level->cat); 648 kfree(levdatum->level); 649 } 650 kfree(datum); 651 return 0; 652 } 653 654 static int cat_destroy(void *key, void *datum, void *p) 655 { 656 kfree(key); 657 kfree(datum); 658 return 0; 659 } 660 661 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = 662 { 663 common_destroy, 664 cls_destroy, 665 role_destroy, 666 type_destroy, 667 user_destroy, 668 cond_destroy_bool, 669 sens_destroy, 670 cat_destroy, 671 }; 672 673 static int range_tr_destroy(void *key, void *datum, void *p) 674 { 675 struct mls_range *rt = datum; 676 kfree(key); 677 ebitmap_destroy(&rt->level[0].cat); 678 ebitmap_destroy(&rt->level[1].cat); 679 kfree(datum); 680 cond_resched(); 681 return 0; 682 } 683 684 static void ocontext_destroy(struct ocontext *c, int i) 685 { 686 if (!c) 687 return; 688 689 context_destroy(&c->context[0]); 690 context_destroy(&c->context[1]); 691 if (i == OCON_ISID || i == OCON_FS || 692 i == OCON_NETIF || i == OCON_FSUSE) 693 kfree(c->u.name); 694 kfree(c); 695 } 696 697 /* 698 * Free any memory allocated by a policy database structure. 699 */ 700 void policydb_destroy(struct policydb *p) 701 { 702 struct ocontext *c, *ctmp; 703 struct genfs *g, *gtmp; 704 int i; 705 struct role_allow *ra, *lra = NULL; 706 struct role_trans *tr, *ltr = NULL; 707 708 for (i = 0; i < SYM_NUM; i++) { 709 cond_resched(); 710 hashtab_map(p->symtab[i].table, destroy_f[i], NULL); 711 hashtab_destroy(p->symtab[i].table); 712 } 713 714 for (i = 0; i < SYM_NUM; i++) { 715 if (p->sym_val_to_name[i]) 716 flex_array_free(p->sym_val_to_name[i]); 717 } 718 719 kfree(p->class_val_to_struct); 720 kfree(p->role_val_to_struct); 721 kfree(p->user_val_to_struct); 722 if (p->type_val_to_struct_array) 723 flex_array_free(p->type_val_to_struct_array); 724 725 avtab_destroy(&p->te_avtab); 726 727 for (i = 0; i < OCON_NUM; i++) { 728 cond_resched(); 729 c = p->ocontexts[i]; 730 while (c) { 731 ctmp = c; 732 c = c->next; 733 ocontext_destroy(ctmp, i); 734 } 735 p->ocontexts[i] = NULL; 736 } 737 738 g = p->genfs; 739 while (g) { 740 cond_resched(); 741 kfree(g->fstype); 742 c = g->head; 743 while (c) { 744 ctmp = c; 745 c = c->next; 746 ocontext_destroy(ctmp, OCON_FSUSE); 747 } 748 gtmp = g; 749 g = g->next; 750 kfree(gtmp); 751 } 752 p->genfs = NULL; 753 754 cond_policydb_destroy(p); 755 756 for (tr = p->role_tr; tr; tr = tr->next) { 757 cond_resched(); 758 kfree(ltr); 759 ltr = tr; 760 } 761 kfree(ltr); 762 763 for (ra = p->role_allow; ra; ra = ra->next) { 764 cond_resched(); 765 kfree(lra); 766 lra = ra; 767 } 768 kfree(lra); 769 770 hashtab_map(p->range_tr, range_tr_destroy, NULL); 771 hashtab_destroy(p->range_tr); 772 773 if (p->type_attr_map_array) { 774 for (i = 0; i < p->p_types.nprim; i++) { 775 struct ebitmap *e; 776 777 e = flex_array_get(p->type_attr_map_array, i); 778 if (!e) 779 continue; 780 ebitmap_destroy(e); 781 } 782 flex_array_free(p->type_attr_map_array); 783 } 784 ebitmap_destroy(&p->policycaps); 785 ebitmap_destroy(&p->permissive_map); 786 787 return; 788 } 789 790 /* 791 * Load the initial SIDs specified in a policy database 792 * structure into a SID table. 793 */ 794 int policydb_load_isids(struct policydb *p, struct sidtab *s) 795 { 796 struct ocontext *head, *c; 797 int rc; 798 799 rc = sidtab_init(s); 800 if (rc) { 801 printk(KERN_ERR "SELinux: out of memory on SID table init\n"); 802 goto out; 803 } 804 805 head = p->ocontexts[OCON_ISID]; 806 for (c = head; c; c = c->next) { 807 rc = -EINVAL; 808 if (!c->context[0].user) { 809 printk(KERN_ERR "SELinux: SID %s was never defined.\n", 810 c->u.name); 811 goto out; 812 } 813 814 rc = sidtab_insert(s, c->sid[0], &c->context[0]); 815 if (rc) { 816 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n", 817 c->u.name); 818 goto out; 819 } 820 } 821 rc = 0; 822 out: 823 return rc; 824 } 825 826 int policydb_class_isvalid(struct policydb *p, unsigned int class) 827 { 828 if (!class || class > p->p_classes.nprim) 829 return 0; 830 return 1; 831 } 832 833 int policydb_role_isvalid(struct policydb *p, unsigned int role) 834 { 835 if (!role || role > p->p_roles.nprim) 836 return 0; 837 return 1; 838 } 839 840 int policydb_type_isvalid(struct policydb *p, unsigned int type) 841 { 842 if (!type || type > p->p_types.nprim) 843 return 0; 844 return 1; 845 } 846 847 /* 848 * Return 1 if the fields in the security context 849 * structure `c' are valid. Return 0 otherwise. 850 */ 851 int policydb_context_isvalid(struct policydb *p, struct context *c) 852 { 853 struct role_datum *role; 854 struct user_datum *usrdatum; 855 856 if (!c->role || c->role > p->p_roles.nprim) 857 return 0; 858 859 if (!c->user || c->user > p->p_users.nprim) 860 return 0; 861 862 if (!c->type || c->type > p->p_types.nprim) 863 return 0; 864 865 if (c->role != OBJECT_R_VAL) { 866 /* 867 * Role must be authorized for the type. 868 */ 869 role = p->role_val_to_struct[c->role - 1]; 870 if (!ebitmap_get_bit(&role->types, c->type - 1)) 871 /* role may not be associated with type */ 872 return 0; 873 874 /* 875 * User must be authorized for the role. 876 */ 877 usrdatum = p->user_val_to_struct[c->user - 1]; 878 if (!usrdatum) 879 return 0; 880 881 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1)) 882 /* user may not be associated with role */ 883 return 0; 884 } 885 886 if (!mls_context_isvalid(p, c)) 887 return 0; 888 889 return 1; 890 } 891 892 /* 893 * Read a MLS range structure from a policydb binary 894 * representation file. 895 */ 896 static int mls_read_range_helper(struct mls_range *r, void *fp) 897 { 898 __le32 buf[2]; 899 u32 items; 900 int rc; 901 902 rc = next_entry(buf, fp, sizeof(u32)); 903 if (rc) 904 goto out; 905 906 rc = -EINVAL; 907 items = le32_to_cpu(buf[0]); 908 if (items > ARRAY_SIZE(buf)) { 909 printk(KERN_ERR "SELinux: mls: range overflow\n"); 910 goto out; 911 } 912 913 rc = next_entry(buf, fp, sizeof(u32) * items); 914 if (rc) { 915 printk(KERN_ERR "SELinux: mls: truncated range\n"); 916 goto out; 917 } 918 919 r->level[0].sens = le32_to_cpu(buf[0]); 920 if (items > 1) 921 r->level[1].sens = le32_to_cpu(buf[1]); 922 else 923 r->level[1].sens = r->level[0].sens; 924 925 rc = ebitmap_read(&r->level[0].cat, fp); 926 if (rc) { 927 printk(KERN_ERR "SELinux: mls: error reading low categories\n"); 928 goto out; 929 } 930 if (items > 1) { 931 rc = ebitmap_read(&r->level[1].cat, fp); 932 if (rc) { 933 printk(KERN_ERR "SELinux: mls: error reading high categories\n"); 934 goto bad_high; 935 } 936 } else { 937 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat); 938 if (rc) { 939 printk(KERN_ERR "SELinux: mls: out of memory\n"); 940 goto bad_high; 941 } 942 } 943 944 return 0; 945 bad_high: 946 ebitmap_destroy(&r->level[0].cat); 947 out: 948 return rc; 949 } 950 951 /* 952 * Read and validate a security context structure 953 * from a policydb binary representation file. 954 */ 955 static int context_read_and_validate(struct context *c, 956 struct policydb *p, 957 void *fp) 958 { 959 __le32 buf[3]; 960 int rc; 961 962 rc = next_entry(buf, fp, sizeof buf); 963 if (rc) { 964 printk(KERN_ERR "SELinux: context truncated\n"); 965 goto out; 966 } 967 c->user = le32_to_cpu(buf[0]); 968 c->role = le32_to_cpu(buf[1]); 969 c->type = le32_to_cpu(buf[2]); 970 if (p->policyvers >= POLICYDB_VERSION_MLS) { 971 rc = mls_read_range_helper(&c->range, fp); 972 if (rc) { 973 printk(KERN_ERR "SELinux: error reading MLS range of context\n"); 974 goto out; 975 } 976 } 977 978 rc = -EINVAL; 979 if (!policydb_context_isvalid(p, c)) { 980 printk(KERN_ERR "SELinux: invalid security context\n"); 981 context_destroy(c); 982 goto out; 983 } 984 rc = 0; 985 out: 986 return rc; 987 } 988 989 /* 990 * The following *_read functions are used to 991 * read the symbol data from a policy database 992 * binary representation file. 993 */ 994 995 static int perm_read(struct policydb *p, struct hashtab *h, void *fp) 996 { 997 char *key = NULL; 998 struct perm_datum *perdatum; 999 int rc; 1000 __le32 buf[2]; 1001 u32 len; 1002 1003 rc = -ENOMEM; 1004 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL); 1005 if (!perdatum) 1006 goto bad; 1007 1008 rc = next_entry(buf, fp, sizeof buf); 1009 if (rc) 1010 goto bad; 1011 1012 len = le32_to_cpu(buf[0]); 1013 perdatum->value = le32_to_cpu(buf[1]); 1014 1015 rc = -ENOMEM; 1016 key = kmalloc(len + 1, GFP_KERNEL); 1017 if (!key) 1018 goto bad; 1019 1020 rc = next_entry(key, fp, len); 1021 if (rc) 1022 goto bad; 1023 key[len] = '\0'; 1024 1025 rc = hashtab_insert(h, key, perdatum); 1026 if (rc) 1027 goto bad; 1028 1029 return 0; 1030 bad: 1031 perm_destroy(key, perdatum, NULL); 1032 return rc; 1033 } 1034 1035 static int common_read(struct policydb *p, struct hashtab *h, void *fp) 1036 { 1037 char *key = NULL; 1038 struct common_datum *comdatum; 1039 __le32 buf[4]; 1040 u32 len, nel; 1041 int i, rc; 1042 1043 rc = -ENOMEM; 1044 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL); 1045 if (!comdatum) 1046 goto bad; 1047 1048 rc = next_entry(buf, fp, sizeof buf); 1049 if (rc) 1050 goto bad; 1051 1052 len = le32_to_cpu(buf[0]); 1053 comdatum->value = le32_to_cpu(buf[1]); 1054 1055 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE); 1056 if (rc) 1057 goto bad; 1058 comdatum->permissions.nprim = le32_to_cpu(buf[2]); 1059 nel = le32_to_cpu(buf[3]); 1060 1061 rc = -ENOMEM; 1062 key = kmalloc(len + 1, GFP_KERNEL); 1063 if (!key) 1064 goto bad; 1065 1066 rc = next_entry(key, fp, len); 1067 if (rc) 1068 goto bad; 1069 key[len] = '\0'; 1070 1071 for (i = 0; i < nel; i++) { 1072 rc = perm_read(p, comdatum->permissions.table, fp); 1073 if (rc) 1074 goto bad; 1075 } 1076 1077 rc = hashtab_insert(h, key, comdatum); 1078 if (rc) 1079 goto bad; 1080 return 0; 1081 bad: 1082 common_destroy(key, comdatum, NULL); 1083 return rc; 1084 } 1085 1086 static int read_cons_helper(struct constraint_node **nodep, int ncons, 1087 int allowxtarget, void *fp) 1088 { 1089 struct constraint_node *c, *lc; 1090 struct constraint_expr *e, *le; 1091 __le32 buf[3]; 1092 u32 nexpr; 1093 int rc, i, j, depth; 1094 1095 lc = NULL; 1096 for (i = 0; i < ncons; i++) { 1097 c = kzalloc(sizeof(*c), GFP_KERNEL); 1098 if (!c) 1099 return -ENOMEM; 1100 1101 if (lc) 1102 lc->next = c; 1103 else 1104 *nodep = c; 1105 1106 rc = next_entry(buf, fp, (sizeof(u32) * 2)); 1107 if (rc) 1108 return rc; 1109 c->permissions = le32_to_cpu(buf[0]); 1110 nexpr = le32_to_cpu(buf[1]); 1111 le = NULL; 1112 depth = -1; 1113 for (j = 0; j < nexpr; j++) { 1114 e = kzalloc(sizeof(*e), GFP_KERNEL); 1115 if (!e) 1116 return -ENOMEM; 1117 1118 if (le) 1119 le->next = e; 1120 else 1121 c->expr = e; 1122 1123 rc = next_entry(buf, fp, (sizeof(u32) * 3)); 1124 if (rc) 1125 return rc; 1126 e->expr_type = le32_to_cpu(buf[0]); 1127 e->attr = le32_to_cpu(buf[1]); 1128 e->op = le32_to_cpu(buf[2]); 1129 1130 switch (e->expr_type) { 1131 case CEXPR_NOT: 1132 if (depth < 0) 1133 return -EINVAL; 1134 break; 1135 case CEXPR_AND: 1136 case CEXPR_OR: 1137 if (depth < 1) 1138 return -EINVAL; 1139 depth--; 1140 break; 1141 case CEXPR_ATTR: 1142 if (depth == (CEXPR_MAXDEPTH - 1)) 1143 return -EINVAL; 1144 depth++; 1145 break; 1146 case CEXPR_NAMES: 1147 if (!allowxtarget && (e->attr & CEXPR_XTARGET)) 1148 return -EINVAL; 1149 if (depth == (CEXPR_MAXDEPTH - 1)) 1150 return -EINVAL; 1151 depth++; 1152 rc = ebitmap_read(&e->names, fp); 1153 if (rc) 1154 return rc; 1155 break; 1156 default: 1157 return -EINVAL; 1158 } 1159 le = e; 1160 } 1161 if (depth != 0) 1162 return -EINVAL; 1163 lc = c; 1164 } 1165 1166 return 0; 1167 } 1168 1169 static int class_read(struct policydb *p, struct hashtab *h, void *fp) 1170 { 1171 char *key = NULL; 1172 struct class_datum *cladatum; 1173 __le32 buf[6]; 1174 u32 len, len2, ncons, nel; 1175 int i, rc; 1176 1177 rc = -ENOMEM; 1178 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL); 1179 if (!cladatum) 1180 goto bad; 1181 1182 rc = next_entry(buf, fp, sizeof(u32)*6); 1183 if (rc) 1184 goto bad; 1185 1186 len = le32_to_cpu(buf[0]); 1187 len2 = le32_to_cpu(buf[1]); 1188 cladatum->value = le32_to_cpu(buf[2]); 1189 1190 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE); 1191 if (rc) 1192 goto bad; 1193 cladatum->permissions.nprim = le32_to_cpu(buf[3]); 1194 nel = le32_to_cpu(buf[4]); 1195 1196 ncons = le32_to_cpu(buf[5]); 1197 1198 rc = -ENOMEM; 1199 key = kmalloc(len + 1, GFP_KERNEL); 1200 if (!key) 1201 goto bad; 1202 1203 rc = next_entry(key, fp, len); 1204 if (rc) 1205 goto bad; 1206 key[len] = '\0'; 1207 1208 if (len2) { 1209 rc = -ENOMEM; 1210 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL); 1211 if (!cladatum->comkey) 1212 goto bad; 1213 rc = next_entry(cladatum->comkey, fp, len2); 1214 if (rc) 1215 goto bad; 1216 cladatum->comkey[len2] = '\0'; 1217 1218 rc = -EINVAL; 1219 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); 1220 if (!cladatum->comdatum) { 1221 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey); 1222 goto bad; 1223 } 1224 } 1225 for (i = 0; i < nel; i++) { 1226 rc = perm_read(p, cladatum->permissions.table, fp); 1227 if (rc) 1228 goto bad; 1229 } 1230 1231 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp); 1232 if (rc) 1233 goto bad; 1234 1235 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) { 1236 /* grab the validatetrans rules */ 1237 rc = next_entry(buf, fp, sizeof(u32)); 1238 if (rc) 1239 goto bad; 1240 ncons = le32_to_cpu(buf[0]); 1241 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp); 1242 if (rc) 1243 goto bad; 1244 } 1245 1246 rc = hashtab_insert(h, key, cladatum); 1247 if (rc) 1248 goto bad; 1249 1250 return 0; 1251 bad: 1252 cls_destroy(key, cladatum, NULL); 1253 return rc; 1254 } 1255 1256 static int role_read(struct policydb *p, struct hashtab *h, void *fp) 1257 { 1258 char *key = NULL; 1259 struct role_datum *role; 1260 int rc, to_read = 2; 1261 __le32 buf[3]; 1262 u32 len; 1263 1264 rc = -ENOMEM; 1265 role = kzalloc(sizeof(*role), GFP_KERNEL); 1266 if (!role) 1267 goto bad; 1268 1269 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 1270 to_read = 3; 1271 1272 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); 1273 if (rc) 1274 goto bad; 1275 1276 len = le32_to_cpu(buf[0]); 1277 role->value = le32_to_cpu(buf[1]); 1278 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 1279 role->bounds = le32_to_cpu(buf[2]); 1280 1281 rc = -ENOMEM; 1282 key = kmalloc(len + 1, GFP_KERNEL); 1283 if (!key) 1284 goto bad; 1285 1286 rc = next_entry(key, fp, len); 1287 if (rc) 1288 goto bad; 1289 key[len] = '\0'; 1290 1291 rc = ebitmap_read(&role->dominates, fp); 1292 if (rc) 1293 goto bad; 1294 1295 rc = ebitmap_read(&role->types, fp); 1296 if (rc) 1297 goto bad; 1298 1299 if (strcmp(key, OBJECT_R) == 0) { 1300 rc = -EINVAL; 1301 if (role->value != OBJECT_R_VAL) { 1302 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n", 1303 OBJECT_R, role->value); 1304 goto bad; 1305 } 1306 rc = 0; 1307 goto bad; 1308 } 1309 1310 rc = hashtab_insert(h, key, role); 1311 if (rc) 1312 goto bad; 1313 return 0; 1314 bad: 1315 role_destroy(key, role, NULL); 1316 return rc; 1317 } 1318 1319 static int type_read(struct policydb *p, struct hashtab *h, void *fp) 1320 { 1321 char *key = NULL; 1322 struct type_datum *typdatum; 1323 int rc, to_read = 3; 1324 __le32 buf[4]; 1325 u32 len; 1326 1327 rc = -ENOMEM; 1328 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL); 1329 if (!typdatum) 1330 goto bad; 1331 1332 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 1333 to_read = 4; 1334 1335 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); 1336 if (rc) 1337 goto bad; 1338 1339 len = le32_to_cpu(buf[0]); 1340 typdatum->value = le32_to_cpu(buf[1]); 1341 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { 1342 u32 prop = le32_to_cpu(buf[2]); 1343 1344 if (prop & TYPEDATUM_PROPERTY_PRIMARY) 1345 typdatum->primary = 1; 1346 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE) 1347 typdatum->attribute = 1; 1348 1349 typdatum->bounds = le32_to_cpu(buf[3]); 1350 } else { 1351 typdatum->primary = le32_to_cpu(buf[2]); 1352 } 1353 1354 rc = -ENOMEM; 1355 key = kmalloc(len + 1, GFP_KERNEL); 1356 if (!key) 1357 goto bad; 1358 rc = next_entry(key, fp, len); 1359 if (rc) 1360 goto bad; 1361 key[len] = '\0'; 1362 1363 rc = hashtab_insert(h, key, typdatum); 1364 if (rc) 1365 goto bad; 1366 return 0; 1367 bad: 1368 type_destroy(key, typdatum, NULL); 1369 return rc; 1370 } 1371 1372 1373 /* 1374 * Read a MLS level structure from a policydb binary 1375 * representation file. 1376 */ 1377 static int mls_read_level(struct mls_level *lp, void *fp) 1378 { 1379 __le32 buf[1]; 1380 int rc; 1381 1382 memset(lp, 0, sizeof(*lp)); 1383 1384 rc = next_entry(buf, fp, sizeof buf); 1385 if (rc) { 1386 printk(KERN_ERR "SELinux: mls: truncated level\n"); 1387 return rc; 1388 } 1389 lp->sens = le32_to_cpu(buf[0]); 1390 1391 rc = ebitmap_read(&lp->cat, fp); 1392 if (rc) { 1393 printk(KERN_ERR "SELinux: mls: error reading level categories\n"); 1394 return rc; 1395 } 1396 return 0; 1397 } 1398 1399 static int user_read(struct policydb *p, struct hashtab *h, void *fp) 1400 { 1401 char *key = NULL; 1402 struct user_datum *usrdatum; 1403 int rc, to_read = 2; 1404 __le32 buf[3]; 1405 u32 len; 1406 1407 rc = -ENOMEM; 1408 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL); 1409 if (!usrdatum) 1410 goto bad; 1411 1412 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 1413 to_read = 3; 1414 1415 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); 1416 if (rc) 1417 goto bad; 1418 1419 len = le32_to_cpu(buf[0]); 1420 usrdatum->value = le32_to_cpu(buf[1]); 1421 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 1422 usrdatum->bounds = le32_to_cpu(buf[2]); 1423 1424 rc = -ENOMEM; 1425 key = kmalloc(len + 1, GFP_KERNEL); 1426 if (!key) 1427 goto bad; 1428 rc = next_entry(key, fp, len); 1429 if (rc) 1430 goto bad; 1431 key[len] = '\0'; 1432 1433 rc = ebitmap_read(&usrdatum->roles, fp); 1434 if (rc) 1435 goto bad; 1436 1437 if (p->policyvers >= POLICYDB_VERSION_MLS) { 1438 rc = mls_read_range_helper(&usrdatum->range, fp); 1439 if (rc) 1440 goto bad; 1441 rc = mls_read_level(&usrdatum->dfltlevel, fp); 1442 if (rc) 1443 goto bad; 1444 } 1445 1446 rc = hashtab_insert(h, key, usrdatum); 1447 if (rc) 1448 goto bad; 1449 return 0; 1450 bad: 1451 user_destroy(key, usrdatum, NULL); 1452 return rc; 1453 } 1454 1455 static int sens_read(struct policydb *p, struct hashtab *h, void *fp) 1456 { 1457 char *key = NULL; 1458 struct level_datum *levdatum; 1459 int rc; 1460 __le32 buf[2]; 1461 u32 len; 1462 1463 rc = -ENOMEM; 1464 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC); 1465 if (!levdatum) 1466 goto bad; 1467 1468 rc = next_entry(buf, fp, sizeof buf); 1469 if (rc) 1470 goto bad; 1471 1472 len = le32_to_cpu(buf[0]); 1473 levdatum->isalias = le32_to_cpu(buf[1]); 1474 1475 rc = -ENOMEM; 1476 key = kmalloc(len + 1, GFP_ATOMIC); 1477 if (!key) 1478 goto bad; 1479 rc = next_entry(key, fp, len); 1480 if (rc) 1481 goto bad; 1482 key[len] = '\0'; 1483 1484 rc = -ENOMEM; 1485 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); 1486 if (!levdatum->level) 1487 goto bad; 1488 1489 rc = mls_read_level(levdatum->level, fp); 1490 if (rc) 1491 goto bad; 1492 1493 rc = hashtab_insert(h, key, levdatum); 1494 if (rc) 1495 goto bad; 1496 return 0; 1497 bad: 1498 sens_destroy(key, levdatum, NULL); 1499 return rc; 1500 } 1501 1502 static int cat_read(struct policydb *p, struct hashtab *h, void *fp) 1503 { 1504 char *key = NULL; 1505 struct cat_datum *catdatum; 1506 int rc; 1507 __le32 buf[3]; 1508 u32 len; 1509 1510 rc = -ENOMEM; 1511 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC); 1512 if (!catdatum) 1513 goto bad; 1514 1515 rc = next_entry(buf, fp, sizeof buf); 1516 if (rc) 1517 goto bad; 1518 1519 len = le32_to_cpu(buf[0]); 1520 catdatum->value = le32_to_cpu(buf[1]); 1521 catdatum->isalias = le32_to_cpu(buf[2]); 1522 1523 rc = -ENOMEM; 1524 key = kmalloc(len + 1, GFP_ATOMIC); 1525 if (!key) 1526 goto bad; 1527 rc = next_entry(key, fp, len); 1528 if (rc) 1529 goto bad; 1530 key[len] = '\0'; 1531 1532 rc = hashtab_insert(h, key, catdatum); 1533 if (rc) 1534 goto bad; 1535 return 0; 1536 bad: 1537 cat_destroy(key, catdatum, NULL); 1538 return rc; 1539 } 1540 1541 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) = 1542 { 1543 common_read, 1544 class_read, 1545 role_read, 1546 type_read, 1547 user_read, 1548 cond_read_bool, 1549 sens_read, 1550 cat_read, 1551 }; 1552 1553 static int user_bounds_sanity_check(void *key, void *datum, void *datap) 1554 { 1555 struct user_datum *upper, *user; 1556 struct policydb *p = datap; 1557 int depth = 0; 1558 1559 upper = user = datum; 1560 while (upper->bounds) { 1561 struct ebitmap_node *node; 1562 unsigned long bit; 1563 1564 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { 1565 printk(KERN_ERR "SELinux: user %s: " 1566 "too deep or looped boundary", 1567 (char *) key); 1568 return -EINVAL; 1569 } 1570 1571 upper = p->user_val_to_struct[upper->bounds - 1]; 1572 ebitmap_for_each_positive_bit(&user->roles, node, bit) { 1573 if (ebitmap_get_bit(&upper->roles, bit)) 1574 continue; 1575 1576 printk(KERN_ERR 1577 "SELinux: boundary violated policy: " 1578 "user=%s role=%s bounds=%s\n", 1579 sym_name(p, SYM_USERS, user->value - 1), 1580 sym_name(p, SYM_ROLES, bit), 1581 sym_name(p, SYM_USERS, upper->value - 1)); 1582 1583 return -EINVAL; 1584 } 1585 } 1586 1587 return 0; 1588 } 1589 1590 static int role_bounds_sanity_check(void *key, void *datum, void *datap) 1591 { 1592 struct role_datum *upper, *role; 1593 struct policydb *p = datap; 1594 int depth = 0; 1595 1596 upper = role = datum; 1597 while (upper->bounds) { 1598 struct ebitmap_node *node; 1599 unsigned long bit; 1600 1601 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { 1602 printk(KERN_ERR "SELinux: role %s: " 1603 "too deep or looped bounds\n", 1604 (char *) key); 1605 return -EINVAL; 1606 } 1607 1608 upper = p->role_val_to_struct[upper->bounds - 1]; 1609 ebitmap_for_each_positive_bit(&role->types, node, bit) { 1610 if (ebitmap_get_bit(&upper->types, bit)) 1611 continue; 1612 1613 printk(KERN_ERR 1614 "SELinux: boundary violated policy: " 1615 "role=%s type=%s bounds=%s\n", 1616 sym_name(p, SYM_ROLES, role->value - 1), 1617 sym_name(p, SYM_TYPES, bit), 1618 sym_name(p, SYM_ROLES, upper->value - 1)); 1619 1620 return -EINVAL; 1621 } 1622 } 1623 1624 return 0; 1625 } 1626 1627 static int type_bounds_sanity_check(void *key, void *datum, void *datap) 1628 { 1629 struct type_datum *upper; 1630 struct policydb *p = datap; 1631 int depth = 0; 1632 1633 upper = datum; 1634 while (upper->bounds) { 1635 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { 1636 printk(KERN_ERR "SELinux: type %s: " 1637 "too deep or looped boundary\n", 1638 (char *) key); 1639 return -EINVAL; 1640 } 1641 1642 upper = flex_array_get_ptr(p->type_val_to_struct_array, 1643 upper->bounds - 1); 1644 BUG_ON(!upper); 1645 1646 if (upper->attribute) { 1647 printk(KERN_ERR "SELinux: type %s: " 1648 "bounded by attribute %s", 1649 (char *) key, 1650 sym_name(p, SYM_TYPES, upper->value - 1)); 1651 return -EINVAL; 1652 } 1653 } 1654 1655 return 0; 1656 } 1657 1658 static int policydb_bounds_sanity_check(struct policydb *p) 1659 { 1660 int rc; 1661 1662 if (p->policyvers < POLICYDB_VERSION_BOUNDARY) 1663 return 0; 1664 1665 rc = hashtab_map(p->p_users.table, 1666 user_bounds_sanity_check, p); 1667 if (rc) 1668 return rc; 1669 1670 rc = hashtab_map(p->p_roles.table, 1671 role_bounds_sanity_check, p); 1672 if (rc) 1673 return rc; 1674 1675 rc = hashtab_map(p->p_types.table, 1676 type_bounds_sanity_check, p); 1677 if (rc) 1678 return rc; 1679 1680 return 0; 1681 } 1682 1683 extern int ss_initialized; 1684 1685 u16 string_to_security_class(struct policydb *p, const char *name) 1686 { 1687 struct class_datum *cladatum; 1688 1689 cladatum = hashtab_search(p->p_classes.table, name); 1690 if (!cladatum) 1691 return 0; 1692 1693 return cladatum->value; 1694 } 1695 1696 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name) 1697 { 1698 struct class_datum *cladatum; 1699 struct perm_datum *perdatum = NULL; 1700 struct common_datum *comdatum; 1701 1702 if (!tclass || tclass > p->p_classes.nprim) 1703 return 0; 1704 1705 cladatum = p->class_val_to_struct[tclass-1]; 1706 comdatum = cladatum->comdatum; 1707 if (comdatum) 1708 perdatum = hashtab_search(comdatum->permissions.table, 1709 name); 1710 if (!perdatum) 1711 perdatum = hashtab_search(cladatum->permissions.table, 1712 name); 1713 if (!perdatum) 1714 return 0; 1715 1716 return 1U << (perdatum->value-1); 1717 } 1718 1719 static int range_read(struct policydb *p, void *fp) 1720 { 1721 struct range_trans *rt = NULL; 1722 struct mls_range *r = NULL; 1723 int i, rc; 1724 __le32 buf[2]; 1725 u32 nel; 1726 1727 if (p->policyvers < POLICYDB_VERSION_MLS) 1728 return 0; 1729 1730 rc = next_entry(buf, fp, sizeof(u32)); 1731 if (rc) 1732 goto out; 1733 1734 nel = le32_to_cpu(buf[0]); 1735 for (i = 0; i < nel; i++) { 1736 rc = -ENOMEM; 1737 rt = kzalloc(sizeof(*rt), GFP_KERNEL); 1738 if (!rt) 1739 goto out; 1740 1741 rc = next_entry(buf, fp, (sizeof(u32) * 2)); 1742 if (rc) 1743 goto out; 1744 1745 rt->source_type = le32_to_cpu(buf[0]); 1746 rt->target_type = le32_to_cpu(buf[1]); 1747 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { 1748 rc = next_entry(buf, fp, sizeof(u32)); 1749 if (rc) 1750 goto out; 1751 rt->target_class = le32_to_cpu(buf[0]); 1752 } else 1753 rt->target_class = p->process_class; 1754 1755 rc = -EINVAL; 1756 if (!policydb_type_isvalid(p, rt->source_type) || 1757 !policydb_type_isvalid(p, rt->target_type) || 1758 !policydb_class_isvalid(p, rt->target_class)) 1759 goto out; 1760 1761 rc = -ENOMEM; 1762 r = kzalloc(sizeof(*r), GFP_KERNEL); 1763 if (!r) 1764 goto out; 1765 1766 rc = mls_read_range_helper(r, fp); 1767 if (rc) 1768 goto out; 1769 1770 rc = -EINVAL; 1771 if (!mls_range_isvalid(p, r)) { 1772 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n"); 1773 goto out; 1774 } 1775 1776 rc = hashtab_insert(p->range_tr, rt, r); 1777 if (rc) 1778 goto out; 1779 1780 rt = NULL; 1781 r = NULL; 1782 } 1783 rangetr_hash_eval(p->range_tr); 1784 rc = 0; 1785 out: 1786 kfree(rt); 1787 kfree(r); 1788 return rc; 1789 } 1790 1791 static int genfs_read(struct policydb *p, void *fp) 1792 { 1793 int i, j, rc; 1794 u32 nel, nel2, len, len2; 1795 __le32 buf[1]; 1796 struct ocontext *l, *c; 1797 struct ocontext *newc = NULL; 1798 struct genfs *genfs_p, *genfs; 1799 struct genfs *newgenfs = NULL; 1800 1801 rc = next_entry(buf, fp, sizeof(u32)); 1802 if (rc) 1803 goto out; 1804 nel = le32_to_cpu(buf[0]); 1805 1806 for (i = 0; i < nel; i++) { 1807 rc = next_entry(buf, fp, sizeof(u32)); 1808 if (rc) 1809 goto out; 1810 len = le32_to_cpu(buf[0]); 1811 1812 rc = -ENOMEM; 1813 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL); 1814 if (!newgenfs) 1815 goto out; 1816 1817 rc = -ENOMEM; 1818 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL); 1819 if (!newgenfs->fstype) 1820 goto out; 1821 1822 rc = next_entry(newgenfs->fstype, fp, len); 1823 if (rc) 1824 goto out; 1825 1826 newgenfs->fstype[len] = 0; 1827 1828 for (genfs_p = NULL, genfs = p->genfs; genfs; 1829 genfs_p = genfs, genfs = genfs->next) { 1830 rc = -EINVAL; 1831 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) { 1832 printk(KERN_ERR "SELinux: dup genfs fstype %s\n", 1833 newgenfs->fstype); 1834 goto out; 1835 } 1836 if (strcmp(newgenfs->fstype, genfs->fstype) < 0) 1837 break; 1838 } 1839 newgenfs->next = genfs; 1840 if (genfs_p) 1841 genfs_p->next = newgenfs; 1842 else 1843 p->genfs = newgenfs; 1844 genfs = newgenfs; 1845 newgenfs = NULL; 1846 1847 rc = next_entry(buf, fp, sizeof(u32)); 1848 if (rc) 1849 goto out; 1850 1851 nel2 = le32_to_cpu(buf[0]); 1852 for (j = 0; j < nel2; j++) { 1853 rc = next_entry(buf, fp, sizeof(u32)); 1854 if (rc) 1855 goto out; 1856 len = le32_to_cpu(buf[0]); 1857 1858 rc = -ENOMEM; 1859 newc = kzalloc(sizeof(*newc), GFP_KERNEL); 1860 if (!newc) 1861 goto out; 1862 1863 rc = -ENOMEM; 1864 newc->u.name = kmalloc(len + 1, GFP_KERNEL); 1865 if (!newc->u.name) 1866 goto out; 1867 1868 rc = next_entry(newc->u.name, fp, len); 1869 if (rc) 1870 goto out; 1871 newc->u.name[len] = 0; 1872 1873 rc = next_entry(buf, fp, sizeof(u32)); 1874 if (rc) 1875 goto out; 1876 1877 newc->v.sclass = le32_to_cpu(buf[0]); 1878 rc = context_read_and_validate(&newc->context[0], p, fp); 1879 if (rc) 1880 goto out; 1881 1882 for (l = NULL, c = genfs->head; c; 1883 l = c, c = c->next) { 1884 rc = -EINVAL; 1885 if (!strcmp(newc->u.name, c->u.name) && 1886 (!c->v.sclass || !newc->v.sclass || 1887 newc->v.sclass == c->v.sclass)) { 1888 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n", 1889 genfs->fstype, c->u.name); 1890 goto out; 1891 } 1892 len = strlen(newc->u.name); 1893 len2 = strlen(c->u.name); 1894 if (len > len2) 1895 break; 1896 } 1897 1898 newc->next = c; 1899 if (l) 1900 l->next = newc; 1901 else 1902 genfs->head = newc; 1903 newc = NULL; 1904 } 1905 } 1906 rc = 0; 1907 out: 1908 if (newgenfs) 1909 kfree(newgenfs->fstype); 1910 kfree(newgenfs); 1911 ocontext_destroy(newc, OCON_FSUSE); 1912 1913 return rc; 1914 } 1915 1916 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, 1917 void *fp) 1918 { 1919 int i, j, rc; 1920 u32 nel, len; 1921 __le32 buf[3]; 1922 struct ocontext *l, *c; 1923 u32 nodebuf[8]; 1924 1925 for (i = 0; i < info->ocon_num; i++) { 1926 rc = next_entry(buf, fp, sizeof(u32)); 1927 if (rc) 1928 goto out; 1929 nel = le32_to_cpu(buf[0]); 1930 1931 l = NULL; 1932 for (j = 0; j < nel; j++) { 1933 rc = -ENOMEM; 1934 c = kzalloc(sizeof(*c), GFP_KERNEL); 1935 if (!c) 1936 goto out; 1937 if (l) 1938 l->next = c; 1939 else 1940 p->ocontexts[i] = c; 1941 l = c; 1942 1943 switch (i) { 1944 case OCON_ISID: 1945 rc = next_entry(buf, fp, sizeof(u32)); 1946 if (rc) 1947 goto out; 1948 1949 c->sid[0] = le32_to_cpu(buf[0]); 1950 rc = context_read_and_validate(&c->context[0], p, fp); 1951 if (rc) 1952 goto out; 1953 break; 1954 case OCON_FS: 1955 case OCON_NETIF: 1956 rc = next_entry(buf, fp, sizeof(u32)); 1957 if (rc) 1958 goto out; 1959 len = le32_to_cpu(buf[0]); 1960 1961 rc = -ENOMEM; 1962 c->u.name = kmalloc(len + 1, GFP_KERNEL); 1963 if (!c->u.name) 1964 goto out; 1965 1966 rc = next_entry(c->u.name, fp, len); 1967 if (rc) 1968 goto out; 1969 1970 c->u.name[len] = 0; 1971 rc = context_read_and_validate(&c->context[0], p, fp); 1972 if (rc) 1973 goto out; 1974 rc = context_read_and_validate(&c->context[1], p, fp); 1975 if (rc) 1976 goto out; 1977 break; 1978 case OCON_PORT: 1979 rc = next_entry(buf, fp, sizeof(u32)*3); 1980 if (rc) 1981 goto out; 1982 c->u.port.protocol = le32_to_cpu(buf[0]); 1983 c->u.port.low_port = le32_to_cpu(buf[1]); 1984 c->u.port.high_port = le32_to_cpu(buf[2]); 1985 rc = context_read_and_validate(&c->context[0], p, fp); 1986 if (rc) 1987 goto out; 1988 break; 1989 case OCON_NODE: 1990 rc = next_entry(nodebuf, fp, sizeof(u32) * 2); 1991 if (rc) 1992 goto out; 1993 c->u.node.addr = nodebuf[0]; /* network order */ 1994 c->u.node.mask = nodebuf[1]; /* network order */ 1995 rc = context_read_and_validate(&c->context[0], p, fp); 1996 if (rc) 1997 goto out; 1998 break; 1999 case OCON_FSUSE: 2000 rc = next_entry(buf, fp, sizeof(u32)*2); 2001 if (rc) 2002 goto out; 2003 2004 rc = -EINVAL; 2005 c->v.behavior = le32_to_cpu(buf[0]); 2006 if (c->v.behavior > SECURITY_FS_USE_NONE) 2007 goto out; 2008 2009 rc = -ENOMEM; 2010 len = le32_to_cpu(buf[1]); 2011 c->u.name = kmalloc(len + 1, GFP_KERNEL); 2012 if (!c->u.name) 2013 goto out; 2014 2015 rc = next_entry(c->u.name, fp, len); 2016 if (rc) 2017 goto out; 2018 c->u.name[len] = 0; 2019 rc = context_read_and_validate(&c->context[0], p, fp); 2020 if (rc) 2021 goto out; 2022 break; 2023 case OCON_NODE6: { 2024 int k; 2025 2026 rc = next_entry(nodebuf, fp, sizeof(u32) * 8); 2027 if (rc) 2028 goto out; 2029 for (k = 0; k < 4; k++) 2030 c->u.node6.addr[k] = nodebuf[k]; 2031 for (k = 0; k < 4; k++) 2032 c->u.node6.mask[k] = nodebuf[k+4]; 2033 rc = context_read_and_validate(&c->context[0], p, fp); 2034 if (rc) 2035 goto out; 2036 break; 2037 } 2038 } 2039 } 2040 } 2041 rc = 0; 2042 out: 2043 return rc; 2044 } 2045 2046 /* 2047 * Read the configuration data from a policy database binary 2048 * representation file into a policy database structure. 2049 */ 2050 int policydb_read(struct policydb *p, void *fp) 2051 { 2052 struct role_allow *ra, *lra; 2053 struct role_trans *tr, *ltr; 2054 int i, j, rc; 2055 __le32 buf[4]; 2056 u32 len, nprim, nel; 2057 2058 char *policydb_str; 2059 struct policydb_compat_info *info; 2060 2061 rc = policydb_init(p); 2062 if (rc) 2063 return rc; 2064 2065 /* Read the magic number and string length. */ 2066 rc = next_entry(buf, fp, sizeof(u32) * 2); 2067 if (rc) 2068 goto bad; 2069 2070 rc = -EINVAL; 2071 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) { 2072 printk(KERN_ERR "SELinux: policydb magic number 0x%x does " 2073 "not match expected magic number 0x%x\n", 2074 le32_to_cpu(buf[0]), POLICYDB_MAGIC); 2075 goto bad; 2076 } 2077 2078 rc = -EINVAL; 2079 len = le32_to_cpu(buf[1]); 2080 if (len != strlen(POLICYDB_STRING)) { 2081 printk(KERN_ERR "SELinux: policydb string length %d does not " 2082 "match expected length %Zu\n", 2083 len, strlen(POLICYDB_STRING)); 2084 goto bad; 2085 } 2086 2087 rc = -ENOMEM; 2088 policydb_str = kmalloc(len + 1, GFP_KERNEL); 2089 if (!policydb_str) { 2090 printk(KERN_ERR "SELinux: unable to allocate memory for policydb " 2091 "string of length %d\n", len); 2092 goto bad; 2093 } 2094 2095 rc = next_entry(policydb_str, fp, len); 2096 if (rc) { 2097 printk(KERN_ERR "SELinux: truncated policydb string identifier\n"); 2098 kfree(policydb_str); 2099 goto bad; 2100 } 2101 2102 rc = -EINVAL; 2103 policydb_str[len] = '\0'; 2104 if (strcmp(policydb_str, POLICYDB_STRING)) { 2105 printk(KERN_ERR "SELinux: policydb string %s does not match " 2106 "my string %s\n", policydb_str, POLICYDB_STRING); 2107 kfree(policydb_str); 2108 goto bad; 2109 } 2110 /* Done with policydb_str. */ 2111 kfree(policydb_str); 2112 policydb_str = NULL; 2113 2114 /* Read the version and table sizes. */ 2115 rc = next_entry(buf, fp, sizeof(u32)*4); 2116 if (rc) 2117 goto bad; 2118 2119 rc = -EINVAL; 2120 p->policyvers = le32_to_cpu(buf[0]); 2121 if (p->policyvers < POLICYDB_VERSION_MIN || 2122 p->policyvers > POLICYDB_VERSION_MAX) { 2123 printk(KERN_ERR "SELinux: policydb version %d does not match " 2124 "my version range %d-%d\n", 2125 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX); 2126 goto bad; 2127 } 2128 2129 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) { 2130 p->mls_enabled = 1; 2131 2132 rc = -EINVAL; 2133 if (p->policyvers < POLICYDB_VERSION_MLS) { 2134 printk(KERN_ERR "SELinux: security policydb version %d " 2135 "(MLS) not backwards compatible\n", 2136 p->policyvers); 2137 goto bad; 2138 } 2139 } 2140 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN); 2141 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN); 2142 2143 if (p->policyvers >= POLICYDB_VERSION_POLCAP) { 2144 rc = ebitmap_read(&p->policycaps, fp); 2145 if (rc) 2146 goto bad; 2147 } 2148 2149 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { 2150 rc = ebitmap_read(&p->permissive_map, fp); 2151 if (rc) 2152 goto bad; 2153 } 2154 2155 rc = -EINVAL; 2156 info = policydb_lookup_compat(p->policyvers); 2157 if (!info) { 2158 printk(KERN_ERR "SELinux: unable to find policy compat info " 2159 "for version %d\n", p->policyvers); 2160 goto bad; 2161 } 2162 2163 rc = -EINVAL; 2164 if (le32_to_cpu(buf[2]) != info->sym_num || 2165 le32_to_cpu(buf[3]) != info->ocon_num) { 2166 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do " 2167 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]), 2168 le32_to_cpu(buf[3]), 2169 info->sym_num, info->ocon_num); 2170 goto bad; 2171 } 2172 2173 for (i = 0; i < info->sym_num; i++) { 2174 rc = next_entry(buf, fp, sizeof(u32)*2); 2175 if (rc) 2176 goto bad; 2177 nprim = le32_to_cpu(buf[0]); 2178 nel = le32_to_cpu(buf[1]); 2179 for (j = 0; j < nel; j++) { 2180 rc = read_f[i](p, p->symtab[i].table, fp); 2181 if (rc) 2182 goto bad; 2183 } 2184 2185 p->symtab[i].nprim = nprim; 2186 } 2187 2188 rc = avtab_read(&p->te_avtab, fp, p); 2189 if (rc) 2190 goto bad; 2191 2192 if (p->policyvers >= POLICYDB_VERSION_BOOL) { 2193 rc = cond_read_list(p, fp); 2194 if (rc) 2195 goto bad; 2196 } 2197 2198 rc = next_entry(buf, fp, sizeof(u32)); 2199 if (rc) 2200 goto bad; 2201 nel = le32_to_cpu(buf[0]); 2202 ltr = NULL; 2203 for (i = 0; i < nel; i++) { 2204 rc = -ENOMEM; 2205 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 2206 if (!tr) 2207 goto bad; 2208 if (ltr) 2209 ltr->next = tr; 2210 else 2211 p->role_tr = tr; 2212 rc = next_entry(buf, fp, sizeof(u32)*3); 2213 if (rc) 2214 goto bad; 2215 2216 rc = -EINVAL; 2217 tr->role = le32_to_cpu(buf[0]); 2218 tr->type = le32_to_cpu(buf[1]); 2219 tr->new_role = le32_to_cpu(buf[2]); 2220 if (!policydb_role_isvalid(p, tr->role) || 2221 !policydb_type_isvalid(p, tr->type) || 2222 !policydb_role_isvalid(p, tr->new_role)) 2223 goto bad; 2224 ltr = tr; 2225 } 2226 2227 rc = next_entry(buf, fp, sizeof(u32)); 2228 if (rc) 2229 goto bad; 2230 nel = le32_to_cpu(buf[0]); 2231 lra = NULL; 2232 for (i = 0; i < nel; i++) { 2233 rc = -ENOMEM; 2234 ra = kzalloc(sizeof(*ra), GFP_KERNEL); 2235 if (!ra) 2236 goto bad; 2237 if (lra) 2238 lra->next = ra; 2239 else 2240 p->role_allow = ra; 2241 rc = next_entry(buf, fp, sizeof(u32)*2); 2242 if (rc) 2243 goto bad; 2244 2245 rc = -EINVAL; 2246 ra->role = le32_to_cpu(buf[0]); 2247 ra->new_role = le32_to_cpu(buf[1]); 2248 if (!policydb_role_isvalid(p, ra->role) || 2249 !policydb_role_isvalid(p, ra->new_role)) 2250 goto bad; 2251 lra = ra; 2252 } 2253 2254 rc = policydb_index(p); 2255 if (rc) 2256 goto bad; 2257 2258 rc = -EINVAL; 2259 p->process_class = string_to_security_class(p, "process"); 2260 if (!p->process_class) 2261 goto bad; 2262 2263 rc = -EINVAL; 2264 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition"); 2265 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition"); 2266 if (!p->process_trans_perms) 2267 goto bad; 2268 2269 rc = ocontext_read(p, info, fp); 2270 if (rc) 2271 goto bad; 2272 2273 rc = genfs_read(p, fp); 2274 if (rc) 2275 goto bad; 2276 2277 rc = range_read(p, fp); 2278 if (rc) 2279 goto bad; 2280 2281 rc = -ENOMEM; 2282 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap), 2283 p->p_types.nprim, 2284 GFP_KERNEL | __GFP_ZERO); 2285 if (!p->type_attr_map_array) 2286 goto bad; 2287 2288 /* preallocate so we don't have to worry about the put ever failing */ 2289 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1, 2290 GFP_KERNEL | __GFP_ZERO); 2291 if (rc) 2292 goto bad; 2293 2294 for (i = 0; i < p->p_types.nprim; i++) { 2295 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); 2296 2297 BUG_ON(!e); 2298 ebitmap_init(e); 2299 if (p->policyvers >= POLICYDB_VERSION_AVTAB) { 2300 rc = ebitmap_read(e, fp); 2301 if (rc) 2302 goto bad; 2303 } 2304 /* add the type itself as the degenerate case */ 2305 rc = ebitmap_set_bit(e, i, 1); 2306 if (rc) 2307 goto bad; 2308 } 2309 2310 rc = policydb_bounds_sanity_check(p); 2311 if (rc) 2312 goto bad; 2313 2314 rc = 0; 2315 out: 2316 return rc; 2317 bad: 2318 policydb_destroy(p); 2319 goto out; 2320 } 2321 2322 /* 2323 * Write a MLS level structure to a policydb binary 2324 * representation file. 2325 */ 2326 static int mls_write_level(struct mls_level *l, void *fp) 2327 { 2328 __le32 buf[1]; 2329 int rc; 2330 2331 buf[0] = cpu_to_le32(l->sens); 2332 rc = put_entry(buf, sizeof(u32), 1, fp); 2333 if (rc) 2334 return rc; 2335 2336 rc = ebitmap_write(&l->cat, fp); 2337 if (rc) 2338 return rc; 2339 2340 return 0; 2341 } 2342 2343 /* 2344 * Write a MLS range structure to a policydb binary 2345 * representation file. 2346 */ 2347 static int mls_write_range_helper(struct mls_range *r, void *fp) 2348 { 2349 __le32 buf[3]; 2350 size_t items; 2351 int rc, eq; 2352 2353 eq = mls_level_eq(&r->level[1], &r->level[0]); 2354 2355 if (eq) 2356 items = 2; 2357 else 2358 items = 3; 2359 buf[0] = cpu_to_le32(items-1); 2360 buf[1] = cpu_to_le32(r->level[0].sens); 2361 if (!eq) 2362 buf[2] = cpu_to_le32(r->level[1].sens); 2363 2364 BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); 2365 2366 rc = put_entry(buf, sizeof(u32), items, fp); 2367 if (rc) 2368 return rc; 2369 2370 rc = ebitmap_write(&r->level[0].cat, fp); 2371 if (rc) 2372 return rc; 2373 if (!eq) { 2374 rc = ebitmap_write(&r->level[1].cat, fp); 2375 if (rc) 2376 return rc; 2377 } 2378 2379 return 0; 2380 } 2381 2382 static int sens_write(void *vkey, void *datum, void *ptr) 2383 { 2384 char *key = vkey; 2385 struct level_datum *levdatum = datum; 2386 struct policy_data *pd = ptr; 2387 void *fp = pd->fp; 2388 __le32 buf[2]; 2389 size_t len; 2390 int rc; 2391 2392 len = strlen(key); 2393 buf[0] = cpu_to_le32(len); 2394 buf[1] = cpu_to_le32(levdatum->isalias); 2395 rc = put_entry(buf, sizeof(u32), 2, fp); 2396 if (rc) 2397 return rc; 2398 2399 rc = put_entry(key, 1, len, fp); 2400 if (rc) 2401 return rc; 2402 2403 rc = mls_write_level(levdatum->level, fp); 2404 if (rc) 2405 return rc; 2406 2407 return 0; 2408 } 2409 2410 static int cat_write(void *vkey, void *datum, void *ptr) 2411 { 2412 char *key = vkey; 2413 struct cat_datum *catdatum = datum; 2414 struct policy_data *pd = ptr; 2415 void *fp = pd->fp; 2416 __le32 buf[3]; 2417 size_t len; 2418 int rc; 2419 2420 len = strlen(key); 2421 buf[0] = cpu_to_le32(len); 2422 buf[1] = cpu_to_le32(catdatum->value); 2423 buf[2] = cpu_to_le32(catdatum->isalias); 2424 rc = put_entry(buf, sizeof(u32), 3, fp); 2425 if (rc) 2426 return rc; 2427 2428 rc = put_entry(key, 1, len, fp); 2429 if (rc) 2430 return rc; 2431 2432 return 0; 2433 } 2434 2435 static int role_trans_write(struct role_trans *r, void *fp) 2436 { 2437 struct role_trans *tr; 2438 u32 buf[3]; 2439 size_t nel; 2440 int rc; 2441 2442 nel = 0; 2443 for (tr = r; tr; tr = tr->next) 2444 nel++; 2445 buf[0] = cpu_to_le32(nel); 2446 rc = put_entry(buf, sizeof(u32), 1, fp); 2447 if (rc) 2448 return rc; 2449 for (tr = r; tr; tr = tr->next) { 2450 buf[0] = cpu_to_le32(tr->role); 2451 buf[1] = cpu_to_le32(tr->type); 2452 buf[2] = cpu_to_le32(tr->new_role); 2453 rc = put_entry(buf, sizeof(u32), 3, fp); 2454 if (rc) 2455 return rc; 2456 } 2457 2458 return 0; 2459 } 2460 2461 static int role_allow_write(struct role_allow *r, void *fp) 2462 { 2463 struct role_allow *ra; 2464 u32 buf[2]; 2465 size_t nel; 2466 int rc; 2467 2468 nel = 0; 2469 for (ra = r; ra; ra = ra->next) 2470 nel++; 2471 buf[0] = cpu_to_le32(nel); 2472 rc = put_entry(buf, sizeof(u32), 1, fp); 2473 if (rc) 2474 return rc; 2475 for (ra = r; ra; ra = ra->next) { 2476 buf[0] = cpu_to_le32(ra->role); 2477 buf[1] = cpu_to_le32(ra->new_role); 2478 rc = put_entry(buf, sizeof(u32), 2, fp); 2479 if (rc) 2480 return rc; 2481 } 2482 return 0; 2483 } 2484 2485 /* 2486 * Write a security context structure 2487 * to a policydb binary representation file. 2488 */ 2489 static int context_write(struct policydb *p, struct context *c, 2490 void *fp) 2491 { 2492 int rc; 2493 __le32 buf[3]; 2494 2495 buf[0] = cpu_to_le32(c->user); 2496 buf[1] = cpu_to_le32(c->role); 2497 buf[2] = cpu_to_le32(c->type); 2498 2499 rc = put_entry(buf, sizeof(u32), 3, fp); 2500 if (rc) 2501 return rc; 2502 2503 rc = mls_write_range_helper(&c->range, fp); 2504 if (rc) 2505 return rc; 2506 2507 return 0; 2508 } 2509 2510 /* 2511 * The following *_write functions are used to 2512 * write the symbol data to a policy database 2513 * binary representation file. 2514 */ 2515 2516 static int perm_write(void *vkey, void *datum, void *fp) 2517 { 2518 char *key = vkey; 2519 struct perm_datum *perdatum = datum; 2520 __le32 buf[2]; 2521 size_t len; 2522 int rc; 2523 2524 len = strlen(key); 2525 buf[0] = cpu_to_le32(len); 2526 buf[1] = cpu_to_le32(perdatum->value); 2527 rc = put_entry(buf, sizeof(u32), 2, fp); 2528 if (rc) 2529 return rc; 2530 2531 rc = put_entry(key, 1, len, fp); 2532 if (rc) 2533 return rc; 2534 2535 return 0; 2536 } 2537 2538 static int common_write(void *vkey, void *datum, void *ptr) 2539 { 2540 char *key = vkey; 2541 struct common_datum *comdatum = datum; 2542 struct policy_data *pd = ptr; 2543 void *fp = pd->fp; 2544 __le32 buf[4]; 2545 size_t len; 2546 int rc; 2547 2548 len = strlen(key); 2549 buf[0] = cpu_to_le32(len); 2550 buf[1] = cpu_to_le32(comdatum->value); 2551 buf[2] = cpu_to_le32(comdatum->permissions.nprim); 2552 buf[3] = cpu_to_le32(comdatum->permissions.table->nel); 2553 rc = put_entry(buf, sizeof(u32), 4, fp); 2554 if (rc) 2555 return rc; 2556 2557 rc = put_entry(key, 1, len, fp); 2558 if (rc) 2559 return rc; 2560 2561 rc = hashtab_map(comdatum->permissions.table, perm_write, fp); 2562 if (rc) 2563 return rc; 2564 2565 return 0; 2566 } 2567 2568 static int write_cons_helper(struct policydb *p, struct constraint_node *node, 2569 void *fp) 2570 { 2571 struct constraint_node *c; 2572 struct constraint_expr *e; 2573 __le32 buf[3]; 2574 u32 nel; 2575 int rc; 2576 2577 for (c = node; c; c = c->next) { 2578 nel = 0; 2579 for (e = c->expr; e; e = e->next) 2580 nel++; 2581 buf[0] = cpu_to_le32(c->permissions); 2582 buf[1] = cpu_to_le32(nel); 2583 rc = put_entry(buf, sizeof(u32), 2, fp); 2584 if (rc) 2585 return rc; 2586 for (e = c->expr; e; e = e->next) { 2587 buf[0] = cpu_to_le32(e->expr_type); 2588 buf[1] = cpu_to_le32(e->attr); 2589 buf[2] = cpu_to_le32(e->op); 2590 rc = put_entry(buf, sizeof(u32), 3, fp); 2591 if (rc) 2592 return rc; 2593 2594 switch (e->expr_type) { 2595 case CEXPR_NAMES: 2596 rc = ebitmap_write(&e->names, fp); 2597 if (rc) 2598 return rc; 2599 break; 2600 default: 2601 break; 2602 } 2603 } 2604 } 2605 2606 return 0; 2607 } 2608 2609 static int class_write(void *vkey, void *datum, void *ptr) 2610 { 2611 char *key = vkey; 2612 struct class_datum *cladatum = datum; 2613 struct policy_data *pd = ptr; 2614 void *fp = pd->fp; 2615 struct policydb *p = pd->p; 2616 struct constraint_node *c; 2617 __le32 buf[6]; 2618 u32 ncons; 2619 size_t len, len2; 2620 int rc; 2621 2622 len = strlen(key); 2623 if (cladatum->comkey) 2624 len2 = strlen(cladatum->comkey); 2625 else 2626 len2 = 0; 2627 2628 ncons = 0; 2629 for (c = cladatum->constraints; c; c = c->next) 2630 ncons++; 2631 2632 buf[0] = cpu_to_le32(len); 2633 buf[1] = cpu_to_le32(len2); 2634 buf[2] = cpu_to_le32(cladatum->value); 2635 buf[3] = cpu_to_le32(cladatum->permissions.nprim); 2636 if (cladatum->permissions.table) 2637 buf[4] = cpu_to_le32(cladatum->permissions.table->nel); 2638 else 2639 buf[4] = 0; 2640 buf[5] = cpu_to_le32(ncons); 2641 rc = put_entry(buf, sizeof(u32), 6, fp); 2642 if (rc) 2643 return rc; 2644 2645 rc = put_entry(key, 1, len, fp); 2646 if (rc) 2647 return rc; 2648 2649 if (cladatum->comkey) { 2650 rc = put_entry(cladatum->comkey, 1, len2, fp); 2651 if (rc) 2652 return rc; 2653 } 2654 2655 rc = hashtab_map(cladatum->permissions.table, perm_write, fp); 2656 if (rc) 2657 return rc; 2658 2659 rc = write_cons_helper(p, cladatum->constraints, fp); 2660 if (rc) 2661 return rc; 2662 2663 /* write out the validatetrans rule */ 2664 ncons = 0; 2665 for (c = cladatum->validatetrans; c; c = c->next) 2666 ncons++; 2667 2668 buf[0] = cpu_to_le32(ncons); 2669 rc = put_entry(buf, sizeof(u32), 1, fp); 2670 if (rc) 2671 return rc; 2672 2673 rc = write_cons_helper(p, cladatum->validatetrans, fp); 2674 if (rc) 2675 return rc; 2676 2677 return 0; 2678 } 2679 2680 static int role_write(void *vkey, void *datum, void *ptr) 2681 { 2682 char *key = vkey; 2683 struct role_datum *role = datum; 2684 struct policy_data *pd = ptr; 2685 void *fp = pd->fp; 2686 struct policydb *p = pd->p; 2687 __le32 buf[3]; 2688 size_t items, len; 2689 int rc; 2690 2691 len = strlen(key); 2692 items = 0; 2693 buf[items++] = cpu_to_le32(len); 2694 buf[items++] = cpu_to_le32(role->value); 2695 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 2696 buf[items++] = cpu_to_le32(role->bounds); 2697 2698 BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); 2699 2700 rc = put_entry(buf, sizeof(u32), items, fp); 2701 if (rc) 2702 return rc; 2703 2704 rc = put_entry(key, 1, len, fp); 2705 if (rc) 2706 return rc; 2707 2708 rc = ebitmap_write(&role->dominates, fp); 2709 if (rc) 2710 return rc; 2711 2712 rc = ebitmap_write(&role->types, fp); 2713 if (rc) 2714 return rc; 2715 2716 return 0; 2717 } 2718 2719 static int type_write(void *vkey, void *datum, void *ptr) 2720 { 2721 char *key = vkey; 2722 struct type_datum *typdatum = datum; 2723 struct policy_data *pd = ptr; 2724 struct policydb *p = pd->p; 2725 void *fp = pd->fp; 2726 __le32 buf[4]; 2727 int rc; 2728 size_t items, len; 2729 2730 len = strlen(key); 2731 items = 0; 2732 buf[items++] = cpu_to_le32(len); 2733 buf[items++] = cpu_to_le32(typdatum->value); 2734 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { 2735 u32 properties = 0; 2736 2737 if (typdatum->primary) 2738 properties |= TYPEDATUM_PROPERTY_PRIMARY; 2739 2740 if (typdatum->attribute) 2741 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE; 2742 2743 buf[items++] = cpu_to_le32(properties); 2744 buf[items++] = cpu_to_le32(typdatum->bounds); 2745 } else { 2746 buf[items++] = cpu_to_le32(typdatum->primary); 2747 } 2748 BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); 2749 rc = put_entry(buf, sizeof(u32), items, fp); 2750 if (rc) 2751 return rc; 2752 2753 rc = put_entry(key, 1, len, fp); 2754 if (rc) 2755 return rc; 2756 2757 return 0; 2758 } 2759 2760 static int user_write(void *vkey, void *datum, void *ptr) 2761 { 2762 char *key = vkey; 2763 struct user_datum *usrdatum = datum; 2764 struct policy_data *pd = ptr; 2765 struct policydb *p = pd->p; 2766 void *fp = pd->fp; 2767 __le32 buf[3]; 2768 size_t items, len; 2769 int rc; 2770 2771 len = strlen(key); 2772 items = 0; 2773 buf[items++] = cpu_to_le32(len); 2774 buf[items++] = cpu_to_le32(usrdatum->value); 2775 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) 2776 buf[items++] = cpu_to_le32(usrdatum->bounds); 2777 BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); 2778 rc = put_entry(buf, sizeof(u32), items, fp); 2779 if (rc) 2780 return rc; 2781 2782 rc = put_entry(key, 1, len, fp); 2783 if (rc) 2784 return rc; 2785 2786 rc = ebitmap_write(&usrdatum->roles, fp); 2787 if (rc) 2788 return rc; 2789 2790 rc = mls_write_range_helper(&usrdatum->range, fp); 2791 if (rc) 2792 return rc; 2793 2794 rc = mls_write_level(&usrdatum->dfltlevel, fp); 2795 if (rc) 2796 return rc; 2797 2798 return 0; 2799 } 2800 2801 static int (*write_f[SYM_NUM]) (void *key, void *datum, 2802 void *datap) = 2803 { 2804 common_write, 2805 class_write, 2806 role_write, 2807 type_write, 2808 user_write, 2809 cond_write_bool, 2810 sens_write, 2811 cat_write, 2812 }; 2813 2814 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info, 2815 void *fp) 2816 { 2817 unsigned int i, j, rc; 2818 size_t nel, len; 2819 __le32 buf[3]; 2820 u32 nodebuf[8]; 2821 struct ocontext *c; 2822 for (i = 0; i < info->ocon_num; i++) { 2823 nel = 0; 2824 for (c = p->ocontexts[i]; c; c = c->next) 2825 nel++; 2826 buf[0] = cpu_to_le32(nel); 2827 rc = put_entry(buf, sizeof(u32), 1, fp); 2828 if (rc) 2829 return rc; 2830 for (c = p->ocontexts[i]; c; c = c->next) { 2831 switch (i) { 2832 case OCON_ISID: 2833 buf[0] = cpu_to_le32(c->sid[0]); 2834 rc = put_entry(buf, sizeof(u32), 1, fp); 2835 if (rc) 2836 return rc; 2837 rc = context_write(p, &c->context[0], fp); 2838 if (rc) 2839 return rc; 2840 break; 2841 case OCON_FS: 2842 case OCON_NETIF: 2843 len = strlen(c->u.name); 2844 buf[0] = cpu_to_le32(len); 2845 rc = put_entry(buf, sizeof(u32), 1, fp); 2846 if (rc) 2847 return rc; 2848 rc = put_entry(c->u.name, 1, len, fp); 2849 if (rc) 2850 return rc; 2851 rc = context_write(p, &c->context[0], fp); 2852 if (rc) 2853 return rc; 2854 rc = context_write(p, &c->context[1], fp); 2855 if (rc) 2856 return rc; 2857 break; 2858 case OCON_PORT: 2859 buf[0] = cpu_to_le32(c->u.port.protocol); 2860 buf[1] = cpu_to_le32(c->u.port.low_port); 2861 buf[2] = cpu_to_le32(c->u.port.high_port); 2862 rc = put_entry(buf, sizeof(u32), 3, fp); 2863 if (rc) 2864 return rc; 2865 rc = context_write(p, &c->context[0], fp); 2866 if (rc) 2867 return rc; 2868 break; 2869 case OCON_NODE: 2870 nodebuf[0] = c->u.node.addr; /* network order */ 2871 nodebuf[1] = c->u.node.mask; /* network order */ 2872 rc = put_entry(nodebuf, sizeof(u32), 2, fp); 2873 if (rc) 2874 return rc; 2875 rc = context_write(p, &c->context[0], fp); 2876 if (rc) 2877 return rc; 2878 break; 2879 case OCON_FSUSE: 2880 buf[0] = cpu_to_le32(c->v.behavior); 2881 len = strlen(c->u.name); 2882 buf[1] = cpu_to_le32(len); 2883 rc = put_entry(buf, sizeof(u32), 2, fp); 2884 if (rc) 2885 return rc; 2886 rc = put_entry(c->u.name, 1, len, fp); 2887 if (rc) 2888 return rc; 2889 rc = context_write(p, &c->context[0], fp); 2890 if (rc) 2891 return rc; 2892 break; 2893 case OCON_NODE6: 2894 for (j = 0; j < 4; j++) 2895 nodebuf[j] = c->u.node6.addr[j]; /* network order */ 2896 for (j = 0; j < 4; j++) 2897 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */ 2898 rc = put_entry(nodebuf, sizeof(u32), 8, fp); 2899 if (rc) 2900 return rc; 2901 rc = context_write(p, &c->context[0], fp); 2902 if (rc) 2903 return rc; 2904 break; 2905 } 2906 } 2907 } 2908 return 0; 2909 } 2910 2911 static int genfs_write(struct policydb *p, void *fp) 2912 { 2913 struct genfs *genfs; 2914 struct ocontext *c; 2915 size_t len; 2916 __le32 buf[1]; 2917 int rc; 2918 2919 len = 0; 2920 for (genfs = p->genfs; genfs; genfs = genfs->next) 2921 len++; 2922 buf[0] = cpu_to_le32(len); 2923 rc = put_entry(buf, sizeof(u32), 1, fp); 2924 if (rc) 2925 return rc; 2926 for (genfs = p->genfs; genfs; genfs = genfs->next) { 2927 len = strlen(genfs->fstype); 2928 buf[0] = cpu_to_le32(len); 2929 rc = put_entry(buf, sizeof(u32), 1, fp); 2930 if (rc) 2931 return rc; 2932 rc = put_entry(genfs->fstype, 1, len, fp); 2933 if (rc) 2934 return rc; 2935 len = 0; 2936 for (c = genfs->head; c; c = c->next) 2937 len++; 2938 buf[0] = cpu_to_le32(len); 2939 rc = put_entry(buf, sizeof(u32), 1, fp); 2940 if (rc) 2941 return rc; 2942 for (c = genfs->head; c; c = c->next) { 2943 len = strlen(c->u.name); 2944 buf[0] = cpu_to_le32(len); 2945 rc = put_entry(buf, sizeof(u32), 1, fp); 2946 if (rc) 2947 return rc; 2948 rc = put_entry(c->u.name, 1, len, fp); 2949 if (rc) 2950 return rc; 2951 buf[0] = cpu_to_le32(c->v.sclass); 2952 rc = put_entry(buf, sizeof(u32), 1, fp); 2953 if (rc) 2954 return rc; 2955 rc = context_write(p, &c->context[0], fp); 2956 if (rc) 2957 return rc; 2958 } 2959 } 2960 return 0; 2961 } 2962 2963 static int range_count(void *key, void *data, void *ptr) 2964 { 2965 int *cnt = ptr; 2966 *cnt = *cnt + 1; 2967 2968 return 0; 2969 } 2970 2971 static int range_write_helper(void *key, void *data, void *ptr) 2972 { 2973 __le32 buf[2]; 2974 struct range_trans *rt = key; 2975 struct mls_range *r = data; 2976 struct policy_data *pd = ptr; 2977 void *fp = pd->fp; 2978 struct policydb *p = pd->p; 2979 int rc; 2980 2981 buf[0] = cpu_to_le32(rt->source_type); 2982 buf[1] = cpu_to_le32(rt->target_type); 2983 rc = put_entry(buf, sizeof(u32), 2, fp); 2984 if (rc) 2985 return rc; 2986 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { 2987 buf[0] = cpu_to_le32(rt->target_class); 2988 rc = put_entry(buf, sizeof(u32), 1, fp); 2989 if (rc) 2990 return rc; 2991 } 2992 rc = mls_write_range_helper(r, fp); 2993 if (rc) 2994 return rc; 2995 2996 return 0; 2997 } 2998 2999 static int range_write(struct policydb *p, void *fp) 3000 { 3001 size_t nel; 3002 __le32 buf[1]; 3003 int rc; 3004 struct policy_data pd; 3005 3006 pd.p = p; 3007 pd.fp = fp; 3008 3009 /* count the number of entries in the hashtab */ 3010 nel = 0; 3011 rc = hashtab_map(p->range_tr, range_count, &nel); 3012 if (rc) 3013 return rc; 3014 3015 buf[0] = cpu_to_le32(nel); 3016 rc = put_entry(buf, sizeof(u32), 1, fp); 3017 if (rc) 3018 return rc; 3019 3020 /* actually write all of the entries */ 3021 rc = hashtab_map(p->range_tr, range_write_helper, &pd); 3022 if (rc) 3023 return rc; 3024 3025 return 0; 3026 } 3027 3028 /* 3029 * Write the configuration data in a policy database 3030 * structure to a policy database binary representation 3031 * file. 3032 */ 3033 int policydb_write(struct policydb *p, void *fp) 3034 { 3035 unsigned int i, num_syms; 3036 int rc; 3037 __le32 buf[4]; 3038 u32 config; 3039 size_t len; 3040 struct policydb_compat_info *info; 3041 3042 /* 3043 * refuse to write policy older than compressed avtab 3044 * to simplify the writer. There are other tests dropped 3045 * since we assume this throughout the writer code. Be 3046 * careful if you ever try to remove this restriction 3047 */ 3048 if (p->policyvers < POLICYDB_VERSION_AVTAB) { 3049 printk(KERN_ERR "SELinux: refusing to write policy version %d." 3050 " Because it is less than version %d\n", p->policyvers, 3051 POLICYDB_VERSION_AVTAB); 3052 return -EINVAL; 3053 } 3054 3055 config = 0; 3056 if (p->mls_enabled) 3057 config |= POLICYDB_CONFIG_MLS; 3058 3059 if (p->reject_unknown) 3060 config |= REJECT_UNKNOWN; 3061 if (p->allow_unknown) 3062 config |= ALLOW_UNKNOWN; 3063 3064 /* Write the magic number and string identifiers. */ 3065 buf[0] = cpu_to_le32(POLICYDB_MAGIC); 3066 len = strlen(POLICYDB_STRING); 3067 buf[1] = cpu_to_le32(len); 3068 rc = put_entry(buf, sizeof(u32), 2, fp); 3069 if (rc) 3070 return rc; 3071 rc = put_entry(POLICYDB_STRING, 1, len, fp); 3072 if (rc) 3073 return rc; 3074 3075 /* Write the version, config, and table sizes. */ 3076 info = policydb_lookup_compat(p->policyvers); 3077 if (!info) { 3078 printk(KERN_ERR "SELinux: compatibility lookup failed for policy " 3079 "version %d", p->policyvers); 3080 return -EINVAL; 3081 } 3082 3083 buf[0] = cpu_to_le32(p->policyvers); 3084 buf[1] = cpu_to_le32(config); 3085 buf[2] = cpu_to_le32(info->sym_num); 3086 buf[3] = cpu_to_le32(info->ocon_num); 3087 3088 rc = put_entry(buf, sizeof(u32), 4, fp); 3089 if (rc) 3090 return rc; 3091 3092 if (p->policyvers >= POLICYDB_VERSION_POLCAP) { 3093 rc = ebitmap_write(&p->policycaps, fp); 3094 if (rc) 3095 return rc; 3096 } 3097 3098 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { 3099 rc = ebitmap_write(&p->permissive_map, fp); 3100 if (rc) 3101 return rc; 3102 } 3103 3104 num_syms = info->sym_num; 3105 for (i = 0; i < num_syms; i++) { 3106 struct policy_data pd; 3107 3108 pd.fp = fp; 3109 pd.p = p; 3110 3111 buf[0] = cpu_to_le32(p->symtab[i].nprim); 3112 buf[1] = cpu_to_le32(p->symtab[i].table->nel); 3113 3114 rc = put_entry(buf, sizeof(u32), 2, fp); 3115 if (rc) 3116 return rc; 3117 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd); 3118 if (rc) 3119 return rc; 3120 } 3121 3122 rc = avtab_write(p, &p->te_avtab, fp); 3123 if (rc) 3124 return rc; 3125 3126 rc = cond_write_list(p, p->cond_list, fp); 3127 if (rc) 3128 return rc; 3129 3130 rc = role_trans_write(p->role_tr, fp); 3131 if (rc) 3132 return rc; 3133 3134 rc = role_allow_write(p->role_allow, fp); 3135 if (rc) 3136 return rc; 3137 3138 rc = ocontext_write(p, info, fp); 3139 if (rc) 3140 return rc; 3141 3142 rc = genfs_write(p, fp); 3143 if (rc) 3144 return rc; 3145 3146 rc = range_write(p, fp); 3147 if (rc) 3148 return rc; 3149 3150 for (i = 0; i < p->p_types.nprim; i++) { 3151 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); 3152 3153 BUG_ON(!e); 3154 rc = ebitmap_write(e, fp); 3155 if (rc) 3156 return rc; 3157 } 3158 3159 return 0; 3160 } 3161