2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@tycho.nsa.gov>
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@paul-moore.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 * Updated: Guido Trentalancia <guido@trentalancia.com>
31 * Added support for runtime switching of the policy type
33 * Copyright (C) 2008, 2009 NEC Corporation
34 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
35 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
36 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
37 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, version 2.
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/spinlock.h>
46 #include <linux/rcupdate.h>
47 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/audit.h>
51 #include <linux/mutex.h>
52 #include <linux/flex_array.h>
53 #include <linux/vmalloc.h>
54 #include <net/netlabel.h>
64 #include "conditional.h"
72 /* Policy capability names */
73 const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
74 "network_peer_controls",
76 "extended_socket_class",
77 "always_check_network",
79 "nnp_nosuid_transition"
82 static struct selinux_ss selinux_ss;
84 void selinux_ss_init(struct selinux_ss **ss)
86 rwlock_init(&selinux_ss.policy_rwlock);
87 mutex_init(&selinux_ss.status_lock);
91 /* Forward declaration. */
92 static int context_struct_to_string(struct policydb *policydb,
93 struct context *context,
97 static void context_struct_compute_av(struct policydb *policydb,
98 struct context *scontext,
99 struct context *tcontext,
101 struct av_decision *avd,
102 struct extended_perms *xperms);
104 static int selinux_set_mapping(struct policydb *pol,
105 struct security_class_mapping *map,
106 struct selinux_map *out_map)
110 bool print_unknown_handle = false;
112 /* Find number of classes in the input mapping */
119 /* Allocate space for the class records, plus one for class zero */
120 out_map->mapping = kcalloc(++i, sizeof(*out_map->mapping), GFP_ATOMIC);
121 if (!out_map->mapping)
124 /* Store the raw class and permission values */
126 while (map[j].name) {
127 struct security_class_mapping *p_in = map + (j++);
128 struct selinux_mapping *p_out = out_map->mapping + j;
130 /* An empty class string skips ahead */
131 if (!strcmp(p_in->name, "")) {
132 p_out->num_perms = 0;
136 p_out->value = string_to_security_class(pol, p_in->name);
138 pr_info("SELinux: Class %s not defined in policy.\n",
140 if (pol->reject_unknown)
142 p_out->num_perms = 0;
143 print_unknown_handle = true;
148 while (p_in->perms[k]) {
149 /* An empty permission string skips ahead */
150 if (!*p_in->perms[k]) {
154 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
156 if (!p_out->perms[k]) {
157 pr_info("SELinux: Permission %s in class %s not defined in policy.\n",
158 p_in->perms[k], p_in->name);
159 if (pol->reject_unknown)
161 print_unknown_handle = true;
166 p_out->num_perms = k;
169 if (print_unknown_handle)
170 pr_info("SELinux: the above unknown classes and permissions will be %s\n",
171 pol->allow_unknown ? "allowed" : "denied");
176 kfree(out_map->mapping);
177 out_map->mapping = NULL;
182 * Get real, policy values from mapped values
185 static u16 unmap_class(struct selinux_map *map, u16 tclass)
187 if (tclass < map->size)
188 return map->mapping[tclass].value;
194 * Get kernel value for class from its policy value
196 static u16 map_class(struct selinux_map *map, u16 pol_value)
200 for (i = 1; i < map->size; i++) {
201 if (map->mapping[i].value == pol_value)
205 return SECCLASS_NULL;
208 static void map_decision(struct selinux_map *map,
209 u16 tclass, struct av_decision *avd,
212 if (tclass < map->size) {
213 struct selinux_mapping *mapping = &map->mapping[tclass];
214 unsigned int i, n = mapping->num_perms;
217 for (i = 0, result = 0; i < n; i++) {
218 if (avd->allowed & mapping->perms[i])
220 if (allow_unknown && !mapping->perms[i])
223 avd->allowed = result;
225 for (i = 0, result = 0; i < n; i++)
226 if (avd->auditallow & mapping->perms[i])
228 avd->auditallow = result;
230 for (i = 0, result = 0; i < n; i++) {
231 if (avd->auditdeny & mapping->perms[i])
233 if (!allow_unknown && !mapping->perms[i])
237 * In case the kernel has a bug and requests a permission
238 * between num_perms and the maximum permission number, we
239 * should audit that denial
241 for (; i < (sizeof(u32)*8); i++)
243 avd->auditdeny = result;
247 int security_mls_enabled(struct selinux_state *state)
249 struct policydb *p = &state->ss->policydb;
251 return p->mls_enabled;
255 * Return the boolean value of a constraint expression
256 * when it is applied to the specified source and target
259 * xcontext is a special beast... It is used by the validatetrans rules
260 * only. For these rules, scontext is the context before the transition,
261 * tcontext is the context after the transition, and xcontext is the context
262 * of the process performing the transition. All other callers of
263 * constraint_expr_eval should pass in NULL for xcontext.
265 static int constraint_expr_eval(struct policydb *policydb,
266 struct context *scontext,
267 struct context *tcontext,
268 struct context *xcontext,
269 struct constraint_expr *cexpr)
273 struct role_datum *r1, *r2;
274 struct mls_level *l1, *l2;
275 struct constraint_expr *e;
276 int s[CEXPR_MAXDEPTH];
279 for (e = cexpr; e; e = e->next) {
280 switch (e->expr_type) {
296 if (sp == (CEXPR_MAXDEPTH - 1))
300 val1 = scontext->user;
301 val2 = tcontext->user;
304 val1 = scontext->type;
305 val2 = tcontext->type;
308 val1 = scontext->role;
309 val2 = tcontext->role;
310 r1 = policydb->role_val_to_struct[val1 - 1];
311 r2 = policydb->role_val_to_struct[val2 - 1];
314 s[++sp] = ebitmap_get_bit(&r1->dominates,
318 s[++sp] = ebitmap_get_bit(&r2->dominates,
322 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
324 !ebitmap_get_bit(&r2->dominates,
332 l1 = &(scontext->range.level[0]);
333 l2 = &(tcontext->range.level[0]);
336 l1 = &(scontext->range.level[0]);
337 l2 = &(tcontext->range.level[1]);
340 l1 = &(scontext->range.level[1]);
341 l2 = &(tcontext->range.level[0]);
344 l1 = &(scontext->range.level[1]);
345 l2 = &(tcontext->range.level[1]);
348 l1 = &(scontext->range.level[0]);
349 l2 = &(scontext->range.level[1]);
352 l1 = &(tcontext->range.level[0]);
353 l2 = &(tcontext->range.level[1]);
358 s[++sp] = mls_level_eq(l1, l2);
361 s[++sp] = !mls_level_eq(l1, l2);
364 s[++sp] = mls_level_dom(l1, l2);
367 s[++sp] = mls_level_dom(l2, l1);
370 s[++sp] = mls_level_incomp(l2, l1);
384 s[++sp] = (val1 == val2);
387 s[++sp] = (val1 != val2);
395 if (sp == (CEXPR_MAXDEPTH-1))
398 if (e->attr & CEXPR_TARGET)
400 else if (e->attr & CEXPR_XTARGET) {
407 if (e->attr & CEXPR_USER)
409 else if (e->attr & CEXPR_ROLE)
411 else if (e->attr & CEXPR_TYPE)
420 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
423 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
441 * security_dump_masked_av - dumps masked permissions during
442 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
444 static int dump_masked_av_helper(void *k, void *d, void *args)
446 struct perm_datum *pdatum = d;
447 char **permission_names = args;
449 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
451 permission_names[pdatum->value - 1] = (char *)k;
456 static void security_dump_masked_av(struct policydb *policydb,
457 struct context *scontext,
458 struct context *tcontext,
463 struct common_datum *common_dat;
464 struct class_datum *tclass_dat;
465 struct audit_buffer *ab;
467 char *scontext_name = NULL;
468 char *tcontext_name = NULL;
469 char *permission_names[32];
472 bool need_comma = false;
477 tclass_name = sym_name(policydb, SYM_CLASSES, tclass - 1);
478 tclass_dat = policydb->class_val_to_struct[tclass - 1];
479 common_dat = tclass_dat->comdatum;
481 /* init permission_names */
483 hashtab_map(common_dat->permissions.table,
484 dump_masked_av_helper, permission_names) < 0)
487 if (hashtab_map(tclass_dat->permissions.table,
488 dump_masked_av_helper, permission_names) < 0)
491 /* get scontext/tcontext in text form */
492 if (context_struct_to_string(policydb, scontext,
493 &scontext_name, &length) < 0)
496 if (context_struct_to_string(policydb, tcontext,
497 &tcontext_name, &length) < 0)
500 /* audit a message */
501 ab = audit_log_start(audit_context(),
502 GFP_ATOMIC, AUDIT_SELINUX_ERR);
506 audit_log_format(ab, "op=security_compute_av reason=%s "
507 "scontext=%s tcontext=%s tclass=%s perms=",
508 reason, scontext_name, tcontext_name, tclass_name);
510 for (index = 0; index < 32; index++) {
511 u32 mask = (1 << index);
513 if ((mask & permissions) == 0)
516 audit_log_format(ab, "%s%s",
517 need_comma ? "," : "",
518 permission_names[index]
519 ? permission_names[index] : "????");
524 /* release scontext/tcontext */
525 kfree(tcontext_name);
526 kfree(scontext_name);
532 * security_boundary_permission - drops violated permissions
533 * on boundary constraint.
535 static void type_attribute_bounds_av(struct policydb *policydb,
536 struct context *scontext,
537 struct context *tcontext,
539 struct av_decision *avd)
541 struct context lo_scontext;
542 struct context lo_tcontext, *tcontextp = tcontext;
543 struct av_decision lo_avd;
544 struct type_datum *source;
545 struct type_datum *target;
548 source = flex_array_get_ptr(policydb->type_val_to_struct_array,
555 target = flex_array_get_ptr(policydb->type_val_to_struct_array,
559 memset(&lo_avd, 0, sizeof(lo_avd));
561 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
562 lo_scontext.type = source->bounds;
564 if (target->bounds) {
565 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
566 lo_tcontext.type = target->bounds;
567 tcontextp = &lo_tcontext;
570 context_struct_compute_av(policydb, &lo_scontext,
576 masked = ~lo_avd.allowed & avd->allowed;
579 return; /* no masked permission */
581 /* mask violated permissions */
582 avd->allowed &= ~masked;
584 /* audit masked permissions */
585 security_dump_masked_av(policydb, scontext, tcontext,
586 tclass, masked, "bounds");
590 * flag which drivers have permissions
591 * only looking for ioctl based extended permssions
593 void services_compute_xperms_drivers(
594 struct extended_perms *xperms,
595 struct avtab_node *node)
599 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
600 /* if one or more driver has all permissions allowed */
601 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
602 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
603 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
604 /* if allowing permissions within a driver */
605 security_xperm_set(xperms->drivers.p,
606 node->datum.u.xperms->driver);
609 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
610 if (node->key.specified & AVTAB_XPERMS_ALLOWED)
615 * Compute access vectors and extended permissions based on a context
616 * structure pair for the permissions in a particular class.
618 static void context_struct_compute_av(struct policydb *policydb,
619 struct context *scontext,
620 struct context *tcontext,
622 struct av_decision *avd,
623 struct extended_perms *xperms)
625 struct constraint_node *constraint;
626 struct role_allow *ra;
627 struct avtab_key avkey;
628 struct avtab_node *node;
629 struct class_datum *tclass_datum;
630 struct ebitmap *sattr, *tattr;
631 struct ebitmap_node *snode, *tnode;
636 avd->auditdeny = 0xffffffff;
638 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
642 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
643 if (printk_ratelimit())
644 pr_warn("SELinux: Invalid class %hu\n", tclass);
648 tclass_datum = policydb->class_val_to_struct[tclass - 1];
651 * If a specific type enforcement rule was defined for
652 * this permission check, then use it.
654 avkey.target_class = tclass;
655 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
656 sattr = flex_array_get(policydb->type_attr_map_array,
659 tattr = flex_array_get(policydb->type_attr_map_array,
662 ebitmap_for_each_positive_bit(sattr, snode, i) {
663 ebitmap_for_each_positive_bit(tattr, tnode, j) {
664 avkey.source_type = i + 1;
665 avkey.target_type = j + 1;
666 for (node = avtab_search_node(&policydb->te_avtab,
669 node = avtab_search_node_next(node, avkey.specified)) {
670 if (node->key.specified == AVTAB_ALLOWED)
671 avd->allowed |= node->datum.u.data;
672 else if (node->key.specified == AVTAB_AUDITALLOW)
673 avd->auditallow |= node->datum.u.data;
674 else if (node->key.specified == AVTAB_AUDITDENY)
675 avd->auditdeny &= node->datum.u.data;
676 else if (xperms && (node->key.specified & AVTAB_XPERMS))
677 services_compute_xperms_drivers(xperms, node);
680 /* Check conditional av table for additional permissions */
681 cond_compute_av(&policydb->te_cond_avtab, &avkey,
688 * Remove any permissions prohibited by a constraint (this includes
691 constraint = tclass_datum->constraints;
693 if ((constraint->permissions & (avd->allowed)) &&
694 !constraint_expr_eval(policydb, scontext, tcontext, NULL,
696 avd->allowed &= ~(constraint->permissions);
698 constraint = constraint->next;
702 * If checking process transition permission and the
703 * role is changing, then check the (current_role, new_role)
706 if (tclass == policydb->process_class &&
707 (avd->allowed & policydb->process_trans_perms) &&
708 scontext->role != tcontext->role) {
709 for (ra = policydb->role_allow; ra; ra = ra->next) {
710 if (scontext->role == ra->role &&
711 tcontext->role == ra->new_role)
715 avd->allowed &= ~policydb->process_trans_perms;
719 * If the given source and target types have boundary
720 * constraint, lazy checks have to mask any violated
721 * permission and notice it to userspace via audit.
723 type_attribute_bounds_av(policydb, scontext, tcontext,
727 static int security_validtrans_handle_fail(struct selinux_state *state,
728 struct context *ocontext,
729 struct context *ncontext,
730 struct context *tcontext,
733 struct policydb *p = &state->ss->policydb;
734 char *o = NULL, *n = NULL, *t = NULL;
735 u32 olen, nlen, tlen;
737 if (context_struct_to_string(p, ocontext, &o, &olen))
739 if (context_struct_to_string(p, ncontext, &n, &nlen))
741 if (context_struct_to_string(p, tcontext, &t, &tlen))
743 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
744 "op=security_validate_transition seresult=denied"
745 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
746 o, n, t, sym_name(p, SYM_CLASSES, tclass-1));
752 if (!enforcing_enabled(state))
757 static int security_compute_validatetrans(struct selinux_state *state,
758 u32 oldsid, u32 newsid, u32 tasksid,
759 u16 orig_tclass, bool user)
761 struct policydb *policydb;
762 struct sidtab *sidtab;
763 struct context *ocontext;
764 struct context *ncontext;
765 struct context *tcontext;
766 struct class_datum *tclass_datum;
767 struct constraint_node *constraint;
772 if (!state->initialized)
775 read_lock(&state->ss->policy_rwlock);
777 policydb = &state->ss->policydb;
778 sidtab = state->ss->sidtab;
781 tclass = unmap_class(&state->ss->map, orig_tclass);
783 tclass = orig_tclass;
785 if (!tclass || tclass > policydb->p_classes.nprim) {
789 tclass_datum = policydb->class_val_to_struct[tclass - 1];
791 ocontext = sidtab_search(sidtab, oldsid);
793 pr_err("SELinux: %s: unrecognized SID %d\n",
799 ncontext = sidtab_search(sidtab, newsid);
801 pr_err("SELinux: %s: unrecognized SID %d\n",
807 tcontext = sidtab_search(sidtab, tasksid);
809 pr_err("SELinux: %s: unrecognized SID %d\n",
815 constraint = tclass_datum->validatetrans;
817 if (!constraint_expr_eval(policydb, ocontext, ncontext,
818 tcontext, constraint->expr)) {
822 rc = security_validtrans_handle_fail(state,
829 constraint = constraint->next;
833 read_unlock(&state->ss->policy_rwlock);
837 int security_validate_transition_user(struct selinux_state *state,
838 u32 oldsid, u32 newsid, u32 tasksid,
841 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
845 int security_validate_transition(struct selinux_state *state,
846 u32 oldsid, u32 newsid, u32 tasksid,
849 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
854 * security_bounded_transition - check whether the given
855 * transition is directed to bounded, or not.
856 * It returns 0, if @newsid is bounded by @oldsid.
857 * Otherwise, it returns error code.
859 * @oldsid : current security identifier
860 * @newsid : destinated security identifier
862 int security_bounded_transition(struct selinux_state *state,
863 u32 old_sid, u32 new_sid)
865 struct policydb *policydb;
866 struct sidtab *sidtab;
867 struct context *old_context, *new_context;
868 struct type_datum *type;
872 if (!state->initialized)
875 read_lock(&state->ss->policy_rwlock);
877 policydb = &state->ss->policydb;
878 sidtab = state->ss->sidtab;
881 old_context = sidtab_search(sidtab, old_sid);
883 pr_err("SELinux: %s: unrecognized SID %u\n",
889 new_context = sidtab_search(sidtab, new_sid);
891 pr_err("SELinux: %s: unrecognized SID %u\n",
897 /* type/domain unchanged */
898 if (old_context->type == new_context->type)
901 index = new_context->type;
903 type = flex_array_get_ptr(policydb->type_val_to_struct_array,
907 /* not bounded anymore */
912 /* @newsid is bounded by @oldsid */
914 if (type->bounds == old_context->type)
917 index = type->bounds;
921 char *old_name = NULL;
922 char *new_name = NULL;
925 if (!context_struct_to_string(policydb, old_context,
926 &old_name, &length) &&
927 !context_struct_to_string(policydb, new_context,
928 &new_name, &length)) {
929 audit_log(audit_context(),
930 GFP_ATOMIC, AUDIT_SELINUX_ERR,
931 "op=security_bounded_transition "
933 "oldcontext=%s newcontext=%s",
940 read_unlock(&state->ss->policy_rwlock);
945 static void avd_init(struct selinux_state *state, struct av_decision *avd)
949 avd->auditdeny = 0xffffffff;
950 avd->seqno = state->ss->latest_granting;
954 void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
955 struct avtab_node *node)
959 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
960 if (xpermd->driver != node->datum.u.xperms->driver)
962 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
963 if (!security_xperm_test(node->datum.u.xperms->perms.p,
970 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
971 xpermd->used |= XPERMS_ALLOWED;
972 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
973 memset(xpermd->allowed->p, 0xff,
974 sizeof(xpermd->allowed->p));
976 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
977 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
978 xpermd->allowed->p[i] |=
979 node->datum.u.xperms->perms.p[i];
981 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
982 xpermd->used |= XPERMS_AUDITALLOW;
983 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
984 memset(xpermd->auditallow->p, 0xff,
985 sizeof(xpermd->auditallow->p));
987 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
988 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
989 xpermd->auditallow->p[i] |=
990 node->datum.u.xperms->perms.p[i];
992 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
993 xpermd->used |= XPERMS_DONTAUDIT;
994 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
995 memset(xpermd->dontaudit->p, 0xff,
996 sizeof(xpermd->dontaudit->p));
998 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
999 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
1000 xpermd->dontaudit->p[i] |=
1001 node->datum.u.xperms->perms.p[i];
1008 void security_compute_xperms_decision(struct selinux_state *state,
1013 struct extended_perms_decision *xpermd)
1015 struct policydb *policydb;
1016 struct sidtab *sidtab;
1018 struct context *scontext, *tcontext;
1019 struct avtab_key avkey;
1020 struct avtab_node *node;
1021 struct ebitmap *sattr, *tattr;
1022 struct ebitmap_node *snode, *tnode;
1025 xpermd->driver = driver;
1027 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1028 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1029 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1031 read_lock(&state->ss->policy_rwlock);
1032 if (!state->initialized)
1035 policydb = &state->ss->policydb;
1036 sidtab = state->ss->sidtab;
1038 scontext = sidtab_search(sidtab, ssid);
1040 pr_err("SELinux: %s: unrecognized SID %d\n",
1045 tcontext = sidtab_search(sidtab, tsid);
1047 pr_err("SELinux: %s: unrecognized SID %d\n",
1052 tclass = unmap_class(&state->ss->map, orig_tclass);
1053 if (unlikely(orig_tclass && !tclass)) {
1054 if (policydb->allow_unknown)
1060 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
1061 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1065 avkey.target_class = tclass;
1066 avkey.specified = AVTAB_XPERMS;
1067 sattr = flex_array_get(policydb->type_attr_map_array,
1068 scontext->type - 1);
1070 tattr = flex_array_get(policydb->type_attr_map_array,
1071 tcontext->type - 1);
1073 ebitmap_for_each_positive_bit(sattr, snode, i) {
1074 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1075 avkey.source_type = i + 1;
1076 avkey.target_type = j + 1;
1077 for (node = avtab_search_node(&policydb->te_avtab,
1080 node = avtab_search_node_next(node, avkey.specified))
1081 services_compute_xperms_decision(xpermd, node);
1083 cond_compute_xperms(&policydb->te_cond_avtab,
1088 read_unlock(&state->ss->policy_rwlock);
1091 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1096 * security_compute_av - Compute access vector decisions.
1097 * @ssid: source security identifier
1098 * @tsid: target security identifier
1099 * @tclass: target security class
1100 * @avd: access vector decisions
1101 * @xperms: extended permissions
1103 * Compute a set of access vector decisions based on the
1104 * SID pair (@ssid, @tsid) for the permissions in @tclass.
1106 void security_compute_av(struct selinux_state *state,
1110 struct av_decision *avd,
1111 struct extended_perms *xperms)
1113 struct policydb *policydb;
1114 struct sidtab *sidtab;
1116 struct context *scontext = NULL, *tcontext = NULL;
1118 read_lock(&state->ss->policy_rwlock);
1119 avd_init(state, avd);
1121 if (!state->initialized)
1124 policydb = &state->ss->policydb;
1125 sidtab = state->ss->sidtab;
1127 scontext = sidtab_search(sidtab, ssid);
1129 pr_err("SELinux: %s: unrecognized SID %d\n",
1134 /* permissive domain? */
1135 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1136 avd->flags |= AVD_FLAGS_PERMISSIVE;
1138 tcontext = sidtab_search(sidtab, tsid);
1140 pr_err("SELinux: %s: unrecognized SID %d\n",
1145 tclass = unmap_class(&state->ss->map, orig_tclass);
1146 if (unlikely(orig_tclass && !tclass)) {
1147 if (policydb->allow_unknown)
1151 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1153 map_decision(&state->ss->map, orig_tclass, avd,
1154 policydb->allow_unknown);
1156 read_unlock(&state->ss->policy_rwlock);
1159 avd->allowed = 0xffffffff;
1163 void security_compute_av_user(struct selinux_state *state,
1167 struct av_decision *avd)
1169 struct policydb *policydb;
1170 struct sidtab *sidtab;
1171 struct context *scontext = NULL, *tcontext = NULL;
1173 read_lock(&state->ss->policy_rwlock);
1174 avd_init(state, avd);
1175 if (!state->initialized)
1178 policydb = &state->ss->policydb;
1179 sidtab = state->ss->sidtab;
1181 scontext = sidtab_search(sidtab, ssid);
1183 pr_err("SELinux: %s: unrecognized SID %d\n",
1188 /* permissive domain? */
1189 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1190 avd->flags |= AVD_FLAGS_PERMISSIVE;
1192 tcontext = sidtab_search(sidtab, tsid);
1194 pr_err("SELinux: %s: unrecognized SID %d\n",
1199 if (unlikely(!tclass)) {
1200 if (policydb->allow_unknown)
1205 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1208 read_unlock(&state->ss->policy_rwlock);
1211 avd->allowed = 0xffffffff;
1216 * Write the security context string representation of
1217 * the context structure `context' into a dynamically
1218 * allocated string of the correct size. Set `*scontext'
1219 * to point to this string and set `*scontext_len' to
1220 * the length of the string.
1222 static int context_struct_to_string(struct policydb *p,
1223 struct context *context,
1224 char **scontext, u32 *scontext_len)
1233 *scontext_len = context->len;
1235 *scontext = kstrdup(context->str, GFP_ATOMIC);
1242 /* Compute the size of the context. */
1243 *scontext_len += strlen(sym_name(p, SYM_USERS, context->user - 1)) + 1;
1244 *scontext_len += strlen(sym_name(p, SYM_ROLES, context->role - 1)) + 1;
1245 *scontext_len += strlen(sym_name(p, SYM_TYPES, context->type - 1)) + 1;
1246 *scontext_len += mls_compute_context_len(p, context);
1251 /* Allocate space for the context; caller must free this space. */
1252 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1255 *scontext = scontextp;
1258 * Copy the user name, role name and type name into the context.
1260 scontextp += sprintf(scontextp, "%s:%s:%s",
1261 sym_name(p, SYM_USERS, context->user - 1),
1262 sym_name(p, SYM_ROLES, context->role - 1),
1263 sym_name(p, SYM_TYPES, context->type - 1));
1265 mls_sid_to_context(p, context, &scontextp);
1272 #include "initial_sid_to_string.h"
1274 const char *security_get_initial_sid_context(u32 sid)
1276 if (unlikely(sid > SECINITSID_NUM))
1278 return initial_sid_to_string[sid];
1281 static int security_sid_to_context_core(struct selinux_state *state,
1282 u32 sid, char **scontext,
1283 u32 *scontext_len, int force,
1286 struct policydb *policydb;
1287 struct sidtab *sidtab;
1288 struct context *context;
1295 if (!state->initialized) {
1296 if (sid <= SECINITSID_NUM) {
1299 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
1302 scontextp = kmemdup(initial_sid_to_string[sid],
1303 *scontext_len, GFP_ATOMIC);
1308 *scontext = scontextp;
1311 pr_err("SELinux: %s: called before initial "
1312 "load_policy on unknown SID %d\n", __func__, sid);
1316 read_lock(&state->ss->policy_rwlock);
1317 policydb = &state->ss->policydb;
1318 sidtab = state->ss->sidtab;
1320 context = sidtab_search_force(sidtab, sid);
1322 context = sidtab_search(sidtab, sid);
1324 pr_err("SELinux: %s: unrecognized SID %d\n",
1329 if (only_invalid && !context->len) {
1334 rc = context_struct_to_string(policydb, context, scontext,
1338 read_unlock(&state->ss->policy_rwlock);
1345 * security_sid_to_context - Obtain a context for a given SID.
1346 * @sid: security identifier, SID
1347 * @scontext: security context
1348 * @scontext_len: length in bytes
1350 * Write the string representation of the context associated with @sid
1351 * into a dynamically allocated string of the correct size. Set @scontext
1352 * to point to this string and set @scontext_len to the length of the string.
1354 int security_sid_to_context(struct selinux_state *state,
1355 u32 sid, char **scontext, u32 *scontext_len)
1357 return security_sid_to_context_core(state, sid, scontext,
1358 scontext_len, 0, 0);
1361 int security_sid_to_context_force(struct selinux_state *state, u32 sid,
1362 char **scontext, u32 *scontext_len)
1364 return security_sid_to_context_core(state, sid, scontext,
1365 scontext_len, 1, 0);
1369 * security_sid_to_context_inval - Obtain a context for a given SID if it
1371 * @sid: security identifier, SID
1372 * @scontext: security context
1373 * @scontext_len: length in bytes
1375 * Write the string representation of the context associated with @sid
1376 * into a dynamically allocated string of the correct size, but only if the
1377 * context is invalid in the current policy. Set @scontext to point to
1378 * this string (or NULL if the context is valid) and set @scontext_len to
1379 * the length of the string (or 0 if the context is valid).
1381 int security_sid_to_context_inval(struct selinux_state *state, u32 sid,
1382 char **scontext, u32 *scontext_len)
1384 return security_sid_to_context_core(state, sid, scontext,
1385 scontext_len, 1, 1);
1389 * Caveat: Mutates scontext.
1391 static int string_to_context_struct(struct policydb *pol,
1392 struct sidtab *sidtabp,
1394 struct context *ctx,
1397 struct role_datum *role;
1398 struct type_datum *typdatum;
1399 struct user_datum *usrdatum;
1400 char *scontextp, *p, oldc;
1405 /* Parse the security context. */
1408 scontextp = (char *) scontext;
1410 /* Extract the user. */
1412 while (*p && *p != ':')
1420 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1424 ctx->user = usrdatum->value;
1428 while (*p && *p != ':')
1436 role = hashtab_search(pol->p_roles.table, scontextp);
1439 ctx->role = role->value;
1443 while (*p && *p != ':')
1448 typdatum = hashtab_search(pol->p_types.table, scontextp);
1449 if (!typdatum || typdatum->attribute)
1452 ctx->type = typdatum->value;
1454 rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid);
1458 /* Check the validity of the new context. */
1460 if (!policydb_context_isvalid(pol, ctx))
1465 context_destroy(ctx);
1469 static int security_context_to_sid_core(struct selinux_state *state,
1470 const char *scontext, u32 scontext_len,
1471 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1474 struct policydb *policydb;
1475 struct sidtab *sidtab;
1476 char *scontext2, *str = NULL;
1477 struct context context;
1480 /* An empty security context is never valid. */
1484 /* Copy the string to allow changes and ensure a NUL terminator */
1485 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1489 if (!state->initialized) {
1492 for (i = 1; i < SECINITSID_NUM; i++) {
1493 if (!strcmp(initial_sid_to_string[i], scontext2)) {
1498 *sid = SECINITSID_KERNEL;
1504 /* Save another copy for storing in uninterpreted form */
1506 str = kstrdup(scontext2, gfp_flags);
1510 read_lock(&state->ss->policy_rwlock);
1511 policydb = &state->ss->policydb;
1512 sidtab = state->ss->sidtab;
1513 rc = string_to_context_struct(policydb, sidtab, scontext2,
1515 if (rc == -EINVAL && force) {
1517 context.len = strlen(str) + 1;
1521 rc = sidtab_context_to_sid(sidtab, &context, sid);
1522 context_destroy(&context);
1524 read_unlock(&state->ss->policy_rwlock);
1532 * security_context_to_sid - Obtain a SID for a given security context.
1533 * @scontext: security context
1534 * @scontext_len: length in bytes
1535 * @sid: security identifier, SID
1536 * @gfp: context for the allocation
1538 * Obtains a SID associated with the security context that
1539 * has the string representation specified by @scontext.
1540 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1541 * memory is available, or 0 on success.
1543 int security_context_to_sid(struct selinux_state *state,
1544 const char *scontext, u32 scontext_len, u32 *sid,
1547 return security_context_to_sid_core(state, scontext, scontext_len,
1548 sid, SECSID_NULL, gfp, 0);
1551 int security_context_str_to_sid(struct selinux_state *state,
1552 const char *scontext, u32 *sid, gfp_t gfp)
1554 return security_context_to_sid(state, scontext, strlen(scontext),
1559 * security_context_to_sid_default - Obtain a SID for a given security context,
1560 * falling back to specified default if needed.
1562 * @scontext: security context
1563 * @scontext_len: length in bytes
1564 * @sid: security identifier, SID
1565 * @def_sid: default SID to assign on error
1567 * Obtains a SID associated with the security context that
1568 * has the string representation specified by @scontext.
1569 * The default SID is passed to the MLS layer to be used to allow
1570 * kernel labeling of the MLS field if the MLS field is not present
1571 * (for upgrading to MLS without full relabel).
1572 * Implicitly forces adding of the context even if it cannot be mapped yet.
1573 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1574 * memory is available, or 0 on success.
1576 int security_context_to_sid_default(struct selinux_state *state,
1577 const char *scontext, u32 scontext_len,
1578 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1580 return security_context_to_sid_core(state, scontext, scontext_len,
1581 sid, def_sid, gfp_flags, 1);
1584 int security_context_to_sid_force(struct selinux_state *state,
1585 const char *scontext, u32 scontext_len,
1588 return security_context_to_sid_core(state, scontext, scontext_len,
1589 sid, SECSID_NULL, GFP_KERNEL, 1);
1592 static int compute_sid_handle_invalid_context(
1593 struct selinux_state *state,
1594 struct context *scontext,
1595 struct context *tcontext,
1597 struct context *newcontext)
1599 struct policydb *policydb = &state->ss->policydb;
1600 char *s = NULL, *t = NULL, *n = NULL;
1601 u32 slen, tlen, nlen;
1603 if (context_struct_to_string(policydb, scontext, &s, &slen))
1605 if (context_struct_to_string(policydb, tcontext, &t, &tlen))
1607 if (context_struct_to_string(policydb, newcontext, &n, &nlen))
1609 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
1610 "op=security_compute_sid invalid_context=%s"
1614 n, s, t, sym_name(policydb, SYM_CLASSES, tclass-1));
1619 if (!enforcing_enabled(state))
1624 static void filename_compute_type(struct policydb *policydb,
1625 struct context *newcontext,
1626 u32 stype, u32 ttype, u16 tclass,
1627 const char *objname)
1629 struct filename_trans ft;
1630 struct filename_trans_datum *otype;
1633 * Most filename trans rules are going to live in specific directories
1634 * like /dev or /var/run. This bitmap will quickly skip rule searches
1635 * if the ttype does not contain any rules.
1637 if (!ebitmap_get_bit(&policydb->filename_trans_ttypes, ttype))
1645 otype = hashtab_search(policydb->filename_trans, &ft);
1647 newcontext->type = otype->otype;
1650 static int security_compute_sid(struct selinux_state *state,
1655 const char *objname,
1659 struct policydb *policydb;
1660 struct sidtab *sidtab;
1661 struct class_datum *cladatum = NULL;
1662 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1663 struct role_trans *roletr = NULL;
1664 struct avtab_key avkey;
1665 struct avtab_datum *avdatum;
1666 struct avtab_node *node;
1671 if (!state->initialized) {
1672 switch (orig_tclass) {
1673 case SECCLASS_PROCESS: /* kernel value */
1683 context_init(&newcontext);
1685 read_lock(&state->ss->policy_rwlock);
1688 tclass = unmap_class(&state->ss->map, orig_tclass);
1689 sock = security_is_socket_class(orig_tclass);
1691 tclass = orig_tclass;
1692 sock = security_is_socket_class(map_class(&state->ss->map,
1696 policydb = &state->ss->policydb;
1697 sidtab = state->ss->sidtab;
1699 scontext = sidtab_search(sidtab, ssid);
1701 pr_err("SELinux: %s: unrecognized SID %d\n",
1706 tcontext = sidtab_search(sidtab, tsid);
1708 pr_err("SELinux: %s: unrecognized SID %d\n",
1714 if (tclass && tclass <= policydb->p_classes.nprim)
1715 cladatum = policydb->class_val_to_struct[tclass - 1];
1717 /* Set the user identity. */
1718 switch (specified) {
1719 case AVTAB_TRANSITION:
1721 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1722 newcontext.user = tcontext->user;
1724 /* notice this gets both DEFAULT_SOURCE and unset */
1725 /* Use the process user identity. */
1726 newcontext.user = scontext->user;
1730 /* Use the related object owner. */
1731 newcontext.user = tcontext->user;
1735 /* Set the role to default values. */
1736 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
1737 newcontext.role = scontext->role;
1738 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1739 newcontext.role = tcontext->role;
1741 if ((tclass == policydb->process_class) || (sock == true))
1742 newcontext.role = scontext->role;
1744 newcontext.role = OBJECT_R_VAL;
1747 /* Set the type to default values. */
1748 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
1749 newcontext.type = scontext->type;
1750 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
1751 newcontext.type = tcontext->type;
1753 if ((tclass == policydb->process_class) || (sock == true)) {
1754 /* Use the type of process. */
1755 newcontext.type = scontext->type;
1757 /* Use the type of the related object. */
1758 newcontext.type = tcontext->type;
1762 /* Look for a type transition/member/change rule. */
1763 avkey.source_type = scontext->type;
1764 avkey.target_type = tcontext->type;
1765 avkey.target_class = tclass;
1766 avkey.specified = specified;
1767 avdatum = avtab_search(&policydb->te_avtab, &avkey);
1769 /* If no permanent rule, also check for enabled conditional rules */
1771 node = avtab_search_node(&policydb->te_cond_avtab, &avkey);
1772 for (; node; node = avtab_search_node_next(node, specified)) {
1773 if (node->key.specified & AVTAB_ENABLED) {
1774 avdatum = &node->datum;
1781 /* Use the type from the type transition/member/change rule. */
1782 newcontext.type = avdatum->u.data;
1785 /* if we have a objname this is a file trans check so check those rules */
1787 filename_compute_type(policydb, &newcontext, scontext->type,
1788 tcontext->type, tclass, objname);
1790 /* Check for class-specific changes. */
1791 if (specified & AVTAB_TRANSITION) {
1792 /* Look for a role transition rule. */
1793 for (roletr = policydb->role_tr; roletr;
1794 roletr = roletr->next) {
1795 if ((roletr->role == scontext->role) &&
1796 (roletr->type == tcontext->type) &&
1797 (roletr->tclass == tclass)) {
1798 /* Use the role transition rule. */
1799 newcontext.role = roletr->new_role;
1805 /* Set the MLS attributes.
1806 This is done last because it may allocate memory. */
1807 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified,
1812 /* Check the validity of the context. */
1813 if (!policydb_context_isvalid(policydb, &newcontext)) {
1814 rc = compute_sid_handle_invalid_context(state, scontext,
1821 /* Obtain the sid for the context. */
1822 rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
1824 read_unlock(&state->ss->policy_rwlock);
1825 context_destroy(&newcontext);
1831 * security_transition_sid - Compute the SID for a new subject/object.
1832 * @ssid: source security identifier
1833 * @tsid: target security identifier
1834 * @tclass: target security class
1835 * @out_sid: security identifier for new subject/object
1837 * Compute a SID to use for labeling a new subject or object in the
1838 * class @tclass based on a SID pair (@ssid, @tsid).
1839 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1840 * if insufficient memory is available, or %0 if the new SID was
1841 * computed successfully.
1843 int security_transition_sid(struct selinux_state *state,
1844 u32 ssid, u32 tsid, u16 tclass,
1845 const struct qstr *qstr, u32 *out_sid)
1847 return security_compute_sid(state, ssid, tsid, tclass,
1849 qstr ? qstr->name : NULL, out_sid, true);
1852 int security_transition_sid_user(struct selinux_state *state,
1853 u32 ssid, u32 tsid, u16 tclass,
1854 const char *objname, u32 *out_sid)
1856 return security_compute_sid(state, ssid, tsid, tclass,
1858 objname, out_sid, false);
1862 * security_member_sid - Compute the SID for member selection.
1863 * @ssid: source security identifier
1864 * @tsid: target security identifier
1865 * @tclass: target security class
1866 * @out_sid: security identifier for selected member
1868 * Compute a SID to use when selecting a member of a polyinstantiated
1869 * object of class @tclass based on a SID pair (@ssid, @tsid).
1870 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1871 * if insufficient memory is available, or %0 if the SID was
1872 * computed successfully.
1874 int security_member_sid(struct selinux_state *state,
1880 return security_compute_sid(state, ssid, tsid, tclass,
1886 * security_change_sid - Compute the SID for object relabeling.
1887 * @ssid: source security identifier
1888 * @tsid: target security identifier
1889 * @tclass: target security class
1890 * @out_sid: security identifier for selected member
1892 * Compute a SID to use for relabeling an object of class @tclass
1893 * based on a SID pair (@ssid, @tsid).
1894 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1895 * if insufficient memory is available, or %0 if the SID was
1896 * computed successfully.
1898 int security_change_sid(struct selinux_state *state,
1904 return security_compute_sid(state,
1905 ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1909 static inline int convert_context_handle_invalid_context(
1910 struct selinux_state *state,
1911 struct context *context)
1913 struct policydb *policydb = &state->ss->policydb;
1917 if (enforcing_enabled(state))
1920 if (!context_struct_to_string(policydb, context, &s, &len)) {
1921 pr_warn("SELinux: Context %s would be invalid if enforcing\n",
1928 struct convert_context_args {
1929 struct selinux_state *state;
1930 struct policydb *oldp;
1931 struct policydb *newp;
1935 * Convert the values in the security context
1936 * structure `oldc' from the values specified
1937 * in the policy `p->oldp' to the values specified
1938 * in the policy `p->newp', storing the new context
1939 * in `newc'. Verify that the context is valid
1940 * under the new policy.
1942 static int convert_context(struct context *oldc, struct context *newc, void *p)
1944 struct convert_context_args *args;
1945 struct ocontext *oc;
1946 struct role_datum *role;
1947 struct type_datum *typdatum;
1948 struct user_datum *usrdatum;
1956 s = kstrdup(oldc->str, GFP_KERNEL);
1960 rc = string_to_context_struct(args->newp, NULL, s,
1962 if (rc == -EINVAL) {
1963 /* Retain string representation for later mapping. */
1966 newc->len = oldc->len;
1971 /* Other error condition, e.g. ENOMEM. */
1972 pr_err("SELinux: Unable to map context %s, rc = %d.\n",
1976 pr_info("SELinux: Context %s became valid (mapped).\n",
1983 /* Convert the user. */
1985 usrdatum = hashtab_search(args->newp->p_users.table,
1986 sym_name(args->oldp,
1987 SYM_USERS, oldc->user - 1));
1990 newc->user = usrdatum->value;
1992 /* Convert the role. */
1994 role = hashtab_search(args->newp->p_roles.table,
1995 sym_name(args->oldp, SYM_ROLES, oldc->role - 1));
1998 newc->role = role->value;
2000 /* Convert the type. */
2002 typdatum = hashtab_search(args->newp->p_types.table,
2003 sym_name(args->oldp,
2004 SYM_TYPES, oldc->type - 1));
2007 newc->type = typdatum->value;
2009 /* Convert the MLS fields if dealing with MLS policies */
2010 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
2011 rc = mls_convert_context(args->oldp, args->newp, oldc, newc);
2014 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
2016 * Switching between non-MLS and MLS policy:
2017 * ensure that the MLS fields of the context for all
2018 * existing entries in the sidtab are filled in with a
2019 * suitable default value, likely taken from one of the
2022 oc = args->newp->ocontexts[OCON_ISID];
2023 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
2027 pr_err("SELinux: unable to look up"
2028 " the initial SIDs list\n");
2031 rc = mls_range_set(newc, &oc->context[0].range);
2036 /* Check the validity of the new context. */
2037 if (!policydb_context_isvalid(args->newp, newc)) {
2038 rc = convert_context_handle_invalid_context(args->state, oldc);
2045 /* Map old representation to string and save it. */
2046 rc = context_struct_to_string(args->oldp, oldc, &s, &len);
2049 context_destroy(newc);
2052 pr_info("SELinux: Context %s became invalid (unmapped).\n",
2057 static void security_load_policycaps(struct selinux_state *state)
2059 struct policydb *p = &state->ss->policydb;
2061 struct ebitmap_node *node;
2063 for (i = 0; i < ARRAY_SIZE(state->policycap); i++)
2064 state->policycap[i] = ebitmap_get_bit(&p->policycaps, i);
2066 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2067 pr_info("SELinux: policy capability %s=%d\n",
2068 selinux_policycap_names[i],
2069 ebitmap_get_bit(&p->policycaps, i));
2071 ebitmap_for_each_positive_bit(&p->policycaps, node, i) {
2072 if (i >= ARRAY_SIZE(selinux_policycap_names))
2073 pr_info("SELinux: unknown policy capability %u\n",
2078 static int security_preserve_bools(struct selinux_state *state,
2079 struct policydb *newpolicydb);
2082 * security_load_policy - Load a security policy configuration.
2083 * @data: binary policy data
2084 * @len: length of data in bytes
2086 * Load a new set of security policy configuration data,
2087 * validate it and convert the SID table as necessary.
2088 * This function will flush the access vector cache after
2089 * loading the new policy.
2091 int security_load_policy(struct selinux_state *state, void *data, size_t len)
2093 struct policydb *policydb;
2094 struct sidtab *oldsidtab, *newsidtab;
2095 struct policydb *oldpolicydb, *newpolicydb;
2096 struct selinux_mapping *oldmapping;
2097 struct selinux_map newmap;
2098 struct sidtab_convert_params convert_params;
2099 struct convert_context_args args;
2102 struct policy_file file = { data, len }, *fp = &file;
2104 oldpolicydb = kcalloc(2, sizeof(*oldpolicydb), GFP_KERNEL);
2109 newpolicydb = oldpolicydb + 1;
2111 policydb = &state->ss->policydb;
2113 newsidtab = kmalloc(sizeof(*newsidtab), GFP_KERNEL);
2119 if (!state->initialized) {
2120 rc = policydb_read(policydb, fp);
2126 policydb->len = len;
2127 rc = selinux_set_mapping(policydb, secclass_map,
2131 policydb_destroy(policydb);
2135 rc = policydb_load_isids(policydb, newsidtab);
2138 policydb_destroy(policydb);
2142 state->ss->sidtab = newsidtab;
2143 security_load_policycaps(state);
2144 state->initialized = 1;
2145 seqno = ++state->ss->latest_granting;
2146 selinux_complete_init();
2147 avc_ss_reset(state->avc, seqno);
2148 selnl_notify_policyload(seqno);
2149 selinux_status_update_policyload(state, seqno);
2150 selinux_netlbl_cache_invalidate();
2151 selinux_xfrm_notify_policyload();
2155 rc = policydb_read(newpolicydb, fp);
2161 newpolicydb->len = len;
2162 /* If switching between different policy types, log MLS status */
2163 if (policydb->mls_enabled && !newpolicydb->mls_enabled)
2164 pr_info("SELinux: Disabling MLS support...\n");
2165 else if (!policydb->mls_enabled && newpolicydb->mls_enabled)
2166 pr_info("SELinux: Enabling MLS support...\n");
2168 rc = policydb_load_isids(newpolicydb, newsidtab);
2170 pr_err("SELinux: unable to load the initial SIDs\n");
2171 policydb_destroy(newpolicydb);
2176 rc = selinux_set_mapping(newpolicydb, secclass_map, &newmap);
2180 rc = security_preserve_bools(state, newpolicydb);
2182 pr_err("SELinux: unable to preserve booleans\n");
2186 oldsidtab = state->ss->sidtab;
2189 * Convert the internal representations of contexts
2190 * in the new SID table.
2193 args.oldp = policydb;
2194 args.newp = newpolicydb;
2196 convert_params.func = convert_context;
2197 convert_params.args = &args;
2198 convert_params.target = newsidtab;
2200 rc = sidtab_convert(oldsidtab, &convert_params);
2202 pr_err("SELinux: unable to convert the internal"
2203 " representation of contexts in the new SID"
2208 /* Save the old policydb and SID table to free later. */
2209 memcpy(oldpolicydb, policydb, sizeof(*policydb));
2211 /* Install the new policydb and SID table. */
2212 write_lock_irq(&state->ss->policy_rwlock);
2213 memcpy(policydb, newpolicydb, sizeof(*policydb));
2214 state->ss->sidtab = newsidtab;
2215 security_load_policycaps(state);
2216 oldmapping = state->ss->map.mapping;
2217 state->ss->map.mapping = newmap.mapping;
2218 state->ss->map.size = newmap.size;
2219 seqno = ++state->ss->latest_granting;
2220 write_unlock_irq(&state->ss->policy_rwlock);
2222 /* Free the old policydb and SID table. */
2223 policydb_destroy(oldpolicydb);
2224 sidtab_destroy(oldsidtab);
2228 avc_ss_reset(state->avc, seqno);
2229 selnl_notify_policyload(seqno);
2230 selinux_status_update_policyload(state, seqno);
2231 selinux_netlbl_cache_invalidate();
2232 selinux_xfrm_notify_policyload();
2238 kfree(newmap.mapping);
2239 sidtab_destroy(newsidtab);
2241 policydb_destroy(newpolicydb);
2248 size_t security_policydb_len(struct selinux_state *state)
2250 struct policydb *p = &state->ss->policydb;
2253 read_lock(&state->ss->policy_rwlock);
2255 read_unlock(&state->ss->policy_rwlock);
2261 * security_port_sid - Obtain the SID for a port.
2262 * @protocol: protocol number
2263 * @port: port number
2264 * @out_sid: security identifier
2266 int security_port_sid(struct selinux_state *state,
2267 u8 protocol, u16 port, u32 *out_sid)
2269 struct policydb *policydb;
2270 struct sidtab *sidtab;
2274 read_lock(&state->ss->policy_rwlock);
2276 policydb = &state->ss->policydb;
2277 sidtab = state->ss->sidtab;
2279 c = policydb->ocontexts[OCON_PORT];
2281 if (c->u.port.protocol == protocol &&
2282 c->u.port.low_port <= port &&
2283 c->u.port.high_port >= port)
2290 rc = sidtab_context_to_sid(sidtab,
2296 *out_sid = c->sid[0];
2298 *out_sid = SECINITSID_PORT;
2302 read_unlock(&state->ss->policy_rwlock);
2307 * security_pkey_sid - Obtain the SID for a pkey.
2308 * @subnet_prefix: Subnet Prefix
2309 * @pkey_num: pkey number
2310 * @out_sid: security identifier
2312 int security_ib_pkey_sid(struct selinux_state *state,
2313 u64 subnet_prefix, u16 pkey_num, u32 *out_sid)
2315 struct policydb *policydb;
2316 struct sidtab *sidtab;
2320 read_lock(&state->ss->policy_rwlock);
2322 policydb = &state->ss->policydb;
2323 sidtab = state->ss->sidtab;
2325 c = policydb->ocontexts[OCON_IBPKEY];
2327 if (c->u.ibpkey.low_pkey <= pkey_num &&
2328 c->u.ibpkey.high_pkey >= pkey_num &&
2329 c->u.ibpkey.subnet_prefix == subnet_prefix)
2337 rc = sidtab_context_to_sid(sidtab,
2343 *out_sid = c->sid[0];
2345 *out_sid = SECINITSID_UNLABELED;
2348 read_unlock(&state->ss->policy_rwlock);
2353 * security_ib_endport_sid - Obtain the SID for a subnet management interface.
2354 * @dev_name: device name
2355 * @port: port number
2356 * @out_sid: security identifier
2358 int security_ib_endport_sid(struct selinux_state *state,
2359 const char *dev_name, u8 port_num, u32 *out_sid)
2361 struct policydb *policydb;
2362 struct sidtab *sidtab;
2366 read_lock(&state->ss->policy_rwlock);
2368 policydb = &state->ss->policydb;
2369 sidtab = state->ss->sidtab;
2371 c = policydb->ocontexts[OCON_IBENDPORT];
2373 if (c->u.ibendport.port == port_num &&
2374 !strncmp(c->u.ibendport.dev_name,
2376 IB_DEVICE_NAME_MAX))
2384 rc = sidtab_context_to_sid(sidtab,
2390 *out_sid = c->sid[0];
2392 *out_sid = SECINITSID_UNLABELED;
2395 read_unlock(&state->ss->policy_rwlock);
2400 * security_netif_sid - Obtain the SID for a network interface.
2401 * @name: interface name
2402 * @if_sid: interface SID
2404 int security_netif_sid(struct selinux_state *state,
2405 char *name, u32 *if_sid)
2407 struct policydb *policydb;
2408 struct sidtab *sidtab;
2412 read_lock(&state->ss->policy_rwlock);
2414 policydb = &state->ss->policydb;
2415 sidtab = state->ss->sidtab;
2417 c = policydb->ocontexts[OCON_NETIF];
2419 if (strcmp(name, c->u.name) == 0)
2425 if (!c->sid[0] || !c->sid[1]) {
2426 rc = sidtab_context_to_sid(sidtab,
2431 rc = sidtab_context_to_sid(sidtab,
2437 *if_sid = c->sid[0];
2439 *if_sid = SECINITSID_NETIF;
2442 read_unlock(&state->ss->policy_rwlock);
2446 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2450 for (i = 0; i < 4; i++)
2451 if (addr[i] != (input[i] & mask[i])) {
2460 * security_node_sid - Obtain the SID for a node (host).
2461 * @domain: communication domain aka address family
2463 * @addrlen: address length in bytes
2464 * @out_sid: security identifier
2466 int security_node_sid(struct selinux_state *state,
2472 struct policydb *policydb;
2473 struct sidtab *sidtab;
2477 read_lock(&state->ss->policy_rwlock);
2479 policydb = &state->ss->policydb;
2480 sidtab = state->ss->sidtab;
2487 if (addrlen != sizeof(u32))
2490 addr = *((u32 *)addrp);
2492 c = policydb->ocontexts[OCON_NODE];
2494 if (c->u.node.addr == (addr & c->u.node.mask))
2503 if (addrlen != sizeof(u64) * 2)
2505 c = policydb->ocontexts[OCON_NODE6];
2507 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2516 *out_sid = SECINITSID_NODE;
2522 rc = sidtab_context_to_sid(sidtab,
2528 *out_sid = c->sid[0];
2530 *out_sid = SECINITSID_NODE;
2535 read_unlock(&state->ss->policy_rwlock);
2542 * security_get_user_sids - Obtain reachable SIDs for a user.
2543 * @fromsid: starting SID
2544 * @username: username
2545 * @sids: array of reachable SIDs for user
2546 * @nel: number of elements in @sids
2548 * Generate the set of SIDs for legal security contexts
2549 * for a given user that can be reached by @fromsid.
2550 * Set *@sids to point to a dynamically allocated
2551 * array containing the set of SIDs. Set *@nel to the
2552 * number of elements in the array.
2555 int security_get_user_sids(struct selinux_state *state,
2561 struct policydb *policydb;
2562 struct sidtab *sidtab;
2563 struct context *fromcon, usercon;
2564 u32 *mysids = NULL, *mysids2, sid;
2565 u32 mynel = 0, maxnel = SIDS_NEL;
2566 struct user_datum *user;
2567 struct role_datum *role;
2568 struct ebitmap_node *rnode, *tnode;
2574 if (!state->initialized)
2577 read_lock(&state->ss->policy_rwlock);
2579 policydb = &state->ss->policydb;
2580 sidtab = state->ss->sidtab;
2582 context_init(&usercon);
2585 fromcon = sidtab_search(sidtab, fromsid);
2590 user = hashtab_search(policydb->p_users.table, username);
2594 usercon.user = user->value;
2597 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
2601 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2602 role = policydb->role_val_to_struct[i];
2603 usercon.role = i + 1;
2604 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2605 usercon.type = j + 1;
2607 if (mls_setup_user_range(policydb, fromcon, user,
2611 rc = sidtab_context_to_sid(sidtab, &usercon, &sid);
2614 if (mynel < maxnel) {
2615 mysids[mynel++] = sid;
2619 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2622 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2625 mysids[mynel++] = sid;
2631 read_unlock(&state->ss->policy_rwlock);
2638 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2643 for (i = 0, j = 0; i < mynel; i++) {
2644 struct av_decision dummy_avd;
2645 rc = avc_has_perm_noaudit(state,
2647 SECCLASS_PROCESS, /* kernel value */
2648 PROCESS__TRANSITION, AVC_STRICT,
2651 mysids2[j++] = mysids[i];
2663 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
2664 * @fstype: filesystem type
2665 * @path: path from root of mount
2666 * @sclass: file security class
2667 * @sid: SID for path
2669 * Obtain a SID to use for a file in a filesystem that
2670 * cannot support xattr or use a fixed labeling behavior like
2671 * transition SIDs or task SIDs.
2673 * The caller must acquire the policy_rwlock before calling this function.
2675 static inline int __security_genfs_sid(struct selinux_state *state,
2681 struct policydb *policydb = &state->ss->policydb;
2682 struct sidtab *sidtab = state->ss->sidtab;
2685 struct genfs *genfs;
2689 while (path[0] == '/' && path[1] == '/')
2692 sclass = unmap_class(&state->ss->map, orig_sclass);
2693 *sid = SECINITSID_UNLABELED;
2695 for (genfs = policydb->genfs; genfs; genfs = genfs->next) {
2696 cmp = strcmp(fstype, genfs->fstype);
2705 for (c = genfs->head; c; c = c->next) {
2706 len = strlen(c->u.name);
2707 if ((!c->v.sclass || sclass == c->v.sclass) &&
2708 (strncmp(c->u.name, path, len) == 0))
2717 rc = sidtab_context_to_sid(sidtab, &c->context[0], &c->sid[0]);
2729 * security_genfs_sid - Obtain a SID for a file in a filesystem
2730 * @fstype: filesystem type
2731 * @path: path from root of mount
2732 * @sclass: file security class
2733 * @sid: SID for path
2735 * Acquire policy_rwlock before calling __security_genfs_sid() and release
2738 int security_genfs_sid(struct selinux_state *state,
2746 read_lock(&state->ss->policy_rwlock);
2747 retval = __security_genfs_sid(state, fstype, path, orig_sclass, sid);
2748 read_unlock(&state->ss->policy_rwlock);
2753 * security_fs_use - Determine how to handle labeling for a filesystem.
2754 * @sb: superblock in question
2756 int security_fs_use(struct selinux_state *state, struct super_block *sb)
2758 struct policydb *policydb;
2759 struct sidtab *sidtab;
2762 struct superblock_security_struct *sbsec = sb->s_security;
2763 const char *fstype = sb->s_type->name;
2765 read_lock(&state->ss->policy_rwlock);
2767 policydb = &state->ss->policydb;
2768 sidtab = state->ss->sidtab;
2770 c = policydb->ocontexts[OCON_FSUSE];
2772 if (strcmp(fstype, c->u.name) == 0)
2778 sbsec->behavior = c->v.behavior;
2780 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2785 sbsec->sid = c->sid[0];
2787 rc = __security_genfs_sid(state, fstype, "/", SECCLASS_DIR,
2790 sbsec->behavior = SECURITY_FS_USE_NONE;
2793 sbsec->behavior = SECURITY_FS_USE_GENFS;
2798 read_unlock(&state->ss->policy_rwlock);
2802 int security_get_bools(struct selinux_state *state,
2803 int *len, char ***names, int **values)
2805 struct policydb *policydb;
2808 if (!state->initialized) {
2815 read_lock(&state->ss->policy_rwlock);
2817 policydb = &state->ss->policydb;
2823 *len = policydb->p_bools.nprim;
2828 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
2833 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2837 for (i = 0; i < *len; i++) {
2838 (*values)[i] = policydb->bool_val_to_struct[i]->state;
2841 (*names)[i] = kstrdup(sym_name(policydb, SYM_BOOLS, i),
2848 read_unlock(&state->ss->policy_rwlock);
2852 for (i = 0; i < *len; i++)
2860 int security_set_bools(struct selinux_state *state, int len, int *values)
2862 struct policydb *policydb;
2864 int lenp, seqno = 0;
2865 struct cond_node *cur;
2867 write_lock_irq(&state->ss->policy_rwlock);
2869 policydb = &state->ss->policydb;
2872 lenp = policydb->p_bools.nprim;
2876 for (i = 0; i < len; i++) {
2877 if (!!values[i] != policydb->bool_val_to_struct[i]->state) {
2878 audit_log(audit_context(), GFP_ATOMIC,
2879 AUDIT_MAC_CONFIG_CHANGE,
2880 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2881 sym_name(policydb, SYM_BOOLS, i),
2883 policydb->bool_val_to_struct[i]->state,
2884 from_kuid(&init_user_ns, audit_get_loginuid(current)),
2885 audit_get_sessionid(current));
2888 policydb->bool_val_to_struct[i]->state = 1;
2890 policydb->bool_val_to_struct[i]->state = 0;
2893 for (cur = policydb->cond_list; cur; cur = cur->next) {
2894 rc = evaluate_cond_node(policydb, cur);
2899 seqno = ++state->ss->latest_granting;
2902 write_unlock_irq(&state->ss->policy_rwlock);
2904 avc_ss_reset(state->avc, seqno);
2905 selnl_notify_policyload(seqno);
2906 selinux_status_update_policyload(state, seqno);
2907 selinux_xfrm_notify_policyload();
2912 int security_get_bool_value(struct selinux_state *state,
2915 struct policydb *policydb;
2919 read_lock(&state->ss->policy_rwlock);
2921 policydb = &state->ss->policydb;
2924 len = policydb->p_bools.nprim;
2928 rc = policydb->bool_val_to_struct[index]->state;
2930 read_unlock(&state->ss->policy_rwlock);
2934 static int security_preserve_bools(struct selinux_state *state,
2935 struct policydb *policydb)
2937 int rc, nbools = 0, *bvalues = NULL, i;
2938 char **bnames = NULL;
2939 struct cond_bool_datum *booldatum;
2940 struct cond_node *cur;
2942 rc = security_get_bools(state, &nbools, &bnames, &bvalues);
2945 for (i = 0; i < nbools; i++) {
2946 booldatum = hashtab_search(policydb->p_bools.table, bnames[i]);
2948 booldatum->state = bvalues[i];
2950 for (cur = policydb->cond_list; cur; cur = cur->next) {
2951 rc = evaluate_cond_node(policydb, cur);
2958 for (i = 0; i < nbools; i++)
2967 * security_sid_mls_copy() - computes a new sid based on the given
2968 * sid and the mls portion of mls_sid.
2970 int security_sid_mls_copy(struct selinux_state *state,
2971 u32 sid, u32 mls_sid, u32 *new_sid)
2973 struct policydb *policydb = &state->ss->policydb;
2974 struct sidtab *sidtab = state->ss->sidtab;
2975 struct context *context1;
2976 struct context *context2;
2977 struct context newcon;
2983 if (!state->initialized || !policydb->mls_enabled) {
2988 context_init(&newcon);
2990 read_lock(&state->ss->policy_rwlock);
2993 context1 = sidtab_search(sidtab, sid);
2995 pr_err("SELinux: %s: unrecognized SID %d\n",
3001 context2 = sidtab_search(sidtab, mls_sid);
3003 pr_err("SELinux: %s: unrecognized SID %d\n",
3008 newcon.user = context1->user;
3009 newcon.role = context1->role;
3010 newcon.type = context1->type;
3011 rc = mls_context_cpy(&newcon, context2);
3015 /* Check the validity of the new context. */
3016 if (!policydb_context_isvalid(policydb, &newcon)) {
3017 rc = convert_context_handle_invalid_context(state, &newcon);
3019 if (!context_struct_to_string(policydb, &newcon, &s,
3021 audit_log(audit_context(),
3022 GFP_ATOMIC, AUDIT_SELINUX_ERR,
3023 "op=security_sid_mls_copy "
3024 "invalid_context=%s", s);
3031 rc = sidtab_context_to_sid(sidtab, &newcon, new_sid);
3033 read_unlock(&state->ss->policy_rwlock);
3034 context_destroy(&newcon);
3040 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
3041 * @nlbl_sid: NetLabel SID
3042 * @nlbl_type: NetLabel labeling protocol type
3043 * @xfrm_sid: XFRM SID
3046 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
3047 * resolved into a single SID it is returned via @peer_sid and the function
3048 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
3049 * returns a negative value. A table summarizing the behavior is below:
3051 * | function return | @sid
3052 * ------------------------------+-----------------+-----------------
3053 * no peer labels | 0 | SECSID_NULL
3054 * single peer label | 0 | <peer_label>
3055 * multiple, consistent labels | 0 | <peer_label>
3056 * multiple, inconsistent labels | -<errno> | SECSID_NULL
3059 int security_net_peersid_resolve(struct selinux_state *state,
3060 u32 nlbl_sid, u32 nlbl_type,
3064 struct policydb *policydb = &state->ss->policydb;
3065 struct sidtab *sidtab = state->ss->sidtab;
3067 struct context *nlbl_ctx;
3068 struct context *xfrm_ctx;
3070 *peer_sid = SECSID_NULL;
3072 /* handle the common (which also happens to be the set of easy) cases
3073 * right away, these two if statements catch everything involving a
3074 * single or absent peer SID/label */
3075 if (xfrm_sid == SECSID_NULL) {
3076 *peer_sid = nlbl_sid;
3079 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
3080 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
3082 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
3083 *peer_sid = xfrm_sid;
3088 * We don't need to check initialized here since the only way both
3089 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
3090 * security server was initialized and state->initialized was true.
3092 if (!policydb->mls_enabled)
3095 read_lock(&state->ss->policy_rwlock);
3098 nlbl_ctx = sidtab_search(sidtab, nlbl_sid);
3100 pr_err("SELinux: %s: unrecognized SID %d\n",
3101 __func__, nlbl_sid);
3105 xfrm_ctx = sidtab_search(sidtab, xfrm_sid);
3107 pr_err("SELinux: %s: unrecognized SID %d\n",
3108 __func__, xfrm_sid);
3111 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
3115 /* at present NetLabel SIDs/labels really only carry MLS
3116 * information so if the MLS portion of the NetLabel SID
3117 * matches the MLS portion of the labeled XFRM SID/label
3118 * then pass along the XFRM SID as it is the most
3120 *peer_sid = xfrm_sid;
3122 read_unlock(&state->ss->policy_rwlock);
3126 static int get_classes_callback(void *k, void *d, void *args)
3128 struct class_datum *datum = d;
3129 char *name = k, **classes = args;
3130 int value = datum->value - 1;
3132 classes[value] = kstrdup(name, GFP_ATOMIC);
3133 if (!classes[value])
3139 int security_get_classes(struct selinux_state *state,
3140 char ***classes, int *nclasses)
3142 struct policydb *policydb = &state->ss->policydb;
3145 if (!state->initialized) {
3151 read_lock(&state->ss->policy_rwlock);
3154 *nclasses = policydb->p_classes.nprim;
3155 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
3159 rc = hashtab_map(policydb->p_classes.table, get_classes_callback,
3163 for (i = 0; i < *nclasses; i++)
3164 kfree((*classes)[i]);
3169 read_unlock(&state->ss->policy_rwlock);
3173 static int get_permissions_callback(void *k, void *d, void *args)
3175 struct perm_datum *datum = d;
3176 char *name = k, **perms = args;
3177 int value = datum->value - 1;
3179 perms[value] = kstrdup(name, GFP_ATOMIC);
3186 int security_get_permissions(struct selinux_state *state,
3187 char *class, char ***perms, int *nperms)
3189 struct policydb *policydb = &state->ss->policydb;
3191 struct class_datum *match;
3193 read_lock(&state->ss->policy_rwlock);
3196 match = hashtab_search(policydb->p_classes.table, class);
3198 pr_err("SELinux: %s: unrecognized class %s\n",
3204 *nperms = match->permissions.nprim;
3205 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
3209 if (match->comdatum) {
3210 rc = hashtab_map(match->comdatum->permissions.table,
3211 get_permissions_callback, *perms);
3216 rc = hashtab_map(match->permissions.table, get_permissions_callback,
3222 read_unlock(&state->ss->policy_rwlock);
3226 read_unlock(&state->ss->policy_rwlock);
3227 for (i = 0; i < *nperms; i++)
3233 int security_get_reject_unknown(struct selinux_state *state)
3235 return state->ss->policydb.reject_unknown;
3238 int security_get_allow_unknown(struct selinux_state *state)
3240 return state->ss->policydb.allow_unknown;
3244 * security_policycap_supported - Check for a specific policy capability
3245 * @req_cap: capability
3248 * This function queries the currently loaded policy to see if it supports the
3249 * capability specified by @req_cap. Returns true (1) if the capability is
3250 * supported, false (0) if it isn't supported.
3253 int security_policycap_supported(struct selinux_state *state,
3254 unsigned int req_cap)
3256 struct policydb *policydb = &state->ss->policydb;
3259 read_lock(&state->ss->policy_rwlock);
3260 rc = ebitmap_get_bit(&policydb->policycaps, req_cap);
3261 read_unlock(&state->ss->policy_rwlock);
3266 struct selinux_audit_rule {
3268 struct context au_ctxt;
3271 void selinux_audit_rule_free(void *vrule)
3273 struct selinux_audit_rule *rule = vrule;
3276 context_destroy(&rule->au_ctxt);
3281 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3283 struct selinux_state *state = &selinux_state;
3284 struct policydb *policydb = &state->ss->policydb;
3285 struct selinux_audit_rule *tmprule;
3286 struct role_datum *roledatum;
3287 struct type_datum *typedatum;
3288 struct user_datum *userdatum;
3289 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
3294 if (!state->initialized)
3298 case AUDIT_SUBJ_USER:
3299 case AUDIT_SUBJ_ROLE:
3300 case AUDIT_SUBJ_TYPE:
3301 case AUDIT_OBJ_USER:
3302 case AUDIT_OBJ_ROLE:
3303 case AUDIT_OBJ_TYPE:
3304 /* only 'equals' and 'not equals' fit user, role, and type */
3305 if (op != Audit_equal && op != Audit_not_equal)
3308 case AUDIT_SUBJ_SEN:
3309 case AUDIT_SUBJ_CLR:
3310 case AUDIT_OBJ_LEV_LOW:
3311 case AUDIT_OBJ_LEV_HIGH:
3312 /* we do not allow a range, indicated by the presence of '-' */
3313 if (strchr(rulestr, '-'))
3317 /* only the above fields are valid */
3321 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3325 context_init(&tmprule->au_ctxt);
3327 read_lock(&state->ss->policy_rwlock);
3329 tmprule->au_seqno = state->ss->latest_granting;
3332 case AUDIT_SUBJ_USER:
3333 case AUDIT_OBJ_USER:
3335 userdatum = hashtab_search(policydb->p_users.table, rulestr);
3338 tmprule->au_ctxt.user = userdatum->value;
3340 case AUDIT_SUBJ_ROLE:
3341 case AUDIT_OBJ_ROLE:
3343 roledatum = hashtab_search(policydb->p_roles.table, rulestr);
3346 tmprule->au_ctxt.role = roledatum->value;
3348 case AUDIT_SUBJ_TYPE:
3349 case AUDIT_OBJ_TYPE:
3351 typedatum = hashtab_search(policydb->p_types.table, rulestr);
3354 tmprule->au_ctxt.type = typedatum->value;
3356 case AUDIT_SUBJ_SEN:
3357 case AUDIT_SUBJ_CLR:
3358 case AUDIT_OBJ_LEV_LOW:
3359 case AUDIT_OBJ_LEV_HIGH:
3360 rc = mls_from_string(policydb, rulestr, &tmprule->au_ctxt,
3368 read_unlock(&state->ss->policy_rwlock);
3371 selinux_audit_rule_free(tmprule);
3380 /* Check to see if the rule contains any selinux fields */
3381 int selinux_audit_rule_known(struct audit_krule *rule)
3385 for (i = 0; i < rule->field_count; i++) {
3386 struct audit_field *f = &rule->fields[i];
3388 case AUDIT_SUBJ_USER:
3389 case AUDIT_SUBJ_ROLE:
3390 case AUDIT_SUBJ_TYPE:
3391 case AUDIT_SUBJ_SEN:
3392 case AUDIT_SUBJ_CLR:
3393 case AUDIT_OBJ_USER:
3394 case AUDIT_OBJ_ROLE:
3395 case AUDIT_OBJ_TYPE:
3396 case AUDIT_OBJ_LEV_LOW:
3397 case AUDIT_OBJ_LEV_HIGH:
3405 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule)
3407 struct selinux_state *state = &selinux_state;
3408 struct context *ctxt;
3409 struct mls_level *level;
3410 struct selinux_audit_rule *rule = vrule;
3413 if (unlikely(!rule)) {
3414 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
3418 read_lock(&state->ss->policy_rwlock);
3420 if (rule->au_seqno < state->ss->latest_granting) {
3425 ctxt = sidtab_search(state->ss->sidtab, sid);
3426 if (unlikely(!ctxt)) {
3427 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
3433 /* a field/op pair that is not caught here will simply fall through
3436 case AUDIT_SUBJ_USER:
3437 case AUDIT_OBJ_USER:
3440 match = (ctxt->user == rule->au_ctxt.user);
3442 case Audit_not_equal:
3443 match = (ctxt->user != rule->au_ctxt.user);
3447 case AUDIT_SUBJ_ROLE:
3448 case AUDIT_OBJ_ROLE:
3451 match = (ctxt->role == rule->au_ctxt.role);
3453 case Audit_not_equal:
3454 match = (ctxt->role != rule->au_ctxt.role);
3458 case AUDIT_SUBJ_TYPE:
3459 case AUDIT_OBJ_TYPE:
3462 match = (ctxt->type == rule->au_ctxt.type);
3464 case Audit_not_equal:
3465 match = (ctxt->type != rule->au_ctxt.type);
3469 case AUDIT_SUBJ_SEN:
3470 case AUDIT_SUBJ_CLR:
3471 case AUDIT_OBJ_LEV_LOW:
3472 case AUDIT_OBJ_LEV_HIGH:
3473 level = ((field == AUDIT_SUBJ_SEN ||
3474 field == AUDIT_OBJ_LEV_LOW) ?
3475 &ctxt->range.level[0] : &ctxt->range.level[1]);
3478 match = mls_level_eq(&rule->au_ctxt.range.level[0],
3481 case Audit_not_equal:
3482 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
3486 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
3488 !mls_level_eq(&rule->au_ctxt.range.level[0],
3492 match = mls_level_dom(&rule->au_ctxt.range.level[0],
3496 match = (mls_level_dom(level,
3497 &rule->au_ctxt.range.level[0]) &&
3498 !mls_level_eq(level,
3499 &rule->au_ctxt.range.level[0]));
3502 match = mls_level_dom(level,
3503 &rule->au_ctxt.range.level[0]);
3509 read_unlock(&state->ss->policy_rwlock);
3513 static int (*aurule_callback)(void) = audit_update_lsm_rules;
3515 static int aurule_avc_callback(u32 event)
3519 if (event == AVC_CALLBACK_RESET && aurule_callback)
3520 err = aurule_callback();
3524 static int __init aurule_init(void)
3528 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
3530 panic("avc_add_callback() failed, error %d\n", err);
3534 __initcall(aurule_init);
3536 #ifdef CONFIG_NETLABEL
3538 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3539 * @secattr: the NetLabel packet security attributes
3540 * @sid: the SELinux SID
3543 * Attempt to cache the context in @ctx, which was derived from the packet in
3544 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3545 * already been initialized.
3548 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
3553 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3554 if (sid_cache == NULL)
3556 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
3557 if (secattr->cache == NULL) {
3563 secattr->cache->free = kfree;
3564 secattr->cache->data = sid_cache;
3565 secattr->flags |= NETLBL_SECATTR_CACHE;
3569 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
3570 * @secattr: the NetLabel packet security attributes
3571 * @sid: the SELinux SID
3574 * Convert the given NetLabel security attributes in @secattr into a
3575 * SELinux SID. If the @secattr field does not contain a full SELinux
3576 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
3577 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3578 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3579 * conversion for future lookups. Returns zero on success, negative values on
3583 int security_netlbl_secattr_to_sid(struct selinux_state *state,
3584 struct netlbl_lsm_secattr *secattr,
3587 struct policydb *policydb = &state->ss->policydb;
3588 struct sidtab *sidtab = state->ss->sidtab;
3590 struct context *ctx;
3591 struct context ctx_new;
3593 if (!state->initialized) {
3598 read_lock(&state->ss->policy_rwlock);
3600 if (secattr->flags & NETLBL_SECATTR_CACHE)
3601 *sid = *(u32 *)secattr->cache->data;
3602 else if (secattr->flags & NETLBL_SECATTR_SECID)
3603 *sid = secattr->attr.secid;
3604 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3606 ctx = sidtab_search(sidtab, SECINITSID_NETMSG);
3610 context_init(&ctx_new);
3611 ctx_new.user = ctx->user;
3612 ctx_new.role = ctx->role;
3613 ctx_new.type = ctx->type;
3614 mls_import_netlbl_lvl(policydb, &ctx_new, secattr);
3615 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
3616 rc = mls_import_netlbl_cat(policydb, &ctx_new, secattr);
3621 if (!mls_context_isvalid(policydb, &ctx_new))
3624 rc = sidtab_context_to_sid(sidtab, &ctx_new, sid);
3628 security_netlbl_cache_add(secattr, *sid);
3630 ebitmap_destroy(&ctx_new.range.level[0].cat);
3634 read_unlock(&state->ss->policy_rwlock);
3637 ebitmap_destroy(&ctx_new.range.level[0].cat);
3639 read_unlock(&state->ss->policy_rwlock);
3644 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3645 * @sid: the SELinux SID
3646 * @secattr: the NetLabel packet security attributes
3649 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3650 * Returns zero on success, negative values on failure.
3653 int security_netlbl_sid_to_secattr(struct selinux_state *state,
3654 u32 sid, struct netlbl_lsm_secattr *secattr)
3656 struct policydb *policydb = &state->ss->policydb;
3658 struct context *ctx;
3660 if (!state->initialized)
3663 read_lock(&state->ss->policy_rwlock);
3666 ctx = sidtab_search(state->ss->sidtab, sid);
3671 secattr->domain = kstrdup(sym_name(policydb, SYM_TYPES, ctx->type - 1),
3673 if (secattr->domain == NULL)
3676 secattr->attr.secid = sid;
3677 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3678 mls_export_netlbl_lvl(policydb, ctx, secattr);
3679 rc = mls_export_netlbl_cat(policydb, ctx, secattr);
3681 read_unlock(&state->ss->policy_rwlock);
3684 #endif /* CONFIG_NETLABEL */
3687 * security_read_policy - read the policy.
3688 * @data: binary policy data
3689 * @len: length of data in bytes
3692 int security_read_policy(struct selinux_state *state,
3693 void **data, size_t *len)
3695 struct policydb *policydb = &state->ss->policydb;
3697 struct policy_file fp;
3699 if (!state->initialized)
3702 *len = security_policydb_len(state);
3704 *data = vmalloc_user(*len);
3711 read_lock(&state->ss->policy_rwlock);
3712 rc = policydb_write(policydb, &fp);
3713 read_unlock(&state->ss->policy_rwlock);
3718 *len = (unsigned long)fp.data - (unsigned long)*data;