2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
21 * Updated: Chad Sellers <csellers@tresys.com>
23 * Added validation of kernel classes and permissions
25 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
27 * Added support for bounds domain and audit messaged on masked permissions
29 * Copyright (C) 2008, 2009 NEC Corporation
30 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
31 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
32 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
33 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, version 2.
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/spinlock.h>
42 #include <linux/rcupdate.h>
43 #include <linux/errno.h>
45 #include <linux/sched.h>
46 #include <linux/audit.h>
47 #include <linux/mutex.h>
48 #include <linux/selinux.h>
49 #include <net/netlabel.h>
59 #include "conditional.h"
67 extern void selnl_notify_policyload(u32 seqno);
69 int selinux_policycap_netpeer;
70 int selinux_policycap_openperm;
72 static DEFINE_RWLOCK(policy_rwlock);
74 static struct sidtab sidtab;
75 struct policydb policydb;
79 * The largest sequence number that has been used when
80 * providing an access decision to the access vector cache.
81 * The sequence number only changes when a policy change
84 static u32 latest_granting;
86 /* Forward declaration. */
87 static int context_struct_to_string(struct context *context, char **scontext,
90 static void context_struct_compute_av(struct context *scontext,
91 struct context *tcontext,
93 struct av_decision *avd);
95 struct selinux_mapping {
96 u16 value; /* policy value */
98 u32 perms[sizeof(u32) * 8];
101 static struct selinux_mapping *current_mapping;
102 static u16 current_mapping_size;
104 static int selinux_set_mapping(struct policydb *pol,
105 struct security_class_mapping *map,
106 struct selinux_mapping **out_map_p,
109 struct selinux_mapping *out_map = NULL;
110 size_t size = sizeof(struct selinux_mapping);
113 bool print_unknown_handle = false;
115 /* Find number of classes in the input mapping */
122 /* Allocate space for the class records, plus one for class zero */
123 out_map = kcalloc(++i, size, GFP_ATOMIC);
127 /* Store the raw class and permission values */
129 while (map[j].name) {
130 struct security_class_mapping *p_in = map + (j++);
131 struct selinux_mapping *p_out = out_map + j;
133 /* An empty class string skips ahead */
134 if (!strcmp(p_in->name, "")) {
135 p_out->num_perms = 0;
139 p_out->value = string_to_security_class(pol, p_in->name);
142 "SELinux: Class %s not defined in policy.\n",
144 if (pol->reject_unknown)
146 p_out->num_perms = 0;
147 print_unknown_handle = true;
152 while (p_in->perms && p_in->perms[k]) {
153 /* An empty permission string skips ahead */
154 if (!*p_in->perms[k]) {
158 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
160 if (!p_out->perms[k]) {
162 "SELinux: Permission %s in class %s not defined in policy.\n",
163 p_in->perms[k], p_in->name);
164 if (pol->reject_unknown)
166 print_unknown_handle = true;
171 p_out->num_perms = k;
174 if (print_unknown_handle)
175 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
176 pol->allow_unknown ? "allowed" : "denied");
178 *out_map_p = out_map;
187 * Get real, policy values from mapped values
190 static u16 unmap_class(u16 tclass)
192 if (tclass < current_mapping_size)
193 return current_mapping[tclass].value;
198 static void map_decision(u16 tclass, struct av_decision *avd,
201 if (tclass < current_mapping_size) {
202 unsigned i, n = current_mapping[tclass].num_perms;
205 for (i = 0, result = 0; i < n; i++) {
206 if (avd->allowed & current_mapping[tclass].perms[i])
208 if (allow_unknown && !current_mapping[tclass].perms[i])
211 avd->allowed = result;
213 for (i = 0, result = 0; i < n; i++)
214 if (avd->auditallow & current_mapping[tclass].perms[i])
216 avd->auditallow = result;
218 for (i = 0, result = 0; i < n; i++) {
219 if (avd->auditdeny & current_mapping[tclass].perms[i])
221 if (!allow_unknown && !current_mapping[tclass].perms[i])
225 * In case the kernel has a bug and requests a permission
226 * between num_perms and the maximum permission number, we
227 * should audit that denial
229 for (; i < (sizeof(u32)*8); i++)
231 avd->auditdeny = result;
237 * Return the boolean value of a constraint expression
238 * when it is applied to the specified source and target
241 * xcontext is a special beast... It is used by the validatetrans rules
242 * only. For these rules, scontext is the context before the transition,
243 * tcontext is the context after the transition, and xcontext is the context
244 * of the process performing the transition. All other callers of
245 * constraint_expr_eval should pass in NULL for xcontext.
247 static int constraint_expr_eval(struct context *scontext,
248 struct context *tcontext,
249 struct context *xcontext,
250 struct constraint_expr *cexpr)
254 struct role_datum *r1, *r2;
255 struct mls_level *l1, *l2;
256 struct constraint_expr *e;
257 int s[CEXPR_MAXDEPTH];
260 for (e = cexpr; e; e = e->next) {
261 switch (e->expr_type) {
277 if (sp == (CEXPR_MAXDEPTH-1))
281 val1 = scontext->user;
282 val2 = tcontext->user;
285 val1 = scontext->type;
286 val2 = tcontext->type;
289 val1 = scontext->role;
290 val2 = tcontext->role;
291 r1 = policydb.role_val_to_struct[val1 - 1];
292 r2 = policydb.role_val_to_struct[val2 - 1];
295 s[++sp] = ebitmap_get_bit(&r1->dominates,
299 s[++sp] = ebitmap_get_bit(&r2->dominates,
303 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
305 !ebitmap_get_bit(&r2->dominates,
313 l1 = &(scontext->range.level[0]);
314 l2 = &(tcontext->range.level[0]);
317 l1 = &(scontext->range.level[0]);
318 l2 = &(tcontext->range.level[1]);
321 l1 = &(scontext->range.level[1]);
322 l2 = &(tcontext->range.level[0]);
325 l1 = &(scontext->range.level[1]);
326 l2 = &(tcontext->range.level[1]);
329 l1 = &(scontext->range.level[0]);
330 l2 = &(scontext->range.level[1]);
333 l1 = &(tcontext->range.level[0]);
334 l2 = &(tcontext->range.level[1]);
339 s[++sp] = mls_level_eq(l1, l2);
342 s[++sp] = !mls_level_eq(l1, l2);
345 s[++sp] = mls_level_dom(l1, l2);
348 s[++sp] = mls_level_dom(l2, l1);
351 s[++sp] = mls_level_incomp(l2, l1);
365 s[++sp] = (val1 == val2);
368 s[++sp] = (val1 != val2);
376 if (sp == (CEXPR_MAXDEPTH-1))
379 if (e->attr & CEXPR_TARGET)
381 else if (e->attr & CEXPR_XTARGET) {
388 if (e->attr & CEXPR_USER)
390 else if (e->attr & CEXPR_ROLE)
392 else if (e->attr & CEXPR_TYPE)
401 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
404 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
422 * security_dump_masked_av - dumps masked permissions during
423 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
425 static int dump_masked_av_helper(void *k, void *d, void *args)
427 struct perm_datum *pdatum = d;
428 char **permission_names = args;
430 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
432 permission_names[pdatum->value - 1] = (char *)k;
437 static void security_dump_masked_av(struct context *scontext,
438 struct context *tcontext,
443 struct common_datum *common_dat;
444 struct class_datum *tclass_dat;
445 struct audit_buffer *ab;
447 char *scontext_name = NULL;
448 char *tcontext_name = NULL;
449 char *permission_names[32];
451 bool need_comma = false;
456 tclass_name = policydb.p_class_val_to_name[tclass - 1];
457 tclass_dat = policydb.class_val_to_struct[tclass - 1];
458 common_dat = tclass_dat->comdatum;
460 /* init permission_names */
462 hashtab_map(common_dat->permissions.table,
463 dump_masked_av_helper, permission_names) < 0)
466 if (hashtab_map(tclass_dat->permissions.table,
467 dump_masked_av_helper, permission_names) < 0)
470 /* get scontext/tcontext in text form */
471 if (context_struct_to_string(scontext,
472 &scontext_name, &length) < 0)
475 if (context_struct_to_string(tcontext,
476 &tcontext_name, &length) < 0)
479 /* audit a message */
480 ab = audit_log_start(current->audit_context,
481 GFP_ATOMIC, AUDIT_SELINUX_ERR);
485 audit_log_format(ab, "op=security_compute_av reason=%s "
486 "scontext=%s tcontext=%s tclass=%s perms=",
487 reason, scontext_name, tcontext_name, tclass_name);
489 for (index = 0; index < 32; index++) {
490 u32 mask = (1 << index);
492 if ((mask & permissions) == 0)
495 audit_log_format(ab, "%s%s",
496 need_comma ? "," : "",
497 permission_names[index]
498 ? permission_names[index] : "????");
503 /* release scontext/tcontext */
504 kfree(tcontext_name);
505 kfree(scontext_name);
511 * security_boundary_permission - drops violated permissions
512 * on boundary constraint.
514 static void type_attribute_bounds_av(struct context *scontext,
515 struct context *tcontext,
517 struct av_decision *avd)
519 struct type_datum *source
520 = policydb.type_val_to_struct[scontext->type - 1];
522 if (source->bounds) {
523 struct context lo_scontext;
524 struct av_decision lo_avd;
527 memset(&lo_avd, 0, sizeof(lo_avd));
529 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
530 lo_scontext.type = source->bounds;
532 context_struct_compute_av(&lo_scontext,
536 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
537 return; /* no masked permission */
538 masked = ~lo_avd.allowed & avd->allowed;
540 /* mask violated permissions */
541 avd->allowed &= ~masked;
543 /* audit masked permissions */
544 security_dump_masked_av(scontext, tcontext,
545 tclass, masked, "bounds");
550 * Compute access vectors based on a context structure pair for
551 * the permissions in a particular class.
553 static void context_struct_compute_av(struct context *scontext,
554 struct context *tcontext,
556 struct av_decision *avd)
558 struct constraint_node *constraint;
559 struct role_allow *ra;
560 struct avtab_key avkey;
561 struct avtab_node *node;
562 struct class_datum *tclass_datum;
563 struct ebitmap *sattr, *tattr;
564 struct ebitmap_node *snode, *tnode;
569 avd->auditdeny = 0xffffffff;
571 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
572 if (printk_ratelimit())
573 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
577 tclass_datum = policydb.class_val_to_struct[tclass - 1];
580 * If a specific type enforcement rule was defined for
581 * this permission check, then use it.
583 avkey.target_class = tclass;
584 avkey.specified = AVTAB_AV;
585 sattr = &policydb.type_attr_map[scontext->type - 1];
586 tattr = &policydb.type_attr_map[tcontext->type - 1];
587 ebitmap_for_each_positive_bit(sattr, snode, i) {
588 ebitmap_for_each_positive_bit(tattr, tnode, j) {
589 avkey.source_type = i + 1;
590 avkey.target_type = j + 1;
591 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
593 node = avtab_search_node_next(node, avkey.specified)) {
594 if (node->key.specified == AVTAB_ALLOWED)
595 avd->allowed |= node->datum.data;
596 else if (node->key.specified == AVTAB_AUDITALLOW)
597 avd->auditallow |= node->datum.data;
598 else if (node->key.specified == AVTAB_AUDITDENY)
599 avd->auditdeny &= node->datum.data;
602 /* Check conditional av table for additional permissions */
603 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
609 * Remove any permissions prohibited by a constraint (this includes
612 constraint = tclass_datum->constraints;
614 if ((constraint->permissions & (avd->allowed)) &&
615 !constraint_expr_eval(scontext, tcontext, NULL,
617 avd->allowed &= ~(constraint->permissions);
619 constraint = constraint->next;
623 * If checking process transition permission and the
624 * role is changing, then check the (current_role, new_role)
627 if (tclass == policydb.process_class &&
628 (avd->allowed & policydb.process_trans_perms) &&
629 scontext->role != tcontext->role) {
630 for (ra = policydb.role_allow; ra; ra = ra->next) {
631 if (scontext->role == ra->role &&
632 tcontext->role == ra->new_role)
636 avd->allowed &= ~policydb.process_trans_perms;
640 * If the given source and target types have boundary
641 * constraint, lazy checks have to mask any violated
642 * permission and notice it to userspace via audit.
644 type_attribute_bounds_av(scontext, tcontext,
648 static int security_validtrans_handle_fail(struct context *ocontext,
649 struct context *ncontext,
650 struct context *tcontext,
653 char *o = NULL, *n = NULL, *t = NULL;
654 u32 olen, nlen, tlen;
656 if (context_struct_to_string(ocontext, &o, &olen) < 0)
658 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
660 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
662 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
663 "security_validate_transition: denied for"
664 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
665 o, n, t, policydb.p_class_val_to_name[tclass-1]);
671 if (!selinux_enforcing)
676 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
679 struct context *ocontext;
680 struct context *ncontext;
681 struct context *tcontext;
682 struct class_datum *tclass_datum;
683 struct constraint_node *constraint;
690 read_lock(&policy_rwlock);
692 tclass = unmap_class(orig_tclass);
694 if (!tclass || tclass > policydb.p_classes.nprim) {
695 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
700 tclass_datum = policydb.class_val_to_struct[tclass - 1];
702 ocontext = sidtab_search(&sidtab, oldsid);
704 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
710 ncontext = sidtab_search(&sidtab, newsid);
712 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
718 tcontext = sidtab_search(&sidtab, tasksid);
720 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
726 constraint = tclass_datum->validatetrans;
728 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
730 rc = security_validtrans_handle_fail(ocontext, ncontext,
734 constraint = constraint->next;
738 read_unlock(&policy_rwlock);
743 * security_bounded_transition - check whether the given
744 * transition is directed to bounded, or not.
745 * It returns 0, if @newsid is bounded by @oldsid.
746 * Otherwise, it returns error code.
748 * @oldsid : current security identifier
749 * @newsid : destinated security identifier
751 int security_bounded_transition(u32 old_sid, u32 new_sid)
753 struct context *old_context, *new_context;
754 struct type_datum *type;
758 read_lock(&policy_rwlock);
760 old_context = sidtab_search(&sidtab, old_sid);
762 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
767 new_context = sidtab_search(&sidtab, new_sid);
769 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
774 /* type/domain unchanged */
775 if (old_context->type == new_context->type) {
780 index = new_context->type;
782 type = policydb.type_val_to_struct[index - 1];
785 /* not bounded anymore */
791 /* @newsid is bounded by @oldsid */
792 if (type->bounds == old_context->type) {
796 index = type->bounds;
800 char *old_name = NULL;
801 char *new_name = NULL;
804 if (!context_struct_to_string(old_context,
805 &old_name, &length) &&
806 !context_struct_to_string(new_context,
807 &new_name, &length)) {
808 audit_log(current->audit_context,
809 GFP_ATOMIC, AUDIT_SELINUX_ERR,
810 "op=security_bounded_transition "
812 "oldcontext=%s newcontext=%s",
819 read_unlock(&policy_rwlock);
824 static void avd_init(struct av_decision *avd)
828 avd->auditdeny = 0xffffffff;
829 avd->seqno = latest_granting;
835 * security_compute_av - Compute access vector decisions.
836 * @ssid: source security identifier
837 * @tsid: target security identifier
838 * @tclass: target security class
839 * @avd: access vector decisions
841 * Compute a set of access vector decisions based on the
842 * SID pair (@ssid, @tsid) for the permissions in @tclass.
844 void security_compute_av(u32 ssid,
847 struct av_decision *avd)
850 struct context *scontext = NULL, *tcontext = NULL;
852 read_lock(&policy_rwlock);
857 scontext = sidtab_search(&sidtab, ssid);
859 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
864 /* permissive domain? */
865 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
866 avd->flags |= AVD_FLAGS_PERMISSIVE;
868 tcontext = sidtab_search(&sidtab, tsid);
870 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
875 tclass = unmap_class(orig_tclass);
876 if (unlikely(orig_tclass && !tclass)) {
877 if (policydb.allow_unknown)
881 context_struct_compute_av(scontext, tcontext, tclass, avd);
882 map_decision(orig_tclass, avd, policydb.allow_unknown);
884 read_unlock(&policy_rwlock);
887 avd->allowed = 0xffffffff;
891 void security_compute_av_user(u32 ssid,
894 struct av_decision *avd)
896 struct context *scontext = NULL, *tcontext = NULL;
898 read_lock(&policy_rwlock);
903 scontext = sidtab_search(&sidtab, ssid);
905 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
910 /* permissive domain? */
911 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
912 avd->flags |= AVD_FLAGS_PERMISSIVE;
914 tcontext = sidtab_search(&sidtab, tsid);
916 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
921 if (unlikely(!tclass)) {
922 if (policydb.allow_unknown)
927 context_struct_compute_av(scontext, tcontext, tclass, avd);
929 read_unlock(&policy_rwlock);
932 avd->allowed = 0xffffffff;
937 * Write the security context string representation of
938 * the context structure `context' into a dynamically
939 * allocated string of the correct size. Set `*scontext'
940 * to point to this string and set `*scontext_len' to
941 * the length of the string.
943 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
951 *scontext_len = context->len;
952 *scontext = kstrdup(context->str, GFP_ATOMIC);
958 /* Compute the size of the context. */
959 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
960 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
961 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
962 *scontext_len += mls_compute_context_len(context);
964 /* Allocate space for the context; caller must free this space. */
965 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
968 *scontext = scontextp;
971 * Copy the user name, role name and type name into the context.
973 sprintf(scontextp, "%s:%s:%s",
974 policydb.p_user_val_to_name[context->user - 1],
975 policydb.p_role_val_to_name[context->role - 1],
976 policydb.p_type_val_to_name[context->type - 1]);
977 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
978 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
979 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
981 mls_sid_to_context(context, &scontextp);
988 #include "initial_sid_to_string.h"
990 const char *security_get_initial_sid_context(u32 sid)
992 if (unlikely(sid > SECINITSID_NUM))
994 return initial_sid_to_string[sid];
997 static int security_sid_to_context_core(u32 sid, char **scontext,
998 u32 *scontext_len, int force)
1000 struct context *context;
1006 if (!ss_initialized) {
1007 if (sid <= SECINITSID_NUM) {
1010 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
1011 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1016 strcpy(scontextp, initial_sid_to_string[sid]);
1017 *scontext = scontextp;
1020 printk(KERN_ERR "SELinux: %s: called before initial "
1021 "load_policy on unknown SID %d\n", __func__, sid);
1025 read_lock(&policy_rwlock);
1027 context = sidtab_search_force(&sidtab, sid);
1029 context = sidtab_search(&sidtab, sid);
1031 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1036 rc = context_struct_to_string(context, scontext, scontext_len);
1038 read_unlock(&policy_rwlock);
1045 * security_sid_to_context - Obtain a context for a given SID.
1046 * @sid: security identifier, SID
1047 * @scontext: security context
1048 * @scontext_len: length in bytes
1050 * Write the string representation of the context associated with @sid
1051 * into a dynamically allocated string of the correct size. Set @scontext
1052 * to point to this string and set @scontext_len to the length of the string.
1054 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
1056 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1059 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1061 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1065 * Caveat: Mutates scontext.
1067 static int string_to_context_struct(struct policydb *pol,
1068 struct sidtab *sidtabp,
1071 struct context *ctx,
1074 struct role_datum *role;
1075 struct type_datum *typdatum;
1076 struct user_datum *usrdatum;
1077 char *scontextp, *p, oldc;
1082 /* Parse the security context. */
1085 scontextp = (char *) scontext;
1087 /* Extract the user. */
1089 while (*p && *p != ':')
1097 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1101 ctx->user = usrdatum->value;
1105 while (*p && *p != ':')
1113 role = hashtab_search(pol->p_roles.table, scontextp);
1116 ctx->role = role->value;
1120 while (*p && *p != ':')
1125 typdatum = hashtab_search(pol->p_types.table, scontextp);
1126 if (!typdatum || typdatum->attribute)
1129 ctx->type = typdatum->value;
1131 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1135 if ((p - scontext) < scontext_len) {
1140 /* Check the validity of the new context. */
1141 if (!policydb_context_isvalid(pol, ctx)) {
1148 context_destroy(ctx);
1152 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1153 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1156 char *scontext2, *str = NULL;
1157 struct context context;
1160 if (!ss_initialized) {
1163 for (i = 1; i < SECINITSID_NUM; i++) {
1164 if (!strcmp(initial_sid_to_string[i], scontext)) {
1169 *sid = SECINITSID_KERNEL;
1174 /* Copy the string so that we can modify the copy as we parse it. */
1175 scontext2 = kmalloc(scontext_len+1, gfp_flags);
1178 memcpy(scontext2, scontext, scontext_len);
1179 scontext2[scontext_len] = 0;
1182 /* Save another copy for storing in uninterpreted form */
1183 str = kstrdup(scontext2, gfp_flags);
1190 read_lock(&policy_rwlock);
1191 rc = string_to_context_struct(&policydb, &sidtab,
1192 scontext2, scontext_len,
1194 if (rc == -EINVAL && force) {
1196 context.len = scontext_len;
1200 rc = sidtab_context_to_sid(&sidtab, &context, sid);
1201 context_destroy(&context);
1203 read_unlock(&policy_rwlock);
1210 * security_context_to_sid - Obtain a SID for a given security context.
1211 * @scontext: security context
1212 * @scontext_len: length in bytes
1213 * @sid: security identifier, SID
1215 * Obtains a SID associated with the security context that
1216 * has the string representation specified by @scontext.
1217 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1218 * memory is available, or 0 on success.
1220 int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
1222 return security_context_to_sid_core(scontext, scontext_len,
1223 sid, SECSID_NULL, GFP_KERNEL, 0);
1227 * security_context_to_sid_default - Obtain a SID for a given security context,
1228 * falling back to specified default if needed.
1230 * @scontext: security context
1231 * @scontext_len: length in bytes
1232 * @sid: security identifier, SID
1233 * @def_sid: default SID to assign on error
1235 * Obtains a SID associated with the security context that
1236 * has the string representation specified by @scontext.
1237 * The default SID is passed to the MLS layer to be used to allow
1238 * kernel labeling of the MLS field if the MLS field is not present
1239 * (for upgrading to MLS without full relabel).
1240 * Implicitly forces adding of the context even if it cannot be mapped yet.
1241 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1242 * memory is available, or 0 on success.
1244 int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1245 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1247 return security_context_to_sid_core(scontext, scontext_len,
1248 sid, def_sid, gfp_flags, 1);
1251 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1254 return security_context_to_sid_core(scontext, scontext_len,
1255 sid, SECSID_NULL, GFP_KERNEL, 1);
1258 static int compute_sid_handle_invalid_context(
1259 struct context *scontext,
1260 struct context *tcontext,
1262 struct context *newcontext)
1264 char *s = NULL, *t = NULL, *n = NULL;
1265 u32 slen, tlen, nlen;
1267 if (context_struct_to_string(scontext, &s, &slen) < 0)
1269 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1271 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1273 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1274 "security_compute_sid: invalid context %s"
1278 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1283 if (!selinux_enforcing)
1288 static int security_compute_sid(u32 ssid,
1295 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1296 struct role_trans *roletr = NULL;
1297 struct avtab_key avkey;
1298 struct avtab_datum *avdatum;
1299 struct avtab_node *node;
1303 if (!ss_initialized) {
1304 switch (orig_tclass) {
1305 case SECCLASS_PROCESS: /* kernel value */
1315 context_init(&newcontext);
1317 read_lock(&policy_rwlock);
1320 tclass = unmap_class(orig_tclass);
1322 tclass = orig_tclass;
1324 scontext = sidtab_search(&sidtab, ssid);
1326 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1331 tcontext = sidtab_search(&sidtab, tsid);
1333 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1339 /* Set the user identity. */
1340 switch (specified) {
1341 case AVTAB_TRANSITION:
1343 /* Use the process user identity. */
1344 newcontext.user = scontext->user;
1347 /* Use the related object owner. */
1348 newcontext.user = tcontext->user;
1352 /* Set the role and type to default values. */
1353 if (tclass == policydb.process_class) {
1354 /* Use the current role and type of process. */
1355 newcontext.role = scontext->role;
1356 newcontext.type = scontext->type;
1358 /* Use the well-defined object role. */
1359 newcontext.role = OBJECT_R_VAL;
1360 /* Use the type of the related object. */
1361 newcontext.type = tcontext->type;
1364 /* Look for a type transition/member/change rule. */
1365 avkey.source_type = scontext->type;
1366 avkey.target_type = tcontext->type;
1367 avkey.target_class = tclass;
1368 avkey.specified = specified;
1369 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1371 /* If no permanent rule, also check for enabled conditional rules */
1373 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1374 for (; node; node = avtab_search_node_next(node, specified)) {
1375 if (node->key.specified & AVTAB_ENABLED) {
1376 avdatum = &node->datum;
1383 /* Use the type from the type transition/member/change rule. */
1384 newcontext.type = avdatum->data;
1387 /* Check for class-specific changes. */
1388 if (tclass == policydb.process_class) {
1389 if (specified & AVTAB_TRANSITION) {
1390 /* Look for a role transition rule. */
1391 for (roletr = policydb.role_tr; roletr;
1392 roletr = roletr->next) {
1393 if (roletr->role == scontext->role &&
1394 roletr->type == tcontext->type) {
1395 /* Use the role transition rule. */
1396 newcontext.role = roletr->new_role;
1403 /* Set the MLS attributes.
1404 This is done last because it may allocate memory. */
1405 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1409 /* Check the validity of the context. */
1410 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1411 rc = compute_sid_handle_invalid_context(scontext,
1418 /* Obtain the sid for the context. */
1419 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1421 read_unlock(&policy_rwlock);
1422 context_destroy(&newcontext);
1428 * security_transition_sid - Compute the SID for a new subject/object.
1429 * @ssid: source security identifier
1430 * @tsid: target security identifier
1431 * @tclass: target security class
1432 * @out_sid: security identifier for new subject/object
1434 * Compute a SID to use for labeling a new subject or object in the
1435 * class @tclass based on a SID pair (@ssid, @tsid).
1436 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1437 * if insufficient memory is available, or %0 if the new SID was
1438 * computed successfully.
1440 int security_transition_sid(u32 ssid,
1445 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1449 int security_transition_sid_user(u32 ssid,
1454 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1459 * security_member_sid - Compute the SID for member selection.
1460 * @ssid: source security identifier
1461 * @tsid: target security identifier
1462 * @tclass: target security class
1463 * @out_sid: security identifier for selected member
1465 * Compute a SID to use when selecting a member of a polyinstantiated
1466 * object of class @tclass based on a SID pair (@ssid, @tsid).
1467 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1468 * if insufficient memory is available, or %0 if the SID was
1469 * computed successfully.
1471 int security_member_sid(u32 ssid,
1476 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid,
1481 * security_change_sid - Compute the SID for object relabeling.
1482 * @ssid: source security identifier
1483 * @tsid: target security identifier
1484 * @tclass: target security class
1485 * @out_sid: security identifier for selected member
1487 * Compute a SID to use for relabeling an object of class @tclass
1488 * based on a SID pair (@ssid, @tsid).
1489 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1490 * if insufficient memory is available, or %0 if the SID was
1491 * computed successfully.
1493 int security_change_sid(u32 ssid,
1498 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid,
1502 /* Clone the SID into the new SID table. */
1503 static int clone_sid(u32 sid,
1504 struct context *context,
1507 struct sidtab *s = arg;
1509 return sidtab_insert(s, sid, context);
1512 static inline int convert_context_handle_invalid_context(struct context *context)
1516 if (selinux_enforcing) {
1522 if (!context_struct_to_string(context, &s, &len)) {
1524 "SELinux: Context %s would be invalid if enforcing\n",
1532 struct convert_context_args {
1533 struct policydb *oldp;
1534 struct policydb *newp;
1538 * Convert the values in the security context
1539 * structure `c' from the values specified
1540 * in the policy `p->oldp' to the values specified
1541 * in the policy `p->newp'. Verify that the
1542 * context is valid under the new policy.
1544 static int convert_context(u32 key,
1548 struct convert_context_args *args;
1549 struct context oldc;
1550 struct role_datum *role;
1551 struct type_datum *typdatum;
1552 struct user_datum *usrdatum;
1561 s = kstrdup(c->str, GFP_KERNEL);
1566 rc = string_to_context_struct(args->newp, NULL, s,
1567 c->len, &ctx, SECSID_NULL);
1571 "SELinux: Context %s became valid (mapped).\n",
1573 /* Replace string with mapped representation. */
1575 memcpy(c, &ctx, sizeof(*c));
1577 } else if (rc == -EINVAL) {
1578 /* Retain string representation for later mapping. */
1582 /* Other error condition, e.g. ENOMEM. */
1584 "SELinux: Unable to map context %s, rc = %d.\n",
1590 rc = context_cpy(&oldc, c);
1596 /* Convert the user. */
1597 usrdatum = hashtab_search(args->newp->p_users.table,
1598 args->oldp->p_user_val_to_name[c->user - 1]);
1601 c->user = usrdatum->value;
1603 /* Convert the role. */
1604 role = hashtab_search(args->newp->p_roles.table,
1605 args->oldp->p_role_val_to_name[c->role - 1]);
1608 c->role = role->value;
1610 /* Convert the type. */
1611 typdatum = hashtab_search(args->newp->p_types.table,
1612 args->oldp->p_type_val_to_name[c->type - 1]);
1615 c->type = typdatum->value;
1617 rc = mls_convert_context(args->oldp, args->newp, c);
1621 /* Check the validity of the new context. */
1622 if (!policydb_context_isvalid(args->newp, c)) {
1623 rc = convert_context_handle_invalid_context(&oldc);
1628 context_destroy(&oldc);
1633 /* Map old representation to string and save it. */
1634 if (context_struct_to_string(&oldc, &s, &len))
1636 context_destroy(&oldc);
1641 "SELinux: Context %s became invalid (unmapped).\n",
1647 static void security_load_policycaps(void)
1649 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1650 POLICYDB_CAPABILITY_NETPEER);
1651 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1652 POLICYDB_CAPABILITY_OPENPERM);
1655 extern void selinux_complete_init(void);
1656 static int security_preserve_bools(struct policydb *p);
1659 * security_load_policy - Load a security policy configuration.
1660 * @data: binary policy data
1661 * @len: length of data in bytes
1663 * Load a new set of security policy configuration data,
1664 * validate it and convert the SID table as necessary.
1665 * This function will flush the access vector cache after
1666 * loading the new policy.
1668 int security_load_policy(void *data, size_t len)
1670 struct policydb oldpolicydb, newpolicydb;
1671 struct sidtab oldsidtab, newsidtab;
1672 struct selinux_mapping *oldmap, *map = NULL;
1673 struct convert_context_args args;
1677 struct policy_file file = { data, len }, *fp = &file;
1679 if (!ss_initialized) {
1681 if (policydb_read(&policydb, fp)) {
1682 avtab_cache_destroy();
1685 if (selinux_set_mapping(&policydb, secclass_map,
1687 ¤t_mapping_size)) {
1688 policydb_destroy(&policydb);
1689 avtab_cache_destroy();
1692 if (policydb_load_isids(&policydb, &sidtab)) {
1693 policydb_destroy(&policydb);
1694 avtab_cache_destroy();
1697 security_load_policycaps();
1699 seqno = ++latest_granting;
1700 selinux_complete_init();
1701 avc_ss_reset(seqno);
1702 selnl_notify_policyload(seqno);
1703 selinux_netlbl_cache_invalidate();
1704 selinux_xfrm_notify_policyload();
1709 sidtab_hash_eval(&sidtab, "sids");
1712 if (policydb_read(&newpolicydb, fp))
1715 if (sidtab_init(&newsidtab)) {
1716 policydb_destroy(&newpolicydb);
1720 if (selinux_set_mapping(&newpolicydb, secclass_map,
1724 rc = security_preserve_bools(&newpolicydb);
1726 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
1730 /* Clone the SID table. */
1731 sidtab_shutdown(&sidtab);
1732 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1738 * Convert the internal representations of contexts
1739 * in the new SID table.
1741 args.oldp = &policydb;
1742 args.newp = &newpolicydb;
1743 rc = sidtab_map(&newsidtab, convert_context, &args);
1747 /* Save the old policydb and SID table to free later. */
1748 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1749 sidtab_set(&oldsidtab, &sidtab);
1751 /* Install the new policydb and SID table. */
1752 write_lock_irq(&policy_rwlock);
1753 memcpy(&policydb, &newpolicydb, sizeof policydb);
1754 sidtab_set(&sidtab, &newsidtab);
1755 security_load_policycaps();
1756 oldmap = current_mapping;
1757 current_mapping = map;
1758 current_mapping_size = map_size;
1759 seqno = ++latest_granting;
1760 write_unlock_irq(&policy_rwlock);
1762 /* Free the old policydb and SID table. */
1763 policydb_destroy(&oldpolicydb);
1764 sidtab_destroy(&oldsidtab);
1767 avc_ss_reset(seqno);
1768 selnl_notify_policyload(seqno);
1769 selinux_netlbl_cache_invalidate();
1770 selinux_xfrm_notify_policyload();
1776 sidtab_destroy(&newsidtab);
1777 policydb_destroy(&newpolicydb);
1783 * security_port_sid - Obtain the SID for a port.
1784 * @protocol: protocol number
1785 * @port: port number
1786 * @out_sid: security identifier
1788 int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
1793 read_lock(&policy_rwlock);
1795 c = policydb.ocontexts[OCON_PORT];
1797 if (c->u.port.protocol == protocol &&
1798 c->u.port.low_port <= port &&
1799 c->u.port.high_port >= port)
1806 rc = sidtab_context_to_sid(&sidtab,
1812 *out_sid = c->sid[0];
1814 *out_sid = SECINITSID_PORT;
1818 read_unlock(&policy_rwlock);
1823 * security_netif_sid - Obtain the SID for a network interface.
1824 * @name: interface name
1825 * @if_sid: interface SID
1827 int security_netif_sid(char *name, u32 *if_sid)
1832 read_lock(&policy_rwlock);
1834 c = policydb.ocontexts[OCON_NETIF];
1836 if (strcmp(name, c->u.name) == 0)
1842 if (!c->sid[0] || !c->sid[1]) {
1843 rc = sidtab_context_to_sid(&sidtab,
1848 rc = sidtab_context_to_sid(&sidtab,
1854 *if_sid = c->sid[0];
1856 *if_sid = SECINITSID_NETIF;
1859 read_unlock(&policy_rwlock);
1863 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1867 for (i = 0; i < 4; i++)
1868 if (addr[i] != (input[i] & mask[i])) {
1877 * security_node_sid - Obtain the SID for a node (host).
1878 * @domain: communication domain aka address family
1880 * @addrlen: address length in bytes
1881 * @out_sid: security identifier
1883 int security_node_sid(u16 domain,
1891 read_lock(&policy_rwlock);
1897 if (addrlen != sizeof(u32)) {
1902 addr = *((u32 *)addrp);
1904 c = policydb.ocontexts[OCON_NODE];
1906 if (c->u.node.addr == (addr & c->u.node.mask))
1914 if (addrlen != sizeof(u64) * 2) {
1918 c = policydb.ocontexts[OCON_NODE6];
1920 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1928 *out_sid = SECINITSID_NODE;
1934 rc = sidtab_context_to_sid(&sidtab,
1940 *out_sid = c->sid[0];
1942 *out_sid = SECINITSID_NODE;
1946 read_unlock(&policy_rwlock);
1953 * security_get_user_sids - Obtain reachable SIDs for a user.
1954 * @fromsid: starting SID
1955 * @username: username
1956 * @sids: array of reachable SIDs for user
1957 * @nel: number of elements in @sids
1959 * Generate the set of SIDs for legal security contexts
1960 * for a given user that can be reached by @fromsid.
1961 * Set *@sids to point to a dynamically allocated
1962 * array containing the set of SIDs. Set *@nel to the
1963 * number of elements in the array.
1966 int security_get_user_sids(u32 fromsid,
1971 struct context *fromcon, usercon;
1972 u32 *mysids = NULL, *mysids2, sid;
1973 u32 mynel = 0, maxnel = SIDS_NEL;
1974 struct user_datum *user;
1975 struct role_datum *role;
1976 struct ebitmap_node *rnode, *tnode;
1982 if (!ss_initialized)
1985 read_lock(&policy_rwlock);
1987 context_init(&usercon);
1989 fromcon = sidtab_search(&sidtab, fromsid);
1995 user = hashtab_search(policydb.p_users.table, username);
2000 usercon.user = user->value;
2002 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
2008 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2009 role = policydb.role_val_to_struct[i];
2011 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2014 if (mls_setup_user_range(fromcon, user, &usercon))
2017 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
2020 if (mynel < maxnel) {
2021 mysids[mynel++] = sid;
2024 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2029 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2032 mysids[mynel++] = sid;
2038 read_unlock(&policy_rwlock);
2044 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2050 for (i = 0, j = 0; i < mynel; i++) {
2051 rc = avc_has_perm_noaudit(fromsid, mysids[i],
2052 SECCLASS_PROCESS, /* kernel value */
2053 PROCESS__TRANSITION, AVC_STRICT,
2056 mysids2[j++] = mysids[i];
2068 * security_genfs_sid - Obtain a SID for a file in a filesystem
2069 * @fstype: filesystem type
2070 * @path: path from root of mount
2071 * @sclass: file security class
2072 * @sid: SID for path
2074 * Obtain a SID to use for a file in a filesystem that
2075 * cannot support xattr or use a fixed labeling behavior like
2076 * transition SIDs or task SIDs.
2078 int security_genfs_sid(const char *fstype,
2085 struct genfs *genfs;
2087 int rc = 0, cmp = 0;
2089 while (path[0] == '/' && path[1] == '/')
2092 read_lock(&policy_rwlock);
2094 sclass = unmap_class(orig_sclass);
2096 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2097 cmp = strcmp(fstype, genfs->fstype);
2102 if (!genfs || cmp) {
2103 *sid = SECINITSID_UNLABELED;
2108 for (c = genfs->head; c; c = c->next) {
2109 len = strlen(c->u.name);
2110 if ((!c->v.sclass || sclass == c->v.sclass) &&
2111 (strncmp(c->u.name, path, len) == 0))
2116 *sid = SECINITSID_UNLABELED;
2122 rc = sidtab_context_to_sid(&sidtab,
2131 read_unlock(&policy_rwlock);
2136 * security_fs_use - Determine how to handle labeling for a filesystem.
2137 * @fstype: filesystem type
2138 * @behavior: labeling behavior
2139 * @sid: SID for filesystem (superblock)
2141 int security_fs_use(
2143 unsigned int *behavior,
2149 read_lock(&policy_rwlock);
2151 c = policydb.ocontexts[OCON_FSUSE];
2153 if (strcmp(fstype, c->u.name) == 0)
2159 *behavior = c->v.behavior;
2161 rc = sidtab_context_to_sid(&sidtab,
2169 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2171 *behavior = SECURITY_FS_USE_NONE;
2174 *behavior = SECURITY_FS_USE_GENFS;
2179 read_unlock(&policy_rwlock);
2183 int security_get_bools(int *len, char ***names, int **values)
2185 int i, rc = -ENOMEM;
2187 read_lock(&policy_rwlock);
2191 *len = policydb.p_bools.nprim;
2197 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
2201 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2205 for (i = 0; i < *len; i++) {
2207 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2208 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
2209 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
2212 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2213 (*names)[i][name_len - 1] = 0;
2217 read_unlock(&policy_rwlock);
2221 for (i = 0; i < *len; i++)
2229 int security_set_bools(int len, int *values)
2232 int lenp, seqno = 0;
2233 struct cond_node *cur;
2235 write_lock_irq(&policy_rwlock);
2237 lenp = policydb.p_bools.nprim;
2243 for (i = 0; i < len; i++) {
2244 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2245 audit_log(current->audit_context, GFP_ATOMIC,
2246 AUDIT_MAC_CONFIG_CHANGE,
2247 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2248 policydb.p_bool_val_to_name[i],
2250 policydb.bool_val_to_struct[i]->state,
2251 audit_get_loginuid(current),
2252 audit_get_sessionid(current));
2255 policydb.bool_val_to_struct[i]->state = 1;
2257 policydb.bool_val_to_struct[i]->state = 0;
2260 for (cur = policydb.cond_list; cur; cur = cur->next) {
2261 rc = evaluate_cond_node(&policydb, cur);
2266 seqno = ++latest_granting;
2269 write_unlock_irq(&policy_rwlock);
2271 avc_ss_reset(seqno);
2272 selnl_notify_policyload(seqno);
2273 selinux_xfrm_notify_policyload();
2278 int security_get_bool_value(int bool)
2283 read_lock(&policy_rwlock);
2285 len = policydb.p_bools.nprim;
2291 rc = policydb.bool_val_to_struct[bool]->state;
2293 read_unlock(&policy_rwlock);
2297 static int security_preserve_bools(struct policydb *p)
2299 int rc, nbools = 0, *bvalues = NULL, i;
2300 char **bnames = NULL;
2301 struct cond_bool_datum *booldatum;
2302 struct cond_node *cur;
2304 rc = security_get_bools(&nbools, &bnames, &bvalues);
2307 for (i = 0; i < nbools; i++) {
2308 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2310 booldatum->state = bvalues[i];
2312 for (cur = p->cond_list; cur; cur = cur->next) {
2313 rc = evaluate_cond_node(p, cur);
2320 for (i = 0; i < nbools; i++)
2329 * security_sid_mls_copy() - computes a new sid based on the given
2330 * sid and the mls portion of mls_sid.
2332 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2334 struct context *context1;
2335 struct context *context2;
2336 struct context newcon;
2341 if (!ss_initialized || !selinux_mls_enabled) {
2346 context_init(&newcon);
2348 read_lock(&policy_rwlock);
2349 context1 = sidtab_search(&sidtab, sid);
2351 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2357 context2 = sidtab_search(&sidtab, mls_sid);
2359 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2365 newcon.user = context1->user;
2366 newcon.role = context1->role;
2367 newcon.type = context1->type;
2368 rc = mls_context_cpy(&newcon, context2);
2372 /* Check the validity of the new context. */
2373 if (!policydb_context_isvalid(&policydb, &newcon)) {
2374 rc = convert_context_handle_invalid_context(&newcon);
2379 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2383 if (!context_struct_to_string(&newcon, &s, &len)) {
2384 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2385 "security_sid_mls_copy: invalid context %s", s);
2390 read_unlock(&policy_rwlock);
2391 context_destroy(&newcon);
2397 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2398 * @nlbl_sid: NetLabel SID
2399 * @nlbl_type: NetLabel labeling protocol type
2400 * @xfrm_sid: XFRM SID
2403 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2404 * resolved into a single SID it is returned via @peer_sid and the function
2405 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2406 * returns a negative value. A table summarizing the behavior is below:
2408 * | function return | @sid
2409 * ------------------------------+-----------------+-----------------
2410 * no peer labels | 0 | SECSID_NULL
2411 * single peer label | 0 | <peer_label>
2412 * multiple, consistent labels | 0 | <peer_label>
2413 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2416 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2421 struct context *nlbl_ctx;
2422 struct context *xfrm_ctx;
2424 /* handle the common (which also happens to be the set of easy) cases
2425 * right away, these two if statements catch everything involving a
2426 * single or absent peer SID/label */
2427 if (xfrm_sid == SECSID_NULL) {
2428 *peer_sid = nlbl_sid;
2431 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2432 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2434 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2435 *peer_sid = xfrm_sid;
2439 /* we don't need to check ss_initialized here since the only way both
2440 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2441 * security server was initialized and ss_initialized was true */
2442 if (!selinux_mls_enabled) {
2443 *peer_sid = SECSID_NULL;
2447 read_lock(&policy_rwlock);
2449 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2451 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2452 __func__, nlbl_sid);
2456 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2458 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2459 __func__, xfrm_sid);
2463 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2466 read_unlock(&policy_rwlock);
2468 /* at present NetLabel SIDs/labels really only carry MLS
2469 * information so if the MLS portion of the NetLabel SID
2470 * matches the MLS portion of the labeled XFRM SID/label
2471 * then pass along the XFRM SID as it is the most
2473 *peer_sid = xfrm_sid;
2475 *peer_sid = SECSID_NULL;
2479 static int get_classes_callback(void *k, void *d, void *args)
2481 struct class_datum *datum = d;
2482 char *name = k, **classes = args;
2483 int value = datum->value - 1;
2485 classes[value] = kstrdup(name, GFP_ATOMIC);
2486 if (!classes[value])
2492 int security_get_classes(char ***classes, int *nclasses)
2496 read_lock(&policy_rwlock);
2498 *nclasses = policydb.p_classes.nprim;
2499 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
2503 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2507 for (i = 0; i < *nclasses; i++)
2508 kfree((*classes)[i]);
2513 read_unlock(&policy_rwlock);
2517 static int get_permissions_callback(void *k, void *d, void *args)
2519 struct perm_datum *datum = d;
2520 char *name = k, **perms = args;
2521 int value = datum->value - 1;
2523 perms[value] = kstrdup(name, GFP_ATOMIC);
2530 int security_get_permissions(char *class, char ***perms, int *nperms)
2532 int rc = -ENOMEM, i;
2533 struct class_datum *match;
2535 read_lock(&policy_rwlock);
2537 match = hashtab_search(policydb.p_classes.table, class);
2539 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
2545 *nperms = match->permissions.nprim;
2546 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
2550 if (match->comdatum) {
2551 rc = hashtab_map(match->comdatum->permissions.table,
2552 get_permissions_callback, *perms);
2557 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2563 read_unlock(&policy_rwlock);
2567 read_unlock(&policy_rwlock);
2568 for (i = 0; i < *nperms; i++)
2574 int security_get_reject_unknown(void)
2576 return policydb.reject_unknown;
2579 int security_get_allow_unknown(void)
2581 return policydb.allow_unknown;
2585 * security_policycap_supported - Check for a specific policy capability
2586 * @req_cap: capability
2589 * This function queries the currently loaded policy to see if it supports the
2590 * capability specified by @req_cap. Returns true (1) if the capability is
2591 * supported, false (0) if it isn't supported.
2594 int security_policycap_supported(unsigned int req_cap)
2598 read_lock(&policy_rwlock);
2599 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2600 read_unlock(&policy_rwlock);
2605 struct selinux_audit_rule {
2607 struct context au_ctxt;
2610 void selinux_audit_rule_free(void *vrule)
2612 struct selinux_audit_rule *rule = vrule;
2615 context_destroy(&rule->au_ctxt);
2620 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2622 struct selinux_audit_rule *tmprule;
2623 struct role_datum *roledatum;
2624 struct type_datum *typedatum;
2625 struct user_datum *userdatum;
2626 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
2631 if (!ss_initialized)
2635 case AUDIT_SUBJ_USER:
2636 case AUDIT_SUBJ_ROLE:
2637 case AUDIT_SUBJ_TYPE:
2638 case AUDIT_OBJ_USER:
2639 case AUDIT_OBJ_ROLE:
2640 case AUDIT_OBJ_TYPE:
2641 /* only 'equals' and 'not equals' fit user, role, and type */
2642 if (op != Audit_equal && op != Audit_not_equal)
2645 case AUDIT_SUBJ_SEN:
2646 case AUDIT_SUBJ_CLR:
2647 case AUDIT_OBJ_LEV_LOW:
2648 case AUDIT_OBJ_LEV_HIGH:
2649 /* we do not allow a range, indicated by the presense of '-' */
2650 if (strchr(rulestr, '-'))
2654 /* only the above fields are valid */
2658 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2662 context_init(&tmprule->au_ctxt);
2664 read_lock(&policy_rwlock);
2666 tmprule->au_seqno = latest_granting;
2669 case AUDIT_SUBJ_USER:
2670 case AUDIT_OBJ_USER:
2671 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2675 tmprule->au_ctxt.user = userdatum->value;
2677 case AUDIT_SUBJ_ROLE:
2678 case AUDIT_OBJ_ROLE:
2679 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2683 tmprule->au_ctxt.role = roledatum->value;
2685 case AUDIT_SUBJ_TYPE:
2686 case AUDIT_OBJ_TYPE:
2687 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2691 tmprule->au_ctxt.type = typedatum->value;
2693 case AUDIT_SUBJ_SEN:
2694 case AUDIT_SUBJ_CLR:
2695 case AUDIT_OBJ_LEV_LOW:
2696 case AUDIT_OBJ_LEV_HIGH:
2697 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2701 read_unlock(&policy_rwlock);
2704 selinux_audit_rule_free(tmprule);
2713 /* Check to see if the rule contains any selinux fields */
2714 int selinux_audit_rule_known(struct audit_krule *rule)
2718 for (i = 0; i < rule->field_count; i++) {
2719 struct audit_field *f = &rule->fields[i];
2721 case AUDIT_SUBJ_USER:
2722 case AUDIT_SUBJ_ROLE:
2723 case AUDIT_SUBJ_TYPE:
2724 case AUDIT_SUBJ_SEN:
2725 case AUDIT_SUBJ_CLR:
2726 case AUDIT_OBJ_USER:
2727 case AUDIT_OBJ_ROLE:
2728 case AUDIT_OBJ_TYPE:
2729 case AUDIT_OBJ_LEV_LOW:
2730 case AUDIT_OBJ_LEV_HIGH:
2738 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
2739 struct audit_context *actx)
2741 struct context *ctxt;
2742 struct mls_level *level;
2743 struct selinux_audit_rule *rule = vrule;
2747 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2748 "selinux_audit_rule_match: missing rule\n");
2752 read_lock(&policy_rwlock);
2754 if (rule->au_seqno < latest_granting) {
2755 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2756 "selinux_audit_rule_match: stale rule\n");
2761 ctxt = sidtab_search(&sidtab, sid);
2763 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2764 "selinux_audit_rule_match: unrecognized SID %d\n",
2770 /* a field/op pair that is not caught here will simply fall through
2773 case AUDIT_SUBJ_USER:
2774 case AUDIT_OBJ_USER:
2777 match = (ctxt->user == rule->au_ctxt.user);
2779 case Audit_not_equal:
2780 match = (ctxt->user != rule->au_ctxt.user);
2784 case AUDIT_SUBJ_ROLE:
2785 case AUDIT_OBJ_ROLE:
2788 match = (ctxt->role == rule->au_ctxt.role);
2790 case Audit_not_equal:
2791 match = (ctxt->role != rule->au_ctxt.role);
2795 case AUDIT_SUBJ_TYPE:
2796 case AUDIT_OBJ_TYPE:
2799 match = (ctxt->type == rule->au_ctxt.type);
2801 case Audit_not_equal:
2802 match = (ctxt->type != rule->au_ctxt.type);
2806 case AUDIT_SUBJ_SEN:
2807 case AUDIT_SUBJ_CLR:
2808 case AUDIT_OBJ_LEV_LOW:
2809 case AUDIT_OBJ_LEV_HIGH:
2810 level = ((field == AUDIT_SUBJ_SEN ||
2811 field == AUDIT_OBJ_LEV_LOW) ?
2812 &ctxt->range.level[0] : &ctxt->range.level[1]);
2815 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2818 case Audit_not_equal:
2819 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2823 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2825 !mls_level_eq(&rule->au_ctxt.range.level[0],
2829 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2833 match = (mls_level_dom(level,
2834 &rule->au_ctxt.range.level[0]) &&
2835 !mls_level_eq(level,
2836 &rule->au_ctxt.range.level[0]));
2839 match = mls_level_dom(level,
2840 &rule->au_ctxt.range.level[0]);
2846 read_unlock(&policy_rwlock);
2850 static int (*aurule_callback)(void) = audit_update_lsm_rules;
2852 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2853 u16 class, u32 perms, u32 *retained)
2857 if (event == AVC_CALLBACK_RESET && aurule_callback)
2858 err = aurule_callback();
2862 static int __init aurule_init(void)
2866 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2867 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2869 panic("avc_add_callback() failed, error %d\n", err);
2873 __initcall(aurule_init);
2875 #ifdef CONFIG_NETLABEL
2877 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2878 * @secattr: the NetLabel packet security attributes
2879 * @sid: the SELinux SID
2882 * Attempt to cache the context in @ctx, which was derived from the packet in
2883 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2884 * already been initialized.
2887 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2892 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2893 if (sid_cache == NULL)
2895 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2896 if (secattr->cache == NULL) {
2902 secattr->cache->free = kfree;
2903 secattr->cache->data = sid_cache;
2904 secattr->flags |= NETLBL_SECATTR_CACHE;
2908 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2909 * @secattr: the NetLabel packet security attributes
2910 * @sid: the SELinux SID
2913 * Convert the given NetLabel security attributes in @secattr into a
2914 * SELinux SID. If the @secattr field does not contain a full SELinux
2915 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2916 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2917 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2918 * conversion for future lookups. Returns zero on success, negative values on
2922 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2926 struct context *ctx;
2927 struct context ctx_new;
2929 if (!ss_initialized) {
2934 read_lock(&policy_rwlock);
2936 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2937 *sid = *(u32 *)secattr->cache->data;
2939 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2940 *sid = secattr->attr.secid;
2942 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2943 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
2945 goto netlbl_secattr_to_sid_return;
2947 context_init(&ctx_new);
2948 ctx_new.user = ctx->user;
2949 ctx_new.role = ctx->role;
2950 ctx_new.type = ctx->type;
2951 mls_import_netlbl_lvl(&ctx_new, secattr);
2952 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2953 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2954 secattr->attr.mls.cat) != 0)
2955 goto netlbl_secattr_to_sid_return;
2956 memcpy(&ctx_new.range.level[1].cat,
2957 &ctx_new.range.level[0].cat,
2958 sizeof(ctx_new.range.level[0].cat));
2960 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2961 goto netlbl_secattr_to_sid_return_cleanup;
2963 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2965 goto netlbl_secattr_to_sid_return_cleanup;
2967 security_netlbl_cache_add(secattr, *sid);
2969 ebitmap_destroy(&ctx_new.range.level[0].cat);
2975 netlbl_secattr_to_sid_return:
2976 read_unlock(&policy_rwlock);
2978 netlbl_secattr_to_sid_return_cleanup:
2979 ebitmap_destroy(&ctx_new.range.level[0].cat);
2980 goto netlbl_secattr_to_sid_return;
2984 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2985 * @sid: the SELinux SID
2986 * @secattr: the NetLabel packet security attributes
2989 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2990 * Returns zero on success, negative values on failure.
2993 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2996 struct context *ctx;
2998 if (!ss_initialized)
3001 read_lock(&policy_rwlock);
3002 ctx = sidtab_search(&sidtab, sid);
3005 goto netlbl_sid_to_secattr_failure;
3007 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
3009 if (secattr->domain == NULL) {
3011 goto netlbl_sid_to_secattr_failure;
3013 secattr->attr.secid = sid;
3014 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3015 mls_export_netlbl_lvl(ctx, secattr);
3016 rc = mls_export_netlbl_cat(ctx, secattr);
3018 goto netlbl_sid_to_secattr_failure;
3019 read_unlock(&policy_rwlock);
3023 netlbl_sid_to_secattr_failure:
3024 read_unlock(&policy_rwlock);
3027 #endif /* CONFIG_NETLABEL */