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