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
20 * Updated: Chad Sellers <csellers@tresys.com>
22 * Added validation of kernel classes and permissions
24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/spinlock.h>
36 #include <linux/rcupdate.h>
37 #include <linux/errno.h>
39 #include <linux/sched.h>
40 #include <linux/audit.h>
41 #include <linux/mutex.h>
42 #include <net/netlabel.h>
52 #include "conditional.h"
59 extern void selnl_notify_policyload(u32 seqno);
60 unsigned int policydb_loaded_version;
63 * This is declared in avc.c
65 extern const struct selinux_class_perm selinux_class_perm;
67 static DEFINE_RWLOCK(policy_rwlock);
68 #define POLICY_RDLOCK read_lock(&policy_rwlock)
69 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
70 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
71 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
73 static DEFINE_MUTEX(load_mutex);
74 #define LOAD_LOCK mutex_lock(&load_mutex)
75 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
77 static struct sidtab sidtab;
78 struct policydb policydb;
79 int ss_initialized = 0;
82 * The largest sequence number that has been used when
83 * providing an access decision to the access vector cache.
84 * The sequence number only changes when a policy change
87 static u32 latest_granting = 0;
89 /* Forward declaration. */
90 static int context_struct_to_string(struct context *context, char **scontext,
94 * Return the boolean value of a constraint expression
95 * when it is applied to the specified source and target
98 * xcontext is a special beast... It is used by the validatetrans rules
99 * only. For these rules, scontext is the context before the transition,
100 * tcontext is the context after the transition, and xcontext is the context
101 * of the process performing the transition. All other callers of
102 * constraint_expr_eval should pass in NULL for xcontext.
104 static int constraint_expr_eval(struct context *scontext,
105 struct context *tcontext,
106 struct context *xcontext,
107 struct constraint_expr *cexpr)
111 struct role_datum *r1, *r2;
112 struct mls_level *l1, *l2;
113 struct constraint_expr *e;
114 int s[CEXPR_MAXDEPTH];
117 for (e = cexpr; e; e = e->next) {
118 switch (e->expr_type) {
134 if (sp == (CEXPR_MAXDEPTH-1))
138 val1 = scontext->user;
139 val2 = tcontext->user;
142 val1 = scontext->type;
143 val2 = tcontext->type;
146 val1 = scontext->role;
147 val2 = tcontext->role;
148 r1 = policydb.role_val_to_struct[val1 - 1];
149 r2 = policydb.role_val_to_struct[val2 - 1];
152 s[++sp] = ebitmap_get_bit(&r1->dominates,
156 s[++sp] = ebitmap_get_bit(&r2->dominates,
160 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
162 !ebitmap_get_bit(&r2->dominates,
170 l1 = &(scontext->range.level[0]);
171 l2 = &(tcontext->range.level[0]);
174 l1 = &(scontext->range.level[0]);
175 l2 = &(tcontext->range.level[1]);
178 l1 = &(scontext->range.level[1]);
179 l2 = &(tcontext->range.level[0]);
182 l1 = &(scontext->range.level[1]);
183 l2 = &(tcontext->range.level[1]);
186 l1 = &(scontext->range.level[0]);
187 l2 = &(scontext->range.level[1]);
190 l1 = &(tcontext->range.level[0]);
191 l2 = &(tcontext->range.level[1]);
196 s[++sp] = mls_level_eq(l1, l2);
199 s[++sp] = !mls_level_eq(l1, l2);
202 s[++sp] = mls_level_dom(l1, l2);
205 s[++sp] = mls_level_dom(l2, l1);
208 s[++sp] = mls_level_incomp(l2, l1);
222 s[++sp] = (val1 == val2);
225 s[++sp] = (val1 != val2);
233 if (sp == (CEXPR_MAXDEPTH-1))
236 if (e->attr & CEXPR_TARGET)
238 else if (e->attr & CEXPR_XTARGET) {
245 if (e->attr & CEXPR_USER)
247 else if (e->attr & CEXPR_ROLE)
249 else if (e->attr & CEXPR_TYPE)
258 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
261 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
279 * Compute access vectors based on a context structure pair for
280 * the permissions in a particular class.
282 static int context_struct_compute_av(struct context *scontext,
283 struct context *tcontext,
286 struct av_decision *avd)
288 struct constraint_node *constraint;
289 struct role_allow *ra;
290 struct avtab_key avkey;
291 struct avtab_node *node;
292 struct class_datum *tclass_datum;
293 struct ebitmap *sattr, *tattr;
294 struct ebitmap_node *snode, *tnode;
295 const struct selinux_class_perm *kdefs = &selinux_class_perm;
299 * Remap extended Netlink classes for old policy versions.
300 * Do this here rather than socket_type_to_security_class()
301 * in case a newer policy version is loaded, allowing sockets
302 * to remain in the correct class.
304 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
305 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
306 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
307 tclass = SECCLASS_NETLINK_SOCKET;
310 * Initialize the access vectors to the default values.
313 avd->decided = 0xffffffff;
315 avd->auditdeny = 0xffffffff;
316 avd->seqno = latest_granting;
319 * Check for all the invalid cases.
321 * - tclass > policy and > kernel
322 * - tclass > policy but is a userspace class
323 * - tclass > policy but we do not allow unknowns
325 if (unlikely(!tclass))
327 if (unlikely(tclass > policydb.p_classes.nprim))
328 if (tclass > kdefs->cts_len ||
329 !kdefs->class_to_string[tclass - 1] ||
330 !policydb.allow_unknown)
334 * Kernel class and we allow unknown so pad the allow decision
335 * the pad will be all 1 for unknown classes.
337 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
338 avd->allowed = policydb.undefined_perms[tclass - 1];
341 * Not in policy. Since decision is completed (all 1 or all 0) return.
343 if (unlikely(tclass > policydb.p_classes.nprim))
346 tclass_datum = policydb.class_val_to_struct[tclass - 1];
349 * If a specific type enforcement rule was defined for
350 * this permission check, then use it.
352 avkey.target_class = tclass;
353 avkey.specified = AVTAB_AV;
354 sattr = &policydb.type_attr_map[scontext->type - 1];
355 tattr = &policydb.type_attr_map[tcontext->type - 1];
356 ebitmap_for_each_positive_bit(sattr, snode, i) {
357 ebitmap_for_each_positive_bit(tattr, tnode, j) {
358 avkey.source_type = i + 1;
359 avkey.target_type = j + 1;
360 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
362 node = avtab_search_node_next(node, avkey.specified)) {
363 if (node->key.specified == AVTAB_ALLOWED)
364 avd->allowed |= node->datum.data;
365 else if (node->key.specified == AVTAB_AUDITALLOW)
366 avd->auditallow |= node->datum.data;
367 else if (node->key.specified == AVTAB_AUDITDENY)
368 avd->auditdeny &= node->datum.data;
371 /* Check conditional av table for additional permissions */
372 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
378 * Remove any permissions prohibited by a constraint (this includes
381 constraint = tclass_datum->constraints;
383 if ((constraint->permissions & (avd->allowed)) &&
384 !constraint_expr_eval(scontext, tcontext, NULL,
386 avd->allowed = (avd->allowed) & ~(constraint->permissions);
388 constraint = constraint->next;
392 * If checking process transition permission and the
393 * role is changing, then check the (current_role, new_role)
396 if (tclass == SECCLASS_PROCESS &&
397 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
398 scontext->role != tcontext->role) {
399 for (ra = policydb.role_allow; ra; ra = ra->next) {
400 if (scontext->role == ra->role &&
401 tcontext->role == ra->new_role)
405 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
406 PROCESS__DYNTRANSITION);
412 printk(KERN_ERR "%s: unrecognized class %d\n", __FUNCTION__, tclass);
416 static int security_validtrans_handle_fail(struct context *ocontext,
417 struct context *ncontext,
418 struct context *tcontext,
421 char *o = NULL, *n = NULL, *t = NULL;
422 u32 olen, nlen, tlen;
424 if (context_struct_to_string(ocontext, &o, &olen) < 0)
426 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
428 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
430 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
431 "security_validate_transition: denied for"
432 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
433 o, n, t, policydb.p_class_val_to_name[tclass-1]);
439 if (!selinux_enforcing)
444 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
447 struct context *ocontext;
448 struct context *ncontext;
449 struct context *tcontext;
450 struct class_datum *tclass_datum;
451 struct constraint_node *constraint;
460 * Remap extended Netlink classes for old policy versions.
461 * Do this here rather than socket_type_to_security_class()
462 * in case a newer policy version is loaded, allowing sockets
463 * to remain in the correct class.
465 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
466 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
467 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
468 tclass = SECCLASS_NETLINK_SOCKET;
470 if (!tclass || tclass > policydb.p_classes.nprim) {
471 printk(KERN_ERR "security_validate_transition: "
472 "unrecognized class %d\n", tclass);
476 tclass_datum = policydb.class_val_to_struct[tclass - 1];
478 ocontext = sidtab_search(&sidtab, oldsid);
480 printk(KERN_ERR "security_validate_transition: "
481 " unrecognized SID %d\n", oldsid);
486 ncontext = sidtab_search(&sidtab, newsid);
488 printk(KERN_ERR "security_validate_transition: "
489 " unrecognized SID %d\n", newsid);
494 tcontext = sidtab_search(&sidtab, tasksid);
496 printk(KERN_ERR "security_validate_transition: "
497 " unrecognized SID %d\n", tasksid);
502 constraint = tclass_datum->validatetrans;
504 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
506 rc = security_validtrans_handle_fail(ocontext, ncontext,
510 constraint = constraint->next;
519 * security_compute_av - Compute access vector decisions.
520 * @ssid: source security identifier
521 * @tsid: target security identifier
522 * @tclass: target security class
523 * @requested: requested permissions
524 * @avd: access vector decisions
526 * Compute a set of access vector decisions based on the
527 * SID pair (@ssid, @tsid) for the permissions in @tclass.
528 * Return -%EINVAL if any of the parameters are invalid or %0
529 * if the access vector decisions were computed successfully.
531 int security_compute_av(u32 ssid,
535 struct av_decision *avd)
537 struct context *scontext = NULL, *tcontext = NULL;
540 if (!ss_initialized) {
541 avd->allowed = 0xffffffff;
542 avd->decided = 0xffffffff;
544 avd->auditdeny = 0xffffffff;
545 avd->seqno = latest_granting;
551 scontext = sidtab_search(&sidtab, ssid);
553 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
558 tcontext = sidtab_search(&sidtab, tsid);
560 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
566 rc = context_struct_compute_av(scontext, tcontext, tclass,
574 * Write the security context string representation of
575 * the context structure `context' into a dynamically
576 * allocated string of the correct size. Set `*scontext'
577 * to point to this string and set `*scontext_len' to
578 * the length of the string.
580 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
587 /* Compute the size of the context. */
588 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
589 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
590 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
591 *scontext_len += mls_compute_context_len(context);
593 /* Allocate space for the context; caller must free this space. */
594 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
598 *scontext = scontextp;
601 * Copy the user name, role name and type name into the context.
603 sprintf(scontextp, "%s:%s:%s",
604 policydb.p_user_val_to_name[context->user - 1],
605 policydb.p_role_val_to_name[context->role - 1],
606 policydb.p_type_val_to_name[context->type - 1]);
607 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
608 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
609 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
611 mls_sid_to_context(context, &scontextp);
618 #include "initial_sid_to_string.h"
620 const char *security_get_initial_sid_context(u32 sid)
622 if (unlikely(sid > SECINITSID_NUM))
624 return initial_sid_to_string[sid];
628 * security_sid_to_context - Obtain a context for a given SID.
629 * @sid: security identifier, SID
630 * @scontext: security context
631 * @scontext_len: length in bytes
633 * Write the string representation of the context associated with @sid
634 * into a dynamically allocated string of the correct size. Set @scontext
635 * to point to this string and set @scontext_len to the length of the string.
637 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
639 struct context *context;
645 if (!ss_initialized) {
646 if (sid <= SECINITSID_NUM) {
649 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
650 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
655 strcpy(scontextp, initial_sid_to_string[sid]);
656 *scontext = scontextp;
659 printk(KERN_ERR "security_sid_to_context: called before initial "
660 "load_policy on unknown SID %d\n", sid);
665 context = sidtab_search(&sidtab, sid);
667 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
672 rc = context_struct_to_string(context, scontext, scontext_len);
680 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
683 struct context context;
684 struct role_datum *role;
685 struct type_datum *typdatum;
686 struct user_datum *usrdatum;
687 char *scontextp, *p, oldc;
690 if (!ss_initialized) {
693 for (i = 1; i < SECINITSID_NUM; i++) {
694 if (!strcmp(initial_sid_to_string[i], scontext)) {
699 *sid = SECINITSID_KERNEL;
704 /* Copy the string so that we can modify the copy as we parse it.
705 The string should already by null terminated, but we append a
706 null suffix to the copy to avoid problems with the existing
707 attr package, which doesn't view the null terminator as part
708 of the attribute value. */
709 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
714 memcpy(scontext2, scontext, scontext_len);
715 scontext2[scontext_len] = 0;
717 context_init(&context);
722 /* Parse the security context. */
725 scontextp = (char *) scontext2;
727 /* Extract the user. */
729 while (*p && *p != ':')
737 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
741 context.user = usrdatum->value;
745 while (*p && *p != ':')
753 role = hashtab_search(policydb.p_roles.table, scontextp);
756 context.role = role->value;
760 while (*p && *p != ':')
765 typdatum = hashtab_search(policydb.p_types.table, scontextp);
769 context.type = typdatum->value;
771 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
775 if ((p - scontext2) < scontext_len) {
780 /* Check the validity of the new context. */
781 if (!policydb_context_isvalid(&policydb, &context)) {
785 /* Obtain the new sid. */
786 rc = sidtab_context_to_sid(&sidtab, &context, sid);
789 context_destroy(&context);
796 * security_context_to_sid - Obtain a SID for a given security context.
797 * @scontext: security context
798 * @scontext_len: length in bytes
799 * @sid: security identifier, SID
801 * Obtains a SID associated with the security context that
802 * has the string representation specified by @scontext.
803 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
804 * memory is available, or 0 on success.
806 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
808 return security_context_to_sid_core(scontext, scontext_len,
813 * security_context_to_sid_default - Obtain a SID for a given security context,
814 * falling back to specified default if needed.
816 * @scontext: security context
817 * @scontext_len: length in bytes
818 * @sid: security identifier, SID
819 * @def_sid: default SID to assign on error
821 * Obtains a SID associated with the security context that
822 * has the string representation specified by @scontext.
823 * The default SID is passed to the MLS layer to be used to allow
824 * kernel labeling of the MLS field if the MLS field is not present
825 * (for upgrading to MLS without full relabel).
826 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
827 * memory is available, or 0 on success.
829 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
831 return security_context_to_sid_core(scontext, scontext_len,
835 static int compute_sid_handle_invalid_context(
836 struct context *scontext,
837 struct context *tcontext,
839 struct context *newcontext)
841 char *s = NULL, *t = NULL, *n = NULL;
842 u32 slen, tlen, nlen;
844 if (context_struct_to_string(scontext, &s, &slen) < 0)
846 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
848 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
850 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
851 "security_compute_sid: invalid context %s"
855 n, s, t, policydb.p_class_val_to_name[tclass-1]);
860 if (!selinux_enforcing)
865 static int security_compute_sid(u32 ssid,
871 struct context *scontext = NULL, *tcontext = NULL, newcontext;
872 struct role_trans *roletr = NULL;
873 struct avtab_key avkey;
874 struct avtab_datum *avdatum;
875 struct avtab_node *node;
878 if (!ss_initialized) {
880 case SECCLASS_PROCESS:
890 context_init(&newcontext);
894 scontext = sidtab_search(&sidtab, ssid);
896 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
901 tcontext = sidtab_search(&sidtab, tsid);
903 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
909 /* Set the user identity. */
911 case AVTAB_TRANSITION:
913 /* Use the process user identity. */
914 newcontext.user = scontext->user;
917 /* Use the related object owner. */
918 newcontext.user = tcontext->user;
922 /* Set the role and type to default values. */
924 case SECCLASS_PROCESS:
925 /* Use the current role and type of process. */
926 newcontext.role = scontext->role;
927 newcontext.type = scontext->type;
930 /* Use the well-defined object role. */
931 newcontext.role = OBJECT_R_VAL;
932 /* Use the type of the related object. */
933 newcontext.type = tcontext->type;
936 /* Look for a type transition/member/change rule. */
937 avkey.source_type = scontext->type;
938 avkey.target_type = tcontext->type;
939 avkey.target_class = tclass;
940 avkey.specified = specified;
941 avdatum = avtab_search(&policydb.te_avtab, &avkey);
943 /* If no permanent rule, also check for enabled conditional rules */
945 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
946 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
947 if (node->key.specified & AVTAB_ENABLED) {
948 avdatum = &node->datum;
955 /* Use the type from the type transition/member/change rule. */
956 newcontext.type = avdatum->data;
959 /* Check for class-specific changes. */
961 case SECCLASS_PROCESS:
962 if (specified & AVTAB_TRANSITION) {
963 /* Look for a role transition rule. */
964 for (roletr = policydb.role_tr; roletr;
965 roletr = roletr->next) {
966 if (roletr->role == scontext->role &&
967 roletr->type == tcontext->type) {
968 /* Use the role transition rule. */
969 newcontext.role = roletr->new_role;
979 /* Set the MLS attributes.
980 This is done last because it may allocate memory. */
981 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
985 /* Check the validity of the context. */
986 if (!policydb_context_isvalid(&policydb, &newcontext)) {
987 rc = compute_sid_handle_invalid_context(scontext,
994 /* Obtain the sid for the context. */
995 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
998 context_destroy(&newcontext);
1004 * security_transition_sid - Compute the SID for a new subject/object.
1005 * @ssid: source security identifier
1006 * @tsid: target security identifier
1007 * @tclass: target security class
1008 * @out_sid: security identifier for new subject/object
1010 * Compute a SID to use for labeling a new subject or object in the
1011 * class @tclass based on a SID pair (@ssid, @tsid).
1012 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1013 * if insufficient memory is available, or %0 if the new SID was
1014 * computed successfully.
1016 int security_transition_sid(u32 ssid,
1021 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1025 * security_member_sid - Compute the SID for member selection.
1026 * @ssid: source security identifier
1027 * @tsid: target security identifier
1028 * @tclass: target security class
1029 * @out_sid: security identifier for selected member
1031 * Compute a SID to use when selecting a member of a polyinstantiated
1032 * object of class @tclass based on a SID pair (@ssid, @tsid).
1033 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1034 * if insufficient memory is available, or %0 if the SID was
1035 * computed successfully.
1037 int security_member_sid(u32 ssid,
1042 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1046 * security_change_sid - Compute the SID for object relabeling.
1047 * @ssid: source security identifier
1048 * @tsid: target security identifier
1049 * @tclass: target security class
1050 * @out_sid: security identifier for selected member
1052 * Compute a SID to use for relabeling an object of class @tclass
1053 * based on a SID pair (@ssid, @tsid).
1054 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1055 * if insufficient memory is available, or %0 if the SID was
1056 * computed successfully.
1058 int security_change_sid(u32 ssid,
1063 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1067 * Verify that each kernel class that is defined in the
1070 static int validate_classes(struct policydb *p)
1073 struct class_datum *cladatum;
1074 struct perm_datum *perdatum;
1075 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1077 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1078 const char *def_class, *def_perm, *pol_class;
1079 struct symtab *perms;
1081 if (p->allow_unknown) {
1082 u32 num_classes = kdefs->cts_len;
1083 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1084 if (!p->undefined_perms)
1088 for (i = 1; i < kdefs->cts_len; i++) {
1089 def_class = kdefs->class_to_string[i];
1092 if (i > p->p_classes.nprim) {
1094 "security: class %s not defined in policy\n",
1096 if (p->reject_unknown)
1098 if (p->allow_unknown)
1099 p->undefined_perms[i-1] = ~0U;
1102 pol_class = p->p_class_val_to_name[i-1];
1103 if (strcmp(pol_class, def_class)) {
1105 "security: class %d is incorrect, found %s but should be %s\n",
1106 i, pol_class, def_class);
1110 for (i = 0; i < kdefs->av_pts_len; i++) {
1111 class_val = kdefs->av_perm_to_string[i].tclass;
1112 perm_val = kdefs->av_perm_to_string[i].value;
1113 def_perm = kdefs->av_perm_to_string[i].name;
1114 if (class_val > p->p_classes.nprim)
1116 pol_class = p->p_class_val_to_name[class_val-1];
1117 cladatum = hashtab_search(p->p_classes.table, pol_class);
1119 perms = &cladatum->permissions;
1120 nprim = 1 << (perms->nprim - 1);
1121 if (perm_val > nprim) {
1123 "security: permission %s in class %s not defined in policy\n",
1124 def_perm, pol_class);
1125 if (p->reject_unknown)
1127 if (p->allow_unknown)
1128 p->undefined_perms[class_val-1] |= perm_val;
1131 perdatum = hashtab_search(perms->table, def_perm);
1132 if (perdatum == NULL) {
1134 "security: permission %s in class %s not found in policy, bad policy\n",
1135 def_perm, pol_class);
1138 pol_val = 1 << (perdatum->value - 1);
1139 if (pol_val != perm_val) {
1141 "security: permission %s in class %s has incorrect value\n",
1142 def_perm, pol_class);
1146 for (i = 0; i < kdefs->av_inherit_len; i++) {
1147 class_val = kdefs->av_inherit[i].tclass;
1148 if (class_val > p->p_classes.nprim)
1150 pol_class = p->p_class_val_to_name[class_val-1];
1151 cladatum = hashtab_search(p->p_classes.table, pol_class);
1153 if (!cladatum->comdatum) {
1155 "security: class %s should have an inherits clause but does not\n",
1159 tmp = kdefs->av_inherit[i].common_base;
1161 while (!(tmp & 0x01)) {
1165 perms = &cladatum->comdatum->permissions;
1166 for (j = 0; j < common_pts_len; j++) {
1167 def_perm = kdefs->av_inherit[i].common_pts[j];
1168 if (j >= perms->nprim) {
1170 "security: permission %s in class %s not defined in policy\n",
1171 def_perm, pol_class);
1172 if (p->reject_unknown)
1174 if (p->allow_unknown)
1175 p->undefined_perms[class_val-1] |= (1 << j);
1178 perdatum = hashtab_search(perms->table, def_perm);
1179 if (perdatum == NULL) {
1181 "security: permission %s in class %s not found in policy, bad policy\n",
1182 def_perm, pol_class);
1185 if (perdatum->value != j + 1) {
1187 "security: permission %s in class %s has incorrect value\n",
1188 def_perm, pol_class);
1196 /* Clone the SID into the new SID table. */
1197 static int clone_sid(u32 sid,
1198 struct context *context,
1201 struct sidtab *s = arg;
1203 return sidtab_insert(s, sid, context);
1206 static inline int convert_context_handle_invalid_context(struct context *context)
1210 if (selinux_enforcing) {
1216 context_struct_to_string(context, &s, &len);
1217 printk(KERN_ERR "security: context %s is invalid\n", s);
1223 struct convert_context_args {
1224 struct policydb *oldp;
1225 struct policydb *newp;
1229 * Convert the values in the security context
1230 * structure `c' from the values specified
1231 * in the policy `p->oldp' to the values specified
1232 * in the policy `p->newp'. Verify that the
1233 * context is valid under the new policy.
1235 static int convert_context(u32 key,
1239 struct convert_context_args *args;
1240 struct context oldc;
1241 struct role_datum *role;
1242 struct type_datum *typdatum;
1243 struct user_datum *usrdatum;
1250 rc = context_cpy(&oldc, c);
1256 /* Convert the user. */
1257 usrdatum = hashtab_search(args->newp->p_users.table,
1258 args->oldp->p_user_val_to_name[c->user - 1]);
1262 c->user = usrdatum->value;
1264 /* Convert the role. */
1265 role = hashtab_search(args->newp->p_roles.table,
1266 args->oldp->p_role_val_to_name[c->role - 1]);
1270 c->role = role->value;
1272 /* Convert the type. */
1273 typdatum = hashtab_search(args->newp->p_types.table,
1274 args->oldp->p_type_val_to_name[c->type - 1]);
1278 c->type = typdatum->value;
1280 rc = mls_convert_context(args->oldp, args->newp, c);
1284 /* Check the validity of the new context. */
1285 if (!policydb_context_isvalid(args->newp, c)) {
1286 rc = convert_context_handle_invalid_context(&oldc);
1291 context_destroy(&oldc);
1295 context_struct_to_string(&oldc, &s, &len);
1296 context_destroy(&oldc);
1297 printk(KERN_ERR "security: invalidating context %s\n", s);
1302 extern void selinux_complete_init(void);
1303 static int security_preserve_bools(struct policydb *p);
1306 * security_load_policy - Load a security policy configuration.
1307 * @data: binary policy data
1308 * @len: length of data in bytes
1310 * Load a new set of security policy configuration data,
1311 * validate it and convert the SID table as necessary.
1312 * This function will flush the access vector cache after
1313 * loading the new policy.
1315 int security_load_policy(void *data, size_t len)
1317 struct policydb oldpolicydb, newpolicydb;
1318 struct sidtab oldsidtab, newsidtab;
1319 struct convert_context_args args;
1322 struct policy_file file = { data, len }, *fp = &file;
1326 if (!ss_initialized) {
1328 if (policydb_read(&policydb, fp)) {
1330 avtab_cache_destroy();
1333 if (policydb_load_isids(&policydb, &sidtab)) {
1335 policydb_destroy(&policydb);
1336 avtab_cache_destroy();
1339 /* Verify that the kernel defined classes are correct. */
1340 if (validate_classes(&policydb)) {
1342 "security: the definition of a class is incorrect\n");
1344 sidtab_destroy(&sidtab);
1345 policydb_destroy(&policydb);
1346 avtab_cache_destroy();
1349 policydb_loaded_version = policydb.policyvers;
1351 seqno = ++latest_granting;
1353 selinux_complete_init();
1354 avc_ss_reset(seqno);
1355 selnl_notify_policyload(seqno);
1356 selinux_netlbl_cache_invalidate();
1357 selinux_xfrm_notify_policyload();
1362 sidtab_hash_eval(&sidtab, "sids");
1365 if (policydb_read(&newpolicydb, fp)) {
1370 sidtab_init(&newsidtab);
1372 /* Verify that the kernel defined classes are correct. */
1373 if (validate_classes(&newpolicydb)) {
1375 "security: the definition of a class is incorrect\n");
1380 rc = security_preserve_bools(&newpolicydb);
1382 printk(KERN_ERR "security: unable to preserve booleans\n");
1386 /* Clone the SID table. */
1387 sidtab_shutdown(&sidtab);
1388 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1393 /* Convert the internal representations of contexts
1394 in the new SID table and remove invalid SIDs. */
1395 args.oldp = &policydb;
1396 args.newp = &newpolicydb;
1397 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1399 /* Save the old policydb and SID table to free later. */
1400 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1401 sidtab_set(&oldsidtab, &sidtab);
1403 /* Install the new policydb and SID table. */
1405 memcpy(&policydb, &newpolicydb, sizeof policydb);
1406 sidtab_set(&sidtab, &newsidtab);
1407 seqno = ++latest_granting;
1408 policydb_loaded_version = policydb.policyvers;
1412 /* Free the old policydb and SID table. */
1413 policydb_destroy(&oldpolicydb);
1414 sidtab_destroy(&oldsidtab);
1416 avc_ss_reset(seqno);
1417 selnl_notify_policyload(seqno);
1418 selinux_netlbl_cache_invalidate();
1419 selinux_xfrm_notify_policyload();
1425 sidtab_destroy(&newsidtab);
1426 policydb_destroy(&newpolicydb);
1432 * security_port_sid - Obtain the SID for a port.
1433 * @domain: communication domain aka address family
1434 * @type: socket type
1435 * @protocol: protocol number
1436 * @port: port number
1437 * @out_sid: security identifier
1439 int security_port_sid(u16 domain,
1450 c = policydb.ocontexts[OCON_PORT];
1452 if (c->u.port.protocol == protocol &&
1453 c->u.port.low_port <= port &&
1454 c->u.port.high_port >= port)
1461 rc = sidtab_context_to_sid(&sidtab,
1467 *out_sid = c->sid[0];
1469 *out_sid = SECINITSID_PORT;
1478 * security_netif_sid - Obtain the SID for a network interface.
1479 * @name: interface name
1480 * @if_sid: interface SID
1481 * @msg_sid: default SID for received packets
1483 int security_netif_sid(char *name,
1492 c = policydb.ocontexts[OCON_NETIF];
1494 if (strcmp(name, c->u.name) == 0)
1500 if (!c->sid[0] || !c->sid[1]) {
1501 rc = sidtab_context_to_sid(&sidtab,
1506 rc = sidtab_context_to_sid(&sidtab,
1512 *if_sid = c->sid[0];
1513 *msg_sid = c->sid[1];
1515 *if_sid = SECINITSID_NETIF;
1516 *msg_sid = SECINITSID_NETMSG;
1524 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1528 for(i = 0; i < 4; i++)
1529 if(addr[i] != (input[i] & mask[i])) {
1538 * security_node_sid - Obtain the SID for a node (host).
1539 * @domain: communication domain aka address family
1541 * @addrlen: address length in bytes
1542 * @out_sid: security identifier
1544 int security_node_sid(u16 domain,
1558 if (addrlen != sizeof(u32)) {
1563 addr = *((u32 *)addrp);
1565 c = policydb.ocontexts[OCON_NODE];
1567 if (c->u.node.addr == (addr & c->u.node.mask))
1575 if (addrlen != sizeof(u64) * 2) {
1579 c = policydb.ocontexts[OCON_NODE6];
1581 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1589 *out_sid = SECINITSID_NODE;
1595 rc = sidtab_context_to_sid(&sidtab,
1601 *out_sid = c->sid[0];
1603 *out_sid = SECINITSID_NODE;
1614 * security_get_user_sids - Obtain reachable SIDs for a user.
1615 * @fromsid: starting SID
1616 * @username: username
1617 * @sids: array of reachable SIDs for user
1618 * @nel: number of elements in @sids
1620 * Generate the set of SIDs for legal security contexts
1621 * for a given user that can be reached by @fromsid.
1622 * Set *@sids to point to a dynamically allocated
1623 * array containing the set of SIDs. Set *@nel to the
1624 * number of elements in the array.
1627 int security_get_user_sids(u32 fromsid,
1632 struct context *fromcon, usercon;
1633 u32 *mysids = NULL, *mysids2, sid;
1634 u32 mynel = 0, maxnel = SIDS_NEL;
1635 struct user_datum *user;
1636 struct role_datum *role;
1637 struct ebitmap_node *rnode, *tnode;
1643 if (!ss_initialized)
1648 fromcon = sidtab_search(&sidtab, fromsid);
1654 user = hashtab_search(policydb.p_users.table, username);
1659 usercon.user = user->value;
1661 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1667 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
1668 role = policydb.role_val_to_struct[i];
1670 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
1673 if (mls_setup_user_range(fromcon, user, &usercon))
1676 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1679 if (mynel < maxnel) {
1680 mysids[mynel++] = sid;
1683 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1688 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1691 mysids[mynel++] = sid;
1703 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1709 for (i = 0, j = 0; i < mynel; i++) {
1710 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1712 PROCESS__TRANSITION, AVC_STRICT,
1715 mysids2[j++] = mysids[i];
1727 * security_genfs_sid - Obtain a SID for a file in a filesystem
1728 * @fstype: filesystem type
1729 * @path: path from root of mount
1730 * @sclass: file security class
1731 * @sid: SID for path
1733 * Obtain a SID to use for a file in a filesystem that
1734 * cannot support xattr or use a fixed labeling behavior like
1735 * transition SIDs or task SIDs.
1737 int security_genfs_sid(const char *fstype,
1743 struct genfs *genfs;
1745 int rc = 0, cmp = 0;
1749 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1750 cmp = strcmp(fstype, genfs->fstype);
1755 if (!genfs || cmp) {
1756 *sid = SECINITSID_UNLABELED;
1761 for (c = genfs->head; c; c = c->next) {
1762 len = strlen(c->u.name);
1763 if ((!c->v.sclass || sclass == c->v.sclass) &&
1764 (strncmp(c->u.name, path, len) == 0))
1769 *sid = SECINITSID_UNLABELED;
1775 rc = sidtab_context_to_sid(&sidtab,
1789 * security_fs_use - Determine how to handle labeling for a filesystem.
1790 * @fstype: filesystem type
1791 * @behavior: labeling behavior
1792 * @sid: SID for filesystem (superblock)
1794 int security_fs_use(
1796 unsigned int *behavior,
1804 c = policydb.ocontexts[OCON_FSUSE];
1806 if (strcmp(fstype, c->u.name) == 0)
1812 *behavior = c->v.behavior;
1814 rc = sidtab_context_to_sid(&sidtab,
1822 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1824 *behavior = SECURITY_FS_USE_NONE;
1827 *behavior = SECURITY_FS_USE_GENFS;
1836 int security_get_bools(int *len, char ***names, int **values)
1838 int i, rc = -ENOMEM;
1844 *len = policydb.p_bools.nprim;
1850 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1854 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1858 for (i = 0; i < *len; i++) {
1860 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1861 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1862 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1865 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1866 (*names)[i][name_len - 1] = 0;
1874 for (i = 0; i < *len; i++)
1882 int security_set_bools(int len, int *values)
1885 int lenp, seqno = 0;
1886 struct cond_node *cur;
1890 lenp = policydb.p_bools.nprim;
1896 for (i = 0; i < len; i++) {
1897 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1898 audit_log(current->audit_context, GFP_ATOMIC,
1899 AUDIT_MAC_CONFIG_CHANGE,
1900 "bool=%s val=%d old_val=%d auid=%u",
1901 policydb.p_bool_val_to_name[i],
1903 policydb.bool_val_to_struct[i]->state,
1904 audit_get_loginuid(current->audit_context));
1907 policydb.bool_val_to_struct[i]->state = 1;
1909 policydb.bool_val_to_struct[i]->state = 0;
1913 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1914 rc = evaluate_cond_node(&policydb, cur);
1919 seqno = ++latest_granting;
1924 avc_ss_reset(seqno);
1925 selnl_notify_policyload(seqno);
1926 selinux_xfrm_notify_policyload();
1931 int security_get_bool_value(int bool)
1938 len = policydb.p_bools.nprim;
1944 rc = policydb.bool_val_to_struct[bool]->state;
1950 static int security_preserve_bools(struct policydb *p)
1952 int rc, nbools = 0, *bvalues = NULL, i;
1953 char **bnames = NULL;
1954 struct cond_bool_datum *booldatum;
1955 struct cond_node *cur;
1957 rc = security_get_bools(&nbools, &bnames, &bvalues);
1960 for (i = 0; i < nbools; i++) {
1961 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1963 booldatum->state = bvalues[i];
1965 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1966 rc = evaluate_cond_node(p, cur);
1973 for (i = 0; i < nbools; i++)
1982 * security_sid_mls_copy() - computes a new sid based on the given
1983 * sid and the mls portion of mls_sid.
1985 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1987 struct context *context1;
1988 struct context *context2;
1989 struct context newcon;
1994 if (!ss_initialized || !selinux_mls_enabled) {
1999 context_init(&newcon);
2002 context1 = sidtab_search(&sidtab, sid);
2004 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2010 context2 = sidtab_search(&sidtab, mls_sid);
2012 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2018 newcon.user = context1->user;
2019 newcon.role = context1->role;
2020 newcon.type = context1->type;
2021 rc = mls_context_cpy(&newcon, context2);
2025 /* Check the validity of the new context. */
2026 if (!policydb_context_isvalid(&policydb, &newcon)) {
2027 rc = convert_context_handle_invalid_context(&newcon);
2032 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2036 if (!context_struct_to_string(&newcon, &s, &len)) {
2037 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2038 "security_sid_mls_copy: invalid context %s", s);
2044 context_destroy(&newcon);
2049 static int get_classes_callback(void *k, void *d, void *args)
2051 struct class_datum *datum = d;
2052 char *name = k, **classes = args;
2053 int value = datum->value - 1;
2055 classes[value] = kstrdup(name, GFP_ATOMIC);
2056 if (!classes[value])
2062 int security_get_classes(char ***classes, int *nclasses)
2068 *nclasses = policydb.p_classes.nprim;
2069 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2073 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2077 for (i = 0; i < *nclasses; i++)
2078 kfree((*classes)[i]);
2087 static int get_permissions_callback(void *k, void *d, void *args)
2089 struct perm_datum *datum = d;
2090 char *name = k, **perms = args;
2091 int value = datum->value - 1;
2093 perms[value] = kstrdup(name, GFP_ATOMIC);
2100 int security_get_permissions(char *class, char ***perms, int *nperms)
2102 int rc = -ENOMEM, i;
2103 struct class_datum *match;
2107 match = hashtab_search(policydb.p_classes.table, class);
2109 printk(KERN_ERR "%s: unrecognized class %s\n",
2110 __FUNCTION__, class);
2115 *nperms = match->permissions.nprim;
2116 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2120 if (match->comdatum) {
2121 rc = hashtab_map(match->comdatum->permissions.table,
2122 get_permissions_callback, *perms);
2127 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2138 for (i = 0; i < *nperms; i++)
2144 int security_get_reject_unknown(void)
2146 return policydb.reject_unknown;
2149 int security_get_allow_unknown(void)
2151 return policydb.allow_unknown;
2154 struct selinux_audit_rule {
2156 struct context au_ctxt;
2159 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
2162 context_destroy(&rule->au_ctxt);
2167 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
2168 struct selinux_audit_rule **rule)
2170 struct selinux_audit_rule *tmprule;
2171 struct role_datum *roledatum;
2172 struct type_datum *typedatum;
2173 struct user_datum *userdatum;
2178 if (!ss_initialized)
2182 case AUDIT_SUBJ_USER:
2183 case AUDIT_SUBJ_ROLE:
2184 case AUDIT_SUBJ_TYPE:
2185 case AUDIT_OBJ_USER:
2186 case AUDIT_OBJ_ROLE:
2187 case AUDIT_OBJ_TYPE:
2188 /* only 'equals' and 'not equals' fit user, role, and type */
2189 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2192 case AUDIT_SUBJ_SEN:
2193 case AUDIT_SUBJ_CLR:
2194 case AUDIT_OBJ_LEV_LOW:
2195 case AUDIT_OBJ_LEV_HIGH:
2196 /* we do not allow a range, indicated by the presense of '-' */
2197 if (strchr(rulestr, '-'))
2201 /* only the above fields are valid */
2205 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2209 context_init(&tmprule->au_ctxt);
2213 tmprule->au_seqno = latest_granting;
2216 case AUDIT_SUBJ_USER:
2217 case AUDIT_OBJ_USER:
2218 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2222 tmprule->au_ctxt.user = userdatum->value;
2224 case AUDIT_SUBJ_ROLE:
2225 case AUDIT_OBJ_ROLE:
2226 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2230 tmprule->au_ctxt.role = roledatum->value;
2232 case AUDIT_SUBJ_TYPE:
2233 case AUDIT_OBJ_TYPE:
2234 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2238 tmprule->au_ctxt.type = typedatum->value;
2240 case AUDIT_SUBJ_SEN:
2241 case AUDIT_SUBJ_CLR:
2242 case AUDIT_OBJ_LEV_LOW:
2243 case AUDIT_OBJ_LEV_HIGH:
2244 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2251 selinux_audit_rule_free(tmprule);
2260 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2261 struct selinux_audit_rule *rule,
2262 struct audit_context *actx)
2264 struct context *ctxt;
2265 struct mls_level *level;
2269 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2270 "selinux_audit_rule_match: missing rule\n");
2276 if (rule->au_seqno < latest_granting) {
2277 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2278 "selinux_audit_rule_match: stale rule\n");
2283 ctxt = sidtab_search(&sidtab, sid);
2285 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2286 "selinux_audit_rule_match: unrecognized SID %d\n",
2292 /* a field/op pair that is not caught here will simply fall through
2295 case AUDIT_SUBJ_USER:
2296 case AUDIT_OBJ_USER:
2299 match = (ctxt->user == rule->au_ctxt.user);
2301 case AUDIT_NOT_EQUAL:
2302 match = (ctxt->user != rule->au_ctxt.user);
2306 case AUDIT_SUBJ_ROLE:
2307 case AUDIT_OBJ_ROLE:
2310 match = (ctxt->role == rule->au_ctxt.role);
2312 case AUDIT_NOT_EQUAL:
2313 match = (ctxt->role != rule->au_ctxt.role);
2317 case AUDIT_SUBJ_TYPE:
2318 case AUDIT_OBJ_TYPE:
2321 match = (ctxt->type == rule->au_ctxt.type);
2323 case AUDIT_NOT_EQUAL:
2324 match = (ctxt->type != rule->au_ctxt.type);
2328 case AUDIT_SUBJ_SEN:
2329 case AUDIT_SUBJ_CLR:
2330 case AUDIT_OBJ_LEV_LOW:
2331 case AUDIT_OBJ_LEV_HIGH:
2332 level = ((field == AUDIT_SUBJ_SEN ||
2333 field == AUDIT_OBJ_LEV_LOW) ?
2334 &ctxt->range.level[0] : &ctxt->range.level[1]);
2337 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2340 case AUDIT_NOT_EQUAL:
2341 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2344 case AUDIT_LESS_THAN:
2345 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2347 !mls_level_eq(&rule->au_ctxt.range.level[0],
2350 case AUDIT_LESS_THAN_OR_EQUAL:
2351 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2354 case AUDIT_GREATER_THAN:
2355 match = (mls_level_dom(level,
2356 &rule->au_ctxt.range.level[0]) &&
2357 !mls_level_eq(level,
2358 &rule->au_ctxt.range.level[0]));
2360 case AUDIT_GREATER_THAN_OR_EQUAL:
2361 match = mls_level_dom(level,
2362 &rule->au_ctxt.range.level[0]);
2372 static int (*aurule_callback)(void) = NULL;
2374 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2375 u16 class, u32 perms, u32 *retained)
2379 if (event == AVC_CALLBACK_RESET && aurule_callback)
2380 err = aurule_callback();
2384 static int __init aurule_init(void)
2388 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2389 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2391 panic("avc_add_callback() failed, error %d\n", err);
2395 __initcall(aurule_init);
2397 void selinux_audit_set_callback(int (*callback)(void))
2399 aurule_callback = callback;
2402 #ifdef CONFIG_NETLABEL
2404 * NetLabel cache structure
2406 #define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
2407 #define NETLBL_CACHE_T_NONE 0
2408 #define NETLBL_CACHE_T_SID 1
2409 #define NETLBL_CACHE_T_MLS 2
2410 struct selinux_netlbl_cache {
2414 struct mls_range mls_label;
2419 * security_netlbl_cache_free - Free the NetLabel cached data
2420 * @data: the data to free
2423 * This function is intended to be used as the free() callback inside the
2424 * netlbl_lsm_cache structure.
2427 static void security_netlbl_cache_free(const void *data)
2429 struct selinux_netlbl_cache *cache;
2434 cache = NETLBL_CACHE(data);
2435 switch (cache->type) {
2436 case NETLBL_CACHE_T_MLS:
2437 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2444 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2445 * @secattr: the NetLabel packet security attributes
2446 * @ctx: the SELinux context
2449 * Attempt to cache the context in @ctx, which was derived from the packet in
2450 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2451 * already been initialized.
2454 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2455 struct context *ctx)
2457 struct selinux_netlbl_cache *cache = NULL;
2459 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2460 if (secattr->cache == NULL)
2463 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2467 cache->type = NETLBL_CACHE_T_MLS;
2468 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2469 &ctx->range.level[0].cat) != 0) {
2473 cache->data.mls_label.level[1].cat.highbit =
2474 cache->data.mls_label.level[0].cat.highbit;
2475 cache->data.mls_label.level[1].cat.node =
2476 cache->data.mls_label.level[0].cat.node;
2477 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2478 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2480 secattr->cache->free = security_netlbl_cache_free;
2481 secattr->cache->data = (void *)cache;
2482 secattr->flags |= NETLBL_SECATTR_CACHE;
2486 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2487 * @secattr: the NetLabel packet security attributes
2488 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2489 * @sid: the SELinux SID
2492 * Convert the given NetLabel security attributes in @secattr into a
2493 * SELinux SID. If the @secattr field does not contain a full SELinux
2494 * SID/context then use the context in @base_sid as the foundation. If
2495 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2496 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2497 * SID conversion for future lookups. Returns zero on success, negative
2498 * values on failure.
2501 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2506 struct context *ctx;
2507 struct context ctx_new;
2508 struct selinux_netlbl_cache *cache;
2510 if (!ss_initialized) {
2517 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2518 cache = NETLBL_CACHE(secattr->cache->data);
2519 switch (cache->type) {
2520 case NETLBL_CACHE_T_SID:
2521 *sid = cache->data.sid;
2524 case NETLBL_CACHE_T_MLS:
2525 ctx = sidtab_search(&sidtab, base_sid);
2527 goto netlbl_secattr_to_sid_return;
2529 ctx_new.user = ctx->user;
2530 ctx_new.role = ctx->role;
2531 ctx_new.type = ctx->type;
2532 ctx_new.range.level[0].sens =
2533 cache->data.mls_label.level[0].sens;
2534 ctx_new.range.level[0].cat.highbit =
2535 cache->data.mls_label.level[0].cat.highbit;
2536 ctx_new.range.level[0].cat.node =
2537 cache->data.mls_label.level[0].cat.node;
2538 ctx_new.range.level[1].sens =
2539 cache->data.mls_label.level[1].sens;
2540 ctx_new.range.level[1].cat.highbit =
2541 cache->data.mls_label.level[1].cat.highbit;
2542 ctx_new.range.level[1].cat.node =
2543 cache->data.mls_label.level[1].cat.node;
2545 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2548 goto netlbl_secattr_to_sid_return;
2550 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2551 ctx = sidtab_search(&sidtab, base_sid);
2553 goto netlbl_secattr_to_sid_return;
2555 ctx_new.user = ctx->user;
2556 ctx_new.role = ctx->role;
2557 ctx_new.type = ctx->type;
2558 mls_import_netlbl_lvl(&ctx_new, secattr);
2559 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2560 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2561 secattr->mls_cat) != 0)
2562 goto netlbl_secattr_to_sid_return;
2563 ctx_new.range.level[1].cat.highbit =
2564 ctx_new.range.level[0].cat.highbit;
2565 ctx_new.range.level[1].cat.node =
2566 ctx_new.range.level[0].cat.node;
2568 ebitmap_init(&ctx_new.range.level[0].cat);
2569 ebitmap_init(&ctx_new.range.level[1].cat);
2571 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2572 goto netlbl_secattr_to_sid_return_cleanup;
2574 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2576 goto netlbl_secattr_to_sid_return_cleanup;
2578 security_netlbl_cache_add(secattr, &ctx_new);
2580 ebitmap_destroy(&ctx_new.range.level[0].cat);
2586 netlbl_secattr_to_sid_return:
2589 netlbl_secattr_to_sid_return_cleanup:
2590 ebitmap_destroy(&ctx_new.range.level[0].cat);
2591 goto netlbl_secattr_to_sid_return;
2595 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2596 * @sid: the SELinux SID
2597 * @secattr: the NetLabel packet security attributes
2600 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2601 * Returns zero on success, negative values on failure.
2604 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2607 struct context *ctx;
2609 if (!ss_initialized)
2613 ctx = sidtab_search(&sidtab, sid);
2615 goto netlbl_sid_to_secattr_failure;
2616 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2618 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2619 mls_export_netlbl_lvl(ctx, secattr);
2620 rc = mls_export_netlbl_cat(ctx, secattr);
2622 goto netlbl_sid_to_secattr_failure;
2627 netlbl_sid_to_secattr_failure:
2629 netlbl_secattr_destroy(secattr);
2632 #endif /* CONFIG_NETLABEL */