2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
20 * Updated: Chad Sellers <csellers@tresys.com>
22 * Added validation of kernel classes and permissions
24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/spinlock.h>
36 #include <linux/rcupdate.h>
37 #include <linux/errno.h>
39 #include <linux/sched.h>
40 #include <linux/audit.h>
41 #include <linux/mutex.h>
43 #include <net/netlabel.h>
53 #include "conditional.h"
56 #include "selinux_netlabel.h"
60 extern void selnl_notify_policyload(u32 seqno);
61 unsigned int policydb_loaded_version;
64 * This is declared in avc.c
66 extern const struct selinux_class_perm selinux_class_perm;
68 static DEFINE_RWLOCK(policy_rwlock);
69 #define POLICY_RDLOCK read_lock(&policy_rwlock)
70 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
71 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
72 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
74 static DEFINE_MUTEX(load_mutex);
75 #define LOAD_LOCK mutex_lock(&load_mutex)
76 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
78 static struct sidtab sidtab;
79 struct policydb policydb;
80 int ss_initialized = 0;
83 * The largest sequence number that has been used when
84 * providing an access decision to the access vector cache.
85 * The sequence number only changes when a policy change
88 static u32 latest_granting = 0;
90 /* Forward declaration. */
91 static int context_struct_to_string(struct context *context, char **scontext,
95 * Return the boolean value of a constraint expression
96 * when it is applied to the specified source and target
99 * xcontext is a special beast... It is used by the validatetrans rules
100 * only. For these rules, scontext is the context before the transition,
101 * tcontext is the context after the transition, and xcontext is the context
102 * of the process performing the transition. All other callers of
103 * constraint_expr_eval should pass in NULL for xcontext.
105 static int constraint_expr_eval(struct context *scontext,
106 struct context *tcontext,
107 struct context *xcontext,
108 struct constraint_expr *cexpr)
112 struct role_datum *r1, *r2;
113 struct mls_level *l1, *l2;
114 struct constraint_expr *e;
115 int s[CEXPR_MAXDEPTH];
118 for (e = cexpr; e; e = e->next) {
119 switch (e->expr_type) {
135 if (sp == (CEXPR_MAXDEPTH-1))
139 val1 = scontext->user;
140 val2 = tcontext->user;
143 val1 = scontext->type;
144 val2 = tcontext->type;
147 val1 = scontext->role;
148 val2 = tcontext->role;
149 r1 = policydb.role_val_to_struct[val1 - 1];
150 r2 = policydb.role_val_to_struct[val2 - 1];
153 s[++sp] = ebitmap_get_bit(&r1->dominates,
157 s[++sp] = ebitmap_get_bit(&r2->dominates,
161 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
163 !ebitmap_get_bit(&r2->dominates,
171 l1 = &(scontext->range.level[0]);
172 l2 = &(tcontext->range.level[0]);
175 l1 = &(scontext->range.level[0]);
176 l2 = &(tcontext->range.level[1]);
179 l1 = &(scontext->range.level[1]);
180 l2 = &(tcontext->range.level[0]);
183 l1 = &(scontext->range.level[1]);
184 l2 = &(tcontext->range.level[1]);
187 l1 = &(scontext->range.level[0]);
188 l2 = &(scontext->range.level[1]);
191 l1 = &(tcontext->range.level[0]);
192 l2 = &(tcontext->range.level[1]);
197 s[++sp] = mls_level_eq(l1, l2);
200 s[++sp] = !mls_level_eq(l1, l2);
203 s[++sp] = mls_level_dom(l1, l2);
206 s[++sp] = mls_level_dom(l2, l1);
209 s[++sp] = mls_level_incomp(l2, l1);
223 s[++sp] = (val1 == val2);
226 s[++sp] = (val1 != val2);
234 if (sp == (CEXPR_MAXDEPTH-1))
237 if (e->attr & CEXPR_TARGET)
239 else if (e->attr & CEXPR_XTARGET) {
246 if (e->attr & CEXPR_USER)
248 else if (e->attr & CEXPR_ROLE)
250 else if (e->attr & CEXPR_TYPE)
259 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
262 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
280 * Compute access vectors based on a context structure pair for
281 * the permissions in a particular class.
283 static int context_struct_compute_av(struct context *scontext,
284 struct context *tcontext,
287 struct av_decision *avd)
289 struct constraint_node *constraint;
290 struct role_allow *ra;
291 struct avtab_key avkey;
292 struct avtab_node *node;
293 struct class_datum *tclass_datum;
294 struct ebitmap *sattr, *tattr;
295 struct ebitmap_node *snode, *tnode;
299 * Remap extended Netlink classes for old policy versions.
300 * Do this here rather than socket_type_to_security_class()
301 * in case a newer policy version is loaded, allowing sockets
302 * to remain in the correct class.
304 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
305 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
306 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
307 tclass = SECCLASS_NETLINK_SOCKET;
309 if (!tclass || tclass > policydb.p_classes.nprim) {
310 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
314 tclass_datum = policydb.class_val_to_struct[tclass - 1];
317 * Initialize the access vectors to the default values.
320 avd->decided = 0xffffffff;
322 avd->auditdeny = 0xffffffff;
323 avd->seqno = latest_granting;
326 * If a specific type enforcement rule was defined for
327 * this permission check, then use it.
329 avkey.target_class = tclass;
330 avkey.specified = AVTAB_AV;
331 sattr = &policydb.type_attr_map[scontext->type - 1];
332 tattr = &policydb.type_attr_map[tcontext->type - 1];
333 ebitmap_for_each_bit(sattr, snode, i) {
334 if (!ebitmap_node_get_bit(snode, i))
336 ebitmap_for_each_bit(tattr, tnode, j) {
337 if (!ebitmap_node_get_bit(tnode, j))
339 avkey.source_type = i + 1;
340 avkey.target_type = j + 1;
341 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
343 node = avtab_search_node_next(node, avkey.specified)) {
344 if (node->key.specified == AVTAB_ALLOWED)
345 avd->allowed |= node->datum.data;
346 else if (node->key.specified == AVTAB_AUDITALLOW)
347 avd->auditallow |= node->datum.data;
348 else if (node->key.specified == AVTAB_AUDITDENY)
349 avd->auditdeny &= node->datum.data;
352 /* Check conditional av table for additional permissions */
353 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
359 * Remove any permissions prohibited by a constraint (this includes
362 constraint = tclass_datum->constraints;
364 if ((constraint->permissions & (avd->allowed)) &&
365 !constraint_expr_eval(scontext, tcontext, NULL,
367 avd->allowed = (avd->allowed) & ~(constraint->permissions);
369 constraint = constraint->next;
373 * If checking process transition permission and the
374 * role is changing, then check the (current_role, new_role)
377 if (tclass == SECCLASS_PROCESS &&
378 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
379 scontext->role != tcontext->role) {
380 for (ra = policydb.role_allow; ra; ra = ra->next) {
381 if (scontext->role == ra->role &&
382 tcontext->role == ra->new_role)
386 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
387 PROCESS__DYNTRANSITION);
393 static int security_validtrans_handle_fail(struct context *ocontext,
394 struct context *ncontext,
395 struct context *tcontext,
398 char *o = NULL, *n = NULL, *t = NULL;
399 u32 olen, nlen, tlen;
401 if (context_struct_to_string(ocontext, &o, &olen) < 0)
403 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
405 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
407 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
408 "security_validate_transition: denied for"
409 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
410 o, n, t, policydb.p_class_val_to_name[tclass-1]);
416 if (!selinux_enforcing)
421 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
424 struct context *ocontext;
425 struct context *ncontext;
426 struct context *tcontext;
427 struct class_datum *tclass_datum;
428 struct constraint_node *constraint;
437 * Remap extended Netlink classes for old policy versions.
438 * Do this here rather than socket_type_to_security_class()
439 * in case a newer policy version is loaded, allowing sockets
440 * to remain in the correct class.
442 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
443 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
444 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
445 tclass = SECCLASS_NETLINK_SOCKET;
447 if (!tclass || tclass > policydb.p_classes.nprim) {
448 printk(KERN_ERR "security_validate_transition: "
449 "unrecognized class %d\n", tclass);
453 tclass_datum = policydb.class_val_to_struct[tclass - 1];
455 ocontext = sidtab_search(&sidtab, oldsid);
457 printk(KERN_ERR "security_validate_transition: "
458 " unrecognized SID %d\n", oldsid);
463 ncontext = sidtab_search(&sidtab, newsid);
465 printk(KERN_ERR "security_validate_transition: "
466 " unrecognized SID %d\n", newsid);
471 tcontext = sidtab_search(&sidtab, tasksid);
473 printk(KERN_ERR "security_validate_transition: "
474 " unrecognized SID %d\n", tasksid);
479 constraint = tclass_datum->validatetrans;
481 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
483 rc = security_validtrans_handle_fail(ocontext, ncontext,
487 constraint = constraint->next;
496 * security_compute_av - Compute access vector decisions.
497 * @ssid: source security identifier
498 * @tsid: target security identifier
499 * @tclass: target security class
500 * @requested: requested permissions
501 * @avd: access vector decisions
503 * Compute a set of access vector decisions based on the
504 * SID pair (@ssid, @tsid) for the permissions in @tclass.
505 * Return -%EINVAL if any of the parameters are invalid or %0
506 * if the access vector decisions were computed successfully.
508 int security_compute_av(u32 ssid,
512 struct av_decision *avd)
514 struct context *scontext = NULL, *tcontext = NULL;
517 if (!ss_initialized) {
518 avd->allowed = 0xffffffff;
519 avd->decided = 0xffffffff;
521 avd->auditdeny = 0xffffffff;
522 avd->seqno = latest_granting;
528 scontext = sidtab_search(&sidtab, ssid);
530 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
535 tcontext = sidtab_search(&sidtab, tsid);
537 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
543 rc = context_struct_compute_av(scontext, tcontext, tclass,
551 * Write the security context string representation of
552 * the context structure `context' into a dynamically
553 * allocated string of the correct size. Set `*scontext'
554 * to point to this string and set `*scontext_len' to
555 * the length of the string.
557 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
564 /* Compute the size of the context. */
565 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
566 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
567 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
568 *scontext_len += mls_compute_context_len(context);
570 /* Allocate space for the context; caller must free this space. */
571 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
575 *scontext = scontextp;
578 * Copy the user name, role name and type name into the context.
580 sprintf(scontextp, "%s:%s:%s",
581 policydb.p_user_val_to_name[context->user - 1],
582 policydb.p_role_val_to_name[context->role - 1],
583 policydb.p_type_val_to_name[context->type - 1]);
584 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
585 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
586 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
588 mls_sid_to_context(context, &scontextp);
595 #include "initial_sid_to_string.h"
598 * security_sid_to_context - Obtain a context for a given SID.
599 * @sid: security identifier, SID
600 * @scontext: security context
601 * @scontext_len: length in bytes
603 * Write the string representation of the context associated with @sid
604 * into a dynamically allocated string of the correct size. Set @scontext
605 * to point to this string and set @scontext_len to the length of the string.
607 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
609 struct context *context;
612 if (!ss_initialized) {
613 if (sid <= SECINITSID_NUM) {
616 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
617 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
622 strcpy(scontextp, initial_sid_to_string[sid]);
623 *scontext = scontextp;
626 printk(KERN_ERR "security_sid_to_context: called before initial "
627 "load_policy on unknown SID %d\n", sid);
632 context = sidtab_search(&sidtab, sid);
634 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
639 rc = context_struct_to_string(context, scontext, scontext_len);
647 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
650 struct context context;
651 struct role_datum *role;
652 struct type_datum *typdatum;
653 struct user_datum *usrdatum;
654 char *scontextp, *p, oldc;
657 if (!ss_initialized) {
660 for (i = 1; i < SECINITSID_NUM; i++) {
661 if (!strcmp(initial_sid_to_string[i], scontext)) {
666 *sid = SECINITSID_KERNEL;
671 /* Copy the string so that we can modify the copy as we parse it.
672 The string should already by null terminated, but we append a
673 null suffix to the copy to avoid problems with the existing
674 attr package, which doesn't view the null terminator as part
675 of the attribute value. */
676 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
681 memcpy(scontext2, scontext, scontext_len);
682 scontext2[scontext_len] = 0;
684 context_init(&context);
689 /* Parse the security context. */
692 scontextp = (char *) scontext2;
694 /* Extract the user. */
696 while (*p && *p != ':')
704 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
708 context.user = usrdatum->value;
712 while (*p && *p != ':')
720 role = hashtab_search(policydb.p_roles.table, scontextp);
723 context.role = role->value;
727 while (*p && *p != ':')
732 typdatum = hashtab_search(policydb.p_types.table, scontextp);
736 context.type = typdatum->value;
738 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
742 if ((p - scontext2) < scontext_len) {
747 /* Check the validity of the new context. */
748 if (!policydb_context_isvalid(&policydb, &context)) {
752 /* Obtain the new sid. */
753 rc = sidtab_context_to_sid(&sidtab, &context, sid);
756 context_destroy(&context);
763 * security_context_to_sid - Obtain a SID for a given security context.
764 * @scontext: security context
765 * @scontext_len: length in bytes
766 * @sid: security identifier, SID
768 * Obtains a SID associated with the security context that
769 * has the string representation specified by @scontext.
770 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
771 * memory is available, or 0 on success.
773 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
775 return security_context_to_sid_core(scontext, scontext_len,
780 * security_context_to_sid_default - Obtain a SID for a given security context,
781 * falling back to specified default if needed.
783 * @scontext: security context
784 * @scontext_len: length in bytes
785 * @sid: security identifier, SID
786 * @def_sid: default SID to assign on errror
788 * Obtains a SID associated with the security context that
789 * has the string representation specified by @scontext.
790 * The default SID is passed to the MLS layer to be used to allow
791 * kernel labeling of the MLS field if the MLS field is not present
792 * (for upgrading to MLS without full relabel).
793 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
794 * memory is available, or 0 on success.
796 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
798 return security_context_to_sid_core(scontext, scontext_len,
802 static int compute_sid_handle_invalid_context(
803 struct context *scontext,
804 struct context *tcontext,
806 struct context *newcontext)
808 char *s = NULL, *t = NULL, *n = NULL;
809 u32 slen, tlen, nlen;
811 if (context_struct_to_string(scontext, &s, &slen) < 0)
813 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
815 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
817 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
818 "security_compute_sid: invalid context %s"
822 n, s, t, policydb.p_class_val_to_name[tclass-1]);
827 if (!selinux_enforcing)
832 static int security_compute_sid(u32 ssid,
838 struct context *scontext = NULL, *tcontext = NULL, newcontext;
839 struct role_trans *roletr = NULL;
840 struct avtab_key avkey;
841 struct avtab_datum *avdatum;
842 struct avtab_node *node;
845 if (!ss_initialized) {
847 case SECCLASS_PROCESS:
857 context_init(&newcontext);
861 scontext = sidtab_search(&sidtab, ssid);
863 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
868 tcontext = sidtab_search(&sidtab, tsid);
870 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
876 /* Set the user identity. */
878 case AVTAB_TRANSITION:
880 /* Use the process user identity. */
881 newcontext.user = scontext->user;
884 /* Use the related object owner. */
885 newcontext.user = tcontext->user;
889 /* Set the role and type to default values. */
891 case SECCLASS_PROCESS:
892 /* Use the current role and type of process. */
893 newcontext.role = scontext->role;
894 newcontext.type = scontext->type;
897 /* Use the well-defined object role. */
898 newcontext.role = OBJECT_R_VAL;
899 /* Use the type of the related object. */
900 newcontext.type = tcontext->type;
903 /* Look for a type transition/member/change rule. */
904 avkey.source_type = scontext->type;
905 avkey.target_type = tcontext->type;
906 avkey.target_class = tclass;
907 avkey.specified = specified;
908 avdatum = avtab_search(&policydb.te_avtab, &avkey);
910 /* If no permanent rule, also check for enabled conditional rules */
912 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
913 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
914 if (node->key.specified & AVTAB_ENABLED) {
915 avdatum = &node->datum;
922 /* Use the type from the type transition/member/change rule. */
923 newcontext.type = avdatum->data;
926 /* Check for class-specific changes. */
928 case SECCLASS_PROCESS:
929 if (specified & AVTAB_TRANSITION) {
930 /* Look for a role transition rule. */
931 for (roletr = policydb.role_tr; roletr;
932 roletr = roletr->next) {
933 if (roletr->role == scontext->role &&
934 roletr->type == tcontext->type) {
935 /* Use the role transition rule. */
936 newcontext.role = roletr->new_role;
946 /* Set the MLS attributes.
947 This is done last because it may allocate memory. */
948 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
952 /* Check the validity of the context. */
953 if (!policydb_context_isvalid(&policydb, &newcontext)) {
954 rc = compute_sid_handle_invalid_context(scontext,
961 /* Obtain the sid for the context. */
962 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
965 context_destroy(&newcontext);
971 * security_transition_sid - Compute the SID for a new subject/object.
972 * @ssid: source security identifier
973 * @tsid: target security identifier
974 * @tclass: target security class
975 * @out_sid: security identifier for new subject/object
977 * Compute a SID to use for labeling a new subject or object in the
978 * class @tclass based on a SID pair (@ssid, @tsid).
979 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
980 * if insufficient memory is available, or %0 if the new SID was
981 * computed successfully.
983 int security_transition_sid(u32 ssid,
988 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
992 * security_member_sid - Compute the SID for member selection.
993 * @ssid: source security identifier
994 * @tsid: target security identifier
995 * @tclass: target security class
996 * @out_sid: security identifier for selected member
998 * Compute a SID to use when selecting a member of a polyinstantiated
999 * object of class @tclass based on a SID pair (@ssid, @tsid).
1000 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1001 * if insufficient memory is available, or %0 if the SID was
1002 * computed successfully.
1004 int security_member_sid(u32 ssid,
1009 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1013 * security_change_sid - Compute the SID for object relabeling.
1014 * @ssid: source security identifier
1015 * @tsid: target security identifier
1016 * @tclass: target security class
1017 * @out_sid: security identifier for selected member
1019 * Compute a SID to use for relabeling an object of class @tclass
1020 * based on a SID pair (@ssid, @tsid).
1021 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1022 * if insufficient memory is available, or %0 if the SID was
1023 * computed successfully.
1025 int security_change_sid(u32 ssid,
1030 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1034 * Verify that each kernel class that is defined in the
1037 static int validate_classes(struct policydb *p)
1040 struct class_datum *cladatum;
1041 struct perm_datum *perdatum;
1042 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1044 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1045 const char *def_class, *def_perm, *pol_class;
1046 struct symtab *perms;
1048 for (i = 1; i < kdefs->cts_len; i++) {
1049 def_class = kdefs->class_to_string[i];
1050 if (i > p->p_classes.nprim) {
1052 "security: class %s not defined in policy\n",
1056 pol_class = p->p_class_val_to_name[i-1];
1057 if (strcmp(pol_class, def_class)) {
1059 "security: class %d is incorrect, found %s but should be %s\n",
1060 i, pol_class, def_class);
1064 for (i = 0; i < kdefs->av_pts_len; i++) {
1065 class_val = kdefs->av_perm_to_string[i].tclass;
1066 perm_val = kdefs->av_perm_to_string[i].value;
1067 def_perm = kdefs->av_perm_to_string[i].name;
1068 if (class_val > p->p_classes.nprim)
1070 pol_class = p->p_class_val_to_name[class_val-1];
1071 cladatum = hashtab_search(p->p_classes.table, pol_class);
1073 perms = &cladatum->permissions;
1074 nprim = 1 << (perms->nprim - 1);
1075 if (perm_val > nprim) {
1077 "security: permission %s in class %s not defined in policy\n",
1078 def_perm, pol_class);
1081 perdatum = hashtab_search(perms->table, def_perm);
1082 if (perdatum == NULL) {
1084 "security: permission %s in class %s not found in policy\n",
1085 def_perm, pol_class);
1088 pol_val = 1 << (perdatum->value - 1);
1089 if (pol_val != perm_val) {
1091 "security: permission %s in class %s has incorrect value\n",
1092 def_perm, pol_class);
1096 for (i = 0; i < kdefs->av_inherit_len; i++) {
1097 class_val = kdefs->av_inherit[i].tclass;
1098 if (class_val > p->p_classes.nprim)
1100 pol_class = p->p_class_val_to_name[class_val-1];
1101 cladatum = hashtab_search(p->p_classes.table, pol_class);
1103 if (!cladatum->comdatum) {
1105 "security: class %s should have an inherits clause but does not\n",
1109 tmp = kdefs->av_inherit[i].common_base;
1111 while (!(tmp & 0x01)) {
1115 perms = &cladatum->comdatum->permissions;
1116 for (j = 0; j < common_pts_len; j++) {
1117 def_perm = kdefs->av_inherit[i].common_pts[j];
1118 if (j >= perms->nprim) {
1120 "security: permission %s in class %s not defined in policy\n",
1121 def_perm, pol_class);
1124 perdatum = hashtab_search(perms->table, def_perm);
1125 if (perdatum == NULL) {
1127 "security: permission %s in class %s not found in policy\n",
1128 def_perm, pol_class);
1131 if (perdatum->value != j + 1) {
1133 "security: permission %s in class %s has incorrect value\n",
1134 def_perm, pol_class);
1142 /* Clone the SID into the new SID table. */
1143 static int clone_sid(u32 sid,
1144 struct context *context,
1147 struct sidtab *s = arg;
1149 return sidtab_insert(s, sid, context);
1152 static inline int convert_context_handle_invalid_context(struct context *context)
1156 if (selinux_enforcing) {
1162 context_struct_to_string(context, &s, &len);
1163 printk(KERN_ERR "security: context %s is invalid\n", s);
1169 struct convert_context_args {
1170 struct policydb *oldp;
1171 struct policydb *newp;
1175 * Convert the values in the security context
1176 * structure `c' from the values specified
1177 * in the policy `p->oldp' to the values specified
1178 * in the policy `p->newp'. Verify that the
1179 * context is valid under the new policy.
1181 static int convert_context(u32 key,
1185 struct convert_context_args *args;
1186 struct context oldc;
1187 struct role_datum *role;
1188 struct type_datum *typdatum;
1189 struct user_datum *usrdatum;
1196 rc = context_cpy(&oldc, c);
1202 /* Convert the user. */
1203 usrdatum = hashtab_search(args->newp->p_users.table,
1204 args->oldp->p_user_val_to_name[c->user - 1]);
1208 c->user = usrdatum->value;
1210 /* Convert the role. */
1211 role = hashtab_search(args->newp->p_roles.table,
1212 args->oldp->p_role_val_to_name[c->role - 1]);
1216 c->role = role->value;
1218 /* Convert the type. */
1219 typdatum = hashtab_search(args->newp->p_types.table,
1220 args->oldp->p_type_val_to_name[c->type - 1]);
1224 c->type = typdatum->value;
1226 rc = mls_convert_context(args->oldp, args->newp, c);
1230 /* Check the validity of the new context. */
1231 if (!policydb_context_isvalid(args->newp, c)) {
1232 rc = convert_context_handle_invalid_context(&oldc);
1237 context_destroy(&oldc);
1241 context_struct_to_string(&oldc, &s, &len);
1242 context_destroy(&oldc);
1243 printk(KERN_ERR "security: invalidating context %s\n", s);
1248 extern void selinux_complete_init(void);
1251 * security_load_policy - Load a security policy configuration.
1252 * @data: binary policy data
1253 * @len: length of data in bytes
1255 * Load a new set of security policy configuration data,
1256 * validate it and convert the SID table as necessary.
1257 * This function will flush the access vector cache after
1258 * loading the new policy.
1260 int security_load_policy(void *data, size_t len)
1262 struct policydb oldpolicydb, newpolicydb;
1263 struct sidtab oldsidtab, newsidtab;
1264 struct convert_context_args args;
1267 struct policy_file file = { data, len }, *fp = &file;
1271 if (!ss_initialized) {
1273 if (policydb_read(&policydb, fp)) {
1275 avtab_cache_destroy();
1278 if (policydb_load_isids(&policydb, &sidtab)) {
1280 policydb_destroy(&policydb);
1281 avtab_cache_destroy();
1284 /* Verify that the kernel defined classes are correct. */
1285 if (validate_classes(&policydb)) {
1287 "security: the definition of a class is incorrect\n");
1289 sidtab_destroy(&sidtab);
1290 policydb_destroy(&policydb);
1291 avtab_cache_destroy();
1294 policydb_loaded_version = policydb.policyvers;
1296 seqno = ++latest_granting;
1298 selinux_complete_init();
1299 avc_ss_reset(seqno);
1300 selnl_notify_policyload(seqno);
1301 selinux_netlbl_cache_invalidate();
1306 sidtab_hash_eval(&sidtab, "sids");
1309 if (policydb_read(&newpolicydb, fp)) {
1314 sidtab_init(&newsidtab);
1316 /* Verify that the kernel defined classes are correct. */
1317 if (validate_classes(&newpolicydb)) {
1319 "security: the definition of a class is incorrect\n");
1324 /* Clone the SID table. */
1325 sidtab_shutdown(&sidtab);
1326 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1331 /* Convert the internal representations of contexts
1332 in the new SID table and remove invalid SIDs. */
1333 args.oldp = &policydb;
1334 args.newp = &newpolicydb;
1335 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1337 /* Save the old policydb and SID table to free later. */
1338 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1339 sidtab_set(&oldsidtab, &sidtab);
1341 /* Install the new policydb and SID table. */
1343 memcpy(&policydb, &newpolicydb, sizeof policydb);
1344 sidtab_set(&sidtab, &newsidtab);
1345 seqno = ++latest_granting;
1346 policydb_loaded_version = policydb.policyvers;
1350 /* Free the old policydb and SID table. */
1351 policydb_destroy(&oldpolicydb);
1352 sidtab_destroy(&oldsidtab);
1354 avc_ss_reset(seqno);
1355 selnl_notify_policyload(seqno);
1356 selinux_netlbl_cache_invalidate();
1362 sidtab_destroy(&newsidtab);
1363 policydb_destroy(&newpolicydb);
1369 * security_port_sid - Obtain the SID for a port.
1370 * @domain: communication domain aka address family
1371 * @type: socket type
1372 * @protocol: protocol number
1373 * @port: port number
1374 * @out_sid: security identifier
1376 int security_port_sid(u16 domain,
1387 c = policydb.ocontexts[OCON_PORT];
1389 if (c->u.port.protocol == protocol &&
1390 c->u.port.low_port <= port &&
1391 c->u.port.high_port >= port)
1398 rc = sidtab_context_to_sid(&sidtab,
1404 *out_sid = c->sid[0];
1406 *out_sid = SECINITSID_PORT;
1415 * security_netif_sid - Obtain the SID for a network interface.
1416 * @name: interface name
1417 * @if_sid: interface SID
1418 * @msg_sid: default SID for received packets
1420 int security_netif_sid(char *name,
1429 c = policydb.ocontexts[OCON_NETIF];
1431 if (strcmp(name, c->u.name) == 0)
1437 if (!c->sid[0] || !c->sid[1]) {
1438 rc = sidtab_context_to_sid(&sidtab,
1443 rc = sidtab_context_to_sid(&sidtab,
1449 *if_sid = c->sid[0];
1450 *msg_sid = c->sid[1];
1452 *if_sid = SECINITSID_NETIF;
1453 *msg_sid = SECINITSID_NETMSG;
1461 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1465 for(i = 0; i < 4; i++)
1466 if(addr[i] != (input[i] & mask[i])) {
1475 * security_node_sid - Obtain the SID for a node (host).
1476 * @domain: communication domain aka address family
1478 * @addrlen: address length in bytes
1479 * @out_sid: security identifier
1481 int security_node_sid(u16 domain,
1495 if (addrlen != sizeof(u32)) {
1500 addr = *((u32 *)addrp);
1502 c = policydb.ocontexts[OCON_NODE];
1504 if (c->u.node.addr == (addr & c->u.node.mask))
1512 if (addrlen != sizeof(u64) * 2) {
1516 c = policydb.ocontexts[OCON_NODE6];
1518 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1526 *out_sid = SECINITSID_NODE;
1532 rc = sidtab_context_to_sid(&sidtab,
1538 *out_sid = c->sid[0];
1540 *out_sid = SECINITSID_NODE;
1551 * security_get_user_sids - Obtain reachable SIDs for a user.
1552 * @fromsid: starting SID
1553 * @username: username
1554 * @sids: array of reachable SIDs for user
1555 * @nel: number of elements in @sids
1557 * Generate the set of SIDs for legal security contexts
1558 * for a given user that can be reached by @fromsid.
1559 * Set *@sids to point to a dynamically allocated
1560 * array containing the set of SIDs. Set *@nel to the
1561 * number of elements in the array.
1564 int security_get_user_sids(u32 fromsid,
1569 struct context *fromcon, usercon;
1570 u32 *mysids, *mysids2, sid;
1571 u32 mynel = 0, maxnel = SIDS_NEL;
1572 struct user_datum *user;
1573 struct role_datum *role;
1574 struct av_decision avd;
1575 struct ebitmap_node *rnode, *tnode;
1578 if (!ss_initialized) {
1586 fromcon = sidtab_search(&sidtab, fromsid);
1592 user = hashtab_search(policydb.p_users.table, username);
1597 usercon.user = user->value;
1599 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1605 ebitmap_for_each_bit(&user->roles, rnode, i) {
1606 if (!ebitmap_node_get_bit(rnode, i))
1608 role = policydb.role_val_to_struct[i];
1610 ebitmap_for_each_bit(&role->types, tnode, j) {
1611 if (!ebitmap_node_get_bit(tnode, j))
1615 if (mls_setup_user_range(fromcon, user, &usercon))
1618 rc = context_struct_compute_av(fromcon, &usercon,
1620 PROCESS__TRANSITION,
1622 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1624 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1629 if (mynel < maxnel) {
1630 mysids[mynel++] = sid;
1633 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1639 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1642 mysids[mynel++] = sid;
1657 * security_genfs_sid - Obtain a SID for a file in a filesystem
1658 * @fstype: filesystem type
1659 * @path: path from root of mount
1660 * @sclass: file security class
1661 * @sid: SID for path
1663 * Obtain a SID to use for a file in a filesystem that
1664 * cannot support xattr or use a fixed labeling behavior like
1665 * transition SIDs or task SIDs.
1667 int security_genfs_sid(const char *fstype,
1673 struct genfs *genfs;
1675 int rc = 0, cmp = 0;
1679 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1680 cmp = strcmp(fstype, genfs->fstype);
1685 if (!genfs || cmp) {
1686 *sid = SECINITSID_UNLABELED;
1691 for (c = genfs->head; c; c = c->next) {
1692 len = strlen(c->u.name);
1693 if ((!c->v.sclass || sclass == c->v.sclass) &&
1694 (strncmp(c->u.name, path, len) == 0))
1699 *sid = SECINITSID_UNLABELED;
1705 rc = sidtab_context_to_sid(&sidtab,
1719 * security_fs_use - Determine how to handle labeling for a filesystem.
1720 * @fstype: filesystem type
1721 * @behavior: labeling behavior
1722 * @sid: SID for filesystem (superblock)
1724 int security_fs_use(
1726 unsigned int *behavior,
1734 c = policydb.ocontexts[OCON_FSUSE];
1736 if (strcmp(fstype, c->u.name) == 0)
1742 *behavior = c->v.behavior;
1744 rc = sidtab_context_to_sid(&sidtab,
1752 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1754 *behavior = SECURITY_FS_USE_NONE;
1757 *behavior = SECURITY_FS_USE_GENFS;
1766 int security_get_bools(int *len, char ***names, int **values)
1768 int i, rc = -ENOMEM;
1774 *len = policydb.p_bools.nprim;
1780 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1784 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1788 for (i = 0; i < *len; i++) {
1790 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1791 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1792 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1795 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1796 (*names)[i][name_len - 1] = 0;
1804 for (i = 0; i < *len; i++)
1812 int security_set_bools(int len, int *values)
1815 int lenp, seqno = 0;
1816 struct cond_node *cur;
1820 lenp = policydb.p_bools.nprim;
1826 for (i = 0; i < len; i++) {
1827 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1828 audit_log(current->audit_context, GFP_ATOMIC,
1829 AUDIT_MAC_CONFIG_CHANGE,
1830 "bool=%s val=%d old_val=%d auid=%u",
1831 policydb.p_bool_val_to_name[i],
1833 policydb.bool_val_to_struct[i]->state,
1834 audit_get_loginuid(current->audit_context));
1837 policydb.bool_val_to_struct[i]->state = 1;
1839 policydb.bool_val_to_struct[i]->state = 0;
1843 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1844 rc = evaluate_cond_node(&policydb, cur);
1849 seqno = ++latest_granting;
1854 avc_ss_reset(seqno);
1855 selnl_notify_policyload(seqno);
1860 int security_get_bool_value(int bool)
1867 len = policydb.p_bools.nprim;
1873 rc = policydb.bool_val_to_struct[bool]->state;
1880 * security_sid_mls_copy() - computes a new sid based on the given
1881 * sid and the mls portion of mls_sid.
1883 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1885 struct context *context1;
1886 struct context *context2;
1887 struct context newcon;
1892 if (!ss_initialized || !selinux_mls_enabled) {
1897 context_init(&newcon);
1900 context1 = sidtab_search(&sidtab, sid);
1902 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1908 context2 = sidtab_search(&sidtab, mls_sid);
1910 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1916 newcon.user = context1->user;
1917 newcon.role = context1->role;
1918 newcon.type = context1->type;
1919 rc = mls_copy_context(&newcon, context2);
1924 /* Check the validity of the new context. */
1925 if (!policydb_context_isvalid(&policydb, &newcon)) {
1926 rc = convert_context_handle_invalid_context(&newcon);
1931 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1935 if (!context_struct_to_string(&newcon, &s, &len)) {
1936 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1937 "security_sid_mls_copy: invalid context %s", s);
1943 context_destroy(&newcon);
1948 struct selinux_audit_rule {
1950 struct context au_ctxt;
1953 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1956 context_destroy(&rule->au_ctxt);
1961 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1962 struct selinux_audit_rule **rule)
1964 struct selinux_audit_rule *tmprule;
1965 struct role_datum *roledatum;
1966 struct type_datum *typedatum;
1967 struct user_datum *userdatum;
1972 if (!ss_initialized)
1976 case AUDIT_SUBJ_USER:
1977 case AUDIT_SUBJ_ROLE:
1978 case AUDIT_SUBJ_TYPE:
1979 case AUDIT_OBJ_USER:
1980 case AUDIT_OBJ_ROLE:
1981 case AUDIT_OBJ_TYPE:
1982 /* only 'equals' and 'not equals' fit user, role, and type */
1983 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1986 case AUDIT_SUBJ_SEN:
1987 case AUDIT_SUBJ_CLR:
1988 case AUDIT_OBJ_LEV_LOW:
1989 case AUDIT_OBJ_LEV_HIGH:
1990 /* we do not allow a range, indicated by the presense of '-' */
1991 if (strchr(rulestr, '-'))
1995 /* only the above fields are valid */
1999 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2003 context_init(&tmprule->au_ctxt);
2007 tmprule->au_seqno = latest_granting;
2010 case AUDIT_SUBJ_USER:
2011 case AUDIT_OBJ_USER:
2012 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2016 tmprule->au_ctxt.user = userdatum->value;
2018 case AUDIT_SUBJ_ROLE:
2019 case AUDIT_OBJ_ROLE:
2020 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2024 tmprule->au_ctxt.role = roledatum->value;
2026 case AUDIT_SUBJ_TYPE:
2027 case AUDIT_OBJ_TYPE:
2028 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2032 tmprule->au_ctxt.type = typedatum->value;
2034 case AUDIT_SUBJ_SEN:
2035 case AUDIT_SUBJ_CLR:
2036 case AUDIT_OBJ_LEV_LOW:
2037 case AUDIT_OBJ_LEV_HIGH:
2038 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2045 selinux_audit_rule_free(tmprule);
2054 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2055 struct selinux_audit_rule *rule,
2056 struct audit_context *actx)
2058 struct context *ctxt;
2059 struct mls_level *level;
2063 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2064 "selinux_audit_rule_match: missing rule\n");
2070 if (rule->au_seqno < latest_granting) {
2071 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2072 "selinux_audit_rule_match: stale rule\n");
2077 ctxt = sidtab_search(&sidtab, sid);
2079 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2080 "selinux_audit_rule_match: unrecognized SID %d\n",
2086 /* a field/op pair that is not caught here will simply fall through
2089 case AUDIT_SUBJ_USER:
2090 case AUDIT_OBJ_USER:
2093 match = (ctxt->user == rule->au_ctxt.user);
2095 case AUDIT_NOT_EQUAL:
2096 match = (ctxt->user != rule->au_ctxt.user);
2100 case AUDIT_SUBJ_ROLE:
2101 case AUDIT_OBJ_ROLE:
2104 match = (ctxt->role == rule->au_ctxt.role);
2106 case AUDIT_NOT_EQUAL:
2107 match = (ctxt->role != rule->au_ctxt.role);
2111 case AUDIT_SUBJ_TYPE:
2112 case AUDIT_OBJ_TYPE:
2115 match = (ctxt->type == rule->au_ctxt.type);
2117 case AUDIT_NOT_EQUAL:
2118 match = (ctxt->type != rule->au_ctxt.type);
2122 case AUDIT_SUBJ_SEN:
2123 case AUDIT_SUBJ_CLR:
2124 case AUDIT_OBJ_LEV_LOW:
2125 case AUDIT_OBJ_LEV_HIGH:
2126 level = ((field == AUDIT_SUBJ_SEN ||
2127 field == AUDIT_OBJ_LEV_LOW) ?
2128 &ctxt->range.level[0] : &ctxt->range.level[1]);
2131 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2134 case AUDIT_NOT_EQUAL:
2135 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2138 case AUDIT_LESS_THAN:
2139 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2141 !mls_level_eq(&rule->au_ctxt.range.level[0],
2144 case AUDIT_LESS_THAN_OR_EQUAL:
2145 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2148 case AUDIT_GREATER_THAN:
2149 match = (mls_level_dom(level,
2150 &rule->au_ctxt.range.level[0]) &&
2151 !mls_level_eq(level,
2152 &rule->au_ctxt.range.level[0]));
2154 case AUDIT_GREATER_THAN_OR_EQUAL:
2155 match = mls_level_dom(level,
2156 &rule->au_ctxt.range.level[0]);
2166 static int (*aurule_callback)(void) = NULL;
2168 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2169 u16 class, u32 perms, u32 *retained)
2173 if (event == AVC_CALLBACK_RESET && aurule_callback)
2174 err = aurule_callback();
2178 static int __init aurule_init(void)
2182 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2183 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2185 panic("avc_add_callback() failed, error %d\n", err);
2189 __initcall(aurule_init);
2191 void selinux_audit_set_callback(int (*callback)(void))
2193 aurule_callback = callback;
2197 * security_skb_extlbl_sid - Determine the external label of a packet
2199 * @base_sid: the SELinux SID to use as a context for MLS only external labels
2200 * @sid: the packet's SID
2203 * Check the various different forms of external packet labeling and determine
2204 * the external SID for the packet.
2207 void security_skb_extlbl_sid(struct sk_buff *skb, u32 base_sid, u32 *sid)
2212 selinux_skb_xfrm_sid(skb, &xfrm_sid);
2213 if (selinux_netlbl_skbuff_getsid(skb,
2214 (xfrm_sid == SECSID_NULL ?
2215 base_sid : xfrm_sid),
2217 nlbl_sid = SECSID_NULL;
2219 *sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
2222 #ifdef CONFIG_NETLABEL
2224 * This is the structure we store inside the NetLabel cache block.
2226 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2227 #define NETLBL_CACHE_T_NONE 0
2228 #define NETLBL_CACHE_T_SID 1
2229 #define NETLBL_CACHE_T_MLS 2
2230 struct netlbl_cache {
2234 struct mls_range mls_label;
2239 * selinux_netlbl_cache_free - Free the NetLabel cached data
2240 * @data: the data to free
2243 * This function is intended to be used as the free() callback inside the
2244 * netlbl_lsm_cache structure.
2247 static void selinux_netlbl_cache_free(const void *data)
2249 struct netlbl_cache *cache;
2254 cache = NETLBL_CACHE(data);
2255 switch (cache->type) {
2256 case NETLBL_CACHE_T_MLS:
2257 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2264 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2266 * @ctx: the SELinux context
2269 * Attempt to cache the context in @ctx, which was derived from the packet in
2270 * @skb, in the NetLabel subsystem cache.
2273 static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
2275 struct netlbl_cache *cache = NULL;
2276 struct netlbl_lsm_secattr secattr;
2278 netlbl_secattr_init(&secattr);
2279 secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2280 if (secattr.cache == NULL)
2281 goto netlbl_cache_add_return;
2283 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2285 goto netlbl_cache_add_return;
2287 cache->type = NETLBL_CACHE_T_MLS;
2288 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2289 &ctx->range.level[0].cat) != 0)
2290 goto netlbl_cache_add_return;
2291 cache->data.mls_label.level[1].cat.highbit =
2292 cache->data.mls_label.level[0].cat.highbit;
2293 cache->data.mls_label.level[1].cat.node =
2294 cache->data.mls_label.level[0].cat.node;
2295 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2296 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2298 secattr.cache->free = selinux_netlbl_cache_free;
2299 secattr.cache->data = (void *)cache;
2300 secattr.flags = NETLBL_SECATTR_CACHE;
2302 netlbl_cache_add(skb, &secattr);
2304 netlbl_cache_add_return:
2305 netlbl_secattr_destroy(&secattr);
2309 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2312 * Invalidate the NetLabel security attribute mapping cache.
2315 void selinux_netlbl_cache_invalidate(void)
2317 netlbl_cache_invalidate();
2321 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2322 * @skb: the network packet
2323 * @secattr: the NetLabel packet security attributes
2324 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2325 * @sid: the SELinux SID
2328 * Convert the given NetLabel packet security attributes in @secattr into a
2329 * SELinux SID. If the @secattr field does not contain a full SELinux
2330 * SID/context then use the context in @base_sid as the foundation. If @skb
2331 * is not NULL attempt to cache as much data as possibile. Returns zero on
2332 * success, negative values on failure.
2335 static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
2336 struct netlbl_lsm_secattr *secattr,
2341 struct context *ctx;
2342 struct context ctx_new;
2343 struct netlbl_cache *cache;
2347 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2348 cache = NETLBL_CACHE(secattr->cache->data);
2349 switch (cache->type) {
2350 case NETLBL_CACHE_T_SID:
2351 *sid = cache->data.sid;
2354 case NETLBL_CACHE_T_MLS:
2355 ctx = sidtab_search(&sidtab, base_sid);
2357 goto netlbl_secattr_to_sid_return;
2359 ctx_new.user = ctx->user;
2360 ctx_new.role = ctx->role;
2361 ctx_new.type = ctx->type;
2362 ctx_new.range.level[0].sens =
2363 cache->data.mls_label.level[0].sens;
2364 ctx_new.range.level[0].cat.highbit =
2365 cache->data.mls_label.level[0].cat.highbit;
2366 ctx_new.range.level[0].cat.node =
2367 cache->data.mls_label.level[0].cat.node;
2368 ctx_new.range.level[1].sens =
2369 cache->data.mls_label.level[1].sens;
2370 ctx_new.range.level[1].cat.highbit =
2371 cache->data.mls_label.level[1].cat.highbit;
2372 ctx_new.range.level[1].cat.node =
2373 cache->data.mls_label.level[1].cat.node;
2375 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2378 goto netlbl_secattr_to_sid_return;
2380 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2381 ctx = sidtab_search(&sidtab, base_sid);
2383 goto netlbl_secattr_to_sid_return;
2385 ctx_new.user = ctx->user;
2386 ctx_new.role = ctx->role;
2387 ctx_new.type = ctx->type;
2388 mls_import_netlbl_lvl(&ctx_new, secattr);
2389 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2390 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2391 secattr->mls_cat) != 0)
2392 goto netlbl_secattr_to_sid_return;
2393 ctx_new.range.level[1].cat.highbit =
2394 ctx_new.range.level[0].cat.highbit;
2395 ctx_new.range.level[1].cat.node =
2396 ctx_new.range.level[0].cat.node;
2398 ebitmap_init(&ctx_new.range.level[0].cat);
2399 ebitmap_init(&ctx_new.range.level[1].cat);
2401 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2402 goto netlbl_secattr_to_sid_return_cleanup;
2404 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2406 goto netlbl_secattr_to_sid_return_cleanup;
2409 selinux_netlbl_cache_add(skb, &ctx_new);
2410 ebitmap_destroy(&ctx_new.range.level[0].cat);
2416 netlbl_secattr_to_sid_return:
2419 netlbl_secattr_to_sid_return_cleanup:
2420 ebitmap_destroy(&ctx_new.range.level[0].cat);
2421 goto netlbl_secattr_to_sid_return;
2425 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2427 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2431 * Call the NetLabel mechanism to get the security attributes of the given
2432 * packet and use those attributes to determine the correct context/SID to
2433 * assign to the packet. Returns zero on success, negative values on failure.
2436 int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, u32 base_sid, u32 *sid)
2439 struct netlbl_lsm_secattr secattr;
2441 netlbl_secattr_init(&secattr);
2442 rc = netlbl_skbuff_getattr(skb, &secattr);
2443 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
2444 rc = selinux_netlbl_secattr_to_sid(skb,
2450 netlbl_secattr_destroy(&secattr);
2456 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2457 * @sock: the socket to label
2458 * @sid: the SID to use
2461 * Attempt to label a socket using the NetLabel mechanism using the given
2462 * SID. Returns zero values on success, negative values on failure. The
2463 * caller is responsibile for calling rcu_read_lock() before calling this
2464 * this function and rcu_read_unlock() after this function returns.
2467 static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
2470 struct sk_security_struct *sksec = sock->sk->sk_security;
2471 struct netlbl_lsm_secattr secattr;
2472 struct context *ctx;
2474 if (!ss_initialized)
2477 netlbl_secattr_init(&secattr);
2481 ctx = sidtab_search(&sidtab, sid);
2483 goto netlbl_socket_setsid_return;
2485 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2487 secattr.flags |= NETLBL_SECATTR_DOMAIN;
2488 mls_export_netlbl_lvl(ctx, &secattr);
2489 rc = mls_export_netlbl_cat(ctx, &secattr);
2491 goto netlbl_socket_setsid_return;
2493 rc = netlbl_socket_setattr(sock, &secattr);
2495 spin_lock(&sksec->nlbl_lock);
2496 sksec->nlbl_state = NLBL_LABELED;
2497 spin_unlock(&sksec->nlbl_lock);
2500 netlbl_socket_setsid_return:
2502 netlbl_secattr_destroy(&secattr);
2507 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
2508 * @ssec: the sk_security_struct
2509 * @family: the socket family
2512 * Called when the NetLabel state of a sk_security_struct needs to be reset.
2513 * The caller is responsibile for all the NetLabel sk_security_struct locking.
2516 void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
2519 if (family == PF_INET)
2520 ssec->nlbl_state = NLBL_REQUIRE;
2522 ssec->nlbl_state = NLBL_UNSET;
2526 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2527 * @ssec: the sk_security_struct
2528 * @family: the socket family
2531 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2535 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
2538 /* No locking needed, we are the only one who has access to ssec */
2539 selinux_netlbl_sk_security_reset(ssec, family);
2540 spin_lock_init(&ssec->nlbl_lock);
2544 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
2545 * @ssec: the original sk_security_struct
2546 * @newssec: the cloned sk_security_struct
2549 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2553 void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
2554 struct sk_security_struct *newssec)
2556 /* We don't need to take newssec->nlbl_lock because we are the only
2557 * thread with access to newssec, but we do need to take the RCU read
2558 * lock as other threads could have access to ssec */
2560 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
2561 newssec->sclass = ssec->sclass;
2566 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2567 * @sock: the socket to label
2570 * Attempt to label a socket using the NetLabel mechanism using the given
2571 * SID. Returns zero values on success, negative values on failure.
2574 int selinux_netlbl_socket_post_create(struct socket *sock)
2577 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2578 struct sk_security_struct *sksec = sock->sk->sk_security;
2580 sksec->sclass = isec->sclass;
2583 if (sksec->nlbl_state == NLBL_REQUIRE)
2584 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2591 * selinux_netlbl_sock_graft - Netlabel the new socket
2592 * @sk: the new connection
2593 * @sock: the new socket
2596 * The connection represented by @sk is being grafted onto @sock so set the
2597 * socket's NetLabel to match the SID of @sk.
2600 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
2602 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2603 struct sk_security_struct *sksec = sk->sk_security;
2604 struct netlbl_lsm_secattr secattr;
2607 sksec->sclass = isec->sclass;
2611 if (sksec->nlbl_state != NLBL_REQUIRE) {
2616 netlbl_secattr_init(&secattr);
2617 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
2618 secattr.flags != NETLBL_SECATTR_NONE &&
2619 selinux_netlbl_secattr_to_sid(NULL,
2621 SECINITSID_UNLABELED,
2622 &nlbl_peer_sid) == 0)
2623 sksec->peer_sid = nlbl_peer_sid;
2624 netlbl_secattr_destroy(&secattr);
2626 /* Try to set the NetLabel on the socket to save time later, if we fail
2627 * here we will pick up the pieces in later calls to
2628 * selinux_netlbl_inode_permission(). */
2629 selinux_netlbl_socket_setsid(sock, sksec->sid);
2635 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
2636 * @inode: the file descriptor's inode
2637 * @mask: the permission mask
2640 * Looks at a file's inode and if it is marked as a socket protected by
2641 * NetLabel then verify that the socket has been labeled, if not try to label
2642 * the socket now with the inode's SID. Returns zero on success, negative
2643 * values on failure.
2646 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
2649 struct sk_security_struct *sksec;
2650 struct socket *sock;
2652 if (!S_ISSOCK(inode->i_mode) ||
2653 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
2655 sock = SOCKET_I(inode);
2656 sksec = sock->sk->sk_security;
2659 if (sksec->nlbl_state != NLBL_REQUIRE) {
2664 bh_lock_sock_nested(sock->sk);
2665 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2666 bh_unlock_sock(sock->sk);
2674 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2675 * @sksec: the sock's sk_security_struct
2677 * @ad: the audit data
2680 * Fetch the NetLabel security attributes from @skb and perform an access check
2681 * against the receiving socket. Returns zero on success, negative values on
2685 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
2686 struct sk_buff *skb,
2687 struct avc_audit_data *ad)
2693 rc = selinux_netlbl_skbuff_getsid(skb,
2694 SECINITSID_UNLABELED,
2699 if (netlbl_sid == SECSID_NULL)
2702 switch (sksec->sclass) {
2703 case SECCLASS_UDP_SOCKET:
2704 recv_perm = UDP_SOCKET__RECVFROM;
2706 case SECCLASS_TCP_SOCKET:
2707 recv_perm = TCP_SOCKET__RECVFROM;
2710 recv_perm = RAWIP_SOCKET__RECVFROM;
2713 rc = avc_has_perm(sksec->sid,
2721 netlbl_skbuff_err(skb, rc);
2726 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
2728 * @level: the socket level or protocol
2729 * @optname: the socket option name
2732 * Check the setsockopt() call and if the user is trying to replace the IP
2733 * options on a socket and a NetLabel is in place for the socket deny the
2734 * access; otherwise allow the access. Returns zero when the access is
2735 * allowed, -EACCES when denied, and other negative values on error.
2738 int selinux_netlbl_socket_setsockopt(struct socket *sock,
2743 struct sk_security_struct *sksec = sock->sk->sk_security;
2744 struct netlbl_lsm_secattr secattr;
2747 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
2748 sksec->nlbl_state == NLBL_LABELED) {
2749 netlbl_secattr_init(&secattr);
2750 rc = netlbl_socket_getattr(sock, &secattr);
2751 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
2753 netlbl_secattr_destroy(&secattr);
2759 #endif /* CONFIG_NETLABEL */