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