2 * Implementation of the security services.
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
14 * Added conditional policy language extensions
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
23 * Added validation of kernel classes and permissions
27 * Added support for bounds domain and audit messaged on masked permissions
31 * Added support for runtime switching of the policy type
33 * Copyright (C) 2008, 2009 NEC Corporation
34 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
35 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
36 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, version 2.
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/spinlock.h>
46 #include <linux/rcupdate.h>
47 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/audit.h>
51 #include <linux/mutex.h>
52 #include <linux/selinux.h>
53 #include <linux/flex_array.h>
54 #include <linux/vmalloc.h>
55 #include <net/netlabel.h>
65 #include "conditional.h"
73 /* Policy capability names */
74 char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
75 "network_peer_controls",
77 "extended_socket_class",
78 "always_check_network",
80 "nnp_nosuid_transition"
83 static struct selinux_ss selinux_ss;
85 void selinux_ss_init(struct selinux_ss **ss)
87 rwlock_init(&selinux_ss.policy_rwlock);
88 mutex_init(&selinux_ss.status_lock);
92 /* Forward declaration. */
93 static int context_struct_to_string(struct policydb *policydb,
94 struct context *context,
98 static void context_struct_compute_av(struct policydb *policydb,
99 struct context *scontext,
100 struct context *tcontext,
102 struct av_decision *avd,
103 struct extended_perms *xperms);
105 static int selinux_set_mapping(struct policydb *pol,
106 struct security_class_mapping *map,
107 struct selinux_map *out_map)
111 bool print_unknown_handle = false;
113 /* Find number of classes in the input mapping */
120 /* Allocate space for the class records, plus one for class zero */
121 out_map->mapping = kcalloc(++i, sizeof(*out_map->mapping), GFP_ATOMIC);
122 if (!out_map->mapping)
125 /* Store the raw class and permission values */
127 while (map[j].name) {
128 struct security_class_mapping *p_in = map + (j++);
129 struct selinux_mapping *p_out = out_map->mapping + j;
131 /* An empty class string skips ahead */
132 if (!strcmp(p_in->name, "")) {
133 p_out->num_perms = 0;
137 p_out->value = string_to_security_class(pol, p_in->name);
139 pr_info("SELinux: Class %s not defined in policy.\n",
141 if (pol->reject_unknown)
143 p_out->num_perms = 0;
144 print_unknown_handle = true;
149 while (p_in->perms[k]) {
150 /* An empty permission string skips ahead */
151 if (!*p_in->perms[k]) {
155 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
157 if (!p_out->perms[k]) {
158 pr_info("SELinux: Permission %s in class %s not defined in policy.\n",
159 p_in->perms[k], p_in->name);
160 if (pol->reject_unknown)
162 print_unknown_handle = true;
167 p_out->num_perms = k;
170 if (print_unknown_handle)
171 pr_info("SELinux: the above unknown classes and permissions will be %s\n",
172 pol->allow_unknown ? "allowed" : "denied");
177 kfree(out_map->mapping);
178 out_map->mapping = NULL;
183 * Get real, policy values from mapped values
186 static u16 unmap_class(struct selinux_map *map, u16 tclass)
188 if (tclass < map->size)
189 return map->mapping[tclass].value;
195 * Get kernel value for class from its policy value
197 static u16 map_class(struct selinux_map *map, u16 pol_value)
201 for (i = 1; i < map->size; i++) {
202 if (map->mapping[i].value == pol_value)
206 return SECCLASS_NULL;
209 static void map_decision(struct selinux_map *map,
210 u16 tclass, struct av_decision *avd,
213 if (tclass < map->size) {
214 struct selinux_mapping *mapping = &map->mapping[tclass];
215 unsigned int i, n = mapping->num_perms;
218 for (i = 0, result = 0; i < n; i++) {
219 if (avd->allowed & mapping->perms[i])
221 if (allow_unknown && !mapping->perms[i])
224 avd->allowed = result;
226 for (i = 0, result = 0; i < n; i++)
227 if (avd->auditallow & mapping->perms[i])
229 avd->auditallow = result;
231 for (i = 0, result = 0; i < n; i++) {
232 if (avd->auditdeny & mapping->perms[i])
234 if (!allow_unknown && !mapping->perms[i])
238 * In case the kernel has a bug and requests a permission
239 * between num_perms and the maximum permission number, we
240 * should audit that denial
242 for (; i < (sizeof(u32)*8); i++)
244 avd->auditdeny = result;
248 int security_mls_enabled(struct selinux_state *state)
250 struct policydb *p = &state->ss->policydb;
252 return p->mls_enabled;
256 * Return the boolean value of a constraint expression
257 * when it is applied to the specified source and target
260 * xcontext is a special beast... It is used by the validatetrans rules
261 * only. For these rules, scontext is the context before the transition,
262 * tcontext is the context after the transition, and xcontext is the context
263 * of the process performing the transition. All other callers of
264 * constraint_expr_eval should pass in NULL for xcontext.
266 static int constraint_expr_eval(struct policydb *policydb,
267 struct context *scontext,
268 struct context *tcontext,
269 struct context *xcontext,
270 struct constraint_expr *cexpr)
274 struct role_datum *r1, *r2;
275 struct mls_level *l1, *l2;
276 struct constraint_expr *e;
277 int s[CEXPR_MAXDEPTH];
280 for (e = cexpr; e; e = e->next) {
281 switch (e->expr_type) {
297 if (sp == (CEXPR_MAXDEPTH - 1))
301 val1 = scontext->user;
302 val2 = tcontext->user;
305 val1 = scontext->type;
306 val2 = tcontext->type;
309 val1 = scontext->role;
310 val2 = tcontext->role;
311 r1 = policydb->role_val_to_struct[val1 - 1];
312 r2 = policydb->role_val_to_struct[val2 - 1];
315 s[++sp] = ebitmap_get_bit(&r1->dominates,
319 s[++sp] = ebitmap_get_bit(&r2->dominates,
323 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
325 !ebitmap_get_bit(&r2->dominates,
333 l1 = &(scontext->range.level[0]);
334 l2 = &(tcontext->range.level[0]);
337 l1 = &(scontext->range.level[0]);
338 l2 = &(tcontext->range.level[1]);
341 l1 = &(scontext->range.level[1]);
342 l2 = &(tcontext->range.level[0]);
345 l1 = &(scontext->range.level[1]);
346 l2 = &(tcontext->range.level[1]);
349 l1 = &(scontext->range.level[0]);
350 l2 = &(scontext->range.level[1]);
353 l1 = &(tcontext->range.level[0]);
354 l2 = &(tcontext->range.level[1]);
359 s[++sp] = mls_level_eq(l1, l2);
362 s[++sp] = !mls_level_eq(l1, l2);
365 s[++sp] = mls_level_dom(l1, l2);
368 s[++sp] = mls_level_dom(l2, l1);
371 s[++sp] = mls_level_incomp(l2, l1);
385 s[++sp] = (val1 == val2);
388 s[++sp] = (val1 != val2);
396 if (sp == (CEXPR_MAXDEPTH-1))
399 if (e->attr & CEXPR_TARGET)
401 else if (e->attr & CEXPR_XTARGET) {
408 if (e->attr & CEXPR_USER)
410 else if (e->attr & CEXPR_ROLE)
412 else if (e->attr & CEXPR_TYPE)
421 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
424 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
442 * security_dump_masked_av - dumps masked permissions during
443 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
445 static int dump_masked_av_helper(void *k, void *d, void *args)
447 struct perm_datum *pdatum = d;
448 char **permission_names = args;
450 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
452 permission_names[pdatum->value - 1] = (char *)k;
457 static void security_dump_masked_av(struct policydb *policydb,
458 struct context *scontext,
459 struct context *tcontext,
464 struct common_datum *common_dat;
465 struct class_datum *tclass_dat;
466 struct audit_buffer *ab;
468 char *scontext_name = NULL;
469 char *tcontext_name = NULL;
470 char *permission_names[32];
473 bool need_comma = false;
478 tclass_name = sym_name(policydb, SYM_CLASSES, tclass - 1);
479 tclass_dat = policydb->class_val_to_struct[tclass - 1];
480 common_dat = tclass_dat->comdatum;
482 /* init permission_names */
484 hashtab_map(common_dat->permissions.table,
485 dump_masked_av_helper, permission_names) < 0)
488 if (hashtab_map(tclass_dat->permissions.table,
489 dump_masked_av_helper, permission_names) < 0)
492 /* get scontext/tcontext in text form */
493 if (context_struct_to_string(policydb, scontext,
494 &scontext_name, &length) < 0)
497 if (context_struct_to_string(policydb, tcontext,
498 &tcontext_name, &length) < 0)
501 /* audit a message */
502 ab = audit_log_start(audit_context(),
503 GFP_ATOMIC, AUDIT_SELINUX_ERR);
507 audit_log_format(ab, "op=security_compute_av reason=%s "
508 "scontext=%s tcontext=%s tclass=%s perms=",
509 reason, scontext_name, tcontext_name, tclass_name);
511 for (index = 0; index < 32; index++) {
512 u32 mask = (1 << index);
514 if ((mask & permissions) == 0)
517 audit_log_format(ab, "%s%s",
518 need_comma ? "," : "",
519 permission_names[index]
520 ? permission_names[index] : "????");
525 /* release scontext/tcontext */
526 kfree(tcontext_name);
527 kfree(scontext_name);
533 * security_boundary_permission - drops violated permissions
534 * on boundary constraint.
536 static void type_attribute_bounds_av(struct policydb *policydb,
537 struct context *scontext,
538 struct context *tcontext,
540 struct av_decision *avd)
542 struct context lo_scontext;
543 struct context lo_tcontext, *tcontextp = tcontext;
544 struct av_decision lo_avd;
545 struct type_datum *source;
546 struct type_datum *target;
549 source = flex_array_get_ptr(policydb->type_val_to_struct_array,
556 target = flex_array_get_ptr(policydb->type_val_to_struct_array,
560 memset(&lo_avd, 0, sizeof(lo_avd));
562 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
563 lo_scontext.type = source->bounds;
565 if (target->bounds) {
566 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
567 lo_tcontext.type = target->bounds;
568 tcontextp = &lo_tcontext;
571 context_struct_compute_av(policydb, &lo_scontext,
577 masked = ~lo_avd.allowed & avd->allowed;
580 return; /* no masked permission */
582 /* mask violated permissions */
583 avd->allowed &= ~masked;
585 /* audit masked permissions */
586 security_dump_masked_av(policydb, scontext, tcontext,
587 tclass, masked, "bounds");
591 * flag which drivers have permissions
592 * only looking for ioctl based extended permssions
594 void services_compute_xperms_drivers(
595 struct extended_perms *xperms,
596 struct avtab_node *node)
600 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
601 /* if one or more driver has all permissions allowed */
602 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
603 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
604 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
605 /* if allowing permissions within a driver */
606 security_xperm_set(xperms->drivers.p,
607 node->datum.u.xperms->driver);
610 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
611 if (node->key.specified & AVTAB_XPERMS_ALLOWED)
616 * Compute access vectors and extended permissions based on a context
617 * structure pair for the permissions in a particular class.
619 static void context_struct_compute_av(struct policydb *policydb,
620 struct context *scontext,
621 struct context *tcontext,
623 struct av_decision *avd,
624 struct extended_perms *xperms)
626 struct constraint_node *constraint;
627 struct role_allow *ra;
628 struct avtab_key avkey;
629 struct avtab_node *node;
630 struct class_datum *tclass_datum;
631 struct ebitmap *sattr, *tattr;
632 struct ebitmap_node *snode, *tnode;
637 avd->auditdeny = 0xffffffff;
639 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
643 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
644 if (printk_ratelimit())
645 pr_warn("SELinux: Invalid class %hu\n", tclass);
649 tclass_datum = policydb->class_val_to_struct[tclass - 1];
652 * If a specific type enforcement rule was defined for
653 * this permission check, then use it.
655 avkey.target_class = tclass;
656 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
657 sattr = flex_array_get(policydb->type_attr_map_array,
660 tattr = flex_array_get(policydb->type_attr_map_array,
663 ebitmap_for_each_positive_bit(sattr, snode, i) {
664 ebitmap_for_each_positive_bit(tattr, tnode, j) {
665 avkey.source_type = i + 1;
666 avkey.target_type = j + 1;
667 for (node = avtab_search_node(&policydb->te_avtab,
670 node = avtab_search_node_next(node, avkey.specified)) {
671 if (node->key.specified == AVTAB_ALLOWED)
672 avd->allowed |= node->datum.u.data;
673 else if (node->key.specified == AVTAB_AUDITALLOW)
674 avd->auditallow |= node->datum.u.data;
675 else if (node->key.specified == AVTAB_AUDITDENY)
676 avd->auditdeny &= node->datum.u.data;
677 else if (xperms && (node->key.specified & AVTAB_XPERMS))
678 services_compute_xperms_drivers(xperms, node);
681 /* Check conditional av table for additional permissions */
682 cond_compute_av(&policydb->te_cond_avtab, &avkey,
689 * Remove any permissions prohibited by a constraint (this includes
692 constraint = tclass_datum->constraints;
694 if ((constraint->permissions & (avd->allowed)) &&
695 !constraint_expr_eval(policydb, scontext, tcontext, NULL,
697 avd->allowed &= ~(constraint->permissions);
699 constraint = constraint->next;
703 * If checking process transition permission and the
704 * role is changing, then check the (current_role, new_role)
707 if (tclass == policydb->process_class &&
708 (avd->allowed & policydb->process_trans_perms) &&
709 scontext->role != tcontext->role) {
710 for (ra = policydb->role_allow; ra; ra = ra->next) {
711 if (scontext->role == ra->role &&
712 tcontext->role == ra->new_role)
716 avd->allowed &= ~policydb->process_trans_perms;
720 * If the given source and target types have boundary
721 * constraint, lazy checks have to mask any violated
722 * permission and notice it to userspace via audit.
724 type_attribute_bounds_av(policydb, scontext, tcontext,
728 static int security_validtrans_handle_fail(struct selinux_state *state,
729 struct context *ocontext,
730 struct context *ncontext,
731 struct context *tcontext,
734 struct policydb *p = &state->ss->policydb;
735 char *o = NULL, *n = NULL, *t = NULL;
736 u32 olen, nlen, tlen;
738 if (context_struct_to_string(p, ocontext, &o, &olen))
740 if (context_struct_to_string(p, ncontext, &n, &nlen))
742 if (context_struct_to_string(p, tcontext, &t, &tlen))
744 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
745 "op=security_validate_transition seresult=denied"
746 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
747 o, n, t, sym_name(p, SYM_CLASSES, tclass-1));
753 if (!enforcing_enabled(state))
758 static int security_compute_validatetrans(struct selinux_state *state,
759 u32 oldsid, u32 newsid, u32 tasksid,
760 u16 orig_tclass, bool user)
762 struct policydb *policydb;
763 struct sidtab *sidtab;
764 struct context *ocontext;
765 struct context *ncontext;
766 struct context *tcontext;
767 struct class_datum *tclass_datum;
768 struct constraint_node *constraint;
773 if (!state->initialized)
776 read_lock(&state->ss->policy_rwlock);
778 policydb = &state->ss->policydb;
779 sidtab = &state->ss->sidtab;
782 tclass = unmap_class(&state->ss->map, orig_tclass);
784 tclass = orig_tclass;
786 if (!tclass || tclass > policydb->p_classes.nprim) {
790 tclass_datum = policydb->class_val_to_struct[tclass - 1];
792 ocontext = sidtab_search(sidtab, oldsid);
794 pr_err("SELinux: %s: unrecognized SID %d\n",
800 ncontext = sidtab_search(sidtab, newsid);
802 pr_err("SELinux: %s: unrecognized SID %d\n",
808 tcontext = sidtab_search(sidtab, tasksid);
810 pr_err("SELinux: %s: unrecognized SID %d\n",
816 constraint = tclass_datum->validatetrans;
818 if (!constraint_expr_eval(policydb, ocontext, ncontext,
819 tcontext, constraint->expr)) {
823 rc = security_validtrans_handle_fail(state,
830 constraint = constraint->next;
834 read_unlock(&state->ss->policy_rwlock);
838 int security_validate_transition_user(struct selinux_state *state,
839 u32 oldsid, u32 newsid, u32 tasksid,
842 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
846 int security_validate_transition(struct selinux_state *state,
847 u32 oldsid, u32 newsid, u32 tasksid,
850 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
855 * security_bounded_transition - check whether the given
856 * transition is directed to bounded, or not.
857 * It returns 0, if @newsid is bounded by @oldsid.
858 * Otherwise, it returns error code.
860 * @oldsid : current security identifier
861 * @newsid : destinated security identifier
863 int security_bounded_transition(struct selinux_state *state,
864 u32 old_sid, u32 new_sid)
866 struct policydb *policydb;
867 struct sidtab *sidtab;
868 struct context *old_context, *new_context;
869 struct type_datum *type;
873 if (!state->initialized)
876 read_lock(&state->ss->policy_rwlock);
878 policydb = &state->ss->policydb;
879 sidtab = &state->ss->sidtab;
882 old_context = sidtab_search(sidtab, old_sid);
884 pr_err("SELinux: %s: unrecognized SID %u\n",
890 new_context = sidtab_search(sidtab, new_sid);
892 pr_err("SELinux: %s: unrecognized SID %u\n",
898 /* type/domain unchanged */
899 if (old_context->type == new_context->type)
902 index = new_context->type;
904 type = flex_array_get_ptr(policydb->type_val_to_struct_array,
908 /* not bounded anymore */
913 /* @newsid is bounded by @oldsid */
915 if (type->bounds == old_context->type)
918 index = type->bounds;
922 char *old_name = NULL;
923 char *new_name = NULL;
926 if (!context_struct_to_string(policydb, old_context,
927 &old_name, &length) &&
928 !context_struct_to_string(policydb, new_context,
929 &new_name, &length)) {
930 audit_log(audit_context(),
931 GFP_ATOMIC, AUDIT_SELINUX_ERR,
932 "op=security_bounded_transition "
934 "oldcontext=%s newcontext=%s",
941 read_unlock(&state->ss->policy_rwlock);
946 static void avd_init(struct selinux_state *state, struct av_decision *avd)
950 avd->auditdeny = 0xffffffff;
951 avd->seqno = state->ss->latest_granting;
955 void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
956 struct avtab_node *node)
960 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
961 if (xpermd->driver != node->datum.u.xperms->driver)
963 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
964 if (!security_xperm_test(node->datum.u.xperms->perms.p,
971 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
972 xpermd->used |= XPERMS_ALLOWED;
973 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
974 memset(xpermd->allowed->p, 0xff,
975 sizeof(xpermd->allowed->p));
977 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
978 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
979 xpermd->allowed->p[i] |=
980 node->datum.u.xperms->perms.p[i];
982 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
983 xpermd->used |= XPERMS_AUDITALLOW;
984 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
985 memset(xpermd->auditallow->p, 0xff,
986 sizeof(xpermd->auditallow->p));
988 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
989 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
990 xpermd->auditallow->p[i] |=
991 node->datum.u.xperms->perms.p[i];
993 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
994 xpermd->used |= XPERMS_DONTAUDIT;
995 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
996 memset(xpermd->dontaudit->p, 0xff,
997 sizeof(xpermd->dontaudit->p));
999 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
1000 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
1001 xpermd->dontaudit->p[i] |=
1002 node->datum.u.xperms->perms.p[i];
1009 void security_compute_xperms_decision(struct selinux_state *state,
1014 struct extended_perms_decision *xpermd)
1016 struct policydb *policydb;
1017 struct sidtab *sidtab;
1019 struct context *scontext, *tcontext;
1020 struct avtab_key avkey;
1021 struct avtab_node *node;
1022 struct ebitmap *sattr, *tattr;
1023 struct ebitmap_node *snode, *tnode;
1026 xpermd->driver = driver;
1028 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1029 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1030 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1032 read_lock(&state->ss->policy_rwlock);
1033 if (!state->initialized)
1036 policydb = &state->ss->policydb;
1037 sidtab = &state->ss->sidtab;
1039 scontext = sidtab_search(sidtab, ssid);
1041 pr_err("SELinux: %s: unrecognized SID %d\n",
1046 tcontext = sidtab_search(sidtab, tsid);
1048 pr_err("SELinux: %s: unrecognized SID %d\n",
1053 tclass = unmap_class(&state->ss->map, orig_tclass);
1054 if (unlikely(orig_tclass && !tclass)) {
1055 if (policydb->allow_unknown)
1061 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
1062 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1066 avkey.target_class = tclass;
1067 avkey.specified = AVTAB_XPERMS;
1068 sattr = flex_array_get(policydb->type_attr_map_array,
1069 scontext->type - 1);
1071 tattr = flex_array_get(policydb->type_attr_map_array,
1072 tcontext->type - 1);
1074 ebitmap_for_each_positive_bit(sattr, snode, i) {
1075 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1076 avkey.source_type = i + 1;
1077 avkey.target_type = j + 1;
1078 for (node = avtab_search_node(&policydb->te_avtab,
1081 node = avtab_search_node_next(node, avkey.specified))
1082 services_compute_xperms_decision(xpermd, node);
1084 cond_compute_xperms(&policydb->te_cond_avtab,
1089 read_unlock(&state->ss->policy_rwlock);
1092 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1097 * security_compute_av - Compute access vector decisions.
1098 * @ssid: source security identifier
1099 * @tsid: target security identifier
1100 * @tclass: target security class
1101 * @avd: access vector decisions
1102 * @xperms: extended permissions
1104 * Compute a set of access vector decisions based on the
1105 * SID pair (@ssid, @tsid) for the permissions in @tclass.
1107 void security_compute_av(struct selinux_state *state,
1111 struct av_decision *avd,
1112 struct extended_perms *xperms)
1114 struct policydb *policydb;
1115 struct sidtab *sidtab;
1117 struct context *scontext = NULL, *tcontext = NULL;
1119 read_lock(&state->ss->policy_rwlock);
1120 avd_init(state, avd);
1122 if (!state->initialized)
1125 policydb = &state->ss->policydb;
1126 sidtab = &state->ss->sidtab;
1128 scontext = sidtab_search(sidtab, ssid);
1130 pr_err("SELinux: %s: unrecognized SID %d\n",
1135 /* permissive domain? */
1136 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1137 avd->flags |= AVD_FLAGS_PERMISSIVE;
1139 tcontext = sidtab_search(sidtab, tsid);
1141 pr_err("SELinux: %s: unrecognized SID %d\n",
1146 tclass = unmap_class(&state->ss->map, orig_tclass);
1147 if (unlikely(orig_tclass && !tclass)) {
1148 if (policydb->allow_unknown)
1152 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1154 map_decision(&state->ss->map, orig_tclass, avd,
1155 policydb->allow_unknown);
1157 read_unlock(&state->ss->policy_rwlock);
1160 avd->allowed = 0xffffffff;
1164 void security_compute_av_user(struct selinux_state *state,
1168 struct av_decision *avd)
1170 struct policydb *policydb;
1171 struct sidtab *sidtab;
1172 struct context *scontext = NULL, *tcontext = NULL;
1174 read_lock(&state->ss->policy_rwlock);
1175 avd_init(state, avd);
1176 if (!state->initialized)
1179 policydb = &state->ss->policydb;
1180 sidtab = &state->ss->sidtab;
1182 scontext = sidtab_search(sidtab, ssid);
1184 pr_err("SELinux: %s: unrecognized SID %d\n",
1189 /* permissive domain? */
1190 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1191 avd->flags |= AVD_FLAGS_PERMISSIVE;
1193 tcontext = sidtab_search(sidtab, tsid);
1195 pr_err("SELinux: %s: unrecognized SID %d\n",
1200 if (unlikely(!tclass)) {
1201 if (policydb->allow_unknown)
1206 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1209 read_unlock(&state->ss->policy_rwlock);
1212 avd->allowed = 0xffffffff;
1217 * Write the security context string representation of
1218 * the context structure `context' into a dynamically
1219 * allocated string of the correct size. Set `*scontext'
1220 * to point to this string and set `*scontext_len' to
1221 * the length of the string.
1223 static int context_struct_to_string(struct policydb *p,
1224 struct context *context,
1225 char **scontext, u32 *scontext_len)
1234 *scontext_len = context->len;
1236 *scontext = kstrdup(context->str, GFP_ATOMIC);
1243 /* Compute the size of the context. */
1244 *scontext_len += strlen(sym_name(p, SYM_USERS, context->user - 1)) + 1;
1245 *scontext_len += strlen(sym_name(p, SYM_ROLES, context->role - 1)) + 1;
1246 *scontext_len += strlen(sym_name(p, SYM_TYPES, context->type - 1)) + 1;
1247 *scontext_len += mls_compute_context_len(p, context);
1252 /* Allocate space for the context; caller must free this space. */
1253 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1256 *scontext = scontextp;
1259 * Copy the user name, role name and type name into the context.
1261 scontextp += sprintf(scontextp, "%s:%s:%s",
1262 sym_name(p, SYM_USERS, context->user - 1),
1263 sym_name(p, SYM_ROLES, context->role - 1),
1264 sym_name(p, SYM_TYPES, context->type - 1));
1266 mls_sid_to_context(p, context, &scontextp);
1273 #include "initial_sid_to_string.h"
1275 const char *security_get_initial_sid_context(u32 sid)
1277 if (unlikely(sid > SECINITSID_NUM))
1279 return initial_sid_to_string[sid];
1282 static int security_sid_to_context_core(struct selinux_state *state,
1283 u32 sid, char **scontext,
1284 u32 *scontext_len, int force)
1286 struct policydb *policydb;
1287 struct sidtab *sidtab;
1288 struct context *context;
1295 if (!state->initialized) {
1296 if (sid <= SECINITSID_NUM) {
1299 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
1302 scontextp = kmemdup(initial_sid_to_string[sid],
1303 *scontext_len, GFP_ATOMIC);
1308 *scontext = scontextp;
1311 pr_err("SELinux: %s: called before initial "
1312 "load_policy on unknown SID %d\n", __func__, sid);
1316 read_lock(&state->ss->policy_rwlock);
1317 policydb = &state->ss->policydb;
1318 sidtab = &state->ss->sidtab;
1320 context = sidtab_search_force(sidtab, sid);
1322 context = sidtab_search(sidtab, sid);
1324 pr_err("SELinux: %s: unrecognized SID %d\n",
1329 rc = context_struct_to_string(policydb, context, scontext,
1332 read_unlock(&state->ss->policy_rwlock);
1339 * security_sid_to_context - Obtain a context for a given SID.
1340 * @sid: security identifier, SID
1341 * @scontext: security context
1342 * @scontext_len: length in bytes
1344 * Write the string representation of the context associated with @sid
1345 * into a dynamically allocated string of the correct size. Set @scontext
1346 * to point to this string and set @scontext_len to the length of the string.
1348 int security_sid_to_context(struct selinux_state *state,
1349 u32 sid, char **scontext, u32 *scontext_len)
1351 return security_sid_to_context_core(state, sid, scontext,
1355 int security_sid_to_context_force(struct selinux_state *state, u32 sid,
1356 char **scontext, u32 *scontext_len)
1358 return security_sid_to_context_core(state, sid, scontext,
1363 * Caveat: Mutates scontext.
1365 static int string_to_context_struct(struct policydb *pol,
1366 struct sidtab *sidtabp,
1368 struct context *ctx,
1371 struct role_datum *role;
1372 struct type_datum *typdatum;
1373 struct user_datum *usrdatum;
1374 char *scontextp, *p, oldc;
1379 /* Parse the security context. */
1382 scontextp = (char *) scontext;
1384 /* Extract the user. */
1386 while (*p && *p != ':')
1394 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1398 ctx->user = usrdatum->value;
1402 while (*p && *p != ':')
1410 role = hashtab_search(pol->p_roles.table, scontextp);
1413 ctx->role = role->value;
1417 while (*p && *p != ':')
1422 typdatum = hashtab_search(pol->p_types.table, scontextp);
1423 if (!typdatum || typdatum->attribute)
1426 ctx->type = typdatum->value;
1428 rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid);
1432 /* Check the validity of the new context. */
1434 if (!policydb_context_isvalid(pol, ctx))
1439 context_destroy(ctx);
1443 static int security_context_to_sid_core(struct selinux_state *state,
1444 const char *scontext, u32 scontext_len,
1445 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1448 struct policydb *policydb;
1449 struct sidtab *sidtab;
1450 char *scontext2, *str = NULL;
1451 struct context context;
1454 /* An empty security context is never valid. */
1458 /* Copy the string to allow changes and ensure a NUL terminator */
1459 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1463 if (!state->initialized) {
1466 for (i = 1; i < SECINITSID_NUM; i++) {
1467 if (!strcmp(initial_sid_to_string[i], scontext2)) {
1472 *sid = SECINITSID_KERNEL;
1478 /* Save another copy for storing in uninterpreted form */
1480 str = kstrdup(scontext2, gfp_flags);
1484 read_lock(&state->ss->policy_rwlock);
1485 policydb = &state->ss->policydb;
1486 sidtab = &state->ss->sidtab;
1487 rc = string_to_context_struct(policydb, sidtab, scontext2,
1489 if (rc == -EINVAL && force) {
1491 context.len = strlen(str) + 1;
1495 rc = sidtab_context_to_sid(sidtab, &context, sid);
1496 context_destroy(&context);
1498 read_unlock(&state->ss->policy_rwlock);
1506 * security_context_to_sid - Obtain a SID for a given security context.
1507 * @scontext: security context
1508 * @scontext_len: length in bytes
1509 * @sid: security identifier, SID
1510 * @gfp: context for the allocation
1512 * Obtains a SID associated with the security context that
1513 * has the string representation specified by @scontext.
1514 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1515 * memory is available, or 0 on success.
1517 int security_context_to_sid(struct selinux_state *state,
1518 const char *scontext, u32 scontext_len, u32 *sid,
1521 return security_context_to_sid_core(state, scontext, scontext_len,
1522 sid, SECSID_NULL, gfp, 0);
1525 int security_context_str_to_sid(struct selinux_state *state,
1526 const char *scontext, u32 *sid, gfp_t gfp)
1528 return security_context_to_sid(state, scontext, strlen(scontext),
1533 * security_context_to_sid_default - Obtain a SID for a given security context,
1534 * falling back to specified default if needed.
1536 * @scontext: security context
1537 * @scontext_len: length in bytes
1538 * @sid: security identifier, SID
1539 * @def_sid: default SID to assign on error
1541 * Obtains a SID associated with the security context that
1542 * has the string representation specified by @scontext.
1543 * The default SID is passed to the MLS layer to be used to allow
1544 * kernel labeling of the MLS field if the MLS field is not present
1545 * (for upgrading to MLS without full relabel).
1546 * Implicitly forces adding of the context even if it cannot be mapped yet.
1547 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1548 * memory is available, or 0 on success.
1550 int security_context_to_sid_default(struct selinux_state *state,
1551 const char *scontext, u32 scontext_len,
1552 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1554 return security_context_to_sid_core(state, scontext, scontext_len,
1555 sid, def_sid, gfp_flags, 1);
1558 int security_context_to_sid_force(struct selinux_state *state,
1559 const char *scontext, u32 scontext_len,
1562 return security_context_to_sid_core(state, scontext, scontext_len,
1563 sid, SECSID_NULL, GFP_KERNEL, 1);
1566 static int compute_sid_handle_invalid_context(
1567 struct selinux_state *state,
1568 struct context *scontext,
1569 struct context *tcontext,
1571 struct context *newcontext)
1573 struct policydb *policydb = &state->ss->policydb;
1574 char *s = NULL, *t = NULL, *n = NULL;
1575 u32 slen, tlen, nlen;
1577 if (context_struct_to_string(policydb, scontext, &s, &slen))
1579 if (context_struct_to_string(policydb, tcontext, &t, &tlen))
1581 if (context_struct_to_string(policydb, newcontext, &n, &nlen))
1583 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
1584 "op=security_compute_sid invalid_context=%s"
1588 n, s, t, sym_name(policydb, SYM_CLASSES, tclass-1));
1593 if (!enforcing_enabled(state))
1598 static void filename_compute_type(struct policydb *policydb,
1599 struct context *newcontext,
1600 u32 stype, u32 ttype, u16 tclass,
1601 const char *objname)
1603 struct filename_trans ft;
1604 struct filename_trans_datum *otype;
1607 * Most filename trans rules are going to live in specific directories
1608 * like /dev or /var/run. This bitmap will quickly skip rule searches
1609 * if the ttype does not contain any rules.
1611 if (!ebitmap_get_bit(&policydb->filename_trans_ttypes, ttype))
1619 otype = hashtab_search(policydb->filename_trans, &ft);
1621 newcontext->type = otype->otype;
1624 static int security_compute_sid(struct selinux_state *state,
1629 const char *objname,
1633 struct policydb *policydb;
1634 struct sidtab *sidtab;
1635 struct class_datum *cladatum = NULL;
1636 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1637 struct role_trans *roletr = NULL;
1638 struct avtab_key avkey;
1639 struct avtab_datum *avdatum;
1640 struct avtab_node *node;
1645 if (!state->initialized) {
1646 switch (orig_tclass) {
1647 case SECCLASS_PROCESS: /* kernel value */
1657 context_init(&newcontext);
1659 read_lock(&state->ss->policy_rwlock);
1662 tclass = unmap_class(&state->ss->map, orig_tclass);
1663 sock = security_is_socket_class(orig_tclass);
1665 tclass = orig_tclass;
1666 sock = security_is_socket_class(map_class(&state->ss->map,
1670 policydb = &state->ss->policydb;
1671 sidtab = &state->ss->sidtab;
1673 scontext = sidtab_search(sidtab, ssid);
1675 pr_err("SELinux: %s: unrecognized SID %d\n",
1680 tcontext = sidtab_search(sidtab, tsid);
1682 pr_err("SELinux: %s: unrecognized SID %d\n",
1688 if (tclass && tclass <= policydb->p_classes.nprim)
1689 cladatum = policydb->class_val_to_struct[tclass - 1];
1691 /* Set the user identity. */
1692 switch (specified) {
1693 case AVTAB_TRANSITION:
1695 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1696 newcontext.user = tcontext->user;
1698 /* notice this gets both DEFAULT_SOURCE and unset */
1699 /* Use the process user identity. */
1700 newcontext.user = scontext->user;
1704 /* Use the related object owner. */
1705 newcontext.user = tcontext->user;
1709 /* Set the role to default values. */
1710 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
1711 newcontext.role = scontext->role;
1712 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1713 newcontext.role = tcontext->role;
1715 if ((tclass == policydb->process_class) || (sock == true))
1716 newcontext.role = scontext->role;
1718 newcontext.role = OBJECT_R_VAL;
1721 /* Set the type to default values. */
1722 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
1723 newcontext.type = scontext->type;
1724 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
1725 newcontext.type = tcontext->type;
1727 if ((tclass == policydb->process_class) || (sock == true)) {
1728 /* Use the type of process. */
1729 newcontext.type = scontext->type;
1731 /* Use the type of the related object. */
1732 newcontext.type = tcontext->type;
1736 /* Look for a type transition/member/change rule. */
1737 avkey.source_type = scontext->type;
1738 avkey.target_type = tcontext->type;
1739 avkey.target_class = tclass;
1740 avkey.specified = specified;
1741 avdatum = avtab_search(&policydb->te_avtab, &avkey);
1743 /* If no permanent rule, also check for enabled conditional rules */
1745 node = avtab_search_node(&policydb->te_cond_avtab, &avkey);
1746 for (; node; node = avtab_search_node_next(node, specified)) {
1747 if (node->key.specified & AVTAB_ENABLED) {
1748 avdatum = &node->datum;
1755 /* Use the type from the type transition/member/change rule. */
1756 newcontext.type = avdatum->u.data;
1759 /* if we have a objname this is a file trans check so check those rules */
1761 filename_compute_type(policydb, &newcontext, scontext->type,
1762 tcontext->type, tclass, objname);
1764 /* Check for class-specific changes. */
1765 if (specified & AVTAB_TRANSITION) {
1766 /* Look for a role transition rule. */
1767 for (roletr = policydb->role_tr; roletr;
1768 roletr = roletr->next) {
1769 if ((roletr->role == scontext->role) &&
1770 (roletr->type == tcontext->type) &&
1771 (roletr->tclass == tclass)) {
1772 /* Use the role transition rule. */
1773 newcontext.role = roletr->new_role;
1779 /* Set the MLS attributes.
1780 This is done last because it may allocate memory. */
1781 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified,
1786 /* Check the validity of the context. */
1787 if (!policydb_context_isvalid(policydb, &newcontext)) {
1788 rc = compute_sid_handle_invalid_context(state, scontext,
1795 /* Obtain the sid for the context. */
1796 rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
1798 read_unlock(&state->ss->policy_rwlock);
1799 context_destroy(&newcontext);
1805 * security_transition_sid - Compute the SID for a new subject/object.
1806 * @ssid: source security identifier
1807 * @tsid: target security identifier
1808 * @tclass: target security class
1809 * @out_sid: security identifier for new subject/object
1811 * Compute a SID to use for labeling a new subject or object in the
1812 * class @tclass based on a SID pair (@ssid, @tsid).
1813 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1814 * if insufficient memory is available, or %0 if the new SID was
1815 * computed successfully.
1817 int security_transition_sid(struct selinux_state *state,
1818 u32 ssid, u32 tsid, u16 tclass,
1819 const struct qstr *qstr, u32 *out_sid)
1821 return security_compute_sid(state, ssid, tsid, tclass,
1823 qstr ? qstr->name : NULL, out_sid, true);
1826 int security_transition_sid_user(struct selinux_state *state,
1827 u32 ssid, u32 tsid, u16 tclass,
1828 const char *objname, u32 *out_sid)
1830 return security_compute_sid(state, ssid, tsid, tclass,
1832 objname, out_sid, false);
1836 * security_member_sid - Compute the SID for member selection.
1837 * @ssid: source security identifier
1838 * @tsid: target security identifier
1839 * @tclass: target security class
1840 * @out_sid: security identifier for selected member
1842 * Compute a SID to use when selecting a member of a polyinstantiated
1843 * object of class @tclass based on a SID pair (@ssid, @tsid).
1844 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1845 * if insufficient memory is available, or %0 if the SID was
1846 * computed successfully.
1848 int security_member_sid(struct selinux_state *state,
1854 return security_compute_sid(state, ssid, tsid, tclass,
1860 * security_change_sid - Compute the SID for object relabeling.
1861 * @ssid: source security identifier
1862 * @tsid: target security identifier
1863 * @tclass: target security class
1864 * @out_sid: security identifier for selected member
1866 * Compute a SID to use for relabeling an object of class @tclass
1867 * based on a SID pair (@ssid, @tsid).
1868 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1869 * if insufficient memory is available, or %0 if the SID was
1870 * computed successfully.
1872 int security_change_sid(struct selinux_state *state,
1878 return security_compute_sid(state,
1879 ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1883 /* Clone the SID into the new SID table. */
1884 static int clone_sid(u32 sid,
1885 struct context *context,
1888 struct sidtab *s = arg;
1890 if (sid > SECINITSID_NUM)
1891 return sidtab_insert(s, sid, context);
1896 static inline int convert_context_handle_invalid_context(
1897 struct selinux_state *state,
1898 struct context *context)
1900 struct policydb *policydb = &state->ss->policydb;
1904 if (enforcing_enabled(state))
1907 if (!context_struct_to_string(policydb, context, &s, &len)) {
1908 pr_warn("SELinux: Context %s would be invalid if enforcing\n",
1915 struct convert_context_args {
1916 struct selinux_state *state;
1917 struct policydb *oldp;
1918 struct policydb *newp;
1922 * Convert the values in the security context
1923 * structure `c' from the values specified
1924 * in the policy `p->oldp' to the values specified
1925 * in the policy `p->newp'. Verify that the
1926 * context is valid under the new policy.
1928 static int convert_context(u32 key,
1932 struct convert_context_args *args;
1933 struct context oldc;
1934 struct ocontext *oc;
1935 struct mls_range *range;
1936 struct role_datum *role;
1937 struct type_datum *typdatum;
1938 struct user_datum *usrdatum;
1943 if (key <= SECINITSID_NUM)
1952 s = kstrdup(c->str, GFP_KERNEL);
1956 rc = string_to_context_struct(args->newp, NULL, s,
1960 pr_info("SELinux: Context %s became valid (mapped).\n",
1962 /* Replace string with mapped representation. */
1964 memcpy(c, &ctx, sizeof(*c));
1966 } else if (rc == -EINVAL) {
1967 /* Retain string representation for later mapping. */
1971 /* Other error condition, e.g. ENOMEM. */
1972 pr_err("SELinux: Unable to map context %s, rc = %d.\n",
1978 rc = context_cpy(&oldc, c);
1982 /* Convert the user. */
1984 usrdatum = hashtab_search(args->newp->p_users.table,
1985 sym_name(args->oldp, SYM_USERS, c->user - 1));
1988 c->user = usrdatum->value;
1990 /* Convert the role. */
1992 role = hashtab_search(args->newp->p_roles.table,
1993 sym_name(args->oldp, SYM_ROLES, c->role - 1));
1996 c->role = role->value;
1998 /* Convert the type. */
2000 typdatum = hashtab_search(args->newp->p_types.table,
2001 sym_name(args->oldp, SYM_TYPES, c->type - 1));
2004 c->type = typdatum->value;
2006 /* Convert the MLS fields if dealing with MLS policies */
2007 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
2008 rc = mls_convert_context(args->oldp, args->newp, c);
2011 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
2013 * Switching between MLS and non-MLS policy:
2014 * free any storage used by the MLS fields in the
2015 * context for all existing entries in the sidtab.
2017 mls_context_destroy(c);
2018 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
2020 * Switching between non-MLS and MLS policy:
2021 * ensure that the MLS fields of the context for all
2022 * existing entries in the sidtab are filled in with a
2023 * suitable default value, likely taken from one of the
2026 oc = args->newp->ocontexts[OCON_ISID];
2027 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
2031 pr_err("SELinux: unable to look up"
2032 " the initial SIDs list\n");
2035 range = &oc->context[0].range;
2036 rc = mls_range_set(c, range);
2041 /* Check the validity of the new context. */
2042 if (!policydb_context_isvalid(args->newp, c)) {
2043 rc = convert_context_handle_invalid_context(args->state,
2049 context_destroy(&oldc);
2055 /* Map old representation to string and save it. */
2056 rc = context_struct_to_string(args->oldp, &oldc, &s, &len);
2059 context_destroy(&oldc);
2063 pr_info("SELinux: Context %s became invalid (unmapped).\n",
2069 static void security_load_policycaps(struct selinux_state *state)
2071 struct policydb *p = &state->ss->policydb;
2073 struct ebitmap_node *node;
2075 for (i = 0; i < ARRAY_SIZE(state->policycap); i++)
2076 state->policycap[i] = ebitmap_get_bit(&p->policycaps, i);
2078 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2079 pr_info("SELinux: policy capability %s=%d\n",
2080 selinux_policycap_names[i],
2081 ebitmap_get_bit(&p->policycaps, i));
2083 ebitmap_for_each_positive_bit(&p->policycaps, node, i) {
2084 if (i >= ARRAY_SIZE(selinux_policycap_names))
2085 pr_info("SELinux: unknown policy capability %u\n",
2090 static int security_preserve_bools(struct selinux_state *state,
2091 struct policydb *newpolicydb);
2094 * security_load_policy - Load a security policy configuration.
2095 * @data: binary policy data
2096 * @len: length of data in bytes
2098 * Load a new set of security policy configuration data,
2099 * validate it and convert the SID table as necessary.
2100 * This function will flush the access vector cache after
2101 * loading the new policy.
2103 int security_load_policy(struct selinux_state *state, void *data, size_t len)
2105 struct policydb *policydb;
2106 struct sidtab *sidtab;
2107 struct policydb *oldpolicydb, *newpolicydb;
2108 struct sidtab oldsidtab, newsidtab;
2109 struct selinux_mapping *oldmapping;
2110 struct selinux_map newmap;
2111 struct convert_context_args args;
2114 struct policy_file file = { data, len }, *fp = &file;
2116 oldpolicydb = kcalloc(2, sizeof(*oldpolicydb), GFP_KERNEL);
2121 newpolicydb = oldpolicydb + 1;
2123 policydb = &state->ss->policydb;
2124 sidtab = &state->ss->sidtab;
2126 if (!state->initialized) {
2127 rc = policydb_read(policydb, fp);
2131 policydb->len = len;
2132 rc = selinux_set_mapping(policydb, secclass_map,
2135 policydb_destroy(policydb);
2139 rc = policydb_load_isids(policydb, sidtab);
2141 policydb_destroy(policydb);
2145 security_load_policycaps(state);
2146 state->initialized = 1;
2147 seqno = ++state->ss->latest_granting;
2148 selinux_complete_init();
2149 avc_ss_reset(state->avc, seqno);
2150 selnl_notify_policyload(seqno);
2151 selinux_status_update_policyload(state, seqno);
2152 selinux_netlbl_cache_invalidate();
2153 selinux_xfrm_notify_policyload();
2158 sidtab_hash_eval(sidtab, "sids");
2161 rc = policydb_read(newpolicydb, fp);
2165 newpolicydb->len = len;
2166 /* If switching between different policy types, log MLS status */
2167 if (policydb->mls_enabled && !newpolicydb->mls_enabled)
2168 pr_info("SELinux: Disabling MLS support...\n");
2169 else if (!policydb->mls_enabled && newpolicydb->mls_enabled)
2170 pr_info("SELinux: Enabling MLS support...\n");
2172 rc = policydb_load_isids(newpolicydb, &newsidtab);
2174 pr_err("SELinux: unable to load the initial SIDs\n");
2175 policydb_destroy(newpolicydb);
2179 rc = selinux_set_mapping(newpolicydb, secclass_map, &newmap);
2183 rc = security_preserve_bools(state, newpolicydb);
2185 pr_err("SELinux: unable to preserve booleans\n");
2189 /* Clone the SID table. */
2190 sidtab_shutdown(sidtab);
2192 rc = sidtab_map(sidtab, clone_sid, &newsidtab);
2197 * Convert the internal representations of contexts
2198 * in the new SID table.
2201 args.oldp = policydb;
2202 args.newp = newpolicydb;
2203 rc = sidtab_map(&newsidtab, convert_context, &args);
2205 pr_err("SELinux: unable to convert the internal"
2206 " representation of contexts in the new SID"
2211 /* Save the old policydb and SID table to free later. */
2212 memcpy(oldpolicydb, policydb, sizeof(*policydb));
2213 sidtab_set(&oldsidtab, sidtab);
2215 /* Install the new policydb and SID table. */
2216 write_lock_irq(&state->ss->policy_rwlock);
2217 memcpy(policydb, newpolicydb, sizeof(*policydb));
2218 sidtab_set(sidtab, &newsidtab);
2219 security_load_policycaps(state);
2220 oldmapping = state->ss->map.mapping;
2221 state->ss->map.mapping = newmap.mapping;
2222 state->ss->map.size = newmap.size;
2223 seqno = ++state->ss->latest_granting;
2224 write_unlock_irq(&state->ss->policy_rwlock);
2226 /* Free the old policydb and SID table. */
2227 policydb_destroy(oldpolicydb);
2228 sidtab_destroy(&oldsidtab);
2231 avc_ss_reset(state->avc, seqno);
2232 selnl_notify_policyload(seqno);
2233 selinux_status_update_policyload(state, seqno);
2234 selinux_netlbl_cache_invalidate();
2235 selinux_xfrm_notify_policyload();
2241 kfree(newmap.mapping);
2242 sidtab_destroy(&newsidtab);
2243 policydb_destroy(newpolicydb);
2250 size_t security_policydb_len(struct selinux_state *state)
2252 struct policydb *p = &state->ss->policydb;
2255 read_lock(&state->ss->policy_rwlock);
2257 read_unlock(&state->ss->policy_rwlock);
2263 * security_port_sid - Obtain the SID for a port.
2264 * @protocol: protocol number
2265 * @port: port number
2266 * @out_sid: security identifier
2268 int security_port_sid(struct selinux_state *state,
2269 u8 protocol, u16 port, u32 *out_sid)
2271 struct policydb *policydb;
2272 struct sidtab *sidtab;
2276 read_lock(&state->ss->policy_rwlock);
2278 policydb = &state->ss->policydb;
2279 sidtab = &state->ss->sidtab;
2281 c = policydb->ocontexts[OCON_PORT];
2283 if (c->u.port.protocol == protocol &&
2284 c->u.port.low_port <= port &&
2285 c->u.port.high_port >= port)
2292 rc = sidtab_context_to_sid(sidtab,
2298 *out_sid = c->sid[0];
2300 *out_sid = SECINITSID_PORT;
2304 read_unlock(&state->ss->policy_rwlock);
2309 * security_pkey_sid - Obtain the SID for a pkey.
2310 * @subnet_prefix: Subnet Prefix
2311 * @pkey_num: pkey number
2312 * @out_sid: security identifier
2314 int security_ib_pkey_sid(struct selinux_state *state,
2315 u64 subnet_prefix, u16 pkey_num, u32 *out_sid)
2317 struct policydb *policydb;
2318 struct sidtab *sidtab;
2322 read_lock(&state->ss->policy_rwlock);
2324 policydb = &state->ss->policydb;
2325 sidtab = &state->ss->sidtab;
2327 c = policydb->ocontexts[OCON_IBPKEY];
2329 if (c->u.ibpkey.low_pkey <= pkey_num &&
2330 c->u.ibpkey.high_pkey >= pkey_num &&
2331 c->u.ibpkey.subnet_prefix == subnet_prefix)
2339 rc = sidtab_context_to_sid(sidtab,
2345 *out_sid = c->sid[0];
2347 *out_sid = SECINITSID_UNLABELED;
2350 read_unlock(&state->ss->policy_rwlock);
2355 * security_ib_endport_sid - Obtain the SID for a subnet management interface.
2356 * @dev_name: device name
2357 * @port: port number
2358 * @out_sid: security identifier
2360 int security_ib_endport_sid(struct selinux_state *state,
2361 const char *dev_name, u8 port_num, u32 *out_sid)
2363 struct policydb *policydb;
2364 struct sidtab *sidtab;
2368 read_lock(&state->ss->policy_rwlock);
2370 policydb = &state->ss->policydb;
2371 sidtab = &state->ss->sidtab;
2373 c = policydb->ocontexts[OCON_IBENDPORT];
2375 if (c->u.ibendport.port == port_num &&
2376 !strncmp(c->u.ibendport.dev_name,
2378 IB_DEVICE_NAME_MAX))
2386 rc = sidtab_context_to_sid(sidtab,
2392 *out_sid = c->sid[0];
2394 *out_sid = SECINITSID_UNLABELED;
2397 read_unlock(&state->ss->policy_rwlock);
2402 * security_netif_sid - Obtain the SID for a network interface.
2403 * @name: interface name
2404 * @if_sid: interface SID
2406 int security_netif_sid(struct selinux_state *state,
2407 char *name, u32 *if_sid)
2409 struct policydb *policydb;
2410 struct sidtab *sidtab;
2414 read_lock(&state->ss->policy_rwlock);
2416 policydb = &state->ss->policydb;
2417 sidtab = &state->ss->sidtab;
2419 c = policydb->ocontexts[OCON_NETIF];
2421 if (strcmp(name, c->u.name) == 0)
2427 if (!c->sid[0] || !c->sid[1]) {
2428 rc = sidtab_context_to_sid(sidtab,
2433 rc = sidtab_context_to_sid(sidtab,
2439 *if_sid = c->sid[0];
2441 *if_sid = SECINITSID_NETIF;
2444 read_unlock(&state->ss->policy_rwlock);
2448 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2452 for (i = 0; i < 4; i++)
2453 if (addr[i] != (input[i] & mask[i])) {
2462 * security_node_sid - Obtain the SID for a node (host).
2463 * @domain: communication domain aka address family
2465 * @addrlen: address length in bytes
2466 * @out_sid: security identifier
2468 int security_node_sid(struct selinux_state *state,
2474 struct policydb *policydb;
2475 struct sidtab *sidtab;
2479 read_lock(&state->ss->policy_rwlock);
2481 policydb = &state->ss->policydb;
2482 sidtab = &state->ss->sidtab;
2489 if (addrlen != sizeof(u32))
2492 addr = *((u32 *)addrp);
2494 c = policydb->ocontexts[OCON_NODE];
2496 if (c->u.node.addr == (addr & c->u.node.mask))
2505 if (addrlen != sizeof(u64) * 2)
2507 c = policydb->ocontexts[OCON_NODE6];
2509 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2518 *out_sid = SECINITSID_NODE;
2524 rc = sidtab_context_to_sid(sidtab,
2530 *out_sid = c->sid[0];
2532 *out_sid = SECINITSID_NODE;
2537 read_unlock(&state->ss->policy_rwlock);
2544 * security_get_user_sids - Obtain reachable SIDs for a user.
2545 * @fromsid: starting SID
2546 * @username: username
2547 * @sids: array of reachable SIDs for user
2548 * @nel: number of elements in @sids
2550 * Generate the set of SIDs for legal security contexts
2551 * for a given user that can be reached by @fromsid.
2552 * Set *@sids to point to a dynamically allocated
2553 * array containing the set of SIDs. Set *@nel to the
2554 * number of elements in the array.
2557 int security_get_user_sids(struct selinux_state *state,
2563 struct policydb *policydb;
2564 struct sidtab *sidtab;
2565 struct context *fromcon, usercon;
2566 u32 *mysids = NULL, *mysids2, sid;
2567 u32 mynel = 0, maxnel = SIDS_NEL;
2568 struct user_datum *user;
2569 struct role_datum *role;
2570 struct ebitmap_node *rnode, *tnode;
2576 if (!state->initialized)
2579 read_lock(&state->ss->policy_rwlock);
2581 policydb = &state->ss->policydb;
2582 sidtab = &state->ss->sidtab;
2584 context_init(&usercon);
2587 fromcon = sidtab_search(sidtab, fromsid);
2592 user = hashtab_search(policydb->p_users.table, username);
2596 usercon.user = user->value;
2599 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
2603 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2604 role = policydb->role_val_to_struct[i];
2605 usercon.role = i + 1;
2606 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2607 usercon.type = j + 1;
2609 if (mls_setup_user_range(policydb, fromcon, user,
2613 rc = sidtab_context_to_sid(sidtab, &usercon, &sid);
2616 if (mynel < maxnel) {
2617 mysids[mynel++] = sid;
2621 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2624 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2627 mysids[mynel++] = sid;
2633 read_unlock(&state->ss->policy_rwlock);
2640 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2645 for (i = 0, j = 0; i < mynel; i++) {
2646 struct av_decision dummy_avd;
2647 rc = avc_has_perm_noaudit(state,
2649 SECCLASS_PROCESS, /* kernel value */
2650 PROCESS__TRANSITION, AVC_STRICT,
2653 mysids2[j++] = mysids[i];
2665 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
2666 * @fstype: filesystem type
2667 * @path: path from root of mount
2668 * @sclass: file security class
2669 * @sid: SID for path
2671 * Obtain a SID to use for a file in a filesystem that
2672 * cannot support xattr or use a fixed labeling behavior like
2673 * transition SIDs or task SIDs.
2675 * The caller must acquire the policy_rwlock before calling this function.
2677 static inline int __security_genfs_sid(struct selinux_state *state,
2683 struct policydb *policydb = &state->ss->policydb;
2684 struct sidtab *sidtab = &state->ss->sidtab;
2687 struct genfs *genfs;
2691 while (path[0] == '/' && path[1] == '/')
2694 sclass = unmap_class(&state->ss->map, orig_sclass);
2695 *sid = SECINITSID_UNLABELED;
2697 for (genfs = policydb->genfs; genfs; genfs = genfs->next) {
2698 cmp = strcmp(fstype, genfs->fstype);
2707 for (c = genfs->head; c; c = c->next) {
2708 len = strlen(c->u.name);
2709 if ((!c->v.sclass || sclass == c->v.sclass) &&
2710 (strncmp(c->u.name, path, len) == 0))
2719 rc = sidtab_context_to_sid(sidtab, &c->context[0], &c->sid[0]);
2731 * security_genfs_sid - Obtain a SID for a file in a filesystem
2732 * @fstype: filesystem type
2733 * @path: path from root of mount
2734 * @sclass: file security class
2735 * @sid: SID for path
2737 * Acquire policy_rwlock before calling __security_genfs_sid() and release
2740 int security_genfs_sid(struct selinux_state *state,
2748 read_lock(&state->ss->policy_rwlock);
2749 retval = __security_genfs_sid(state, fstype, path, orig_sclass, sid);
2750 read_unlock(&state->ss->policy_rwlock);
2755 * security_fs_use - Determine how to handle labeling for a filesystem.
2756 * @sb: superblock in question
2758 int security_fs_use(struct selinux_state *state, struct super_block *sb)
2760 struct policydb *policydb;
2761 struct sidtab *sidtab;
2764 struct superblock_security_struct *sbsec = sb->s_security;
2765 const char *fstype = sb->s_type->name;
2767 read_lock(&state->ss->policy_rwlock);
2769 policydb = &state->ss->policydb;
2770 sidtab = &state->ss->sidtab;
2772 c = policydb->ocontexts[OCON_FSUSE];
2774 if (strcmp(fstype, c->u.name) == 0)
2780 sbsec->behavior = c->v.behavior;
2782 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2787 sbsec->sid = c->sid[0];
2789 rc = __security_genfs_sid(state, fstype, "/", SECCLASS_DIR,
2792 sbsec->behavior = SECURITY_FS_USE_NONE;
2795 sbsec->behavior = SECURITY_FS_USE_GENFS;
2800 read_unlock(&state->ss->policy_rwlock);
2804 int security_get_bools(struct selinux_state *state,
2805 int *len, char ***names, int **values)
2807 struct policydb *policydb;
2810 if (!state->initialized) {
2817 read_lock(&state->ss->policy_rwlock);
2819 policydb = &state->ss->policydb;
2825 *len = policydb->p_bools.nprim;
2830 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
2835 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2839 for (i = 0; i < *len; i++) {
2840 (*values)[i] = policydb->bool_val_to_struct[i]->state;
2843 (*names)[i] = kstrdup(sym_name(policydb, SYM_BOOLS, i),
2850 read_unlock(&state->ss->policy_rwlock);
2854 for (i = 0; i < *len; i++)
2862 int security_set_bools(struct selinux_state *state, int len, int *values)
2864 struct policydb *policydb;
2866 int lenp, seqno = 0;
2867 struct cond_node *cur;
2869 write_lock_irq(&state->ss->policy_rwlock);
2871 policydb = &state->ss->policydb;
2874 lenp = policydb->p_bools.nprim;
2878 for (i = 0; i < len; i++) {
2879 if (!!values[i] != policydb->bool_val_to_struct[i]->state) {
2880 audit_log(audit_context(), GFP_ATOMIC,
2881 AUDIT_MAC_CONFIG_CHANGE,
2882 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2883 sym_name(policydb, SYM_BOOLS, i),
2885 policydb->bool_val_to_struct[i]->state,
2886 from_kuid(&init_user_ns, audit_get_loginuid(current)),
2887 audit_get_sessionid(current));
2890 policydb->bool_val_to_struct[i]->state = 1;
2892 policydb->bool_val_to_struct[i]->state = 0;
2895 for (cur = policydb->cond_list; cur; cur = cur->next) {
2896 rc = evaluate_cond_node(policydb, cur);
2901 seqno = ++state->ss->latest_granting;
2904 write_unlock_irq(&state->ss->policy_rwlock);
2906 avc_ss_reset(state->avc, seqno);
2907 selnl_notify_policyload(seqno);
2908 selinux_status_update_policyload(state, seqno);
2909 selinux_xfrm_notify_policyload();
2914 int security_get_bool_value(struct selinux_state *state,
2917 struct policydb *policydb;
2921 read_lock(&state->ss->policy_rwlock);
2923 policydb = &state->ss->policydb;
2926 len = policydb->p_bools.nprim;
2930 rc = policydb->bool_val_to_struct[index]->state;
2932 read_unlock(&state->ss->policy_rwlock);
2936 static int security_preserve_bools(struct selinux_state *state,
2937 struct policydb *policydb)
2939 int rc, nbools = 0, *bvalues = NULL, i;
2940 char **bnames = NULL;
2941 struct cond_bool_datum *booldatum;
2942 struct cond_node *cur;
2944 rc = security_get_bools(state, &nbools, &bnames, &bvalues);
2947 for (i = 0; i < nbools; i++) {
2948 booldatum = hashtab_search(policydb->p_bools.table, bnames[i]);
2950 booldatum->state = bvalues[i];
2952 for (cur = policydb->cond_list; cur; cur = cur->next) {
2953 rc = evaluate_cond_node(policydb, cur);
2960 for (i = 0; i < nbools; i++)
2969 * security_sid_mls_copy() - computes a new sid based on the given
2970 * sid and the mls portion of mls_sid.
2972 int security_sid_mls_copy(struct selinux_state *state,
2973 u32 sid, u32 mls_sid, u32 *new_sid)
2975 struct policydb *policydb = &state->ss->policydb;
2976 struct sidtab *sidtab = &state->ss->sidtab;
2977 struct context *context1;
2978 struct context *context2;
2979 struct context newcon;
2985 if (!state->initialized || !policydb->mls_enabled) {
2990 context_init(&newcon);
2992 read_lock(&state->ss->policy_rwlock);
2995 context1 = sidtab_search(sidtab, sid);
2997 pr_err("SELinux: %s: unrecognized SID %d\n",
3003 context2 = sidtab_search(sidtab, mls_sid);
3005 pr_err("SELinux: %s: unrecognized SID %d\n",
3010 newcon.user = context1->user;
3011 newcon.role = context1->role;
3012 newcon.type = context1->type;
3013 rc = mls_context_cpy(&newcon, context2);
3017 /* Check the validity of the new context. */
3018 if (!policydb_context_isvalid(policydb, &newcon)) {
3019 rc = convert_context_handle_invalid_context(state, &newcon);
3021 if (!context_struct_to_string(policydb, &newcon, &s,
3023 audit_log(audit_context(),
3024 GFP_ATOMIC, AUDIT_SELINUX_ERR,
3025 "op=security_sid_mls_copy "
3026 "invalid_context=%s", s);
3033 rc = sidtab_context_to_sid(sidtab, &newcon, new_sid);
3035 read_unlock(&state->ss->policy_rwlock);
3036 context_destroy(&newcon);
3042 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
3043 * @nlbl_sid: NetLabel SID
3044 * @nlbl_type: NetLabel labeling protocol type
3045 * @xfrm_sid: XFRM SID
3048 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
3049 * resolved into a single SID it is returned via @peer_sid and the function
3050 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
3051 * returns a negative value. A table summarizing the behavior is below:
3053 * | function return | @sid
3054 * ------------------------------+-----------------+-----------------
3055 * no peer labels | 0 | SECSID_NULL
3056 * single peer label | 0 | <peer_label>
3057 * multiple, consistent labels | 0 | <peer_label>
3058 * multiple, inconsistent labels | -<errno> | SECSID_NULL
3061 int security_net_peersid_resolve(struct selinux_state *state,
3062 u32 nlbl_sid, u32 nlbl_type,
3066 struct policydb *policydb = &state->ss->policydb;
3067 struct sidtab *sidtab = &state->ss->sidtab;
3069 struct context *nlbl_ctx;
3070 struct context *xfrm_ctx;
3072 *peer_sid = SECSID_NULL;
3074 /* handle the common (which also happens to be the set of easy) cases
3075 * right away, these two if statements catch everything involving a
3076 * single or absent peer SID/label */
3077 if (xfrm_sid == SECSID_NULL) {
3078 *peer_sid = nlbl_sid;
3081 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
3082 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
3084 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
3085 *peer_sid = xfrm_sid;
3090 * We don't need to check initialized here since the only way both
3091 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
3092 * security server was initialized and state->initialized was true.
3094 if (!policydb->mls_enabled)
3097 read_lock(&state->ss->policy_rwlock);
3100 nlbl_ctx = sidtab_search(sidtab, nlbl_sid);
3102 pr_err("SELinux: %s: unrecognized SID %d\n",
3103 __func__, nlbl_sid);
3107 xfrm_ctx = sidtab_search(sidtab, xfrm_sid);
3109 pr_err("SELinux: %s: unrecognized SID %d\n",
3110 __func__, xfrm_sid);
3113 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
3117 /* at present NetLabel SIDs/labels really only carry MLS
3118 * information so if the MLS portion of the NetLabel SID
3119 * matches the MLS portion of the labeled XFRM SID/label
3120 * then pass along the XFRM SID as it is the most
3122 *peer_sid = xfrm_sid;
3124 read_unlock(&state->ss->policy_rwlock);
3128 static int get_classes_callback(void *k, void *d, void *args)
3130 struct class_datum *datum = d;
3131 char *name = k, **classes = args;
3132 int value = datum->value - 1;
3134 classes[value] = kstrdup(name, GFP_ATOMIC);
3135 if (!classes[value])
3141 int security_get_classes(struct selinux_state *state,
3142 char ***classes, int *nclasses)
3144 struct policydb *policydb = &state->ss->policydb;
3147 if (!state->initialized) {
3153 read_lock(&state->ss->policy_rwlock);
3156 *nclasses = policydb->p_classes.nprim;
3157 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
3161 rc = hashtab_map(policydb->p_classes.table, get_classes_callback,
3165 for (i = 0; i < *nclasses; i++)
3166 kfree((*classes)[i]);
3171 read_unlock(&state->ss->policy_rwlock);
3175 static int get_permissions_callback(void *k, void *d, void *args)
3177 struct perm_datum *datum = d;
3178 char *name = k, **perms = args;
3179 int value = datum->value - 1;
3181 perms[value] = kstrdup(name, GFP_ATOMIC);
3188 int security_get_permissions(struct selinux_state *state,
3189 char *class, char ***perms, int *nperms)
3191 struct policydb *policydb = &state->ss->policydb;
3193 struct class_datum *match;
3195 read_lock(&state->ss->policy_rwlock);
3198 match = hashtab_search(policydb->p_classes.table, class);
3200 pr_err("SELinux: %s: unrecognized class %s\n",
3206 *nperms = match->permissions.nprim;
3207 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
3211 if (match->comdatum) {
3212 rc = hashtab_map(match->comdatum->permissions.table,
3213 get_permissions_callback, *perms);
3218 rc = hashtab_map(match->permissions.table, get_permissions_callback,
3224 read_unlock(&state->ss->policy_rwlock);
3228 read_unlock(&state->ss->policy_rwlock);
3229 for (i = 0; i < *nperms; i++)
3235 int security_get_reject_unknown(struct selinux_state *state)
3237 return state->ss->policydb.reject_unknown;
3240 int security_get_allow_unknown(struct selinux_state *state)
3242 return state->ss->policydb.allow_unknown;
3246 * security_policycap_supported - Check for a specific policy capability
3247 * @req_cap: capability
3250 * This function queries the currently loaded policy to see if it supports the
3251 * capability specified by @req_cap. Returns true (1) if the capability is
3252 * supported, false (0) if it isn't supported.
3255 int security_policycap_supported(struct selinux_state *state,
3256 unsigned int req_cap)
3258 struct policydb *policydb = &state->ss->policydb;
3261 read_lock(&state->ss->policy_rwlock);
3262 rc = ebitmap_get_bit(&policydb->policycaps, req_cap);
3263 read_unlock(&state->ss->policy_rwlock);
3268 struct selinux_audit_rule {
3270 struct context au_ctxt;
3273 void selinux_audit_rule_free(void *vrule)
3275 struct selinux_audit_rule *rule = vrule;
3278 context_destroy(&rule->au_ctxt);
3283 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3285 struct selinux_state *state = &selinux_state;
3286 struct policydb *policydb = &state->ss->policydb;
3287 struct selinux_audit_rule *tmprule;
3288 struct role_datum *roledatum;
3289 struct type_datum *typedatum;
3290 struct user_datum *userdatum;
3291 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
3296 if (!state->initialized)
3300 case AUDIT_SUBJ_USER:
3301 case AUDIT_SUBJ_ROLE:
3302 case AUDIT_SUBJ_TYPE:
3303 case AUDIT_OBJ_USER:
3304 case AUDIT_OBJ_ROLE:
3305 case AUDIT_OBJ_TYPE:
3306 /* only 'equals' and 'not equals' fit user, role, and type */
3307 if (op != Audit_equal && op != Audit_not_equal)
3310 case AUDIT_SUBJ_SEN:
3311 case AUDIT_SUBJ_CLR:
3312 case AUDIT_OBJ_LEV_LOW:
3313 case AUDIT_OBJ_LEV_HIGH:
3314 /* we do not allow a range, indicated by the presence of '-' */
3315 if (strchr(rulestr, '-'))
3319 /* only the above fields are valid */
3323 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3327 context_init(&tmprule->au_ctxt);
3329 read_lock(&state->ss->policy_rwlock);
3331 tmprule->au_seqno = state->ss->latest_granting;
3334 case AUDIT_SUBJ_USER:
3335 case AUDIT_OBJ_USER:
3337 userdatum = hashtab_search(policydb->p_users.table, rulestr);
3340 tmprule->au_ctxt.user = userdatum->value;
3342 case AUDIT_SUBJ_ROLE:
3343 case AUDIT_OBJ_ROLE:
3345 roledatum = hashtab_search(policydb->p_roles.table, rulestr);
3348 tmprule->au_ctxt.role = roledatum->value;
3350 case AUDIT_SUBJ_TYPE:
3351 case AUDIT_OBJ_TYPE:
3353 typedatum = hashtab_search(policydb->p_types.table, rulestr);
3356 tmprule->au_ctxt.type = typedatum->value;
3358 case AUDIT_SUBJ_SEN:
3359 case AUDIT_SUBJ_CLR:
3360 case AUDIT_OBJ_LEV_LOW:
3361 case AUDIT_OBJ_LEV_HIGH:
3362 rc = mls_from_string(policydb, rulestr, &tmprule->au_ctxt,
3370 read_unlock(&state->ss->policy_rwlock);
3373 selinux_audit_rule_free(tmprule);
3382 /* Check to see if the rule contains any selinux fields */
3383 int selinux_audit_rule_known(struct audit_krule *rule)
3387 for (i = 0; i < rule->field_count; i++) {
3388 struct audit_field *f = &rule->fields[i];
3390 case AUDIT_SUBJ_USER:
3391 case AUDIT_SUBJ_ROLE:
3392 case AUDIT_SUBJ_TYPE:
3393 case AUDIT_SUBJ_SEN:
3394 case AUDIT_SUBJ_CLR:
3395 case AUDIT_OBJ_USER:
3396 case AUDIT_OBJ_ROLE:
3397 case AUDIT_OBJ_TYPE:
3398 case AUDIT_OBJ_LEV_LOW:
3399 case AUDIT_OBJ_LEV_HIGH:
3407 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
3408 struct audit_context *actx)
3410 struct selinux_state *state = &selinux_state;
3411 struct context *ctxt;
3412 struct mls_level *level;
3413 struct selinux_audit_rule *rule = vrule;
3416 if (unlikely(!rule)) {
3417 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
3421 read_lock(&state->ss->policy_rwlock);
3423 if (rule->au_seqno < state->ss->latest_granting) {
3428 ctxt = sidtab_search(&state->ss->sidtab, sid);
3429 if (unlikely(!ctxt)) {
3430 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
3436 /* a field/op pair that is not caught here will simply fall through
3439 case AUDIT_SUBJ_USER:
3440 case AUDIT_OBJ_USER:
3443 match = (ctxt->user == rule->au_ctxt.user);
3445 case Audit_not_equal:
3446 match = (ctxt->user != rule->au_ctxt.user);
3450 case AUDIT_SUBJ_ROLE:
3451 case AUDIT_OBJ_ROLE:
3454 match = (ctxt->role == rule->au_ctxt.role);
3456 case Audit_not_equal:
3457 match = (ctxt->role != rule->au_ctxt.role);
3461 case AUDIT_SUBJ_TYPE:
3462 case AUDIT_OBJ_TYPE:
3465 match = (ctxt->type == rule->au_ctxt.type);
3467 case Audit_not_equal:
3468 match = (ctxt->type != rule->au_ctxt.type);
3472 case AUDIT_SUBJ_SEN:
3473 case AUDIT_SUBJ_CLR:
3474 case AUDIT_OBJ_LEV_LOW:
3475 case AUDIT_OBJ_LEV_HIGH:
3476 level = ((field == AUDIT_SUBJ_SEN ||
3477 field == AUDIT_OBJ_LEV_LOW) ?
3478 &ctxt->range.level[0] : &ctxt->range.level[1]);
3481 match = mls_level_eq(&rule->au_ctxt.range.level[0],
3484 case Audit_not_equal:
3485 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
3489 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
3491 !mls_level_eq(&rule->au_ctxt.range.level[0],
3495 match = mls_level_dom(&rule->au_ctxt.range.level[0],
3499 match = (mls_level_dom(level,
3500 &rule->au_ctxt.range.level[0]) &&
3501 !mls_level_eq(level,
3502 &rule->au_ctxt.range.level[0]));
3505 match = mls_level_dom(level,
3506 &rule->au_ctxt.range.level[0]);
3512 read_unlock(&state->ss->policy_rwlock);
3516 static int (*aurule_callback)(void) = audit_update_lsm_rules;
3518 static int aurule_avc_callback(u32 event)
3522 if (event == AVC_CALLBACK_RESET && aurule_callback)
3523 err = aurule_callback();
3527 static int __init aurule_init(void)
3531 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
3533 panic("avc_add_callback() failed, error %d\n", err);
3537 __initcall(aurule_init);
3539 #ifdef CONFIG_NETLABEL
3541 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3542 * @secattr: the NetLabel packet security attributes
3543 * @sid: the SELinux SID
3546 * Attempt to cache the context in @ctx, which was derived from the packet in
3547 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3548 * already been initialized.
3551 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
3556 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3557 if (sid_cache == NULL)
3559 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
3560 if (secattr->cache == NULL) {
3566 secattr->cache->free = kfree;
3567 secattr->cache->data = sid_cache;
3568 secattr->flags |= NETLBL_SECATTR_CACHE;
3572 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
3573 * @secattr: the NetLabel packet security attributes
3574 * @sid: the SELinux SID
3577 * Convert the given NetLabel security attributes in @secattr into a
3578 * SELinux SID. If the @secattr field does not contain a full SELinux
3579 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
3580 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3581 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3582 * conversion for future lookups. Returns zero on success, negative values on
3586 int security_netlbl_secattr_to_sid(struct selinux_state *state,
3587 struct netlbl_lsm_secattr *secattr,
3590 struct policydb *policydb = &state->ss->policydb;
3591 struct sidtab *sidtab = &state->ss->sidtab;
3593 struct context *ctx;
3594 struct context ctx_new;
3596 if (!state->initialized) {
3601 read_lock(&state->ss->policy_rwlock);
3603 if (secattr->flags & NETLBL_SECATTR_CACHE)
3604 *sid = *(u32 *)secattr->cache->data;
3605 else if (secattr->flags & NETLBL_SECATTR_SECID)
3606 *sid = secattr->attr.secid;
3607 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3609 ctx = sidtab_search(sidtab, SECINITSID_NETMSG);
3613 context_init(&ctx_new);
3614 ctx_new.user = ctx->user;
3615 ctx_new.role = ctx->role;
3616 ctx_new.type = ctx->type;
3617 mls_import_netlbl_lvl(policydb, &ctx_new, secattr);
3618 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
3619 rc = mls_import_netlbl_cat(policydb, &ctx_new, secattr);
3624 if (!mls_context_isvalid(policydb, &ctx_new))
3627 rc = sidtab_context_to_sid(sidtab, &ctx_new, sid);
3631 security_netlbl_cache_add(secattr, *sid);
3633 ebitmap_destroy(&ctx_new.range.level[0].cat);
3637 read_unlock(&state->ss->policy_rwlock);
3640 ebitmap_destroy(&ctx_new.range.level[0].cat);
3642 read_unlock(&state->ss->policy_rwlock);
3647 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3648 * @sid: the SELinux SID
3649 * @secattr: the NetLabel packet security attributes
3652 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3653 * Returns zero on success, negative values on failure.
3656 int security_netlbl_sid_to_secattr(struct selinux_state *state,
3657 u32 sid, struct netlbl_lsm_secattr *secattr)
3659 struct policydb *policydb = &state->ss->policydb;
3661 struct context *ctx;
3663 if (!state->initialized)
3666 read_lock(&state->ss->policy_rwlock);
3669 ctx = sidtab_search(&state->ss->sidtab, sid);
3674 secattr->domain = kstrdup(sym_name(policydb, SYM_TYPES, ctx->type - 1),
3676 if (secattr->domain == NULL)
3679 secattr->attr.secid = sid;
3680 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3681 mls_export_netlbl_lvl(policydb, ctx, secattr);
3682 rc = mls_export_netlbl_cat(policydb, ctx, secattr);
3684 read_unlock(&state->ss->policy_rwlock);
3687 #endif /* CONFIG_NETLABEL */
3690 * security_read_policy - read the policy.
3691 * @data: binary policy data
3692 * @len: length of data in bytes
3695 int security_read_policy(struct selinux_state *state,
3696 void **data, size_t *len)
3698 struct policydb *policydb = &state->ss->policydb;
3700 struct policy_file fp;
3702 if (!state->initialized)
3705 *len = security_policydb_len(state);
3707 *data = vmalloc_user(*len);
3714 read_lock(&state->ss->policy_rwlock);
3715 rc = policydb_write(policydb, &fp);
3716 read_unlock(&state->ss->policy_rwlock);
3721 *len = (unsigned long)fp.data - (unsigned long)*data;