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 <linux/selinux.h>
44 #include <net/netlabel.h>
54 #include "conditional.h"
61 extern void selnl_notify_policyload(u32 seqno
);
62 unsigned int policydb_loaded_version
;
64 int selinux_policycap_netpeer
;
65 int selinux_policycap_openperm
;
68 * This is declared in avc.c
70 extern const struct selinux_class_perm selinux_class_perm
;
72 static DEFINE_RWLOCK(policy_rwlock
);
73 #define POLICY_RDLOCK read_lock(&policy_rwlock)
74 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
75 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
76 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
78 static DEFINE_MUTEX(load_mutex
);
79 #define LOAD_LOCK mutex_lock(&load_mutex)
80 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
82 static struct sidtab sidtab
;
83 struct policydb policydb
;
84 int ss_initialized
= 0;
87 * The largest sequence number that has been used when
88 * providing an access decision to the access vector cache.
89 * The sequence number only changes when a policy change
92 static u32 latest_granting
= 0;
94 /* Forward declaration. */
95 static int context_struct_to_string(struct context
*context
, char **scontext
,
99 * Return the boolean value of a constraint expression
100 * when it is applied to the specified source and target
103 * xcontext is a special beast... It is used by the validatetrans rules
104 * only. For these rules, scontext is the context before the transition,
105 * tcontext is the context after the transition, and xcontext is the context
106 * of the process performing the transition. All other callers of
107 * constraint_expr_eval should pass in NULL for xcontext.
109 static int constraint_expr_eval(struct context
*scontext
,
110 struct context
*tcontext
,
111 struct context
*xcontext
,
112 struct constraint_expr
*cexpr
)
116 struct role_datum
*r1
, *r2
;
117 struct mls_level
*l1
, *l2
;
118 struct constraint_expr
*e
;
119 int s
[CEXPR_MAXDEPTH
];
122 for (e
= cexpr
; e
; e
= e
->next
) {
123 switch (e
->expr_type
) {
139 if (sp
== (CEXPR_MAXDEPTH
-1))
143 val1
= scontext
->user
;
144 val2
= tcontext
->user
;
147 val1
= scontext
->type
;
148 val2
= tcontext
->type
;
151 val1
= scontext
->role
;
152 val2
= tcontext
->role
;
153 r1
= policydb
.role_val_to_struct
[val1
- 1];
154 r2
= policydb
.role_val_to_struct
[val2
- 1];
157 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
161 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
165 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
167 !ebitmap_get_bit(&r2
->dominates
,
175 l1
= &(scontext
->range
.level
[0]);
176 l2
= &(tcontext
->range
.level
[0]);
179 l1
= &(scontext
->range
.level
[0]);
180 l2
= &(tcontext
->range
.level
[1]);
183 l1
= &(scontext
->range
.level
[1]);
184 l2
= &(tcontext
->range
.level
[0]);
187 l1
= &(scontext
->range
.level
[1]);
188 l2
= &(tcontext
->range
.level
[1]);
191 l1
= &(scontext
->range
.level
[0]);
192 l2
= &(scontext
->range
.level
[1]);
195 l1
= &(tcontext
->range
.level
[0]);
196 l2
= &(tcontext
->range
.level
[1]);
201 s
[++sp
] = mls_level_eq(l1
, l2
);
204 s
[++sp
] = !mls_level_eq(l1
, l2
);
207 s
[++sp
] = mls_level_dom(l1
, l2
);
210 s
[++sp
] = mls_level_dom(l2
, l1
);
213 s
[++sp
] = mls_level_incomp(l2
, l1
);
227 s
[++sp
] = (val1
== val2
);
230 s
[++sp
] = (val1
!= val2
);
238 if (sp
== (CEXPR_MAXDEPTH
-1))
241 if (e
->attr
& CEXPR_TARGET
)
243 else if (e
->attr
& CEXPR_XTARGET
) {
250 if (e
->attr
& CEXPR_USER
)
252 else if (e
->attr
& CEXPR_ROLE
)
254 else if (e
->attr
& CEXPR_TYPE
)
263 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
266 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
284 * Compute access vectors based on a context structure pair for
285 * the permissions in a particular class.
287 static int context_struct_compute_av(struct context
*scontext
,
288 struct context
*tcontext
,
291 struct av_decision
*avd
)
293 struct constraint_node
*constraint
;
294 struct role_allow
*ra
;
295 struct avtab_key avkey
;
296 struct avtab_node
*node
;
297 struct class_datum
*tclass_datum
;
298 struct ebitmap
*sattr
, *tattr
;
299 struct ebitmap_node
*snode
, *tnode
;
300 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
304 * Remap extended Netlink classes for old policy versions.
305 * Do this here rather than socket_type_to_security_class()
306 * in case a newer policy version is loaded, allowing sockets
307 * to remain in the correct class.
309 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
310 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
311 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
312 tclass
= SECCLASS_NETLINK_SOCKET
;
315 * Initialize the access vectors to the default values.
318 avd
->decided
= 0xffffffff;
320 avd
->auditdeny
= 0xffffffff;
321 avd
->seqno
= latest_granting
;
324 * Check for all the invalid cases.
326 * - tclass > policy and > kernel
327 * - tclass > policy but is a userspace class
328 * - tclass > policy but we do not allow unknowns
330 if (unlikely(!tclass
))
332 if (unlikely(tclass
> policydb
.p_classes
.nprim
))
333 if (tclass
> kdefs
->cts_len
||
334 !kdefs
->class_to_string
[tclass
- 1] ||
335 !policydb
.allow_unknown
)
339 * Kernel class and we allow unknown so pad the allow decision
340 * the pad will be all 1 for unknown classes.
342 if (tclass
<= kdefs
->cts_len
&& policydb
.allow_unknown
)
343 avd
->allowed
= policydb
.undefined_perms
[tclass
- 1];
346 * Not in policy. Since decision is completed (all 1 or all 0) return.
348 if (unlikely(tclass
> policydb
.p_classes
.nprim
))
351 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
354 * If a specific type enforcement rule was defined for
355 * this permission check, then use it.
357 avkey
.target_class
= tclass
;
358 avkey
.specified
= AVTAB_AV
;
359 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
360 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
361 ebitmap_for_each_positive_bit(sattr
, snode
, i
) {
362 ebitmap_for_each_positive_bit(tattr
, tnode
, j
) {
363 avkey
.source_type
= i
+ 1;
364 avkey
.target_type
= j
+ 1;
365 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
367 node
= avtab_search_node_next(node
, avkey
.specified
)) {
368 if (node
->key
.specified
== AVTAB_ALLOWED
)
369 avd
->allowed
|= node
->datum
.data
;
370 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
371 avd
->auditallow
|= node
->datum
.data
;
372 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
373 avd
->auditdeny
&= node
->datum
.data
;
376 /* Check conditional av table for additional permissions */
377 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
383 * Remove any permissions prohibited by a constraint (this includes
386 constraint
= tclass_datum
->constraints
;
388 if ((constraint
->permissions
& (avd
->allowed
)) &&
389 !constraint_expr_eval(scontext
, tcontext
, NULL
,
391 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
393 constraint
= constraint
->next
;
397 * If checking process transition permission and the
398 * role is changing, then check the (current_role, new_role)
401 if (tclass
== SECCLASS_PROCESS
&&
402 (avd
->allowed
& (PROCESS__TRANSITION
| PROCESS__DYNTRANSITION
)) &&
403 scontext
->role
!= tcontext
->role
) {
404 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
405 if (scontext
->role
== ra
->role
&&
406 tcontext
->role
== ra
->new_role
)
410 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
|
411 PROCESS__DYNTRANSITION
);
417 printk(KERN_ERR
"%s: unrecognized class %d\n", __func__
, tclass
);
422 * Given a sid find if the type has the permissive flag set
424 int security_permissive_sid(u32 sid
)
426 struct context
*context
;
432 context
= sidtab_search(&sidtab
, sid
);
435 type
= context
->type
;
437 * we are intentionally using type here, not type-1, the 0th bit may
438 * someday indicate that we are globally setting permissive in policy.
440 rc
= ebitmap_get_bit(&policydb
.permissive_map
, type
);
446 static int security_validtrans_handle_fail(struct context
*ocontext
,
447 struct context
*ncontext
,
448 struct context
*tcontext
,
451 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
452 u32 olen
, nlen
, tlen
;
454 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
456 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
458 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
460 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
461 "security_validate_transition: denied for"
462 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
463 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
469 if (!selinux_enforcing
)
474 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
477 struct context
*ocontext
;
478 struct context
*ncontext
;
479 struct context
*tcontext
;
480 struct class_datum
*tclass_datum
;
481 struct constraint_node
*constraint
;
490 * Remap extended Netlink classes for old policy versions.
491 * Do this here rather than socket_type_to_security_class()
492 * in case a newer policy version is loaded, allowing sockets
493 * to remain in the correct class.
495 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
496 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
497 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
498 tclass
= SECCLASS_NETLINK_SOCKET
;
500 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
501 printk(KERN_ERR
"security_validate_transition: "
502 "unrecognized class %d\n", tclass
);
506 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
508 ocontext
= sidtab_search(&sidtab
, oldsid
);
510 printk(KERN_ERR
"security_validate_transition: "
511 " unrecognized SID %d\n", oldsid
);
516 ncontext
= sidtab_search(&sidtab
, newsid
);
518 printk(KERN_ERR
"security_validate_transition: "
519 " unrecognized SID %d\n", newsid
);
524 tcontext
= sidtab_search(&sidtab
, tasksid
);
526 printk(KERN_ERR
"security_validate_transition: "
527 " unrecognized SID %d\n", tasksid
);
532 constraint
= tclass_datum
->validatetrans
;
534 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
536 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
540 constraint
= constraint
->next
;
549 * security_compute_av - Compute access vector decisions.
550 * @ssid: source security identifier
551 * @tsid: target security identifier
552 * @tclass: target security class
553 * @requested: requested permissions
554 * @avd: access vector decisions
556 * Compute a set of access vector decisions based on the
557 * SID pair (@ssid, @tsid) for the permissions in @tclass.
558 * Return -%EINVAL if any of the parameters are invalid or %0
559 * if the access vector decisions were computed successfully.
561 int security_compute_av(u32 ssid
,
565 struct av_decision
*avd
)
567 struct context
*scontext
= NULL
, *tcontext
= NULL
;
570 if (!ss_initialized
) {
571 avd
->allowed
= 0xffffffff;
572 avd
->decided
= 0xffffffff;
574 avd
->auditdeny
= 0xffffffff;
575 avd
->seqno
= latest_granting
;
581 scontext
= sidtab_search(&sidtab
, ssid
);
583 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
588 tcontext
= sidtab_search(&sidtab
, tsid
);
590 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
596 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
604 * Write the security context string representation of
605 * the context structure `context' into a dynamically
606 * allocated string of the correct size. Set `*scontext'
607 * to point to this string and set `*scontext_len' to
608 * the length of the string.
610 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
617 /* Compute the size of the context. */
618 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
619 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
620 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
621 *scontext_len
+= mls_compute_context_len(context
);
623 /* Allocate space for the context; caller must free this space. */
624 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
628 *scontext
= scontextp
;
631 * Copy the user name, role name and type name into the context.
633 sprintf(scontextp
, "%s:%s:%s",
634 policydb
.p_user_val_to_name
[context
->user
- 1],
635 policydb
.p_role_val_to_name
[context
->role
- 1],
636 policydb
.p_type_val_to_name
[context
->type
- 1]);
637 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
638 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
639 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
641 mls_sid_to_context(context
, &scontextp
);
648 #include "initial_sid_to_string.h"
650 const char *security_get_initial_sid_context(u32 sid
)
652 if (unlikely(sid
> SECINITSID_NUM
))
654 return initial_sid_to_string
[sid
];
658 * security_sid_to_context - Obtain a context for a given SID.
659 * @sid: security identifier, SID
660 * @scontext: security context
661 * @scontext_len: length in bytes
663 * Write the string representation of the context associated with @sid
664 * into a dynamically allocated string of the correct size. Set @scontext
665 * to point to this string and set @scontext_len to the length of the string.
667 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
669 struct context
*context
;
675 if (!ss_initialized
) {
676 if (sid
<= SECINITSID_NUM
) {
679 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
680 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
685 strcpy(scontextp
, initial_sid_to_string
[sid
]);
686 *scontext
= scontextp
;
689 printk(KERN_ERR
"security_sid_to_context: called before initial "
690 "load_policy on unknown SID %d\n", sid
);
695 context
= sidtab_search(&sidtab
, sid
);
697 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
702 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
710 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
,
711 u32
*sid
, u32 def_sid
, gfp_t gfp_flags
)
714 struct context context
;
715 struct role_datum
*role
;
716 struct type_datum
*typdatum
;
717 struct user_datum
*usrdatum
;
718 char *scontextp
, *p
, oldc
;
721 if (!ss_initialized
) {
724 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
725 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
730 *sid
= SECINITSID_KERNEL
;
735 /* Copy the string so that we can modify the copy as we parse it.
736 The string should already by null terminated, but we append a
737 null suffix to the copy to avoid problems with the existing
738 attr package, which doesn't view the null terminator as part
739 of the attribute value. */
740 scontext2
= kmalloc(scontext_len
+1, gfp_flags
);
745 memcpy(scontext2
, scontext
, scontext_len
);
746 scontext2
[scontext_len
] = 0;
748 context_init(&context
);
753 /* Parse the security context. */
756 scontextp
= (char *) scontext2
;
758 /* Extract the user. */
760 while (*p
&& *p
!= ':')
768 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
772 context
.user
= usrdatum
->value
;
776 while (*p
&& *p
!= ':')
784 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
787 context
.role
= role
->value
;
791 while (*p
&& *p
!= ':')
796 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
800 context
.type
= typdatum
->value
;
802 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
806 if ((p
- scontext2
) < scontext_len
) {
811 /* Check the validity of the new context. */
812 if (!policydb_context_isvalid(&policydb
, &context
)) {
816 /* Obtain the new sid. */
817 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
820 context_destroy(&context
);
827 * security_context_to_sid - Obtain a SID for a given security context.
828 * @scontext: security context
829 * @scontext_len: length in bytes
830 * @sid: security identifier, SID
832 * Obtains a SID associated with the security context that
833 * has the string representation specified by @scontext.
834 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
835 * memory is available, or 0 on success.
837 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
839 return security_context_to_sid_core(scontext
, scontext_len
,
840 sid
, SECSID_NULL
, GFP_KERNEL
);
844 * security_context_to_sid_default - Obtain a SID for a given security context,
845 * falling back to specified default if needed.
847 * @scontext: security context
848 * @scontext_len: length in bytes
849 * @sid: security identifier, SID
850 * @def_sid: default SID to assign on error
852 * Obtains a SID associated with the security context that
853 * has the string representation specified by @scontext.
854 * The default SID is passed to the MLS layer to be used to allow
855 * kernel labeling of the MLS field if the MLS field is not present
856 * (for upgrading to MLS without full relabel).
857 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
858 * memory is available, or 0 on success.
860 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
,
861 u32 def_sid
, gfp_t gfp_flags
)
863 return security_context_to_sid_core(scontext
, scontext_len
,
864 sid
, def_sid
, gfp_flags
);
867 static int compute_sid_handle_invalid_context(
868 struct context
*scontext
,
869 struct context
*tcontext
,
871 struct context
*newcontext
)
873 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
874 u32 slen
, tlen
, nlen
;
876 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
878 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
880 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
882 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
883 "security_compute_sid: invalid context %s"
887 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
892 if (!selinux_enforcing
)
897 static int security_compute_sid(u32 ssid
,
903 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
904 struct role_trans
*roletr
= NULL
;
905 struct avtab_key avkey
;
906 struct avtab_datum
*avdatum
;
907 struct avtab_node
*node
;
910 if (!ss_initialized
) {
912 case SECCLASS_PROCESS
:
922 context_init(&newcontext
);
926 scontext
= sidtab_search(&sidtab
, ssid
);
928 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
933 tcontext
= sidtab_search(&sidtab
, tsid
);
935 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
941 /* Set the user identity. */
943 case AVTAB_TRANSITION
:
945 /* Use the process user identity. */
946 newcontext
.user
= scontext
->user
;
949 /* Use the related object owner. */
950 newcontext
.user
= tcontext
->user
;
954 /* Set the role and type to default values. */
956 case SECCLASS_PROCESS
:
957 /* Use the current role and type of process. */
958 newcontext
.role
= scontext
->role
;
959 newcontext
.type
= scontext
->type
;
962 /* Use the well-defined object role. */
963 newcontext
.role
= OBJECT_R_VAL
;
964 /* Use the type of the related object. */
965 newcontext
.type
= tcontext
->type
;
968 /* Look for a type transition/member/change rule. */
969 avkey
.source_type
= scontext
->type
;
970 avkey
.target_type
= tcontext
->type
;
971 avkey
.target_class
= tclass
;
972 avkey
.specified
= specified
;
973 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
975 /* If no permanent rule, also check for enabled conditional rules */
977 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
978 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
979 if (node
->key
.specified
& AVTAB_ENABLED
) {
980 avdatum
= &node
->datum
;
987 /* Use the type from the type transition/member/change rule. */
988 newcontext
.type
= avdatum
->data
;
991 /* Check for class-specific changes. */
993 case SECCLASS_PROCESS
:
994 if (specified
& AVTAB_TRANSITION
) {
995 /* Look for a role transition rule. */
996 for (roletr
= policydb
.role_tr
; roletr
;
997 roletr
= roletr
->next
) {
998 if (roletr
->role
== scontext
->role
&&
999 roletr
->type
== tcontext
->type
) {
1000 /* Use the role transition rule. */
1001 newcontext
.role
= roletr
->new_role
;
1011 /* Set the MLS attributes.
1012 This is done last because it may allocate memory. */
1013 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
1017 /* Check the validity of the context. */
1018 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
1019 rc
= compute_sid_handle_invalid_context(scontext
,
1026 /* Obtain the sid for the context. */
1027 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
1030 context_destroy(&newcontext
);
1036 * security_transition_sid - Compute the SID for a new subject/object.
1037 * @ssid: source security identifier
1038 * @tsid: target security identifier
1039 * @tclass: target security class
1040 * @out_sid: security identifier for new subject/object
1042 * Compute a SID to use for labeling a new subject or object in the
1043 * class @tclass based on a SID pair (@ssid, @tsid).
1044 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1045 * if insufficient memory is available, or %0 if the new SID was
1046 * computed successfully.
1048 int security_transition_sid(u32 ssid
,
1053 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
1057 * security_member_sid - Compute the SID for member selection.
1058 * @ssid: source security identifier
1059 * @tsid: target security identifier
1060 * @tclass: target security class
1061 * @out_sid: security identifier for selected member
1063 * Compute a SID to use when selecting a member of a polyinstantiated
1064 * object of class @tclass based on a SID pair (@ssid, @tsid).
1065 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1066 * if insufficient memory is available, or %0 if the SID was
1067 * computed successfully.
1069 int security_member_sid(u32 ssid
,
1074 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
1078 * security_change_sid - Compute the SID for object relabeling.
1079 * @ssid: source security identifier
1080 * @tsid: target security identifier
1081 * @tclass: target security class
1082 * @out_sid: security identifier for selected member
1084 * Compute a SID to use for relabeling an object of class @tclass
1085 * based on a SID pair (@ssid, @tsid).
1086 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1087 * if insufficient memory is available, or %0 if the SID was
1088 * computed successfully.
1090 int security_change_sid(u32 ssid
,
1095 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1099 * Verify that each kernel class that is defined in the
1102 static int validate_classes(struct policydb
*p
)
1105 struct class_datum
*cladatum
;
1106 struct perm_datum
*perdatum
;
1107 u32 nprim
, tmp
, common_pts_len
, perm_val
, pol_val
;
1109 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
1110 const char *def_class
, *def_perm
, *pol_class
;
1111 struct symtab
*perms
;
1113 if (p
->allow_unknown
) {
1114 u32 num_classes
= kdefs
->cts_len
;
1115 p
->undefined_perms
= kcalloc(num_classes
, sizeof(u32
), GFP_KERNEL
);
1116 if (!p
->undefined_perms
)
1120 for (i
= 1; i
< kdefs
->cts_len
; i
++) {
1121 def_class
= kdefs
->class_to_string
[i
];
1124 if (i
> p
->p_classes
.nprim
) {
1126 "SELinux: class %s not defined in policy\n",
1128 if (p
->reject_unknown
)
1130 if (p
->allow_unknown
)
1131 p
->undefined_perms
[i
-1] = ~0U;
1134 pol_class
= p
->p_class_val_to_name
[i
-1];
1135 if (strcmp(pol_class
, def_class
)) {
1137 "SELinux: class %d is incorrect, found %s but should be %s\n",
1138 i
, pol_class
, def_class
);
1142 for (i
= 0; i
< kdefs
->av_pts_len
; i
++) {
1143 class_val
= kdefs
->av_perm_to_string
[i
].tclass
;
1144 perm_val
= kdefs
->av_perm_to_string
[i
].value
;
1145 def_perm
= kdefs
->av_perm_to_string
[i
].name
;
1146 if (class_val
> p
->p_classes
.nprim
)
1148 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1149 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1151 perms
= &cladatum
->permissions
;
1152 nprim
= 1 << (perms
->nprim
- 1);
1153 if (perm_val
> nprim
) {
1155 "SELinux: permission %s in class %s not defined in policy\n",
1156 def_perm
, pol_class
);
1157 if (p
->reject_unknown
)
1159 if (p
->allow_unknown
)
1160 p
->undefined_perms
[class_val
-1] |= perm_val
;
1163 perdatum
= hashtab_search(perms
->table
, def_perm
);
1164 if (perdatum
== NULL
) {
1166 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1167 def_perm
, pol_class
);
1170 pol_val
= 1 << (perdatum
->value
- 1);
1171 if (pol_val
!= perm_val
) {
1173 "SELinux: permission %s in class %s has incorrect value\n",
1174 def_perm
, pol_class
);
1178 for (i
= 0; i
< kdefs
->av_inherit_len
; i
++) {
1179 class_val
= kdefs
->av_inherit
[i
].tclass
;
1180 if (class_val
> p
->p_classes
.nprim
)
1182 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1183 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1185 if (!cladatum
->comdatum
) {
1187 "SELinux: class %s should have an inherits clause but does not\n",
1191 tmp
= kdefs
->av_inherit
[i
].common_base
;
1193 while (!(tmp
& 0x01)) {
1197 perms
= &cladatum
->comdatum
->permissions
;
1198 for (j
= 0; j
< common_pts_len
; j
++) {
1199 def_perm
= kdefs
->av_inherit
[i
].common_pts
[j
];
1200 if (j
>= perms
->nprim
) {
1202 "SELinux: permission %s in class %s not defined in policy\n",
1203 def_perm
, pol_class
);
1204 if (p
->reject_unknown
)
1206 if (p
->allow_unknown
)
1207 p
->undefined_perms
[class_val
-1] |= (1 << j
);
1210 perdatum
= hashtab_search(perms
->table
, def_perm
);
1211 if (perdatum
== NULL
) {
1213 "SELinux: permission %s in class %s not found in policy, bad policy\n",
1214 def_perm
, pol_class
);
1217 if (perdatum
->value
!= j
+ 1) {
1219 "SELinux: permission %s in class %s has incorrect value\n",
1220 def_perm
, pol_class
);
1228 /* Clone the SID into the new SID table. */
1229 static int clone_sid(u32 sid
,
1230 struct context
*context
,
1233 struct sidtab
*s
= arg
;
1235 return sidtab_insert(s
, sid
, context
);
1238 static inline int convert_context_handle_invalid_context(struct context
*context
)
1242 if (selinux_enforcing
) {
1248 context_struct_to_string(context
, &s
, &len
);
1249 printk(KERN_ERR
"SELinux: context %s is invalid\n", s
);
1255 struct convert_context_args
{
1256 struct policydb
*oldp
;
1257 struct policydb
*newp
;
1261 * Convert the values in the security context
1262 * structure `c' from the values specified
1263 * in the policy `p->oldp' to the values specified
1264 * in the policy `p->newp'. Verify that the
1265 * context is valid under the new policy.
1267 static int convert_context(u32 key
,
1271 struct convert_context_args
*args
;
1272 struct context oldc
;
1273 struct role_datum
*role
;
1274 struct type_datum
*typdatum
;
1275 struct user_datum
*usrdatum
;
1282 rc
= context_cpy(&oldc
, c
);
1288 /* Convert the user. */
1289 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1290 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1294 c
->user
= usrdatum
->value
;
1296 /* Convert the role. */
1297 role
= hashtab_search(args
->newp
->p_roles
.table
,
1298 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1302 c
->role
= role
->value
;
1304 /* Convert the type. */
1305 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1306 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1310 c
->type
= typdatum
->value
;
1312 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1316 /* Check the validity of the new context. */
1317 if (!policydb_context_isvalid(args
->newp
, c
)) {
1318 rc
= convert_context_handle_invalid_context(&oldc
);
1323 context_destroy(&oldc
);
1327 context_struct_to_string(&oldc
, &s
, &len
);
1328 context_destroy(&oldc
);
1329 printk(KERN_ERR
"SELinux: invalidating context %s\n", s
);
1334 static void security_load_policycaps(void)
1336 selinux_policycap_netpeer
= ebitmap_get_bit(&policydb
.policycaps
,
1337 POLICYDB_CAPABILITY_NETPEER
);
1338 selinux_policycap_openperm
= ebitmap_get_bit(&policydb
.policycaps
,
1339 POLICYDB_CAPABILITY_OPENPERM
);
1342 extern void selinux_complete_init(void);
1343 static int security_preserve_bools(struct policydb
*p
);
1346 * security_load_policy - Load a security policy configuration.
1347 * @data: binary policy data
1348 * @len: length of data in bytes
1350 * Load a new set of security policy configuration data,
1351 * validate it and convert the SID table as necessary.
1352 * This function will flush the access vector cache after
1353 * loading the new policy.
1355 int security_load_policy(void *data
, size_t len
)
1357 struct policydb oldpolicydb
, newpolicydb
;
1358 struct sidtab oldsidtab
, newsidtab
;
1359 struct convert_context_args args
;
1362 struct policy_file file
= { data
, len
}, *fp
= &file
;
1366 if (!ss_initialized
) {
1368 if (policydb_read(&policydb
, fp
)) {
1370 avtab_cache_destroy();
1373 if (policydb_load_isids(&policydb
, &sidtab
)) {
1375 policydb_destroy(&policydb
);
1376 avtab_cache_destroy();
1379 /* Verify that the kernel defined classes are correct. */
1380 if (validate_classes(&policydb
)) {
1382 "SELinux: the definition of a class is incorrect\n");
1384 sidtab_destroy(&sidtab
);
1385 policydb_destroy(&policydb
);
1386 avtab_cache_destroy();
1389 security_load_policycaps();
1390 policydb_loaded_version
= policydb
.policyvers
;
1392 seqno
= ++latest_granting
;
1394 selinux_complete_init();
1395 avc_ss_reset(seqno
);
1396 selnl_notify_policyload(seqno
);
1397 selinux_netlbl_cache_invalidate();
1398 selinux_xfrm_notify_policyload();
1403 sidtab_hash_eval(&sidtab
, "sids");
1406 if (policydb_read(&newpolicydb
, fp
)) {
1411 sidtab_init(&newsidtab
);
1413 /* Verify that the kernel defined classes are correct. */
1414 if (validate_classes(&newpolicydb
)) {
1416 "SELinux: the definition of a class is incorrect\n");
1421 rc
= security_preserve_bools(&newpolicydb
);
1423 printk(KERN_ERR
"SELinux: unable to preserve booleans\n");
1427 /* Clone the SID table. */
1428 sidtab_shutdown(&sidtab
);
1429 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1434 /* Convert the internal representations of contexts
1435 in the new SID table and remove invalid SIDs. */
1436 args
.oldp
= &policydb
;
1437 args
.newp
= &newpolicydb
;
1438 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1440 /* Save the old policydb and SID table to free later. */
1441 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1442 sidtab_set(&oldsidtab
, &sidtab
);
1444 /* Install the new policydb and SID table. */
1446 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1447 sidtab_set(&sidtab
, &newsidtab
);
1448 security_load_policycaps();
1449 seqno
= ++latest_granting
;
1450 policydb_loaded_version
= policydb
.policyvers
;
1454 /* Free the old policydb and SID table. */
1455 policydb_destroy(&oldpolicydb
);
1456 sidtab_destroy(&oldsidtab
);
1458 avc_ss_reset(seqno
);
1459 selnl_notify_policyload(seqno
);
1460 selinux_netlbl_cache_invalidate();
1461 selinux_xfrm_notify_policyload();
1467 sidtab_destroy(&newsidtab
);
1468 policydb_destroy(&newpolicydb
);
1474 * security_port_sid - Obtain the SID for a port.
1475 * @protocol: protocol number
1476 * @port: port number
1477 * @out_sid: security identifier
1479 int security_port_sid(u8 protocol
, u16 port
, u32
*out_sid
)
1486 c
= policydb
.ocontexts
[OCON_PORT
];
1488 if (c
->u
.port
.protocol
== protocol
&&
1489 c
->u
.port
.low_port
<= port
&&
1490 c
->u
.port
.high_port
>= port
)
1497 rc
= sidtab_context_to_sid(&sidtab
,
1503 *out_sid
= c
->sid
[0];
1505 *out_sid
= SECINITSID_PORT
;
1514 * security_netif_sid - Obtain the SID for a network interface.
1515 * @name: interface name
1516 * @if_sid: interface SID
1518 int security_netif_sid(char *name
, u32
*if_sid
)
1525 c
= policydb
.ocontexts
[OCON_NETIF
];
1527 if (strcmp(name
, c
->u
.name
) == 0)
1533 if (!c
->sid
[0] || !c
->sid
[1]) {
1534 rc
= sidtab_context_to_sid(&sidtab
,
1539 rc
= sidtab_context_to_sid(&sidtab
,
1545 *if_sid
= c
->sid
[0];
1547 *if_sid
= SECINITSID_NETIF
;
1554 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1558 for(i
= 0; i
< 4; i
++)
1559 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1568 * security_node_sid - Obtain the SID for a node (host).
1569 * @domain: communication domain aka address family
1571 * @addrlen: address length in bytes
1572 * @out_sid: security identifier
1574 int security_node_sid(u16 domain
,
1588 if (addrlen
!= sizeof(u32
)) {
1593 addr
= *((u32
*)addrp
);
1595 c
= policydb
.ocontexts
[OCON_NODE
];
1597 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1605 if (addrlen
!= sizeof(u64
) * 2) {
1609 c
= policydb
.ocontexts
[OCON_NODE6
];
1611 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1619 *out_sid
= SECINITSID_NODE
;
1625 rc
= sidtab_context_to_sid(&sidtab
,
1631 *out_sid
= c
->sid
[0];
1633 *out_sid
= SECINITSID_NODE
;
1644 * security_get_user_sids - Obtain reachable SIDs for a user.
1645 * @fromsid: starting SID
1646 * @username: username
1647 * @sids: array of reachable SIDs for user
1648 * @nel: number of elements in @sids
1650 * Generate the set of SIDs for legal security contexts
1651 * for a given user that can be reached by @fromsid.
1652 * Set *@sids to point to a dynamically allocated
1653 * array containing the set of SIDs. Set *@nel to the
1654 * number of elements in the array.
1657 int security_get_user_sids(u32 fromsid
,
1662 struct context
*fromcon
, usercon
;
1663 u32
*mysids
= NULL
, *mysids2
, sid
;
1664 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1665 struct user_datum
*user
;
1666 struct role_datum
*role
;
1667 struct ebitmap_node
*rnode
, *tnode
;
1673 if (!ss_initialized
)
1678 fromcon
= sidtab_search(&sidtab
, fromsid
);
1684 user
= hashtab_search(policydb
.p_users
.table
, username
);
1689 usercon
.user
= user
->value
;
1691 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1697 ebitmap_for_each_positive_bit(&user
->roles
, rnode
, i
) {
1698 role
= policydb
.role_val_to_struct
[i
];
1700 ebitmap_for_each_positive_bit(&role
->types
, tnode
, j
) {
1703 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1706 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1709 if (mynel
< maxnel
) {
1710 mysids
[mynel
++] = sid
;
1713 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1718 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1721 mysids
[mynel
++] = sid
;
1733 mysids2
= kcalloc(mynel
, sizeof(*mysids2
), GFP_KERNEL
);
1739 for (i
= 0, j
= 0; i
< mynel
; i
++) {
1740 rc
= avc_has_perm_noaudit(fromsid
, mysids
[i
],
1742 PROCESS__TRANSITION
, AVC_STRICT
,
1745 mysids2
[j
++] = mysids
[i
];
1757 * security_genfs_sid - Obtain a SID for a file in a filesystem
1758 * @fstype: filesystem type
1759 * @path: path from root of mount
1760 * @sclass: file security class
1761 * @sid: SID for path
1763 * Obtain a SID to use for a file in a filesystem that
1764 * cannot support xattr or use a fixed labeling behavior like
1765 * transition SIDs or task SIDs.
1767 int security_genfs_sid(const char *fstype
,
1773 struct genfs
*genfs
;
1775 int rc
= 0, cmp
= 0;
1777 while (path
[0] == '/' && path
[1] == '/')
1782 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1783 cmp
= strcmp(fstype
, genfs
->fstype
);
1788 if (!genfs
|| cmp
) {
1789 *sid
= SECINITSID_UNLABELED
;
1794 for (c
= genfs
->head
; c
; c
= c
->next
) {
1795 len
= strlen(c
->u
.name
);
1796 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1797 (strncmp(c
->u
.name
, path
, len
) == 0))
1802 *sid
= SECINITSID_UNLABELED
;
1808 rc
= sidtab_context_to_sid(&sidtab
,
1822 * security_fs_use - Determine how to handle labeling for a filesystem.
1823 * @fstype: filesystem type
1824 * @behavior: labeling behavior
1825 * @sid: SID for filesystem (superblock)
1827 int security_fs_use(
1829 unsigned int *behavior
,
1837 c
= policydb
.ocontexts
[OCON_FSUSE
];
1839 if (strcmp(fstype
, c
->u
.name
) == 0)
1845 *behavior
= c
->v
.behavior
;
1847 rc
= sidtab_context_to_sid(&sidtab
,
1855 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1857 *behavior
= SECURITY_FS_USE_NONE
;
1860 *behavior
= SECURITY_FS_USE_GENFS
;
1869 int security_get_bools(int *len
, char ***names
, int **values
)
1871 int i
, rc
= -ENOMEM
;
1877 *len
= policydb
.p_bools
.nprim
;
1883 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1887 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1891 for (i
= 0; i
< *len
; i
++) {
1893 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1894 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1895 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1898 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1899 (*names
)[i
][name_len
- 1] = 0;
1907 for (i
= 0; i
< *len
; i
++)
1915 int security_set_bools(int len
, int *values
)
1918 int lenp
, seqno
= 0;
1919 struct cond_node
*cur
;
1923 lenp
= policydb
.p_bools
.nprim
;
1929 for (i
= 0; i
< len
; i
++) {
1930 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
1931 audit_log(current
->audit_context
, GFP_ATOMIC
,
1932 AUDIT_MAC_CONFIG_CHANGE
,
1933 "bool=%s val=%d old_val=%d auid=%u ses=%u",
1934 policydb
.p_bool_val_to_name
[i
],
1936 policydb
.bool_val_to_struct
[i
]->state
,
1937 audit_get_loginuid(current
),
1938 audit_get_sessionid(current
));
1941 policydb
.bool_val_to_struct
[i
]->state
= 1;
1943 policydb
.bool_val_to_struct
[i
]->state
= 0;
1947 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1948 rc
= evaluate_cond_node(&policydb
, cur
);
1953 seqno
= ++latest_granting
;
1958 avc_ss_reset(seqno
);
1959 selnl_notify_policyload(seqno
);
1960 selinux_xfrm_notify_policyload();
1965 int security_get_bool_value(int bool)
1972 len
= policydb
.p_bools
.nprim
;
1978 rc
= policydb
.bool_val_to_struct
[bool]->state
;
1984 static int security_preserve_bools(struct policydb
*p
)
1986 int rc
, nbools
= 0, *bvalues
= NULL
, i
;
1987 char **bnames
= NULL
;
1988 struct cond_bool_datum
*booldatum
;
1989 struct cond_node
*cur
;
1991 rc
= security_get_bools(&nbools
, &bnames
, &bvalues
);
1994 for (i
= 0; i
< nbools
; i
++) {
1995 booldatum
= hashtab_search(p
->p_bools
.table
, bnames
[i
]);
1997 booldatum
->state
= bvalues
[i
];
1999 for (cur
= p
->cond_list
; cur
!= NULL
; cur
= cur
->next
) {
2000 rc
= evaluate_cond_node(p
, cur
);
2007 for (i
= 0; i
< nbools
; i
++)
2016 * security_sid_mls_copy() - computes a new sid based on the given
2017 * sid and the mls portion of mls_sid.
2019 int security_sid_mls_copy(u32 sid
, u32 mls_sid
, u32
*new_sid
)
2021 struct context
*context1
;
2022 struct context
*context2
;
2023 struct context newcon
;
2028 if (!ss_initialized
|| !selinux_mls_enabled
) {
2033 context_init(&newcon
);
2036 context1
= sidtab_search(&sidtab
, sid
);
2038 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2044 context2
= sidtab_search(&sidtab
, mls_sid
);
2046 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
2052 newcon
.user
= context1
->user
;
2053 newcon
.role
= context1
->role
;
2054 newcon
.type
= context1
->type
;
2055 rc
= mls_context_cpy(&newcon
, context2
);
2059 /* Check the validity of the new context. */
2060 if (!policydb_context_isvalid(&policydb
, &newcon
)) {
2061 rc
= convert_context_handle_invalid_context(&newcon
);
2066 rc
= sidtab_context_to_sid(&sidtab
, &newcon
, new_sid
);
2070 if (!context_struct_to_string(&newcon
, &s
, &len
)) {
2071 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2072 "security_sid_mls_copy: invalid context %s", s
);
2078 context_destroy(&newcon
);
2084 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2085 * @nlbl_sid: NetLabel SID
2086 * @nlbl_type: NetLabel labeling protocol type
2087 * @xfrm_sid: XFRM SID
2090 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2091 * resolved into a single SID it is returned via @peer_sid and the function
2092 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2093 * returns a negative value. A table summarizing the behavior is below:
2095 * | function return | @sid
2096 * ------------------------------+-----------------+-----------------
2097 * no peer labels | 0 | SECSID_NULL
2098 * single peer label | 0 | <peer_label>
2099 * multiple, consistent labels | 0 | <peer_label>
2100 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2103 int security_net_peersid_resolve(u32 nlbl_sid
, u32 nlbl_type
,
2108 struct context
*nlbl_ctx
;
2109 struct context
*xfrm_ctx
;
2111 /* handle the common (which also happens to be the set of easy) cases
2112 * right away, these two if statements catch everything involving a
2113 * single or absent peer SID/label */
2114 if (xfrm_sid
== SECSID_NULL
) {
2115 *peer_sid
= nlbl_sid
;
2118 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2119 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2121 if (nlbl_sid
== SECSID_NULL
|| nlbl_type
== NETLBL_NLTYPE_UNLABELED
) {
2122 *peer_sid
= xfrm_sid
;
2126 /* we don't need to check ss_initialized here since the only way both
2127 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2128 * security server was initialized and ss_initialized was true */
2129 if (!selinux_mls_enabled
) {
2130 *peer_sid
= SECSID_NULL
;
2136 nlbl_ctx
= sidtab_search(&sidtab
, nlbl_sid
);
2139 "security_sid_mls_cmp: unrecognized SID %d\n",
2144 xfrm_ctx
= sidtab_search(&sidtab
, xfrm_sid
);
2147 "security_sid_mls_cmp: unrecognized SID %d\n",
2152 rc
= (mls_context_cmp(nlbl_ctx
, xfrm_ctx
) ? 0 : -EACCES
);
2157 /* at present NetLabel SIDs/labels really only carry MLS
2158 * information so if the MLS portion of the NetLabel SID
2159 * matches the MLS portion of the labeled XFRM SID/label
2160 * then pass along the XFRM SID as it is the most
2162 *peer_sid
= xfrm_sid
;
2164 *peer_sid
= SECSID_NULL
;
2168 static int get_classes_callback(void *k
, void *d
, void *args
)
2170 struct class_datum
*datum
= d
;
2171 char *name
= k
, **classes
= args
;
2172 int value
= datum
->value
- 1;
2174 classes
[value
] = kstrdup(name
, GFP_ATOMIC
);
2175 if (!classes
[value
])
2181 int security_get_classes(char ***classes
, int *nclasses
)
2187 *nclasses
= policydb
.p_classes
.nprim
;
2188 *classes
= kcalloc(*nclasses
, sizeof(*classes
), GFP_ATOMIC
);
2192 rc
= hashtab_map(policydb
.p_classes
.table
, get_classes_callback
,
2196 for (i
= 0; i
< *nclasses
; i
++)
2197 kfree((*classes
)[i
]);
2206 static int get_permissions_callback(void *k
, void *d
, void *args
)
2208 struct perm_datum
*datum
= d
;
2209 char *name
= k
, **perms
= args
;
2210 int value
= datum
->value
- 1;
2212 perms
[value
] = kstrdup(name
, GFP_ATOMIC
);
2219 int security_get_permissions(char *class, char ***perms
, int *nperms
)
2221 int rc
= -ENOMEM
, i
;
2222 struct class_datum
*match
;
2226 match
= hashtab_search(policydb
.p_classes
.table
, class);
2228 printk(KERN_ERR
"%s: unrecognized class %s\n",
2234 *nperms
= match
->permissions
.nprim
;
2235 *perms
= kcalloc(*nperms
, sizeof(*perms
), GFP_ATOMIC
);
2239 if (match
->comdatum
) {
2240 rc
= hashtab_map(match
->comdatum
->permissions
.table
,
2241 get_permissions_callback
, *perms
);
2246 rc
= hashtab_map(match
->permissions
.table
, get_permissions_callback
,
2257 for (i
= 0; i
< *nperms
; i
++)
2263 int security_get_reject_unknown(void)
2265 return policydb
.reject_unknown
;
2268 int security_get_allow_unknown(void)
2270 return policydb
.allow_unknown
;
2274 * security_policycap_supported - Check for a specific policy capability
2275 * @req_cap: capability
2278 * This function queries the currently loaded policy to see if it supports the
2279 * capability specified by @req_cap. Returns true (1) if the capability is
2280 * supported, false (0) if it isn't supported.
2283 int security_policycap_supported(unsigned int req_cap
)
2288 rc
= ebitmap_get_bit(&policydb
.policycaps
, req_cap
);
2294 struct selinux_audit_rule
{
2296 struct context au_ctxt
;
2299 void selinux_audit_rule_free(struct selinux_audit_rule
*rule
)
2302 context_destroy(&rule
->au_ctxt
);
2307 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
,
2308 struct selinux_audit_rule
**rule
)
2310 struct selinux_audit_rule
*tmprule
;
2311 struct role_datum
*roledatum
;
2312 struct type_datum
*typedatum
;
2313 struct user_datum
*userdatum
;
2318 if (!ss_initialized
)
2322 case AUDIT_SUBJ_USER
:
2323 case AUDIT_SUBJ_ROLE
:
2324 case AUDIT_SUBJ_TYPE
:
2325 case AUDIT_OBJ_USER
:
2326 case AUDIT_OBJ_ROLE
:
2327 case AUDIT_OBJ_TYPE
:
2328 /* only 'equals' and 'not equals' fit user, role, and type */
2329 if (op
!= AUDIT_EQUAL
&& op
!= AUDIT_NOT_EQUAL
)
2332 case AUDIT_SUBJ_SEN
:
2333 case AUDIT_SUBJ_CLR
:
2334 case AUDIT_OBJ_LEV_LOW
:
2335 case AUDIT_OBJ_LEV_HIGH
:
2336 /* we do not allow a range, indicated by the presense of '-' */
2337 if (strchr(rulestr
, '-'))
2341 /* only the above fields are valid */
2345 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
2349 context_init(&tmprule
->au_ctxt
);
2353 tmprule
->au_seqno
= latest_granting
;
2356 case AUDIT_SUBJ_USER
:
2357 case AUDIT_OBJ_USER
:
2358 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
2362 tmprule
->au_ctxt
.user
= userdatum
->value
;
2364 case AUDIT_SUBJ_ROLE
:
2365 case AUDIT_OBJ_ROLE
:
2366 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
2370 tmprule
->au_ctxt
.role
= roledatum
->value
;
2372 case AUDIT_SUBJ_TYPE
:
2373 case AUDIT_OBJ_TYPE
:
2374 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
2378 tmprule
->au_ctxt
.type
= typedatum
->value
;
2380 case AUDIT_SUBJ_SEN
:
2381 case AUDIT_SUBJ_CLR
:
2382 case AUDIT_OBJ_LEV_LOW
:
2383 case AUDIT_OBJ_LEV_HIGH
:
2384 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
2391 selinux_audit_rule_free(tmprule
);
2400 int selinux_audit_rule_match(u32 sid
, u32 field
, u32 op
,
2401 struct selinux_audit_rule
*rule
,
2402 struct audit_context
*actx
)
2404 struct context
*ctxt
;
2405 struct mls_level
*level
;
2409 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2410 "selinux_audit_rule_match: missing rule\n");
2416 if (rule
->au_seqno
< latest_granting
) {
2417 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2418 "selinux_audit_rule_match: stale rule\n");
2423 ctxt
= sidtab_search(&sidtab
, sid
);
2425 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2426 "selinux_audit_rule_match: unrecognized SID %d\n",
2432 /* a field/op pair that is not caught here will simply fall through
2435 case AUDIT_SUBJ_USER
:
2436 case AUDIT_OBJ_USER
:
2439 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
2441 case AUDIT_NOT_EQUAL
:
2442 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
2446 case AUDIT_SUBJ_ROLE
:
2447 case AUDIT_OBJ_ROLE
:
2450 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
2452 case AUDIT_NOT_EQUAL
:
2453 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
2457 case AUDIT_SUBJ_TYPE
:
2458 case AUDIT_OBJ_TYPE
:
2461 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
2463 case AUDIT_NOT_EQUAL
:
2464 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
2468 case AUDIT_SUBJ_SEN
:
2469 case AUDIT_SUBJ_CLR
:
2470 case AUDIT_OBJ_LEV_LOW
:
2471 case AUDIT_OBJ_LEV_HIGH
:
2472 level
= ((field
== AUDIT_SUBJ_SEN
||
2473 field
== AUDIT_OBJ_LEV_LOW
) ?
2474 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
2477 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2480 case AUDIT_NOT_EQUAL
:
2481 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2484 case AUDIT_LESS_THAN
:
2485 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2487 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2490 case AUDIT_LESS_THAN_OR_EQUAL
:
2491 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2494 case AUDIT_GREATER_THAN
:
2495 match
= (mls_level_dom(level
,
2496 &rule
->au_ctxt
.range
.level
[0]) &&
2497 !mls_level_eq(level
,
2498 &rule
->au_ctxt
.range
.level
[0]));
2500 case AUDIT_GREATER_THAN_OR_EQUAL
:
2501 match
= mls_level_dom(level
,
2502 &rule
->au_ctxt
.range
.level
[0]);
2512 static int (*aurule_callback
)(void) = NULL
;
2514 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2515 u16
class, u32 perms
, u32
*retained
)
2519 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2520 err
= aurule_callback();
2524 static int __init
aurule_init(void)
2528 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2529 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2531 panic("avc_add_callback() failed, error %d\n", err
);
2535 __initcall(aurule_init
);
2537 void selinux_audit_set_callback(int (*callback
)(void))
2539 aurule_callback
= callback
;
2542 #ifdef CONFIG_NETLABEL
2544 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2545 * @secattr: the NetLabel packet security attributes
2546 * @sid: the SELinux SID
2549 * Attempt to cache the context in @ctx, which was derived from the packet in
2550 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2551 * already been initialized.
2554 static void security_netlbl_cache_add(struct netlbl_lsm_secattr
*secattr
,
2559 sid_cache
= kmalloc(sizeof(*sid_cache
), GFP_ATOMIC
);
2560 if (sid_cache
== NULL
)
2562 secattr
->cache
= netlbl_secattr_cache_alloc(GFP_ATOMIC
);
2563 if (secattr
->cache
== NULL
) {
2569 secattr
->cache
->free
= kfree
;
2570 secattr
->cache
->data
= sid_cache
;
2571 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
2575 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2576 * @secattr: the NetLabel packet security attributes
2577 * @sid: the SELinux SID
2580 * Convert the given NetLabel security attributes in @secattr into a
2581 * SELinux SID. If the @secattr field does not contain a full SELinux
2582 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2583 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2584 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2585 * conversion for future lookups. Returns zero on success, negative values on
2589 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr
*secattr
,
2593 struct context
*ctx
;
2594 struct context ctx_new
;
2596 if (!ss_initialized
) {
2603 if (secattr
->flags
& NETLBL_SECATTR_CACHE
) {
2604 *sid
= *(u32
*)secattr
->cache
->data
;
2606 } else if (secattr
->flags
& NETLBL_SECATTR_SECID
) {
2607 *sid
= secattr
->attr
.secid
;
2609 } else if (secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) {
2610 ctx
= sidtab_search(&sidtab
, SECINITSID_NETMSG
);
2612 goto netlbl_secattr_to_sid_return
;
2614 ctx_new
.user
= ctx
->user
;
2615 ctx_new
.role
= ctx
->role
;
2616 ctx_new
.type
= ctx
->type
;
2617 mls_import_netlbl_lvl(&ctx_new
, secattr
);
2618 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
2619 if (ebitmap_netlbl_import(&ctx_new
.range
.level
[0].cat
,
2620 secattr
->attr
.mls
.cat
) != 0)
2621 goto netlbl_secattr_to_sid_return
;
2622 ctx_new
.range
.level
[1].cat
.highbit
=
2623 ctx_new
.range
.level
[0].cat
.highbit
;
2624 ctx_new
.range
.level
[1].cat
.node
=
2625 ctx_new
.range
.level
[0].cat
.node
;
2627 ebitmap_init(&ctx_new
.range
.level
[0].cat
);
2628 ebitmap_init(&ctx_new
.range
.level
[1].cat
);
2630 if (mls_context_isvalid(&policydb
, &ctx_new
) != 1)
2631 goto netlbl_secattr_to_sid_return_cleanup
;
2633 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
2635 goto netlbl_secattr_to_sid_return_cleanup
;
2637 security_netlbl_cache_add(secattr
, *sid
);
2639 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2645 netlbl_secattr_to_sid_return
:
2648 netlbl_secattr_to_sid_return_cleanup
:
2649 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2650 goto netlbl_secattr_to_sid_return
;
2654 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2655 * @sid: the SELinux SID
2656 * @secattr: the NetLabel packet security attributes
2659 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2660 * Returns zero on success, negative values on failure.
2663 int security_netlbl_sid_to_secattr(u32 sid
, struct netlbl_lsm_secattr
*secattr
)
2666 struct context
*ctx
;
2668 if (!ss_initialized
)
2672 ctx
= sidtab_search(&sidtab
, sid
);
2674 goto netlbl_sid_to_secattr_failure
;
2675 secattr
->domain
= kstrdup(policydb
.p_type_val_to_name
[ctx
->type
- 1],
2677 secattr
->flags
|= NETLBL_SECATTR_DOMAIN
;
2678 mls_export_netlbl_lvl(ctx
, secattr
);
2679 rc
= mls_export_netlbl_cat(ctx
, secattr
);
2681 goto netlbl_sid_to_secattr_failure
;
2686 netlbl_sid_to_secattr_failure
:
2690 #endif /* CONFIG_NETLABEL */