2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
21 * Updated: Chad Sellers <csellers@tresys.com>
23 * Added validation of kernel classes and permissions
25 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
28 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/spinlock.h>
37 #include <linux/rcupdate.h>
38 #include <linux/errno.h>
40 #include <linux/sched.h>
41 #include <linux/audit.h>
42 #include <linux/mutex.h>
43 #include <linux/selinux.h>
44 #include <net/netlabel.h>
54 #include "conditional.h"
62 extern void selnl_notify_policyload(u32 seqno);
63 unsigned int policydb_loaded_version;
65 int selinux_policycap_netpeer;
66 int selinux_policycap_openperm;
69 * This is declared in avc.c
71 extern const struct selinux_class_perm selinux_class_perm;
73 static DEFINE_RWLOCK(policy_rwlock);
74 #define POLICY_RDLOCK read_lock(&policy_rwlock)
75 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
76 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
77 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
79 static DEFINE_MUTEX(load_mutex);
80 #define LOAD_LOCK mutex_lock(&load_mutex)
81 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
83 static struct sidtab sidtab;
84 struct policydb policydb;
88 * The largest sequence number that has been used when
89 * providing an access decision to the access vector cache.
90 * The sequence number only changes when a policy change
93 static u32 latest_granting;
95 /* Forward declaration. */
96 static int context_struct_to_string(struct context *context, char **scontext,
100 * Return the boolean value of a constraint expression
101 * when it is applied to the specified source and target
104 * xcontext is a special beast... It is used by the validatetrans rules
105 * only. For these rules, scontext is the context before the transition,
106 * tcontext is the context after the transition, and xcontext is the context
107 * of the process performing the transition. All other callers of
108 * constraint_expr_eval should pass in NULL for xcontext.
110 static int constraint_expr_eval(struct context *scontext,
111 struct context *tcontext,
112 struct context *xcontext,
113 struct constraint_expr *cexpr)
117 struct role_datum *r1, *r2;
118 struct mls_level *l1, *l2;
119 struct constraint_expr *e;
120 int s[CEXPR_MAXDEPTH];
123 for (e = cexpr; e; e = e->next) {
124 switch (e->expr_type) {
140 if (sp == (CEXPR_MAXDEPTH-1))
144 val1 = scontext->user;
145 val2 = tcontext->user;
148 val1 = scontext->type;
149 val2 = tcontext->type;
152 val1 = scontext->role;
153 val2 = tcontext->role;
154 r1 = policydb.role_val_to_struct[val1 - 1];
155 r2 = policydb.role_val_to_struct[val2 - 1];
158 s[++sp] = ebitmap_get_bit(&r1->dominates,
162 s[++sp] = ebitmap_get_bit(&r2->dominates,
166 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
168 !ebitmap_get_bit(&r2->dominates,
176 l1 = &(scontext->range.level[0]);
177 l2 = &(tcontext->range.level[0]);
180 l1 = &(scontext->range.level[0]);
181 l2 = &(tcontext->range.level[1]);
184 l1 = &(scontext->range.level[1]);
185 l2 = &(tcontext->range.level[0]);
188 l1 = &(scontext->range.level[1]);
189 l2 = &(tcontext->range.level[1]);
192 l1 = &(scontext->range.level[0]);
193 l2 = &(scontext->range.level[1]);
196 l1 = &(tcontext->range.level[0]);
197 l2 = &(tcontext->range.level[1]);
202 s[++sp] = mls_level_eq(l1, l2);
205 s[++sp] = !mls_level_eq(l1, l2);
208 s[++sp] = mls_level_dom(l1, l2);
211 s[++sp] = mls_level_dom(l2, l1);
214 s[++sp] = mls_level_incomp(l2, l1);
228 s[++sp] = (val1 == val2);
231 s[++sp] = (val1 != val2);
239 if (sp == (CEXPR_MAXDEPTH-1))
242 if (e->attr & CEXPR_TARGET)
244 else if (e->attr & CEXPR_XTARGET) {
251 if (e->attr & CEXPR_USER)
253 else if (e->attr & CEXPR_ROLE)
255 else if (e->attr & CEXPR_TYPE)
264 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
267 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
285 * Compute access vectors based on a context structure pair for
286 * the permissions in a particular class.
288 static int context_struct_compute_av(struct context *scontext,
289 struct context *tcontext,
292 struct av_decision *avd)
294 struct constraint_node *constraint;
295 struct role_allow *ra;
296 struct avtab_key avkey;
297 struct avtab_node *node;
298 struct class_datum *tclass_datum;
299 struct ebitmap *sattr, *tattr;
300 struct ebitmap_node *snode, *tnode;
301 const struct selinux_class_perm *kdefs = &selinux_class_perm;
305 * Remap extended Netlink classes for old policy versions.
306 * Do this here rather than socket_type_to_security_class()
307 * in case a newer policy version is loaded, allowing sockets
308 * to remain in the correct class.
310 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
311 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
312 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
313 tclass = SECCLASS_NETLINK_SOCKET;
316 * Initialize the access vectors to the default values.
319 avd->decided = 0xffffffff;
321 avd->auditdeny = 0xffffffff;
322 avd->seqno = latest_granting;
325 * Check for all the invalid cases.
327 * - tclass > policy and > kernel
328 * - tclass > policy but is a userspace class
329 * - tclass > policy but we do not allow unknowns
331 if (unlikely(!tclass))
333 if (unlikely(tclass > policydb.p_classes.nprim))
334 if (tclass > kdefs->cts_len ||
335 !kdefs->class_to_string[tclass - 1] ||
336 !policydb.allow_unknown)
340 * Kernel class and we allow unknown so pad the allow decision
341 * the pad will be all 1 for unknown classes.
343 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
344 avd->allowed = policydb.undefined_perms[tclass - 1];
347 * Not in policy. Since decision is completed (all 1 or all 0) return.
349 if (unlikely(tclass > policydb.p_classes.nprim))
352 tclass_datum = policydb.class_val_to_struct[tclass - 1];
355 * If a specific type enforcement rule was defined for
356 * this permission check, then use it.
358 avkey.target_class = tclass;
359 avkey.specified = AVTAB_AV;
360 sattr = &policydb.type_attr_map[scontext->type - 1];
361 tattr = &policydb.type_attr_map[tcontext->type - 1];
362 ebitmap_for_each_positive_bit(sattr, snode, i) {
363 ebitmap_for_each_positive_bit(tattr, tnode, j) {
364 avkey.source_type = i + 1;
365 avkey.target_type = j + 1;
366 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
368 node = avtab_search_node_next(node, avkey.specified)) {
369 if (node->key.specified == AVTAB_ALLOWED)
370 avd->allowed |= node->datum.data;
371 else if (node->key.specified == AVTAB_AUDITALLOW)
372 avd->auditallow |= node->datum.data;
373 else if (node->key.specified == AVTAB_AUDITDENY)
374 avd->auditdeny &= node->datum.data;
377 /* Check conditional av table for additional permissions */
378 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
384 * Remove any permissions prohibited by a constraint (this includes
387 constraint = tclass_datum->constraints;
389 if ((constraint->permissions & (avd->allowed)) &&
390 !constraint_expr_eval(scontext, tcontext, NULL,
392 avd->allowed = (avd->allowed) & ~(constraint->permissions);
394 constraint = constraint->next;
398 * If checking process transition permission and the
399 * role is changing, then check the (current_role, new_role)
402 if (tclass == SECCLASS_PROCESS &&
403 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
404 scontext->role != tcontext->role) {
405 for (ra = policydb.role_allow; ra; ra = ra->next) {
406 if (scontext->role == ra->role &&
407 tcontext->role == ra->new_role)
411 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
412 PROCESS__DYNTRANSITION);
418 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
424 * Given a sid find if the type has the permissive flag set
426 int security_permissive_sid(u32 sid)
428 struct context *context;
434 context = sidtab_search(&sidtab, sid);
437 type = context->type;
439 * we are intentionally using type here, not type-1, the 0th bit may
440 * someday indicate that we are globally setting permissive in policy.
442 rc = ebitmap_get_bit(&policydb.permissive_map, type);
448 static int security_validtrans_handle_fail(struct context *ocontext,
449 struct context *ncontext,
450 struct context *tcontext,
453 char *o = NULL, *n = NULL, *t = NULL;
454 u32 olen, nlen, tlen;
456 if (context_struct_to_string(ocontext, &o, &olen) < 0)
458 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
460 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
462 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
463 "security_validate_transition: denied for"
464 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
465 o, n, t, policydb.p_class_val_to_name[tclass-1]);
471 if (!selinux_enforcing)
476 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
479 struct context *ocontext;
480 struct context *ncontext;
481 struct context *tcontext;
482 struct class_datum *tclass_datum;
483 struct constraint_node *constraint;
492 * Remap extended Netlink classes for old policy versions.
493 * Do this here rather than socket_type_to_security_class()
494 * in case a newer policy version is loaded, allowing sockets
495 * to remain in the correct class.
497 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
498 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
499 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
500 tclass = SECCLASS_NETLINK_SOCKET;
502 if (!tclass || tclass > policydb.p_classes.nprim) {
503 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
508 tclass_datum = policydb.class_val_to_struct[tclass - 1];
510 ocontext = sidtab_search(&sidtab, oldsid);
512 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
518 ncontext = sidtab_search(&sidtab, newsid);
520 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
526 tcontext = sidtab_search(&sidtab, tasksid);
528 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
534 constraint = tclass_datum->validatetrans;
536 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
538 rc = security_validtrans_handle_fail(ocontext, ncontext,
542 constraint = constraint->next;
551 * security_compute_av - Compute access vector decisions.
552 * @ssid: source security identifier
553 * @tsid: target security identifier
554 * @tclass: target security class
555 * @requested: requested permissions
556 * @avd: access vector decisions
558 * Compute a set of access vector decisions based on the
559 * SID pair (@ssid, @tsid) for the permissions in @tclass.
560 * Return -%EINVAL if any of the parameters are invalid or %0
561 * if the access vector decisions were computed successfully.
563 int security_compute_av(u32 ssid,
567 struct av_decision *avd)
569 struct context *scontext = NULL, *tcontext = NULL;
572 if (!ss_initialized) {
573 avd->allowed = 0xffffffff;
574 avd->decided = 0xffffffff;
576 avd->auditdeny = 0xffffffff;
577 avd->seqno = latest_granting;
583 scontext = sidtab_search(&sidtab, ssid);
585 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
590 tcontext = sidtab_search(&sidtab, tsid);
592 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
598 rc = context_struct_compute_av(scontext, tcontext, tclass,
606 * Write the security context string representation of
607 * the context structure `context' into a dynamically
608 * allocated string of the correct size. Set `*scontext'
609 * to point to this string and set `*scontext_len' to
610 * the length of the string.
612 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
620 *scontext_len = context->len;
621 *scontext = kstrdup(context->str, GFP_ATOMIC);
627 /* Compute the size of the context. */
628 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
629 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
630 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
631 *scontext_len += mls_compute_context_len(context);
633 /* Allocate space for the context; caller must free this space. */
634 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
637 *scontext = scontextp;
640 * Copy the user name, role name and type name into the context.
642 sprintf(scontextp, "%s:%s:%s",
643 policydb.p_user_val_to_name[context->user - 1],
644 policydb.p_role_val_to_name[context->role - 1],
645 policydb.p_type_val_to_name[context->type - 1]);
646 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
647 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
648 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
650 mls_sid_to_context(context, &scontextp);
657 #include "initial_sid_to_string.h"
659 const char *security_get_initial_sid_context(u32 sid)
661 if (unlikely(sid > SECINITSID_NUM))
663 return initial_sid_to_string[sid];
666 static int security_sid_to_context_core(u32 sid, char **scontext,
667 u32 *scontext_len, int force)
669 struct context *context;
675 if (!ss_initialized) {
676 if (sid <= SECINITSID_NUM) {
679 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
680 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
685 strcpy(scontextp, initial_sid_to_string[sid]);
686 *scontext = scontextp;
689 printk(KERN_ERR "SELinux: %s: called before initial "
690 "load_policy on unknown SID %d\n", __func__, sid);
696 context = sidtab_search_force(&sidtab, sid);
698 context = sidtab_search(&sidtab, sid);
700 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
705 rc = context_struct_to_string(context, scontext, scontext_len);
714 * security_sid_to_context - Obtain a context for a given SID.
715 * @sid: security identifier, SID
716 * @scontext: security context
717 * @scontext_len: length in bytes
719 * Write the string representation of the context associated with @sid
720 * into a dynamically allocated string of the correct size. Set @scontext
721 * to point to this string and set @scontext_len to the length of the string.
723 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
725 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
728 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
730 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
734 * Caveat: Mutates scontext.
736 static int string_to_context_struct(struct policydb *pol,
737 struct sidtab *sidtabp,
743 struct role_datum *role;
744 struct type_datum *typdatum;
745 struct user_datum *usrdatum;
746 char *scontextp, *p, oldc;
751 /* Parse the security context. */
754 scontextp = (char *) scontext;
756 /* Extract the user. */
758 while (*p && *p != ':')
766 usrdatum = hashtab_search(pol->p_users.table, scontextp);
770 ctx->user = usrdatum->value;
774 while (*p && *p != ':')
782 role = hashtab_search(pol->p_roles.table, scontextp);
785 ctx->role = role->value;
789 while (*p && *p != ':')
794 typdatum = hashtab_search(pol->p_types.table, scontextp);
798 ctx->type = typdatum->value;
800 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
804 if ((p - scontext) < scontext_len) {
809 /* Check the validity of the new context. */
810 if (!policydb_context_isvalid(pol, ctx)) {
812 context_destroy(ctx);
820 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
821 u32 *sid, u32 def_sid, gfp_t gfp_flags,
824 char *scontext2, *str = NULL;
825 struct context context;
828 if (!ss_initialized) {
831 for (i = 1; i < SECINITSID_NUM; i++) {
832 if (!strcmp(initial_sid_to_string[i], scontext)) {
837 *sid = SECINITSID_KERNEL;
842 /* Copy the string so that we can modify the copy as we parse it. */
843 scontext2 = kmalloc(scontext_len+1, gfp_flags);
846 memcpy(scontext2, scontext, scontext_len);
847 scontext2[scontext_len] = 0;
850 /* Save another copy for storing in uninterpreted form */
851 str = kstrdup(scontext2, gfp_flags);
859 rc = string_to_context_struct(&policydb, &sidtab,
860 scontext2, scontext_len,
862 if (rc == -EINVAL && force) {
864 context.len = scontext_len;
868 rc = sidtab_context_to_sid(&sidtab, &context, sid);
870 context_destroy(&context);
879 * security_context_to_sid - Obtain a SID for a given security context.
880 * @scontext: security context
881 * @scontext_len: length in bytes
882 * @sid: security identifier, SID
884 * Obtains a SID associated with the security context that
885 * has the string representation specified by @scontext.
886 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
887 * memory is available, or 0 on success.
889 int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
891 return security_context_to_sid_core(scontext, scontext_len,
892 sid, SECSID_NULL, GFP_KERNEL, 0);
896 * security_context_to_sid_default - Obtain a SID for a given security context,
897 * falling back to specified default if needed.
899 * @scontext: security context
900 * @scontext_len: length in bytes
901 * @sid: security identifier, SID
902 * @def_sid: default SID to assign on error
904 * Obtains a SID associated with the security context that
905 * has the string representation specified by @scontext.
906 * The default SID is passed to the MLS layer to be used to allow
907 * kernel labeling of the MLS field if the MLS field is not present
908 * (for upgrading to MLS without full relabel).
909 * Implicitly forces adding of the context even if it cannot be mapped yet.
910 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
911 * memory is available, or 0 on success.
913 int security_context_to_sid_default(const char *scontext, u32 scontext_len,
914 u32 *sid, u32 def_sid, gfp_t gfp_flags)
916 return security_context_to_sid_core(scontext, scontext_len,
917 sid, def_sid, gfp_flags, 1);
920 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
923 return security_context_to_sid_core(scontext, scontext_len,
924 sid, SECSID_NULL, GFP_KERNEL, 1);
927 static int compute_sid_handle_invalid_context(
928 struct context *scontext,
929 struct context *tcontext,
931 struct context *newcontext)
933 char *s = NULL, *t = NULL, *n = NULL;
934 u32 slen, tlen, nlen;
936 if (context_struct_to_string(scontext, &s, &slen) < 0)
938 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
940 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
942 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
943 "security_compute_sid: invalid context %s"
947 n, s, t, policydb.p_class_val_to_name[tclass-1]);
952 if (!selinux_enforcing)
957 static int security_compute_sid(u32 ssid,
963 struct context *scontext = NULL, *tcontext = NULL, newcontext;
964 struct role_trans *roletr = NULL;
965 struct avtab_key avkey;
966 struct avtab_datum *avdatum;
967 struct avtab_node *node;
970 if (!ss_initialized) {
972 case SECCLASS_PROCESS:
982 context_init(&newcontext);
986 scontext = sidtab_search(&sidtab, ssid);
988 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
993 tcontext = sidtab_search(&sidtab, tsid);
995 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1001 /* Set the user identity. */
1002 switch (specified) {
1003 case AVTAB_TRANSITION:
1005 /* Use the process user identity. */
1006 newcontext.user = scontext->user;
1009 /* Use the related object owner. */
1010 newcontext.user = tcontext->user;
1014 /* Set the role and type to default values. */
1016 case SECCLASS_PROCESS:
1017 /* Use the current role and type of process. */
1018 newcontext.role = scontext->role;
1019 newcontext.type = scontext->type;
1022 /* Use the well-defined object role. */
1023 newcontext.role = OBJECT_R_VAL;
1024 /* Use the type of the related object. */
1025 newcontext.type = tcontext->type;
1028 /* Look for a type transition/member/change rule. */
1029 avkey.source_type = scontext->type;
1030 avkey.target_type = tcontext->type;
1031 avkey.target_class = tclass;
1032 avkey.specified = specified;
1033 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1035 /* If no permanent rule, also check for enabled conditional rules */
1037 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1038 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
1039 if (node->key.specified & AVTAB_ENABLED) {
1040 avdatum = &node->datum;
1047 /* Use the type from the type transition/member/change rule. */
1048 newcontext.type = avdatum->data;
1051 /* Check for class-specific changes. */
1053 case SECCLASS_PROCESS:
1054 if (specified & AVTAB_TRANSITION) {
1055 /* Look for a role transition rule. */
1056 for (roletr = policydb.role_tr; roletr;
1057 roletr = roletr->next) {
1058 if (roletr->role == scontext->role &&
1059 roletr->type == tcontext->type) {
1060 /* Use the role transition rule. */
1061 newcontext.role = roletr->new_role;
1071 /* Set the MLS attributes.
1072 This is done last because it may allocate memory. */
1073 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1077 /* Check the validity of the context. */
1078 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1079 rc = compute_sid_handle_invalid_context(scontext,
1086 /* Obtain the sid for the context. */
1087 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1090 context_destroy(&newcontext);
1096 * security_transition_sid - Compute the SID for a new subject/object.
1097 * @ssid: source security identifier
1098 * @tsid: target security identifier
1099 * @tclass: target security class
1100 * @out_sid: security identifier for new subject/object
1102 * Compute a SID to use for labeling a new subject or object in the
1103 * class @tclass based on a SID pair (@ssid, @tsid).
1104 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1105 * if insufficient memory is available, or %0 if the new SID was
1106 * computed successfully.
1108 int security_transition_sid(u32 ssid,
1113 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1117 * security_member_sid - Compute the SID for member selection.
1118 * @ssid: source security identifier
1119 * @tsid: target security identifier
1120 * @tclass: target security class
1121 * @out_sid: security identifier for selected member
1123 * Compute a SID to use when selecting a member of a polyinstantiated
1124 * object of class @tclass based on a SID pair (@ssid, @tsid).
1125 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1126 * if insufficient memory is available, or %0 if the SID was
1127 * computed successfully.
1129 int security_member_sid(u32 ssid,
1134 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1138 * security_change_sid - Compute the SID for object relabeling.
1139 * @ssid: source security identifier
1140 * @tsid: target security identifier
1141 * @tclass: target security class
1142 * @out_sid: security identifier for selected member
1144 * Compute a SID to use for relabeling an object of class @tclass
1145 * based on a SID pair (@ssid, @tsid).
1146 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1147 * if insufficient memory is available, or %0 if the SID was
1148 * computed successfully.
1150 int security_change_sid(u32 ssid,
1155 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1159 * Verify that each kernel class that is defined in the
1162 static int validate_classes(struct policydb *p)
1165 struct class_datum *cladatum;
1166 struct perm_datum *perdatum;
1167 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1169 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1170 const char *def_class, *def_perm, *pol_class;
1171 struct symtab *perms;
1173 if (p->allow_unknown) {
1174 u32 num_classes = kdefs->cts_len;
1175 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1176 if (!p->undefined_perms)
1180 for (i = 1; i < kdefs->cts_len; i++) {
1181 def_class = kdefs->class_to_string[i];
1184 if (i > p->p_classes.nprim) {
1186 "SELinux: class %s not defined in policy\n",
1188 if (p->reject_unknown)
1190 if (p->allow_unknown)
1191 p->undefined_perms[i-1] = ~0U;
1194 pol_class = p->p_class_val_to_name[i-1];
1195 if (strcmp(pol_class, def_class)) {
1197 "SELinux: class %d is incorrect, found %s but should be %s\n",
1198 i, pol_class, def_class);
1202 for (i = 0; i < kdefs->av_pts_len; i++) {
1203 class_val = kdefs->av_perm_to_string[i].tclass;
1204 perm_val = kdefs->av_perm_to_string[i].value;
1205 def_perm = kdefs->av_perm_to_string[i].name;
1206 if (class_val > p->p_classes.nprim)
1208 pol_class = p->p_class_val_to_name[class_val-1];
1209 cladatum = hashtab_search(p->p_classes.table, pol_class);
1211 perms = &cladatum->permissions;
1212 nprim = 1 << (perms->nprim - 1);
1213 if (perm_val > nprim) {
1215 "SELinux: permission %s in class %s not defined in policy\n",
1216 def_perm, pol_class);
1217 if (p->reject_unknown)
1219 if (p->allow_unknown)
1220 p->undefined_perms[class_val-1] |= perm_val;
1223 perdatum = hashtab_search(perms->table, def_perm);
1224 if (perdatum == NULL) {
1226 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1227 def_perm, pol_class);
1230 pol_val = 1 << (perdatum->value - 1);
1231 if (pol_val != perm_val) {
1233 "SELinux: permission %s in class %s has incorrect value\n",
1234 def_perm, pol_class);
1238 for (i = 0; i < kdefs->av_inherit_len; i++) {
1239 class_val = kdefs->av_inherit[i].tclass;
1240 if (class_val > p->p_classes.nprim)
1242 pol_class = p->p_class_val_to_name[class_val-1];
1243 cladatum = hashtab_search(p->p_classes.table, pol_class);
1245 if (!cladatum->comdatum) {
1247 "SELinux: class %s should have an inherits clause but does not\n",
1251 tmp = kdefs->av_inherit[i].common_base;
1253 while (!(tmp & 0x01)) {
1257 perms = &cladatum->comdatum->permissions;
1258 for (j = 0; j < common_pts_len; j++) {
1259 def_perm = kdefs->av_inherit[i].common_pts[j];
1260 if (j >= perms->nprim) {
1262 "SELinux: permission %s in class %s not defined in policy\n",
1263 def_perm, pol_class);
1264 if (p->reject_unknown)
1266 if (p->allow_unknown)
1267 p->undefined_perms[class_val-1] |= (1 << j);
1270 perdatum = hashtab_search(perms->table, def_perm);
1271 if (perdatum == NULL) {
1273 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1274 def_perm, pol_class);
1277 if (perdatum->value != j + 1) {
1279 "SELinux: permission %s in class %s has incorrect value\n",
1280 def_perm, pol_class);
1288 /* Clone the SID into the new SID table. */
1289 static int clone_sid(u32 sid,
1290 struct context *context,
1293 struct sidtab *s = arg;
1295 return sidtab_insert(s, sid, context);
1298 static inline int convert_context_handle_invalid_context(struct context *context)
1302 if (selinux_enforcing) {
1308 if (!context_struct_to_string(context, &s, &len)) {
1310 "SELinux: Context %s would be invalid if enforcing\n",
1318 struct convert_context_args {
1319 struct policydb *oldp;
1320 struct policydb *newp;
1324 * Convert the values in the security context
1325 * structure `c' from the values specified
1326 * in the policy `p->oldp' to the values specified
1327 * in the policy `p->newp'. Verify that the
1328 * context is valid under the new policy.
1330 static int convert_context(u32 key,
1334 struct convert_context_args *args;
1335 struct context oldc;
1336 struct role_datum *role;
1337 struct type_datum *typdatum;
1338 struct user_datum *usrdatum;
1347 s = kstrdup(c->str, GFP_KERNEL);
1352 rc = string_to_context_struct(args->newp, NULL, s,
1353 c->len, &ctx, SECSID_NULL);
1357 "SELinux: Context %s became valid (mapped).\n",
1359 /* Replace string with mapped representation. */
1361 memcpy(c, &ctx, sizeof(*c));
1363 } else if (rc == -EINVAL) {
1364 /* Retain string representation for later mapping. */
1368 /* Other error condition, e.g. ENOMEM. */
1370 "SELinux: Unable to map context %s, rc = %d.\n",
1376 rc = context_cpy(&oldc, c);
1382 /* Convert the user. */
1383 usrdatum = hashtab_search(args->newp->p_users.table,
1384 args->oldp->p_user_val_to_name[c->user - 1]);
1387 c->user = usrdatum->value;
1389 /* Convert the role. */
1390 role = hashtab_search(args->newp->p_roles.table,
1391 args->oldp->p_role_val_to_name[c->role - 1]);
1394 c->role = role->value;
1396 /* Convert the type. */
1397 typdatum = hashtab_search(args->newp->p_types.table,
1398 args->oldp->p_type_val_to_name[c->type - 1]);
1401 c->type = typdatum->value;
1403 rc = mls_convert_context(args->oldp, args->newp, c);
1407 /* Check the validity of the new context. */
1408 if (!policydb_context_isvalid(args->newp, c)) {
1409 rc = convert_context_handle_invalid_context(&oldc);
1414 context_destroy(&oldc);
1419 /* Map old representation to string and save it. */
1420 if (context_struct_to_string(&oldc, &s, &len))
1422 context_destroy(&oldc);
1427 "SELinux: Context %s became invalid (unmapped).\n",
1433 static void security_load_policycaps(void)
1435 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1436 POLICYDB_CAPABILITY_NETPEER);
1437 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1438 POLICYDB_CAPABILITY_OPENPERM);
1441 extern void selinux_complete_init(void);
1442 static int security_preserve_bools(struct policydb *p);
1445 * security_load_policy - Load a security policy configuration.
1446 * @data: binary policy data
1447 * @len: length of data in bytes
1449 * Load a new set of security policy configuration data,
1450 * validate it and convert the SID table as necessary.
1451 * This function will flush the access vector cache after
1452 * loading the new policy.
1454 int security_load_policy(void *data, size_t len)
1456 struct policydb oldpolicydb, newpolicydb;
1457 struct sidtab oldsidtab, newsidtab;
1458 struct convert_context_args args;
1461 struct policy_file file = { data, len }, *fp = &file;
1465 if (!ss_initialized) {
1467 if (policydb_read(&policydb, fp)) {
1469 avtab_cache_destroy();
1472 if (policydb_load_isids(&policydb, &sidtab)) {
1474 policydb_destroy(&policydb);
1475 avtab_cache_destroy();
1478 /* Verify that the kernel defined classes are correct. */
1479 if (validate_classes(&policydb)) {
1481 "SELinux: the definition of a class is incorrect\n");
1483 sidtab_destroy(&sidtab);
1484 policydb_destroy(&policydb);
1485 avtab_cache_destroy();
1488 security_load_policycaps();
1489 policydb_loaded_version = policydb.policyvers;
1491 seqno = ++latest_granting;
1493 selinux_complete_init();
1494 avc_ss_reset(seqno);
1495 selnl_notify_policyload(seqno);
1496 selinux_netlbl_cache_invalidate();
1497 selinux_xfrm_notify_policyload();
1502 sidtab_hash_eval(&sidtab, "sids");
1505 if (policydb_read(&newpolicydb, fp)) {
1510 if (sidtab_init(&newsidtab)) {
1512 policydb_destroy(&newpolicydb);
1516 /* Verify that the kernel defined classes are correct. */
1517 if (validate_classes(&newpolicydb)) {
1519 "SELinux: the definition of a class is incorrect\n");
1524 rc = security_preserve_bools(&newpolicydb);
1526 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
1530 /* Clone the SID table. */
1531 sidtab_shutdown(&sidtab);
1532 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1538 * Convert the internal representations of contexts
1539 * in the new SID table.
1541 args.oldp = &policydb;
1542 args.newp = &newpolicydb;
1543 rc = sidtab_map(&newsidtab, convert_context, &args);
1547 /* Save the old policydb and SID table to free later. */
1548 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1549 sidtab_set(&oldsidtab, &sidtab);
1551 /* Install the new policydb and SID table. */
1553 memcpy(&policydb, &newpolicydb, sizeof policydb);
1554 sidtab_set(&sidtab, &newsidtab);
1555 security_load_policycaps();
1556 seqno = ++latest_granting;
1557 policydb_loaded_version = policydb.policyvers;
1561 /* Free the old policydb and SID table. */
1562 policydb_destroy(&oldpolicydb);
1563 sidtab_destroy(&oldsidtab);
1565 avc_ss_reset(seqno);
1566 selnl_notify_policyload(seqno);
1567 selinux_netlbl_cache_invalidate();
1568 selinux_xfrm_notify_policyload();
1574 sidtab_destroy(&newsidtab);
1575 policydb_destroy(&newpolicydb);
1581 * security_port_sid - Obtain the SID for a port.
1582 * @protocol: protocol number
1583 * @port: port number
1584 * @out_sid: security identifier
1586 int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
1593 c = policydb.ocontexts[OCON_PORT];
1595 if (c->u.port.protocol == protocol &&
1596 c->u.port.low_port <= port &&
1597 c->u.port.high_port >= port)
1604 rc = sidtab_context_to_sid(&sidtab,
1610 *out_sid = c->sid[0];
1612 *out_sid = SECINITSID_PORT;
1621 * security_netif_sid - Obtain the SID for a network interface.
1622 * @name: interface name
1623 * @if_sid: interface SID
1625 int security_netif_sid(char *name, u32 *if_sid)
1632 c = policydb.ocontexts[OCON_NETIF];
1634 if (strcmp(name, c->u.name) == 0)
1640 if (!c->sid[0] || !c->sid[1]) {
1641 rc = sidtab_context_to_sid(&sidtab,
1646 rc = sidtab_context_to_sid(&sidtab,
1652 *if_sid = c->sid[0];
1654 *if_sid = SECINITSID_NETIF;
1661 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1665 for (i = 0; i < 4; i++)
1666 if (addr[i] != (input[i] & mask[i])) {
1675 * security_node_sid - Obtain the SID for a node (host).
1676 * @domain: communication domain aka address family
1678 * @addrlen: address length in bytes
1679 * @out_sid: security identifier
1681 int security_node_sid(u16 domain,
1695 if (addrlen != sizeof(u32)) {
1700 addr = *((u32 *)addrp);
1702 c = policydb.ocontexts[OCON_NODE];
1704 if (c->u.node.addr == (addr & c->u.node.mask))
1712 if (addrlen != sizeof(u64) * 2) {
1716 c = policydb.ocontexts[OCON_NODE6];
1718 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1726 *out_sid = SECINITSID_NODE;
1732 rc = sidtab_context_to_sid(&sidtab,
1738 *out_sid = c->sid[0];
1740 *out_sid = SECINITSID_NODE;
1751 * security_get_user_sids - Obtain reachable SIDs for a user.
1752 * @fromsid: starting SID
1753 * @username: username
1754 * @sids: array of reachable SIDs for user
1755 * @nel: number of elements in @sids
1757 * Generate the set of SIDs for legal security contexts
1758 * for a given user that can be reached by @fromsid.
1759 * Set *@sids to point to a dynamically allocated
1760 * array containing the set of SIDs. Set *@nel to the
1761 * number of elements in the array.
1764 int security_get_user_sids(u32 fromsid,
1769 struct context *fromcon, usercon;
1770 u32 *mysids = NULL, *mysids2, sid;
1771 u32 mynel = 0, maxnel = SIDS_NEL;
1772 struct user_datum *user;
1773 struct role_datum *role;
1774 struct ebitmap_node *rnode, *tnode;
1780 if (!ss_initialized)
1785 context_init(&usercon);
1787 fromcon = sidtab_search(&sidtab, fromsid);
1793 user = hashtab_search(policydb.p_users.table, username);
1798 usercon.user = user->value;
1800 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1806 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
1807 role = policydb.role_val_to_struct[i];
1809 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
1812 if (mls_setup_user_range(fromcon, user, &usercon))
1815 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1818 if (mynel < maxnel) {
1819 mysids[mynel++] = sid;
1822 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1827 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1830 mysids[mynel++] = sid;
1842 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1848 for (i = 0, j = 0; i < mynel; i++) {
1849 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1851 PROCESS__TRANSITION, AVC_STRICT,
1854 mysids2[j++] = mysids[i];
1866 * security_genfs_sid - Obtain a SID for a file in a filesystem
1867 * @fstype: filesystem type
1868 * @path: path from root of mount
1869 * @sclass: file security class
1870 * @sid: SID for path
1872 * Obtain a SID to use for a file in a filesystem that
1873 * cannot support xattr or use a fixed labeling behavior like
1874 * transition SIDs or task SIDs.
1876 int security_genfs_sid(const char *fstype,
1882 struct genfs *genfs;
1884 int rc = 0, cmp = 0;
1886 while (path[0] == '/' && path[1] == '/')
1891 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1892 cmp = strcmp(fstype, genfs->fstype);
1897 if (!genfs || cmp) {
1898 *sid = SECINITSID_UNLABELED;
1903 for (c = genfs->head; c; c = c->next) {
1904 len = strlen(c->u.name);
1905 if ((!c->v.sclass || sclass == c->v.sclass) &&
1906 (strncmp(c->u.name, path, len) == 0))
1911 *sid = SECINITSID_UNLABELED;
1917 rc = sidtab_context_to_sid(&sidtab,
1931 * security_fs_use - Determine how to handle labeling for a filesystem.
1932 * @fstype: filesystem type
1933 * @behavior: labeling behavior
1934 * @sid: SID for filesystem (superblock)
1936 int security_fs_use(
1938 unsigned int *behavior,
1946 c = policydb.ocontexts[OCON_FSUSE];
1948 if (strcmp(fstype, c->u.name) == 0)
1954 *behavior = c->v.behavior;
1956 rc = sidtab_context_to_sid(&sidtab,
1964 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1966 *behavior = SECURITY_FS_USE_NONE;
1969 *behavior = SECURITY_FS_USE_GENFS;
1978 int security_get_bools(int *len, char ***names, int **values)
1980 int i, rc = -ENOMEM;
1986 *len = policydb.p_bools.nprim;
1992 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
1996 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2000 for (i = 0; i < *len; i++) {
2002 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2003 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
2004 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
2007 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2008 (*names)[i][name_len - 1] = 0;
2016 for (i = 0; i < *len; i++)
2024 int security_set_bools(int len, int *values)
2027 int lenp, seqno = 0;
2028 struct cond_node *cur;
2032 lenp = policydb.p_bools.nprim;
2038 for (i = 0; i < len; i++) {
2039 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2040 audit_log(current->audit_context, GFP_ATOMIC,
2041 AUDIT_MAC_CONFIG_CHANGE,
2042 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2043 policydb.p_bool_val_to_name[i],
2045 policydb.bool_val_to_struct[i]->state,
2046 audit_get_loginuid(current),
2047 audit_get_sessionid(current));
2050 policydb.bool_val_to_struct[i]->state = 1;
2052 policydb.bool_val_to_struct[i]->state = 0;
2055 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2056 rc = evaluate_cond_node(&policydb, cur);
2061 seqno = ++latest_granting;
2066 avc_ss_reset(seqno);
2067 selnl_notify_policyload(seqno);
2068 selinux_xfrm_notify_policyload();
2073 int security_get_bool_value(int bool)
2080 len = policydb.p_bools.nprim;
2086 rc = policydb.bool_val_to_struct[bool]->state;
2092 static int security_preserve_bools(struct policydb *p)
2094 int rc, nbools = 0, *bvalues = NULL, i;
2095 char **bnames = NULL;
2096 struct cond_bool_datum *booldatum;
2097 struct cond_node *cur;
2099 rc = security_get_bools(&nbools, &bnames, &bvalues);
2102 for (i = 0; i < nbools; i++) {
2103 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2105 booldatum->state = bvalues[i];
2107 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2108 rc = evaluate_cond_node(p, cur);
2115 for (i = 0; i < nbools; i++)
2124 * security_sid_mls_copy() - computes a new sid based on the given
2125 * sid and the mls portion of mls_sid.
2127 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2129 struct context *context1;
2130 struct context *context2;
2131 struct context newcon;
2136 if (!ss_initialized || !selinux_mls_enabled) {
2141 context_init(&newcon);
2144 context1 = sidtab_search(&sidtab, sid);
2146 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2152 context2 = sidtab_search(&sidtab, mls_sid);
2154 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2160 newcon.user = context1->user;
2161 newcon.role = context1->role;
2162 newcon.type = context1->type;
2163 rc = mls_context_cpy(&newcon, context2);
2167 /* Check the validity of the new context. */
2168 if (!policydb_context_isvalid(&policydb, &newcon)) {
2169 rc = convert_context_handle_invalid_context(&newcon);
2174 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2178 if (!context_struct_to_string(&newcon, &s, &len)) {
2179 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2180 "security_sid_mls_copy: invalid context %s", s);
2186 context_destroy(&newcon);
2192 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2193 * @nlbl_sid: NetLabel SID
2194 * @nlbl_type: NetLabel labeling protocol type
2195 * @xfrm_sid: XFRM SID
2198 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2199 * resolved into a single SID it is returned via @peer_sid and the function
2200 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2201 * returns a negative value. A table summarizing the behavior is below:
2203 * | function return | @sid
2204 * ------------------------------+-----------------+-----------------
2205 * no peer labels | 0 | SECSID_NULL
2206 * single peer label | 0 | <peer_label>
2207 * multiple, consistent labels | 0 | <peer_label>
2208 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2211 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2216 struct context *nlbl_ctx;
2217 struct context *xfrm_ctx;
2219 /* handle the common (which also happens to be the set of easy) cases
2220 * right away, these two if statements catch everything involving a
2221 * single or absent peer SID/label */
2222 if (xfrm_sid == SECSID_NULL) {
2223 *peer_sid = nlbl_sid;
2226 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2227 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2229 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2230 *peer_sid = xfrm_sid;
2234 /* we don't need to check ss_initialized here since the only way both
2235 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2236 * security server was initialized and ss_initialized was true */
2237 if (!selinux_mls_enabled) {
2238 *peer_sid = SECSID_NULL;
2244 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2246 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2247 __func__, nlbl_sid);
2251 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2253 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2254 __func__, xfrm_sid);
2258 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2263 /* at present NetLabel SIDs/labels really only carry MLS
2264 * information so if the MLS portion of the NetLabel SID
2265 * matches the MLS portion of the labeled XFRM SID/label
2266 * then pass along the XFRM SID as it is the most
2268 *peer_sid = xfrm_sid;
2270 *peer_sid = SECSID_NULL;
2274 static int get_classes_callback(void *k, void *d, void *args)
2276 struct class_datum *datum = d;
2277 char *name = k, **classes = args;
2278 int value = datum->value - 1;
2280 classes[value] = kstrdup(name, GFP_ATOMIC);
2281 if (!classes[value])
2287 int security_get_classes(char ***classes, int *nclasses)
2293 *nclasses = policydb.p_classes.nprim;
2294 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2298 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2302 for (i = 0; i < *nclasses; i++)
2303 kfree((*classes)[i]);
2312 static int get_permissions_callback(void *k, void *d, void *args)
2314 struct perm_datum *datum = d;
2315 char *name = k, **perms = args;
2316 int value = datum->value - 1;
2318 perms[value] = kstrdup(name, GFP_ATOMIC);
2325 int security_get_permissions(char *class, char ***perms, int *nperms)
2327 int rc = -ENOMEM, i;
2328 struct class_datum *match;
2332 match = hashtab_search(policydb.p_classes.table, class);
2334 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
2340 *nperms = match->permissions.nprim;
2341 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2345 if (match->comdatum) {
2346 rc = hashtab_map(match->comdatum->permissions.table,
2347 get_permissions_callback, *perms);
2352 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2363 for (i = 0; i < *nperms; i++)
2369 int security_get_reject_unknown(void)
2371 return policydb.reject_unknown;
2374 int security_get_allow_unknown(void)
2376 return policydb.allow_unknown;
2380 * security_policycap_supported - Check for a specific policy capability
2381 * @req_cap: capability
2384 * This function queries the currently loaded policy to see if it supports the
2385 * capability specified by @req_cap. Returns true (1) if the capability is
2386 * supported, false (0) if it isn't supported.
2389 int security_policycap_supported(unsigned int req_cap)
2394 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2400 struct selinux_audit_rule {
2402 struct context au_ctxt;
2405 void selinux_audit_rule_free(void *vrule)
2407 struct selinux_audit_rule *rule = vrule;
2410 context_destroy(&rule->au_ctxt);
2415 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2417 struct selinux_audit_rule *tmprule;
2418 struct role_datum *roledatum;
2419 struct type_datum *typedatum;
2420 struct user_datum *userdatum;
2421 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
2426 if (!ss_initialized)
2430 case AUDIT_SUBJ_USER:
2431 case AUDIT_SUBJ_ROLE:
2432 case AUDIT_SUBJ_TYPE:
2433 case AUDIT_OBJ_USER:
2434 case AUDIT_OBJ_ROLE:
2435 case AUDIT_OBJ_TYPE:
2436 /* only 'equals' and 'not equals' fit user, role, and type */
2437 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2440 case AUDIT_SUBJ_SEN:
2441 case AUDIT_SUBJ_CLR:
2442 case AUDIT_OBJ_LEV_LOW:
2443 case AUDIT_OBJ_LEV_HIGH:
2444 /* we do not allow a range, indicated by the presense of '-' */
2445 if (strchr(rulestr, '-'))
2449 /* only the above fields are valid */
2453 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2457 context_init(&tmprule->au_ctxt);
2461 tmprule->au_seqno = latest_granting;
2464 case AUDIT_SUBJ_USER:
2465 case AUDIT_OBJ_USER:
2466 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2470 tmprule->au_ctxt.user = userdatum->value;
2472 case AUDIT_SUBJ_ROLE:
2473 case AUDIT_OBJ_ROLE:
2474 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2478 tmprule->au_ctxt.role = roledatum->value;
2480 case AUDIT_SUBJ_TYPE:
2481 case AUDIT_OBJ_TYPE:
2482 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2486 tmprule->au_ctxt.type = typedatum->value;
2488 case AUDIT_SUBJ_SEN:
2489 case AUDIT_SUBJ_CLR:
2490 case AUDIT_OBJ_LEV_LOW:
2491 case AUDIT_OBJ_LEV_HIGH:
2492 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2499 selinux_audit_rule_free(tmprule);
2508 /* Check to see if the rule contains any selinux fields */
2509 int selinux_audit_rule_known(struct audit_krule *rule)
2513 for (i = 0; i < rule->field_count; i++) {
2514 struct audit_field *f = &rule->fields[i];
2516 case AUDIT_SUBJ_USER:
2517 case AUDIT_SUBJ_ROLE:
2518 case AUDIT_SUBJ_TYPE:
2519 case AUDIT_SUBJ_SEN:
2520 case AUDIT_SUBJ_CLR:
2521 case AUDIT_OBJ_USER:
2522 case AUDIT_OBJ_ROLE:
2523 case AUDIT_OBJ_TYPE:
2524 case AUDIT_OBJ_LEV_LOW:
2525 case AUDIT_OBJ_LEV_HIGH:
2533 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
2534 struct audit_context *actx)
2536 struct context *ctxt;
2537 struct mls_level *level;
2538 struct selinux_audit_rule *rule = vrule;
2542 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2543 "selinux_audit_rule_match: missing rule\n");
2549 if (rule->au_seqno < latest_granting) {
2550 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2551 "selinux_audit_rule_match: stale rule\n");
2556 ctxt = sidtab_search(&sidtab, sid);
2558 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2559 "selinux_audit_rule_match: unrecognized SID %d\n",
2565 /* a field/op pair that is not caught here will simply fall through
2568 case AUDIT_SUBJ_USER:
2569 case AUDIT_OBJ_USER:
2572 match = (ctxt->user == rule->au_ctxt.user);
2574 case AUDIT_NOT_EQUAL:
2575 match = (ctxt->user != rule->au_ctxt.user);
2579 case AUDIT_SUBJ_ROLE:
2580 case AUDIT_OBJ_ROLE:
2583 match = (ctxt->role == rule->au_ctxt.role);
2585 case AUDIT_NOT_EQUAL:
2586 match = (ctxt->role != rule->au_ctxt.role);
2590 case AUDIT_SUBJ_TYPE:
2591 case AUDIT_OBJ_TYPE:
2594 match = (ctxt->type == rule->au_ctxt.type);
2596 case AUDIT_NOT_EQUAL:
2597 match = (ctxt->type != rule->au_ctxt.type);
2601 case AUDIT_SUBJ_SEN:
2602 case AUDIT_SUBJ_CLR:
2603 case AUDIT_OBJ_LEV_LOW:
2604 case AUDIT_OBJ_LEV_HIGH:
2605 level = ((field == AUDIT_SUBJ_SEN ||
2606 field == AUDIT_OBJ_LEV_LOW) ?
2607 &ctxt->range.level[0] : &ctxt->range.level[1]);
2610 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2613 case AUDIT_NOT_EQUAL:
2614 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2617 case AUDIT_LESS_THAN:
2618 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2620 !mls_level_eq(&rule->au_ctxt.range.level[0],
2623 case AUDIT_LESS_THAN_OR_EQUAL:
2624 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2627 case AUDIT_GREATER_THAN:
2628 match = (mls_level_dom(level,
2629 &rule->au_ctxt.range.level[0]) &&
2630 !mls_level_eq(level,
2631 &rule->au_ctxt.range.level[0]));
2633 case AUDIT_GREATER_THAN_OR_EQUAL:
2634 match = mls_level_dom(level,
2635 &rule->au_ctxt.range.level[0]);
2645 static int (*aurule_callback)(void) = audit_update_lsm_rules;
2647 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2648 u16 class, u32 perms, u32 *retained)
2652 if (event == AVC_CALLBACK_RESET && aurule_callback)
2653 err = aurule_callback();
2657 static int __init aurule_init(void)
2661 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2662 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2664 panic("avc_add_callback() failed, error %d\n", err);
2668 __initcall(aurule_init);
2670 #ifdef CONFIG_NETLABEL
2672 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2673 * @secattr: the NetLabel packet security attributes
2674 * @sid: the SELinux SID
2677 * Attempt to cache the context in @ctx, which was derived from the packet in
2678 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2679 * already been initialized.
2682 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2687 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2688 if (sid_cache == NULL)
2690 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2691 if (secattr->cache == NULL) {
2697 secattr->cache->free = kfree;
2698 secattr->cache->data = sid_cache;
2699 secattr->flags |= NETLBL_SECATTR_CACHE;
2703 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2704 * @secattr: the NetLabel packet security attributes
2705 * @sid: the SELinux SID
2708 * Convert the given NetLabel security attributes in @secattr into a
2709 * SELinux SID. If the @secattr field does not contain a full SELinux
2710 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2711 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2712 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2713 * conversion for future lookups. Returns zero on success, negative values on
2717 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2721 struct context *ctx;
2722 struct context ctx_new;
2724 if (!ss_initialized) {
2731 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2732 *sid = *(u32 *)secattr->cache->data;
2734 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2735 *sid = secattr->attr.secid;
2737 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2738 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
2740 goto netlbl_secattr_to_sid_return;
2742 ctx_new.user = ctx->user;
2743 ctx_new.role = ctx->role;
2744 ctx_new.type = ctx->type;
2745 mls_import_netlbl_lvl(&ctx_new, secattr);
2746 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2747 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2748 secattr->attr.mls.cat) != 0)
2749 goto netlbl_secattr_to_sid_return;
2750 ctx_new.range.level[1].cat.highbit =
2751 ctx_new.range.level[0].cat.highbit;
2752 ctx_new.range.level[1].cat.node =
2753 ctx_new.range.level[0].cat.node;
2755 ebitmap_init(&ctx_new.range.level[0].cat);
2756 ebitmap_init(&ctx_new.range.level[1].cat);
2758 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2759 goto netlbl_secattr_to_sid_return_cleanup;
2761 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2763 goto netlbl_secattr_to_sid_return_cleanup;
2765 security_netlbl_cache_add(secattr, *sid);
2767 ebitmap_destroy(&ctx_new.range.level[0].cat);
2773 netlbl_secattr_to_sid_return:
2776 netlbl_secattr_to_sid_return_cleanup:
2777 ebitmap_destroy(&ctx_new.range.level[0].cat);
2778 goto netlbl_secattr_to_sid_return;
2782 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2783 * @sid: the SELinux SID
2784 * @secattr: the NetLabel packet security attributes
2787 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2788 * Returns zero on success, negative values on failure.
2791 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2794 struct context *ctx;
2796 if (!ss_initialized)
2800 ctx = sidtab_search(&sidtab, sid);
2802 goto netlbl_sid_to_secattr_failure;
2803 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2805 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
2806 mls_export_netlbl_lvl(ctx, secattr);
2807 rc = mls_export_netlbl_cat(ctx, secattr);
2809 goto netlbl_sid_to_secattr_failure;
2814 netlbl_sid_to_secattr_failure:
2818 #endif /* CONFIG_NETLABEL */