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