1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Implementation of the security services. 4 * 5 * Authors : Stephen Smalley, <sds@tycho.nsa.gov> 6 * James Morris <jmorris@redhat.com> 7 * 8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> 9 * 10 * Support for enhanced MLS infrastructure. 11 * Support for context based audit filters. 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 NetLabel 20 * Added support for the policy capability bitmap 21 * 22 * Updated: Chad Sellers <csellers@tresys.com> 23 * 24 * Added validation of kernel classes and permissions 25 * 26 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com> 27 * 28 * Added support for bounds domain and audit messaged on masked permissions 29 * 30 * Updated: Guido Trentalancia <guido@trentalancia.com> 31 * 32 * Added support for runtime switching of the policy type 33 * 34 * Copyright (C) 2008, 2009 NEC Corporation 35 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. 36 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. 37 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC 38 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> 39 */ 40 #include <linux/kernel.h> 41 #include <linux/slab.h> 42 #include <linux/string.h> 43 #include <linux/spinlock.h> 44 #include <linux/rcupdate.h> 45 #include <linux/errno.h> 46 #include <linux/in.h> 47 #include <linux/sched.h> 48 #include <linux/audit.h> 49 #include <linux/vmalloc.h> 50 #include <net/netlabel.h> 51 52 #include "flask.h" 53 #include "avc.h" 54 #include "avc_ss.h" 55 #include "security.h" 56 #include "context.h" 57 #include "policydb.h" 58 #include "sidtab.h" 59 #include "services.h" 60 #include "conditional.h" 61 #include "mls.h" 62 #include "objsec.h" 63 #include "netlabel.h" 64 #include "xfrm.h" 65 #include "ebitmap.h" 66 #include "audit.h" 67 #include "policycap_names.h" 68 69 static struct selinux_ss selinux_ss; 70 71 void selinux_ss_init(struct selinux_ss **ss) 72 { 73 rwlock_init(&selinux_ss.policy_rwlock); 74 *ss = &selinux_ss; 75 } 76 77 /* Forward declaration. */ 78 static int context_struct_to_string(struct policydb *policydb, 79 struct context *context, 80 char **scontext, 81 u32 *scontext_len); 82 83 static int sidtab_entry_to_string(struct policydb *policydb, 84 struct sidtab *sidtab, 85 struct sidtab_entry *entry, 86 char **scontext, 87 u32 *scontext_len); 88 89 static void context_struct_compute_av(struct policydb *policydb, 90 struct context *scontext, 91 struct context *tcontext, 92 u16 tclass, 93 struct av_decision *avd, 94 struct extended_perms *xperms); 95 96 static int selinux_set_mapping(struct policydb *pol, 97 struct security_class_mapping *map, 98 struct selinux_map *out_map) 99 { 100 u16 i, j; 101 unsigned k; 102 bool print_unknown_handle = false; 103 104 /* Find number of classes in the input mapping */ 105 if (!map) 106 return -EINVAL; 107 i = 0; 108 while (map[i].name) 109 i++; 110 111 /* Allocate space for the class records, plus one for class zero */ 112 out_map->mapping = kcalloc(++i, sizeof(*out_map->mapping), GFP_ATOMIC); 113 if (!out_map->mapping) 114 return -ENOMEM; 115 116 /* Store the raw class and permission values */ 117 j = 0; 118 while (map[j].name) { 119 struct security_class_mapping *p_in = map + (j++); 120 struct selinux_mapping *p_out = out_map->mapping + j; 121 122 /* An empty class string skips ahead */ 123 if (!strcmp(p_in->name, "")) { 124 p_out->num_perms = 0; 125 continue; 126 } 127 128 p_out->value = string_to_security_class(pol, p_in->name); 129 if (!p_out->value) { 130 pr_info("SELinux: Class %s not defined in policy.\n", 131 p_in->name); 132 if (pol->reject_unknown) 133 goto err; 134 p_out->num_perms = 0; 135 print_unknown_handle = true; 136 continue; 137 } 138 139 k = 0; 140 while (p_in->perms[k]) { 141 /* An empty permission string skips ahead */ 142 if (!*p_in->perms[k]) { 143 k++; 144 continue; 145 } 146 p_out->perms[k] = string_to_av_perm(pol, p_out->value, 147 p_in->perms[k]); 148 if (!p_out->perms[k]) { 149 pr_info("SELinux: Permission %s in class %s not defined in policy.\n", 150 p_in->perms[k], p_in->name); 151 if (pol->reject_unknown) 152 goto err; 153 print_unknown_handle = true; 154 } 155 156 k++; 157 } 158 p_out->num_perms = k; 159 } 160 161 if (print_unknown_handle) 162 pr_info("SELinux: the above unknown classes and permissions will be %s\n", 163 pol->allow_unknown ? "allowed" : "denied"); 164 165 out_map->size = i; 166 return 0; 167 err: 168 kfree(out_map->mapping); 169 out_map->mapping = NULL; 170 return -EINVAL; 171 } 172 173 /* 174 * Get real, policy values from mapped values 175 */ 176 177 static u16 unmap_class(struct selinux_map *map, u16 tclass) 178 { 179 if (tclass < map->size) 180 return map->mapping[tclass].value; 181 182 return tclass; 183 } 184 185 /* 186 * Get kernel value for class from its policy value 187 */ 188 static u16 map_class(struct selinux_map *map, u16 pol_value) 189 { 190 u16 i; 191 192 for (i = 1; i < map->size; i++) { 193 if (map->mapping[i].value == pol_value) 194 return i; 195 } 196 197 return SECCLASS_NULL; 198 } 199 200 static void map_decision(struct selinux_map *map, 201 u16 tclass, struct av_decision *avd, 202 int allow_unknown) 203 { 204 if (tclass < map->size) { 205 struct selinux_mapping *mapping = &map->mapping[tclass]; 206 unsigned int i, n = mapping->num_perms; 207 u32 result; 208 209 for (i = 0, result = 0; i < n; i++) { 210 if (avd->allowed & mapping->perms[i]) 211 result |= 1<<i; 212 if (allow_unknown && !mapping->perms[i]) 213 result |= 1<<i; 214 } 215 avd->allowed = result; 216 217 for (i = 0, result = 0; i < n; i++) 218 if (avd->auditallow & mapping->perms[i]) 219 result |= 1<<i; 220 avd->auditallow = result; 221 222 for (i = 0, result = 0; i < n; i++) { 223 if (avd->auditdeny & mapping->perms[i]) 224 result |= 1<<i; 225 if (!allow_unknown && !mapping->perms[i]) 226 result |= 1<<i; 227 } 228 /* 229 * In case the kernel has a bug and requests a permission 230 * between num_perms and the maximum permission number, we 231 * should audit that denial 232 */ 233 for (; i < (sizeof(u32)*8); i++) 234 result |= 1<<i; 235 avd->auditdeny = result; 236 } 237 } 238 239 int security_mls_enabled(struct selinux_state *state) 240 { 241 struct policydb *p = &state->ss->policydb; 242 243 return p->mls_enabled; 244 } 245 246 /* 247 * Return the boolean value of a constraint expression 248 * when it is applied to the specified source and target 249 * security contexts. 250 * 251 * xcontext is a special beast... It is used by the validatetrans rules 252 * only. For these rules, scontext is the context before the transition, 253 * tcontext is the context after the transition, and xcontext is the context 254 * of the process performing the transition. All other callers of 255 * constraint_expr_eval should pass in NULL for xcontext. 256 */ 257 static int constraint_expr_eval(struct policydb *policydb, 258 struct context *scontext, 259 struct context *tcontext, 260 struct context *xcontext, 261 struct constraint_expr *cexpr) 262 { 263 u32 val1, val2; 264 struct context *c; 265 struct role_datum *r1, *r2; 266 struct mls_level *l1, *l2; 267 struct constraint_expr *e; 268 int s[CEXPR_MAXDEPTH]; 269 int sp = -1; 270 271 for (e = cexpr; e; e = e->next) { 272 switch (e->expr_type) { 273 case CEXPR_NOT: 274 BUG_ON(sp < 0); 275 s[sp] = !s[sp]; 276 break; 277 case CEXPR_AND: 278 BUG_ON(sp < 1); 279 sp--; 280 s[sp] &= s[sp + 1]; 281 break; 282 case CEXPR_OR: 283 BUG_ON(sp < 1); 284 sp--; 285 s[sp] |= s[sp + 1]; 286 break; 287 case CEXPR_ATTR: 288 if (sp == (CEXPR_MAXDEPTH - 1)) 289 return 0; 290 switch (e->attr) { 291 case CEXPR_USER: 292 val1 = scontext->user; 293 val2 = tcontext->user; 294 break; 295 case CEXPR_TYPE: 296 val1 = scontext->type; 297 val2 = tcontext->type; 298 break; 299 case CEXPR_ROLE: 300 val1 = scontext->role; 301 val2 = tcontext->role; 302 r1 = policydb->role_val_to_struct[val1 - 1]; 303 r2 = policydb->role_val_to_struct[val2 - 1]; 304 switch (e->op) { 305 case CEXPR_DOM: 306 s[++sp] = ebitmap_get_bit(&r1->dominates, 307 val2 - 1); 308 continue; 309 case CEXPR_DOMBY: 310 s[++sp] = ebitmap_get_bit(&r2->dominates, 311 val1 - 1); 312 continue; 313 case CEXPR_INCOMP: 314 s[++sp] = (!ebitmap_get_bit(&r1->dominates, 315 val2 - 1) && 316 !ebitmap_get_bit(&r2->dominates, 317 val1 - 1)); 318 continue; 319 default: 320 break; 321 } 322 break; 323 case CEXPR_L1L2: 324 l1 = &(scontext->range.level[0]); 325 l2 = &(tcontext->range.level[0]); 326 goto mls_ops; 327 case CEXPR_L1H2: 328 l1 = &(scontext->range.level[0]); 329 l2 = &(tcontext->range.level[1]); 330 goto mls_ops; 331 case CEXPR_H1L2: 332 l1 = &(scontext->range.level[1]); 333 l2 = &(tcontext->range.level[0]); 334 goto mls_ops; 335 case CEXPR_H1H2: 336 l1 = &(scontext->range.level[1]); 337 l2 = &(tcontext->range.level[1]); 338 goto mls_ops; 339 case CEXPR_L1H1: 340 l1 = &(scontext->range.level[0]); 341 l2 = &(scontext->range.level[1]); 342 goto mls_ops; 343 case CEXPR_L2H2: 344 l1 = &(tcontext->range.level[0]); 345 l2 = &(tcontext->range.level[1]); 346 goto mls_ops; 347 mls_ops: 348 switch (e->op) { 349 case CEXPR_EQ: 350 s[++sp] = mls_level_eq(l1, l2); 351 continue; 352 case CEXPR_NEQ: 353 s[++sp] = !mls_level_eq(l1, l2); 354 continue; 355 case CEXPR_DOM: 356 s[++sp] = mls_level_dom(l1, l2); 357 continue; 358 case CEXPR_DOMBY: 359 s[++sp] = mls_level_dom(l2, l1); 360 continue; 361 case CEXPR_INCOMP: 362 s[++sp] = mls_level_incomp(l2, l1); 363 continue; 364 default: 365 BUG(); 366 return 0; 367 } 368 break; 369 default: 370 BUG(); 371 return 0; 372 } 373 374 switch (e->op) { 375 case CEXPR_EQ: 376 s[++sp] = (val1 == val2); 377 break; 378 case CEXPR_NEQ: 379 s[++sp] = (val1 != val2); 380 break; 381 default: 382 BUG(); 383 return 0; 384 } 385 break; 386 case CEXPR_NAMES: 387 if (sp == (CEXPR_MAXDEPTH-1)) 388 return 0; 389 c = scontext; 390 if (e->attr & CEXPR_TARGET) 391 c = tcontext; 392 else if (e->attr & CEXPR_XTARGET) { 393 c = xcontext; 394 if (!c) { 395 BUG(); 396 return 0; 397 } 398 } 399 if (e->attr & CEXPR_USER) 400 val1 = c->user; 401 else if (e->attr & CEXPR_ROLE) 402 val1 = c->role; 403 else if (e->attr & CEXPR_TYPE) 404 val1 = c->type; 405 else { 406 BUG(); 407 return 0; 408 } 409 410 switch (e->op) { 411 case CEXPR_EQ: 412 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1); 413 break; 414 case CEXPR_NEQ: 415 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1); 416 break; 417 default: 418 BUG(); 419 return 0; 420 } 421 break; 422 default: 423 BUG(); 424 return 0; 425 } 426 } 427 428 BUG_ON(sp != 0); 429 return s[0]; 430 } 431 432 /* 433 * security_dump_masked_av - dumps masked permissions during 434 * security_compute_av due to RBAC, MLS/Constraint and Type bounds. 435 */ 436 static int dump_masked_av_helper(void *k, void *d, void *args) 437 { 438 struct perm_datum *pdatum = d; 439 char **permission_names = args; 440 441 BUG_ON(pdatum->value < 1 || pdatum->value > 32); 442 443 permission_names[pdatum->value - 1] = (char *)k; 444 445 return 0; 446 } 447 448 static void security_dump_masked_av(struct policydb *policydb, 449 struct context *scontext, 450 struct context *tcontext, 451 u16 tclass, 452 u32 permissions, 453 const char *reason) 454 { 455 struct common_datum *common_dat; 456 struct class_datum *tclass_dat; 457 struct audit_buffer *ab; 458 char *tclass_name; 459 char *scontext_name = NULL; 460 char *tcontext_name = NULL; 461 char *permission_names[32]; 462 int index; 463 u32 length; 464 bool need_comma = false; 465 466 if (!permissions) 467 return; 468 469 tclass_name = sym_name(policydb, SYM_CLASSES, tclass - 1); 470 tclass_dat = policydb->class_val_to_struct[tclass - 1]; 471 common_dat = tclass_dat->comdatum; 472 473 /* init permission_names */ 474 if (common_dat && 475 hashtab_map(&common_dat->permissions.table, 476 dump_masked_av_helper, permission_names) < 0) 477 goto out; 478 479 if (hashtab_map(&tclass_dat->permissions.table, 480 dump_masked_av_helper, permission_names) < 0) 481 goto out; 482 483 /* get scontext/tcontext in text form */ 484 if (context_struct_to_string(policydb, scontext, 485 &scontext_name, &length) < 0) 486 goto out; 487 488 if (context_struct_to_string(policydb, tcontext, 489 &tcontext_name, &length) < 0) 490 goto out; 491 492 /* audit a message */ 493 ab = audit_log_start(audit_context(), 494 GFP_ATOMIC, AUDIT_SELINUX_ERR); 495 if (!ab) 496 goto out; 497 498 audit_log_format(ab, "op=security_compute_av reason=%s " 499 "scontext=%s tcontext=%s tclass=%s perms=", 500 reason, scontext_name, tcontext_name, tclass_name); 501 502 for (index = 0; index < 32; index++) { 503 u32 mask = (1 << index); 504 505 if ((mask & permissions) == 0) 506 continue; 507 508 audit_log_format(ab, "%s%s", 509 need_comma ? "," : "", 510 permission_names[index] 511 ? permission_names[index] : "????"); 512 need_comma = true; 513 } 514 audit_log_end(ab); 515 out: 516 /* release scontext/tcontext */ 517 kfree(tcontext_name); 518 kfree(scontext_name); 519 520 return; 521 } 522 523 /* 524 * security_boundary_permission - drops violated permissions 525 * on boundary constraint. 526 */ 527 static void type_attribute_bounds_av(struct policydb *policydb, 528 struct context *scontext, 529 struct context *tcontext, 530 u16 tclass, 531 struct av_decision *avd) 532 { 533 struct context lo_scontext; 534 struct context lo_tcontext, *tcontextp = tcontext; 535 struct av_decision lo_avd; 536 struct type_datum *source; 537 struct type_datum *target; 538 u32 masked = 0; 539 540 source = policydb->type_val_to_struct[scontext->type - 1]; 541 BUG_ON(!source); 542 543 if (!source->bounds) 544 return; 545 546 target = policydb->type_val_to_struct[tcontext->type - 1]; 547 BUG_ON(!target); 548 549 memset(&lo_avd, 0, sizeof(lo_avd)); 550 551 memcpy(&lo_scontext, scontext, sizeof(lo_scontext)); 552 lo_scontext.type = source->bounds; 553 554 if (target->bounds) { 555 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext)); 556 lo_tcontext.type = target->bounds; 557 tcontextp = &lo_tcontext; 558 } 559 560 context_struct_compute_av(policydb, &lo_scontext, 561 tcontextp, 562 tclass, 563 &lo_avd, 564 NULL); 565 566 masked = ~lo_avd.allowed & avd->allowed; 567 568 if (likely(!masked)) 569 return; /* no masked permission */ 570 571 /* mask violated permissions */ 572 avd->allowed &= ~masked; 573 574 /* audit masked permissions */ 575 security_dump_masked_av(policydb, scontext, tcontext, 576 tclass, masked, "bounds"); 577 } 578 579 /* 580 * flag which drivers have permissions 581 * only looking for ioctl based extended permssions 582 */ 583 void services_compute_xperms_drivers( 584 struct extended_perms *xperms, 585 struct avtab_node *node) 586 { 587 unsigned int i; 588 589 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) { 590 /* if one or more driver has all permissions allowed */ 591 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++) 592 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i]; 593 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) { 594 /* if allowing permissions within a driver */ 595 security_xperm_set(xperms->drivers.p, 596 node->datum.u.xperms->driver); 597 } 598 599 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */ 600 if (node->key.specified & AVTAB_XPERMS_ALLOWED) 601 xperms->len = 1; 602 } 603 604 /* 605 * Compute access vectors and extended permissions based on a context 606 * structure pair for the permissions in a particular class. 607 */ 608 static void context_struct_compute_av(struct policydb *policydb, 609 struct context *scontext, 610 struct context *tcontext, 611 u16 tclass, 612 struct av_decision *avd, 613 struct extended_perms *xperms) 614 { 615 struct constraint_node *constraint; 616 struct role_allow *ra; 617 struct avtab_key avkey; 618 struct avtab_node *node; 619 struct class_datum *tclass_datum; 620 struct ebitmap *sattr, *tattr; 621 struct ebitmap_node *snode, *tnode; 622 unsigned int i, j; 623 624 avd->allowed = 0; 625 avd->auditallow = 0; 626 avd->auditdeny = 0xffffffff; 627 if (xperms) { 628 memset(&xperms->drivers, 0, sizeof(xperms->drivers)); 629 xperms->len = 0; 630 } 631 632 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) { 633 if (printk_ratelimit()) 634 pr_warn("SELinux: Invalid class %hu\n", tclass); 635 return; 636 } 637 638 tclass_datum = policydb->class_val_to_struct[tclass - 1]; 639 640 /* 641 * If a specific type enforcement rule was defined for 642 * this permission check, then use it. 643 */ 644 avkey.target_class = tclass; 645 avkey.specified = AVTAB_AV | AVTAB_XPERMS; 646 sattr = &policydb->type_attr_map_array[scontext->type - 1]; 647 tattr = &policydb->type_attr_map_array[tcontext->type - 1]; 648 ebitmap_for_each_positive_bit(sattr, snode, i) { 649 ebitmap_for_each_positive_bit(tattr, tnode, j) { 650 avkey.source_type = i + 1; 651 avkey.target_type = j + 1; 652 for (node = avtab_search_node(&policydb->te_avtab, 653 &avkey); 654 node; 655 node = avtab_search_node_next(node, avkey.specified)) { 656 if (node->key.specified == AVTAB_ALLOWED) 657 avd->allowed |= node->datum.u.data; 658 else if (node->key.specified == AVTAB_AUDITALLOW) 659 avd->auditallow |= node->datum.u.data; 660 else if (node->key.specified == AVTAB_AUDITDENY) 661 avd->auditdeny &= node->datum.u.data; 662 else if (xperms && (node->key.specified & AVTAB_XPERMS)) 663 services_compute_xperms_drivers(xperms, node); 664 } 665 666 /* Check conditional av table for additional permissions */ 667 cond_compute_av(&policydb->te_cond_avtab, &avkey, 668 avd, xperms); 669 670 } 671 } 672 673 /* 674 * Remove any permissions prohibited by a constraint (this includes 675 * the MLS policy). 676 */ 677 constraint = tclass_datum->constraints; 678 while (constraint) { 679 if ((constraint->permissions & (avd->allowed)) && 680 !constraint_expr_eval(policydb, scontext, tcontext, NULL, 681 constraint->expr)) { 682 avd->allowed &= ~(constraint->permissions); 683 } 684 constraint = constraint->next; 685 } 686 687 /* 688 * If checking process transition permission and the 689 * role is changing, then check the (current_role, new_role) 690 * pair. 691 */ 692 if (tclass == policydb->process_class && 693 (avd->allowed & policydb->process_trans_perms) && 694 scontext->role != tcontext->role) { 695 for (ra = policydb->role_allow; ra; ra = ra->next) { 696 if (scontext->role == ra->role && 697 tcontext->role == ra->new_role) 698 break; 699 } 700 if (!ra) 701 avd->allowed &= ~policydb->process_trans_perms; 702 } 703 704 /* 705 * If the given source and target types have boundary 706 * constraint, lazy checks have to mask any violated 707 * permission and notice it to userspace via audit. 708 */ 709 type_attribute_bounds_av(policydb, scontext, tcontext, 710 tclass, avd); 711 } 712 713 static int security_validtrans_handle_fail(struct selinux_state *state, 714 struct sidtab_entry *oentry, 715 struct sidtab_entry *nentry, 716 struct sidtab_entry *tentry, 717 u16 tclass) 718 { 719 struct policydb *p = &state->ss->policydb; 720 struct sidtab *sidtab = state->ss->sidtab; 721 char *o = NULL, *n = NULL, *t = NULL; 722 u32 olen, nlen, tlen; 723 724 if (sidtab_entry_to_string(p, sidtab, oentry, &o, &olen)) 725 goto out; 726 if (sidtab_entry_to_string(p, sidtab, nentry, &n, &nlen)) 727 goto out; 728 if (sidtab_entry_to_string(p, sidtab, tentry, &t, &tlen)) 729 goto out; 730 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR, 731 "op=security_validate_transition seresult=denied" 732 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s", 733 o, n, t, sym_name(p, SYM_CLASSES, tclass-1)); 734 out: 735 kfree(o); 736 kfree(n); 737 kfree(t); 738 739 if (!enforcing_enabled(state)) 740 return 0; 741 return -EPERM; 742 } 743 744 static int security_compute_validatetrans(struct selinux_state *state, 745 u32 oldsid, u32 newsid, u32 tasksid, 746 u16 orig_tclass, bool user) 747 { 748 struct policydb *policydb; 749 struct sidtab *sidtab; 750 struct sidtab_entry *oentry; 751 struct sidtab_entry *nentry; 752 struct sidtab_entry *tentry; 753 struct class_datum *tclass_datum; 754 struct constraint_node *constraint; 755 u16 tclass; 756 int rc = 0; 757 758 759 if (!selinux_initialized(state)) 760 return 0; 761 762 read_lock(&state->ss->policy_rwlock); 763 764 policydb = &state->ss->policydb; 765 sidtab = state->ss->sidtab; 766 767 if (!user) 768 tclass = unmap_class(&state->ss->map, orig_tclass); 769 else 770 tclass = orig_tclass; 771 772 if (!tclass || tclass > policydb->p_classes.nprim) { 773 rc = -EINVAL; 774 goto out; 775 } 776 tclass_datum = policydb->class_val_to_struct[tclass - 1]; 777 778 oentry = sidtab_search_entry(sidtab, oldsid); 779 if (!oentry) { 780 pr_err("SELinux: %s: unrecognized SID %d\n", 781 __func__, oldsid); 782 rc = -EINVAL; 783 goto out; 784 } 785 786 nentry = sidtab_search_entry(sidtab, newsid); 787 if (!nentry) { 788 pr_err("SELinux: %s: unrecognized SID %d\n", 789 __func__, newsid); 790 rc = -EINVAL; 791 goto out; 792 } 793 794 tentry = sidtab_search_entry(sidtab, tasksid); 795 if (!tentry) { 796 pr_err("SELinux: %s: unrecognized SID %d\n", 797 __func__, tasksid); 798 rc = -EINVAL; 799 goto out; 800 } 801 802 constraint = tclass_datum->validatetrans; 803 while (constraint) { 804 if (!constraint_expr_eval(policydb, &oentry->context, 805 &nentry->context, &tentry->context, 806 constraint->expr)) { 807 if (user) 808 rc = -EPERM; 809 else 810 rc = security_validtrans_handle_fail(state, 811 oentry, 812 nentry, 813 tentry, 814 tclass); 815 goto out; 816 } 817 constraint = constraint->next; 818 } 819 820 out: 821 read_unlock(&state->ss->policy_rwlock); 822 return rc; 823 } 824 825 int security_validate_transition_user(struct selinux_state *state, 826 u32 oldsid, u32 newsid, u32 tasksid, 827 u16 tclass) 828 { 829 return security_compute_validatetrans(state, oldsid, newsid, tasksid, 830 tclass, true); 831 } 832 833 int security_validate_transition(struct selinux_state *state, 834 u32 oldsid, u32 newsid, u32 tasksid, 835 u16 orig_tclass) 836 { 837 return security_compute_validatetrans(state, oldsid, newsid, tasksid, 838 orig_tclass, false); 839 } 840 841 /* 842 * security_bounded_transition - check whether the given 843 * transition is directed to bounded, or not. 844 * It returns 0, if @newsid is bounded by @oldsid. 845 * Otherwise, it returns error code. 846 * 847 * @oldsid : current security identifier 848 * @newsid : destinated security identifier 849 */ 850 int security_bounded_transition(struct selinux_state *state, 851 u32 old_sid, u32 new_sid) 852 { 853 struct policydb *policydb; 854 struct sidtab *sidtab; 855 struct sidtab_entry *old_entry, *new_entry; 856 struct type_datum *type; 857 int index; 858 int rc; 859 860 if (!selinux_initialized(state)) 861 return 0; 862 863 read_lock(&state->ss->policy_rwlock); 864 865 policydb = &state->ss->policydb; 866 sidtab = state->ss->sidtab; 867 868 rc = -EINVAL; 869 old_entry = sidtab_search_entry(sidtab, old_sid); 870 if (!old_entry) { 871 pr_err("SELinux: %s: unrecognized SID %u\n", 872 __func__, old_sid); 873 goto out; 874 } 875 876 rc = -EINVAL; 877 new_entry = sidtab_search_entry(sidtab, new_sid); 878 if (!new_entry) { 879 pr_err("SELinux: %s: unrecognized SID %u\n", 880 __func__, new_sid); 881 goto out; 882 } 883 884 rc = 0; 885 /* type/domain unchanged */ 886 if (old_entry->context.type == new_entry->context.type) 887 goto out; 888 889 index = new_entry->context.type; 890 while (true) { 891 type = policydb->type_val_to_struct[index - 1]; 892 BUG_ON(!type); 893 894 /* not bounded anymore */ 895 rc = -EPERM; 896 if (!type->bounds) 897 break; 898 899 /* @newsid is bounded by @oldsid */ 900 rc = 0; 901 if (type->bounds == old_entry->context.type) 902 break; 903 904 index = type->bounds; 905 } 906 907 if (rc) { 908 char *old_name = NULL; 909 char *new_name = NULL; 910 u32 length; 911 912 if (!sidtab_entry_to_string(policydb, sidtab, old_entry, 913 &old_name, &length) && 914 !sidtab_entry_to_string(policydb, sidtab, new_entry, 915 &new_name, &length)) { 916 audit_log(audit_context(), 917 GFP_ATOMIC, AUDIT_SELINUX_ERR, 918 "op=security_bounded_transition " 919 "seresult=denied " 920 "oldcontext=%s newcontext=%s", 921 old_name, new_name); 922 } 923 kfree(new_name); 924 kfree(old_name); 925 } 926 out: 927 read_unlock(&state->ss->policy_rwlock); 928 929 return rc; 930 } 931 932 static void avd_init(struct selinux_state *state, struct av_decision *avd) 933 { 934 avd->allowed = 0; 935 avd->auditallow = 0; 936 avd->auditdeny = 0xffffffff; 937 avd->seqno = state->ss->latest_granting; 938 avd->flags = 0; 939 } 940 941 void services_compute_xperms_decision(struct extended_perms_decision *xpermd, 942 struct avtab_node *node) 943 { 944 unsigned int i; 945 946 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) { 947 if (xpermd->driver != node->datum.u.xperms->driver) 948 return; 949 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) { 950 if (!security_xperm_test(node->datum.u.xperms->perms.p, 951 xpermd->driver)) 952 return; 953 } else { 954 BUG(); 955 } 956 957 if (node->key.specified == AVTAB_XPERMS_ALLOWED) { 958 xpermd->used |= XPERMS_ALLOWED; 959 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) { 960 memset(xpermd->allowed->p, 0xff, 961 sizeof(xpermd->allowed->p)); 962 } 963 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) { 964 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++) 965 xpermd->allowed->p[i] |= 966 node->datum.u.xperms->perms.p[i]; 967 } 968 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) { 969 xpermd->used |= XPERMS_AUDITALLOW; 970 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) { 971 memset(xpermd->auditallow->p, 0xff, 972 sizeof(xpermd->auditallow->p)); 973 } 974 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) { 975 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++) 976 xpermd->auditallow->p[i] |= 977 node->datum.u.xperms->perms.p[i]; 978 } 979 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) { 980 xpermd->used |= XPERMS_DONTAUDIT; 981 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) { 982 memset(xpermd->dontaudit->p, 0xff, 983 sizeof(xpermd->dontaudit->p)); 984 } 985 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) { 986 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++) 987 xpermd->dontaudit->p[i] |= 988 node->datum.u.xperms->perms.p[i]; 989 } 990 } else { 991 BUG(); 992 } 993 } 994 995 void security_compute_xperms_decision(struct selinux_state *state, 996 u32 ssid, 997 u32 tsid, 998 u16 orig_tclass, 999 u8 driver, 1000 struct extended_perms_decision *xpermd) 1001 { 1002 struct policydb *policydb; 1003 struct sidtab *sidtab; 1004 u16 tclass; 1005 struct context *scontext, *tcontext; 1006 struct avtab_key avkey; 1007 struct avtab_node *node; 1008 struct ebitmap *sattr, *tattr; 1009 struct ebitmap_node *snode, *tnode; 1010 unsigned int i, j; 1011 1012 xpermd->driver = driver; 1013 xpermd->used = 0; 1014 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p)); 1015 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p)); 1016 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p)); 1017 1018 read_lock(&state->ss->policy_rwlock); 1019 if (!selinux_initialized(state)) 1020 goto allow; 1021 1022 policydb = &state->ss->policydb; 1023 sidtab = state->ss->sidtab; 1024 1025 scontext = sidtab_search(sidtab, ssid); 1026 if (!scontext) { 1027 pr_err("SELinux: %s: unrecognized SID %d\n", 1028 __func__, ssid); 1029 goto out; 1030 } 1031 1032 tcontext = sidtab_search(sidtab, tsid); 1033 if (!tcontext) { 1034 pr_err("SELinux: %s: unrecognized SID %d\n", 1035 __func__, tsid); 1036 goto out; 1037 } 1038 1039 tclass = unmap_class(&state->ss->map, orig_tclass); 1040 if (unlikely(orig_tclass && !tclass)) { 1041 if (policydb->allow_unknown) 1042 goto allow; 1043 goto out; 1044 } 1045 1046 1047 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) { 1048 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass); 1049 goto out; 1050 } 1051 1052 avkey.target_class = tclass; 1053 avkey.specified = AVTAB_XPERMS; 1054 sattr = &policydb->type_attr_map_array[scontext->type - 1]; 1055 tattr = &policydb->type_attr_map_array[tcontext->type - 1]; 1056 ebitmap_for_each_positive_bit(sattr, snode, i) { 1057 ebitmap_for_each_positive_bit(tattr, tnode, j) { 1058 avkey.source_type = i + 1; 1059 avkey.target_type = j + 1; 1060 for (node = avtab_search_node(&policydb->te_avtab, 1061 &avkey); 1062 node; 1063 node = avtab_search_node_next(node, avkey.specified)) 1064 services_compute_xperms_decision(xpermd, node); 1065 1066 cond_compute_xperms(&policydb->te_cond_avtab, 1067 &avkey, xpermd); 1068 } 1069 } 1070 out: 1071 read_unlock(&state->ss->policy_rwlock); 1072 return; 1073 allow: 1074 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p)); 1075 goto out; 1076 } 1077 1078 /** 1079 * security_compute_av - Compute access vector decisions. 1080 * @ssid: source security identifier 1081 * @tsid: target security identifier 1082 * @tclass: target security class 1083 * @avd: access vector decisions 1084 * @xperms: extended permissions 1085 * 1086 * Compute a set of access vector decisions based on the 1087 * SID pair (@ssid, @tsid) for the permissions in @tclass. 1088 */ 1089 void security_compute_av(struct selinux_state *state, 1090 u32 ssid, 1091 u32 tsid, 1092 u16 orig_tclass, 1093 struct av_decision *avd, 1094 struct extended_perms *xperms) 1095 { 1096 struct policydb *policydb; 1097 struct sidtab *sidtab; 1098 u16 tclass; 1099 struct context *scontext = NULL, *tcontext = NULL; 1100 1101 read_lock(&state->ss->policy_rwlock); 1102 avd_init(state, avd); 1103 xperms->len = 0; 1104 if (!selinux_initialized(state)) 1105 goto allow; 1106 1107 policydb = &state->ss->policydb; 1108 sidtab = state->ss->sidtab; 1109 1110 scontext = sidtab_search(sidtab, ssid); 1111 if (!scontext) { 1112 pr_err("SELinux: %s: unrecognized SID %d\n", 1113 __func__, ssid); 1114 goto out; 1115 } 1116 1117 /* permissive domain? */ 1118 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type)) 1119 avd->flags |= AVD_FLAGS_PERMISSIVE; 1120 1121 tcontext = sidtab_search(sidtab, tsid); 1122 if (!tcontext) { 1123 pr_err("SELinux: %s: unrecognized SID %d\n", 1124 __func__, tsid); 1125 goto out; 1126 } 1127 1128 tclass = unmap_class(&state->ss->map, orig_tclass); 1129 if (unlikely(orig_tclass && !tclass)) { 1130 if (policydb->allow_unknown) 1131 goto allow; 1132 goto out; 1133 } 1134 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd, 1135 xperms); 1136 map_decision(&state->ss->map, orig_tclass, avd, 1137 policydb->allow_unknown); 1138 out: 1139 read_unlock(&state->ss->policy_rwlock); 1140 return; 1141 allow: 1142 avd->allowed = 0xffffffff; 1143 goto out; 1144 } 1145 1146 void security_compute_av_user(struct selinux_state *state, 1147 u32 ssid, 1148 u32 tsid, 1149 u16 tclass, 1150 struct av_decision *avd) 1151 { 1152 struct policydb *policydb; 1153 struct sidtab *sidtab; 1154 struct context *scontext = NULL, *tcontext = NULL; 1155 1156 read_lock(&state->ss->policy_rwlock); 1157 avd_init(state, avd); 1158 if (!selinux_initialized(state)) 1159 goto allow; 1160 1161 policydb = &state->ss->policydb; 1162 sidtab = state->ss->sidtab; 1163 1164 scontext = sidtab_search(sidtab, ssid); 1165 if (!scontext) { 1166 pr_err("SELinux: %s: unrecognized SID %d\n", 1167 __func__, ssid); 1168 goto out; 1169 } 1170 1171 /* permissive domain? */ 1172 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type)) 1173 avd->flags |= AVD_FLAGS_PERMISSIVE; 1174 1175 tcontext = sidtab_search(sidtab, tsid); 1176 if (!tcontext) { 1177 pr_err("SELinux: %s: unrecognized SID %d\n", 1178 __func__, tsid); 1179 goto out; 1180 } 1181 1182 if (unlikely(!tclass)) { 1183 if (policydb->allow_unknown) 1184 goto allow; 1185 goto out; 1186 } 1187 1188 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd, 1189 NULL); 1190 out: 1191 read_unlock(&state->ss->policy_rwlock); 1192 return; 1193 allow: 1194 avd->allowed = 0xffffffff; 1195 goto out; 1196 } 1197 1198 /* 1199 * Write the security context string representation of 1200 * the context structure `context' into a dynamically 1201 * allocated string of the correct size. Set `*scontext' 1202 * to point to this string and set `*scontext_len' to 1203 * the length of the string. 1204 */ 1205 static int context_struct_to_string(struct policydb *p, 1206 struct context *context, 1207 char **scontext, u32 *scontext_len) 1208 { 1209 char *scontextp; 1210 1211 if (scontext) 1212 *scontext = NULL; 1213 *scontext_len = 0; 1214 1215 if (context->len) { 1216 *scontext_len = context->len; 1217 if (scontext) { 1218 *scontext = kstrdup(context->str, GFP_ATOMIC); 1219 if (!(*scontext)) 1220 return -ENOMEM; 1221 } 1222 return 0; 1223 } 1224 1225 /* Compute the size of the context. */ 1226 *scontext_len += strlen(sym_name(p, SYM_USERS, context->user - 1)) + 1; 1227 *scontext_len += strlen(sym_name(p, SYM_ROLES, context->role - 1)) + 1; 1228 *scontext_len += strlen(sym_name(p, SYM_TYPES, context->type - 1)) + 1; 1229 *scontext_len += mls_compute_context_len(p, context); 1230 1231 if (!scontext) 1232 return 0; 1233 1234 /* Allocate space for the context; caller must free this space. */ 1235 scontextp = kmalloc(*scontext_len, GFP_ATOMIC); 1236 if (!scontextp) 1237 return -ENOMEM; 1238 *scontext = scontextp; 1239 1240 /* 1241 * Copy the user name, role name and type name into the context. 1242 */ 1243 scontextp += sprintf(scontextp, "%s:%s:%s", 1244 sym_name(p, SYM_USERS, context->user - 1), 1245 sym_name(p, SYM_ROLES, context->role - 1), 1246 sym_name(p, SYM_TYPES, context->type - 1)); 1247 1248 mls_sid_to_context(p, context, &scontextp); 1249 1250 *scontextp = 0; 1251 1252 return 0; 1253 } 1254 1255 static int sidtab_entry_to_string(struct policydb *p, 1256 struct sidtab *sidtab, 1257 struct sidtab_entry *entry, 1258 char **scontext, u32 *scontext_len) 1259 { 1260 int rc = sidtab_sid2str_get(sidtab, entry, scontext, scontext_len); 1261 1262 if (rc != -ENOENT) 1263 return rc; 1264 1265 rc = context_struct_to_string(p, &entry->context, scontext, 1266 scontext_len); 1267 if (!rc && scontext) 1268 sidtab_sid2str_put(sidtab, entry, *scontext, *scontext_len); 1269 return rc; 1270 } 1271 1272 #include "initial_sid_to_string.h" 1273 1274 int security_sidtab_hash_stats(struct selinux_state *state, char *page) 1275 { 1276 int rc; 1277 1278 if (!selinux_initialized(state)) { 1279 pr_err("SELinux: %s: called before initial load_policy\n", 1280 __func__); 1281 return -EINVAL; 1282 } 1283 1284 read_lock(&state->ss->policy_rwlock); 1285 rc = sidtab_hash_stats(state->ss->sidtab, page); 1286 read_unlock(&state->ss->policy_rwlock); 1287 1288 return rc; 1289 } 1290 1291 const char *security_get_initial_sid_context(u32 sid) 1292 { 1293 if (unlikely(sid > SECINITSID_NUM)) 1294 return NULL; 1295 return initial_sid_to_string[sid]; 1296 } 1297 1298 static int security_sid_to_context_core(struct selinux_state *state, 1299 u32 sid, char **scontext, 1300 u32 *scontext_len, int force, 1301 int only_invalid) 1302 { 1303 struct policydb *policydb; 1304 struct sidtab *sidtab; 1305 struct sidtab_entry *entry; 1306 int rc = 0; 1307 1308 if (scontext) 1309 *scontext = NULL; 1310 *scontext_len = 0; 1311 1312 if (!selinux_initialized(state)) { 1313 if (sid <= SECINITSID_NUM) { 1314 char *scontextp; 1315 const char *s = initial_sid_to_string[sid]; 1316 1317 if (!s) 1318 return -EINVAL; 1319 *scontext_len = strlen(s) + 1; 1320 if (!scontext) 1321 return 0; 1322 scontextp = kmemdup(s, *scontext_len, GFP_ATOMIC); 1323 if (!scontextp) 1324 return -ENOMEM; 1325 *scontext = scontextp; 1326 return 0; 1327 } 1328 pr_err("SELinux: %s: called before initial " 1329 "load_policy on unknown SID %d\n", __func__, sid); 1330 return -EINVAL; 1331 } 1332 read_lock(&state->ss->policy_rwlock); 1333 policydb = &state->ss->policydb; 1334 sidtab = state->ss->sidtab; 1335 1336 if (force) 1337 entry = sidtab_search_entry_force(sidtab, sid); 1338 else 1339 entry = sidtab_search_entry(sidtab, sid); 1340 if (!entry) { 1341 pr_err("SELinux: %s: unrecognized SID %d\n", 1342 __func__, sid); 1343 rc = -EINVAL; 1344 goto out_unlock; 1345 } 1346 if (only_invalid && !entry->context.len) 1347 goto out_unlock; 1348 1349 rc = sidtab_entry_to_string(policydb, sidtab, entry, scontext, 1350 scontext_len); 1351 1352 out_unlock: 1353 read_unlock(&state->ss->policy_rwlock); 1354 return rc; 1355 1356 } 1357 1358 /** 1359 * security_sid_to_context - Obtain a context for a given SID. 1360 * @sid: security identifier, SID 1361 * @scontext: security context 1362 * @scontext_len: length in bytes 1363 * 1364 * Write the string representation of the context associated with @sid 1365 * into a dynamically allocated string of the correct size. Set @scontext 1366 * to point to this string and set @scontext_len to the length of the string. 1367 */ 1368 int security_sid_to_context(struct selinux_state *state, 1369 u32 sid, char **scontext, u32 *scontext_len) 1370 { 1371 return security_sid_to_context_core(state, sid, scontext, 1372 scontext_len, 0, 0); 1373 } 1374 1375 int security_sid_to_context_force(struct selinux_state *state, u32 sid, 1376 char **scontext, u32 *scontext_len) 1377 { 1378 return security_sid_to_context_core(state, sid, scontext, 1379 scontext_len, 1, 0); 1380 } 1381 1382 /** 1383 * security_sid_to_context_inval - Obtain a context for a given SID if it 1384 * is invalid. 1385 * @sid: security identifier, SID 1386 * @scontext: security context 1387 * @scontext_len: length in bytes 1388 * 1389 * Write the string representation of the context associated with @sid 1390 * into a dynamically allocated string of the correct size, but only if the 1391 * context is invalid in the current policy. Set @scontext to point to 1392 * this string (or NULL if the context is valid) and set @scontext_len to 1393 * the length of the string (or 0 if the context is valid). 1394 */ 1395 int security_sid_to_context_inval(struct selinux_state *state, u32 sid, 1396 char **scontext, u32 *scontext_len) 1397 { 1398 return security_sid_to_context_core(state, sid, scontext, 1399 scontext_len, 1, 1); 1400 } 1401 1402 /* 1403 * Caveat: Mutates scontext. 1404 */ 1405 static int string_to_context_struct(struct policydb *pol, 1406 struct sidtab *sidtabp, 1407 char *scontext, 1408 struct context *ctx, 1409 u32 def_sid) 1410 { 1411 struct role_datum *role; 1412 struct type_datum *typdatum; 1413 struct user_datum *usrdatum; 1414 char *scontextp, *p, oldc; 1415 int rc = 0; 1416 1417 context_init(ctx); 1418 1419 /* Parse the security context. */ 1420 1421 rc = -EINVAL; 1422 scontextp = (char *) scontext; 1423 1424 /* Extract the user. */ 1425 p = scontextp; 1426 while (*p && *p != ':') 1427 p++; 1428 1429 if (*p == 0) 1430 goto out; 1431 1432 *p++ = 0; 1433 1434 usrdatum = symtab_search(&pol->p_users, scontextp); 1435 if (!usrdatum) 1436 goto out; 1437 1438 ctx->user = usrdatum->value; 1439 1440 /* Extract role. */ 1441 scontextp = p; 1442 while (*p && *p != ':') 1443 p++; 1444 1445 if (*p == 0) 1446 goto out; 1447 1448 *p++ = 0; 1449 1450 role = symtab_search(&pol->p_roles, scontextp); 1451 if (!role) 1452 goto out; 1453 ctx->role = role->value; 1454 1455 /* Extract type. */ 1456 scontextp = p; 1457 while (*p && *p != ':') 1458 p++; 1459 oldc = *p; 1460 *p++ = 0; 1461 1462 typdatum = symtab_search(&pol->p_types, scontextp); 1463 if (!typdatum || typdatum->attribute) 1464 goto out; 1465 1466 ctx->type = typdatum->value; 1467 1468 rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid); 1469 if (rc) 1470 goto out; 1471 1472 /* Check the validity of the new context. */ 1473 rc = -EINVAL; 1474 if (!policydb_context_isvalid(pol, ctx)) 1475 goto out; 1476 rc = 0; 1477 out: 1478 if (rc) 1479 context_destroy(ctx); 1480 return rc; 1481 } 1482 1483 static int security_context_to_sid_core(struct selinux_state *state, 1484 const char *scontext, u32 scontext_len, 1485 u32 *sid, u32 def_sid, gfp_t gfp_flags, 1486 int force) 1487 { 1488 struct policydb *policydb; 1489 struct sidtab *sidtab; 1490 char *scontext2, *str = NULL; 1491 struct context context; 1492 int rc = 0; 1493 1494 /* An empty security context is never valid. */ 1495 if (!scontext_len) 1496 return -EINVAL; 1497 1498 /* Copy the string to allow changes and ensure a NUL terminator */ 1499 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags); 1500 if (!scontext2) 1501 return -ENOMEM; 1502 1503 if (!selinux_initialized(state)) { 1504 int i; 1505 1506 for (i = 1; i < SECINITSID_NUM; i++) { 1507 const char *s = initial_sid_to_string[i]; 1508 1509 if (s && !strcmp(s, scontext2)) { 1510 *sid = i; 1511 goto out; 1512 } 1513 } 1514 *sid = SECINITSID_KERNEL; 1515 goto out; 1516 } 1517 *sid = SECSID_NULL; 1518 1519 if (force) { 1520 /* Save another copy for storing in uninterpreted form */ 1521 rc = -ENOMEM; 1522 str = kstrdup(scontext2, gfp_flags); 1523 if (!str) 1524 goto out; 1525 } 1526 read_lock(&state->ss->policy_rwlock); 1527 policydb = &state->ss->policydb; 1528 sidtab = state->ss->sidtab; 1529 rc = string_to_context_struct(policydb, sidtab, scontext2, 1530 &context, def_sid); 1531 if (rc == -EINVAL && force) { 1532 context.str = str; 1533 context.len = strlen(str) + 1; 1534 str = NULL; 1535 } else if (rc) 1536 goto out_unlock; 1537 rc = sidtab_context_to_sid(sidtab, &context, sid); 1538 context_destroy(&context); 1539 out_unlock: 1540 read_unlock(&state->ss->policy_rwlock); 1541 out: 1542 kfree(scontext2); 1543 kfree(str); 1544 return rc; 1545 } 1546 1547 /** 1548 * security_context_to_sid - Obtain a SID for a given security context. 1549 * @scontext: security context 1550 * @scontext_len: length in bytes 1551 * @sid: security identifier, SID 1552 * @gfp: context for the allocation 1553 * 1554 * Obtains a SID associated with the security context that 1555 * has the string representation specified by @scontext. 1556 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient 1557 * memory is available, or 0 on success. 1558 */ 1559 int security_context_to_sid(struct selinux_state *state, 1560 const char *scontext, u32 scontext_len, u32 *sid, 1561 gfp_t gfp) 1562 { 1563 return security_context_to_sid_core(state, scontext, scontext_len, 1564 sid, SECSID_NULL, gfp, 0); 1565 } 1566 1567 int security_context_str_to_sid(struct selinux_state *state, 1568 const char *scontext, u32 *sid, gfp_t gfp) 1569 { 1570 return security_context_to_sid(state, scontext, strlen(scontext), 1571 sid, gfp); 1572 } 1573 1574 /** 1575 * security_context_to_sid_default - Obtain a SID for a given security context, 1576 * falling back to specified default if needed. 1577 * 1578 * @scontext: security context 1579 * @scontext_len: length in bytes 1580 * @sid: security identifier, SID 1581 * @def_sid: default SID to assign on error 1582 * 1583 * Obtains a SID associated with the security context that 1584 * has the string representation specified by @scontext. 1585 * The default SID is passed to the MLS layer to be used to allow 1586 * kernel labeling of the MLS field if the MLS field is not present 1587 * (for upgrading to MLS without full relabel). 1588 * Implicitly forces adding of the context even if it cannot be mapped yet. 1589 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient 1590 * memory is available, or 0 on success. 1591 */ 1592 int security_context_to_sid_default(struct selinux_state *state, 1593 const char *scontext, u32 scontext_len, 1594 u32 *sid, u32 def_sid, gfp_t gfp_flags) 1595 { 1596 return security_context_to_sid_core(state, scontext, scontext_len, 1597 sid, def_sid, gfp_flags, 1); 1598 } 1599 1600 int security_context_to_sid_force(struct selinux_state *state, 1601 const char *scontext, u32 scontext_len, 1602 u32 *sid) 1603 { 1604 return security_context_to_sid_core(state, scontext, scontext_len, 1605 sid, SECSID_NULL, GFP_KERNEL, 1); 1606 } 1607 1608 static int compute_sid_handle_invalid_context( 1609 struct selinux_state *state, 1610 struct sidtab_entry *sentry, 1611 struct sidtab_entry *tentry, 1612 u16 tclass, 1613 struct context *newcontext) 1614 { 1615 struct policydb *policydb = &state->ss->policydb; 1616 struct sidtab *sidtab = state->ss->sidtab; 1617 char *s = NULL, *t = NULL, *n = NULL; 1618 u32 slen, tlen, nlen; 1619 struct audit_buffer *ab; 1620 1621 if (sidtab_entry_to_string(policydb, sidtab, sentry, &s, &slen)) 1622 goto out; 1623 if (sidtab_entry_to_string(policydb, sidtab, tentry, &t, &tlen)) 1624 goto out; 1625 if (context_struct_to_string(policydb, newcontext, &n, &nlen)) 1626 goto out; 1627 ab = audit_log_start(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR); 1628 audit_log_format(ab, 1629 "op=security_compute_sid invalid_context="); 1630 /* no need to record the NUL with untrusted strings */ 1631 audit_log_n_untrustedstring(ab, n, nlen - 1); 1632 audit_log_format(ab, " scontext=%s tcontext=%s tclass=%s", 1633 s, t, sym_name(policydb, SYM_CLASSES, tclass-1)); 1634 audit_log_end(ab); 1635 out: 1636 kfree(s); 1637 kfree(t); 1638 kfree(n); 1639 if (!enforcing_enabled(state)) 1640 return 0; 1641 return -EACCES; 1642 } 1643 1644 static void filename_compute_type(struct policydb *policydb, 1645 struct context *newcontext, 1646 u32 stype, u32 ttype, u16 tclass, 1647 const char *objname) 1648 { 1649 struct filename_trans_key ft; 1650 struct filename_trans_datum *datum; 1651 1652 /* 1653 * Most filename trans rules are going to live in specific directories 1654 * like /dev or /var/run. This bitmap will quickly skip rule searches 1655 * if the ttype does not contain any rules. 1656 */ 1657 if (!ebitmap_get_bit(&policydb->filename_trans_ttypes, ttype)) 1658 return; 1659 1660 ft.ttype = ttype; 1661 ft.tclass = tclass; 1662 ft.name = objname; 1663 1664 datum = policydb_filenametr_search(policydb, &ft); 1665 while (datum) { 1666 if (ebitmap_get_bit(&datum->stypes, stype - 1)) { 1667 newcontext->type = datum->otype; 1668 return; 1669 } 1670 datum = datum->next; 1671 } 1672 } 1673 1674 static int security_compute_sid(struct selinux_state *state, 1675 u32 ssid, 1676 u32 tsid, 1677 u16 orig_tclass, 1678 u32 specified, 1679 const char *objname, 1680 u32 *out_sid, 1681 bool kern) 1682 { 1683 struct policydb *policydb; 1684 struct sidtab *sidtab; 1685 struct class_datum *cladatum = NULL; 1686 struct context *scontext, *tcontext, newcontext; 1687 struct sidtab_entry *sentry, *tentry; 1688 struct avtab_key avkey; 1689 struct avtab_datum *avdatum; 1690 struct avtab_node *node; 1691 u16 tclass; 1692 int rc = 0; 1693 bool sock; 1694 1695 if (!selinux_initialized(state)) { 1696 switch (orig_tclass) { 1697 case SECCLASS_PROCESS: /* kernel value */ 1698 *out_sid = ssid; 1699 break; 1700 default: 1701 *out_sid = tsid; 1702 break; 1703 } 1704 goto out; 1705 } 1706 1707 context_init(&newcontext); 1708 1709 read_lock(&state->ss->policy_rwlock); 1710 1711 if (kern) { 1712 tclass = unmap_class(&state->ss->map, orig_tclass); 1713 sock = security_is_socket_class(orig_tclass); 1714 } else { 1715 tclass = orig_tclass; 1716 sock = security_is_socket_class(map_class(&state->ss->map, 1717 tclass)); 1718 } 1719 1720 policydb = &state->ss->policydb; 1721 sidtab = state->ss->sidtab; 1722 1723 sentry = sidtab_search_entry(sidtab, ssid); 1724 if (!sentry) { 1725 pr_err("SELinux: %s: unrecognized SID %d\n", 1726 __func__, ssid); 1727 rc = -EINVAL; 1728 goto out_unlock; 1729 } 1730 tentry = sidtab_search_entry(sidtab, tsid); 1731 if (!tentry) { 1732 pr_err("SELinux: %s: unrecognized SID %d\n", 1733 __func__, tsid); 1734 rc = -EINVAL; 1735 goto out_unlock; 1736 } 1737 1738 scontext = &sentry->context; 1739 tcontext = &tentry->context; 1740 1741 if (tclass && tclass <= policydb->p_classes.nprim) 1742 cladatum = policydb->class_val_to_struct[tclass - 1]; 1743 1744 /* Set the user identity. */ 1745 switch (specified) { 1746 case AVTAB_TRANSITION: 1747 case AVTAB_CHANGE: 1748 if (cladatum && cladatum->default_user == DEFAULT_TARGET) { 1749 newcontext.user = tcontext->user; 1750 } else { 1751 /* notice this gets both DEFAULT_SOURCE and unset */ 1752 /* Use the process user identity. */ 1753 newcontext.user = scontext->user; 1754 } 1755 break; 1756 case AVTAB_MEMBER: 1757 /* Use the related object owner. */ 1758 newcontext.user = tcontext->user; 1759 break; 1760 } 1761 1762 /* Set the role to default values. */ 1763 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) { 1764 newcontext.role = scontext->role; 1765 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) { 1766 newcontext.role = tcontext->role; 1767 } else { 1768 if ((tclass == policydb->process_class) || sock) 1769 newcontext.role = scontext->role; 1770 else 1771 newcontext.role = OBJECT_R_VAL; 1772 } 1773 1774 /* Set the type to default values. */ 1775 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) { 1776 newcontext.type = scontext->type; 1777 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) { 1778 newcontext.type = tcontext->type; 1779 } else { 1780 if ((tclass == policydb->process_class) || sock) { 1781 /* Use the type of process. */ 1782 newcontext.type = scontext->type; 1783 } else { 1784 /* Use the type of the related object. */ 1785 newcontext.type = tcontext->type; 1786 } 1787 } 1788 1789 /* Look for a type transition/member/change rule. */ 1790 avkey.source_type = scontext->type; 1791 avkey.target_type = tcontext->type; 1792 avkey.target_class = tclass; 1793 avkey.specified = specified; 1794 avdatum = avtab_search(&policydb->te_avtab, &avkey); 1795 1796 /* If no permanent rule, also check for enabled conditional rules */ 1797 if (!avdatum) { 1798 node = avtab_search_node(&policydb->te_cond_avtab, &avkey); 1799 for (; node; node = avtab_search_node_next(node, specified)) { 1800 if (node->key.specified & AVTAB_ENABLED) { 1801 avdatum = &node->datum; 1802 break; 1803 } 1804 } 1805 } 1806 1807 if (avdatum) { 1808 /* Use the type from the type transition/member/change rule. */ 1809 newcontext.type = avdatum->u.data; 1810 } 1811 1812 /* if we have a objname this is a file trans check so check those rules */ 1813 if (objname) 1814 filename_compute_type(policydb, &newcontext, scontext->type, 1815 tcontext->type, tclass, objname); 1816 1817 /* Check for class-specific changes. */ 1818 if (specified & AVTAB_TRANSITION) { 1819 /* Look for a role transition rule. */ 1820 struct role_trans_datum *rtd; 1821 struct role_trans_key rtk = { 1822 .role = scontext->role, 1823 .type = tcontext->type, 1824 .tclass = tclass, 1825 }; 1826 1827 rtd = policydb_roletr_search(policydb, &rtk); 1828 if (rtd) 1829 newcontext.role = rtd->new_role; 1830 } 1831 1832 /* Set the MLS attributes. 1833 This is done last because it may allocate memory. */ 1834 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified, 1835 &newcontext, sock); 1836 if (rc) 1837 goto out_unlock; 1838 1839 /* Check the validity of the context. */ 1840 if (!policydb_context_isvalid(policydb, &newcontext)) { 1841 rc = compute_sid_handle_invalid_context(state, sentry, tentry, 1842 tclass, &newcontext); 1843 if (rc) 1844 goto out_unlock; 1845 } 1846 /* Obtain the sid for the context. */ 1847 rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid); 1848 out_unlock: 1849 read_unlock(&state->ss->policy_rwlock); 1850 context_destroy(&newcontext); 1851 out: 1852 return rc; 1853 } 1854 1855 /** 1856 * security_transition_sid - Compute the SID for a new subject/object. 1857 * @ssid: source security identifier 1858 * @tsid: target security identifier 1859 * @tclass: target security class 1860 * @out_sid: security identifier for new subject/object 1861 * 1862 * Compute a SID to use for labeling a new subject or object in the 1863 * class @tclass based on a SID pair (@ssid, @tsid). 1864 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 1865 * if insufficient memory is available, or %0 if the new SID was 1866 * computed successfully. 1867 */ 1868 int security_transition_sid(struct selinux_state *state, 1869 u32 ssid, u32 tsid, u16 tclass, 1870 const struct qstr *qstr, u32 *out_sid) 1871 { 1872 return security_compute_sid(state, ssid, tsid, tclass, 1873 AVTAB_TRANSITION, 1874 qstr ? qstr->name : NULL, out_sid, true); 1875 } 1876 1877 int security_transition_sid_user(struct selinux_state *state, 1878 u32 ssid, u32 tsid, u16 tclass, 1879 const char *objname, u32 *out_sid) 1880 { 1881 return security_compute_sid(state, ssid, tsid, tclass, 1882 AVTAB_TRANSITION, 1883 objname, out_sid, false); 1884 } 1885 1886 /** 1887 * security_member_sid - Compute the SID for member selection. 1888 * @ssid: source security identifier 1889 * @tsid: target security identifier 1890 * @tclass: target security class 1891 * @out_sid: security identifier for selected member 1892 * 1893 * Compute a SID to use when selecting a member of a polyinstantiated 1894 * object of class @tclass based on a SID pair (@ssid, @tsid). 1895 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 1896 * if insufficient memory is available, or %0 if the SID was 1897 * computed successfully. 1898 */ 1899 int security_member_sid(struct selinux_state *state, 1900 u32 ssid, 1901 u32 tsid, 1902 u16 tclass, 1903 u32 *out_sid) 1904 { 1905 return security_compute_sid(state, ssid, tsid, tclass, 1906 AVTAB_MEMBER, NULL, 1907 out_sid, false); 1908 } 1909 1910 /** 1911 * security_change_sid - Compute the SID for object relabeling. 1912 * @ssid: source security identifier 1913 * @tsid: target security identifier 1914 * @tclass: target security class 1915 * @out_sid: security identifier for selected member 1916 * 1917 * Compute a SID to use for relabeling an object of class @tclass 1918 * based on a SID pair (@ssid, @tsid). 1919 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 1920 * if insufficient memory is available, or %0 if the SID was 1921 * computed successfully. 1922 */ 1923 int security_change_sid(struct selinux_state *state, 1924 u32 ssid, 1925 u32 tsid, 1926 u16 tclass, 1927 u32 *out_sid) 1928 { 1929 return security_compute_sid(state, 1930 ssid, tsid, tclass, AVTAB_CHANGE, NULL, 1931 out_sid, false); 1932 } 1933 1934 static inline int convert_context_handle_invalid_context( 1935 struct selinux_state *state, 1936 struct context *context) 1937 { 1938 struct policydb *policydb = &state->ss->policydb; 1939 char *s; 1940 u32 len; 1941 1942 if (enforcing_enabled(state)) 1943 return -EINVAL; 1944 1945 if (!context_struct_to_string(policydb, context, &s, &len)) { 1946 pr_warn("SELinux: Context %s would be invalid if enforcing\n", 1947 s); 1948 kfree(s); 1949 } 1950 return 0; 1951 } 1952 1953 struct convert_context_args { 1954 struct selinux_state *state; 1955 struct policydb *oldp; 1956 struct policydb *newp; 1957 }; 1958 1959 /* 1960 * Convert the values in the security context 1961 * structure `oldc' from the values specified 1962 * in the policy `p->oldp' to the values specified 1963 * in the policy `p->newp', storing the new context 1964 * in `newc'. Verify that the context is valid 1965 * under the new policy. 1966 */ 1967 static int convert_context(struct context *oldc, struct context *newc, void *p) 1968 { 1969 struct convert_context_args *args; 1970 struct ocontext *oc; 1971 struct role_datum *role; 1972 struct type_datum *typdatum; 1973 struct user_datum *usrdatum; 1974 char *s; 1975 u32 len; 1976 int rc; 1977 1978 args = p; 1979 1980 if (oldc->str) { 1981 s = kstrdup(oldc->str, GFP_KERNEL); 1982 if (!s) 1983 return -ENOMEM; 1984 1985 rc = string_to_context_struct(args->newp, NULL, s, 1986 newc, SECSID_NULL); 1987 if (rc == -EINVAL) { 1988 /* 1989 * Retain string representation for later mapping. 1990 * 1991 * IMPORTANT: We need to copy the contents of oldc->str 1992 * back into s again because string_to_context_struct() 1993 * may have garbled it. 1994 */ 1995 memcpy(s, oldc->str, oldc->len); 1996 context_init(newc); 1997 newc->str = s; 1998 newc->len = oldc->len; 1999 return 0; 2000 } 2001 kfree(s); 2002 if (rc) { 2003 /* Other error condition, e.g. ENOMEM. */ 2004 pr_err("SELinux: Unable to map context %s, rc = %d.\n", 2005 oldc->str, -rc); 2006 return rc; 2007 } 2008 pr_info("SELinux: Context %s became valid (mapped).\n", 2009 oldc->str); 2010 return 0; 2011 } 2012 2013 context_init(newc); 2014 2015 /* Convert the user. */ 2016 rc = -EINVAL; 2017 usrdatum = symtab_search(&args->newp->p_users, 2018 sym_name(args->oldp, 2019 SYM_USERS, oldc->user - 1)); 2020 if (!usrdatum) 2021 goto bad; 2022 newc->user = usrdatum->value; 2023 2024 /* Convert the role. */ 2025 rc = -EINVAL; 2026 role = symtab_search(&args->newp->p_roles, 2027 sym_name(args->oldp, SYM_ROLES, oldc->role - 1)); 2028 if (!role) 2029 goto bad; 2030 newc->role = role->value; 2031 2032 /* Convert the type. */ 2033 rc = -EINVAL; 2034 typdatum = symtab_search(&args->newp->p_types, 2035 sym_name(args->oldp, 2036 SYM_TYPES, oldc->type - 1)); 2037 if (!typdatum) 2038 goto bad; 2039 newc->type = typdatum->value; 2040 2041 /* Convert the MLS fields if dealing with MLS policies */ 2042 if (args->oldp->mls_enabled && args->newp->mls_enabled) { 2043 rc = mls_convert_context(args->oldp, args->newp, oldc, newc); 2044 if (rc) 2045 goto bad; 2046 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) { 2047 /* 2048 * Switching between non-MLS and MLS policy: 2049 * ensure that the MLS fields of the context for all 2050 * existing entries in the sidtab are filled in with a 2051 * suitable default value, likely taken from one of the 2052 * initial SIDs. 2053 */ 2054 oc = args->newp->ocontexts[OCON_ISID]; 2055 while (oc && oc->sid[0] != SECINITSID_UNLABELED) 2056 oc = oc->next; 2057 rc = -EINVAL; 2058 if (!oc) { 2059 pr_err("SELinux: unable to look up" 2060 " the initial SIDs list\n"); 2061 goto bad; 2062 } 2063 rc = mls_range_set(newc, &oc->context[0].range); 2064 if (rc) 2065 goto bad; 2066 } 2067 2068 /* Check the validity of the new context. */ 2069 if (!policydb_context_isvalid(args->newp, newc)) { 2070 rc = convert_context_handle_invalid_context(args->state, oldc); 2071 if (rc) 2072 goto bad; 2073 } 2074 2075 return 0; 2076 bad: 2077 /* Map old representation to string and save it. */ 2078 rc = context_struct_to_string(args->oldp, oldc, &s, &len); 2079 if (rc) 2080 return rc; 2081 context_destroy(newc); 2082 newc->str = s; 2083 newc->len = len; 2084 pr_info("SELinux: Context %s became invalid (unmapped).\n", 2085 newc->str); 2086 return 0; 2087 } 2088 2089 static void security_load_policycaps(struct selinux_state *state) 2090 { 2091 struct policydb *p = &state->ss->policydb; 2092 unsigned int i; 2093 struct ebitmap_node *node; 2094 2095 for (i = 0; i < ARRAY_SIZE(state->policycap); i++) 2096 state->policycap[i] = ebitmap_get_bit(&p->policycaps, i); 2097 2098 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++) 2099 pr_info("SELinux: policy capability %s=%d\n", 2100 selinux_policycap_names[i], 2101 ebitmap_get_bit(&p->policycaps, i)); 2102 2103 ebitmap_for_each_positive_bit(&p->policycaps, node, i) { 2104 if (i >= ARRAY_SIZE(selinux_policycap_names)) 2105 pr_info("SELinux: unknown policy capability %u\n", 2106 i); 2107 } 2108 } 2109 2110 static int security_preserve_bools(struct selinux_state *state, 2111 struct policydb *newpolicydb); 2112 2113 /** 2114 * security_load_policy - Load a security policy configuration. 2115 * @data: binary policy data 2116 * @len: length of data in bytes 2117 * 2118 * Load a new set of security policy configuration data, 2119 * validate it and convert the SID table as necessary. 2120 * This function will flush the access vector cache after 2121 * loading the new policy. 2122 */ 2123 int security_load_policy(struct selinux_state *state, void *data, size_t len) 2124 { 2125 struct policydb *policydb; 2126 struct sidtab *oldsidtab, *newsidtab; 2127 struct policydb *oldpolicydb, *newpolicydb; 2128 struct selinux_mapping *oldmapping; 2129 struct selinux_map newmap; 2130 struct sidtab_convert_params convert_params; 2131 struct convert_context_args args; 2132 u32 seqno; 2133 int rc = 0; 2134 struct policy_file file = { data, len }, *fp = &file; 2135 2136 policydb = &state->ss->policydb; 2137 2138 newsidtab = kmalloc(sizeof(*newsidtab), GFP_KERNEL); 2139 if (!newsidtab) 2140 return -ENOMEM; 2141 2142 if (!selinux_initialized(state)) { 2143 rc = policydb_read(policydb, fp); 2144 if (rc) { 2145 kfree(newsidtab); 2146 return rc; 2147 } 2148 2149 policydb->len = len; 2150 rc = selinux_set_mapping(policydb, secclass_map, 2151 &state->ss->map); 2152 if (rc) { 2153 kfree(newsidtab); 2154 policydb_destroy(policydb); 2155 return rc; 2156 } 2157 2158 rc = policydb_load_isids(policydb, newsidtab); 2159 if (rc) { 2160 kfree(newsidtab); 2161 policydb_destroy(policydb); 2162 return rc; 2163 } 2164 2165 state->ss->sidtab = newsidtab; 2166 security_load_policycaps(state); 2167 selinux_mark_initialized(state); 2168 seqno = ++state->ss->latest_granting; 2169 selinux_complete_init(); 2170 avc_ss_reset(state->avc, seqno); 2171 selnl_notify_policyload(seqno); 2172 selinux_status_update_policyload(state, seqno); 2173 selinux_netlbl_cache_invalidate(); 2174 selinux_xfrm_notify_policyload(); 2175 return 0; 2176 } 2177 2178 oldpolicydb = kcalloc(2, sizeof(*oldpolicydb), GFP_KERNEL); 2179 if (!oldpolicydb) { 2180 kfree(newsidtab); 2181 return -ENOMEM; 2182 } 2183 newpolicydb = oldpolicydb + 1; 2184 2185 rc = policydb_read(newpolicydb, fp); 2186 if (rc) { 2187 kfree(newsidtab); 2188 goto out; 2189 } 2190 2191 newpolicydb->len = len; 2192 /* If switching between different policy types, log MLS status */ 2193 if (policydb->mls_enabled && !newpolicydb->mls_enabled) 2194 pr_info("SELinux: Disabling MLS support...\n"); 2195 else if (!policydb->mls_enabled && newpolicydb->mls_enabled) 2196 pr_info("SELinux: Enabling MLS support...\n"); 2197 2198 rc = policydb_load_isids(newpolicydb, newsidtab); 2199 if (rc) { 2200 pr_err("SELinux: unable to load the initial SIDs\n"); 2201 policydb_destroy(newpolicydb); 2202 kfree(newsidtab); 2203 goto out; 2204 } 2205 2206 rc = selinux_set_mapping(newpolicydb, secclass_map, &newmap); 2207 if (rc) 2208 goto err; 2209 2210 rc = security_preserve_bools(state, newpolicydb); 2211 if (rc) { 2212 pr_err("SELinux: unable to preserve booleans\n"); 2213 goto err; 2214 } 2215 2216 oldsidtab = state->ss->sidtab; 2217 2218 /* 2219 * Convert the internal representations of contexts 2220 * in the new SID table. 2221 */ 2222 args.state = state; 2223 args.oldp = policydb; 2224 args.newp = newpolicydb; 2225 2226 convert_params.func = convert_context; 2227 convert_params.args = &args; 2228 convert_params.target = newsidtab; 2229 2230 rc = sidtab_convert(oldsidtab, &convert_params); 2231 if (rc) { 2232 pr_err("SELinux: unable to convert the internal" 2233 " representation of contexts in the new SID" 2234 " table\n"); 2235 goto err; 2236 } 2237 2238 /* Save the old policydb and SID table to free later. */ 2239 memcpy(oldpolicydb, policydb, sizeof(*policydb)); 2240 2241 /* Install the new policydb and SID table. */ 2242 write_lock_irq(&state->ss->policy_rwlock); 2243 memcpy(policydb, newpolicydb, sizeof(*policydb)); 2244 state->ss->sidtab = newsidtab; 2245 security_load_policycaps(state); 2246 oldmapping = state->ss->map.mapping; 2247 state->ss->map.mapping = newmap.mapping; 2248 state->ss->map.size = newmap.size; 2249 seqno = ++state->ss->latest_granting; 2250 write_unlock_irq(&state->ss->policy_rwlock); 2251 2252 /* Free the old policydb and SID table. */ 2253 policydb_destroy(oldpolicydb); 2254 sidtab_destroy(oldsidtab); 2255 kfree(oldsidtab); 2256 kfree(oldmapping); 2257 2258 avc_ss_reset(state->avc, seqno); 2259 selnl_notify_policyload(seqno); 2260 selinux_status_update_policyload(state, seqno); 2261 selinux_netlbl_cache_invalidate(); 2262 selinux_xfrm_notify_policyload(); 2263 2264 rc = 0; 2265 goto out; 2266 2267 err: 2268 kfree(newmap.mapping); 2269 sidtab_destroy(newsidtab); 2270 kfree(newsidtab); 2271 policydb_destroy(newpolicydb); 2272 2273 out: 2274 kfree(oldpolicydb); 2275 return rc; 2276 } 2277 2278 size_t security_policydb_len(struct selinux_state *state) 2279 { 2280 struct policydb *p = &state->ss->policydb; 2281 size_t len; 2282 2283 read_lock(&state->ss->policy_rwlock); 2284 len = p->len; 2285 read_unlock(&state->ss->policy_rwlock); 2286 2287 return len; 2288 } 2289 2290 /** 2291 * security_port_sid - Obtain the SID for a port. 2292 * @protocol: protocol number 2293 * @port: port number 2294 * @out_sid: security identifier 2295 */ 2296 int security_port_sid(struct selinux_state *state, 2297 u8 protocol, u16 port, u32 *out_sid) 2298 { 2299 struct policydb *policydb; 2300 struct sidtab *sidtab; 2301 struct ocontext *c; 2302 int rc = 0; 2303 2304 read_lock(&state->ss->policy_rwlock); 2305 2306 policydb = &state->ss->policydb; 2307 sidtab = state->ss->sidtab; 2308 2309 c = policydb->ocontexts[OCON_PORT]; 2310 while (c) { 2311 if (c->u.port.protocol == protocol && 2312 c->u.port.low_port <= port && 2313 c->u.port.high_port >= port) 2314 break; 2315 c = c->next; 2316 } 2317 2318 if (c) { 2319 if (!c->sid[0]) { 2320 rc = sidtab_context_to_sid(sidtab, &c->context[0], 2321 &c->sid[0]); 2322 if (rc) 2323 goto out; 2324 } 2325 *out_sid = c->sid[0]; 2326 } else { 2327 *out_sid = SECINITSID_PORT; 2328 } 2329 2330 out: 2331 read_unlock(&state->ss->policy_rwlock); 2332 return rc; 2333 } 2334 2335 /** 2336 * security_pkey_sid - Obtain the SID for a pkey. 2337 * @subnet_prefix: Subnet Prefix 2338 * @pkey_num: pkey number 2339 * @out_sid: security identifier 2340 */ 2341 int security_ib_pkey_sid(struct selinux_state *state, 2342 u64 subnet_prefix, u16 pkey_num, u32 *out_sid) 2343 { 2344 struct policydb *policydb; 2345 struct sidtab *sidtab; 2346 struct ocontext *c; 2347 int rc = 0; 2348 2349 read_lock(&state->ss->policy_rwlock); 2350 2351 policydb = &state->ss->policydb; 2352 sidtab = state->ss->sidtab; 2353 2354 c = policydb->ocontexts[OCON_IBPKEY]; 2355 while (c) { 2356 if (c->u.ibpkey.low_pkey <= pkey_num && 2357 c->u.ibpkey.high_pkey >= pkey_num && 2358 c->u.ibpkey.subnet_prefix == subnet_prefix) 2359 break; 2360 2361 c = c->next; 2362 } 2363 2364 if (c) { 2365 if (!c->sid[0]) { 2366 rc = sidtab_context_to_sid(sidtab, 2367 &c->context[0], 2368 &c->sid[0]); 2369 if (rc) 2370 goto out; 2371 } 2372 *out_sid = c->sid[0]; 2373 } else 2374 *out_sid = SECINITSID_UNLABELED; 2375 2376 out: 2377 read_unlock(&state->ss->policy_rwlock); 2378 return rc; 2379 } 2380 2381 /** 2382 * security_ib_endport_sid - Obtain the SID for a subnet management interface. 2383 * @dev_name: device name 2384 * @port: port number 2385 * @out_sid: security identifier 2386 */ 2387 int security_ib_endport_sid(struct selinux_state *state, 2388 const char *dev_name, u8 port_num, u32 *out_sid) 2389 { 2390 struct policydb *policydb; 2391 struct sidtab *sidtab; 2392 struct ocontext *c; 2393 int rc = 0; 2394 2395 read_lock(&state->ss->policy_rwlock); 2396 2397 policydb = &state->ss->policydb; 2398 sidtab = state->ss->sidtab; 2399 2400 c = policydb->ocontexts[OCON_IBENDPORT]; 2401 while (c) { 2402 if (c->u.ibendport.port == port_num && 2403 !strncmp(c->u.ibendport.dev_name, 2404 dev_name, 2405 IB_DEVICE_NAME_MAX)) 2406 break; 2407 2408 c = c->next; 2409 } 2410 2411 if (c) { 2412 if (!c->sid[0]) { 2413 rc = sidtab_context_to_sid(sidtab, &c->context[0], 2414 &c->sid[0]); 2415 if (rc) 2416 goto out; 2417 } 2418 *out_sid = c->sid[0]; 2419 } else 2420 *out_sid = SECINITSID_UNLABELED; 2421 2422 out: 2423 read_unlock(&state->ss->policy_rwlock); 2424 return rc; 2425 } 2426 2427 /** 2428 * security_netif_sid - Obtain the SID for a network interface. 2429 * @name: interface name 2430 * @if_sid: interface SID 2431 */ 2432 int security_netif_sid(struct selinux_state *state, 2433 char *name, u32 *if_sid) 2434 { 2435 struct policydb *policydb; 2436 struct sidtab *sidtab; 2437 int rc = 0; 2438 struct ocontext *c; 2439 2440 read_lock(&state->ss->policy_rwlock); 2441 2442 policydb = &state->ss->policydb; 2443 sidtab = state->ss->sidtab; 2444 2445 c = policydb->ocontexts[OCON_NETIF]; 2446 while (c) { 2447 if (strcmp(name, c->u.name) == 0) 2448 break; 2449 c = c->next; 2450 } 2451 2452 if (c) { 2453 if (!c->sid[0] || !c->sid[1]) { 2454 rc = sidtab_context_to_sid(sidtab, &c->context[0], 2455 &c->sid[0]); 2456 if (rc) 2457 goto out; 2458 rc = sidtab_context_to_sid(sidtab, &c->context[1], 2459 &c->sid[1]); 2460 if (rc) 2461 goto out; 2462 } 2463 *if_sid = c->sid[0]; 2464 } else 2465 *if_sid = SECINITSID_NETIF; 2466 2467 out: 2468 read_unlock(&state->ss->policy_rwlock); 2469 return rc; 2470 } 2471 2472 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask) 2473 { 2474 int i, fail = 0; 2475 2476 for (i = 0; i < 4; i++) 2477 if (addr[i] != (input[i] & mask[i])) { 2478 fail = 1; 2479 break; 2480 } 2481 2482 return !fail; 2483 } 2484 2485 /** 2486 * security_node_sid - Obtain the SID for a node (host). 2487 * @domain: communication domain aka address family 2488 * @addrp: address 2489 * @addrlen: address length in bytes 2490 * @out_sid: security identifier 2491 */ 2492 int security_node_sid(struct selinux_state *state, 2493 u16 domain, 2494 void *addrp, 2495 u32 addrlen, 2496 u32 *out_sid) 2497 { 2498 struct policydb *policydb; 2499 struct sidtab *sidtab; 2500 int rc; 2501 struct ocontext *c; 2502 2503 read_lock(&state->ss->policy_rwlock); 2504 2505 policydb = &state->ss->policydb; 2506 sidtab = state->ss->sidtab; 2507 2508 switch (domain) { 2509 case AF_INET: { 2510 u32 addr; 2511 2512 rc = -EINVAL; 2513 if (addrlen != sizeof(u32)) 2514 goto out; 2515 2516 addr = *((u32 *)addrp); 2517 2518 c = policydb->ocontexts[OCON_NODE]; 2519 while (c) { 2520 if (c->u.node.addr == (addr & c->u.node.mask)) 2521 break; 2522 c = c->next; 2523 } 2524 break; 2525 } 2526 2527 case AF_INET6: 2528 rc = -EINVAL; 2529 if (addrlen != sizeof(u64) * 2) 2530 goto out; 2531 c = policydb->ocontexts[OCON_NODE6]; 2532 while (c) { 2533 if (match_ipv6_addrmask(addrp, c->u.node6.addr, 2534 c->u.node6.mask)) 2535 break; 2536 c = c->next; 2537 } 2538 break; 2539 2540 default: 2541 rc = 0; 2542 *out_sid = SECINITSID_NODE; 2543 goto out; 2544 } 2545 2546 if (c) { 2547 if (!c->sid[0]) { 2548 rc = sidtab_context_to_sid(sidtab, 2549 &c->context[0], 2550 &c->sid[0]); 2551 if (rc) 2552 goto out; 2553 } 2554 *out_sid = c->sid[0]; 2555 } else { 2556 *out_sid = SECINITSID_NODE; 2557 } 2558 2559 rc = 0; 2560 out: 2561 read_unlock(&state->ss->policy_rwlock); 2562 return rc; 2563 } 2564 2565 #define SIDS_NEL 25 2566 2567 /** 2568 * security_get_user_sids - Obtain reachable SIDs for a user. 2569 * @fromsid: starting SID 2570 * @username: username 2571 * @sids: array of reachable SIDs for user 2572 * @nel: number of elements in @sids 2573 * 2574 * Generate the set of SIDs for legal security contexts 2575 * for a given user that can be reached by @fromsid. 2576 * Set *@sids to point to a dynamically allocated 2577 * array containing the set of SIDs. Set *@nel to the 2578 * number of elements in the array. 2579 */ 2580 2581 int security_get_user_sids(struct selinux_state *state, 2582 u32 fromsid, 2583 char *username, 2584 u32 **sids, 2585 u32 *nel) 2586 { 2587 struct policydb *policydb; 2588 struct sidtab *sidtab; 2589 struct context *fromcon, usercon; 2590 u32 *mysids = NULL, *mysids2, sid; 2591 u32 mynel = 0, maxnel = SIDS_NEL; 2592 struct user_datum *user; 2593 struct role_datum *role; 2594 struct ebitmap_node *rnode, *tnode; 2595 int rc = 0, i, j; 2596 2597 *sids = NULL; 2598 *nel = 0; 2599 2600 if (!selinux_initialized(state)) 2601 goto out; 2602 2603 read_lock(&state->ss->policy_rwlock); 2604 2605 policydb = &state->ss->policydb; 2606 sidtab = state->ss->sidtab; 2607 2608 context_init(&usercon); 2609 2610 rc = -EINVAL; 2611 fromcon = sidtab_search(sidtab, fromsid); 2612 if (!fromcon) 2613 goto out_unlock; 2614 2615 rc = -EINVAL; 2616 user = symtab_search(&policydb->p_users, username); 2617 if (!user) 2618 goto out_unlock; 2619 2620 usercon.user = user->value; 2621 2622 rc = -ENOMEM; 2623 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC); 2624 if (!mysids) 2625 goto out_unlock; 2626 2627 ebitmap_for_each_positive_bit(&user->roles, rnode, i) { 2628 role = policydb->role_val_to_struct[i]; 2629 usercon.role = i + 1; 2630 ebitmap_for_each_positive_bit(&role->types, tnode, j) { 2631 usercon.type = j + 1; 2632 2633 if (mls_setup_user_range(policydb, fromcon, user, 2634 &usercon)) 2635 continue; 2636 2637 rc = sidtab_context_to_sid(sidtab, &usercon, &sid); 2638 if (rc) 2639 goto out_unlock; 2640 if (mynel < maxnel) { 2641 mysids[mynel++] = sid; 2642 } else { 2643 rc = -ENOMEM; 2644 maxnel += SIDS_NEL; 2645 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC); 2646 if (!mysids2) 2647 goto out_unlock; 2648 memcpy(mysids2, mysids, mynel * sizeof(*mysids2)); 2649 kfree(mysids); 2650 mysids = mysids2; 2651 mysids[mynel++] = sid; 2652 } 2653 } 2654 } 2655 rc = 0; 2656 out_unlock: 2657 read_unlock(&state->ss->policy_rwlock); 2658 if (rc || !mynel) { 2659 kfree(mysids); 2660 goto out; 2661 } 2662 2663 rc = -ENOMEM; 2664 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL); 2665 if (!mysids2) { 2666 kfree(mysids); 2667 goto out; 2668 } 2669 for (i = 0, j = 0; i < mynel; i++) { 2670 struct av_decision dummy_avd; 2671 rc = avc_has_perm_noaudit(state, 2672 fromsid, mysids[i], 2673 SECCLASS_PROCESS, /* kernel value */ 2674 PROCESS__TRANSITION, AVC_STRICT, 2675 &dummy_avd); 2676 if (!rc) 2677 mysids2[j++] = mysids[i]; 2678 cond_resched(); 2679 } 2680 rc = 0; 2681 kfree(mysids); 2682 *sids = mysids2; 2683 *nel = j; 2684 out: 2685 return rc; 2686 } 2687 2688 /** 2689 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem 2690 * @fstype: filesystem type 2691 * @path: path from root of mount 2692 * @sclass: file security class 2693 * @sid: SID for path 2694 * 2695 * Obtain a SID to use for a file in a filesystem that 2696 * cannot support xattr or use a fixed labeling behavior like 2697 * transition SIDs or task SIDs. 2698 * 2699 * The caller must acquire the policy_rwlock before calling this function. 2700 */ 2701 static inline int __security_genfs_sid(struct selinux_state *state, 2702 const char *fstype, 2703 char *path, 2704 u16 orig_sclass, 2705 u32 *sid) 2706 { 2707 struct policydb *policydb = &state->ss->policydb; 2708 struct sidtab *sidtab = state->ss->sidtab; 2709 int len; 2710 u16 sclass; 2711 struct genfs *genfs; 2712 struct ocontext *c; 2713 int rc, cmp = 0; 2714 2715 while (path[0] == '/' && path[1] == '/') 2716 path++; 2717 2718 sclass = unmap_class(&state->ss->map, orig_sclass); 2719 *sid = SECINITSID_UNLABELED; 2720 2721 for (genfs = policydb->genfs; genfs; genfs = genfs->next) { 2722 cmp = strcmp(fstype, genfs->fstype); 2723 if (cmp <= 0) 2724 break; 2725 } 2726 2727 rc = -ENOENT; 2728 if (!genfs || cmp) 2729 goto out; 2730 2731 for (c = genfs->head; c; c = c->next) { 2732 len = strlen(c->u.name); 2733 if ((!c->v.sclass || sclass == c->v.sclass) && 2734 (strncmp(c->u.name, path, len) == 0)) 2735 break; 2736 } 2737 2738 rc = -ENOENT; 2739 if (!c) 2740 goto out; 2741 2742 if (!c->sid[0]) { 2743 rc = sidtab_context_to_sid(sidtab, &c->context[0], &c->sid[0]); 2744 if (rc) 2745 goto out; 2746 } 2747 2748 *sid = c->sid[0]; 2749 rc = 0; 2750 out: 2751 return rc; 2752 } 2753 2754 /** 2755 * security_genfs_sid - Obtain a SID for a file in a filesystem 2756 * @fstype: filesystem type 2757 * @path: path from root of mount 2758 * @sclass: file security class 2759 * @sid: SID for path 2760 * 2761 * Acquire policy_rwlock before calling __security_genfs_sid() and release 2762 * it afterward. 2763 */ 2764 int security_genfs_sid(struct selinux_state *state, 2765 const char *fstype, 2766 char *path, 2767 u16 orig_sclass, 2768 u32 *sid) 2769 { 2770 int retval; 2771 2772 read_lock(&state->ss->policy_rwlock); 2773 retval = __security_genfs_sid(state, fstype, path, orig_sclass, sid); 2774 read_unlock(&state->ss->policy_rwlock); 2775 return retval; 2776 } 2777 2778 /** 2779 * security_fs_use - Determine how to handle labeling for a filesystem. 2780 * @sb: superblock in question 2781 */ 2782 int security_fs_use(struct selinux_state *state, struct super_block *sb) 2783 { 2784 struct policydb *policydb; 2785 struct sidtab *sidtab; 2786 int rc = 0; 2787 struct ocontext *c; 2788 struct superblock_security_struct *sbsec = sb->s_security; 2789 const char *fstype = sb->s_type->name; 2790 2791 read_lock(&state->ss->policy_rwlock); 2792 2793 policydb = &state->ss->policydb; 2794 sidtab = state->ss->sidtab; 2795 2796 c = policydb->ocontexts[OCON_FSUSE]; 2797 while (c) { 2798 if (strcmp(fstype, c->u.name) == 0) 2799 break; 2800 c = c->next; 2801 } 2802 2803 if (c) { 2804 sbsec->behavior = c->v.behavior; 2805 if (!c->sid[0]) { 2806 rc = sidtab_context_to_sid(sidtab, &c->context[0], 2807 &c->sid[0]); 2808 if (rc) 2809 goto out; 2810 } 2811 sbsec->sid = c->sid[0]; 2812 } else { 2813 rc = __security_genfs_sid(state, fstype, "/", SECCLASS_DIR, 2814 &sbsec->sid); 2815 if (rc) { 2816 sbsec->behavior = SECURITY_FS_USE_NONE; 2817 rc = 0; 2818 } else { 2819 sbsec->behavior = SECURITY_FS_USE_GENFS; 2820 } 2821 } 2822 2823 out: 2824 read_unlock(&state->ss->policy_rwlock); 2825 return rc; 2826 } 2827 2828 int security_get_bools(struct selinux_state *state, 2829 u32 *len, char ***names, int **values) 2830 { 2831 struct policydb *policydb; 2832 u32 i; 2833 int rc; 2834 2835 if (!selinux_initialized(state)) { 2836 *len = 0; 2837 *names = NULL; 2838 *values = NULL; 2839 return 0; 2840 } 2841 2842 read_lock(&state->ss->policy_rwlock); 2843 2844 policydb = &state->ss->policydb; 2845 2846 *names = NULL; 2847 *values = NULL; 2848 2849 rc = 0; 2850 *len = policydb->p_bools.nprim; 2851 if (!*len) 2852 goto out; 2853 2854 rc = -ENOMEM; 2855 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC); 2856 if (!*names) 2857 goto err; 2858 2859 rc = -ENOMEM; 2860 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC); 2861 if (!*values) 2862 goto err; 2863 2864 for (i = 0; i < *len; i++) { 2865 (*values)[i] = policydb->bool_val_to_struct[i]->state; 2866 2867 rc = -ENOMEM; 2868 (*names)[i] = kstrdup(sym_name(policydb, SYM_BOOLS, i), 2869 GFP_ATOMIC); 2870 if (!(*names)[i]) 2871 goto err; 2872 } 2873 rc = 0; 2874 out: 2875 read_unlock(&state->ss->policy_rwlock); 2876 return rc; 2877 err: 2878 if (*names) { 2879 for (i = 0; i < *len; i++) 2880 kfree((*names)[i]); 2881 kfree(*names); 2882 } 2883 kfree(*values); 2884 *len = 0; 2885 *names = NULL; 2886 *values = NULL; 2887 goto out; 2888 } 2889 2890 2891 int security_set_bools(struct selinux_state *state, u32 len, int *values) 2892 { 2893 struct policydb *policydb; 2894 int rc; 2895 u32 i, lenp, seqno = 0; 2896 2897 write_lock_irq(&state->ss->policy_rwlock); 2898 2899 policydb = &state->ss->policydb; 2900 2901 rc = -EFAULT; 2902 lenp = policydb->p_bools.nprim; 2903 if (len != lenp) 2904 goto out; 2905 2906 for (i = 0; i < len; i++) { 2907 if (!!values[i] != policydb->bool_val_to_struct[i]->state) { 2908 audit_log(audit_context(), GFP_ATOMIC, 2909 AUDIT_MAC_CONFIG_CHANGE, 2910 "bool=%s val=%d old_val=%d auid=%u ses=%u", 2911 sym_name(policydb, SYM_BOOLS, i), 2912 !!values[i], 2913 policydb->bool_val_to_struct[i]->state, 2914 from_kuid(&init_user_ns, audit_get_loginuid(current)), 2915 audit_get_sessionid(current)); 2916 } 2917 if (values[i]) 2918 policydb->bool_val_to_struct[i]->state = 1; 2919 else 2920 policydb->bool_val_to_struct[i]->state = 0; 2921 } 2922 2923 evaluate_cond_nodes(policydb); 2924 2925 seqno = ++state->ss->latest_granting; 2926 rc = 0; 2927 out: 2928 write_unlock_irq(&state->ss->policy_rwlock); 2929 if (!rc) { 2930 avc_ss_reset(state->avc, seqno); 2931 selnl_notify_policyload(seqno); 2932 selinux_status_update_policyload(state, seqno); 2933 selinux_xfrm_notify_policyload(); 2934 } 2935 return rc; 2936 } 2937 2938 int security_get_bool_value(struct selinux_state *state, 2939 u32 index) 2940 { 2941 struct policydb *policydb; 2942 int rc; 2943 u32 len; 2944 2945 read_lock(&state->ss->policy_rwlock); 2946 2947 policydb = &state->ss->policydb; 2948 2949 rc = -EFAULT; 2950 len = policydb->p_bools.nprim; 2951 if (index >= len) 2952 goto out; 2953 2954 rc = policydb->bool_val_to_struct[index]->state; 2955 out: 2956 read_unlock(&state->ss->policy_rwlock); 2957 return rc; 2958 } 2959 2960 static int security_preserve_bools(struct selinux_state *state, 2961 struct policydb *policydb) 2962 { 2963 int rc, *bvalues = NULL; 2964 char **bnames = NULL; 2965 struct cond_bool_datum *booldatum; 2966 u32 i, nbools = 0; 2967 2968 rc = security_get_bools(state, &nbools, &bnames, &bvalues); 2969 if (rc) 2970 goto out; 2971 for (i = 0; i < nbools; i++) { 2972 booldatum = symtab_search(&policydb->p_bools, bnames[i]); 2973 if (booldatum) 2974 booldatum->state = bvalues[i]; 2975 } 2976 evaluate_cond_nodes(policydb); 2977 2978 out: 2979 if (bnames) { 2980 for (i = 0; i < nbools; i++) 2981 kfree(bnames[i]); 2982 } 2983 kfree(bnames); 2984 kfree(bvalues); 2985 return rc; 2986 } 2987 2988 /* 2989 * security_sid_mls_copy() - computes a new sid based on the given 2990 * sid and the mls portion of mls_sid. 2991 */ 2992 int security_sid_mls_copy(struct selinux_state *state, 2993 u32 sid, u32 mls_sid, u32 *new_sid) 2994 { 2995 struct policydb *policydb = &state->ss->policydb; 2996 struct sidtab *sidtab = state->ss->sidtab; 2997 struct context *context1; 2998 struct context *context2; 2999 struct context newcon; 3000 char *s; 3001 u32 len; 3002 int rc; 3003 3004 rc = 0; 3005 if (!selinux_initialized(state) || !policydb->mls_enabled) { 3006 *new_sid = sid; 3007 goto out; 3008 } 3009 3010 context_init(&newcon); 3011 3012 read_lock(&state->ss->policy_rwlock); 3013 3014 rc = -EINVAL; 3015 context1 = sidtab_search(sidtab, sid); 3016 if (!context1) { 3017 pr_err("SELinux: %s: unrecognized SID %d\n", 3018 __func__, sid); 3019 goto out_unlock; 3020 } 3021 3022 rc = -EINVAL; 3023 context2 = sidtab_search(sidtab, mls_sid); 3024 if (!context2) { 3025 pr_err("SELinux: %s: unrecognized SID %d\n", 3026 __func__, mls_sid); 3027 goto out_unlock; 3028 } 3029 3030 newcon.user = context1->user; 3031 newcon.role = context1->role; 3032 newcon.type = context1->type; 3033 rc = mls_context_cpy(&newcon, context2); 3034 if (rc) 3035 goto out_unlock; 3036 3037 /* Check the validity of the new context. */ 3038 if (!policydb_context_isvalid(policydb, &newcon)) { 3039 rc = convert_context_handle_invalid_context(state, &newcon); 3040 if (rc) { 3041 if (!context_struct_to_string(policydb, &newcon, &s, 3042 &len)) { 3043 struct audit_buffer *ab; 3044 3045 ab = audit_log_start(audit_context(), 3046 GFP_ATOMIC, 3047 AUDIT_SELINUX_ERR); 3048 audit_log_format(ab, 3049 "op=security_sid_mls_copy invalid_context="); 3050 /* don't record NUL with untrusted strings */ 3051 audit_log_n_untrustedstring(ab, s, len - 1); 3052 audit_log_end(ab); 3053 kfree(s); 3054 } 3055 goto out_unlock; 3056 } 3057 } 3058 rc = sidtab_context_to_sid(sidtab, &newcon, new_sid); 3059 out_unlock: 3060 read_unlock(&state->ss->policy_rwlock); 3061 context_destroy(&newcon); 3062 out: 3063 return rc; 3064 } 3065 3066 /** 3067 * security_net_peersid_resolve - Compare and resolve two network peer SIDs 3068 * @nlbl_sid: NetLabel SID 3069 * @nlbl_type: NetLabel labeling protocol type 3070 * @xfrm_sid: XFRM SID 3071 * 3072 * Description: 3073 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be 3074 * resolved into a single SID it is returned via @peer_sid and the function 3075 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function 3076 * returns a negative value. A table summarizing the behavior is below: 3077 * 3078 * | function return | @sid 3079 * ------------------------------+-----------------+----------------- 3080 * no peer labels | 0 | SECSID_NULL 3081 * single peer label | 0 | <peer_label> 3082 * multiple, consistent labels | 0 | <peer_label> 3083 * multiple, inconsistent labels | -<errno> | SECSID_NULL 3084 * 3085 */ 3086 int security_net_peersid_resolve(struct selinux_state *state, 3087 u32 nlbl_sid, u32 nlbl_type, 3088 u32 xfrm_sid, 3089 u32 *peer_sid) 3090 { 3091 struct policydb *policydb = &state->ss->policydb; 3092 struct sidtab *sidtab = state->ss->sidtab; 3093 int rc; 3094 struct context *nlbl_ctx; 3095 struct context *xfrm_ctx; 3096 3097 *peer_sid = SECSID_NULL; 3098 3099 /* handle the common (which also happens to be the set of easy) cases 3100 * right away, these two if statements catch everything involving a 3101 * single or absent peer SID/label */ 3102 if (xfrm_sid == SECSID_NULL) { 3103 *peer_sid = nlbl_sid; 3104 return 0; 3105 } 3106 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label 3107 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label 3108 * is present */ 3109 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) { 3110 *peer_sid = xfrm_sid; 3111 return 0; 3112 } 3113 3114 /* 3115 * We don't need to check initialized here since the only way both 3116 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the 3117 * security server was initialized and state->initialized was true. 3118 */ 3119 if (!policydb->mls_enabled) 3120 return 0; 3121 3122 read_lock(&state->ss->policy_rwlock); 3123 3124 rc = -EINVAL; 3125 nlbl_ctx = sidtab_search(sidtab, nlbl_sid); 3126 if (!nlbl_ctx) { 3127 pr_err("SELinux: %s: unrecognized SID %d\n", 3128 __func__, nlbl_sid); 3129 goto out; 3130 } 3131 rc = -EINVAL; 3132 xfrm_ctx = sidtab_search(sidtab, xfrm_sid); 3133 if (!xfrm_ctx) { 3134 pr_err("SELinux: %s: unrecognized SID %d\n", 3135 __func__, xfrm_sid); 3136 goto out; 3137 } 3138 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES); 3139 if (rc) 3140 goto out; 3141 3142 /* at present NetLabel SIDs/labels really only carry MLS 3143 * information so if the MLS portion of the NetLabel SID 3144 * matches the MLS portion of the labeled XFRM SID/label 3145 * then pass along the XFRM SID as it is the most 3146 * expressive */ 3147 *peer_sid = xfrm_sid; 3148 out: 3149 read_unlock(&state->ss->policy_rwlock); 3150 return rc; 3151 } 3152 3153 static int get_classes_callback(void *k, void *d, void *args) 3154 { 3155 struct class_datum *datum = d; 3156 char *name = k, **classes = args; 3157 int value = datum->value - 1; 3158 3159 classes[value] = kstrdup(name, GFP_ATOMIC); 3160 if (!classes[value]) 3161 return -ENOMEM; 3162 3163 return 0; 3164 } 3165 3166 int security_get_classes(struct selinux_state *state, 3167 char ***classes, int *nclasses) 3168 { 3169 struct policydb *policydb = &state->ss->policydb; 3170 int rc; 3171 3172 if (!selinux_initialized(state)) { 3173 *nclasses = 0; 3174 *classes = NULL; 3175 return 0; 3176 } 3177 3178 read_lock(&state->ss->policy_rwlock); 3179 3180 rc = -ENOMEM; 3181 *nclasses = policydb->p_classes.nprim; 3182 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC); 3183 if (!*classes) 3184 goto out; 3185 3186 rc = hashtab_map(&policydb->p_classes.table, get_classes_callback, 3187 *classes); 3188 if (rc) { 3189 int i; 3190 for (i = 0; i < *nclasses; i++) 3191 kfree((*classes)[i]); 3192 kfree(*classes); 3193 } 3194 3195 out: 3196 read_unlock(&state->ss->policy_rwlock); 3197 return rc; 3198 } 3199 3200 static int get_permissions_callback(void *k, void *d, void *args) 3201 { 3202 struct perm_datum *datum = d; 3203 char *name = k, **perms = args; 3204 int value = datum->value - 1; 3205 3206 perms[value] = kstrdup(name, GFP_ATOMIC); 3207 if (!perms[value]) 3208 return -ENOMEM; 3209 3210 return 0; 3211 } 3212 3213 int security_get_permissions(struct selinux_state *state, 3214 char *class, char ***perms, int *nperms) 3215 { 3216 struct policydb *policydb = &state->ss->policydb; 3217 int rc, i; 3218 struct class_datum *match; 3219 3220 read_lock(&state->ss->policy_rwlock); 3221 3222 rc = -EINVAL; 3223 match = symtab_search(&policydb->p_classes, class); 3224 if (!match) { 3225 pr_err("SELinux: %s: unrecognized class %s\n", 3226 __func__, class); 3227 goto out; 3228 } 3229 3230 rc = -ENOMEM; 3231 *nperms = match->permissions.nprim; 3232 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC); 3233 if (!*perms) 3234 goto out; 3235 3236 if (match->comdatum) { 3237 rc = hashtab_map(&match->comdatum->permissions.table, 3238 get_permissions_callback, *perms); 3239 if (rc) 3240 goto err; 3241 } 3242 3243 rc = hashtab_map(&match->permissions.table, get_permissions_callback, 3244 *perms); 3245 if (rc) 3246 goto err; 3247 3248 out: 3249 read_unlock(&state->ss->policy_rwlock); 3250 return rc; 3251 3252 err: 3253 read_unlock(&state->ss->policy_rwlock); 3254 for (i = 0; i < *nperms; i++) 3255 kfree((*perms)[i]); 3256 kfree(*perms); 3257 return rc; 3258 } 3259 3260 int security_get_reject_unknown(struct selinux_state *state) 3261 { 3262 return state->ss->policydb.reject_unknown; 3263 } 3264 3265 int security_get_allow_unknown(struct selinux_state *state) 3266 { 3267 return state->ss->policydb.allow_unknown; 3268 } 3269 3270 /** 3271 * security_policycap_supported - Check for a specific policy capability 3272 * @req_cap: capability 3273 * 3274 * Description: 3275 * This function queries the currently loaded policy to see if it supports the 3276 * capability specified by @req_cap. Returns true (1) if the capability is 3277 * supported, false (0) if it isn't supported. 3278 * 3279 */ 3280 int security_policycap_supported(struct selinux_state *state, 3281 unsigned int req_cap) 3282 { 3283 struct policydb *policydb = &state->ss->policydb; 3284 int rc; 3285 3286 read_lock(&state->ss->policy_rwlock); 3287 rc = ebitmap_get_bit(&policydb->policycaps, req_cap); 3288 read_unlock(&state->ss->policy_rwlock); 3289 3290 return rc; 3291 } 3292 3293 struct selinux_audit_rule { 3294 u32 au_seqno; 3295 struct context au_ctxt; 3296 }; 3297 3298 void selinux_audit_rule_free(void *vrule) 3299 { 3300 struct selinux_audit_rule *rule = vrule; 3301 3302 if (rule) { 3303 context_destroy(&rule->au_ctxt); 3304 kfree(rule); 3305 } 3306 } 3307 3308 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) 3309 { 3310 struct selinux_state *state = &selinux_state; 3311 struct policydb *policydb = &state->ss->policydb; 3312 struct selinux_audit_rule *tmprule; 3313 struct role_datum *roledatum; 3314 struct type_datum *typedatum; 3315 struct user_datum *userdatum; 3316 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule; 3317 int rc = 0; 3318 3319 *rule = NULL; 3320 3321 if (!selinux_initialized(state)) 3322 return -EOPNOTSUPP; 3323 3324 switch (field) { 3325 case AUDIT_SUBJ_USER: 3326 case AUDIT_SUBJ_ROLE: 3327 case AUDIT_SUBJ_TYPE: 3328 case AUDIT_OBJ_USER: 3329 case AUDIT_OBJ_ROLE: 3330 case AUDIT_OBJ_TYPE: 3331 /* only 'equals' and 'not equals' fit user, role, and type */ 3332 if (op != Audit_equal && op != Audit_not_equal) 3333 return -EINVAL; 3334 break; 3335 case AUDIT_SUBJ_SEN: 3336 case AUDIT_SUBJ_CLR: 3337 case AUDIT_OBJ_LEV_LOW: 3338 case AUDIT_OBJ_LEV_HIGH: 3339 /* we do not allow a range, indicated by the presence of '-' */ 3340 if (strchr(rulestr, '-')) 3341 return -EINVAL; 3342 break; 3343 default: 3344 /* only the above fields are valid */ 3345 return -EINVAL; 3346 } 3347 3348 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL); 3349 if (!tmprule) 3350 return -ENOMEM; 3351 3352 context_init(&tmprule->au_ctxt); 3353 3354 read_lock(&state->ss->policy_rwlock); 3355 3356 tmprule->au_seqno = state->ss->latest_granting; 3357 3358 switch (field) { 3359 case AUDIT_SUBJ_USER: 3360 case AUDIT_OBJ_USER: 3361 rc = -EINVAL; 3362 userdatum = symtab_search(&policydb->p_users, rulestr); 3363 if (!userdatum) 3364 goto out; 3365 tmprule->au_ctxt.user = userdatum->value; 3366 break; 3367 case AUDIT_SUBJ_ROLE: 3368 case AUDIT_OBJ_ROLE: 3369 rc = -EINVAL; 3370 roledatum = symtab_search(&policydb->p_roles, rulestr); 3371 if (!roledatum) 3372 goto out; 3373 tmprule->au_ctxt.role = roledatum->value; 3374 break; 3375 case AUDIT_SUBJ_TYPE: 3376 case AUDIT_OBJ_TYPE: 3377 rc = -EINVAL; 3378 typedatum = symtab_search(&policydb->p_types, rulestr); 3379 if (!typedatum) 3380 goto out; 3381 tmprule->au_ctxt.type = typedatum->value; 3382 break; 3383 case AUDIT_SUBJ_SEN: 3384 case AUDIT_SUBJ_CLR: 3385 case AUDIT_OBJ_LEV_LOW: 3386 case AUDIT_OBJ_LEV_HIGH: 3387 rc = mls_from_string(policydb, rulestr, &tmprule->au_ctxt, 3388 GFP_ATOMIC); 3389 if (rc) 3390 goto out; 3391 break; 3392 } 3393 rc = 0; 3394 out: 3395 read_unlock(&state->ss->policy_rwlock); 3396 3397 if (rc) { 3398 selinux_audit_rule_free(tmprule); 3399 tmprule = NULL; 3400 } 3401 3402 *rule = tmprule; 3403 3404 return rc; 3405 } 3406 3407 /* Check to see if the rule contains any selinux fields */ 3408 int selinux_audit_rule_known(struct audit_krule *rule) 3409 { 3410 int i; 3411 3412 for (i = 0; i < rule->field_count; i++) { 3413 struct audit_field *f = &rule->fields[i]; 3414 switch (f->type) { 3415 case AUDIT_SUBJ_USER: 3416 case AUDIT_SUBJ_ROLE: 3417 case AUDIT_SUBJ_TYPE: 3418 case AUDIT_SUBJ_SEN: 3419 case AUDIT_SUBJ_CLR: 3420 case AUDIT_OBJ_USER: 3421 case AUDIT_OBJ_ROLE: 3422 case AUDIT_OBJ_TYPE: 3423 case AUDIT_OBJ_LEV_LOW: 3424 case AUDIT_OBJ_LEV_HIGH: 3425 return 1; 3426 } 3427 } 3428 3429 return 0; 3430 } 3431 3432 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule) 3433 { 3434 struct selinux_state *state = &selinux_state; 3435 struct context *ctxt; 3436 struct mls_level *level; 3437 struct selinux_audit_rule *rule = vrule; 3438 int match = 0; 3439 3440 if (unlikely(!rule)) { 3441 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n"); 3442 return -ENOENT; 3443 } 3444 3445 read_lock(&state->ss->policy_rwlock); 3446 3447 if (rule->au_seqno < state->ss->latest_granting) { 3448 match = -ESTALE; 3449 goto out; 3450 } 3451 3452 ctxt = sidtab_search(state->ss->sidtab, sid); 3453 if (unlikely(!ctxt)) { 3454 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n", 3455 sid); 3456 match = -ENOENT; 3457 goto out; 3458 } 3459 3460 /* a field/op pair that is not caught here will simply fall through 3461 without a match */ 3462 switch (field) { 3463 case AUDIT_SUBJ_USER: 3464 case AUDIT_OBJ_USER: 3465 switch (op) { 3466 case Audit_equal: 3467 match = (ctxt->user == rule->au_ctxt.user); 3468 break; 3469 case Audit_not_equal: 3470 match = (ctxt->user != rule->au_ctxt.user); 3471 break; 3472 } 3473 break; 3474 case AUDIT_SUBJ_ROLE: 3475 case AUDIT_OBJ_ROLE: 3476 switch (op) { 3477 case Audit_equal: 3478 match = (ctxt->role == rule->au_ctxt.role); 3479 break; 3480 case Audit_not_equal: 3481 match = (ctxt->role != rule->au_ctxt.role); 3482 break; 3483 } 3484 break; 3485 case AUDIT_SUBJ_TYPE: 3486 case AUDIT_OBJ_TYPE: 3487 switch (op) { 3488 case Audit_equal: 3489 match = (ctxt->type == rule->au_ctxt.type); 3490 break; 3491 case Audit_not_equal: 3492 match = (ctxt->type != rule->au_ctxt.type); 3493 break; 3494 } 3495 break; 3496 case AUDIT_SUBJ_SEN: 3497 case AUDIT_SUBJ_CLR: 3498 case AUDIT_OBJ_LEV_LOW: 3499 case AUDIT_OBJ_LEV_HIGH: 3500 level = ((field == AUDIT_SUBJ_SEN || 3501 field == AUDIT_OBJ_LEV_LOW) ? 3502 &ctxt->range.level[0] : &ctxt->range.level[1]); 3503 switch (op) { 3504 case Audit_equal: 3505 match = mls_level_eq(&rule->au_ctxt.range.level[0], 3506 level); 3507 break; 3508 case Audit_not_equal: 3509 match = !mls_level_eq(&rule->au_ctxt.range.level[0], 3510 level); 3511 break; 3512 case Audit_lt: 3513 match = (mls_level_dom(&rule->au_ctxt.range.level[0], 3514 level) && 3515 !mls_level_eq(&rule->au_ctxt.range.level[0], 3516 level)); 3517 break; 3518 case Audit_le: 3519 match = mls_level_dom(&rule->au_ctxt.range.level[0], 3520 level); 3521 break; 3522 case Audit_gt: 3523 match = (mls_level_dom(level, 3524 &rule->au_ctxt.range.level[0]) && 3525 !mls_level_eq(level, 3526 &rule->au_ctxt.range.level[0])); 3527 break; 3528 case Audit_ge: 3529 match = mls_level_dom(level, 3530 &rule->au_ctxt.range.level[0]); 3531 break; 3532 } 3533 } 3534 3535 out: 3536 read_unlock(&state->ss->policy_rwlock); 3537 return match; 3538 } 3539 3540 static int (*aurule_callback)(void) = audit_update_lsm_rules; 3541 3542 static int aurule_avc_callback(u32 event) 3543 { 3544 int err = 0; 3545 3546 if (event == AVC_CALLBACK_RESET && aurule_callback) 3547 err = aurule_callback(); 3548 return err; 3549 } 3550 3551 static int __init aurule_init(void) 3552 { 3553 int err; 3554 3555 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET); 3556 if (err) 3557 panic("avc_add_callback() failed, error %d\n", err); 3558 3559 return err; 3560 } 3561 __initcall(aurule_init); 3562 3563 #ifdef CONFIG_NETLABEL 3564 /** 3565 * security_netlbl_cache_add - Add an entry to the NetLabel cache 3566 * @secattr: the NetLabel packet security attributes 3567 * @sid: the SELinux SID 3568 * 3569 * Description: 3570 * Attempt to cache the context in @ctx, which was derived from the packet in 3571 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has 3572 * already been initialized. 3573 * 3574 */ 3575 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr, 3576 u32 sid) 3577 { 3578 u32 *sid_cache; 3579 3580 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC); 3581 if (sid_cache == NULL) 3582 return; 3583 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC); 3584 if (secattr->cache == NULL) { 3585 kfree(sid_cache); 3586 return; 3587 } 3588 3589 *sid_cache = sid; 3590 secattr->cache->free = kfree; 3591 secattr->cache->data = sid_cache; 3592 secattr->flags |= NETLBL_SECATTR_CACHE; 3593 } 3594 3595 /** 3596 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID 3597 * @secattr: the NetLabel packet security attributes 3598 * @sid: the SELinux SID 3599 * 3600 * Description: 3601 * Convert the given NetLabel security attributes in @secattr into a 3602 * SELinux SID. If the @secattr field does not contain a full SELinux 3603 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the 3604 * 'cache' field of @secattr is set and the CACHE flag is set; this is to 3605 * allow the @secattr to be used by NetLabel to cache the secattr to SID 3606 * conversion for future lookups. Returns zero on success, negative values on 3607 * failure. 3608 * 3609 */ 3610 int security_netlbl_secattr_to_sid(struct selinux_state *state, 3611 struct netlbl_lsm_secattr *secattr, 3612 u32 *sid) 3613 { 3614 struct policydb *policydb = &state->ss->policydb; 3615 struct sidtab *sidtab = state->ss->sidtab; 3616 int rc; 3617 struct context *ctx; 3618 struct context ctx_new; 3619 3620 if (!selinux_initialized(state)) { 3621 *sid = SECSID_NULL; 3622 return 0; 3623 } 3624 3625 read_lock(&state->ss->policy_rwlock); 3626 3627 if (secattr->flags & NETLBL_SECATTR_CACHE) 3628 *sid = *(u32 *)secattr->cache->data; 3629 else if (secattr->flags & NETLBL_SECATTR_SECID) 3630 *sid = secattr->attr.secid; 3631 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) { 3632 rc = -EIDRM; 3633 ctx = sidtab_search(sidtab, SECINITSID_NETMSG); 3634 if (ctx == NULL) 3635 goto out; 3636 3637 context_init(&ctx_new); 3638 ctx_new.user = ctx->user; 3639 ctx_new.role = ctx->role; 3640 ctx_new.type = ctx->type; 3641 mls_import_netlbl_lvl(policydb, &ctx_new, secattr); 3642 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 3643 rc = mls_import_netlbl_cat(policydb, &ctx_new, secattr); 3644 if (rc) 3645 goto out; 3646 } 3647 rc = -EIDRM; 3648 if (!mls_context_isvalid(policydb, &ctx_new)) 3649 goto out_free; 3650 3651 rc = sidtab_context_to_sid(sidtab, &ctx_new, sid); 3652 if (rc) 3653 goto out_free; 3654 3655 security_netlbl_cache_add(secattr, *sid); 3656 3657 ebitmap_destroy(&ctx_new.range.level[0].cat); 3658 } else 3659 *sid = SECSID_NULL; 3660 3661 read_unlock(&state->ss->policy_rwlock); 3662 return 0; 3663 out_free: 3664 ebitmap_destroy(&ctx_new.range.level[0].cat); 3665 out: 3666 read_unlock(&state->ss->policy_rwlock); 3667 return rc; 3668 } 3669 3670 /** 3671 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr 3672 * @sid: the SELinux SID 3673 * @secattr: the NetLabel packet security attributes 3674 * 3675 * Description: 3676 * Convert the given SELinux SID in @sid into a NetLabel security attribute. 3677 * Returns zero on success, negative values on failure. 3678 * 3679 */ 3680 int security_netlbl_sid_to_secattr(struct selinux_state *state, 3681 u32 sid, struct netlbl_lsm_secattr *secattr) 3682 { 3683 struct policydb *policydb = &state->ss->policydb; 3684 int rc; 3685 struct context *ctx; 3686 3687 if (!selinux_initialized(state)) 3688 return 0; 3689 3690 read_lock(&state->ss->policy_rwlock); 3691 3692 rc = -ENOENT; 3693 ctx = sidtab_search(state->ss->sidtab, sid); 3694 if (ctx == NULL) 3695 goto out; 3696 3697 rc = -ENOMEM; 3698 secattr->domain = kstrdup(sym_name(policydb, SYM_TYPES, ctx->type - 1), 3699 GFP_ATOMIC); 3700 if (secattr->domain == NULL) 3701 goto out; 3702 3703 secattr->attr.secid = sid; 3704 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID; 3705 mls_export_netlbl_lvl(policydb, ctx, secattr); 3706 rc = mls_export_netlbl_cat(policydb, ctx, secattr); 3707 out: 3708 read_unlock(&state->ss->policy_rwlock); 3709 return rc; 3710 } 3711 #endif /* CONFIG_NETLABEL */ 3712 3713 /** 3714 * security_read_policy - read the policy. 3715 * @data: binary policy data 3716 * @len: length of data in bytes 3717 * 3718 */ 3719 int security_read_policy(struct selinux_state *state, 3720 void **data, size_t *len) 3721 { 3722 struct policydb *policydb = &state->ss->policydb; 3723 int rc; 3724 struct policy_file fp; 3725 3726 if (!selinux_initialized(state)) 3727 return -EINVAL; 3728 3729 *len = security_policydb_len(state); 3730 3731 *data = vmalloc_user(*len); 3732 if (!*data) 3733 return -ENOMEM; 3734 3735 fp.data = *data; 3736 fp.len = *len; 3737 3738 read_lock(&state->ss->policy_rwlock); 3739 rc = policydb_write(policydb, &fp); 3740 read_unlock(&state->ss->policy_rwlock); 3741 3742 if (rc) 3743 return rc; 3744 3745 *len = (unsigned long)fp.data - (unsigned long)*data; 3746 return 0; 3747 3748 } 3749