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
20 * Updated: Chad Sellers <csellers@tresys.com>
22 * Added validation of kernel classes and permissions
24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/spinlock.h>
36 #include <linux/rcupdate.h>
37 #include <linux/errno.h>
39 #include <linux/sched.h>
40 #include <linux/audit.h>
41 #include <linux/mutex.h>
43 #include <net/netlabel.h>
53 #include "conditional.h"
56 #include "selinux_netlabel.h"
58 extern void selnl_notify_policyload(u32 seqno
);
59 unsigned int policydb_loaded_version
;
62 * This is declared in avc.c
64 extern const struct selinux_class_perm selinux_class_perm
;
66 static DEFINE_RWLOCK(policy_rwlock
);
67 #define POLICY_RDLOCK read_lock(&policy_rwlock)
68 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
69 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
70 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
72 static DEFINE_MUTEX(load_mutex
);
73 #define LOAD_LOCK mutex_lock(&load_mutex)
74 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
76 static struct sidtab sidtab
;
77 struct policydb policydb
;
78 int ss_initialized
= 0;
81 * The largest sequence number that has been used when
82 * providing an access decision to the access vector cache.
83 * The sequence number only changes when a policy change
86 static u32 latest_granting
= 0;
88 /* Forward declaration. */
89 static int context_struct_to_string(struct context
*context
, char **scontext
,
93 * Return the boolean value of a constraint expression
94 * when it is applied to the specified source and target
97 * xcontext is a special beast... It is used by the validatetrans rules
98 * only. For these rules, scontext is the context before the transition,
99 * tcontext is the context after the transition, and xcontext is the context
100 * of the process performing the transition. All other callers of
101 * constraint_expr_eval should pass in NULL for xcontext.
103 static int constraint_expr_eval(struct context
*scontext
,
104 struct context
*tcontext
,
105 struct context
*xcontext
,
106 struct constraint_expr
*cexpr
)
110 struct role_datum
*r1
, *r2
;
111 struct mls_level
*l1
, *l2
;
112 struct constraint_expr
*e
;
113 int s
[CEXPR_MAXDEPTH
];
116 for (e
= cexpr
; e
; e
= e
->next
) {
117 switch (e
->expr_type
) {
133 if (sp
== (CEXPR_MAXDEPTH
-1))
137 val1
= scontext
->user
;
138 val2
= tcontext
->user
;
141 val1
= scontext
->type
;
142 val2
= tcontext
->type
;
145 val1
= scontext
->role
;
146 val2
= tcontext
->role
;
147 r1
= policydb
.role_val_to_struct
[val1
- 1];
148 r2
= policydb
.role_val_to_struct
[val2
- 1];
151 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
155 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
159 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
161 !ebitmap_get_bit(&r2
->dominates
,
169 l1
= &(scontext
->range
.level
[0]);
170 l2
= &(tcontext
->range
.level
[0]);
173 l1
= &(scontext
->range
.level
[0]);
174 l2
= &(tcontext
->range
.level
[1]);
177 l1
= &(scontext
->range
.level
[1]);
178 l2
= &(tcontext
->range
.level
[0]);
181 l1
= &(scontext
->range
.level
[1]);
182 l2
= &(tcontext
->range
.level
[1]);
185 l1
= &(scontext
->range
.level
[0]);
186 l2
= &(scontext
->range
.level
[1]);
189 l1
= &(tcontext
->range
.level
[0]);
190 l2
= &(tcontext
->range
.level
[1]);
195 s
[++sp
] = mls_level_eq(l1
, l2
);
198 s
[++sp
] = !mls_level_eq(l1
, l2
);
201 s
[++sp
] = mls_level_dom(l1
, l2
);
204 s
[++sp
] = mls_level_dom(l2
, l1
);
207 s
[++sp
] = mls_level_incomp(l2
, l1
);
221 s
[++sp
] = (val1
== val2
);
224 s
[++sp
] = (val1
!= val2
);
232 if (sp
== (CEXPR_MAXDEPTH
-1))
235 if (e
->attr
& CEXPR_TARGET
)
237 else if (e
->attr
& CEXPR_XTARGET
) {
244 if (e
->attr
& CEXPR_USER
)
246 else if (e
->attr
& CEXPR_ROLE
)
248 else if (e
->attr
& CEXPR_TYPE
)
257 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
260 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
278 * Compute access vectors based on a context structure pair for
279 * the permissions in a particular class.
281 static int context_struct_compute_av(struct context
*scontext
,
282 struct context
*tcontext
,
285 struct av_decision
*avd
)
287 struct constraint_node
*constraint
;
288 struct role_allow
*ra
;
289 struct avtab_key avkey
;
290 struct avtab_node
*node
;
291 struct class_datum
*tclass_datum
;
292 struct ebitmap
*sattr
, *tattr
;
293 struct ebitmap_node
*snode
, *tnode
;
297 * Remap extended Netlink classes for old policy versions.
298 * Do this here rather than socket_type_to_security_class()
299 * in case a newer policy version is loaded, allowing sockets
300 * to remain in the correct class.
302 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
303 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
304 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
305 tclass
= SECCLASS_NETLINK_SOCKET
;
307 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
308 printk(KERN_ERR
"security_compute_av: unrecognized class %d\n",
312 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
315 * Initialize the access vectors to the default values.
318 avd
->decided
= 0xffffffff;
320 avd
->auditdeny
= 0xffffffff;
321 avd
->seqno
= latest_granting
;
324 * If a specific type enforcement rule was defined for
325 * this permission check, then use it.
327 avkey
.target_class
= tclass
;
328 avkey
.specified
= AVTAB_AV
;
329 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
330 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
331 ebitmap_for_each_bit(sattr
, snode
, i
) {
332 if (!ebitmap_node_get_bit(snode
, i
))
334 ebitmap_for_each_bit(tattr
, tnode
, j
) {
335 if (!ebitmap_node_get_bit(tnode
, j
))
337 avkey
.source_type
= i
+ 1;
338 avkey
.target_type
= j
+ 1;
339 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
341 node
= avtab_search_node_next(node
, avkey
.specified
)) {
342 if (node
->key
.specified
== AVTAB_ALLOWED
)
343 avd
->allowed
|= node
->datum
.data
;
344 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
345 avd
->auditallow
|= node
->datum
.data
;
346 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
347 avd
->auditdeny
&= node
->datum
.data
;
350 /* Check conditional av table for additional permissions */
351 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
357 * Remove any permissions prohibited by a constraint (this includes
360 constraint
= tclass_datum
->constraints
;
362 if ((constraint
->permissions
& (avd
->allowed
)) &&
363 !constraint_expr_eval(scontext
, tcontext
, NULL
,
365 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
367 constraint
= constraint
->next
;
371 * If checking process transition permission and the
372 * role is changing, then check the (current_role, new_role)
375 if (tclass
== SECCLASS_PROCESS
&&
376 (avd
->allowed
& (PROCESS__TRANSITION
| PROCESS__DYNTRANSITION
)) &&
377 scontext
->role
!= tcontext
->role
) {
378 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
379 if (scontext
->role
== ra
->role
&&
380 tcontext
->role
== ra
->new_role
)
384 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
|
385 PROCESS__DYNTRANSITION
);
391 static int security_validtrans_handle_fail(struct context
*ocontext
,
392 struct context
*ncontext
,
393 struct context
*tcontext
,
396 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
397 u32 olen
, nlen
, tlen
;
399 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
401 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
403 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
405 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
406 "security_validate_transition: denied for"
407 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
408 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
414 if (!selinux_enforcing
)
419 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
422 struct context
*ocontext
;
423 struct context
*ncontext
;
424 struct context
*tcontext
;
425 struct class_datum
*tclass_datum
;
426 struct constraint_node
*constraint
;
435 * Remap extended Netlink classes for old policy versions.
436 * Do this here rather than socket_type_to_security_class()
437 * in case a newer policy version is loaded, allowing sockets
438 * to remain in the correct class.
440 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
441 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
442 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
443 tclass
= SECCLASS_NETLINK_SOCKET
;
445 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
446 printk(KERN_ERR
"security_validate_transition: "
447 "unrecognized class %d\n", tclass
);
451 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
453 ocontext
= sidtab_search(&sidtab
, oldsid
);
455 printk(KERN_ERR
"security_validate_transition: "
456 " unrecognized SID %d\n", oldsid
);
461 ncontext
= sidtab_search(&sidtab
, newsid
);
463 printk(KERN_ERR
"security_validate_transition: "
464 " unrecognized SID %d\n", newsid
);
469 tcontext
= sidtab_search(&sidtab
, tasksid
);
471 printk(KERN_ERR
"security_validate_transition: "
472 " unrecognized SID %d\n", tasksid
);
477 constraint
= tclass_datum
->validatetrans
;
479 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
481 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
485 constraint
= constraint
->next
;
494 * security_compute_av - Compute access vector decisions.
495 * @ssid: source security identifier
496 * @tsid: target security identifier
497 * @tclass: target security class
498 * @requested: requested permissions
499 * @avd: access vector decisions
501 * Compute a set of access vector decisions based on the
502 * SID pair (@ssid, @tsid) for the permissions in @tclass.
503 * Return -%EINVAL if any of the parameters are invalid or %0
504 * if the access vector decisions were computed successfully.
506 int security_compute_av(u32 ssid
,
510 struct av_decision
*avd
)
512 struct context
*scontext
= NULL
, *tcontext
= NULL
;
515 if (!ss_initialized
) {
516 avd
->allowed
= 0xffffffff;
517 avd
->decided
= 0xffffffff;
519 avd
->auditdeny
= 0xffffffff;
520 avd
->seqno
= latest_granting
;
526 scontext
= sidtab_search(&sidtab
, ssid
);
528 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
533 tcontext
= sidtab_search(&sidtab
, tsid
);
535 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
541 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
549 * Write the security context string representation of
550 * the context structure `context' into a dynamically
551 * allocated string of the correct size. Set `*scontext'
552 * to point to this string and set `*scontext_len' to
553 * the length of the string.
555 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
562 /* Compute the size of the context. */
563 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
564 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
565 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
566 *scontext_len
+= mls_compute_context_len(context
);
568 /* Allocate space for the context; caller must free this space. */
569 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
573 *scontext
= scontextp
;
576 * Copy the user name, role name and type name into the context.
578 sprintf(scontextp
, "%s:%s:%s",
579 policydb
.p_user_val_to_name
[context
->user
- 1],
580 policydb
.p_role_val_to_name
[context
->role
- 1],
581 policydb
.p_type_val_to_name
[context
->type
- 1]);
582 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
583 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
584 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
586 mls_sid_to_context(context
, &scontextp
);
593 #include "initial_sid_to_string.h"
596 * security_sid_to_context - Obtain a context for a given SID.
597 * @sid: security identifier, SID
598 * @scontext: security context
599 * @scontext_len: length in bytes
601 * Write the string representation of the context associated with @sid
602 * into a dynamically allocated string of the correct size. Set @scontext
603 * to point to this string and set @scontext_len to the length of the string.
605 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
607 struct context
*context
;
610 if (!ss_initialized
) {
611 if (sid
<= SECINITSID_NUM
) {
614 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
615 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
620 strcpy(scontextp
, initial_sid_to_string
[sid
]);
621 *scontext
= scontextp
;
624 printk(KERN_ERR
"security_sid_to_context: called before initial "
625 "load_policy on unknown SID %d\n", sid
);
630 context
= sidtab_search(&sidtab
, sid
);
632 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
637 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
645 static int security_context_to_sid_core(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
648 struct context context
;
649 struct role_datum
*role
;
650 struct type_datum
*typdatum
;
651 struct user_datum
*usrdatum
;
652 char *scontextp
, *p
, oldc
;
655 if (!ss_initialized
) {
658 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
659 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
664 *sid
= SECINITSID_KERNEL
;
669 /* Copy the string so that we can modify the copy as we parse it.
670 The string should already by null terminated, but we append a
671 null suffix to the copy to avoid problems with the existing
672 attr package, which doesn't view the null terminator as part
673 of the attribute value. */
674 scontext2
= kmalloc(scontext_len
+1,GFP_KERNEL
);
679 memcpy(scontext2
, scontext
, scontext_len
);
680 scontext2
[scontext_len
] = 0;
682 context_init(&context
);
687 /* Parse the security context. */
690 scontextp
= (char *) scontext2
;
692 /* Extract the user. */
694 while (*p
&& *p
!= ':')
702 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
706 context
.user
= usrdatum
->value
;
710 while (*p
&& *p
!= ':')
718 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
721 context
.role
= role
->value
;
725 while (*p
&& *p
!= ':')
730 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
734 context
.type
= typdatum
->value
;
736 rc
= mls_context_to_sid(oldc
, &p
, &context
, &sidtab
, def_sid
);
740 if ((p
- scontext2
) < scontext_len
) {
745 /* Check the validity of the new context. */
746 if (!policydb_context_isvalid(&policydb
, &context
)) {
750 /* Obtain the new sid. */
751 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
754 context_destroy(&context
);
761 * security_context_to_sid - Obtain a SID for a given security context.
762 * @scontext: security context
763 * @scontext_len: length in bytes
764 * @sid: security identifier, SID
766 * Obtains a SID associated with the security context that
767 * has the string representation specified by @scontext.
768 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
769 * memory is available, or 0 on success.
771 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
773 return security_context_to_sid_core(scontext
, scontext_len
,
778 * security_context_to_sid_default - Obtain a SID for a given security context,
779 * falling back to specified default if needed.
781 * @scontext: security context
782 * @scontext_len: length in bytes
783 * @sid: security identifier, SID
784 * @def_sid: default SID to assign on errror
786 * Obtains a SID associated with the security context that
787 * has the string representation specified by @scontext.
788 * The default SID is passed to the MLS layer to be used to allow
789 * kernel labeling of the MLS field if the MLS field is not present
790 * (for upgrading to MLS without full relabel).
791 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
792 * memory is available, or 0 on success.
794 int security_context_to_sid_default(char *scontext
, u32 scontext_len
, u32
*sid
, u32 def_sid
)
796 return security_context_to_sid_core(scontext
, scontext_len
,
800 static int compute_sid_handle_invalid_context(
801 struct context
*scontext
,
802 struct context
*tcontext
,
804 struct context
*newcontext
)
806 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
807 u32 slen
, tlen
, nlen
;
809 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
811 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
813 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
815 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
816 "security_compute_sid: invalid context %s"
820 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
825 if (!selinux_enforcing
)
830 static int security_compute_sid(u32 ssid
,
836 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
837 struct role_trans
*roletr
= NULL
;
838 struct avtab_key avkey
;
839 struct avtab_datum
*avdatum
;
840 struct avtab_node
*node
;
843 if (!ss_initialized
) {
845 case SECCLASS_PROCESS
:
855 context_init(&newcontext
);
859 scontext
= sidtab_search(&sidtab
, ssid
);
861 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
866 tcontext
= sidtab_search(&sidtab
, tsid
);
868 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
874 /* Set the user identity. */
876 case AVTAB_TRANSITION
:
878 /* Use the process user identity. */
879 newcontext
.user
= scontext
->user
;
882 /* Use the related object owner. */
883 newcontext
.user
= tcontext
->user
;
887 /* Set the role and type to default values. */
889 case SECCLASS_PROCESS
:
890 /* Use the current role and type of process. */
891 newcontext
.role
= scontext
->role
;
892 newcontext
.type
= scontext
->type
;
895 /* Use the well-defined object role. */
896 newcontext
.role
= OBJECT_R_VAL
;
897 /* Use the type of the related object. */
898 newcontext
.type
= tcontext
->type
;
901 /* Look for a type transition/member/change rule. */
902 avkey
.source_type
= scontext
->type
;
903 avkey
.target_type
= tcontext
->type
;
904 avkey
.target_class
= tclass
;
905 avkey
.specified
= specified
;
906 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
908 /* If no permanent rule, also check for enabled conditional rules */
910 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
911 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
912 if (node
->key
.specified
& AVTAB_ENABLED
) {
913 avdatum
= &node
->datum
;
920 /* Use the type from the type transition/member/change rule. */
921 newcontext
.type
= avdatum
->data
;
924 /* Check for class-specific changes. */
926 case SECCLASS_PROCESS
:
927 if (specified
& AVTAB_TRANSITION
) {
928 /* Look for a role transition rule. */
929 for (roletr
= policydb
.role_tr
; roletr
;
930 roletr
= roletr
->next
) {
931 if (roletr
->role
== scontext
->role
&&
932 roletr
->type
== tcontext
->type
) {
933 /* Use the role transition rule. */
934 newcontext
.role
= roletr
->new_role
;
944 /* Set the MLS attributes.
945 This is done last because it may allocate memory. */
946 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
950 /* Check the validity of the context. */
951 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
952 rc
= compute_sid_handle_invalid_context(scontext
,
959 /* Obtain the sid for the context. */
960 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
963 context_destroy(&newcontext
);
969 * security_transition_sid - Compute the SID for a new subject/object.
970 * @ssid: source security identifier
971 * @tsid: target security identifier
972 * @tclass: target security class
973 * @out_sid: security identifier for new subject/object
975 * Compute a SID to use for labeling a new subject or object in the
976 * class @tclass based on a SID pair (@ssid, @tsid).
977 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
978 * if insufficient memory is available, or %0 if the new SID was
979 * computed successfully.
981 int security_transition_sid(u32 ssid
,
986 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
990 * security_member_sid - Compute the SID for member selection.
991 * @ssid: source security identifier
992 * @tsid: target security identifier
993 * @tclass: target security class
994 * @out_sid: security identifier for selected member
996 * Compute a SID to use when selecting a member of a polyinstantiated
997 * object of class @tclass based on a SID pair (@ssid, @tsid).
998 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
999 * if insufficient memory is available, or %0 if the SID was
1000 * computed successfully.
1002 int security_member_sid(u32 ssid
,
1007 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
1011 * security_change_sid - Compute the SID for object relabeling.
1012 * @ssid: source security identifier
1013 * @tsid: target security identifier
1014 * @tclass: target security class
1015 * @out_sid: security identifier for selected member
1017 * Compute a SID to use for relabeling an object of class @tclass
1018 * based on a SID pair (@ssid, @tsid).
1019 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1020 * if insufficient memory is available, or %0 if the SID was
1021 * computed successfully.
1023 int security_change_sid(u32 ssid
,
1028 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
1032 * Verify that each kernel class that is defined in the
1035 static int validate_classes(struct policydb
*p
)
1038 struct class_datum
*cladatum
;
1039 struct perm_datum
*perdatum
;
1040 u32 nprim
, tmp
, common_pts_len
, perm_val
, pol_val
;
1042 const struct selinux_class_perm
*kdefs
= &selinux_class_perm
;
1043 const char *def_class
, *def_perm
, *pol_class
;
1044 struct symtab
*perms
;
1046 for (i
= 1; i
< kdefs
->cts_len
; i
++) {
1047 def_class
= kdefs
->class_to_string
[i
];
1048 if (i
> p
->p_classes
.nprim
) {
1050 "security: class %s not defined in policy\n",
1054 pol_class
= p
->p_class_val_to_name
[i
-1];
1055 if (strcmp(pol_class
, def_class
)) {
1057 "security: class %d is incorrect, found %s but should be %s\n",
1058 i
, pol_class
, def_class
);
1062 for (i
= 0; i
< kdefs
->av_pts_len
; i
++) {
1063 class_val
= kdefs
->av_perm_to_string
[i
].tclass
;
1064 perm_val
= kdefs
->av_perm_to_string
[i
].value
;
1065 def_perm
= kdefs
->av_perm_to_string
[i
].name
;
1066 if (class_val
> p
->p_classes
.nprim
)
1068 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1069 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1071 perms
= &cladatum
->permissions
;
1072 nprim
= 1 << (perms
->nprim
- 1);
1073 if (perm_val
> nprim
) {
1075 "security: permission %s in class %s not defined in policy\n",
1076 def_perm
, pol_class
);
1079 perdatum
= hashtab_search(perms
->table
, def_perm
);
1080 if (perdatum
== NULL
) {
1082 "security: permission %s in class %s not found in policy\n",
1083 def_perm
, pol_class
);
1086 pol_val
= 1 << (perdatum
->value
- 1);
1087 if (pol_val
!= perm_val
) {
1089 "security: permission %s in class %s has incorrect value\n",
1090 def_perm
, pol_class
);
1094 for (i
= 0; i
< kdefs
->av_inherit_len
; i
++) {
1095 class_val
= kdefs
->av_inherit
[i
].tclass
;
1096 if (class_val
> p
->p_classes
.nprim
)
1098 pol_class
= p
->p_class_val_to_name
[class_val
-1];
1099 cladatum
= hashtab_search(p
->p_classes
.table
, pol_class
);
1101 if (!cladatum
->comdatum
) {
1103 "security: class %s should have an inherits clause but does not\n",
1107 tmp
= kdefs
->av_inherit
[i
].common_base
;
1109 while (!(tmp
& 0x01)) {
1113 perms
= &cladatum
->comdatum
->permissions
;
1114 for (j
= 0; j
< common_pts_len
; j
++) {
1115 def_perm
= kdefs
->av_inherit
[i
].common_pts
[j
];
1116 if (j
>= perms
->nprim
) {
1118 "security: permission %s in class %s not defined in policy\n",
1119 def_perm
, pol_class
);
1122 perdatum
= hashtab_search(perms
->table
, def_perm
);
1123 if (perdatum
== NULL
) {
1125 "security: permission %s in class %s not found in policy\n",
1126 def_perm
, pol_class
);
1129 if (perdatum
->value
!= j
+ 1) {
1131 "security: permission %s in class %s has incorrect value\n",
1132 def_perm
, pol_class
);
1140 /* Clone the SID into the new SID table. */
1141 static int clone_sid(u32 sid
,
1142 struct context
*context
,
1145 struct sidtab
*s
= arg
;
1147 return sidtab_insert(s
, sid
, context
);
1150 static inline int convert_context_handle_invalid_context(struct context
*context
)
1154 if (selinux_enforcing
) {
1160 context_struct_to_string(context
, &s
, &len
);
1161 printk(KERN_ERR
"security: context %s is invalid\n", s
);
1167 struct convert_context_args
{
1168 struct policydb
*oldp
;
1169 struct policydb
*newp
;
1173 * Convert the values in the security context
1174 * structure `c' from the values specified
1175 * in the policy `p->oldp' to the values specified
1176 * in the policy `p->newp'. Verify that the
1177 * context is valid under the new policy.
1179 static int convert_context(u32 key
,
1183 struct convert_context_args
*args
;
1184 struct context oldc
;
1185 struct role_datum
*role
;
1186 struct type_datum
*typdatum
;
1187 struct user_datum
*usrdatum
;
1194 rc
= context_cpy(&oldc
, c
);
1200 /* Convert the user. */
1201 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1202 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1206 c
->user
= usrdatum
->value
;
1208 /* Convert the role. */
1209 role
= hashtab_search(args
->newp
->p_roles
.table
,
1210 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1214 c
->role
= role
->value
;
1216 /* Convert the type. */
1217 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1218 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1222 c
->type
= typdatum
->value
;
1224 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1228 /* Check the validity of the new context. */
1229 if (!policydb_context_isvalid(args
->newp
, c
)) {
1230 rc
= convert_context_handle_invalid_context(&oldc
);
1235 context_destroy(&oldc
);
1239 context_struct_to_string(&oldc
, &s
, &len
);
1240 context_destroy(&oldc
);
1241 printk(KERN_ERR
"security: invalidating context %s\n", s
);
1246 extern void selinux_complete_init(void);
1249 * security_load_policy - Load a security policy configuration.
1250 * @data: binary policy data
1251 * @len: length of data in bytes
1253 * Load a new set of security policy configuration data,
1254 * validate it and convert the SID table as necessary.
1255 * This function will flush the access vector cache after
1256 * loading the new policy.
1258 int security_load_policy(void *data
, size_t len
)
1260 struct policydb oldpolicydb
, newpolicydb
;
1261 struct sidtab oldsidtab
, newsidtab
;
1262 struct convert_context_args args
;
1265 struct policy_file file
= { data
, len
}, *fp
= &file
;
1269 if (!ss_initialized
) {
1271 if (policydb_read(&policydb
, fp
)) {
1273 avtab_cache_destroy();
1276 if (policydb_load_isids(&policydb
, &sidtab
)) {
1278 policydb_destroy(&policydb
);
1279 avtab_cache_destroy();
1282 /* Verify that the kernel defined classes are correct. */
1283 if (validate_classes(&policydb
)) {
1285 "security: the definition of a class is incorrect\n");
1287 sidtab_destroy(&sidtab
);
1288 policydb_destroy(&policydb
);
1289 avtab_cache_destroy();
1292 policydb_loaded_version
= policydb
.policyvers
;
1294 seqno
= ++latest_granting
;
1296 selinux_complete_init();
1297 avc_ss_reset(seqno
);
1298 selnl_notify_policyload(seqno
);
1299 selinux_netlbl_cache_invalidate();
1304 sidtab_hash_eval(&sidtab
, "sids");
1307 if (policydb_read(&newpolicydb
, fp
)) {
1312 sidtab_init(&newsidtab
);
1314 /* Verify that the kernel defined classes are correct. */
1315 if (validate_classes(&newpolicydb
)) {
1317 "security: the definition of a class is incorrect\n");
1322 /* Clone the SID table. */
1323 sidtab_shutdown(&sidtab
);
1324 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1329 /* Convert the internal representations of contexts
1330 in the new SID table and remove invalid SIDs. */
1331 args
.oldp
= &policydb
;
1332 args
.newp
= &newpolicydb
;
1333 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1335 /* Save the old policydb and SID table to free later. */
1336 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1337 sidtab_set(&oldsidtab
, &sidtab
);
1339 /* Install the new policydb and SID table. */
1341 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1342 sidtab_set(&sidtab
, &newsidtab
);
1343 seqno
= ++latest_granting
;
1344 policydb_loaded_version
= policydb
.policyvers
;
1348 /* Free the old policydb and SID table. */
1349 policydb_destroy(&oldpolicydb
);
1350 sidtab_destroy(&oldsidtab
);
1352 avc_ss_reset(seqno
);
1353 selnl_notify_policyload(seqno
);
1354 selinux_netlbl_cache_invalidate();
1360 sidtab_destroy(&newsidtab
);
1361 policydb_destroy(&newpolicydb
);
1367 * security_port_sid - Obtain the SID for a port.
1368 * @domain: communication domain aka address family
1369 * @type: socket type
1370 * @protocol: protocol number
1371 * @port: port number
1372 * @out_sid: security identifier
1374 int security_port_sid(u16 domain
,
1385 c
= policydb
.ocontexts
[OCON_PORT
];
1387 if (c
->u
.port
.protocol
== protocol
&&
1388 c
->u
.port
.low_port
<= port
&&
1389 c
->u
.port
.high_port
>= port
)
1396 rc
= sidtab_context_to_sid(&sidtab
,
1402 *out_sid
= c
->sid
[0];
1404 *out_sid
= SECINITSID_PORT
;
1413 * security_netif_sid - Obtain the SID for a network interface.
1414 * @name: interface name
1415 * @if_sid: interface SID
1416 * @msg_sid: default SID for received packets
1418 int security_netif_sid(char *name
,
1427 c
= policydb
.ocontexts
[OCON_NETIF
];
1429 if (strcmp(name
, c
->u
.name
) == 0)
1435 if (!c
->sid
[0] || !c
->sid
[1]) {
1436 rc
= sidtab_context_to_sid(&sidtab
,
1441 rc
= sidtab_context_to_sid(&sidtab
,
1447 *if_sid
= c
->sid
[0];
1448 *msg_sid
= c
->sid
[1];
1450 *if_sid
= SECINITSID_NETIF
;
1451 *msg_sid
= SECINITSID_NETMSG
;
1459 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1463 for(i
= 0; i
< 4; i
++)
1464 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1473 * security_node_sid - Obtain the SID for a node (host).
1474 * @domain: communication domain aka address family
1476 * @addrlen: address length in bytes
1477 * @out_sid: security identifier
1479 int security_node_sid(u16 domain
,
1493 if (addrlen
!= sizeof(u32
)) {
1498 addr
= *((u32
*)addrp
);
1500 c
= policydb
.ocontexts
[OCON_NODE
];
1502 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1510 if (addrlen
!= sizeof(u64
) * 2) {
1514 c
= policydb
.ocontexts
[OCON_NODE6
];
1516 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1524 *out_sid
= SECINITSID_NODE
;
1530 rc
= sidtab_context_to_sid(&sidtab
,
1536 *out_sid
= c
->sid
[0];
1538 *out_sid
= SECINITSID_NODE
;
1549 * security_get_user_sids - Obtain reachable SIDs for a user.
1550 * @fromsid: starting SID
1551 * @username: username
1552 * @sids: array of reachable SIDs for user
1553 * @nel: number of elements in @sids
1555 * Generate the set of SIDs for legal security contexts
1556 * for a given user that can be reached by @fromsid.
1557 * Set *@sids to point to a dynamically allocated
1558 * array containing the set of SIDs. Set *@nel to the
1559 * number of elements in the array.
1562 int security_get_user_sids(u32 fromsid
,
1567 struct context
*fromcon
, usercon
;
1568 u32
*mysids
, *mysids2
, sid
;
1569 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1570 struct user_datum
*user
;
1571 struct role_datum
*role
;
1572 struct av_decision avd
;
1573 struct ebitmap_node
*rnode
, *tnode
;
1576 if (!ss_initialized
) {
1584 fromcon
= sidtab_search(&sidtab
, fromsid
);
1590 user
= hashtab_search(policydb
.p_users
.table
, username
);
1595 usercon
.user
= user
->value
;
1597 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
1603 ebitmap_for_each_bit(&user
->roles
, rnode
, i
) {
1604 if (!ebitmap_node_get_bit(rnode
, i
))
1606 role
= policydb
.role_val_to_struct
[i
];
1608 ebitmap_for_each_bit(&role
->types
, tnode
, j
) {
1609 if (!ebitmap_node_get_bit(tnode
, j
))
1613 if (mls_setup_user_range(fromcon
, user
, &usercon
))
1616 rc
= context_struct_compute_av(fromcon
, &usercon
,
1618 PROCESS__TRANSITION
,
1620 if (rc
|| !(avd
.allowed
& PROCESS__TRANSITION
))
1622 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1627 if (mynel
< maxnel
) {
1628 mysids
[mynel
++] = sid
;
1631 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
1637 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1640 mysids
[mynel
++] = sid
;
1655 * security_genfs_sid - Obtain a SID for a file in a filesystem
1656 * @fstype: filesystem type
1657 * @path: path from root of mount
1658 * @sclass: file security class
1659 * @sid: SID for path
1661 * Obtain a SID to use for a file in a filesystem that
1662 * cannot support xattr or use a fixed labeling behavior like
1663 * transition SIDs or task SIDs.
1665 int security_genfs_sid(const char *fstype
,
1671 struct genfs
*genfs
;
1673 int rc
= 0, cmp
= 0;
1677 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1678 cmp
= strcmp(fstype
, genfs
->fstype
);
1683 if (!genfs
|| cmp
) {
1684 *sid
= SECINITSID_UNLABELED
;
1689 for (c
= genfs
->head
; c
; c
= c
->next
) {
1690 len
= strlen(c
->u
.name
);
1691 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1692 (strncmp(c
->u
.name
, path
, len
) == 0))
1697 *sid
= SECINITSID_UNLABELED
;
1703 rc
= sidtab_context_to_sid(&sidtab
,
1717 * security_fs_use - Determine how to handle labeling for a filesystem.
1718 * @fstype: filesystem type
1719 * @behavior: labeling behavior
1720 * @sid: SID for filesystem (superblock)
1722 int security_fs_use(
1724 unsigned int *behavior
,
1732 c
= policydb
.ocontexts
[OCON_FSUSE
];
1734 if (strcmp(fstype
, c
->u
.name
) == 0)
1740 *behavior
= c
->v
.behavior
;
1742 rc
= sidtab_context_to_sid(&sidtab
,
1750 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1752 *behavior
= SECURITY_FS_USE_NONE
;
1755 *behavior
= SECURITY_FS_USE_GENFS
;
1764 int security_get_bools(int *len
, char ***names
, int **values
)
1766 int i
, rc
= -ENOMEM
;
1772 *len
= policydb
.p_bools
.nprim
;
1778 *names
= kcalloc(*len
, sizeof(char*), GFP_ATOMIC
);
1782 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
1786 for (i
= 0; i
< *len
; i
++) {
1788 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1789 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1790 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1793 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1794 (*names
)[i
][name_len
- 1] = 0;
1802 for (i
= 0; i
< *len
; i
++)
1810 int security_set_bools(int len
, int *values
)
1813 int lenp
, seqno
= 0;
1814 struct cond_node
*cur
;
1818 lenp
= policydb
.p_bools
.nprim
;
1824 for (i
= 0; i
< len
; i
++) {
1825 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
1826 audit_log(current
->audit_context
, GFP_ATOMIC
,
1827 AUDIT_MAC_CONFIG_CHANGE
,
1828 "bool=%s val=%d old_val=%d auid=%u",
1829 policydb
.p_bool_val_to_name
[i
],
1831 policydb
.bool_val_to_struct
[i
]->state
,
1832 audit_get_loginuid(current
->audit_context
));
1835 policydb
.bool_val_to_struct
[i
]->state
= 1;
1837 policydb
.bool_val_to_struct
[i
]->state
= 0;
1841 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1842 rc
= evaluate_cond_node(&policydb
, cur
);
1847 seqno
= ++latest_granting
;
1852 avc_ss_reset(seqno
);
1853 selnl_notify_policyload(seqno
);
1858 int security_get_bool_value(int bool)
1865 len
= policydb
.p_bools
.nprim
;
1871 rc
= policydb
.bool_val_to_struct
[bool]->state
;
1878 * security_sid_mls_copy() - computes a new sid based on the given
1879 * sid and the mls portion of mls_sid.
1881 int security_sid_mls_copy(u32 sid
, u32 mls_sid
, u32
*new_sid
)
1883 struct context
*context1
;
1884 struct context
*context2
;
1885 struct context newcon
;
1890 if (!ss_initialized
|| !selinux_mls_enabled
) {
1895 context_init(&newcon
);
1898 context1
= sidtab_search(&sidtab
, sid
);
1900 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
1906 context2
= sidtab_search(&sidtab
, mls_sid
);
1908 printk(KERN_ERR
"security_sid_mls_copy: unrecognized SID "
1914 newcon
.user
= context1
->user
;
1915 newcon
.role
= context1
->role
;
1916 newcon
.type
= context1
->type
;
1917 rc
= mls_copy_context(&newcon
, context2
);
1922 /* Check the validity of the new context. */
1923 if (!policydb_context_isvalid(&policydb
, &newcon
)) {
1924 rc
= convert_context_handle_invalid_context(&newcon
);
1929 rc
= sidtab_context_to_sid(&sidtab
, &newcon
, new_sid
);
1933 if (!context_struct_to_string(&newcon
, &s
, &len
)) {
1934 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
1935 "security_sid_mls_copy: invalid context %s", s
);
1941 context_destroy(&newcon
);
1946 struct selinux_audit_rule
{
1948 struct context au_ctxt
;
1951 void selinux_audit_rule_free(struct selinux_audit_rule
*rule
)
1954 context_destroy(&rule
->au_ctxt
);
1959 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
,
1960 struct selinux_audit_rule
**rule
)
1962 struct selinux_audit_rule
*tmprule
;
1963 struct role_datum
*roledatum
;
1964 struct type_datum
*typedatum
;
1965 struct user_datum
*userdatum
;
1970 if (!ss_initialized
)
1974 case AUDIT_SUBJ_USER
:
1975 case AUDIT_SUBJ_ROLE
:
1976 case AUDIT_SUBJ_TYPE
:
1977 case AUDIT_OBJ_USER
:
1978 case AUDIT_OBJ_ROLE
:
1979 case AUDIT_OBJ_TYPE
:
1980 /* only 'equals' and 'not equals' fit user, role, and type */
1981 if (op
!= AUDIT_EQUAL
&& op
!= AUDIT_NOT_EQUAL
)
1984 case AUDIT_SUBJ_SEN
:
1985 case AUDIT_SUBJ_CLR
:
1986 case AUDIT_OBJ_LEV_LOW
:
1987 case AUDIT_OBJ_LEV_HIGH
:
1988 /* we do not allow a range, indicated by the presense of '-' */
1989 if (strchr(rulestr
, '-'))
1993 /* only the above fields are valid */
1997 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
2001 context_init(&tmprule
->au_ctxt
);
2005 tmprule
->au_seqno
= latest_granting
;
2008 case AUDIT_SUBJ_USER
:
2009 case AUDIT_OBJ_USER
:
2010 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
2014 tmprule
->au_ctxt
.user
= userdatum
->value
;
2016 case AUDIT_SUBJ_ROLE
:
2017 case AUDIT_OBJ_ROLE
:
2018 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
2022 tmprule
->au_ctxt
.role
= roledatum
->value
;
2024 case AUDIT_SUBJ_TYPE
:
2025 case AUDIT_OBJ_TYPE
:
2026 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
2030 tmprule
->au_ctxt
.type
= typedatum
->value
;
2032 case AUDIT_SUBJ_SEN
:
2033 case AUDIT_SUBJ_CLR
:
2034 case AUDIT_OBJ_LEV_LOW
:
2035 case AUDIT_OBJ_LEV_HIGH
:
2036 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
2043 selinux_audit_rule_free(tmprule
);
2052 int selinux_audit_rule_match(u32 sid
, u32 field
, u32 op
,
2053 struct selinux_audit_rule
*rule
,
2054 struct audit_context
*actx
)
2056 struct context
*ctxt
;
2057 struct mls_level
*level
;
2061 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2062 "selinux_audit_rule_match: missing rule\n");
2068 if (rule
->au_seqno
< latest_granting
) {
2069 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2070 "selinux_audit_rule_match: stale rule\n");
2075 ctxt
= sidtab_search(&sidtab
, sid
);
2077 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2078 "selinux_audit_rule_match: unrecognized SID %d\n",
2084 /* a field/op pair that is not caught here will simply fall through
2087 case AUDIT_SUBJ_USER
:
2088 case AUDIT_OBJ_USER
:
2091 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
2093 case AUDIT_NOT_EQUAL
:
2094 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
2098 case AUDIT_SUBJ_ROLE
:
2099 case AUDIT_OBJ_ROLE
:
2102 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
2104 case AUDIT_NOT_EQUAL
:
2105 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
2109 case AUDIT_SUBJ_TYPE
:
2110 case AUDIT_OBJ_TYPE
:
2113 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
2115 case AUDIT_NOT_EQUAL
:
2116 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
2120 case AUDIT_SUBJ_SEN
:
2121 case AUDIT_SUBJ_CLR
:
2122 case AUDIT_OBJ_LEV_LOW
:
2123 case AUDIT_OBJ_LEV_HIGH
:
2124 level
= ((field
== AUDIT_SUBJ_SEN
||
2125 field
== AUDIT_OBJ_LEV_LOW
) ?
2126 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
2129 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2132 case AUDIT_NOT_EQUAL
:
2133 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2136 case AUDIT_LESS_THAN
:
2137 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2139 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2142 case AUDIT_LESS_THAN_OR_EQUAL
:
2143 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2146 case AUDIT_GREATER_THAN
:
2147 match
= (mls_level_dom(level
,
2148 &rule
->au_ctxt
.range
.level
[0]) &&
2149 !mls_level_eq(level
,
2150 &rule
->au_ctxt
.range
.level
[0]));
2152 case AUDIT_GREATER_THAN_OR_EQUAL
:
2153 match
= mls_level_dom(level
,
2154 &rule
->au_ctxt
.range
.level
[0]);
2164 static int (*aurule_callback
)(void) = NULL
;
2166 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2167 u16
class, u32 perms
, u32
*retained
)
2171 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2172 err
= aurule_callback();
2176 static int __init
aurule_init(void)
2180 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2181 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2183 panic("avc_add_callback() failed, error %d\n", err
);
2187 __initcall(aurule_init
);
2189 void selinux_audit_set_callback(int (*callback
)(void))
2191 aurule_callback
= callback
;
2194 #ifdef CONFIG_NETLABEL
2196 * This is the structure we store inside the NetLabel cache block.
2198 #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2199 #define NETLBL_CACHE_T_NONE 0
2200 #define NETLBL_CACHE_T_SID 1
2201 #define NETLBL_CACHE_T_MLS 2
2202 struct netlbl_cache
{
2206 struct mls_range mls_label
;
2211 * selinux_netlbl_cache_free - Free the NetLabel cached data
2212 * @data: the data to free
2215 * This function is intended to be used as the free() callback inside the
2216 * netlbl_lsm_cache structure.
2219 static void selinux_netlbl_cache_free(const void *data
)
2221 struct netlbl_cache
*cache
;
2226 cache
= NETLBL_CACHE(data
);
2227 switch (cache
->type
) {
2228 case NETLBL_CACHE_T_MLS
:
2229 ebitmap_destroy(&cache
->data
.mls_label
.level
[0].cat
);
2236 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2238 * @ctx: the SELinux context
2241 * Attempt to cache the context in @ctx, which was derived from the packet in
2242 * @skb, in the NetLabel subsystem cache.
2245 static void selinux_netlbl_cache_add(struct sk_buff
*skb
, struct context
*ctx
)
2247 struct netlbl_cache
*cache
= NULL
;
2248 struct netlbl_lsm_secattr secattr
;
2250 netlbl_secattr_init(&secattr
);
2251 secattr
.cache
= netlbl_secattr_cache_alloc(GFP_ATOMIC
);
2252 if (secattr
.cache
== NULL
)
2253 goto netlbl_cache_add_return
;
2255 cache
= kzalloc(sizeof(*cache
), GFP_ATOMIC
);
2257 goto netlbl_cache_add_return
;
2259 cache
->type
= NETLBL_CACHE_T_MLS
;
2260 if (ebitmap_cpy(&cache
->data
.mls_label
.level
[0].cat
,
2261 &ctx
->range
.level
[0].cat
) != 0)
2262 goto netlbl_cache_add_return
;
2263 cache
->data
.mls_label
.level
[1].cat
.highbit
=
2264 cache
->data
.mls_label
.level
[0].cat
.highbit
;
2265 cache
->data
.mls_label
.level
[1].cat
.node
=
2266 cache
->data
.mls_label
.level
[0].cat
.node
;
2267 cache
->data
.mls_label
.level
[0].sens
= ctx
->range
.level
[0].sens
;
2268 cache
->data
.mls_label
.level
[1].sens
= ctx
->range
.level
[0].sens
;
2270 secattr
.cache
->free
= selinux_netlbl_cache_free
;
2271 secattr
.cache
->data
= (void *)cache
;
2272 secattr
.flags
= NETLBL_SECATTR_CACHE
;
2274 netlbl_cache_add(skb
, &secattr
);
2276 netlbl_cache_add_return
:
2277 netlbl_secattr_destroy(&secattr
);
2281 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2284 * Invalidate the NetLabel security attribute mapping cache.
2287 void selinux_netlbl_cache_invalidate(void)
2289 netlbl_cache_invalidate();
2293 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2294 * @skb: the network packet
2295 * @secattr: the NetLabel packet security attributes
2296 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2297 * @sid: the SELinux SID
2300 * Convert the given NetLabel packet security attributes in @secattr into a
2301 * SELinux SID. If the @secattr field does not contain a full SELinux
2302 * SID/context then use the context in @base_sid as the foundation. If @skb
2303 * is not NULL attempt to cache as much data as possibile. Returns zero on
2304 * success, negative values on failure.
2307 static int selinux_netlbl_secattr_to_sid(struct sk_buff
*skb
,
2308 struct netlbl_lsm_secattr
*secattr
,
2313 struct context
*ctx
;
2314 struct context ctx_new
;
2315 struct netlbl_cache
*cache
;
2319 if (secattr
->flags
& NETLBL_SECATTR_CACHE
) {
2320 cache
= NETLBL_CACHE(secattr
->cache
->data
);
2321 switch (cache
->type
) {
2322 case NETLBL_CACHE_T_SID
:
2323 *sid
= cache
->data
.sid
;
2326 case NETLBL_CACHE_T_MLS
:
2327 ctx
= sidtab_search(&sidtab
, base_sid
);
2329 goto netlbl_secattr_to_sid_return
;
2331 ctx_new
.user
= ctx
->user
;
2332 ctx_new
.role
= ctx
->role
;
2333 ctx_new
.type
= ctx
->type
;
2334 ctx_new
.range
.level
[0].sens
=
2335 cache
->data
.mls_label
.level
[0].sens
;
2336 ctx_new
.range
.level
[0].cat
.highbit
=
2337 cache
->data
.mls_label
.level
[0].cat
.highbit
;
2338 ctx_new
.range
.level
[0].cat
.node
=
2339 cache
->data
.mls_label
.level
[0].cat
.node
;
2340 ctx_new
.range
.level
[1].sens
=
2341 cache
->data
.mls_label
.level
[1].sens
;
2342 ctx_new
.range
.level
[1].cat
.highbit
=
2343 cache
->data
.mls_label
.level
[1].cat
.highbit
;
2344 ctx_new
.range
.level
[1].cat
.node
=
2345 cache
->data
.mls_label
.level
[1].cat
.node
;
2347 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
2350 goto netlbl_secattr_to_sid_return
;
2352 } else if (secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) {
2353 ctx
= sidtab_search(&sidtab
, base_sid
);
2355 goto netlbl_secattr_to_sid_return
;
2357 ctx_new
.user
= ctx
->user
;
2358 ctx_new
.role
= ctx
->role
;
2359 ctx_new
.type
= ctx
->type
;
2360 mls_import_lvl(&ctx_new
, secattr
->mls_lvl
, secattr
->mls_lvl
);
2361 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
2362 if (mls_import_cat(&ctx_new
,
2364 secattr
->mls_cat_len
,
2367 goto netlbl_secattr_to_sid_return
;
2368 ctx_new
.range
.level
[1].cat
.highbit
=
2369 ctx_new
.range
.level
[0].cat
.highbit
;
2370 ctx_new
.range
.level
[1].cat
.node
=
2371 ctx_new
.range
.level
[0].cat
.node
;
2373 ebitmap_init(&ctx_new
.range
.level
[0].cat
);
2374 ebitmap_init(&ctx_new
.range
.level
[1].cat
);
2376 if (mls_context_isvalid(&policydb
, &ctx_new
) != 1)
2377 goto netlbl_secattr_to_sid_return_cleanup
;
2379 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
2381 goto netlbl_secattr_to_sid_return_cleanup
;
2384 selinux_netlbl_cache_add(skb
, &ctx_new
);
2385 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2391 netlbl_secattr_to_sid_return
:
2394 netlbl_secattr_to_sid_return_cleanup
:
2395 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
2396 goto netlbl_secattr_to_sid_return
;
2400 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2402 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2406 * Call the NetLabel mechanism to get the security attributes of the given
2407 * packet and use those attributes to determine the correct context/SID to
2408 * assign to the packet. Returns zero on success, negative values on failure.
2411 static int selinux_netlbl_skbuff_getsid(struct sk_buff
*skb
,
2416 struct netlbl_lsm_secattr secattr
;
2418 netlbl_secattr_init(&secattr
);
2419 rc
= netlbl_skbuff_getattr(skb
, &secattr
);
2420 if (rc
== 0 && secattr
.flags
!= NETLBL_SECATTR_NONE
)
2421 rc
= selinux_netlbl_secattr_to_sid(skb
,
2427 netlbl_secattr_destroy(&secattr
);
2433 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2434 * @sock: the socket to label
2435 * @sid: the SID to use
2438 * Attempt to label a socket using the NetLabel mechanism using the given
2439 * SID. Returns zero values on success, negative values on failure. The
2440 * caller is responsibile for calling rcu_read_lock() before calling this
2441 * this function and rcu_read_unlock() after this function returns.
2444 static int selinux_netlbl_socket_setsid(struct socket
*sock
, u32 sid
)
2447 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
2448 struct netlbl_lsm_secattr secattr
;
2449 struct context
*ctx
;
2451 if (!ss_initialized
)
2454 netlbl_secattr_init(&secattr
);
2458 ctx
= sidtab_search(&sidtab
, sid
);
2460 goto netlbl_socket_setsid_return
;
2462 secattr
.domain
= kstrdup(policydb
.p_type_val_to_name
[ctx
->type
- 1],
2464 mls_export_lvl(ctx
, &secattr
.mls_lvl
, NULL
);
2465 rc
= mls_export_cat(ctx
,
2467 &secattr
.mls_cat_len
,
2471 goto netlbl_socket_setsid_return
;
2473 secattr
.flags
|= NETLBL_SECATTR_DOMAIN
| NETLBL_SECATTR_MLS_LVL
;
2474 if (secattr
.mls_cat
)
2475 secattr
.flags
|= NETLBL_SECATTR_MLS_CAT
;
2477 rc
= netlbl_socket_setattr(sock
, &secattr
);
2479 spin_lock(&sksec
->nlbl_lock
);
2480 sksec
->nlbl_state
= NLBL_LABELED
;
2481 spin_unlock(&sksec
->nlbl_lock
);
2484 netlbl_socket_setsid_return
:
2486 netlbl_secattr_destroy(&secattr
);
2491 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
2492 * @ssec: the sk_security_struct
2493 * @family: the socket family
2496 * Called when the NetLabel state of a sk_security_struct needs to be reset.
2497 * The caller is responsibile for all the NetLabel sk_security_struct locking.
2500 void selinux_netlbl_sk_security_reset(struct sk_security_struct
*ssec
,
2503 if (family
== PF_INET
)
2504 ssec
->nlbl_state
= NLBL_REQUIRE
;
2506 ssec
->nlbl_state
= NLBL_UNSET
;
2510 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2511 * @ssec: the sk_security_struct
2512 * @family: the socket family
2515 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2519 void selinux_netlbl_sk_security_init(struct sk_security_struct
*ssec
,
2522 /* No locking needed, we are the only one who has access to ssec */
2523 selinux_netlbl_sk_security_reset(ssec
, family
);
2524 spin_lock_init(&ssec
->nlbl_lock
);
2528 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
2529 * @ssec: the original sk_security_struct
2530 * @newssec: the cloned sk_security_struct
2533 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2537 void selinux_netlbl_sk_security_clone(struct sk_security_struct
*ssec
,
2538 struct sk_security_struct
*newssec
)
2540 /* We don't need to take newssec->nlbl_lock because we are the only
2541 * thread with access to newssec, but we do need to take the RCU read
2542 * lock as other threads could have access to ssec */
2544 selinux_netlbl_sk_security_reset(newssec
, ssec
->sk
->sk_family
);
2545 newssec
->sclass
= ssec
->sclass
;
2550 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2551 * @sock: the socket to label
2554 * Attempt to label a socket using the NetLabel mechanism using the given
2555 * SID. Returns zero values on success, negative values on failure.
2558 int selinux_netlbl_socket_post_create(struct socket
*sock
)
2561 struct inode_security_struct
*isec
= SOCK_INODE(sock
)->i_security
;
2562 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
2564 sksec
->sclass
= isec
->sclass
;
2567 if (sksec
->nlbl_state
== NLBL_REQUIRE
)
2568 rc
= selinux_netlbl_socket_setsid(sock
, sksec
->sid
);
2575 * selinux_netlbl_sock_graft - Netlabel the new socket
2576 * @sk: the new connection
2577 * @sock: the new socket
2580 * The connection represented by @sk is being grafted onto @sock so set the
2581 * socket's NetLabel to match the SID of @sk.
2584 void selinux_netlbl_sock_graft(struct sock
*sk
, struct socket
*sock
)
2586 struct inode_security_struct
*isec
= SOCK_INODE(sock
)->i_security
;
2587 struct sk_security_struct
*sksec
= sk
->sk_security
;
2588 struct netlbl_lsm_secattr secattr
;
2591 sksec
->sclass
= isec
->sclass
;
2595 if (sksec
->nlbl_state
!= NLBL_REQUIRE
) {
2600 netlbl_secattr_init(&secattr
);
2601 if (netlbl_sock_getattr(sk
, &secattr
) == 0 &&
2602 secattr
.flags
!= NETLBL_SECATTR_NONE
&&
2603 selinux_netlbl_secattr_to_sid(NULL
,
2605 SECINITSID_UNLABELED
,
2606 &nlbl_peer_sid
) == 0)
2607 sksec
->peer_sid
= nlbl_peer_sid
;
2608 netlbl_secattr_destroy(&secattr
);
2610 /* Try to set the NetLabel on the socket to save time later, if we fail
2611 * here we will pick up the pieces in later calls to
2612 * selinux_netlbl_inode_permission(). */
2613 selinux_netlbl_socket_setsid(sock
, sksec
->sid
);
2619 * selinux_netlbl_inet_conn_request - Handle a new connection request
2621 * @sock_sid: the SID of the parent socket
2624 * If present, use the security attributes of the packet in @skb and the
2625 * parent sock's SID to arrive at a SID for the new child sock. Returns the
2626 * SID of the connection or SECSID_NULL on failure.
2629 u32
selinux_netlbl_inet_conn_request(struct sk_buff
*skb
, u32 sock_sid
)
2634 rc
= selinux_netlbl_skbuff_getsid(skb
, sock_sid
, &peer_sid
);
2642 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
2643 * @inode: the file descriptor's inode
2644 * @mask: the permission mask
2647 * Looks at a file's inode and if it is marked as a socket protected by
2648 * NetLabel then verify that the socket has been labeled, if not try to label
2649 * the socket now with the inode's SID. Returns zero on success, negative
2650 * values on failure.
2653 int selinux_netlbl_inode_permission(struct inode
*inode
, int mask
)
2656 struct sk_security_struct
*sksec
;
2657 struct socket
*sock
;
2659 if (!S_ISSOCK(inode
->i_mode
) ||
2660 ((mask
& (MAY_WRITE
| MAY_APPEND
)) == 0))
2662 sock
= SOCKET_I(inode
);
2663 sksec
= sock
->sk
->sk_security
;
2666 if (sksec
->nlbl_state
!= NLBL_REQUIRE
) {
2670 lock_sock(sock
->sk
);
2671 rc
= selinux_netlbl_socket_setsid(sock
, sksec
->sid
);
2672 release_sock(sock
->sk
);
2679 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2680 * @sksec: the sock's sk_security_struct
2682 * @ad: the audit data
2685 * Fetch the NetLabel security attributes from @skb and perform an access check
2686 * against the receiving socket. Returns zero on success, negative values on
2690 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct
*sksec
,
2691 struct sk_buff
*skb
,
2692 struct avc_audit_data
*ad
)
2698 rc
= selinux_netlbl_skbuff_getsid(skb
,
2699 SECINITSID_UNLABELED
,
2704 if (netlbl_sid
== SECSID_NULL
)
2707 switch (sksec
->sclass
) {
2708 case SECCLASS_UDP_SOCKET
:
2709 recv_perm
= UDP_SOCKET__RECVFROM
;
2711 case SECCLASS_TCP_SOCKET
:
2712 recv_perm
= TCP_SOCKET__RECVFROM
;
2715 recv_perm
= RAWIP_SOCKET__RECVFROM
;
2718 rc
= avc_has_perm(sksec
->sid
,
2726 netlbl_skbuff_err(skb
, rc
);
2731 * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID
2735 * Examine @sock to find the connected peer's SID. Returns the SID on success
2736 * or SECSID_NULL on error.
2739 u32
selinux_netlbl_socket_getpeersec_stream(struct socket
*sock
)
2741 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
2742 return sksec
->peer_sid
;
2746 * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet
2750 * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on
2751 * success, SECSID_NULL on error.
2754 u32
selinux_netlbl_socket_getpeersec_dgram(struct sk_buff
*skb
)
2758 if (selinux_netlbl_skbuff_getsid(skb
,
2759 SECINITSID_UNLABELED
,
2767 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
2769 * @level: the socket level or protocol
2770 * @optname: the socket option name
2773 * Check the setsockopt() call and if the user is trying to replace the IP
2774 * options on a socket and a NetLabel is in place for the socket deny the
2775 * access; otherwise allow the access. Returns zero when the access is
2776 * allowed, -EACCES when denied, and other negative values on error.
2779 int selinux_netlbl_socket_setsockopt(struct socket
*sock
,
2784 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
2785 struct netlbl_lsm_secattr secattr
;
2788 if (level
== IPPROTO_IP
&& optname
== IP_OPTIONS
&&
2789 sksec
->nlbl_state
== NLBL_LABELED
) {
2790 netlbl_secattr_init(&secattr
);
2791 rc
= netlbl_socket_getattr(sock
, &secattr
);
2792 if (rc
== 0 && secattr
.flags
!= NETLBL_SECATTR_NONE
)
2794 netlbl_secattr_destroy(&secattr
);
2800 #endif /* CONFIG_NETLABEL */