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