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 /* Check the validity of the new context. */
536 if (!policydb_context_isvalid(&policydb, &context)) {
540 /* Obtain the new sid. */
541 rc = sidtab_context_to_sid(&sidtab, &context, sid);
544 context_destroy(&context);
550 static int compute_sid_handle_invalid_context(
551 struct context *scontext,
552 struct context *tcontext,
554 struct context *newcontext)
556 char *s = NULL, *t = NULL, *n = NULL;
557 u32 slen, tlen, nlen;
559 if (context_struct_to_string(scontext, &s, &slen) < 0)
561 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
563 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
565 audit_log(current->audit_context,
566 "security_compute_sid: invalid context %s"
570 n, s, t, policydb.p_class_val_to_name[tclass-1]);
575 if (!selinux_enforcing)
580 static int security_compute_sid(u32 ssid,
586 struct context *scontext = 0, *tcontext = 0, newcontext;
587 struct role_trans *roletr = 0;
588 struct avtab_key avkey;
589 struct avtab_datum *avdatum;
590 struct avtab_node *node;
591 unsigned int type_change = 0;
594 if (!ss_initialized) {
596 case SECCLASS_PROCESS:
608 scontext = sidtab_search(&sidtab, ssid);
610 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
615 tcontext = sidtab_search(&sidtab, tsid);
617 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
623 context_init(&newcontext);
625 /* Set the user identity. */
627 case AVTAB_TRANSITION:
629 /* Use the process user identity. */
630 newcontext.user = scontext->user;
633 /* Use the related object owner. */
634 newcontext.user = tcontext->user;
638 /* Set the role and type to default values. */
640 case SECCLASS_PROCESS:
641 /* Use the current role and type of process. */
642 newcontext.role = scontext->role;
643 newcontext.type = scontext->type;
646 /* Use the well-defined object role. */
647 newcontext.role = OBJECT_R_VAL;
648 /* Use the type of the related object. */
649 newcontext.type = tcontext->type;
652 /* Look for a type transition/member/change rule. */
653 avkey.source_type = scontext->type;
654 avkey.target_type = tcontext->type;
655 avkey.target_class = tclass;
656 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
658 /* If no permanent rule, also check for enabled conditional rules */
660 node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
661 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
662 if (node->datum.specified & AVTAB_ENABLED) {
663 avdatum = &node->datum;
669 type_change = (avdatum && (avdatum->specified & specified));
671 /* Use the type from the type transition/member/change rule. */
673 case AVTAB_TRANSITION:
674 newcontext.type = avtab_transition(avdatum);
677 newcontext.type = avtab_member(avdatum);
680 newcontext.type = avtab_change(avdatum);
685 /* Check for class-specific changes. */
687 case SECCLASS_PROCESS:
688 if (specified & AVTAB_TRANSITION) {
689 /* Look for a role transition rule. */
690 for (roletr = policydb.role_tr; roletr;
691 roletr = roletr->next) {
692 if (roletr->role == scontext->role &&
693 roletr->type == tcontext->type) {
694 /* Use the role transition rule. */
695 newcontext.role = roletr->new_role;
701 if (!type_change && !roletr) {
702 /* No change in process role or type. */
710 (newcontext.user == tcontext->user) &&
711 mls_context_cmp(scontext, tcontext)) {
712 /* No change in object type, owner,
713 or MLS attributes. */
720 /* Set the MLS attributes.
721 This is done last because it may allocate memory. */
722 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
726 /* Check the validity of the context. */
727 if (!policydb_context_isvalid(&policydb, &newcontext)) {
728 rc = compute_sid_handle_invalid_context(scontext,
735 /* Obtain the sid for the context. */
736 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
739 context_destroy(&newcontext);
745 * security_transition_sid - Compute the SID for a new subject/object.
746 * @ssid: source security identifier
747 * @tsid: target security identifier
748 * @tclass: target security class
749 * @out_sid: security identifier for new subject/object
751 * Compute a SID to use for labeling a new subject or object in the
752 * class @tclass based on a SID pair (@ssid, @tsid).
753 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
754 * if insufficient memory is available, or %0 if the new SID was
755 * computed successfully.
757 int security_transition_sid(u32 ssid,
762 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
766 * security_member_sid - Compute the SID for member selection.
767 * @ssid: source security identifier
768 * @tsid: target security identifier
769 * @tclass: target security class
770 * @out_sid: security identifier for selected member
772 * Compute a SID to use when selecting a member of a polyinstantiated
773 * object of class @tclass based on a SID pair (@ssid, @tsid).
774 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
775 * if insufficient memory is available, or %0 if the SID was
776 * computed successfully.
778 int security_member_sid(u32 ssid,
783 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
787 * security_change_sid - Compute the SID for object relabeling.
788 * @ssid: source security identifier
789 * @tsid: target security identifier
790 * @tclass: target security class
791 * @out_sid: security identifier for selected member
793 * Compute a SID to use for relabeling an object of class @tclass
794 * based on a SID pair (@ssid, @tsid).
795 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
796 * if insufficient memory is available, or %0 if the SID was
797 * computed successfully.
799 int security_change_sid(u32 ssid,
804 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
808 * Verify that each permission that is defined under the
809 * existing policy is still defined with the same value
812 static int validate_perm(void *key, void *datum, void *p)
815 struct perm_datum *perdatum, *perdatum2;
822 perdatum2 = hashtab_search(h, key);
824 printk(KERN_ERR "security: permission %s disappeared",
829 if (perdatum->value != perdatum2->value) {
830 printk(KERN_ERR "security: the value of permission %s changed",
839 * Verify that each class that is defined under the
840 * existing policy is still defined with the same
841 * attributes in the new policy.
843 static int validate_class(void *key, void *datum, void *p)
845 struct policydb *newp;
846 struct class_datum *cladatum, *cladatum2;
852 cladatum2 = hashtab_search(newp->p_classes.table, key);
854 printk(KERN_ERR "security: class %s disappeared\n",
859 if (cladatum->value != cladatum2->value) {
860 printk(KERN_ERR "security: the value of class %s changed\n",
865 if ((cladatum->comdatum && !cladatum2->comdatum) ||
866 (!cladatum->comdatum && cladatum2->comdatum)) {
867 printk(KERN_ERR "security: the inherits clause for the access "
868 "vector definition for class %s changed\n", (char *)key);
872 if (cladatum->comdatum) {
873 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
874 cladatum2->comdatum->permissions.table);
876 printk(" in the access vector definition for class "
877 "%s\n", (char *)key);
881 rc = hashtab_map(cladatum->permissions.table, validate_perm,
882 cladatum2->permissions.table);
884 printk(" in access vector definition for class %s\n",
890 /* Clone the SID into the new SID table. */
891 static int clone_sid(u32 sid,
892 struct context *context,
895 struct sidtab *s = arg;
897 return sidtab_insert(s, sid, context);
900 static inline int convert_context_handle_invalid_context(struct context *context)
904 if (selinux_enforcing) {
910 context_struct_to_string(context, &s, &len);
911 printk(KERN_ERR "security: context %s is invalid\n", s);
917 struct convert_context_args {
918 struct policydb *oldp;
919 struct policydb *newp;
923 * Convert the values in the security context
924 * structure `c' from the values specified
925 * in the policy `p->oldp' to the values specified
926 * in the policy `p->newp'. Verify that the
927 * context is valid under the new policy.
929 static int convert_context(u32 key,
933 struct convert_context_args *args;
935 struct role_datum *role;
936 struct type_datum *typdatum;
937 struct user_datum *usrdatum;
944 rc = context_cpy(&oldc, c);
950 /* Convert the user. */
951 usrdatum = hashtab_search(args->newp->p_users.table,
952 args->oldp->p_user_val_to_name[c->user - 1]);
956 c->user = usrdatum->value;
958 /* Convert the role. */
959 role = hashtab_search(args->newp->p_roles.table,
960 args->oldp->p_role_val_to_name[c->role - 1]);
964 c->role = role->value;
966 /* Convert the type. */
967 typdatum = hashtab_search(args->newp->p_types.table,
968 args->oldp->p_type_val_to_name[c->type - 1]);
972 c->type = typdatum->value;
974 rc = mls_convert_context(args->oldp, args->newp, c);
978 /* Check the validity of the new context. */
979 if (!policydb_context_isvalid(args->newp, c)) {
980 rc = convert_context_handle_invalid_context(&oldc);
985 context_destroy(&oldc);
989 context_struct_to_string(&oldc, &s, &len);
990 context_destroy(&oldc);
991 printk(KERN_ERR "security: invalidating context %s\n", s);
996 extern void selinux_complete_init(void);
999 * security_load_policy - Load a security policy configuration.
1000 * @data: binary policy data
1001 * @len: length of data in bytes
1003 * Load a new set of security policy configuration data,
1004 * validate it and convert the SID table as necessary.
1005 * This function will flush the access vector cache after
1006 * loading the new policy.
1008 int security_load_policy(void *data, size_t len)
1010 struct policydb oldpolicydb, newpolicydb;
1011 struct sidtab oldsidtab, newsidtab;
1012 struct convert_context_args args;
1015 struct policy_file file = { data, len }, *fp = &file;
1019 if (!ss_initialized) {
1020 if (policydb_read(&policydb, fp)) {
1024 if (policydb_load_isids(&policydb, &sidtab)) {
1026 policydb_destroy(&policydb);
1032 selinux_complete_init();
1037 sidtab_hash_eval(&sidtab, "sids");
1040 if (policydb_read(&newpolicydb, fp)) {
1045 sidtab_init(&newsidtab);
1047 /* Verify that the existing classes did not change. */
1048 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1049 printk(KERN_ERR "security: the definition of an existing "
1055 /* Clone the SID table. */
1056 sidtab_shutdown(&sidtab);
1057 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1062 /* Convert the internal representations of contexts
1063 in the new SID table and remove invalid SIDs. */
1064 args.oldp = &policydb;
1065 args.newp = &newpolicydb;
1066 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1068 /* Save the old policydb and SID table to free later. */
1069 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1070 sidtab_set(&oldsidtab, &sidtab);
1072 /* Install the new policydb and SID table. */
1074 memcpy(&policydb, &newpolicydb, sizeof policydb);
1075 sidtab_set(&sidtab, &newsidtab);
1076 seqno = ++latest_granting;
1081 /* Free the old policydb and SID table. */
1082 policydb_destroy(&oldpolicydb);
1083 sidtab_destroy(&oldsidtab);
1085 avc_ss_reset(seqno);
1086 selnl_notify_policyload(seqno);
1092 sidtab_destroy(&newsidtab);
1093 policydb_destroy(&newpolicydb);
1099 * security_port_sid - Obtain the SID for a port.
1100 * @domain: communication domain aka address family
1101 * @type: socket type
1102 * @protocol: protocol number
1103 * @port: port number
1104 * @out_sid: security identifier
1106 int security_port_sid(u16 domain,
1117 c = policydb.ocontexts[OCON_PORT];
1119 if (c->u.port.protocol == protocol &&
1120 c->u.port.low_port <= port &&
1121 c->u.port.high_port >= port)
1128 rc = sidtab_context_to_sid(&sidtab,
1134 *out_sid = c->sid[0];
1136 *out_sid = SECINITSID_PORT;
1145 * security_netif_sid - Obtain the SID for a network interface.
1146 * @name: interface name
1147 * @if_sid: interface SID
1148 * @msg_sid: default SID for received packets
1150 int security_netif_sid(char *name,
1159 c = policydb.ocontexts[OCON_NETIF];
1161 if (strcmp(name, c->u.name) == 0)
1167 if (!c->sid[0] || !c->sid[1]) {
1168 rc = sidtab_context_to_sid(&sidtab,
1173 rc = sidtab_context_to_sid(&sidtab,
1179 *if_sid = c->sid[0];
1180 *msg_sid = c->sid[1];
1182 *if_sid = SECINITSID_NETIF;
1183 *msg_sid = SECINITSID_NETMSG;
1191 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1195 for(i = 0; i < 4; i++)
1196 if(addr[i] != (input[i] & mask[i])) {
1205 * security_node_sid - Obtain the SID for a node (host).
1206 * @domain: communication domain aka address family
1208 * @addrlen: address length in bytes
1209 * @out_sid: security identifier
1211 int security_node_sid(u16 domain,
1225 if (addrlen != sizeof(u32)) {
1230 addr = *((u32 *)addrp);
1232 c = policydb.ocontexts[OCON_NODE];
1234 if (c->u.node.addr == (addr & c->u.node.mask))
1242 if (addrlen != sizeof(u64) * 2) {
1246 c = policydb.ocontexts[OCON_NODE6];
1248 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1256 *out_sid = SECINITSID_NODE;
1262 rc = sidtab_context_to_sid(&sidtab,
1268 *out_sid = c->sid[0];
1270 *out_sid = SECINITSID_NODE;
1281 * security_get_user_sids - Obtain reachable SIDs for a user.
1282 * @fromsid: starting SID
1283 * @username: username
1284 * @sids: array of reachable SIDs for user
1285 * @nel: number of elements in @sids
1287 * Generate the set of SIDs for legal security contexts
1288 * for a given user that can be reached by @fromsid.
1289 * Set *@sids to point to a dynamically allocated
1290 * array containing the set of SIDs. Set *@nel to the
1291 * number of elements in the array.
1294 int security_get_user_sids(u32 fromsid,
1299 struct context *fromcon, usercon;
1300 u32 *mysids, *mysids2, sid;
1301 u32 mynel = 0, maxnel = SIDS_NEL;
1302 struct user_datum *user;
1303 struct role_datum *role;
1304 struct av_decision avd;
1307 if (!ss_initialized) {
1315 fromcon = sidtab_search(&sidtab, fromsid);
1321 user = hashtab_search(policydb.p_users.table, username);
1326 usercon.user = user->value;
1328 mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1333 memset(mysids, 0, maxnel*sizeof(*mysids));
1335 for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1336 if (!ebitmap_get_bit(&user->roles, i))
1338 role = policydb.role_val_to_struct[i];
1340 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1341 if (!ebitmap_get_bit(&role->types, j))
1344 mls_for_user_ranges(user,usercon) {
1345 rc = context_struct_compute_av(fromcon, &usercon,
1347 PROCESS__TRANSITION,
1349 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1351 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1356 if (mynel < maxnel) {
1357 mysids[mynel++] = sid;
1360 mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1366 memset(mysids2, 0, maxnel*sizeof(*mysids2));
1367 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1370 mysids[mynel++] = sid;
1373 mls_end_user_ranges;
1387 * security_genfs_sid - Obtain a SID for a file in a filesystem
1388 * @fstype: filesystem type
1389 * @path: path from root of mount
1390 * @sclass: file security class
1391 * @sid: SID for path
1393 * Obtain a SID to use for a file in a filesystem that
1394 * cannot support xattr or use a fixed labeling behavior like
1395 * transition SIDs or task SIDs.
1397 int security_genfs_sid(const char *fstype,
1403 struct genfs *genfs;
1405 int rc = 0, cmp = 0;
1409 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1410 cmp = strcmp(fstype, genfs->fstype);
1415 if (!genfs || cmp) {
1416 *sid = SECINITSID_UNLABELED;
1421 for (c = genfs->head; c; c = c->next) {
1422 len = strlen(c->u.name);
1423 if ((!c->v.sclass || sclass == c->v.sclass) &&
1424 (strncmp(c->u.name, path, len) == 0))
1429 *sid = SECINITSID_UNLABELED;
1435 rc = sidtab_context_to_sid(&sidtab,
1449 * security_fs_use - Determine how to handle labeling for a filesystem.
1450 * @fstype: filesystem type
1451 * @behavior: labeling behavior
1452 * @sid: SID for filesystem (superblock)
1454 int security_fs_use(
1456 unsigned int *behavior,
1464 c = policydb.ocontexts[OCON_FSUSE];
1466 if (strcmp(fstype, c->u.name) == 0)
1472 *behavior = c->v.behavior;
1474 rc = sidtab_context_to_sid(&sidtab,
1482 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1484 *behavior = SECURITY_FS_USE_NONE;
1487 *behavior = SECURITY_FS_USE_GENFS;
1496 int security_get_bools(int *len, char ***names, int **values)
1498 int i, rc = -ENOMEM;
1504 *len = policydb.p_bools.nprim;
1510 *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
1513 memset(*names, 0, sizeof(char*) * *len);
1515 *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
1519 for (i = 0; i < *len; i++) {
1521 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1522 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1523 (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1526 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1527 (*names)[i][name_len - 1] = 0;
1535 for (i = 0; i < *len; i++)
1545 int security_set_bools(int len, int *values)
1548 int lenp, seqno = 0;
1549 struct cond_node *cur;
1553 lenp = policydb.p_bools.nprim;
1559 printk(KERN_INFO "security: committed booleans { ");
1560 for (i = 0; i < len; i++) {
1562 policydb.bool_val_to_struct[i]->state = 1;
1564 policydb.bool_val_to_struct[i]->state = 0;
1568 printk("%s:%d", policydb.p_bool_val_to_name[i],
1569 policydb.bool_val_to_struct[i]->state);
1573 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1574 rc = evaluate_cond_node(&policydb, cur);
1579 seqno = ++latest_granting;
1584 avc_ss_reset(seqno);
1585 selnl_notify_policyload(seqno);
1590 int security_get_bool_value(int bool)
1597 len = policydb.p_bools.nprim;
1603 rc = policydb.bool_val_to_struct[bool]->state;