2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
21 * Updated: Chad Sellers <csellers@tresys.com>
23 * Added validation of kernel classes and permissions
25 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
28 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/spinlock.h>
37 #include <linux/rcupdate.h>
38 #include <linux/errno.h>
40 #include <linux/sched.h>
41 #include <linux/audit.h>
42 #include <linux/mutex.h>
43 #include <net/netlabel.h>
53 #include "conditional.h"
60 extern void selnl_notify_policyload(u32 seqno);
61 unsigned int policydb_loaded_version;
63 int selinux_policycap_netpeer;
66 * This is declared in avc.c
68 extern const struct selinux_class_perm selinux_class_perm;
70 static DEFINE_RWLOCK(policy_rwlock);
71 #define POLICY_RDLOCK read_lock(&policy_rwlock)
72 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
73 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
74 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
76 static DEFINE_MUTEX(load_mutex);
77 #define LOAD_LOCK mutex_lock(&load_mutex)
78 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
80 static struct sidtab sidtab;
81 struct policydb policydb;
82 int ss_initialized = 0;
85 * The largest sequence number that has been used when
86 * providing an access decision to the access vector cache.
87 * The sequence number only changes when a policy change
90 static u32 latest_granting = 0;
92 /* Forward declaration. */
93 static int context_struct_to_string(struct context *context, char **scontext,
97 * Return the boolean value of a constraint expression
98 * when it is applied to the specified source and target
101 * xcontext is a special beast... It is used by the validatetrans rules
102 * only. For these rules, scontext is the context before the transition,
103 * tcontext is the context after the transition, and xcontext is the context
104 * of the process performing the transition. All other callers of
105 * constraint_expr_eval should pass in NULL for xcontext.
107 static int constraint_expr_eval(struct context *scontext,
108 struct context *tcontext,
109 struct context *xcontext,
110 struct constraint_expr *cexpr)
114 struct role_datum *r1, *r2;
115 struct mls_level *l1, *l2;
116 struct constraint_expr *e;
117 int s[CEXPR_MAXDEPTH];
120 for (e = cexpr; e; e = e->next) {
121 switch (e->expr_type) {
137 if (sp == (CEXPR_MAXDEPTH-1))
141 val1 = scontext->user;
142 val2 = tcontext->user;
145 val1 = scontext->type;
146 val2 = tcontext->type;
149 val1 = scontext->role;
150 val2 = tcontext->role;
151 r1 = policydb.role_val_to_struct[val1 - 1];
152 r2 = policydb.role_val_to_struct[val2 - 1];
155 s[++sp] = ebitmap_get_bit(&r1->dominates,
159 s[++sp] = ebitmap_get_bit(&r2->dominates,
163 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
165 !ebitmap_get_bit(&r2->dominates,
173 l1 = &(scontext->range.level[0]);
174 l2 = &(tcontext->range.level[0]);
177 l1 = &(scontext->range.level[0]);
178 l2 = &(tcontext->range.level[1]);
181 l1 = &(scontext->range.level[1]);
182 l2 = &(tcontext->range.level[0]);
185 l1 = &(scontext->range.level[1]);
186 l2 = &(tcontext->range.level[1]);
189 l1 = &(scontext->range.level[0]);
190 l2 = &(scontext->range.level[1]);
193 l1 = &(tcontext->range.level[0]);
194 l2 = &(tcontext->range.level[1]);
199 s[++sp] = mls_level_eq(l1, l2);
202 s[++sp] = !mls_level_eq(l1, l2);
205 s[++sp] = mls_level_dom(l1, l2);
208 s[++sp] = mls_level_dom(l2, l1);
211 s[++sp] = mls_level_incomp(l2, l1);
225 s[++sp] = (val1 == val2);
228 s[++sp] = (val1 != val2);
236 if (sp == (CEXPR_MAXDEPTH-1))
239 if (e->attr & CEXPR_TARGET)
241 else if (e->attr & CEXPR_XTARGET) {
248 if (e->attr & CEXPR_USER)
250 else if (e->attr & CEXPR_ROLE)
252 else if (e->attr & CEXPR_TYPE)
261 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
264 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
282 * Compute access vectors based on a context structure pair for
283 * the permissions in a particular class.
285 static int context_struct_compute_av(struct context *scontext,
286 struct context *tcontext,
289 struct av_decision *avd)
291 struct constraint_node *constraint;
292 struct role_allow *ra;
293 struct avtab_key avkey;
294 struct avtab_node *node;
295 struct class_datum *tclass_datum;
296 struct ebitmap *sattr, *tattr;
297 struct ebitmap_node *snode, *tnode;
298 const struct selinux_class_perm *kdefs = &selinux_class_perm;
302 * Remap extended Netlink classes for old policy versions.
303 * Do this here rather than socket_type_to_security_class()
304 * in case a newer policy version is loaded, allowing sockets
305 * to remain in the correct class.
307 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
308 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
309 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
310 tclass = SECCLASS_NETLINK_SOCKET;
313 * Initialize the access vectors to the default values.
316 avd->decided = 0xffffffff;
318 avd->auditdeny = 0xffffffff;
319 avd->seqno = latest_granting;
322 * Check for all the invalid cases.
324 * - tclass > policy and > kernel
325 * - tclass > policy but is a userspace class
326 * - tclass > policy but we do not allow unknowns
328 if (unlikely(!tclass))
330 if (unlikely(tclass > policydb.p_classes.nprim))
331 if (tclass > kdefs->cts_len ||
332 !kdefs->class_to_string[tclass - 1] ||
333 !policydb.allow_unknown)
337 * Kernel class and we allow unknown so pad the allow decision
338 * the pad will be all 1 for unknown classes.
340 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
341 avd->allowed = policydb.undefined_perms[tclass - 1];
344 * Not in policy. Since decision is completed (all 1 or all 0) return.
346 if (unlikely(tclass > policydb.p_classes.nprim))
349 tclass_datum = policydb.class_val_to_struct[tclass - 1];
352 * If a specific type enforcement rule was defined for
353 * this permission check, then use it.
355 avkey.target_class = tclass;
356 avkey.specified = AVTAB_AV;
357 sattr = &policydb.type_attr_map[scontext->type - 1];
358 tattr = &policydb.type_attr_map[tcontext->type - 1];
359 ebitmap_for_each_positive_bit(sattr, snode, i) {
360 ebitmap_for_each_positive_bit(tattr, tnode, j) {
361 avkey.source_type = i + 1;
362 avkey.target_type = j + 1;
363 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
365 node = avtab_search_node_next(node, avkey.specified)) {
366 if (node->key.specified == AVTAB_ALLOWED)
367 avd->allowed |= node->datum.data;
368 else if (node->key.specified == AVTAB_AUDITALLOW)
369 avd->auditallow |= node->datum.data;
370 else if (node->key.specified == AVTAB_AUDITDENY)
371 avd->auditdeny &= node->datum.data;
374 /* Check conditional av table for additional permissions */
375 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
381 * Remove any permissions prohibited by a constraint (this includes
384 constraint = tclass_datum->constraints;
386 if ((constraint->permissions & (avd->allowed)) &&
387 !constraint_expr_eval(scontext, tcontext, NULL,
389 avd->allowed = (avd->allowed) & ~(constraint->permissions);
391 constraint = constraint->next;
395 * If checking process transition permission and the
396 * role is changing, then check the (current_role, new_role)
399 if (tclass == SECCLASS_PROCESS &&
400 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
401 scontext->role != tcontext->role) {
402 for (ra = policydb.role_allow; ra; ra = ra->next) {
403 if (scontext->role == ra->role &&
404 tcontext->role == ra->new_role)
408 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
409 PROCESS__DYNTRANSITION);
415 printk(KERN_ERR "%s: unrecognized class %d\n", __FUNCTION__, tclass);
419 static int security_validtrans_handle_fail(struct context *ocontext,
420 struct context *ncontext,
421 struct context *tcontext,
424 char *o = NULL, *n = NULL, *t = NULL;
425 u32 olen, nlen, tlen;
427 if (context_struct_to_string(ocontext, &o, &olen) < 0)
429 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
431 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
433 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
434 "security_validate_transition: denied for"
435 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
436 o, n, t, policydb.p_class_val_to_name[tclass-1]);
442 if (!selinux_enforcing)
447 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
450 struct context *ocontext;
451 struct context *ncontext;
452 struct context *tcontext;
453 struct class_datum *tclass_datum;
454 struct constraint_node *constraint;
463 * Remap extended Netlink classes for old policy versions.
464 * Do this here rather than socket_type_to_security_class()
465 * in case a newer policy version is loaded, allowing sockets
466 * to remain in the correct class.
468 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
469 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
470 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
471 tclass = SECCLASS_NETLINK_SOCKET;
473 if (!tclass || tclass > policydb.p_classes.nprim) {
474 printk(KERN_ERR "security_validate_transition: "
475 "unrecognized class %d\n", tclass);
479 tclass_datum = policydb.class_val_to_struct[tclass - 1];
481 ocontext = sidtab_search(&sidtab, oldsid);
483 printk(KERN_ERR "security_validate_transition: "
484 " unrecognized SID %d\n", oldsid);
489 ncontext = sidtab_search(&sidtab, newsid);
491 printk(KERN_ERR "security_validate_transition: "
492 " unrecognized SID %d\n", newsid);
497 tcontext = sidtab_search(&sidtab, tasksid);
499 printk(KERN_ERR "security_validate_transition: "
500 " unrecognized SID %d\n", tasksid);
505 constraint = tclass_datum->validatetrans;
507 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
509 rc = security_validtrans_handle_fail(ocontext, ncontext,
513 constraint = constraint->next;
522 * security_compute_av - Compute access vector decisions.
523 * @ssid: source security identifier
524 * @tsid: target security identifier
525 * @tclass: target security class
526 * @requested: requested permissions
527 * @avd: access vector decisions
529 * Compute a set of access vector decisions based on the
530 * SID pair (@ssid, @tsid) for the permissions in @tclass.
531 * Return -%EINVAL if any of the parameters are invalid or %0
532 * if the access vector decisions were computed successfully.
534 int security_compute_av(u32 ssid,
538 struct av_decision *avd)
540 struct context *scontext = NULL, *tcontext = NULL;
543 if (!ss_initialized) {
544 avd->allowed = 0xffffffff;
545 avd->decided = 0xffffffff;
547 avd->auditdeny = 0xffffffff;
548 avd->seqno = latest_granting;
554 scontext = sidtab_search(&sidtab, ssid);
556 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
561 tcontext = sidtab_search(&sidtab, tsid);
563 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
569 rc = context_struct_compute_av(scontext, tcontext, tclass,
577 * Write the security context string representation of
578 * the context structure `context' into a dynamically
579 * allocated string of the correct size. Set `*scontext'
580 * to point to this string and set `*scontext_len' to
581 * the length of the string.
583 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
590 /* Compute the size of the context. */
591 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
592 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
593 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
594 *scontext_len += mls_compute_context_len(context);
596 /* Allocate space for the context; caller must free this space. */
597 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
601 *scontext = scontextp;
604 * Copy the user name, role name and type name into the context.
606 sprintf(scontextp, "%s:%s:%s",
607 policydb.p_user_val_to_name[context->user - 1],
608 policydb.p_role_val_to_name[context->role - 1],
609 policydb.p_type_val_to_name[context->type - 1]);
610 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
611 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
612 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
614 mls_sid_to_context(context, &scontextp);
621 #include "initial_sid_to_string.h"
623 const char *security_get_initial_sid_context(u32 sid)
625 if (unlikely(sid > SECINITSID_NUM))
627 return initial_sid_to_string[sid];
631 * security_sid_to_context - Obtain a context for a given SID.
632 * @sid: security identifier, SID
633 * @scontext: security context
634 * @scontext_len: length in bytes
636 * Write the string representation of the context associated with @sid
637 * into a dynamically allocated string of the correct size. Set @scontext
638 * to point to this string and set @scontext_len to the length of the string.
640 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
642 struct context *context;
648 if (!ss_initialized) {
649 if (sid <= SECINITSID_NUM) {
652 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
653 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
658 strcpy(scontextp, initial_sid_to_string[sid]);
659 *scontext = scontextp;
662 printk(KERN_ERR "security_sid_to_context: called before initial "
663 "load_policy on unknown SID %d\n", sid);
668 context = sidtab_search(&sidtab, sid);
670 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
675 rc = context_struct_to_string(context, scontext, scontext_len);
683 static int security_context_to_sid_core(char *scontext, u32 scontext_len,
684 u32 *sid, u32 def_sid, gfp_t gfp_flags)
687 struct context context;
688 struct role_datum *role;
689 struct type_datum *typdatum;
690 struct user_datum *usrdatum;
691 char *scontextp, *p, oldc;
694 if (!ss_initialized) {
697 for (i = 1; i < SECINITSID_NUM; i++) {
698 if (!strcmp(initial_sid_to_string[i], scontext)) {
703 *sid = SECINITSID_KERNEL;
708 /* Copy the string so that we can modify the copy as we parse it.
709 The string should already by null terminated, but we append a
710 null suffix to the copy to avoid problems with the existing
711 attr package, which doesn't view the null terminator as part
712 of the attribute value. */
713 scontext2 = kmalloc(scontext_len+1, gfp_flags);
718 memcpy(scontext2, scontext, scontext_len);
719 scontext2[scontext_len] = 0;
721 context_init(&context);
726 /* Parse the security context. */
729 scontextp = (char *) scontext2;
731 /* Extract the user. */
733 while (*p && *p != ':')
741 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
745 context.user = usrdatum->value;
749 while (*p && *p != ':')
757 role = hashtab_search(policydb.p_roles.table, scontextp);
760 context.role = role->value;
764 while (*p && *p != ':')
769 typdatum = hashtab_search(policydb.p_types.table, scontextp);
773 context.type = typdatum->value;
775 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
779 if ((p - scontext2) < scontext_len) {
784 /* Check the validity of the new context. */
785 if (!policydb_context_isvalid(&policydb, &context)) {
789 /* Obtain the new sid. */
790 rc = sidtab_context_to_sid(&sidtab, &context, sid);
793 context_destroy(&context);
800 * security_context_to_sid - Obtain a SID for a given security context.
801 * @scontext: security context
802 * @scontext_len: length in bytes
803 * @sid: security identifier, SID
805 * Obtains a SID associated with the security context that
806 * has the string representation specified by @scontext.
807 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
808 * memory is available, or 0 on success.
810 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
812 return security_context_to_sid_core(scontext, scontext_len,
813 sid, SECSID_NULL, GFP_KERNEL);
817 * security_context_to_sid_default - Obtain a SID for a given security context,
818 * falling back to specified default if needed.
820 * @scontext: security context
821 * @scontext_len: length in bytes
822 * @sid: security identifier, SID
823 * @def_sid: default SID to assign on error
825 * Obtains a SID associated with the security context that
826 * has the string representation specified by @scontext.
827 * The default SID is passed to the MLS layer to be used to allow
828 * kernel labeling of the MLS field if the MLS field is not present
829 * (for upgrading to MLS without full relabel).
830 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
831 * memory is available, or 0 on success.
833 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid,
834 u32 def_sid, gfp_t gfp_flags)
836 return security_context_to_sid_core(scontext, scontext_len,
837 sid, def_sid, gfp_flags);
840 static int compute_sid_handle_invalid_context(
841 struct context *scontext,
842 struct context *tcontext,
844 struct context *newcontext)
846 char *s = NULL, *t = NULL, *n = NULL;
847 u32 slen, tlen, nlen;
849 if (context_struct_to_string(scontext, &s, &slen) < 0)
851 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
853 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
855 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
856 "security_compute_sid: invalid context %s"
860 n, s, t, policydb.p_class_val_to_name[tclass-1]);
865 if (!selinux_enforcing)
870 static int security_compute_sid(u32 ssid,
876 struct context *scontext = NULL, *tcontext = NULL, newcontext;
877 struct role_trans *roletr = NULL;
878 struct avtab_key avkey;
879 struct avtab_datum *avdatum;
880 struct avtab_node *node;
883 if (!ss_initialized) {
885 case SECCLASS_PROCESS:
895 context_init(&newcontext);
899 scontext = sidtab_search(&sidtab, ssid);
901 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
906 tcontext = sidtab_search(&sidtab, tsid);
908 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
914 /* Set the user identity. */
916 case AVTAB_TRANSITION:
918 /* Use the process user identity. */
919 newcontext.user = scontext->user;
922 /* Use the related object owner. */
923 newcontext.user = tcontext->user;
927 /* Set the role and type to default values. */
929 case SECCLASS_PROCESS:
930 /* Use the current role and type of process. */
931 newcontext.role = scontext->role;
932 newcontext.type = scontext->type;
935 /* Use the well-defined object role. */
936 newcontext.role = OBJECT_R_VAL;
937 /* Use the type of the related object. */
938 newcontext.type = tcontext->type;
941 /* Look for a type transition/member/change rule. */
942 avkey.source_type = scontext->type;
943 avkey.target_type = tcontext->type;
944 avkey.target_class = tclass;
945 avkey.specified = specified;
946 avdatum = avtab_search(&policydb.te_avtab, &avkey);
948 /* If no permanent rule, also check for enabled conditional rules */
950 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
951 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
952 if (node->key.specified & AVTAB_ENABLED) {
953 avdatum = &node->datum;
960 /* Use the type from the type transition/member/change rule. */
961 newcontext.type = avdatum->data;
964 /* Check for class-specific changes. */
966 case SECCLASS_PROCESS:
967 if (specified & AVTAB_TRANSITION) {
968 /* Look for a role transition rule. */
969 for (roletr = policydb.role_tr; roletr;
970 roletr = roletr->next) {
971 if (roletr->role == scontext->role &&
972 roletr->type == tcontext->type) {
973 /* Use the role transition rule. */
974 newcontext.role = roletr->new_role;
984 /* Set the MLS attributes.
985 This is done last because it may allocate memory. */
986 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
990 /* Check the validity of the context. */
991 if (!policydb_context_isvalid(&policydb, &newcontext)) {
992 rc = compute_sid_handle_invalid_context(scontext,
999 /* Obtain the sid for the context. */
1000 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1003 context_destroy(&newcontext);
1009 * security_transition_sid - Compute the SID for a new subject/object.
1010 * @ssid: source security identifier
1011 * @tsid: target security identifier
1012 * @tclass: target security class
1013 * @out_sid: security identifier for new subject/object
1015 * Compute a SID to use for labeling a new subject or object in the
1016 * class @tclass based on a SID pair (@ssid, @tsid).
1017 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1018 * if insufficient memory is available, or %0 if the new SID was
1019 * computed successfully.
1021 int security_transition_sid(u32 ssid,
1026 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1030 * security_member_sid - Compute the SID for member selection.
1031 * @ssid: source security identifier
1032 * @tsid: target security identifier
1033 * @tclass: target security class
1034 * @out_sid: security identifier for selected member
1036 * Compute a SID to use when selecting a member of a polyinstantiated
1037 * object of class @tclass based on a SID pair (@ssid, @tsid).
1038 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1039 * if insufficient memory is available, or %0 if the SID was
1040 * computed successfully.
1042 int security_member_sid(u32 ssid,
1047 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1051 * security_change_sid - Compute the SID for object relabeling.
1052 * @ssid: source security identifier
1053 * @tsid: target security identifier
1054 * @tclass: target security class
1055 * @out_sid: security identifier for selected member
1057 * Compute a SID to use for relabeling an object of class @tclass
1058 * based on a SID pair (@ssid, @tsid).
1059 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1060 * if insufficient memory is available, or %0 if the SID was
1061 * computed successfully.
1063 int security_change_sid(u32 ssid,
1068 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1072 * Verify that each kernel class that is defined in the
1075 static int validate_classes(struct policydb *p)
1078 struct class_datum *cladatum;
1079 struct perm_datum *perdatum;
1080 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1082 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1083 const char *def_class, *def_perm, *pol_class;
1084 struct symtab *perms;
1086 if (p->allow_unknown) {
1087 u32 num_classes = kdefs->cts_len;
1088 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1089 if (!p->undefined_perms)
1093 for (i = 1; i < kdefs->cts_len; i++) {
1094 def_class = kdefs->class_to_string[i];
1097 if (i > p->p_classes.nprim) {
1099 "security: class %s not defined in policy\n",
1101 if (p->reject_unknown)
1103 if (p->allow_unknown)
1104 p->undefined_perms[i-1] = ~0U;
1107 pol_class = p->p_class_val_to_name[i-1];
1108 if (strcmp(pol_class, def_class)) {
1110 "security: class %d is incorrect, found %s but should be %s\n",
1111 i, pol_class, def_class);
1115 for (i = 0; i < kdefs->av_pts_len; i++) {
1116 class_val = kdefs->av_perm_to_string[i].tclass;
1117 perm_val = kdefs->av_perm_to_string[i].value;
1118 def_perm = kdefs->av_perm_to_string[i].name;
1119 if (class_val > p->p_classes.nprim)
1121 pol_class = p->p_class_val_to_name[class_val-1];
1122 cladatum = hashtab_search(p->p_classes.table, pol_class);
1124 perms = &cladatum->permissions;
1125 nprim = 1 << (perms->nprim - 1);
1126 if (perm_val > nprim) {
1128 "security: permission %s in class %s not defined in policy\n",
1129 def_perm, pol_class);
1130 if (p->reject_unknown)
1132 if (p->allow_unknown)
1133 p->undefined_perms[class_val-1] |= perm_val;
1136 perdatum = hashtab_search(perms->table, def_perm);
1137 if (perdatum == NULL) {
1139 "security: permission %s in class %s not found in policy, bad policy\n",
1140 def_perm, pol_class);
1143 pol_val = 1 << (perdatum->value - 1);
1144 if (pol_val != perm_val) {
1146 "security: permission %s in class %s has incorrect value\n",
1147 def_perm, pol_class);
1151 for (i = 0; i < kdefs->av_inherit_len; i++) {
1152 class_val = kdefs->av_inherit[i].tclass;
1153 if (class_val > p->p_classes.nprim)
1155 pol_class = p->p_class_val_to_name[class_val-1];
1156 cladatum = hashtab_search(p->p_classes.table, pol_class);
1158 if (!cladatum->comdatum) {
1160 "security: class %s should have an inherits clause but does not\n",
1164 tmp = kdefs->av_inherit[i].common_base;
1166 while (!(tmp & 0x01)) {
1170 perms = &cladatum->comdatum->permissions;
1171 for (j = 0; j < common_pts_len; j++) {
1172 def_perm = kdefs->av_inherit[i].common_pts[j];
1173 if (j >= perms->nprim) {
1175 "security: permission %s in class %s not defined in policy\n",
1176 def_perm, pol_class);
1177 if (p->reject_unknown)
1179 if (p->allow_unknown)
1180 p->undefined_perms[class_val-1] |= (1 << j);
1183 perdatum = hashtab_search(perms->table, def_perm);
1184 if (perdatum == NULL) {
1186 "security: permission %s in class %s not found in policy, bad policy\n",
1187 def_perm, pol_class);
1190 if (perdatum->value != j + 1) {
1192 "security: permission %s in class %s has incorrect value\n",
1193 def_perm, pol_class);
1201 /* Clone the SID into the new SID table. */
1202 static int clone_sid(u32 sid,
1203 struct context *context,
1206 struct sidtab *s = arg;
1208 return sidtab_insert(s, sid, context);
1211 static inline int convert_context_handle_invalid_context(struct context *context)
1215 if (selinux_enforcing) {
1221 context_struct_to_string(context, &s, &len);
1222 printk(KERN_ERR "security: context %s is invalid\n", s);
1228 struct convert_context_args {
1229 struct policydb *oldp;
1230 struct policydb *newp;
1234 * Convert the values in the security context
1235 * structure `c' from the values specified
1236 * in the policy `p->oldp' to the values specified
1237 * in the policy `p->newp'. Verify that the
1238 * context is valid under the new policy.
1240 static int convert_context(u32 key,
1244 struct convert_context_args *args;
1245 struct context oldc;
1246 struct role_datum *role;
1247 struct type_datum *typdatum;
1248 struct user_datum *usrdatum;
1255 rc = context_cpy(&oldc, c);
1261 /* Convert the user. */
1262 usrdatum = hashtab_search(args->newp->p_users.table,
1263 args->oldp->p_user_val_to_name[c->user - 1]);
1267 c->user = usrdatum->value;
1269 /* Convert the role. */
1270 role = hashtab_search(args->newp->p_roles.table,
1271 args->oldp->p_role_val_to_name[c->role - 1]);
1275 c->role = role->value;
1277 /* Convert the type. */
1278 typdatum = hashtab_search(args->newp->p_types.table,
1279 args->oldp->p_type_val_to_name[c->type - 1]);
1283 c->type = typdatum->value;
1285 rc = mls_convert_context(args->oldp, args->newp, c);
1289 /* Check the validity of the new context. */
1290 if (!policydb_context_isvalid(args->newp, c)) {
1291 rc = convert_context_handle_invalid_context(&oldc);
1296 context_destroy(&oldc);
1300 context_struct_to_string(&oldc, &s, &len);
1301 context_destroy(&oldc);
1302 printk(KERN_ERR "security: invalidating context %s\n", s);
1307 static void security_load_policycaps(void)
1309 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1310 POLICYDB_CAPABILITY_NETPEER);
1313 extern void selinux_complete_init(void);
1314 static int security_preserve_bools(struct policydb *p);
1317 * security_load_policy - Load a security policy configuration.
1318 * @data: binary policy data
1319 * @len: length of data in bytes
1321 * Load a new set of security policy configuration data,
1322 * validate it and convert the SID table as necessary.
1323 * This function will flush the access vector cache after
1324 * loading the new policy.
1326 int security_load_policy(void *data, size_t len)
1328 struct policydb oldpolicydb, newpolicydb;
1329 struct sidtab oldsidtab, newsidtab;
1330 struct convert_context_args args;
1333 struct policy_file file = { data, len }, *fp = &file;
1337 if (!ss_initialized) {
1339 if (policydb_read(&policydb, fp)) {
1341 avtab_cache_destroy();
1344 if (policydb_load_isids(&policydb, &sidtab)) {
1346 policydb_destroy(&policydb);
1347 avtab_cache_destroy();
1350 /* Verify that the kernel defined classes are correct. */
1351 if (validate_classes(&policydb)) {
1353 "security: the definition of a class is incorrect\n");
1355 sidtab_destroy(&sidtab);
1356 policydb_destroy(&policydb);
1357 avtab_cache_destroy();
1360 security_load_policycaps();
1361 policydb_loaded_version = policydb.policyvers;
1363 seqno = ++latest_granting;
1365 selinux_complete_init();
1366 avc_ss_reset(seqno);
1367 selnl_notify_policyload(seqno);
1368 selinux_netlbl_cache_invalidate();
1369 selinux_xfrm_notify_policyload();
1374 sidtab_hash_eval(&sidtab, "sids");
1377 if (policydb_read(&newpolicydb, fp)) {
1382 sidtab_init(&newsidtab);
1384 /* Verify that the kernel defined classes are correct. */
1385 if (validate_classes(&newpolicydb)) {
1387 "security: the definition of a class is incorrect\n");
1392 rc = security_preserve_bools(&newpolicydb);
1394 printk(KERN_ERR "security: unable to preserve booleans\n");
1398 /* Clone the SID table. */
1399 sidtab_shutdown(&sidtab);
1400 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1405 /* Convert the internal representations of contexts
1406 in the new SID table and remove invalid SIDs. */
1407 args.oldp = &policydb;
1408 args.newp = &newpolicydb;
1409 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1411 /* Save the old policydb and SID table to free later. */
1412 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1413 sidtab_set(&oldsidtab, &sidtab);
1415 /* Install the new policydb and SID table. */
1417 memcpy(&policydb, &newpolicydb, sizeof policydb);
1418 sidtab_set(&sidtab, &newsidtab);
1419 security_load_policycaps();
1420 seqno = ++latest_granting;
1421 policydb_loaded_version = policydb.policyvers;
1425 /* Free the old policydb and SID table. */
1426 policydb_destroy(&oldpolicydb);
1427 sidtab_destroy(&oldsidtab);
1429 avc_ss_reset(seqno);
1430 selnl_notify_policyload(seqno);
1431 selinux_netlbl_cache_invalidate();
1432 selinux_xfrm_notify_policyload();
1438 sidtab_destroy(&newsidtab);
1439 policydb_destroy(&newpolicydb);
1445 * security_port_sid - Obtain the SID for a port.
1446 * @domain: communication domain aka address family
1447 * @type: socket type
1448 * @protocol: protocol number
1449 * @port: port number
1450 * @out_sid: security identifier
1452 int security_port_sid(u16 domain,
1463 c = policydb.ocontexts[OCON_PORT];
1465 if (c->u.port.protocol == protocol &&
1466 c->u.port.low_port <= port &&
1467 c->u.port.high_port >= port)
1474 rc = sidtab_context_to_sid(&sidtab,
1480 *out_sid = c->sid[0];
1482 *out_sid = SECINITSID_PORT;
1491 * security_netif_sid - Obtain the SID for a network interface.
1492 * @name: interface name
1493 * @if_sid: interface SID
1495 int security_netif_sid(char *name, u32 *if_sid)
1502 c = policydb.ocontexts[OCON_NETIF];
1504 if (strcmp(name, c->u.name) == 0)
1510 if (!c->sid[0] || !c->sid[1]) {
1511 rc = sidtab_context_to_sid(&sidtab,
1516 rc = sidtab_context_to_sid(&sidtab,
1522 *if_sid = c->sid[0];
1524 *if_sid = SECINITSID_NETIF;
1531 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1535 for(i = 0; i < 4; i++)
1536 if(addr[i] != (input[i] & mask[i])) {
1545 * security_node_sid - Obtain the SID for a node (host).
1546 * @domain: communication domain aka address family
1548 * @addrlen: address length in bytes
1549 * @out_sid: security identifier
1551 int security_node_sid(u16 domain,
1565 if (addrlen != sizeof(u32)) {
1570 addr = *((u32 *)addrp);
1572 c = policydb.ocontexts[OCON_NODE];
1574 if (c->u.node.addr == (addr & c->u.node.mask))
1582 if (addrlen != sizeof(u64) * 2) {
1586 c = policydb.ocontexts[OCON_NODE6];
1588 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1596 *out_sid = SECINITSID_NODE;
1602 rc = sidtab_context_to_sid(&sidtab,
1608 *out_sid = c->sid[0];
1610 *out_sid = SECINITSID_NODE;
1621 * security_get_user_sids - Obtain reachable SIDs for a user.
1622 * @fromsid: starting SID
1623 * @username: username
1624 * @sids: array of reachable SIDs for user
1625 * @nel: number of elements in @sids
1627 * Generate the set of SIDs for legal security contexts
1628 * for a given user that can be reached by @fromsid.
1629 * Set *@sids to point to a dynamically allocated
1630 * array containing the set of SIDs. Set *@nel to the
1631 * number of elements in the array.
1634 int security_get_user_sids(u32 fromsid,
1639 struct context *fromcon, usercon;
1640 u32 *mysids = NULL, *mysids2, sid;
1641 u32 mynel = 0, maxnel = SIDS_NEL;
1642 struct user_datum *user;
1643 struct role_datum *role;
1644 struct ebitmap_node *rnode, *tnode;
1650 if (!ss_initialized)
1655 fromcon = sidtab_search(&sidtab, fromsid);
1661 user = hashtab_search(policydb.p_users.table, username);
1666 usercon.user = user->value;
1668 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1674 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
1675 role = policydb.role_val_to_struct[i];
1677 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
1680 if (mls_setup_user_range(fromcon, user, &usercon))
1683 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1686 if (mynel < maxnel) {
1687 mysids[mynel++] = sid;
1690 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1695 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1698 mysids[mynel++] = sid;
1710 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1716 for (i = 0, j = 0; i < mynel; i++) {
1717 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1719 PROCESS__TRANSITION, AVC_STRICT,
1722 mysids2[j++] = mysids[i];
1734 * security_genfs_sid - Obtain a SID for a file in a filesystem
1735 * @fstype: filesystem type
1736 * @path: path from root of mount
1737 * @sclass: file security class
1738 * @sid: SID for path
1740 * Obtain a SID to use for a file in a filesystem that
1741 * cannot support xattr or use a fixed labeling behavior like
1742 * transition SIDs or task SIDs.
1744 int security_genfs_sid(const char *fstype,
1750 struct genfs *genfs;
1752 int rc = 0, cmp = 0;
1754 while (path[0] == '/' && path[1] == '/')
1759 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1760 cmp = strcmp(fstype, genfs->fstype);
1765 if (!genfs || cmp) {
1766 *sid = SECINITSID_UNLABELED;
1771 for (c = genfs->head; c; c = c->next) {
1772 len = strlen(c->u.name);
1773 if ((!c->v.sclass || sclass == c->v.sclass) &&
1774 (strncmp(c->u.name, path, len) == 0))
1779 *sid = SECINITSID_UNLABELED;
1785 rc = sidtab_context_to_sid(&sidtab,
1799 * security_fs_use - Determine how to handle labeling for a filesystem.
1800 * @fstype: filesystem type
1801 * @behavior: labeling behavior
1802 * @sid: SID for filesystem (superblock)
1804 int security_fs_use(
1806 unsigned int *behavior,
1814 c = policydb.ocontexts[OCON_FSUSE];
1816 if (strcmp(fstype, c->u.name) == 0)
1822 *behavior = c->v.behavior;
1824 rc = sidtab_context_to_sid(&sidtab,
1832 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1834 *behavior = SECURITY_FS_USE_NONE;
1837 *behavior = SECURITY_FS_USE_GENFS;
1846 int security_get_bools(int *len, char ***names, int **values)
1848 int i, rc = -ENOMEM;
1854 *len = policydb.p_bools.nprim;
1860 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1864 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1868 for (i = 0; i < *len; i++) {
1870 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1871 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1872 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1875 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1876 (*names)[i][name_len - 1] = 0;
1884 for (i = 0; i < *len; i++)
1892 int security_set_bools(int len, int *values)
1895 int lenp, seqno = 0;
1896 struct cond_node *cur;
1900 lenp = policydb.p_bools.nprim;
1906 for (i = 0; i < len; i++) {
1907 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1908 audit_log(current->audit_context, GFP_ATOMIC,
1909 AUDIT_MAC_CONFIG_CHANGE,
1910 "bool=%s val=%d old_val=%d auid=%u ses=%u",
1911 policydb.p_bool_val_to_name[i],
1913 policydb.bool_val_to_struct[i]->state,
1914 audit_get_loginuid(current),
1915 audit_get_sessionid(current));
1918 policydb.bool_val_to_struct[i]->state = 1;
1920 policydb.bool_val_to_struct[i]->state = 0;
1924 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1925 rc = evaluate_cond_node(&policydb, cur);
1930 seqno = ++latest_granting;
1935 avc_ss_reset(seqno);
1936 selnl_notify_policyload(seqno);
1937 selinux_xfrm_notify_policyload();
1942 int security_get_bool_value(int bool)
1949 len = policydb.p_bools.nprim;
1955 rc = policydb.bool_val_to_struct[bool]->state;
1961 static int security_preserve_bools(struct policydb *p)
1963 int rc, nbools = 0, *bvalues = NULL, i;
1964 char **bnames = NULL;
1965 struct cond_bool_datum *booldatum;
1966 struct cond_node *cur;
1968 rc = security_get_bools(&nbools, &bnames, &bvalues);
1971 for (i = 0; i < nbools; i++) {
1972 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1974 booldatum->state = bvalues[i];
1976 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1977 rc = evaluate_cond_node(p, cur);
1984 for (i = 0; i < nbools; i++)
1993 * security_sid_mls_copy() - computes a new sid based on the given
1994 * sid and the mls portion of mls_sid.
1996 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1998 struct context *context1;
1999 struct context *context2;
2000 struct context newcon;
2005 if (!ss_initialized || !selinux_mls_enabled) {
2010 context_init(&newcon);
2013 context1 = sidtab_search(&sidtab, sid);
2015 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2021 context2 = sidtab_search(&sidtab, mls_sid);
2023 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2029 newcon.user = context1->user;
2030 newcon.role = context1->role;
2031 newcon.type = context1->type;
2032 rc = mls_context_cpy(&newcon, context2);
2036 /* Check the validity of the new context. */
2037 if (!policydb_context_isvalid(&policydb, &newcon)) {
2038 rc = convert_context_handle_invalid_context(&newcon);
2043 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2047 if (!context_struct_to_string(&newcon, &s, &len)) {
2048 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2049 "security_sid_mls_copy: invalid context %s", s);
2055 context_destroy(&newcon);
2061 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2062 * @nlbl_sid: NetLabel SID
2063 * @nlbl_type: NetLabel labeling protocol type
2064 * @xfrm_sid: XFRM SID
2067 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2068 * resolved into a single SID it is returned via @peer_sid and the function
2069 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2070 * returns a negative value. A table summarizing the behavior is below:
2072 * | function return | @sid
2073 * ------------------------------+-----------------+-----------------
2074 * no peer labels | 0 | SECSID_NULL
2075 * single peer label | 0 | <peer_label>
2076 * multiple, consistent labels | 0 | <peer_label>
2077 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2080 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2085 struct context *nlbl_ctx;
2086 struct context *xfrm_ctx;
2088 /* handle the common (which also happens to be the set of easy) cases
2089 * right away, these two if statements catch everything involving a
2090 * single or absent peer SID/label */
2091 if (xfrm_sid == SECSID_NULL) {
2092 *peer_sid = nlbl_sid;
2095 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2096 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2098 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2099 *peer_sid = xfrm_sid;
2103 /* we don't need to check ss_initialized here since the only way both
2104 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2105 * security server was initialized and ss_initialized was true */
2106 if (!selinux_mls_enabled) {
2107 *peer_sid = SECSID_NULL;
2113 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2116 "security_sid_mls_cmp: unrecognized SID %d\n",
2121 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2124 "security_sid_mls_cmp: unrecognized SID %d\n",
2129 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2134 /* at present NetLabel SIDs/labels really only carry MLS
2135 * information so if the MLS portion of the NetLabel SID
2136 * matches the MLS portion of the labeled XFRM SID/label
2137 * then pass along the XFRM SID as it is the most
2139 *peer_sid = xfrm_sid;
2141 *peer_sid = SECSID_NULL;
2145 static int get_classes_callback(void *k, void *d, void *args)
2147 struct class_datum *datum = d;
2148 char *name = k, **classes = args;
2149 int value = datum->value - 1;
2151 classes[value] = kstrdup(name, GFP_ATOMIC);
2152 if (!classes[value])
2158 int security_get_classes(char ***classes, int *nclasses)
2164 *nclasses = policydb.p_classes.nprim;
2165 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2169 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2173 for (i = 0; i < *nclasses; i++)
2174 kfree((*classes)[i]);
2183 static int get_permissions_callback(void *k, void *d, void *args)
2185 struct perm_datum *datum = d;
2186 char *name = k, **perms = args;
2187 int value = datum->value - 1;
2189 perms[value] = kstrdup(name, GFP_ATOMIC);
2196 int security_get_permissions(char *class, char ***perms, int *nperms)
2198 int rc = -ENOMEM, i;
2199 struct class_datum *match;
2203 match = hashtab_search(policydb.p_classes.table, class);
2205 printk(KERN_ERR "%s: unrecognized class %s\n",
2206 __FUNCTION__, class);
2211 *nperms = match->permissions.nprim;
2212 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2216 if (match->comdatum) {
2217 rc = hashtab_map(match->comdatum->permissions.table,
2218 get_permissions_callback, *perms);
2223 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2234 for (i = 0; i < *nperms; i++)
2240 int security_get_reject_unknown(void)
2242 return policydb.reject_unknown;
2245 int security_get_allow_unknown(void)
2247 return policydb.allow_unknown;
2251 * security_policycap_supported - Check for a specific policy capability
2252 * @req_cap: capability
2255 * This function queries the currently loaded policy to see if it supports the
2256 * capability specified by @req_cap. Returns true (1) if the capability is
2257 * supported, false (0) if it isn't supported.
2260 int security_policycap_supported(unsigned int req_cap)
2265 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2271 struct selinux_audit_rule {
2273 struct context au_ctxt;
2276 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
2279 context_destroy(&rule->au_ctxt);
2284 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
2285 struct selinux_audit_rule **rule)
2287 struct selinux_audit_rule *tmprule;
2288 struct role_datum *roledatum;
2289 struct type_datum *typedatum;
2290 struct user_datum *userdatum;
2295 if (!ss_initialized)
2299 case AUDIT_SUBJ_USER:
2300 case AUDIT_SUBJ_ROLE:
2301 case AUDIT_SUBJ_TYPE:
2302 case AUDIT_OBJ_USER:
2303 case AUDIT_OBJ_ROLE:
2304 case AUDIT_OBJ_TYPE:
2305 /* only 'equals' and 'not equals' fit user, role, and type */
2306 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2309 case AUDIT_SUBJ_SEN:
2310 case AUDIT_SUBJ_CLR:
2311 case AUDIT_OBJ_LEV_LOW:
2312 case AUDIT_OBJ_LEV_HIGH:
2313 /* we do not allow a range, indicated by the presense of '-' */
2314 if (strchr(rulestr, '-'))
2318 /* only the above fields are valid */
2322 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2326 context_init(&tmprule->au_ctxt);
2330 tmprule->au_seqno = latest_granting;
2333 case AUDIT_SUBJ_USER:
2334 case AUDIT_OBJ_USER:
2335 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2339 tmprule->au_ctxt.user = userdatum->value;
2341 case AUDIT_SUBJ_ROLE:
2342 case AUDIT_OBJ_ROLE:
2343 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2347 tmprule->au_ctxt.role = roledatum->value;
2349 case AUDIT_SUBJ_TYPE:
2350 case AUDIT_OBJ_TYPE:
2351 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2355 tmprule->au_ctxt.type = typedatum->value;
2357 case AUDIT_SUBJ_SEN:
2358 case AUDIT_SUBJ_CLR:
2359 case AUDIT_OBJ_LEV_LOW:
2360 case AUDIT_OBJ_LEV_HIGH:
2361 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2368 selinux_audit_rule_free(tmprule);
2377 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2378 struct selinux_audit_rule *rule,
2379 struct audit_context *actx)
2381 struct context *ctxt;
2382 struct mls_level *level;
2386 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2387 "selinux_audit_rule_match: missing rule\n");
2393 if (rule->au_seqno < latest_granting) {
2394 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2395 "selinux_audit_rule_match: stale rule\n");
2400 ctxt = sidtab_search(&sidtab, sid);
2402 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2403 "selinux_audit_rule_match: unrecognized SID %d\n",
2409 /* a field/op pair that is not caught here will simply fall through
2412 case AUDIT_SUBJ_USER:
2413 case AUDIT_OBJ_USER:
2416 match = (ctxt->user == rule->au_ctxt.user);
2418 case AUDIT_NOT_EQUAL:
2419 match = (ctxt->user != rule->au_ctxt.user);
2423 case AUDIT_SUBJ_ROLE:
2424 case AUDIT_OBJ_ROLE:
2427 match = (ctxt->role == rule->au_ctxt.role);
2429 case AUDIT_NOT_EQUAL:
2430 match = (ctxt->role != rule->au_ctxt.role);
2434 case AUDIT_SUBJ_TYPE:
2435 case AUDIT_OBJ_TYPE:
2438 match = (ctxt->type == rule->au_ctxt.type);
2440 case AUDIT_NOT_EQUAL:
2441 match = (ctxt->type != rule->au_ctxt.type);
2445 case AUDIT_SUBJ_SEN:
2446 case AUDIT_SUBJ_CLR:
2447 case AUDIT_OBJ_LEV_LOW:
2448 case AUDIT_OBJ_LEV_HIGH:
2449 level = ((field == AUDIT_SUBJ_SEN ||
2450 field == AUDIT_OBJ_LEV_LOW) ?
2451 &ctxt->range.level[0] : &ctxt->range.level[1]);
2454 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2457 case AUDIT_NOT_EQUAL:
2458 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2461 case AUDIT_LESS_THAN:
2462 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2464 !mls_level_eq(&rule->au_ctxt.range.level[0],
2467 case AUDIT_LESS_THAN_OR_EQUAL:
2468 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2471 case AUDIT_GREATER_THAN:
2472 match = (mls_level_dom(level,
2473 &rule->au_ctxt.range.level[0]) &&
2474 !mls_level_eq(level,
2475 &rule->au_ctxt.range.level[0]));
2477 case AUDIT_GREATER_THAN_OR_EQUAL:
2478 match = mls_level_dom(level,
2479 &rule->au_ctxt.range.level[0]);
2489 static int (*aurule_callback)(void) = NULL;
2491 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2492 u16 class, u32 perms, u32 *retained)
2496 if (event == AVC_CALLBACK_RESET && aurule_callback)
2497 err = aurule_callback();
2501 static int __init aurule_init(void)
2505 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2506 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2508 panic("avc_add_callback() failed, error %d\n", err);
2512 __initcall(aurule_init);
2514 void selinux_audit_set_callback(int (*callback)(void))
2516 aurule_callback = callback;
2519 #ifdef CONFIG_NETLABEL
2521 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2522 * @secattr: the NetLabel packet security attributes
2523 * @sid: the SELinux SID
2526 * Attempt to cache the context in @ctx, which was derived from the packet in
2527 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2528 * already been initialized.
2531 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2536 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2537 if (sid_cache == NULL)
2539 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2540 if (secattr->cache == NULL) {
2546 secattr->cache->free = kfree;
2547 secattr->cache->data = sid_cache;
2548 secattr->flags |= NETLBL_SECATTR_CACHE;
2552 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2553 * @secattr: the NetLabel packet security attributes
2554 * @sid: the SELinux SID
2557 * Convert the given NetLabel security attributes in @secattr into a
2558 * SELinux SID. If the @secattr field does not contain a full SELinux
2559 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2560 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2561 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2562 * conversion for future lookups. Returns zero on success, negative values on
2566 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2570 struct context *ctx;
2571 struct context ctx_new;
2573 if (!ss_initialized) {
2580 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2581 *sid = *(u32 *)secattr->cache->data;
2583 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2584 *sid = secattr->attr.secid;
2586 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2587 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
2589 goto netlbl_secattr_to_sid_return;
2591 ctx_new.user = ctx->user;
2592 ctx_new.role = ctx->role;
2593 ctx_new.type = ctx->type;
2594 mls_import_netlbl_lvl(&ctx_new, secattr);
2595 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2596 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2597 secattr->attr.mls.cat) != 0)
2598 goto netlbl_secattr_to_sid_return;
2599 ctx_new.range.level[1].cat.highbit =
2600 ctx_new.range.level[0].cat.highbit;
2601 ctx_new.range.level[1].cat.node =
2602 ctx_new.range.level[0].cat.node;
2604 ebitmap_init(&ctx_new.range.level[0].cat);
2605 ebitmap_init(&ctx_new.range.level[1].cat);
2607 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2608 goto netlbl_secattr_to_sid_return_cleanup;
2610 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2612 goto netlbl_secattr_to_sid_return_cleanup;
2614 security_netlbl_cache_add(secattr, *sid);
2616 ebitmap_destroy(&ctx_new.range.level[0].cat);
2622 netlbl_secattr_to_sid_return:
2625 netlbl_secattr_to_sid_return_cleanup:
2626 ebitmap_destroy(&ctx_new.range.level[0].cat);
2627 goto netlbl_secattr_to_sid_return;
2631 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2632 * @sid: the SELinux SID
2633 * @secattr: the NetLabel packet security attributes
2636 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2637 * Returns zero on success, negative values on failure.
2640 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2643 struct context *ctx;
2645 if (!ss_initialized)
2649 ctx = sidtab_search(&sidtab, sid);
2651 goto netlbl_sid_to_secattr_failure;
2652 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2654 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2655 mls_export_netlbl_lvl(ctx, secattr);
2656 rc = mls_export_netlbl_cat(ctx, secattr);
2658 goto netlbl_sid_to_secattr_failure;
2663 netlbl_sid_to_secattr_failure:
2667 #endif /* CONFIG_NETLABEL */