1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the security services.
10 * Support for enhanced MLS infrastructure.
11 * Support for context based audit filters.
15 * Added conditional policy language extensions
19 * Added support for NetLabel
20 * Added support for the policy capability bitmap
24 * Added validation of kernel classes and permissions
28 * Added support for bounds domain and audit messaged on masked permissions
32 * Added support for runtime switching of the policy type
34 * Copyright (C) 2008, 2009 NEC Corporation
35 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
36 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
37 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/spinlock.h>
44 #include <linux/rcupdate.h>
45 #include <linux/errno.h>
47 #include <linux/sched.h>
48 #include <linux/audit.h>
49 #include <linux/vmalloc.h>
50 #include <net/netlabel.h>
60 #include "conditional.h"
67 #include "policycap_names.h"
69 struct convert_context_args {
70 struct selinux_state *state;
71 struct policydb *oldp;
72 struct policydb *newp;
75 struct selinux_policy_convert_data {
76 struct convert_context_args args;
77 struct sidtab_convert_params sidtab_params;
80 /* Forward declaration. */
81 static int context_struct_to_string(struct policydb *policydb,
82 struct context *context,
86 static int sidtab_entry_to_string(struct policydb *policydb,
87 struct sidtab *sidtab,
88 struct sidtab_entry *entry,
92 static void context_struct_compute_av(struct policydb *policydb,
93 struct context *scontext,
94 struct context *tcontext,
96 struct av_decision *avd,
97 struct extended_perms *xperms);
99 static int selinux_set_mapping(struct policydb *pol,
100 struct security_class_mapping *map,
101 struct selinux_map *out_map)
105 bool print_unknown_handle = false;
107 /* Find number of classes in the input mapping */
114 /* Allocate space for the class records, plus one for class zero */
115 out_map->mapping = kcalloc(++i, sizeof(*out_map->mapping), GFP_ATOMIC);
116 if (!out_map->mapping)
119 /* Store the raw class and permission values */
121 while (map[j].name) {
122 struct security_class_mapping *p_in = map + (j++);
123 struct selinux_mapping *p_out = out_map->mapping + j;
125 /* An empty class string skips ahead */
126 if (!strcmp(p_in->name, "")) {
127 p_out->num_perms = 0;
131 p_out->value = string_to_security_class(pol, p_in->name);
133 pr_info("SELinux: Class %s not defined in policy.\n",
135 if (pol->reject_unknown)
137 p_out->num_perms = 0;
138 print_unknown_handle = true;
143 while (p_in->perms[k]) {
144 /* An empty permission string skips ahead */
145 if (!*p_in->perms[k]) {
149 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
151 if (!p_out->perms[k]) {
152 pr_info("SELinux: Permission %s in class %s not defined in policy.\n",
153 p_in->perms[k], p_in->name);
154 if (pol->reject_unknown)
156 print_unknown_handle = true;
161 p_out->num_perms = k;
164 if (print_unknown_handle)
165 pr_info("SELinux: the above unknown classes and permissions will be %s\n",
166 pol->allow_unknown ? "allowed" : "denied");
171 kfree(out_map->mapping);
172 out_map->mapping = NULL;
177 * Get real, policy values from mapped values
180 static u16 unmap_class(struct selinux_map *map, u16 tclass)
182 if (tclass < map->size)
183 return map->mapping[tclass].value;
189 * Get kernel value for class from its policy value
191 static u16 map_class(struct selinux_map *map, u16 pol_value)
195 for (i = 1; i < map->size; i++) {
196 if (map->mapping[i].value == pol_value)
200 return SECCLASS_NULL;
203 static void map_decision(struct selinux_map *map,
204 u16 tclass, struct av_decision *avd,
207 if (tclass < map->size) {
208 struct selinux_mapping *mapping = &map->mapping[tclass];
209 unsigned int i, n = mapping->num_perms;
212 for (i = 0, result = 0; i < n; i++) {
213 if (avd->allowed & mapping->perms[i])
215 if (allow_unknown && !mapping->perms[i])
218 avd->allowed = result;
220 for (i = 0, result = 0; i < n; i++)
221 if (avd->auditallow & mapping->perms[i])
223 avd->auditallow = result;
225 for (i = 0, result = 0; i < n; i++) {
226 if (avd->auditdeny & mapping->perms[i])
228 if (!allow_unknown && !mapping->perms[i])
232 * In case the kernel has a bug and requests a permission
233 * between num_perms and the maximum permission number, we
234 * should audit that denial
236 for (; i < (sizeof(u32)*8); i++)
238 avd->auditdeny = result;
242 int security_mls_enabled(struct selinux_state *state)
245 struct selinux_policy *policy;
247 if (!selinux_initialized(state))
251 policy = rcu_dereference(state->policy);
252 mls_enabled = policy->policydb.mls_enabled;
258 * Return the boolean value of a constraint expression
259 * when it is applied to the specified source and target
262 * xcontext is a special beast... It is used by the validatetrans rules
263 * only. For these rules, scontext is the context before the transition,
264 * tcontext is the context after the transition, and xcontext is the context
265 * of the process performing the transition. All other callers of
266 * constraint_expr_eval should pass in NULL for xcontext.
268 static int constraint_expr_eval(struct policydb *policydb,
269 struct context *scontext,
270 struct context *tcontext,
271 struct context *xcontext,
272 struct constraint_expr *cexpr)
276 struct role_datum *r1, *r2;
277 struct mls_level *l1, *l2;
278 struct constraint_expr *e;
279 int s[CEXPR_MAXDEPTH];
282 for (e = cexpr; e; e = e->next) {
283 switch (e->expr_type) {
299 if (sp == (CEXPR_MAXDEPTH - 1))
303 val1 = scontext->user;
304 val2 = tcontext->user;
307 val1 = scontext->type;
308 val2 = tcontext->type;
311 val1 = scontext->role;
312 val2 = tcontext->role;
313 r1 = policydb->role_val_to_struct[val1 - 1];
314 r2 = policydb->role_val_to_struct[val2 - 1];
317 s[++sp] = ebitmap_get_bit(&r1->dominates,
321 s[++sp] = ebitmap_get_bit(&r2->dominates,
325 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
327 !ebitmap_get_bit(&r2->dominates,
335 l1 = &(scontext->range.level[0]);
336 l2 = &(tcontext->range.level[0]);
339 l1 = &(scontext->range.level[0]);
340 l2 = &(tcontext->range.level[1]);
343 l1 = &(scontext->range.level[1]);
344 l2 = &(tcontext->range.level[0]);
347 l1 = &(scontext->range.level[1]);
348 l2 = &(tcontext->range.level[1]);
351 l1 = &(scontext->range.level[0]);
352 l2 = &(scontext->range.level[1]);
355 l1 = &(tcontext->range.level[0]);
356 l2 = &(tcontext->range.level[1]);
361 s[++sp] = mls_level_eq(l1, l2);
364 s[++sp] = !mls_level_eq(l1, l2);
367 s[++sp] = mls_level_dom(l1, l2);
370 s[++sp] = mls_level_dom(l2, l1);
373 s[++sp] = mls_level_incomp(l2, l1);
387 s[++sp] = (val1 == val2);
390 s[++sp] = (val1 != val2);
398 if (sp == (CEXPR_MAXDEPTH-1))
401 if (e->attr & CEXPR_TARGET)
403 else if (e->attr & CEXPR_XTARGET) {
410 if (e->attr & CEXPR_USER)
412 else if (e->attr & CEXPR_ROLE)
414 else if (e->attr & CEXPR_TYPE)
423 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
426 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
444 * security_dump_masked_av - dumps masked permissions during
445 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
447 static int dump_masked_av_helper(void *k, void *d, void *args)
449 struct perm_datum *pdatum = d;
450 char **permission_names = args;
452 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
454 permission_names[pdatum->value - 1] = (char *)k;
459 static void security_dump_masked_av(struct policydb *policydb,
460 struct context *scontext,
461 struct context *tcontext,
466 struct common_datum *common_dat;
467 struct class_datum *tclass_dat;
468 struct audit_buffer *ab;
470 char *scontext_name = NULL;
471 char *tcontext_name = NULL;
472 char *permission_names[32];
475 bool need_comma = false;
480 tclass_name = sym_name(policydb, SYM_CLASSES, tclass - 1);
481 tclass_dat = policydb->class_val_to_struct[tclass - 1];
482 common_dat = tclass_dat->comdatum;
484 /* init permission_names */
486 hashtab_map(&common_dat->permissions.table,
487 dump_masked_av_helper, permission_names) < 0)
490 if (hashtab_map(&tclass_dat->permissions.table,
491 dump_masked_av_helper, permission_names) < 0)
494 /* get scontext/tcontext in text form */
495 if (context_struct_to_string(policydb, scontext,
496 &scontext_name, &length) < 0)
499 if (context_struct_to_string(policydb, tcontext,
500 &tcontext_name, &length) < 0)
503 /* audit a message */
504 ab = audit_log_start(audit_context(),
505 GFP_ATOMIC, AUDIT_SELINUX_ERR);
509 audit_log_format(ab, "op=security_compute_av reason=%s "
510 "scontext=%s tcontext=%s tclass=%s perms=",
511 reason, scontext_name, tcontext_name, tclass_name);
513 for (index = 0; index < 32; index++) {
514 u32 mask = (1 << index);
516 if ((mask & permissions) == 0)
519 audit_log_format(ab, "%s%s",
520 need_comma ? "," : "",
521 permission_names[index]
522 ? permission_names[index] : "????");
527 /* release scontext/tcontext */
528 kfree(tcontext_name);
529 kfree(scontext_name);
535 * security_boundary_permission - drops violated permissions
536 * on boundary constraint.
538 static void type_attribute_bounds_av(struct policydb *policydb,
539 struct context *scontext,
540 struct context *tcontext,
542 struct av_decision *avd)
544 struct context lo_scontext;
545 struct context lo_tcontext, *tcontextp = tcontext;
546 struct av_decision lo_avd;
547 struct type_datum *source;
548 struct type_datum *target;
551 source = policydb->type_val_to_struct[scontext->type - 1];
557 target = policydb->type_val_to_struct[tcontext->type - 1];
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);
614 * Compute access vectors and extended permissions based on a context
615 * structure pair for the permissions in a particular class.
617 static void context_struct_compute_av(struct policydb *policydb,
618 struct context *scontext,
619 struct context *tcontext,
621 struct av_decision *avd,
622 struct extended_perms *xperms)
624 struct constraint_node *constraint;
625 struct role_allow *ra;
626 struct avtab_key avkey;
627 struct avtab_node *node;
628 struct class_datum *tclass_datum;
629 struct ebitmap *sattr, *tattr;
630 struct ebitmap_node *snode, *tnode;
635 avd->auditdeny = 0xffffffff;
637 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
641 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
642 if (printk_ratelimit())
643 pr_warn("SELinux: Invalid class %hu\n", tclass);
647 tclass_datum = policydb->class_val_to_struct[tclass - 1];
650 * If a specific type enforcement rule was defined for
651 * this permission check, then use it.
653 avkey.target_class = tclass;
654 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
655 sattr = &policydb->type_attr_map_array[scontext->type - 1];
656 tattr = &policydb->type_attr_map_array[tcontext->type - 1];
657 ebitmap_for_each_positive_bit(sattr, snode, i) {
658 ebitmap_for_each_positive_bit(tattr, tnode, j) {
659 avkey.source_type = i + 1;
660 avkey.target_type = j + 1;
661 for (node = avtab_search_node(&policydb->te_avtab,
664 node = avtab_search_node_next(node, avkey.specified)) {
665 if (node->key.specified == AVTAB_ALLOWED)
666 avd->allowed |= node->datum.u.data;
667 else if (node->key.specified == AVTAB_AUDITALLOW)
668 avd->auditallow |= node->datum.u.data;
669 else if (node->key.specified == AVTAB_AUDITDENY)
670 avd->auditdeny &= node->datum.u.data;
671 else if (xperms && (node->key.specified & AVTAB_XPERMS))
672 services_compute_xperms_drivers(xperms, node);
675 /* Check conditional av table for additional permissions */
676 cond_compute_av(&policydb->te_cond_avtab, &avkey,
683 * Remove any permissions prohibited by a constraint (this includes
686 constraint = tclass_datum->constraints;
688 if ((constraint->permissions & (avd->allowed)) &&
689 !constraint_expr_eval(policydb, scontext, tcontext, NULL,
691 avd->allowed &= ~(constraint->permissions);
693 constraint = constraint->next;
697 * If checking process transition permission and the
698 * role is changing, then check the (current_role, new_role)
701 if (tclass == policydb->process_class &&
702 (avd->allowed & policydb->process_trans_perms) &&
703 scontext->role != tcontext->role) {
704 for (ra = policydb->role_allow; ra; ra = ra->next) {
705 if (scontext->role == ra->role &&
706 tcontext->role == ra->new_role)
710 avd->allowed &= ~policydb->process_trans_perms;
714 * If the given source and target types have boundary
715 * constraint, lazy checks have to mask any violated
716 * permission and notice it to userspace via audit.
718 type_attribute_bounds_av(policydb, scontext, tcontext,
722 static int security_validtrans_handle_fail(struct selinux_state *state,
723 struct selinux_policy *policy,
724 struct sidtab_entry *oentry,
725 struct sidtab_entry *nentry,
726 struct sidtab_entry *tentry,
729 struct policydb *p = &policy->policydb;
730 struct sidtab *sidtab = policy->sidtab;
731 char *o = NULL, *n = NULL, *t = NULL;
732 u32 olen, nlen, tlen;
734 if (sidtab_entry_to_string(p, sidtab, oentry, &o, &olen))
736 if (sidtab_entry_to_string(p, sidtab, nentry, &n, &nlen))
738 if (sidtab_entry_to_string(p, sidtab, tentry, &t, &tlen))
740 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
741 "op=security_validate_transition seresult=denied"
742 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
743 o, n, t, sym_name(p, SYM_CLASSES, tclass-1));
749 if (!enforcing_enabled(state))
754 static int security_compute_validatetrans(struct selinux_state *state,
755 u32 oldsid, u32 newsid, u32 tasksid,
756 u16 orig_tclass, bool user)
758 struct selinux_policy *policy;
759 struct policydb *policydb;
760 struct sidtab *sidtab;
761 struct sidtab_entry *oentry;
762 struct sidtab_entry *nentry;
763 struct sidtab_entry *tentry;
764 struct class_datum *tclass_datum;
765 struct constraint_node *constraint;
770 if (!selinux_initialized(state))
775 policy = rcu_dereference(state->policy);
776 policydb = &policy->policydb;
777 sidtab = policy->sidtab;
780 tclass = unmap_class(&policy->map, orig_tclass);
782 tclass = orig_tclass;
784 if (!tclass || tclass > policydb->p_classes.nprim) {
788 tclass_datum = policydb->class_val_to_struct[tclass - 1];
790 oentry = sidtab_search_entry(sidtab, oldsid);
792 pr_err("SELinux: %s: unrecognized SID %d\n",
798 nentry = sidtab_search_entry(sidtab, newsid);
800 pr_err("SELinux: %s: unrecognized SID %d\n",
806 tentry = sidtab_search_entry(sidtab, tasksid);
808 pr_err("SELinux: %s: unrecognized SID %d\n",
814 constraint = tclass_datum->validatetrans;
816 if (!constraint_expr_eval(policydb, &oentry->context,
817 &nentry->context, &tentry->context,
822 rc = security_validtrans_handle_fail(state,
830 constraint = constraint->next;
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 selinux_policy *policy;
867 struct policydb *policydb;
868 struct sidtab *sidtab;
869 struct sidtab_entry *old_entry, *new_entry;
870 struct type_datum *type;
874 if (!selinux_initialized(state))
878 policy = rcu_dereference(state->policy);
879 policydb = &policy->policydb;
880 sidtab = policy->sidtab;
883 old_entry = sidtab_search_entry(sidtab, old_sid);
885 pr_err("SELinux: %s: unrecognized SID %u\n",
891 new_entry = sidtab_search_entry(sidtab, new_sid);
893 pr_err("SELinux: %s: unrecognized SID %u\n",
899 /* type/domain unchanged */
900 if (old_entry->context.type == new_entry->context.type)
903 index = new_entry->context.type;
905 type = policydb->type_val_to_struct[index - 1];
908 /* not bounded anymore */
913 /* @newsid is bounded by @oldsid */
915 if (type->bounds == old_entry->context.type)
918 index = type->bounds;
922 char *old_name = NULL;
923 char *new_name = NULL;
926 if (!sidtab_entry_to_string(policydb, sidtab, old_entry,
927 &old_name, &length) &&
928 !sidtab_entry_to_string(policydb, sidtab, new_entry,
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",
946 static void avd_init(struct selinux_policy *policy, struct av_decision *avd)
950 avd->auditdeny = 0xffffffff;
952 avd->seqno = policy->latest_granting;
958 void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
959 struct avtab_node *node)
963 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
964 if (xpermd->driver != node->datum.u.xperms->driver)
966 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
967 if (!security_xperm_test(node->datum.u.xperms->perms.p,
974 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
975 xpermd->used |= XPERMS_ALLOWED;
976 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
977 memset(xpermd->allowed->p, 0xff,
978 sizeof(xpermd->allowed->p));
980 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
981 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
982 xpermd->allowed->p[i] |=
983 node->datum.u.xperms->perms.p[i];
985 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
986 xpermd->used |= XPERMS_AUDITALLOW;
987 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
988 memset(xpermd->auditallow->p, 0xff,
989 sizeof(xpermd->auditallow->p));
991 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
992 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
993 xpermd->auditallow->p[i] |=
994 node->datum.u.xperms->perms.p[i];
996 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
997 xpermd->used |= XPERMS_DONTAUDIT;
998 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
999 memset(xpermd->dontaudit->p, 0xff,
1000 sizeof(xpermd->dontaudit->p));
1002 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
1003 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
1004 xpermd->dontaudit->p[i] |=
1005 node->datum.u.xperms->perms.p[i];
1012 void security_compute_xperms_decision(struct selinux_state *state,
1017 struct extended_perms_decision *xpermd)
1019 struct selinux_policy *policy;
1020 struct policydb *policydb;
1021 struct sidtab *sidtab;
1023 struct context *scontext, *tcontext;
1024 struct avtab_key avkey;
1025 struct avtab_node *node;
1026 struct ebitmap *sattr, *tattr;
1027 struct ebitmap_node *snode, *tnode;
1030 xpermd->driver = driver;
1032 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1033 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1034 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1037 if (!selinux_initialized(state))
1040 policy = rcu_dereference(state->policy);
1041 policydb = &policy->policydb;
1042 sidtab = policy->sidtab;
1044 scontext = sidtab_search(sidtab, ssid);
1046 pr_err("SELinux: %s: unrecognized SID %d\n",
1051 tcontext = sidtab_search(sidtab, tsid);
1053 pr_err("SELinux: %s: unrecognized SID %d\n",
1058 tclass = unmap_class(&policy->map, orig_tclass);
1059 if (unlikely(orig_tclass && !tclass)) {
1060 if (policydb->allow_unknown)
1066 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
1067 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1071 avkey.target_class = tclass;
1072 avkey.specified = AVTAB_XPERMS;
1073 sattr = &policydb->type_attr_map_array[scontext->type - 1];
1074 tattr = &policydb->type_attr_map_array[tcontext->type - 1];
1075 ebitmap_for_each_positive_bit(sattr, snode, i) {
1076 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1077 avkey.source_type = i + 1;
1078 avkey.target_type = j + 1;
1079 for (node = avtab_search_node(&policydb->te_avtab,
1082 node = avtab_search_node_next(node, avkey.specified))
1083 services_compute_xperms_decision(xpermd, node);
1085 cond_compute_xperms(&policydb->te_cond_avtab,
1093 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1098 * security_compute_av - Compute access vector decisions.
1099 * @ssid: source security identifier
1100 * @tsid: target security identifier
1101 * @tclass: target security class
1102 * @avd: access vector decisions
1103 * @xperms: extended permissions
1105 * Compute a set of access vector decisions based on the
1106 * SID pair (@ssid, @tsid) for the permissions in @tclass.
1108 void security_compute_av(struct selinux_state *state,
1112 struct av_decision *avd,
1113 struct extended_perms *xperms)
1115 struct selinux_policy *policy;
1116 struct policydb *policydb;
1117 struct sidtab *sidtab;
1119 struct context *scontext = NULL, *tcontext = NULL;
1122 policy = rcu_dereference(state->policy);
1123 avd_init(policy, avd);
1125 if (!selinux_initialized(state))
1128 policydb = &policy->policydb;
1129 sidtab = policy->sidtab;
1131 scontext = sidtab_search(sidtab, ssid);
1133 pr_err("SELinux: %s: unrecognized SID %d\n",
1138 /* permissive domain? */
1139 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1140 avd->flags |= AVD_FLAGS_PERMISSIVE;
1142 tcontext = sidtab_search(sidtab, tsid);
1144 pr_err("SELinux: %s: unrecognized SID %d\n",
1149 tclass = unmap_class(&policy->map, orig_tclass);
1150 if (unlikely(orig_tclass && !tclass)) {
1151 if (policydb->allow_unknown)
1155 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1157 map_decision(&policy->map, orig_tclass, avd,
1158 policydb->allow_unknown);
1163 avd->allowed = 0xffffffff;
1167 void security_compute_av_user(struct selinux_state *state,
1171 struct av_decision *avd)
1173 struct selinux_policy *policy;
1174 struct policydb *policydb;
1175 struct sidtab *sidtab;
1176 struct context *scontext = NULL, *tcontext = NULL;
1179 policy = rcu_dereference(state->policy);
1180 avd_init(policy, avd);
1181 if (!selinux_initialized(state))
1184 policydb = &policy->policydb;
1185 sidtab = policy->sidtab;
1187 scontext = sidtab_search(sidtab, ssid);
1189 pr_err("SELinux: %s: unrecognized SID %d\n",
1194 /* permissive domain? */
1195 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1196 avd->flags |= AVD_FLAGS_PERMISSIVE;
1198 tcontext = sidtab_search(sidtab, tsid);
1200 pr_err("SELinux: %s: unrecognized SID %d\n",
1205 if (unlikely(!tclass)) {
1206 if (policydb->allow_unknown)
1211 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1217 avd->allowed = 0xffffffff;
1222 * Write the security context string representation of
1223 * the context structure `context' into a dynamically
1224 * allocated string of the correct size. Set `*scontext'
1225 * to point to this string and set `*scontext_len' to
1226 * the length of the string.
1228 static int context_struct_to_string(struct policydb *p,
1229 struct context *context,
1230 char **scontext, u32 *scontext_len)
1239 *scontext_len = context->len;
1241 *scontext = kstrdup(context->str, GFP_ATOMIC);
1248 /* Compute the size of the context. */
1249 *scontext_len += strlen(sym_name(p, SYM_USERS, context->user - 1)) + 1;
1250 *scontext_len += strlen(sym_name(p, SYM_ROLES, context->role - 1)) + 1;
1251 *scontext_len += strlen(sym_name(p, SYM_TYPES, context->type - 1)) + 1;
1252 *scontext_len += mls_compute_context_len(p, context);
1257 /* Allocate space for the context; caller must free this space. */
1258 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1261 *scontext = scontextp;
1264 * Copy the user name, role name and type name into the context.
1266 scontextp += sprintf(scontextp, "%s:%s:%s",
1267 sym_name(p, SYM_USERS, context->user - 1),
1268 sym_name(p, SYM_ROLES, context->role - 1),
1269 sym_name(p, SYM_TYPES, context->type - 1));
1271 mls_sid_to_context(p, context, &scontextp);
1278 static int sidtab_entry_to_string(struct policydb *p,
1279 struct sidtab *sidtab,
1280 struct sidtab_entry *entry,
1281 char **scontext, u32 *scontext_len)
1283 int rc = sidtab_sid2str_get(sidtab, entry, scontext, scontext_len);
1288 rc = context_struct_to_string(p, &entry->context, scontext,
1290 if (!rc && scontext)
1291 sidtab_sid2str_put(sidtab, entry, *scontext, *scontext_len);
1295 #include "initial_sid_to_string.h"
1297 int security_sidtab_hash_stats(struct selinux_state *state, char *page)
1299 struct selinux_policy *policy;
1302 if (!selinux_initialized(state)) {
1303 pr_err("SELinux: %s: called before initial load_policy\n",
1309 policy = rcu_dereference(state->policy);
1310 rc = sidtab_hash_stats(policy->sidtab, page);
1316 const char *security_get_initial_sid_context(u32 sid)
1318 if (unlikely(sid > SECINITSID_NUM))
1320 return initial_sid_to_string[sid];
1323 static int security_sid_to_context_core(struct selinux_state *state,
1324 u32 sid, char **scontext,
1325 u32 *scontext_len, int force,
1328 struct selinux_policy *policy;
1329 struct policydb *policydb;
1330 struct sidtab *sidtab;
1331 struct sidtab_entry *entry;
1338 if (!selinux_initialized(state)) {
1339 if (sid <= SECINITSID_NUM) {
1341 const char *s = initial_sid_to_string[sid];
1345 *scontext_len = strlen(s) + 1;
1348 scontextp = kmemdup(s, *scontext_len, GFP_ATOMIC);
1351 *scontext = scontextp;
1354 pr_err("SELinux: %s: called before initial "
1355 "load_policy on unknown SID %d\n", __func__, sid);
1359 policy = rcu_dereference(state->policy);
1360 policydb = &policy->policydb;
1361 sidtab = policy->sidtab;
1364 entry = sidtab_search_entry_force(sidtab, sid);
1366 entry = sidtab_search_entry(sidtab, sid);
1368 pr_err("SELinux: %s: unrecognized SID %d\n",
1373 if (only_invalid && !entry->context.len)
1376 rc = sidtab_entry_to_string(policydb, sidtab, entry, scontext,
1386 * security_sid_to_context - Obtain a context for a given SID.
1387 * @sid: security identifier, SID
1388 * @scontext: security context
1389 * @scontext_len: length in bytes
1391 * Write the string representation of the context associated with @sid
1392 * into a dynamically allocated string of the correct size. Set @scontext
1393 * to point to this string and set @scontext_len to the length of the string.
1395 int security_sid_to_context(struct selinux_state *state,
1396 u32 sid, char **scontext, u32 *scontext_len)
1398 return security_sid_to_context_core(state, sid, scontext,
1399 scontext_len, 0, 0);
1402 int security_sid_to_context_force(struct selinux_state *state, u32 sid,
1403 char **scontext, u32 *scontext_len)
1405 return security_sid_to_context_core(state, sid, scontext,
1406 scontext_len, 1, 0);
1410 * security_sid_to_context_inval - Obtain a context for a given SID if it
1412 * @sid: security identifier, SID
1413 * @scontext: security context
1414 * @scontext_len: length in bytes
1416 * Write the string representation of the context associated with @sid
1417 * into a dynamically allocated string of the correct size, but only if the
1418 * context is invalid in the current policy. Set @scontext to point to
1419 * this string (or NULL if the context is valid) and set @scontext_len to
1420 * the length of the string (or 0 if the context is valid).
1422 int security_sid_to_context_inval(struct selinux_state *state, u32 sid,
1423 char **scontext, u32 *scontext_len)
1425 return security_sid_to_context_core(state, sid, scontext,
1426 scontext_len, 1, 1);
1430 * Caveat: Mutates scontext.
1432 static int string_to_context_struct(struct policydb *pol,
1433 struct sidtab *sidtabp,
1435 struct context *ctx,
1438 struct role_datum *role;
1439 struct type_datum *typdatum;
1440 struct user_datum *usrdatum;
1441 char *scontextp, *p, oldc;
1446 /* Parse the security context. */
1449 scontextp = (char *) scontext;
1451 /* Extract the user. */
1453 while (*p && *p != ':')
1461 usrdatum = symtab_search(&pol->p_users, scontextp);
1465 ctx->user = usrdatum->value;
1469 while (*p && *p != ':')
1477 role = symtab_search(&pol->p_roles, scontextp);
1480 ctx->role = role->value;
1484 while (*p && *p != ':')
1489 typdatum = symtab_search(&pol->p_types, scontextp);
1490 if (!typdatum || typdatum->attribute)
1493 ctx->type = typdatum->value;
1495 rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid);
1499 /* Check the validity of the new context. */
1501 if (!policydb_context_isvalid(pol, ctx))
1506 context_destroy(ctx);
1510 static int security_context_to_sid_core(struct selinux_state *state,
1511 const char *scontext, u32 scontext_len,
1512 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1515 struct selinux_policy *policy;
1516 struct policydb *policydb;
1517 struct sidtab *sidtab;
1518 char *scontext2, *str = NULL;
1519 struct context context;
1522 /* An empty security context is never valid. */
1526 /* Copy the string to allow changes and ensure a NUL terminator */
1527 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1531 if (!selinux_initialized(state)) {
1534 for (i = 1; i < SECINITSID_NUM; i++) {
1535 const char *s = initial_sid_to_string[i];
1537 if (s && !strcmp(s, scontext2)) {
1542 *sid = SECINITSID_KERNEL;
1548 /* Save another copy for storing in uninterpreted form */
1550 str = kstrdup(scontext2, gfp_flags);
1555 policy = rcu_dereference(state->policy);
1556 policydb = &policy->policydb;
1557 sidtab = policy->sidtab;
1558 rc = string_to_context_struct(policydb, sidtab, scontext2,
1560 if (rc == -EINVAL && force) {
1562 context.len = strlen(str) + 1;
1566 rc = sidtab_context_to_sid(sidtab, &context, sid);
1567 context_destroy(&context);
1577 * security_context_to_sid - Obtain a SID for a given security context.
1578 * @scontext: security context
1579 * @scontext_len: length in bytes
1580 * @sid: security identifier, SID
1581 * @gfp: context for the allocation
1583 * Obtains a SID associated with the security context that
1584 * has the string representation specified by @scontext.
1585 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1586 * memory is available, or 0 on success.
1588 int security_context_to_sid(struct selinux_state *state,
1589 const char *scontext, u32 scontext_len, u32 *sid,
1592 return security_context_to_sid_core(state, scontext, scontext_len,
1593 sid, SECSID_NULL, gfp, 0);
1596 int security_context_str_to_sid(struct selinux_state *state,
1597 const char *scontext, u32 *sid, gfp_t gfp)
1599 return security_context_to_sid(state, scontext, strlen(scontext),
1604 * security_context_to_sid_default - Obtain a SID for a given security context,
1605 * falling back to specified default if needed.
1607 * @scontext: security context
1608 * @scontext_len: length in bytes
1609 * @sid: security identifier, SID
1610 * @def_sid: default SID to assign on error
1612 * Obtains a SID associated with the security context that
1613 * has the string representation specified by @scontext.
1614 * The default SID is passed to the MLS layer to be used to allow
1615 * kernel labeling of the MLS field if the MLS field is not present
1616 * (for upgrading to MLS without full relabel).
1617 * Implicitly forces adding of the context even if it cannot be mapped yet.
1618 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1619 * memory is available, or 0 on success.
1621 int security_context_to_sid_default(struct selinux_state *state,
1622 const char *scontext, u32 scontext_len,
1623 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1625 return security_context_to_sid_core(state, scontext, scontext_len,
1626 sid, def_sid, gfp_flags, 1);
1629 int security_context_to_sid_force(struct selinux_state *state,
1630 const char *scontext, u32 scontext_len,
1633 return security_context_to_sid_core(state, scontext, scontext_len,
1634 sid, SECSID_NULL, GFP_KERNEL, 1);
1637 static int compute_sid_handle_invalid_context(
1638 struct selinux_state *state,
1639 struct selinux_policy *policy,
1640 struct sidtab_entry *sentry,
1641 struct sidtab_entry *tentry,
1643 struct context *newcontext)
1645 struct policydb *policydb = &policy->policydb;
1646 struct sidtab *sidtab = policy->sidtab;
1647 char *s = NULL, *t = NULL, *n = NULL;
1648 u32 slen, tlen, nlen;
1649 struct audit_buffer *ab;
1651 if (sidtab_entry_to_string(policydb, sidtab, sentry, &s, &slen))
1653 if (sidtab_entry_to_string(policydb, sidtab, tentry, &t, &tlen))
1655 if (context_struct_to_string(policydb, newcontext, &n, &nlen))
1657 ab = audit_log_start(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR);
1658 audit_log_format(ab,
1659 "op=security_compute_sid invalid_context=");
1660 /* no need to record the NUL with untrusted strings */
1661 audit_log_n_untrustedstring(ab, n, nlen - 1);
1662 audit_log_format(ab, " scontext=%s tcontext=%s tclass=%s",
1663 s, t, sym_name(policydb, SYM_CLASSES, tclass-1));
1669 if (!enforcing_enabled(state))
1674 static void filename_compute_type(struct policydb *policydb,
1675 struct context *newcontext,
1676 u32 stype, u32 ttype, u16 tclass,
1677 const char *objname)
1679 struct filename_trans_key ft;
1680 struct filename_trans_datum *datum;
1683 * Most filename trans rules are going to live in specific directories
1684 * like /dev or /var/run. This bitmap will quickly skip rule searches
1685 * if the ttype does not contain any rules.
1687 if (!ebitmap_get_bit(&policydb->filename_trans_ttypes, ttype))
1694 datum = policydb_filenametr_search(policydb, &ft);
1696 if (ebitmap_get_bit(&datum->stypes, stype - 1)) {
1697 newcontext->type = datum->otype;
1700 datum = datum->next;
1704 static int security_compute_sid(struct selinux_state *state,
1709 const char *objname,
1713 struct selinux_policy *policy;
1714 struct policydb *policydb;
1715 struct sidtab *sidtab;
1716 struct class_datum *cladatum = NULL;
1717 struct context *scontext, *tcontext, newcontext;
1718 struct sidtab_entry *sentry, *tentry;
1719 struct avtab_key avkey;
1720 struct avtab_datum *avdatum;
1721 struct avtab_node *node;
1726 if (!selinux_initialized(state)) {
1727 switch (orig_tclass) {
1728 case SECCLASS_PROCESS: /* kernel value */
1738 context_init(&newcontext);
1742 policy = rcu_dereference(state->policy);
1745 tclass = unmap_class(&policy->map, orig_tclass);
1746 sock = security_is_socket_class(orig_tclass);
1748 tclass = orig_tclass;
1749 sock = security_is_socket_class(map_class(&policy->map,
1753 policydb = &policy->policydb;
1754 sidtab = policy->sidtab;
1756 sentry = sidtab_search_entry(sidtab, ssid);
1758 pr_err("SELinux: %s: unrecognized SID %d\n",
1763 tentry = sidtab_search_entry(sidtab, tsid);
1765 pr_err("SELinux: %s: unrecognized SID %d\n",
1771 scontext = &sentry->context;
1772 tcontext = &tentry->context;
1774 if (tclass && tclass <= policydb->p_classes.nprim)
1775 cladatum = policydb->class_val_to_struct[tclass - 1];
1777 /* Set the user identity. */
1778 switch (specified) {
1779 case AVTAB_TRANSITION:
1781 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1782 newcontext.user = tcontext->user;
1784 /* notice this gets both DEFAULT_SOURCE and unset */
1785 /* Use the process user identity. */
1786 newcontext.user = scontext->user;
1790 /* Use the related object owner. */
1791 newcontext.user = tcontext->user;
1795 /* Set the role to default values. */
1796 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
1797 newcontext.role = scontext->role;
1798 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1799 newcontext.role = tcontext->role;
1801 if ((tclass == policydb->process_class) || sock)
1802 newcontext.role = scontext->role;
1804 newcontext.role = OBJECT_R_VAL;
1807 /* Set the type to default values. */
1808 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
1809 newcontext.type = scontext->type;
1810 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
1811 newcontext.type = tcontext->type;
1813 if ((tclass == policydb->process_class) || sock) {
1814 /* Use the type of process. */
1815 newcontext.type = scontext->type;
1817 /* Use the type of the related object. */
1818 newcontext.type = tcontext->type;
1822 /* Look for a type transition/member/change rule. */
1823 avkey.source_type = scontext->type;
1824 avkey.target_type = tcontext->type;
1825 avkey.target_class = tclass;
1826 avkey.specified = specified;
1827 avdatum = avtab_search(&policydb->te_avtab, &avkey);
1829 /* If no permanent rule, also check for enabled conditional rules */
1831 node = avtab_search_node(&policydb->te_cond_avtab, &avkey);
1832 for (; node; node = avtab_search_node_next(node, specified)) {
1833 if (node->key.specified & AVTAB_ENABLED) {
1834 avdatum = &node->datum;
1841 /* Use the type from the type transition/member/change rule. */
1842 newcontext.type = avdatum->u.data;
1845 /* if we have a objname this is a file trans check so check those rules */
1847 filename_compute_type(policydb, &newcontext, scontext->type,
1848 tcontext->type, tclass, objname);
1850 /* Check for class-specific changes. */
1851 if (specified & AVTAB_TRANSITION) {
1852 /* Look for a role transition rule. */
1853 struct role_trans_datum *rtd;
1854 struct role_trans_key rtk = {
1855 .role = scontext->role,
1856 .type = tcontext->type,
1860 rtd = policydb_roletr_search(policydb, &rtk);
1862 newcontext.role = rtd->new_role;
1865 /* Set the MLS attributes.
1866 This is done last because it may allocate memory. */
1867 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified,
1872 /* Check the validity of the context. */
1873 if (!policydb_context_isvalid(policydb, &newcontext)) {
1874 rc = compute_sid_handle_invalid_context(state, policy, sentry,
1880 /* Obtain the sid for the context. */
1881 rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
1884 context_destroy(&newcontext);
1890 * security_transition_sid - Compute the SID for a new subject/object.
1891 * @ssid: source security identifier
1892 * @tsid: target security identifier
1893 * @tclass: target security class
1894 * @out_sid: security identifier for new subject/object
1896 * Compute a SID to use for labeling a new subject or object in the
1897 * class @tclass based on a SID pair (@ssid, @tsid).
1898 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1899 * if insufficient memory is available, or %0 if the new SID was
1900 * computed successfully.
1902 int security_transition_sid(struct selinux_state *state,
1903 u32 ssid, u32 tsid, u16 tclass,
1904 const struct qstr *qstr, u32 *out_sid)
1906 return security_compute_sid(state, ssid, tsid, tclass,
1908 qstr ? qstr->name : NULL, out_sid, true);
1911 int security_transition_sid_user(struct selinux_state *state,
1912 u32 ssid, u32 tsid, u16 tclass,
1913 const char *objname, u32 *out_sid)
1915 return security_compute_sid(state, ssid, tsid, tclass,
1917 objname, out_sid, false);
1921 * security_member_sid - Compute the SID for member selection.
1922 * @ssid: source security identifier
1923 * @tsid: target security identifier
1924 * @tclass: target security class
1925 * @out_sid: security identifier for selected member
1927 * Compute a SID to use when selecting a member of a polyinstantiated
1928 * object of class @tclass based on a SID pair (@ssid, @tsid).
1929 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1930 * if insufficient memory is available, or %0 if the SID was
1931 * computed successfully.
1933 int security_member_sid(struct selinux_state *state,
1939 return security_compute_sid(state, ssid, tsid, tclass,
1945 * security_change_sid - Compute the SID for object relabeling.
1946 * @ssid: source security identifier
1947 * @tsid: target security identifier
1948 * @tclass: target security class
1949 * @out_sid: security identifier for selected member
1951 * Compute a SID to use for relabeling an object of class @tclass
1952 * based on a SID pair (@ssid, @tsid).
1953 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1954 * if insufficient memory is available, or %0 if the SID was
1955 * computed successfully.
1957 int security_change_sid(struct selinux_state *state,
1963 return security_compute_sid(state,
1964 ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1968 static inline int convert_context_handle_invalid_context(
1969 struct selinux_state *state,
1970 struct policydb *policydb,
1971 struct context *context)
1976 if (enforcing_enabled(state))
1979 if (!context_struct_to_string(policydb, context, &s, &len)) {
1980 pr_warn("SELinux: Context %s would be invalid if enforcing\n",
1988 * Convert the values in the security context
1989 * structure `oldc' from the values specified
1990 * in the policy `p->oldp' to the values specified
1991 * in the policy `p->newp', storing the new context
1992 * in `newc'. Verify that the context is valid
1993 * under the new policy.
1995 static int convert_context(struct context *oldc, struct context *newc, void *p)
1997 struct convert_context_args *args;
1998 struct ocontext *oc;
1999 struct role_datum *role;
2000 struct type_datum *typdatum;
2001 struct user_datum *usrdatum;
2009 s = kstrdup(oldc->str, GFP_KERNEL);
2013 rc = string_to_context_struct(args->newp, NULL, s,
2015 if (rc == -EINVAL) {
2017 * Retain string representation for later mapping.
2019 * IMPORTANT: We need to copy the contents of oldc->str
2020 * back into s again because string_to_context_struct()
2021 * may have garbled it.
2023 memcpy(s, oldc->str, oldc->len);
2026 newc->len = oldc->len;
2031 /* Other error condition, e.g. ENOMEM. */
2032 pr_err("SELinux: Unable to map context %s, rc = %d.\n",
2036 pr_info("SELinux: Context %s became valid (mapped).\n",
2043 /* Convert the user. */
2045 usrdatum = symtab_search(&args->newp->p_users,
2046 sym_name(args->oldp,
2047 SYM_USERS, oldc->user - 1));
2050 newc->user = usrdatum->value;
2052 /* Convert the role. */
2054 role = symtab_search(&args->newp->p_roles,
2055 sym_name(args->oldp, SYM_ROLES, oldc->role - 1));
2058 newc->role = role->value;
2060 /* Convert the type. */
2062 typdatum = symtab_search(&args->newp->p_types,
2063 sym_name(args->oldp,
2064 SYM_TYPES, oldc->type - 1));
2067 newc->type = typdatum->value;
2069 /* Convert the MLS fields if dealing with MLS policies */
2070 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
2071 rc = mls_convert_context(args->oldp, args->newp, oldc, newc);
2074 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
2076 * Switching between non-MLS and MLS policy:
2077 * ensure that the MLS fields of the context for all
2078 * existing entries in the sidtab are filled in with a
2079 * suitable default value, likely taken from one of the
2082 oc = args->newp->ocontexts[OCON_ISID];
2083 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
2087 pr_err("SELinux: unable to look up"
2088 " the initial SIDs list\n");
2091 rc = mls_range_set(newc, &oc->context[0].range);
2096 /* Check the validity of the new context. */
2097 if (!policydb_context_isvalid(args->newp, newc)) {
2098 rc = convert_context_handle_invalid_context(args->state,
2107 /* Map old representation to string and save it. */
2108 rc = context_struct_to_string(args->oldp, oldc, &s, &len);
2111 context_destroy(newc);
2114 pr_info("SELinux: Context %s became invalid (unmapped).\n",
2119 static void security_load_policycaps(struct selinux_state *state,
2120 struct selinux_policy *policy)
2124 struct ebitmap_node *node;
2126 p = &policy->policydb;
2128 for (i = 0; i < ARRAY_SIZE(state->policycap); i++)
2129 WRITE_ONCE(state->policycap[i],
2130 ebitmap_get_bit(&p->policycaps, i));
2132 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2133 pr_info("SELinux: policy capability %s=%d\n",
2134 selinux_policycap_names[i],
2135 ebitmap_get_bit(&p->policycaps, i));
2137 ebitmap_for_each_positive_bit(&p->policycaps, node, i) {
2138 if (i >= ARRAY_SIZE(selinux_policycap_names))
2139 pr_info("SELinux: unknown policy capability %u\n",
2144 static int security_preserve_bools(struct selinux_policy *oldpolicy,
2145 struct selinux_policy *newpolicy);
2147 static void selinux_policy_free(struct selinux_policy *policy)
2152 sidtab_destroy(policy->sidtab);
2153 kfree(policy->map.mapping);
2154 policydb_destroy(&policy->policydb);
2155 kfree(policy->sidtab);
2159 static void selinux_policy_cond_free(struct selinux_policy *policy)
2161 cond_policydb_destroy_dup(&policy->policydb);
2165 void selinux_policy_cancel(struct selinux_state *state,
2166 struct selinux_load_state *load_state)
2168 struct selinux_policy *oldpolicy;
2170 oldpolicy = rcu_dereference_protected(state->policy,
2171 lockdep_is_held(&state->policy_mutex));
2173 sidtab_cancel_convert(oldpolicy->sidtab);
2174 selinux_policy_free(load_state->policy);
2175 kfree(load_state->convert_data);
2178 static void selinux_notify_policy_change(struct selinux_state *state,
2181 /* Flush external caches and notify userspace of policy load */
2182 avc_ss_reset(state->avc, seqno);
2183 selnl_notify_policyload(seqno);
2184 selinux_status_update_policyload(state, seqno);
2185 selinux_netlbl_cache_invalidate();
2186 selinux_xfrm_notify_policyload();
2189 void selinux_policy_commit(struct selinux_state *state,
2190 struct selinux_load_state *load_state)
2192 struct selinux_policy *oldpolicy, *newpolicy = load_state->policy;
2195 oldpolicy = rcu_dereference_protected(state->policy,
2196 lockdep_is_held(&state->policy_mutex));
2198 /* If switching between different policy types, log MLS status */
2200 if (oldpolicy->policydb.mls_enabled && !newpolicy->policydb.mls_enabled)
2201 pr_info("SELinux: Disabling MLS support...\n");
2202 else if (!oldpolicy->policydb.mls_enabled && newpolicy->policydb.mls_enabled)
2203 pr_info("SELinux: Enabling MLS support...\n");
2206 /* Set latest granting seqno for new policy. */
2208 newpolicy->latest_granting = oldpolicy->latest_granting + 1;
2210 newpolicy->latest_granting = 1;
2211 seqno = newpolicy->latest_granting;
2213 /* Install the new policy. */
2214 rcu_assign_pointer(state->policy, newpolicy);
2216 /* Load the policycaps from the new policy */
2217 security_load_policycaps(state, newpolicy);
2219 if (!selinux_initialized(state)) {
2221 * After first policy load, the security server is
2222 * marked as initialized and ready to handle requests and
2223 * any objects created prior to policy load are then labeled.
2225 selinux_mark_initialized(state);
2226 selinux_complete_init();
2229 /* Free the old policy */
2231 selinux_policy_free(oldpolicy);
2232 kfree(load_state->convert_data);
2234 /* Notify others of the policy change */
2235 selinux_notify_policy_change(state, seqno);
2239 * security_load_policy - Load a security policy configuration.
2240 * @data: binary policy data
2241 * @len: length of data in bytes
2243 * Load a new set of security policy configuration data,
2244 * validate it and convert the SID table as necessary.
2245 * This function will flush the access vector cache after
2246 * loading the new policy.
2248 int security_load_policy(struct selinux_state *state, void *data, size_t len,
2249 struct selinux_load_state *load_state)
2251 struct selinux_policy *newpolicy, *oldpolicy;
2252 struct selinux_policy_convert_data *convert_data;
2254 struct policy_file file = { data, len }, *fp = &file;
2256 newpolicy = kzalloc(sizeof(*newpolicy), GFP_KERNEL);
2260 newpolicy->sidtab = kzalloc(sizeof(*newpolicy->sidtab), GFP_KERNEL);
2261 if (!newpolicy->sidtab) {
2266 rc = policydb_read(&newpolicy->policydb, fp);
2270 newpolicy->policydb.len = len;
2271 rc = selinux_set_mapping(&newpolicy->policydb, secclass_map,
2276 rc = policydb_load_isids(&newpolicy->policydb, newpolicy->sidtab);
2278 pr_err("SELinux: unable to load the initial SIDs\n");
2282 if (!selinux_initialized(state)) {
2283 /* First policy load, so no need to preserve state from old policy */
2284 load_state->policy = newpolicy;
2285 load_state->convert_data = NULL;
2289 oldpolicy = rcu_dereference_protected(state->policy,
2290 lockdep_is_held(&state->policy_mutex));
2292 /* Preserve active boolean values from the old policy */
2293 rc = security_preserve_bools(oldpolicy, newpolicy);
2295 pr_err("SELinux: unable to preserve booleans\n");
2296 goto err_free_isids;
2299 convert_data = kmalloc(sizeof(*convert_data), GFP_KERNEL);
2300 if (!convert_data) {
2302 goto err_free_isids;
2306 * Convert the internal representations of contexts
2307 * in the new SID table.
2309 convert_data->args.state = state;
2310 convert_data->args.oldp = &oldpolicy->policydb;
2311 convert_data->args.newp = &newpolicy->policydb;
2313 convert_data->sidtab_params.func = convert_context;
2314 convert_data->sidtab_params.args = &convert_data->args;
2315 convert_data->sidtab_params.target = newpolicy->sidtab;
2317 rc = sidtab_convert(oldpolicy->sidtab, &convert_data->sidtab_params);
2319 pr_err("SELinux: unable to convert the internal"
2320 " representation of contexts in the new SID"
2322 goto err_free_convert_data;
2325 load_state->policy = newpolicy;
2326 load_state->convert_data = convert_data;
2329 err_free_convert_data:
2330 kfree(convert_data);
2332 sidtab_destroy(newpolicy->sidtab);
2334 kfree(newpolicy->map.mapping);
2336 policydb_destroy(&newpolicy->policydb);
2338 kfree(newpolicy->sidtab);
2346 * security_port_sid - Obtain the SID for a port.
2347 * @protocol: protocol number
2348 * @port: port number
2349 * @out_sid: security identifier
2351 int security_port_sid(struct selinux_state *state,
2352 u8 protocol, u16 port, u32 *out_sid)
2354 struct selinux_policy *policy;
2355 struct policydb *policydb;
2356 struct sidtab *sidtab;
2360 if (!selinux_initialized(state)) {
2361 *out_sid = SECINITSID_PORT;
2366 policy = rcu_dereference(state->policy);
2367 policydb = &policy->policydb;
2368 sidtab = policy->sidtab;
2370 c = policydb->ocontexts[OCON_PORT];
2372 if (c->u.port.protocol == protocol &&
2373 c->u.port.low_port <= port &&
2374 c->u.port.high_port >= port)
2381 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2386 *out_sid = c->sid[0];
2388 *out_sid = SECINITSID_PORT;
2397 * security_pkey_sid - Obtain the SID for a pkey.
2398 * @subnet_prefix: Subnet Prefix
2399 * @pkey_num: pkey number
2400 * @out_sid: security identifier
2402 int security_ib_pkey_sid(struct selinux_state *state,
2403 u64 subnet_prefix, u16 pkey_num, u32 *out_sid)
2405 struct selinux_policy *policy;
2406 struct policydb *policydb;
2407 struct sidtab *sidtab;
2411 if (!selinux_initialized(state)) {
2412 *out_sid = SECINITSID_UNLABELED;
2417 policy = rcu_dereference(state->policy);
2418 policydb = &policy->policydb;
2419 sidtab = policy->sidtab;
2421 c = policydb->ocontexts[OCON_IBPKEY];
2423 if (c->u.ibpkey.low_pkey <= pkey_num &&
2424 c->u.ibpkey.high_pkey >= pkey_num &&
2425 c->u.ibpkey.subnet_prefix == subnet_prefix)
2433 rc = sidtab_context_to_sid(sidtab,
2439 *out_sid = c->sid[0];
2441 *out_sid = SECINITSID_UNLABELED;
2449 * security_ib_endport_sid - Obtain the SID for a subnet management interface.
2450 * @dev_name: device name
2451 * @port: port number
2452 * @out_sid: security identifier
2454 int security_ib_endport_sid(struct selinux_state *state,
2455 const char *dev_name, u8 port_num, u32 *out_sid)
2457 struct selinux_policy *policy;
2458 struct policydb *policydb;
2459 struct sidtab *sidtab;
2463 if (!selinux_initialized(state)) {
2464 *out_sid = SECINITSID_UNLABELED;
2469 policy = rcu_dereference(state->policy);
2470 policydb = &policy->policydb;
2471 sidtab = policy->sidtab;
2473 c = policydb->ocontexts[OCON_IBENDPORT];
2475 if (c->u.ibendport.port == port_num &&
2476 !strncmp(c->u.ibendport.dev_name,
2478 IB_DEVICE_NAME_MAX))
2486 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2491 *out_sid = c->sid[0];
2493 *out_sid = SECINITSID_UNLABELED;
2501 * security_netif_sid - Obtain the SID for a network interface.
2502 * @name: interface name
2503 * @if_sid: interface SID
2505 int security_netif_sid(struct selinux_state *state,
2506 char *name, u32 *if_sid)
2508 struct selinux_policy *policy;
2509 struct policydb *policydb;
2510 struct sidtab *sidtab;
2514 if (!selinux_initialized(state)) {
2515 *if_sid = SECINITSID_NETIF;
2520 policy = rcu_dereference(state->policy);
2521 policydb = &policy->policydb;
2522 sidtab = policy->sidtab;
2524 c = policydb->ocontexts[OCON_NETIF];
2526 if (strcmp(name, c->u.name) == 0)
2532 if (!c->sid[0] || !c->sid[1]) {
2533 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2537 rc = sidtab_context_to_sid(sidtab, &c->context[1],
2542 *if_sid = c->sid[0];
2544 *if_sid = SECINITSID_NETIF;
2551 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2555 for (i = 0; i < 4; i++)
2556 if (addr[i] != (input[i] & mask[i])) {
2565 * security_node_sid - Obtain the SID for a node (host).
2566 * @domain: communication domain aka address family
2568 * @addrlen: address length in bytes
2569 * @out_sid: security identifier
2571 int security_node_sid(struct selinux_state *state,
2577 struct selinux_policy *policy;
2578 struct policydb *policydb;
2579 struct sidtab *sidtab;
2583 if (!selinux_initialized(state)) {
2584 *out_sid = SECINITSID_NODE;
2589 policy = rcu_dereference(state->policy);
2590 policydb = &policy->policydb;
2591 sidtab = policy->sidtab;
2598 if (addrlen != sizeof(u32))
2601 addr = *((u32 *)addrp);
2603 c = policydb->ocontexts[OCON_NODE];
2605 if (c->u.node.addr == (addr & c->u.node.mask))
2614 if (addrlen != sizeof(u64) * 2)
2616 c = policydb->ocontexts[OCON_NODE6];
2618 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2627 *out_sid = SECINITSID_NODE;
2633 rc = sidtab_context_to_sid(sidtab,
2639 *out_sid = c->sid[0];
2641 *out_sid = SECINITSID_NODE;
2653 * security_get_user_sids - Obtain reachable SIDs for a user.
2654 * @fromsid: starting SID
2655 * @username: username
2656 * @sids: array of reachable SIDs for user
2657 * @nel: number of elements in @sids
2659 * Generate the set of SIDs for legal security contexts
2660 * for a given user that can be reached by @fromsid.
2661 * Set *@sids to point to a dynamically allocated
2662 * array containing the set of SIDs. Set *@nel to the
2663 * number of elements in the array.
2666 int security_get_user_sids(struct selinux_state *state,
2672 struct selinux_policy *policy;
2673 struct policydb *policydb;
2674 struct sidtab *sidtab;
2675 struct context *fromcon, usercon;
2676 u32 *mysids = NULL, *mysids2, sid;
2677 u32 mynel = 0, maxnel = SIDS_NEL;
2678 struct user_datum *user;
2679 struct role_datum *role;
2680 struct ebitmap_node *rnode, *tnode;
2686 if (!selinux_initialized(state))
2690 policy = rcu_dereference(state->policy);
2691 policydb = &policy->policydb;
2692 sidtab = policy->sidtab;
2694 context_init(&usercon);
2697 fromcon = sidtab_search(sidtab, fromsid);
2702 user = symtab_search(&policydb->p_users, username);
2706 usercon.user = user->value;
2709 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
2713 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2714 role = policydb->role_val_to_struct[i];
2715 usercon.role = i + 1;
2716 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2717 usercon.type = j + 1;
2719 if (mls_setup_user_range(policydb, fromcon, user,
2723 rc = sidtab_context_to_sid(sidtab, &usercon, &sid);
2726 if (mynel < maxnel) {
2727 mysids[mynel++] = sid;
2731 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2734 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2737 mysids[mynel++] = sid;
2750 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2755 for (i = 0, j = 0; i < mynel; i++) {
2756 struct av_decision dummy_avd;
2757 rc = avc_has_perm_noaudit(state,
2759 SECCLASS_PROCESS, /* kernel value */
2760 PROCESS__TRANSITION, AVC_STRICT,
2763 mysids2[j++] = mysids[i];
2775 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
2776 * @fstype: filesystem type
2777 * @path: path from root of mount
2778 * @sclass: file security class
2779 * @sid: SID for path
2781 * Obtain a SID to use for a file in a filesystem that
2782 * cannot support xattr or use a fixed labeling behavior like
2783 * transition SIDs or task SIDs.
2785 static inline int __security_genfs_sid(struct selinux_policy *policy,
2791 struct policydb *policydb = &policy->policydb;
2792 struct sidtab *sidtab = policy->sidtab;
2795 struct genfs *genfs;
2799 while (path[0] == '/' && path[1] == '/')
2802 sclass = unmap_class(&policy->map, orig_sclass);
2803 *sid = SECINITSID_UNLABELED;
2805 for (genfs = policydb->genfs; genfs; genfs = genfs->next) {
2806 cmp = strcmp(fstype, genfs->fstype);
2815 for (c = genfs->head; c; c = c->next) {
2816 len = strlen(c->u.name);
2817 if ((!c->v.sclass || sclass == c->v.sclass) &&
2818 (strncmp(c->u.name, path, len) == 0))
2827 rc = sidtab_context_to_sid(sidtab, &c->context[0], &c->sid[0]);
2839 * security_genfs_sid - Obtain a SID for a file in a filesystem
2840 * @fstype: filesystem type
2841 * @path: path from root of mount
2842 * @sclass: file security class
2843 * @sid: SID for path
2845 * Acquire policy_rwlock before calling __security_genfs_sid() and release
2848 int security_genfs_sid(struct selinux_state *state,
2854 struct selinux_policy *policy;
2857 if (!selinux_initialized(state)) {
2858 *sid = SECINITSID_UNLABELED;
2863 policy = rcu_dereference(state->policy);
2864 retval = __security_genfs_sid(policy,
2865 fstype, path, orig_sclass, sid);
2870 int selinux_policy_genfs_sid(struct selinux_policy *policy,
2876 /* no lock required, policy is not yet accessible by other threads */
2877 return __security_genfs_sid(policy, fstype, path, orig_sclass, sid);
2881 * security_fs_use - Determine how to handle labeling for a filesystem.
2882 * @sb: superblock in question
2884 int security_fs_use(struct selinux_state *state, struct super_block *sb)
2886 struct selinux_policy *policy;
2887 struct policydb *policydb;
2888 struct sidtab *sidtab;
2891 struct superblock_security_struct *sbsec = sb->s_security;
2892 const char *fstype = sb->s_type->name;
2894 if (!selinux_initialized(state)) {
2895 sbsec->behavior = SECURITY_FS_USE_NONE;
2896 sbsec->sid = SECINITSID_UNLABELED;
2901 policy = rcu_dereference(state->policy);
2902 policydb = &policy->policydb;
2903 sidtab = policy->sidtab;
2905 c = policydb->ocontexts[OCON_FSUSE];
2907 if (strcmp(fstype, c->u.name) == 0)
2913 sbsec->behavior = c->v.behavior;
2915 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2920 sbsec->sid = c->sid[0];
2922 rc = __security_genfs_sid(policy, fstype, "/",
2923 SECCLASS_DIR, &sbsec->sid);
2925 sbsec->behavior = SECURITY_FS_USE_NONE;
2928 sbsec->behavior = SECURITY_FS_USE_GENFS;
2937 int security_get_bools(struct selinux_policy *policy,
2938 u32 *len, char ***names, int **values)
2940 struct policydb *policydb;
2944 policydb = &policy->policydb;
2950 *len = policydb->p_bools.nprim;
2955 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
2960 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2964 for (i = 0; i < *len; i++) {
2965 (*values)[i] = policydb->bool_val_to_struct[i]->state;
2968 (*names)[i] = kstrdup(sym_name(policydb, SYM_BOOLS, i),
2978 for (i = 0; i < *len; i++)
2990 int security_set_bools(struct selinux_state *state, u32 len, int *values)
2992 struct selinux_policy *newpolicy, *oldpolicy;
2996 if (!selinux_initialized(state))
2999 oldpolicy = rcu_dereference_protected(state->policy,
3000 lockdep_is_held(&state->policy_mutex));
3002 /* Consistency check on number of booleans, should never fail */
3003 if (WARN_ON(len != oldpolicy->policydb.p_bools.nprim))
3006 newpolicy = kmemdup(oldpolicy, sizeof(*newpolicy), GFP_KERNEL);
3011 * Deep copy only the parts of the policydb that might be
3012 * modified as a result of changing booleans.
3014 rc = cond_policydb_dup(&newpolicy->policydb, &oldpolicy->policydb);
3020 /* Update the boolean states in the copy */
3021 for (i = 0; i < len; i++) {
3022 int new_state = !!values[i];
3023 int old_state = newpolicy->policydb.bool_val_to_struct[i]->state;
3025 if (new_state != old_state) {
3026 audit_log(audit_context(), GFP_ATOMIC,
3027 AUDIT_MAC_CONFIG_CHANGE,
3028 "bool=%s val=%d old_val=%d auid=%u ses=%u",
3029 sym_name(&newpolicy->policydb, SYM_BOOLS, i),
3032 from_kuid(&init_user_ns, audit_get_loginuid(current)),
3033 audit_get_sessionid(current));
3034 newpolicy->policydb.bool_val_to_struct[i]->state = new_state;
3038 /* Re-evaluate the conditional rules in the copy */
3039 evaluate_cond_nodes(&newpolicy->policydb);
3041 /* Set latest granting seqno for new policy */
3042 newpolicy->latest_granting = oldpolicy->latest_granting + 1;
3043 seqno = newpolicy->latest_granting;
3045 /* Install the new policy */
3046 rcu_assign_pointer(state->policy, newpolicy);
3049 * Free the conditional portions of the old policydb
3050 * that were copied for the new policy, and the oldpolicy
3051 * structure itself but not what it references.
3054 selinux_policy_cond_free(oldpolicy);
3056 /* Notify others of the policy change */
3057 selinux_notify_policy_change(state, seqno);
3061 int security_get_bool_value(struct selinux_state *state,
3064 struct selinux_policy *policy;
3065 struct policydb *policydb;
3069 if (!selinux_initialized(state))
3073 policy = rcu_dereference(state->policy);
3074 policydb = &policy->policydb;
3077 len = policydb->p_bools.nprim;
3081 rc = policydb->bool_val_to_struct[index]->state;
3087 static int security_preserve_bools(struct selinux_policy *oldpolicy,
3088 struct selinux_policy *newpolicy)
3090 int rc, *bvalues = NULL;
3091 char **bnames = NULL;
3092 struct cond_bool_datum *booldatum;
3095 rc = security_get_bools(oldpolicy, &nbools, &bnames, &bvalues);
3098 for (i = 0; i < nbools; i++) {
3099 booldatum = symtab_search(&newpolicy->policydb.p_bools,
3102 booldatum->state = bvalues[i];
3104 evaluate_cond_nodes(&newpolicy->policydb);
3108 for (i = 0; i < nbools; i++)
3117 * security_sid_mls_copy() - computes a new sid based on the given
3118 * sid and the mls portion of mls_sid.
3120 int security_sid_mls_copy(struct selinux_state *state,
3121 u32 sid, u32 mls_sid, u32 *new_sid)
3123 struct selinux_policy *policy;
3124 struct policydb *policydb;
3125 struct sidtab *sidtab;
3126 struct context *context1;
3127 struct context *context2;
3128 struct context newcon;
3134 if (!selinux_initialized(state)) {
3139 context_init(&newcon);
3142 policy = rcu_dereference(state->policy);
3143 policydb = &policy->policydb;
3144 sidtab = policy->sidtab;
3146 if (!policydb->mls_enabled) {
3152 context1 = sidtab_search(sidtab, sid);
3154 pr_err("SELinux: %s: unrecognized SID %d\n",
3160 context2 = sidtab_search(sidtab, mls_sid);
3162 pr_err("SELinux: %s: unrecognized SID %d\n",
3167 newcon.user = context1->user;
3168 newcon.role = context1->role;
3169 newcon.type = context1->type;
3170 rc = mls_context_cpy(&newcon, context2);
3174 /* Check the validity of the new context. */
3175 if (!policydb_context_isvalid(policydb, &newcon)) {
3176 rc = convert_context_handle_invalid_context(state, policydb,
3179 if (!context_struct_to_string(policydb, &newcon, &s,
3181 struct audit_buffer *ab;
3183 ab = audit_log_start(audit_context(),
3186 audit_log_format(ab,
3187 "op=security_sid_mls_copy invalid_context=");
3188 /* don't record NUL with untrusted strings */
3189 audit_log_n_untrustedstring(ab, s, len - 1);
3196 rc = sidtab_context_to_sid(sidtab, &newcon, new_sid);
3199 context_destroy(&newcon);
3205 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
3206 * @nlbl_sid: NetLabel SID
3207 * @nlbl_type: NetLabel labeling protocol type
3208 * @xfrm_sid: XFRM SID
3211 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
3212 * resolved into a single SID it is returned via @peer_sid and the function
3213 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
3214 * returns a negative value. A table summarizing the behavior is below:
3216 * | function return | @sid
3217 * ------------------------------+-----------------+-----------------
3218 * no peer labels | 0 | SECSID_NULL
3219 * single peer label | 0 | <peer_label>
3220 * multiple, consistent labels | 0 | <peer_label>
3221 * multiple, inconsistent labels | -<errno> | SECSID_NULL
3224 int security_net_peersid_resolve(struct selinux_state *state,
3225 u32 nlbl_sid, u32 nlbl_type,
3229 struct selinux_policy *policy;
3230 struct policydb *policydb;
3231 struct sidtab *sidtab;
3233 struct context *nlbl_ctx;
3234 struct context *xfrm_ctx;
3236 *peer_sid = SECSID_NULL;
3238 /* handle the common (which also happens to be the set of easy) cases
3239 * right away, these two if statements catch everything involving a
3240 * single or absent peer SID/label */
3241 if (xfrm_sid == SECSID_NULL) {
3242 *peer_sid = nlbl_sid;
3245 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
3246 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
3248 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
3249 *peer_sid = xfrm_sid;
3253 if (!selinux_initialized(state))
3257 policy = rcu_dereference(state->policy);
3258 policydb = &policy->policydb;
3259 sidtab = policy->sidtab;
3262 * We don't need to check initialized here since the only way both
3263 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
3264 * security server was initialized and state->initialized was true.
3266 if (!policydb->mls_enabled) {
3272 nlbl_ctx = sidtab_search(sidtab, nlbl_sid);
3274 pr_err("SELinux: %s: unrecognized SID %d\n",
3275 __func__, nlbl_sid);
3279 xfrm_ctx = sidtab_search(sidtab, xfrm_sid);
3281 pr_err("SELinux: %s: unrecognized SID %d\n",
3282 __func__, xfrm_sid);
3285 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
3289 /* at present NetLabel SIDs/labels really only carry MLS
3290 * information so if the MLS portion of the NetLabel SID
3291 * matches the MLS portion of the labeled XFRM SID/label
3292 * then pass along the XFRM SID as it is the most
3294 *peer_sid = xfrm_sid;
3300 static int get_classes_callback(void *k, void *d, void *args)
3302 struct class_datum *datum = d;
3303 char *name = k, **classes = args;
3304 int value = datum->value - 1;
3306 classes[value] = kstrdup(name, GFP_ATOMIC);
3307 if (!classes[value])
3313 int security_get_classes(struct selinux_policy *policy,
3314 char ***classes, int *nclasses)
3316 struct policydb *policydb;
3319 policydb = &policy->policydb;
3322 *nclasses = policydb->p_classes.nprim;
3323 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
3327 rc = hashtab_map(&policydb->p_classes.table, get_classes_callback,
3331 for (i = 0; i < *nclasses; i++)
3332 kfree((*classes)[i]);
3340 static int get_permissions_callback(void *k, void *d, void *args)
3342 struct perm_datum *datum = d;
3343 char *name = k, **perms = args;
3344 int value = datum->value - 1;
3346 perms[value] = kstrdup(name, GFP_ATOMIC);
3353 int security_get_permissions(struct selinux_policy *policy,
3354 char *class, char ***perms, int *nperms)
3356 struct policydb *policydb;
3358 struct class_datum *match;
3360 policydb = &policy->policydb;
3363 match = symtab_search(&policydb->p_classes, class);
3365 pr_err("SELinux: %s: unrecognized class %s\n",
3371 *nperms = match->permissions.nprim;
3372 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
3376 if (match->comdatum) {
3377 rc = hashtab_map(&match->comdatum->permissions.table,
3378 get_permissions_callback, *perms);
3383 rc = hashtab_map(&match->permissions.table, get_permissions_callback,
3392 for (i = 0; i < *nperms; i++)
3398 int security_get_reject_unknown(struct selinux_state *state)
3400 struct selinux_policy *policy;
3403 if (!selinux_initialized(state))
3407 policy = rcu_dereference(state->policy);
3408 value = policy->policydb.reject_unknown;
3413 int security_get_allow_unknown(struct selinux_state *state)
3415 struct selinux_policy *policy;
3418 if (!selinux_initialized(state))
3422 policy = rcu_dereference(state->policy);
3423 value = policy->policydb.allow_unknown;
3429 * security_policycap_supported - Check for a specific policy capability
3430 * @req_cap: capability
3433 * This function queries the currently loaded policy to see if it supports the
3434 * capability specified by @req_cap. Returns true (1) if the capability is
3435 * supported, false (0) if it isn't supported.
3438 int security_policycap_supported(struct selinux_state *state,
3439 unsigned int req_cap)
3441 struct selinux_policy *policy;
3444 if (!selinux_initialized(state))
3448 policy = rcu_dereference(state->policy);
3449 rc = ebitmap_get_bit(&policy->policydb.policycaps, req_cap);
3455 struct selinux_audit_rule {
3457 struct context au_ctxt;
3460 void selinux_audit_rule_free(void *vrule)
3462 struct selinux_audit_rule *rule = vrule;
3465 context_destroy(&rule->au_ctxt);
3470 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3472 struct selinux_state *state = &selinux_state;
3473 struct selinux_policy *policy;
3474 struct policydb *policydb;
3475 struct selinux_audit_rule *tmprule;
3476 struct role_datum *roledatum;
3477 struct type_datum *typedatum;
3478 struct user_datum *userdatum;
3479 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
3484 if (!selinux_initialized(state))
3488 case AUDIT_SUBJ_USER:
3489 case AUDIT_SUBJ_ROLE:
3490 case AUDIT_SUBJ_TYPE:
3491 case AUDIT_OBJ_USER:
3492 case AUDIT_OBJ_ROLE:
3493 case AUDIT_OBJ_TYPE:
3494 /* only 'equals' and 'not equals' fit user, role, and type */
3495 if (op != Audit_equal && op != Audit_not_equal)
3498 case AUDIT_SUBJ_SEN:
3499 case AUDIT_SUBJ_CLR:
3500 case AUDIT_OBJ_LEV_LOW:
3501 case AUDIT_OBJ_LEV_HIGH:
3502 /* we do not allow a range, indicated by the presence of '-' */
3503 if (strchr(rulestr, '-'))
3507 /* only the above fields are valid */
3511 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3515 context_init(&tmprule->au_ctxt);
3518 policy = rcu_dereference(state->policy);
3519 policydb = &policy->policydb;
3521 tmprule->au_seqno = policy->latest_granting;
3524 case AUDIT_SUBJ_USER:
3525 case AUDIT_OBJ_USER:
3527 userdatum = symtab_search(&policydb->p_users, rulestr);
3530 tmprule->au_ctxt.user = userdatum->value;
3532 case AUDIT_SUBJ_ROLE:
3533 case AUDIT_OBJ_ROLE:
3535 roledatum = symtab_search(&policydb->p_roles, rulestr);
3538 tmprule->au_ctxt.role = roledatum->value;
3540 case AUDIT_SUBJ_TYPE:
3541 case AUDIT_OBJ_TYPE:
3543 typedatum = symtab_search(&policydb->p_types, rulestr);
3546 tmprule->au_ctxt.type = typedatum->value;
3548 case AUDIT_SUBJ_SEN:
3549 case AUDIT_SUBJ_CLR:
3550 case AUDIT_OBJ_LEV_LOW:
3551 case AUDIT_OBJ_LEV_HIGH:
3552 rc = mls_from_string(policydb, rulestr, &tmprule->au_ctxt,
3563 selinux_audit_rule_free(tmprule);
3572 /* Check to see if the rule contains any selinux fields */
3573 int selinux_audit_rule_known(struct audit_krule *rule)
3577 for (i = 0; i < rule->field_count; i++) {
3578 struct audit_field *f = &rule->fields[i];
3580 case AUDIT_SUBJ_USER:
3581 case AUDIT_SUBJ_ROLE:
3582 case AUDIT_SUBJ_TYPE:
3583 case AUDIT_SUBJ_SEN:
3584 case AUDIT_SUBJ_CLR:
3585 case AUDIT_OBJ_USER:
3586 case AUDIT_OBJ_ROLE:
3587 case AUDIT_OBJ_TYPE:
3588 case AUDIT_OBJ_LEV_LOW:
3589 case AUDIT_OBJ_LEV_HIGH:
3597 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule)
3599 struct selinux_state *state = &selinux_state;
3600 struct selinux_policy *policy;
3601 struct context *ctxt;
3602 struct mls_level *level;
3603 struct selinux_audit_rule *rule = vrule;
3606 if (unlikely(!rule)) {
3607 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
3611 if (!selinux_initialized(state))
3616 policy = rcu_dereference(state->policy);
3618 if (rule->au_seqno < policy->latest_granting) {
3623 ctxt = sidtab_search(policy->sidtab, sid);
3624 if (unlikely(!ctxt)) {
3625 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
3631 /* a field/op pair that is not caught here will simply fall through
3634 case AUDIT_SUBJ_USER:
3635 case AUDIT_OBJ_USER:
3638 match = (ctxt->user == rule->au_ctxt.user);
3640 case Audit_not_equal:
3641 match = (ctxt->user != rule->au_ctxt.user);
3645 case AUDIT_SUBJ_ROLE:
3646 case AUDIT_OBJ_ROLE:
3649 match = (ctxt->role == rule->au_ctxt.role);
3651 case Audit_not_equal:
3652 match = (ctxt->role != rule->au_ctxt.role);
3656 case AUDIT_SUBJ_TYPE:
3657 case AUDIT_OBJ_TYPE:
3660 match = (ctxt->type == rule->au_ctxt.type);
3662 case Audit_not_equal:
3663 match = (ctxt->type != rule->au_ctxt.type);
3667 case AUDIT_SUBJ_SEN:
3668 case AUDIT_SUBJ_CLR:
3669 case AUDIT_OBJ_LEV_LOW:
3670 case AUDIT_OBJ_LEV_HIGH:
3671 level = ((field == AUDIT_SUBJ_SEN ||
3672 field == AUDIT_OBJ_LEV_LOW) ?
3673 &ctxt->range.level[0] : &ctxt->range.level[1]);
3676 match = mls_level_eq(&rule->au_ctxt.range.level[0],
3679 case Audit_not_equal:
3680 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
3684 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
3686 !mls_level_eq(&rule->au_ctxt.range.level[0],
3690 match = mls_level_dom(&rule->au_ctxt.range.level[0],
3694 match = (mls_level_dom(level,
3695 &rule->au_ctxt.range.level[0]) &&
3696 !mls_level_eq(level,
3697 &rule->au_ctxt.range.level[0]));
3700 match = mls_level_dom(level,
3701 &rule->au_ctxt.range.level[0]);
3711 static int aurule_avc_callback(u32 event)
3713 if (event == AVC_CALLBACK_RESET)
3714 return audit_update_lsm_rules();
3718 static int __init aurule_init(void)
3722 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
3724 panic("avc_add_callback() failed, error %d\n", err);
3728 __initcall(aurule_init);
3730 #ifdef CONFIG_NETLABEL
3732 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3733 * @secattr: the NetLabel packet security attributes
3734 * @sid: the SELinux SID
3737 * Attempt to cache the context in @ctx, which was derived from the packet in
3738 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3739 * already been initialized.
3742 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
3747 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3748 if (sid_cache == NULL)
3750 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
3751 if (secattr->cache == NULL) {
3757 secattr->cache->free = kfree;
3758 secattr->cache->data = sid_cache;
3759 secattr->flags |= NETLBL_SECATTR_CACHE;
3763 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
3764 * @secattr: the NetLabel packet security attributes
3765 * @sid: the SELinux SID
3768 * Convert the given NetLabel security attributes in @secattr into a
3769 * SELinux SID. If the @secattr field does not contain a full SELinux
3770 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
3771 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3772 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3773 * conversion for future lookups. Returns zero on success, negative values on
3777 int security_netlbl_secattr_to_sid(struct selinux_state *state,
3778 struct netlbl_lsm_secattr *secattr,
3781 struct selinux_policy *policy;
3782 struct policydb *policydb;
3783 struct sidtab *sidtab;
3785 struct context *ctx;
3786 struct context ctx_new;
3788 if (!selinux_initialized(state)) {
3794 policy = rcu_dereference(state->policy);
3795 policydb = &policy->policydb;
3796 sidtab = policy->sidtab;
3798 if (secattr->flags & NETLBL_SECATTR_CACHE)
3799 *sid = *(u32 *)secattr->cache->data;
3800 else if (secattr->flags & NETLBL_SECATTR_SECID)
3801 *sid = secattr->attr.secid;
3802 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3804 ctx = sidtab_search(sidtab, SECINITSID_NETMSG);
3808 context_init(&ctx_new);
3809 ctx_new.user = ctx->user;
3810 ctx_new.role = ctx->role;
3811 ctx_new.type = ctx->type;
3812 mls_import_netlbl_lvl(policydb, &ctx_new, secattr);
3813 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
3814 rc = mls_import_netlbl_cat(policydb, &ctx_new, secattr);
3819 if (!mls_context_isvalid(policydb, &ctx_new))
3822 rc = sidtab_context_to_sid(sidtab, &ctx_new, sid);
3826 security_netlbl_cache_add(secattr, *sid);
3828 ebitmap_destroy(&ctx_new.range.level[0].cat);
3835 ebitmap_destroy(&ctx_new.range.level[0].cat);
3842 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3843 * @sid: the SELinux SID
3844 * @secattr: the NetLabel packet security attributes
3847 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3848 * Returns zero on success, negative values on failure.
3851 int security_netlbl_sid_to_secattr(struct selinux_state *state,
3852 u32 sid, struct netlbl_lsm_secattr *secattr)
3854 struct selinux_policy *policy;
3855 struct policydb *policydb;
3857 struct context *ctx;
3859 if (!selinux_initialized(state))
3863 policy = rcu_dereference(state->policy);
3864 policydb = &policy->policydb;
3867 ctx = sidtab_search(policy->sidtab, sid);
3872 secattr->domain = kstrdup(sym_name(policydb, SYM_TYPES, ctx->type - 1),
3874 if (secattr->domain == NULL)
3877 secattr->attr.secid = sid;
3878 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3879 mls_export_netlbl_lvl(policydb, ctx, secattr);
3880 rc = mls_export_netlbl_cat(policydb, ctx, secattr);
3885 #endif /* CONFIG_NETLABEL */
3888 * security_read_policy - read the policy.
3889 * @data: binary policy data
3890 * @len: length of data in bytes
3893 int security_read_policy(struct selinux_state *state,
3894 void **data, size_t *len)
3896 struct selinux_policy *policy;
3898 struct policy_file fp;
3900 policy = rcu_dereference_protected(
3901 state->policy, lockdep_is_held(&state->policy_mutex));
3905 *len = policy->policydb.len;
3906 *data = vmalloc_user(*len);
3913 rc = policydb_write(&policy->policydb, &fp);
3917 *len = (unsigned long)fp.data - (unsigned long)*data;