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