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 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
21 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, version 2.
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/spinlock.h>
32 #include <linux/errno.h>
34 #include <linux/sched.h>
35 #include <linux/audit.h>
36 #include <linux/mutex.h>
38 #include <net/netlabel.h>
48 #include "conditional.h"
51 #include "selinux_netlabel.h"
53 extern void selnl_notify_policyload(u32 seqno);
54 unsigned int policydb_loaded_version;
56 static DEFINE_RWLOCK(policy_rwlock);
57 #define POLICY_RDLOCK read_lock(&policy_rwlock)
58 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
59 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
60 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
62 static DEFINE_MUTEX(load_mutex);
63 #define LOAD_LOCK mutex_lock(&load_mutex)
64 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
66 static struct sidtab sidtab;
67 struct policydb policydb;
68 int ss_initialized = 0;
71 * The largest sequence number that has been used when
72 * providing an access decision to the access vector cache.
73 * The sequence number only changes when a policy change
76 static u32 latest_granting = 0;
78 /* Forward declaration. */
79 static int context_struct_to_string(struct context *context, char **scontext,
83 * Return the boolean value of a constraint expression
84 * when it is applied to the specified source and target
87 * xcontext is a special beast... It is used by the validatetrans rules
88 * only. For these rules, scontext is the context before the transition,
89 * tcontext is the context after the transition, and xcontext is the context
90 * of the process performing the transition. All other callers of
91 * constraint_expr_eval should pass in NULL for xcontext.
93 static int constraint_expr_eval(struct context *scontext,
94 struct context *tcontext,
95 struct context *xcontext,
96 struct constraint_expr *cexpr)
100 struct role_datum *r1, *r2;
101 struct mls_level *l1, *l2;
102 struct constraint_expr *e;
103 int s[CEXPR_MAXDEPTH];
106 for (e = cexpr; e; e = e->next) {
107 switch (e->expr_type) {
123 if (sp == (CEXPR_MAXDEPTH-1))
127 val1 = scontext->user;
128 val2 = tcontext->user;
131 val1 = scontext->type;
132 val2 = tcontext->type;
135 val1 = scontext->role;
136 val2 = tcontext->role;
137 r1 = policydb.role_val_to_struct[val1 - 1];
138 r2 = policydb.role_val_to_struct[val2 - 1];
141 s[++sp] = ebitmap_get_bit(&r1->dominates,
145 s[++sp] = ebitmap_get_bit(&r2->dominates,
149 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
151 !ebitmap_get_bit(&r2->dominates,
159 l1 = &(scontext->range.level[0]);
160 l2 = &(tcontext->range.level[0]);
163 l1 = &(scontext->range.level[0]);
164 l2 = &(tcontext->range.level[1]);
167 l1 = &(scontext->range.level[1]);
168 l2 = &(tcontext->range.level[0]);
171 l1 = &(scontext->range.level[1]);
172 l2 = &(tcontext->range.level[1]);
175 l1 = &(scontext->range.level[0]);
176 l2 = &(scontext->range.level[1]);
179 l1 = &(tcontext->range.level[0]);
180 l2 = &(tcontext->range.level[1]);
185 s[++sp] = mls_level_eq(l1, l2);
188 s[++sp] = !mls_level_eq(l1, l2);
191 s[++sp] = mls_level_dom(l1, l2);
194 s[++sp] = mls_level_dom(l2, l1);
197 s[++sp] = mls_level_incomp(l2, l1);
211 s[++sp] = (val1 == val2);
214 s[++sp] = (val1 != val2);
222 if (sp == (CEXPR_MAXDEPTH-1))
225 if (e->attr & CEXPR_TARGET)
227 else if (e->attr & CEXPR_XTARGET) {
234 if (e->attr & CEXPR_USER)
236 else if (e->attr & CEXPR_ROLE)
238 else if (e->attr & CEXPR_TYPE)
247 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
250 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
268 * Compute access vectors based on a context structure pair for
269 * the permissions in a particular class.
271 static int context_struct_compute_av(struct context *scontext,
272 struct context *tcontext,
275 struct av_decision *avd)
277 struct constraint_node *constraint;
278 struct role_allow *ra;
279 struct avtab_key avkey;
280 struct avtab_node *node;
281 struct class_datum *tclass_datum;
282 struct ebitmap *sattr, *tattr;
283 struct ebitmap_node *snode, *tnode;
287 * Remap extended Netlink classes for old policy versions.
288 * Do this here rather than socket_type_to_security_class()
289 * in case a newer policy version is loaded, allowing sockets
290 * to remain in the correct class.
292 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
293 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
294 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
295 tclass = SECCLASS_NETLINK_SOCKET;
297 if (!tclass || tclass > policydb.p_classes.nprim) {
298 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
302 tclass_datum = policydb.class_val_to_struct[tclass - 1];
305 * Initialize the access vectors to the default values.
308 avd->decided = 0xffffffff;
310 avd->auditdeny = 0xffffffff;
311 avd->seqno = latest_granting;
314 * If a specific type enforcement rule was defined for
315 * this permission check, then use it.
317 avkey.target_class = tclass;
318 avkey.specified = AVTAB_AV;
319 sattr = &policydb.type_attr_map[scontext->type - 1];
320 tattr = &policydb.type_attr_map[tcontext->type - 1];
321 ebitmap_for_each_bit(sattr, snode, i) {
322 if (!ebitmap_node_get_bit(snode, i))
324 ebitmap_for_each_bit(tattr, tnode, j) {
325 if (!ebitmap_node_get_bit(tnode, j))
327 avkey.source_type = i + 1;
328 avkey.target_type = j + 1;
329 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
331 node = avtab_search_node_next(node, avkey.specified)) {
332 if (node->key.specified == AVTAB_ALLOWED)
333 avd->allowed |= node->datum.data;
334 else if (node->key.specified == AVTAB_AUDITALLOW)
335 avd->auditallow |= node->datum.data;
336 else if (node->key.specified == AVTAB_AUDITDENY)
337 avd->auditdeny &= node->datum.data;
340 /* Check conditional av table for additional permissions */
341 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
347 * Remove any permissions prohibited by a constraint (this includes
350 constraint = tclass_datum->constraints;
352 if ((constraint->permissions & (avd->allowed)) &&
353 !constraint_expr_eval(scontext, tcontext, NULL,
355 avd->allowed = (avd->allowed) & ~(constraint->permissions);
357 constraint = constraint->next;
361 * If checking process transition permission and the
362 * role is changing, then check the (current_role, new_role)
365 if (tclass == SECCLASS_PROCESS &&
366 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
367 scontext->role != tcontext->role) {
368 for (ra = policydb.role_allow; ra; ra = ra->next) {
369 if (scontext->role == ra->role &&
370 tcontext->role == ra->new_role)
374 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
375 PROCESS__DYNTRANSITION);
381 static int security_validtrans_handle_fail(struct context *ocontext,
382 struct context *ncontext,
383 struct context *tcontext,
386 char *o = NULL, *n = NULL, *t = NULL;
387 u32 olen, nlen, tlen;
389 if (context_struct_to_string(ocontext, &o, &olen) < 0)
391 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
393 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
395 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
396 "security_validate_transition: denied for"
397 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
398 o, n, t, policydb.p_class_val_to_name[tclass-1]);
404 if (!selinux_enforcing)
409 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
412 struct context *ocontext;
413 struct context *ncontext;
414 struct context *tcontext;
415 struct class_datum *tclass_datum;
416 struct constraint_node *constraint;
425 * Remap extended Netlink classes for old policy versions.
426 * Do this here rather than socket_type_to_security_class()
427 * in case a newer policy version is loaded, allowing sockets
428 * to remain in the correct class.
430 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
431 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
432 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
433 tclass = SECCLASS_NETLINK_SOCKET;
435 if (!tclass || tclass > policydb.p_classes.nprim) {
436 printk(KERN_ERR "security_validate_transition: "
437 "unrecognized class %d\n", tclass);
441 tclass_datum = policydb.class_val_to_struct[tclass - 1];
443 ocontext = sidtab_search(&sidtab, oldsid);
445 printk(KERN_ERR "security_validate_transition: "
446 " unrecognized SID %d\n", oldsid);
451 ncontext = sidtab_search(&sidtab, newsid);
453 printk(KERN_ERR "security_validate_transition: "
454 " unrecognized SID %d\n", newsid);
459 tcontext = sidtab_search(&sidtab, tasksid);
461 printk(KERN_ERR "security_validate_transition: "
462 " unrecognized SID %d\n", tasksid);
467 constraint = tclass_datum->validatetrans;
469 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
471 rc = security_validtrans_handle_fail(ocontext, ncontext,
475 constraint = constraint->next;
484 * security_compute_av - Compute access vector decisions.
485 * @ssid: source security identifier
486 * @tsid: target security identifier
487 * @tclass: target security class
488 * @requested: requested permissions
489 * @avd: access vector decisions
491 * Compute a set of access vector decisions based on the
492 * SID pair (@ssid, @tsid) for the permissions in @tclass.
493 * Return -%EINVAL if any of the parameters are invalid or %0
494 * if the access vector decisions were computed successfully.
496 int security_compute_av(u32 ssid,
500 struct av_decision *avd)
502 struct context *scontext = NULL, *tcontext = NULL;
505 if (!ss_initialized) {
506 avd->allowed = 0xffffffff;
507 avd->decided = 0xffffffff;
509 avd->auditdeny = 0xffffffff;
510 avd->seqno = latest_granting;
516 scontext = sidtab_search(&sidtab, ssid);
518 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
523 tcontext = sidtab_search(&sidtab, tsid);
525 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
531 rc = context_struct_compute_av(scontext, tcontext, tclass,
539 * Write the security context string representation of
540 * the context structure `context' into a dynamically
541 * allocated string of the correct size. Set `*scontext'
542 * to point to this string and set `*scontext_len' to
543 * the length of the string.
545 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
552 /* Compute the size of the context. */
553 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
554 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
555 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
556 *scontext_len += mls_compute_context_len(context);
558 /* Allocate space for the context; caller must free this space. */
559 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
563 *scontext = scontextp;
566 * Copy the user name, role name and type name into the context.
568 sprintf(scontextp, "%s:%s:%s",
569 policydb.p_user_val_to_name[context->user - 1],
570 policydb.p_role_val_to_name[context->role - 1],
571 policydb.p_type_val_to_name[context->type - 1]);
572 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
573 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
574 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
576 mls_sid_to_context(context, &scontextp);
583 #include "initial_sid_to_string.h"
586 * security_sid_to_context - Obtain a context for a given SID.
587 * @sid: security identifier, SID
588 * @scontext: security context
589 * @scontext_len: length in bytes
591 * Write the string representation of the context associated with @sid
592 * into a dynamically allocated string of the correct size. Set @scontext
593 * to point to this string and set @scontext_len to the length of the string.
595 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
597 struct context *context;
600 if (!ss_initialized) {
601 if (sid <= SECINITSID_NUM) {
604 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
605 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
610 strcpy(scontextp, initial_sid_to_string[sid]);
611 *scontext = scontextp;
614 printk(KERN_ERR "security_sid_to_context: called before initial "
615 "load_policy on unknown SID %d\n", sid);
620 context = sidtab_search(&sidtab, sid);
622 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
627 rc = context_struct_to_string(context, scontext, scontext_len);
635 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
638 struct context context;
639 struct role_datum *role;
640 struct type_datum *typdatum;
641 struct user_datum *usrdatum;
642 char *scontextp, *p, oldc;
645 if (!ss_initialized) {
648 for (i = 1; i < SECINITSID_NUM; i++) {
649 if (!strcmp(initial_sid_to_string[i], scontext)) {
654 *sid = SECINITSID_KERNEL;
659 /* Copy the string so that we can modify the copy as we parse it.
660 The string should already by null terminated, but we append a
661 null suffix to the copy to avoid problems with the existing
662 attr package, which doesn't view the null terminator as part
663 of the attribute value. */
664 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
669 memcpy(scontext2, scontext, scontext_len);
670 scontext2[scontext_len] = 0;
672 context_init(&context);
677 /* Parse the security context. */
680 scontextp = (char *) scontext2;
682 /* Extract the user. */
684 while (*p && *p != ':')
692 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
696 context.user = usrdatum->value;
700 while (*p && *p != ':')
708 role = hashtab_search(policydb.p_roles.table, scontextp);
711 context.role = role->value;
715 while (*p && *p != ':')
720 typdatum = hashtab_search(policydb.p_types.table, scontextp);
724 context.type = typdatum->value;
726 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
730 if ((p - scontext2) < scontext_len) {
735 /* Check the validity of the new context. */
736 if (!policydb_context_isvalid(&policydb, &context)) {
740 /* Obtain the new sid. */
741 rc = sidtab_context_to_sid(&sidtab, &context, sid);
744 context_destroy(&context);
751 * security_context_to_sid - Obtain a SID for a given security context.
752 * @scontext: security context
753 * @scontext_len: length in bytes
754 * @sid: security identifier, SID
756 * Obtains a SID associated with the security context that
757 * has the string representation specified by @scontext.
758 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
759 * memory is available, or 0 on success.
761 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
763 return security_context_to_sid_core(scontext, scontext_len,
768 * security_context_to_sid_default - Obtain a SID for a given security context,
769 * falling back to specified default if needed.
771 * @scontext: security context
772 * @scontext_len: length in bytes
773 * @sid: security identifier, SID
774 * @def_sid: default SID to assign on errror
776 * Obtains a SID associated with the security context that
777 * has the string representation specified by @scontext.
778 * The default SID is passed to the MLS layer to be used to allow
779 * kernel labeling of the MLS field if the MLS field is not present
780 * (for upgrading to MLS without full relabel).
781 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
782 * memory is available, or 0 on success.
784 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
786 return security_context_to_sid_core(scontext, scontext_len,
790 static int compute_sid_handle_invalid_context(
791 struct context *scontext,
792 struct context *tcontext,
794 struct context *newcontext)
796 char *s = NULL, *t = NULL, *n = NULL;
797 u32 slen, tlen, nlen;
799 if (context_struct_to_string(scontext, &s, &slen) < 0)
801 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
803 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
805 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
806 "security_compute_sid: invalid context %s"
810 n, s, t, policydb.p_class_val_to_name[tclass-1]);
815 if (!selinux_enforcing)
820 static int security_compute_sid(u32 ssid,
826 struct context *scontext = NULL, *tcontext = NULL, newcontext;
827 struct role_trans *roletr = NULL;
828 struct avtab_key avkey;
829 struct avtab_datum *avdatum;
830 struct avtab_node *node;
833 if (!ss_initialized) {
835 case SECCLASS_PROCESS:
845 context_init(&newcontext);
849 scontext = sidtab_search(&sidtab, ssid);
851 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
856 tcontext = sidtab_search(&sidtab, tsid);
858 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
864 /* Set the user identity. */
866 case AVTAB_TRANSITION:
868 /* Use the process user identity. */
869 newcontext.user = scontext->user;
872 /* Use the related object owner. */
873 newcontext.user = tcontext->user;
877 /* Set the role and type to default values. */
879 case SECCLASS_PROCESS:
880 /* Use the current role and type of process. */
881 newcontext.role = scontext->role;
882 newcontext.type = scontext->type;
885 /* Use the well-defined object role. */
886 newcontext.role = OBJECT_R_VAL;
887 /* Use the type of the related object. */
888 newcontext.type = tcontext->type;
891 /* Look for a type transition/member/change rule. */
892 avkey.source_type = scontext->type;
893 avkey.target_type = tcontext->type;
894 avkey.target_class = tclass;
895 avkey.specified = specified;
896 avdatum = avtab_search(&policydb.te_avtab, &avkey);
898 /* If no permanent rule, also check for enabled conditional rules */
900 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
901 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
902 if (node->key.specified & AVTAB_ENABLED) {
903 avdatum = &node->datum;
910 /* Use the type from the type transition/member/change rule. */
911 newcontext.type = avdatum->data;
914 /* Check for class-specific changes. */
916 case SECCLASS_PROCESS:
917 if (specified & AVTAB_TRANSITION) {
918 /* Look for a role transition rule. */
919 for (roletr = policydb.role_tr; roletr;
920 roletr = roletr->next) {
921 if (roletr->role == scontext->role &&
922 roletr->type == tcontext->type) {
923 /* Use the role transition rule. */
924 newcontext.role = roletr->new_role;
934 /* Set the MLS attributes.
935 This is done last because it may allocate memory. */
936 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
940 /* Check the validity of the context. */
941 if (!policydb_context_isvalid(&policydb, &newcontext)) {
942 rc = compute_sid_handle_invalid_context(scontext,
949 /* Obtain the sid for the context. */
950 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
953 context_destroy(&newcontext);
959 * security_transition_sid - Compute the SID for a new subject/object.
960 * @ssid: source security identifier
961 * @tsid: target security identifier
962 * @tclass: target security class
963 * @out_sid: security identifier for new subject/object
965 * Compute a SID to use for labeling a new subject or object in the
966 * class @tclass based on a SID pair (@ssid, @tsid).
967 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
968 * if insufficient memory is available, or %0 if the new SID was
969 * computed successfully.
971 int security_transition_sid(u32 ssid,
976 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
980 * security_member_sid - Compute the SID for member selection.
981 * @ssid: source security identifier
982 * @tsid: target security identifier
983 * @tclass: target security class
984 * @out_sid: security identifier for selected member
986 * Compute a SID to use when selecting a member of a polyinstantiated
987 * object of class @tclass based on a SID pair (@ssid, @tsid).
988 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
989 * if insufficient memory is available, or %0 if the SID was
990 * computed successfully.
992 int security_member_sid(u32 ssid,
997 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1001 * security_change_sid - Compute the SID for object relabeling.
1002 * @ssid: source security identifier
1003 * @tsid: target security identifier
1004 * @tclass: target security class
1005 * @out_sid: security identifier for selected member
1007 * Compute a SID to use for relabeling an object of class @tclass
1008 * based on a SID pair (@ssid, @tsid).
1009 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1010 * if insufficient memory is available, or %0 if the SID was
1011 * computed successfully.
1013 int security_change_sid(u32 ssid,
1018 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1022 * Verify that each permission that is defined under the
1023 * existing policy is still defined with the same value
1024 * in the new policy.
1026 static int validate_perm(void *key, void *datum, void *p)
1029 struct perm_datum *perdatum, *perdatum2;
1036 perdatum2 = hashtab_search(h, key);
1038 printk(KERN_ERR "security: permission %s disappeared",
1043 if (perdatum->value != perdatum2->value) {
1044 printk(KERN_ERR "security: the value of permission %s changed",
1053 * Verify that each class that is defined under the
1054 * existing policy is still defined with the same
1055 * attributes in the new policy.
1057 static int validate_class(void *key, void *datum, void *p)
1059 struct policydb *newp;
1060 struct class_datum *cladatum, *cladatum2;
1066 cladatum2 = hashtab_search(newp->p_classes.table, key);
1068 printk(KERN_ERR "security: class %s disappeared\n",
1073 if (cladatum->value != cladatum2->value) {
1074 printk(KERN_ERR "security: the value of class %s changed\n",
1079 if ((cladatum->comdatum && !cladatum2->comdatum) ||
1080 (!cladatum->comdatum && cladatum2->comdatum)) {
1081 printk(KERN_ERR "security: the inherits clause for the access "
1082 "vector definition for class %s changed\n", (char *)key);
1086 if (cladatum->comdatum) {
1087 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
1088 cladatum2->comdatum->permissions.table);
1090 printk(" in the access vector definition for class "
1091 "%s\n", (char *)key);
1095 rc = hashtab_map(cladatum->permissions.table, validate_perm,
1096 cladatum2->permissions.table);
1098 printk(" in access vector definition for class %s\n",
1104 /* Clone the SID into the new SID table. */
1105 static int clone_sid(u32 sid,
1106 struct context *context,
1109 struct sidtab *s = arg;
1111 return sidtab_insert(s, sid, context);
1114 static inline int convert_context_handle_invalid_context(struct context *context)
1118 if (selinux_enforcing) {
1124 context_struct_to_string(context, &s, &len);
1125 printk(KERN_ERR "security: context %s is invalid\n", s);
1131 struct convert_context_args {
1132 struct policydb *oldp;
1133 struct policydb *newp;
1137 * Convert the values in the security context
1138 * structure `c' from the values specified
1139 * in the policy `p->oldp' to the values specified
1140 * in the policy `p->newp'. Verify that the
1141 * context is valid under the new policy.
1143 static int convert_context(u32 key,
1147 struct convert_context_args *args;
1148 struct context oldc;
1149 struct role_datum *role;
1150 struct type_datum *typdatum;
1151 struct user_datum *usrdatum;
1158 rc = context_cpy(&oldc, c);
1164 /* Convert the user. */
1165 usrdatum = hashtab_search(args->newp->p_users.table,
1166 args->oldp->p_user_val_to_name[c->user - 1]);
1170 c->user = usrdatum->value;
1172 /* Convert the role. */
1173 role = hashtab_search(args->newp->p_roles.table,
1174 args->oldp->p_role_val_to_name[c->role - 1]);
1178 c->role = role->value;
1180 /* Convert the type. */
1181 typdatum = hashtab_search(args->newp->p_types.table,
1182 args->oldp->p_type_val_to_name[c->type - 1]);
1186 c->type = typdatum->value;
1188 rc = mls_convert_context(args->oldp, args->newp, c);
1192 /* Check the validity of the new context. */
1193 if (!policydb_context_isvalid(args->newp, c)) {
1194 rc = convert_context_handle_invalid_context(&oldc);
1199 context_destroy(&oldc);
1203 context_struct_to_string(&oldc, &s, &len);
1204 context_destroy(&oldc);
1205 printk(KERN_ERR "security: invalidating context %s\n", s);
1210 extern void selinux_complete_init(void);
1213 * security_load_policy - Load a security policy configuration.
1214 * @data: binary policy data
1215 * @len: length of data in bytes
1217 * Load a new set of security policy configuration data,
1218 * validate it and convert the SID table as necessary.
1219 * This function will flush the access vector cache after
1220 * loading the new policy.
1222 int security_load_policy(void *data, size_t len)
1224 struct policydb oldpolicydb, newpolicydb;
1225 struct sidtab oldsidtab, newsidtab;
1226 struct convert_context_args args;
1229 struct policy_file file = { data, len }, *fp = &file;
1233 if (!ss_initialized) {
1235 if (policydb_read(&policydb, fp)) {
1237 avtab_cache_destroy();
1240 if (policydb_load_isids(&policydb, &sidtab)) {
1242 policydb_destroy(&policydb);
1243 avtab_cache_destroy();
1246 policydb_loaded_version = policydb.policyvers;
1248 seqno = ++latest_granting;
1250 selinux_complete_init();
1251 avc_ss_reset(seqno);
1252 selnl_notify_policyload(seqno);
1253 selinux_netlbl_cache_invalidate();
1258 sidtab_hash_eval(&sidtab, "sids");
1261 if (policydb_read(&newpolicydb, fp)) {
1266 sidtab_init(&newsidtab);
1268 /* Verify that the existing classes did not change. */
1269 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1270 printk(KERN_ERR "security: the definition of an existing "
1276 /* Clone the SID table. */
1277 sidtab_shutdown(&sidtab);
1278 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1283 /* Convert the internal representations of contexts
1284 in the new SID table and remove invalid SIDs. */
1285 args.oldp = &policydb;
1286 args.newp = &newpolicydb;
1287 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1289 /* Save the old policydb and SID table to free later. */
1290 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1291 sidtab_set(&oldsidtab, &sidtab);
1293 /* Install the new policydb and SID table. */
1295 memcpy(&policydb, &newpolicydb, sizeof policydb);
1296 sidtab_set(&sidtab, &newsidtab);
1297 seqno = ++latest_granting;
1298 policydb_loaded_version = policydb.policyvers;
1302 /* Free the old policydb and SID table. */
1303 policydb_destroy(&oldpolicydb);
1304 sidtab_destroy(&oldsidtab);
1306 avc_ss_reset(seqno);
1307 selnl_notify_policyload(seqno);
1308 selinux_netlbl_cache_invalidate();
1314 sidtab_destroy(&newsidtab);
1315 policydb_destroy(&newpolicydb);
1321 * security_port_sid - Obtain the SID for a port.
1322 * @domain: communication domain aka address family
1323 * @type: socket type
1324 * @protocol: protocol number
1325 * @port: port number
1326 * @out_sid: security identifier
1328 int security_port_sid(u16 domain,
1339 c = policydb.ocontexts[OCON_PORT];
1341 if (c->u.port.protocol == protocol &&
1342 c->u.port.low_port <= port &&
1343 c->u.port.high_port >= port)
1350 rc = sidtab_context_to_sid(&sidtab,
1356 *out_sid = c->sid[0];
1358 *out_sid = SECINITSID_PORT;
1367 * security_netif_sid - Obtain the SID for a network interface.
1368 * @name: interface name
1369 * @if_sid: interface SID
1370 * @msg_sid: default SID for received packets
1372 int security_netif_sid(char *name,
1381 c = policydb.ocontexts[OCON_NETIF];
1383 if (strcmp(name, c->u.name) == 0)
1389 if (!c->sid[0] || !c->sid[1]) {
1390 rc = sidtab_context_to_sid(&sidtab,
1395 rc = sidtab_context_to_sid(&sidtab,
1401 *if_sid = c->sid[0];
1402 *msg_sid = c->sid[1];
1404 *if_sid = SECINITSID_NETIF;
1405 *msg_sid = SECINITSID_NETMSG;
1413 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1417 for(i = 0; i < 4; i++)
1418 if(addr[i] != (input[i] & mask[i])) {
1427 * security_node_sid - Obtain the SID for a node (host).
1428 * @domain: communication domain aka address family
1430 * @addrlen: address length in bytes
1431 * @out_sid: security identifier
1433 int security_node_sid(u16 domain,
1447 if (addrlen != sizeof(u32)) {
1452 addr = *((u32 *)addrp);
1454 c = policydb.ocontexts[OCON_NODE];
1456 if (c->u.node.addr == (addr & c->u.node.mask))
1464 if (addrlen != sizeof(u64) * 2) {
1468 c = policydb.ocontexts[OCON_NODE6];
1470 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1478 *out_sid = SECINITSID_NODE;
1484 rc = sidtab_context_to_sid(&sidtab,
1490 *out_sid = c->sid[0];
1492 *out_sid = SECINITSID_NODE;
1503 * security_get_user_sids - Obtain reachable SIDs for a user.
1504 * @fromsid: starting SID
1505 * @username: username
1506 * @sids: array of reachable SIDs for user
1507 * @nel: number of elements in @sids
1509 * Generate the set of SIDs for legal security contexts
1510 * for a given user that can be reached by @fromsid.
1511 * Set *@sids to point to a dynamically allocated
1512 * array containing the set of SIDs. Set *@nel to the
1513 * number of elements in the array.
1516 int security_get_user_sids(u32 fromsid,
1521 struct context *fromcon, usercon;
1522 u32 *mysids, *mysids2, sid;
1523 u32 mynel = 0, maxnel = SIDS_NEL;
1524 struct user_datum *user;
1525 struct role_datum *role;
1526 struct av_decision avd;
1527 struct ebitmap_node *rnode, *tnode;
1530 if (!ss_initialized) {
1538 fromcon = sidtab_search(&sidtab, fromsid);
1544 user = hashtab_search(policydb.p_users.table, username);
1549 usercon.user = user->value;
1551 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1557 ebitmap_for_each_bit(&user->roles, rnode, i) {
1558 if (!ebitmap_node_get_bit(rnode, i))
1560 role = policydb.role_val_to_struct[i];
1562 ebitmap_for_each_bit(&role->types, tnode, j) {
1563 if (!ebitmap_node_get_bit(tnode, j))
1567 if (mls_setup_user_range(fromcon, user, &usercon))
1570 rc = context_struct_compute_av(fromcon, &usercon,
1572 PROCESS__TRANSITION,
1574 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1576 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1581 if (mynel < maxnel) {
1582 mysids[mynel++] = sid;
1585 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1591 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1594 mysids[mynel++] = sid;
1609 * security_genfs_sid - Obtain a SID for a file in a filesystem
1610 * @fstype: filesystem type
1611 * @path: path from root of mount
1612 * @sclass: file security class
1613 * @sid: SID for path
1615 * Obtain a SID to use for a file in a filesystem that
1616 * cannot support xattr or use a fixed labeling behavior like
1617 * transition SIDs or task SIDs.
1619 int security_genfs_sid(const char *fstype,
1625 struct genfs *genfs;
1627 int rc = 0, cmp = 0;
1631 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1632 cmp = strcmp(fstype, genfs->fstype);
1637 if (!genfs || cmp) {
1638 *sid = SECINITSID_UNLABELED;
1643 for (c = genfs->head; c; c = c->next) {
1644 len = strlen(c->u.name);
1645 if ((!c->v.sclass || sclass == c->v.sclass) &&
1646 (strncmp(c->u.name, path, len) == 0))
1651 *sid = SECINITSID_UNLABELED;
1657 rc = sidtab_context_to_sid(&sidtab,
1671 * security_fs_use - Determine how to handle labeling for a filesystem.
1672 * @fstype: filesystem type
1673 * @behavior: labeling behavior
1674 * @sid: SID for filesystem (superblock)
1676 int security_fs_use(
1678 unsigned int *behavior,
1686 c = policydb.ocontexts[OCON_FSUSE];
1688 if (strcmp(fstype, c->u.name) == 0)
1694 *behavior = c->v.behavior;
1696 rc = sidtab_context_to_sid(&sidtab,
1704 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1706 *behavior = SECURITY_FS_USE_NONE;
1709 *behavior = SECURITY_FS_USE_GENFS;
1718 int security_get_bools(int *len, char ***names, int **values)
1720 int i, rc = -ENOMEM;
1726 *len = policydb.p_bools.nprim;
1732 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1736 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1740 for (i = 0; i < *len; i++) {
1742 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1743 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1744 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1747 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1748 (*names)[i][name_len - 1] = 0;
1756 for (i = 0; i < *len; i++)
1764 int security_set_bools(int len, int *values)
1767 int lenp, seqno = 0;
1768 struct cond_node *cur;
1772 lenp = policydb.p_bools.nprim;
1778 for (i = 0; i < len; i++) {
1779 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1780 audit_log(current->audit_context, GFP_ATOMIC,
1781 AUDIT_MAC_CONFIG_CHANGE,
1782 "bool=%s val=%d old_val=%d auid=%u",
1783 policydb.p_bool_val_to_name[i],
1785 policydb.bool_val_to_struct[i]->state,
1786 audit_get_loginuid(current->audit_context));
1789 policydb.bool_val_to_struct[i]->state = 1;
1791 policydb.bool_val_to_struct[i]->state = 0;
1795 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1796 rc = evaluate_cond_node(&policydb, cur);
1801 seqno = ++latest_granting;
1806 avc_ss_reset(seqno);
1807 selnl_notify_policyload(seqno);
1812 int security_get_bool_value(int bool)
1819 len = policydb.p_bools.nprim;
1825 rc = policydb.bool_val_to_struct[bool]->state;
1832 * security_sid_mls_copy() - computes a new sid based on the given
1833 * sid and the mls portion of mls_sid.
1835 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1837 struct context *context1;
1838 struct context *context2;
1839 struct context newcon;
1844 if (!ss_initialized || !selinux_mls_enabled) {
1849 context_init(&newcon);
1852 context1 = sidtab_search(&sidtab, sid);
1854 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1860 context2 = sidtab_search(&sidtab, mls_sid);
1862 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1868 newcon.user = context1->user;
1869 newcon.role = context1->role;
1870 newcon.type = context1->type;
1871 rc = mls_copy_context(&newcon, context2);
1876 /* Check the validity of the new context. */
1877 if (!policydb_context_isvalid(&policydb, &newcon)) {
1878 rc = convert_context_handle_invalid_context(&newcon);
1883 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1887 if (!context_struct_to_string(&newcon, &s, &len)) {
1888 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1889 "security_sid_mls_copy: invalid context %s", s);
1895 context_destroy(&newcon);
1900 struct selinux_audit_rule {
1902 struct context au_ctxt;
1905 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1908 context_destroy(&rule->au_ctxt);
1913 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1914 struct selinux_audit_rule **rule)
1916 struct selinux_audit_rule *tmprule;
1917 struct role_datum *roledatum;
1918 struct type_datum *typedatum;
1919 struct user_datum *userdatum;
1924 if (!ss_initialized)
1928 case AUDIT_SUBJ_USER:
1929 case AUDIT_SUBJ_ROLE:
1930 case AUDIT_SUBJ_TYPE:
1931 case AUDIT_OBJ_USER:
1932 case AUDIT_OBJ_ROLE:
1933 case AUDIT_OBJ_TYPE:
1934 /* only 'equals' and 'not equals' fit user, role, and type */
1935 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1938 case AUDIT_SUBJ_SEN:
1939 case AUDIT_SUBJ_CLR:
1940 case AUDIT_OBJ_LEV_LOW:
1941 case AUDIT_OBJ_LEV_HIGH:
1942 /* we do not allow a range, indicated by the presense of '-' */
1943 if (strchr(rulestr, '-'))
1947 /* only the above fields are valid */
1951 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
1955 context_init(&tmprule->au_ctxt);
1959 tmprule->au_seqno = latest_granting;
1962 case AUDIT_SUBJ_USER:
1963 case AUDIT_OBJ_USER:
1964 userdatum = hashtab_search(policydb.p_users.table, rulestr);
1968 tmprule->au_ctxt.user = userdatum->value;
1970 case AUDIT_SUBJ_ROLE:
1971 case AUDIT_OBJ_ROLE:
1972 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
1976 tmprule->au_ctxt.role = roledatum->value;
1978 case AUDIT_SUBJ_TYPE:
1979 case AUDIT_OBJ_TYPE:
1980 typedatum = hashtab_search(policydb.p_types.table, rulestr);
1984 tmprule->au_ctxt.type = typedatum->value;
1986 case AUDIT_SUBJ_SEN:
1987 case AUDIT_SUBJ_CLR:
1988 case AUDIT_OBJ_LEV_LOW:
1989 case AUDIT_OBJ_LEV_HIGH:
1990 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
1997 selinux_audit_rule_free(tmprule);
2006 int selinux_audit_rule_match(u32 ctxid, u32 field, u32 op,
2007 struct selinux_audit_rule *rule,
2008 struct audit_context *actx)
2010 struct context *ctxt;
2011 struct mls_level *level;
2015 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2016 "selinux_audit_rule_match: missing rule\n");
2022 if (rule->au_seqno < latest_granting) {
2023 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2024 "selinux_audit_rule_match: stale rule\n");
2029 ctxt = sidtab_search(&sidtab, ctxid);
2031 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2032 "selinux_audit_rule_match: unrecognized SID %d\n",
2038 /* a field/op pair that is not caught here will simply fall through
2041 case AUDIT_SUBJ_USER:
2042 case AUDIT_OBJ_USER:
2045 match = (ctxt->user == rule->au_ctxt.user);
2047 case AUDIT_NOT_EQUAL:
2048 match = (ctxt->user != rule->au_ctxt.user);
2052 case AUDIT_SUBJ_ROLE:
2053 case AUDIT_OBJ_ROLE:
2056 match = (ctxt->role == rule->au_ctxt.role);
2058 case AUDIT_NOT_EQUAL:
2059 match = (ctxt->role != rule->au_ctxt.role);
2063 case AUDIT_SUBJ_TYPE:
2064 case AUDIT_OBJ_TYPE:
2067 match = (ctxt->type == rule->au_ctxt.type);
2069 case AUDIT_NOT_EQUAL:
2070 match = (ctxt->type != rule->au_ctxt.type);
2074 case AUDIT_SUBJ_SEN:
2075 case AUDIT_SUBJ_CLR:
2076 case AUDIT_OBJ_LEV_LOW:
2077 case AUDIT_OBJ_LEV_HIGH:
2078 level = ((field == AUDIT_SUBJ_SEN ||
2079 field == AUDIT_OBJ_LEV_LOW) ?
2080 &ctxt->range.level[0] : &ctxt->range.level[1]);
2083 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2086 case AUDIT_NOT_EQUAL:
2087 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2090 case AUDIT_LESS_THAN:
2091 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2093 !mls_level_eq(&rule->au_ctxt.range.level[0],
2096 case AUDIT_LESS_THAN_OR_EQUAL:
2097 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2100 case AUDIT_GREATER_THAN:
2101 match = (mls_level_dom(level,
2102 &rule->au_ctxt.range.level[0]) &&
2103 !mls_level_eq(level,
2104 &rule->au_ctxt.range.level[0]));
2106 case AUDIT_GREATER_THAN_OR_EQUAL:
2107 match = mls_level_dom(level,
2108 &rule->au_ctxt.range.level[0]);
2118 static int (*aurule_callback)(void) = NULL;
2120 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2121 u16 class, u32 perms, u32 *retained)
2125 if (event == AVC_CALLBACK_RESET && aurule_callback)
2126 err = aurule_callback();
2130 static int __init aurule_init(void)
2134 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2135 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2137 panic("avc_add_callback() failed, error %d\n", err);
2141 __initcall(aurule_init);
2143 void selinux_audit_set_callback(int (*callback)(void))
2145 aurule_callback = callback;
2148 #ifdef CONFIG_NETLABEL
2150 * This is the structure we store inside the NetLabel cache block.
2152 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2153 #define NETLBL_CACHE_T_NONE 0
2154 #define NETLBL_CACHE_T_SID 1
2155 #define NETLBL_CACHE_T_MLS 2
2156 struct netlbl_cache {
2160 struct mls_range mls_label;
2165 * selinux_netlbl_cache_free - Free the NetLabel cached data
2166 * @data: the data to free
2169 * This function is intended to be used as the free() callback inside the
2170 * netlbl_lsm_cache structure.
2173 static void selinux_netlbl_cache_free(const void *data)
2175 struct netlbl_cache *cache = NETLBL_CACHE(data);
2176 switch (cache->type) {
2177 case NETLBL_CACHE_T_MLS:
2178 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2185 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2187 * @ctx: the SELinux context
2190 * Attempt to cache the context in @ctx, which was derived from the packet in
2191 * @skb, in the NetLabel subsystem cache.
2194 static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
2196 struct netlbl_cache *cache = NULL;
2197 struct netlbl_lsm_secattr secattr;
2199 netlbl_secattr_init(&secattr);
2201 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2203 goto netlbl_cache_add_failure;
2204 secattr.cache.free = selinux_netlbl_cache_free;
2205 secattr.cache.data = (void *)cache;
2207 cache->type = NETLBL_CACHE_T_MLS;
2208 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2209 &ctx->range.level[0].cat) != 0)
2210 goto netlbl_cache_add_failure;
2211 cache->data.mls_label.level[1].cat.highbit =
2212 cache->data.mls_label.level[0].cat.highbit;
2213 cache->data.mls_label.level[1].cat.node =
2214 cache->data.mls_label.level[0].cat.node;
2215 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2216 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2218 if (netlbl_cache_add(skb, &secattr) != 0)
2219 goto netlbl_cache_add_failure;
2223 netlbl_cache_add_failure:
2224 netlbl_secattr_destroy(&secattr, 1);
2228 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2231 * Invalidate the NetLabel security attribute mapping cache.
2234 void selinux_netlbl_cache_invalidate(void)
2236 netlbl_cache_invalidate();
2240 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2241 * @skb: the network packet
2242 * @secattr: the NetLabel packet security attributes
2243 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2244 * @sid: the SELinux SID
2247 * Convert the given NetLabel packet security attributes in @secattr into a
2248 * SELinux SID. If the @secattr field does not contain a full SELinux
2249 * SID/context then use the context in @base_sid as the foundation. If @skb
2250 * is not NULL attempt to cache as much data as possibile. Returns zero on
2251 * success, negative values on failure.
2254 static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
2255 struct netlbl_lsm_secattr *secattr,
2260 struct context *ctx;
2261 struct context ctx_new;
2262 struct netlbl_cache *cache;
2266 if (secattr->cache.data) {
2267 cache = NETLBL_CACHE(secattr->cache.data);
2268 switch (cache->type) {
2269 case NETLBL_CACHE_T_SID:
2270 *sid = cache->data.sid;
2273 case NETLBL_CACHE_T_MLS:
2274 ctx = sidtab_search(&sidtab, base_sid);
2276 goto netlbl_secattr_to_sid_return;
2278 ctx_new.user = ctx->user;
2279 ctx_new.role = ctx->role;
2280 ctx_new.type = ctx->type;
2281 ctx_new.range.level[0].sens =
2282 cache->data.mls_label.level[0].sens;
2283 ctx_new.range.level[0].cat.highbit =
2284 cache->data.mls_label.level[0].cat.highbit;
2285 ctx_new.range.level[0].cat.node =
2286 cache->data.mls_label.level[0].cat.node;
2287 ctx_new.range.level[1].sens =
2288 cache->data.mls_label.level[1].sens;
2289 ctx_new.range.level[1].cat.highbit =
2290 cache->data.mls_label.level[1].cat.highbit;
2291 ctx_new.range.level[1].cat.node =
2292 cache->data.mls_label.level[1].cat.node;
2294 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2297 goto netlbl_secattr_to_sid_return;
2299 } else if (secattr->mls_lvl_vld) {
2300 ctx = sidtab_search(&sidtab, base_sid);
2302 goto netlbl_secattr_to_sid_return;
2304 ctx_new.user = ctx->user;
2305 ctx_new.role = ctx->role;
2306 ctx_new.type = ctx->type;
2307 mls_import_lvl(&ctx_new, secattr->mls_lvl, secattr->mls_lvl);
2308 if (secattr->mls_cat) {
2309 if (mls_import_cat(&ctx_new,
2311 secattr->mls_cat_len,
2314 goto netlbl_secattr_to_sid_return;
2315 ctx_new.range.level[1].cat.highbit =
2316 ctx_new.range.level[0].cat.highbit;
2317 ctx_new.range.level[1].cat.node =
2318 ctx_new.range.level[0].cat.node;
2320 ebitmap_init(&ctx_new.range.level[0].cat);
2321 ebitmap_init(&ctx_new.range.level[1].cat);
2323 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2324 goto netlbl_secattr_to_sid_return_cleanup;
2326 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2328 goto netlbl_secattr_to_sid_return_cleanup;
2331 selinux_netlbl_cache_add(skb, &ctx_new);
2332 ebitmap_destroy(&ctx_new.range.level[0].cat);
2334 *sid = SECINITSID_UNLABELED;
2338 netlbl_secattr_to_sid_return:
2341 netlbl_secattr_to_sid_return_cleanup:
2342 ebitmap_destroy(&ctx_new.range.level[0].cat);
2343 goto netlbl_secattr_to_sid_return;
2347 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2349 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2353 * Call the NetLabel mechanism to get the security attributes of the given
2354 * packet and use those attributes to determine the correct context/SID to
2355 * assign to the packet. Returns zero on success, negative values on failure.
2358 static int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
2363 struct netlbl_lsm_secattr secattr;
2365 netlbl_secattr_init(&secattr);
2366 rc = netlbl_skbuff_getattr(skb, &secattr);
2368 rc = selinux_netlbl_secattr_to_sid(skb,
2372 netlbl_secattr_destroy(&secattr, 0);
2378 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2379 * @sock: the socket to label
2380 * @sid: the SID to use
2383 * Attempt to label a socket using the NetLabel mechanism using the given
2384 * SID. Returns zero values on success, negative values on failure.
2387 static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
2390 struct sk_security_struct *sksec = sock->sk->sk_security;
2391 struct netlbl_lsm_secattr secattr;
2392 struct context *ctx;
2394 if (!ss_initialized)
2399 ctx = sidtab_search(&sidtab, sid);
2401 goto netlbl_socket_setsid_return;
2403 netlbl_secattr_init(&secattr);
2404 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2406 mls_export_lvl(ctx, &secattr.mls_lvl, NULL);
2407 secattr.mls_lvl_vld = 1;
2410 &secattr.mls_cat_len,
2414 rc = netlbl_socket_setattr(sock, &secattr);
2416 sksec->nlbl_state = NLBL_LABELED;
2418 netlbl_secattr_destroy(&secattr, 0);
2420 netlbl_socket_setsid_return:
2426 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2427 * @ssec: the sk_security_struct
2428 * @family: the socket family
2431 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2435 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
2438 if (family == PF_INET)
2439 ssec->nlbl_state = NLBL_REQUIRE;
2441 ssec->nlbl_state = NLBL_UNSET;
2445 * selinux_netlbl_sk_clone_security - Copy the NetLabel fields
2446 * @ssec: the original sk_security_struct
2447 * @newssec: the cloned sk_security_struct
2450 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2454 void selinux_netlbl_sk_clone_security(struct sk_security_struct *ssec,
2455 struct sk_security_struct *newssec)
2457 newssec->sclass = ssec->sclass;
2458 if (ssec->nlbl_state != NLBL_UNSET)
2459 newssec->nlbl_state = NLBL_REQUIRE;
2461 newssec->nlbl_state = NLBL_UNSET;
2465 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2466 * @sock: the socket to label
2467 * @sock_family: the socket family
2468 * @sid: the SID to use
2471 * Attempt to label a socket using the NetLabel mechanism using the given
2472 * SID. Returns zero values on success, negative values on failure.
2475 int selinux_netlbl_socket_post_create(struct socket *sock,
2479 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2480 struct sk_security_struct *sksec = sock->sk->sk_security;
2482 sksec->sclass = isec->sclass;
2484 if (sock_family != PF_INET)
2487 sksec->nlbl_state = NLBL_REQUIRE;
2488 return selinux_netlbl_socket_setsid(sock, sid);
2492 * selinux_netlbl_sock_graft - Netlabel the new socket
2493 * @sk: the new connection
2494 * @sock: the new socket
2497 * The connection represented by @sk is being grafted onto @sock so set the
2498 * socket's NetLabel to match the SID of @sk.
2501 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
2503 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2504 struct sk_security_struct *sksec = sk->sk_security;
2506 sksec->sclass = isec->sclass;
2508 if (sk->sk_family != PF_INET)
2511 sksec->nlbl_state = NLBL_REQUIRE;
2512 sksec->peer_sid = sksec->sid;
2514 /* Try to set the NetLabel on the socket to save time later, if we fail
2515 * here we will pick up the pieces in later calls to
2516 * selinux_netlbl_inode_permission(). */
2517 selinux_netlbl_socket_setsid(sock, sksec->sid);
2521 * selinux_netlbl_inet_conn_request - Handle a new connection request
2523 * @sock_sid: the SID of the parent socket
2526 * If present, use the security attributes of the packet in @skb and the
2527 * parent sock's SID to arrive at a SID for the new child sock. Returns the
2528 * SID of the connection or SECSID_NULL on failure.
2531 u32 selinux_netlbl_inet_conn_request(struct sk_buff *skb, u32 sock_sid)
2536 rc = selinux_netlbl_skbuff_getsid(skb, sock_sid, &peer_sid);
2540 if (peer_sid == SECINITSID_UNLABELED)
2547 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
2548 * @inode: the file descriptor's inode
2549 * @mask: the permission mask
2552 * Looks at a file's inode and if it is marked as a socket protected by
2553 * NetLabel then verify that the socket has been labeled, if not try to label
2554 * the socket now with the inode's SID. Returns zero on success, negative
2555 * values on failure.
2558 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
2561 struct inode_security_struct *isec;
2562 struct sk_security_struct *sksec;
2563 struct socket *sock;
2565 if (!S_ISSOCK(inode->i_mode))
2568 sock = SOCKET_I(inode);
2569 isec = inode->i_security;
2570 sksec = sock->sk->sk_security;
2572 if (unlikely(sksec->nlbl_state == NLBL_REQUIRE &&
2573 (mask & (MAY_WRITE | MAY_APPEND)))) {
2574 lock_sock(sock->sk);
2575 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2576 release_sock(sock->sk);
2585 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2586 * @sksec: the sock's sk_security_struct
2588 * @ad: the audit data
2591 * Fetch the NetLabel security attributes from @skb and perform an access check
2592 * against the receiving socket. Returns zero on success, negative values on
2596 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
2597 struct sk_buff *skb,
2598 struct avc_audit_data *ad)
2604 rc = selinux_netlbl_skbuff_getsid(skb, sksec->sid, &netlbl_sid);
2608 if (netlbl_sid == SECINITSID_UNLABELED)
2611 switch (sksec->sclass) {
2612 case SECCLASS_UDP_SOCKET:
2613 recv_perm = UDP_SOCKET__RECV_MSG;
2615 case SECCLASS_TCP_SOCKET:
2616 recv_perm = TCP_SOCKET__RECV_MSG;
2619 recv_perm = RAWIP_SOCKET__RECV_MSG;
2622 rc = avc_has_perm(sksec->sid,
2630 netlbl_skbuff_err(skb, rc);
2635 * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID
2639 * Examine @sock to find the connected peer's SID. Returns the SID on success
2640 * or SECSID_NULL on error.
2643 u32 selinux_netlbl_socket_getpeersec_stream(struct socket *sock)
2645 struct sk_security_struct *sksec = sock->sk->sk_security;
2647 if (sksec->peer_sid == SECINITSID_UNLABELED)
2650 return sksec->peer_sid;
2654 * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet
2658 * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on
2659 * success, SECSID_NULL on error.
2662 u32 selinux_netlbl_socket_getpeersec_dgram(struct sk_buff *skb)
2665 struct sock *sk = skb->sk;
2666 struct inode_security_struct *isec;
2668 if (sk == NULL || sk->sk_socket == NULL)
2671 isec = SOCK_INODE(sk->sk_socket)->i_security;
2672 if (selinux_netlbl_skbuff_getsid(skb, isec->sid, &peer_sid) != 0)
2674 if (peer_sid == SECINITSID_UNLABELED)
2679 #endif /* CONFIG_NETLABEL */