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 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
21 * Updated: Chad Sellers <csellers@tresys.com>
23 * Added validation of kernel classes and permissions
25 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
28 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/spinlock.h>
37 #include <linux/rcupdate.h>
38 #include <linux/errno.h>
40 #include <linux/sched.h>
41 #include <linux/audit.h>
42 #include <linux/mutex.h>
43 #include <net/netlabel.h>
53 #include "conditional.h"
60 extern void selnl_notify_policyload(u32 seqno
);
61 unsigned int policydb_loaded_version
;
63 int selinux_policycap_netpeer
;
64 int selinux_policycap_openperm
;
67 * This is declared in avc.c
69 extern const struct selinux_class_perm selinux_class_perm
;
71 static DEFINE_RWLOCK(policy_rwlock
);
72 #define POLICY_RDLOCK read_lock(&policy_rwlock)
73 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
74 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
75 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
77 static DEFINE_MUTEX(load_mutex
);
78 #define LOAD_LOCK mutex_lock(&load_mutex)
79 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
81 static struct sidtab sidtab
;
82 struct policydb policydb
;
83 int ss_initialized
= 0;
86 * The largest sequence number that has been used when
87 * providing an access decision to the access vector cache.
88 * The sequence number only changes when a policy change
91 static u32 latest_granting
= 0;
93 /* Forward declaration. */
94 static int context_struct_to_string(struct context
*context
, char **scontext
,
98 * Return the boolean value of a constraint expression
99 * when it is applied to the specified source and target
102 * xcontext is a special beast... It is used by the validatetrans rules
103 * only. For these rules, scontext is the context before the transition,
104 * tcontext is the context after the transition, and xcontext is the context
105 * of the process performing the transition. All other callers of
106 * constraint_expr_eval should pass in NULL for xcontext.
108 static int constraint_expr_eval(struct context
*scontext
,
109 struct context
*tcontext
,
110 struct context
*xcontext
,
111 struct constraint_expr
*cexpr
)
115 struct role_datum
*r1
, *r2
;
116 struct mls_level
*l1
, *l2
;
117 struct constraint_expr
*e
;
118 int s
[CEXPR_MAXDEPTH
];
121 for (e
= cexpr
; e
; e
= e
->next
) {
122 switch (e
->expr_type
) {
138 if (sp
== (CEXPR_MAXDEPTH
-1))
142 val1
= scontext
->user
;
143 val2
= tcontext
->user
;
146 val1
= scontext
->type
;
147 val2
= tcontext
->type
;
150 val1
= scontext
->role
;
151 val2
= tcontext
->role
;
152 r1
= policydb
.role_val_to_struct
[val1
- 1];
153 r2
= policydb
.role_val_to_struct
[val2
- 1];
156 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
160 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
164 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
166 !ebitmap_get_bit(&r2
->dominates
,
174 l1
= &(scontext
->range
.level
[0]);
175 l2
= &(tcontext
->range
.level
[0]);
178 l1
= &(scontext
->range
.level
[0]);
179 l2
= &(tcontext
->range
.level
[1]);
182 l1
= &(scontext
->range
.level
[1]);
183 l2
= &(tcontext
->range
.level
[0]);
186 l1
= &(scontext
->range
.level
[1]);
187 l2
= &(tcontext
->range
.level
[1]);
190 l1
= &(scontext
->range
.level
[0]);
191 l2
= &(scontext
->range
.level
[1]);
194 l1
= &(tcontext
->range
.level
[0]);
195 l2
= &(tcontext
->range
.level
[1]);
200 s
[++sp
] = mls_level_eq(l1
, l2
);
203 s
[++sp
] = !mls_level_eq(l1
, l2
);
206 s
[++sp
] = mls_level_dom(l1
, l2
);
209 s
[++sp
] = mls_level_dom(l2
, l1
);
212 s
[++sp
] = mls_level_incomp(l2
, l1
);
226 s
[++sp
] = (val1
== val2
);
229 s
[++sp
] = (val1
!= val2
);
237 if (sp
== (CEXPR_MAXDEPTH
-1))
240 if (e
->attr
& CEXPR_TARGET
)
242 else if (e
->attr
& CEXPR_XTARGET
) {
249 if (e
->attr
& CEXPR_USER
)
251 else if (e
->attr
& CEXPR_ROLE
)
253 else if (e
->attr
& CEXPR_TYPE
)
262 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
265 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
283 * Compute access vectors based on a context structure pair for
284 * the permissions in a particular class.
286 static int context_struct_compute_av(struct context
*scontext
,
287 struct context
*tcontext
,
290 struct av_decision
*avd
)
292 struct constraint_node
*constraint
;
293 struct role_allow
*ra
;
294 struct avtab_key avkey
;
295 struct avtab_node
*node
;
296 struct class_datum
*tclass_datum
;
297 struct ebitmap
*sattr
, *tattr
;
298 struct ebitmap_node
*snode
, *tnode
;
299 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
303 * Remap extended Netlink classes for old policy versions.
304 * Do this here rather than socket_type_to_security_class()
305 * in case a newer policy version is loaded, allowing sockets
306 * to remain in the correct class.
308 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
309 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
310 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
311 tclass
= SECCLASS_NETLINK_SOCKET
;
314 * Initialize the access vectors to the default values.
317 avd
->decided
= 0xffffffff;
319 avd
->auditdeny
= 0xffffffff;
320 avd
->seqno
= latest_granting
;
323 * Check for all the invalid cases.
325 * - tclass > policy and > kernel
326 * - tclass > policy but is a userspace class
327 * - tclass > policy but we do not allow unknowns
329 if (unlikely(!tclass
))
331 if (unlikely(tclass
> policydb
.p_classes
.nprim
))
332 if (tclass
> kdefs
->cts_len
||
333 !kdefs
->class_to_string
[tclass
- 1] ||
334 !policydb
.allow_unknown
)
338 * Kernel class and we allow unknown so pad the allow decision
339 * the pad will be all 1 for unknown classes.
341 if (tclass
<= kdefs
->cts_len
&& policydb
.allow_unknown
)
342 avd
->allowed
= policydb
.undefined_perms
[tclass
- 1];
345 * Not in policy. Since decision is completed (all 1 or all 0) return.
347 if (unlikely(tclass
> policydb
.p_classes
.nprim
))
350 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
353 * If a specific type enforcement rule was defined for
354 * this permission check, then use it.
356 avkey
.target_class
= tclass
;
357 avkey
.specified
= AVTAB_AV
;
358 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
359 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
360 ebitmap_for_each_positive_bit(sattr
, snode
, i
) {
361 ebitmap_for_each_positive_bit(tattr
, tnode
, j
) {
362 avkey
.source_type
= i
+ 1;
363 avkey
.target_type
= j
+ 1;
364 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
366 node
= avtab_search_node_next(node
, avkey
.specified
)) {
367 if (node
->key
.specified
== AVTAB_ALLOWED
)
368 avd
->allowed
|= node
->datum
.data
;
369 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
370 avd
->auditallow
|= node
->datum
.data
;
371 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
372 avd
->auditdeny
&= node
->datum
.data
;
375 /* Check conditional av table for additional permissions */
376 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
382 * Remove any permissions prohibited by a constraint (this includes
385 constraint
= tclass_datum
->constraints
;
387 if ((constraint
->permissions
& (avd
->allowed
)) &&
388 !constraint_expr_eval(scontext
, tcontext
, NULL
,
390 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
392 constraint
= constraint
->next
;
396 * If checking process transition permission and the
397 * role is changing, then check the (current_role, new_role)
400 if (tclass
== SECCLASS_PROCESS
&&
401 (avd
->allowed
& (PROCESS__TRANSITION
| PROCESS__DYNTRANSITION
)) &&
402 scontext
->role
!= tcontext
->role
) {
403 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
404 if (scontext
->role
== ra
->role
&&
405 tcontext
->role
== ra
->new_role
)
409 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
|
410 PROCESS__DYNTRANSITION
);
416 printk(KERN_ERR
"%s: unrecognized class %d\n", __func__
, tclass
);
420 static int security_validtrans_handle_fail(struct context
*ocontext
,
421 struct context
*ncontext
,
422 struct context
*tcontext
,
425 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
426 u32 olen
, nlen
, tlen
;
428 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
430 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
432 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
434 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
435 "security_validate_transition: denied for"
436 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
437 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
443 if (!selinux_enforcing
)
448 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
451 struct context
*ocontext
;
452 struct context
*ncontext
;
453 struct context
*tcontext
;
454 struct class_datum
*tclass_datum
;
455 struct constraint_node
*constraint
;
464 * Remap extended Netlink classes for old policy versions.
465 * Do this here rather than socket_type_to_security_class()
466 * in case a newer policy version is loaded, allowing sockets
467 * to remain in the correct class.
469 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
470 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
471 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
472 tclass
= SECCLASS_NETLINK_SOCKET
;
474 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
475 printk(KERN_ERR
"security_validate_transition: "
476 "unrecognized class %d\n", tclass
);
480 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
482 ocontext
= sidtab_search(&sidtab
, oldsid
);
484 printk(KERN_ERR
"security_validate_transition: "
485 " unrecognized SID %d\n", oldsid
);
490 ncontext
= sidtab_search(&sidtab
, newsid
);
492 printk(KERN_ERR
"security_validate_transition: "
493 " unrecognized SID %d\n", newsid
);
498 tcontext
= sidtab_search(&sidtab
, tasksid
);
500 printk(KERN_ERR
"security_validate_transition: "
501 " unrecognized SID %d\n", tasksid
);
506 constraint
= tclass_datum
->validatetrans
;
508 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
510 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
514 constraint
= constraint
->next
;
523 * security_compute_av - Compute access vector decisions.
524 * @ssid: source security identifier
525 * @tsid: target security identifier
526 * @tclass: target security class
527 * @requested: requested permissions
528 * @avd: access vector decisions
530 * Compute a set of access vector decisions based on the
531 * SID pair (@ssid, @tsid) for the permissions in @tclass.
532 * Return -%EINVAL if any of the parameters are invalid or %0
533 * if the access vector decisions were computed successfully.
535 int security_compute_av(u32 ssid
,
539 struct av_decision
*avd
)
541 struct context
*scontext
= NULL
, *tcontext
= NULL
;
544 if (!ss_initialized
) {
545 avd
->allowed
= 0xffffffff;
546 avd
->decided
= 0xffffffff;
548 avd
->auditdeny
= 0xffffffff;
549 avd
->seqno
= latest_granting
;
555 scontext
= sidtab_search(&sidtab
, ssid
);
557 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
562 tcontext
= sidtab_search(&sidtab
, tsid
);
564 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
570 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
578 * Write the security context string representation of
579 * the context structure `context' into a dynamically
580 * allocated string of the correct size. Set `*scontext'
581 * to point to this string and set `*scontext_len' to
582 * the length of the string.
584 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
591 /* Compute the size of the context. */
592 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
593 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
594 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
595 *scontext_len
+= mls_compute_context_len(context
);
597 /* Allocate space for the context; caller must free this space. */
598 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
602 *scontext
= scontextp
;
605 * Copy the user name, role name and type name into the context.
607 sprintf(scontextp
, "%s:%s:%s",
608 policydb
.p_user_val_to_name
[context
->user
- 1],
609 policydb
.p_role_val_to_name
[context
->role
- 1],
610 policydb
.p_type_val_to_name
[context
->type
- 1]);
611 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
612 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
613 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
615 mls_sid_to_context(context
, &scontextp
);
622 #include "initial_sid_to_string.h"
624 const char *security_get_initial_sid_context(u32 sid
)
626 if (unlikely(sid
> SECINITSID_NUM
))
628 return initial_sid_to_string
[sid
];
632 * security_sid_to_context - Obtain a context for a given SID.
633 * @sid: security identifier, SID
634 * @scontext: security context
635 * @scontext_len: length in bytes
637 * Write the string representation of the context associated with @sid
638 * into a dynamically allocated string of the correct size. Set @scontext
639 * to point to this string and set @scontext_len to the length of the string.
641 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
643 struct context
*context
;
649 if (!ss_initialized
) {
650 if (sid
<= SECINITSID_NUM
) {
653 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
654 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
659 strcpy(scontextp
, initial_sid_to_string
[sid
]);
660 *scontext
= scontextp
;
663 printk(KERN_ERR
"security_sid_to_context: called before initial "
664 "load_policy on unknown SID %d\n", sid
);
669 context
= sidtab_search(&sidtab
, sid
);
671 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
676 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
684 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
,
685 u32
*sid
, u32 def_sid
, gfp_t gfp_flags
)
688 struct context context
;
689 struct role_datum
*role
;
690 struct type_datum
*typdatum
;
691 struct user_datum
*usrdatum
;
692 char *scontextp
, *p
, oldc
;
695 if (!ss_initialized
) {
698 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
699 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
704 *sid
= SECINITSID_KERNEL
;
709 /* Copy the string so that we can modify the copy as we parse it.
710 The string should already by null terminated, but we append a
711 null suffix to the copy to avoid problems with the existing
712 attr package, which doesn't view the null terminator as part
713 of the attribute value. */
714 scontext2
= kmalloc(scontext_len
+1, gfp_flags
);
719 memcpy(scontext2
, scontext
, scontext_len
);
720 scontext2
[scontext_len
] = 0;
722 context_init(&context
);
727 /* Parse the security context. */
730 scontextp
= (char *) scontext2
;
732 /* Extract the user. */
734 while (*p
&& *p
!= ':')
742 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
746 context
.user
= usrdatum
->value
;
750 while (*p
&& *p
!= ':')
758 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
761 context
.role
= role
->value
;
765 while (*p
&& *p
!= ':')
770 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
774 context
.type
= typdatum
->value
;
776 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
780 if ((p
- scontext2
) < scontext_len
) {
785 /* Check the validity of the new context. */
786 if (!policydb_context_isvalid(&policydb
, &context
)) {
790 /* Obtain the new sid. */
791 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
794 context_destroy(&context
);
801 * security_context_to_sid - Obtain a SID for a given security context.
802 * @scontext: security context
803 * @scontext_len: length in bytes
804 * @sid: security identifier, SID
806 * Obtains a SID associated with the security context that
807 * has the string representation specified by @scontext.
808 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
809 * memory is available, or 0 on success.
811 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
813 return security_context_to_sid_core(scontext
, scontext_len
,
814 sid
, SECSID_NULL
, GFP_KERNEL
);
818 * security_context_to_sid_default - Obtain a SID for a given security context,
819 * falling back to specified default if needed.
821 * @scontext: security context
822 * @scontext_len: length in bytes
823 * @sid: security identifier, SID
824 * @def_sid: default SID to assign on error
826 * Obtains a SID associated with the security context that
827 * has the string representation specified by @scontext.
828 * The default SID is passed to the MLS layer to be used to allow
829 * kernel labeling of the MLS field if the MLS field is not present
830 * (for upgrading to MLS without full relabel).
831 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
832 * memory is available, or 0 on success.
834 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
,
835 u32 def_sid
, gfp_t gfp_flags
)
837 return security_context_to_sid_core(scontext
, scontext_len
,
838 sid
, def_sid
, gfp_flags
);
841 static int compute_sid_handle_invalid_context(
842 struct context
*scontext
,
843 struct context
*tcontext
,
845 struct context
*newcontext
)
847 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
848 u32 slen
, tlen
, nlen
;
850 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
852 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
854 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
856 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
857 "security_compute_sid: invalid context %s"
861 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
866 if (!selinux_enforcing
)
871 static int security_compute_sid(u32 ssid
,
877 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
878 struct role_trans
*roletr
= NULL
;
879 struct avtab_key avkey
;
880 struct avtab_datum
*avdatum
;
881 struct avtab_node
*node
;
884 if (!ss_initialized
) {
886 case SECCLASS_PROCESS
:
896 context_init(&newcontext
);
900 scontext
= sidtab_search(&sidtab
, ssid
);
902 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
907 tcontext
= sidtab_search(&sidtab
, tsid
);
909 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
915 /* Set the user identity. */
917 case AVTAB_TRANSITION
:
919 /* Use the process user identity. */
920 newcontext
.user
= scontext
->user
;
923 /* Use the related object owner. */
924 newcontext
.user
= tcontext
->user
;
928 /* Set the role and type to default values. */
930 case SECCLASS_PROCESS
:
931 /* Use the current role and type of process. */
932 newcontext
.role
= scontext
->role
;
933 newcontext
.type
= scontext
->type
;
936 /* Use the well-defined object role. */
937 newcontext
.role
= OBJECT_R_VAL
;
938 /* Use the type of the related object. */
939 newcontext
.type
= tcontext
->type
;
942 /* Look for a type transition/member/change rule. */
943 avkey
.source_type
= scontext
->type
;
944 avkey
.target_type
= tcontext
->type
;
945 avkey
.target_class
= tclass
;
946 avkey
.specified
= specified
;
947 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
949 /* If no permanent rule, also check for enabled conditional rules */
951 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
952 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
953 if (node
->key
.specified
& AVTAB_ENABLED
) {
954 avdatum
= &node
->datum
;
961 /* Use the type from the type transition/member/change rule. */
962 newcontext
.type
= avdatum
->data
;
965 /* Check for class-specific changes. */
967 case SECCLASS_PROCESS
:
968 if (specified
& AVTAB_TRANSITION
) {
969 /* Look for a role transition rule. */
970 for (roletr
= policydb
.role_tr
; roletr
;
971 roletr
= roletr
->next
) {
972 if (roletr
->role
== scontext
->role
&&
973 roletr
->type
== tcontext
->type
) {
974 /* Use the role transition rule. */
975 newcontext
.role
= roletr
->new_role
;
985 /* Set the MLS attributes.
986 This is done last because it may allocate memory. */
987 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
991 /* Check the validity of the context. */
992 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
993 rc
= compute_sid_handle_invalid_context(scontext
,
1000 /* Obtain the sid for the context. */
1001 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
1004 context_destroy(&newcontext
);
1010 * security_transition_sid - Compute the SID for a new subject/object.
1011 * @ssid: source security identifier
1012 * @tsid: target security identifier
1013 * @tclass: target security class
1014 * @out_sid: security identifier for new subject/object
1016 * Compute a SID to use for labeling a new subject or object in the
1017 * class @tclass based on a SID pair (@ssid, @tsid).
1018 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1019 * if insufficient memory is available, or %0 if the new SID was
1020 * computed successfully.
1022 int security_transition_sid(u32 ssid
,
1027 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
1031 * security_member_sid - Compute the SID for member selection.
1032 * @ssid: source security identifier
1033 * @tsid: target security identifier
1034 * @tclass: target security class
1035 * @out_sid: security identifier for selected member
1037 * Compute a SID to use when selecting a member of a polyinstantiated
1038 * object of class @tclass based on a SID pair (@ssid, @tsid).
1039 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1040 * if insufficient memory is available, or %0 if the SID was
1041 * computed successfully.
1043 int security_member_sid(u32 ssid
,
1048 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
1052 * security_change_sid - Compute the SID for object relabeling.
1053 * @ssid: source security identifier
1054 * @tsid: target security identifier
1055 * @tclass: target security class
1056 * @out_sid: security identifier for selected member
1058 * Compute a SID to use for relabeling an object of class @tclass
1059 * based on a SID pair (@ssid, @tsid).
1060 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1061 * if insufficient memory is available, or %0 if the SID was
1062 * computed successfully.
1064 int security_change_sid(u32 ssid
,
1069 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1073 * Verify that each kernel class that is defined in the
1076 static int validate_classes(struct policydb
*p
)
1079 struct class_datum
*cladatum
;
1080 struct perm_datum
*perdatum
;
1081 u32 nprim
, tmp
, common_pts_len
, perm_val
, pol_val
;
1083 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
1084 const char *def_class
, *def_perm
, *pol_class
;
1085 struct symtab
*perms
;
1087 if (p
->allow_unknown
) {
1088 u32 num_classes
= kdefs
->cts_len
;
1089 p
->undefined_perms
= kcalloc(num_classes
, sizeof(u32
), GFP_KERNEL
);
1090 if (!p
->undefined_perms
)
1094 for (i
= 1; i
< kdefs
->cts_len
; i
++) {
1095 def_class
= kdefs
->class_to_string
[i
];
1098 if (i
> p
->p_classes
.nprim
) {
1100 "SELinux: class %s not defined in policy\n",
1102 if (p
->reject_unknown
)
1104 if (p
->allow_unknown
)
1105 p
->undefined_perms
[i
-1] = ~0U;
1108 pol_class
= p
->p_class_val_to_name
[i
-1];
1109 if (strcmp(pol_class
, def_class
)) {
1111 "SELinux: class %d is incorrect, found %s but should be %s\n",
1112 i
, pol_class
, def_class
);
1116 for (i
= 0; i
< kdefs
->av_pts_len
; i
++) {
1117 class_val
= kdefs
->av_perm_to_string
[i
].tclass
;
1118 perm_val
= kdefs
->av_perm_to_string
[i
].value
;
1119 def_perm
= kdefs
->av_perm_to_string
[i
].name
;
1120 if (class_val
> p
->p_classes
.nprim
)
1122 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1123 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1125 perms
= &cladatum
->permissions
;
1126 nprim
= 1 << (perms
->nprim
- 1);
1127 if (perm_val
> nprim
) {
1129 "SELinux: permission %s in class %s not defined in policy\n",
1130 def_perm
, pol_class
);
1131 if (p
->reject_unknown
)
1133 if (p
->allow_unknown
)
1134 p
->undefined_perms
[class_val
-1] |= perm_val
;
1137 perdatum
= hashtab_search(perms
->table
, def_perm
);
1138 if (perdatum
== NULL
) {
1140 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1141 def_perm
, pol_class
);
1144 pol_val
= 1 << (perdatum
->value
- 1);
1145 if (pol_val
!= perm_val
) {
1147 "SELinux: permission %s in class %s has incorrect value\n",
1148 def_perm
, pol_class
);
1152 for (i
= 0; i
< kdefs
->av_inherit_len
; i
++) {
1153 class_val
= kdefs
->av_inherit
[i
].tclass
;
1154 if (class_val
> p
->p_classes
.nprim
)
1156 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1157 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1159 if (!cladatum
->comdatum
) {
1161 "SELinux: class %s should have an inherits clause but does not\n",
1165 tmp
= kdefs
->av_inherit
[i
].common_base
;
1167 while (!(tmp
& 0x01)) {
1171 perms
= &cladatum
->comdatum
->permissions
;
1172 for (j
= 0; j
< common_pts_len
; j
++) {
1173 def_perm
= kdefs
->av_inherit
[i
].common_pts
[j
];
1174 if (j
>= perms
->nprim
) {
1176 "SELinux: permission %s in class %s not defined in policy\n",
1177 def_perm
, pol_class
);
1178 if (p
->reject_unknown
)
1180 if (p
->allow_unknown
)
1181 p
->undefined_perms
[class_val
-1] |= (1 << j
);
1184 perdatum
= hashtab_search(perms
->table
, def_perm
);
1185 if (perdatum
== NULL
) {
1187 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1188 def_perm
, pol_class
);
1191 if (perdatum
->value
!= j
+ 1) {
1193 "SELinux: permission %s in class %s has incorrect value\n",
1194 def_perm
, pol_class
);
1202 /* Clone the SID into the new SID table. */
1203 static int clone_sid(u32 sid
,
1204 struct context
*context
,
1207 struct sidtab
*s
= arg
;
1209 return sidtab_insert(s
, sid
, context
);
1212 static inline int convert_context_handle_invalid_context(struct context
*context
)
1216 if (selinux_enforcing
) {
1222 context_struct_to_string(context
, &s
, &len
);
1223 printk(KERN_ERR
"SELinux: context %s is invalid\n", s
);
1229 struct convert_context_args
{
1230 struct policydb
*oldp
;
1231 struct policydb
*newp
;
1235 * Convert the values in the security context
1236 * structure `c' from the values specified
1237 * in the policy `p->oldp' to the values specified
1238 * in the policy `p->newp'. Verify that the
1239 * context is valid under the new policy.
1241 static int convert_context(u32 key
,
1245 struct convert_context_args
*args
;
1246 struct context oldc
;
1247 struct role_datum
*role
;
1248 struct type_datum
*typdatum
;
1249 struct user_datum
*usrdatum
;
1256 rc
= context_cpy(&oldc
, c
);
1262 /* Convert the user. */
1263 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1264 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1268 c
->user
= usrdatum
->value
;
1270 /* Convert the role. */
1271 role
= hashtab_search(args
->newp
->p_roles
.table
,
1272 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1276 c
->role
= role
->value
;
1278 /* Convert the type. */
1279 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1280 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1284 c
->type
= typdatum
->value
;
1286 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1290 /* Check the validity of the new context. */
1291 if (!policydb_context_isvalid(args
->newp
, c
)) {
1292 rc
= convert_context_handle_invalid_context(&oldc
);
1297 context_destroy(&oldc
);
1301 context_struct_to_string(&oldc
, &s
, &len
);
1302 context_destroy(&oldc
);
1303 printk(KERN_ERR
"SELinux: invalidating context %s\n", s
);
1308 static void security_load_policycaps(void)
1310 selinux_policycap_netpeer
= ebitmap_get_bit(&policydb
.policycaps
,
1311 POLICYDB_CAPABILITY_NETPEER
);
1312 selinux_policycap_openperm
= ebitmap_get_bit(&policydb
.policycaps
,
1313 POLICYDB_CAPABILITY_OPENPERM
);
1316 extern void selinux_complete_init(void);
1317 static int security_preserve_bools(struct policydb
*p
);
1320 * security_load_policy - Load a security policy configuration.
1321 * @data: binary policy data
1322 * @len: length of data in bytes
1324 * Load a new set of security policy configuration data,
1325 * validate it and convert the SID table as necessary.
1326 * This function will flush the access vector cache after
1327 * loading the new policy.
1329 int security_load_policy(void *data
, size_t len
)
1331 struct policydb oldpolicydb
, newpolicydb
;
1332 struct sidtab oldsidtab
, newsidtab
;
1333 struct convert_context_args args
;
1336 struct policy_file file
= { data
, len
}, *fp
= &file
;
1340 if (!ss_initialized
) {
1342 if (policydb_read(&policydb
, fp
)) {
1344 avtab_cache_destroy();
1347 if (policydb_load_isids(&policydb
, &sidtab
)) {
1349 policydb_destroy(&policydb
);
1350 avtab_cache_destroy();
1353 /* Verify that the kernel defined classes are correct. */
1354 if (validate_classes(&policydb
)) {
1356 "SELinux: the definition of a class is incorrect\n");
1358 sidtab_destroy(&sidtab
);
1359 policydb_destroy(&policydb
);
1360 avtab_cache_destroy();
1363 security_load_policycaps();
1364 policydb_loaded_version
= policydb
.policyvers
;
1366 seqno
= ++latest_granting
;
1368 selinux_complete_init();
1369 avc_ss_reset(seqno
);
1370 selnl_notify_policyload(seqno
);
1371 selinux_netlbl_cache_invalidate();
1372 selinux_xfrm_notify_policyload();
1377 sidtab_hash_eval(&sidtab
, "sids");
1380 if (policydb_read(&newpolicydb
, fp
)) {
1385 sidtab_init(&newsidtab
);
1387 /* Verify that the kernel defined classes are correct. */
1388 if (validate_classes(&newpolicydb
)) {
1390 "SELinux: the definition of a class is incorrect\n");
1395 rc
= security_preserve_bools(&newpolicydb
);
1397 printk(KERN_ERR
"SELinux: unable to preserve booleans\n");
1401 /* Clone the SID table. */
1402 sidtab_shutdown(&sidtab
);
1403 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1408 /* Convert the internal representations of contexts
1409 in the new SID table and remove invalid SIDs. */
1410 args
.oldp
= &policydb
;
1411 args
.newp
= &newpolicydb
;
1412 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1414 /* Save the old policydb and SID table to free later. */
1415 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1416 sidtab_set(&oldsidtab
, &sidtab
);
1418 /* Install the new policydb and SID table. */
1420 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1421 sidtab_set(&sidtab
, &newsidtab
);
1422 security_load_policycaps();
1423 seqno
= ++latest_granting
;
1424 policydb_loaded_version
= policydb
.policyvers
;
1428 /* Free the old policydb and SID table. */
1429 policydb_destroy(&oldpolicydb
);
1430 sidtab_destroy(&oldsidtab
);
1432 avc_ss_reset(seqno
);
1433 selnl_notify_policyload(seqno
);
1434 selinux_netlbl_cache_invalidate();
1435 selinux_xfrm_notify_policyload();
1441 sidtab_destroy(&newsidtab
);
1442 policydb_destroy(&newpolicydb
);
1448 * security_port_sid - Obtain the SID for a port.
1449 * @domain: communication domain aka address family
1450 * @type: socket type
1451 * @protocol: protocol number
1452 * @port: port number
1453 * @out_sid: security identifier
1455 int security_port_sid(u16 domain
,
1466 c
= policydb
.ocontexts
[OCON_PORT
];
1468 if (c
->u
.port
.protocol
== protocol
&&
1469 c
->u
.port
.low_port
<= port
&&
1470 c
->u
.port
.high_port
>= port
)
1477 rc
= sidtab_context_to_sid(&sidtab
,
1483 *out_sid
= c
->sid
[0];
1485 *out_sid
= SECINITSID_PORT
;
1494 * security_netif_sid - Obtain the SID for a network interface.
1495 * @name: interface name
1496 * @if_sid: interface SID
1498 int security_netif_sid(char *name
, u32
*if_sid
)
1505 c
= policydb
.ocontexts
[OCON_NETIF
];
1507 if (strcmp(name
, c
->u
.name
) == 0)
1513 if (!c
->sid
[0] || !c
->sid
[1]) {
1514 rc
= sidtab_context_to_sid(&sidtab
,
1519 rc
= sidtab_context_to_sid(&sidtab
,
1525 *if_sid
= c
->sid
[0];
1527 *if_sid
= SECINITSID_NETIF
;
1534 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1538 for(i
= 0; i
< 4; i
++)
1539 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1548 * security_node_sid - Obtain the SID for a node (host).
1549 * @domain: communication domain aka address family
1551 * @addrlen: address length in bytes
1552 * @out_sid: security identifier
1554 int security_node_sid(u16 domain
,
1568 if (addrlen
!= sizeof(u32
)) {
1573 addr
= *((u32
*)addrp
);
1575 c
= policydb
.ocontexts
[OCON_NODE
];
1577 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1585 if (addrlen
!= sizeof(u64
) * 2) {
1589 c
= policydb
.ocontexts
[OCON_NODE6
];
1591 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1599 *out_sid
= SECINITSID_NODE
;
1605 rc
= sidtab_context_to_sid(&sidtab
,
1611 *out_sid
= c
->sid
[0];
1613 *out_sid
= SECINITSID_NODE
;
1624 * security_get_user_sids - Obtain reachable SIDs for a user.
1625 * @fromsid: starting SID
1626 * @username: username
1627 * @sids: array of reachable SIDs for user
1628 * @nel: number of elements in @sids
1630 * Generate the set of SIDs for legal security contexts
1631 * for a given user that can be reached by @fromsid.
1632 * Set *@sids to point to a dynamically allocated
1633 * array containing the set of SIDs. Set *@nel to the
1634 * number of elements in the array.
1637 int security_get_user_sids(u32 fromsid
,
1642 struct context
*fromcon
, usercon
;
1643 u32
*mysids
= NULL
, *mysids2
, sid
;
1644 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1645 struct user_datum
*user
;
1646 struct role_datum
*role
;
1647 struct ebitmap_node
*rnode
, *tnode
;
1653 if (!ss_initialized
)
1658 fromcon
= sidtab_search(&sidtab
, fromsid
);
1664 user
= hashtab_search(policydb
.p_users
.table
, username
);
1669 usercon
.user
= user
->value
;
1671 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1677 ebitmap_for_each_positive_bit(&user
->roles
, rnode
, i
) {
1678 role
= policydb
.role_val_to_struct
[i
];
1680 ebitmap_for_each_positive_bit(&role
->types
, tnode
, j
) {
1683 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1686 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1689 if (mynel
< maxnel
) {
1690 mysids
[mynel
++] = sid
;
1693 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1698 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1701 mysids
[mynel
++] = sid
;
1713 mysids2
= kcalloc(mynel
, sizeof(*mysids2
), GFP_KERNEL
);
1719 for (i
= 0, j
= 0; i
< mynel
; i
++) {
1720 rc
= avc_has_perm_noaudit(fromsid
, mysids
[i
],
1722 PROCESS__TRANSITION
, AVC_STRICT
,
1725 mysids2
[j
++] = mysids
[i
];
1737 * security_genfs_sid - Obtain a SID for a file in a filesystem
1738 * @fstype: filesystem type
1739 * @path: path from root of mount
1740 * @sclass: file security class
1741 * @sid: SID for path
1743 * Obtain a SID to use for a file in a filesystem that
1744 * cannot support xattr or use a fixed labeling behavior like
1745 * transition SIDs or task SIDs.
1747 int security_genfs_sid(const char *fstype
,
1753 struct genfs
*genfs
;
1755 int rc
= 0, cmp
= 0;
1757 while (path
[0] == '/' && path
[1] == '/')
1762 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1763 cmp
= strcmp(fstype
, genfs
->fstype
);
1768 if (!genfs
|| cmp
) {
1769 *sid
= SECINITSID_UNLABELED
;
1774 for (c
= genfs
->head
; c
; c
= c
->next
) {
1775 len
= strlen(c
->u
.name
);
1776 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1777 (strncmp(c
->u
.name
, path
, len
) == 0))
1782 *sid
= SECINITSID_UNLABELED
;
1788 rc
= sidtab_context_to_sid(&sidtab
,
1802 * security_fs_use - Determine how to handle labeling for a filesystem.
1803 * @fstype: filesystem type
1804 * @behavior: labeling behavior
1805 * @sid: SID for filesystem (superblock)
1807 int security_fs_use(
1809 unsigned int *behavior
,
1817 c
= policydb
.ocontexts
[OCON_FSUSE
];
1819 if (strcmp(fstype
, c
->u
.name
) == 0)
1825 *behavior
= c
->v
.behavior
;
1827 rc
= sidtab_context_to_sid(&sidtab
,
1835 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1837 *behavior
= SECURITY_FS_USE_NONE
;
1840 *behavior
= SECURITY_FS_USE_GENFS
;
1849 int security_get_bools(int *len
, char ***names
, int **values
)
1851 int i
, rc
= -ENOMEM
;
1857 *len
= policydb
.p_bools
.nprim
;
1863 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1867 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1871 for (i
= 0; i
< *len
; i
++) {
1873 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1874 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1875 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1878 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1879 (*names
)[i
][name_len
- 1] = 0;
1887 for (i
= 0; i
< *len
; i
++)
1895 int security_set_bools(int len
, int *values
)
1898 int lenp
, seqno
= 0;
1899 struct cond_node
*cur
;
1903 lenp
= policydb
.p_bools
.nprim
;
1909 for (i
= 0; i
< len
; i
++) {
1910 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
1911 audit_log(current
->audit_context
, GFP_ATOMIC
,
1912 AUDIT_MAC_CONFIG_CHANGE
,
1913 "bool=%s val=%d old_val=%d auid=%u ses=%u",
1914 policydb
.p_bool_val_to_name
[i
],
1916 policydb
.bool_val_to_struct
[i
]->state
,
1917 audit_get_loginuid(current
),
1918 audit_get_sessionid(current
));
1921 policydb
.bool_val_to_struct
[i
]->state
= 1;
1923 policydb
.bool_val_to_struct
[i
]->state
= 0;
1927 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1928 rc
= evaluate_cond_node(&policydb
, cur
);
1933 seqno
= ++latest_granting
;
1938 avc_ss_reset(seqno
);
1939 selnl_notify_policyload(seqno
);
1940 selinux_xfrm_notify_policyload();
1945 int security_get_bool_value(int bool)
1952 len
= policydb
.p_bools
.nprim
;
1958 rc
= policydb
.bool_val_to_struct
[bool]->state
;
1964 static int security_preserve_bools(struct policydb
*p
)
1966 int rc
, nbools
= 0, *bvalues
= NULL
, i
;
1967 char **bnames
= NULL
;
1968 struct cond_bool_datum
*booldatum
;
1969 struct cond_node
*cur
;
1971 rc
= security_get_bools(&nbools
, &bnames
, &bvalues
);
1974 for (i
= 0; i
< nbools
; i
++) {
1975 booldatum
= hashtab_search(p
->p_bools
.table
, bnames
[i
]);
1977 booldatum
->state
= bvalues
[i
];
1979 for (cur
= p
->cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1980 rc
= evaluate_cond_node(p
, cur
);
1987 for (i
= 0; i
< nbools
; i
++)
1996 * security_sid_mls_copy() - computes a new sid based on the given
1997 * sid and the mls portion of mls_sid.
1999 int security_sid_mls_copy(u32 sid
, u32 mls_sid
, u32
*new_sid
)
2001 struct context
*context1
;
2002 struct context
*context2
;
2003 struct context newcon
;
2008 if (!ss_initialized
|| !selinux_mls_enabled
) {
2013 context_init(&newcon
);
2016 context1
= sidtab_search(&sidtab
, sid
);
2018 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2024 context2
= sidtab_search(&sidtab
, mls_sid
);
2026 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2032 newcon
.user
= context1
->user
;
2033 newcon
.role
= context1
->role
;
2034 newcon
.type
= context1
->type
;
2035 rc
= mls_context_cpy(&newcon
, context2
);
2039 /* Check the validity of the new context. */
2040 if (!policydb_context_isvalid(&policydb
, &newcon
)) {
2041 rc
= convert_context_handle_invalid_context(&newcon
);
2046 rc
= sidtab_context_to_sid(&sidtab
, &newcon
, new_sid
);
2050 if (!context_struct_to_string(&newcon
, &s
, &len
)) {
2051 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2052 "security_sid_mls_copy: invalid context %s", s
);
2058 context_destroy(&newcon
);
2064 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2065 * @nlbl_sid: NetLabel SID
2066 * @nlbl_type: NetLabel labeling protocol type
2067 * @xfrm_sid: XFRM SID
2070 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2071 * resolved into a single SID it is returned via @peer_sid and the function
2072 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2073 * returns a negative value. A table summarizing the behavior is below:
2075 * | function return | @sid
2076 * ------------------------------+-----------------+-----------------
2077 * no peer labels | 0 | SECSID_NULL
2078 * single peer label | 0 | <peer_label>
2079 * multiple, consistent labels | 0 | <peer_label>
2080 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2083 int security_net_peersid_resolve(u32 nlbl_sid
, u32 nlbl_type
,
2088 struct context
*nlbl_ctx
;
2089 struct context
*xfrm_ctx
;
2091 /* handle the common (which also happens to be the set of easy) cases
2092 * right away, these two if statements catch everything involving a
2093 * single or absent peer SID/label */
2094 if (xfrm_sid
== SECSID_NULL
) {
2095 *peer_sid
= nlbl_sid
;
2098 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2099 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2101 if (nlbl_sid
== SECSID_NULL
|| nlbl_type
== NETLBL_NLTYPE_UNLABELED
) {
2102 *peer_sid
= xfrm_sid
;
2106 /* we don't need to check ss_initialized here since the only way both
2107 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2108 * security server was initialized and ss_initialized was true */
2109 if (!selinux_mls_enabled
) {
2110 *peer_sid
= SECSID_NULL
;
2116 nlbl_ctx
= sidtab_search(&sidtab
, nlbl_sid
);
2119 "security_sid_mls_cmp: unrecognized SID %d\n",
2124 xfrm_ctx
= sidtab_search(&sidtab
, xfrm_sid
);
2127 "security_sid_mls_cmp: unrecognized SID %d\n",
2132 rc
= (mls_context_cmp(nlbl_ctx
, xfrm_ctx
) ? 0 : -EACCES
);
2137 /* at present NetLabel SIDs/labels really only carry MLS
2138 * information so if the MLS portion of the NetLabel SID
2139 * matches the MLS portion of the labeled XFRM SID/label
2140 * then pass along the XFRM SID as it is the most
2142 *peer_sid
= xfrm_sid
;
2144 *peer_sid
= SECSID_NULL
;
2148 static int get_classes_callback(void *k
, void *d
, void *args
)
2150 struct class_datum
*datum
= d
;
2151 char *name
= k
, **classes
= args
;
2152 int value
= datum
->value
- 1;
2154 classes
[value
] = kstrdup(name
, GFP_ATOMIC
);
2155 if (!classes
[value
])
2161 int security_get_classes(char ***classes
, int *nclasses
)
2167 *nclasses
= policydb
.p_classes
.nprim
;
2168 *classes
= kcalloc(*nclasses
, sizeof(*classes
), GFP_ATOMIC
);
2172 rc
= hashtab_map(policydb
.p_classes
.table
, get_classes_callback
,
2176 for (i
= 0; i
< *nclasses
; i
++)
2177 kfree((*classes
)[i
]);
2186 static int get_permissions_callback(void *k
, void *d
, void *args
)
2188 struct perm_datum
*datum
= d
;
2189 char *name
= k
, **perms
= args
;
2190 int value
= datum
->value
- 1;
2192 perms
[value
] = kstrdup(name
, GFP_ATOMIC
);
2199 int security_get_permissions(char *class, char ***perms
, int *nperms
)
2201 int rc
= -ENOMEM
, i
;
2202 struct class_datum
*match
;
2206 match
= hashtab_search(policydb
.p_classes
.table
, class);
2208 printk(KERN_ERR
"%s: unrecognized class %s\n",
2214 *nperms
= match
->permissions
.nprim
;
2215 *perms
= kcalloc(*nperms
, sizeof(*perms
), GFP_ATOMIC
);
2219 if (match
->comdatum
) {
2220 rc
= hashtab_map(match
->comdatum
->permissions
.table
,
2221 get_permissions_callback
, *perms
);
2226 rc
= hashtab_map(match
->permissions
.table
, get_permissions_callback
,
2237 for (i
= 0; i
< *nperms
; i
++)
2243 int security_get_reject_unknown(void)
2245 return policydb
.reject_unknown
;
2248 int security_get_allow_unknown(void)
2250 return policydb
.allow_unknown
;
2254 * security_policycap_supported - Check for a specific policy capability
2255 * @req_cap: capability
2258 * This function queries the currently loaded policy to see if it supports the
2259 * capability specified by @req_cap. Returns true (1) if the capability is
2260 * supported, false (0) if it isn't supported.
2263 int security_policycap_supported(unsigned int req_cap
)
2268 rc
= ebitmap_get_bit(&policydb
.policycaps
, req_cap
);
2274 struct selinux_audit_rule
{
2276 struct context au_ctxt
;
2279 void selinux_audit_rule_free(struct selinux_audit_rule
*rule
)
2282 context_destroy(&rule
->au_ctxt
);
2287 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
,
2288 struct selinux_audit_rule
**rule
)
2290 struct selinux_audit_rule
*tmprule
;
2291 struct role_datum
*roledatum
;
2292 struct type_datum
*typedatum
;
2293 struct user_datum
*userdatum
;
2298 if (!ss_initialized
)
2302 case AUDIT_SUBJ_USER
:
2303 case AUDIT_SUBJ_ROLE
:
2304 case AUDIT_SUBJ_TYPE
:
2305 case AUDIT_OBJ_USER
:
2306 case AUDIT_OBJ_ROLE
:
2307 case AUDIT_OBJ_TYPE
:
2308 /* only 'equals' and 'not equals' fit user, role, and type */
2309 if (op
!= AUDIT_EQUAL
&& op
!= AUDIT_NOT_EQUAL
)
2312 case AUDIT_SUBJ_SEN
:
2313 case AUDIT_SUBJ_CLR
:
2314 case AUDIT_OBJ_LEV_LOW
:
2315 case AUDIT_OBJ_LEV_HIGH
:
2316 /* we do not allow a range, indicated by the presense of '-' */
2317 if (strchr(rulestr
, '-'))
2321 /* only the above fields are valid */
2325 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
2329 context_init(&tmprule
->au_ctxt
);
2333 tmprule
->au_seqno
= latest_granting
;
2336 case AUDIT_SUBJ_USER
:
2337 case AUDIT_OBJ_USER
:
2338 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
2342 tmprule
->au_ctxt
.user
= userdatum
->value
;
2344 case AUDIT_SUBJ_ROLE
:
2345 case AUDIT_OBJ_ROLE
:
2346 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
2350 tmprule
->au_ctxt
.role
= roledatum
->value
;
2352 case AUDIT_SUBJ_TYPE
:
2353 case AUDIT_OBJ_TYPE
:
2354 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
2358 tmprule
->au_ctxt
.type
= typedatum
->value
;
2360 case AUDIT_SUBJ_SEN
:
2361 case AUDIT_SUBJ_CLR
:
2362 case AUDIT_OBJ_LEV_LOW
:
2363 case AUDIT_OBJ_LEV_HIGH
:
2364 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
2371 selinux_audit_rule_free(tmprule
);
2380 int selinux_audit_rule_match(u32 sid
, u32 field
, u32 op
,
2381 struct selinux_audit_rule
*rule
,
2382 struct audit_context
*actx
)
2384 struct context
*ctxt
;
2385 struct mls_level
*level
;
2389 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2390 "selinux_audit_rule_match: missing rule\n");
2396 if (rule
->au_seqno
< latest_granting
) {
2397 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2398 "selinux_audit_rule_match: stale rule\n");
2403 ctxt
= sidtab_search(&sidtab
, sid
);
2405 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2406 "selinux_audit_rule_match: unrecognized SID %d\n",
2412 /* a field/op pair that is not caught here will simply fall through
2415 case AUDIT_SUBJ_USER
:
2416 case AUDIT_OBJ_USER
:
2419 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
2421 case AUDIT_NOT_EQUAL
:
2422 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
2426 case AUDIT_SUBJ_ROLE
:
2427 case AUDIT_OBJ_ROLE
:
2430 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
2432 case AUDIT_NOT_EQUAL
:
2433 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
2437 case AUDIT_SUBJ_TYPE
:
2438 case AUDIT_OBJ_TYPE
:
2441 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
2443 case AUDIT_NOT_EQUAL
:
2444 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
2448 case AUDIT_SUBJ_SEN
:
2449 case AUDIT_SUBJ_CLR
:
2450 case AUDIT_OBJ_LEV_LOW
:
2451 case AUDIT_OBJ_LEV_HIGH
:
2452 level
= ((field
== AUDIT_SUBJ_SEN
||
2453 field
== AUDIT_OBJ_LEV_LOW
) ?
2454 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
2457 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2460 case AUDIT_NOT_EQUAL
:
2461 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2464 case AUDIT_LESS_THAN
:
2465 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2467 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2470 case AUDIT_LESS_THAN_OR_EQUAL
:
2471 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2474 case AUDIT_GREATER_THAN
:
2475 match
= (mls_level_dom(level
,
2476 &rule
->au_ctxt
.range
.level
[0]) &&
2477 !mls_level_eq(level
,
2478 &rule
->au_ctxt
.range
.level
[0]));
2480 case AUDIT_GREATER_THAN_OR_EQUAL
:
2481 match
= mls_level_dom(level
,
2482 &rule
->au_ctxt
.range
.level
[0]);
2492 static int (*aurule_callback
)(void) = NULL
;
2494 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2495 u16
class, u32 perms
, u32
*retained
)
2499 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2500 err
= aurule_callback();
2504 static int __init
aurule_init(void)
2508 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2509 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2511 panic("avc_add_callback() failed, error %d\n", err
);
2515 __initcall(aurule_init
);
2517 void selinux_audit_set_callback(int (*callback
)(void))
2519 aurule_callback
= callback
;
2522 #ifdef CONFIG_NETLABEL
2524 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2525 * @secattr: the NetLabel packet security attributes
2526 * @sid: the SELinux SID
2529 * Attempt to cache the context in @ctx, which was derived from the packet in
2530 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2531 * already been initialized.
2534 static void security_netlbl_cache_add(struct netlbl_lsm_secattr
*secattr
,
2539 sid_cache
= kmalloc(sizeof(*sid_cache
), GFP_ATOMIC
);
2540 if (sid_cache
== NULL
)
2542 secattr
->cache
= netlbl_secattr_cache_alloc(GFP_ATOMIC
);
2543 if (secattr
->cache
== NULL
) {
2549 secattr
->cache
->free
= kfree
;
2550 secattr
->cache
->data
= sid_cache
;
2551 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
2555 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2556 * @secattr: the NetLabel packet security attributes
2557 * @sid: the SELinux SID
2560 * Convert the given NetLabel security attributes in @secattr into a
2561 * SELinux SID. If the @secattr field does not contain a full SELinux
2562 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2563 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2564 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2565 * conversion for future lookups. Returns zero on success, negative values on
2569 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr
*secattr
,
2573 struct context
*ctx
;
2574 struct context ctx_new
;
2576 if (!ss_initialized
) {
2583 if (secattr
->flags
& NETLBL_SECATTR_CACHE
) {
2584 *sid
= *(u32
*)secattr
->cache
->data
;
2586 } else if (secattr
->flags
& NETLBL_SECATTR_SECID
) {
2587 *sid
= secattr
->attr
.secid
;
2589 } else if (secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) {
2590 ctx
= sidtab_search(&sidtab
, SECINITSID_NETMSG
);
2592 goto netlbl_secattr_to_sid_return
;
2594 ctx_new
.user
= ctx
->user
;
2595 ctx_new
.role
= ctx
->role
;
2596 ctx_new
.type
= ctx
->type
;
2597 mls_import_netlbl_lvl(&ctx_new
, secattr
);
2598 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
2599 if (ebitmap_netlbl_import(&ctx_new
.range
.level
[0].cat
,
2600 secattr
->attr
.mls
.cat
) != 0)
2601 goto netlbl_secattr_to_sid_return
;
2602 ctx_new
.range
.level
[1].cat
.highbit
=
2603 ctx_new
.range
.level
[0].cat
.highbit
;
2604 ctx_new
.range
.level
[1].cat
.node
=
2605 ctx_new
.range
.level
[0].cat
.node
;
2607 ebitmap_init(&ctx_new
.range
.level
[0].cat
);
2608 ebitmap_init(&ctx_new
.range
.level
[1].cat
);
2610 if (mls_context_isvalid(&policydb
, &ctx_new
) != 1)
2611 goto netlbl_secattr_to_sid_return_cleanup
;
2613 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
2615 goto netlbl_secattr_to_sid_return_cleanup
;
2617 security_netlbl_cache_add(secattr
, *sid
);
2619 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2625 netlbl_secattr_to_sid_return
:
2628 netlbl_secattr_to_sid_return_cleanup
:
2629 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2630 goto netlbl_secattr_to_sid_return
;
2634 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2635 * @sid: the SELinux SID
2636 * @secattr: the NetLabel packet security attributes
2639 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2640 * Returns zero on success, negative values on failure.
2643 int security_netlbl_sid_to_secattr(u32 sid
, struct netlbl_lsm_secattr
*secattr
)
2646 struct context
*ctx
;
2648 if (!ss_initialized
)
2652 ctx
= sidtab_search(&sidtab
, sid
);
2654 goto netlbl_sid_to_secattr_failure
;
2655 secattr
->domain
= kstrdup(policydb
.p_type_val_to_name
[ctx
->type
- 1],
2657 secattr
->flags
|= NETLBL_SECATTR_DOMAIN
;
2658 mls_export_netlbl_lvl(ctx
, secattr
);
2659 rc
= mls_export_netlbl_cat(ctx
, secattr
);
2661 goto netlbl_sid_to_secattr_failure
;
2666 netlbl_sid_to_secattr_failure
:
2670 #endif /* CONFIG_NETLABEL */