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);
74 static DEFINE_MUTEX(load_mutex);
76 static struct sidtab sidtab;
77 struct policydb policydb;
81 * The largest sequence number that has been used when
82 * providing an access decision to the access vector cache.
83 * The sequence number only changes when a policy change
86 static u32 latest_granting;
88 /* Forward declaration. */
89 static int context_struct_to_string(struct context *context, char **scontext,
93 * Return the boolean value of a constraint expression
94 * when it is applied to the specified source and target
97 * xcontext is a special beast... It is used by the validatetrans rules
98 * only. For these rules, scontext is the context before the transition,
99 * tcontext is the context after the transition, and xcontext is the context
100 * of the process performing the transition. All other callers of
101 * constraint_expr_eval should pass in NULL for xcontext.
103 static int constraint_expr_eval(struct context *scontext,
104 struct context *tcontext,
105 struct context *xcontext,
106 struct constraint_expr *cexpr)
110 struct role_datum *r1, *r2;
111 struct mls_level *l1, *l2;
112 struct constraint_expr *e;
113 int s[CEXPR_MAXDEPTH];
116 for (e = cexpr; e; e = e->next) {
117 switch (e->expr_type) {
133 if (sp == (CEXPR_MAXDEPTH-1))
137 val1 = scontext->user;
138 val2 = tcontext->user;
141 val1 = scontext->type;
142 val2 = tcontext->type;
145 val1 = scontext->role;
146 val2 = tcontext->role;
147 r1 = policydb.role_val_to_struct[val1 - 1];
148 r2 = policydb.role_val_to_struct[val2 - 1];
151 s[++sp] = ebitmap_get_bit(&r1->dominates,
155 s[++sp] = ebitmap_get_bit(&r2->dominates,
159 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
161 !ebitmap_get_bit(&r2->dominates,
169 l1 = &(scontext->range.level[0]);
170 l2 = &(tcontext->range.level[0]);
173 l1 = &(scontext->range.level[0]);
174 l2 = &(tcontext->range.level[1]);
177 l1 = &(scontext->range.level[1]);
178 l2 = &(tcontext->range.level[0]);
181 l1 = &(scontext->range.level[1]);
182 l2 = &(tcontext->range.level[1]);
185 l1 = &(scontext->range.level[0]);
186 l2 = &(scontext->range.level[1]);
189 l1 = &(tcontext->range.level[0]);
190 l2 = &(tcontext->range.level[1]);
195 s[++sp] = mls_level_eq(l1, l2);
198 s[++sp] = !mls_level_eq(l1, l2);
201 s[++sp] = mls_level_dom(l1, l2);
204 s[++sp] = mls_level_dom(l2, l1);
207 s[++sp] = mls_level_incomp(l2, l1);
221 s[++sp] = (val1 == val2);
224 s[++sp] = (val1 != val2);
232 if (sp == (CEXPR_MAXDEPTH-1))
235 if (e->attr & CEXPR_TARGET)
237 else if (e->attr & CEXPR_XTARGET) {
244 if (e->attr & CEXPR_USER)
246 else if (e->attr & CEXPR_ROLE)
248 else if (e->attr & CEXPR_TYPE)
257 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
260 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
278 * Compute access vectors based on a context structure pair for
279 * the permissions in a particular class.
281 static int context_struct_compute_av(struct context *scontext,
282 struct context *tcontext,
285 struct av_decision *avd)
287 struct constraint_node *constraint;
288 struct role_allow *ra;
289 struct avtab_key avkey;
290 struct avtab_node *node;
291 struct class_datum *tclass_datum;
292 struct ebitmap *sattr, *tattr;
293 struct ebitmap_node *snode, *tnode;
294 const struct selinux_class_perm *kdefs = &selinux_class_perm;
298 * Remap extended Netlink classes for old policy versions.
299 * Do this here rather than socket_type_to_security_class()
300 * in case a newer policy version is loaded, allowing sockets
301 * to remain in the correct class.
303 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
304 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
305 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
306 tclass = SECCLASS_NETLINK_SOCKET;
309 * Initialize the access vectors to the default values.
312 avd->decided = 0xffffffff;
314 avd->auditdeny = 0xffffffff;
315 avd->seqno = latest_granting;
318 * Check for all the invalid cases.
320 * - tclass > policy and > kernel
321 * - tclass > policy but is a userspace class
322 * - tclass > policy but we do not allow unknowns
324 if (unlikely(!tclass))
326 if (unlikely(tclass > policydb.p_classes.nprim))
327 if (tclass > kdefs->cts_len ||
328 !kdefs->class_to_string[tclass] ||
329 !policydb.allow_unknown)
333 * Kernel class and we allow unknown so pad the allow decision
334 * the pad will be all 1 for unknown classes.
336 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
337 avd->allowed = policydb.undefined_perms[tclass - 1];
340 * Not in policy. Since decision is completed (all 1 or all 0) return.
342 if (unlikely(tclass > policydb.p_classes.nprim))
345 tclass_datum = policydb.class_val_to_struct[tclass - 1];
348 * If a specific type enforcement rule was defined for
349 * this permission check, then use it.
351 avkey.target_class = tclass;
352 avkey.specified = AVTAB_AV;
353 sattr = &policydb.type_attr_map[scontext->type - 1];
354 tattr = &policydb.type_attr_map[tcontext->type - 1];
355 ebitmap_for_each_positive_bit(sattr, snode, i) {
356 ebitmap_for_each_positive_bit(tattr, tnode, j) {
357 avkey.source_type = i + 1;
358 avkey.target_type = j + 1;
359 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
361 node = avtab_search_node_next(node, avkey.specified)) {
362 if (node->key.specified == AVTAB_ALLOWED)
363 avd->allowed |= node->datum.data;
364 else if (node->key.specified == AVTAB_AUDITALLOW)
365 avd->auditallow |= node->datum.data;
366 else if (node->key.specified == AVTAB_AUDITDENY)
367 avd->auditdeny &= node->datum.data;
370 /* Check conditional av table for additional permissions */
371 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
377 * Remove any permissions prohibited by a constraint (this includes
380 constraint = tclass_datum->constraints;
382 if ((constraint->permissions & (avd->allowed)) &&
383 !constraint_expr_eval(scontext, tcontext, NULL,
385 avd->allowed = (avd->allowed) & ~(constraint->permissions);
387 constraint = constraint->next;
391 * If checking process transition permission and the
392 * role is changing, then check the (current_role, new_role)
395 if (tclass == SECCLASS_PROCESS &&
396 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
397 scontext->role != tcontext->role) {
398 for (ra = policydb.role_allow; ra; ra = ra->next) {
399 if (scontext->role == ra->role &&
400 tcontext->role == ra->new_role)
404 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
405 PROCESS__DYNTRANSITION);
411 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
417 * Given a sid find if the type has the permissive flag set
419 int security_permissive_sid(u32 sid)
421 struct context *context;
425 read_lock(&policy_rwlock);
427 context = sidtab_search(&sidtab, sid);
430 type = context->type;
432 * we are intentionally using type here, not type-1, the 0th bit may
433 * someday indicate that we are globally setting permissive in policy.
435 rc = ebitmap_get_bit(&policydb.permissive_map, type);
437 read_unlock(&policy_rwlock);
441 static int security_validtrans_handle_fail(struct context *ocontext,
442 struct context *ncontext,
443 struct context *tcontext,
446 char *o = NULL, *n = NULL, *t = NULL;
447 u32 olen, nlen, tlen;
449 if (context_struct_to_string(ocontext, &o, &olen) < 0)
451 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
453 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
455 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
456 "security_validate_transition: denied for"
457 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
458 o, n, t, policydb.p_class_val_to_name[tclass-1]);
464 if (!selinux_enforcing)
469 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
472 struct context *ocontext;
473 struct context *ncontext;
474 struct context *tcontext;
475 struct class_datum *tclass_datum;
476 struct constraint_node *constraint;
482 read_lock(&policy_rwlock);
485 * Remap extended Netlink classes for old policy versions.
486 * Do this here rather than socket_type_to_security_class()
487 * in case a newer policy version is loaded, allowing sockets
488 * to remain in the correct class.
490 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
491 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
492 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
493 tclass = SECCLASS_NETLINK_SOCKET;
495 if (!tclass || tclass > policydb.p_classes.nprim) {
496 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
501 tclass_datum = policydb.class_val_to_struct[tclass - 1];
503 ocontext = sidtab_search(&sidtab, oldsid);
505 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
511 ncontext = sidtab_search(&sidtab, newsid);
513 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
519 tcontext = sidtab_search(&sidtab, tasksid);
521 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
527 constraint = tclass_datum->validatetrans;
529 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
531 rc = security_validtrans_handle_fail(ocontext, ncontext,
535 constraint = constraint->next;
539 read_unlock(&policy_rwlock);
544 * security_compute_av - Compute access vector decisions.
545 * @ssid: source security identifier
546 * @tsid: target security identifier
547 * @tclass: target security class
548 * @requested: requested permissions
549 * @avd: access vector decisions
551 * Compute a set of access vector decisions based on the
552 * SID pair (@ssid, @tsid) for the permissions in @tclass.
553 * Return -%EINVAL if any of the parameters are invalid or %0
554 * if the access vector decisions were computed successfully.
556 int security_compute_av(u32 ssid,
560 struct av_decision *avd)
562 struct context *scontext = NULL, *tcontext = NULL;
565 if (!ss_initialized) {
566 avd->allowed = 0xffffffff;
567 avd->decided = 0xffffffff;
569 avd->auditdeny = 0xffffffff;
570 avd->seqno = latest_granting;
574 read_lock(&policy_rwlock);
576 scontext = sidtab_search(&sidtab, ssid);
578 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
583 tcontext = sidtab_search(&sidtab, tsid);
585 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
591 rc = context_struct_compute_av(scontext, tcontext, tclass,
594 read_unlock(&policy_rwlock);
599 * Write the security context string representation of
600 * the context structure `context' into a dynamically
601 * allocated string of the correct size. Set `*scontext'
602 * to point to this string and set `*scontext_len' to
603 * the length of the string.
605 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
613 *scontext_len = context->len;
614 *scontext = kstrdup(context->str, GFP_ATOMIC);
620 /* Compute the size of the context. */
621 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
622 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
623 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
624 *scontext_len += mls_compute_context_len(context);
626 /* Allocate space for the context; caller must free this space. */
627 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
630 *scontext = scontextp;
633 * Copy the user name, role name and type name into the context.
635 sprintf(scontextp, "%s:%s:%s",
636 policydb.p_user_val_to_name[context->user - 1],
637 policydb.p_role_val_to_name[context->role - 1],
638 policydb.p_type_val_to_name[context->type - 1]);
639 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
640 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
641 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
643 mls_sid_to_context(context, &scontextp);
650 #include "initial_sid_to_string.h"
652 const char *security_get_initial_sid_context(u32 sid)
654 if (unlikely(sid > SECINITSID_NUM))
656 return initial_sid_to_string[sid];
659 static int security_sid_to_context_core(u32 sid, char **scontext,
660 u32 *scontext_len, int force)
662 struct context *context;
668 if (!ss_initialized) {
669 if (sid <= SECINITSID_NUM) {
672 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
673 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
678 strcpy(scontextp, initial_sid_to_string[sid]);
679 *scontext = scontextp;
682 printk(KERN_ERR "SELinux: %s: called before initial "
683 "load_policy on unknown SID %d\n", __func__, sid);
687 read_lock(&policy_rwlock);
689 context = sidtab_search_force(&sidtab, sid);
691 context = sidtab_search(&sidtab, sid);
693 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
698 rc = context_struct_to_string(context, scontext, scontext_len);
700 read_unlock(&policy_rwlock);
707 * security_sid_to_context - Obtain a context for a given SID.
708 * @sid: security identifier, SID
709 * @scontext: security context
710 * @scontext_len: length in bytes
712 * Write the string representation of the context associated with @sid
713 * into a dynamically allocated string of the correct size. Set @scontext
714 * to point to this string and set @scontext_len to the length of the string.
716 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
718 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
721 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
723 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
727 * Caveat: Mutates scontext.
729 static int string_to_context_struct(struct policydb *pol,
730 struct sidtab *sidtabp,
736 struct role_datum *role;
737 struct type_datum *typdatum;
738 struct user_datum *usrdatum;
739 char *scontextp, *p, oldc;
744 /* Parse the security context. */
747 scontextp = (char *) scontext;
749 /* Extract the user. */
751 while (*p && *p != ':')
759 usrdatum = hashtab_search(pol->p_users.table, scontextp);
763 ctx->user = usrdatum->value;
767 while (*p && *p != ':')
775 role = hashtab_search(pol->p_roles.table, scontextp);
778 ctx->role = role->value;
782 while (*p && *p != ':')
787 typdatum = hashtab_search(pol->p_types.table, scontextp);
791 ctx->type = typdatum->value;
793 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
797 if ((p - scontext) < scontext_len) {
802 /* Check the validity of the new context. */
803 if (!policydb_context_isvalid(pol, ctx)) {
805 context_destroy(ctx);
813 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
814 u32 *sid, u32 def_sid, gfp_t gfp_flags,
817 char *scontext2, *str = NULL;
818 struct context context;
821 if (!ss_initialized) {
824 for (i = 1; i < SECINITSID_NUM; i++) {
825 if (!strcmp(initial_sid_to_string[i], scontext)) {
830 *sid = SECINITSID_KERNEL;
835 /* Copy the string so that we can modify the copy as we parse it. */
836 scontext2 = kmalloc(scontext_len+1, gfp_flags);
839 memcpy(scontext2, scontext, scontext_len);
840 scontext2[scontext_len] = 0;
843 /* Save another copy for storing in uninterpreted form */
844 str = kstrdup(scontext2, gfp_flags);
851 read_lock(&policy_rwlock);
852 rc = string_to_context_struct(&policydb, &sidtab,
853 scontext2, scontext_len,
855 if (rc == -EINVAL && force) {
857 context.len = scontext_len;
861 rc = sidtab_context_to_sid(&sidtab, &context, sid);
863 context_destroy(&context);
865 read_unlock(&policy_rwlock);
872 * security_context_to_sid - Obtain a SID for a given security context.
873 * @scontext: security context
874 * @scontext_len: length in bytes
875 * @sid: security identifier, SID
877 * Obtains a SID associated with the security context that
878 * has the string representation specified by @scontext.
879 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
880 * memory is available, or 0 on success.
882 int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
884 return security_context_to_sid_core(scontext, scontext_len,
885 sid, SECSID_NULL, GFP_KERNEL, 0);
889 * security_context_to_sid_default - Obtain a SID for a given security context,
890 * falling back to specified default if needed.
892 * @scontext: security context
893 * @scontext_len: length in bytes
894 * @sid: security identifier, SID
895 * @def_sid: default SID to assign on error
897 * Obtains a SID associated with the security context that
898 * has the string representation specified by @scontext.
899 * The default SID is passed to the MLS layer to be used to allow
900 * kernel labeling of the MLS field if the MLS field is not present
901 * (for upgrading to MLS without full relabel).
902 * Implicitly forces adding of the context even if it cannot be mapped yet.
903 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
904 * memory is available, or 0 on success.
906 int security_context_to_sid_default(const char *scontext, u32 scontext_len,
907 u32 *sid, u32 def_sid, gfp_t gfp_flags)
909 return security_context_to_sid_core(scontext, scontext_len,
910 sid, def_sid, gfp_flags, 1);
913 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
916 return security_context_to_sid_core(scontext, scontext_len,
917 sid, SECSID_NULL, GFP_KERNEL, 1);
920 static int compute_sid_handle_invalid_context(
921 struct context *scontext,
922 struct context *tcontext,
924 struct context *newcontext)
926 char *s = NULL, *t = NULL, *n = NULL;
927 u32 slen, tlen, nlen;
929 if (context_struct_to_string(scontext, &s, &slen) < 0)
931 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
933 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
935 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
936 "security_compute_sid: invalid context %s"
940 n, s, t, policydb.p_class_val_to_name[tclass-1]);
945 if (!selinux_enforcing)
950 static int security_compute_sid(u32 ssid,
956 struct context *scontext = NULL, *tcontext = NULL, newcontext;
957 struct role_trans *roletr = NULL;
958 struct avtab_key avkey;
959 struct avtab_datum *avdatum;
960 struct avtab_node *node;
963 if (!ss_initialized) {
965 case SECCLASS_PROCESS:
975 context_init(&newcontext);
977 read_lock(&policy_rwlock);
979 scontext = sidtab_search(&sidtab, ssid);
981 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
986 tcontext = sidtab_search(&sidtab, tsid);
988 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
994 /* Set the user identity. */
996 case AVTAB_TRANSITION:
998 /* Use the process user identity. */
999 newcontext.user = scontext->user;
1002 /* Use the related object owner. */
1003 newcontext.user = tcontext->user;
1007 /* Set the role and type to default values. */
1009 case SECCLASS_PROCESS:
1010 /* Use the current role and type of process. */
1011 newcontext.role = scontext->role;
1012 newcontext.type = scontext->type;
1015 /* Use the well-defined object role. */
1016 newcontext.role = OBJECT_R_VAL;
1017 /* Use the type of the related object. */
1018 newcontext.type = tcontext->type;
1021 /* Look for a type transition/member/change rule. */
1022 avkey.source_type = scontext->type;
1023 avkey.target_type = tcontext->type;
1024 avkey.target_class = tclass;
1025 avkey.specified = specified;
1026 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1028 /* If no permanent rule, also check for enabled conditional rules */
1030 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1031 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
1032 if (node->key.specified & AVTAB_ENABLED) {
1033 avdatum = &node->datum;
1040 /* Use the type from the type transition/member/change rule. */
1041 newcontext.type = avdatum->data;
1044 /* Check for class-specific changes. */
1046 case SECCLASS_PROCESS:
1047 if (specified & AVTAB_TRANSITION) {
1048 /* Look for a role transition rule. */
1049 for (roletr = policydb.role_tr; roletr;
1050 roletr = roletr->next) {
1051 if (roletr->role == scontext->role &&
1052 roletr->type == tcontext->type) {
1053 /* Use the role transition rule. */
1054 newcontext.role = roletr->new_role;
1064 /* Set the MLS attributes.
1065 This is done last because it may allocate memory. */
1066 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1070 /* Check the validity of the context. */
1071 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1072 rc = compute_sid_handle_invalid_context(scontext,
1079 /* Obtain the sid for the context. */
1080 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1082 read_unlock(&policy_rwlock);
1083 context_destroy(&newcontext);
1089 * security_transition_sid - Compute the SID for a new subject/object.
1090 * @ssid: source security identifier
1091 * @tsid: target security identifier
1092 * @tclass: target security class
1093 * @out_sid: security identifier for new subject/object
1095 * Compute a SID to use for labeling a new subject or object in the
1096 * class @tclass based on a SID pair (@ssid, @tsid).
1097 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1098 * if insufficient memory is available, or %0 if the new SID was
1099 * computed successfully.
1101 int security_transition_sid(u32 ssid,
1106 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1110 * security_member_sid - Compute the SID for member selection.
1111 * @ssid: source security identifier
1112 * @tsid: target security identifier
1113 * @tclass: target security class
1114 * @out_sid: security identifier for selected member
1116 * Compute a SID to use when selecting a member of a polyinstantiated
1117 * object of class @tclass based on a SID pair (@ssid, @tsid).
1118 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1119 * if insufficient memory is available, or %0 if the SID was
1120 * computed successfully.
1122 int security_member_sid(u32 ssid,
1127 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1131 * security_change_sid - Compute the SID for object relabeling.
1132 * @ssid: source security identifier
1133 * @tsid: target security identifier
1134 * @tclass: target security class
1135 * @out_sid: security identifier for selected member
1137 * Compute a SID to use for relabeling an object of class @tclass
1138 * based on a SID pair (@ssid, @tsid).
1139 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1140 * if insufficient memory is available, or %0 if the SID was
1141 * computed successfully.
1143 int security_change_sid(u32 ssid,
1148 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1152 * Verify that each kernel class that is defined in the
1155 static int validate_classes(struct policydb *p)
1158 struct class_datum *cladatum;
1159 struct perm_datum *perdatum;
1160 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1162 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1163 const char *def_class, *def_perm, *pol_class;
1164 struct symtab *perms;
1166 if (p->allow_unknown) {
1167 u32 num_classes = kdefs->cts_len;
1168 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1169 if (!p->undefined_perms)
1173 for (i = 1; i < kdefs->cts_len; i++) {
1174 def_class = kdefs->class_to_string[i];
1177 if (i > p->p_classes.nprim) {
1179 "SELinux: class %s not defined in policy\n",
1181 if (p->reject_unknown)
1183 if (p->allow_unknown)
1184 p->undefined_perms[i-1] = ~0U;
1187 pol_class = p->p_class_val_to_name[i-1];
1188 if (strcmp(pol_class, def_class)) {
1190 "SELinux: class %d is incorrect, found %s but should be %s\n",
1191 i, pol_class, def_class);
1195 for (i = 0; i < kdefs->av_pts_len; i++) {
1196 class_val = kdefs->av_perm_to_string[i].tclass;
1197 perm_val = kdefs->av_perm_to_string[i].value;
1198 def_perm = kdefs->av_perm_to_string[i].name;
1199 if (class_val > p->p_classes.nprim)
1201 pol_class = p->p_class_val_to_name[class_val-1];
1202 cladatum = hashtab_search(p->p_classes.table, pol_class);
1204 perms = &cladatum->permissions;
1205 nprim = 1 << (perms->nprim - 1);
1206 if (perm_val > nprim) {
1208 "SELinux: permission %s in class %s not defined in policy\n",
1209 def_perm, pol_class);
1210 if (p->reject_unknown)
1212 if (p->allow_unknown)
1213 p->undefined_perms[class_val-1] |= perm_val;
1216 perdatum = hashtab_search(perms->table, def_perm);
1217 if (perdatum == NULL) {
1219 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1220 def_perm, pol_class);
1223 pol_val = 1 << (perdatum->value - 1);
1224 if (pol_val != perm_val) {
1226 "SELinux: permission %s in class %s has incorrect value\n",
1227 def_perm, pol_class);
1231 for (i = 0; i < kdefs->av_inherit_len; i++) {
1232 class_val = kdefs->av_inherit[i].tclass;
1233 if (class_val > p->p_classes.nprim)
1235 pol_class = p->p_class_val_to_name[class_val-1];
1236 cladatum = hashtab_search(p->p_classes.table, pol_class);
1238 if (!cladatum->comdatum) {
1240 "SELinux: class %s should have an inherits clause but does not\n",
1244 tmp = kdefs->av_inherit[i].common_base;
1246 while (!(tmp & 0x01)) {
1250 perms = &cladatum->comdatum->permissions;
1251 for (j = 0; j < common_pts_len; j++) {
1252 def_perm = kdefs->av_inherit[i].common_pts[j];
1253 if (j >= perms->nprim) {
1255 "SELinux: permission %s in class %s not defined in policy\n",
1256 def_perm, pol_class);
1257 if (p->reject_unknown)
1259 if (p->allow_unknown)
1260 p->undefined_perms[class_val-1] |= (1 << j);
1263 perdatum = hashtab_search(perms->table, def_perm);
1264 if (perdatum == NULL) {
1266 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1267 def_perm, pol_class);
1270 if (perdatum->value != j + 1) {
1272 "SELinux: permission %s in class %s has incorrect value\n",
1273 def_perm, pol_class);
1281 /* Clone the SID into the new SID table. */
1282 static int clone_sid(u32 sid,
1283 struct context *context,
1286 struct sidtab *s = arg;
1288 return sidtab_insert(s, sid, context);
1291 static inline int convert_context_handle_invalid_context(struct context *context)
1295 if (selinux_enforcing) {
1301 if (!context_struct_to_string(context, &s, &len)) {
1303 "SELinux: Context %s would be invalid if enforcing\n",
1311 struct convert_context_args {
1312 struct policydb *oldp;
1313 struct policydb *newp;
1317 * Convert the values in the security context
1318 * structure `c' from the values specified
1319 * in the policy `p->oldp' to the values specified
1320 * in the policy `p->newp'. Verify that the
1321 * context is valid under the new policy.
1323 static int convert_context(u32 key,
1327 struct convert_context_args *args;
1328 struct context oldc;
1329 struct role_datum *role;
1330 struct type_datum *typdatum;
1331 struct user_datum *usrdatum;
1340 s = kstrdup(c->str, GFP_KERNEL);
1345 rc = string_to_context_struct(args->newp, NULL, s,
1346 c->len, &ctx, SECSID_NULL);
1350 "SELinux: Context %s became valid (mapped).\n",
1352 /* Replace string with mapped representation. */
1354 memcpy(c, &ctx, sizeof(*c));
1356 } else if (rc == -EINVAL) {
1357 /* Retain string representation for later mapping. */
1361 /* Other error condition, e.g. ENOMEM. */
1363 "SELinux: Unable to map context %s, rc = %d.\n",
1369 rc = context_cpy(&oldc, c);
1375 /* Convert the user. */
1376 usrdatum = hashtab_search(args->newp->p_users.table,
1377 args->oldp->p_user_val_to_name[c->user - 1]);
1380 c->user = usrdatum->value;
1382 /* Convert the role. */
1383 role = hashtab_search(args->newp->p_roles.table,
1384 args->oldp->p_role_val_to_name[c->role - 1]);
1387 c->role = role->value;
1389 /* Convert the type. */
1390 typdatum = hashtab_search(args->newp->p_types.table,
1391 args->oldp->p_type_val_to_name[c->type - 1]);
1394 c->type = typdatum->value;
1396 rc = mls_convert_context(args->oldp, args->newp, c);
1400 /* Check the validity of the new context. */
1401 if (!policydb_context_isvalid(args->newp, c)) {
1402 rc = convert_context_handle_invalid_context(&oldc);
1407 context_destroy(&oldc);
1412 /* Map old representation to string and save it. */
1413 if (context_struct_to_string(&oldc, &s, &len))
1415 context_destroy(&oldc);
1420 "SELinux: Context %s became invalid (unmapped).\n",
1426 static void security_load_policycaps(void)
1428 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1429 POLICYDB_CAPABILITY_NETPEER);
1430 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1431 POLICYDB_CAPABILITY_OPENPERM);
1434 extern void selinux_complete_init(void);
1435 static int security_preserve_bools(struct policydb *p);
1438 * security_load_policy - Load a security policy configuration.
1439 * @data: binary policy data
1440 * @len: length of data in bytes
1442 * Load a new set of security policy configuration data,
1443 * validate it and convert the SID table as necessary.
1444 * This function will flush the access vector cache after
1445 * loading the new policy.
1447 int security_load_policy(void *data, size_t len)
1449 struct policydb oldpolicydb, newpolicydb;
1450 struct sidtab oldsidtab, newsidtab;
1451 struct convert_context_args args;
1454 struct policy_file file = { data, len }, *fp = &file;
1456 mutex_lock(&load_mutex);
1458 if (!ss_initialized) {
1460 if (policydb_read(&policydb, fp)) {
1461 mutex_unlock(&load_mutex);
1462 avtab_cache_destroy();
1465 if (policydb_load_isids(&policydb, &sidtab)) {
1466 mutex_unlock(&load_mutex);
1467 policydb_destroy(&policydb);
1468 avtab_cache_destroy();
1471 /* Verify that the kernel defined classes are correct. */
1472 if (validate_classes(&policydb)) {
1474 "SELinux: the definition of a class is incorrect\n");
1475 mutex_unlock(&load_mutex);
1476 sidtab_destroy(&sidtab);
1477 policydb_destroy(&policydb);
1478 avtab_cache_destroy();
1481 security_load_policycaps();
1482 policydb_loaded_version = policydb.policyvers;
1484 seqno = ++latest_granting;
1485 mutex_unlock(&load_mutex);
1486 selinux_complete_init();
1487 avc_ss_reset(seqno);
1488 selnl_notify_policyload(seqno);
1489 selinux_netlbl_cache_invalidate();
1490 selinux_xfrm_notify_policyload();
1495 sidtab_hash_eval(&sidtab, "sids");
1498 if (policydb_read(&newpolicydb, fp)) {
1499 mutex_unlock(&load_mutex);
1503 if (sidtab_init(&newsidtab)) {
1504 mutex_unlock(&load_mutex);
1505 policydb_destroy(&newpolicydb);
1509 /* Verify that the kernel defined classes are correct. */
1510 if (validate_classes(&newpolicydb)) {
1512 "SELinux: the definition of a class is incorrect\n");
1517 rc = security_preserve_bools(&newpolicydb);
1519 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
1523 /* Clone the SID table. */
1524 sidtab_shutdown(&sidtab);
1525 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1531 * Convert the internal representations of contexts
1532 * in the new SID table.
1534 args.oldp = &policydb;
1535 args.newp = &newpolicydb;
1536 rc = sidtab_map(&newsidtab, convert_context, &args);
1540 /* Save the old policydb and SID table to free later. */
1541 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1542 sidtab_set(&oldsidtab, &sidtab);
1544 /* Install the new policydb and SID table. */
1545 write_lock_irq(&policy_rwlock);
1546 memcpy(&policydb, &newpolicydb, sizeof policydb);
1547 sidtab_set(&sidtab, &newsidtab);
1548 security_load_policycaps();
1549 seqno = ++latest_granting;
1550 policydb_loaded_version = policydb.policyvers;
1551 write_unlock_irq(&policy_rwlock);
1552 mutex_unlock(&load_mutex);
1554 /* Free the old policydb and SID table. */
1555 policydb_destroy(&oldpolicydb);
1556 sidtab_destroy(&oldsidtab);
1558 avc_ss_reset(seqno);
1559 selnl_notify_policyload(seqno);
1560 selinux_netlbl_cache_invalidate();
1561 selinux_xfrm_notify_policyload();
1566 mutex_unlock(&load_mutex);
1567 sidtab_destroy(&newsidtab);
1568 policydb_destroy(&newpolicydb);
1574 * security_port_sid - Obtain the SID for a port.
1575 * @protocol: protocol number
1576 * @port: port number
1577 * @out_sid: security identifier
1579 int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
1584 read_lock(&policy_rwlock);
1586 c = policydb.ocontexts[OCON_PORT];
1588 if (c->u.port.protocol == protocol &&
1589 c->u.port.low_port <= port &&
1590 c->u.port.high_port >= port)
1597 rc = sidtab_context_to_sid(&sidtab,
1603 *out_sid = c->sid[0];
1605 *out_sid = SECINITSID_PORT;
1609 read_unlock(&policy_rwlock);
1614 * security_netif_sid - Obtain the SID for a network interface.
1615 * @name: interface name
1616 * @if_sid: interface SID
1618 int security_netif_sid(char *name, u32 *if_sid)
1623 read_lock(&policy_rwlock);
1625 c = policydb.ocontexts[OCON_NETIF];
1627 if (strcmp(name, c->u.name) == 0)
1633 if (!c->sid[0] || !c->sid[1]) {
1634 rc = sidtab_context_to_sid(&sidtab,
1639 rc = sidtab_context_to_sid(&sidtab,
1645 *if_sid = c->sid[0];
1647 *if_sid = SECINITSID_NETIF;
1650 read_unlock(&policy_rwlock);
1654 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1658 for (i = 0; i < 4; i++)
1659 if (addr[i] != (input[i] & mask[i])) {
1668 * security_node_sid - Obtain the SID for a node (host).
1669 * @domain: communication domain aka address family
1671 * @addrlen: address length in bytes
1672 * @out_sid: security identifier
1674 int security_node_sid(u16 domain,
1682 read_lock(&policy_rwlock);
1688 if (addrlen != sizeof(u32)) {
1693 addr = *((u32 *)addrp);
1695 c = policydb.ocontexts[OCON_NODE];
1697 if (c->u.node.addr == (addr & c->u.node.mask))
1705 if (addrlen != sizeof(u64) * 2) {
1709 c = policydb.ocontexts[OCON_NODE6];
1711 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1719 *out_sid = SECINITSID_NODE;
1725 rc = sidtab_context_to_sid(&sidtab,
1731 *out_sid = c->sid[0];
1733 *out_sid = SECINITSID_NODE;
1737 read_unlock(&policy_rwlock);
1744 * security_get_user_sids - Obtain reachable SIDs for a user.
1745 * @fromsid: starting SID
1746 * @username: username
1747 * @sids: array of reachable SIDs for user
1748 * @nel: number of elements in @sids
1750 * Generate the set of SIDs for legal security contexts
1751 * for a given user that can be reached by @fromsid.
1752 * Set *@sids to point to a dynamically allocated
1753 * array containing the set of SIDs. Set *@nel to the
1754 * number of elements in the array.
1757 int security_get_user_sids(u32 fromsid,
1762 struct context *fromcon, usercon;
1763 u32 *mysids = NULL, *mysids2, sid;
1764 u32 mynel = 0, maxnel = SIDS_NEL;
1765 struct user_datum *user;
1766 struct role_datum *role;
1767 struct ebitmap_node *rnode, *tnode;
1773 if (!ss_initialized)
1776 read_lock(&policy_rwlock);
1778 context_init(&usercon);
1780 fromcon = sidtab_search(&sidtab, fromsid);
1786 user = hashtab_search(policydb.p_users.table, username);
1791 usercon.user = user->value;
1793 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1799 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
1800 role = policydb.role_val_to_struct[i];
1802 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
1805 if (mls_setup_user_range(fromcon, user, &usercon))
1808 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1811 if (mynel < maxnel) {
1812 mysids[mynel++] = sid;
1815 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1820 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1823 mysids[mynel++] = sid;
1829 read_unlock(&policy_rwlock);
1835 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1841 for (i = 0, j = 0; i < mynel; i++) {
1842 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1844 PROCESS__TRANSITION, AVC_STRICT,
1847 mysids2[j++] = mysids[i];
1859 * security_genfs_sid - Obtain a SID for a file in a filesystem
1860 * @fstype: filesystem type
1861 * @path: path from root of mount
1862 * @sclass: file security class
1863 * @sid: SID for path
1865 * Obtain a SID to use for a file in a filesystem that
1866 * cannot support xattr or use a fixed labeling behavior like
1867 * transition SIDs or task SIDs.
1869 int security_genfs_sid(const char *fstype,
1875 struct genfs *genfs;
1877 int rc = 0, cmp = 0;
1879 while (path[0] == '/' && path[1] == '/')
1882 read_lock(&policy_rwlock);
1884 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1885 cmp = strcmp(fstype, genfs->fstype);
1890 if (!genfs || cmp) {
1891 *sid = SECINITSID_UNLABELED;
1896 for (c = genfs->head; c; c = c->next) {
1897 len = strlen(c->u.name);
1898 if ((!c->v.sclass || sclass == c->v.sclass) &&
1899 (strncmp(c->u.name, path, len) == 0))
1904 *sid = SECINITSID_UNLABELED;
1910 rc = sidtab_context_to_sid(&sidtab,
1919 read_unlock(&policy_rwlock);
1924 * security_fs_use - Determine how to handle labeling for a filesystem.
1925 * @fstype: filesystem type
1926 * @behavior: labeling behavior
1927 * @sid: SID for filesystem (superblock)
1929 int security_fs_use(
1931 unsigned int *behavior,
1937 read_lock(&policy_rwlock);
1939 c = policydb.ocontexts[OCON_FSUSE];
1941 if (strcmp(fstype, c->u.name) == 0)
1947 *behavior = c->v.behavior;
1949 rc = sidtab_context_to_sid(&sidtab,
1957 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1959 *behavior = SECURITY_FS_USE_NONE;
1962 *behavior = SECURITY_FS_USE_GENFS;
1967 read_unlock(&policy_rwlock);
1971 int security_get_bools(int *len, char ***names, int **values)
1973 int i, rc = -ENOMEM;
1975 read_lock(&policy_rwlock);
1979 *len = policydb.p_bools.nprim;
1985 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
1989 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1993 for (i = 0; i < *len; i++) {
1995 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1996 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1997 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
2000 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2001 (*names)[i][name_len - 1] = 0;
2005 read_unlock(&policy_rwlock);
2009 for (i = 0; i < *len; i++)
2017 int security_set_bools(int len, int *values)
2020 int lenp, seqno = 0;
2021 struct cond_node *cur;
2023 write_lock_irq(&policy_rwlock);
2025 lenp = policydb.p_bools.nprim;
2031 for (i = 0; i < len; i++) {
2032 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2033 audit_log(current->audit_context, GFP_ATOMIC,
2034 AUDIT_MAC_CONFIG_CHANGE,
2035 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2036 policydb.p_bool_val_to_name[i],
2038 policydb.bool_val_to_struct[i]->state,
2039 audit_get_loginuid(current),
2040 audit_get_sessionid(current));
2043 policydb.bool_val_to_struct[i]->state = 1;
2045 policydb.bool_val_to_struct[i]->state = 0;
2048 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2049 rc = evaluate_cond_node(&policydb, cur);
2054 seqno = ++latest_granting;
2057 write_unlock_irq(&policy_rwlock);
2059 avc_ss_reset(seqno);
2060 selnl_notify_policyload(seqno);
2061 selinux_xfrm_notify_policyload();
2066 int security_get_bool_value(int bool)
2071 read_lock(&policy_rwlock);
2073 len = policydb.p_bools.nprim;
2079 rc = policydb.bool_val_to_struct[bool]->state;
2081 read_unlock(&policy_rwlock);
2085 static int security_preserve_bools(struct policydb *p)
2087 int rc, nbools = 0, *bvalues = NULL, i;
2088 char **bnames = NULL;
2089 struct cond_bool_datum *booldatum;
2090 struct cond_node *cur;
2092 rc = security_get_bools(&nbools, &bnames, &bvalues);
2095 for (i = 0; i < nbools; i++) {
2096 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2098 booldatum->state = bvalues[i];
2100 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2101 rc = evaluate_cond_node(p, cur);
2108 for (i = 0; i < nbools; i++)
2117 * security_sid_mls_copy() - computes a new sid based on the given
2118 * sid and the mls portion of mls_sid.
2120 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2122 struct context *context1;
2123 struct context *context2;
2124 struct context newcon;
2129 if (!ss_initialized || !selinux_mls_enabled) {
2134 context_init(&newcon);
2136 read_lock(&policy_rwlock);
2137 context1 = sidtab_search(&sidtab, sid);
2139 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2145 context2 = sidtab_search(&sidtab, mls_sid);
2147 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2153 newcon.user = context1->user;
2154 newcon.role = context1->role;
2155 newcon.type = context1->type;
2156 rc = mls_context_cpy(&newcon, context2);
2160 /* Check the validity of the new context. */
2161 if (!policydb_context_isvalid(&policydb, &newcon)) {
2162 rc = convert_context_handle_invalid_context(&newcon);
2167 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2171 if (!context_struct_to_string(&newcon, &s, &len)) {
2172 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2173 "security_sid_mls_copy: invalid context %s", s);
2178 read_unlock(&policy_rwlock);
2179 context_destroy(&newcon);
2185 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2186 * @nlbl_sid: NetLabel SID
2187 * @nlbl_type: NetLabel labeling protocol type
2188 * @xfrm_sid: XFRM SID
2191 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2192 * resolved into a single SID it is returned via @peer_sid and the function
2193 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2194 * returns a negative value. A table summarizing the behavior is below:
2196 * | function return | @sid
2197 * ------------------------------+-----------------+-----------------
2198 * no peer labels | 0 | SECSID_NULL
2199 * single peer label | 0 | <peer_label>
2200 * multiple, consistent labels | 0 | <peer_label>
2201 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2204 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2209 struct context *nlbl_ctx;
2210 struct context *xfrm_ctx;
2212 /* handle the common (which also happens to be the set of easy) cases
2213 * right away, these two if statements catch everything involving a
2214 * single or absent peer SID/label */
2215 if (xfrm_sid == SECSID_NULL) {
2216 *peer_sid = nlbl_sid;
2219 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2220 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2222 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2223 *peer_sid = xfrm_sid;
2227 /* we don't need to check ss_initialized here since the only way both
2228 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2229 * security server was initialized and ss_initialized was true */
2230 if (!selinux_mls_enabled) {
2231 *peer_sid = SECSID_NULL;
2235 read_lock(&policy_rwlock);
2237 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2239 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2240 __func__, nlbl_sid);
2244 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2246 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2247 __func__, xfrm_sid);
2251 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2254 read_unlock(&policy_rwlock);
2256 /* at present NetLabel SIDs/labels really only carry MLS
2257 * information so if the MLS portion of the NetLabel SID
2258 * matches the MLS portion of the labeled XFRM SID/label
2259 * then pass along the XFRM SID as it is the most
2261 *peer_sid = xfrm_sid;
2263 *peer_sid = SECSID_NULL;
2267 static int get_classes_callback(void *k, void *d, void *args)
2269 struct class_datum *datum = d;
2270 char *name = k, **classes = args;
2271 int value = datum->value - 1;
2273 classes[value] = kstrdup(name, GFP_ATOMIC);
2274 if (!classes[value])
2280 int security_get_classes(char ***classes, int *nclasses)
2284 read_lock(&policy_rwlock);
2286 *nclasses = policydb.p_classes.nprim;
2287 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2291 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2295 for (i = 0; i < *nclasses; i++)
2296 kfree((*classes)[i]);
2301 read_unlock(&policy_rwlock);
2305 static int get_permissions_callback(void *k, void *d, void *args)
2307 struct perm_datum *datum = d;
2308 char *name = k, **perms = args;
2309 int value = datum->value - 1;
2311 perms[value] = kstrdup(name, GFP_ATOMIC);
2318 int security_get_permissions(char *class, char ***perms, int *nperms)
2320 int rc = -ENOMEM, i;
2321 struct class_datum *match;
2323 read_lock(&policy_rwlock);
2325 match = hashtab_search(policydb.p_classes.table, class);
2327 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
2333 *nperms = match->permissions.nprim;
2334 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2338 if (match->comdatum) {
2339 rc = hashtab_map(match->comdatum->permissions.table,
2340 get_permissions_callback, *perms);
2345 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2351 read_unlock(&policy_rwlock);
2355 read_unlock(&policy_rwlock);
2356 for (i = 0; i < *nperms; i++)
2362 int security_get_reject_unknown(void)
2364 return policydb.reject_unknown;
2367 int security_get_allow_unknown(void)
2369 return policydb.allow_unknown;
2373 * security_policycap_supported - Check for a specific policy capability
2374 * @req_cap: capability
2377 * This function queries the currently loaded policy to see if it supports the
2378 * capability specified by @req_cap. Returns true (1) if the capability is
2379 * supported, false (0) if it isn't supported.
2382 int security_policycap_supported(unsigned int req_cap)
2386 read_lock(&policy_rwlock);
2387 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2388 read_unlock(&policy_rwlock);
2393 struct selinux_audit_rule {
2395 struct context au_ctxt;
2398 void selinux_audit_rule_free(void *vrule)
2400 struct selinux_audit_rule *rule = vrule;
2403 context_destroy(&rule->au_ctxt);
2408 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2410 struct selinux_audit_rule *tmprule;
2411 struct role_datum *roledatum;
2412 struct type_datum *typedatum;
2413 struct user_datum *userdatum;
2414 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
2419 if (!ss_initialized)
2423 case AUDIT_SUBJ_USER:
2424 case AUDIT_SUBJ_ROLE:
2425 case AUDIT_SUBJ_TYPE:
2426 case AUDIT_OBJ_USER:
2427 case AUDIT_OBJ_ROLE:
2428 case AUDIT_OBJ_TYPE:
2429 /* only 'equals' and 'not equals' fit user, role, and type */
2430 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2433 case AUDIT_SUBJ_SEN:
2434 case AUDIT_SUBJ_CLR:
2435 case AUDIT_OBJ_LEV_LOW:
2436 case AUDIT_OBJ_LEV_HIGH:
2437 /* we do not allow a range, indicated by the presense of '-' */
2438 if (strchr(rulestr, '-'))
2442 /* only the above fields are valid */
2446 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2450 context_init(&tmprule->au_ctxt);
2452 read_lock(&policy_rwlock);
2454 tmprule->au_seqno = latest_granting;
2457 case AUDIT_SUBJ_USER:
2458 case AUDIT_OBJ_USER:
2459 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2463 tmprule->au_ctxt.user = userdatum->value;
2465 case AUDIT_SUBJ_ROLE:
2466 case AUDIT_OBJ_ROLE:
2467 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2471 tmprule->au_ctxt.role = roledatum->value;
2473 case AUDIT_SUBJ_TYPE:
2474 case AUDIT_OBJ_TYPE:
2475 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2479 tmprule->au_ctxt.type = typedatum->value;
2481 case AUDIT_SUBJ_SEN:
2482 case AUDIT_SUBJ_CLR:
2483 case AUDIT_OBJ_LEV_LOW:
2484 case AUDIT_OBJ_LEV_HIGH:
2485 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2489 read_unlock(&policy_rwlock);
2492 selinux_audit_rule_free(tmprule);
2501 /* Check to see if the rule contains any selinux fields */
2502 int selinux_audit_rule_known(struct audit_krule *rule)
2506 for (i = 0; i < rule->field_count; i++) {
2507 struct audit_field *f = &rule->fields[i];
2509 case AUDIT_SUBJ_USER:
2510 case AUDIT_SUBJ_ROLE:
2511 case AUDIT_SUBJ_TYPE:
2512 case AUDIT_SUBJ_SEN:
2513 case AUDIT_SUBJ_CLR:
2514 case AUDIT_OBJ_USER:
2515 case AUDIT_OBJ_ROLE:
2516 case AUDIT_OBJ_TYPE:
2517 case AUDIT_OBJ_LEV_LOW:
2518 case AUDIT_OBJ_LEV_HIGH:
2526 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
2527 struct audit_context *actx)
2529 struct context *ctxt;
2530 struct mls_level *level;
2531 struct selinux_audit_rule *rule = vrule;
2535 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2536 "selinux_audit_rule_match: missing rule\n");
2540 read_lock(&policy_rwlock);
2542 if (rule->au_seqno < latest_granting) {
2543 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2544 "selinux_audit_rule_match: stale rule\n");
2549 ctxt = sidtab_search(&sidtab, sid);
2551 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2552 "selinux_audit_rule_match: unrecognized SID %d\n",
2558 /* a field/op pair that is not caught here will simply fall through
2561 case AUDIT_SUBJ_USER:
2562 case AUDIT_OBJ_USER:
2565 match = (ctxt->user == rule->au_ctxt.user);
2567 case AUDIT_NOT_EQUAL:
2568 match = (ctxt->user != rule->au_ctxt.user);
2572 case AUDIT_SUBJ_ROLE:
2573 case AUDIT_OBJ_ROLE:
2576 match = (ctxt->role == rule->au_ctxt.role);
2578 case AUDIT_NOT_EQUAL:
2579 match = (ctxt->role != rule->au_ctxt.role);
2583 case AUDIT_SUBJ_TYPE:
2584 case AUDIT_OBJ_TYPE:
2587 match = (ctxt->type == rule->au_ctxt.type);
2589 case AUDIT_NOT_EQUAL:
2590 match = (ctxt->type != rule->au_ctxt.type);
2594 case AUDIT_SUBJ_SEN:
2595 case AUDIT_SUBJ_CLR:
2596 case AUDIT_OBJ_LEV_LOW:
2597 case AUDIT_OBJ_LEV_HIGH:
2598 level = ((field == AUDIT_SUBJ_SEN ||
2599 field == AUDIT_OBJ_LEV_LOW) ?
2600 &ctxt->range.level[0] : &ctxt->range.level[1]);
2603 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2606 case AUDIT_NOT_EQUAL:
2607 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2610 case AUDIT_LESS_THAN:
2611 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2613 !mls_level_eq(&rule->au_ctxt.range.level[0],
2616 case AUDIT_LESS_THAN_OR_EQUAL:
2617 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2620 case AUDIT_GREATER_THAN:
2621 match = (mls_level_dom(level,
2622 &rule->au_ctxt.range.level[0]) &&
2623 !mls_level_eq(level,
2624 &rule->au_ctxt.range.level[0]));
2626 case AUDIT_GREATER_THAN_OR_EQUAL:
2627 match = mls_level_dom(level,
2628 &rule->au_ctxt.range.level[0]);
2634 read_unlock(&policy_rwlock);
2638 static int (*aurule_callback)(void) = audit_update_lsm_rules;
2640 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2641 u16 class, u32 perms, u32 *retained)
2645 if (event == AVC_CALLBACK_RESET && aurule_callback)
2646 err = aurule_callback();
2650 static int __init aurule_init(void)
2654 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2655 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2657 panic("avc_add_callback() failed, error %d\n", err);
2661 __initcall(aurule_init);
2663 #ifdef CONFIG_NETLABEL
2665 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2666 * @secattr: the NetLabel packet security attributes
2667 * @sid: the SELinux SID
2670 * Attempt to cache the context in @ctx, which was derived from the packet in
2671 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2672 * already been initialized.
2675 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2680 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2681 if (sid_cache == NULL)
2683 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2684 if (secattr->cache == NULL) {
2690 secattr->cache->free = kfree;
2691 secattr->cache->data = sid_cache;
2692 secattr->flags |= NETLBL_SECATTR_CACHE;
2696 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2697 * @secattr: the NetLabel packet security attributes
2698 * @sid: the SELinux SID
2701 * Convert the given NetLabel security attributes in @secattr into a
2702 * SELinux SID. If the @secattr field does not contain a full SELinux
2703 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2704 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2705 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2706 * conversion for future lookups. Returns zero on success, negative values on
2710 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2714 struct context *ctx;
2715 struct context ctx_new;
2717 if (!ss_initialized) {
2722 read_lock(&policy_rwlock);
2724 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2725 *sid = *(u32 *)secattr->cache->data;
2727 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2728 *sid = secattr->attr.secid;
2730 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2731 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
2733 goto netlbl_secattr_to_sid_return;
2735 ctx_new.user = ctx->user;
2736 ctx_new.role = ctx->role;
2737 ctx_new.type = ctx->type;
2738 mls_import_netlbl_lvl(&ctx_new, secattr);
2739 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2740 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2741 secattr->attr.mls.cat) != 0)
2742 goto netlbl_secattr_to_sid_return;
2743 ctx_new.range.level[1].cat.highbit =
2744 ctx_new.range.level[0].cat.highbit;
2745 ctx_new.range.level[1].cat.node =
2746 ctx_new.range.level[0].cat.node;
2748 ebitmap_init(&ctx_new.range.level[0].cat);
2749 ebitmap_init(&ctx_new.range.level[1].cat);
2751 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2752 goto netlbl_secattr_to_sid_return_cleanup;
2754 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2756 goto netlbl_secattr_to_sid_return_cleanup;
2758 security_netlbl_cache_add(secattr, *sid);
2760 ebitmap_destroy(&ctx_new.range.level[0].cat);
2766 netlbl_secattr_to_sid_return:
2767 read_unlock(&policy_rwlock);
2769 netlbl_secattr_to_sid_return_cleanup:
2770 ebitmap_destroy(&ctx_new.range.level[0].cat);
2771 goto netlbl_secattr_to_sid_return;
2775 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2776 * @sid: the SELinux SID
2777 * @secattr: the NetLabel packet security attributes
2780 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2781 * Returns zero on success, negative values on failure.
2784 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2787 struct context *ctx;
2789 if (!ss_initialized)
2792 read_lock(&policy_rwlock);
2793 ctx = sidtab_search(&sidtab, sid);
2795 goto netlbl_sid_to_secattr_failure;
2796 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2798 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
2799 mls_export_netlbl_lvl(ctx, secattr);
2800 rc = mls_export_netlbl_cat(ctx, secattr);
2802 goto netlbl_sid_to_secattr_failure;
2803 read_unlock(&policy_rwlock);
2807 netlbl_sid_to_secattr_failure:
2808 read_unlock(&policy_rwlock);
2811 #endif /* CONFIG_NETLABEL */