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