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 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, version 2.
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/spinlock.h>
27 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/audit.h>
31 #include <linux/mutex.h>
41 #include "conditional.h"
44 extern void selnl_notify_policyload(u32 seqno);
45 unsigned int policydb_loaded_version;
47 static DEFINE_RWLOCK(policy_rwlock);
48 #define POLICY_RDLOCK read_lock(&policy_rwlock)
49 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
50 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
51 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
53 static DEFINE_MUTEX(load_mutex);
54 #define LOAD_LOCK mutex_lock(&load_mutex)
55 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
57 static struct sidtab sidtab;
58 struct policydb policydb;
59 int ss_initialized = 0;
62 * The largest sequence number that has been used when
63 * providing an access decision to the access vector cache.
64 * The sequence number only changes when a policy change
67 static u32 latest_granting = 0;
69 /* Forward declaration. */
70 static int context_struct_to_string(struct context *context, char **scontext,
74 * Return the boolean value of a constraint expression
75 * when it is applied to the specified source and target
78 * xcontext is a special beast... It is used by the validatetrans rules
79 * only. For these rules, scontext is the context before the transition,
80 * tcontext is the context after the transition, and xcontext is the context
81 * of the process performing the transition. All other callers of
82 * constraint_expr_eval should pass in NULL for xcontext.
84 static int constraint_expr_eval(struct context *scontext,
85 struct context *tcontext,
86 struct context *xcontext,
87 struct constraint_expr *cexpr)
91 struct role_datum *r1, *r2;
92 struct mls_level *l1, *l2;
93 struct constraint_expr *e;
94 int s[CEXPR_MAXDEPTH];
97 for (e = cexpr; e; e = e->next) {
98 switch (e->expr_type) {
114 if (sp == (CEXPR_MAXDEPTH-1))
118 val1 = scontext->user;
119 val2 = tcontext->user;
122 val1 = scontext->type;
123 val2 = tcontext->type;
126 val1 = scontext->role;
127 val2 = tcontext->role;
128 r1 = policydb.role_val_to_struct[val1 - 1];
129 r2 = policydb.role_val_to_struct[val2 - 1];
132 s[++sp] = ebitmap_get_bit(&r1->dominates,
136 s[++sp] = ebitmap_get_bit(&r2->dominates,
140 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
142 !ebitmap_get_bit(&r2->dominates,
150 l1 = &(scontext->range.level[0]);
151 l2 = &(tcontext->range.level[0]);
154 l1 = &(scontext->range.level[0]);
155 l2 = &(tcontext->range.level[1]);
158 l1 = &(scontext->range.level[1]);
159 l2 = &(tcontext->range.level[0]);
162 l1 = &(scontext->range.level[1]);
163 l2 = &(tcontext->range.level[1]);
166 l1 = &(scontext->range.level[0]);
167 l2 = &(scontext->range.level[1]);
170 l1 = &(tcontext->range.level[0]);
171 l2 = &(tcontext->range.level[1]);
176 s[++sp] = mls_level_eq(l1, l2);
179 s[++sp] = !mls_level_eq(l1, l2);
182 s[++sp] = mls_level_dom(l1, l2);
185 s[++sp] = mls_level_dom(l2, l1);
188 s[++sp] = mls_level_incomp(l2, l1);
202 s[++sp] = (val1 == val2);
205 s[++sp] = (val1 != val2);
213 if (sp == (CEXPR_MAXDEPTH-1))
216 if (e->attr & CEXPR_TARGET)
218 else if (e->attr & CEXPR_XTARGET) {
225 if (e->attr & CEXPR_USER)
227 else if (e->attr & CEXPR_ROLE)
229 else if (e->attr & CEXPR_TYPE)
238 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
241 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
259 * Compute access vectors based on a context structure pair for
260 * the permissions in a particular class.
262 static int context_struct_compute_av(struct context *scontext,
263 struct context *tcontext,
266 struct av_decision *avd)
268 struct constraint_node *constraint;
269 struct role_allow *ra;
270 struct avtab_key avkey;
271 struct avtab_node *node;
272 struct class_datum *tclass_datum;
273 struct ebitmap *sattr, *tattr;
274 struct ebitmap_node *snode, *tnode;
278 * Remap extended Netlink classes for old policy versions.
279 * Do this here rather than socket_type_to_security_class()
280 * in case a newer policy version is loaded, allowing sockets
281 * to remain in the correct class.
283 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
284 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
285 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
286 tclass = SECCLASS_NETLINK_SOCKET;
288 if (!tclass || tclass > policydb.p_classes.nprim) {
289 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
293 tclass_datum = policydb.class_val_to_struct[tclass - 1];
296 * Initialize the access vectors to the default values.
299 avd->decided = 0xffffffff;
301 avd->auditdeny = 0xffffffff;
302 avd->seqno = latest_granting;
305 * If a specific type enforcement rule was defined for
306 * this permission check, then use it.
308 avkey.target_class = tclass;
309 avkey.specified = AVTAB_AV;
310 sattr = &policydb.type_attr_map[scontext->type - 1];
311 tattr = &policydb.type_attr_map[tcontext->type - 1];
312 ebitmap_for_each_bit(sattr, snode, i) {
313 if (!ebitmap_node_get_bit(snode, i))
315 ebitmap_for_each_bit(tattr, tnode, j) {
316 if (!ebitmap_node_get_bit(tnode, j))
318 avkey.source_type = i + 1;
319 avkey.target_type = j + 1;
320 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
322 node = avtab_search_node_next(node, avkey.specified)) {
323 if (node->key.specified == AVTAB_ALLOWED)
324 avd->allowed |= node->datum.data;
325 else if (node->key.specified == AVTAB_AUDITALLOW)
326 avd->auditallow |= node->datum.data;
327 else if (node->key.specified == AVTAB_AUDITDENY)
328 avd->auditdeny &= node->datum.data;
331 /* Check conditional av table for additional permissions */
332 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
338 * Remove any permissions prohibited by a constraint (this includes
341 constraint = tclass_datum->constraints;
343 if ((constraint->permissions & (avd->allowed)) &&
344 !constraint_expr_eval(scontext, tcontext, NULL,
346 avd->allowed = (avd->allowed) & ~(constraint->permissions);
348 constraint = constraint->next;
352 * If checking process transition permission and the
353 * role is changing, then check the (current_role, new_role)
356 if (tclass == SECCLASS_PROCESS &&
357 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
358 scontext->role != tcontext->role) {
359 for (ra = policydb.role_allow; ra; ra = ra->next) {
360 if (scontext->role == ra->role &&
361 tcontext->role == ra->new_role)
365 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
366 PROCESS__DYNTRANSITION);
372 static int security_validtrans_handle_fail(struct context *ocontext,
373 struct context *ncontext,
374 struct context *tcontext,
377 char *o = NULL, *n = NULL, *t = NULL;
378 u32 olen, nlen, tlen;
380 if (context_struct_to_string(ocontext, &o, &olen) < 0)
382 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
384 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
386 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
387 "security_validate_transition: denied for"
388 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
389 o, n, t, policydb.p_class_val_to_name[tclass-1]);
395 if (!selinux_enforcing)
400 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
403 struct context *ocontext;
404 struct context *ncontext;
405 struct context *tcontext;
406 struct class_datum *tclass_datum;
407 struct constraint_node *constraint;
416 * Remap extended Netlink classes for old policy versions.
417 * Do this here rather than socket_type_to_security_class()
418 * in case a newer policy version is loaded, allowing sockets
419 * to remain in the correct class.
421 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
422 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
423 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
424 tclass = SECCLASS_NETLINK_SOCKET;
426 if (!tclass || tclass > policydb.p_classes.nprim) {
427 printk(KERN_ERR "security_validate_transition: "
428 "unrecognized class %d\n", tclass);
432 tclass_datum = policydb.class_val_to_struct[tclass - 1];
434 ocontext = sidtab_search(&sidtab, oldsid);
436 printk(KERN_ERR "security_validate_transition: "
437 " unrecognized SID %d\n", oldsid);
442 ncontext = sidtab_search(&sidtab, newsid);
444 printk(KERN_ERR "security_validate_transition: "
445 " unrecognized SID %d\n", newsid);
450 tcontext = sidtab_search(&sidtab, tasksid);
452 printk(KERN_ERR "security_validate_transition: "
453 " unrecognized SID %d\n", tasksid);
458 constraint = tclass_datum->validatetrans;
460 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
462 rc = security_validtrans_handle_fail(ocontext, ncontext,
466 constraint = constraint->next;
475 * security_compute_av - Compute access vector decisions.
476 * @ssid: source security identifier
477 * @tsid: target security identifier
478 * @tclass: target security class
479 * @requested: requested permissions
480 * @avd: access vector decisions
482 * Compute a set of access vector decisions based on the
483 * SID pair (@ssid, @tsid) for the permissions in @tclass.
484 * Return -%EINVAL if any of the parameters are invalid or %0
485 * if the access vector decisions were computed successfully.
487 int security_compute_av(u32 ssid,
491 struct av_decision *avd)
493 struct context *scontext = NULL, *tcontext = NULL;
496 if (!ss_initialized) {
497 avd->allowed = 0xffffffff;
498 avd->decided = 0xffffffff;
500 avd->auditdeny = 0xffffffff;
501 avd->seqno = latest_granting;
507 scontext = sidtab_search(&sidtab, ssid);
509 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
514 tcontext = sidtab_search(&sidtab, tsid);
516 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
522 rc = context_struct_compute_av(scontext, tcontext, tclass,
530 * Write the security context string representation of
531 * the context structure `context' into a dynamically
532 * allocated string of the correct size. Set `*scontext'
533 * to point to this string and set `*scontext_len' to
534 * the length of the string.
536 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
543 /* Compute the size of the context. */
544 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
545 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
546 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
547 *scontext_len += mls_compute_context_len(context);
549 /* Allocate space for the context; caller must free this space. */
550 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
554 *scontext = scontextp;
557 * Copy the user name, role name and type name into the context.
559 sprintf(scontextp, "%s:%s:%s",
560 policydb.p_user_val_to_name[context->user - 1],
561 policydb.p_role_val_to_name[context->role - 1],
562 policydb.p_type_val_to_name[context->type - 1]);
563 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
564 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
565 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
567 mls_sid_to_context(context, &scontextp);
574 #include "initial_sid_to_string.h"
577 * security_sid_to_context - Obtain a context for a given SID.
578 * @sid: security identifier, SID
579 * @scontext: security context
580 * @scontext_len: length in bytes
582 * Write the string representation of the context associated with @sid
583 * into a dynamically allocated string of the correct size. Set @scontext
584 * to point to this string and set @scontext_len to the length of the string.
586 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
588 struct context *context;
591 if (!ss_initialized) {
592 if (sid <= SECINITSID_NUM) {
595 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
596 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
597 strcpy(scontextp, initial_sid_to_string[sid]);
598 *scontext = scontextp;
601 printk(KERN_ERR "security_sid_to_context: called before initial "
602 "load_policy on unknown SID %d\n", sid);
607 context = sidtab_search(&sidtab, sid);
609 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
614 rc = context_struct_to_string(context, scontext, scontext_len);
622 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
625 struct context context;
626 struct role_datum *role;
627 struct type_datum *typdatum;
628 struct user_datum *usrdatum;
629 char *scontextp, *p, oldc;
632 if (!ss_initialized) {
635 for (i = 1; i < SECINITSID_NUM; i++) {
636 if (!strcmp(initial_sid_to_string[i], scontext)) {
641 *sid = SECINITSID_KERNEL;
646 /* Copy the string so that we can modify the copy as we parse it.
647 The string should already by null terminated, but we append a
648 null suffix to the copy to avoid problems with the existing
649 attr package, which doesn't view the null terminator as part
650 of the attribute value. */
651 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
656 memcpy(scontext2, scontext, scontext_len);
657 scontext2[scontext_len] = 0;
659 context_init(&context);
664 /* Parse the security context. */
667 scontextp = (char *) scontext2;
669 /* Extract the user. */
671 while (*p && *p != ':')
679 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
683 context.user = usrdatum->value;
687 while (*p && *p != ':')
695 role = hashtab_search(policydb.p_roles.table, scontextp);
698 context.role = role->value;
702 while (*p && *p != ':')
707 typdatum = hashtab_search(policydb.p_types.table, scontextp);
711 context.type = typdatum->value;
713 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
717 if ((p - scontext2) < scontext_len) {
722 /* Check the validity of the new context. */
723 if (!policydb_context_isvalid(&policydb, &context)) {
727 /* Obtain the new sid. */
728 rc = sidtab_context_to_sid(&sidtab, &context, sid);
731 context_destroy(&context);
738 * security_context_to_sid - Obtain a SID for a given security context.
739 * @scontext: security context
740 * @scontext_len: length in bytes
741 * @sid: security identifier, SID
743 * Obtains a SID associated with the security context that
744 * has the string representation specified by @scontext.
745 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
746 * memory is available, or 0 on success.
748 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
750 return security_context_to_sid_core(scontext, scontext_len,
755 * security_context_to_sid_default - Obtain a SID for a given security context,
756 * falling back to specified default if needed.
758 * @scontext: security context
759 * @scontext_len: length in bytes
760 * @sid: security identifier, SID
761 * @def_sid: default SID to assign on errror
763 * Obtains a SID associated with the security context that
764 * has the string representation specified by @scontext.
765 * The default SID is passed to the MLS layer to be used to allow
766 * kernel labeling of the MLS field if the MLS field is not present
767 * (for upgrading to MLS without full relabel).
768 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
769 * memory is available, or 0 on success.
771 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
773 return security_context_to_sid_core(scontext, scontext_len,
777 static int compute_sid_handle_invalid_context(
778 struct context *scontext,
779 struct context *tcontext,
781 struct context *newcontext)
783 char *s = NULL, *t = NULL, *n = NULL;
784 u32 slen, tlen, nlen;
786 if (context_struct_to_string(scontext, &s, &slen) < 0)
788 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
790 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
792 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
793 "security_compute_sid: invalid context %s"
797 n, s, t, policydb.p_class_val_to_name[tclass-1]);
802 if (!selinux_enforcing)
807 static int security_compute_sid(u32 ssid,
813 struct context *scontext = NULL, *tcontext = NULL, newcontext;
814 struct role_trans *roletr = NULL;
815 struct avtab_key avkey;
816 struct avtab_datum *avdatum;
817 struct avtab_node *node;
820 if (!ss_initialized) {
822 case SECCLASS_PROCESS:
834 scontext = sidtab_search(&sidtab, ssid);
836 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
841 tcontext = sidtab_search(&sidtab, tsid);
843 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
849 context_init(&newcontext);
851 /* Set the user identity. */
853 case AVTAB_TRANSITION:
855 /* Use the process user identity. */
856 newcontext.user = scontext->user;
859 /* Use the related object owner. */
860 newcontext.user = tcontext->user;
864 /* Set the role and type to default values. */
866 case SECCLASS_PROCESS:
867 /* Use the current role and type of process. */
868 newcontext.role = scontext->role;
869 newcontext.type = scontext->type;
872 /* Use the well-defined object role. */
873 newcontext.role = OBJECT_R_VAL;
874 /* Use the type of the related object. */
875 newcontext.type = tcontext->type;
878 /* Look for a type transition/member/change rule. */
879 avkey.source_type = scontext->type;
880 avkey.target_type = tcontext->type;
881 avkey.target_class = tclass;
882 avkey.specified = specified;
883 avdatum = avtab_search(&policydb.te_avtab, &avkey);
885 /* If no permanent rule, also check for enabled conditional rules */
887 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
888 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
889 if (node->key.specified & AVTAB_ENABLED) {
890 avdatum = &node->datum;
897 /* Use the type from the type transition/member/change rule. */
898 newcontext.type = avdatum->data;
901 /* Check for class-specific changes. */
903 case SECCLASS_PROCESS:
904 if (specified & AVTAB_TRANSITION) {
905 /* Look for a role transition rule. */
906 for (roletr = policydb.role_tr; roletr;
907 roletr = roletr->next) {
908 if (roletr->role == scontext->role &&
909 roletr->type == tcontext->type) {
910 /* Use the role transition rule. */
911 newcontext.role = roletr->new_role;
921 /* Set the MLS attributes.
922 This is done last because it may allocate memory. */
923 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
927 /* Check the validity of the context. */
928 if (!policydb_context_isvalid(&policydb, &newcontext)) {
929 rc = compute_sid_handle_invalid_context(scontext,
936 /* Obtain the sid for the context. */
937 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
940 context_destroy(&newcontext);
946 * security_transition_sid - Compute the SID for a new subject/object.
947 * @ssid: source security identifier
948 * @tsid: target security identifier
949 * @tclass: target security class
950 * @out_sid: security identifier for new subject/object
952 * Compute a SID to use for labeling a new subject or object in the
953 * class @tclass based on a SID pair (@ssid, @tsid).
954 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
955 * if insufficient memory is available, or %0 if the new SID was
956 * computed successfully.
958 int security_transition_sid(u32 ssid,
963 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
967 * security_member_sid - Compute the SID for member selection.
968 * @ssid: source security identifier
969 * @tsid: target security identifier
970 * @tclass: target security class
971 * @out_sid: security identifier for selected member
973 * Compute a SID to use when selecting a member of a polyinstantiated
974 * object of class @tclass based on a SID pair (@ssid, @tsid).
975 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
976 * if insufficient memory is available, or %0 if the SID was
977 * computed successfully.
979 int security_member_sid(u32 ssid,
984 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
988 * security_change_sid - Compute the SID for object relabeling.
989 * @ssid: source security identifier
990 * @tsid: target security identifier
991 * @tclass: target security class
992 * @out_sid: security identifier for selected member
994 * Compute a SID to use for relabeling an object of class @tclass
995 * based on a SID pair (@ssid, @tsid).
996 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
997 * if insufficient memory is available, or %0 if the SID was
998 * computed successfully.
1000 int security_change_sid(u32 ssid,
1005 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1009 * Verify that each permission that is defined under the
1010 * existing policy is still defined with the same value
1011 * in the new policy.
1013 static int validate_perm(void *key, void *datum, void *p)
1016 struct perm_datum *perdatum, *perdatum2;
1023 perdatum2 = hashtab_search(h, key);
1025 printk(KERN_ERR "security: permission %s disappeared",
1030 if (perdatum->value != perdatum2->value) {
1031 printk(KERN_ERR "security: the value of permission %s changed",
1040 * Verify that each class that is defined under the
1041 * existing policy is still defined with the same
1042 * attributes in the new policy.
1044 static int validate_class(void *key, void *datum, void *p)
1046 struct policydb *newp;
1047 struct class_datum *cladatum, *cladatum2;
1053 cladatum2 = hashtab_search(newp->p_classes.table, key);
1055 printk(KERN_ERR "security: class %s disappeared\n",
1060 if (cladatum->value != cladatum2->value) {
1061 printk(KERN_ERR "security: the value of class %s changed\n",
1066 if ((cladatum->comdatum && !cladatum2->comdatum) ||
1067 (!cladatum->comdatum && cladatum2->comdatum)) {
1068 printk(KERN_ERR "security: the inherits clause for the access "
1069 "vector definition for class %s changed\n", (char *)key);
1073 if (cladatum->comdatum) {
1074 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
1075 cladatum2->comdatum->permissions.table);
1077 printk(" in the access vector definition for class "
1078 "%s\n", (char *)key);
1082 rc = hashtab_map(cladatum->permissions.table, validate_perm,
1083 cladatum2->permissions.table);
1085 printk(" in access vector definition for class %s\n",
1091 /* Clone the SID into the new SID table. */
1092 static int clone_sid(u32 sid,
1093 struct context *context,
1096 struct sidtab *s = arg;
1098 return sidtab_insert(s, sid, context);
1101 static inline int convert_context_handle_invalid_context(struct context *context)
1105 if (selinux_enforcing) {
1111 context_struct_to_string(context, &s, &len);
1112 printk(KERN_ERR "security: context %s is invalid\n", s);
1118 struct convert_context_args {
1119 struct policydb *oldp;
1120 struct policydb *newp;
1124 * Convert the values in the security context
1125 * structure `c' from the values specified
1126 * in the policy `p->oldp' to the values specified
1127 * in the policy `p->newp'. Verify that the
1128 * context is valid under the new policy.
1130 static int convert_context(u32 key,
1134 struct convert_context_args *args;
1135 struct context oldc;
1136 struct role_datum *role;
1137 struct type_datum *typdatum;
1138 struct user_datum *usrdatum;
1145 rc = context_cpy(&oldc, c);
1151 /* Convert the user. */
1152 usrdatum = hashtab_search(args->newp->p_users.table,
1153 args->oldp->p_user_val_to_name[c->user - 1]);
1157 c->user = usrdatum->value;
1159 /* Convert the role. */
1160 role = hashtab_search(args->newp->p_roles.table,
1161 args->oldp->p_role_val_to_name[c->role - 1]);
1165 c->role = role->value;
1167 /* Convert the type. */
1168 typdatum = hashtab_search(args->newp->p_types.table,
1169 args->oldp->p_type_val_to_name[c->type - 1]);
1173 c->type = typdatum->value;
1175 rc = mls_convert_context(args->oldp, args->newp, c);
1179 /* Check the validity of the new context. */
1180 if (!policydb_context_isvalid(args->newp, c)) {
1181 rc = convert_context_handle_invalid_context(&oldc);
1186 context_destroy(&oldc);
1190 context_struct_to_string(&oldc, &s, &len);
1191 context_destroy(&oldc);
1192 printk(KERN_ERR "security: invalidating context %s\n", s);
1197 extern void selinux_complete_init(void);
1200 * security_load_policy - Load a security policy configuration.
1201 * @data: binary policy data
1202 * @len: length of data in bytes
1204 * Load a new set of security policy configuration data,
1205 * validate it and convert the SID table as necessary.
1206 * This function will flush the access vector cache after
1207 * loading the new policy.
1209 int security_load_policy(void *data, size_t len)
1211 struct policydb oldpolicydb, newpolicydb;
1212 struct sidtab oldsidtab, newsidtab;
1213 struct convert_context_args args;
1216 struct policy_file file = { data, len }, *fp = &file;
1220 if (!ss_initialized) {
1222 if (policydb_read(&policydb, fp)) {
1224 avtab_cache_destroy();
1227 if (policydb_load_isids(&policydb, &sidtab)) {
1229 policydb_destroy(&policydb);
1230 avtab_cache_destroy();
1233 policydb_loaded_version = policydb.policyvers;
1235 seqno = ++latest_granting;
1237 selinux_complete_init();
1238 avc_ss_reset(seqno);
1239 selnl_notify_policyload(seqno);
1244 sidtab_hash_eval(&sidtab, "sids");
1247 if (policydb_read(&newpolicydb, fp)) {
1252 sidtab_init(&newsidtab);
1254 /* Verify that the existing classes did not change. */
1255 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1256 printk(KERN_ERR "security: the definition of an existing "
1262 /* Clone the SID table. */
1263 sidtab_shutdown(&sidtab);
1264 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1269 /* Convert the internal representations of contexts
1270 in the new SID table and remove invalid SIDs. */
1271 args.oldp = &policydb;
1272 args.newp = &newpolicydb;
1273 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1275 /* Save the old policydb and SID table to free later. */
1276 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1277 sidtab_set(&oldsidtab, &sidtab);
1279 /* Install the new policydb and SID table. */
1281 memcpy(&policydb, &newpolicydb, sizeof policydb);
1282 sidtab_set(&sidtab, &newsidtab);
1283 seqno = ++latest_granting;
1284 policydb_loaded_version = policydb.policyvers;
1288 /* Free the old policydb and SID table. */
1289 policydb_destroy(&oldpolicydb);
1290 sidtab_destroy(&oldsidtab);
1292 avc_ss_reset(seqno);
1293 selnl_notify_policyload(seqno);
1299 sidtab_destroy(&newsidtab);
1300 policydb_destroy(&newpolicydb);
1306 * security_port_sid - Obtain the SID for a port.
1307 * @domain: communication domain aka address family
1308 * @type: socket type
1309 * @protocol: protocol number
1310 * @port: port number
1311 * @out_sid: security identifier
1313 int security_port_sid(u16 domain,
1324 c = policydb.ocontexts[OCON_PORT];
1326 if (c->u.port.protocol == protocol &&
1327 c->u.port.low_port <= port &&
1328 c->u.port.high_port >= port)
1335 rc = sidtab_context_to_sid(&sidtab,
1341 *out_sid = c->sid[0];
1343 *out_sid = SECINITSID_PORT;
1352 * security_netif_sid - Obtain the SID for a network interface.
1353 * @name: interface name
1354 * @if_sid: interface SID
1355 * @msg_sid: default SID for received packets
1357 int security_netif_sid(char *name,
1366 c = policydb.ocontexts[OCON_NETIF];
1368 if (strcmp(name, c->u.name) == 0)
1374 if (!c->sid[0] || !c->sid[1]) {
1375 rc = sidtab_context_to_sid(&sidtab,
1380 rc = sidtab_context_to_sid(&sidtab,
1386 *if_sid = c->sid[0];
1387 *msg_sid = c->sid[1];
1389 *if_sid = SECINITSID_NETIF;
1390 *msg_sid = SECINITSID_NETMSG;
1398 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1402 for(i = 0; i < 4; i++)
1403 if(addr[i] != (input[i] & mask[i])) {
1412 * security_node_sid - Obtain the SID for a node (host).
1413 * @domain: communication domain aka address family
1415 * @addrlen: address length in bytes
1416 * @out_sid: security identifier
1418 int security_node_sid(u16 domain,
1432 if (addrlen != sizeof(u32)) {
1437 addr = *((u32 *)addrp);
1439 c = policydb.ocontexts[OCON_NODE];
1441 if (c->u.node.addr == (addr & c->u.node.mask))
1449 if (addrlen != sizeof(u64) * 2) {
1453 c = policydb.ocontexts[OCON_NODE6];
1455 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1463 *out_sid = SECINITSID_NODE;
1469 rc = sidtab_context_to_sid(&sidtab,
1475 *out_sid = c->sid[0];
1477 *out_sid = SECINITSID_NODE;
1488 * security_get_user_sids - Obtain reachable SIDs for a user.
1489 * @fromsid: starting SID
1490 * @username: username
1491 * @sids: array of reachable SIDs for user
1492 * @nel: number of elements in @sids
1494 * Generate the set of SIDs for legal security contexts
1495 * for a given user that can be reached by @fromsid.
1496 * Set *@sids to point to a dynamically allocated
1497 * array containing the set of SIDs. Set *@nel to the
1498 * number of elements in the array.
1501 int security_get_user_sids(u32 fromsid,
1506 struct context *fromcon, usercon;
1507 u32 *mysids, *mysids2, sid;
1508 u32 mynel = 0, maxnel = SIDS_NEL;
1509 struct user_datum *user;
1510 struct role_datum *role;
1511 struct av_decision avd;
1512 struct ebitmap_node *rnode, *tnode;
1515 if (!ss_initialized) {
1523 fromcon = sidtab_search(&sidtab, fromsid);
1529 user = hashtab_search(policydb.p_users.table, username);
1534 usercon.user = user->value;
1536 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1542 ebitmap_for_each_bit(&user->roles, rnode, i) {
1543 if (!ebitmap_node_get_bit(rnode, i))
1545 role = policydb.role_val_to_struct[i];
1547 ebitmap_for_each_bit(&role->types, tnode, j) {
1548 if (!ebitmap_node_get_bit(tnode, j))
1552 if (mls_setup_user_range(fromcon, user, &usercon))
1555 rc = context_struct_compute_av(fromcon, &usercon,
1557 PROCESS__TRANSITION,
1559 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1561 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1566 if (mynel < maxnel) {
1567 mysids[mynel++] = sid;
1570 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1576 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1579 mysids[mynel++] = sid;
1594 * security_genfs_sid - Obtain a SID for a file in a filesystem
1595 * @fstype: filesystem type
1596 * @path: path from root of mount
1597 * @sclass: file security class
1598 * @sid: SID for path
1600 * Obtain a SID to use for a file in a filesystem that
1601 * cannot support xattr or use a fixed labeling behavior like
1602 * transition SIDs or task SIDs.
1604 int security_genfs_sid(const char *fstype,
1610 struct genfs *genfs;
1612 int rc = 0, cmp = 0;
1616 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1617 cmp = strcmp(fstype, genfs->fstype);
1622 if (!genfs || cmp) {
1623 *sid = SECINITSID_UNLABELED;
1628 for (c = genfs->head; c; c = c->next) {
1629 len = strlen(c->u.name);
1630 if ((!c->v.sclass || sclass == c->v.sclass) &&
1631 (strncmp(c->u.name, path, len) == 0))
1636 *sid = SECINITSID_UNLABELED;
1642 rc = sidtab_context_to_sid(&sidtab,
1656 * security_fs_use - Determine how to handle labeling for a filesystem.
1657 * @fstype: filesystem type
1658 * @behavior: labeling behavior
1659 * @sid: SID for filesystem (superblock)
1661 int security_fs_use(
1663 unsigned int *behavior,
1671 c = policydb.ocontexts[OCON_FSUSE];
1673 if (strcmp(fstype, c->u.name) == 0)
1679 *behavior = c->v.behavior;
1681 rc = sidtab_context_to_sid(&sidtab,
1689 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1691 *behavior = SECURITY_FS_USE_NONE;
1694 *behavior = SECURITY_FS_USE_GENFS;
1703 int security_get_bools(int *len, char ***names, int **values)
1705 int i, rc = -ENOMEM;
1711 *len = policydb.p_bools.nprim;
1717 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1721 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1725 for (i = 0; i < *len; i++) {
1727 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1728 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1729 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1732 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1733 (*names)[i][name_len - 1] = 0;
1741 for (i = 0; i < *len; i++)
1749 int security_set_bools(int len, int *values)
1752 int lenp, seqno = 0;
1753 struct cond_node *cur;
1757 lenp = policydb.p_bools.nprim;
1763 for (i = 0; i < len; i++) {
1764 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1765 audit_log(current->audit_context, GFP_ATOMIC,
1766 AUDIT_MAC_CONFIG_CHANGE,
1767 "bool=%s val=%d old_val=%d auid=%u",
1768 policydb.p_bool_val_to_name[i],
1770 policydb.bool_val_to_struct[i]->state,
1771 audit_get_loginuid(current->audit_context));
1774 policydb.bool_val_to_struct[i]->state = 1;
1776 policydb.bool_val_to_struct[i]->state = 0;
1780 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1781 rc = evaluate_cond_node(&policydb, cur);
1786 seqno = ++latest_granting;
1791 avc_ss_reset(seqno);
1792 selnl_notify_policyload(seqno);
1797 int security_get_bool_value(int bool)
1804 len = policydb.p_bools.nprim;
1810 rc = policydb.bool_val_to_struct[bool]->state;
1816 struct selinux_audit_rule {
1818 struct context au_ctxt;
1821 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1824 context_destroy(&rule->au_ctxt);
1829 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1830 struct selinux_audit_rule **rule)
1832 struct selinux_audit_rule *tmprule;
1833 struct role_datum *roledatum;
1834 struct type_datum *typedatum;
1835 struct user_datum *userdatum;
1840 if (!ss_initialized)
1847 /* only 'equals' and 'not equals' fit user, role, and type */
1848 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1853 /* we do not allow a range, indicated by the presense of '-' */
1854 if (strchr(rulestr, '-'))
1858 /* only the above fields are valid */
1862 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
1866 context_init(&tmprule->au_ctxt);
1870 tmprule->au_seqno = latest_granting;
1874 userdatum = hashtab_search(policydb.p_users.table, rulestr);
1878 tmprule->au_ctxt.user = userdatum->value;
1881 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
1885 tmprule->au_ctxt.role = roledatum->value;
1888 typedatum = hashtab_search(policydb.p_types.table, rulestr);
1892 tmprule->au_ctxt.type = typedatum->value;
1896 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
1903 selinux_audit_rule_free(tmprule);
1912 int selinux_audit_rule_match(u32 ctxid, u32 field, u32 op,
1913 struct selinux_audit_rule *rule,
1914 struct audit_context *actx)
1916 struct context *ctxt;
1917 struct mls_level *level;
1921 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1922 "selinux_audit_rule_match: missing rule\n");
1928 if (rule->au_seqno < latest_granting) {
1929 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1930 "selinux_audit_rule_match: stale rule\n");
1935 ctxt = sidtab_search(&sidtab, ctxid);
1937 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1938 "selinux_audit_rule_match: unrecognized SID %d\n",
1944 /* a field/op pair that is not caught here will simply fall through
1950 match = (ctxt->user == rule->au_ctxt.user);
1952 case AUDIT_NOT_EQUAL:
1953 match = (ctxt->user != rule->au_ctxt.user);
1960 match = (ctxt->role == rule->au_ctxt.role);
1962 case AUDIT_NOT_EQUAL:
1963 match = (ctxt->role != rule->au_ctxt.role);
1970 match = (ctxt->type == rule->au_ctxt.type);
1972 case AUDIT_NOT_EQUAL:
1973 match = (ctxt->type != rule->au_ctxt.type);
1979 level = (op == AUDIT_SE_SEN ?
1980 &ctxt->range.level[0] : &ctxt->range.level[1]);
1983 match = mls_level_eq(&rule->au_ctxt.range.level[0],
1986 case AUDIT_NOT_EQUAL:
1987 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
1990 case AUDIT_LESS_THAN:
1991 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
1993 !mls_level_eq(&rule->au_ctxt.range.level[0],
1996 case AUDIT_LESS_THAN_OR_EQUAL:
1997 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2000 case AUDIT_GREATER_THAN:
2001 match = (mls_level_dom(level,
2002 &rule->au_ctxt.range.level[0]) &&
2003 !mls_level_eq(level,
2004 &rule->au_ctxt.range.level[0]));
2006 case AUDIT_GREATER_THAN_OR_EQUAL:
2007 match = mls_level_dom(level,
2008 &rule->au_ctxt.range.level[0]);
2018 static int (*aurule_callback)(void) = NULL;
2020 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2021 u16 class, u32 perms, u32 *retained)
2025 if (event == AVC_CALLBACK_RESET && aurule_callback)
2026 err = aurule_callback();
2030 static int __init aurule_init(void)
2034 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2035 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2037 panic("avc_add_callback() failed, error %d\n", err);
2041 __initcall(aurule_init);
2043 void selinux_audit_set_callback(int (*callback)(void))
2045 aurule_callback = callback;