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