2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
20 * Updated: Chad Sellers <csellers@tresys.com>
22 * Added validation of kernel classes and permissions
24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/spinlock.h>
36 #include <linux/errno.h>
38 #include <linux/sched.h>
39 #include <linux/audit.h>
40 #include <linux/mutex.h>
42 #include <net/netlabel.h>
52 #include "conditional.h"
55 #include "selinux_netlabel.h"
57 extern void selnl_notify_policyload(u32 seqno);
58 unsigned int policydb_loaded_version;
61 * This is declared in avc.c
63 extern const struct selinux_class_perm selinux_class_perm;
65 static DEFINE_RWLOCK(policy_rwlock);
66 #define POLICY_RDLOCK read_lock(&policy_rwlock)
67 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
68 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
69 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
71 static DEFINE_MUTEX(load_mutex);
72 #define LOAD_LOCK mutex_lock(&load_mutex)
73 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
75 static struct sidtab sidtab;
76 struct policydb policydb;
77 int ss_initialized = 0;
80 * The largest sequence number that has been used when
81 * providing an access decision to the access vector cache.
82 * The sequence number only changes when a policy change
85 static u32 latest_granting = 0;
87 /* Forward declaration. */
88 static int context_struct_to_string(struct context *context, char **scontext,
92 * Return the boolean value of a constraint expression
93 * when it is applied to the specified source and target
96 * xcontext is a special beast... It is used by the validatetrans rules
97 * only. For these rules, scontext is the context before the transition,
98 * tcontext is the context after the transition, and xcontext is the context
99 * of the process performing the transition. All other callers of
100 * constraint_expr_eval should pass in NULL for xcontext.
102 static int constraint_expr_eval(struct context *scontext,
103 struct context *tcontext,
104 struct context *xcontext,
105 struct constraint_expr *cexpr)
109 struct role_datum *r1, *r2;
110 struct mls_level *l1, *l2;
111 struct constraint_expr *e;
112 int s[CEXPR_MAXDEPTH];
115 for (e = cexpr; e; e = e->next) {
116 switch (e->expr_type) {
132 if (sp == (CEXPR_MAXDEPTH-1))
136 val1 = scontext->user;
137 val2 = tcontext->user;
140 val1 = scontext->type;
141 val2 = tcontext->type;
144 val1 = scontext->role;
145 val2 = tcontext->role;
146 r1 = policydb.role_val_to_struct[val1 - 1];
147 r2 = policydb.role_val_to_struct[val2 - 1];
150 s[++sp] = ebitmap_get_bit(&r1->dominates,
154 s[++sp] = ebitmap_get_bit(&r2->dominates,
158 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
160 !ebitmap_get_bit(&r2->dominates,
168 l1 = &(scontext->range.level[0]);
169 l2 = &(tcontext->range.level[0]);
172 l1 = &(scontext->range.level[0]);
173 l2 = &(tcontext->range.level[1]);
176 l1 = &(scontext->range.level[1]);
177 l2 = &(tcontext->range.level[0]);
180 l1 = &(scontext->range.level[1]);
181 l2 = &(tcontext->range.level[1]);
184 l1 = &(scontext->range.level[0]);
185 l2 = &(scontext->range.level[1]);
188 l1 = &(tcontext->range.level[0]);
189 l2 = &(tcontext->range.level[1]);
194 s[++sp] = mls_level_eq(l1, l2);
197 s[++sp] = !mls_level_eq(l1, l2);
200 s[++sp] = mls_level_dom(l1, l2);
203 s[++sp] = mls_level_dom(l2, l1);
206 s[++sp] = mls_level_incomp(l2, l1);
220 s[++sp] = (val1 == val2);
223 s[++sp] = (val1 != val2);
231 if (sp == (CEXPR_MAXDEPTH-1))
234 if (e->attr & CEXPR_TARGET)
236 else if (e->attr & CEXPR_XTARGET) {
243 if (e->attr & CEXPR_USER)
245 else if (e->attr & CEXPR_ROLE)
247 else if (e->attr & CEXPR_TYPE)
256 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
259 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
277 * Compute access vectors based on a context structure pair for
278 * the permissions in a particular class.
280 static int context_struct_compute_av(struct context *scontext,
281 struct context *tcontext,
284 struct av_decision *avd)
286 struct constraint_node *constraint;
287 struct role_allow *ra;
288 struct avtab_key avkey;
289 struct avtab_node *node;
290 struct class_datum *tclass_datum;
291 struct ebitmap *sattr, *tattr;
292 struct ebitmap_node *snode, *tnode;
296 * Remap extended Netlink classes for old policy versions.
297 * Do this here rather than socket_type_to_security_class()
298 * in case a newer policy version is loaded, allowing sockets
299 * to remain in the correct class.
301 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
302 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
303 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
304 tclass = SECCLASS_NETLINK_SOCKET;
306 if (!tclass || tclass > policydb.p_classes.nprim) {
307 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
311 tclass_datum = policydb.class_val_to_struct[tclass - 1];
314 * Initialize the access vectors to the default values.
317 avd->decided = 0xffffffff;
319 avd->auditdeny = 0xffffffff;
320 avd->seqno = latest_granting;
323 * If a specific type enforcement rule was defined for
324 * this permission check, then use it.
326 avkey.target_class = tclass;
327 avkey.specified = AVTAB_AV;
328 sattr = &policydb.type_attr_map[scontext->type - 1];
329 tattr = &policydb.type_attr_map[tcontext->type - 1];
330 ebitmap_for_each_bit(sattr, snode, i) {
331 if (!ebitmap_node_get_bit(snode, i))
333 ebitmap_for_each_bit(tattr, tnode, j) {
334 if (!ebitmap_node_get_bit(tnode, j))
336 avkey.source_type = i + 1;
337 avkey.target_type = j + 1;
338 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
340 node = avtab_search_node_next(node, avkey.specified)) {
341 if (node->key.specified == AVTAB_ALLOWED)
342 avd->allowed |= node->datum.data;
343 else if (node->key.specified == AVTAB_AUDITALLOW)
344 avd->auditallow |= node->datum.data;
345 else if (node->key.specified == AVTAB_AUDITDENY)
346 avd->auditdeny &= node->datum.data;
349 /* Check conditional av table for additional permissions */
350 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
356 * Remove any permissions prohibited by a constraint (this includes
359 constraint = tclass_datum->constraints;
361 if ((constraint->permissions & (avd->allowed)) &&
362 !constraint_expr_eval(scontext, tcontext, NULL,
364 avd->allowed = (avd->allowed) & ~(constraint->permissions);
366 constraint = constraint->next;
370 * If checking process transition permission and the
371 * role is changing, then check the (current_role, new_role)
374 if (tclass == SECCLASS_PROCESS &&
375 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
376 scontext->role != tcontext->role) {
377 for (ra = policydb.role_allow; ra; ra = ra->next) {
378 if (scontext->role == ra->role &&
379 tcontext->role == ra->new_role)
383 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
384 PROCESS__DYNTRANSITION);
390 static int security_validtrans_handle_fail(struct context *ocontext,
391 struct context *ncontext,
392 struct context *tcontext,
395 char *o = NULL, *n = NULL, *t = NULL;
396 u32 olen, nlen, tlen;
398 if (context_struct_to_string(ocontext, &o, &olen) < 0)
400 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
402 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
404 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
405 "security_validate_transition: denied for"
406 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
407 o, n, t, policydb.p_class_val_to_name[tclass-1]);
413 if (!selinux_enforcing)
418 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
421 struct context *ocontext;
422 struct context *ncontext;
423 struct context *tcontext;
424 struct class_datum *tclass_datum;
425 struct constraint_node *constraint;
434 * Remap extended Netlink classes for old policy versions.
435 * Do this here rather than socket_type_to_security_class()
436 * in case a newer policy version is loaded, allowing sockets
437 * to remain in the correct class.
439 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
440 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
441 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
442 tclass = SECCLASS_NETLINK_SOCKET;
444 if (!tclass || tclass > policydb.p_classes.nprim) {
445 printk(KERN_ERR "security_validate_transition: "
446 "unrecognized class %d\n", tclass);
450 tclass_datum = policydb.class_val_to_struct[tclass - 1];
452 ocontext = sidtab_search(&sidtab, oldsid);
454 printk(KERN_ERR "security_validate_transition: "
455 " unrecognized SID %d\n", oldsid);
460 ncontext = sidtab_search(&sidtab, newsid);
462 printk(KERN_ERR "security_validate_transition: "
463 " unrecognized SID %d\n", newsid);
468 tcontext = sidtab_search(&sidtab, tasksid);
470 printk(KERN_ERR "security_validate_transition: "
471 " unrecognized SID %d\n", tasksid);
476 constraint = tclass_datum->validatetrans;
478 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
480 rc = security_validtrans_handle_fail(ocontext, ncontext,
484 constraint = constraint->next;
493 * security_compute_av - Compute access vector decisions.
494 * @ssid: source security identifier
495 * @tsid: target security identifier
496 * @tclass: target security class
497 * @requested: requested permissions
498 * @avd: access vector decisions
500 * Compute a set of access vector decisions based on the
501 * SID pair (@ssid, @tsid) for the permissions in @tclass.
502 * Return -%EINVAL if any of the parameters are invalid or %0
503 * if the access vector decisions were computed successfully.
505 int security_compute_av(u32 ssid,
509 struct av_decision *avd)
511 struct context *scontext = NULL, *tcontext = NULL;
514 if (!ss_initialized) {
515 avd->allowed = 0xffffffff;
516 avd->decided = 0xffffffff;
518 avd->auditdeny = 0xffffffff;
519 avd->seqno = latest_granting;
525 scontext = sidtab_search(&sidtab, ssid);
527 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
532 tcontext = sidtab_search(&sidtab, tsid);
534 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
540 rc = context_struct_compute_av(scontext, tcontext, tclass,
548 * Write the security context string representation of
549 * the context structure `context' into a dynamically
550 * allocated string of the correct size. Set `*scontext'
551 * to point to this string and set `*scontext_len' to
552 * the length of the string.
554 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
561 /* Compute the size of the context. */
562 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
563 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
564 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
565 *scontext_len += mls_compute_context_len(context);
567 /* Allocate space for the context; caller must free this space. */
568 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
572 *scontext = scontextp;
575 * Copy the user name, role name and type name into the context.
577 sprintf(scontextp, "%s:%s:%s",
578 policydb.p_user_val_to_name[context->user - 1],
579 policydb.p_role_val_to_name[context->role - 1],
580 policydb.p_type_val_to_name[context->type - 1]);
581 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
582 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
583 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
585 mls_sid_to_context(context, &scontextp);
592 #include "initial_sid_to_string.h"
595 * security_sid_to_context - Obtain a context for a given SID.
596 * @sid: security identifier, SID
597 * @scontext: security context
598 * @scontext_len: length in bytes
600 * Write the string representation of the context associated with @sid
601 * into a dynamically allocated string of the correct size. Set @scontext
602 * to point to this string and set @scontext_len to the length of the string.
604 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
606 struct context *context;
609 if (!ss_initialized) {
610 if (sid <= SECINITSID_NUM) {
613 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
614 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
619 strcpy(scontextp, initial_sid_to_string[sid]);
620 *scontext = scontextp;
623 printk(KERN_ERR "security_sid_to_context: called before initial "
624 "load_policy on unknown SID %d\n", sid);
629 context = sidtab_search(&sidtab, sid);
631 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
636 rc = context_struct_to_string(context, scontext, scontext_len);
644 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
647 struct context context;
648 struct role_datum *role;
649 struct type_datum *typdatum;
650 struct user_datum *usrdatum;
651 char *scontextp, *p, oldc;
654 if (!ss_initialized) {
657 for (i = 1; i < SECINITSID_NUM; i++) {
658 if (!strcmp(initial_sid_to_string[i], scontext)) {
663 *sid = SECINITSID_KERNEL;
668 /* Copy the string so that we can modify the copy as we parse it.
669 The string should already by null terminated, but we append a
670 null suffix to the copy to avoid problems with the existing
671 attr package, which doesn't view the null terminator as part
672 of the attribute value. */
673 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
678 memcpy(scontext2, scontext, scontext_len);
679 scontext2[scontext_len] = 0;
681 context_init(&context);
686 /* Parse the security context. */
689 scontextp = (char *) scontext2;
691 /* Extract the user. */
693 while (*p && *p != ':')
701 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
705 context.user = usrdatum->value;
709 while (*p && *p != ':')
717 role = hashtab_search(policydb.p_roles.table, scontextp);
720 context.role = role->value;
724 while (*p && *p != ':')
729 typdatum = hashtab_search(policydb.p_types.table, scontextp);
733 context.type = typdatum->value;
735 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
739 if ((p - scontext2) < scontext_len) {
744 /* Check the validity of the new context. */
745 if (!policydb_context_isvalid(&policydb, &context)) {
749 /* Obtain the new sid. */
750 rc = sidtab_context_to_sid(&sidtab, &context, sid);
753 context_destroy(&context);
760 * security_context_to_sid - Obtain a SID for a given security context.
761 * @scontext: security context
762 * @scontext_len: length in bytes
763 * @sid: security identifier, SID
765 * Obtains a SID associated with the security context that
766 * has the string representation specified by @scontext.
767 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
768 * memory is available, or 0 on success.
770 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
772 return security_context_to_sid_core(scontext, scontext_len,
777 * security_context_to_sid_default - Obtain a SID for a given security context,
778 * falling back to specified default if needed.
780 * @scontext: security context
781 * @scontext_len: length in bytes
782 * @sid: security identifier, SID
783 * @def_sid: default SID to assign on errror
785 * Obtains a SID associated with the security context that
786 * has the string representation specified by @scontext.
787 * The default SID is passed to the MLS layer to be used to allow
788 * kernel labeling of the MLS field if the MLS field is not present
789 * (for upgrading to MLS without full relabel).
790 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
791 * memory is available, or 0 on success.
793 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
795 return security_context_to_sid_core(scontext, scontext_len,
799 static int compute_sid_handle_invalid_context(
800 struct context *scontext,
801 struct context *tcontext,
803 struct context *newcontext)
805 char *s = NULL, *t = NULL, *n = NULL;
806 u32 slen, tlen, nlen;
808 if (context_struct_to_string(scontext, &s, &slen) < 0)
810 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
812 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
814 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
815 "security_compute_sid: invalid context %s"
819 n, s, t, policydb.p_class_val_to_name[tclass-1]);
824 if (!selinux_enforcing)
829 static int security_compute_sid(u32 ssid,
835 struct context *scontext = NULL, *tcontext = NULL, newcontext;
836 struct role_trans *roletr = NULL;
837 struct avtab_key avkey;
838 struct avtab_datum *avdatum;
839 struct avtab_node *node;
842 if (!ss_initialized) {
844 case SECCLASS_PROCESS:
854 context_init(&newcontext);
858 scontext = sidtab_search(&sidtab, ssid);
860 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
865 tcontext = sidtab_search(&sidtab, tsid);
867 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
873 /* Set the user identity. */
875 case AVTAB_TRANSITION:
877 /* Use the process user identity. */
878 newcontext.user = scontext->user;
881 /* Use the related object owner. */
882 newcontext.user = tcontext->user;
886 /* Set the role and type to default values. */
888 case SECCLASS_PROCESS:
889 /* Use the current role and type of process. */
890 newcontext.role = scontext->role;
891 newcontext.type = scontext->type;
894 /* Use the well-defined object role. */
895 newcontext.role = OBJECT_R_VAL;
896 /* Use the type of the related object. */
897 newcontext.type = tcontext->type;
900 /* Look for a type transition/member/change rule. */
901 avkey.source_type = scontext->type;
902 avkey.target_type = tcontext->type;
903 avkey.target_class = tclass;
904 avkey.specified = specified;
905 avdatum = avtab_search(&policydb.te_avtab, &avkey);
907 /* If no permanent rule, also check for enabled conditional rules */
909 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
910 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
911 if (node->key.specified & AVTAB_ENABLED) {
912 avdatum = &node->datum;
919 /* Use the type from the type transition/member/change rule. */
920 newcontext.type = avdatum->data;
923 /* Check for class-specific changes. */
925 case SECCLASS_PROCESS:
926 if (specified & AVTAB_TRANSITION) {
927 /* Look for a role transition rule. */
928 for (roletr = policydb.role_tr; roletr;
929 roletr = roletr->next) {
930 if (roletr->role == scontext->role &&
931 roletr->type == tcontext->type) {
932 /* Use the role transition rule. */
933 newcontext.role = roletr->new_role;
943 /* Set the MLS attributes.
944 This is done last because it may allocate memory. */
945 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
949 /* Check the validity of the context. */
950 if (!policydb_context_isvalid(&policydb, &newcontext)) {
951 rc = compute_sid_handle_invalid_context(scontext,
958 /* Obtain the sid for the context. */
959 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
962 context_destroy(&newcontext);
968 * security_transition_sid - Compute the SID for a new subject/object.
969 * @ssid: source security identifier
970 * @tsid: target security identifier
971 * @tclass: target security class
972 * @out_sid: security identifier for new subject/object
974 * Compute a SID to use for labeling a new subject or object in the
975 * class @tclass based on a SID pair (@ssid, @tsid).
976 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
977 * if insufficient memory is available, or %0 if the new SID was
978 * computed successfully.
980 int security_transition_sid(u32 ssid,
985 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
989 * security_member_sid - Compute the SID for member selection.
990 * @ssid: source security identifier
991 * @tsid: target security identifier
992 * @tclass: target security class
993 * @out_sid: security identifier for selected member
995 * Compute a SID to use when selecting a member of a polyinstantiated
996 * object of class @tclass based on a SID pair (@ssid, @tsid).
997 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
998 * if insufficient memory is available, or %0 if the SID was
999 * computed successfully.
1001 int security_member_sid(u32 ssid,
1006 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1010 * security_change_sid - Compute the SID for object relabeling.
1011 * @ssid: source security identifier
1012 * @tsid: target security identifier
1013 * @tclass: target security class
1014 * @out_sid: security identifier for selected member
1016 * Compute a SID to use for relabeling an object of class @tclass
1017 * based on a SID pair (@ssid, @tsid).
1018 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1019 * if insufficient memory is available, or %0 if the SID was
1020 * computed successfully.
1022 int security_change_sid(u32 ssid,
1027 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1031 * Verify that each kernel class that is defined in the
1034 static int validate_classes(struct policydb *p)
1037 struct class_datum *cladatum;
1038 struct perm_datum *perdatum;
1039 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1041 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1042 const char *def_class, *def_perm, *pol_class;
1043 struct symtab *perms;
1045 for (i = 1; i < kdefs->cts_len; i++) {
1046 def_class = kdefs->class_to_string[i];
1047 if (i > p->p_classes.nprim) {
1049 "security: class %s not defined in policy\n",
1053 pol_class = p->p_class_val_to_name[i-1];
1054 if (strcmp(pol_class, def_class)) {
1056 "security: class %d is incorrect, found %s but should be %s\n",
1057 i, pol_class, def_class);
1061 for (i = 0; i < kdefs->av_pts_len; i++) {
1062 class_val = kdefs->av_perm_to_string[i].tclass;
1063 perm_val = kdefs->av_perm_to_string[i].value;
1064 def_perm = kdefs->av_perm_to_string[i].name;
1065 if (class_val > p->p_classes.nprim)
1067 pol_class = p->p_class_val_to_name[class_val-1];
1068 cladatum = hashtab_search(p->p_classes.table, pol_class);
1070 perms = &cladatum->permissions;
1071 nprim = 1 << (perms->nprim - 1);
1072 if (perm_val > nprim) {
1074 "security: permission %s in class %s not defined in policy\n",
1075 def_perm, pol_class);
1078 perdatum = hashtab_search(perms->table, def_perm);
1079 if (perdatum == NULL) {
1081 "security: permission %s in class %s not found in policy\n",
1082 def_perm, pol_class);
1085 pol_val = 1 << (perdatum->value - 1);
1086 if (pol_val != perm_val) {
1088 "security: permission %s in class %s has incorrect value\n",
1089 def_perm, pol_class);
1093 for (i = 0; i < kdefs->av_inherit_len; i++) {
1094 class_val = kdefs->av_inherit[i].tclass;
1095 if (class_val > p->p_classes.nprim)
1097 pol_class = p->p_class_val_to_name[class_val-1];
1098 cladatum = hashtab_search(p->p_classes.table, pol_class);
1100 if (!cladatum->comdatum) {
1102 "security: class %s should have an inherits clause but does not\n",
1106 tmp = kdefs->av_inherit[i].common_base;
1108 while (!(tmp & 0x01)) {
1112 perms = &cladatum->comdatum->permissions;
1113 for (j = 0; j < common_pts_len; j++) {
1114 def_perm = kdefs->av_inherit[i].common_pts[j];
1115 if (j >= perms->nprim) {
1117 "security: permission %s in class %s not defined in policy\n",
1118 def_perm, pol_class);
1121 perdatum = hashtab_search(perms->table, def_perm);
1122 if (perdatum == NULL) {
1124 "security: permission %s in class %s not found in policy\n",
1125 def_perm, pol_class);
1128 if (perdatum->value != j + 1) {
1130 "security: permission %s in class %s has incorrect value\n",
1131 def_perm, pol_class);
1139 /* Clone the SID into the new SID table. */
1140 static int clone_sid(u32 sid,
1141 struct context *context,
1144 struct sidtab *s = arg;
1146 return sidtab_insert(s, sid, context);
1149 static inline int convert_context_handle_invalid_context(struct context *context)
1153 if (selinux_enforcing) {
1159 context_struct_to_string(context, &s, &len);
1160 printk(KERN_ERR "security: context %s is invalid\n", s);
1166 struct convert_context_args {
1167 struct policydb *oldp;
1168 struct policydb *newp;
1172 * Convert the values in the security context
1173 * structure `c' from the values specified
1174 * in the policy `p->oldp' to the values specified
1175 * in the policy `p->newp'. Verify that the
1176 * context is valid under the new policy.
1178 static int convert_context(u32 key,
1182 struct convert_context_args *args;
1183 struct context oldc;
1184 struct role_datum *role;
1185 struct type_datum *typdatum;
1186 struct user_datum *usrdatum;
1193 rc = context_cpy(&oldc, c);
1199 /* Convert the user. */
1200 usrdatum = hashtab_search(args->newp->p_users.table,
1201 args->oldp->p_user_val_to_name[c->user - 1]);
1205 c->user = usrdatum->value;
1207 /* Convert the role. */
1208 role = hashtab_search(args->newp->p_roles.table,
1209 args->oldp->p_role_val_to_name[c->role - 1]);
1213 c->role = role->value;
1215 /* Convert the type. */
1216 typdatum = hashtab_search(args->newp->p_types.table,
1217 args->oldp->p_type_val_to_name[c->type - 1]);
1221 c->type = typdatum->value;
1223 rc = mls_convert_context(args->oldp, args->newp, c);
1227 /* Check the validity of the new context. */
1228 if (!policydb_context_isvalid(args->newp, c)) {
1229 rc = convert_context_handle_invalid_context(&oldc);
1234 context_destroy(&oldc);
1238 context_struct_to_string(&oldc, &s, &len);
1239 context_destroy(&oldc);
1240 printk(KERN_ERR "security: invalidating context %s\n", s);
1245 extern void selinux_complete_init(void);
1248 * security_load_policy - Load a security policy configuration.
1249 * @data: binary policy data
1250 * @len: length of data in bytes
1252 * Load a new set of security policy configuration data,
1253 * validate it and convert the SID table as necessary.
1254 * This function will flush the access vector cache after
1255 * loading the new policy.
1257 int security_load_policy(void *data, size_t len)
1259 struct policydb oldpolicydb, newpolicydb;
1260 struct sidtab oldsidtab, newsidtab;
1261 struct convert_context_args args;
1264 struct policy_file file = { data, len }, *fp = &file;
1268 if (!ss_initialized) {
1270 if (policydb_read(&policydb, fp)) {
1272 avtab_cache_destroy();
1275 if (policydb_load_isids(&policydb, &sidtab)) {
1277 policydb_destroy(&policydb);
1278 avtab_cache_destroy();
1281 /* Verify that the kernel defined classes are correct. */
1282 if (validate_classes(&policydb)) {
1284 "security: the definition of a class is incorrect\n");
1286 sidtab_destroy(&sidtab);
1287 policydb_destroy(&policydb);
1288 avtab_cache_destroy();
1291 policydb_loaded_version = policydb.policyvers;
1293 seqno = ++latest_granting;
1295 selinux_complete_init();
1296 avc_ss_reset(seqno);
1297 selnl_notify_policyload(seqno);
1298 selinux_netlbl_cache_invalidate();
1303 sidtab_hash_eval(&sidtab, "sids");
1306 if (policydb_read(&newpolicydb, fp)) {
1311 sidtab_init(&newsidtab);
1313 /* Verify that the kernel defined classes are correct. */
1314 if (validate_classes(&newpolicydb)) {
1316 "security: the definition of a class is incorrect\n");
1321 /* Clone the SID table. */
1322 sidtab_shutdown(&sidtab);
1323 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1328 /* Convert the internal representations of contexts
1329 in the new SID table and remove invalid SIDs. */
1330 args.oldp = &policydb;
1331 args.newp = &newpolicydb;
1332 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1334 /* Save the old policydb and SID table to free later. */
1335 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1336 sidtab_set(&oldsidtab, &sidtab);
1338 /* Install the new policydb and SID table. */
1340 memcpy(&policydb, &newpolicydb, sizeof policydb);
1341 sidtab_set(&sidtab, &newsidtab);
1342 seqno = ++latest_granting;
1343 policydb_loaded_version = policydb.policyvers;
1347 /* Free the old policydb and SID table. */
1348 policydb_destroy(&oldpolicydb);
1349 sidtab_destroy(&oldsidtab);
1351 avc_ss_reset(seqno);
1352 selnl_notify_policyload(seqno);
1353 selinux_netlbl_cache_invalidate();
1359 sidtab_destroy(&newsidtab);
1360 policydb_destroy(&newpolicydb);
1366 * security_port_sid - Obtain the SID for a port.
1367 * @domain: communication domain aka address family
1368 * @type: socket type
1369 * @protocol: protocol number
1370 * @port: port number
1371 * @out_sid: security identifier
1373 int security_port_sid(u16 domain,
1384 c = policydb.ocontexts[OCON_PORT];
1386 if (c->u.port.protocol == protocol &&
1387 c->u.port.low_port <= port &&
1388 c->u.port.high_port >= port)
1395 rc = sidtab_context_to_sid(&sidtab,
1401 *out_sid = c->sid[0];
1403 *out_sid = SECINITSID_PORT;
1412 * security_netif_sid - Obtain the SID for a network interface.
1413 * @name: interface name
1414 * @if_sid: interface SID
1415 * @msg_sid: default SID for received packets
1417 int security_netif_sid(char *name,
1426 c = policydb.ocontexts[OCON_NETIF];
1428 if (strcmp(name, c->u.name) == 0)
1434 if (!c->sid[0] || !c->sid[1]) {
1435 rc = sidtab_context_to_sid(&sidtab,
1440 rc = sidtab_context_to_sid(&sidtab,
1446 *if_sid = c->sid[0];
1447 *msg_sid = c->sid[1];
1449 *if_sid = SECINITSID_NETIF;
1450 *msg_sid = SECINITSID_NETMSG;
1458 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1462 for(i = 0; i < 4; i++)
1463 if(addr[i] != (input[i] & mask[i])) {
1472 * security_node_sid - Obtain the SID for a node (host).
1473 * @domain: communication domain aka address family
1475 * @addrlen: address length in bytes
1476 * @out_sid: security identifier
1478 int security_node_sid(u16 domain,
1492 if (addrlen != sizeof(u32)) {
1497 addr = *((u32 *)addrp);
1499 c = policydb.ocontexts[OCON_NODE];
1501 if (c->u.node.addr == (addr & c->u.node.mask))
1509 if (addrlen != sizeof(u64) * 2) {
1513 c = policydb.ocontexts[OCON_NODE6];
1515 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1523 *out_sid = SECINITSID_NODE;
1529 rc = sidtab_context_to_sid(&sidtab,
1535 *out_sid = c->sid[0];
1537 *out_sid = SECINITSID_NODE;
1548 * security_get_user_sids - Obtain reachable SIDs for a user.
1549 * @fromsid: starting SID
1550 * @username: username
1551 * @sids: array of reachable SIDs for user
1552 * @nel: number of elements in @sids
1554 * Generate the set of SIDs for legal security contexts
1555 * for a given user that can be reached by @fromsid.
1556 * Set *@sids to point to a dynamically allocated
1557 * array containing the set of SIDs. Set *@nel to the
1558 * number of elements in the array.
1561 int security_get_user_sids(u32 fromsid,
1566 struct context *fromcon, usercon;
1567 u32 *mysids, *mysids2, sid;
1568 u32 mynel = 0, maxnel = SIDS_NEL;
1569 struct user_datum *user;
1570 struct role_datum *role;
1571 struct av_decision avd;
1572 struct ebitmap_node *rnode, *tnode;
1575 if (!ss_initialized) {
1583 fromcon = sidtab_search(&sidtab, fromsid);
1589 user = hashtab_search(policydb.p_users.table, username);
1594 usercon.user = user->value;
1596 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1602 ebitmap_for_each_bit(&user->roles, rnode, i) {
1603 if (!ebitmap_node_get_bit(rnode, i))
1605 role = policydb.role_val_to_struct[i];
1607 ebitmap_for_each_bit(&role->types, tnode, j) {
1608 if (!ebitmap_node_get_bit(tnode, j))
1612 if (mls_setup_user_range(fromcon, user, &usercon))
1615 rc = context_struct_compute_av(fromcon, &usercon,
1617 PROCESS__TRANSITION,
1619 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1621 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1626 if (mynel < maxnel) {
1627 mysids[mynel++] = sid;
1630 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1636 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1639 mysids[mynel++] = sid;
1654 * security_genfs_sid - Obtain a SID for a file in a filesystem
1655 * @fstype: filesystem type
1656 * @path: path from root of mount
1657 * @sclass: file security class
1658 * @sid: SID for path
1660 * Obtain a SID to use for a file in a filesystem that
1661 * cannot support xattr or use a fixed labeling behavior like
1662 * transition SIDs or task SIDs.
1664 int security_genfs_sid(const char *fstype,
1670 struct genfs *genfs;
1672 int rc = 0, cmp = 0;
1676 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1677 cmp = strcmp(fstype, genfs->fstype);
1682 if (!genfs || cmp) {
1683 *sid = SECINITSID_UNLABELED;
1688 for (c = genfs->head; c; c = c->next) {
1689 len = strlen(c->u.name);
1690 if ((!c->v.sclass || sclass == c->v.sclass) &&
1691 (strncmp(c->u.name, path, len) == 0))
1696 *sid = SECINITSID_UNLABELED;
1702 rc = sidtab_context_to_sid(&sidtab,
1716 * security_fs_use - Determine how to handle labeling for a filesystem.
1717 * @fstype: filesystem type
1718 * @behavior: labeling behavior
1719 * @sid: SID for filesystem (superblock)
1721 int security_fs_use(
1723 unsigned int *behavior,
1731 c = policydb.ocontexts[OCON_FSUSE];
1733 if (strcmp(fstype, c->u.name) == 0)
1739 *behavior = c->v.behavior;
1741 rc = sidtab_context_to_sid(&sidtab,
1749 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1751 *behavior = SECURITY_FS_USE_NONE;
1754 *behavior = SECURITY_FS_USE_GENFS;
1763 int security_get_bools(int *len, char ***names, int **values)
1765 int i, rc = -ENOMEM;
1771 *len = policydb.p_bools.nprim;
1777 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1781 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1785 for (i = 0; i < *len; i++) {
1787 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1788 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1789 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1792 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1793 (*names)[i][name_len - 1] = 0;
1801 for (i = 0; i < *len; i++)
1809 int security_set_bools(int len, int *values)
1812 int lenp, seqno = 0;
1813 struct cond_node *cur;
1817 lenp = policydb.p_bools.nprim;
1823 for (i = 0; i < len; i++) {
1824 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1825 audit_log(current->audit_context, GFP_ATOMIC,
1826 AUDIT_MAC_CONFIG_CHANGE,
1827 "bool=%s val=%d old_val=%d auid=%u",
1828 policydb.p_bool_val_to_name[i],
1830 policydb.bool_val_to_struct[i]->state,
1831 audit_get_loginuid(current->audit_context));
1834 policydb.bool_val_to_struct[i]->state = 1;
1836 policydb.bool_val_to_struct[i]->state = 0;
1840 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1841 rc = evaluate_cond_node(&policydb, cur);
1846 seqno = ++latest_granting;
1851 avc_ss_reset(seqno);
1852 selnl_notify_policyload(seqno);
1857 int security_get_bool_value(int bool)
1864 len = policydb.p_bools.nprim;
1870 rc = policydb.bool_val_to_struct[bool]->state;
1877 * security_sid_mls_copy() - computes a new sid based on the given
1878 * sid and the mls portion of mls_sid.
1880 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1882 struct context *context1;
1883 struct context *context2;
1884 struct context newcon;
1889 if (!ss_initialized || !selinux_mls_enabled) {
1894 context_init(&newcon);
1897 context1 = sidtab_search(&sidtab, sid);
1899 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1905 context2 = sidtab_search(&sidtab, mls_sid);
1907 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1913 newcon.user = context1->user;
1914 newcon.role = context1->role;
1915 newcon.type = context1->type;
1916 rc = mls_copy_context(&newcon, context2);
1921 /* Check the validity of the new context. */
1922 if (!policydb_context_isvalid(&policydb, &newcon)) {
1923 rc = convert_context_handle_invalid_context(&newcon);
1928 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1932 if (!context_struct_to_string(&newcon, &s, &len)) {
1933 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1934 "security_sid_mls_copy: invalid context %s", s);
1940 context_destroy(&newcon);
1945 struct selinux_audit_rule {
1947 struct context au_ctxt;
1950 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1953 context_destroy(&rule->au_ctxt);
1958 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1959 struct selinux_audit_rule **rule)
1961 struct selinux_audit_rule *tmprule;
1962 struct role_datum *roledatum;
1963 struct type_datum *typedatum;
1964 struct user_datum *userdatum;
1969 if (!ss_initialized)
1973 case AUDIT_SUBJ_USER:
1974 case AUDIT_SUBJ_ROLE:
1975 case AUDIT_SUBJ_TYPE:
1976 case AUDIT_OBJ_USER:
1977 case AUDIT_OBJ_ROLE:
1978 case AUDIT_OBJ_TYPE:
1979 /* only 'equals' and 'not equals' fit user, role, and type */
1980 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1983 case AUDIT_SUBJ_SEN:
1984 case AUDIT_SUBJ_CLR:
1985 case AUDIT_OBJ_LEV_LOW:
1986 case AUDIT_OBJ_LEV_HIGH:
1987 /* we do not allow a range, indicated by the presense of '-' */
1988 if (strchr(rulestr, '-'))
1992 /* only the above fields are valid */
1996 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2000 context_init(&tmprule->au_ctxt);
2004 tmprule->au_seqno = latest_granting;
2007 case AUDIT_SUBJ_USER:
2008 case AUDIT_OBJ_USER:
2009 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2013 tmprule->au_ctxt.user = userdatum->value;
2015 case AUDIT_SUBJ_ROLE:
2016 case AUDIT_OBJ_ROLE:
2017 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2021 tmprule->au_ctxt.role = roledatum->value;
2023 case AUDIT_SUBJ_TYPE:
2024 case AUDIT_OBJ_TYPE:
2025 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2029 tmprule->au_ctxt.type = typedatum->value;
2031 case AUDIT_SUBJ_SEN:
2032 case AUDIT_SUBJ_CLR:
2033 case AUDIT_OBJ_LEV_LOW:
2034 case AUDIT_OBJ_LEV_HIGH:
2035 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2042 selinux_audit_rule_free(tmprule);
2051 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2052 struct selinux_audit_rule *rule,
2053 struct audit_context *actx)
2055 struct context *ctxt;
2056 struct mls_level *level;
2060 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2061 "selinux_audit_rule_match: missing rule\n");
2067 if (rule->au_seqno < latest_granting) {
2068 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2069 "selinux_audit_rule_match: stale rule\n");
2074 ctxt = sidtab_search(&sidtab, sid);
2076 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2077 "selinux_audit_rule_match: unrecognized SID %d\n",
2083 /* a field/op pair that is not caught here will simply fall through
2086 case AUDIT_SUBJ_USER:
2087 case AUDIT_OBJ_USER:
2090 match = (ctxt->user == rule->au_ctxt.user);
2092 case AUDIT_NOT_EQUAL:
2093 match = (ctxt->user != rule->au_ctxt.user);
2097 case AUDIT_SUBJ_ROLE:
2098 case AUDIT_OBJ_ROLE:
2101 match = (ctxt->role == rule->au_ctxt.role);
2103 case AUDIT_NOT_EQUAL:
2104 match = (ctxt->role != rule->au_ctxt.role);
2108 case AUDIT_SUBJ_TYPE:
2109 case AUDIT_OBJ_TYPE:
2112 match = (ctxt->type == rule->au_ctxt.type);
2114 case AUDIT_NOT_EQUAL:
2115 match = (ctxt->type != rule->au_ctxt.type);
2119 case AUDIT_SUBJ_SEN:
2120 case AUDIT_SUBJ_CLR:
2121 case AUDIT_OBJ_LEV_LOW:
2122 case AUDIT_OBJ_LEV_HIGH:
2123 level = ((field == AUDIT_SUBJ_SEN ||
2124 field == AUDIT_OBJ_LEV_LOW) ?
2125 &ctxt->range.level[0] : &ctxt->range.level[1]);
2128 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2131 case AUDIT_NOT_EQUAL:
2132 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2135 case AUDIT_LESS_THAN:
2136 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2138 !mls_level_eq(&rule->au_ctxt.range.level[0],
2141 case AUDIT_LESS_THAN_OR_EQUAL:
2142 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2145 case AUDIT_GREATER_THAN:
2146 match = (mls_level_dom(level,
2147 &rule->au_ctxt.range.level[0]) &&
2148 !mls_level_eq(level,
2149 &rule->au_ctxt.range.level[0]));
2151 case AUDIT_GREATER_THAN_OR_EQUAL:
2152 match = mls_level_dom(level,
2153 &rule->au_ctxt.range.level[0]);
2163 static int (*aurule_callback)(void) = NULL;
2165 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2166 u16 class, u32 perms, u32 *retained)
2170 if (event == AVC_CALLBACK_RESET && aurule_callback)
2171 err = aurule_callback();
2175 static int __init aurule_init(void)
2179 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2180 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2182 panic("avc_add_callback() failed, error %d\n", err);
2186 __initcall(aurule_init);
2188 void selinux_audit_set_callback(int (*callback)(void))
2190 aurule_callback = callback;
2193 #ifdef CONFIG_NETLABEL
2195 * This is the structure we store inside the NetLabel cache block.
2197 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2198 #define NETLBL_CACHE_T_NONE 0
2199 #define NETLBL_CACHE_T_SID 1
2200 #define NETLBL_CACHE_T_MLS 2
2201 struct netlbl_cache {
2205 struct mls_range mls_label;
2210 * selinux_netlbl_cache_free - Free the NetLabel cached data
2211 * @data: the data to free
2214 * This function is intended to be used as the free() callback inside the
2215 * netlbl_lsm_cache structure.
2218 static void selinux_netlbl_cache_free(const void *data)
2220 struct netlbl_cache *cache;
2225 cache = NETLBL_CACHE(data);
2226 switch (cache->type) {
2227 case NETLBL_CACHE_T_MLS:
2228 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2235 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2237 * @ctx: the SELinux context
2240 * Attempt to cache the context in @ctx, which was derived from the packet in
2241 * @skb, in the NetLabel subsystem cache.
2244 static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
2246 struct netlbl_cache *cache = NULL;
2247 struct netlbl_lsm_secattr secattr;
2249 netlbl_secattr_init(&secattr);
2250 secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2251 if (secattr.cache == NULL)
2252 goto netlbl_cache_add_return;
2254 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2256 goto netlbl_cache_add_return;
2257 secattr.cache->free = selinux_netlbl_cache_free;
2258 secattr.cache->data = (void *)cache;
2260 cache->type = NETLBL_CACHE_T_MLS;
2261 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2262 &ctx->range.level[0].cat) != 0)
2263 goto netlbl_cache_add_return;
2264 cache->data.mls_label.level[1].cat.highbit =
2265 cache->data.mls_label.level[0].cat.highbit;
2266 cache->data.mls_label.level[1].cat.node =
2267 cache->data.mls_label.level[0].cat.node;
2268 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2269 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2271 netlbl_cache_add(skb, &secattr);
2273 netlbl_cache_add_return:
2274 netlbl_secattr_destroy(&secattr);
2278 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2281 * Invalidate the NetLabel security attribute mapping cache.
2284 void selinux_netlbl_cache_invalidate(void)
2286 netlbl_cache_invalidate();
2290 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2291 * @skb: the network packet
2292 * @secattr: the NetLabel packet security attributes
2293 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2294 * @sid: the SELinux SID
2297 * Convert the given NetLabel packet security attributes in @secattr into a
2298 * SELinux SID. If the @secattr field does not contain a full SELinux
2299 * SID/context then use the context in @base_sid as the foundation. If @skb
2300 * is not NULL attempt to cache as much data as possibile. Returns zero on
2301 * success, negative values on failure.
2304 static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
2305 struct netlbl_lsm_secattr *secattr,
2310 struct context *ctx;
2311 struct context ctx_new;
2312 struct netlbl_cache *cache;
2316 if (secattr->cache) {
2317 cache = NETLBL_CACHE(secattr->cache->data);
2318 switch (cache->type) {
2319 case NETLBL_CACHE_T_SID:
2320 *sid = cache->data.sid;
2323 case NETLBL_CACHE_T_MLS:
2324 ctx = sidtab_search(&sidtab, base_sid);
2326 goto netlbl_secattr_to_sid_return;
2328 ctx_new.user = ctx->user;
2329 ctx_new.role = ctx->role;
2330 ctx_new.type = ctx->type;
2331 ctx_new.range.level[0].sens =
2332 cache->data.mls_label.level[0].sens;
2333 ctx_new.range.level[0].cat.highbit =
2334 cache->data.mls_label.level[0].cat.highbit;
2335 ctx_new.range.level[0].cat.node =
2336 cache->data.mls_label.level[0].cat.node;
2337 ctx_new.range.level[1].sens =
2338 cache->data.mls_label.level[1].sens;
2339 ctx_new.range.level[1].cat.highbit =
2340 cache->data.mls_label.level[1].cat.highbit;
2341 ctx_new.range.level[1].cat.node =
2342 cache->data.mls_label.level[1].cat.node;
2344 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2347 goto netlbl_secattr_to_sid_return;
2349 } else if (secattr->mls_lvl_vld) {
2350 ctx = sidtab_search(&sidtab, base_sid);
2352 goto netlbl_secattr_to_sid_return;
2354 ctx_new.user = ctx->user;
2355 ctx_new.role = ctx->role;
2356 ctx_new.type = ctx->type;
2357 mls_import_lvl(&ctx_new, secattr->mls_lvl, secattr->mls_lvl);
2358 if (secattr->mls_cat) {
2359 if (mls_import_cat(&ctx_new,
2361 secattr->mls_cat_len,
2364 goto netlbl_secattr_to_sid_return;
2365 ctx_new.range.level[1].cat.highbit =
2366 ctx_new.range.level[0].cat.highbit;
2367 ctx_new.range.level[1].cat.node =
2368 ctx_new.range.level[0].cat.node;
2370 ebitmap_init(&ctx_new.range.level[0].cat);
2371 ebitmap_init(&ctx_new.range.level[1].cat);
2373 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2374 goto netlbl_secattr_to_sid_return_cleanup;
2376 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2378 goto netlbl_secattr_to_sid_return_cleanup;
2381 selinux_netlbl_cache_add(skb, &ctx_new);
2382 ebitmap_destroy(&ctx_new.range.level[0].cat);
2388 netlbl_secattr_to_sid_return:
2391 netlbl_secattr_to_sid_return_cleanup:
2392 ebitmap_destroy(&ctx_new.range.level[0].cat);
2393 goto netlbl_secattr_to_sid_return;
2397 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2399 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2403 * Call the NetLabel mechanism to get the security attributes of the given
2404 * packet and use those attributes to determine the correct context/SID to
2405 * assign to the packet. Returns zero on success, negative values on failure.
2408 static int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
2413 struct netlbl_lsm_secattr secattr;
2415 netlbl_secattr_init(&secattr);
2416 rc = netlbl_skbuff_getattr(skb, &secattr);
2418 rc = selinux_netlbl_secattr_to_sid(skb,
2422 netlbl_secattr_destroy(&secattr);
2428 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2429 * @sock: the socket to label
2430 * @sid: the SID to use
2433 * Attempt to label a socket using the NetLabel mechanism using the given
2434 * SID. Returns zero values on success, negative values on failure.
2437 static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
2440 struct sk_security_struct *sksec = sock->sk->sk_security;
2441 struct netlbl_lsm_secattr secattr;
2442 struct context *ctx;
2444 if (!ss_initialized)
2447 netlbl_secattr_init(&secattr);
2451 ctx = sidtab_search(&sidtab, sid);
2453 goto netlbl_socket_setsid_return;
2455 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2457 mls_export_lvl(ctx, &secattr.mls_lvl, NULL);
2458 secattr.mls_lvl_vld = 1;
2459 rc = mls_export_cat(ctx,
2461 &secattr.mls_cat_len,
2465 goto netlbl_socket_setsid_return;
2467 rc = netlbl_socket_setattr(sock, &secattr);
2469 sksec->nlbl_state = NLBL_LABELED;
2471 netlbl_socket_setsid_return:
2473 netlbl_secattr_destroy(&secattr);
2478 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2479 * @ssec: the sk_security_struct
2480 * @family: the socket family
2483 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2487 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
2490 if (family == PF_INET)
2491 ssec->nlbl_state = NLBL_REQUIRE;
2493 ssec->nlbl_state = NLBL_UNSET;
2497 * selinux_netlbl_sk_clone_security - Copy the NetLabel fields
2498 * @ssec: the original sk_security_struct
2499 * @newssec: the cloned sk_security_struct
2502 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2506 void selinux_netlbl_sk_clone_security(struct sk_security_struct *ssec,
2507 struct sk_security_struct *newssec)
2509 newssec->sclass = ssec->sclass;
2510 if (ssec->nlbl_state != NLBL_UNSET)
2511 newssec->nlbl_state = NLBL_REQUIRE;
2513 newssec->nlbl_state = NLBL_UNSET;
2517 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2518 * @sock: the socket to label
2519 * @sock_family: the socket family
2520 * @sid: the SID to use
2523 * Attempt to label a socket using the NetLabel mechanism using the given
2524 * SID. Returns zero values on success, negative values on failure.
2527 int selinux_netlbl_socket_post_create(struct socket *sock,
2531 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2532 struct sk_security_struct *sksec = sock->sk->sk_security;
2534 sksec->sclass = isec->sclass;
2536 if (sock_family != PF_INET)
2539 sksec->nlbl_state = NLBL_REQUIRE;
2540 return selinux_netlbl_socket_setsid(sock, sid);
2544 * selinux_netlbl_sock_graft - Netlabel the new socket
2545 * @sk: the new connection
2546 * @sock: the new socket
2549 * The connection represented by @sk is being grafted onto @sock so set the
2550 * socket's NetLabel to match the SID of @sk.
2553 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
2555 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2556 struct sk_security_struct *sksec = sk->sk_security;
2557 struct netlbl_lsm_secattr secattr;
2560 sksec->sclass = isec->sclass;
2562 if (sk->sk_family != PF_INET)
2565 netlbl_secattr_init(&secattr);
2566 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
2567 selinux_netlbl_secattr_to_sid(NULL,
2569 SECINITSID_UNLABELED,
2570 &nlbl_peer_sid) == 0)
2571 sksec->peer_sid = nlbl_peer_sid;
2572 netlbl_secattr_destroy(&secattr);
2574 sksec->nlbl_state = NLBL_REQUIRE;
2576 /* Try to set the NetLabel on the socket to save time later, if we fail
2577 * here we will pick up the pieces in later calls to
2578 * selinux_netlbl_inode_permission(). */
2579 selinux_netlbl_socket_setsid(sock, sksec->sid);
2583 * selinux_netlbl_inet_conn_request - Handle a new connection request
2585 * @sock_sid: the SID of the parent socket
2588 * If present, use the security attributes of the packet in @skb and the
2589 * parent sock's SID to arrive at a SID for the new child sock. Returns the
2590 * SID of the connection or SECSID_NULL on failure.
2593 u32 selinux_netlbl_inet_conn_request(struct sk_buff *skb, u32 sock_sid)
2598 rc = selinux_netlbl_skbuff_getsid(skb, sock_sid, &peer_sid);
2606 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
2607 * @inode: the file descriptor's inode
2608 * @mask: the permission mask
2611 * Looks at a file's inode and if it is marked as a socket protected by
2612 * NetLabel then verify that the socket has been labeled, if not try to label
2613 * the socket now with the inode's SID. Returns zero on success, negative
2614 * values on failure.
2617 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
2620 struct inode_security_struct *isec;
2621 struct sk_security_struct *sksec;
2622 struct socket *sock;
2624 if (!S_ISSOCK(inode->i_mode))
2627 sock = SOCKET_I(inode);
2628 isec = inode->i_security;
2629 sksec = sock->sk->sk_security;
2630 mutex_lock(&isec->lock);
2631 if (unlikely(sksec->nlbl_state == NLBL_REQUIRE &&
2632 (mask & (MAY_WRITE | MAY_APPEND)))) {
2633 lock_sock(sock->sk);
2634 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2635 release_sock(sock->sk);
2638 mutex_unlock(&isec->lock);
2644 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2645 * @sksec: the sock's sk_security_struct
2647 * @ad: the audit data
2650 * Fetch the NetLabel security attributes from @skb and perform an access check
2651 * against the receiving socket. Returns zero on success, negative values on
2655 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
2656 struct sk_buff *skb,
2657 struct avc_audit_data *ad)
2663 rc = selinux_netlbl_skbuff_getsid(skb,
2664 SECINITSID_UNLABELED,
2669 if (netlbl_sid == SECSID_NULL)
2672 switch (sksec->sclass) {
2673 case SECCLASS_UDP_SOCKET:
2674 recv_perm = UDP_SOCKET__RECVFROM;
2676 case SECCLASS_TCP_SOCKET:
2677 recv_perm = TCP_SOCKET__RECVFROM;
2680 recv_perm = RAWIP_SOCKET__RECVFROM;
2683 rc = avc_has_perm(sksec->sid,
2691 netlbl_skbuff_err(skb, rc);
2696 * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID
2700 * Examine @sock to find the connected peer's SID. Returns the SID on success
2701 * or SECSID_NULL on error.
2704 u32 selinux_netlbl_socket_getpeersec_stream(struct socket *sock)
2706 struct sk_security_struct *sksec = sock->sk->sk_security;
2707 return sksec->peer_sid;
2711 * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet
2715 * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on
2716 * success, SECSID_NULL on error.
2719 u32 selinux_netlbl_socket_getpeersec_dgram(struct sk_buff *skb)
2723 if (selinux_netlbl_skbuff_getsid(skb,
2724 SECINITSID_UNLABELED,
2732 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
2734 * @level: the socket level or protocol
2735 * @optname: the socket option name
2738 * Check the setsockopt() call and if the user is trying to replace the IP
2739 * options on a socket and a NetLabel is in place for the socket deny the
2740 * access; otherwise allow the access. Returns zero when the access is
2741 * allowed, -EACCES when denied, and other negative values on error.
2744 int selinux_netlbl_socket_setsockopt(struct socket *sock,
2749 struct inode *inode = SOCK_INODE(sock);
2750 struct sk_security_struct *sksec = sock->sk->sk_security;
2751 struct inode_security_struct *isec = inode->i_security;
2752 struct netlbl_lsm_secattr secattr;
2754 mutex_lock(&isec->lock);
2755 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
2756 sksec->nlbl_state == NLBL_LABELED) {
2757 netlbl_secattr_init(&secattr);
2758 rc = netlbl_socket_getattr(sock, &secattr);
2759 if (rc == 0 && (secattr.cache || secattr.mls_lvl_vld))
2761 netlbl_secattr_destroy(&secattr);
2763 mutex_unlock(&isec->lock);
2767 #endif /* CONFIG_NETLABEL */