2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
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);
44 static rwlock_t policy_rwlock = RW_LOCK_UNLOCKED;
45 #define POLICY_RDLOCK read_lock(&policy_rwlock)
46 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
47 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
48 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
50 static DECLARE_MUTEX(load_sem);
51 #define LOAD_LOCK down(&load_sem)
52 #define LOAD_UNLOCK up(&load_sem)
55 struct policydb policydb;
56 int ss_initialized = 0;
59 * The largest sequence number that has been used when
60 * providing an access decision to the access vector cache.
61 * The sequence number only changes when a policy change
64 static u32 latest_granting = 0;
67 * Return the boolean value of a constraint expression
68 * when it is applied to the specified source and target
71 static int constraint_expr_eval(struct context *scontext,
72 struct context *tcontext,
73 struct constraint_expr *cexpr)
77 struct role_datum *r1, *r2;
78 struct constraint_expr *e;
79 int s[CEXPR_MAXDEPTH];
82 for (e = cexpr; e; e = e->next) {
83 switch (e->expr_type) {
99 if (sp == (CEXPR_MAXDEPTH-1))
103 val1 = scontext->user;
104 val2 = tcontext->user;
107 val1 = scontext->type;
108 val2 = tcontext->type;
111 val1 = scontext->role;
112 val2 = tcontext->role;
113 r1 = policydb.role_val_to_struct[val1 - 1];
114 r2 = policydb.role_val_to_struct[val2 - 1];
117 s[++sp] = ebitmap_get_bit(&r1->dominates,
121 s[++sp] = ebitmap_get_bit(&r2->dominates,
125 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
127 !ebitmap_get_bit(&r2->dominates,
141 s[++sp] = (val1 == val2);
144 s[++sp] = (val1 != val2);
152 if (sp == (CEXPR_MAXDEPTH-1))
155 if (e->attr & CEXPR_TARGET)
157 if (e->attr & CEXPR_USER)
159 else if (e->attr & CEXPR_ROLE)
161 else if (e->attr & CEXPR_TYPE)
170 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
173 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
191 * Compute access vectors based on a context structure pair for
192 * the permissions in a particular class.
194 static int context_struct_compute_av(struct context *scontext,
195 struct context *tcontext,
198 struct av_decision *avd)
200 struct constraint_node *constraint;
201 struct role_allow *ra;
202 struct avtab_key avkey;
203 struct avtab_datum *avdatum;
204 struct class_datum *tclass_datum;
206 if (!tclass || tclass > policydb.p_classes.nprim) {
207 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
211 tclass_datum = policydb.class_val_to_struct[tclass - 1];
214 * Initialize the access vectors to the default values.
217 avd->decided = 0xffffffff;
219 avd->auditdeny = 0xffffffff;
220 avd->seqno = latest_granting;
223 * If a specific type enforcement rule was defined for
224 * this permission check, then use it.
226 avkey.source_type = scontext->type;
227 avkey.target_type = tcontext->type;
228 avkey.target_class = tclass;
229 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_AV);
231 if (avdatum->specified & AVTAB_ALLOWED)
232 avd->allowed = avtab_allowed(avdatum);
233 if (avdatum->specified & AVTAB_AUDITDENY)
234 avd->auditdeny = avtab_auditdeny(avdatum);
235 if (avdatum->specified & AVTAB_AUDITALLOW)
236 avd->auditallow = avtab_auditallow(avdatum);
239 /* Check conditional av table for additional permissions */
240 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
243 * Remove any permissions prohibited by the MLS policy.
245 mls_compute_av(scontext, tcontext, tclass_datum, &avd->allowed);
248 * Remove any permissions prohibited by a constraint.
250 constraint = tclass_datum->constraints;
252 if ((constraint->permissions & (avd->allowed)) &&
253 !constraint_expr_eval(scontext, tcontext,
255 avd->allowed = (avd->allowed) & ~(constraint->permissions);
257 constraint = constraint->next;
261 * If checking process transition permission and the
262 * role is changing, then check the (current_role, new_role)
265 if (tclass == SECCLASS_PROCESS &&
266 (avd->allowed & PROCESS__TRANSITION) &&
267 scontext->role != tcontext->role) {
268 for (ra = policydb.role_allow; ra; ra = ra->next) {
269 if (scontext->role == ra->role &&
270 tcontext->role == ra->new_role)
274 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION);
281 * security_compute_av - Compute access vector decisions.
282 * @ssid: source security identifier
283 * @tsid: target security identifier
284 * @tclass: target security class
285 * @requested: requested permissions
286 * @avd: access vector decisions
288 * Compute a set of access vector decisions based on the
289 * SID pair (@ssid, @tsid) for the permissions in @tclass.
290 * Return -%EINVAL if any of the parameters are invalid or %0
291 * if the access vector decisions were computed successfully.
293 int security_compute_av(u32 ssid,
297 struct av_decision *avd)
299 struct context *scontext = 0, *tcontext = 0;
302 if (!ss_initialized) {
303 avd->allowed = requested;
304 avd->decided = requested;
306 avd->auditdeny = 0xffffffff;
307 avd->seqno = latest_granting;
313 scontext = sidtab_search(&sidtab, ssid);
315 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
320 tcontext = sidtab_search(&sidtab, tsid);
322 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
328 rc = context_struct_compute_av(scontext, tcontext, tclass,
336 * Write the security context string representation of
337 * the context structure `context' into a dynamically
338 * allocated string of the correct size. Set `*scontext'
339 * to point to this string and set `*scontext_len' to
340 * the length of the string.
342 int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
349 /* Compute the size of the context. */
350 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
351 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
352 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
353 *scontext_len += mls_compute_context_len(context);
355 /* Allocate space for the context; caller must free this space. */
356 scontextp = kmalloc(*scontext_len+1,GFP_ATOMIC);
360 *scontext = scontextp;
363 * Copy the user name, role name and type name into the context.
365 sprintf(scontextp, "%s:%s:%s:",
366 policydb.p_user_val_to_name[context->user - 1],
367 policydb.p_role_val_to_name[context->role - 1],
368 policydb.p_type_val_to_name[context->type - 1]);
369 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
370 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
371 1 + strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
373 mls_sid_to_context(context, &scontextp);
381 #include "initial_sid_to_string.h"
384 * security_sid_to_context - Obtain a context for a given SID.
385 * @sid: security identifier, SID
386 * @scontext: security context
387 * @scontext_len: length in bytes
389 * Write the string representation of the context associated with @sid
390 * into a dynamically allocated string of the correct size. Set @scontext
391 * to point to this string and set @scontext_len to the length of the string.
393 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
395 struct context *context;
398 if (!ss_initialized) {
399 if (sid <= SECINITSID_NUM) {
402 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
403 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
404 strcpy(scontextp, initial_sid_to_string[sid]);
405 *scontext = scontextp;
408 printk(KERN_ERR "security_sid_to_context: called before initial "
409 "load_policy on unknown SID %d\n", sid);
414 context = sidtab_search(&sidtab, sid);
416 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
421 rc = context_struct_to_string(context, scontext, scontext_len);
430 * security_context_to_sid - Obtain a SID for a given security context.
431 * @scontext: security context
432 * @scontext_len: length in bytes
433 * @sid: security identifier, SID
435 * Obtains a SID associated with the security context that
436 * has the string representation specified by @scontext.
437 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
438 * memory is available, or 0 on success.
440 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
443 struct context context;
444 struct role_datum *role;
445 struct type_datum *typdatum;
446 struct user_datum *usrdatum;
447 char *scontextp, *p, oldc;
450 if (!ss_initialized) {
453 for (i = 1; i < SECINITSID_NUM; i++) {
454 if (!strcmp(initial_sid_to_string[i], scontext)) {
459 *sid = SECINITSID_KERNEL;
464 /* Copy the string so that we can modify the copy as we parse it.
465 The string should already by null terminated, but we append a
466 null suffix to the copy to avoid problems with the existing
467 attr package, which doesn't view the null terminator as part
468 of the attribute value. */
469 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
474 memcpy(scontext2, scontext, scontext_len);
475 scontext2[scontext_len] = 0;
477 context_init(&context);
482 /* Parse the security context. */
485 scontextp = (char *) scontext2;
487 /* Extract the user. */
489 while (*p && *p != ':')
497 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
501 context.user = usrdatum->value;
505 while (*p && *p != ':')
513 role = hashtab_search(policydb.p_roles.table, scontextp);
516 context.role = role->value;
520 while (*p && *p != ':')
525 typdatum = hashtab_search(policydb.p_types.table, scontextp);
529 context.type = typdatum->value;
531 rc = mls_context_to_sid(oldc, &p, &context);
535 if ((p - scontext2) < scontext_len) {
540 /* Check the validity of the new context. */
541 if (!policydb_context_isvalid(&policydb, &context)) {
545 /* Obtain the new sid. */
546 rc = sidtab_context_to_sid(&sidtab, &context, sid);
549 context_destroy(&context);
555 static int compute_sid_handle_invalid_context(
556 struct context *scontext,
557 struct context *tcontext,
559 struct context *newcontext)
561 char *s = NULL, *t = NULL, *n = NULL;
562 u32 slen, tlen, nlen;
564 if (context_struct_to_string(scontext, &s, &slen) < 0)
566 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
568 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
570 audit_log(current->audit_context,
571 "security_compute_sid: invalid context %s"
575 n, s, t, policydb.p_class_val_to_name[tclass-1]);
580 if (!selinux_enforcing)
585 static int security_compute_sid(u32 ssid,
591 struct context *scontext = 0, *tcontext = 0, newcontext;
592 struct role_trans *roletr = 0;
593 struct avtab_key avkey;
594 struct avtab_datum *avdatum;
595 struct avtab_node *node;
596 unsigned int type_change = 0;
599 if (!ss_initialized) {
601 case SECCLASS_PROCESS:
613 scontext = sidtab_search(&sidtab, ssid);
615 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
620 tcontext = sidtab_search(&sidtab, tsid);
622 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
628 context_init(&newcontext);
630 /* Set the user identity. */
632 case AVTAB_TRANSITION:
634 /* Use the process user identity. */
635 newcontext.user = scontext->user;
638 /* Use the related object owner. */
639 newcontext.user = tcontext->user;
643 /* Set the role and type to default values. */
645 case SECCLASS_PROCESS:
646 /* Use the current role and type of process. */
647 newcontext.role = scontext->role;
648 newcontext.type = scontext->type;
651 /* Use the well-defined object role. */
652 newcontext.role = OBJECT_R_VAL;
653 /* Use the type of the related object. */
654 newcontext.type = tcontext->type;
657 /* Look for a type transition/member/change rule. */
658 avkey.source_type = scontext->type;
659 avkey.target_type = tcontext->type;
660 avkey.target_class = tclass;
661 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
663 /* If no permanent rule, also check for enabled conditional rules */
665 node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
666 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
667 if (node->datum.specified & AVTAB_ENABLED) {
668 avdatum = &node->datum;
674 type_change = (avdatum && (avdatum->specified & specified));
676 /* Use the type from the type transition/member/change rule. */
678 case AVTAB_TRANSITION:
679 newcontext.type = avtab_transition(avdatum);
682 newcontext.type = avtab_member(avdatum);
685 newcontext.type = avtab_change(avdatum);
690 /* Check for class-specific changes. */
692 case SECCLASS_PROCESS:
693 if (specified & AVTAB_TRANSITION) {
694 /* Look for a role transition rule. */
695 for (roletr = policydb.role_tr; roletr;
696 roletr = roletr->next) {
697 if (roletr->role == scontext->role &&
698 roletr->type == tcontext->type) {
699 /* Use the role transition rule. */
700 newcontext.role = roletr->new_role;
706 if (!type_change && !roletr) {
707 /* No change in process role or type. */
715 (newcontext.user == tcontext->user) &&
716 mls_context_cmp(scontext, tcontext)) {
717 /* No change in object type, owner,
718 or MLS attributes. */
725 /* Set the MLS attributes.
726 This is done last because it may allocate memory. */
727 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
731 /* Check the validity of the context. */
732 if (!policydb_context_isvalid(&policydb, &newcontext)) {
733 rc = compute_sid_handle_invalid_context(scontext,
740 /* Obtain the sid for the context. */
741 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
744 context_destroy(&newcontext);
750 * security_transition_sid - Compute the SID for a new subject/object.
751 * @ssid: source security identifier
752 * @tsid: target security identifier
753 * @tclass: target security class
754 * @out_sid: security identifier for new subject/object
756 * Compute a SID to use for labeling a new subject or object in the
757 * class @tclass based on a SID pair (@ssid, @tsid).
758 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
759 * if insufficient memory is available, or %0 if the new SID was
760 * computed successfully.
762 int security_transition_sid(u32 ssid,
767 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
771 * security_member_sid - Compute the SID for member selection.
772 * @ssid: source security identifier
773 * @tsid: target security identifier
774 * @tclass: target security class
775 * @out_sid: security identifier for selected member
777 * Compute a SID to use when selecting a member of a polyinstantiated
778 * object of class @tclass based on a SID pair (@ssid, @tsid).
779 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
780 * if insufficient memory is available, or %0 if the SID was
781 * computed successfully.
783 int security_member_sid(u32 ssid,
788 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
792 * security_change_sid - Compute the SID for object relabeling.
793 * @ssid: source security identifier
794 * @tsid: target security identifier
795 * @tclass: target security class
796 * @out_sid: security identifier for selected member
798 * Compute a SID to use for relabeling an object of class @tclass
799 * based on a SID pair (@ssid, @tsid).
800 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
801 * if insufficient memory is available, or %0 if the SID was
802 * computed successfully.
804 int security_change_sid(u32 ssid,
809 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
813 * Verify that each permission that is defined under the
814 * existing policy is still defined with the same value
817 static int validate_perm(void *key, void *datum, void *p)
820 struct perm_datum *perdatum, *perdatum2;
827 perdatum2 = hashtab_search(h, key);
829 printk(KERN_ERR "security: permission %s disappeared",
834 if (perdatum->value != perdatum2->value) {
835 printk(KERN_ERR "security: the value of permission %s changed",
844 * Verify that each class that is defined under the
845 * existing policy is still defined with the same
846 * attributes in the new policy.
848 static int validate_class(void *key, void *datum, void *p)
850 struct policydb *newp;
851 struct class_datum *cladatum, *cladatum2;
857 cladatum2 = hashtab_search(newp->p_classes.table, key);
859 printk(KERN_ERR "security: class %s disappeared\n",
864 if (cladatum->value != cladatum2->value) {
865 printk(KERN_ERR "security: the value of class %s changed\n",
870 if ((cladatum->comdatum && !cladatum2->comdatum) ||
871 (!cladatum->comdatum && cladatum2->comdatum)) {
872 printk(KERN_ERR "security: the inherits clause for the access "
873 "vector definition for class %s changed\n", (char *)key);
877 if (cladatum->comdatum) {
878 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
879 cladatum2->comdatum->permissions.table);
881 printk(" in the access vector definition for class "
882 "%s\n", (char *)key);
886 rc = hashtab_map(cladatum->permissions.table, validate_perm,
887 cladatum2->permissions.table);
889 printk(" in access vector definition for class %s\n",
895 /* Clone the SID into the new SID table. */
896 static int clone_sid(u32 sid,
897 struct context *context,
900 struct sidtab *s = arg;
902 return sidtab_insert(s, sid, context);
905 static inline int convert_context_handle_invalid_context(struct context *context)
909 if (selinux_enforcing) {
915 context_struct_to_string(context, &s, &len);
916 printk(KERN_ERR "security: context %s is invalid\n", s);
922 struct convert_context_args {
923 struct policydb *oldp;
924 struct policydb *newp;
928 * Convert the values in the security context
929 * structure `c' from the values specified
930 * in the policy `p->oldp' to the values specified
931 * in the policy `p->newp'. Verify that the
932 * context is valid under the new policy.
934 static int convert_context(u32 key,
938 struct convert_context_args *args;
940 struct role_datum *role;
941 struct type_datum *typdatum;
942 struct user_datum *usrdatum;
949 rc = context_cpy(&oldc, c);
955 /* Convert the user. */
956 usrdatum = hashtab_search(args->newp->p_users.table,
957 args->oldp->p_user_val_to_name[c->user - 1]);
961 c->user = usrdatum->value;
963 /* Convert the role. */
964 role = hashtab_search(args->newp->p_roles.table,
965 args->oldp->p_role_val_to_name[c->role - 1]);
969 c->role = role->value;
971 /* Convert the type. */
972 typdatum = hashtab_search(args->newp->p_types.table,
973 args->oldp->p_type_val_to_name[c->type - 1]);
977 c->type = typdatum->value;
979 rc = mls_convert_context(args->oldp, args->newp, c);
983 /* Check the validity of the new context. */
984 if (!policydb_context_isvalid(args->newp, c)) {
985 rc = convert_context_handle_invalid_context(&oldc);
990 context_destroy(&oldc);
994 context_struct_to_string(&oldc, &s, &len);
995 context_destroy(&oldc);
996 printk(KERN_ERR "security: invalidating context %s\n", s);
1001 extern void selinux_complete_init(void);
1004 * security_load_policy - Load a security policy configuration.
1005 * @data: binary policy data
1006 * @len: length of data in bytes
1008 * Load a new set of security policy configuration data,
1009 * validate it and convert the SID table as necessary.
1010 * This function will flush the access vector cache after
1011 * loading the new policy.
1013 int security_load_policy(void *data, size_t len)
1015 struct policydb oldpolicydb, newpolicydb;
1016 struct sidtab oldsidtab, newsidtab;
1017 struct convert_context_args args;
1020 struct policy_file file = { data, len }, *fp = &file;
1024 if (!ss_initialized) {
1025 if (policydb_read(&policydb, fp)) {
1029 if (policydb_load_isids(&policydb, &sidtab)) {
1031 policydb_destroy(&policydb);
1037 selinux_complete_init();
1042 sidtab_hash_eval(&sidtab, "sids");
1045 if (policydb_read(&newpolicydb, fp)) {
1050 sidtab_init(&newsidtab);
1052 /* Verify that the existing classes did not change. */
1053 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1054 printk(KERN_ERR "security: the definition of an existing "
1060 /* Clone the SID table. */
1061 sidtab_shutdown(&sidtab);
1062 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1067 /* Convert the internal representations of contexts
1068 in the new SID table and remove invalid SIDs. */
1069 args.oldp = &policydb;
1070 args.newp = &newpolicydb;
1071 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1073 /* Save the old policydb and SID table to free later. */
1074 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1075 sidtab_set(&oldsidtab, &sidtab);
1077 /* Install the new policydb and SID table. */
1079 memcpy(&policydb, &newpolicydb, sizeof policydb);
1080 sidtab_set(&sidtab, &newsidtab);
1081 seqno = ++latest_granting;
1086 /* Free the old policydb and SID table. */
1087 policydb_destroy(&oldpolicydb);
1088 sidtab_destroy(&oldsidtab);
1090 avc_ss_reset(seqno);
1091 selnl_notify_policyload(seqno);
1097 sidtab_destroy(&newsidtab);
1098 policydb_destroy(&newpolicydb);
1104 * security_port_sid - Obtain the SID for a port.
1105 * @domain: communication domain aka address family
1106 * @type: socket type
1107 * @protocol: protocol number
1108 * @port: port number
1109 * @out_sid: security identifier
1111 int security_port_sid(u16 domain,
1122 c = policydb.ocontexts[OCON_PORT];
1124 if (c->u.port.protocol == protocol &&
1125 c->u.port.low_port <= port &&
1126 c->u.port.high_port >= port)
1133 rc = sidtab_context_to_sid(&sidtab,
1139 *out_sid = c->sid[0];
1141 *out_sid = SECINITSID_PORT;
1150 * security_netif_sid - Obtain the SID for a network interface.
1151 * @name: interface name
1152 * @if_sid: interface SID
1153 * @msg_sid: default SID for received packets
1155 int security_netif_sid(char *name,
1164 c = policydb.ocontexts[OCON_NETIF];
1166 if (strcmp(name, c->u.name) == 0)
1172 if (!c->sid[0] || !c->sid[1]) {
1173 rc = sidtab_context_to_sid(&sidtab,
1178 rc = sidtab_context_to_sid(&sidtab,
1184 *if_sid = c->sid[0];
1185 *msg_sid = c->sid[1];
1187 *if_sid = SECINITSID_NETIF;
1188 *msg_sid = SECINITSID_NETMSG;
1196 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1200 for(i = 0; i < 4; i++)
1201 if(addr[i] != (input[i] & mask[i])) {
1210 * security_node_sid - Obtain the SID for a node (host).
1211 * @domain: communication domain aka address family
1213 * @addrlen: address length in bytes
1214 * @out_sid: security identifier
1216 int security_node_sid(u16 domain,
1230 if (addrlen != sizeof(u32)) {
1235 addr = *((u32 *)addrp);
1237 c = policydb.ocontexts[OCON_NODE];
1239 if (c->u.node.addr == (addr & c->u.node.mask))
1247 if (addrlen != sizeof(u64) * 2) {
1251 c = policydb.ocontexts[OCON_NODE6];
1253 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1261 *out_sid = SECINITSID_NODE;
1267 rc = sidtab_context_to_sid(&sidtab,
1273 *out_sid = c->sid[0];
1275 *out_sid = SECINITSID_NODE;
1286 * security_get_user_sids - Obtain reachable SIDs for a user.
1287 * @fromsid: starting SID
1288 * @username: username
1289 * @sids: array of reachable SIDs for user
1290 * @nel: number of elements in @sids
1292 * Generate the set of SIDs for legal security contexts
1293 * for a given user that can be reached by @fromsid.
1294 * Set *@sids to point to a dynamically allocated
1295 * array containing the set of SIDs. Set *@nel to the
1296 * number of elements in the array.
1299 int security_get_user_sids(u32 fromsid,
1304 struct context *fromcon, usercon;
1305 u32 *mysids, *mysids2, sid;
1306 u32 mynel = 0, maxnel = SIDS_NEL;
1307 struct user_datum *user;
1308 struct role_datum *role;
1309 struct av_decision avd;
1312 if (!ss_initialized) {
1320 fromcon = sidtab_search(&sidtab, fromsid);
1326 user = hashtab_search(policydb.p_users.table, username);
1331 usercon.user = user->value;
1333 mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1338 memset(mysids, 0, maxnel*sizeof(*mysids));
1340 for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1341 if (!ebitmap_get_bit(&user->roles, i))
1343 role = policydb.role_val_to_struct[i];
1345 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1346 if (!ebitmap_get_bit(&role->types, j))
1349 mls_for_user_ranges(user,usercon) {
1350 rc = context_struct_compute_av(fromcon, &usercon,
1352 PROCESS__TRANSITION,
1354 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1356 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1361 if (mynel < maxnel) {
1362 mysids[mynel++] = sid;
1365 mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1371 memset(mysids2, 0, maxnel*sizeof(*mysids2));
1372 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1375 mysids[mynel++] = sid;
1378 mls_end_user_ranges;
1392 * security_genfs_sid - Obtain a SID for a file in a filesystem
1393 * @fstype: filesystem type
1394 * @path: path from root of mount
1395 * @sclass: file security class
1396 * @sid: SID for path
1398 * Obtain a SID to use for a file in a filesystem that
1399 * cannot support xattr or use a fixed labeling behavior like
1400 * transition SIDs or task SIDs.
1402 int security_genfs_sid(const char *fstype,
1408 struct genfs *genfs;
1410 int rc = 0, cmp = 0;
1414 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1415 cmp = strcmp(fstype, genfs->fstype);
1420 if (!genfs || cmp) {
1421 *sid = SECINITSID_UNLABELED;
1426 for (c = genfs->head; c; c = c->next) {
1427 len = strlen(c->u.name);
1428 if ((!c->v.sclass || sclass == c->v.sclass) &&
1429 (strncmp(c->u.name, path, len) == 0))
1434 *sid = SECINITSID_UNLABELED;
1440 rc = sidtab_context_to_sid(&sidtab,
1454 * security_fs_use - Determine how to handle labeling for a filesystem.
1455 * @fstype: filesystem type
1456 * @behavior: labeling behavior
1457 * @sid: SID for filesystem (superblock)
1459 int security_fs_use(
1461 unsigned int *behavior,
1469 c = policydb.ocontexts[OCON_FSUSE];
1471 if (strcmp(fstype, c->u.name) == 0)
1477 *behavior = c->v.behavior;
1479 rc = sidtab_context_to_sid(&sidtab,
1487 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1489 *behavior = SECURITY_FS_USE_NONE;
1492 *behavior = SECURITY_FS_USE_GENFS;
1501 int security_get_bools(int *len, char ***names, int **values)
1503 int i, rc = -ENOMEM;
1509 *len = policydb.p_bools.nprim;
1515 *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
1518 memset(*names, 0, sizeof(char*) * *len);
1520 *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
1524 for (i = 0; i < *len; i++) {
1526 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1527 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1528 (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1531 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1532 (*names)[i][name_len - 1] = 0;
1540 for (i = 0; i < *len; i++)
1550 int security_set_bools(int len, int *values)
1553 int lenp, seqno = 0;
1554 struct cond_node *cur;
1558 lenp = policydb.p_bools.nprim;
1564 printk(KERN_INFO "security: committed booleans { ");
1565 for (i = 0; i < len; i++) {
1567 policydb.bool_val_to_struct[i]->state = 1;
1569 policydb.bool_val_to_struct[i]->state = 0;
1573 printk("%s:%d", policydb.p_bool_val_to_name[i],
1574 policydb.bool_val_to_struct[i]->state);
1578 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1579 rc = evaluate_cond_node(&policydb, cur);
1584 seqno = ++latest_granting;
1589 avc_ss_reset(seqno);
1590 selnl_notify_policyload(seqno);
1595 int security_get_bool_value(int bool)
1602 len = policydb.p_bools.nprim;
1608 rc = policydb.bool_val_to_struct[bool]->state;