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
);
421 * Given a sid find if the type has the permissive flag set
423 int security_permissive_sid(u32 sid
)
425 struct context
*context
;
431 context
= sidtab_search(&sidtab
, sid
);
434 type
= context
->type
;
436 * we are intentionally using type here, not type-1, the 0th bit may
437 * someday indicate that we are globally setting permissive in policy.
439 rc
= ebitmap_get_bit(&policydb
.permissive_map
, type
);
445 static int security_validtrans_handle_fail(struct context
*ocontext
,
446 struct context
*ncontext
,
447 struct context
*tcontext
,
450 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
451 u32 olen
, nlen
, tlen
;
453 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
455 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
457 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
459 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
460 "security_validate_transition: denied for"
461 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
462 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
468 if (!selinux_enforcing
)
473 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
476 struct context
*ocontext
;
477 struct context
*ncontext
;
478 struct context
*tcontext
;
479 struct class_datum
*tclass_datum
;
480 struct constraint_node
*constraint
;
489 * Remap extended Netlink classes for old policy versions.
490 * Do this here rather than socket_type_to_security_class()
491 * in case a newer policy version is loaded, allowing sockets
492 * to remain in the correct class.
494 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
495 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
496 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
497 tclass
= SECCLASS_NETLINK_SOCKET
;
499 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
500 printk(KERN_ERR
"security_validate_transition: "
501 "unrecognized class %d\n", tclass
);
505 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
507 ocontext
= sidtab_search(&sidtab
, oldsid
);
509 printk(KERN_ERR
"security_validate_transition: "
510 " unrecognized SID %d\n", oldsid
);
515 ncontext
= sidtab_search(&sidtab
, newsid
);
517 printk(KERN_ERR
"security_validate_transition: "
518 " unrecognized SID %d\n", newsid
);
523 tcontext
= sidtab_search(&sidtab
, tasksid
);
525 printk(KERN_ERR
"security_validate_transition: "
526 " unrecognized SID %d\n", tasksid
);
531 constraint
= tclass_datum
->validatetrans
;
533 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
535 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
539 constraint
= constraint
->next
;
548 * security_compute_av - Compute access vector decisions.
549 * @ssid: source security identifier
550 * @tsid: target security identifier
551 * @tclass: target security class
552 * @requested: requested permissions
553 * @avd: access vector decisions
555 * Compute a set of access vector decisions based on the
556 * SID pair (@ssid, @tsid) for the permissions in @tclass.
557 * Return -%EINVAL if any of the parameters are invalid or %0
558 * if the access vector decisions were computed successfully.
560 int security_compute_av(u32 ssid
,
564 struct av_decision
*avd
)
566 struct context
*scontext
= NULL
, *tcontext
= NULL
;
569 if (!ss_initialized
) {
570 avd
->allowed
= 0xffffffff;
571 avd
->decided
= 0xffffffff;
573 avd
->auditdeny
= 0xffffffff;
574 avd
->seqno
= latest_granting
;
580 scontext
= sidtab_search(&sidtab
, ssid
);
582 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
587 tcontext
= sidtab_search(&sidtab
, tsid
);
589 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
595 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
603 * Write the security context string representation of
604 * the context structure `context' into a dynamically
605 * allocated string of the correct size. Set `*scontext'
606 * to point to this string and set `*scontext_len' to
607 * the length of the string.
609 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
616 /* Compute the size of the context. */
617 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
618 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
619 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
620 *scontext_len
+= mls_compute_context_len(context
);
622 /* Allocate space for the context; caller must free this space. */
623 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
627 *scontext
= scontextp
;
630 * Copy the user name, role name and type name into the context.
632 sprintf(scontextp
, "%s:%s:%s",
633 policydb
.p_user_val_to_name
[context
->user
- 1],
634 policydb
.p_role_val_to_name
[context
->role
- 1],
635 policydb
.p_type_val_to_name
[context
->type
- 1]);
636 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
637 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
638 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
640 mls_sid_to_context(context
, &scontextp
);
647 #include "initial_sid_to_string.h"
649 const char *security_get_initial_sid_context(u32 sid
)
651 if (unlikely(sid
> SECINITSID_NUM
))
653 return initial_sid_to_string
[sid
];
657 * security_sid_to_context - Obtain a context for a given SID.
658 * @sid: security identifier, SID
659 * @scontext: security context
660 * @scontext_len: length in bytes
662 * Write the string representation of the context associated with @sid
663 * into a dynamically allocated string of the correct size. Set @scontext
664 * to point to this string and set @scontext_len to the length of the string.
666 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
668 struct context
*context
;
674 if (!ss_initialized
) {
675 if (sid
<= SECINITSID_NUM
) {
678 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
679 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
684 strcpy(scontextp
, initial_sid_to_string
[sid
]);
685 *scontext
= scontextp
;
688 printk(KERN_ERR
"security_sid_to_context: called before initial "
689 "load_policy on unknown SID %d\n", sid
);
694 context
= sidtab_search(&sidtab
, sid
);
696 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
701 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
709 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
,
710 u32
*sid
, u32 def_sid
, gfp_t gfp_flags
)
713 struct context context
;
714 struct role_datum
*role
;
715 struct type_datum
*typdatum
;
716 struct user_datum
*usrdatum
;
717 char *scontextp
, *p
, oldc
;
720 if (!ss_initialized
) {
723 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
724 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
729 *sid
= SECINITSID_KERNEL
;
734 /* Copy the string so that we can modify the copy as we parse it.
735 The string should already by null terminated, but we append a
736 null suffix to the copy to avoid problems with the existing
737 attr package, which doesn't view the null terminator as part
738 of the attribute value. */
739 scontext2
= kmalloc(scontext_len
+1, gfp_flags
);
744 memcpy(scontext2
, scontext
, scontext_len
);
745 scontext2
[scontext_len
] = 0;
747 context_init(&context
);
752 /* Parse the security context. */
755 scontextp
= (char *) scontext2
;
757 /* Extract the user. */
759 while (*p
&& *p
!= ':')
767 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
771 context
.user
= usrdatum
->value
;
775 while (*p
&& *p
!= ':')
783 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
786 context
.role
= role
->value
;
790 while (*p
&& *p
!= ':')
795 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
799 context
.type
= typdatum
->value
;
801 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
805 if ((p
- scontext2
) < scontext_len
) {
810 /* Check the validity of the new context. */
811 if (!policydb_context_isvalid(&policydb
, &context
)) {
815 /* Obtain the new sid. */
816 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
819 context_destroy(&context
);
826 * security_context_to_sid - Obtain a SID for a given security context.
827 * @scontext: security context
828 * @scontext_len: length in bytes
829 * @sid: security identifier, SID
831 * Obtains a SID associated with the security context that
832 * has the string representation specified by @scontext.
833 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
834 * memory is available, or 0 on success.
836 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
838 return security_context_to_sid_core(scontext
, scontext_len
,
839 sid
, SECSID_NULL
, GFP_KERNEL
);
843 * security_context_to_sid_default - Obtain a SID for a given security context,
844 * falling back to specified default if needed.
846 * @scontext: security context
847 * @scontext_len: length in bytes
848 * @sid: security identifier, SID
849 * @def_sid: default SID to assign on error
851 * Obtains a SID associated with the security context that
852 * has the string representation specified by @scontext.
853 * The default SID is passed to the MLS layer to be used to allow
854 * kernel labeling of the MLS field if the MLS field is not present
855 * (for upgrading to MLS without full relabel).
856 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
857 * memory is available, or 0 on success.
859 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
,
860 u32 def_sid
, gfp_t gfp_flags
)
862 return security_context_to_sid_core(scontext
, scontext_len
,
863 sid
, def_sid
, gfp_flags
);
866 static int compute_sid_handle_invalid_context(
867 struct context
*scontext
,
868 struct context
*tcontext
,
870 struct context
*newcontext
)
872 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
873 u32 slen
, tlen
, nlen
;
875 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
877 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
879 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
881 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
882 "security_compute_sid: invalid context %s"
886 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
891 if (!selinux_enforcing
)
896 static int security_compute_sid(u32 ssid
,
902 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
903 struct role_trans
*roletr
= NULL
;
904 struct avtab_key avkey
;
905 struct avtab_datum
*avdatum
;
906 struct avtab_node
*node
;
909 if (!ss_initialized
) {
911 case SECCLASS_PROCESS
:
921 context_init(&newcontext
);
925 scontext
= sidtab_search(&sidtab
, ssid
);
927 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
932 tcontext
= sidtab_search(&sidtab
, tsid
);
934 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
940 /* Set the user identity. */
942 case AVTAB_TRANSITION
:
944 /* Use the process user identity. */
945 newcontext
.user
= scontext
->user
;
948 /* Use the related object owner. */
949 newcontext
.user
= tcontext
->user
;
953 /* Set the role and type to default values. */
955 case SECCLASS_PROCESS
:
956 /* Use the current role and type of process. */
957 newcontext
.role
= scontext
->role
;
958 newcontext
.type
= scontext
->type
;
961 /* Use the well-defined object role. */
962 newcontext
.role
= OBJECT_R_VAL
;
963 /* Use the type of the related object. */
964 newcontext
.type
= tcontext
->type
;
967 /* Look for a type transition/member/change rule. */
968 avkey
.source_type
= scontext
->type
;
969 avkey
.target_type
= tcontext
->type
;
970 avkey
.target_class
= tclass
;
971 avkey
.specified
= specified
;
972 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
974 /* If no permanent rule, also check for enabled conditional rules */
976 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
977 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
978 if (node
->key
.specified
& AVTAB_ENABLED
) {
979 avdatum
= &node
->datum
;
986 /* Use the type from the type transition/member/change rule. */
987 newcontext
.type
= avdatum
->data
;
990 /* Check for class-specific changes. */
992 case SECCLASS_PROCESS
:
993 if (specified
& AVTAB_TRANSITION
) {
994 /* Look for a role transition rule. */
995 for (roletr
= policydb
.role_tr
; roletr
;
996 roletr
= roletr
->next
) {
997 if (roletr
->role
== scontext
->role
&&
998 roletr
->type
== tcontext
->type
) {
999 /* Use the role transition rule. */
1000 newcontext
.role
= roletr
->new_role
;
1010 /* Set the MLS attributes.
1011 This is done last because it may allocate memory. */
1012 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
1016 /* Check the validity of the context. */
1017 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
1018 rc
= compute_sid_handle_invalid_context(scontext
,
1025 /* Obtain the sid for the context. */
1026 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
1029 context_destroy(&newcontext
);
1035 * security_transition_sid - Compute the SID for a new subject/object.
1036 * @ssid: source security identifier
1037 * @tsid: target security identifier
1038 * @tclass: target security class
1039 * @out_sid: security identifier for new subject/object
1041 * Compute a SID to use for labeling a new subject or object in the
1042 * class @tclass based on a SID pair (@ssid, @tsid).
1043 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1044 * if insufficient memory is available, or %0 if the new SID was
1045 * computed successfully.
1047 int security_transition_sid(u32 ssid
,
1052 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
1056 * security_member_sid - Compute the SID for member selection.
1057 * @ssid: source security identifier
1058 * @tsid: target security identifier
1059 * @tclass: target security class
1060 * @out_sid: security identifier for selected member
1062 * Compute a SID to use when selecting a member of a polyinstantiated
1063 * object of class @tclass based on a SID pair (@ssid, @tsid).
1064 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1065 * if insufficient memory is available, or %0 if the SID was
1066 * computed successfully.
1068 int security_member_sid(u32 ssid
,
1073 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
1077 * security_change_sid - Compute the SID for object relabeling.
1078 * @ssid: source security identifier
1079 * @tsid: target security identifier
1080 * @tclass: target security class
1081 * @out_sid: security identifier for selected member
1083 * Compute a SID to use for relabeling an object of class @tclass
1084 * based on a SID pair (@ssid, @tsid).
1085 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1086 * if insufficient memory is available, or %0 if the SID was
1087 * computed successfully.
1089 int security_change_sid(u32 ssid
,
1094 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1098 * Verify that each kernel class that is defined in the
1101 static int validate_classes(struct policydb
*p
)
1104 struct class_datum
*cladatum
;
1105 struct perm_datum
*perdatum
;
1106 u32 nprim
, tmp
, common_pts_len
, perm_val
, pol_val
;
1108 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
1109 const char *def_class
, *def_perm
, *pol_class
;
1110 struct symtab
*perms
;
1112 if (p
->allow_unknown
) {
1113 u32 num_classes
= kdefs
->cts_len
;
1114 p
->undefined_perms
= kcalloc(num_classes
, sizeof(u32
), GFP_KERNEL
);
1115 if (!p
->undefined_perms
)
1119 for (i
= 1; i
< kdefs
->cts_len
; i
++) {
1120 def_class
= kdefs
->class_to_string
[i
];
1123 if (i
> p
->p_classes
.nprim
) {
1125 "SELinux: class %s not defined in policy\n",
1127 if (p
->reject_unknown
)
1129 if (p
->allow_unknown
)
1130 p
->undefined_perms
[i
-1] = ~0U;
1133 pol_class
= p
->p_class_val_to_name
[i
-1];
1134 if (strcmp(pol_class
, def_class
)) {
1136 "SELinux: class %d is incorrect, found %s but should be %s\n",
1137 i
, pol_class
, def_class
);
1141 for (i
= 0; i
< kdefs
->av_pts_len
; i
++) {
1142 class_val
= kdefs
->av_perm_to_string
[i
].tclass
;
1143 perm_val
= kdefs
->av_perm_to_string
[i
].value
;
1144 def_perm
= kdefs
->av_perm_to_string
[i
].name
;
1145 if (class_val
> p
->p_classes
.nprim
)
1147 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1148 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1150 perms
= &cladatum
->permissions
;
1151 nprim
= 1 << (perms
->nprim
- 1);
1152 if (perm_val
> nprim
) {
1154 "SELinux: permission %s in class %s not defined in policy\n",
1155 def_perm
, pol_class
);
1156 if (p
->reject_unknown
)
1158 if (p
->allow_unknown
)
1159 p
->undefined_perms
[class_val
-1] |= perm_val
;
1162 perdatum
= hashtab_search(perms
->table
, def_perm
);
1163 if (perdatum
== NULL
) {
1165 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1166 def_perm
, pol_class
);
1169 pol_val
= 1 << (perdatum
->value
- 1);
1170 if (pol_val
!= perm_val
) {
1172 "SELinux: permission %s in class %s has incorrect value\n",
1173 def_perm
, pol_class
);
1177 for (i
= 0; i
< kdefs
->av_inherit_len
; i
++) {
1178 class_val
= kdefs
->av_inherit
[i
].tclass
;
1179 if (class_val
> p
->p_classes
.nprim
)
1181 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1182 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1184 if (!cladatum
->comdatum
) {
1186 "SELinux: class %s should have an inherits clause but does not\n",
1190 tmp
= kdefs
->av_inherit
[i
].common_base
;
1192 while (!(tmp
& 0x01)) {
1196 perms
= &cladatum
->comdatum
->permissions
;
1197 for (j
= 0; j
< common_pts_len
; j
++) {
1198 def_perm
= kdefs
->av_inherit
[i
].common_pts
[j
];
1199 if (j
>= perms
->nprim
) {
1201 "SELinux: permission %s in class %s not defined in policy\n",
1202 def_perm
, pol_class
);
1203 if (p
->reject_unknown
)
1205 if (p
->allow_unknown
)
1206 p
->undefined_perms
[class_val
-1] |= (1 << j
);
1209 perdatum
= hashtab_search(perms
->table
, def_perm
);
1210 if (perdatum
== NULL
) {
1212 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1213 def_perm
, pol_class
);
1216 if (perdatum
->value
!= j
+ 1) {
1218 "SELinux: permission %s in class %s has incorrect value\n",
1219 def_perm
, pol_class
);
1227 /* Clone the SID into the new SID table. */
1228 static int clone_sid(u32 sid
,
1229 struct context
*context
,
1232 struct sidtab
*s
= arg
;
1234 return sidtab_insert(s
, sid
, context
);
1237 static inline int convert_context_handle_invalid_context(struct context
*context
)
1241 if (selinux_enforcing
) {
1247 context_struct_to_string(context
, &s
, &len
);
1248 printk(KERN_ERR
"SELinux: context %s is invalid\n", s
);
1254 struct convert_context_args
{
1255 struct policydb
*oldp
;
1256 struct policydb
*newp
;
1260 * Convert the values in the security context
1261 * structure `c' from the values specified
1262 * in the policy `p->oldp' to the values specified
1263 * in the policy `p->newp'. Verify that the
1264 * context is valid under the new policy.
1266 static int convert_context(u32 key
,
1270 struct convert_context_args
*args
;
1271 struct context oldc
;
1272 struct role_datum
*role
;
1273 struct type_datum
*typdatum
;
1274 struct user_datum
*usrdatum
;
1281 rc
= context_cpy(&oldc
, c
);
1287 /* Convert the user. */
1288 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1289 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1293 c
->user
= usrdatum
->value
;
1295 /* Convert the role. */
1296 role
= hashtab_search(args
->newp
->p_roles
.table
,
1297 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1301 c
->role
= role
->value
;
1303 /* Convert the type. */
1304 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1305 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1309 c
->type
= typdatum
->value
;
1311 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1315 /* Check the validity of the new context. */
1316 if (!policydb_context_isvalid(args
->newp
, c
)) {
1317 rc
= convert_context_handle_invalid_context(&oldc
);
1322 context_destroy(&oldc
);
1326 context_struct_to_string(&oldc
, &s
, &len
);
1327 context_destroy(&oldc
);
1328 printk(KERN_ERR
"SELinux: invalidating context %s\n", s
);
1333 static void security_load_policycaps(void)
1335 selinux_policycap_netpeer
= ebitmap_get_bit(&policydb
.policycaps
,
1336 POLICYDB_CAPABILITY_NETPEER
);
1337 selinux_policycap_openperm
= ebitmap_get_bit(&policydb
.policycaps
,
1338 POLICYDB_CAPABILITY_OPENPERM
);
1341 extern void selinux_complete_init(void);
1342 static int security_preserve_bools(struct policydb
*p
);
1345 * security_load_policy - Load a security policy configuration.
1346 * @data: binary policy data
1347 * @len: length of data in bytes
1349 * Load a new set of security policy configuration data,
1350 * validate it and convert the SID table as necessary.
1351 * This function will flush the access vector cache after
1352 * loading the new policy.
1354 int security_load_policy(void *data
, size_t len
)
1356 struct policydb oldpolicydb
, newpolicydb
;
1357 struct sidtab oldsidtab
, newsidtab
;
1358 struct convert_context_args args
;
1361 struct policy_file file
= { data
, len
}, *fp
= &file
;
1365 if (!ss_initialized
) {
1367 if (policydb_read(&policydb
, fp
)) {
1369 avtab_cache_destroy();
1372 if (policydb_load_isids(&policydb
, &sidtab
)) {
1374 policydb_destroy(&policydb
);
1375 avtab_cache_destroy();
1378 /* Verify that the kernel defined classes are correct. */
1379 if (validate_classes(&policydb
)) {
1381 "SELinux: the definition of a class is incorrect\n");
1383 sidtab_destroy(&sidtab
);
1384 policydb_destroy(&policydb
);
1385 avtab_cache_destroy();
1388 security_load_policycaps();
1389 policydb_loaded_version
= policydb
.policyvers
;
1391 seqno
= ++latest_granting
;
1393 selinux_complete_init();
1394 avc_ss_reset(seqno
);
1395 selnl_notify_policyload(seqno
);
1396 selinux_netlbl_cache_invalidate();
1397 selinux_xfrm_notify_policyload();
1402 sidtab_hash_eval(&sidtab
, "sids");
1405 if (policydb_read(&newpolicydb
, fp
)) {
1410 sidtab_init(&newsidtab
);
1412 /* Verify that the kernel defined classes are correct. */
1413 if (validate_classes(&newpolicydb
)) {
1415 "SELinux: the definition of a class is incorrect\n");
1420 rc
= security_preserve_bools(&newpolicydb
);
1422 printk(KERN_ERR
"SELinux: unable to preserve booleans\n");
1426 /* Clone the SID table. */
1427 sidtab_shutdown(&sidtab
);
1428 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1433 /* Convert the internal representations of contexts
1434 in the new SID table and remove invalid SIDs. */
1435 args
.oldp
= &policydb
;
1436 args
.newp
= &newpolicydb
;
1437 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1439 /* Save the old policydb and SID table to free later. */
1440 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1441 sidtab_set(&oldsidtab
, &sidtab
);
1443 /* Install the new policydb and SID table. */
1445 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1446 sidtab_set(&sidtab
, &newsidtab
);
1447 security_load_policycaps();
1448 seqno
= ++latest_granting
;
1449 policydb_loaded_version
= policydb
.policyvers
;
1453 /* Free the old policydb and SID table. */
1454 policydb_destroy(&oldpolicydb
);
1455 sidtab_destroy(&oldsidtab
);
1457 avc_ss_reset(seqno
);
1458 selnl_notify_policyload(seqno
);
1459 selinux_netlbl_cache_invalidate();
1460 selinux_xfrm_notify_policyload();
1466 sidtab_destroy(&newsidtab
);
1467 policydb_destroy(&newpolicydb
);
1473 * security_port_sid - Obtain the SID for a port.
1474 * @domain: communication domain aka address family
1475 * @type: socket type
1476 * @protocol: protocol number
1477 * @port: port number
1478 * @out_sid: security identifier
1480 int security_port_sid(u16 domain
,
1491 c
= policydb
.ocontexts
[OCON_PORT
];
1493 if (c
->u
.port
.protocol
== protocol
&&
1494 c
->u
.port
.low_port
<= port
&&
1495 c
->u
.port
.high_port
>= port
)
1502 rc
= sidtab_context_to_sid(&sidtab
,
1508 *out_sid
= c
->sid
[0];
1510 *out_sid
= SECINITSID_PORT
;
1519 * security_netif_sid - Obtain the SID for a network interface.
1520 * @name: interface name
1521 * @if_sid: interface SID
1523 int security_netif_sid(char *name
, u32
*if_sid
)
1530 c
= policydb
.ocontexts
[OCON_NETIF
];
1532 if (strcmp(name
, c
->u
.name
) == 0)
1538 if (!c
->sid
[0] || !c
->sid
[1]) {
1539 rc
= sidtab_context_to_sid(&sidtab
,
1544 rc
= sidtab_context_to_sid(&sidtab
,
1550 *if_sid
= c
->sid
[0];
1552 *if_sid
= SECINITSID_NETIF
;
1559 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1563 for(i
= 0; i
< 4; i
++)
1564 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1573 * security_node_sid - Obtain the SID for a node (host).
1574 * @domain: communication domain aka address family
1576 * @addrlen: address length in bytes
1577 * @out_sid: security identifier
1579 int security_node_sid(u16 domain
,
1593 if (addrlen
!= sizeof(u32
)) {
1598 addr
= *((u32
*)addrp
);
1600 c
= policydb
.ocontexts
[OCON_NODE
];
1602 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1610 if (addrlen
!= sizeof(u64
) * 2) {
1614 c
= policydb
.ocontexts
[OCON_NODE6
];
1616 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1624 *out_sid
= SECINITSID_NODE
;
1630 rc
= sidtab_context_to_sid(&sidtab
,
1636 *out_sid
= c
->sid
[0];
1638 *out_sid
= SECINITSID_NODE
;
1649 * security_get_user_sids - Obtain reachable SIDs for a user.
1650 * @fromsid: starting SID
1651 * @username: username
1652 * @sids: array of reachable SIDs for user
1653 * @nel: number of elements in @sids
1655 * Generate the set of SIDs for legal security contexts
1656 * for a given user that can be reached by @fromsid.
1657 * Set *@sids to point to a dynamically allocated
1658 * array containing the set of SIDs. Set *@nel to the
1659 * number of elements in the array.
1662 int security_get_user_sids(u32 fromsid
,
1667 struct context
*fromcon
, usercon
;
1668 u32
*mysids
= NULL
, *mysids2
, sid
;
1669 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1670 struct user_datum
*user
;
1671 struct role_datum
*role
;
1672 struct ebitmap_node
*rnode
, *tnode
;
1678 if (!ss_initialized
)
1683 fromcon
= sidtab_search(&sidtab
, fromsid
);
1689 user
= hashtab_search(policydb
.p_users
.table
, username
);
1694 usercon
.user
= user
->value
;
1696 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1702 ebitmap_for_each_positive_bit(&user
->roles
, rnode
, i
) {
1703 role
= policydb
.role_val_to_struct
[i
];
1705 ebitmap_for_each_positive_bit(&role
->types
, tnode
, j
) {
1708 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1711 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1714 if (mynel
< maxnel
) {
1715 mysids
[mynel
++] = sid
;
1718 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1723 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1726 mysids
[mynel
++] = sid
;
1738 mysids2
= kcalloc(mynel
, sizeof(*mysids2
), GFP_KERNEL
);
1744 for (i
= 0, j
= 0; i
< mynel
; i
++) {
1745 rc
= avc_has_perm_noaudit(fromsid
, mysids
[i
],
1747 PROCESS__TRANSITION
, AVC_STRICT
,
1750 mysids2
[j
++] = mysids
[i
];
1762 * security_genfs_sid - Obtain a SID for a file in a filesystem
1763 * @fstype: filesystem type
1764 * @path: path from root of mount
1765 * @sclass: file security class
1766 * @sid: SID for path
1768 * Obtain a SID to use for a file in a filesystem that
1769 * cannot support xattr or use a fixed labeling behavior like
1770 * transition SIDs or task SIDs.
1772 int security_genfs_sid(const char *fstype
,
1778 struct genfs
*genfs
;
1780 int rc
= 0, cmp
= 0;
1782 while (path
[0] == '/' && path
[1] == '/')
1787 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1788 cmp
= strcmp(fstype
, genfs
->fstype
);
1793 if (!genfs
|| cmp
) {
1794 *sid
= SECINITSID_UNLABELED
;
1799 for (c
= genfs
->head
; c
; c
= c
->next
) {
1800 len
= strlen(c
->u
.name
);
1801 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1802 (strncmp(c
->u
.name
, path
, len
) == 0))
1807 *sid
= SECINITSID_UNLABELED
;
1813 rc
= sidtab_context_to_sid(&sidtab
,
1827 * security_fs_use - Determine how to handle labeling for a filesystem.
1828 * @fstype: filesystem type
1829 * @behavior: labeling behavior
1830 * @sid: SID for filesystem (superblock)
1832 int security_fs_use(
1834 unsigned int *behavior
,
1842 c
= policydb
.ocontexts
[OCON_FSUSE
];
1844 if (strcmp(fstype
, c
->u
.name
) == 0)
1850 *behavior
= c
->v
.behavior
;
1852 rc
= sidtab_context_to_sid(&sidtab
,
1860 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1862 *behavior
= SECURITY_FS_USE_NONE
;
1865 *behavior
= SECURITY_FS_USE_GENFS
;
1874 int security_get_bools(int *len
, char ***names
, int **values
)
1876 int i
, rc
= -ENOMEM
;
1882 *len
= policydb
.p_bools
.nprim
;
1888 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1892 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1896 for (i
= 0; i
< *len
; i
++) {
1898 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1899 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1900 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1903 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1904 (*names
)[i
][name_len
- 1] = 0;
1912 for (i
= 0; i
< *len
; i
++)
1920 int security_set_bools(int len
, int *values
)
1923 int lenp
, seqno
= 0;
1924 struct cond_node
*cur
;
1928 lenp
= policydb
.p_bools
.nprim
;
1934 for (i
= 0; i
< len
; i
++) {
1935 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
1936 audit_log(current
->audit_context
, GFP_ATOMIC
,
1937 AUDIT_MAC_CONFIG_CHANGE
,
1938 "bool=%s val=%d old_val=%d auid=%u ses=%u",
1939 policydb
.p_bool_val_to_name
[i
],
1941 policydb
.bool_val_to_struct
[i
]->state
,
1942 audit_get_loginuid(current
),
1943 audit_get_sessionid(current
));
1946 policydb
.bool_val_to_struct
[i
]->state
= 1;
1948 policydb
.bool_val_to_struct
[i
]->state
= 0;
1952 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1953 rc
= evaluate_cond_node(&policydb
, cur
);
1958 seqno
= ++latest_granting
;
1963 avc_ss_reset(seqno
);
1964 selnl_notify_policyload(seqno
);
1965 selinux_xfrm_notify_policyload();
1970 int security_get_bool_value(int bool)
1977 len
= policydb
.p_bools
.nprim
;
1983 rc
= policydb
.bool_val_to_struct
[bool]->state
;
1989 static int security_preserve_bools(struct policydb
*p
)
1991 int rc
, nbools
= 0, *bvalues
= NULL
, i
;
1992 char **bnames
= NULL
;
1993 struct cond_bool_datum
*booldatum
;
1994 struct cond_node
*cur
;
1996 rc
= security_get_bools(&nbools
, &bnames
, &bvalues
);
1999 for (i
= 0; i
< nbools
; i
++) {
2000 booldatum
= hashtab_search(p
->p_bools
.table
, bnames
[i
]);
2002 booldatum
->state
= bvalues
[i
];
2004 for (cur
= p
->cond_list
; cur
!= NULL
; cur
= cur
->next
) {
2005 rc
= evaluate_cond_node(p
, cur
);
2012 for (i
= 0; i
< nbools
; i
++)
2021 * security_sid_mls_copy() - computes a new sid based on the given
2022 * sid and the mls portion of mls_sid.
2024 int security_sid_mls_copy(u32 sid
, u32 mls_sid
, u32
*new_sid
)
2026 struct context
*context1
;
2027 struct context
*context2
;
2028 struct context newcon
;
2033 if (!ss_initialized
|| !selinux_mls_enabled
) {
2038 context_init(&newcon
);
2041 context1
= sidtab_search(&sidtab
, sid
);
2043 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2049 context2
= sidtab_search(&sidtab
, mls_sid
);
2051 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2057 newcon
.user
= context1
->user
;
2058 newcon
.role
= context1
->role
;
2059 newcon
.type
= context1
->type
;
2060 rc
= mls_context_cpy(&newcon
, context2
);
2064 /* Check the validity of the new context. */
2065 if (!policydb_context_isvalid(&policydb
, &newcon
)) {
2066 rc
= convert_context_handle_invalid_context(&newcon
);
2071 rc
= sidtab_context_to_sid(&sidtab
, &newcon
, new_sid
);
2075 if (!context_struct_to_string(&newcon
, &s
, &len
)) {
2076 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2077 "security_sid_mls_copy: invalid context %s", s
);
2083 context_destroy(&newcon
);
2089 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2090 * @nlbl_sid: NetLabel SID
2091 * @nlbl_type: NetLabel labeling protocol type
2092 * @xfrm_sid: XFRM SID
2095 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2096 * resolved into a single SID it is returned via @peer_sid and the function
2097 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2098 * returns a negative value. A table summarizing the behavior is below:
2100 * | function return | @sid
2101 * ------------------------------+-----------------+-----------------
2102 * no peer labels | 0 | SECSID_NULL
2103 * single peer label | 0 | <peer_label>
2104 * multiple, consistent labels | 0 | <peer_label>
2105 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2108 int security_net_peersid_resolve(u32 nlbl_sid
, u32 nlbl_type
,
2113 struct context
*nlbl_ctx
;
2114 struct context
*xfrm_ctx
;
2116 /* handle the common (which also happens to be the set of easy) cases
2117 * right away, these two if statements catch everything involving a
2118 * single or absent peer SID/label */
2119 if (xfrm_sid
== SECSID_NULL
) {
2120 *peer_sid
= nlbl_sid
;
2123 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2124 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2126 if (nlbl_sid
== SECSID_NULL
|| nlbl_type
== NETLBL_NLTYPE_UNLABELED
) {
2127 *peer_sid
= xfrm_sid
;
2131 /* we don't need to check ss_initialized here since the only way both
2132 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2133 * security server was initialized and ss_initialized was true */
2134 if (!selinux_mls_enabled
) {
2135 *peer_sid
= SECSID_NULL
;
2141 nlbl_ctx
= sidtab_search(&sidtab
, nlbl_sid
);
2144 "security_sid_mls_cmp: unrecognized SID %d\n",
2149 xfrm_ctx
= sidtab_search(&sidtab
, xfrm_sid
);
2152 "security_sid_mls_cmp: unrecognized SID %d\n",
2157 rc
= (mls_context_cmp(nlbl_ctx
, xfrm_ctx
) ? 0 : -EACCES
);
2162 /* at present NetLabel SIDs/labels really only carry MLS
2163 * information so if the MLS portion of the NetLabel SID
2164 * matches the MLS portion of the labeled XFRM SID/label
2165 * then pass along the XFRM SID as it is the most
2167 *peer_sid
= xfrm_sid
;
2169 *peer_sid
= SECSID_NULL
;
2173 static int get_classes_callback(void *k
, void *d
, void *args
)
2175 struct class_datum
*datum
= d
;
2176 char *name
= k
, **classes
= args
;
2177 int value
= datum
->value
- 1;
2179 classes
[value
] = kstrdup(name
, GFP_ATOMIC
);
2180 if (!classes
[value
])
2186 int security_get_classes(char ***classes
, int *nclasses
)
2192 *nclasses
= policydb
.p_classes
.nprim
;
2193 *classes
= kcalloc(*nclasses
, sizeof(*classes
), GFP_ATOMIC
);
2197 rc
= hashtab_map(policydb
.p_classes
.table
, get_classes_callback
,
2201 for (i
= 0; i
< *nclasses
; i
++)
2202 kfree((*classes
)[i
]);
2211 static int get_permissions_callback(void *k
, void *d
, void *args
)
2213 struct perm_datum
*datum
= d
;
2214 char *name
= k
, **perms
= args
;
2215 int value
= datum
->value
- 1;
2217 perms
[value
] = kstrdup(name
, GFP_ATOMIC
);
2224 int security_get_permissions(char *class, char ***perms
, int *nperms
)
2226 int rc
= -ENOMEM
, i
;
2227 struct class_datum
*match
;
2231 match
= hashtab_search(policydb
.p_classes
.table
, class);
2233 printk(KERN_ERR
"%s: unrecognized class %s\n",
2239 *nperms
= match
->permissions
.nprim
;
2240 *perms
= kcalloc(*nperms
, sizeof(*perms
), GFP_ATOMIC
);
2244 if (match
->comdatum
) {
2245 rc
= hashtab_map(match
->comdatum
->permissions
.table
,
2246 get_permissions_callback
, *perms
);
2251 rc
= hashtab_map(match
->permissions
.table
, get_permissions_callback
,
2262 for (i
= 0; i
< *nperms
; i
++)
2268 int security_get_reject_unknown(void)
2270 return policydb
.reject_unknown
;
2273 int security_get_allow_unknown(void)
2275 return policydb
.allow_unknown
;
2279 * security_policycap_supported - Check for a specific policy capability
2280 * @req_cap: capability
2283 * This function queries the currently loaded policy to see if it supports the
2284 * capability specified by @req_cap. Returns true (1) if the capability is
2285 * supported, false (0) if it isn't supported.
2288 int security_policycap_supported(unsigned int req_cap
)
2293 rc
= ebitmap_get_bit(&policydb
.policycaps
, req_cap
);
2299 struct selinux_audit_rule
{
2301 struct context au_ctxt
;
2304 void selinux_audit_rule_free(struct selinux_audit_rule
*rule
)
2307 context_destroy(&rule
->au_ctxt
);
2312 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
,
2313 struct selinux_audit_rule
**rule
)
2315 struct selinux_audit_rule
*tmprule
;
2316 struct role_datum
*roledatum
;
2317 struct type_datum
*typedatum
;
2318 struct user_datum
*userdatum
;
2323 if (!ss_initialized
)
2327 case AUDIT_SUBJ_USER
:
2328 case AUDIT_SUBJ_ROLE
:
2329 case AUDIT_SUBJ_TYPE
:
2330 case AUDIT_OBJ_USER
:
2331 case AUDIT_OBJ_ROLE
:
2332 case AUDIT_OBJ_TYPE
:
2333 /* only 'equals' and 'not equals' fit user, role, and type */
2334 if (op
!= AUDIT_EQUAL
&& op
!= AUDIT_NOT_EQUAL
)
2337 case AUDIT_SUBJ_SEN
:
2338 case AUDIT_SUBJ_CLR
:
2339 case AUDIT_OBJ_LEV_LOW
:
2340 case AUDIT_OBJ_LEV_HIGH
:
2341 /* we do not allow a range, indicated by the presense of '-' */
2342 if (strchr(rulestr
, '-'))
2346 /* only the above fields are valid */
2350 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
2354 context_init(&tmprule
->au_ctxt
);
2358 tmprule
->au_seqno
= latest_granting
;
2361 case AUDIT_SUBJ_USER
:
2362 case AUDIT_OBJ_USER
:
2363 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
2367 tmprule
->au_ctxt
.user
= userdatum
->value
;
2369 case AUDIT_SUBJ_ROLE
:
2370 case AUDIT_OBJ_ROLE
:
2371 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
2375 tmprule
->au_ctxt
.role
= roledatum
->value
;
2377 case AUDIT_SUBJ_TYPE
:
2378 case AUDIT_OBJ_TYPE
:
2379 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
2383 tmprule
->au_ctxt
.type
= typedatum
->value
;
2385 case AUDIT_SUBJ_SEN
:
2386 case AUDIT_SUBJ_CLR
:
2387 case AUDIT_OBJ_LEV_LOW
:
2388 case AUDIT_OBJ_LEV_HIGH
:
2389 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
2396 selinux_audit_rule_free(tmprule
);
2405 int selinux_audit_rule_match(u32 sid
, u32 field
, u32 op
,
2406 struct selinux_audit_rule
*rule
,
2407 struct audit_context
*actx
)
2409 struct context
*ctxt
;
2410 struct mls_level
*level
;
2414 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2415 "selinux_audit_rule_match: missing rule\n");
2421 if (rule
->au_seqno
< latest_granting
) {
2422 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2423 "selinux_audit_rule_match: stale rule\n");
2428 ctxt
= sidtab_search(&sidtab
, sid
);
2430 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2431 "selinux_audit_rule_match: unrecognized SID %d\n",
2437 /* a field/op pair that is not caught here will simply fall through
2440 case AUDIT_SUBJ_USER
:
2441 case AUDIT_OBJ_USER
:
2444 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
2446 case AUDIT_NOT_EQUAL
:
2447 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
2451 case AUDIT_SUBJ_ROLE
:
2452 case AUDIT_OBJ_ROLE
:
2455 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
2457 case AUDIT_NOT_EQUAL
:
2458 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
2462 case AUDIT_SUBJ_TYPE
:
2463 case AUDIT_OBJ_TYPE
:
2466 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
2468 case AUDIT_NOT_EQUAL
:
2469 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
2473 case AUDIT_SUBJ_SEN
:
2474 case AUDIT_SUBJ_CLR
:
2475 case AUDIT_OBJ_LEV_LOW
:
2476 case AUDIT_OBJ_LEV_HIGH
:
2477 level
= ((field
== AUDIT_SUBJ_SEN
||
2478 field
== AUDIT_OBJ_LEV_LOW
) ?
2479 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
2482 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2485 case AUDIT_NOT_EQUAL
:
2486 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2489 case AUDIT_LESS_THAN
:
2490 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2492 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2495 case AUDIT_LESS_THAN_OR_EQUAL
:
2496 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2499 case AUDIT_GREATER_THAN
:
2500 match
= (mls_level_dom(level
,
2501 &rule
->au_ctxt
.range
.level
[0]) &&
2502 !mls_level_eq(level
,
2503 &rule
->au_ctxt
.range
.level
[0]));
2505 case AUDIT_GREATER_THAN_OR_EQUAL
:
2506 match
= mls_level_dom(level
,
2507 &rule
->au_ctxt
.range
.level
[0]);
2517 static int (*aurule_callback
)(void) = NULL
;
2519 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2520 u16
class, u32 perms
, u32
*retained
)
2524 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2525 err
= aurule_callback();
2529 static int __init
aurule_init(void)
2533 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2534 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2536 panic("avc_add_callback() failed, error %d\n", err
);
2540 __initcall(aurule_init
);
2542 void selinux_audit_set_callback(int (*callback
)(void))
2544 aurule_callback
= callback
;
2547 #ifdef CONFIG_NETLABEL
2549 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2550 * @secattr: the NetLabel packet security attributes
2551 * @sid: the SELinux SID
2554 * Attempt to cache the context in @ctx, which was derived from the packet in
2555 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2556 * already been initialized.
2559 static void security_netlbl_cache_add(struct netlbl_lsm_secattr
*secattr
,
2564 sid_cache
= kmalloc(sizeof(*sid_cache
), GFP_ATOMIC
);
2565 if (sid_cache
== NULL
)
2567 secattr
->cache
= netlbl_secattr_cache_alloc(GFP_ATOMIC
);
2568 if (secattr
->cache
== NULL
) {
2574 secattr
->cache
->free
= kfree
;
2575 secattr
->cache
->data
= sid_cache
;
2576 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
2580 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2581 * @secattr: the NetLabel packet security attributes
2582 * @sid: the SELinux SID
2585 * Convert the given NetLabel security attributes in @secattr into a
2586 * SELinux SID. If the @secattr field does not contain a full SELinux
2587 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2588 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2589 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2590 * conversion for future lookups. Returns zero on success, negative values on
2594 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr
*secattr
,
2598 struct context
*ctx
;
2599 struct context ctx_new
;
2601 if (!ss_initialized
) {
2608 if (secattr
->flags
& NETLBL_SECATTR_CACHE
) {
2609 *sid
= *(u32
*)secattr
->cache
->data
;
2611 } else if (secattr
->flags
& NETLBL_SECATTR_SECID
) {
2612 *sid
= secattr
->attr
.secid
;
2614 } else if (secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) {
2615 ctx
= sidtab_search(&sidtab
, SECINITSID_NETMSG
);
2617 goto netlbl_secattr_to_sid_return
;
2619 ctx_new
.user
= ctx
->user
;
2620 ctx_new
.role
= ctx
->role
;
2621 ctx_new
.type
= ctx
->type
;
2622 mls_import_netlbl_lvl(&ctx_new
, secattr
);
2623 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
2624 if (ebitmap_netlbl_import(&ctx_new
.range
.level
[0].cat
,
2625 secattr
->attr
.mls
.cat
) != 0)
2626 goto netlbl_secattr_to_sid_return
;
2627 ctx_new
.range
.level
[1].cat
.highbit
=
2628 ctx_new
.range
.level
[0].cat
.highbit
;
2629 ctx_new
.range
.level
[1].cat
.node
=
2630 ctx_new
.range
.level
[0].cat
.node
;
2632 ebitmap_init(&ctx_new
.range
.level
[0].cat
);
2633 ebitmap_init(&ctx_new
.range
.level
[1].cat
);
2635 if (mls_context_isvalid(&policydb
, &ctx_new
) != 1)
2636 goto netlbl_secattr_to_sid_return_cleanup
;
2638 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
2640 goto netlbl_secattr_to_sid_return_cleanup
;
2642 security_netlbl_cache_add(secattr
, *sid
);
2644 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2650 netlbl_secattr_to_sid_return
:
2653 netlbl_secattr_to_sid_return_cleanup
:
2654 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2655 goto netlbl_secattr_to_sid_return
;
2659 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2660 * @sid: the SELinux SID
2661 * @secattr: the NetLabel packet security attributes
2664 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2665 * Returns zero on success, negative values on failure.
2668 int security_netlbl_sid_to_secattr(u32 sid
, struct netlbl_lsm_secattr
*secattr
)
2671 struct context
*ctx
;
2673 if (!ss_initialized
)
2677 ctx
= sidtab_search(&sidtab
, sid
);
2679 goto netlbl_sid_to_secattr_failure
;
2680 secattr
->domain
= kstrdup(policydb
.p_type_val_to_name
[ctx
->type
- 1],
2682 secattr
->flags
|= NETLBL_SECATTR_DOMAIN
;
2683 mls_export_netlbl_lvl(ctx
, secattr
);
2684 rc
= mls_export_netlbl_cat(ctx
, secattr
);
2686 goto netlbl_sid_to_secattr_failure
;
2691 netlbl_sid_to_secattr_failure
:
2695 #endif /* CONFIG_NETLABEL */