1 /* 2 * Implementation of the security services. 3 * 4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil> 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.moore@hp.com> 17 * 18 * Added support for NetLabel 19 * 20 * Updated: Chad Sellers <csellers@tresys.com> 21 * 22 * Added validation of kernel classes and permissions 23 * 24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P. 25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. 26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC 27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> 28 * This program is free software; you can redistribute it and/or modify 29 * it under the terms of the GNU General Public License as published by 30 * the Free Software Foundation, version 2. 31 */ 32 #include <linux/kernel.h> 33 #include <linux/slab.h> 34 #include <linux/string.h> 35 #include <linux/spinlock.h> 36 #include <linux/rcupdate.h> 37 #include <linux/errno.h> 38 #include <linux/in.h> 39 #include <linux/sched.h> 40 #include <linux/audit.h> 41 #include <linux/mutex.h> 42 #include <net/sock.h> 43 #include <net/netlabel.h> 44 45 #include "flask.h" 46 #include "avc.h" 47 #include "avc_ss.h" 48 #include "security.h" 49 #include "context.h" 50 #include "policydb.h" 51 #include "sidtab.h" 52 #include "services.h" 53 #include "conditional.h" 54 #include "mls.h" 55 #include "objsec.h" 56 #include "selinux_netlabel.h" 57 58 extern void selnl_notify_policyload(u32 seqno); 59 unsigned int policydb_loaded_version; 60 61 /* 62 * This is declared in avc.c 63 */ 64 extern const struct selinux_class_perm selinux_class_perm; 65 66 static DEFINE_RWLOCK(policy_rwlock); 67 #define POLICY_RDLOCK read_lock(&policy_rwlock) 68 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock) 69 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock) 70 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock) 71 72 static DEFINE_MUTEX(load_mutex); 73 #define LOAD_LOCK mutex_lock(&load_mutex) 74 #define LOAD_UNLOCK mutex_unlock(&load_mutex) 75 76 static struct sidtab sidtab; 77 struct policydb policydb; 78 int ss_initialized = 0; 79 80 /* 81 * The largest sequence number that has been used when 82 * providing an access decision to the access vector cache. 83 * The sequence number only changes when a policy change 84 * occurs. 85 */ 86 static u32 latest_granting = 0; 87 88 /* Forward declaration. */ 89 static int context_struct_to_string(struct context *context, char **scontext, 90 u32 *scontext_len); 91 92 /* 93 * Return the boolean value of a constraint expression 94 * when it is applied to the specified source and target 95 * security contexts. 96 * 97 * xcontext is a special beast... It is used by the validatetrans rules 98 * only. For these rules, scontext is the context before the transition, 99 * tcontext is the context after the transition, and xcontext is the context 100 * of the process performing the transition. All other callers of 101 * constraint_expr_eval should pass in NULL for xcontext. 102 */ 103 static int constraint_expr_eval(struct context *scontext, 104 struct context *tcontext, 105 struct context *xcontext, 106 struct constraint_expr *cexpr) 107 { 108 u32 val1, val2; 109 struct context *c; 110 struct role_datum *r1, *r2; 111 struct mls_level *l1, *l2; 112 struct constraint_expr *e; 113 int s[CEXPR_MAXDEPTH]; 114 int sp = -1; 115 116 for (e = cexpr; e; e = e->next) { 117 switch (e->expr_type) { 118 case CEXPR_NOT: 119 BUG_ON(sp < 0); 120 s[sp] = !s[sp]; 121 break; 122 case CEXPR_AND: 123 BUG_ON(sp < 1); 124 sp--; 125 s[sp] &= s[sp+1]; 126 break; 127 case CEXPR_OR: 128 BUG_ON(sp < 1); 129 sp--; 130 s[sp] |= s[sp+1]; 131 break; 132 case CEXPR_ATTR: 133 if (sp == (CEXPR_MAXDEPTH-1)) 134 return 0; 135 switch (e->attr) { 136 case CEXPR_USER: 137 val1 = scontext->user; 138 val2 = tcontext->user; 139 break; 140 case CEXPR_TYPE: 141 val1 = scontext->type; 142 val2 = tcontext->type; 143 break; 144 case CEXPR_ROLE: 145 val1 = scontext->role; 146 val2 = tcontext->role; 147 r1 = policydb.role_val_to_struct[val1 - 1]; 148 r2 = policydb.role_val_to_struct[val2 - 1]; 149 switch (e->op) { 150 case CEXPR_DOM: 151 s[++sp] = ebitmap_get_bit(&r1->dominates, 152 val2 - 1); 153 continue; 154 case CEXPR_DOMBY: 155 s[++sp] = ebitmap_get_bit(&r2->dominates, 156 val1 - 1); 157 continue; 158 case CEXPR_INCOMP: 159 s[++sp] = ( !ebitmap_get_bit(&r1->dominates, 160 val2 - 1) && 161 !ebitmap_get_bit(&r2->dominates, 162 val1 - 1) ); 163 continue; 164 default: 165 break; 166 } 167 break; 168 case CEXPR_L1L2: 169 l1 = &(scontext->range.level[0]); 170 l2 = &(tcontext->range.level[0]); 171 goto mls_ops; 172 case CEXPR_L1H2: 173 l1 = &(scontext->range.level[0]); 174 l2 = &(tcontext->range.level[1]); 175 goto mls_ops; 176 case CEXPR_H1L2: 177 l1 = &(scontext->range.level[1]); 178 l2 = &(tcontext->range.level[0]); 179 goto mls_ops; 180 case CEXPR_H1H2: 181 l1 = &(scontext->range.level[1]); 182 l2 = &(tcontext->range.level[1]); 183 goto mls_ops; 184 case CEXPR_L1H1: 185 l1 = &(scontext->range.level[0]); 186 l2 = &(scontext->range.level[1]); 187 goto mls_ops; 188 case CEXPR_L2H2: 189 l1 = &(tcontext->range.level[0]); 190 l2 = &(tcontext->range.level[1]); 191 goto mls_ops; 192 mls_ops: 193 switch (e->op) { 194 case CEXPR_EQ: 195 s[++sp] = mls_level_eq(l1, l2); 196 continue; 197 case CEXPR_NEQ: 198 s[++sp] = !mls_level_eq(l1, l2); 199 continue; 200 case CEXPR_DOM: 201 s[++sp] = mls_level_dom(l1, l2); 202 continue; 203 case CEXPR_DOMBY: 204 s[++sp] = mls_level_dom(l2, l1); 205 continue; 206 case CEXPR_INCOMP: 207 s[++sp] = mls_level_incomp(l2, l1); 208 continue; 209 default: 210 BUG(); 211 return 0; 212 } 213 break; 214 default: 215 BUG(); 216 return 0; 217 } 218 219 switch (e->op) { 220 case CEXPR_EQ: 221 s[++sp] = (val1 == val2); 222 break; 223 case CEXPR_NEQ: 224 s[++sp] = (val1 != val2); 225 break; 226 default: 227 BUG(); 228 return 0; 229 } 230 break; 231 case CEXPR_NAMES: 232 if (sp == (CEXPR_MAXDEPTH-1)) 233 return 0; 234 c = scontext; 235 if (e->attr & CEXPR_TARGET) 236 c = tcontext; 237 else if (e->attr & CEXPR_XTARGET) { 238 c = xcontext; 239 if (!c) { 240 BUG(); 241 return 0; 242 } 243 } 244 if (e->attr & CEXPR_USER) 245 val1 = c->user; 246 else if (e->attr & CEXPR_ROLE) 247 val1 = c->role; 248 else if (e->attr & CEXPR_TYPE) 249 val1 = c->type; 250 else { 251 BUG(); 252 return 0; 253 } 254 255 switch (e->op) { 256 case CEXPR_EQ: 257 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1); 258 break; 259 case CEXPR_NEQ: 260 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1); 261 break; 262 default: 263 BUG(); 264 return 0; 265 } 266 break; 267 default: 268 BUG(); 269 return 0; 270 } 271 } 272 273 BUG_ON(sp != 0); 274 return s[0]; 275 } 276 277 /* 278 * Compute access vectors based on a context structure pair for 279 * the permissions in a particular class. 280 */ 281 static int context_struct_compute_av(struct context *scontext, 282 struct context *tcontext, 283 u16 tclass, 284 u32 requested, 285 struct av_decision *avd) 286 { 287 struct constraint_node *constraint; 288 struct role_allow *ra; 289 struct avtab_key avkey; 290 struct avtab_node *node; 291 struct class_datum *tclass_datum; 292 struct ebitmap *sattr, *tattr; 293 struct ebitmap_node *snode, *tnode; 294 unsigned int i, j; 295 296 /* 297 * Remap extended Netlink classes for old policy versions. 298 * Do this here rather than socket_type_to_security_class() 299 * in case a newer policy version is loaded, allowing sockets 300 * to remain in the correct class. 301 */ 302 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS) 303 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET && 304 tclass <= SECCLASS_NETLINK_DNRT_SOCKET) 305 tclass = SECCLASS_NETLINK_SOCKET; 306 307 if (!tclass || tclass > policydb.p_classes.nprim) { 308 printk(KERN_ERR "security_compute_av: unrecognized class %d\n", 309 tclass); 310 return -EINVAL; 311 } 312 tclass_datum = policydb.class_val_to_struct[tclass - 1]; 313 314 /* 315 * Initialize the access vectors to the default values. 316 */ 317 avd->allowed = 0; 318 avd->decided = 0xffffffff; 319 avd->auditallow = 0; 320 avd->auditdeny = 0xffffffff; 321 avd->seqno = latest_granting; 322 323 /* 324 * If a specific type enforcement rule was defined for 325 * this permission check, then use it. 326 */ 327 avkey.target_class = tclass; 328 avkey.specified = AVTAB_AV; 329 sattr = &policydb.type_attr_map[scontext->type - 1]; 330 tattr = &policydb.type_attr_map[tcontext->type - 1]; 331 ebitmap_for_each_bit(sattr, snode, i) { 332 if (!ebitmap_node_get_bit(snode, i)) 333 continue; 334 ebitmap_for_each_bit(tattr, tnode, j) { 335 if (!ebitmap_node_get_bit(tnode, j)) 336 continue; 337 avkey.source_type = i + 1; 338 avkey.target_type = j + 1; 339 for (node = avtab_search_node(&policydb.te_avtab, &avkey); 340 node != NULL; 341 node = avtab_search_node_next(node, avkey.specified)) { 342 if (node->key.specified == AVTAB_ALLOWED) 343 avd->allowed |= node->datum.data; 344 else if (node->key.specified == AVTAB_AUDITALLOW) 345 avd->auditallow |= node->datum.data; 346 else if (node->key.specified == AVTAB_AUDITDENY) 347 avd->auditdeny &= node->datum.data; 348 } 349 350 /* Check conditional av table for additional permissions */ 351 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd); 352 353 } 354 } 355 356 /* 357 * Remove any permissions prohibited by a constraint (this includes 358 * the MLS policy). 359 */ 360 constraint = tclass_datum->constraints; 361 while (constraint) { 362 if ((constraint->permissions & (avd->allowed)) && 363 !constraint_expr_eval(scontext, tcontext, NULL, 364 constraint->expr)) { 365 avd->allowed = (avd->allowed) & ~(constraint->permissions); 366 } 367 constraint = constraint->next; 368 } 369 370 /* 371 * If checking process transition permission and the 372 * role is changing, then check the (current_role, new_role) 373 * pair. 374 */ 375 if (tclass == SECCLASS_PROCESS && 376 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) && 377 scontext->role != tcontext->role) { 378 for (ra = policydb.role_allow; ra; ra = ra->next) { 379 if (scontext->role == ra->role && 380 tcontext->role == ra->new_role) 381 break; 382 } 383 if (!ra) 384 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION | 385 PROCESS__DYNTRANSITION); 386 } 387 388 return 0; 389 } 390 391 static int security_validtrans_handle_fail(struct context *ocontext, 392 struct context *ncontext, 393 struct context *tcontext, 394 u16 tclass) 395 { 396 char *o = NULL, *n = NULL, *t = NULL; 397 u32 olen, nlen, tlen; 398 399 if (context_struct_to_string(ocontext, &o, &olen) < 0) 400 goto out; 401 if (context_struct_to_string(ncontext, &n, &nlen) < 0) 402 goto out; 403 if (context_struct_to_string(tcontext, &t, &tlen) < 0) 404 goto out; 405 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, 406 "security_validate_transition: denied for" 407 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s", 408 o, n, t, policydb.p_class_val_to_name[tclass-1]); 409 out: 410 kfree(o); 411 kfree(n); 412 kfree(t); 413 414 if (!selinux_enforcing) 415 return 0; 416 return -EPERM; 417 } 418 419 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid, 420 u16 tclass) 421 { 422 struct context *ocontext; 423 struct context *ncontext; 424 struct context *tcontext; 425 struct class_datum *tclass_datum; 426 struct constraint_node *constraint; 427 int rc = 0; 428 429 if (!ss_initialized) 430 return 0; 431 432 POLICY_RDLOCK; 433 434 /* 435 * Remap extended Netlink classes for old policy versions. 436 * Do this here rather than socket_type_to_security_class() 437 * in case a newer policy version is loaded, allowing sockets 438 * to remain in the correct class. 439 */ 440 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS) 441 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET && 442 tclass <= SECCLASS_NETLINK_DNRT_SOCKET) 443 tclass = SECCLASS_NETLINK_SOCKET; 444 445 if (!tclass || tclass > policydb.p_classes.nprim) { 446 printk(KERN_ERR "security_validate_transition: " 447 "unrecognized class %d\n", tclass); 448 rc = -EINVAL; 449 goto out; 450 } 451 tclass_datum = policydb.class_val_to_struct[tclass - 1]; 452 453 ocontext = sidtab_search(&sidtab, oldsid); 454 if (!ocontext) { 455 printk(KERN_ERR "security_validate_transition: " 456 " unrecognized SID %d\n", oldsid); 457 rc = -EINVAL; 458 goto out; 459 } 460 461 ncontext = sidtab_search(&sidtab, newsid); 462 if (!ncontext) { 463 printk(KERN_ERR "security_validate_transition: " 464 " unrecognized SID %d\n", newsid); 465 rc = -EINVAL; 466 goto out; 467 } 468 469 tcontext = sidtab_search(&sidtab, tasksid); 470 if (!tcontext) { 471 printk(KERN_ERR "security_validate_transition: " 472 " unrecognized SID %d\n", tasksid); 473 rc = -EINVAL; 474 goto out; 475 } 476 477 constraint = tclass_datum->validatetrans; 478 while (constraint) { 479 if (!constraint_expr_eval(ocontext, ncontext, tcontext, 480 constraint->expr)) { 481 rc = security_validtrans_handle_fail(ocontext, ncontext, 482 tcontext, tclass); 483 goto out; 484 } 485 constraint = constraint->next; 486 } 487 488 out: 489 POLICY_RDUNLOCK; 490 return rc; 491 } 492 493 /** 494 * security_compute_av - Compute access vector decisions. 495 * @ssid: source security identifier 496 * @tsid: target security identifier 497 * @tclass: target security class 498 * @requested: requested permissions 499 * @avd: access vector decisions 500 * 501 * Compute a set of access vector decisions based on the 502 * SID pair (@ssid, @tsid) for the permissions in @tclass. 503 * Return -%EINVAL if any of the parameters are invalid or %0 504 * if the access vector decisions were computed successfully. 505 */ 506 int security_compute_av(u32 ssid, 507 u32 tsid, 508 u16 tclass, 509 u32 requested, 510 struct av_decision *avd) 511 { 512 struct context *scontext = NULL, *tcontext = NULL; 513 int rc = 0; 514 515 if (!ss_initialized) { 516 avd->allowed = 0xffffffff; 517 avd->decided = 0xffffffff; 518 avd->auditallow = 0; 519 avd->auditdeny = 0xffffffff; 520 avd->seqno = latest_granting; 521 return 0; 522 } 523 524 POLICY_RDLOCK; 525 526 scontext = sidtab_search(&sidtab, ssid); 527 if (!scontext) { 528 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n", 529 ssid); 530 rc = -EINVAL; 531 goto out; 532 } 533 tcontext = sidtab_search(&sidtab, tsid); 534 if (!tcontext) { 535 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n", 536 tsid); 537 rc = -EINVAL; 538 goto out; 539 } 540 541 rc = context_struct_compute_av(scontext, tcontext, tclass, 542 requested, avd); 543 out: 544 POLICY_RDUNLOCK; 545 return rc; 546 } 547 548 /* 549 * Write the security context string representation of 550 * the context structure `context' into a dynamically 551 * allocated string of the correct size. Set `*scontext' 552 * to point to this string and set `*scontext_len' to 553 * the length of the string. 554 */ 555 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len) 556 { 557 char *scontextp; 558 559 *scontext = NULL; 560 *scontext_len = 0; 561 562 /* Compute the size of the context. */ 563 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1; 564 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1; 565 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1; 566 *scontext_len += mls_compute_context_len(context); 567 568 /* Allocate space for the context; caller must free this space. */ 569 scontextp = kmalloc(*scontext_len, GFP_ATOMIC); 570 if (!scontextp) { 571 return -ENOMEM; 572 } 573 *scontext = scontextp; 574 575 /* 576 * Copy the user name, role name and type name into the context. 577 */ 578 sprintf(scontextp, "%s:%s:%s", 579 policydb.p_user_val_to_name[context->user - 1], 580 policydb.p_role_val_to_name[context->role - 1], 581 policydb.p_type_val_to_name[context->type - 1]); 582 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) + 583 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) + 584 1 + strlen(policydb.p_type_val_to_name[context->type - 1]); 585 586 mls_sid_to_context(context, &scontextp); 587 588 *scontextp = 0; 589 590 return 0; 591 } 592 593 #include "initial_sid_to_string.h" 594 595 /** 596 * security_sid_to_context - Obtain a context for a given SID. 597 * @sid: security identifier, SID 598 * @scontext: security context 599 * @scontext_len: length in bytes 600 * 601 * Write the string representation of the context associated with @sid 602 * into a dynamically allocated string of the correct size. Set @scontext 603 * to point to this string and set @scontext_len to the length of the string. 604 */ 605 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len) 606 { 607 struct context *context; 608 int rc = 0; 609 610 if (!ss_initialized) { 611 if (sid <= SECINITSID_NUM) { 612 char *scontextp; 613 614 *scontext_len = strlen(initial_sid_to_string[sid]) + 1; 615 scontextp = kmalloc(*scontext_len,GFP_ATOMIC); 616 if (!scontextp) { 617 rc = -ENOMEM; 618 goto out; 619 } 620 strcpy(scontextp, initial_sid_to_string[sid]); 621 *scontext = scontextp; 622 goto out; 623 } 624 printk(KERN_ERR "security_sid_to_context: called before initial " 625 "load_policy on unknown SID %d\n", sid); 626 rc = -EINVAL; 627 goto out; 628 } 629 POLICY_RDLOCK; 630 context = sidtab_search(&sidtab, sid); 631 if (!context) { 632 printk(KERN_ERR "security_sid_to_context: unrecognized SID " 633 "%d\n", sid); 634 rc = -EINVAL; 635 goto out_unlock; 636 } 637 rc = context_struct_to_string(context, scontext, scontext_len); 638 out_unlock: 639 POLICY_RDUNLOCK; 640 out: 641 return rc; 642 643 } 644 645 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid) 646 { 647 char *scontext2; 648 struct context context; 649 struct role_datum *role; 650 struct type_datum *typdatum; 651 struct user_datum *usrdatum; 652 char *scontextp, *p, oldc; 653 int rc = 0; 654 655 if (!ss_initialized) { 656 int i; 657 658 for (i = 1; i < SECINITSID_NUM; i++) { 659 if (!strcmp(initial_sid_to_string[i], scontext)) { 660 *sid = i; 661 goto out; 662 } 663 } 664 *sid = SECINITSID_KERNEL; 665 goto out; 666 } 667 *sid = SECSID_NULL; 668 669 /* Copy the string so that we can modify the copy as we parse it. 670 The string should already by null terminated, but we append a 671 null suffix to the copy to avoid problems with the existing 672 attr package, which doesn't view the null terminator as part 673 of the attribute value. */ 674 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL); 675 if (!scontext2) { 676 rc = -ENOMEM; 677 goto out; 678 } 679 memcpy(scontext2, scontext, scontext_len); 680 scontext2[scontext_len] = 0; 681 682 context_init(&context); 683 *sid = SECSID_NULL; 684 685 POLICY_RDLOCK; 686 687 /* Parse the security context. */ 688 689 rc = -EINVAL; 690 scontextp = (char *) scontext2; 691 692 /* Extract the user. */ 693 p = scontextp; 694 while (*p && *p != ':') 695 p++; 696 697 if (*p == 0) 698 goto out_unlock; 699 700 *p++ = 0; 701 702 usrdatum = hashtab_search(policydb.p_users.table, scontextp); 703 if (!usrdatum) 704 goto out_unlock; 705 706 context.user = usrdatum->value; 707 708 /* Extract role. */ 709 scontextp = p; 710 while (*p && *p != ':') 711 p++; 712 713 if (*p == 0) 714 goto out_unlock; 715 716 *p++ = 0; 717 718 role = hashtab_search(policydb.p_roles.table, scontextp); 719 if (!role) 720 goto out_unlock; 721 context.role = role->value; 722 723 /* Extract type. */ 724 scontextp = p; 725 while (*p && *p != ':') 726 p++; 727 oldc = *p; 728 *p++ = 0; 729 730 typdatum = hashtab_search(policydb.p_types.table, scontextp); 731 if (!typdatum) 732 goto out_unlock; 733 734 context.type = typdatum->value; 735 736 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid); 737 if (rc) 738 goto out_unlock; 739 740 if ((p - scontext2) < scontext_len) { 741 rc = -EINVAL; 742 goto out_unlock; 743 } 744 745 /* Check the validity of the new context. */ 746 if (!policydb_context_isvalid(&policydb, &context)) { 747 rc = -EINVAL; 748 goto out_unlock; 749 } 750 /* Obtain the new sid. */ 751 rc = sidtab_context_to_sid(&sidtab, &context, sid); 752 out_unlock: 753 POLICY_RDUNLOCK; 754 context_destroy(&context); 755 kfree(scontext2); 756 out: 757 return rc; 758 } 759 760 /** 761 * security_context_to_sid - Obtain a SID for a given security context. 762 * @scontext: security context 763 * @scontext_len: length in bytes 764 * @sid: security identifier, SID 765 * 766 * Obtains a SID associated with the security context that 767 * has the string representation specified by @scontext. 768 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient 769 * memory is available, or 0 on success. 770 */ 771 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid) 772 { 773 return security_context_to_sid_core(scontext, scontext_len, 774 sid, SECSID_NULL); 775 } 776 777 /** 778 * security_context_to_sid_default - Obtain a SID for a given security context, 779 * falling back to specified default if needed. 780 * 781 * @scontext: security context 782 * @scontext_len: length in bytes 783 * @sid: security identifier, SID 784 * @def_sid: default SID to assign on errror 785 * 786 * Obtains a SID associated with the security context that 787 * has the string representation specified by @scontext. 788 * The default SID is passed to the MLS layer to be used to allow 789 * kernel labeling of the MLS field if the MLS field is not present 790 * (for upgrading to MLS without full relabel). 791 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient 792 * memory is available, or 0 on success. 793 */ 794 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid) 795 { 796 return security_context_to_sid_core(scontext, scontext_len, 797 sid, def_sid); 798 } 799 800 static int compute_sid_handle_invalid_context( 801 struct context *scontext, 802 struct context *tcontext, 803 u16 tclass, 804 struct context *newcontext) 805 { 806 char *s = NULL, *t = NULL, *n = NULL; 807 u32 slen, tlen, nlen; 808 809 if (context_struct_to_string(scontext, &s, &slen) < 0) 810 goto out; 811 if (context_struct_to_string(tcontext, &t, &tlen) < 0) 812 goto out; 813 if (context_struct_to_string(newcontext, &n, &nlen) < 0) 814 goto out; 815 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, 816 "security_compute_sid: invalid context %s" 817 " for scontext=%s" 818 " tcontext=%s" 819 " tclass=%s", 820 n, s, t, policydb.p_class_val_to_name[tclass-1]); 821 out: 822 kfree(s); 823 kfree(t); 824 kfree(n); 825 if (!selinux_enforcing) 826 return 0; 827 return -EACCES; 828 } 829 830 static int security_compute_sid(u32 ssid, 831 u32 tsid, 832 u16 tclass, 833 u32 specified, 834 u32 *out_sid) 835 { 836 struct context *scontext = NULL, *tcontext = NULL, newcontext; 837 struct role_trans *roletr = NULL; 838 struct avtab_key avkey; 839 struct avtab_datum *avdatum; 840 struct avtab_node *node; 841 int rc = 0; 842 843 if (!ss_initialized) { 844 switch (tclass) { 845 case SECCLASS_PROCESS: 846 *out_sid = ssid; 847 break; 848 default: 849 *out_sid = tsid; 850 break; 851 } 852 goto out; 853 } 854 855 context_init(&newcontext); 856 857 POLICY_RDLOCK; 858 859 scontext = sidtab_search(&sidtab, ssid); 860 if (!scontext) { 861 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n", 862 ssid); 863 rc = -EINVAL; 864 goto out_unlock; 865 } 866 tcontext = sidtab_search(&sidtab, tsid); 867 if (!tcontext) { 868 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n", 869 tsid); 870 rc = -EINVAL; 871 goto out_unlock; 872 } 873 874 /* Set the user identity. */ 875 switch (specified) { 876 case AVTAB_TRANSITION: 877 case AVTAB_CHANGE: 878 /* Use the process user identity. */ 879 newcontext.user = scontext->user; 880 break; 881 case AVTAB_MEMBER: 882 /* Use the related object owner. */ 883 newcontext.user = tcontext->user; 884 break; 885 } 886 887 /* Set the role and type to default values. */ 888 switch (tclass) { 889 case SECCLASS_PROCESS: 890 /* Use the current role and type of process. */ 891 newcontext.role = scontext->role; 892 newcontext.type = scontext->type; 893 break; 894 default: 895 /* Use the well-defined object role. */ 896 newcontext.role = OBJECT_R_VAL; 897 /* Use the type of the related object. */ 898 newcontext.type = tcontext->type; 899 } 900 901 /* Look for a type transition/member/change rule. */ 902 avkey.source_type = scontext->type; 903 avkey.target_type = tcontext->type; 904 avkey.target_class = tclass; 905 avkey.specified = specified; 906 avdatum = avtab_search(&policydb.te_avtab, &avkey); 907 908 /* If no permanent rule, also check for enabled conditional rules */ 909 if(!avdatum) { 910 node = avtab_search_node(&policydb.te_cond_avtab, &avkey); 911 for (; node != NULL; node = avtab_search_node_next(node, specified)) { 912 if (node->key.specified & AVTAB_ENABLED) { 913 avdatum = &node->datum; 914 break; 915 } 916 } 917 } 918 919 if (avdatum) { 920 /* Use the type from the type transition/member/change rule. */ 921 newcontext.type = avdatum->data; 922 } 923 924 /* Check for class-specific changes. */ 925 switch (tclass) { 926 case SECCLASS_PROCESS: 927 if (specified & AVTAB_TRANSITION) { 928 /* Look for a role transition rule. */ 929 for (roletr = policydb.role_tr; roletr; 930 roletr = roletr->next) { 931 if (roletr->role == scontext->role && 932 roletr->type == tcontext->type) { 933 /* Use the role transition rule. */ 934 newcontext.role = roletr->new_role; 935 break; 936 } 937 } 938 } 939 break; 940 default: 941 break; 942 } 943 944 /* Set the MLS attributes. 945 This is done last because it may allocate memory. */ 946 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext); 947 if (rc) 948 goto out_unlock; 949 950 /* Check the validity of the context. */ 951 if (!policydb_context_isvalid(&policydb, &newcontext)) { 952 rc = compute_sid_handle_invalid_context(scontext, 953 tcontext, 954 tclass, 955 &newcontext); 956 if (rc) 957 goto out_unlock; 958 } 959 /* Obtain the sid for the context. */ 960 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid); 961 out_unlock: 962 POLICY_RDUNLOCK; 963 context_destroy(&newcontext); 964 out: 965 return rc; 966 } 967 968 /** 969 * security_transition_sid - Compute the SID for a new subject/object. 970 * @ssid: source security identifier 971 * @tsid: target security identifier 972 * @tclass: target security class 973 * @out_sid: security identifier for new subject/object 974 * 975 * Compute a SID to use for labeling a new subject or object in the 976 * class @tclass based on a SID pair (@ssid, @tsid). 977 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 978 * if insufficient memory is available, or %0 if the new SID was 979 * computed successfully. 980 */ 981 int security_transition_sid(u32 ssid, 982 u32 tsid, 983 u16 tclass, 984 u32 *out_sid) 985 { 986 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid); 987 } 988 989 /** 990 * security_member_sid - Compute the SID for member selection. 991 * @ssid: source security identifier 992 * @tsid: target security identifier 993 * @tclass: target security class 994 * @out_sid: security identifier for selected member 995 * 996 * Compute a SID to use when selecting a member of a polyinstantiated 997 * object of class @tclass based on a SID pair (@ssid, @tsid). 998 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 999 * if insufficient memory is available, or %0 if the SID was 1000 * computed successfully. 1001 */ 1002 int security_member_sid(u32 ssid, 1003 u32 tsid, 1004 u16 tclass, 1005 u32 *out_sid) 1006 { 1007 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid); 1008 } 1009 1010 /** 1011 * security_change_sid - Compute the SID for object relabeling. 1012 * @ssid: source security identifier 1013 * @tsid: target security identifier 1014 * @tclass: target security class 1015 * @out_sid: security identifier for selected member 1016 * 1017 * Compute a SID to use for relabeling an object of class @tclass 1018 * based on a SID pair (@ssid, @tsid). 1019 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM 1020 * if insufficient memory is available, or %0 if the SID was 1021 * computed successfully. 1022 */ 1023 int security_change_sid(u32 ssid, 1024 u32 tsid, 1025 u16 tclass, 1026 u32 *out_sid) 1027 { 1028 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid); 1029 } 1030 1031 /* 1032 * Verify that each kernel class that is defined in the 1033 * policy is correct 1034 */ 1035 static int validate_classes(struct policydb *p) 1036 { 1037 int i, j; 1038 struct class_datum *cladatum; 1039 struct perm_datum *perdatum; 1040 u32 nprim, tmp, common_pts_len, perm_val, pol_val; 1041 u16 class_val; 1042 const struct selinux_class_perm *kdefs = &selinux_class_perm; 1043 const char *def_class, *def_perm, *pol_class; 1044 struct symtab *perms; 1045 1046 for (i = 1; i < kdefs->cts_len; i++) { 1047 def_class = kdefs->class_to_string[i]; 1048 if (i > p->p_classes.nprim) { 1049 printk(KERN_INFO 1050 "security: class %s not defined in policy\n", 1051 def_class); 1052 continue; 1053 } 1054 pol_class = p->p_class_val_to_name[i-1]; 1055 if (strcmp(pol_class, def_class)) { 1056 printk(KERN_ERR 1057 "security: class %d is incorrect, found %s but should be %s\n", 1058 i, pol_class, def_class); 1059 return -EINVAL; 1060 } 1061 } 1062 for (i = 0; i < kdefs->av_pts_len; i++) { 1063 class_val = kdefs->av_perm_to_string[i].tclass; 1064 perm_val = kdefs->av_perm_to_string[i].value; 1065 def_perm = kdefs->av_perm_to_string[i].name; 1066 if (class_val > p->p_classes.nprim) 1067 continue; 1068 pol_class = p->p_class_val_to_name[class_val-1]; 1069 cladatum = hashtab_search(p->p_classes.table, pol_class); 1070 BUG_ON(!cladatum); 1071 perms = &cladatum->permissions; 1072 nprim = 1 << (perms->nprim - 1); 1073 if (perm_val > nprim) { 1074 printk(KERN_INFO 1075 "security: permission %s in class %s not defined in policy\n", 1076 def_perm, pol_class); 1077 continue; 1078 } 1079 perdatum = hashtab_search(perms->table, def_perm); 1080 if (perdatum == NULL) { 1081 printk(KERN_ERR 1082 "security: permission %s in class %s not found in policy\n", 1083 def_perm, pol_class); 1084 return -EINVAL; 1085 } 1086 pol_val = 1 << (perdatum->value - 1); 1087 if (pol_val != perm_val) { 1088 printk(KERN_ERR 1089 "security: permission %s in class %s has incorrect value\n", 1090 def_perm, pol_class); 1091 return -EINVAL; 1092 } 1093 } 1094 for (i = 0; i < kdefs->av_inherit_len; i++) { 1095 class_val = kdefs->av_inherit[i].tclass; 1096 if (class_val > p->p_classes.nprim) 1097 continue; 1098 pol_class = p->p_class_val_to_name[class_val-1]; 1099 cladatum = hashtab_search(p->p_classes.table, pol_class); 1100 BUG_ON(!cladatum); 1101 if (!cladatum->comdatum) { 1102 printk(KERN_ERR 1103 "security: class %s should have an inherits clause but does not\n", 1104 pol_class); 1105 return -EINVAL; 1106 } 1107 tmp = kdefs->av_inherit[i].common_base; 1108 common_pts_len = 0; 1109 while (!(tmp & 0x01)) { 1110 common_pts_len++; 1111 tmp >>= 1; 1112 } 1113 perms = &cladatum->comdatum->permissions; 1114 for (j = 0; j < common_pts_len; j++) { 1115 def_perm = kdefs->av_inherit[i].common_pts[j]; 1116 if (j >= perms->nprim) { 1117 printk(KERN_INFO 1118 "security: permission %s in class %s not defined in policy\n", 1119 def_perm, pol_class); 1120 continue; 1121 } 1122 perdatum = hashtab_search(perms->table, def_perm); 1123 if (perdatum == NULL) { 1124 printk(KERN_ERR 1125 "security: permission %s in class %s not found in policy\n", 1126 def_perm, pol_class); 1127 return -EINVAL; 1128 } 1129 if (perdatum->value != j + 1) { 1130 printk(KERN_ERR 1131 "security: permission %s in class %s has incorrect value\n", 1132 def_perm, pol_class); 1133 return -EINVAL; 1134 } 1135 } 1136 } 1137 return 0; 1138 } 1139 1140 /* Clone the SID into the new SID table. */ 1141 static int clone_sid(u32 sid, 1142 struct context *context, 1143 void *arg) 1144 { 1145 struct sidtab *s = arg; 1146 1147 return sidtab_insert(s, sid, context); 1148 } 1149 1150 static inline int convert_context_handle_invalid_context(struct context *context) 1151 { 1152 int rc = 0; 1153 1154 if (selinux_enforcing) { 1155 rc = -EINVAL; 1156 } else { 1157 char *s; 1158 u32 len; 1159 1160 context_struct_to_string(context, &s, &len); 1161 printk(KERN_ERR "security: context %s is invalid\n", s); 1162 kfree(s); 1163 } 1164 return rc; 1165 } 1166 1167 struct convert_context_args { 1168 struct policydb *oldp; 1169 struct policydb *newp; 1170 }; 1171 1172 /* 1173 * Convert the values in the security context 1174 * structure `c' from the values specified 1175 * in the policy `p->oldp' to the values specified 1176 * in the policy `p->newp'. Verify that the 1177 * context is valid under the new policy. 1178 */ 1179 static int convert_context(u32 key, 1180 struct context *c, 1181 void *p) 1182 { 1183 struct convert_context_args *args; 1184 struct context oldc; 1185 struct role_datum *role; 1186 struct type_datum *typdatum; 1187 struct user_datum *usrdatum; 1188 char *s; 1189 u32 len; 1190 int rc; 1191 1192 args = p; 1193 1194 rc = context_cpy(&oldc, c); 1195 if (rc) 1196 goto out; 1197 1198 rc = -EINVAL; 1199 1200 /* Convert the user. */ 1201 usrdatum = hashtab_search(args->newp->p_users.table, 1202 args->oldp->p_user_val_to_name[c->user - 1]); 1203 if (!usrdatum) { 1204 goto bad; 1205 } 1206 c->user = usrdatum->value; 1207 1208 /* Convert the role. */ 1209 role = hashtab_search(args->newp->p_roles.table, 1210 args->oldp->p_role_val_to_name[c->role - 1]); 1211 if (!role) { 1212 goto bad; 1213 } 1214 c->role = role->value; 1215 1216 /* Convert the type. */ 1217 typdatum = hashtab_search(args->newp->p_types.table, 1218 args->oldp->p_type_val_to_name[c->type - 1]); 1219 if (!typdatum) { 1220 goto bad; 1221 } 1222 c->type = typdatum->value; 1223 1224 rc = mls_convert_context(args->oldp, args->newp, c); 1225 if (rc) 1226 goto bad; 1227 1228 /* Check the validity of the new context. */ 1229 if (!policydb_context_isvalid(args->newp, c)) { 1230 rc = convert_context_handle_invalid_context(&oldc); 1231 if (rc) 1232 goto bad; 1233 } 1234 1235 context_destroy(&oldc); 1236 out: 1237 return rc; 1238 bad: 1239 context_struct_to_string(&oldc, &s, &len); 1240 context_destroy(&oldc); 1241 printk(KERN_ERR "security: invalidating context %s\n", s); 1242 kfree(s); 1243 goto out; 1244 } 1245 1246 extern void selinux_complete_init(void); 1247 1248 /** 1249 * security_load_policy - Load a security policy configuration. 1250 * @data: binary policy data 1251 * @len: length of data in bytes 1252 * 1253 * Load a new set of security policy configuration data, 1254 * validate it and convert the SID table as necessary. 1255 * This function will flush the access vector cache after 1256 * loading the new policy. 1257 */ 1258 int security_load_policy(void *data, size_t len) 1259 { 1260 struct policydb oldpolicydb, newpolicydb; 1261 struct sidtab oldsidtab, newsidtab; 1262 struct convert_context_args args; 1263 u32 seqno; 1264 int rc = 0; 1265 struct policy_file file = { data, len }, *fp = &file; 1266 1267 LOAD_LOCK; 1268 1269 if (!ss_initialized) { 1270 avtab_cache_init(); 1271 if (policydb_read(&policydb, fp)) { 1272 LOAD_UNLOCK; 1273 avtab_cache_destroy(); 1274 return -EINVAL; 1275 } 1276 if (policydb_load_isids(&policydb, &sidtab)) { 1277 LOAD_UNLOCK; 1278 policydb_destroy(&policydb); 1279 avtab_cache_destroy(); 1280 return -EINVAL; 1281 } 1282 /* Verify that the kernel defined classes are correct. */ 1283 if (validate_classes(&policydb)) { 1284 printk(KERN_ERR 1285 "security: the definition of a class is incorrect\n"); 1286 LOAD_UNLOCK; 1287 sidtab_destroy(&sidtab); 1288 policydb_destroy(&policydb); 1289 avtab_cache_destroy(); 1290 return -EINVAL; 1291 } 1292 policydb_loaded_version = policydb.policyvers; 1293 ss_initialized = 1; 1294 seqno = ++latest_granting; 1295 LOAD_UNLOCK; 1296 selinux_complete_init(); 1297 avc_ss_reset(seqno); 1298 selnl_notify_policyload(seqno); 1299 selinux_netlbl_cache_invalidate(); 1300 return 0; 1301 } 1302 1303 #if 0 1304 sidtab_hash_eval(&sidtab, "sids"); 1305 #endif 1306 1307 if (policydb_read(&newpolicydb, fp)) { 1308 LOAD_UNLOCK; 1309 return -EINVAL; 1310 } 1311 1312 sidtab_init(&newsidtab); 1313 1314 /* Verify that the kernel defined classes are correct. */ 1315 if (validate_classes(&newpolicydb)) { 1316 printk(KERN_ERR 1317 "security: the definition of a class is incorrect\n"); 1318 rc = -EINVAL; 1319 goto err; 1320 } 1321 1322 /* Clone the SID table. */ 1323 sidtab_shutdown(&sidtab); 1324 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) { 1325 rc = -ENOMEM; 1326 goto err; 1327 } 1328 1329 /* Convert the internal representations of contexts 1330 in the new SID table and remove invalid SIDs. */ 1331 args.oldp = &policydb; 1332 args.newp = &newpolicydb; 1333 sidtab_map_remove_on_error(&newsidtab, convert_context, &args); 1334 1335 /* Save the old policydb and SID table to free later. */ 1336 memcpy(&oldpolicydb, &policydb, sizeof policydb); 1337 sidtab_set(&oldsidtab, &sidtab); 1338 1339 /* Install the new policydb and SID table. */ 1340 POLICY_WRLOCK; 1341 memcpy(&policydb, &newpolicydb, sizeof policydb); 1342 sidtab_set(&sidtab, &newsidtab); 1343 seqno = ++latest_granting; 1344 policydb_loaded_version = policydb.policyvers; 1345 POLICY_WRUNLOCK; 1346 LOAD_UNLOCK; 1347 1348 /* Free the old policydb and SID table. */ 1349 policydb_destroy(&oldpolicydb); 1350 sidtab_destroy(&oldsidtab); 1351 1352 avc_ss_reset(seqno); 1353 selnl_notify_policyload(seqno); 1354 selinux_netlbl_cache_invalidate(); 1355 1356 return 0; 1357 1358 err: 1359 LOAD_UNLOCK; 1360 sidtab_destroy(&newsidtab); 1361 policydb_destroy(&newpolicydb); 1362 return rc; 1363 1364 } 1365 1366 /** 1367 * security_port_sid - Obtain the SID for a port. 1368 * @domain: communication domain aka address family 1369 * @type: socket type 1370 * @protocol: protocol number 1371 * @port: port number 1372 * @out_sid: security identifier 1373 */ 1374 int security_port_sid(u16 domain, 1375 u16 type, 1376 u8 protocol, 1377 u16 port, 1378 u32 *out_sid) 1379 { 1380 struct ocontext *c; 1381 int rc = 0; 1382 1383 POLICY_RDLOCK; 1384 1385 c = policydb.ocontexts[OCON_PORT]; 1386 while (c) { 1387 if (c->u.port.protocol == protocol && 1388 c->u.port.low_port <= port && 1389 c->u.port.high_port >= port) 1390 break; 1391 c = c->next; 1392 } 1393 1394 if (c) { 1395 if (!c->sid[0]) { 1396 rc = sidtab_context_to_sid(&sidtab, 1397 &c->context[0], 1398 &c->sid[0]); 1399 if (rc) 1400 goto out; 1401 } 1402 *out_sid = c->sid[0]; 1403 } else { 1404 *out_sid = SECINITSID_PORT; 1405 } 1406 1407 out: 1408 POLICY_RDUNLOCK; 1409 return rc; 1410 } 1411 1412 /** 1413 * security_netif_sid - Obtain the SID for a network interface. 1414 * @name: interface name 1415 * @if_sid: interface SID 1416 * @msg_sid: default SID for received packets 1417 */ 1418 int security_netif_sid(char *name, 1419 u32 *if_sid, 1420 u32 *msg_sid) 1421 { 1422 int rc = 0; 1423 struct ocontext *c; 1424 1425 POLICY_RDLOCK; 1426 1427 c = policydb.ocontexts[OCON_NETIF]; 1428 while (c) { 1429 if (strcmp(name, c->u.name) == 0) 1430 break; 1431 c = c->next; 1432 } 1433 1434 if (c) { 1435 if (!c->sid[0] || !c->sid[1]) { 1436 rc = sidtab_context_to_sid(&sidtab, 1437 &c->context[0], 1438 &c->sid[0]); 1439 if (rc) 1440 goto out; 1441 rc = sidtab_context_to_sid(&sidtab, 1442 &c->context[1], 1443 &c->sid[1]); 1444 if (rc) 1445 goto out; 1446 } 1447 *if_sid = c->sid[0]; 1448 *msg_sid = c->sid[1]; 1449 } else { 1450 *if_sid = SECINITSID_NETIF; 1451 *msg_sid = SECINITSID_NETMSG; 1452 } 1453 1454 out: 1455 POLICY_RDUNLOCK; 1456 return rc; 1457 } 1458 1459 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask) 1460 { 1461 int i, fail = 0; 1462 1463 for(i = 0; i < 4; i++) 1464 if(addr[i] != (input[i] & mask[i])) { 1465 fail = 1; 1466 break; 1467 } 1468 1469 return !fail; 1470 } 1471 1472 /** 1473 * security_node_sid - Obtain the SID for a node (host). 1474 * @domain: communication domain aka address family 1475 * @addrp: address 1476 * @addrlen: address length in bytes 1477 * @out_sid: security identifier 1478 */ 1479 int security_node_sid(u16 domain, 1480 void *addrp, 1481 u32 addrlen, 1482 u32 *out_sid) 1483 { 1484 int rc = 0; 1485 struct ocontext *c; 1486 1487 POLICY_RDLOCK; 1488 1489 switch (domain) { 1490 case AF_INET: { 1491 u32 addr; 1492 1493 if (addrlen != sizeof(u32)) { 1494 rc = -EINVAL; 1495 goto out; 1496 } 1497 1498 addr = *((u32 *)addrp); 1499 1500 c = policydb.ocontexts[OCON_NODE]; 1501 while (c) { 1502 if (c->u.node.addr == (addr & c->u.node.mask)) 1503 break; 1504 c = c->next; 1505 } 1506 break; 1507 } 1508 1509 case AF_INET6: 1510 if (addrlen != sizeof(u64) * 2) { 1511 rc = -EINVAL; 1512 goto out; 1513 } 1514 c = policydb.ocontexts[OCON_NODE6]; 1515 while (c) { 1516 if (match_ipv6_addrmask(addrp, c->u.node6.addr, 1517 c->u.node6.mask)) 1518 break; 1519 c = c->next; 1520 } 1521 break; 1522 1523 default: 1524 *out_sid = SECINITSID_NODE; 1525 goto out; 1526 } 1527 1528 if (c) { 1529 if (!c->sid[0]) { 1530 rc = sidtab_context_to_sid(&sidtab, 1531 &c->context[0], 1532 &c->sid[0]); 1533 if (rc) 1534 goto out; 1535 } 1536 *out_sid = c->sid[0]; 1537 } else { 1538 *out_sid = SECINITSID_NODE; 1539 } 1540 1541 out: 1542 POLICY_RDUNLOCK; 1543 return rc; 1544 } 1545 1546 #define SIDS_NEL 25 1547 1548 /** 1549 * security_get_user_sids - Obtain reachable SIDs for a user. 1550 * @fromsid: starting SID 1551 * @username: username 1552 * @sids: array of reachable SIDs for user 1553 * @nel: number of elements in @sids 1554 * 1555 * Generate the set of SIDs for legal security contexts 1556 * for a given user that can be reached by @fromsid. 1557 * Set *@sids to point to a dynamically allocated 1558 * array containing the set of SIDs. Set *@nel to the 1559 * number of elements in the array. 1560 */ 1561 1562 int security_get_user_sids(u32 fromsid, 1563 char *username, 1564 u32 **sids, 1565 u32 *nel) 1566 { 1567 struct context *fromcon, usercon; 1568 u32 *mysids, *mysids2, sid; 1569 u32 mynel = 0, maxnel = SIDS_NEL; 1570 struct user_datum *user; 1571 struct role_datum *role; 1572 struct av_decision avd; 1573 struct ebitmap_node *rnode, *tnode; 1574 int rc = 0, i, j; 1575 1576 if (!ss_initialized) { 1577 *sids = NULL; 1578 *nel = 0; 1579 goto out; 1580 } 1581 1582 POLICY_RDLOCK; 1583 1584 fromcon = sidtab_search(&sidtab, fromsid); 1585 if (!fromcon) { 1586 rc = -EINVAL; 1587 goto out_unlock; 1588 } 1589 1590 user = hashtab_search(policydb.p_users.table, username); 1591 if (!user) { 1592 rc = -EINVAL; 1593 goto out_unlock; 1594 } 1595 usercon.user = user->value; 1596 1597 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC); 1598 if (!mysids) { 1599 rc = -ENOMEM; 1600 goto out_unlock; 1601 } 1602 1603 ebitmap_for_each_bit(&user->roles, rnode, i) { 1604 if (!ebitmap_node_get_bit(rnode, i)) 1605 continue; 1606 role = policydb.role_val_to_struct[i]; 1607 usercon.role = i+1; 1608 ebitmap_for_each_bit(&role->types, tnode, j) { 1609 if (!ebitmap_node_get_bit(tnode, j)) 1610 continue; 1611 usercon.type = j+1; 1612 1613 if (mls_setup_user_range(fromcon, user, &usercon)) 1614 continue; 1615 1616 rc = context_struct_compute_av(fromcon, &usercon, 1617 SECCLASS_PROCESS, 1618 PROCESS__TRANSITION, 1619 &avd); 1620 if (rc || !(avd.allowed & PROCESS__TRANSITION)) 1621 continue; 1622 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid); 1623 if (rc) { 1624 kfree(mysids); 1625 goto out_unlock; 1626 } 1627 if (mynel < maxnel) { 1628 mysids[mynel++] = sid; 1629 } else { 1630 maxnel += SIDS_NEL; 1631 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC); 1632 if (!mysids2) { 1633 rc = -ENOMEM; 1634 kfree(mysids); 1635 goto out_unlock; 1636 } 1637 memcpy(mysids2, mysids, mynel * sizeof(*mysids2)); 1638 kfree(mysids); 1639 mysids = mysids2; 1640 mysids[mynel++] = sid; 1641 } 1642 } 1643 } 1644 1645 *sids = mysids; 1646 *nel = mynel; 1647 1648 out_unlock: 1649 POLICY_RDUNLOCK; 1650 out: 1651 return rc; 1652 } 1653 1654 /** 1655 * security_genfs_sid - Obtain a SID for a file in a filesystem 1656 * @fstype: filesystem type 1657 * @path: path from root of mount 1658 * @sclass: file security class 1659 * @sid: SID for path 1660 * 1661 * Obtain a SID to use for a file in a filesystem that 1662 * cannot support xattr or use a fixed labeling behavior like 1663 * transition SIDs or task SIDs. 1664 */ 1665 int security_genfs_sid(const char *fstype, 1666 char *path, 1667 u16 sclass, 1668 u32 *sid) 1669 { 1670 int len; 1671 struct genfs *genfs; 1672 struct ocontext *c; 1673 int rc = 0, cmp = 0; 1674 1675 POLICY_RDLOCK; 1676 1677 for (genfs = policydb.genfs; genfs; genfs = genfs->next) { 1678 cmp = strcmp(fstype, genfs->fstype); 1679 if (cmp <= 0) 1680 break; 1681 } 1682 1683 if (!genfs || cmp) { 1684 *sid = SECINITSID_UNLABELED; 1685 rc = -ENOENT; 1686 goto out; 1687 } 1688 1689 for (c = genfs->head; c; c = c->next) { 1690 len = strlen(c->u.name); 1691 if ((!c->v.sclass || sclass == c->v.sclass) && 1692 (strncmp(c->u.name, path, len) == 0)) 1693 break; 1694 } 1695 1696 if (!c) { 1697 *sid = SECINITSID_UNLABELED; 1698 rc = -ENOENT; 1699 goto out; 1700 } 1701 1702 if (!c->sid[0]) { 1703 rc = sidtab_context_to_sid(&sidtab, 1704 &c->context[0], 1705 &c->sid[0]); 1706 if (rc) 1707 goto out; 1708 } 1709 1710 *sid = c->sid[0]; 1711 out: 1712 POLICY_RDUNLOCK; 1713 return rc; 1714 } 1715 1716 /** 1717 * security_fs_use - Determine how to handle labeling for a filesystem. 1718 * @fstype: filesystem type 1719 * @behavior: labeling behavior 1720 * @sid: SID for filesystem (superblock) 1721 */ 1722 int security_fs_use( 1723 const char *fstype, 1724 unsigned int *behavior, 1725 u32 *sid) 1726 { 1727 int rc = 0; 1728 struct ocontext *c; 1729 1730 POLICY_RDLOCK; 1731 1732 c = policydb.ocontexts[OCON_FSUSE]; 1733 while (c) { 1734 if (strcmp(fstype, c->u.name) == 0) 1735 break; 1736 c = c->next; 1737 } 1738 1739 if (c) { 1740 *behavior = c->v.behavior; 1741 if (!c->sid[0]) { 1742 rc = sidtab_context_to_sid(&sidtab, 1743 &c->context[0], 1744 &c->sid[0]); 1745 if (rc) 1746 goto out; 1747 } 1748 *sid = c->sid[0]; 1749 } else { 1750 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid); 1751 if (rc) { 1752 *behavior = SECURITY_FS_USE_NONE; 1753 rc = 0; 1754 } else { 1755 *behavior = SECURITY_FS_USE_GENFS; 1756 } 1757 } 1758 1759 out: 1760 POLICY_RDUNLOCK; 1761 return rc; 1762 } 1763 1764 int security_get_bools(int *len, char ***names, int **values) 1765 { 1766 int i, rc = -ENOMEM; 1767 1768 POLICY_RDLOCK; 1769 *names = NULL; 1770 *values = NULL; 1771 1772 *len = policydb.p_bools.nprim; 1773 if (!*len) { 1774 rc = 0; 1775 goto out; 1776 } 1777 1778 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC); 1779 if (!*names) 1780 goto err; 1781 1782 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC); 1783 if (!*values) 1784 goto err; 1785 1786 for (i = 0; i < *len; i++) { 1787 size_t name_len; 1788 (*values)[i] = policydb.bool_val_to_struct[i]->state; 1789 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1; 1790 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC); 1791 if (!(*names)[i]) 1792 goto err; 1793 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len); 1794 (*names)[i][name_len - 1] = 0; 1795 } 1796 rc = 0; 1797 out: 1798 POLICY_RDUNLOCK; 1799 return rc; 1800 err: 1801 if (*names) { 1802 for (i = 0; i < *len; i++) 1803 kfree((*names)[i]); 1804 } 1805 kfree(*values); 1806 goto out; 1807 } 1808 1809 1810 int security_set_bools(int len, int *values) 1811 { 1812 int i, rc = 0; 1813 int lenp, seqno = 0; 1814 struct cond_node *cur; 1815 1816 POLICY_WRLOCK; 1817 1818 lenp = policydb.p_bools.nprim; 1819 if (len != lenp) { 1820 rc = -EFAULT; 1821 goto out; 1822 } 1823 1824 for (i = 0; i < len; i++) { 1825 if (!!values[i] != policydb.bool_val_to_struct[i]->state) { 1826 audit_log(current->audit_context, GFP_ATOMIC, 1827 AUDIT_MAC_CONFIG_CHANGE, 1828 "bool=%s val=%d old_val=%d auid=%u", 1829 policydb.p_bool_val_to_name[i], 1830 !!values[i], 1831 policydb.bool_val_to_struct[i]->state, 1832 audit_get_loginuid(current->audit_context)); 1833 } 1834 if (values[i]) { 1835 policydb.bool_val_to_struct[i]->state = 1; 1836 } else { 1837 policydb.bool_val_to_struct[i]->state = 0; 1838 } 1839 } 1840 1841 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) { 1842 rc = evaluate_cond_node(&policydb, cur); 1843 if (rc) 1844 goto out; 1845 } 1846 1847 seqno = ++latest_granting; 1848 1849 out: 1850 POLICY_WRUNLOCK; 1851 if (!rc) { 1852 avc_ss_reset(seqno); 1853 selnl_notify_policyload(seqno); 1854 } 1855 return rc; 1856 } 1857 1858 int security_get_bool_value(int bool) 1859 { 1860 int rc = 0; 1861 int len; 1862 1863 POLICY_RDLOCK; 1864 1865 len = policydb.p_bools.nprim; 1866 if (bool >= len) { 1867 rc = -EFAULT; 1868 goto out; 1869 } 1870 1871 rc = policydb.bool_val_to_struct[bool]->state; 1872 out: 1873 POLICY_RDUNLOCK; 1874 return rc; 1875 } 1876 1877 /* 1878 * security_sid_mls_copy() - computes a new sid based on the given 1879 * sid and the mls portion of mls_sid. 1880 */ 1881 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid) 1882 { 1883 struct context *context1; 1884 struct context *context2; 1885 struct context newcon; 1886 char *s; 1887 u32 len; 1888 int rc = 0; 1889 1890 if (!ss_initialized || !selinux_mls_enabled) { 1891 *new_sid = sid; 1892 goto out; 1893 } 1894 1895 context_init(&newcon); 1896 1897 POLICY_RDLOCK; 1898 context1 = sidtab_search(&sidtab, sid); 1899 if (!context1) { 1900 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID " 1901 "%d\n", sid); 1902 rc = -EINVAL; 1903 goto out_unlock; 1904 } 1905 1906 context2 = sidtab_search(&sidtab, mls_sid); 1907 if (!context2) { 1908 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID " 1909 "%d\n", mls_sid); 1910 rc = -EINVAL; 1911 goto out_unlock; 1912 } 1913 1914 newcon.user = context1->user; 1915 newcon.role = context1->role; 1916 newcon.type = context1->type; 1917 rc = mls_copy_context(&newcon, context2); 1918 if (rc) 1919 goto out_unlock; 1920 1921 1922 /* Check the validity of the new context. */ 1923 if (!policydb_context_isvalid(&policydb, &newcon)) { 1924 rc = convert_context_handle_invalid_context(&newcon); 1925 if (rc) 1926 goto bad; 1927 } 1928 1929 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid); 1930 goto out_unlock; 1931 1932 bad: 1933 if (!context_struct_to_string(&newcon, &s, &len)) { 1934 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, 1935 "security_sid_mls_copy: invalid context %s", s); 1936 kfree(s); 1937 } 1938 1939 out_unlock: 1940 POLICY_RDUNLOCK; 1941 context_destroy(&newcon); 1942 out: 1943 return rc; 1944 } 1945 1946 struct selinux_audit_rule { 1947 u32 au_seqno; 1948 struct context au_ctxt; 1949 }; 1950 1951 void selinux_audit_rule_free(struct selinux_audit_rule *rule) 1952 { 1953 if (rule) { 1954 context_destroy(&rule->au_ctxt); 1955 kfree(rule); 1956 } 1957 } 1958 1959 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, 1960 struct selinux_audit_rule **rule) 1961 { 1962 struct selinux_audit_rule *tmprule; 1963 struct role_datum *roledatum; 1964 struct type_datum *typedatum; 1965 struct user_datum *userdatum; 1966 int rc = 0; 1967 1968 *rule = NULL; 1969 1970 if (!ss_initialized) 1971 return -ENOTSUPP; 1972 1973 switch (field) { 1974 case AUDIT_SUBJ_USER: 1975 case AUDIT_SUBJ_ROLE: 1976 case AUDIT_SUBJ_TYPE: 1977 case AUDIT_OBJ_USER: 1978 case AUDIT_OBJ_ROLE: 1979 case AUDIT_OBJ_TYPE: 1980 /* only 'equals' and 'not equals' fit user, role, and type */ 1981 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL) 1982 return -EINVAL; 1983 break; 1984 case AUDIT_SUBJ_SEN: 1985 case AUDIT_SUBJ_CLR: 1986 case AUDIT_OBJ_LEV_LOW: 1987 case AUDIT_OBJ_LEV_HIGH: 1988 /* we do not allow a range, indicated by the presense of '-' */ 1989 if (strchr(rulestr, '-')) 1990 return -EINVAL; 1991 break; 1992 default: 1993 /* only the above fields are valid */ 1994 return -EINVAL; 1995 } 1996 1997 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL); 1998 if (!tmprule) 1999 return -ENOMEM; 2000 2001 context_init(&tmprule->au_ctxt); 2002 2003 POLICY_RDLOCK; 2004 2005 tmprule->au_seqno = latest_granting; 2006 2007 switch (field) { 2008 case AUDIT_SUBJ_USER: 2009 case AUDIT_OBJ_USER: 2010 userdatum = hashtab_search(policydb.p_users.table, rulestr); 2011 if (!userdatum) 2012 rc = -EINVAL; 2013 else 2014 tmprule->au_ctxt.user = userdatum->value; 2015 break; 2016 case AUDIT_SUBJ_ROLE: 2017 case AUDIT_OBJ_ROLE: 2018 roledatum = hashtab_search(policydb.p_roles.table, rulestr); 2019 if (!roledatum) 2020 rc = -EINVAL; 2021 else 2022 tmprule->au_ctxt.role = roledatum->value; 2023 break; 2024 case AUDIT_SUBJ_TYPE: 2025 case AUDIT_OBJ_TYPE: 2026 typedatum = hashtab_search(policydb.p_types.table, rulestr); 2027 if (!typedatum) 2028 rc = -EINVAL; 2029 else 2030 tmprule->au_ctxt.type = typedatum->value; 2031 break; 2032 case AUDIT_SUBJ_SEN: 2033 case AUDIT_SUBJ_CLR: 2034 case AUDIT_OBJ_LEV_LOW: 2035 case AUDIT_OBJ_LEV_HIGH: 2036 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC); 2037 break; 2038 } 2039 2040 POLICY_RDUNLOCK; 2041 2042 if (rc) { 2043 selinux_audit_rule_free(tmprule); 2044 tmprule = NULL; 2045 } 2046 2047 *rule = tmprule; 2048 2049 return rc; 2050 } 2051 2052 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, 2053 struct selinux_audit_rule *rule, 2054 struct audit_context *actx) 2055 { 2056 struct context *ctxt; 2057 struct mls_level *level; 2058 int match = 0; 2059 2060 if (!rule) { 2061 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, 2062 "selinux_audit_rule_match: missing rule\n"); 2063 return -ENOENT; 2064 } 2065 2066 POLICY_RDLOCK; 2067 2068 if (rule->au_seqno < latest_granting) { 2069 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, 2070 "selinux_audit_rule_match: stale rule\n"); 2071 match = -ESTALE; 2072 goto out; 2073 } 2074 2075 ctxt = sidtab_search(&sidtab, sid); 2076 if (!ctxt) { 2077 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, 2078 "selinux_audit_rule_match: unrecognized SID %d\n", 2079 sid); 2080 match = -ENOENT; 2081 goto out; 2082 } 2083 2084 /* a field/op pair that is not caught here will simply fall through 2085 without a match */ 2086 switch (field) { 2087 case AUDIT_SUBJ_USER: 2088 case AUDIT_OBJ_USER: 2089 switch (op) { 2090 case AUDIT_EQUAL: 2091 match = (ctxt->user == rule->au_ctxt.user); 2092 break; 2093 case AUDIT_NOT_EQUAL: 2094 match = (ctxt->user != rule->au_ctxt.user); 2095 break; 2096 } 2097 break; 2098 case AUDIT_SUBJ_ROLE: 2099 case AUDIT_OBJ_ROLE: 2100 switch (op) { 2101 case AUDIT_EQUAL: 2102 match = (ctxt->role == rule->au_ctxt.role); 2103 break; 2104 case AUDIT_NOT_EQUAL: 2105 match = (ctxt->role != rule->au_ctxt.role); 2106 break; 2107 } 2108 break; 2109 case AUDIT_SUBJ_TYPE: 2110 case AUDIT_OBJ_TYPE: 2111 switch (op) { 2112 case AUDIT_EQUAL: 2113 match = (ctxt->type == rule->au_ctxt.type); 2114 break; 2115 case AUDIT_NOT_EQUAL: 2116 match = (ctxt->type != rule->au_ctxt.type); 2117 break; 2118 } 2119 break; 2120 case AUDIT_SUBJ_SEN: 2121 case AUDIT_SUBJ_CLR: 2122 case AUDIT_OBJ_LEV_LOW: 2123 case AUDIT_OBJ_LEV_HIGH: 2124 level = ((field == AUDIT_SUBJ_SEN || 2125 field == AUDIT_OBJ_LEV_LOW) ? 2126 &ctxt->range.level[0] : &ctxt->range.level[1]); 2127 switch (op) { 2128 case AUDIT_EQUAL: 2129 match = mls_level_eq(&rule->au_ctxt.range.level[0], 2130 level); 2131 break; 2132 case AUDIT_NOT_EQUAL: 2133 match = !mls_level_eq(&rule->au_ctxt.range.level[0], 2134 level); 2135 break; 2136 case AUDIT_LESS_THAN: 2137 match = (mls_level_dom(&rule->au_ctxt.range.level[0], 2138 level) && 2139 !mls_level_eq(&rule->au_ctxt.range.level[0], 2140 level)); 2141 break; 2142 case AUDIT_LESS_THAN_OR_EQUAL: 2143 match = mls_level_dom(&rule->au_ctxt.range.level[0], 2144 level); 2145 break; 2146 case AUDIT_GREATER_THAN: 2147 match = (mls_level_dom(level, 2148 &rule->au_ctxt.range.level[0]) && 2149 !mls_level_eq(level, 2150 &rule->au_ctxt.range.level[0])); 2151 break; 2152 case AUDIT_GREATER_THAN_OR_EQUAL: 2153 match = mls_level_dom(level, 2154 &rule->au_ctxt.range.level[0]); 2155 break; 2156 } 2157 } 2158 2159 out: 2160 POLICY_RDUNLOCK; 2161 return match; 2162 } 2163 2164 static int (*aurule_callback)(void) = NULL; 2165 2166 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid, 2167 u16 class, u32 perms, u32 *retained) 2168 { 2169 int err = 0; 2170 2171 if (event == AVC_CALLBACK_RESET && aurule_callback) 2172 err = aurule_callback(); 2173 return err; 2174 } 2175 2176 static int __init aurule_init(void) 2177 { 2178 int err; 2179 2180 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET, 2181 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0); 2182 if (err) 2183 panic("avc_add_callback() failed, error %d\n", err); 2184 2185 return err; 2186 } 2187 __initcall(aurule_init); 2188 2189 void selinux_audit_set_callback(int (*callback)(void)) 2190 { 2191 aurule_callback = callback; 2192 } 2193 2194 #ifdef CONFIG_NETLABEL 2195 /* 2196 * This is the structure we store inside the NetLabel cache block. 2197 */ 2198 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x)) 2199 #define NETLBL_CACHE_T_NONE 0 2200 #define NETLBL_CACHE_T_SID 1 2201 #define NETLBL_CACHE_T_MLS 2 2202 struct netlbl_cache { 2203 u32 type; 2204 union { 2205 u32 sid; 2206 struct mls_range mls_label; 2207 } data; 2208 }; 2209 2210 /** 2211 * selinux_netlbl_cache_free - Free the NetLabel cached data 2212 * @data: the data to free 2213 * 2214 * Description: 2215 * This function is intended to be used as the free() callback inside the 2216 * netlbl_lsm_cache structure. 2217 * 2218 */ 2219 static void selinux_netlbl_cache_free(const void *data) 2220 { 2221 struct netlbl_cache *cache; 2222 2223 if (data == NULL) 2224 return; 2225 2226 cache = NETLBL_CACHE(data); 2227 switch (cache->type) { 2228 case NETLBL_CACHE_T_MLS: 2229 ebitmap_destroy(&cache->data.mls_label.level[0].cat); 2230 break; 2231 } 2232 kfree(data); 2233 } 2234 2235 /** 2236 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache 2237 * @skb: the packet 2238 * @ctx: the SELinux context 2239 * 2240 * Description: 2241 * Attempt to cache the context in @ctx, which was derived from the packet in 2242 * @skb, in the NetLabel subsystem cache. 2243 * 2244 */ 2245 static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx) 2246 { 2247 struct netlbl_cache *cache = NULL; 2248 struct netlbl_lsm_secattr secattr; 2249 2250 netlbl_secattr_init(&secattr); 2251 secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC); 2252 if (secattr.cache == NULL) 2253 goto netlbl_cache_add_return; 2254 2255 cache = kzalloc(sizeof(*cache), GFP_ATOMIC); 2256 if (cache == NULL) 2257 goto netlbl_cache_add_return; 2258 2259 cache->type = NETLBL_CACHE_T_MLS; 2260 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat, 2261 &ctx->range.level[0].cat) != 0) 2262 goto netlbl_cache_add_return; 2263 cache->data.mls_label.level[1].cat.highbit = 2264 cache->data.mls_label.level[0].cat.highbit; 2265 cache->data.mls_label.level[1].cat.node = 2266 cache->data.mls_label.level[0].cat.node; 2267 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens; 2268 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens; 2269 2270 secattr.cache->free = selinux_netlbl_cache_free; 2271 secattr.cache->data = (void *)cache; 2272 secattr.flags = NETLBL_SECATTR_CACHE; 2273 2274 netlbl_cache_add(skb, &secattr); 2275 2276 netlbl_cache_add_return: 2277 netlbl_secattr_destroy(&secattr); 2278 } 2279 2280 /** 2281 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache 2282 * 2283 * Description: 2284 * Invalidate the NetLabel security attribute mapping cache. 2285 * 2286 */ 2287 void selinux_netlbl_cache_invalidate(void) 2288 { 2289 netlbl_cache_invalidate(); 2290 } 2291 2292 /** 2293 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID 2294 * @skb: the network packet 2295 * @secattr: the NetLabel packet security attributes 2296 * @base_sid: the SELinux SID to use as a context for MLS only attributes 2297 * @sid: the SELinux SID 2298 * 2299 * Description: 2300 * Convert the given NetLabel packet security attributes in @secattr into a 2301 * SELinux SID. If the @secattr field does not contain a full SELinux 2302 * SID/context then use the context in @base_sid as the foundation. If @skb 2303 * is not NULL attempt to cache as much data as possibile. Returns zero on 2304 * success, negative values on failure. 2305 * 2306 */ 2307 static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb, 2308 struct netlbl_lsm_secattr *secattr, 2309 u32 base_sid, 2310 u32 *sid) 2311 { 2312 int rc = -EIDRM; 2313 struct context *ctx; 2314 struct context ctx_new; 2315 struct netlbl_cache *cache; 2316 2317 POLICY_RDLOCK; 2318 2319 if (secattr->flags & NETLBL_SECATTR_CACHE) { 2320 cache = NETLBL_CACHE(secattr->cache->data); 2321 switch (cache->type) { 2322 case NETLBL_CACHE_T_SID: 2323 *sid = cache->data.sid; 2324 rc = 0; 2325 break; 2326 case NETLBL_CACHE_T_MLS: 2327 ctx = sidtab_search(&sidtab, base_sid); 2328 if (ctx == NULL) 2329 goto netlbl_secattr_to_sid_return; 2330 2331 ctx_new.user = ctx->user; 2332 ctx_new.role = ctx->role; 2333 ctx_new.type = ctx->type; 2334 ctx_new.range.level[0].sens = 2335 cache->data.mls_label.level[0].sens; 2336 ctx_new.range.level[0].cat.highbit = 2337 cache->data.mls_label.level[0].cat.highbit; 2338 ctx_new.range.level[0].cat.node = 2339 cache->data.mls_label.level[0].cat.node; 2340 ctx_new.range.level[1].sens = 2341 cache->data.mls_label.level[1].sens; 2342 ctx_new.range.level[1].cat.highbit = 2343 cache->data.mls_label.level[1].cat.highbit; 2344 ctx_new.range.level[1].cat.node = 2345 cache->data.mls_label.level[1].cat.node; 2346 2347 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid); 2348 break; 2349 default: 2350 goto netlbl_secattr_to_sid_return; 2351 } 2352 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) { 2353 ctx = sidtab_search(&sidtab, base_sid); 2354 if (ctx == NULL) 2355 goto netlbl_secattr_to_sid_return; 2356 2357 ctx_new.user = ctx->user; 2358 ctx_new.role = ctx->role; 2359 ctx_new.type = ctx->type; 2360 mls_import_lvl(&ctx_new, secattr->mls_lvl, secattr->mls_lvl); 2361 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 2362 if (mls_import_cat(&ctx_new, 2363 secattr->mls_cat, 2364 secattr->mls_cat_len, 2365 NULL, 2366 0) != 0) 2367 goto netlbl_secattr_to_sid_return; 2368 ctx_new.range.level[1].cat.highbit = 2369 ctx_new.range.level[0].cat.highbit; 2370 ctx_new.range.level[1].cat.node = 2371 ctx_new.range.level[0].cat.node; 2372 } else { 2373 ebitmap_init(&ctx_new.range.level[0].cat); 2374 ebitmap_init(&ctx_new.range.level[1].cat); 2375 } 2376 if (mls_context_isvalid(&policydb, &ctx_new) != 1) 2377 goto netlbl_secattr_to_sid_return_cleanup; 2378 2379 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid); 2380 if (rc != 0) 2381 goto netlbl_secattr_to_sid_return_cleanup; 2382 2383 if (skb != NULL) 2384 selinux_netlbl_cache_add(skb, &ctx_new); 2385 ebitmap_destroy(&ctx_new.range.level[0].cat); 2386 } else { 2387 *sid = SECSID_NULL; 2388 rc = 0; 2389 } 2390 2391 netlbl_secattr_to_sid_return: 2392 POLICY_RDUNLOCK; 2393 return rc; 2394 netlbl_secattr_to_sid_return_cleanup: 2395 ebitmap_destroy(&ctx_new.range.level[0].cat); 2396 goto netlbl_secattr_to_sid_return; 2397 } 2398 2399 /** 2400 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel 2401 * @skb: the packet 2402 * @base_sid: the SELinux SID to use as a context for MLS only attributes 2403 * @sid: the SID 2404 * 2405 * Description: 2406 * Call the NetLabel mechanism to get the security attributes of the given 2407 * packet and use those attributes to determine the correct context/SID to 2408 * assign to the packet. Returns zero on success, negative values on failure. 2409 * 2410 */ 2411 static int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, 2412 u32 base_sid, 2413 u32 *sid) 2414 { 2415 int rc; 2416 struct netlbl_lsm_secattr secattr; 2417 2418 netlbl_secattr_init(&secattr); 2419 rc = netlbl_skbuff_getattr(skb, &secattr); 2420 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) 2421 rc = selinux_netlbl_secattr_to_sid(skb, 2422 &secattr, 2423 base_sid, 2424 sid); 2425 else 2426 *sid = SECSID_NULL; 2427 netlbl_secattr_destroy(&secattr); 2428 2429 return rc; 2430 } 2431 2432 /** 2433 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism 2434 * @sock: the socket to label 2435 * @sid: the SID to use 2436 * 2437 * Description: 2438 * Attempt to label a socket using the NetLabel mechanism using the given 2439 * SID. Returns zero values on success, negative values on failure. The 2440 * caller is responsibile for calling rcu_read_lock() before calling this 2441 * this function and rcu_read_unlock() after this function returns. 2442 * 2443 */ 2444 static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid) 2445 { 2446 int rc = -ENOENT; 2447 struct sk_security_struct *sksec = sock->sk->sk_security; 2448 struct netlbl_lsm_secattr secattr; 2449 struct context *ctx; 2450 2451 if (!ss_initialized) 2452 return 0; 2453 2454 netlbl_secattr_init(&secattr); 2455 2456 POLICY_RDLOCK; 2457 2458 ctx = sidtab_search(&sidtab, sid); 2459 if (ctx == NULL) 2460 goto netlbl_socket_setsid_return; 2461 2462 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1], 2463 GFP_ATOMIC); 2464 mls_export_lvl(ctx, &secattr.mls_lvl, NULL); 2465 rc = mls_export_cat(ctx, 2466 &secattr.mls_cat, 2467 &secattr.mls_cat_len, 2468 NULL, 2469 NULL); 2470 if (rc != 0) 2471 goto netlbl_socket_setsid_return; 2472 2473 secattr.flags |= NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL; 2474 if (secattr.mls_cat) 2475 secattr.flags |= NETLBL_SECATTR_MLS_CAT; 2476 2477 rc = netlbl_socket_setattr(sock, &secattr); 2478 if (rc == 0) { 2479 spin_lock(&sksec->nlbl_lock); 2480 sksec->nlbl_state = NLBL_LABELED; 2481 spin_unlock(&sksec->nlbl_lock); 2482 } 2483 2484 netlbl_socket_setsid_return: 2485 POLICY_RDUNLOCK; 2486 netlbl_secattr_destroy(&secattr); 2487 return rc; 2488 } 2489 2490 /** 2491 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields 2492 * @ssec: the sk_security_struct 2493 * @family: the socket family 2494 * 2495 * Description: 2496 * Called when the NetLabel state of a sk_security_struct needs to be reset. 2497 * The caller is responsibile for all the NetLabel sk_security_struct locking. 2498 * 2499 */ 2500 void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec, 2501 int family) 2502 { 2503 if (family == PF_INET) 2504 ssec->nlbl_state = NLBL_REQUIRE; 2505 else 2506 ssec->nlbl_state = NLBL_UNSET; 2507 } 2508 2509 /** 2510 * selinux_netlbl_sk_security_init - Setup the NetLabel fields 2511 * @ssec: the sk_security_struct 2512 * @family: the socket family 2513 * 2514 * Description: 2515 * Called when a new sk_security_struct is allocated to initialize the NetLabel 2516 * fields. 2517 * 2518 */ 2519 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec, 2520 int family) 2521 { 2522 /* No locking needed, we are the only one who has access to ssec */ 2523 selinux_netlbl_sk_security_reset(ssec, family); 2524 spin_lock_init(&ssec->nlbl_lock); 2525 } 2526 2527 /** 2528 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields 2529 * @ssec: the original sk_security_struct 2530 * @newssec: the cloned sk_security_struct 2531 * 2532 * Description: 2533 * Clone the NetLabel specific sk_security_struct fields from @ssec to 2534 * @newssec. 2535 * 2536 */ 2537 void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec, 2538 struct sk_security_struct *newssec) 2539 { 2540 /* We don't need to take newssec->nlbl_lock because we are the only 2541 * thread with access to newssec, but we do need to take the RCU read 2542 * lock as other threads could have access to ssec */ 2543 rcu_read_lock(); 2544 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family); 2545 newssec->sclass = ssec->sclass; 2546 rcu_read_unlock(); 2547 } 2548 2549 /** 2550 * selinux_netlbl_socket_post_create - Label a socket using NetLabel 2551 * @sock: the socket to label 2552 * 2553 * Description: 2554 * Attempt to label a socket using the NetLabel mechanism using the given 2555 * SID. Returns zero values on success, negative values on failure. 2556 * 2557 */ 2558 int selinux_netlbl_socket_post_create(struct socket *sock) 2559 { 2560 int rc = 0; 2561 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 2562 struct sk_security_struct *sksec = sock->sk->sk_security; 2563 2564 sksec->sclass = isec->sclass; 2565 2566 rcu_read_lock(); 2567 if (sksec->nlbl_state == NLBL_REQUIRE) 2568 rc = selinux_netlbl_socket_setsid(sock, sksec->sid); 2569 rcu_read_unlock(); 2570 2571 return rc; 2572 } 2573 2574 /** 2575 * selinux_netlbl_sock_graft - Netlabel the new socket 2576 * @sk: the new connection 2577 * @sock: the new socket 2578 * 2579 * Description: 2580 * The connection represented by @sk is being grafted onto @sock so set the 2581 * socket's NetLabel to match the SID of @sk. 2582 * 2583 */ 2584 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock) 2585 { 2586 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 2587 struct sk_security_struct *sksec = sk->sk_security; 2588 struct netlbl_lsm_secattr secattr; 2589 u32 nlbl_peer_sid; 2590 2591 sksec->sclass = isec->sclass; 2592 2593 rcu_read_lock(); 2594 2595 if (sksec->nlbl_state != NLBL_REQUIRE) { 2596 rcu_read_unlock(); 2597 return; 2598 } 2599 2600 netlbl_secattr_init(&secattr); 2601 if (netlbl_sock_getattr(sk, &secattr) == 0 && 2602 secattr.flags != NETLBL_SECATTR_NONE && 2603 selinux_netlbl_secattr_to_sid(NULL, 2604 &secattr, 2605 SECINITSID_UNLABELED, 2606 &nlbl_peer_sid) == 0) 2607 sksec->peer_sid = nlbl_peer_sid; 2608 netlbl_secattr_destroy(&secattr); 2609 2610 /* Try to set the NetLabel on the socket to save time later, if we fail 2611 * here we will pick up the pieces in later calls to 2612 * selinux_netlbl_inode_permission(). */ 2613 selinux_netlbl_socket_setsid(sock, sksec->sid); 2614 2615 rcu_read_unlock(); 2616 } 2617 2618 /** 2619 * selinux_netlbl_inet_conn_request - Handle a new connection request 2620 * @skb: the packet 2621 * @sock_sid: the SID of the parent socket 2622 * 2623 * Description: 2624 * If present, use the security attributes of the packet in @skb and the 2625 * parent sock's SID to arrive at a SID for the new child sock. Returns the 2626 * SID of the connection or SECSID_NULL on failure. 2627 * 2628 */ 2629 u32 selinux_netlbl_inet_conn_request(struct sk_buff *skb, u32 sock_sid) 2630 { 2631 int rc; 2632 u32 peer_sid; 2633 2634 rc = selinux_netlbl_skbuff_getsid(skb, sock_sid, &peer_sid); 2635 if (rc != 0) 2636 return SECSID_NULL; 2637 2638 return peer_sid; 2639 } 2640 2641 /** 2642 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled 2643 * @inode: the file descriptor's inode 2644 * @mask: the permission mask 2645 * 2646 * Description: 2647 * Looks at a file's inode and if it is marked as a socket protected by 2648 * NetLabel then verify that the socket has been labeled, if not try to label 2649 * the socket now with the inode's SID. Returns zero on success, negative 2650 * values on failure. 2651 * 2652 */ 2653 int selinux_netlbl_inode_permission(struct inode *inode, int mask) 2654 { 2655 int rc; 2656 struct sk_security_struct *sksec; 2657 struct socket *sock; 2658 2659 if (!S_ISSOCK(inode->i_mode) || 2660 ((mask & (MAY_WRITE | MAY_APPEND)) == 0)) 2661 return 0; 2662 sock = SOCKET_I(inode); 2663 sksec = sock->sk->sk_security; 2664 2665 rcu_read_lock(); 2666 if (sksec->nlbl_state != NLBL_REQUIRE) { 2667 rcu_read_unlock(); 2668 return 0; 2669 } 2670 lock_sock(sock->sk); 2671 rc = selinux_netlbl_socket_setsid(sock, sksec->sid); 2672 release_sock(sock->sk); 2673 rcu_read_unlock(); 2674 2675 return rc; 2676 } 2677 2678 /** 2679 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel 2680 * @sksec: the sock's sk_security_struct 2681 * @skb: the packet 2682 * @ad: the audit data 2683 * 2684 * Description: 2685 * Fetch the NetLabel security attributes from @skb and perform an access check 2686 * against the receiving socket. Returns zero on success, negative values on 2687 * error. 2688 * 2689 */ 2690 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec, 2691 struct sk_buff *skb, 2692 struct avc_audit_data *ad) 2693 { 2694 int rc; 2695 u32 netlbl_sid; 2696 u32 recv_perm; 2697 2698 rc = selinux_netlbl_skbuff_getsid(skb, 2699 SECINITSID_UNLABELED, 2700 &netlbl_sid); 2701 if (rc != 0) 2702 return rc; 2703 2704 if (netlbl_sid == SECSID_NULL) 2705 return 0; 2706 2707 switch (sksec->sclass) { 2708 case SECCLASS_UDP_SOCKET: 2709 recv_perm = UDP_SOCKET__RECVFROM; 2710 break; 2711 case SECCLASS_TCP_SOCKET: 2712 recv_perm = TCP_SOCKET__RECVFROM; 2713 break; 2714 default: 2715 recv_perm = RAWIP_SOCKET__RECVFROM; 2716 } 2717 2718 rc = avc_has_perm(sksec->sid, 2719 netlbl_sid, 2720 sksec->sclass, 2721 recv_perm, 2722 ad); 2723 if (rc == 0) 2724 return 0; 2725 2726 netlbl_skbuff_err(skb, rc); 2727 return rc; 2728 } 2729 2730 /** 2731 * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID 2732 * @sock: the socket 2733 * 2734 * Description: 2735 * Examine @sock to find the connected peer's SID. Returns the SID on success 2736 * or SECSID_NULL on error. 2737 * 2738 */ 2739 u32 selinux_netlbl_socket_getpeersec_stream(struct socket *sock) 2740 { 2741 struct sk_security_struct *sksec = sock->sk->sk_security; 2742 return sksec->peer_sid; 2743 } 2744 2745 /** 2746 * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet 2747 * @skb: the packet 2748 * 2749 * Description: 2750 * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on 2751 * success, SECSID_NULL on error. 2752 * 2753 */ 2754 u32 selinux_netlbl_socket_getpeersec_dgram(struct sk_buff *skb) 2755 { 2756 int peer_sid; 2757 2758 if (selinux_netlbl_skbuff_getsid(skb, 2759 SECINITSID_UNLABELED, 2760 &peer_sid) != 0) 2761 return SECSID_NULL; 2762 2763 return peer_sid; 2764 } 2765 2766 /** 2767 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel 2768 * @sock: the socket 2769 * @level: the socket level or protocol 2770 * @optname: the socket option name 2771 * 2772 * Description: 2773 * Check the setsockopt() call and if the user is trying to replace the IP 2774 * options on a socket and a NetLabel is in place for the socket deny the 2775 * access; otherwise allow the access. Returns zero when the access is 2776 * allowed, -EACCES when denied, and other negative values on error. 2777 * 2778 */ 2779 int selinux_netlbl_socket_setsockopt(struct socket *sock, 2780 int level, 2781 int optname) 2782 { 2783 int rc = 0; 2784 struct sk_security_struct *sksec = sock->sk->sk_security; 2785 struct netlbl_lsm_secattr secattr; 2786 2787 rcu_read_lock(); 2788 if (level == IPPROTO_IP && optname == IP_OPTIONS && 2789 sksec->nlbl_state == NLBL_LABELED) { 2790 netlbl_secattr_init(&secattr); 2791 rc = netlbl_socket_getattr(sock, &secattr); 2792 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) 2793 rc = -EACCES; 2794 netlbl_secattr_destroy(&secattr); 2795 } 2796 rcu_read_unlock(); 2797 2798 return rc; 2799 } 2800 #endif /* CONFIG_NETLABEL */ 2801