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.
11 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 * Added conditional policy language extensions
15 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
16 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
17 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/spinlock.h>
26 #include <linux/errno.h>
28 #include <linux/sched.h>
29 #include <linux/audit.h>
30 #include <asm/semaphore.h>
39 #include "conditional.h"
42 extern void selnl_notify_policyload(u32 seqno);
43 unsigned int policydb_loaded_version;
45 static DEFINE_RWLOCK(policy_rwlock);
46 #define POLICY_RDLOCK read_lock(&policy_rwlock)
47 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
48 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
49 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
51 static DECLARE_MUTEX(load_sem);
52 #define LOAD_LOCK down(&load_sem)
53 #define LOAD_UNLOCK up(&load_sem)
55 static struct sidtab sidtab;
56 struct policydb policydb;
57 int ss_initialized = 0;
60 * The largest sequence number that has been used when
61 * providing an access decision to the access vector cache.
62 * The sequence number only changes when a policy change
65 static u32 latest_granting = 0;
67 /* Forward declaration. */
68 static int context_struct_to_string(struct context *context, char **scontext,
72 * Return the boolean value of a constraint expression
73 * when it is applied to the specified source and target
76 * xcontext is a special beast... It is used by the validatetrans rules
77 * only. For these rules, scontext is the context before the transition,
78 * tcontext is the context after the transition, and xcontext is the context
79 * of the process performing the transition. All other callers of
80 * constraint_expr_eval should pass in NULL for xcontext.
82 static int constraint_expr_eval(struct context *scontext,
83 struct context *tcontext,
84 struct context *xcontext,
85 struct constraint_expr *cexpr)
89 struct role_datum *r1, *r2;
90 struct mls_level *l1, *l2;
91 struct constraint_expr *e;
92 int s[CEXPR_MAXDEPTH];
95 for (e = cexpr; e; e = e->next) {
96 switch (e->expr_type) {
112 if (sp == (CEXPR_MAXDEPTH-1))
116 val1 = scontext->user;
117 val2 = tcontext->user;
120 val1 = scontext->type;
121 val2 = tcontext->type;
124 val1 = scontext->role;
125 val2 = tcontext->role;
126 r1 = policydb.role_val_to_struct[val1 - 1];
127 r2 = policydb.role_val_to_struct[val2 - 1];
130 s[++sp] = ebitmap_get_bit(&r1->dominates,
134 s[++sp] = ebitmap_get_bit(&r2->dominates,
138 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
140 !ebitmap_get_bit(&r2->dominates,
148 l1 = &(scontext->range.level[0]);
149 l2 = &(tcontext->range.level[0]);
152 l1 = &(scontext->range.level[0]);
153 l2 = &(tcontext->range.level[1]);
156 l1 = &(scontext->range.level[1]);
157 l2 = &(tcontext->range.level[0]);
160 l1 = &(scontext->range.level[1]);
161 l2 = &(tcontext->range.level[1]);
164 l1 = &(scontext->range.level[0]);
165 l2 = &(scontext->range.level[1]);
168 l1 = &(tcontext->range.level[0]);
169 l2 = &(tcontext->range.level[1]);
174 s[++sp] = mls_level_eq(l1, l2);
177 s[++sp] = !mls_level_eq(l1, l2);
180 s[++sp] = mls_level_dom(l1, l2);
183 s[++sp] = mls_level_dom(l2, l1);
186 s[++sp] = mls_level_incomp(l2, l1);
200 s[++sp] = (val1 == val2);
203 s[++sp] = (val1 != val2);
211 if (sp == (CEXPR_MAXDEPTH-1))
214 if (e->attr & CEXPR_TARGET)
216 else if (e->attr & CEXPR_XTARGET) {
223 if (e->attr & CEXPR_USER)
225 else if (e->attr & CEXPR_ROLE)
227 else if (e->attr & CEXPR_TYPE)
236 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
239 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
257 * Compute access vectors based on a context structure pair for
258 * the permissions in a particular class.
260 static int context_struct_compute_av(struct context *scontext,
261 struct context *tcontext,
264 struct av_decision *avd)
266 struct constraint_node *constraint;
267 struct role_allow *ra;
268 struct avtab_key avkey;
269 struct avtab_datum *avdatum;
270 struct class_datum *tclass_datum;
273 * Remap extended Netlink classes for old policy versions.
274 * Do this here rather than socket_type_to_security_class()
275 * in case a newer policy version is loaded, allowing sockets
276 * to remain in the correct class.
278 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
279 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
280 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
281 tclass = SECCLASS_NETLINK_SOCKET;
283 if (!tclass || tclass > policydb.p_classes.nprim) {
284 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
288 tclass_datum = policydb.class_val_to_struct[tclass - 1];
291 * Initialize the access vectors to the default values.
294 avd->decided = 0xffffffff;
296 avd->auditdeny = 0xffffffff;
297 avd->seqno = latest_granting;
300 * If a specific type enforcement rule was defined for
301 * this permission check, then use it.
303 avkey.source_type = scontext->type;
304 avkey.target_type = tcontext->type;
305 avkey.target_class = tclass;
306 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_AV);
308 if (avdatum->specified & AVTAB_ALLOWED)
309 avd->allowed = avtab_allowed(avdatum);
310 if (avdatum->specified & AVTAB_AUDITDENY)
311 avd->auditdeny = avtab_auditdeny(avdatum);
312 if (avdatum->specified & AVTAB_AUDITALLOW)
313 avd->auditallow = avtab_auditallow(avdatum);
316 /* Check conditional av table for additional permissions */
317 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
320 * Remove any permissions prohibited by a constraint (this includes
323 constraint = tclass_datum->constraints;
325 if ((constraint->permissions & (avd->allowed)) &&
326 !constraint_expr_eval(scontext, tcontext, NULL,
328 avd->allowed = (avd->allowed) & ~(constraint->permissions);
330 constraint = constraint->next;
334 * If checking process transition permission and the
335 * role is changing, then check the (current_role, new_role)
338 if (tclass == SECCLASS_PROCESS &&
339 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
340 scontext->role != tcontext->role) {
341 for (ra = policydb.role_allow; ra; ra = ra->next) {
342 if (scontext->role == ra->role &&
343 tcontext->role == ra->new_role)
347 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
348 PROCESS__DYNTRANSITION);
354 static int security_validtrans_handle_fail(struct context *ocontext,
355 struct context *ncontext,
356 struct context *tcontext,
359 char *o = NULL, *n = NULL, *t = NULL;
360 u32 olen, nlen, tlen;
362 if (context_struct_to_string(ocontext, &o, &olen) < 0)
364 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
366 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
368 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
369 "security_validate_transition: denied for"
370 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
371 o, n, t, policydb.p_class_val_to_name[tclass-1]);
377 if (!selinux_enforcing)
382 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
385 struct context *ocontext;
386 struct context *ncontext;
387 struct context *tcontext;
388 struct class_datum *tclass_datum;
389 struct constraint_node *constraint;
398 * Remap extended Netlink classes for old policy versions.
399 * Do this here rather than socket_type_to_security_class()
400 * in case a newer policy version is loaded, allowing sockets
401 * to remain in the correct class.
403 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
404 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
405 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
406 tclass = SECCLASS_NETLINK_SOCKET;
408 if (!tclass || tclass > policydb.p_classes.nprim) {
409 printk(KERN_ERR "security_validate_transition: "
410 "unrecognized class %d\n", tclass);
414 tclass_datum = policydb.class_val_to_struct[tclass - 1];
416 ocontext = sidtab_search(&sidtab, oldsid);
418 printk(KERN_ERR "security_validate_transition: "
419 " unrecognized SID %d\n", oldsid);
424 ncontext = sidtab_search(&sidtab, newsid);
426 printk(KERN_ERR "security_validate_transition: "
427 " unrecognized SID %d\n", newsid);
432 tcontext = sidtab_search(&sidtab, tasksid);
434 printk(KERN_ERR "security_validate_transition: "
435 " unrecognized SID %d\n", tasksid);
440 constraint = tclass_datum->validatetrans;
442 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
444 rc = security_validtrans_handle_fail(ocontext, ncontext,
448 constraint = constraint->next;
457 * security_compute_av - Compute access vector decisions.
458 * @ssid: source security identifier
459 * @tsid: target security identifier
460 * @tclass: target security class
461 * @requested: requested permissions
462 * @avd: access vector decisions
464 * Compute a set of access vector decisions based on the
465 * SID pair (@ssid, @tsid) for the permissions in @tclass.
466 * Return -%EINVAL if any of the parameters are invalid or %0
467 * if the access vector decisions were computed successfully.
469 int security_compute_av(u32 ssid,
473 struct av_decision *avd)
475 struct context *scontext = NULL, *tcontext = NULL;
478 if (!ss_initialized) {
479 avd->allowed = 0xffffffff;
480 avd->decided = 0xffffffff;
482 avd->auditdeny = 0xffffffff;
483 avd->seqno = latest_granting;
489 scontext = sidtab_search(&sidtab, ssid);
491 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
496 tcontext = sidtab_search(&sidtab, tsid);
498 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
504 rc = context_struct_compute_av(scontext, tcontext, tclass,
512 * Write the security context string representation of
513 * the context structure `context' into a dynamically
514 * allocated string of the correct size. Set `*scontext'
515 * to point to this string and set `*scontext_len' to
516 * the length of the string.
518 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
525 /* Compute the size of the context. */
526 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
527 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
528 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
529 *scontext_len += mls_compute_context_len(context);
531 /* Allocate space for the context; caller must free this space. */
532 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
536 *scontext = scontextp;
539 * Copy the user name, role name and type name into the context.
541 sprintf(scontextp, "%s:%s:%s",
542 policydb.p_user_val_to_name[context->user - 1],
543 policydb.p_role_val_to_name[context->role - 1],
544 policydb.p_type_val_to_name[context->type - 1]);
545 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
546 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
547 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
549 mls_sid_to_context(context, &scontextp);
556 #include "initial_sid_to_string.h"
559 * security_sid_to_context - Obtain a context for a given SID.
560 * @sid: security identifier, SID
561 * @scontext: security context
562 * @scontext_len: length in bytes
564 * Write the string representation of the context associated with @sid
565 * into a dynamically allocated string of the correct size. Set @scontext
566 * to point to this string and set @scontext_len to the length of the string.
568 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
570 struct context *context;
573 if (!ss_initialized) {
574 if (sid <= SECINITSID_NUM) {
577 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
578 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
579 strcpy(scontextp, initial_sid_to_string[sid]);
580 *scontext = scontextp;
583 printk(KERN_ERR "security_sid_to_context: called before initial "
584 "load_policy on unknown SID %d\n", sid);
589 context = sidtab_search(&sidtab, sid);
591 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
596 rc = context_struct_to_string(context, scontext, scontext_len);
605 * security_context_to_sid - Obtain a SID for a given security context.
606 * @scontext: security context
607 * @scontext_len: length in bytes
608 * @sid: security identifier, SID
610 * Obtains a SID associated with the security context that
611 * has the string representation specified by @scontext.
612 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
613 * memory is available, or 0 on success.
615 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
618 struct context context;
619 struct role_datum *role;
620 struct type_datum *typdatum;
621 struct user_datum *usrdatum;
622 char *scontextp, *p, oldc;
625 if (!ss_initialized) {
628 for (i = 1; i < SECINITSID_NUM; i++) {
629 if (!strcmp(initial_sid_to_string[i], scontext)) {
634 *sid = SECINITSID_KERNEL;
639 /* Copy the string so that we can modify the copy as we parse it.
640 The string should already by null terminated, but we append a
641 null suffix to the copy to avoid problems with the existing
642 attr package, which doesn't view the null terminator as part
643 of the attribute value. */
644 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
649 memcpy(scontext2, scontext, scontext_len);
650 scontext2[scontext_len] = 0;
652 context_init(&context);
657 /* Parse the security context. */
660 scontextp = (char *) scontext2;
662 /* Extract the user. */
664 while (*p && *p != ':')
672 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
676 context.user = usrdatum->value;
680 while (*p && *p != ':')
688 role = hashtab_search(policydb.p_roles.table, scontextp);
691 context.role = role->value;
695 while (*p && *p != ':')
700 typdatum = hashtab_search(policydb.p_types.table, scontextp);
704 context.type = typdatum->value;
706 rc = mls_context_to_sid(oldc, &p, &context);
710 if ((p - scontext2) < scontext_len) {
715 /* Check the validity of the new context. */
716 if (!policydb_context_isvalid(&policydb, &context)) {
720 /* Obtain the new sid. */
721 rc = sidtab_context_to_sid(&sidtab, &context, sid);
724 context_destroy(&context);
730 static int compute_sid_handle_invalid_context(
731 struct context *scontext,
732 struct context *tcontext,
734 struct context *newcontext)
736 char *s = NULL, *t = NULL, *n = NULL;
737 u32 slen, tlen, nlen;
739 if (context_struct_to_string(scontext, &s, &slen) < 0)
741 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
743 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
745 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
746 "security_compute_sid: invalid context %s"
750 n, s, t, policydb.p_class_val_to_name[tclass-1]);
755 if (!selinux_enforcing)
760 static int security_compute_sid(u32 ssid,
766 struct context *scontext = NULL, *tcontext = NULL, newcontext;
767 struct role_trans *roletr = NULL;
768 struct avtab_key avkey;
769 struct avtab_datum *avdatum;
770 struct avtab_node *node;
771 unsigned int type_change = 0;
774 if (!ss_initialized) {
776 case SECCLASS_PROCESS:
788 scontext = sidtab_search(&sidtab, ssid);
790 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
795 tcontext = sidtab_search(&sidtab, tsid);
797 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
803 context_init(&newcontext);
805 /* Set the user identity. */
807 case AVTAB_TRANSITION:
809 /* Use the process user identity. */
810 newcontext.user = scontext->user;
813 /* Use the related object owner. */
814 newcontext.user = tcontext->user;
818 /* Set the role and type to default values. */
820 case SECCLASS_PROCESS:
821 /* Use the current role and type of process. */
822 newcontext.role = scontext->role;
823 newcontext.type = scontext->type;
826 /* Use the well-defined object role. */
827 newcontext.role = OBJECT_R_VAL;
828 /* Use the type of the related object. */
829 newcontext.type = tcontext->type;
832 /* Look for a type transition/member/change rule. */
833 avkey.source_type = scontext->type;
834 avkey.target_type = tcontext->type;
835 avkey.target_class = tclass;
836 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
838 /* If no permanent rule, also check for enabled conditional rules */
840 node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
841 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
842 if (node->datum.specified & AVTAB_ENABLED) {
843 avdatum = &node->datum;
849 type_change = (avdatum && (avdatum->specified & specified));
851 /* Use the type from the type transition/member/change rule. */
853 case AVTAB_TRANSITION:
854 newcontext.type = avtab_transition(avdatum);
857 newcontext.type = avtab_member(avdatum);
860 newcontext.type = avtab_change(avdatum);
865 /* Check for class-specific changes. */
867 case SECCLASS_PROCESS:
868 if (specified & AVTAB_TRANSITION) {
869 /* Look for a role transition rule. */
870 for (roletr = policydb.role_tr; roletr;
871 roletr = roletr->next) {
872 if (roletr->role == scontext->role &&
873 roletr->type == tcontext->type) {
874 /* Use the role transition rule. */
875 newcontext.role = roletr->new_role;
885 /* Set the MLS attributes.
886 This is done last because it may allocate memory. */
887 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
891 /* Check the validity of the context. */
892 if (!policydb_context_isvalid(&policydb, &newcontext)) {
893 rc = compute_sid_handle_invalid_context(scontext,
900 /* Obtain the sid for the context. */
901 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
904 context_destroy(&newcontext);
910 * security_transition_sid - Compute the SID for a new subject/object.
911 * @ssid: source security identifier
912 * @tsid: target security identifier
913 * @tclass: target security class
914 * @out_sid: security identifier for new subject/object
916 * Compute a SID to use for labeling a new subject or object in the
917 * class @tclass based on a SID pair (@ssid, @tsid).
918 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
919 * if insufficient memory is available, or %0 if the new SID was
920 * computed successfully.
922 int security_transition_sid(u32 ssid,
927 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
931 * security_member_sid - Compute the SID for member selection.
932 * @ssid: source security identifier
933 * @tsid: target security identifier
934 * @tclass: target security class
935 * @out_sid: security identifier for selected member
937 * Compute a SID to use when selecting a member of a polyinstantiated
938 * object of class @tclass based on a SID pair (@ssid, @tsid).
939 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
940 * if insufficient memory is available, or %0 if the SID was
941 * computed successfully.
943 int security_member_sid(u32 ssid,
948 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
952 * security_change_sid - Compute the SID for object relabeling.
953 * @ssid: source security identifier
954 * @tsid: target security identifier
955 * @tclass: target security class
956 * @out_sid: security identifier for selected member
958 * Compute a SID to use for relabeling an object of class @tclass
959 * based on a SID pair (@ssid, @tsid).
960 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
961 * if insufficient memory is available, or %0 if the SID was
962 * computed successfully.
964 int security_change_sid(u32 ssid,
969 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
973 * Verify that each permission that is defined under the
974 * existing policy is still defined with the same value
977 static int validate_perm(void *key, void *datum, void *p)
980 struct perm_datum *perdatum, *perdatum2;
987 perdatum2 = hashtab_search(h, key);
989 printk(KERN_ERR "security: permission %s disappeared",
994 if (perdatum->value != perdatum2->value) {
995 printk(KERN_ERR "security: the value of permission %s changed",
1004 * Verify that each class that is defined under the
1005 * existing policy is still defined with the same
1006 * attributes in the new policy.
1008 static int validate_class(void *key, void *datum, void *p)
1010 struct policydb *newp;
1011 struct class_datum *cladatum, *cladatum2;
1017 cladatum2 = hashtab_search(newp->p_classes.table, key);
1019 printk(KERN_ERR "security: class %s disappeared\n",
1024 if (cladatum->value != cladatum2->value) {
1025 printk(KERN_ERR "security: the value of class %s changed\n",
1030 if ((cladatum->comdatum && !cladatum2->comdatum) ||
1031 (!cladatum->comdatum && cladatum2->comdatum)) {
1032 printk(KERN_ERR "security: the inherits clause for the access "
1033 "vector definition for class %s changed\n", (char *)key);
1037 if (cladatum->comdatum) {
1038 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
1039 cladatum2->comdatum->permissions.table);
1041 printk(" in the access vector definition for class "
1042 "%s\n", (char *)key);
1046 rc = hashtab_map(cladatum->permissions.table, validate_perm,
1047 cladatum2->permissions.table);
1049 printk(" in access vector definition for class %s\n",
1055 /* Clone the SID into the new SID table. */
1056 static int clone_sid(u32 sid,
1057 struct context *context,
1060 struct sidtab *s = arg;
1062 return sidtab_insert(s, sid, context);
1065 static inline int convert_context_handle_invalid_context(struct context *context)
1069 if (selinux_enforcing) {
1075 context_struct_to_string(context, &s, &len);
1076 printk(KERN_ERR "security: context %s is invalid\n", s);
1082 struct convert_context_args {
1083 struct policydb *oldp;
1084 struct policydb *newp;
1088 * Convert the values in the security context
1089 * structure `c' from the values specified
1090 * in the policy `p->oldp' to the values specified
1091 * in the policy `p->newp'. Verify that the
1092 * context is valid under the new policy.
1094 static int convert_context(u32 key,
1098 struct convert_context_args *args;
1099 struct context oldc;
1100 struct role_datum *role;
1101 struct type_datum *typdatum;
1102 struct user_datum *usrdatum;
1109 rc = context_cpy(&oldc, c);
1115 /* Convert the user. */
1116 usrdatum = hashtab_search(args->newp->p_users.table,
1117 args->oldp->p_user_val_to_name[c->user - 1]);
1121 c->user = usrdatum->value;
1123 /* Convert the role. */
1124 role = hashtab_search(args->newp->p_roles.table,
1125 args->oldp->p_role_val_to_name[c->role - 1]);
1129 c->role = role->value;
1131 /* Convert the type. */
1132 typdatum = hashtab_search(args->newp->p_types.table,
1133 args->oldp->p_type_val_to_name[c->type - 1]);
1137 c->type = typdatum->value;
1139 rc = mls_convert_context(args->oldp, args->newp, c);
1143 /* Check the validity of the new context. */
1144 if (!policydb_context_isvalid(args->newp, c)) {
1145 rc = convert_context_handle_invalid_context(&oldc);
1150 context_destroy(&oldc);
1154 context_struct_to_string(&oldc, &s, &len);
1155 context_destroy(&oldc);
1156 printk(KERN_ERR "security: invalidating context %s\n", s);
1161 extern void selinux_complete_init(void);
1164 * security_load_policy - Load a security policy configuration.
1165 * @data: binary policy data
1166 * @len: length of data in bytes
1168 * Load a new set of security policy configuration data,
1169 * validate it and convert the SID table as necessary.
1170 * This function will flush the access vector cache after
1171 * loading the new policy.
1173 int security_load_policy(void *data, size_t len)
1175 struct policydb oldpolicydb, newpolicydb;
1176 struct sidtab oldsidtab, newsidtab;
1177 struct convert_context_args args;
1180 struct policy_file file = { data, len }, *fp = &file;
1184 if (!ss_initialized) {
1186 if (policydb_read(&policydb, fp)) {
1188 avtab_cache_destroy();
1191 if (policydb_load_isids(&policydb, &sidtab)) {
1193 policydb_destroy(&policydb);
1194 avtab_cache_destroy();
1197 policydb_loaded_version = policydb.policyvers;
1199 seqno = ++latest_granting;
1201 selinux_complete_init();
1202 avc_ss_reset(seqno);
1203 selnl_notify_policyload(seqno);
1208 sidtab_hash_eval(&sidtab, "sids");
1211 if (policydb_read(&newpolicydb, fp)) {
1216 sidtab_init(&newsidtab);
1218 /* Verify that the existing classes did not change. */
1219 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1220 printk(KERN_ERR "security: the definition of an existing "
1226 /* Clone the SID table. */
1227 sidtab_shutdown(&sidtab);
1228 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1233 /* Convert the internal representations of contexts
1234 in the new SID table and remove invalid SIDs. */
1235 args.oldp = &policydb;
1236 args.newp = &newpolicydb;
1237 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1239 /* Save the old policydb and SID table to free later. */
1240 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1241 sidtab_set(&oldsidtab, &sidtab);
1243 /* Install the new policydb and SID table. */
1245 memcpy(&policydb, &newpolicydb, sizeof policydb);
1246 sidtab_set(&sidtab, &newsidtab);
1247 seqno = ++latest_granting;
1248 policydb_loaded_version = policydb.policyvers;
1252 /* Free the old policydb and SID table. */
1253 policydb_destroy(&oldpolicydb);
1254 sidtab_destroy(&oldsidtab);
1256 avc_ss_reset(seqno);
1257 selnl_notify_policyload(seqno);
1263 sidtab_destroy(&newsidtab);
1264 policydb_destroy(&newpolicydb);
1270 * security_port_sid - Obtain the SID for a port.
1271 * @domain: communication domain aka address family
1272 * @type: socket type
1273 * @protocol: protocol number
1274 * @port: port number
1275 * @out_sid: security identifier
1277 int security_port_sid(u16 domain,
1288 c = policydb.ocontexts[OCON_PORT];
1290 if (c->u.port.protocol == protocol &&
1291 c->u.port.low_port <= port &&
1292 c->u.port.high_port >= port)
1299 rc = sidtab_context_to_sid(&sidtab,
1305 *out_sid = c->sid[0];
1307 *out_sid = SECINITSID_PORT;
1316 * security_netif_sid - Obtain the SID for a network interface.
1317 * @name: interface name
1318 * @if_sid: interface SID
1319 * @msg_sid: default SID for received packets
1321 int security_netif_sid(char *name,
1330 c = policydb.ocontexts[OCON_NETIF];
1332 if (strcmp(name, c->u.name) == 0)
1338 if (!c->sid[0] || !c->sid[1]) {
1339 rc = sidtab_context_to_sid(&sidtab,
1344 rc = sidtab_context_to_sid(&sidtab,
1350 *if_sid = c->sid[0];
1351 *msg_sid = c->sid[1];
1353 *if_sid = SECINITSID_NETIF;
1354 *msg_sid = SECINITSID_NETMSG;
1362 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1366 for(i = 0; i < 4; i++)
1367 if(addr[i] != (input[i] & mask[i])) {
1376 * security_node_sid - Obtain the SID for a node (host).
1377 * @domain: communication domain aka address family
1379 * @addrlen: address length in bytes
1380 * @out_sid: security identifier
1382 int security_node_sid(u16 domain,
1396 if (addrlen != sizeof(u32)) {
1401 addr = *((u32 *)addrp);
1403 c = policydb.ocontexts[OCON_NODE];
1405 if (c->u.node.addr == (addr & c->u.node.mask))
1413 if (addrlen != sizeof(u64) * 2) {
1417 c = policydb.ocontexts[OCON_NODE6];
1419 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1427 *out_sid = SECINITSID_NODE;
1433 rc = sidtab_context_to_sid(&sidtab,
1439 *out_sid = c->sid[0];
1441 *out_sid = SECINITSID_NODE;
1452 * security_get_user_sids - Obtain reachable SIDs for a user.
1453 * @fromsid: starting SID
1454 * @username: username
1455 * @sids: array of reachable SIDs for user
1456 * @nel: number of elements in @sids
1458 * Generate the set of SIDs for legal security contexts
1459 * for a given user that can be reached by @fromsid.
1460 * Set *@sids to point to a dynamically allocated
1461 * array containing the set of SIDs. Set *@nel to the
1462 * number of elements in the array.
1465 int security_get_user_sids(u32 fromsid,
1470 struct context *fromcon, usercon;
1471 u32 *mysids, *mysids2, sid;
1472 u32 mynel = 0, maxnel = SIDS_NEL;
1473 struct user_datum *user;
1474 struct role_datum *role;
1475 struct av_decision avd;
1478 if (!ss_initialized) {
1486 fromcon = sidtab_search(&sidtab, fromsid);
1492 user = hashtab_search(policydb.p_users.table, username);
1497 usercon.user = user->value;
1499 mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1504 memset(mysids, 0, maxnel*sizeof(*mysids));
1506 for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1507 if (!ebitmap_get_bit(&user->roles, i))
1509 role = policydb.role_val_to_struct[i];
1511 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1512 if (!ebitmap_get_bit(&role->types, j))
1516 if (mls_setup_user_range(fromcon, user, &usercon))
1519 rc = context_struct_compute_av(fromcon, &usercon,
1521 PROCESS__TRANSITION,
1523 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1525 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1530 if (mynel < maxnel) {
1531 mysids[mynel++] = sid;
1534 mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1540 memset(mysids2, 0, maxnel*sizeof(*mysids2));
1541 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1544 mysids[mynel++] = sid;
1559 * security_genfs_sid - Obtain a SID for a file in a filesystem
1560 * @fstype: filesystem type
1561 * @path: path from root of mount
1562 * @sclass: file security class
1563 * @sid: SID for path
1565 * Obtain a SID to use for a file in a filesystem that
1566 * cannot support xattr or use a fixed labeling behavior like
1567 * transition SIDs or task SIDs.
1569 int security_genfs_sid(const char *fstype,
1575 struct genfs *genfs;
1577 int rc = 0, cmp = 0;
1581 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1582 cmp = strcmp(fstype, genfs->fstype);
1587 if (!genfs || cmp) {
1588 *sid = SECINITSID_UNLABELED;
1593 for (c = genfs->head; c; c = c->next) {
1594 len = strlen(c->u.name);
1595 if ((!c->v.sclass || sclass == c->v.sclass) &&
1596 (strncmp(c->u.name, path, len) == 0))
1601 *sid = SECINITSID_UNLABELED;
1607 rc = sidtab_context_to_sid(&sidtab,
1621 * security_fs_use - Determine how to handle labeling for a filesystem.
1622 * @fstype: filesystem type
1623 * @behavior: labeling behavior
1624 * @sid: SID for filesystem (superblock)
1626 int security_fs_use(
1628 unsigned int *behavior,
1636 c = policydb.ocontexts[OCON_FSUSE];
1638 if (strcmp(fstype, c->u.name) == 0)
1644 *behavior = c->v.behavior;
1646 rc = sidtab_context_to_sid(&sidtab,
1654 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1656 *behavior = SECURITY_FS_USE_NONE;
1659 *behavior = SECURITY_FS_USE_GENFS;
1668 int security_get_bools(int *len, char ***names, int **values)
1670 int i, rc = -ENOMEM;
1676 *len = policydb.p_bools.nprim;
1682 *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
1685 memset(*names, 0, sizeof(char*) * *len);
1687 *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
1691 for (i = 0; i < *len; i++) {
1693 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1694 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1695 (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1698 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1699 (*names)[i][name_len - 1] = 0;
1707 for (i = 0; i < *len; i++)
1717 int security_set_bools(int len, int *values)
1720 int lenp, seqno = 0;
1721 struct cond_node *cur;
1725 lenp = policydb.p_bools.nprim;
1731 printk(KERN_INFO "security: committed booleans { ");
1732 for (i = 0; i < len; i++) {
1734 policydb.bool_val_to_struct[i]->state = 1;
1736 policydb.bool_val_to_struct[i]->state = 0;
1740 printk("%s:%d", policydb.p_bool_val_to_name[i],
1741 policydb.bool_val_to_struct[i]->state);
1745 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1746 rc = evaluate_cond_node(&policydb, cur);
1751 seqno = ++latest_granting;
1756 avc_ss_reset(seqno);
1757 selnl_notify_policyload(seqno);
1762 int security_get_bool_value(int bool)
1769 len = policydb.p_bools.nprim;
1775 rc = policydb.bool_val_to_struct[bool]->state;