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;
298 * Remap extended Netlink classes for old policy versions.
299 * Do this here rather than socket_type_to_security_class()
300 * in case a newer policy version is loaded, allowing sockets
301 * to remain in the correct class.
303 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
304 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
305 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
306 tclass = SECCLASS_NETLINK_SOCKET;
308 if (!tclass || tclass > policydb.p_classes.nprim) {
309 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
313 tclass_datum = policydb.class_val_to_struct[tclass - 1];
316 * Initialize the access vectors to the default values.
319 avd->decided = 0xffffffff;
321 avd->auditdeny = 0xffffffff;
322 avd->seqno = latest_granting;
325 * If a specific type enforcement rule was defined for
326 * this permission check, then use it.
328 avkey.target_class = tclass;
329 avkey.specified = AVTAB_AV;
330 sattr = &policydb.type_attr_map[scontext->type - 1];
331 tattr = &policydb.type_attr_map[tcontext->type - 1];
332 ebitmap_for_each_bit(sattr, snode, i) {
333 if (!ebitmap_node_get_bit(snode, i))
335 ebitmap_for_each_bit(tattr, tnode, j) {
336 if (!ebitmap_node_get_bit(tnode, j))
338 avkey.source_type = i + 1;
339 avkey.target_type = j + 1;
340 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
342 node = avtab_search_node_next(node, avkey.specified)) {
343 if (node->key.specified == AVTAB_ALLOWED)
344 avd->allowed |= node->datum.data;
345 else if (node->key.specified == AVTAB_AUDITALLOW)
346 avd->auditallow |= node->datum.data;
347 else if (node->key.specified == AVTAB_AUDITDENY)
348 avd->auditdeny &= node->datum.data;
351 /* Check conditional av table for additional permissions */
352 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
358 * Remove any permissions prohibited by a constraint (this includes
361 constraint = tclass_datum->constraints;
363 if ((constraint->permissions & (avd->allowed)) &&
364 !constraint_expr_eval(scontext, tcontext, NULL,
366 avd->allowed = (avd->allowed) & ~(constraint->permissions);
368 constraint = constraint->next;
372 * If checking process transition permission and the
373 * role is changing, then check the (current_role, new_role)
376 if (tclass == SECCLASS_PROCESS &&
377 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
378 scontext->role != tcontext->role) {
379 for (ra = policydb.role_allow; ra; ra = ra->next) {
380 if (scontext->role == ra->role &&
381 tcontext->role == ra->new_role)
385 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
386 PROCESS__DYNTRANSITION);
392 static int security_validtrans_handle_fail(struct context *ocontext,
393 struct context *ncontext,
394 struct context *tcontext,
397 char *o = NULL, *n = NULL, *t = NULL;
398 u32 olen, nlen, tlen;
400 if (context_struct_to_string(ocontext, &o, &olen) < 0)
402 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
404 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
406 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
407 "security_validate_transition: denied for"
408 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
409 o, n, t, policydb.p_class_val_to_name[tclass-1]);
415 if (!selinux_enforcing)
420 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
423 struct context *ocontext;
424 struct context *ncontext;
425 struct context *tcontext;
426 struct class_datum *tclass_datum;
427 struct constraint_node *constraint;
436 * Remap extended Netlink classes for old policy versions.
437 * Do this here rather than socket_type_to_security_class()
438 * in case a newer policy version is loaded, allowing sockets
439 * to remain in the correct class.
441 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
442 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
443 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
444 tclass = SECCLASS_NETLINK_SOCKET;
446 if (!tclass || tclass > policydb.p_classes.nprim) {
447 printk(KERN_ERR "security_validate_transition: "
448 "unrecognized class %d\n", tclass);
452 tclass_datum = policydb.class_val_to_struct[tclass - 1];
454 ocontext = sidtab_search(&sidtab, oldsid);
456 printk(KERN_ERR "security_validate_transition: "
457 " unrecognized SID %d\n", oldsid);
462 ncontext = sidtab_search(&sidtab, newsid);
464 printk(KERN_ERR "security_validate_transition: "
465 " unrecognized SID %d\n", newsid);
470 tcontext = sidtab_search(&sidtab, tasksid);
472 printk(KERN_ERR "security_validate_transition: "
473 " unrecognized SID %d\n", tasksid);
478 constraint = tclass_datum->validatetrans;
480 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
482 rc = security_validtrans_handle_fail(ocontext, ncontext,
486 constraint = constraint->next;
495 * security_compute_av - Compute access vector decisions.
496 * @ssid: source security identifier
497 * @tsid: target security identifier
498 * @tclass: target security class
499 * @requested: requested permissions
500 * @avd: access vector decisions
502 * Compute a set of access vector decisions based on the
503 * SID pair (@ssid, @tsid) for the permissions in @tclass.
504 * Return -%EINVAL if any of the parameters are invalid or %0
505 * if the access vector decisions were computed successfully.
507 int security_compute_av(u32 ssid,
511 struct av_decision *avd)
513 struct context *scontext = NULL, *tcontext = NULL;
516 if (!ss_initialized) {
517 avd->allowed = 0xffffffff;
518 avd->decided = 0xffffffff;
520 avd->auditdeny = 0xffffffff;
521 avd->seqno = latest_granting;
527 scontext = sidtab_search(&sidtab, ssid);
529 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
534 tcontext = sidtab_search(&sidtab, tsid);
536 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
542 rc = context_struct_compute_av(scontext, tcontext, tclass,
550 * Write the security context string representation of
551 * the context structure `context' into a dynamically
552 * allocated string of the correct size. Set `*scontext'
553 * to point to this string and set `*scontext_len' to
554 * the length of the string.
556 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
563 /* Compute the size of the context. */
564 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
565 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
566 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
567 *scontext_len += mls_compute_context_len(context);
569 /* Allocate space for the context; caller must free this space. */
570 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
574 *scontext = scontextp;
577 * Copy the user name, role name and type name into the context.
579 sprintf(scontextp, "%s:%s:%s",
580 policydb.p_user_val_to_name[context->user - 1],
581 policydb.p_role_val_to_name[context->role - 1],
582 policydb.p_type_val_to_name[context->type - 1]);
583 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
584 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
585 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
587 mls_sid_to_context(context, &scontextp);
594 #include "initial_sid_to_string.h"
596 const char *security_get_initial_sid_context(u32 sid)
598 if (unlikely(sid > SECINITSID_NUM))
600 return initial_sid_to_string[sid];
604 * security_sid_to_context - Obtain a context for a given SID.
605 * @sid: security identifier, SID
606 * @scontext: security context
607 * @scontext_len: length in bytes
609 * Write the string representation of the context associated with @sid
610 * into a dynamically allocated string of the correct size. Set @scontext
611 * to point to this string and set @scontext_len to the length of the string.
613 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
615 struct context *context;
621 if (!ss_initialized) {
622 if (sid <= SECINITSID_NUM) {
625 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
626 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
631 strcpy(scontextp, initial_sid_to_string[sid]);
632 *scontext = scontextp;
635 printk(KERN_ERR "security_sid_to_context: called before initial "
636 "load_policy on unknown SID %d\n", sid);
641 context = sidtab_search(&sidtab, sid);
643 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
648 rc = context_struct_to_string(context, scontext, scontext_len);
656 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
659 struct context context;
660 struct role_datum *role;
661 struct type_datum *typdatum;
662 struct user_datum *usrdatum;
663 char *scontextp, *p, oldc;
666 if (!ss_initialized) {
669 for (i = 1; i < SECINITSID_NUM; i++) {
670 if (!strcmp(initial_sid_to_string[i], scontext)) {
675 *sid = SECINITSID_KERNEL;
680 /* Copy the string so that we can modify the copy as we parse it.
681 The string should already by null terminated, but we append a
682 null suffix to the copy to avoid problems with the existing
683 attr package, which doesn't view the null terminator as part
684 of the attribute value. */
685 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
690 memcpy(scontext2, scontext, scontext_len);
691 scontext2[scontext_len] = 0;
693 context_init(&context);
698 /* Parse the security context. */
701 scontextp = (char *) scontext2;
703 /* Extract the user. */
705 while (*p && *p != ':')
713 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
717 context.user = usrdatum->value;
721 while (*p && *p != ':')
729 role = hashtab_search(policydb.p_roles.table, scontextp);
732 context.role = role->value;
736 while (*p && *p != ':')
741 typdatum = hashtab_search(policydb.p_types.table, scontextp);
745 context.type = typdatum->value;
747 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
751 if ((p - scontext2) < scontext_len) {
756 /* Check the validity of the new context. */
757 if (!policydb_context_isvalid(&policydb, &context)) {
761 /* Obtain the new sid. */
762 rc = sidtab_context_to_sid(&sidtab, &context, sid);
765 context_destroy(&context);
772 * security_context_to_sid - Obtain a SID for a given security context.
773 * @scontext: security context
774 * @scontext_len: length in bytes
775 * @sid: security identifier, SID
777 * Obtains a SID associated with the security context that
778 * has the string representation specified by @scontext.
779 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
780 * memory is available, or 0 on success.
782 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
784 return security_context_to_sid_core(scontext, scontext_len,
789 * security_context_to_sid_default - Obtain a SID for a given security context,
790 * falling back to specified default if needed.
792 * @scontext: security context
793 * @scontext_len: length in bytes
794 * @sid: security identifier, SID
795 * @def_sid: default SID to assign on errror
797 * Obtains a SID associated with the security context that
798 * has the string representation specified by @scontext.
799 * The default SID is passed to the MLS layer to be used to allow
800 * kernel labeling of the MLS field if the MLS field is not present
801 * (for upgrading to MLS without full relabel).
802 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
803 * memory is available, or 0 on success.
805 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
807 return security_context_to_sid_core(scontext, scontext_len,
811 static int compute_sid_handle_invalid_context(
812 struct context *scontext,
813 struct context *tcontext,
815 struct context *newcontext)
817 char *s = NULL, *t = NULL, *n = NULL;
818 u32 slen, tlen, nlen;
820 if (context_struct_to_string(scontext, &s, &slen) < 0)
822 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
824 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
826 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
827 "security_compute_sid: invalid context %s"
831 n, s, t, policydb.p_class_val_to_name[tclass-1]);
836 if (!selinux_enforcing)
841 static int security_compute_sid(u32 ssid,
847 struct context *scontext = NULL, *tcontext = NULL, newcontext;
848 struct role_trans *roletr = NULL;
849 struct avtab_key avkey;
850 struct avtab_datum *avdatum;
851 struct avtab_node *node;
854 if (!ss_initialized) {
856 case SECCLASS_PROCESS:
866 context_init(&newcontext);
870 scontext = sidtab_search(&sidtab, ssid);
872 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
877 tcontext = sidtab_search(&sidtab, tsid);
879 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
885 /* Set the user identity. */
887 case AVTAB_TRANSITION:
889 /* Use the process user identity. */
890 newcontext.user = scontext->user;
893 /* Use the related object owner. */
894 newcontext.user = tcontext->user;
898 /* Set the role and type to default values. */
900 case SECCLASS_PROCESS:
901 /* Use the current role and type of process. */
902 newcontext.role = scontext->role;
903 newcontext.type = scontext->type;
906 /* Use the well-defined object role. */
907 newcontext.role = OBJECT_R_VAL;
908 /* Use the type of the related object. */
909 newcontext.type = tcontext->type;
912 /* Look for a type transition/member/change rule. */
913 avkey.source_type = scontext->type;
914 avkey.target_type = tcontext->type;
915 avkey.target_class = tclass;
916 avkey.specified = specified;
917 avdatum = avtab_search(&policydb.te_avtab, &avkey);
919 /* If no permanent rule, also check for enabled conditional rules */
921 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
922 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
923 if (node->key.specified & AVTAB_ENABLED) {
924 avdatum = &node->datum;
931 /* Use the type from the type transition/member/change rule. */
932 newcontext.type = avdatum->data;
935 /* Check for class-specific changes. */
937 case SECCLASS_PROCESS:
938 if (specified & AVTAB_TRANSITION) {
939 /* Look for a role transition rule. */
940 for (roletr = policydb.role_tr; roletr;
941 roletr = roletr->next) {
942 if (roletr->role == scontext->role &&
943 roletr->type == tcontext->type) {
944 /* Use the role transition rule. */
945 newcontext.role = roletr->new_role;
955 /* Set the MLS attributes.
956 This is done last because it may allocate memory. */
957 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
961 /* Check the validity of the context. */
962 if (!policydb_context_isvalid(&policydb, &newcontext)) {
963 rc = compute_sid_handle_invalid_context(scontext,
970 /* Obtain the sid for the context. */
971 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
974 context_destroy(&newcontext);
980 * security_transition_sid - Compute the SID for a new subject/object.
981 * @ssid: source security identifier
982 * @tsid: target security identifier
983 * @tclass: target security class
984 * @out_sid: security identifier for new subject/object
986 * Compute a SID to use for labeling a new subject or object in the
987 * class @tclass based on a SID pair (@ssid, @tsid).
988 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
989 * if insufficient memory is available, or %0 if the new SID was
990 * computed successfully.
992 int security_transition_sid(u32 ssid,
997 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1001 * security_member_sid - Compute the SID for member selection.
1002 * @ssid: source security identifier
1003 * @tsid: target security identifier
1004 * @tclass: target security class
1005 * @out_sid: security identifier for selected member
1007 * Compute a SID to use when selecting a member of a polyinstantiated
1008 * object of class @tclass based on a SID pair (@ssid, @tsid).
1009 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1010 * if insufficient memory is available, or %0 if the SID was
1011 * computed successfully.
1013 int security_member_sid(u32 ssid,
1018 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1022 * security_change_sid - Compute the SID for object relabeling.
1023 * @ssid: source security identifier
1024 * @tsid: target security identifier
1025 * @tclass: target security class
1026 * @out_sid: security identifier for selected member
1028 * Compute a SID to use for relabeling an object of class @tclass
1029 * based on a SID pair (@ssid, @tsid).
1030 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1031 * if insufficient memory is available, or %0 if the SID was
1032 * computed successfully.
1034 int security_change_sid(u32 ssid,
1039 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1043 * Verify that each kernel class that is defined in the
1046 static int validate_classes(struct policydb *p)
1049 struct class_datum *cladatum;
1050 struct perm_datum *perdatum;
1051 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1053 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1054 const char *def_class, *def_perm, *pol_class;
1055 struct symtab *perms;
1057 for (i = 1; i < kdefs->cts_len; i++) {
1058 def_class = kdefs->class_to_string[i];
1061 if (i > p->p_classes.nprim) {
1063 "security: class %s not defined in policy\n",
1067 pol_class = p->p_class_val_to_name[i-1];
1068 if (strcmp(pol_class, def_class)) {
1070 "security: class %d is incorrect, found %s but should be %s\n",
1071 i, pol_class, def_class);
1075 for (i = 0; i < kdefs->av_pts_len; i++) {
1076 class_val = kdefs->av_perm_to_string[i].tclass;
1077 perm_val = kdefs->av_perm_to_string[i].value;
1078 def_perm = kdefs->av_perm_to_string[i].name;
1079 if (class_val > p->p_classes.nprim)
1081 pol_class = p->p_class_val_to_name[class_val-1];
1082 cladatum = hashtab_search(p->p_classes.table, pol_class);
1084 perms = &cladatum->permissions;
1085 nprim = 1 << (perms->nprim - 1);
1086 if (perm_val > nprim) {
1088 "security: permission %s in class %s not defined in policy\n",
1089 def_perm, pol_class);
1092 perdatum = hashtab_search(perms->table, def_perm);
1093 if (perdatum == NULL) {
1095 "security: permission %s in class %s not found in policy\n",
1096 def_perm, pol_class);
1099 pol_val = 1 << (perdatum->value - 1);
1100 if (pol_val != perm_val) {
1102 "security: permission %s in class %s has incorrect value\n",
1103 def_perm, pol_class);
1107 for (i = 0; i < kdefs->av_inherit_len; i++) {
1108 class_val = kdefs->av_inherit[i].tclass;
1109 if (class_val > p->p_classes.nprim)
1111 pol_class = p->p_class_val_to_name[class_val-1];
1112 cladatum = hashtab_search(p->p_classes.table, pol_class);
1114 if (!cladatum->comdatum) {
1116 "security: class %s should have an inherits clause but does not\n",
1120 tmp = kdefs->av_inherit[i].common_base;
1122 while (!(tmp & 0x01)) {
1126 perms = &cladatum->comdatum->permissions;
1127 for (j = 0; j < common_pts_len; j++) {
1128 def_perm = kdefs->av_inherit[i].common_pts[j];
1129 if (j >= perms->nprim) {
1131 "security: permission %s in class %s not defined in policy\n",
1132 def_perm, pol_class);
1135 perdatum = hashtab_search(perms->table, def_perm);
1136 if (perdatum == NULL) {
1138 "security: permission %s in class %s not found in policy\n",
1139 def_perm, pol_class);
1142 if (perdatum->value != j + 1) {
1144 "security: permission %s in class %s has incorrect value\n",
1145 def_perm, pol_class);
1153 /* Clone the SID into the new SID table. */
1154 static int clone_sid(u32 sid,
1155 struct context *context,
1158 struct sidtab *s = arg;
1160 return sidtab_insert(s, sid, context);
1163 static inline int convert_context_handle_invalid_context(struct context *context)
1167 if (selinux_enforcing) {
1173 context_struct_to_string(context, &s, &len);
1174 printk(KERN_ERR "security: context %s is invalid\n", s);
1180 struct convert_context_args {
1181 struct policydb *oldp;
1182 struct policydb *newp;
1186 * Convert the values in the security context
1187 * structure `c' from the values specified
1188 * in the policy `p->oldp' to the values specified
1189 * in the policy `p->newp'. Verify that the
1190 * context is valid under the new policy.
1192 static int convert_context(u32 key,
1196 struct convert_context_args *args;
1197 struct context oldc;
1198 struct role_datum *role;
1199 struct type_datum *typdatum;
1200 struct user_datum *usrdatum;
1207 rc = context_cpy(&oldc, c);
1213 /* Convert the user. */
1214 usrdatum = hashtab_search(args->newp->p_users.table,
1215 args->oldp->p_user_val_to_name[c->user - 1]);
1219 c->user = usrdatum->value;
1221 /* Convert the role. */
1222 role = hashtab_search(args->newp->p_roles.table,
1223 args->oldp->p_role_val_to_name[c->role - 1]);
1227 c->role = role->value;
1229 /* Convert the type. */
1230 typdatum = hashtab_search(args->newp->p_types.table,
1231 args->oldp->p_type_val_to_name[c->type - 1]);
1235 c->type = typdatum->value;
1237 rc = mls_convert_context(args->oldp, args->newp, c);
1241 /* Check the validity of the new context. */
1242 if (!policydb_context_isvalid(args->newp, c)) {
1243 rc = convert_context_handle_invalid_context(&oldc);
1248 context_destroy(&oldc);
1252 context_struct_to_string(&oldc, &s, &len);
1253 context_destroy(&oldc);
1254 printk(KERN_ERR "security: invalidating context %s\n", s);
1259 extern void selinux_complete_init(void);
1260 static int security_preserve_bools(struct policydb *p);
1263 * security_load_policy - Load a security policy configuration.
1264 * @data: binary policy data
1265 * @len: length of data in bytes
1267 * Load a new set of security policy configuration data,
1268 * validate it and convert the SID table as necessary.
1269 * This function will flush the access vector cache after
1270 * loading the new policy.
1272 int security_load_policy(void *data, size_t len)
1274 struct policydb oldpolicydb, newpolicydb;
1275 struct sidtab oldsidtab, newsidtab;
1276 struct convert_context_args args;
1279 struct policy_file file = { data, len }, *fp = &file;
1283 if (!ss_initialized) {
1285 if (policydb_read(&policydb, fp)) {
1287 avtab_cache_destroy();
1290 if (policydb_load_isids(&policydb, &sidtab)) {
1292 policydb_destroy(&policydb);
1293 avtab_cache_destroy();
1296 /* Verify that the kernel defined classes are correct. */
1297 if (validate_classes(&policydb)) {
1299 "security: the definition of a class is incorrect\n");
1301 sidtab_destroy(&sidtab);
1302 policydb_destroy(&policydb);
1303 avtab_cache_destroy();
1306 policydb_loaded_version = policydb.policyvers;
1308 seqno = ++latest_granting;
1310 selinux_complete_init();
1311 avc_ss_reset(seqno);
1312 selnl_notify_policyload(seqno);
1313 selinux_netlbl_cache_invalidate();
1314 selinux_xfrm_notify_policyload();
1319 sidtab_hash_eval(&sidtab, "sids");
1322 if (policydb_read(&newpolicydb, fp)) {
1327 sidtab_init(&newsidtab);
1329 /* Verify that the kernel defined classes are correct. */
1330 if (validate_classes(&newpolicydb)) {
1332 "security: the definition of a class is incorrect\n");
1337 rc = security_preserve_bools(&newpolicydb);
1339 printk(KERN_ERR "security: unable to preserve booleans\n");
1343 /* Clone the SID table. */
1344 sidtab_shutdown(&sidtab);
1345 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1350 /* Convert the internal representations of contexts
1351 in the new SID table and remove invalid SIDs. */
1352 args.oldp = &policydb;
1353 args.newp = &newpolicydb;
1354 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1356 /* Save the old policydb and SID table to free later. */
1357 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1358 sidtab_set(&oldsidtab, &sidtab);
1360 /* Install the new policydb and SID table. */
1362 memcpy(&policydb, &newpolicydb, sizeof policydb);
1363 sidtab_set(&sidtab, &newsidtab);
1364 seqno = ++latest_granting;
1365 policydb_loaded_version = policydb.policyvers;
1369 /* Free the old policydb and SID table. */
1370 policydb_destroy(&oldpolicydb);
1371 sidtab_destroy(&oldsidtab);
1373 avc_ss_reset(seqno);
1374 selnl_notify_policyload(seqno);
1375 selinux_netlbl_cache_invalidate();
1376 selinux_xfrm_notify_policyload();
1382 sidtab_destroy(&newsidtab);
1383 policydb_destroy(&newpolicydb);
1389 * security_port_sid - Obtain the SID for a port.
1390 * @domain: communication domain aka address family
1391 * @type: socket type
1392 * @protocol: protocol number
1393 * @port: port number
1394 * @out_sid: security identifier
1396 int security_port_sid(u16 domain,
1407 c = policydb.ocontexts[OCON_PORT];
1409 if (c->u.port.protocol == protocol &&
1410 c->u.port.low_port <= port &&
1411 c->u.port.high_port >= port)
1418 rc = sidtab_context_to_sid(&sidtab,
1424 *out_sid = c->sid[0];
1426 *out_sid = SECINITSID_PORT;
1435 * security_netif_sid - Obtain the SID for a network interface.
1436 * @name: interface name
1437 * @if_sid: interface SID
1438 * @msg_sid: default SID for received packets
1440 int security_netif_sid(char *name,
1449 c = policydb.ocontexts[OCON_NETIF];
1451 if (strcmp(name, c->u.name) == 0)
1457 if (!c->sid[0] || !c->sid[1]) {
1458 rc = sidtab_context_to_sid(&sidtab,
1463 rc = sidtab_context_to_sid(&sidtab,
1469 *if_sid = c->sid[0];
1470 *msg_sid = c->sid[1];
1472 *if_sid = SECINITSID_NETIF;
1473 *msg_sid = SECINITSID_NETMSG;
1481 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1485 for(i = 0; i < 4; i++)
1486 if(addr[i] != (input[i] & mask[i])) {
1495 * security_node_sid - Obtain the SID for a node (host).
1496 * @domain: communication domain aka address family
1498 * @addrlen: address length in bytes
1499 * @out_sid: security identifier
1501 int security_node_sid(u16 domain,
1515 if (addrlen != sizeof(u32)) {
1520 addr = *((u32 *)addrp);
1522 c = policydb.ocontexts[OCON_NODE];
1524 if (c->u.node.addr == (addr & c->u.node.mask))
1532 if (addrlen != sizeof(u64) * 2) {
1536 c = policydb.ocontexts[OCON_NODE6];
1538 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1546 *out_sid = SECINITSID_NODE;
1552 rc = sidtab_context_to_sid(&sidtab,
1558 *out_sid = c->sid[0];
1560 *out_sid = SECINITSID_NODE;
1571 * security_get_user_sids - Obtain reachable SIDs for a user.
1572 * @fromsid: starting SID
1573 * @username: username
1574 * @sids: array of reachable SIDs for user
1575 * @nel: number of elements in @sids
1577 * Generate the set of SIDs for legal security contexts
1578 * for a given user that can be reached by @fromsid.
1579 * Set *@sids to point to a dynamically allocated
1580 * array containing the set of SIDs. Set *@nel to the
1581 * number of elements in the array.
1584 int security_get_user_sids(u32 fromsid,
1589 struct context *fromcon, usercon;
1590 u32 *mysids = NULL, *mysids2, sid;
1591 u32 mynel = 0, maxnel = SIDS_NEL;
1592 struct user_datum *user;
1593 struct role_datum *role;
1594 struct ebitmap_node *rnode, *tnode;
1600 if (!ss_initialized)
1605 fromcon = sidtab_search(&sidtab, fromsid);
1611 user = hashtab_search(policydb.p_users.table, username);
1616 usercon.user = user->value;
1618 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1624 ebitmap_for_each_bit(&user->roles, rnode, i) {
1625 if (!ebitmap_node_get_bit(rnode, i))
1627 role = policydb.role_val_to_struct[i];
1629 ebitmap_for_each_bit(&role->types, tnode, j) {
1630 if (!ebitmap_node_get_bit(tnode, j))
1634 if (mls_setup_user_range(fromcon, user, &usercon))
1637 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1640 if (mynel < maxnel) {
1641 mysids[mynel++] = sid;
1644 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1649 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1652 mysids[mynel++] = sid;
1664 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1670 for (i = 0, j = 0; i < mynel; i++) {
1671 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1673 PROCESS__TRANSITION, AVC_STRICT,
1676 mysids2[j++] = mysids[i];
1688 * security_genfs_sid - Obtain a SID for a file in a filesystem
1689 * @fstype: filesystem type
1690 * @path: path from root of mount
1691 * @sclass: file security class
1692 * @sid: SID for path
1694 * Obtain a SID to use for a file in a filesystem that
1695 * cannot support xattr or use a fixed labeling behavior like
1696 * transition SIDs or task SIDs.
1698 int security_genfs_sid(const char *fstype,
1704 struct genfs *genfs;
1706 int rc = 0, cmp = 0;
1710 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1711 cmp = strcmp(fstype, genfs->fstype);
1716 if (!genfs || cmp) {
1717 *sid = SECINITSID_UNLABELED;
1722 for (c = genfs->head; c; c = c->next) {
1723 len = strlen(c->u.name);
1724 if ((!c->v.sclass || sclass == c->v.sclass) &&
1725 (strncmp(c->u.name, path, len) == 0))
1730 *sid = SECINITSID_UNLABELED;
1736 rc = sidtab_context_to_sid(&sidtab,
1750 * security_fs_use - Determine how to handle labeling for a filesystem.
1751 * @fstype: filesystem type
1752 * @behavior: labeling behavior
1753 * @sid: SID for filesystem (superblock)
1755 int security_fs_use(
1757 unsigned int *behavior,
1765 c = policydb.ocontexts[OCON_FSUSE];
1767 if (strcmp(fstype, c->u.name) == 0)
1773 *behavior = c->v.behavior;
1775 rc = sidtab_context_to_sid(&sidtab,
1783 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1785 *behavior = SECURITY_FS_USE_NONE;
1788 *behavior = SECURITY_FS_USE_GENFS;
1797 int security_get_bools(int *len, char ***names, int **values)
1799 int i, rc = -ENOMEM;
1805 *len = policydb.p_bools.nprim;
1811 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1815 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1819 for (i = 0; i < *len; i++) {
1821 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1822 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1823 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1826 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1827 (*names)[i][name_len - 1] = 0;
1835 for (i = 0; i < *len; i++)
1843 int security_set_bools(int len, int *values)
1846 int lenp, seqno = 0;
1847 struct cond_node *cur;
1851 lenp = policydb.p_bools.nprim;
1857 for (i = 0; i < len; i++) {
1858 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1859 audit_log(current->audit_context, GFP_ATOMIC,
1860 AUDIT_MAC_CONFIG_CHANGE,
1861 "bool=%s val=%d old_val=%d auid=%u",
1862 policydb.p_bool_val_to_name[i],
1864 policydb.bool_val_to_struct[i]->state,
1865 audit_get_loginuid(current->audit_context));
1868 policydb.bool_val_to_struct[i]->state = 1;
1870 policydb.bool_val_to_struct[i]->state = 0;
1874 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1875 rc = evaluate_cond_node(&policydb, cur);
1880 seqno = ++latest_granting;
1885 avc_ss_reset(seqno);
1886 selnl_notify_policyload(seqno);
1887 selinux_xfrm_notify_policyload();
1892 int security_get_bool_value(int bool)
1899 len = policydb.p_bools.nprim;
1905 rc = policydb.bool_val_to_struct[bool]->state;
1911 static int security_preserve_bools(struct policydb *p)
1913 int rc, nbools = 0, *bvalues = NULL, i;
1914 char **bnames = NULL;
1915 struct cond_bool_datum *booldatum;
1916 struct cond_node *cur;
1918 rc = security_get_bools(&nbools, &bnames, &bvalues);
1921 for (i = 0; i < nbools; i++) {
1922 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1924 booldatum->state = bvalues[i];
1926 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1927 rc = evaluate_cond_node(p, cur);
1934 for (i = 0; i < nbools; i++)
1943 * security_sid_mls_copy() - computes a new sid based on the given
1944 * sid and the mls portion of mls_sid.
1946 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1948 struct context *context1;
1949 struct context *context2;
1950 struct context newcon;
1955 if (!ss_initialized || !selinux_mls_enabled) {
1960 context_init(&newcon);
1963 context1 = sidtab_search(&sidtab, sid);
1965 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1971 context2 = sidtab_search(&sidtab, mls_sid);
1973 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1979 newcon.user = context1->user;
1980 newcon.role = context1->role;
1981 newcon.type = context1->type;
1982 rc = mls_context_cpy(&newcon, context2);
1986 /* Check the validity of the new context. */
1987 if (!policydb_context_isvalid(&policydb, &newcon)) {
1988 rc = convert_context_handle_invalid_context(&newcon);
1993 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1997 if (!context_struct_to_string(&newcon, &s, &len)) {
1998 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1999 "security_sid_mls_copy: invalid context %s", s);
2005 context_destroy(&newcon);
2010 static int get_classes_callback(void *k, void *d, void *args)
2012 struct class_datum *datum = d;
2013 char *name = k, **classes = args;
2014 int value = datum->value - 1;
2016 classes[value] = kstrdup(name, GFP_ATOMIC);
2017 if (!classes[value])
2023 int security_get_classes(char ***classes, int *nclasses)
2029 *nclasses = policydb.p_classes.nprim;
2030 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2034 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2038 for (i = 0; i < *nclasses; i++)
2039 kfree((*classes)[i]);
2048 static int get_permissions_callback(void *k, void *d, void *args)
2050 struct perm_datum *datum = d;
2051 char *name = k, **perms = args;
2052 int value = datum->value - 1;
2054 perms[value] = kstrdup(name, GFP_ATOMIC);
2061 int security_get_permissions(char *class, char ***perms, int *nperms)
2063 int rc = -ENOMEM, i;
2064 struct class_datum *match;
2068 match = hashtab_search(policydb.p_classes.table, class);
2070 printk(KERN_ERR "%s: unrecognized class %s\n",
2071 __FUNCTION__, class);
2076 *nperms = match->permissions.nprim;
2077 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2081 if (match->comdatum) {
2082 rc = hashtab_map(match->comdatum->permissions.table,
2083 get_permissions_callback, *perms);
2088 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2099 for (i = 0; i < *nperms; i++)
2105 struct selinux_audit_rule {
2107 struct context au_ctxt;
2110 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
2113 context_destroy(&rule->au_ctxt);
2118 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
2119 struct selinux_audit_rule **rule)
2121 struct selinux_audit_rule *tmprule;
2122 struct role_datum *roledatum;
2123 struct type_datum *typedatum;
2124 struct user_datum *userdatum;
2129 if (!ss_initialized)
2133 case AUDIT_SUBJ_USER:
2134 case AUDIT_SUBJ_ROLE:
2135 case AUDIT_SUBJ_TYPE:
2136 case AUDIT_OBJ_USER:
2137 case AUDIT_OBJ_ROLE:
2138 case AUDIT_OBJ_TYPE:
2139 /* only 'equals' and 'not equals' fit user, role, and type */
2140 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2143 case AUDIT_SUBJ_SEN:
2144 case AUDIT_SUBJ_CLR:
2145 case AUDIT_OBJ_LEV_LOW:
2146 case AUDIT_OBJ_LEV_HIGH:
2147 /* we do not allow a range, indicated by the presense of '-' */
2148 if (strchr(rulestr, '-'))
2152 /* only the above fields are valid */
2156 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2160 context_init(&tmprule->au_ctxt);
2164 tmprule->au_seqno = latest_granting;
2167 case AUDIT_SUBJ_USER:
2168 case AUDIT_OBJ_USER:
2169 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2173 tmprule->au_ctxt.user = userdatum->value;
2175 case AUDIT_SUBJ_ROLE:
2176 case AUDIT_OBJ_ROLE:
2177 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2181 tmprule->au_ctxt.role = roledatum->value;
2183 case AUDIT_SUBJ_TYPE:
2184 case AUDIT_OBJ_TYPE:
2185 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2189 tmprule->au_ctxt.type = typedatum->value;
2191 case AUDIT_SUBJ_SEN:
2192 case AUDIT_SUBJ_CLR:
2193 case AUDIT_OBJ_LEV_LOW:
2194 case AUDIT_OBJ_LEV_HIGH:
2195 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2202 selinux_audit_rule_free(tmprule);
2211 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2212 struct selinux_audit_rule *rule,
2213 struct audit_context *actx)
2215 struct context *ctxt;
2216 struct mls_level *level;
2220 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2221 "selinux_audit_rule_match: missing rule\n");
2227 if (rule->au_seqno < latest_granting) {
2228 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2229 "selinux_audit_rule_match: stale rule\n");
2234 ctxt = sidtab_search(&sidtab, sid);
2236 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2237 "selinux_audit_rule_match: unrecognized SID %d\n",
2243 /* a field/op pair that is not caught here will simply fall through
2246 case AUDIT_SUBJ_USER:
2247 case AUDIT_OBJ_USER:
2250 match = (ctxt->user == rule->au_ctxt.user);
2252 case AUDIT_NOT_EQUAL:
2253 match = (ctxt->user != rule->au_ctxt.user);
2257 case AUDIT_SUBJ_ROLE:
2258 case AUDIT_OBJ_ROLE:
2261 match = (ctxt->role == rule->au_ctxt.role);
2263 case AUDIT_NOT_EQUAL:
2264 match = (ctxt->role != rule->au_ctxt.role);
2268 case AUDIT_SUBJ_TYPE:
2269 case AUDIT_OBJ_TYPE:
2272 match = (ctxt->type == rule->au_ctxt.type);
2274 case AUDIT_NOT_EQUAL:
2275 match = (ctxt->type != rule->au_ctxt.type);
2279 case AUDIT_SUBJ_SEN:
2280 case AUDIT_SUBJ_CLR:
2281 case AUDIT_OBJ_LEV_LOW:
2282 case AUDIT_OBJ_LEV_HIGH:
2283 level = ((field == AUDIT_SUBJ_SEN ||
2284 field == AUDIT_OBJ_LEV_LOW) ?
2285 &ctxt->range.level[0] : &ctxt->range.level[1]);
2288 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2291 case AUDIT_NOT_EQUAL:
2292 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2295 case AUDIT_LESS_THAN:
2296 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2298 !mls_level_eq(&rule->au_ctxt.range.level[0],
2301 case AUDIT_LESS_THAN_OR_EQUAL:
2302 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2305 case AUDIT_GREATER_THAN:
2306 match = (mls_level_dom(level,
2307 &rule->au_ctxt.range.level[0]) &&
2308 !mls_level_eq(level,
2309 &rule->au_ctxt.range.level[0]));
2311 case AUDIT_GREATER_THAN_OR_EQUAL:
2312 match = mls_level_dom(level,
2313 &rule->au_ctxt.range.level[0]);
2323 static int (*aurule_callback)(void) = NULL;
2325 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2326 u16 class, u32 perms, u32 *retained)
2330 if (event == AVC_CALLBACK_RESET && aurule_callback)
2331 err = aurule_callback();
2335 static int __init aurule_init(void)
2339 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2340 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2342 panic("avc_add_callback() failed, error %d\n", err);
2346 __initcall(aurule_init);
2348 void selinux_audit_set_callback(int (*callback)(void))
2350 aurule_callback = callback;
2353 #ifdef CONFIG_NETLABEL
2355 * NetLabel cache structure
2357 #define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
2358 #define NETLBL_CACHE_T_NONE 0
2359 #define NETLBL_CACHE_T_SID 1
2360 #define NETLBL_CACHE_T_MLS 2
2361 struct selinux_netlbl_cache {
2365 struct mls_range mls_label;
2370 * security_netlbl_cache_free - Free the NetLabel cached data
2371 * @data: the data to free
2374 * This function is intended to be used as the free() callback inside the
2375 * netlbl_lsm_cache structure.
2378 static void security_netlbl_cache_free(const void *data)
2380 struct selinux_netlbl_cache *cache;
2385 cache = NETLBL_CACHE(data);
2386 switch (cache->type) {
2387 case NETLBL_CACHE_T_MLS:
2388 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2395 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2396 * @secattr: the NetLabel packet security attributes
2397 * @ctx: the SELinux context
2400 * Attempt to cache the context in @ctx, which was derived from the packet in
2401 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2402 * already been initialized.
2405 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2406 struct context *ctx)
2408 struct selinux_netlbl_cache *cache = NULL;
2410 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2411 if (secattr->cache == NULL)
2414 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2418 cache->type = NETLBL_CACHE_T_MLS;
2419 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2420 &ctx->range.level[0].cat) != 0) {
2424 cache->data.mls_label.level[1].cat.highbit =
2425 cache->data.mls_label.level[0].cat.highbit;
2426 cache->data.mls_label.level[1].cat.node =
2427 cache->data.mls_label.level[0].cat.node;
2428 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2429 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2431 secattr->cache->free = security_netlbl_cache_free;
2432 secattr->cache->data = (void *)cache;
2433 secattr->flags |= NETLBL_SECATTR_CACHE;
2437 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2438 * @secattr: the NetLabel packet security attributes
2439 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2440 * @sid: the SELinux SID
2443 * Convert the given NetLabel security attributes in @secattr into a
2444 * SELinux SID. If the @secattr field does not contain a full SELinux
2445 * SID/context then use the context in @base_sid as the foundation. If
2446 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2447 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2448 * SID conversion for future lookups. Returns zero on success, negative
2449 * values on failure.
2452 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2457 struct context *ctx;
2458 struct context ctx_new;
2459 struct selinux_netlbl_cache *cache;
2461 if (!ss_initialized) {
2468 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2469 cache = NETLBL_CACHE(secattr->cache->data);
2470 switch (cache->type) {
2471 case NETLBL_CACHE_T_SID:
2472 *sid = cache->data.sid;
2475 case NETLBL_CACHE_T_MLS:
2476 ctx = sidtab_search(&sidtab, base_sid);
2478 goto netlbl_secattr_to_sid_return;
2480 ctx_new.user = ctx->user;
2481 ctx_new.role = ctx->role;
2482 ctx_new.type = ctx->type;
2483 ctx_new.range.level[0].sens =
2484 cache->data.mls_label.level[0].sens;
2485 ctx_new.range.level[0].cat.highbit =
2486 cache->data.mls_label.level[0].cat.highbit;
2487 ctx_new.range.level[0].cat.node =
2488 cache->data.mls_label.level[0].cat.node;
2489 ctx_new.range.level[1].sens =
2490 cache->data.mls_label.level[1].sens;
2491 ctx_new.range.level[1].cat.highbit =
2492 cache->data.mls_label.level[1].cat.highbit;
2493 ctx_new.range.level[1].cat.node =
2494 cache->data.mls_label.level[1].cat.node;
2496 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2499 goto netlbl_secattr_to_sid_return;
2501 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2502 ctx = sidtab_search(&sidtab, base_sid);
2504 goto netlbl_secattr_to_sid_return;
2506 ctx_new.user = ctx->user;
2507 ctx_new.role = ctx->role;
2508 ctx_new.type = ctx->type;
2509 mls_import_netlbl_lvl(&ctx_new, secattr);
2510 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2511 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2512 secattr->mls_cat) != 0)
2513 goto netlbl_secattr_to_sid_return;
2514 ctx_new.range.level[1].cat.highbit =
2515 ctx_new.range.level[0].cat.highbit;
2516 ctx_new.range.level[1].cat.node =
2517 ctx_new.range.level[0].cat.node;
2519 ebitmap_init(&ctx_new.range.level[0].cat);
2520 ebitmap_init(&ctx_new.range.level[1].cat);
2522 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2523 goto netlbl_secattr_to_sid_return_cleanup;
2525 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2527 goto netlbl_secattr_to_sid_return_cleanup;
2529 security_netlbl_cache_add(secattr, &ctx_new);
2531 ebitmap_destroy(&ctx_new.range.level[0].cat);
2537 netlbl_secattr_to_sid_return:
2540 netlbl_secattr_to_sid_return_cleanup:
2541 ebitmap_destroy(&ctx_new.range.level[0].cat);
2542 goto netlbl_secattr_to_sid_return;
2546 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2547 * @sid: the SELinux SID
2548 * @secattr: the NetLabel packet security attributes
2551 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2552 * Returns zero on success, negative values on failure.
2555 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2558 struct context *ctx;
2560 netlbl_secattr_init(secattr);
2562 if (!ss_initialized)
2566 ctx = sidtab_search(&sidtab, sid);
2568 goto netlbl_sid_to_secattr_failure;
2569 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2571 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2572 mls_export_netlbl_lvl(ctx, secattr);
2573 rc = mls_export_netlbl_cat(ctx, secattr);
2575 goto netlbl_sid_to_secattr_failure;
2580 netlbl_sid_to_secattr_failure:
2582 netlbl_secattr_destroy(secattr);
2585 #endif /* CONFIG_NETLABEL */