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