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 <linux/mutex.h>
40 #include "conditional.h"
43 extern void selnl_notify_policyload(u32 seqno
);
44 unsigned int policydb_loaded_version
;
46 static DEFINE_RWLOCK(policy_rwlock
);
47 #define POLICY_RDLOCK read_lock(&policy_rwlock)
48 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
49 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
50 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
52 static DEFINE_MUTEX(load_mutex
);
53 #define LOAD_LOCK mutex_lock(&load_mutex)
54 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
56 static struct sidtab sidtab
;
57 struct policydb policydb
;
58 int ss_initialized
= 0;
61 * The largest sequence number that has been used when
62 * providing an access decision to the access vector cache.
63 * The sequence number only changes when a policy change
66 static u32 latest_granting
= 0;
68 /* Forward declaration. */
69 static int context_struct_to_string(struct context
*context
, char **scontext
,
73 * Return the boolean value of a constraint expression
74 * when it is applied to the specified source and target
77 * xcontext is a special beast... It is used by the validatetrans rules
78 * only. For these rules, scontext is the context before the transition,
79 * tcontext is the context after the transition, and xcontext is the context
80 * of the process performing the transition. All other callers of
81 * constraint_expr_eval should pass in NULL for xcontext.
83 static int constraint_expr_eval(struct context
*scontext
,
84 struct context
*tcontext
,
85 struct context
*xcontext
,
86 struct constraint_expr
*cexpr
)
90 struct role_datum
*r1
, *r2
;
91 struct mls_level
*l1
, *l2
;
92 struct constraint_expr
*e
;
93 int s
[CEXPR_MAXDEPTH
];
96 for (e
= cexpr
; e
; e
= e
->next
) {
97 switch (e
->expr_type
) {
113 if (sp
== (CEXPR_MAXDEPTH
-1))
117 val1
= scontext
->user
;
118 val2
= tcontext
->user
;
121 val1
= scontext
->type
;
122 val2
= tcontext
->type
;
125 val1
= scontext
->role
;
126 val2
= tcontext
->role
;
127 r1
= policydb
.role_val_to_struct
[val1
- 1];
128 r2
= policydb
.role_val_to_struct
[val2
- 1];
131 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
135 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
139 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
141 !ebitmap_get_bit(&r2
->dominates
,
149 l1
= &(scontext
->range
.level
[0]);
150 l2
= &(tcontext
->range
.level
[0]);
153 l1
= &(scontext
->range
.level
[0]);
154 l2
= &(tcontext
->range
.level
[1]);
157 l1
= &(scontext
->range
.level
[1]);
158 l2
= &(tcontext
->range
.level
[0]);
161 l1
= &(scontext
->range
.level
[1]);
162 l2
= &(tcontext
->range
.level
[1]);
165 l1
= &(scontext
->range
.level
[0]);
166 l2
= &(scontext
->range
.level
[1]);
169 l1
= &(tcontext
->range
.level
[0]);
170 l2
= &(tcontext
->range
.level
[1]);
175 s
[++sp
] = mls_level_eq(l1
, l2
);
178 s
[++sp
] = !mls_level_eq(l1
, l2
);
181 s
[++sp
] = mls_level_dom(l1
, l2
);
184 s
[++sp
] = mls_level_dom(l2
, l1
);
187 s
[++sp
] = mls_level_incomp(l2
, l1
);
201 s
[++sp
] = (val1
== val2
);
204 s
[++sp
] = (val1
!= val2
);
212 if (sp
== (CEXPR_MAXDEPTH
-1))
215 if (e
->attr
& CEXPR_TARGET
)
217 else if (e
->attr
& CEXPR_XTARGET
) {
224 if (e
->attr
& CEXPR_USER
)
226 else if (e
->attr
& CEXPR_ROLE
)
228 else if (e
->attr
& CEXPR_TYPE
)
237 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
240 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
258 * Compute access vectors based on a context structure pair for
259 * the permissions in a particular class.
261 static int context_struct_compute_av(struct context
*scontext
,
262 struct context
*tcontext
,
265 struct av_decision
*avd
)
267 struct constraint_node
*constraint
;
268 struct role_allow
*ra
;
269 struct avtab_key avkey
;
270 struct avtab_node
*node
;
271 struct class_datum
*tclass_datum
;
272 struct ebitmap
*sattr
, *tattr
;
273 struct ebitmap_node
*snode
, *tnode
;
277 * Remap extended Netlink classes for old policy versions.
278 * Do this here rather than socket_type_to_security_class()
279 * in case a newer policy version is loaded, allowing sockets
280 * to remain in the correct class.
282 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
283 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
284 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
285 tclass
= SECCLASS_NETLINK_SOCKET
;
287 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
288 printk(KERN_ERR
"security_compute_av: unrecognized class %d\n",
292 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
295 * Initialize the access vectors to the default values.
298 avd
->decided
= 0xffffffff;
300 avd
->auditdeny
= 0xffffffff;
301 avd
->seqno
= latest_granting
;
304 * If a specific type enforcement rule was defined for
305 * this permission check, then use it.
307 avkey
.target_class
= tclass
;
308 avkey
.specified
= AVTAB_AV
;
309 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
310 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
311 ebitmap_for_each_bit(sattr
, snode
, i
) {
312 if (!ebitmap_node_get_bit(snode
, i
))
314 ebitmap_for_each_bit(tattr
, tnode
, j
) {
315 if (!ebitmap_node_get_bit(tnode
, j
))
317 avkey
.source_type
= i
+ 1;
318 avkey
.target_type
= j
+ 1;
319 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
321 node
= avtab_search_node_next(node
, avkey
.specified
)) {
322 if (node
->key
.specified
== AVTAB_ALLOWED
)
323 avd
->allowed
|= node
->datum
.data
;
324 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
325 avd
->auditallow
|= node
->datum
.data
;
326 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
327 avd
->auditdeny
&= node
->datum
.data
;
330 /* Check conditional av table for additional permissions */
331 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
337 * Remove any permissions prohibited by a constraint (this includes
340 constraint
= tclass_datum
->constraints
;
342 if ((constraint
->permissions
& (avd
->allowed
)) &&
343 !constraint_expr_eval(scontext
, tcontext
, NULL
,
345 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
347 constraint
= constraint
->next
;
351 * If checking process transition permission and the
352 * role is changing, then check the (current_role, new_role)
355 if (tclass
== SECCLASS_PROCESS
&&
356 (avd
->allowed
& (PROCESS__TRANSITION
| PROCESS__DYNTRANSITION
)) &&
357 scontext
->role
!= tcontext
->role
) {
358 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
359 if (scontext
->role
== ra
->role
&&
360 tcontext
->role
== ra
->new_role
)
364 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
|
365 PROCESS__DYNTRANSITION
);
371 static int security_validtrans_handle_fail(struct context
*ocontext
,
372 struct context
*ncontext
,
373 struct context
*tcontext
,
376 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
377 u32 olen
, nlen
, tlen
;
379 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
381 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
383 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
385 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
386 "security_validate_transition: denied for"
387 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
388 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
394 if (!selinux_enforcing
)
399 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
402 struct context
*ocontext
;
403 struct context
*ncontext
;
404 struct context
*tcontext
;
405 struct class_datum
*tclass_datum
;
406 struct constraint_node
*constraint
;
415 * Remap extended Netlink classes for old policy versions.
416 * Do this here rather than socket_type_to_security_class()
417 * in case a newer policy version is loaded, allowing sockets
418 * to remain in the correct class.
420 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
421 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
422 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
423 tclass
= SECCLASS_NETLINK_SOCKET
;
425 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
426 printk(KERN_ERR
"security_validate_transition: "
427 "unrecognized class %d\n", tclass
);
431 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
433 ocontext
= sidtab_search(&sidtab
, oldsid
);
435 printk(KERN_ERR
"security_validate_transition: "
436 " unrecognized SID %d\n", oldsid
);
441 ncontext
= sidtab_search(&sidtab
, newsid
);
443 printk(KERN_ERR
"security_validate_transition: "
444 " unrecognized SID %d\n", newsid
);
449 tcontext
= sidtab_search(&sidtab
, tasksid
);
451 printk(KERN_ERR
"security_validate_transition: "
452 " unrecognized SID %d\n", tasksid
);
457 constraint
= tclass_datum
->validatetrans
;
459 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
461 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
465 constraint
= constraint
->next
;
474 * security_compute_av - Compute access vector decisions.
475 * @ssid: source security identifier
476 * @tsid: target security identifier
477 * @tclass: target security class
478 * @requested: requested permissions
479 * @avd: access vector decisions
481 * Compute a set of access vector decisions based on the
482 * SID pair (@ssid, @tsid) for the permissions in @tclass.
483 * Return -%EINVAL if any of the parameters are invalid or %0
484 * if the access vector decisions were computed successfully.
486 int security_compute_av(u32 ssid
,
490 struct av_decision
*avd
)
492 struct context
*scontext
= NULL
, *tcontext
= NULL
;
495 if (!ss_initialized
) {
496 avd
->allowed
= 0xffffffff;
497 avd
->decided
= 0xffffffff;
499 avd
->auditdeny
= 0xffffffff;
500 avd
->seqno
= latest_granting
;
506 scontext
= sidtab_search(&sidtab
, ssid
);
508 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
513 tcontext
= sidtab_search(&sidtab
, tsid
);
515 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
521 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
529 * Write the security context string representation of
530 * the context structure `context' into a dynamically
531 * allocated string of the correct size. Set `*scontext'
532 * to point to this string and set `*scontext_len' to
533 * the length of the string.
535 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
542 /* Compute the size of the context. */
543 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
544 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
545 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
546 *scontext_len
+= mls_compute_context_len(context
);
548 /* Allocate space for the context; caller must free this space. */
549 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
553 *scontext
= scontextp
;
556 * Copy the user name, role name and type name into the context.
558 sprintf(scontextp
, "%s:%s:%s",
559 policydb
.p_user_val_to_name
[context
->user
- 1],
560 policydb
.p_role_val_to_name
[context
->role
- 1],
561 policydb
.p_type_val_to_name
[context
->type
- 1]);
562 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
563 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
564 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
566 mls_sid_to_context(context
, &scontextp
);
573 #include "initial_sid_to_string.h"
576 * security_sid_to_context - Obtain a context for a given SID.
577 * @sid: security identifier, SID
578 * @scontext: security context
579 * @scontext_len: length in bytes
581 * Write the string representation of the context associated with @sid
582 * into a dynamically allocated string of the correct size. Set @scontext
583 * to point to this string and set @scontext_len to the length of the string.
585 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
587 struct context
*context
;
590 if (!ss_initialized
) {
591 if (sid
<= SECINITSID_NUM
) {
594 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
595 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
596 strcpy(scontextp
, initial_sid_to_string
[sid
]);
597 *scontext
= scontextp
;
600 printk(KERN_ERR
"security_sid_to_context: called before initial "
601 "load_policy on unknown SID %d\n", sid
);
606 context
= sidtab_search(&sidtab
, sid
);
608 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
613 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
621 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
624 struct context context
;
625 struct role_datum
*role
;
626 struct type_datum
*typdatum
;
627 struct user_datum
*usrdatum
;
628 char *scontextp
, *p
, oldc
;
631 if (!ss_initialized
) {
634 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
635 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
640 *sid
= SECINITSID_KERNEL
;
645 /* Copy the string so that we can modify the copy as we parse it.
646 The string should already by null terminated, but we append a
647 null suffix to the copy to avoid problems with the existing
648 attr package, which doesn't view the null terminator as part
649 of the attribute value. */
650 scontext2
= kmalloc(scontext_len
+1,GFP_KERNEL
);
655 memcpy(scontext2
, scontext
, scontext_len
);
656 scontext2
[scontext_len
] = 0;
658 context_init(&context
);
663 /* Parse the security context. */
666 scontextp
= (char *) scontext2
;
668 /* Extract the user. */
670 while (*p
&& *p
!= ':')
678 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
682 context
.user
= usrdatum
->value
;
686 while (*p
&& *p
!= ':')
694 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
697 context
.role
= role
->value
;
701 while (*p
&& *p
!= ':')
706 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
710 context
.type
= typdatum
->value
;
712 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
716 if ((p
- scontext2
) < scontext_len
) {
721 /* Check the validity of the new context. */
722 if (!policydb_context_isvalid(&policydb
, &context
)) {
726 /* Obtain the new sid. */
727 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
730 context_destroy(&context
);
737 * security_context_to_sid - Obtain a SID for a given security context.
738 * @scontext: security context
739 * @scontext_len: length in bytes
740 * @sid: security identifier, SID
742 * Obtains a SID associated with the security context that
743 * has the string representation specified by @scontext.
744 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
745 * memory is available, or 0 on success.
747 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
749 return security_context_to_sid_core(scontext
, scontext_len
,
754 * security_context_to_sid_default - Obtain a SID for a given security context,
755 * falling back to specified default if needed.
757 * @scontext: security context
758 * @scontext_len: length in bytes
759 * @sid: security identifier, SID
760 * @def_sid: default SID to assign on errror
762 * Obtains a SID associated with the security context that
763 * has the string representation specified by @scontext.
764 * The default SID is passed to the MLS layer to be used to allow
765 * kernel labeling of the MLS field if the MLS field is not present
766 * (for upgrading to MLS without full relabel).
767 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
768 * memory is available, or 0 on success.
770 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
772 return security_context_to_sid_core(scontext
, scontext_len
,
776 static int compute_sid_handle_invalid_context(
777 struct context
*scontext
,
778 struct context
*tcontext
,
780 struct context
*newcontext
)
782 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
783 u32 slen
, tlen
, nlen
;
785 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
787 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
789 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
791 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
792 "security_compute_sid: invalid context %s"
796 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
801 if (!selinux_enforcing
)
806 static int security_compute_sid(u32 ssid
,
812 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
813 struct role_trans
*roletr
= NULL
;
814 struct avtab_key avkey
;
815 struct avtab_datum
*avdatum
;
816 struct avtab_node
*node
;
819 if (!ss_initialized
) {
821 case SECCLASS_PROCESS
:
833 scontext
= sidtab_search(&sidtab
, ssid
);
835 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
840 tcontext
= sidtab_search(&sidtab
, tsid
);
842 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
848 context_init(&newcontext
);
850 /* Set the user identity. */
852 case AVTAB_TRANSITION
:
854 /* Use the process user identity. */
855 newcontext
.user
= scontext
->user
;
858 /* Use the related object owner. */
859 newcontext
.user
= tcontext
->user
;
863 /* Set the role and type to default values. */
865 case SECCLASS_PROCESS
:
866 /* Use the current role and type of process. */
867 newcontext
.role
= scontext
->role
;
868 newcontext
.type
= scontext
->type
;
871 /* Use the well-defined object role. */
872 newcontext
.role
= OBJECT_R_VAL
;
873 /* Use the type of the related object. */
874 newcontext
.type
= tcontext
->type
;
877 /* Look for a type transition/member/change rule. */
878 avkey
.source_type
= scontext
->type
;
879 avkey
.target_type
= tcontext
->type
;
880 avkey
.target_class
= tclass
;
881 avkey
.specified
= specified
;
882 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
884 /* If no permanent rule, also check for enabled conditional rules */
886 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
887 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
888 if (node
->key
.specified
& AVTAB_ENABLED
) {
889 avdatum
= &node
->datum
;
896 /* Use the type from the type transition/member/change rule. */
897 newcontext
.type
= avdatum
->data
;
900 /* Check for class-specific changes. */
902 case SECCLASS_PROCESS
:
903 if (specified
& AVTAB_TRANSITION
) {
904 /* Look for a role transition rule. */
905 for (roletr
= policydb
.role_tr
; roletr
;
906 roletr
= roletr
->next
) {
907 if (roletr
->role
== scontext
->role
&&
908 roletr
->type
== tcontext
->type
) {
909 /* Use the role transition rule. */
910 newcontext
.role
= roletr
->new_role
;
920 /* Set the MLS attributes.
921 This is done last because it may allocate memory. */
922 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
926 /* Check the validity of the context. */
927 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
928 rc
= compute_sid_handle_invalid_context(scontext
,
935 /* Obtain the sid for the context. */
936 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
939 context_destroy(&newcontext
);
945 * security_transition_sid - Compute the SID for a new subject/object.
946 * @ssid: source security identifier
947 * @tsid: target security identifier
948 * @tclass: target security class
949 * @out_sid: security identifier for new subject/object
951 * Compute a SID to use for labeling a new subject or object in the
952 * class @tclass based on a SID pair (@ssid, @tsid).
953 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
954 * if insufficient memory is available, or %0 if the new SID was
955 * computed successfully.
957 int security_transition_sid(u32 ssid
,
962 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
966 * security_member_sid - Compute the SID for member selection.
967 * @ssid: source security identifier
968 * @tsid: target security identifier
969 * @tclass: target security class
970 * @out_sid: security identifier for selected member
972 * Compute a SID to use when selecting a member of a polyinstantiated
973 * object of class @tclass based on a SID pair (@ssid, @tsid).
974 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
975 * if insufficient memory is available, or %0 if the SID was
976 * computed successfully.
978 int security_member_sid(u32 ssid
,
983 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
987 * security_change_sid - Compute the SID for object relabeling.
988 * @ssid: source security identifier
989 * @tsid: target security identifier
990 * @tclass: target security class
991 * @out_sid: security identifier for selected member
993 * Compute a SID to use for relabeling an object of class @tclass
994 * based on a SID pair (@ssid, @tsid).
995 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
996 * if insufficient memory is available, or %0 if the SID was
997 * computed successfully.
999 int security_change_sid(u32 ssid
,
1004 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1008 * Verify that each permission that is defined under the
1009 * existing policy is still defined with the same value
1010 * in the new policy.
1012 static int validate_perm(void *key
, void *datum
, void *p
)
1015 struct perm_datum
*perdatum
, *perdatum2
;
1022 perdatum2
= hashtab_search(h
, key
);
1024 printk(KERN_ERR
"security: permission %s disappeared",
1029 if (perdatum
->value
!= perdatum2
->value
) {
1030 printk(KERN_ERR
"security: the value of permission %s changed",
1039 * Verify that each class that is defined under the
1040 * existing policy is still defined with the same
1041 * attributes in the new policy.
1043 static int validate_class(void *key
, void *datum
, void *p
)
1045 struct policydb
*newp
;
1046 struct class_datum
*cladatum
, *cladatum2
;
1052 cladatum2
= hashtab_search(newp
->p_classes
.table
, key
);
1054 printk(KERN_ERR
"security: class %s disappeared\n",
1059 if (cladatum
->value
!= cladatum2
->value
) {
1060 printk(KERN_ERR
"security: the value of class %s changed\n",
1065 if ((cladatum
->comdatum
&& !cladatum2
->comdatum
) ||
1066 (!cladatum
->comdatum
&& cladatum2
->comdatum
)) {
1067 printk(KERN_ERR
"security: the inherits clause for the access "
1068 "vector definition for class %s changed\n", (char *)key
);
1072 if (cladatum
->comdatum
) {
1073 rc
= hashtab_map(cladatum
->comdatum
->permissions
.table
, validate_perm
,
1074 cladatum2
->comdatum
->permissions
.table
);
1076 printk(" in the access vector definition for class "
1077 "%s\n", (char *)key
);
1081 rc
= hashtab_map(cladatum
->permissions
.table
, validate_perm
,
1082 cladatum2
->permissions
.table
);
1084 printk(" in access vector definition for class %s\n",
1090 /* Clone the SID into the new SID table. */
1091 static int clone_sid(u32 sid
,
1092 struct context
*context
,
1095 struct sidtab
*s
= arg
;
1097 return sidtab_insert(s
, sid
, context
);
1100 static inline int convert_context_handle_invalid_context(struct context
*context
)
1104 if (selinux_enforcing
) {
1110 context_struct_to_string(context
, &s
, &len
);
1111 printk(KERN_ERR
"security: context %s is invalid\n", s
);
1117 struct convert_context_args
{
1118 struct policydb
*oldp
;
1119 struct policydb
*newp
;
1123 * Convert the values in the security context
1124 * structure `c' from the values specified
1125 * in the policy `p->oldp' to the values specified
1126 * in the policy `p->newp'. Verify that the
1127 * context is valid under the new policy.
1129 static int convert_context(u32 key
,
1133 struct convert_context_args
*args
;
1134 struct context oldc
;
1135 struct role_datum
*role
;
1136 struct type_datum
*typdatum
;
1137 struct user_datum
*usrdatum
;
1144 rc
= context_cpy(&oldc
, c
);
1150 /* Convert the user. */
1151 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1152 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1156 c
->user
= usrdatum
->value
;
1158 /* Convert the role. */
1159 role
= hashtab_search(args
->newp
->p_roles
.table
,
1160 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1164 c
->role
= role
->value
;
1166 /* Convert the type. */
1167 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1168 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1172 c
->type
= typdatum
->value
;
1174 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1178 /* Check the validity of the new context. */
1179 if (!policydb_context_isvalid(args
->newp
, c
)) {
1180 rc
= convert_context_handle_invalid_context(&oldc
);
1185 context_destroy(&oldc
);
1189 context_struct_to_string(&oldc
, &s
, &len
);
1190 context_destroy(&oldc
);
1191 printk(KERN_ERR
"security: invalidating context %s\n", s
);
1196 extern void selinux_complete_init(void);
1199 * security_load_policy - Load a security policy configuration.
1200 * @data: binary policy data
1201 * @len: length of data in bytes
1203 * Load a new set of security policy configuration data,
1204 * validate it and convert the SID table as necessary.
1205 * This function will flush the access vector cache after
1206 * loading the new policy.
1208 int security_load_policy(void *data
, size_t len
)
1210 struct policydb oldpolicydb
, newpolicydb
;
1211 struct sidtab oldsidtab
, newsidtab
;
1212 struct convert_context_args args
;
1215 struct policy_file file
= { data
, len
}, *fp
= &file
;
1219 if (!ss_initialized
) {
1221 if (policydb_read(&policydb
, fp
)) {
1223 avtab_cache_destroy();
1226 if (policydb_load_isids(&policydb
, &sidtab
)) {
1228 policydb_destroy(&policydb
);
1229 avtab_cache_destroy();
1232 policydb_loaded_version
= policydb
.policyvers
;
1234 seqno
= ++latest_granting
;
1236 selinux_complete_init();
1237 avc_ss_reset(seqno
);
1238 selnl_notify_policyload(seqno
);
1243 sidtab_hash_eval(&sidtab
, "sids");
1246 if (policydb_read(&newpolicydb
, fp
)) {
1251 sidtab_init(&newsidtab
);
1253 /* Verify that the existing classes did not change. */
1254 if (hashtab_map(policydb
.p_classes
.table
, validate_class
, &newpolicydb
)) {
1255 printk(KERN_ERR
"security: the definition of an existing "
1261 /* Clone the SID table. */
1262 sidtab_shutdown(&sidtab
);
1263 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1268 /* Convert the internal representations of contexts
1269 in the new SID table and remove invalid SIDs. */
1270 args
.oldp
= &policydb
;
1271 args
.newp
= &newpolicydb
;
1272 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1274 /* Save the old policydb and SID table to free later. */
1275 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1276 sidtab_set(&oldsidtab
, &sidtab
);
1278 /* Install the new policydb and SID table. */
1280 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1281 sidtab_set(&sidtab
, &newsidtab
);
1282 seqno
= ++latest_granting
;
1283 policydb_loaded_version
= policydb
.policyvers
;
1287 /* Free the old policydb and SID table. */
1288 policydb_destroy(&oldpolicydb
);
1289 sidtab_destroy(&oldsidtab
);
1291 avc_ss_reset(seqno
);
1292 selnl_notify_policyload(seqno
);
1298 sidtab_destroy(&newsidtab
);
1299 policydb_destroy(&newpolicydb
);
1305 * security_port_sid - Obtain the SID for a port.
1306 * @domain: communication domain aka address family
1307 * @type: socket type
1308 * @protocol: protocol number
1309 * @port: port number
1310 * @out_sid: security identifier
1312 int security_port_sid(u16 domain
,
1323 c
= policydb
.ocontexts
[OCON_PORT
];
1325 if (c
->u
.port
.protocol
== protocol
&&
1326 c
->u
.port
.low_port
<= port
&&
1327 c
->u
.port
.high_port
>= port
)
1334 rc
= sidtab_context_to_sid(&sidtab
,
1340 *out_sid
= c
->sid
[0];
1342 *out_sid
= SECINITSID_PORT
;
1351 * security_netif_sid - Obtain the SID for a network interface.
1352 * @name: interface name
1353 * @if_sid: interface SID
1354 * @msg_sid: default SID for received packets
1356 int security_netif_sid(char *name
,
1365 c
= policydb
.ocontexts
[OCON_NETIF
];
1367 if (strcmp(name
, c
->u
.name
) == 0)
1373 if (!c
->sid
[0] || !c
->sid
[1]) {
1374 rc
= sidtab_context_to_sid(&sidtab
,
1379 rc
= sidtab_context_to_sid(&sidtab
,
1385 *if_sid
= c
->sid
[0];
1386 *msg_sid
= c
->sid
[1];
1388 *if_sid
= SECINITSID_NETIF
;
1389 *msg_sid
= SECINITSID_NETMSG
;
1397 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1401 for(i
= 0; i
< 4; i
++)
1402 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1411 * security_node_sid - Obtain the SID for a node (host).
1412 * @domain: communication domain aka address family
1414 * @addrlen: address length in bytes
1415 * @out_sid: security identifier
1417 int security_node_sid(u16 domain
,
1431 if (addrlen
!= sizeof(u32
)) {
1436 addr
= *((u32
*)addrp
);
1438 c
= policydb
.ocontexts
[OCON_NODE
];
1440 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1448 if (addrlen
!= sizeof(u64
) * 2) {
1452 c
= policydb
.ocontexts
[OCON_NODE6
];
1454 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1462 *out_sid
= SECINITSID_NODE
;
1468 rc
= sidtab_context_to_sid(&sidtab
,
1474 *out_sid
= c
->sid
[0];
1476 *out_sid
= SECINITSID_NODE
;
1487 * security_get_user_sids - Obtain reachable SIDs for a user.
1488 * @fromsid: starting SID
1489 * @username: username
1490 * @sids: array of reachable SIDs for user
1491 * @nel: number of elements in @sids
1493 * Generate the set of SIDs for legal security contexts
1494 * for a given user that can be reached by @fromsid.
1495 * Set *@sids to point to a dynamically allocated
1496 * array containing the set of SIDs. Set *@nel to the
1497 * number of elements in the array.
1500 int security_get_user_sids(u32 fromsid
,
1505 struct context
*fromcon
, usercon
;
1506 u32
*mysids
, *mysids2
, sid
;
1507 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1508 struct user_datum
*user
;
1509 struct role_datum
*role
;
1510 struct av_decision avd
;
1511 struct ebitmap_node
*rnode
, *tnode
;
1514 if (!ss_initialized
) {
1522 fromcon
= sidtab_search(&sidtab
, fromsid
);
1528 user
= hashtab_search(policydb
.p_users
.table
, username
);
1533 usercon
.user
= user
->value
;
1535 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1541 ebitmap_for_each_bit(&user
->roles
, rnode
, i
) {
1542 if (!ebitmap_node_get_bit(rnode
, i
))
1544 role
= policydb
.role_val_to_struct
[i
];
1546 ebitmap_for_each_bit(&role
->types
, tnode
, j
) {
1547 if (!ebitmap_node_get_bit(tnode
, j
))
1551 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1554 rc
= context_struct_compute_av(fromcon
, &usercon
,
1556 PROCESS__TRANSITION
,
1558 if (rc
|| !(avd
.allowed
& PROCESS__TRANSITION
))
1560 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1565 if (mynel
< maxnel
) {
1566 mysids
[mynel
++] = sid
;
1569 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1575 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1578 mysids
[mynel
++] = sid
;
1593 * security_genfs_sid - Obtain a SID for a file in a filesystem
1594 * @fstype: filesystem type
1595 * @path: path from root of mount
1596 * @sclass: file security class
1597 * @sid: SID for path
1599 * Obtain a SID to use for a file in a filesystem that
1600 * cannot support xattr or use a fixed labeling behavior like
1601 * transition SIDs or task SIDs.
1603 int security_genfs_sid(const char *fstype
,
1609 struct genfs
*genfs
;
1611 int rc
= 0, cmp
= 0;
1615 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1616 cmp
= strcmp(fstype
, genfs
->fstype
);
1621 if (!genfs
|| cmp
) {
1622 *sid
= SECINITSID_UNLABELED
;
1627 for (c
= genfs
->head
; c
; c
= c
->next
) {
1628 len
= strlen(c
->u
.name
);
1629 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1630 (strncmp(c
->u
.name
, path
, len
) == 0))
1635 *sid
= SECINITSID_UNLABELED
;
1641 rc
= sidtab_context_to_sid(&sidtab
,
1655 * security_fs_use - Determine how to handle labeling for a filesystem.
1656 * @fstype: filesystem type
1657 * @behavior: labeling behavior
1658 * @sid: SID for filesystem (superblock)
1660 int security_fs_use(
1662 unsigned int *behavior
,
1670 c
= policydb
.ocontexts
[OCON_FSUSE
];
1672 if (strcmp(fstype
, c
->u
.name
) == 0)
1678 *behavior
= c
->v
.behavior
;
1680 rc
= sidtab_context_to_sid(&sidtab
,
1688 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1690 *behavior
= SECURITY_FS_USE_NONE
;
1693 *behavior
= SECURITY_FS_USE_GENFS
;
1702 int security_get_bools(int *len
, char ***names
, int **values
)
1704 int i
, rc
= -ENOMEM
;
1710 *len
= policydb
.p_bools
.nprim
;
1716 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1720 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1724 for (i
= 0; i
< *len
; i
++) {
1726 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1727 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1728 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1731 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1732 (*names
)[i
][name_len
- 1] = 0;
1740 for (i
= 0; i
< *len
; i
++)
1748 int security_set_bools(int len
, int *values
)
1751 int lenp
, seqno
= 0;
1752 struct cond_node
*cur
;
1756 lenp
= policydb
.p_bools
.nprim
;
1762 printk(KERN_INFO
"security: committed booleans { ");
1763 for (i
= 0; i
< len
; i
++) {
1765 policydb
.bool_val_to_struct
[i
]->state
= 1;
1767 policydb
.bool_val_to_struct
[i
]->state
= 0;
1771 printk("%s:%d", policydb
.p_bool_val_to_name
[i
],
1772 policydb
.bool_val_to_struct
[i
]->state
);
1776 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1777 rc
= evaluate_cond_node(&policydb
, cur
);
1782 seqno
= ++latest_granting
;
1787 avc_ss_reset(seqno
);
1788 selnl_notify_policyload(seqno
);
1793 int security_get_bool_value(int bool)
1800 len
= policydb
.p_bools
.nprim
;
1806 rc
= policydb
.bool_val_to_struct
[bool]->state
;