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