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