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