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);
1021 /* Clone the SID into the new SID table. */
1022 static int clone_sid(u32 sid,
1023 struct context *context,
1026 struct sidtab *s = arg;
1028 return sidtab_insert(s, sid, context);
1031 static inline int convert_context_handle_invalid_context(struct context *context)
1035 if (selinux_enforcing) {
1041 context_struct_to_string(context, &s, &len);
1042 printk(KERN_ERR "security: context %s is invalid\n", s);
1048 struct convert_context_args {
1049 struct policydb *oldp;
1050 struct policydb *newp;
1054 * Convert the values in the security context
1055 * structure `c' from the values specified
1056 * in the policy `p->oldp' to the values specified
1057 * in the policy `p->newp'. Verify that the
1058 * context is valid under the new policy.
1060 static int convert_context(u32 key,
1064 struct convert_context_args *args;
1065 struct context oldc;
1066 struct role_datum *role;
1067 struct type_datum *typdatum;
1068 struct user_datum *usrdatum;
1075 rc = context_cpy(&oldc, c);
1081 /* Convert the user. */
1082 usrdatum = hashtab_search(args->newp->p_users.table,
1083 args->oldp->p_user_val_to_name[c->user - 1]);
1087 c->user = usrdatum->value;
1089 /* Convert the role. */
1090 role = hashtab_search(args->newp->p_roles.table,
1091 args->oldp->p_role_val_to_name[c->role - 1]);
1095 c->role = role->value;
1097 /* Convert the type. */
1098 typdatum = hashtab_search(args->newp->p_types.table,
1099 args->oldp->p_type_val_to_name[c->type - 1]);
1103 c->type = typdatum->value;
1105 rc = mls_convert_context(args->oldp, args->newp, c);
1109 /* Check the validity of the new context. */
1110 if (!policydb_context_isvalid(args->newp, c)) {
1111 rc = convert_context_handle_invalid_context(&oldc);
1116 context_destroy(&oldc);
1120 context_struct_to_string(&oldc, &s, &len);
1121 context_destroy(&oldc);
1122 printk(KERN_ERR "security: invalidating context %s\n", s);
1127 extern void selinux_complete_init(void);
1130 * security_load_policy - Load a security policy configuration.
1131 * @data: binary policy data
1132 * @len: length of data in bytes
1134 * Load a new set of security policy configuration data,
1135 * validate it and convert the SID table as necessary.
1136 * This function will flush the access vector cache after
1137 * loading the new policy.
1139 int security_load_policy(void *data, size_t len)
1141 struct policydb oldpolicydb, newpolicydb;
1142 struct sidtab oldsidtab, newsidtab;
1143 struct convert_context_args args;
1146 struct policy_file file = { data, len }, *fp = &file;
1150 if (!ss_initialized) {
1152 if (policydb_read(&policydb, fp)) {
1154 avtab_cache_destroy();
1157 if (policydb_load_isids(&policydb, &sidtab)) {
1159 policydb_destroy(&policydb);
1160 avtab_cache_destroy();
1163 policydb_loaded_version = policydb.policyvers;
1165 seqno = ++latest_granting;
1167 selinux_complete_init();
1168 avc_ss_reset(seqno);
1169 selnl_notify_policyload(seqno);
1170 selinux_netlbl_cache_invalidate();
1175 sidtab_hash_eval(&sidtab, "sids");
1178 if (policydb_read(&newpolicydb, fp)) {
1183 sidtab_init(&newsidtab);
1185 /* Clone the SID table. */
1186 sidtab_shutdown(&sidtab);
1187 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1192 /* Convert the internal representations of contexts
1193 in the new SID table and remove invalid SIDs. */
1194 args.oldp = &policydb;
1195 args.newp = &newpolicydb;
1196 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1198 /* Save the old policydb and SID table to free later. */
1199 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1200 sidtab_set(&oldsidtab, &sidtab);
1202 /* Install the new policydb and SID table. */
1204 memcpy(&policydb, &newpolicydb, sizeof policydb);
1205 sidtab_set(&sidtab, &newsidtab);
1206 seqno = ++latest_granting;
1207 policydb_loaded_version = policydb.policyvers;
1211 /* Free the old policydb and SID table. */
1212 policydb_destroy(&oldpolicydb);
1213 sidtab_destroy(&oldsidtab);
1215 avc_ss_reset(seqno);
1216 selnl_notify_policyload(seqno);
1217 selinux_netlbl_cache_invalidate();
1223 sidtab_destroy(&newsidtab);
1224 policydb_destroy(&newpolicydb);
1230 * security_port_sid - Obtain the SID for a port.
1231 * @domain: communication domain aka address family
1232 * @type: socket type
1233 * @protocol: protocol number
1234 * @port: port number
1235 * @out_sid: security identifier
1237 int security_port_sid(u16 domain,
1248 c = policydb.ocontexts[OCON_PORT];
1250 if (c->u.port.protocol == protocol &&
1251 c->u.port.low_port <= port &&
1252 c->u.port.high_port >= port)
1259 rc = sidtab_context_to_sid(&sidtab,
1265 *out_sid = c->sid[0];
1267 *out_sid = SECINITSID_PORT;
1276 * security_netif_sid - Obtain the SID for a network interface.
1277 * @name: interface name
1278 * @if_sid: interface SID
1279 * @msg_sid: default SID for received packets
1281 int security_netif_sid(char *name,
1290 c = policydb.ocontexts[OCON_NETIF];
1292 if (strcmp(name, c->u.name) == 0)
1298 if (!c->sid[0] || !c->sid[1]) {
1299 rc = sidtab_context_to_sid(&sidtab,
1304 rc = sidtab_context_to_sid(&sidtab,
1310 *if_sid = c->sid[0];
1311 *msg_sid = c->sid[1];
1313 *if_sid = SECINITSID_NETIF;
1314 *msg_sid = SECINITSID_NETMSG;
1322 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1326 for(i = 0; i < 4; i++)
1327 if(addr[i] != (input[i] & mask[i])) {
1336 * security_node_sid - Obtain the SID for a node (host).
1337 * @domain: communication domain aka address family
1339 * @addrlen: address length in bytes
1340 * @out_sid: security identifier
1342 int security_node_sid(u16 domain,
1356 if (addrlen != sizeof(u32)) {
1361 addr = *((u32 *)addrp);
1363 c = policydb.ocontexts[OCON_NODE];
1365 if (c->u.node.addr == (addr & c->u.node.mask))
1373 if (addrlen != sizeof(u64) * 2) {
1377 c = policydb.ocontexts[OCON_NODE6];
1379 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1387 *out_sid = SECINITSID_NODE;
1393 rc = sidtab_context_to_sid(&sidtab,
1399 *out_sid = c->sid[0];
1401 *out_sid = SECINITSID_NODE;
1412 * security_get_user_sids - Obtain reachable SIDs for a user.
1413 * @fromsid: starting SID
1414 * @username: username
1415 * @sids: array of reachable SIDs for user
1416 * @nel: number of elements in @sids
1418 * Generate the set of SIDs for legal security contexts
1419 * for a given user that can be reached by @fromsid.
1420 * Set *@sids to point to a dynamically allocated
1421 * array containing the set of SIDs. Set *@nel to the
1422 * number of elements in the array.
1425 int security_get_user_sids(u32 fromsid,
1430 struct context *fromcon, usercon;
1431 u32 *mysids, *mysids2, sid;
1432 u32 mynel = 0, maxnel = SIDS_NEL;
1433 struct user_datum *user;
1434 struct role_datum *role;
1435 struct av_decision avd;
1436 struct ebitmap_node *rnode, *tnode;
1439 if (!ss_initialized) {
1447 fromcon = sidtab_search(&sidtab, fromsid);
1453 user = hashtab_search(policydb.p_users.table, username);
1458 usercon.user = user->value;
1460 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1466 ebitmap_for_each_bit(&user->roles, rnode, i) {
1467 if (!ebitmap_node_get_bit(rnode, i))
1469 role = policydb.role_val_to_struct[i];
1471 ebitmap_for_each_bit(&role->types, tnode, j) {
1472 if (!ebitmap_node_get_bit(tnode, j))
1476 if (mls_setup_user_range(fromcon, user, &usercon))
1479 rc = context_struct_compute_av(fromcon, &usercon,
1481 PROCESS__TRANSITION,
1483 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1485 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1490 if (mynel < maxnel) {
1491 mysids[mynel++] = sid;
1494 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1500 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1503 mysids[mynel++] = sid;
1518 * security_genfs_sid - Obtain a SID for a file in a filesystem
1519 * @fstype: filesystem type
1520 * @path: path from root of mount
1521 * @sclass: file security class
1522 * @sid: SID for path
1524 * Obtain a SID to use for a file in a filesystem that
1525 * cannot support xattr or use a fixed labeling behavior like
1526 * transition SIDs or task SIDs.
1528 int security_genfs_sid(const char *fstype,
1534 struct genfs *genfs;
1536 int rc = 0, cmp = 0;
1540 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1541 cmp = strcmp(fstype, genfs->fstype);
1546 if (!genfs || cmp) {
1547 *sid = SECINITSID_UNLABELED;
1552 for (c = genfs->head; c; c = c->next) {
1553 len = strlen(c->u.name);
1554 if ((!c->v.sclass || sclass == c->v.sclass) &&
1555 (strncmp(c->u.name, path, len) == 0))
1560 *sid = SECINITSID_UNLABELED;
1566 rc = sidtab_context_to_sid(&sidtab,
1580 * security_fs_use - Determine how to handle labeling for a filesystem.
1581 * @fstype: filesystem type
1582 * @behavior: labeling behavior
1583 * @sid: SID for filesystem (superblock)
1585 int security_fs_use(
1587 unsigned int *behavior,
1595 c = policydb.ocontexts[OCON_FSUSE];
1597 if (strcmp(fstype, c->u.name) == 0)
1603 *behavior = c->v.behavior;
1605 rc = sidtab_context_to_sid(&sidtab,
1613 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1615 *behavior = SECURITY_FS_USE_NONE;
1618 *behavior = SECURITY_FS_USE_GENFS;
1627 int security_get_bools(int *len, char ***names, int **values)
1629 int i, rc = -ENOMEM;
1635 *len = policydb.p_bools.nprim;
1641 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1645 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1649 for (i = 0; i < *len; i++) {
1651 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1652 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1653 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1656 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1657 (*names)[i][name_len - 1] = 0;
1665 for (i = 0; i < *len; i++)
1673 int security_set_bools(int len, int *values)
1676 int lenp, seqno = 0;
1677 struct cond_node *cur;
1681 lenp = policydb.p_bools.nprim;
1687 for (i = 0; i < len; i++) {
1688 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1689 audit_log(current->audit_context, GFP_ATOMIC,
1690 AUDIT_MAC_CONFIG_CHANGE,
1691 "bool=%s val=%d old_val=%d auid=%u",
1692 policydb.p_bool_val_to_name[i],
1694 policydb.bool_val_to_struct[i]->state,
1695 audit_get_loginuid(current->audit_context));
1698 policydb.bool_val_to_struct[i]->state = 1;
1700 policydb.bool_val_to_struct[i]->state = 0;
1704 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1705 rc = evaluate_cond_node(&policydb, cur);
1710 seqno = ++latest_granting;
1715 avc_ss_reset(seqno);
1716 selnl_notify_policyload(seqno);
1721 int security_get_bool_value(int bool)
1728 len = policydb.p_bools.nprim;
1734 rc = policydb.bool_val_to_struct[bool]->state;
1741 * security_sid_mls_copy() - computes a new sid based on the given
1742 * sid and the mls portion of mls_sid.
1744 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1746 struct context *context1;
1747 struct context *context2;
1748 struct context newcon;
1753 if (!ss_initialized || !selinux_mls_enabled) {
1758 context_init(&newcon);
1761 context1 = sidtab_search(&sidtab, sid);
1763 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1769 context2 = sidtab_search(&sidtab, mls_sid);
1771 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1777 newcon.user = context1->user;
1778 newcon.role = context1->role;
1779 newcon.type = context1->type;
1780 rc = mls_copy_context(&newcon, context2);
1785 /* Check the validity of the new context. */
1786 if (!policydb_context_isvalid(&policydb, &newcon)) {
1787 rc = convert_context_handle_invalid_context(&newcon);
1792 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1796 if (!context_struct_to_string(&newcon, &s, &len)) {
1797 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1798 "security_sid_mls_copy: invalid context %s", s);
1804 context_destroy(&newcon);
1809 struct selinux_audit_rule {
1811 struct context au_ctxt;
1814 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1817 context_destroy(&rule->au_ctxt);
1822 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1823 struct selinux_audit_rule **rule)
1825 struct selinux_audit_rule *tmprule;
1826 struct role_datum *roledatum;
1827 struct type_datum *typedatum;
1828 struct user_datum *userdatum;
1833 if (!ss_initialized)
1837 case AUDIT_SUBJ_USER:
1838 case AUDIT_SUBJ_ROLE:
1839 case AUDIT_SUBJ_TYPE:
1840 case AUDIT_OBJ_USER:
1841 case AUDIT_OBJ_ROLE:
1842 case AUDIT_OBJ_TYPE:
1843 /* only 'equals' and 'not equals' fit user, role, and type */
1844 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1847 case AUDIT_SUBJ_SEN:
1848 case AUDIT_SUBJ_CLR:
1849 case AUDIT_OBJ_LEV_LOW:
1850 case AUDIT_OBJ_LEV_HIGH:
1851 /* we do not allow a range, indicated by the presense of '-' */
1852 if (strchr(rulestr, '-'))
1856 /* only the above fields are valid */
1860 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
1864 context_init(&tmprule->au_ctxt);
1868 tmprule->au_seqno = latest_granting;
1871 case AUDIT_SUBJ_USER:
1872 case AUDIT_OBJ_USER:
1873 userdatum = hashtab_search(policydb.p_users.table, rulestr);
1877 tmprule->au_ctxt.user = userdatum->value;
1879 case AUDIT_SUBJ_ROLE:
1880 case AUDIT_OBJ_ROLE:
1881 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
1885 tmprule->au_ctxt.role = roledatum->value;
1887 case AUDIT_SUBJ_TYPE:
1888 case AUDIT_OBJ_TYPE:
1889 typedatum = hashtab_search(policydb.p_types.table, rulestr);
1893 tmprule->au_ctxt.type = typedatum->value;
1895 case AUDIT_SUBJ_SEN:
1896 case AUDIT_SUBJ_CLR:
1897 case AUDIT_OBJ_LEV_LOW:
1898 case AUDIT_OBJ_LEV_HIGH:
1899 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
1906 selinux_audit_rule_free(tmprule);
1915 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
1916 struct selinux_audit_rule *rule,
1917 struct audit_context *actx)
1919 struct context *ctxt;
1920 struct mls_level *level;
1924 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1925 "selinux_audit_rule_match: missing rule\n");
1931 if (rule->au_seqno < latest_granting) {
1932 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1933 "selinux_audit_rule_match: stale rule\n");
1938 ctxt = sidtab_search(&sidtab, sid);
1940 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1941 "selinux_audit_rule_match: unrecognized SID %d\n",
1947 /* a field/op pair that is not caught here will simply fall through
1950 case AUDIT_SUBJ_USER:
1951 case AUDIT_OBJ_USER:
1954 match = (ctxt->user == rule->au_ctxt.user);
1956 case AUDIT_NOT_EQUAL:
1957 match = (ctxt->user != rule->au_ctxt.user);
1961 case AUDIT_SUBJ_ROLE:
1962 case AUDIT_OBJ_ROLE:
1965 match = (ctxt->role == rule->au_ctxt.role);
1967 case AUDIT_NOT_EQUAL:
1968 match = (ctxt->role != rule->au_ctxt.role);
1972 case AUDIT_SUBJ_TYPE:
1973 case AUDIT_OBJ_TYPE:
1976 match = (ctxt->type == rule->au_ctxt.type);
1978 case AUDIT_NOT_EQUAL:
1979 match = (ctxt->type != rule->au_ctxt.type);
1983 case AUDIT_SUBJ_SEN:
1984 case AUDIT_SUBJ_CLR:
1985 case AUDIT_OBJ_LEV_LOW:
1986 case AUDIT_OBJ_LEV_HIGH:
1987 level = ((field == AUDIT_SUBJ_SEN ||
1988 field == AUDIT_OBJ_LEV_LOW) ?
1989 &ctxt->range.level[0] : &ctxt->range.level[1]);
1992 match = mls_level_eq(&rule->au_ctxt.range.level[0],
1995 case AUDIT_NOT_EQUAL:
1996 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
1999 case AUDIT_LESS_THAN:
2000 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2002 !mls_level_eq(&rule->au_ctxt.range.level[0],
2005 case AUDIT_LESS_THAN_OR_EQUAL:
2006 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2009 case AUDIT_GREATER_THAN:
2010 match = (mls_level_dom(level,
2011 &rule->au_ctxt.range.level[0]) &&
2012 !mls_level_eq(level,
2013 &rule->au_ctxt.range.level[0]));
2015 case AUDIT_GREATER_THAN_OR_EQUAL:
2016 match = mls_level_dom(level,
2017 &rule->au_ctxt.range.level[0]);
2027 static int (*aurule_callback)(void) = NULL;
2029 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2030 u16 class, u32 perms, u32 *retained)
2034 if (event == AVC_CALLBACK_RESET && aurule_callback)
2035 err = aurule_callback();
2039 static int __init aurule_init(void)
2043 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2044 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2046 panic("avc_add_callback() failed, error %d\n", err);
2050 __initcall(aurule_init);
2052 void selinux_audit_set_callback(int (*callback)(void))
2054 aurule_callback = callback;
2057 #ifdef CONFIG_NETLABEL
2059 * This is the structure we store inside the NetLabel cache block.
2061 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2062 #define NETLBL_CACHE_T_NONE 0
2063 #define NETLBL_CACHE_T_SID 1
2064 #define NETLBL_CACHE_T_MLS 2
2065 struct netlbl_cache {
2069 struct mls_range mls_label;
2074 * selinux_netlbl_cache_free - Free the NetLabel cached data
2075 * @data: the data to free
2078 * This function is intended to be used as the free() callback inside the
2079 * netlbl_lsm_cache structure.
2082 static void selinux_netlbl_cache_free(const void *data)
2084 struct netlbl_cache *cache;
2089 cache = NETLBL_CACHE(data);
2090 switch (cache->type) {
2091 case NETLBL_CACHE_T_MLS:
2092 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2099 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2101 * @ctx: the SELinux context
2104 * Attempt to cache the context in @ctx, which was derived from the packet in
2105 * @skb, in the NetLabel subsystem cache.
2108 static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
2110 struct netlbl_cache *cache = NULL;
2111 struct netlbl_lsm_secattr secattr;
2113 netlbl_secattr_init(&secattr);
2114 secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2115 if (secattr.cache == NULL)
2116 goto netlbl_cache_add_return;
2118 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2120 goto netlbl_cache_add_return;
2121 secattr.cache->free = selinux_netlbl_cache_free;
2122 secattr.cache->data = (void *)cache;
2124 cache->type = NETLBL_CACHE_T_MLS;
2125 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2126 &ctx->range.level[0].cat) != 0)
2127 goto netlbl_cache_add_return;
2128 cache->data.mls_label.level[1].cat.highbit =
2129 cache->data.mls_label.level[0].cat.highbit;
2130 cache->data.mls_label.level[1].cat.node =
2131 cache->data.mls_label.level[0].cat.node;
2132 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2133 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2135 netlbl_cache_add(skb, &secattr);
2137 netlbl_cache_add_return:
2138 netlbl_secattr_destroy(&secattr);
2142 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2145 * Invalidate the NetLabel security attribute mapping cache.
2148 void selinux_netlbl_cache_invalidate(void)
2150 netlbl_cache_invalidate();
2154 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2155 * @skb: the network packet
2156 * @secattr: the NetLabel packet security attributes
2157 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2158 * @sid: the SELinux SID
2161 * Convert the given NetLabel packet security attributes in @secattr into a
2162 * SELinux SID. If the @secattr field does not contain a full SELinux
2163 * SID/context then use the context in @base_sid as the foundation. If @skb
2164 * is not NULL attempt to cache as much data as possibile. Returns zero on
2165 * success, negative values on failure.
2168 static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
2169 struct netlbl_lsm_secattr *secattr,
2174 struct context *ctx;
2175 struct context ctx_new;
2176 struct netlbl_cache *cache;
2180 if (secattr->cache) {
2181 cache = NETLBL_CACHE(secattr->cache->data);
2182 switch (cache->type) {
2183 case NETLBL_CACHE_T_SID:
2184 *sid = cache->data.sid;
2187 case NETLBL_CACHE_T_MLS:
2188 ctx = sidtab_search(&sidtab, base_sid);
2190 goto netlbl_secattr_to_sid_return;
2192 ctx_new.user = ctx->user;
2193 ctx_new.role = ctx->role;
2194 ctx_new.type = ctx->type;
2195 ctx_new.range.level[0].sens =
2196 cache->data.mls_label.level[0].sens;
2197 ctx_new.range.level[0].cat.highbit =
2198 cache->data.mls_label.level[0].cat.highbit;
2199 ctx_new.range.level[0].cat.node =
2200 cache->data.mls_label.level[0].cat.node;
2201 ctx_new.range.level[1].sens =
2202 cache->data.mls_label.level[1].sens;
2203 ctx_new.range.level[1].cat.highbit =
2204 cache->data.mls_label.level[1].cat.highbit;
2205 ctx_new.range.level[1].cat.node =
2206 cache->data.mls_label.level[1].cat.node;
2208 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2211 goto netlbl_secattr_to_sid_return;
2213 } else if (secattr->mls_lvl_vld) {
2214 ctx = sidtab_search(&sidtab, base_sid);
2216 goto netlbl_secattr_to_sid_return;
2218 ctx_new.user = ctx->user;
2219 ctx_new.role = ctx->role;
2220 ctx_new.type = ctx->type;
2221 mls_import_lvl(&ctx_new, secattr->mls_lvl, secattr->mls_lvl);
2222 if (secattr->mls_cat) {
2223 if (mls_import_cat(&ctx_new,
2225 secattr->mls_cat_len,
2228 goto netlbl_secattr_to_sid_return;
2229 ctx_new.range.level[1].cat.highbit =
2230 ctx_new.range.level[0].cat.highbit;
2231 ctx_new.range.level[1].cat.node =
2232 ctx_new.range.level[0].cat.node;
2234 ebitmap_init(&ctx_new.range.level[0].cat);
2235 ebitmap_init(&ctx_new.range.level[1].cat);
2237 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2238 goto netlbl_secattr_to_sid_return_cleanup;
2240 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2242 goto netlbl_secattr_to_sid_return_cleanup;
2245 selinux_netlbl_cache_add(skb, &ctx_new);
2246 ebitmap_destroy(&ctx_new.range.level[0].cat);
2252 netlbl_secattr_to_sid_return:
2255 netlbl_secattr_to_sid_return_cleanup:
2256 ebitmap_destroy(&ctx_new.range.level[0].cat);
2257 goto netlbl_secattr_to_sid_return;
2261 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2263 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2267 * Call the NetLabel mechanism to get the security attributes of the given
2268 * packet and use those attributes to determine the correct context/SID to
2269 * assign to the packet. Returns zero on success, negative values on failure.
2272 static int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
2277 struct netlbl_lsm_secattr secattr;
2279 netlbl_secattr_init(&secattr);
2280 rc = netlbl_skbuff_getattr(skb, &secattr);
2282 rc = selinux_netlbl_secattr_to_sid(skb,
2286 netlbl_secattr_destroy(&secattr);
2292 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2293 * @sock: the socket to label
2294 * @sid: the SID to use
2297 * Attempt to label a socket using the NetLabel mechanism using the given
2298 * SID. Returns zero values on success, negative values on failure.
2301 static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
2304 struct sk_security_struct *sksec = sock->sk->sk_security;
2305 struct netlbl_lsm_secattr secattr;
2306 struct context *ctx;
2308 if (!ss_initialized)
2311 netlbl_secattr_init(&secattr);
2315 ctx = sidtab_search(&sidtab, sid);
2317 goto netlbl_socket_setsid_return;
2319 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2321 mls_export_lvl(ctx, &secattr.mls_lvl, NULL);
2322 secattr.mls_lvl_vld = 1;
2323 rc = mls_export_cat(ctx,
2325 &secattr.mls_cat_len,
2329 goto netlbl_socket_setsid_return;
2331 rc = netlbl_socket_setattr(sock, &secattr);
2333 sksec->nlbl_state = NLBL_LABELED;
2335 netlbl_socket_setsid_return:
2337 netlbl_secattr_destroy(&secattr);
2342 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2343 * @ssec: the sk_security_struct
2344 * @family: the socket family
2347 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2351 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
2354 if (family == PF_INET)
2355 ssec->nlbl_state = NLBL_REQUIRE;
2357 ssec->nlbl_state = NLBL_UNSET;
2361 * selinux_netlbl_sk_clone_security - Copy the NetLabel fields
2362 * @ssec: the original sk_security_struct
2363 * @newssec: the cloned sk_security_struct
2366 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2370 void selinux_netlbl_sk_clone_security(struct sk_security_struct *ssec,
2371 struct sk_security_struct *newssec)
2373 newssec->sclass = ssec->sclass;
2374 if (ssec->nlbl_state != NLBL_UNSET)
2375 newssec->nlbl_state = NLBL_REQUIRE;
2377 newssec->nlbl_state = NLBL_UNSET;
2381 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2382 * @sock: the socket to label
2383 * @sock_family: the socket family
2384 * @sid: the SID to use
2387 * Attempt to label a socket using the NetLabel mechanism using the given
2388 * SID. Returns zero values on success, negative values on failure.
2391 int selinux_netlbl_socket_post_create(struct socket *sock,
2395 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2396 struct sk_security_struct *sksec = sock->sk->sk_security;
2398 sksec->sclass = isec->sclass;
2400 if (sock_family != PF_INET)
2403 sksec->nlbl_state = NLBL_REQUIRE;
2404 return selinux_netlbl_socket_setsid(sock, sid);
2408 * selinux_netlbl_sock_graft - Netlabel the new socket
2409 * @sk: the new connection
2410 * @sock: the new socket
2413 * The connection represented by @sk is being grafted onto @sock so set the
2414 * socket's NetLabel to match the SID of @sk.
2417 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
2419 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2420 struct sk_security_struct *sksec = sk->sk_security;
2421 struct netlbl_lsm_secattr secattr;
2424 sksec->sclass = isec->sclass;
2426 if (sk->sk_family != PF_INET)
2429 netlbl_secattr_init(&secattr);
2430 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
2431 selinux_netlbl_secattr_to_sid(NULL,
2433 SECINITSID_UNLABELED,
2434 &nlbl_peer_sid) == 0)
2435 sksec->peer_sid = nlbl_peer_sid;
2436 netlbl_secattr_destroy(&secattr);
2438 sksec->nlbl_state = NLBL_REQUIRE;
2440 /* Try to set the NetLabel on the socket to save time later, if we fail
2441 * here we will pick up the pieces in later calls to
2442 * selinux_netlbl_inode_permission(). */
2443 selinux_netlbl_socket_setsid(sock, sksec->sid);
2447 * selinux_netlbl_inet_conn_request - Handle a new connection request
2449 * @sock_sid: the SID of the parent socket
2452 * If present, use the security attributes of the packet in @skb and the
2453 * parent sock's SID to arrive at a SID for the new child sock. Returns the
2454 * SID of the connection or SECSID_NULL on failure.
2457 u32 selinux_netlbl_inet_conn_request(struct sk_buff *skb, u32 sock_sid)
2462 rc = selinux_netlbl_skbuff_getsid(skb, sock_sid, &peer_sid);
2470 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
2471 * @inode: the file descriptor's inode
2472 * @mask: the permission mask
2475 * Looks at a file's inode and if it is marked as a socket protected by
2476 * NetLabel then verify that the socket has been labeled, if not try to label
2477 * the socket now with the inode's SID. Returns zero on success, negative
2478 * values on failure.
2481 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
2484 struct inode_security_struct *isec;
2485 struct sk_security_struct *sksec;
2486 struct socket *sock;
2488 if (!S_ISSOCK(inode->i_mode))
2491 sock = SOCKET_I(inode);
2492 isec = inode->i_security;
2493 sksec = sock->sk->sk_security;
2494 mutex_lock(&isec->lock);
2495 if (unlikely(sksec->nlbl_state == NLBL_REQUIRE &&
2496 (mask & (MAY_WRITE | MAY_APPEND)))) {
2497 lock_sock(sock->sk);
2498 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2499 release_sock(sock->sk);
2502 mutex_unlock(&isec->lock);
2508 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2509 * @sksec: the sock's sk_security_struct
2511 * @ad: the audit data
2514 * Fetch the NetLabel security attributes from @skb and perform an access check
2515 * against the receiving socket. Returns zero on success, negative values on
2519 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
2520 struct sk_buff *skb,
2521 struct avc_audit_data *ad)
2527 rc = selinux_netlbl_skbuff_getsid(skb,
2528 SECINITSID_UNLABELED,
2533 if (netlbl_sid == SECSID_NULL)
2536 switch (sksec->sclass) {
2537 case SECCLASS_UDP_SOCKET:
2538 recv_perm = UDP_SOCKET__RECVFROM;
2540 case SECCLASS_TCP_SOCKET:
2541 recv_perm = TCP_SOCKET__RECVFROM;
2544 recv_perm = RAWIP_SOCKET__RECVFROM;
2547 rc = avc_has_perm(sksec->sid,
2555 netlbl_skbuff_err(skb, rc);
2560 * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID
2564 * Examine @sock to find the connected peer's SID. Returns the SID on success
2565 * or SECSID_NULL on error.
2568 u32 selinux_netlbl_socket_getpeersec_stream(struct socket *sock)
2570 struct sk_security_struct *sksec = sock->sk->sk_security;
2571 return sksec->peer_sid;
2575 * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet
2579 * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on
2580 * success, SECSID_NULL on error.
2583 u32 selinux_netlbl_socket_getpeersec_dgram(struct sk_buff *skb)
2587 if (selinux_netlbl_skbuff_getsid(skb,
2588 SECINITSID_UNLABELED,
2596 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
2598 * @level: the socket level or protocol
2599 * @optname: the socket option name
2602 * Check the setsockopt() call and if the user is trying to replace the IP
2603 * options on a socket and a NetLabel is in place for the socket deny the
2604 * access; otherwise allow the access. Returns zero when the access is
2605 * allowed, -EACCES when denied, and other negative values on error.
2608 int selinux_netlbl_socket_setsockopt(struct socket *sock,
2613 struct inode *inode = SOCK_INODE(sock);
2614 struct sk_security_struct *sksec = sock->sk->sk_security;
2615 struct inode_security_struct *isec = inode->i_security;
2616 struct netlbl_lsm_secattr secattr;
2618 mutex_lock(&isec->lock);
2619 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
2620 sksec->nlbl_state == NLBL_LABELED) {
2621 netlbl_secattr_init(&secattr);
2622 rc = netlbl_socket_getattr(sock, &secattr);
2623 if (rc == 0 && (secattr.cache || secattr.mls_lvl_vld))
2625 netlbl_secattr_destroy(&secattr);
2627 mutex_unlock(&isec->lock);
2631 #endif /* CONFIG_NETLABEL */