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