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 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <asm/semaphore.h>
31 static rwlock_t policy_rwlock = RW_LOCK_UNLOCKED;
32 #define POLICY_RDLOCK read_lock(&policy_rwlock)
33 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
34 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
35 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
37 static DECLARE_MUTEX(load_sem);
38 #define LOAD_LOCK down(&load_sem)
39 #define LOAD_UNLOCK up(&load_sem)
42 struct policydb policydb;
43 int ss_initialized = 0;
46 * The largest sequence number that has been used when
47 * providing an access decision to the access vector cache.
48 * The sequence number only changes when a policy change
51 static u32 latest_granting = 0;
54 * Return the boolean value of a constraint expression
55 * when it is applied to the specified source and target
58 static int constraint_expr_eval(struct context *scontext,
59 struct context *tcontext,
60 struct constraint_expr *cexpr)
64 struct role_datum *r1, *r2;
65 struct constraint_expr *e;
66 int s[CEXPR_MAXDEPTH];
69 for (e = cexpr; e; e = e->next) {
70 switch (e->expr_type) {
86 if (sp == (CEXPR_MAXDEPTH-1))
90 val1 = scontext->user;
91 val2 = tcontext->user;
94 val1 = scontext->type;
95 val2 = tcontext->type;
98 val1 = scontext->role;
99 val2 = tcontext->role;
100 r1 = policydb.role_val_to_struct[val1 - 1];
101 r2 = policydb.role_val_to_struct[val2 - 1];
104 s[++sp] = ebitmap_get_bit(&r1->dominates,
108 s[++sp] = ebitmap_get_bit(&r2->dominates,
112 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
114 !ebitmap_get_bit(&r2->dominates,
128 s[++sp] = (val1 == val2);
131 s[++sp] = (val1 != val2);
139 if (sp == (CEXPR_MAXDEPTH-1))
142 if (e->attr & CEXPR_TARGET)
144 if (e->attr & CEXPR_USER)
146 else if (e->attr & CEXPR_ROLE)
148 else if (e->attr & CEXPR_TYPE)
157 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
160 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
178 * Compute access vectors based on a context structure pair for
179 * the permissions in a particular class.
181 static int context_struct_compute_av(struct context *scontext,
182 struct context *tcontext,
185 struct av_decision *avd)
187 struct constraint_node *constraint;
188 struct role_allow *ra;
189 struct avtab_key avkey;
190 struct avtab_datum *avdatum;
191 struct class_datum *tclass_datum;
193 if (!tclass || tclass > policydb.p_classes.nprim) {
194 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
198 tclass_datum = policydb.class_val_to_struct[tclass - 1];
201 * Initialize the access vectors to the default values.
204 avd->decided = 0xffffffff;
206 avd->auditdeny = 0xffffffff;
207 avd->seqno = latest_granting;
210 * If a specific type enforcement rule was defined for
211 * this permission check, then use it.
213 avkey.source_type = scontext->type;
214 avkey.target_type = tcontext->type;
215 avkey.target_class = tclass;
216 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_AV);
218 if (avdatum->specified & AVTAB_ALLOWED)
219 avd->allowed = avtab_allowed(avdatum);
220 if (avdatum->specified & AVTAB_AUDITDENY)
221 avd->auditdeny = avtab_auditdeny(avdatum);
222 if (avdatum->specified & AVTAB_AUDITALLOW)
223 avd->auditallow = avtab_auditallow(avdatum);
227 * Remove any permissions prohibited by the MLS policy.
229 mls_compute_av(scontext, tcontext, tclass_datum, &avd->allowed);
232 * Remove any permissions prohibited by a constraint.
234 constraint = tclass_datum->constraints;
236 if ((constraint->permissions & (avd->allowed)) &&
237 !constraint_expr_eval(scontext, tcontext,
239 avd->allowed = (avd->allowed) & ~(constraint->permissions);
241 constraint = constraint->next;
245 * If checking process transition permission and the
246 * role is changing, then check the (current_role, new_role)
249 if (tclass == SECCLASS_PROCESS &&
250 avd->allowed && PROCESS__TRANSITION &&
251 scontext->role != tcontext->role) {
252 for (ra = policydb.role_allow; ra; ra = ra->next) {
253 if (scontext->role == ra->role &&
254 tcontext->role == ra->new_role)
258 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION);
265 * security_compute_av - Compute access vector decisions.
266 * @ssid: source security identifier
267 * @tsid: target security identifier
268 * @tclass: target security class
269 * @requested: requested permissions
270 * @avd: access vector decisions
272 * Compute a set of access vector decisions based on the
273 * SID pair (@ssid, @tsid) for the permissions in @tclass.
274 * Return -%EINVAL if any of the parameters are invalid or %0
275 * if the access vector decisions were computed successfully.
277 int security_compute_av(u32 ssid,
281 struct av_decision *avd)
283 struct context *scontext = 0, *tcontext = 0;
286 if (!ss_initialized) {
287 avd->allowed = requested;
288 avd->decided = requested;
290 avd->auditdeny = 0xffffffff;
291 avd->seqno = latest_granting;
297 scontext = sidtab_search(&sidtab, ssid);
299 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
304 tcontext = sidtab_search(&sidtab, tsid);
306 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
312 rc = context_struct_compute_av(scontext, tcontext, tclass,
320 * Write the security context string representation of
321 * the context structure `context' into a dynamically
322 * allocated string of the correct size. Set `*scontext'
323 * to point to this string and set `*scontext_len' to
324 * the length of the string.
326 int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
333 /* Compute the size of the context. */
334 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
335 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
336 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
337 *scontext_len += mls_compute_context_len(context);
339 /* Allocate space for the context; caller must free this space. */
340 scontextp = kmalloc(*scontext_len+1,GFP_ATOMIC);
344 *scontext = scontextp;
347 * Copy the user name, role name and type name into the context.
349 sprintf(scontextp, "%s:%s:%s:",
350 policydb.p_user_val_to_name[context->user - 1],
351 policydb.p_role_val_to_name[context->role - 1],
352 policydb.p_type_val_to_name[context->type - 1]);
353 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
354 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
355 1 + strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
357 mls_sid_to_context(context, &scontextp);
365 #include "initial_sid_to_string.h"
368 * security_sid_to_context - Obtain a context for a given SID.
369 * @sid: security identifier, SID
370 * @scontext: security context
371 * @scontext_len: length in bytes
373 * Write the string representation of the context associated with @sid
374 * into a dynamically allocated string of the correct size. Set @scontext
375 * to point to this string and set @scontext_len to the length of the string.
377 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
379 struct context *context;
382 if (!ss_initialized) {
383 if (sid <= SECINITSID_NUM) {
386 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
387 scontextp = kmalloc(*scontext_len,GFP_KERNEL);
388 strcpy(scontextp, initial_sid_to_string[sid]);
389 *scontext = scontextp;
392 printk(KERN_ERR "security_sid_to_context: called before initial "
393 "load_policy on unknown SID %d\n", sid);
398 context = sidtab_search(&sidtab, sid);
400 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
405 rc = context_struct_to_string(context, scontext, scontext_len);
414 * security_context_to_sid - Obtain a SID for a given security context.
415 * @scontext: security context
416 * @scontext_len: length in bytes
417 * @sid: security identifier, SID
419 * Obtains a SID associated with the security context that
420 * has the string representation specified by @scontext.
421 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
422 * memory is available, or 0 on success.
424 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
427 struct context context;
428 struct role_datum *role;
429 struct type_datum *typdatum;
430 struct user_datum *usrdatum;
431 char *scontextp, *p, oldc;
434 if (!ss_initialized) {
437 for (i = 1; i < SECINITSID_NUM; i++) {
438 if (!strcmp(initial_sid_to_string[i], scontext)) {
443 printk(KERN_ERR "security_context_to_sid: called before "
444 "initial load_policy on unknown context %s\n", scontext);
450 /* Copy the string so that we can modify the copy as we parse it.
451 The string should already by null terminated, but we append a
452 null suffix to the copy to avoid problems with the existing
453 attr package, which doesn't view the null terminator as part
454 of the attribute value. */
455 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
460 memcpy(scontext2, scontext, scontext_len);
461 scontext2[scontext_len] = 0;
463 context_init(&context);
468 /* Parse the security context. */
471 scontextp = (char *) scontext2;
473 /* Extract the user. */
475 while (*p && *p != ':')
483 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
487 context.user = usrdatum->value;
491 while (*p && *p != ':')
499 role = hashtab_search(policydb.p_roles.table, scontextp);
502 context.role = role->value;
506 while (*p && *p != ':')
511 typdatum = hashtab_search(policydb.p_types.table, scontextp);
515 context.type = typdatum->value;
517 rc = mls_context_to_sid(oldc, &p, &context);
521 /* Check the validity of the new context. */
522 if (!policydb_context_isvalid(&policydb, &context)) {
526 /* Obtain the new sid. */
527 rc = sidtab_context_to_sid(&sidtab, &context, sid);
530 context_destroy(&context);
536 static inline int compute_sid_handle_invalid_context(
537 struct context *scontext,
538 struct context *tcontext,
540 struct context *newcontext)
544 if (selinux_enforcing) {
548 u32 slen, tlen, nlen;
550 context_struct_to_string(scontext, &s, &slen);
551 context_struct_to_string(tcontext, &t, &tlen);
552 context_struct_to_string(newcontext, &n, &nlen);
553 printk(KERN_ERR "security_compute_sid: invalid context %s", n);
554 printk(" for scontext=%s", s);
555 printk(" tcontext=%s", t);
556 printk(" tclass=%s\n", policydb.p_class_val_to_name[tclass-1]);
564 static int security_compute_sid(u32 ssid,
570 struct context *scontext = 0, *tcontext = 0, newcontext;
571 struct role_trans *roletr = 0;
572 struct avtab_key avkey;
573 struct avtab_datum *avdatum;
574 unsigned int type_change = 0;
577 if (!ss_initialized) {
579 case SECCLASS_PROCESS:
591 scontext = sidtab_search(&sidtab, ssid);
593 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
598 tcontext = sidtab_search(&sidtab, tsid);
600 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
606 context_init(&newcontext);
608 /* Set the user identity. */
610 case AVTAB_TRANSITION:
612 /* Use the process user identity. */
613 newcontext.user = scontext->user;
616 /* Use the related object owner. */
617 newcontext.user = tcontext->user;
621 /* Set the role and type to default values. */
623 case SECCLASS_PROCESS:
624 /* Use the current role and type of process. */
625 newcontext.role = scontext->role;
626 newcontext.type = scontext->type;
629 /* Use the well-defined object role. */
630 newcontext.role = OBJECT_R_VAL;
631 /* Use the type of the related object. */
632 newcontext.type = tcontext->type;
635 /* Look for a type transition/member/change rule. */
636 avkey.source_type = scontext->type;
637 avkey.target_type = tcontext->type;
638 avkey.target_class = tclass;
639 avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
640 type_change = (avdatum && (avdatum->specified & specified));
642 /* Use the type from the type transition/member/change rule. */
644 case AVTAB_TRANSITION:
645 newcontext.type = avtab_transition(avdatum);
648 newcontext.type = avtab_member(avdatum);
651 newcontext.type = avtab_change(avdatum);
656 /* Check for class-specific changes. */
658 case SECCLASS_PROCESS:
659 if (specified & AVTAB_TRANSITION) {
660 /* Look for a role transition rule. */
661 for (roletr = policydb.role_tr; roletr;
662 roletr = roletr->next) {
663 if (roletr->role == scontext->role &&
664 roletr->type == tcontext->type) {
665 /* Use the role transition rule. */
666 newcontext.role = roletr->new_role;
672 if (!type_change && !roletr) {
673 /* No change in process role or type. */
681 (newcontext.user == tcontext->user) &&
682 mls_context_cmp(scontext, tcontext)) {
683 /* No change in object type, owner,
684 or MLS attributes. */
691 /* Set the MLS attributes.
692 This is done last because it may allocate memory. */
693 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
697 /* Check the validity of the context. */
698 if (!policydb_context_isvalid(&policydb, &newcontext)) {
699 rc = compute_sid_handle_invalid_context(scontext,
706 /* Obtain the sid for the context. */
707 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
710 context_destroy(&newcontext);
716 * security_transition_sid - Compute the SID for a new subject/object.
717 * @ssid: source security identifier
718 * @tsid: target security identifier
719 * @tclass: target security class
720 * @out_sid: security identifier for new subject/object
722 * Compute a SID to use for labeling a new subject or object in the
723 * class @tclass based on a SID pair (@ssid, @tsid).
724 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
725 * if insufficient memory is available, or %0 if the new SID was
726 * computed successfully.
728 int security_transition_sid(u32 ssid,
733 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
737 * security_member_sid - Compute the SID for member selection.
738 * @ssid: source security identifier
739 * @tsid: target security identifier
740 * @tclass: target security class
741 * @out_sid: security identifier for selected member
743 * Compute a SID to use when selecting a member of a polyinstantiated
744 * object of class @tclass based on a SID pair (@ssid, @tsid).
745 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
746 * if insufficient memory is available, or %0 if the SID was
747 * computed successfully.
749 int security_member_sid(u32 ssid,
754 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
758 * security_change_sid - Compute the SID for object relabeling.
759 * @ssid: source security identifier
760 * @tsid: target security identifier
761 * @tclass: target security class
762 * @out_sid: security identifier for selected member
764 * Compute a SID to use for relabeling an object of class @tclass
765 * based on a SID pair (@ssid, @tsid).
766 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
767 * if insufficient memory is available, or %0 if the SID was
768 * computed successfully.
770 int security_change_sid(u32 ssid,
775 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
779 * Verify that each permission that is defined under the
780 * existing policy is still defined with the same value
783 static int validate_perm(void *key, void *datum, void *p)
786 struct perm_datum *perdatum, *perdatum2;
793 perdatum2 = hashtab_search(h, key);
795 printk(KERN_ERR "security: permission %s disappeared",
800 if (perdatum->value != perdatum2->value) {
801 printk(KERN_ERR "security: the value of permission %s changed",
810 * Verify that each class that is defined under the
811 * existing policy is still defined with the same
812 * attributes in the new policy.
814 static int validate_class(void *key, void *datum, void *p)
816 struct policydb *newp;
817 struct class_datum *cladatum, *cladatum2;
823 cladatum2 = hashtab_search(newp->p_classes.table, key);
825 printk(KERN_ERR "security: class %s disappeared\n",
830 if (cladatum->value != cladatum2->value) {
831 printk(KERN_ERR "security: the value of class %s changed\n",
836 if ((cladatum->comdatum && !cladatum2->comdatum) ||
837 (!cladatum->comdatum && cladatum2->comdatum)) {
838 printk(KERN_ERR "security: the inherits clause for the access "
839 "vector definition for class %s changed\n", (char *)key);
843 if (cladatum->comdatum) {
844 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
845 cladatum2->comdatum->permissions.table);
847 printk(" in the access vector definition for class "
848 "%s\n", (char *)key);
852 rc = hashtab_map(cladatum->permissions.table, validate_perm,
853 cladatum2->permissions.table);
855 printk(" in access vector definition for class %s\n",
861 /* Clone the SID into the new SID table. */
862 static int clone_sid(u32 sid,
863 struct context *context,
866 struct sidtab *s = arg;
868 return sidtab_insert(s, sid, context);
871 static inline int convert_context_handle_invalid_context(struct context *context)
875 if (selinux_enforcing) {
881 context_struct_to_string(context, &s, &len);
882 printk(KERN_ERR "security: context %s is invalid\n", s);
888 struct convert_context_args {
889 struct policydb *oldp;
890 struct policydb *newp;
894 * Convert the values in the security context
895 * structure `c' from the values specified
896 * in the policy `p->oldp' to the values specified
897 * in the policy `p->newp'. Verify that the
898 * context is valid under the new policy.
900 static int convert_context(u32 key,
904 struct convert_context_args *args;
906 struct role_datum *role;
907 struct type_datum *typdatum;
908 struct user_datum *usrdatum;
915 rc = context_cpy(&oldc, c);
921 /* Convert the user. */
922 usrdatum = hashtab_search(args->newp->p_users.table,
923 args->oldp->p_user_val_to_name[c->user - 1]);
927 c->user = usrdatum->value;
929 /* Convert the role. */
930 role = hashtab_search(args->newp->p_roles.table,
931 args->oldp->p_role_val_to_name[c->role - 1]);
935 c->role = role->value;
937 /* Convert the type. */
938 typdatum = hashtab_search(args->newp->p_types.table,
939 args->oldp->p_type_val_to_name[c->type - 1]);
943 c->type = typdatum->value;
945 rc = mls_convert_context(args->oldp, args->newp, c);
949 /* Check the validity of the new context. */
950 if (!policydb_context_isvalid(args->newp, c)) {
951 rc = convert_context_handle_invalid_context(&oldc);
956 context_destroy(&oldc);
960 context_struct_to_string(&oldc, &s, &len);
961 context_destroy(&oldc);
962 printk(KERN_ERR "security: invalidating context %s\n", s);
967 extern void selinux_complete_init(void);
970 * security_load_policy - Load a security policy configuration.
971 * @data: binary policy data
972 * @len: length of data in bytes
974 * Load a new set of security policy configuration data,
975 * validate it and convert the SID table as necessary.
976 * This function will flush the access vector cache after
977 * loading the new policy.
979 int security_load_policy(void *data, size_t len)
981 struct policydb oldpolicydb, newpolicydb;
982 struct sidtab oldsidtab, newsidtab;
983 struct convert_context_args args;
986 struct policy_file file = { data, len }, *fp = &file;
990 if (!ss_initialized) {
991 if (policydb_read(&policydb, fp)) {
995 if (policydb_load_isids(&policydb, &sidtab)) {
997 policydb_destroy(&policydb);
1002 selinux_complete_init();
1007 sidtab_hash_eval(&sidtab, "sids");
1010 if (policydb_read(&newpolicydb, fp)) {
1015 sidtab_init(&newsidtab);
1017 /* Verify that the existing classes did not change. */
1018 if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1019 printk(KERN_ERR "security: the definition of an existing "
1025 /* Clone the SID table. */
1026 sidtab_shutdown(&sidtab);
1027 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1032 /* Convert the internal representations of contexts
1033 in the new SID table and remove invalid SIDs. */
1034 args.oldp = &policydb;
1035 args.newp = &newpolicydb;
1036 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1038 /* Save the old policydb and SID table to free later. */
1039 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1040 sidtab_set(&oldsidtab, &sidtab);
1042 /* Install the new policydb and SID table. */
1044 memcpy(&policydb, &newpolicydb, sizeof policydb);
1045 sidtab_set(&sidtab, &newsidtab);
1046 seqno = ++latest_granting;
1050 /* Free the old policydb and SID table. */
1051 policydb_destroy(&oldpolicydb);
1052 sidtab_destroy(&oldsidtab);
1054 avc_ss_reset(seqno);
1060 sidtab_destroy(&newsidtab);
1061 policydb_destroy(&newpolicydb);
1067 * security_port_sid - Obtain the SID for a port.
1068 * @domain: communication domain aka address family
1069 * @type: socket type
1070 * @protocol: protocol number
1071 * @port: port number
1072 * @out_sid: security identifier
1074 int security_port_sid(u16 domain,
1085 c = policydb.ocontexts[OCON_PORT];
1087 if (c->u.port.protocol == protocol &&
1088 c->u.port.low_port <= port &&
1089 c->u.port.high_port >= port)
1096 rc = sidtab_context_to_sid(&sidtab,
1102 *out_sid = c->sid[0];
1104 *out_sid = SECINITSID_PORT;
1113 * security_netif_sid - Obtain the SID for a network interface.
1114 * @name: interface name
1115 * @if_sid: interface SID
1116 * @msg_sid: default SID for received packets
1118 int security_netif_sid(char *name,
1127 c = policydb.ocontexts[OCON_NETIF];
1129 if (strcmp(name, c->u.name) == 0)
1135 if (!c->sid[0] || !c->sid[1]) {
1136 rc = sidtab_context_to_sid(&sidtab,
1141 rc = sidtab_context_to_sid(&sidtab,
1147 *if_sid = c->sid[0];
1148 *msg_sid = c->sid[1];
1150 *if_sid = SECINITSID_NETIF;
1151 *msg_sid = SECINITSID_NETMSG;
1161 * security_node_sid - Obtain the SID for a node (host).
1162 * @domain: communication domain aka address family
1164 * @addrlen: address length in bytes
1165 * @out_sid: security identifier
1167 int security_node_sid(u16 domain,
1178 if (domain != AF_INET || addrlen != sizeof(u32)) {
1179 *out_sid = SECINITSID_NODE;
1182 addr = *((u32 *)addrp);
1184 c = policydb.ocontexts[OCON_NODE];
1186 if (c->u.node.addr == (addr & c->u.node.mask))
1193 rc = sidtab_context_to_sid(&sidtab,
1199 *out_sid = c->sid[0];
1201 *out_sid = SECINITSID_NODE;
1212 * security_get_user_sids - Obtain reachable SIDs for a user.
1213 * @fromsid: starting SID
1214 * @username: username
1215 * @sids: array of reachable SIDs for user
1216 * @nel: number of elements in @sids
1218 * Generate the set of SIDs for legal security contexts
1219 * for a given user that can be reached by @fromsid.
1220 * Set *@sids to point to a dynamically allocated
1221 * array containing the set of SIDs. Set *@nel to the
1222 * number of elements in the array.
1225 int security_get_user_sids(u32 fromsid,
1230 struct context *fromcon, usercon;
1231 u32 *mysids, *mysids2, sid;
1232 u32 mynel = 0, maxnel = SIDS_NEL;
1233 struct user_datum *user;
1234 struct role_datum *role;
1235 struct av_decision avd;
1238 if (!ss_initialized) {
1246 fromcon = sidtab_search(&sidtab, fromsid);
1252 user = hashtab_search(policydb.p_users.table, username);
1257 usercon.user = user->value;
1259 mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1264 memset(mysids, 0, maxnel*sizeof(*mysids));
1266 for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1267 if (!ebitmap_get_bit(&user->roles, i))
1269 role = policydb.role_val_to_struct[i];
1271 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1272 if (!ebitmap_get_bit(&role->types, j))
1275 if (usercon.type == fromcon->type)
1277 mls_for_user_ranges(user,usercon) {
1278 rc = context_struct_compute_av(fromcon, &usercon,
1280 PROCESS__TRANSITION,
1282 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1284 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1289 if (mynel < maxnel) {
1290 mysids[mynel++] = sid;
1293 mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1299 memset(mysids2, 0, maxnel*sizeof(*mysids2));
1300 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1303 mysids[mynel++] = sid;
1306 mls_end_user_ranges;
1320 * security_genfs_sid - Obtain a SID for a file in a filesystem
1321 * @fstype: filesystem type
1322 * @path: path from root of mount
1323 * @sclass: file security class
1324 * @sid: SID for path
1326 * Obtain a SID to use for a file in a filesystem that
1327 * cannot support xattr or use a fixed labeling behavior like
1328 * transition SIDs or task SIDs.
1330 int security_genfs_sid(const char *fstype,
1336 struct genfs *genfs;
1338 int rc = 0, cmp = 0;
1342 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1343 cmp = strcmp(fstype, genfs->fstype);
1348 if (!genfs || cmp) {
1349 *sid = SECINITSID_UNLABELED;
1354 for (c = genfs->head; c; c = c->next) {
1355 len = strlen(c->u.name);
1356 if ((!c->v.sclass || sclass == c->v.sclass) &&
1357 (strncmp(c->u.name, path, len) == 0))
1362 *sid = SECINITSID_UNLABELED;
1368 rc = sidtab_context_to_sid(&sidtab,
1382 * security_fs_use - Determine how to handle labeling for a filesystem.
1383 * @fstype: filesystem type
1384 * @behavior: labeling behavior
1385 * @sid: SID for filesystem (superblock)
1387 int security_fs_use(
1389 unsigned int *behavior,
1397 c = policydb.ocontexts[OCON_FSUSE];
1399 if (strcmp(fstype, c->u.name) == 0)
1405 *behavior = c->v.behavior;
1407 rc = sidtab_context_to_sid(&sidtab,
1415 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1417 *behavior = SECURITY_FS_USE_NONE;
1420 *behavior = SECURITY_FS_USE_GENFS;