2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
21 * Updated: Chad Sellers <csellers@tresys.com>
23 * Added validation of kernel classes and permissions
25 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
28 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/spinlock.h>
37 #include <linux/rcupdate.h>
38 #include <linux/errno.h>
40 #include <linux/sched.h>
41 #include <linux/audit.h>
42 #include <linux/mutex.h>
43 #include <linux/selinux.h>
44 #include <net/netlabel.h>
54 #include "conditional.h"
62 extern void selnl_notify_policyload(u32 seqno);
63 unsigned int policydb_loaded_version;
65 int selinux_policycap_netpeer;
66 int selinux_policycap_openperm;
69 * This is declared in avc.c
71 extern const struct selinux_class_perm selinux_class_perm;
73 static DEFINE_RWLOCK(policy_rwlock);
75 static DEFINE_MUTEX(load_mutex);
76 #define LOAD_LOCK mutex_lock(&load_mutex)
77 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
79 static struct sidtab sidtab;
80 struct policydb policydb;
84 * The largest sequence number that has been used when
85 * providing an access decision to the access vector cache.
86 * The sequence number only changes when a policy change
89 static u32 latest_granting;
91 /* Forward declaration. */
92 static int context_struct_to_string(struct context *context, char **scontext,
96 * Return the boolean value of a constraint expression
97 * when it is applied to the specified source and target
100 * xcontext is a special beast... It is used by the validatetrans rules
101 * only. For these rules, scontext is the context before the transition,
102 * tcontext is the context after the transition, and xcontext is the context
103 * of the process performing the transition. All other callers of
104 * constraint_expr_eval should pass in NULL for xcontext.
106 static int constraint_expr_eval(struct context *scontext,
107 struct context *tcontext,
108 struct context *xcontext,
109 struct constraint_expr *cexpr)
113 struct role_datum *r1, *r2;
114 struct mls_level *l1, *l2;
115 struct constraint_expr *e;
116 int s[CEXPR_MAXDEPTH];
119 for (e = cexpr; e; e = e->next) {
120 switch (e->expr_type) {
136 if (sp == (CEXPR_MAXDEPTH-1))
140 val1 = scontext->user;
141 val2 = tcontext->user;
144 val1 = scontext->type;
145 val2 = tcontext->type;
148 val1 = scontext->role;
149 val2 = tcontext->role;
150 r1 = policydb.role_val_to_struct[val1 - 1];
151 r2 = policydb.role_val_to_struct[val2 - 1];
154 s[++sp] = ebitmap_get_bit(&r1->dominates,
158 s[++sp] = ebitmap_get_bit(&r2->dominates,
162 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
164 !ebitmap_get_bit(&r2->dominates,
172 l1 = &(scontext->range.level[0]);
173 l2 = &(tcontext->range.level[0]);
176 l1 = &(scontext->range.level[0]);
177 l2 = &(tcontext->range.level[1]);
180 l1 = &(scontext->range.level[1]);
181 l2 = &(tcontext->range.level[0]);
184 l1 = &(scontext->range.level[1]);
185 l2 = &(tcontext->range.level[1]);
188 l1 = &(scontext->range.level[0]);
189 l2 = &(scontext->range.level[1]);
192 l1 = &(tcontext->range.level[0]);
193 l2 = &(tcontext->range.level[1]);
198 s[++sp] = mls_level_eq(l1, l2);
201 s[++sp] = !mls_level_eq(l1, l2);
204 s[++sp] = mls_level_dom(l1, l2);
207 s[++sp] = mls_level_dom(l2, l1);
210 s[++sp] = mls_level_incomp(l2, l1);
224 s[++sp] = (val1 == val2);
227 s[++sp] = (val1 != val2);
235 if (sp == (CEXPR_MAXDEPTH-1))
238 if (e->attr & CEXPR_TARGET)
240 else if (e->attr & CEXPR_XTARGET) {
247 if (e->attr & CEXPR_USER)
249 else if (e->attr & CEXPR_ROLE)
251 else if (e->attr & CEXPR_TYPE)
260 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
263 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
281 * Compute access vectors based on a context structure pair for
282 * the permissions in a particular class.
284 static int context_struct_compute_av(struct context *scontext,
285 struct context *tcontext,
288 struct av_decision *avd)
290 struct constraint_node *constraint;
291 struct role_allow *ra;
292 struct avtab_key avkey;
293 struct avtab_node *node;
294 struct class_datum *tclass_datum;
295 struct ebitmap *sattr, *tattr;
296 struct ebitmap_node *snode, *tnode;
297 const struct selinux_class_perm *kdefs = &selinux_class_perm;
301 * Remap extended Netlink classes for old policy versions.
302 * Do this here rather than socket_type_to_security_class()
303 * in case a newer policy version is loaded, allowing sockets
304 * to remain in the correct class.
306 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
307 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
308 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
309 tclass = SECCLASS_NETLINK_SOCKET;
312 * Initialize the access vectors to the default values.
315 avd->decided = 0xffffffff;
317 avd->auditdeny = 0xffffffff;
318 avd->seqno = latest_granting;
321 * Check for all the invalid cases.
323 * - tclass > policy and > kernel
324 * - tclass > policy but is a userspace class
325 * - tclass > policy but we do not allow unknowns
327 if (unlikely(!tclass))
329 if (unlikely(tclass > policydb.p_classes.nprim))
330 if (tclass > kdefs->cts_len ||
331 !kdefs->class_to_string[tclass - 1] ||
332 !policydb.allow_unknown)
336 * Kernel class and we allow unknown so pad the allow decision
337 * the pad will be all 1 for unknown classes.
339 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
340 avd->allowed = policydb.undefined_perms[tclass - 1];
343 * Not in policy. Since decision is completed (all 1 or all 0) return.
345 if (unlikely(tclass > policydb.p_classes.nprim))
348 tclass_datum = policydb.class_val_to_struct[tclass - 1];
351 * If a specific type enforcement rule was defined for
352 * this permission check, then use it.
354 avkey.target_class = tclass;
355 avkey.specified = AVTAB_AV;
356 sattr = &policydb.type_attr_map[scontext->type - 1];
357 tattr = &policydb.type_attr_map[tcontext->type - 1];
358 ebitmap_for_each_positive_bit(sattr, snode, i) {
359 ebitmap_for_each_positive_bit(tattr, tnode, j) {
360 avkey.source_type = i + 1;
361 avkey.target_type = j + 1;
362 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
364 node = avtab_search_node_next(node, avkey.specified)) {
365 if (node->key.specified == AVTAB_ALLOWED)
366 avd->allowed |= node->datum.data;
367 else if (node->key.specified == AVTAB_AUDITALLOW)
368 avd->auditallow |= node->datum.data;
369 else if (node->key.specified == AVTAB_AUDITDENY)
370 avd->auditdeny &= node->datum.data;
373 /* Check conditional av table for additional permissions */
374 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
380 * Remove any permissions prohibited by a constraint (this includes
383 constraint = tclass_datum->constraints;
385 if ((constraint->permissions & (avd->allowed)) &&
386 !constraint_expr_eval(scontext, tcontext, NULL,
388 avd->allowed = (avd->allowed) & ~(constraint->permissions);
390 constraint = constraint->next;
394 * If checking process transition permission and the
395 * role is changing, then check the (current_role, new_role)
398 if (tclass == SECCLASS_PROCESS &&
399 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
400 scontext->role != tcontext->role) {
401 for (ra = policydb.role_allow; ra; ra = ra->next) {
402 if (scontext->role == ra->role &&
403 tcontext->role == ra->new_role)
407 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
408 PROCESS__DYNTRANSITION);
414 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
420 * Given a sid find if the type has the permissive flag set
422 int security_permissive_sid(u32 sid)
424 struct context *context;
428 read_lock(&policy_rwlock);
430 context = sidtab_search(&sidtab, sid);
433 type = context->type;
435 * we are intentionally using type here, not type-1, the 0th bit may
436 * someday indicate that we are globally setting permissive in policy.
438 rc = ebitmap_get_bit(&policydb.permissive_map, type);
440 read_unlock(&policy_rwlock);
444 static int security_validtrans_handle_fail(struct context *ocontext,
445 struct context *ncontext,
446 struct context *tcontext,
449 char *o = NULL, *n = NULL, *t = NULL;
450 u32 olen, nlen, tlen;
452 if (context_struct_to_string(ocontext, &o, &olen) < 0)
454 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
456 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
458 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
459 "security_validate_transition: denied for"
460 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
461 o, n, t, policydb.p_class_val_to_name[tclass-1]);
467 if (!selinux_enforcing)
472 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
475 struct context *ocontext;
476 struct context *ncontext;
477 struct context *tcontext;
478 struct class_datum *tclass_datum;
479 struct constraint_node *constraint;
485 read_lock(&policy_rwlock);
488 * Remap extended Netlink classes for old policy versions.
489 * Do this here rather than socket_type_to_security_class()
490 * in case a newer policy version is loaded, allowing sockets
491 * to remain in the correct class.
493 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
494 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
495 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
496 tclass = SECCLASS_NETLINK_SOCKET;
498 if (!tclass || tclass > policydb.p_classes.nprim) {
499 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
504 tclass_datum = policydb.class_val_to_struct[tclass - 1];
506 ocontext = sidtab_search(&sidtab, oldsid);
508 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
514 ncontext = sidtab_search(&sidtab, newsid);
516 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
522 tcontext = sidtab_search(&sidtab, tasksid);
524 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
530 constraint = tclass_datum->validatetrans;
532 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
534 rc = security_validtrans_handle_fail(ocontext, ncontext,
538 constraint = constraint->next;
542 read_unlock(&policy_rwlock);
547 * security_compute_av - Compute access vector decisions.
548 * @ssid: source security identifier
549 * @tsid: target security identifier
550 * @tclass: target security class
551 * @requested: requested permissions
552 * @avd: access vector decisions
554 * Compute a set of access vector decisions based on the
555 * SID pair (@ssid, @tsid) for the permissions in @tclass.
556 * Return -%EINVAL if any of the parameters are invalid or %0
557 * if the access vector decisions were computed successfully.
559 int security_compute_av(u32 ssid,
563 struct av_decision *avd)
565 struct context *scontext = NULL, *tcontext = NULL;
568 if (!ss_initialized) {
569 avd->allowed = 0xffffffff;
570 avd->decided = 0xffffffff;
572 avd->auditdeny = 0xffffffff;
573 avd->seqno = latest_granting;
577 read_lock(&policy_rwlock);
579 scontext = sidtab_search(&sidtab, ssid);
581 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
586 tcontext = sidtab_search(&sidtab, tsid);
588 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
594 rc = context_struct_compute_av(scontext, tcontext, tclass,
597 read_unlock(&policy_rwlock);
602 * Write the security context string representation of
603 * the context structure `context' into a dynamically
604 * allocated string of the correct size. Set `*scontext'
605 * to point to this string and set `*scontext_len' to
606 * the length of the string.
608 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
616 *scontext_len = context->len;
617 *scontext = kstrdup(context->str, GFP_ATOMIC);
623 /* Compute the size of the context. */
624 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
625 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
626 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
627 *scontext_len += mls_compute_context_len(context);
629 /* Allocate space for the context; caller must free this space. */
630 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
633 *scontext = scontextp;
636 * Copy the user name, role name and type name into the context.
638 sprintf(scontextp, "%s:%s:%s",
639 policydb.p_user_val_to_name[context->user - 1],
640 policydb.p_role_val_to_name[context->role - 1],
641 policydb.p_type_val_to_name[context->type - 1]);
642 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
643 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
644 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
646 mls_sid_to_context(context, &scontextp);
653 #include "initial_sid_to_string.h"
655 const char *security_get_initial_sid_context(u32 sid)
657 if (unlikely(sid > SECINITSID_NUM))
659 return initial_sid_to_string[sid];
662 static int security_sid_to_context_core(u32 sid, char **scontext,
663 u32 *scontext_len, int force)
665 struct context *context;
671 if (!ss_initialized) {
672 if (sid <= SECINITSID_NUM) {
675 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
676 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
681 strcpy(scontextp, initial_sid_to_string[sid]);
682 *scontext = scontextp;
685 printk(KERN_ERR "SELinux: %s: called before initial "
686 "load_policy on unknown SID %d\n", __func__, sid);
690 read_lock(&policy_rwlock);
692 context = sidtab_search_force(&sidtab, sid);
694 context = sidtab_search(&sidtab, sid);
696 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
701 rc = context_struct_to_string(context, scontext, scontext_len);
703 read_unlock(&policy_rwlock);
710 * security_sid_to_context - Obtain a context for a given SID.
711 * @sid: security identifier, SID
712 * @scontext: security context
713 * @scontext_len: length in bytes
715 * Write the string representation of the context associated with @sid
716 * into a dynamically allocated string of the correct size. Set @scontext
717 * to point to this string and set @scontext_len to the length of the string.
719 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
721 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
724 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
726 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
730 * Caveat: Mutates scontext.
732 static int string_to_context_struct(struct policydb *pol,
733 struct sidtab *sidtabp,
739 struct role_datum *role;
740 struct type_datum *typdatum;
741 struct user_datum *usrdatum;
742 char *scontextp, *p, oldc;
747 /* Parse the security context. */
750 scontextp = (char *) scontext;
752 /* Extract the user. */
754 while (*p && *p != ':')
762 usrdatum = hashtab_search(pol->p_users.table, scontextp);
766 ctx->user = usrdatum->value;
770 while (*p && *p != ':')
778 role = hashtab_search(pol->p_roles.table, scontextp);
781 ctx->role = role->value;
785 while (*p && *p != ':')
790 typdatum = hashtab_search(pol->p_types.table, scontextp);
794 ctx->type = typdatum->value;
796 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
800 if ((p - scontext) < scontext_len) {
805 /* Check the validity of the new context. */
806 if (!policydb_context_isvalid(pol, ctx)) {
808 context_destroy(ctx);
816 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
817 u32 *sid, u32 def_sid, gfp_t gfp_flags,
820 char *scontext2, *str = NULL;
821 struct context context;
824 if (!ss_initialized) {
827 for (i = 1; i < SECINITSID_NUM; i++) {
828 if (!strcmp(initial_sid_to_string[i], scontext)) {
833 *sid = SECINITSID_KERNEL;
838 /* Copy the string so that we can modify the copy as we parse it. */
839 scontext2 = kmalloc(scontext_len+1, gfp_flags);
842 memcpy(scontext2, scontext, scontext_len);
843 scontext2[scontext_len] = 0;
846 /* Save another copy for storing in uninterpreted form */
847 str = kstrdup(scontext2, gfp_flags);
854 read_lock(&policy_rwlock);
855 rc = string_to_context_struct(&policydb, &sidtab,
856 scontext2, scontext_len,
858 if (rc == -EINVAL && force) {
860 context.len = scontext_len;
864 rc = sidtab_context_to_sid(&sidtab, &context, sid);
866 context_destroy(&context);
868 read_unlock(&policy_rwlock);
875 * security_context_to_sid - Obtain a SID for a given security context.
876 * @scontext: security context
877 * @scontext_len: length in bytes
878 * @sid: security identifier, SID
880 * Obtains a SID associated with the security context that
881 * has the string representation specified by @scontext.
882 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
883 * memory is available, or 0 on success.
885 int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
887 return security_context_to_sid_core(scontext, scontext_len,
888 sid, SECSID_NULL, GFP_KERNEL, 0);
892 * security_context_to_sid_default - Obtain a SID for a given security context,
893 * falling back to specified default if needed.
895 * @scontext: security context
896 * @scontext_len: length in bytes
897 * @sid: security identifier, SID
898 * @def_sid: default SID to assign on error
900 * Obtains a SID associated with the security context that
901 * has the string representation specified by @scontext.
902 * The default SID is passed to the MLS layer to be used to allow
903 * kernel labeling of the MLS field if the MLS field is not present
904 * (for upgrading to MLS without full relabel).
905 * Implicitly forces adding of the context even if it cannot be mapped yet.
906 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
907 * memory is available, or 0 on success.
909 int security_context_to_sid_default(const char *scontext, u32 scontext_len,
910 u32 *sid, u32 def_sid, gfp_t gfp_flags)
912 return security_context_to_sid_core(scontext, scontext_len,
913 sid, def_sid, gfp_flags, 1);
916 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
919 return security_context_to_sid_core(scontext, scontext_len,
920 sid, SECSID_NULL, GFP_KERNEL, 1);
923 static int compute_sid_handle_invalid_context(
924 struct context *scontext,
925 struct context *tcontext,
927 struct context *newcontext)
929 char *s = NULL, *t = NULL, *n = NULL;
930 u32 slen, tlen, nlen;
932 if (context_struct_to_string(scontext, &s, &slen) < 0)
934 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
936 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
938 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
939 "security_compute_sid: invalid context %s"
943 n, s, t, policydb.p_class_val_to_name[tclass-1]);
948 if (!selinux_enforcing)
953 static int security_compute_sid(u32 ssid,
959 struct context *scontext = NULL, *tcontext = NULL, newcontext;
960 struct role_trans *roletr = NULL;
961 struct avtab_key avkey;
962 struct avtab_datum *avdatum;
963 struct avtab_node *node;
966 if (!ss_initialized) {
968 case SECCLASS_PROCESS:
978 context_init(&newcontext);
980 read_lock(&policy_rwlock);
982 scontext = sidtab_search(&sidtab, ssid);
984 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
989 tcontext = sidtab_search(&sidtab, tsid);
991 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
997 /* Set the user identity. */
999 case AVTAB_TRANSITION:
1001 /* Use the process user identity. */
1002 newcontext.user = scontext->user;
1005 /* Use the related object owner. */
1006 newcontext.user = tcontext->user;
1010 /* Set the role and type to default values. */
1012 case SECCLASS_PROCESS:
1013 /* Use the current role and type of process. */
1014 newcontext.role = scontext->role;
1015 newcontext.type = scontext->type;
1018 /* Use the well-defined object role. */
1019 newcontext.role = OBJECT_R_VAL;
1020 /* Use the type of the related object. */
1021 newcontext.type = tcontext->type;
1024 /* Look for a type transition/member/change rule. */
1025 avkey.source_type = scontext->type;
1026 avkey.target_type = tcontext->type;
1027 avkey.target_class = tclass;
1028 avkey.specified = specified;
1029 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1031 /* If no permanent rule, also check for enabled conditional rules */
1033 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1034 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
1035 if (node->key.specified & AVTAB_ENABLED) {
1036 avdatum = &node->datum;
1043 /* Use the type from the type transition/member/change rule. */
1044 newcontext.type = avdatum->data;
1047 /* Check for class-specific changes. */
1049 case SECCLASS_PROCESS:
1050 if (specified & AVTAB_TRANSITION) {
1051 /* Look for a role transition rule. */
1052 for (roletr = policydb.role_tr; roletr;
1053 roletr = roletr->next) {
1054 if (roletr->role == scontext->role &&
1055 roletr->type == tcontext->type) {
1056 /* Use the role transition rule. */
1057 newcontext.role = roletr->new_role;
1067 /* Set the MLS attributes.
1068 This is done last because it may allocate memory. */
1069 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1073 /* Check the validity of the context. */
1074 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1075 rc = compute_sid_handle_invalid_context(scontext,
1082 /* Obtain the sid for the context. */
1083 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1085 read_unlock(&policy_rwlock);
1086 context_destroy(&newcontext);
1092 * security_transition_sid - Compute the SID for a new subject/object.
1093 * @ssid: source security identifier
1094 * @tsid: target security identifier
1095 * @tclass: target security class
1096 * @out_sid: security identifier for new subject/object
1098 * Compute a SID to use for labeling a new subject or object in the
1099 * class @tclass based on a SID pair (@ssid, @tsid).
1100 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1101 * if insufficient memory is available, or %0 if the new SID was
1102 * computed successfully.
1104 int security_transition_sid(u32 ssid,
1109 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1113 * security_member_sid - Compute the SID for member selection.
1114 * @ssid: source security identifier
1115 * @tsid: target security identifier
1116 * @tclass: target security class
1117 * @out_sid: security identifier for selected member
1119 * Compute a SID to use when selecting a member of a polyinstantiated
1120 * object of class @tclass based on a SID pair (@ssid, @tsid).
1121 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1122 * if insufficient memory is available, or %0 if the SID was
1123 * computed successfully.
1125 int security_member_sid(u32 ssid,
1130 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1134 * security_change_sid - Compute the SID for object relabeling.
1135 * @ssid: source security identifier
1136 * @tsid: target security identifier
1137 * @tclass: target security class
1138 * @out_sid: security identifier for selected member
1140 * Compute a SID to use for relabeling an object of class @tclass
1141 * based on a SID pair (@ssid, @tsid).
1142 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1143 * if insufficient memory is available, or %0 if the SID was
1144 * computed successfully.
1146 int security_change_sid(u32 ssid,
1151 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1155 * Verify that each kernel class that is defined in the
1158 static int validate_classes(struct policydb *p)
1161 struct class_datum *cladatum;
1162 struct perm_datum *perdatum;
1163 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1165 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1166 const char *def_class, *def_perm, *pol_class;
1167 struct symtab *perms;
1169 if (p->allow_unknown) {
1170 u32 num_classes = kdefs->cts_len;
1171 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1172 if (!p->undefined_perms)
1176 for (i = 1; i < kdefs->cts_len; i++) {
1177 def_class = kdefs->class_to_string[i];
1180 if (i > p->p_classes.nprim) {
1182 "SELinux: class %s not defined in policy\n",
1184 if (p->reject_unknown)
1186 if (p->allow_unknown)
1187 p->undefined_perms[i-1] = ~0U;
1190 pol_class = p->p_class_val_to_name[i-1];
1191 if (strcmp(pol_class, def_class)) {
1193 "SELinux: class %d is incorrect, found %s but should be %s\n",
1194 i, pol_class, def_class);
1198 for (i = 0; i < kdefs->av_pts_len; i++) {
1199 class_val = kdefs->av_perm_to_string[i].tclass;
1200 perm_val = kdefs->av_perm_to_string[i].value;
1201 def_perm = kdefs->av_perm_to_string[i].name;
1202 if (class_val > p->p_classes.nprim)
1204 pol_class = p->p_class_val_to_name[class_val-1];
1205 cladatum = hashtab_search(p->p_classes.table, pol_class);
1207 perms = &cladatum->permissions;
1208 nprim = 1 << (perms->nprim - 1);
1209 if (perm_val > nprim) {
1211 "SELinux: permission %s in class %s not defined in policy\n",
1212 def_perm, pol_class);
1213 if (p->reject_unknown)
1215 if (p->allow_unknown)
1216 p->undefined_perms[class_val-1] |= perm_val;
1219 perdatum = hashtab_search(perms->table, def_perm);
1220 if (perdatum == NULL) {
1222 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1223 def_perm, pol_class);
1226 pol_val = 1 << (perdatum->value - 1);
1227 if (pol_val != perm_val) {
1229 "SELinux: permission %s in class %s has incorrect value\n",
1230 def_perm, pol_class);
1234 for (i = 0; i < kdefs->av_inherit_len; i++) {
1235 class_val = kdefs->av_inherit[i].tclass;
1236 if (class_val > p->p_classes.nprim)
1238 pol_class = p->p_class_val_to_name[class_val-1];
1239 cladatum = hashtab_search(p->p_classes.table, pol_class);
1241 if (!cladatum->comdatum) {
1243 "SELinux: class %s should have an inherits clause but does not\n",
1247 tmp = kdefs->av_inherit[i].common_base;
1249 while (!(tmp & 0x01)) {
1253 perms = &cladatum->comdatum->permissions;
1254 for (j = 0; j < common_pts_len; j++) {
1255 def_perm = kdefs->av_inherit[i].common_pts[j];
1256 if (j >= perms->nprim) {
1258 "SELinux: permission %s in class %s not defined in policy\n",
1259 def_perm, pol_class);
1260 if (p->reject_unknown)
1262 if (p->allow_unknown)
1263 p->undefined_perms[class_val-1] |= (1 << j);
1266 perdatum = hashtab_search(perms->table, def_perm);
1267 if (perdatum == NULL) {
1269 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1270 def_perm, pol_class);
1273 if (perdatum->value != j + 1) {
1275 "SELinux: permission %s in class %s has incorrect value\n",
1276 def_perm, pol_class);
1284 /* Clone the SID into the new SID table. */
1285 static int clone_sid(u32 sid,
1286 struct context *context,
1289 struct sidtab *s = arg;
1291 return sidtab_insert(s, sid, context);
1294 static inline int convert_context_handle_invalid_context(struct context *context)
1298 if (selinux_enforcing) {
1304 if (!context_struct_to_string(context, &s, &len)) {
1306 "SELinux: Context %s would be invalid if enforcing\n",
1314 struct convert_context_args {
1315 struct policydb *oldp;
1316 struct policydb *newp;
1320 * Convert the values in the security context
1321 * structure `c' from the values specified
1322 * in the policy `p->oldp' to the values specified
1323 * in the policy `p->newp'. Verify that the
1324 * context is valid under the new policy.
1326 static int convert_context(u32 key,
1330 struct convert_context_args *args;
1331 struct context oldc;
1332 struct role_datum *role;
1333 struct type_datum *typdatum;
1334 struct user_datum *usrdatum;
1343 s = kstrdup(c->str, GFP_KERNEL);
1348 rc = string_to_context_struct(args->newp, NULL, s,
1349 c->len, &ctx, SECSID_NULL);
1353 "SELinux: Context %s became valid (mapped).\n",
1355 /* Replace string with mapped representation. */
1357 memcpy(c, &ctx, sizeof(*c));
1359 } else if (rc == -EINVAL) {
1360 /* Retain string representation for later mapping. */
1364 /* Other error condition, e.g. ENOMEM. */
1366 "SELinux: Unable to map context %s, rc = %d.\n",
1372 rc = context_cpy(&oldc, c);
1378 /* Convert the user. */
1379 usrdatum = hashtab_search(args->newp->p_users.table,
1380 args->oldp->p_user_val_to_name[c->user - 1]);
1383 c->user = usrdatum->value;
1385 /* Convert the role. */
1386 role = hashtab_search(args->newp->p_roles.table,
1387 args->oldp->p_role_val_to_name[c->role - 1]);
1390 c->role = role->value;
1392 /* Convert the type. */
1393 typdatum = hashtab_search(args->newp->p_types.table,
1394 args->oldp->p_type_val_to_name[c->type - 1]);
1397 c->type = typdatum->value;
1399 rc = mls_convert_context(args->oldp, args->newp, c);
1403 /* Check the validity of the new context. */
1404 if (!policydb_context_isvalid(args->newp, c)) {
1405 rc = convert_context_handle_invalid_context(&oldc);
1410 context_destroy(&oldc);
1415 /* Map old representation to string and save it. */
1416 if (context_struct_to_string(&oldc, &s, &len))
1418 context_destroy(&oldc);
1423 "SELinux: Context %s became invalid (unmapped).\n",
1429 static void security_load_policycaps(void)
1431 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1432 POLICYDB_CAPABILITY_NETPEER);
1433 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1434 POLICYDB_CAPABILITY_OPENPERM);
1437 extern void selinux_complete_init(void);
1438 static int security_preserve_bools(struct policydb *p);
1441 * security_load_policy - Load a security policy configuration.
1442 * @data: binary policy data
1443 * @len: length of data in bytes
1445 * Load a new set of security policy configuration data,
1446 * validate it and convert the SID table as necessary.
1447 * This function will flush the access vector cache after
1448 * loading the new policy.
1450 int security_load_policy(void *data, size_t len)
1452 struct policydb oldpolicydb, newpolicydb;
1453 struct sidtab oldsidtab, newsidtab;
1454 struct convert_context_args args;
1457 struct policy_file file = { data, len }, *fp = &file;
1461 if (!ss_initialized) {
1463 if (policydb_read(&policydb, fp)) {
1465 avtab_cache_destroy();
1468 if (policydb_load_isids(&policydb, &sidtab)) {
1470 policydb_destroy(&policydb);
1471 avtab_cache_destroy();
1474 /* Verify that the kernel defined classes are correct. */
1475 if (validate_classes(&policydb)) {
1477 "SELinux: the definition of a class is incorrect\n");
1479 sidtab_destroy(&sidtab);
1480 policydb_destroy(&policydb);
1481 avtab_cache_destroy();
1484 security_load_policycaps();
1485 policydb_loaded_version = policydb.policyvers;
1487 seqno = ++latest_granting;
1489 selinux_complete_init();
1490 avc_ss_reset(seqno);
1491 selnl_notify_policyload(seqno);
1492 selinux_netlbl_cache_invalidate();
1493 selinux_xfrm_notify_policyload();
1498 sidtab_hash_eval(&sidtab, "sids");
1501 if (policydb_read(&newpolicydb, fp)) {
1506 if (sidtab_init(&newsidtab)) {
1508 policydb_destroy(&newpolicydb);
1512 /* Verify that the kernel defined classes are correct. */
1513 if (validate_classes(&newpolicydb)) {
1515 "SELinux: the definition of a class is incorrect\n");
1520 rc = security_preserve_bools(&newpolicydb);
1522 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
1526 /* Clone the SID table. */
1527 sidtab_shutdown(&sidtab);
1528 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1534 * Convert the internal representations of contexts
1535 * in the new SID table.
1537 args.oldp = &policydb;
1538 args.newp = &newpolicydb;
1539 rc = sidtab_map(&newsidtab, convert_context, &args);
1543 /* Save the old policydb and SID table to free later. */
1544 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1545 sidtab_set(&oldsidtab, &sidtab);
1547 /* Install the new policydb and SID table. */
1548 write_lock_irq(&policy_rwlock);
1549 memcpy(&policydb, &newpolicydb, sizeof policydb);
1550 sidtab_set(&sidtab, &newsidtab);
1551 security_load_policycaps();
1552 seqno = ++latest_granting;
1553 policydb_loaded_version = policydb.policyvers;
1554 write_unlock_irq(&policy_rwlock);
1557 /* Free the old policydb and SID table. */
1558 policydb_destroy(&oldpolicydb);
1559 sidtab_destroy(&oldsidtab);
1561 avc_ss_reset(seqno);
1562 selnl_notify_policyload(seqno);
1563 selinux_netlbl_cache_invalidate();
1564 selinux_xfrm_notify_policyload();
1570 sidtab_destroy(&newsidtab);
1571 policydb_destroy(&newpolicydb);
1577 * security_port_sid - Obtain the SID for a port.
1578 * @protocol: protocol number
1579 * @port: port number
1580 * @out_sid: security identifier
1582 int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
1587 read_lock(&policy_rwlock);
1589 c = policydb.ocontexts[OCON_PORT];
1591 if (c->u.port.protocol == protocol &&
1592 c->u.port.low_port <= port &&
1593 c->u.port.high_port >= port)
1600 rc = sidtab_context_to_sid(&sidtab,
1606 *out_sid = c->sid[0];
1608 *out_sid = SECINITSID_PORT;
1612 read_unlock(&policy_rwlock);
1617 * security_netif_sid - Obtain the SID for a network interface.
1618 * @name: interface name
1619 * @if_sid: interface SID
1621 int security_netif_sid(char *name, u32 *if_sid)
1626 read_lock(&policy_rwlock);
1628 c = policydb.ocontexts[OCON_NETIF];
1630 if (strcmp(name, c->u.name) == 0)
1636 if (!c->sid[0] || !c->sid[1]) {
1637 rc = sidtab_context_to_sid(&sidtab,
1642 rc = sidtab_context_to_sid(&sidtab,
1648 *if_sid = c->sid[0];
1650 *if_sid = SECINITSID_NETIF;
1653 read_unlock(&policy_rwlock);
1657 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1661 for (i = 0; i < 4; i++)
1662 if (addr[i] != (input[i] & mask[i])) {
1671 * security_node_sid - Obtain the SID for a node (host).
1672 * @domain: communication domain aka address family
1674 * @addrlen: address length in bytes
1675 * @out_sid: security identifier
1677 int security_node_sid(u16 domain,
1685 read_lock(&policy_rwlock);
1691 if (addrlen != sizeof(u32)) {
1696 addr = *((u32 *)addrp);
1698 c = policydb.ocontexts[OCON_NODE];
1700 if (c->u.node.addr == (addr & c->u.node.mask))
1708 if (addrlen != sizeof(u64) * 2) {
1712 c = policydb.ocontexts[OCON_NODE6];
1714 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1722 *out_sid = SECINITSID_NODE;
1728 rc = sidtab_context_to_sid(&sidtab,
1734 *out_sid = c->sid[0];
1736 *out_sid = SECINITSID_NODE;
1740 read_unlock(&policy_rwlock);
1747 * security_get_user_sids - Obtain reachable SIDs for a user.
1748 * @fromsid: starting SID
1749 * @username: username
1750 * @sids: array of reachable SIDs for user
1751 * @nel: number of elements in @sids
1753 * Generate the set of SIDs for legal security contexts
1754 * for a given user that can be reached by @fromsid.
1755 * Set *@sids to point to a dynamically allocated
1756 * array containing the set of SIDs. Set *@nel to the
1757 * number of elements in the array.
1760 int security_get_user_sids(u32 fromsid,
1765 struct context *fromcon, usercon;
1766 u32 *mysids = NULL, *mysids2, sid;
1767 u32 mynel = 0, maxnel = SIDS_NEL;
1768 struct user_datum *user;
1769 struct role_datum *role;
1770 struct ebitmap_node *rnode, *tnode;
1776 if (!ss_initialized)
1779 read_lock(&policy_rwlock);
1781 context_init(&usercon);
1783 fromcon = sidtab_search(&sidtab, fromsid);
1789 user = hashtab_search(policydb.p_users.table, username);
1794 usercon.user = user->value;
1796 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1802 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
1803 role = policydb.role_val_to_struct[i];
1805 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
1808 if (mls_setup_user_range(fromcon, user, &usercon))
1811 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1814 if (mynel < maxnel) {
1815 mysids[mynel++] = sid;
1818 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1823 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1826 mysids[mynel++] = sid;
1832 read_unlock(&policy_rwlock);
1838 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1844 for (i = 0, j = 0; i < mynel; i++) {
1845 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1847 PROCESS__TRANSITION, AVC_STRICT,
1850 mysids2[j++] = mysids[i];
1862 * security_genfs_sid - Obtain a SID for a file in a filesystem
1863 * @fstype: filesystem type
1864 * @path: path from root of mount
1865 * @sclass: file security class
1866 * @sid: SID for path
1868 * Obtain a SID to use for a file in a filesystem that
1869 * cannot support xattr or use a fixed labeling behavior like
1870 * transition SIDs or task SIDs.
1872 int security_genfs_sid(const char *fstype,
1878 struct genfs *genfs;
1880 int rc = 0, cmp = 0;
1882 while (path[0] == '/' && path[1] == '/')
1885 read_lock(&policy_rwlock);
1887 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1888 cmp = strcmp(fstype, genfs->fstype);
1893 if (!genfs || cmp) {
1894 *sid = SECINITSID_UNLABELED;
1899 for (c = genfs->head; c; c = c->next) {
1900 len = strlen(c->u.name);
1901 if ((!c->v.sclass || sclass == c->v.sclass) &&
1902 (strncmp(c->u.name, path, len) == 0))
1907 *sid = SECINITSID_UNLABELED;
1913 rc = sidtab_context_to_sid(&sidtab,
1922 read_unlock(&policy_rwlock);
1927 * security_fs_use - Determine how to handle labeling for a filesystem.
1928 * @fstype: filesystem type
1929 * @behavior: labeling behavior
1930 * @sid: SID for filesystem (superblock)
1932 int security_fs_use(
1934 unsigned int *behavior,
1940 read_lock(&policy_rwlock);
1942 c = policydb.ocontexts[OCON_FSUSE];
1944 if (strcmp(fstype, c->u.name) == 0)
1950 *behavior = c->v.behavior;
1952 rc = sidtab_context_to_sid(&sidtab,
1960 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1962 *behavior = SECURITY_FS_USE_NONE;
1965 *behavior = SECURITY_FS_USE_GENFS;
1970 read_unlock(&policy_rwlock);
1974 int security_get_bools(int *len, char ***names, int **values)
1976 int i, rc = -ENOMEM;
1978 read_lock(&policy_rwlock);
1982 *len = policydb.p_bools.nprim;
1988 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
1992 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1996 for (i = 0; i < *len; i++) {
1998 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1999 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
2000 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
2003 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2004 (*names)[i][name_len - 1] = 0;
2008 read_unlock(&policy_rwlock);
2012 for (i = 0; i < *len; i++)
2020 int security_set_bools(int len, int *values)
2023 int lenp, seqno = 0;
2024 struct cond_node *cur;
2026 write_lock_irq(&policy_rwlock);
2028 lenp = policydb.p_bools.nprim;
2034 for (i = 0; i < len; i++) {
2035 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2036 audit_log(current->audit_context, GFP_ATOMIC,
2037 AUDIT_MAC_CONFIG_CHANGE,
2038 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2039 policydb.p_bool_val_to_name[i],
2041 policydb.bool_val_to_struct[i]->state,
2042 audit_get_loginuid(current),
2043 audit_get_sessionid(current));
2046 policydb.bool_val_to_struct[i]->state = 1;
2048 policydb.bool_val_to_struct[i]->state = 0;
2051 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2052 rc = evaluate_cond_node(&policydb, cur);
2057 seqno = ++latest_granting;
2060 write_unlock_irq(&policy_rwlock);
2062 avc_ss_reset(seqno);
2063 selnl_notify_policyload(seqno);
2064 selinux_xfrm_notify_policyload();
2069 int security_get_bool_value(int bool)
2074 read_lock(&policy_rwlock);
2076 len = policydb.p_bools.nprim;
2082 rc = policydb.bool_val_to_struct[bool]->state;
2084 read_unlock(&policy_rwlock);
2088 static int security_preserve_bools(struct policydb *p)
2090 int rc, nbools = 0, *bvalues = NULL, i;
2091 char **bnames = NULL;
2092 struct cond_bool_datum *booldatum;
2093 struct cond_node *cur;
2095 rc = security_get_bools(&nbools, &bnames, &bvalues);
2098 for (i = 0; i < nbools; i++) {
2099 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2101 booldatum->state = bvalues[i];
2103 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2104 rc = evaluate_cond_node(p, cur);
2111 for (i = 0; i < nbools; i++)
2120 * security_sid_mls_copy() - computes a new sid based on the given
2121 * sid and the mls portion of mls_sid.
2123 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2125 struct context *context1;
2126 struct context *context2;
2127 struct context newcon;
2132 if (!ss_initialized || !selinux_mls_enabled) {
2137 context_init(&newcon);
2139 read_lock(&policy_rwlock);
2140 context1 = sidtab_search(&sidtab, sid);
2142 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2148 context2 = sidtab_search(&sidtab, mls_sid);
2150 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2156 newcon.user = context1->user;
2157 newcon.role = context1->role;
2158 newcon.type = context1->type;
2159 rc = mls_context_cpy(&newcon, context2);
2163 /* Check the validity of the new context. */
2164 if (!policydb_context_isvalid(&policydb, &newcon)) {
2165 rc = convert_context_handle_invalid_context(&newcon);
2170 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2174 if (!context_struct_to_string(&newcon, &s, &len)) {
2175 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2176 "security_sid_mls_copy: invalid context %s", s);
2181 read_unlock(&policy_rwlock);
2182 context_destroy(&newcon);
2188 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2189 * @nlbl_sid: NetLabel SID
2190 * @nlbl_type: NetLabel labeling protocol type
2191 * @xfrm_sid: XFRM SID
2194 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2195 * resolved into a single SID it is returned via @peer_sid and the function
2196 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2197 * returns a negative value. A table summarizing the behavior is below:
2199 * | function return | @sid
2200 * ------------------------------+-----------------+-----------------
2201 * no peer labels | 0 | SECSID_NULL
2202 * single peer label | 0 | <peer_label>
2203 * multiple, consistent labels | 0 | <peer_label>
2204 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2207 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2212 struct context *nlbl_ctx;
2213 struct context *xfrm_ctx;
2215 /* handle the common (which also happens to be the set of easy) cases
2216 * right away, these two if statements catch everything involving a
2217 * single or absent peer SID/label */
2218 if (xfrm_sid == SECSID_NULL) {
2219 *peer_sid = nlbl_sid;
2222 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2223 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2225 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2226 *peer_sid = xfrm_sid;
2230 /* we don't need to check ss_initialized here since the only way both
2231 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2232 * security server was initialized and ss_initialized was true */
2233 if (!selinux_mls_enabled) {
2234 *peer_sid = SECSID_NULL;
2238 read_lock(&policy_rwlock);
2240 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2242 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2243 __func__, nlbl_sid);
2247 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2249 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2250 __func__, xfrm_sid);
2254 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2257 read_unlock(&policy_rwlock);
2259 /* at present NetLabel SIDs/labels really only carry MLS
2260 * information so if the MLS portion of the NetLabel SID
2261 * matches the MLS portion of the labeled XFRM SID/label
2262 * then pass along the XFRM SID as it is the most
2264 *peer_sid = xfrm_sid;
2266 *peer_sid = SECSID_NULL;
2270 static int get_classes_callback(void *k, void *d, void *args)
2272 struct class_datum *datum = d;
2273 char *name = k, **classes = args;
2274 int value = datum->value - 1;
2276 classes[value] = kstrdup(name, GFP_ATOMIC);
2277 if (!classes[value])
2283 int security_get_classes(char ***classes, int *nclasses)
2287 read_lock(&policy_rwlock);
2289 *nclasses = policydb.p_classes.nprim;
2290 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2294 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2298 for (i = 0; i < *nclasses; i++)
2299 kfree((*classes)[i]);
2304 read_unlock(&policy_rwlock);
2308 static int get_permissions_callback(void *k, void *d, void *args)
2310 struct perm_datum *datum = d;
2311 char *name = k, **perms = args;
2312 int value = datum->value - 1;
2314 perms[value] = kstrdup(name, GFP_ATOMIC);
2321 int security_get_permissions(char *class, char ***perms, int *nperms)
2323 int rc = -ENOMEM, i;
2324 struct class_datum *match;
2326 read_lock(&policy_rwlock);
2328 match = hashtab_search(policydb.p_classes.table, class);
2330 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
2336 *nperms = match->permissions.nprim;
2337 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2341 if (match->comdatum) {
2342 rc = hashtab_map(match->comdatum->permissions.table,
2343 get_permissions_callback, *perms);
2348 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2354 read_unlock(&policy_rwlock);
2358 read_unlock(&policy_rwlock);
2359 for (i = 0; i < *nperms; i++)
2365 int security_get_reject_unknown(void)
2367 return policydb.reject_unknown;
2370 int security_get_allow_unknown(void)
2372 return policydb.allow_unknown;
2376 * security_policycap_supported - Check for a specific policy capability
2377 * @req_cap: capability
2380 * This function queries the currently loaded policy to see if it supports the
2381 * capability specified by @req_cap. Returns true (1) if the capability is
2382 * supported, false (0) if it isn't supported.
2385 int security_policycap_supported(unsigned int req_cap)
2389 read_lock(&policy_rwlock);
2390 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2391 read_unlock(&policy_rwlock);
2396 struct selinux_audit_rule {
2398 struct context au_ctxt;
2401 void selinux_audit_rule_free(void *vrule)
2403 struct selinux_audit_rule *rule = vrule;
2406 context_destroy(&rule->au_ctxt);
2411 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2413 struct selinux_audit_rule *tmprule;
2414 struct role_datum *roledatum;
2415 struct type_datum *typedatum;
2416 struct user_datum *userdatum;
2417 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
2422 if (!ss_initialized)
2426 case AUDIT_SUBJ_USER:
2427 case AUDIT_SUBJ_ROLE:
2428 case AUDIT_SUBJ_TYPE:
2429 case AUDIT_OBJ_USER:
2430 case AUDIT_OBJ_ROLE:
2431 case AUDIT_OBJ_TYPE:
2432 /* only 'equals' and 'not equals' fit user, role, and type */
2433 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2436 case AUDIT_SUBJ_SEN:
2437 case AUDIT_SUBJ_CLR:
2438 case AUDIT_OBJ_LEV_LOW:
2439 case AUDIT_OBJ_LEV_HIGH:
2440 /* we do not allow a range, indicated by the presense of '-' */
2441 if (strchr(rulestr, '-'))
2445 /* only the above fields are valid */
2449 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2453 context_init(&tmprule->au_ctxt);
2455 read_lock(&policy_rwlock);
2457 tmprule->au_seqno = latest_granting;
2460 case AUDIT_SUBJ_USER:
2461 case AUDIT_OBJ_USER:
2462 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2466 tmprule->au_ctxt.user = userdatum->value;
2468 case AUDIT_SUBJ_ROLE:
2469 case AUDIT_OBJ_ROLE:
2470 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2474 tmprule->au_ctxt.role = roledatum->value;
2476 case AUDIT_SUBJ_TYPE:
2477 case AUDIT_OBJ_TYPE:
2478 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2482 tmprule->au_ctxt.type = typedatum->value;
2484 case AUDIT_SUBJ_SEN:
2485 case AUDIT_SUBJ_CLR:
2486 case AUDIT_OBJ_LEV_LOW:
2487 case AUDIT_OBJ_LEV_HIGH:
2488 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2492 read_unlock(&policy_rwlock);
2495 selinux_audit_rule_free(tmprule);
2504 /* Check to see if the rule contains any selinux fields */
2505 int selinux_audit_rule_known(struct audit_krule *rule)
2509 for (i = 0; i < rule->field_count; i++) {
2510 struct audit_field *f = &rule->fields[i];
2512 case AUDIT_SUBJ_USER:
2513 case AUDIT_SUBJ_ROLE:
2514 case AUDIT_SUBJ_TYPE:
2515 case AUDIT_SUBJ_SEN:
2516 case AUDIT_SUBJ_CLR:
2517 case AUDIT_OBJ_USER:
2518 case AUDIT_OBJ_ROLE:
2519 case AUDIT_OBJ_TYPE:
2520 case AUDIT_OBJ_LEV_LOW:
2521 case AUDIT_OBJ_LEV_HIGH:
2529 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
2530 struct audit_context *actx)
2532 struct context *ctxt;
2533 struct mls_level *level;
2534 struct selinux_audit_rule *rule = vrule;
2538 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2539 "selinux_audit_rule_match: missing rule\n");
2543 read_lock(&policy_rwlock);
2545 if (rule->au_seqno < latest_granting) {
2546 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2547 "selinux_audit_rule_match: stale rule\n");
2552 ctxt = sidtab_search(&sidtab, sid);
2554 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2555 "selinux_audit_rule_match: unrecognized SID %d\n",
2561 /* a field/op pair that is not caught here will simply fall through
2564 case AUDIT_SUBJ_USER:
2565 case AUDIT_OBJ_USER:
2568 match = (ctxt->user == rule->au_ctxt.user);
2570 case AUDIT_NOT_EQUAL:
2571 match = (ctxt->user != rule->au_ctxt.user);
2575 case AUDIT_SUBJ_ROLE:
2576 case AUDIT_OBJ_ROLE:
2579 match = (ctxt->role == rule->au_ctxt.role);
2581 case AUDIT_NOT_EQUAL:
2582 match = (ctxt->role != rule->au_ctxt.role);
2586 case AUDIT_SUBJ_TYPE:
2587 case AUDIT_OBJ_TYPE:
2590 match = (ctxt->type == rule->au_ctxt.type);
2592 case AUDIT_NOT_EQUAL:
2593 match = (ctxt->type != rule->au_ctxt.type);
2597 case AUDIT_SUBJ_SEN:
2598 case AUDIT_SUBJ_CLR:
2599 case AUDIT_OBJ_LEV_LOW:
2600 case AUDIT_OBJ_LEV_HIGH:
2601 level = ((field == AUDIT_SUBJ_SEN ||
2602 field == AUDIT_OBJ_LEV_LOW) ?
2603 &ctxt->range.level[0] : &ctxt->range.level[1]);
2606 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2609 case AUDIT_NOT_EQUAL:
2610 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2613 case AUDIT_LESS_THAN:
2614 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2616 !mls_level_eq(&rule->au_ctxt.range.level[0],
2619 case AUDIT_LESS_THAN_OR_EQUAL:
2620 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2623 case AUDIT_GREATER_THAN:
2624 match = (mls_level_dom(level,
2625 &rule->au_ctxt.range.level[0]) &&
2626 !mls_level_eq(level,
2627 &rule->au_ctxt.range.level[0]));
2629 case AUDIT_GREATER_THAN_OR_EQUAL:
2630 match = mls_level_dom(level,
2631 &rule->au_ctxt.range.level[0]);
2637 read_unlock(&policy_rwlock);
2641 static int (*aurule_callback)(void) = audit_update_lsm_rules;
2643 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2644 u16 class, u32 perms, u32 *retained)
2648 if (event == AVC_CALLBACK_RESET && aurule_callback)
2649 err = aurule_callback();
2653 static int __init aurule_init(void)
2657 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2658 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2660 panic("avc_add_callback() failed, error %d\n", err);
2664 __initcall(aurule_init);
2666 #ifdef CONFIG_NETLABEL
2668 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2669 * @secattr: the NetLabel packet security attributes
2670 * @sid: the SELinux SID
2673 * Attempt to cache the context in @ctx, which was derived from the packet in
2674 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2675 * already been initialized.
2678 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2683 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2684 if (sid_cache == NULL)
2686 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2687 if (secattr->cache == NULL) {
2693 secattr->cache->free = kfree;
2694 secattr->cache->data = sid_cache;
2695 secattr->flags |= NETLBL_SECATTR_CACHE;
2699 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2700 * @secattr: the NetLabel packet security attributes
2701 * @sid: the SELinux SID
2704 * Convert the given NetLabel security attributes in @secattr into a
2705 * SELinux SID. If the @secattr field does not contain a full SELinux
2706 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2707 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2708 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2709 * conversion for future lookups. Returns zero on success, negative values on
2713 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2717 struct context *ctx;
2718 struct context ctx_new;
2720 if (!ss_initialized) {
2725 read_lock(&policy_rwlock);
2727 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2728 *sid = *(u32 *)secattr->cache->data;
2730 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2731 *sid = secattr->attr.secid;
2733 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2734 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
2736 goto netlbl_secattr_to_sid_return;
2738 ctx_new.user = ctx->user;
2739 ctx_new.role = ctx->role;
2740 ctx_new.type = ctx->type;
2741 mls_import_netlbl_lvl(&ctx_new, secattr);
2742 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2743 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2744 secattr->attr.mls.cat) != 0)
2745 goto netlbl_secattr_to_sid_return;
2746 ctx_new.range.level[1].cat.highbit =
2747 ctx_new.range.level[0].cat.highbit;
2748 ctx_new.range.level[1].cat.node =
2749 ctx_new.range.level[0].cat.node;
2751 ebitmap_init(&ctx_new.range.level[0].cat);
2752 ebitmap_init(&ctx_new.range.level[1].cat);
2754 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2755 goto netlbl_secattr_to_sid_return_cleanup;
2757 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2759 goto netlbl_secattr_to_sid_return_cleanup;
2761 security_netlbl_cache_add(secattr, *sid);
2763 ebitmap_destroy(&ctx_new.range.level[0].cat);
2769 netlbl_secattr_to_sid_return:
2770 read_unlock(&policy_rwlock);
2772 netlbl_secattr_to_sid_return_cleanup:
2773 ebitmap_destroy(&ctx_new.range.level[0].cat);
2774 goto netlbl_secattr_to_sid_return;
2778 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2779 * @sid: the SELinux SID
2780 * @secattr: the NetLabel packet security attributes
2783 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2784 * Returns zero on success, negative values on failure.
2787 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2790 struct context *ctx;
2792 if (!ss_initialized)
2795 read_lock(&policy_rwlock);
2796 ctx = sidtab_search(&sidtab, sid);
2798 goto netlbl_sid_to_secattr_failure;
2799 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2801 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
2802 mls_export_netlbl_lvl(ctx, secattr);
2803 rc = mls_export_netlbl_cat(ctx, secattr);
2805 goto netlbl_sid_to_secattr_failure;
2806 read_unlock(&policy_rwlock);
2810 netlbl_sid_to_secattr_failure:
2811 read_unlock(&policy_rwlock);
2814 #endif /* CONFIG_NETLABEL */