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.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, version 2.
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/spinlock.h>
27 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/audit.h>
31 #include <linux/mutex.h>
41 #include "conditional.h"
44 extern void selnl_notify_policyload(u32 seqno
);
45 unsigned int policydb_loaded_version
;
47 static DEFINE_RWLOCK(policy_rwlock
);
48 #define POLICY_RDLOCK read_lock(&policy_rwlock)
49 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
50 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
51 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
53 static DEFINE_MUTEX(load_mutex
);
54 #define LOAD_LOCK mutex_lock(&load_mutex)
55 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
57 static struct sidtab sidtab
;
58 struct policydb policydb
;
59 int ss_initialized
= 0;
62 * The largest sequence number that has been used when
63 * providing an access decision to the access vector cache.
64 * The sequence number only changes when a policy change
67 static u32 latest_granting
= 0;
69 /* Forward declaration. */
70 static int context_struct_to_string(struct context
*context
, char **scontext
,
74 * Return the boolean value of a constraint expression
75 * when it is applied to the specified source and target
78 * xcontext is a special beast... It is used by the validatetrans rules
79 * only. For these rules, scontext is the context before the transition,
80 * tcontext is the context after the transition, and xcontext is the context
81 * of the process performing the transition. All other callers of
82 * constraint_expr_eval should pass in NULL for xcontext.
84 static int constraint_expr_eval(struct context
*scontext
,
85 struct context
*tcontext
,
86 struct context
*xcontext
,
87 struct constraint_expr
*cexpr
)
91 struct role_datum
*r1
, *r2
;
92 struct mls_level
*l1
, *l2
;
93 struct constraint_expr
*e
;
94 int s
[CEXPR_MAXDEPTH
];
97 for (e
= cexpr
; e
; e
= e
->next
) {
98 switch (e
->expr_type
) {
114 if (sp
== (CEXPR_MAXDEPTH
-1))
118 val1
= scontext
->user
;
119 val2
= tcontext
->user
;
122 val1
= scontext
->type
;
123 val2
= tcontext
->type
;
126 val1
= scontext
->role
;
127 val2
= tcontext
->role
;
128 r1
= policydb
.role_val_to_struct
[val1
- 1];
129 r2
= policydb
.role_val_to_struct
[val2
- 1];
132 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
136 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
140 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
142 !ebitmap_get_bit(&r2
->dominates
,
150 l1
= &(scontext
->range
.level
[0]);
151 l2
= &(tcontext
->range
.level
[0]);
154 l1
= &(scontext
->range
.level
[0]);
155 l2
= &(tcontext
->range
.level
[1]);
158 l1
= &(scontext
->range
.level
[1]);
159 l2
= &(tcontext
->range
.level
[0]);
162 l1
= &(scontext
->range
.level
[1]);
163 l2
= &(tcontext
->range
.level
[1]);
166 l1
= &(scontext
->range
.level
[0]);
167 l2
= &(scontext
->range
.level
[1]);
170 l1
= &(tcontext
->range
.level
[0]);
171 l2
= &(tcontext
->range
.level
[1]);
176 s
[++sp
] = mls_level_eq(l1
, l2
);
179 s
[++sp
] = !mls_level_eq(l1
, l2
);
182 s
[++sp
] = mls_level_dom(l1
, l2
);
185 s
[++sp
] = mls_level_dom(l2
, l1
);
188 s
[++sp
] = mls_level_incomp(l2
, l1
);
202 s
[++sp
] = (val1
== val2
);
205 s
[++sp
] = (val1
!= val2
);
213 if (sp
== (CEXPR_MAXDEPTH
-1))
216 if (e
->attr
& CEXPR_TARGET
)
218 else if (e
->attr
& CEXPR_XTARGET
) {
225 if (e
->attr
& CEXPR_USER
)
227 else if (e
->attr
& CEXPR_ROLE
)
229 else if (e
->attr
& CEXPR_TYPE
)
238 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
241 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
259 * Compute access vectors based on a context structure pair for
260 * the permissions in a particular class.
262 static int context_struct_compute_av(struct context
*scontext
,
263 struct context
*tcontext
,
266 struct av_decision
*avd
)
268 struct constraint_node
*constraint
;
269 struct role_allow
*ra
;
270 struct avtab_key avkey
;
271 struct avtab_node
*node
;
272 struct class_datum
*tclass_datum
;
273 struct ebitmap
*sattr
, *tattr
;
274 struct ebitmap_node
*snode
, *tnode
;
278 * Remap extended Netlink classes for old policy versions.
279 * Do this here rather than socket_type_to_security_class()
280 * in case a newer policy version is loaded, allowing sockets
281 * to remain in the correct class.
283 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
284 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
285 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
286 tclass
= SECCLASS_NETLINK_SOCKET
;
288 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
289 printk(KERN_ERR
"security_compute_av: unrecognized class %d\n",
293 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
296 * Initialize the access vectors to the default values.
299 avd
->decided
= 0xffffffff;
301 avd
->auditdeny
= 0xffffffff;
302 avd
->seqno
= latest_granting
;
305 * If a specific type enforcement rule was defined for
306 * this permission check, then use it.
308 avkey
.target_class
= tclass
;
309 avkey
.specified
= AVTAB_AV
;
310 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
311 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
312 ebitmap_for_each_bit(sattr
, snode
, i
) {
313 if (!ebitmap_node_get_bit(snode
, i
))
315 ebitmap_for_each_bit(tattr
, tnode
, j
) {
316 if (!ebitmap_node_get_bit(tnode
, j
))
318 avkey
.source_type
= i
+ 1;
319 avkey
.target_type
= j
+ 1;
320 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
322 node
= avtab_search_node_next(node
, avkey
.specified
)) {
323 if (node
->key
.specified
== AVTAB_ALLOWED
)
324 avd
->allowed
|= node
->datum
.data
;
325 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
326 avd
->auditallow
|= node
->datum
.data
;
327 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
328 avd
->auditdeny
&= node
->datum
.data
;
331 /* Check conditional av table for additional permissions */
332 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
338 * Remove any permissions prohibited by a constraint (this includes
341 constraint
= tclass_datum
->constraints
;
343 if ((constraint
->permissions
& (avd
->allowed
)) &&
344 !constraint_expr_eval(scontext
, tcontext
, NULL
,
346 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
348 constraint
= constraint
->next
;
352 * If checking process transition permission and the
353 * role is changing, then check the (current_role, new_role)
356 if (tclass
== SECCLASS_PROCESS
&&
357 (avd
->allowed
& (PROCESS__TRANSITION
| PROCESS__DYNTRANSITION
)) &&
358 scontext
->role
!= tcontext
->role
) {
359 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
360 if (scontext
->role
== ra
->role
&&
361 tcontext
->role
== ra
->new_role
)
365 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
|
366 PROCESS__DYNTRANSITION
);
372 static int security_validtrans_handle_fail(struct context
*ocontext
,
373 struct context
*ncontext
,
374 struct context
*tcontext
,
377 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
378 u32 olen
, nlen
, tlen
;
380 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
382 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
384 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
386 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
387 "security_validate_transition: denied for"
388 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
389 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
395 if (!selinux_enforcing
)
400 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
403 struct context
*ocontext
;
404 struct context
*ncontext
;
405 struct context
*tcontext
;
406 struct class_datum
*tclass_datum
;
407 struct constraint_node
*constraint
;
416 * Remap extended Netlink classes for old policy versions.
417 * Do this here rather than socket_type_to_security_class()
418 * in case a newer policy version is loaded, allowing sockets
419 * to remain in the correct class.
421 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
422 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
423 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
424 tclass
= SECCLASS_NETLINK_SOCKET
;
426 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
427 printk(KERN_ERR
"security_validate_transition: "
428 "unrecognized class %d\n", tclass
);
432 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
434 ocontext
= sidtab_search(&sidtab
, oldsid
);
436 printk(KERN_ERR
"security_validate_transition: "
437 " unrecognized SID %d\n", oldsid
);
442 ncontext
= sidtab_search(&sidtab
, newsid
);
444 printk(KERN_ERR
"security_validate_transition: "
445 " unrecognized SID %d\n", newsid
);
450 tcontext
= sidtab_search(&sidtab
, tasksid
);
452 printk(KERN_ERR
"security_validate_transition: "
453 " unrecognized SID %d\n", tasksid
);
458 constraint
= tclass_datum
->validatetrans
;
460 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
462 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
466 constraint
= constraint
->next
;
475 * security_compute_av - Compute access vector decisions.
476 * @ssid: source security identifier
477 * @tsid: target security identifier
478 * @tclass: target security class
479 * @requested: requested permissions
480 * @avd: access vector decisions
482 * Compute a set of access vector decisions based on the
483 * SID pair (@ssid, @tsid) for the permissions in @tclass.
484 * Return -%EINVAL if any of the parameters are invalid or %0
485 * if the access vector decisions were computed successfully.
487 int security_compute_av(u32 ssid
,
491 struct av_decision
*avd
)
493 struct context
*scontext
= NULL
, *tcontext
= NULL
;
496 if (!ss_initialized
) {
497 avd
->allowed
= 0xffffffff;
498 avd
->decided
= 0xffffffff;
500 avd
->auditdeny
= 0xffffffff;
501 avd
->seqno
= latest_granting
;
507 scontext
= sidtab_search(&sidtab
, ssid
);
509 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
514 tcontext
= sidtab_search(&sidtab
, tsid
);
516 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
522 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
530 * Write the security context string representation of
531 * the context structure `context' into a dynamically
532 * allocated string of the correct size. Set `*scontext'
533 * to point to this string and set `*scontext_len' to
534 * the length of the string.
536 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
543 /* Compute the size of the context. */
544 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
545 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
546 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
547 *scontext_len
+= mls_compute_context_len(context
);
549 /* Allocate space for the context; caller must free this space. */
550 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
554 *scontext
= scontextp
;
557 * Copy the user name, role name and type name into the context.
559 sprintf(scontextp
, "%s:%s:%s",
560 policydb
.p_user_val_to_name
[context
->user
- 1],
561 policydb
.p_role_val_to_name
[context
->role
- 1],
562 policydb
.p_type_val_to_name
[context
->type
- 1]);
563 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
564 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
565 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
567 mls_sid_to_context(context
, &scontextp
);
574 #include "initial_sid_to_string.h"
577 * security_sid_to_context - Obtain a context for a given SID.
578 * @sid: security identifier, SID
579 * @scontext: security context
580 * @scontext_len: length in bytes
582 * Write the string representation of the context associated with @sid
583 * into a dynamically allocated string of the correct size. Set @scontext
584 * to point to this string and set @scontext_len to the length of the string.
586 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
588 struct context
*context
;
591 if (!ss_initialized
) {
592 if (sid
<= SECINITSID_NUM
) {
595 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
596 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
601 strcpy(scontextp
, initial_sid_to_string
[sid
]);
602 *scontext
= scontextp
;
605 printk(KERN_ERR
"security_sid_to_context: called before initial "
606 "load_policy on unknown SID %d\n", sid
);
611 context
= sidtab_search(&sidtab
, sid
);
613 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
618 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
626 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
629 struct context context
;
630 struct role_datum
*role
;
631 struct type_datum
*typdatum
;
632 struct user_datum
*usrdatum
;
633 char *scontextp
, *p
, oldc
;
636 if (!ss_initialized
) {
639 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
640 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
645 *sid
= SECINITSID_KERNEL
;
650 /* Copy the string so that we can modify the copy as we parse it.
651 The string should already by null terminated, but we append a
652 null suffix to the copy to avoid problems with the existing
653 attr package, which doesn't view the null terminator as part
654 of the attribute value. */
655 scontext2
= kmalloc(scontext_len
+1,GFP_KERNEL
);
660 memcpy(scontext2
, scontext
, scontext_len
);
661 scontext2
[scontext_len
] = 0;
663 context_init(&context
);
668 /* Parse the security context. */
671 scontextp
= (char *) scontext2
;
673 /* Extract the user. */
675 while (*p
&& *p
!= ':')
683 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
687 context
.user
= usrdatum
->value
;
691 while (*p
&& *p
!= ':')
699 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
702 context
.role
= role
->value
;
706 while (*p
&& *p
!= ':')
711 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
715 context
.type
= typdatum
->value
;
717 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
721 if ((p
- scontext2
) < scontext_len
) {
726 /* Check the validity of the new context. */
727 if (!policydb_context_isvalid(&policydb
, &context
)) {
731 /* Obtain the new sid. */
732 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
735 context_destroy(&context
);
742 * security_context_to_sid - Obtain a SID for a given security context.
743 * @scontext: security context
744 * @scontext_len: length in bytes
745 * @sid: security identifier, SID
747 * Obtains a SID associated with the security context that
748 * has the string representation specified by @scontext.
749 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
750 * memory is available, or 0 on success.
752 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
754 return security_context_to_sid_core(scontext
, scontext_len
,
759 * security_context_to_sid_default - Obtain a SID for a given security context,
760 * falling back to specified default if needed.
762 * @scontext: security context
763 * @scontext_len: length in bytes
764 * @sid: security identifier, SID
765 * @def_sid: default SID to assign on errror
767 * Obtains a SID associated with the security context that
768 * has the string representation specified by @scontext.
769 * The default SID is passed to the MLS layer to be used to allow
770 * kernel labeling of the MLS field if the MLS field is not present
771 * (for upgrading to MLS without full relabel).
772 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
773 * memory is available, or 0 on success.
775 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
777 return security_context_to_sid_core(scontext
, scontext_len
,
781 static int compute_sid_handle_invalid_context(
782 struct context
*scontext
,
783 struct context
*tcontext
,
785 struct context
*newcontext
)
787 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
788 u32 slen
, tlen
, nlen
;
790 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
792 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
794 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
796 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
797 "security_compute_sid: invalid context %s"
801 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
806 if (!selinux_enforcing
)
811 static int security_compute_sid(u32 ssid
,
817 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
818 struct role_trans
*roletr
= NULL
;
819 struct avtab_key avkey
;
820 struct avtab_datum
*avdatum
;
821 struct avtab_node
*node
;
824 if (!ss_initialized
) {
826 case SECCLASS_PROCESS
:
838 scontext
= sidtab_search(&sidtab
, ssid
);
840 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
845 tcontext
= sidtab_search(&sidtab
, tsid
);
847 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
853 context_init(&newcontext
);
855 /* Set the user identity. */
857 case AVTAB_TRANSITION
:
859 /* Use the process user identity. */
860 newcontext
.user
= scontext
->user
;
863 /* Use the related object owner. */
864 newcontext
.user
= tcontext
->user
;
868 /* Set the role and type to default values. */
870 case SECCLASS_PROCESS
:
871 /* Use the current role and type of process. */
872 newcontext
.role
= scontext
->role
;
873 newcontext
.type
= scontext
->type
;
876 /* Use the well-defined object role. */
877 newcontext
.role
= OBJECT_R_VAL
;
878 /* Use the type of the related object. */
879 newcontext
.type
= tcontext
->type
;
882 /* Look for a type transition/member/change rule. */
883 avkey
.source_type
= scontext
->type
;
884 avkey
.target_type
= tcontext
->type
;
885 avkey
.target_class
= tclass
;
886 avkey
.specified
= specified
;
887 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
889 /* If no permanent rule, also check for enabled conditional rules */
891 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
892 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
893 if (node
->key
.specified
& AVTAB_ENABLED
) {
894 avdatum
= &node
->datum
;
901 /* Use the type from the type transition/member/change rule. */
902 newcontext
.type
= avdatum
->data
;
905 /* Check for class-specific changes. */
907 case SECCLASS_PROCESS
:
908 if (specified
& AVTAB_TRANSITION
) {
909 /* Look for a role transition rule. */
910 for (roletr
= policydb
.role_tr
; roletr
;
911 roletr
= roletr
->next
) {
912 if (roletr
->role
== scontext
->role
&&
913 roletr
->type
== tcontext
->type
) {
914 /* Use the role transition rule. */
915 newcontext
.role
= roletr
->new_role
;
925 /* Set the MLS attributes.
926 This is done last because it may allocate memory. */
927 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
931 /* Check the validity of the context. */
932 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
933 rc
= compute_sid_handle_invalid_context(scontext
,
940 /* Obtain the sid for the context. */
941 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
944 context_destroy(&newcontext
);
950 * security_transition_sid - Compute the SID for a new subject/object.
951 * @ssid: source security identifier
952 * @tsid: target security identifier
953 * @tclass: target security class
954 * @out_sid: security identifier for new subject/object
956 * Compute a SID to use for labeling a new subject or object in the
957 * class @tclass based on a SID pair (@ssid, @tsid).
958 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
959 * if insufficient memory is available, or %0 if the new SID was
960 * computed successfully.
962 int security_transition_sid(u32 ssid
,
967 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
971 * security_member_sid - Compute the SID for member selection.
972 * @ssid: source security identifier
973 * @tsid: target security identifier
974 * @tclass: target security class
975 * @out_sid: security identifier for selected member
977 * Compute a SID to use when selecting a member of a polyinstantiated
978 * object of class @tclass based on a SID pair (@ssid, @tsid).
979 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
980 * if insufficient memory is available, or %0 if the SID was
981 * computed successfully.
983 int security_member_sid(u32 ssid
,
988 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
992 * security_change_sid - Compute the SID for object relabeling.
993 * @ssid: source security identifier
994 * @tsid: target security identifier
995 * @tclass: target security class
996 * @out_sid: security identifier for selected member
998 * Compute a SID to use for relabeling an object of class @tclass
999 * based on a SID pair (@ssid, @tsid).
1000 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1001 * if insufficient memory is available, or %0 if the SID was
1002 * computed successfully.
1004 int security_change_sid(u32 ssid
,
1009 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1013 * Verify that each permission that is defined under the
1014 * existing policy is still defined with the same value
1015 * in the new policy.
1017 static int validate_perm(void *key
, void *datum
, void *p
)
1020 struct perm_datum
*perdatum
, *perdatum2
;
1027 perdatum2
= hashtab_search(h
, key
);
1029 printk(KERN_ERR
"security: permission %s disappeared",
1034 if (perdatum
->value
!= perdatum2
->value
) {
1035 printk(KERN_ERR
"security: the value of permission %s changed",
1044 * Verify that each class that is defined under the
1045 * existing policy is still defined with the same
1046 * attributes in the new policy.
1048 static int validate_class(void *key
, void *datum
, void *p
)
1050 struct policydb
*newp
;
1051 struct class_datum
*cladatum
, *cladatum2
;
1057 cladatum2
= hashtab_search(newp
->p_classes
.table
, key
);
1059 printk(KERN_ERR
"security: class %s disappeared\n",
1064 if (cladatum
->value
!= cladatum2
->value
) {
1065 printk(KERN_ERR
"security: the value of class %s changed\n",
1070 if ((cladatum
->comdatum
&& !cladatum2
->comdatum
) ||
1071 (!cladatum
->comdatum
&& cladatum2
->comdatum
)) {
1072 printk(KERN_ERR
"security: the inherits clause for the access "
1073 "vector definition for class %s changed\n", (char *)key
);
1077 if (cladatum
->comdatum
) {
1078 rc
= hashtab_map(cladatum
->comdatum
->permissions
.table
, validate_perm
,
1079 cladatum2
->comdatum
->permissions
.table
);
1081 printk(" in the access vector definition for class "
1082 "%s\n", (char *)key
);
1086 rc
= hashtab_map(cladatum
->permissions
.table
, validate_perm
,
1087 cladatum2
->permissions
.table
);
1089 printk(" in access vector definition for class %s\n",
1095 /* Clone the SID into the new SID table. */
1096 static int clone_sid(u32 sid
,
1097 struct context
*context
,
1100 struct sidtab
*s
= arg
;
1102 return sidtab_insert(s
, sid
, context
);
1105 static inline int convert_context_handle_invalid_context(struct context
*context
)
1109 if (selinux_enforcing
) {
1115 context_struct_to_string(context
, &s
, &len
);
1116 printk(KERN_ERR
"security: context %s is invalid\n", s
);
1122 struct convert_context_args
{
1123 struct policydb
*oldp
;
1124 struct policydb
*newp
;
1128 * Convert the values in the security context
1129 * structure `c' from the values specified
1130 * in the policy `p->oldp' to the values specified
1131 * in the policy `p->newp'. Verify that the
1132 * context is valid under the new policy.
1134 static int convert_context(u32 key
,
1138 struct convert_context_args
*args
;
1139 struct context oldc
;
1140 struct role_datum
*role
;
1141 struct type_datum
*typdatum
;
1142 struct user_datum
*usrdatum
;
1149 rc
= context_cpy(&oldc
, c
);
1155 /* Convert the user. */
1156 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1157 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1161 c
->user
= usrdatum
->value
;
1163 /* Convert the role. */
1164 role
= hashtab_search(args
->newp
->p_roles
.table
,
1165 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1169 c
->role
= role
->value
;
1171 /* Convert the type. */
1172 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1173 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1177 c
->type
= typdatum
->value
;
1179 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1183 /* Check the validity of the new context. */
1184 if (!policydb_context_isvalid(args
->newp
, c
)) {
1185 rc
= convert_context_handle_invalid_context(&oldc
);
1190 context_destroy(&oldc
);
1194 context_struct_to_string(&oldc
, &s
, &len
);
1195 context_destroy(&oldc
);
1196 printk(KERN_ERR
"security: invalidating context %s\n", s
);
1201 extern void selinux_complete_init(void);
1204 * security_load_policy - Load a security policy configuration.
1205 * @data: binary policy data
1206 * @len: length of data in bytes
1208 * Load a new set of security policy configuration data,
1209 * validate it and convert the SID table as necessary.
1210 * This function will flush the access vector cache after
1211 * loading the new policy.
1213 int security_load_policy(void *data
, size_t len
)
1215 struct policydb oldpolicydb
, newpolicydb
;
1216 struct sidtab oldsidtab
, newsidtab
;
1217 struct convert_context_args args
;
1220 struct policy_file file
= { data
, len
}, *fp
= &file
;
1224 if (!ss_initialized
) {
1226 if (policydb_read(&policydb
, fp
)) {
1228 avtab_cache_destroy();
1231 if (policydb_load_isids(&policydb
, &sidtab
)) {
1233 policydb_destroy(&policydb
);
1234 avtab_cache_destroy();
1237 policydb_loaded_version
= policydb
.policyvers
;
1239 seqno
= ++latest_granting
;
1241 selinux_complete_init();
1242 avc_ss_reset(seqno
);
1243 selnl_notify_policyload(seqno
);
1248 sidtab_hash_eval(&sidtab
, "sids");
1251 if (policydb_read(&newpolicydb
, fp
)) {
1256 sidtab_init(&newsidtab
);
1258 /* Verify that the existing classes did not change. */
1259 if (hashtab_map(policydb
.p_classes
.table
, validate_class
, &newpolicydb
)) {
1260 printk(KERN_ERR
"security: the definition of an existing "
1266 /* Clone the SID table. */
1267 sidtab_shutdown(&sidtab
);
1268 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1273 /* Convert the internal representations of contexts
1274 in the new SID table and remove invalid SIDs. */
1275 args
.oldp
= &policydb
;
1276 args
.newp
= &newpolicydb
;
1277 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1279 /* Save the old policydb and SID table to free later. */
1280 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1281 sidtab_set(&oldsidtab
, &sidtab
);
1283 /* Install the new policydb and SID table. */
1285 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1286 sidtab_set(&sidtab
, &newsidtab
);
1287 seqno
= ++latest_granting
;
1288 policydb_loaded_version
= policydb
.policyvers
;
1292 /* Free the old policydb and SID table. */
1293 policydb_destroy(&oldpolicydb
);
1294 sidtab_destroy(&oldsidtab
);
1296 avc_ss_reset(seqno
);
1297 selnl_notify_policyload(seqno
);
1303 sidtab_destroy(&newsidtab
);
1304 policydb_destroy(&newpolicydb
);
1310 * security_port_sid - Obtain the SID for a port.
1311 * @domain: communication domain aka address family
1312 * @type: socket type
1313 * @protocol: protocol number
1314 * @port: port number
1315 * @out_sid: security identifier
1317 int security_port_sid(u16 domain
,
1328 c
= policydb
.ocontexts
[OCON_PORT
];
1330 if (c
->u
.port
.protocol
== protocol
&&
1331 c
->u
.port
.low_port
<= port
&&
1332 c
->u
.port
.high_port
>= port
)
1339 rc
= sidtab_context_to_sid(&sidtab
,
1345 *out_sid
= c
->sid
[0];
1347 *out_sid
= SECINITSID_PORT
;
1356 * security_netif_sid - Obtain the SID for a network interface.
1357 * @name: interface name
1358 * @if_sid: interface SID
1359 * @msg_sid: default SID for received packets
1361 int security_netif_sid(char *name
,
1370 c
= policydb
.ocontexts
[OCON_NETIF
];
1372 if (strcmp(name
, c
->u
.name
) == 0)
1378 if (!c
->sid
[0] || !c
->sid
[1]) {
1379 rc
= sidtab_context_to_sid(&sidtab
,
1384 rc
= sidtab_context_to_sid(&sidtab
,
1390 *if_sid
= c
->sid
[0];
1391 *msg_sid
= c
->sid
[1];
1393 *if_sid
= SECINITSID_NETIF
;
1394 *msg_sid
= SECINITSID_NETMSG
;
1402 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1406 for(i
= 0; i
< 4; i
++)
1407 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1416 * security_node_sid - Obtain the SID for a node (host).
1417 * @domain: communication domain aka address family
1419 * @addrlen: address length in bytes
1420 * @out_sid: security identifier
1422 int security_node_sid(u16 domain
,
1436 if (addrlen
!= sizeof(u32
)) {
1441 addr
= *((u32
*)addrp
);
1443 c
= policydb
.ocontexts
[OCON_NODE
];
1445 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1453 if (addrlen
!= sizeof(u64
) * 2) {
1457 c
= policydb
.ocontexts
[OCON_NODE6
];
1459 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1467 *out_sid
= SECINITSID_NODE
;
1473 rc
= sidtab_context_to_sid(&sidtab
,
1479 *out_sid
= c
->sid
[0];
1481 *out_sid
= SECINITSID_NODE
;
1492 * security_get_user_sids - Obtain reachable SIDs for a user.
1493 * @fromsid: starting SID
1494 * @username: username
1495 * @sids: array of reachable SIDs for user
1496 * @nel: number of elements in @sids
1498 * Generate the set of SIDs for legal security contexts
1499 * for a given user that can be reached by @fromsid.
1500 * Set *@sids to point to a dynamically allocated
1501 * array containing the set of SIDs. Set *@nel to the
1502 * number of elements in the array.
1505 int security_get_user_sids(u32 fromsid
,
1510 struct context
*fromcon
, usercon
;
1511 u32
*mysids
, *mysids2
, sid
;
1512 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1513 struct user_datum
*user
;
1514 struct role_datum
*role
;
1515 struct av_decision avd
;
1516 struct ebitmap_node
*rnode
, *tnode
;
1519 if (!ss_initialized
) {
1527 fromcon
= sidtab_search(&sidtab
, fromsid
);
1533 user
= hashtab_search(policydb
.p_users
.table
, username
);
1538 usercon
.user
= user
->value
;
1540 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1546 ebitmap_for_each_bit(&user
->roles
, rnode
, i
) {
1547 if (!ebitmap_node_get_bit(rnode
, i
))
1549 role
= policydb
.role_val_to_struct
[i
];
1551 ebitmap_for_each_bit(&role
->types
, tnode
, j
) {
1552 if (!ebitmap_node_get_bit(tnode
, j
))
1556 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1559 rc
= context_struct_compute_av(fromcon
, &usercon
,
1561 PROCESS__TRANSITION
,
1563 if (rc
|| !(avd
.allowed
& PROCESS__TRANSITION
))
1565 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1570 if (mynel
< maxnel
) {
1571 mysids
[mynel
++] = sid
;
1574 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1580 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1583 mysids
[mynel
++] = sid
;
1598 * security_genfs_sid - Obtain a SID for a file in a filesystem
1599 * @fstype: filesystem type
1600 * @path: path from root of mount
1601 * @sclass: file security class
1602 * @sid: SID for path
1604 * Obtain a SID to use for a file in a filesystem that
1605 * cannot support xattr or use a fixed labeling behavior like
1606 * transition SIDs or task SIDs.
1608 int security_genfs_sid(const char *fstype
,
1614 struct genfs
*genfs
;
1616 int rc
= 0, cmp
= 0;
1620 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1621 cmp
= strcmp(fstype
, genfs
->fstype
);
1626 if (!genfs
|| cmp
) {
1627 *sid
= SECINITSID_UNLABELED
;
1632 for (c
= genfs
->head
; c
; c
= c
->next
) {
1633 len
= strlen(c
->u
.name
);
1634 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1635 (strncmp(c
->u
.name
, path
, len
) == 0))
1640 *sid
= SECINITSID_UNLABELED
;
1646 rc
= sidtab_context_to_sid(&sidtab
,
1660 * security_fs_use - Determine how to handle labeling for a filesystem.
1661 * @fstype: filesystem type
1662 * @behavior: labeling behavior
1663 * @sid: SID for filesystem (superblock)
1665 int security_fs_use(
1667 unsigned int *behavior
,
1675 c
= policydb
.ocontexts
[OCON_FSUSE
];
1677 if (strcmp(fstype
, c
->u
.name
) == 0)
1683 *behavior
= c
->v
.behavior
;
1685 rc
= sidtab_context_to_sid(&sidtab
,
1693 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1695 *behavior
= SECURITY_FS_USE_NONE
;
1698 *behavior
= SECURITY_FS_USE_GENFS
;
1707 int security_get_bools(int *len
, char ***names
, int **values
)
1709 int i
, rc
= -ENOMEM
;
1715 *len
= policydb
.p_bools
.nprim
;
1721 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1725 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1729 for (i
= 0; i
< *len
; i
++) {
1731 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1732 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1733 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1736 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1737 (*names
)[i
][name_len
- 1] = 0;
1745 for (i
= 0; i
< *len
; i
++)
1753 int security_set_bools(int len
, int *values
)
1756 int lenp
, seqno
= 0;
1757 struct cond_node
*cur
;
1761 lenp
= policydb
.p_bools
.nprim
;
1767 for (i
= 0; i
< len
; i
++) {
1768 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
1769 audit_log(current
->audit_context
, GFP_ATOMIC
,
1770 AUDIT_MAC_CONFIG_CHANGE
,
1771 "bool=%s val=%d old_val=%d auid=%u",
1772 policydb
.p_bool_val_to_name
[i
],
1774 policydb
.bool_val_to_struct
[i
]->state
,
1775 audit_get_loginuid(current
->audit_context
));
1778 policydb
.bool_val_to_struct
[i
]->state
= 1;
1780 policydb
.bool_val_to_struct
[i
]->state
= 0;
1784 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1785 rc
= evaluate_cond_node(&policydb
, cur
);
1790 seqno
= ++latest_granting
;
1795 avc_ss_reset(seqno
);
1796 selnl_notify_policyload(seqno
);
1801 int security_get_bool_value(int bool)
1808 len
= policydb
.p_bools
.nprim
;
1814 rc
= policydb
.bool_val_to_struct
[bool]->state
;
1820 struct selinux_audit_rule
{
1822 struct context au_ctxt
;
1825 void selinux_audit_rule_free(struct selinux_audit_rule
*rule
)
1828 context_destroy(&rule
->au_ctxt
);
1833 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
,
1834 struct selinux_audit_rule
**rule
)
1836 struct selinux_audit_rule
*tmprule
;
1837 struct role_datum
*roledatum
;
1838 struct type_datum
*typedatum
;
1839 struct user_datum
*userdatum
;
1844 if (!ss_initialized
)
1851 /* only 'equals' and 'not equals' fit user, role, and type */
1852 if (op
!= AUDIT_EQUAL
&& op
!= AUDIT_NOT_EQUAL
)
1857 /* we do not allow a range, indicated by the presense of '-' */
1858 if (strchr(rulestr
, '-'))
1862 /* only the above fields are valid */
1866 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
1870 context_init(&tmprule
->au_ctxt
);
1874 tmprule
->au_seqno
= latest_granting
;
1878 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
1882 tmprule
->au_ctxt
.user
= userdatum
->value
;
1885 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
1889 tmprule
->au_ctxt
.role
= roledatum
->value
;
1892 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
1896 tmprule
->au_ctxt
.type
= typedatum
->value
;
1900 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
1907 selinux_audit_rule_free(tmprule
);
1916 int selinux_audit_rule_match(u32 ctxid
, u32 field
, u32 op
,
1917 struct selinux_audit_rule
*rule
,
1918 struct audit_context
*actx
)
1920 struct context
*ctxt
;
1921 struct mls_level
*level
;
1925 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
1926 "selinux_audit_rule_match: missing rule\n");
1932 if (rule
->au_seqno
< latest_granting
) {
1933 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
1934 "selinux_audit_rule_match: stale rule\n");
1939 ctxt
= sidtab_search(&sidtab
, ctxid
);
1941 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
1942 "selinux_audit_rule_match: unrecognized SID %d\n",
1948 /* a field/op pair that is not caught here will simply fall through
1954 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
1956 case AUDIT_NOT_EQUAL
:
1957 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
1964 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
1966 case AUDIT_NOT_EQUAL
:
1967 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
1974 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
1976 case AUDIT_NOT_EQUAL
:
1977 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
1983 level
= (op
== AUDIT_SE_SEN
?
1984 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
1987 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
1990 case AUDIT_NOT_EQUAL
:
1991 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
1994 case AUDIT_LESS_THAN
:
1995 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
1997 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2000 case AUDIT_LESS_THAN_OR_EQUAL
:
2001 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2004 case AUDIT_GREATER_THAN
:
2005 match
= (mls_level_dom(level
,
2006 &rule
->au_ctxt
.range
.level
[0]) &&
2007 !mls_level_eq(level
,
2008 &rule
->au_ctxt
.range
.level
[0]));
2010 case AUDIT_GREATER_THAN_OR_EQUAL
:
2011 match
= mls_level_dom(level
,
2012 &rule
->au_ctxt
.range
.level
[0]);
2022 static int (*aurule_callback
)(void) = NULL
;
2024 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2025 u16
class, u32 perms
, u32
*retained
)
2029 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2030 err
= aurule_callback();
2034 static int __init
aurule_init(void)
2038 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2039 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2041 panic("avc_add_callback() failed, error %d\n", err
);
2045 __initcall(aurule_init
);
2047 void selinux_audit_set_callback(int (*callback
)(void))
2049 aurule_callback
= callback
;