2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/spinlock.h>
26 #include <linux/errno.h>
28 #include <linux/sched.h>
29 #include <linux/audit.h>
30 #include <asm/semaphore.h>
39 #include "conditional.h"
42 extern void selnl_notify_policyload(u32 seqno
);
43 extern int policydb_loaded_version
;
45 static rwlock_t policy_rwlock
= RW_LOCK_UNLOCKED
;
46 #define POLICY_RDLOCK read_lock(&policy_rwlock)
47 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
48 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
49 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
51 static DECLARE_MUTEX(load_sem
);
52 #define LOAD_LOCK down(&load_sem)
53 #define LOAD_UNLOCK up(&load_sem)
56 struct policydb policydb
;
57 int ss_initialized
= 0;
60 * The largest sequence number that has been used when
61 * providing an access decision to the access vector cache.
62 * The sequence number only changes when a policy change
65 static u32 latest_granting
= 0;
68 * Return the boolean value of a constraint expression
69 * when it is applied to the specified source and target
72 static int constraint_expr_eval(struct context
*scontext
,
73 struct context
*tcontext
,
74 struct constraint_expr
*cexpr
)
78 struct role_datum
*r1
, *r2
;
79 struct constraint_expr
*e
;
80 int s
[CEXPR_MAXDEPTH
];
83 for (e
= cexpr
; e
; e
= e
->next
) {
84 switch (e
->expr_type
) {
100 if (sp
== (CEXPR_MAXDEPTH
-1))
104 val1
= scontext
->user
;
105 val2
= tcontext
->user
;
108 val1
= scontext
->type
;
109 val2
= tcontext
->type
;
112 val1
= scontext
->role
;
113 val2
= tcontext
->role
;
114 r1
= policydb
.role_val_to_struct
[val1
- 1];
115 r2
= policydb
.role_val_to_struct
[val2
- 1];
118 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
122 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
126 s
[++sp
] = ( !ebitmap_get_bit(&r1
->dominates
,
128 !ebitmap_get_bit(&r2
->dominates
,
142 s
[++sp
] = (val1
== val2
);
145 s
[++sp
] = (val1
!= val2
);
153 if (sp
== (CEXPR_MAXDEPTH
-1))
156 if (e
->attr
& CEXPR_TARGET
)
158 if (e
->attr
& CEXPR_USER
)
160 else if (e
->attr
& CEXPR_ROLE
)
162 else if (e
->attr
& CEXPR_TYPE
)
171 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
174 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
192 * Compute access vectors based on a context structure pair for
193 * the permissions in a particular class.
195 static int context_struct_compute_av(struct context
*scontext
,
196 struct context
*tcontext
,
199 struct av_decision
*avd
)
201 struct constraint_node
*constraint
;
202 struct role_allow
*ra
;
203 struct avtab_key avkey
;
204 struct avtab_datum
*avdatum
;
205 struct class_datum
*tclass_datum
;
208 * Remap extended Netlink classes for old policy versions.
209 * Do this here rather than socket_type_to_security_class()
210 * in case a newer policy version is loaded, allowing sockets
211 * to remain in the correct class.
213 if (policydb_loaded_version
< POLICYDB_VERSION_NLCLASS
)
214 if (tclass
>= SECCLASS_NETLINK_ROUTE_SOCKET
&&
215 tclass
<= SECCLASS_NETLINK_DNRT_SOCKET
)
216 tclass
= SECCLASS_NETLINK_SOCKET
;
218 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
219 printk(KERN_ERR
"security_compute_av: unrecognized class %d\n",
223 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
226 * Initialize the access vectors to the default values.
229 avd
->decided
= 0xffffffff;
231 avd
->auditdeny
= 0xffffffff;
232 avd
->seqno
= latest_granting
;
235 * If a specific type enforcement rule was defined for
236 * this permission check, then use it.
238 avkey
.source_type
= scontext
->type
;
239 avkey
.target_type
= tcontext
->type
;
240 avkey
.target_class
= tclass
;
241 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
, AVTAB_AV
);
243 if (avdatum
->specified
& AVTAB_ALLOWED
)
244 avd
->allowed
= avtab_allowed(avdatum
);
245 if (avdatum
->specified
& AVTAB_AUDITDENY
)
246 avd
->auditdeny
= avtab_auditdeny(avdatum
);
247 if (avdatum
->specified
& AVTAB_AUDITALLOW
)
248 avd
->auditallow
= avtab_auditallow(avdatum
);
251 /* Check conditional av table for additional permissions */
252 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
255 * Remove any permissions prohibited by the MLS policy.
257 mls_compute_av(scontext
, tcontext
, tclass_datum
, &avd
->allowed
);
260 * Remove any permissions prohibited by a constraint.
262 constraint
= tclass_datum
->constraints
;
264 if ((constraint
->permissions
& (avd
->allowed
)) &&
265 !constraint_expr_eval(scontext
, tcontext
,
267 avd
->allowed
= (avd
->allowed
) & ~(constraint
->permissions
);
269 constraint
= constraint
->next
;
273 * If checking process transition permission and the
274 * role is changing, then check the (current_role, new_role)
277 if (tclass
== SECCLASS_PROCESS
&&
278 (avd
->allowed
& PROCESS__TRANSITION
) &&
279 scontext
->role
!= tcontext
->role
) {
280 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
281 if (scontext
->role
== ra
->role
&&
282 tcontext
->role
== ra
->new_role
)
286 avd
->allowed
= (avd
->allowed
) & ~(PROCESS__TRANSITION
);
293 * security_compute_av - Compute access vector decisions.
294 * @ssid: source security identifier
295 * @tsid: target security identifier
296 * @tclass: target security class
297 * @requested: requested permissions
298 * @avd: access vector decisions
300 * Compute a set of access vector decisions based on the
301 * SID pair (@ssid, @tsid) for the permissions in @tclass.
302 * Return -%EINVAL if any of the parameters are invalid or %0
303 * if the access vector decisions were computed successfully.
305 int security_compute_av(u32 ssid
,
309 struct av_decision
*avd
)
311 struct context
*scontext
= NULL
, *tcontext
= NULL
;
314 if (!ss_initialized
) {
315 avd
->allowed
= requested
;
316 avd
->decided
= requested
;
318 avd
->auditdeny
= 0xffffffff;
319 avd
->seqno
= latest_granting
;
325 scontext
= sidtab_search(&sidtab
, ssid
);
327 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
332 tcontext
= sidtab_search(&sidtab
, tsid
);
334 printk(KERN_ERR
"security_compute_av: unrecognized SID %d\n",
340 rc
= context_struct_compute_av(scontext
, tcontext
, tclass
,
348 * Write the security context string representation of
349 * the context structure `context' into a dynamically
350 * allocated string of the correct size. Set `*scontext'
351 * to point to this string and set `*scontext_len' to
352 * the length of the string.
354 int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
361 /* Compute the size of the context. */
362 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
363 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
364 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
365 *scontext_len
+= mls_compute_context_len(context
);
367 /* Allocate space for the context; caller must free this space. */
368 scontextp
= kmalloc(*scontext_len
+1,GFP_ATOMIC
);
372 *scontext
= scontextp
;
375 * Copy the user name, role name and type name into the context.
377 sprintf(scontextp
, "%s:%s:%s:",
378 policydb
.p_user_val_to_name
[context
->user
- 1],
379 policydb
.p_role_val_to_name
[context
->role
- 1],
380 policydb
.p_type_val_to_name
[context
->type
- 1]);
381 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
382 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
383 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
385 mls_sid_to_context(context
, &scontextp
);
393 #include "initial_sid_to_string.h"
396 * security_sid_to_context - Obtain a context for a given SID.
397 * @sid: security identifier, SID
398 * @scontext: security context
399 * @scontext_len: length in bytes
401 * Write the string representation of the context associated with @sid
402 * into a dynamically allocated string of the correct size. Set @scontext
403 * to point to this string and set @scontext_len to the length of the string.
405 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
407 struct context
*context
;
410 if (!ss_initialized
) {
411 if (sid
<= SECINITSID_NUM
) {
414 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
415 scontextp
= kmalloc(*scontext_len
,GFP_ATOMIC
);
416 strcpy(scontextp
, initial_sid_to_string
[sid
]);
417 *scontext
= scontextp
;
420 printk(KERN_ERR
"security_sid_to_context: called before initial "
421 "load_policy on unknown SID %d\n", sid
);
426 context
= sidtab_search(&sidtab
, sid
);
428 printk(KERN_ERR
"security_sid_to_context: unrecognized SID "
433 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
442 * security_context_to_sid - Obtain a SID for a given security context.
443 * @scontext: security context
444 * @scontext_len: length in bytes
445 * @sid: security identifier, SID
447 * Obtains a SID associated with the security context that
448 * has the string representation specified by @scontext.
449 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
450 * memory is available, or 0 on success.
452 int security_context_to_sid(char *scontext
, u32 scontext_len
, u32
*sid
)
455 struct context context
;
456 struct role_datum
*role
;
457 struct type_datum
*typdatum
;
458 struct user_datum
*usrdatum
;
459 char *scontextp
, *p
, oldc
;
462 if (!ss_initialized
) {
465 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
466 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
471 *sid
= SECINITSID_KERNEL
;
476 /* Copy the string so that we can modify the copy as we parse it.
477 The string should already by null terminated, but we append a
478 null suffix to the copy to avoid problems with the existing
479 attr package, which doesn't view the null terminator as part
480 of the attribute value. */
481 scontext2
= kmalloc(scontext_len
+1,GFP_KERNEL
);
486 memcpy(scontext2
, scontext
, scontext_len
);
487 scontext2
[scontext_len
] = 0;
489 context_init(&context
);
494 /* Parse the security context. */
497 scontextp
= (char *) scontext2
;
499 /* Extract the user. */
501 while (*p
&& *p
!= ':')
509 usrdatum
= hashtab_search(policydb
.p_users
.table
, scontextp
);
513 context
.user
= usrdatum
->value
;
517 while (*p
&& *p
!= ':')
525 role
= hashtab_search(policydb
.p_roles
.table
, scontextp
);
528 context
.role
= role
->value
;
532 while (*p
&& *p
!= ':')
537 typdatum
= hashtab_search(policydb
.p_types
.table
, scontextp
);
541 context
.type
= typdatum
->value
;
543 rc
= mls_context_to_sid(oldc
, &p
, &context
);
547 if ((p
- scontext2
) < scontext_len
) {
552 /* Check the validity of the new context. */
553 if (!policydb_context_isvalid(&policydb
, &context
)) {
557 /* Obtain the new sid. */
558 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
561 context_destroy(&context
);
567 static int compute_sid_handle_invalid_context(
568 struct context
*scontext
,
569 struct context
*tcontext
,
571 struct context
*newcontext
)
573 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
574 u32 slen
, tlen
, nlen
;
576 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
578 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
580 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
582 audit_log(current
->audit_context
,
583 "security_compute_sid: invalid context %s"
587 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
592 if (!selinux_enforcing
)
597 static int security_compute_sid(u32 ssid
,
603 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
604 struct role_trans
*roletr
= NULL
;
605 struct avtab_key avkey
;
606 struct avtab_datum
*avdatum
;
607 struct avtab_node
*node
;
608 unsigned int type_change
= 0;
611 if (!ss_initialized
) {
613 case SECCLASS_PROCESS
:
625 scontext
= sidtab_search(&sidtab
, ssid
);
627 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
632 tcontext
= sidtab_search(&sidtab
, tsid
);
634 printk(KERN_ERR
"security_compute_sid: unrecognized SID %d\n",
640 context_init(&newcontext
);
642 /* Set the user identity. */
644 case AVTAB_TRANSITION
:
646 /* Use the process user identity. */
647 newcontext
.user
= scontext
->user
;
650 /* Use the related object owner. */
651 newcontext
.user
= tcontext
->user
;
655 /* Set the role and type to default values. */
657 case SECCLASS_PROCESS
:
658 /* Use the current role and type of process. */
659 newcontext
.role
= scontext
->role
;
660 newcontext
.type
= scontext
->type
;
663 /* Use the well-defined object role. */
664 newcontext
.role
= OBJECT_R_VAL
;
665 /* Use the type of the related object. */
666 newcontext
.type
= tcontext
->type
;
669 /* Look for a type transition/member/change rule. */
670 avkey
.source_type
= scontext
->type
;
671 avkey
.target_type
= tcontext
->type
;
672 avkey
.target_class
= tclass
;
673 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
, AVTAB_TYPE
);
675 /* If no permanent rule, also check for enabled conditional rules */
677 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
, specified
);
678 for (; node
!= NULL
; node
= avtab_search_node_next(node
, specified
)) {
679 if (node
->datum
.specified
& AVTAB_ENABLED
) {
680 avdatum
= &node
->datum
;
686 type_change
= (avdatum
&& (avdatum
->specified
& specified
));
688 /* Use the type from the type transition/member/change rule. */
690 case AVTAB_TRANSITION
:
691 newcontext
.type
= avtab_transition(avdatum
);
694 newcontext
.type
= avtab_member(avdatum
);
697 newcontext
.type
= avtab_change(avdatum
);
702 /* Check for class-specific changes. */
704 case SECCLASS_PROCESS
:
705 if (specified
& AVTAB_TRANSITION
) {
706 /* Look for a role transition rule. */
707 for (roletr
= policydb
.role_tr
; roletr
;
708 roletr
= roletr
->next
) {
709 if (roletr
->role
== scontext
->role
&&
710 roletr
->type
== tcontext
->type
) {
711 /* Use the role transition rule. */
712 newcontext
.role
= roletr
->new_role
;
718 if (!type_change
&& !roletr
) {
719 /* No change in process role or type. */
727 (newcontext
.user
== tcontext
->user
) &&
728 mls_context_cmp(scontext
, tcontext
)) {
729 /* No change in object type, owner,
730 or MLS attributes. */
737 /* Set the MLS attributes.
738 This is done last because it may allocate memory. */
739 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
743 /* Check the validity of the context. */
744 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
745 rc
= compute_sid_handle_invalid_context(scontext
,
752 /* Obtain the sid for the context. */
753 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
756 context_destroy(&newcontext
);
762 * security_transition_sid - Compute the SID for a new subject/object.
763 * @ssid: source security identifier
764 * @tsid: target security identifier
765 * @tclass: target security class
766 * @out_sid: security identifier for new subject/object
768 * Compute a SID to use for labeling a new subject or object in the
769 * class @tclass based on a SID pair (@ssid, @tsid).
770 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
771 * if insufficient memory is available, or %0 if the new SID was
772 * computed successfully.
774 int security_transition_sid(u32 ssid
,
779 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
, out_sid
);
783 * security_member_sid - Compute the SID for member selection.
784 * @ssid: source security identifier
785 * @tsid: target security identifier
786 * @tclass: target security class
787 * @out_sid: security identifier for selected member
789 * Compute a SID to use when selecting a member of a polyinstantiated
790 * object of class @tclass based on a SID pair (@ssid, @tsid).
791 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
792 * if insufficient memory is available, or %0 if the SID was
793 * computed successfully.
795 int security_member_sid(u32 ssid
,
800 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
);
804 * security_change_sid - Compute the SID for object relabeling.
805 * @ssid: source security identifier
806 * @tsid: target security identifier
807 * @tclass: target security class
808 * @out_sid: security identifier for selected member
810 * Compute a SID to use for relabeling an object of class @tclass
811 * based on a SID pair (@ssid, @tsid).
812 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
813 * if insufficient memory is available, or %0 if the SID was
814 * computed successfully.
816 int security_change_sid(u32 ssid
,
821 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
);
825 * Verify that each permission that is defined under the
826 * existing policy is still defined with the same value
829 static int validate_perm(void *key
, void *datum
, void *p
)
832 struct perm_datum
*perdatum
, *perdatum2
;
839 perdatum2
= hashtab_search(h
, key
);
841 printk(KERN_ERR
"security: permission %s disappeared",
846 if (perdatum
->value
!= perdatum2
->value
) {
847 printk(KERN_ERR
"security: the value of permission %s changed",
856 * Verify that each class that is defined under the
857 * existing policy is still defined with the same
858 * attributes in the new policy.
860 static int validate_class(void *key
, void *datum
, void *p
)
862 struct policydb
*newp
;
863 struct class_datum
*cladatum
, *cladatum2
;
869 cladatum2
= hashtab_search(newp
->p_classes
.table
, key
);
871 printk(KERN_ERR
"security: class %s disappeared\n",
876 if (cladatum
->value
!= cladatum2
->value
) {
877 printk(KERN_ERR
"security: the value of class %s changed\n",
882 if ((cladatum
->comdatum
&& !cladatum2
->comdatum
) ||
883 (!cladatum
->comdatum
&& cladatum2
->comdatum
)) {
884 printk(KERN_ERR
"security: the inherits clause for the access "
885 "vector definition for class %s changed\n", (char *)key
);
889 if (cladatum
->comdatum
) {
890 rc
= hashtab_map(cladatum
->comdatum
->permissions
.table
, validate_perm
,
891 cladatum2
->comdatum
->permissions
.table
);
893 printk(" in the access vector definition for class "
894 "%s\n", (char *)key
);
898 rc
= hashtab_map(cladatum
->permissions
.table
, validate_perm
,
899 cladatum2
->permissions
.table
);
901 printk(" in access vector definition for class %s\n",
907 /* Clone the SID into the new SID table. */
908 static int clone_sid(u32 sid
,
909 struct context
*context
,
912 struct sidtab
*s
= arg
;
914 return sidtab_insert(s
, sid
, context
);
917 static inline int convert_context_handle_invalid_context(struct context
*context
)
921 if (selinux_enforcing
) {
927 context_struct_to_string(context
, &s
, &len
);
928 printk(KERN_ERR
"security: context %s is invalid\n", s
);
934 struct convert_context_args
{
935 struct policydb
*oldp
;
936 struct policydb
*newp
;
940 * Convert the values in the security context
941 * structure `c' from the values specified
942 * in the policy `p->oldp' to the values specified
943 * in the policy `p->newp'. Verify that the
944 * context is valid under the new policy.
946 static int convert_context(u32 key
,
950 struct convert_context_args
*args
;
952 struct role_datum
*role
;
953 struct type_datum
*typdatum
;
954 struct user_datum
*usrdatum
;
961 rc
= context_cpy(&oldc
, c
);
967 /* Convert the user. */
968 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
969 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
973 c
->user
= usrdatum
->value
;
975 /* Convert the role. */
976 role
= hashtab_search(args
->newp
->p_roles
.table
,
977 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
981 c
->role
= role
->value
;
983 /* Convert the type. */
984 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
985 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
989 c
->type
= typdatum
->value
;
991 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
995 /* Check the validity of the new context. */
996 if (!policydb_context_isvalid(args
->newp
, c
)) {
997 rc
= convert_context_handle_invalid_context(&oldc
);
1002 context_destroy(&oldc
);
1006 context_struct_to_string(&oldc
, &s
, &len
);
1007 context_destroy(&oldc
);
1008 printk(KERN_ERR
"security: invalidating context %s\n", s
);
1013 extern void selinux_complete_init(void);
1016 * security_load_policy - Load a security policy configuration.
1017 * @data: binary policy data
1018 * @len: length of data in bytes
1020 * Load a new set of security policy configuration data,
1021 * validate it and convert the SID table as necessary.
1022 * This function will flush the access vector cache after
1023 * loading the new policy.
1025 int security_load_policy(void *data
, size_t len
)
1027 struct policydb oldpolicydb
, newpolicydb
;
1028 struct sidtab oldsidtab
, newsidtab
;
1029 struct convert_context_args args
;
1032 struct policy_file file
= { data
, len
}, *fp
= &file
;
1036 if (!ss_initialized
) {
1038 if (policydb_read(&policydb
, fp
)) {
1042 if (policydb_load_isids(&policydb
, &sidtab
)) {
1044 policydb_destroy(&policydb
);
1050 selinux_complete_init();
1055 sidtab_hash_eval(&sidtab
, "sids");
1058 if (policydb_read(&newpolicydb
, fp
)) {
1063 sidtab_init(&newsidtab
);
1065 /* Verify that the existing classes did not change. */
1066 if (hashtab_map(policydb
.p_classes
.table
, validate_class
, &newpolicydb
)) {
1067 printk(KERN_ERR
"security: the definition of an existing "
1073 /* Clone the SID table. */
1074 sidtab_shutdown(&sidtab
);
1075 if (sidtab_map(&sidtab
, clone_sid
, &newsidtab
)) {
1080 /* Convert the internal representations of contexts
1081 in the new SID table and remove invalid SIDs. */
1082 args
.oldp
= &policydb
;
1083 args
.newp
= &newpolicydb
;
1084 sidtab_map_remove_on_error(&newsidtab
, convert_context
, &args
);
1086 /* Save the old policydb and SID table to free later. */
1087 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1088 sidtab_set(&oldsidtab
, &sidtab
);
1090 /* Install the new policydb and SID table. */
1092 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1093 sidtab_set(&sidtab
, &newsidtab
);
1094 seqno
= ++latest_granting
;
1099 /* Free the old policydb and SID table. */
1100 policydb_destroy(&oldpolicydb
);
1101 sidtab_destroy(&oldsidtab
);
1103 avc_ss_reset(seqno
);
1104 selnl_notify_policyload(seqno
);
1110 sidtab_destroy(&newsidtab
);
1111 policydb_destroy(&newpolicydb
);
1117 * security_port_sid - Obtain the SID for a port.
1118 * @domain: communication domain aka address family
1119 * @type: socket type
1120 * @protocol: protocol number
1121 * @port: port number
1122 * @out_sid: security identifier
1124 int security_port_sid(u16 domain
,
1135 c
= policydb
.ocontexts
[OCON_PORT
];
1137 if (c
->u
.port
.protocol
== protocol
&&
1138 c
->u
.port
.low_port
<= port
&&
1139 c
->u
.port
.high_port
>= port
)
1146 rc
= sidtab_context_to_sid(&sidtab
,
1152 *out_sid
= c
->sid
[0];
1154 *out_sid
= SECINITSID_PORT
;
1163 * security_netif_sid - Obtain the SID for a network interface.
1164 * @name: interface name
1165 * @if_sid: interface SID
1166 * @msg_sid: default SID for received packets
1168 int security_netif_sid(char *name
,
1177 c
= policydb
.ocontexts
[OCON_NETIF
];
1179 if (strcmp(name
, c
->u
.name
) == 0)
1185 if (!c
->sid
[0] || !c
->sid
[1]) {
1186 rc
= sidtab_context_to_sid(&sidtab
,
1191 rc
= sidtab_context_to_sid(&sidtab
,
1197 *if_sid
= c
->sid
[0];
1198 *msg_sid
= c
->sid
[1];
1200 *if_sid
= SECINITSID_NETIF
;
1201 *msg_sid
= SECINITSID_NETMSG
;
1209 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1213 for(i
= 0; i
< 4; i
++)
1214 if(addr
[i
] != (input
[i
] & mask
[i
])) {
1223 * security_node_sid - Obtain the SID for a node (host).
1224 * @domain: communication domain aka address family
1226 * @addrlen: address length in bytes
1227 * @out_sid: security identifier
1229 int security_node_sid(u16 domain
,
1243 if (addrlen
!= sizeof(u32
)) {
1248 addr
= *((u32
*)addrp
);
1250 c
= policydb
.ocontexts
[OCON_NODE
];
1252 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
1260 if (addrlen
!= sizeof(u64
) * 2) {
1264 c
= policydb
.ocontexts
[OCON_NODE6
];
1266 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
1274 *out_sid
= SECINITSID_NODE
;
1280 rc
= sidtab_context_to_sid(&sidtab
,
1286 *out_sid
= c
->sid
[0];
1288 *out_sid
= SECINITSID_NODE
;
1299 * security_get_user_sids - Obtain reachable SIDs for a user.
1300 * @fromsid: starting SID
1301 * @username: username
1302 * @sids: array of reachable SIDs for user
1303 * @nel: number of elements in @sids
1305 * Generate the set of SIDs for legal security contexts
1306 * for a given user that can be reached by @fromsid.
1307 * Set *@sids to point to a dynamically allocated
1308 * array containing the set of SIDs. Set *@nel to the
1309 * number of elements in the array.
1312 int security_get_user_sids(u32 fromsid
,
1317 struct context
*fromcon
, usercon
;
1318 u32
*mysids
, *mysids2
, sid
;
1319 u32 mynel
= 0, maxnel
= SIDS_NEL
;
1320 struct user_datum
*user
;
1321 struct role_datum
*role
;
1322 struct av_decision avd
;
1325 if (!ss_initialized
) {
1333 fromcon
= sidtab_search(&sidtab
, fromsid
);
1339 user
= hashtab_search(policydb
.p_users
.table
, username
);
1344 usercon
.user
= user
->value
;
1346 mysids
= kmalloc(maxnel
*sizeof(*mysids
), GFP_ATOMIC
);
1351 memset(mysids
, 0, maxnel
*sizeof(*mysids
));
1353 for (i
= ebitmap_startbit(&user
->roles
); i
< ebitmap_length(&user
->roles
); i
++) {
1354 if (!ebitmap_get_bit(&user
->roles
, i
))
1356 role
= policydb
.role_val_to_struct
[i
];
1358 for (j
= ebitmap_startbit(&role
->types
); j
< ebitmap_length(&role
->types
); j
++) {
1359 if (!ebitmap_get_bit(&role
->types
, j
))
1362 mls_for_user_ranges(user
,usercon
) {
1363 rc
= context_struct_compute_av(fromcon
, &usercon
,
1365 PROCESS__TRANSITION
,
1367 if (rc
|| !(avd
.allowed
& PROCESS__TRANSITION
))
1369 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
1374 if (mynel
< maxnel
) {
1375 mysids
[mynel
++] = sid
;
1378 mysids2
= kmalloc(maxnel
*sizeof(*mysids2
), GFP_ATOMIC
);
1384 memset(mysids2
, 0, maxnel
*sizeof(*mysids2
));
1385 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
1388 mysids
[mynel
++] = sid
;
1391 mls_end_user_ranges
;
1405 * security_genfs_sid - Obtain a SID for a file in a filesystem
1406 * @fstype: filesystem type
1407 * @path: path from root of mount
1408 * @sclass: file security class
1409 * @sid: SID for path
1411 * Obtain a SID to use for a file in a filesystem that
1412 * cannot support xattr or use a fixed labeling behavior like
1413 * transition SIDs or task SIDs.
1415 int security_genfs_sid(const char *fstype
,
1421 struct genfs
*genfs
;
1423 int rc
= 0, cmp
= 0;
1427 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
1428 cmp
= strcmp(fstype
, genfs
->fstype
);
1433 if (!genfs
|| cmp
) {
1434 *sid
= SECINITSID_UNLABELED
;
1439 for (c
= genfs
->head
; c
; c
= c
->next
) {
1440 len
= strlen(c
->u
.name
);
1441 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
1442 (strncmp(c
->u
.name
, path
, len
) == 0))
1447 *sid
= SECINITSID_UNLABELED
;
1453 rc
= sidtab_context_to_sid(&sidtab
,
1467 * security_fs_use - Determine how to handle labeling for a filesystem.
1468 * @fstype: filesystem type
1469 * @behavior: labeling behavior
1470 * @sid: SID for filesystem (superblock)
1472 int security_fs_use(
1474 unsigned int *behavior
,
1482 c
= policydb
.ocontexts
[OCON_FSUSE
];
1484 if (strcmp(fstype
, c
->u
.name
) == 0)
1490 *behavior
= c
->v
.behavior
;
1492 rc
= sidtab_context_to_sid(&sidtab
,
1500 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
1502 *behavior
= SECURITY_FS_USE_NONE
;
1505 *behavior
= SECURITY_FS_USE_GENFS
;
1514 int security_get_bools(int *len
, char ***names
, int **values
)
1516 int i
, rc
= -ENOMEM
;
1522 *len
= policydb
.p_bools
.nprim
;
1528 *names
= (char**)kmalloc(sizeof(char*) * *len
, GFP_ATOMIC
);
1531 memset(*names
, 0, sizeof(char*) * *len
);
1533 *values
= (int*)kmalloc(sizeof(int) * *len
, GFP_ATOMIC
);
1537 for (i
= 0; i
< *len
; i
++) {
1539 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
1540 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
1541 (*names
)[i
] = (char*)kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
1544 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
1545 (*names
)[i
][name_len
- 1] = 0;
1553 for (i
= 0; i
< *len
; i
++)
1563 int security_set_bools(int len
, int *values
)
1566 int lenp
, seqno
= 0;
1567 struct cond_node
*cur
;
1571 lenp
= policydb
.p_bools
.nprim
;
1577 printk(KERN_INFO
"security: committed booleans { ");
1578 for (i
= 0; i
< len
; i
++) {
1580 policydb
.bool_val_to_struct
[i
]->state
= 1;
1582 policydb
.bool_val_to_struct
[i
]->state
= 0;
1586 printk("%s:%d", policydb
.p_bool_val_to_name
[i
],
1587 policydb
.bool_val_to_struct
[i
]->state
);
1591 for (cur
= policydb
.cond_list
; cur
!= NULL
; cur
= cur
->next
) {
1592 rc
= evaluate_cond_node(&policydb
, cur
);
1597 seqno
= ++latest_granting
;
1602 avc_ss_reset(seqno
);
1603 selnl_notify_policyload(seqno
);
1608 int security_get_bool_value(int bool)
1615 len
= policydb
.p_bools
.nprim
;
1621 rc
= policydb
.bool_val_to_struct
[bool]->state
;