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 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
27 * Added support for bounds domain and audit messaged on masked permissions
29 * Updated: Guido Trentalancia <guido@trentalancia.com>
31 * Added support for runtime switching of the policy type
33 * Copyright (C) 2008, 2009 NEC Corporation
34 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
35 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
36 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
37 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, version 2.
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/spinlock.h>
46 #include <linux/rcupdate.h>
47 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/audit.h>
51 #include <linux/mutex.h>
52 #include <linux/selinux.h>
53 #include <net/netlabel.h>
63 #include "conditional.h"
71 extern void selnl_notify_policyload(u32 seqno
);
73 int selinux_policycap_netpeer
;
74 int selinux_policycap_openperm
;
76 static DEFINE_RWLOCK(policy_rwlock
);
78 static struct sidtab sidtab
;
79 struct policydb policydb
;
83 * The largest sequence number that has been used when
84 * providing an access decision to the access vector cache.
85 * The sequence number only changes when a policy change
88 static u32 latest_granting
;
90 /* Forward declaration. */
91 static int context_struct_to_string(struct context
*context
, char **scontext
,
94 static void context_struct_compute_av(struct context
*scontext
,
95 struct context
*tcontext
,
97 struct av_decision
*avd
);
99 struct selinux_mapping
{
100 u16 value
; /* policy value */
102 u32 perms
[sizeof(u32
) * 8];
105 static struct selinux_mapping
*current_mapping
;
106 static u16 current_mapping_size
;
108 static int selinux_set_mapping(struct policydb
*pol
,
109 struct security_class_mapping
*map
,
110 struct selinux_mapping
**out_map_p
,
113 struct selinux_mapping
*out_map
= NULL
;
114 size_t size
= sizeof(struct selinux_mapping
);
117 bool print_unknown_handle
= false;
119 /* Find number of classes in the input mapping */
126 /* Allocate space for the class records, plus one for class zero */
127 out_map
= kcalloc(++i
, size
, GFP_ATOMIC
);
131 /* Store the raw class and permission values */
133 while (map
[j
].name
) {
134 struct security_class_mapping
*p_in
= map
+ (j
++);
135 struct selinux_mapping
*p_out
= out_map
+ j
;
137 /* An empty class string skips ahead */
138 if (!strcmp(p_in
->name
, "")) {
139 p_out
->num_perms
= 0;
143 p_out
->value
= string_to_security_class(pol
, p_in
->name
);
146 "SELinux: Class %s not defined in policy.\n",
148 if (pol
->reject_unknown
)
150 p_out
->num_perms
= 0;
151 print_unknown_handle
= true;
156 while (p_in
->perms
&& p_in
->perms
[k
]) {
157 /* An empty permission string skips ahead */
158 if (!*p_in
->perms
[k
]) {
162 p_out
->perms
[k
] = string_to_av_perm(pol
, p_out
->value
,
164 if (!p_out
->perms
[k
]) {
166 "SELinux: Permission %s in class %s not defined in policy.\n",
167 p_in
->perms
[k
], p_in
->name
);
168 if (pol
->reject_unknown
)
170 print_unknown_handle
= true;
175 p_out
->num_perms
= k
;
178 if (print_unknown_handle
)
179 printk(KERN_INFO
"SELinux: the above unknown classes and permissions will be %s\n",
180 pol
->allow_unknown
? "allowed" : "denied");
182 *out_map_p
= out_map
;
191 * Get real, policy values from mapped values
194 static u16
unmap_class(u16 tclass
)
196 if (tclass
< current_mapping_size
)
197 return current_mapping
[tclass
].value
;
202 static void map_decision(u16 tclass
, struct av_decision
*avd
,
205 if (tclass
< current_mapping_size
) {
206 unsigned i
, n
= current_mapping
[tclass
].num_perms
;
209 for (i
= 0, result
= 0; i
< n
; i
++) {
210 if (avd
->allowed
& current_mapping
[tclass
].perms
[i
])
212 if (allow_unknown
&& !current_mapping
[tclass
].perms
[i
])
215 avd
->allowed
= result
;
217 for (i
= 0, result
= 0; i
< n
; i
++)
218 if (avd
->auditallow
& current_mapping
[tclass
].perms
[i
])
220 avd
->auditallow
= result
;
222 for (i
= 0, result
= 0; i
< n
; i
++) {
223 if (avd
->auditdeny
& current_mapping
[tclass
].perms
[i
])
225 if (!allow_unknown
&& !current_mapping
[tclass
].perms
[i
])
229 * In case the kernel has a bug and requests a permission
230 * between num_perms and the maximum permission number, we
231 * should audit that denial
233 for (; i
< (sizeof(u32
)*8); i
++)
235 avd
->auditdeny
= result
;
239 int security_mls_enabled(void)
241 return policydb
.mls_enabled
;
245 * Return the boolean value of a constraint expression
246 * when it is applied to the specified source and target
249 * xcontext is a special beast... It is used by the validatetrans rules
250 * only. For these rules, scontext is the context before the transition,
251 * tcontext is the context after the transition, and xcontext is the context
252 * of the process performing the transition. All other callers of
253 * constraint_expr_eval should pass in NULL for xcontext.
255 static int constraint_expr_eval(struct context
*scontext
,
256 struct context
*tcontext
,
257 struct context
*xcontext
,
258 struct constraint_expr
*cexpr
)
262 struct role_datum
*r1
, *r2
;
263 struct mls_level
*l1
, *l2
;
264 struct constraint_expr
*e
;
265 int s
[CEXPR_MAXDEPTH
];
268 for (e
= cexpr
; e
; e
= e
->next
) {
269 switch (e
->expr_type
) {
285 if (sp
== (CEXPR_MAXDEPTH
- 1))
289 val1
= scontext
->user
;
290 val2
= tcontext
->user
;
293 val1
= scontext
->type
;
294 val2
= tcontext
->type
;
297 val1
= scontext
->role
;
298 val2
= tcontext
->role
;
299 r1
= policydb
.role_val_to_struct
[val1
- 1];
300 r2
= policydb
.role_val_to_struct
[val2
- 1];
303 s
[++sp
] = ebitmap_get_bit(&r1
->dominates
,
307 s
[++sp
] = ebitmap_get_bit(&r2
->dominates
,
311 s
[++sp
] = (!ebitmap_get_bit(&r1
->dominates
,
313 !ebitmap_get_bit(&r2
->dominates
,
321 l1
= &(scontext
->range
.level
[0]);
322 l2
= &(tcontext
->range
.level
[0]);
325 l1
= &(scontext
->range
.level
[0]);
326 l2
= &(tcontext
->range
.level
[1]);
329 l1
= &(scontext
->range
.level
[1]);
330 l2
= &(tcontext
->range
.level
[0]);
333 l1
= &(scontext
->range
.level
[1]);
334 l2
= &(tcontext
->range
.level
[1]);
337 l1
= &(scontext
->range
.level
[0]);
338 l2
= &(scontext
->range
.level
[1]);
341 l1
= &(tcontext
->range
.level
[0]);
342 l2
= &(tcontext
->range
.level
[1]);
347 s
[++sp
] = mls_level_eq(l1
, l2
);
350 s
[++sp
] = !mls_level_eq(l1
, l2
);
353 s
[++sp
] = mls_level_dom(l1
, l2
);
356 s
[++sp
] = mls_level_dom(l2
, l1
);
359 s
[++sp
] = mls_level_incomp(l2
, l1
);
373 s
[++sp
] = (val1
== val2
);
376 s
[++sp
] = (val1
!= val2
);
384 if (sp
== (CEXPR_MAXDEPTH
-1))
387 if (e
->attr
& CEXPR_TARGET
)
389 else if (e
->attr
& CEXPR_XTARGET
) {
396 if (e
->attr
& CEXPR_USER
)
398 else if (e
->attr
& CEXPR_ROLE
)
400 else if (e
->attr
& CEXPR_TYPE
)
409 s
[++sp
] = ebitmap_get_bit(&e
->names
, val1
- 1);
412 s
[++sp
] = !ebitmap_get_bit(&e
->names
, val1
- 1);
430 * security_dump_masked_av - dumps masked permissions during
431 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
433 static int dump_masked_av_helper(void *k
, void *d
, void *args
)
435 struct perm_datum
*pdatum
= d
;
436 char **permission_names
= args
;
438 BUG_ON(pdatum
->value
< 1 || pdatum
->value
> 32);
440 permission_names
[pdatum
->value
- 1] = (char *)k
;
445 static void security_dump_masked_av(struct context
*scontext
,
446 struct context
*tcontext
,
451 struct common_datum
*common_dat
;
452 struct class_datum
*tclass_dat
;
453 struct audit_buffer
*ab
;
455 char *scontext_name
= NULL
;
456 char *tcontext_name
= NULL
;
457 char *permission_names
[32];
460 bool need_comma
= false;
465 tclass_name
= policydb
.p_class_val_to_name
[tclass
- 1];
466 tclass_dat
= policydb
.class_val_to_struct
[tclass
- 1];
467 common_dat
= tclass_dat
->comdatum
;
469 /* init permission_names */
471 hashtab_map(common_dat
->permissions
.table
,
472 dump_masked_av_helper
, permission_names
) < 0)
475 if (hashtab_map(tclass_dat
->permissions
.table
,
476 dump_masked_av_helper
, permission_names
) < 0)
479 /* get scontext/tcontext in text form */
480 if (context_struct_to_string(scontext
,
481 &scontext_name
, &length
) < 0)
484 if (context_struct_to_string(tcontext
,
485 &tcontext_name
, &length
) < 0)
488 /* audit a message */
489 ab
= audit_log_start(current
->audit_context
,
490 GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
494 audit_log_format(ab
, "op=security_compute_av reason=%s "
495 "scontext=%s tcontext=%s tclass=%s perms=",
496 reason
, scontext_name
, tcontext_name
, tclass_name
);
498 for (index
= 0; index
< 32; index
++) {
499 u32 mask
= (1 << index
);
501 if ((mask
& permissions
) == 0)
504 audit_log_format(ab
, "%s%s",
505 need_comma
? "," : "",
506 permission_names
[index
]
507 ? permission_names
[index
] : "????");
512 /* release scontext/tcontext */
513 kfree(tcontext_name
);
514 kfree(scontext_name
);
520 * security_boundary_permission - drops violated permissions
521 * on boundary constraint.
523 static void type_attribute_bounds_av(struct context
*scontext
,
524 struct context
*tcontext
,
526 struct av_decision
*avd
)
528 struct context lo_scontext
;
529 struct context lo_tcontext
;
530 struct av_decision lo_avd
;
531 struct type_datum
*source
532 = policydb
.type_val_to_struct
[scontext
->type
- 1];
533 struct type_datum
*target
534 = policydb
.type_val_to_struct
[tcontext
->type
- 1];
537 if (source
->bounds
) {
538 memset(&lo_avd
, 0, sizeof(lo_avd
));
540 memcpy(&lo_scontext
, scontext
, sizeof(lo_scontext
));
541 lo_scontext
.type
= source
->bounds
;
543 context_struct_compute_av(&lo_scontext
,
547 if ((lo_avd
.allowed
& avd
->allowed
) == avd
->allowed
)
548 return; /* no masked permission */
549 masked
= ~lo_avd
.allowed
& avd
->allowed
;
552 if (target
->bounds
) {
553 memset(&lo_avd
, 0, sizeof(lo_avd
));
555 memcpy(&lo_tcontext
, tcontext
, sizeof(lo_tcontext
));
556 lo_tcontext
.type
= target
->bounds
;
558 context_struct_compute_av(scontext
,
562 if ((lo_avd
.allowed
& avd
->allowed
) == avd
->allowed
)
563 return; /* no masked permission */
564 masked
= ~lo_avd
.allowed
& avd
->allowed
;
567 if (source
->bounds
&& target
->bounds
) {
568 memset(&lo_avd
, 0, sizeof(lo_avd
));
570 * lo_scontext and lo_tcontext are already
574 context_struct_compute_av(&lo_scontext
,
578 if ((lo_avd
.allowed
& avd
->allowed
) == avd
->allowed
)
579 return; /* no masked permission */
580 masked
= ~lo_avd
.allowed
& avd
->allowed
;
584 /* mask violated permissions */
585 avd
->allowed
&= ~masked
;
587 /* audit masked permissions */
588 security_dump_masked_av(scontext
, tcontext
,
589 tclass
, masked
, "bounds");
594 * Compute access vectors based on a context structure pair for
595 * the permissions in a particular class.
597 static void context_struct_compute_av(struct context
*scontext
,
598 struct context
*tcontext
,
600 struct av_decision
*avd
)
602 struct constraint_node
*constraint
;
603 struct role_allow
*ra
;
604 struct avtab_key avkey
;
605 struct avtab_node
*node
;
606 struct class_datum
*tclass_datum
;
607 struct ebitmap
*sattr
, *tattr
;
608 struct ebitmap_node
*snode
, *tnode
;
613 avd
->auditdeny
= 0xffffffff;
615 if (unlikely(!tclass
|| tclass
> policydb
.p_classes
.nprim
)) {
616 if (printk_ratelimit())
617 printk(KERN_WARNING
"SELinux: Invalid class %hu\n", tclass
);
621 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
624 * If a specific type enforcement rule was defined for
625 * this permission check, then use it.
627 avkey
.target_class
= tclass
;
628 avkey
.specified
= AVTAB_AV
;
629 sattr
= &policydb
.type_attr_map
[scontext
->type
- 1];
630 tattr
= &policydb
.type_attr_map
[tcontext
->type
- 1];
631 ebitmap_for_each_positive_bit(sattr
, snode
, i
) {
632 ebitmap_for_each_positive_bit(tattr
, tnode
, j
) {
633 avkey
.source_type
= i
+ 1;
634 avkey
.target_type
= j
+ 1;
635 for (node
= avtab_search_node(&policydb
.te_avtab
, &avkey
);
637 node
= avtab_search_node_next(node
, avkey
.specified
)) {
638 if (node
->key
.specified
== AVTAB_ALLOWED
)
639 avd
->allowed
|= node
->datum
.data
;
640 else if (node
->key
.specified
== AVTAB_AUDITALLOW
)
641 avd
->auditallow
|= node
->datum
.data
;
642 else if (node
->key
.specified
== AVTAB_AUDITDENY
)
643 avd
->auditdeny
&= node
->datum
.data
;
646 /* Check conditional av table for additional permissions */
647 cond_compute_av(&policydb
.te_cond_avtab
, &avkey
, avd
);
653 * Remove any permissions prohibited by a constraint (this includes
656 constraint
= tclass_datum
->constraints
;
658 if ((constraint
->permissions
& (avd
->allowed
)) &&
659 !constraint_expr_eval(scontext
, tcontext
, NULL
,
661 avd
->allowed
&= ~(constraint
->permissions
);
663 constraint
= constraint
->next
;
667 * If checking process transition permission and the
668 * role is changing, then check the (current_role, new_role)
671 if (tclass
== policydb
.process_class
&&
672 (avd
->allowed
& policydb
.process_trans_perms
) &&
673 scontext
->role
!= tcontext
->role
) {
674 for (ra
= policydb
.role_allow
; ra
; ra
= ra
->next
) {
675 if (scontext
->role
== ra
->role
&&
676 tcontext
->role
== ra
->new_role
)
680 avd
->allowed
&= ~policydb
.process_trans_perms
;
684 * If the given source and target types have boundary
685 * constraint, lazy checks have to mask any violated
686 * permission and notice it to userspace via audit.
688 type_attribute_bounds_av(scontext
, tcontext
,
692 static int security_validtrans_handle_fail(struct context
*ocontext
,
693 struct context
*ncontext
,
694 struct context
*tcontext
,
697 char *o
= NULL
, *n
= NULL
, *t
= NULL
;
698 u32 olen
, nlen
, tlen
;
700 if (context_struct_to_string(ocontext
, &o
, &olen
) < 0)
702 if (context_struct_to_string(ncontext
, &n
, &nlen
) < 0)
704 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
706 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
707 "security_validate_transition: denied for"
708 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
709 o
, n
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
715 if (!selinux_enforcing
)
720 int security_validate_transition(u32 oldsid
, u32 newsid
, u32 tasksid
,
723 struct context
*ocontext
;
724 struct context
*ncontext
;
725 struct context
*tcontext
;
726 struct class_datum
*tclass_datum
;
727 struct constraint_node
*constraint
;
734 read_lock(&policy_rwlock
);
736 tclass
= unmap_class(orig_tclass
);
738 if (!tclass
|| tclass
> policydb
.p_classes
.nprim
) {
739 printk(KERN_ERR
"SELinux: %s: unrecognized class %d\n",
744 tclass_datum
= policydb
.class_val_to_struct
[tclass
- 1];
746 ocontext
= sidtab_search(&sidtab
, oldsid
);
748 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
754 ncontext
= sidtab_search(&sidtab
, newsid
);
756 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
762 tcontext
= sidtab_search(&sidtab
, tasksid
);
764 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
770 constraint
= tclass_datum
->validatetrans
;
772 if (!constraint_expr_eval(ocontext
, ncontext
, tcontext
,
774 rc
= security_validtrans_handle_fail(ocontext
, ncontext
,
778 constraint
= constraint
->next
;
782 read_unlock(&policy_rwlock
);
787 * security_bounded_transition - check whether the given
788 * transition is directed to bounded, or not.
789 * It returns 0, if @newsid is bounded by @oldsid.
790 * Otherwise, it returns error code.
792 * @oldsid : current security identifier
793 * @newsid : destinated security identifier
795 int security_bounded_transition(u32 old_sid
, u32 new_sid
)
797 struct context
*old_context
, *new_context
;
798 struct type_datum
*type
;
802 read_lock(&policy_rwlock
);
804 old_context
= sidtab_search(&sidtab
, old_sid
);
806 printk(KERN_ERR
"SELinux: %s: unrecognized SID %u\n",
811 new_context
= sidtab_search(&sidtab
, new_sid
);
813 printk(KERN_ERR
"SELinux: %s: unrecognized SID %u\n",
818 /* type/domain unchanged */
819 if (old_context
->type
== new_context
->type
) {
824 index
= new_context
->type
;
826 type
= policydb
.type_val_to_struct
[index
- 1];
829 /* not bounded anymore */
835 /* @newsid is bounded by @oldsid */
836 if (type
->bounds
== old_context
->type
) {
840 index
= type
->bounds
;
844 char *old_name
= NULL
;
845 char *new_name
= NULL
;
848 if (!context_struct_to_string(old_context
,
849 &old_name
, &length
) &&
850 !context_struct_to_string(new_context
,
851 &new_name
, &length
)) {
852 audit_log(current
->audit_context
,
853 GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
854 "op=security_bounded_transition "
856 "oldcontext=%s newcontext=%s",
863 read_unlock(&policy_rwlock
);
868 static void avd_init(struct av_decision
*avd
)
872 avd
->auditdeny
= 0xffffffff;
873 avd
->seqno
= latest_granting
;
879 * security_compute_av - Compute access vector decisions.
880 * @ssid: source security identifier
881 * @tsid: target security identifier
882 * @tclass: target security class
883 * @avd: access vector decisions
885 * Compute a set of access vector decisions based on the
886 * SID pair (@ssid, @tsid) for the permissions in @tclass.
888 void security_compute_av(u32 ssid
,
891 struct av_decision
*avd
)
894 struct context
*scontext
= NULL
, *tcontext
= NULL
;
896 read_lock(&policy_rwlock
);
901 scontext
= sidtab_search(&sidtab
, ssid
);
903 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
908 /* permissive domain? */
909 if (ebitmap_get_bit(&policydb
.permissive_map
, scontext
->type
))
910 avd
->flags
|= AVD_FLAGS_PERMISSIVE
;
912 tcontext
= sidtab_search(&sidtab
, tsid
);
914 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
919 tclass
= unmap_class(orig_tclass
);
920 if (unlikely(orig_tclass
&& !tclass
)) {
921 if (policydb
.allow_unknown
)
925 context_struct_compute_av(scontext
, tcontext
, tclass
, avd
);
926 map_decision(orig_tclass
, avd
, policydb
.allow_unknown
);
928 read_unlock(&policy_rwlock
);
931 avd
->allowed
= 0xffffffff;
935 void security_compute_av_user(u32 ssid
,
938 struct av_decision
*avd
)
940 struct context
*scontext
= NULL
, *tcontext
= NULL
;
942 read_lock(&policy_rwlock
);
947 scontext
= sidtab_search(&sidtab
, ssid
);
949 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
954 /* permissive domain? */
955 if (ebitmap_get_bit(&policydb
.permissive_map
, scontext
->type
))
956 avd
->flags
|= AVD_FLAGS_PERMISSIVE
;
958 tcontext
= sidtab_search(&sidtab
, tsid
);
960 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
965 if (unlikely(!tclass
)) {
966 if (policydb
.allow_unknown
)
971 context_struct_compute_av(scontext
, tcontext
, tclass
, avd
);
973 read_unlock(&policy_rwlock
);
976 avd
->allowed
= 0xffffffff;
981 * Write the security context string representation of
982 * the context structure `context' into a dynamically
983 * allocated string of the correct size. Set `*scontext'
984 * to point to this string and set `*scontext_len' to
985 * the length of the string.
987 static int context_struct_to_string(struct context
*context
, char **scontext
, u32
*scontext_len
)
995 *scontext_len
= context
->len
;
996 *scontext
= kstrdup(context
->str
, GFP_ATOMIC
);
1002 /* Compute the size of the context. */
1003 *scontext_len
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) + 1;
1004 *scontext_len
+= strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) + 1;
1005 *scontext_len
+= strlen(policydb
.p_type_val_to_name
[context
->type
- 1]) + 1;
1006 *scontext_len
+= mls_compute_context_len(context
);
1008 /* Allocate space for the context; caller must free this space. */
1009 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
1012 *scontext
= scontextp
;
1015 * Copy the user name, role name and type name into the context.
1017 sprintf(scontextp
, "%s:%s:%s",
1018 policydb
.p_user_val_to_name
[context
->user
- 1],
1019 policydb
.p_role_val_to_name
[context
->role
- 1],
1020 policydb
.p_type_val_to_name
[context
->type
- 1]);
1021 scontextp
+= strlen(policydb
.p_user_val_to_name
[context
->user
- 1]) +
1022 1 + strlen(policydb
.p_role_val_to_name
[context
->role
- 1]) +
1023 1 + strlen(policydb
.p_type_val_to_name
[context
->type
- 1]);
1025 mls_sid_to_context(context
, &scontextp
);
1032 #include "initial_sid_to_string.h"
1034 const char *security_get_initial_sid_context(u32 sid
)
1036 if (unlikely(sid
> SECINITSID_NUM
))
1038 return initial_sid_to_string
[sid
];
1041 static int security_sid_to_context_core(u32 sid
, char **scontext
,
1042 u32
*scontext_len
, int force
)
1044 struct context
*context
;
1050 if (!ss_initialized
) {
1051 if (sid
<= SECINITSID_NUM
) {
1054 *scontext_len
= strlen(initial_sid_to_string
[sid
]) + 1;
1055 scontextp
= kmalloc(*scontext_len
, GFP_ATOMIC
);
1060 strcpy(scontextp
, initial_sid_to_string
[sid
]);
1061 *scontext
= scontextp
;
1064 printk(KERN_ERR
"SELinux: %s: called before initial "
1065 "load_policy on unknown SID %d\n", __func__
, sid
);
1069 read_lock(&policy_rwlock
);
1071 context
= sidtab_search_force(&sidtab
, sid
);
1073 context
= sidtab_search(&sidtab
, sid
);
1075 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
1080 rc
= context_struct_to_string(context
, scontext
, scontext_len
);
1082 read_unlock(&policy_rwlock
);
1089 * security_sid_to_context - Obtain a context for a given SID.
1090 * @sid: security identifier, SID
1091 * @scontext: security context
1092 * @scontext_len: length in bytes
1094 * Write the string representation of the context associated with @sid
1095 * into a dynamically allocated string of the correct size. Set @scontext
1096 * to point to this string and set @scontext_len to the length of the string.
1098 int security_sid_to_context(u32 sid
, char **scontext
, u32
*scontext_len
)
1100 return security_sid_to_context_core(sid
, scontext
, scontext_len
, 0);
1103 int security_sid_to_context_force(u32 sid
, char **scontext
, u32
*scontext_len
)
1105 return security_sid_to_context_core(sid
, scontext
, scontext_len
, 1);
1109 * Caveat: Mutates scontext.
1111 static int string_to_context_struct(struct policydb
*pol
,
1112 struct sidtab
*sidtabp
,
1115 struct context
*ctx
,
1118 struct role_datum
*role
;
1119 struct type_datum
*typdatum
;
1120 struct user_datum
*usrdatum
;
1121 char *scontextp
, *p
, oldc
;
1126 /* Parse the security context. */
1129 scontextp
= (char *) scontext
;
1131 /* Extract the user. */
1133 while (*p
&& *p
!= ':')
1141 usrdatum
= hashtab_search(pol
->p_users
.table
, scontextp
);
1145 ctx
->user
= usrdatum
->value
;
1149 while (*p
&& *p
!= ':')
1157 role
= hashtab_search(pol
->p_roles
.table
, scontextp
);
1160 ctx
->role
= role
->value
;
1164 while (*p
&& *p
!= ':')
1169 typdatum
= hashtab_search(pol
->p_types
.table
, scontextp
);
1170 if (!typdatum
|| typdatum
->attribute
)
1173 ctx
->type
= typdatum
->value
;
1175 rc
= mls_context_to_sid(pol
, oldc
, &p
, ctx
, sidtabp
, def_sid
);
1179 if ((p
- scontext
) < scontext_len
) {
1184 /* Check the validity of the new context. */
1185 if (!policydb_context_isvalid(pol
, ctx
)) {
1192 context_destroy(ctx
);
1196 static int security_context_to_sid_core(const char *scontext
, u32 scontext_len
,
1197 u32
*sid
, u32 def_sid
, gfp_t gfp_flags
,
1200 char *scontext2
, *str
= NULL
;
1201 struct context context
;
1204 if (!ss_initialized
) {
1207 for (i
= 1; i
< SECINITSID_NUM
; i
++) {
1208 if (!strcmp(initial_sid_to_string
[i
], scontext
)) {
1213 *sid
= SECINITSID_KERNEL
;
1218 /* Copy the string so that we can modify the copy as we parse it. */
1219 scontext2
= kmalloc(scontext_len
+ 1, gfp_flags
);
1222 memcpy(scontext2
, scontext
, scontext_len
);
1223 scontext2
[scontext_len
] = 0;
1226 /* Save another copy for storing in uninterpreted form */
1227 str
= kstrdup(scontext2
, gfp_flags
);
1234 read_lock(&policy_rwlock
);
1235 rc
= string_to_context_struct(&policydb
, &sidtab
,
1236 scontext2
, scontext_len
,
1238 if (rc
== -EINVAL
&& force
) {
1240 context
.len
= scontext_len
;
1244 rc
= sidtab_context_to_sid(&sidtab
, &context
, sid
);
1245 context_destroy(&context
);
1247 read_unlock(&policy_rwlock
);
1254 * security_context_to_sid - Obtain a SID for a given security context.
1255 * @scontext: security context
1256 * @scontext_len: length in bytes
1257 * @sid: security identifier, SID
1259 * Obtains a SID associated with the security context that
1260 * has the string representation specified by @scontext.
1261 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1262 * memory is available, or 0 on success.
1264 int security_context_to_sid(const char *scontext
, u32 scontext_len
, u32
*sid
)
1266 return security_context_to_sid_core(scontext
, scontext_len
,
1267 sid
, SECSID_NULL
, GFP_KERNEL
, 0);
1271 * security_context_to_sid_default - Obtain a SID for a given security context,
1272 * falling back to specified default if needed.
1274 * @scontext: security context
1275 * @scontext_len: length in bytes
1276 * @sid: security identifier, SID
1277 * @def_sid: default SID to assign on error
1279 * Obtains a SID associated with the security context that
1280 * has the string representation specified by @scontext.
1281 * The default SID is passed to the MLS layer to be used to allow
1282 * kernel labeling of the MLS field if the MLS field is not present
1283 * (for upgrading to MLS without full relabel).
1284 * Implicitly forces adding of the context even if it cannot be mapped yet.
1285 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1286 * memory is available, or 0 on success.
1288 int security_context_to_sid_default(const char *scontext
, u32 scontext_len
,
1289 u32
*sid
, u32 def_sid
, gfp_t gfp_flags
)
1291 return security_context_to_sid_core(scontext
, scontext_len
,
1292 sid
, def_sid
, gfp_flags
, 1);
1295 int security_context_to_sid_force(const char *scontext
, u32 scontext_len
,
1298 return security_context_to_sid_core(scontext
, scontext_len
,
1299 sid
, SECSID_NULL
, GFP_KERNEL
, 1);
1302 static int compute_sid_handle_invalid_context(
1303 struct context
*scontext
,
1304 struct context
*tcontext
,
1306 struct context
*newcontext
)
1308 char *s
= NULL
, *t
= NULL
, *n
= NULL
;
1309 u32 slen
, tlen
, nlen
;
1311 if (context_struct_to_string(scontext
, &s
, &slen
) < 0)
1313 if (context_struct_to_string(tcontext
, &t
, &tlen
) < 0)
1315 if (context_struct_to_string(newcontext
, &n
, &nlen
) < 0)
1317 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
1318 "security_compute_sid: invalid context %s"
1322 n
, s
, t
, policydb
.p_class_val_to_name
[tclass
-1]);
1327 if (!selinux_enforcing
)
1332 static int security_compute_sid(u32 ssid
,
1339 struct context
*scontext
= NULL
, *tcontext
= NULL
, newcontext
;
1340 struct role_trans
*roletr
= NULL
;
1341 struct avtab_key avkey
;
1342 struct avtab_datum
*avdatum
;
1343 struct avtab_node
*node
;
1347 if (!ss_initialized
) {
1348 switch (orig_tclass
) {
1349 case SECCLASS_PROCESS
: /* kernel value */
1359 context_init(&newcontext
);
1361 read_lock(&policy_rwlock
);
1364 tclass
= unmap_class(orig_tclass
);
1366 tclass
= orig_tclass
;
1368 scontext
= sidtab_search(&sidtab
, ssid
);
1370 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
1375 tcontext
= sidtab_search(&sidtab
, tsid
);
1377 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
1383 /* Set the user identity. */
1384 switch (specified
) {
1385 case AVTAB_TRANSITION
:
1387 /* Use the process user identity. */
1388 newcontext
.user
= scontext
->user
;
1391 /* Use the related object owner. */
1392 newcontext
.user
= tcontext
->user
;
1396 /* Set the role and type to default values. */
1397 if (tclass
== policydb
.process_class
) {
1398 /* Use the current role and type of process. */
1399 newcontext
.role
= scontext
->role
;
1400 newcontext
.type
= scontext
->type
;
1402 /* Use the well-defined object role. */
1403 newcontext
.role
= OBJECT_R_VAL
;
1404 /* Use the type of the related object. */
1405 newcontext
.type
= tcontext
->type
;
1408 /* Look for a type transition/member/change rule. */
1409 avkey
.source_type
= scontext
->type
;
1410 avkey
.target_type
= tcontext
->type
;
1411 avkey
.target_class
= tclass
;
1412 avkey
.specified
= specified
;
1413 avdatum
= avtab_search(&policydb
.te_avtab
, &avkey
);
1415 /* If no permanent rule, also check for enabled conditional rules */
1417 node
= avtab_search_node(&policydb
.te_cond_avtab
, &avkey
);
1418 for (; node
; node
= avtab_search_node_next(node
, specified
)) {
1419 if (node
->key
.specified
& AVTAB_ENABLED
) {
1420 avdatum
= &node
->datum
;
1427 /* Use the type from the type transition/member/change rule. */
1428 newcontext
.type
= avdatum
->data
;
1431 /* Check for class-specific changes. */
1432 if (tclass
== policydb
.process_class
) {
1433 if (specified
& AVTAB_TRANSITION
) {
1434 /* Look for a role transition rule. */
1435 for (roletr
= policydb
.role_tr
; roletr
;
1436 roletr
= roletr
->next
) {
1437 if (roletr
->role
== scontext
->role
&&
1438 roletr
->type
== tcontext
->type
) {
1439 /* Use the role transition rule. */
1440 newcontext
.role
= roletr
->new_role
;
1447 /* Set the MLS attributes.
1448 This is done last because it may allocate memory. */
1449 rc
= mls_compute_sid(scontext
, tcontext
, tclass
, specified
, &newcontext
);
1453 /* Check the validity of the context. */
1454 if (!policydb_context_isvalid(&policydb
, &newcontext
)) {
1455 rc
= compute_sid_handle_invalid_context(scontext
,
1462 /* Obtain the sid for the context. */
1463 rc
= sidtab_context_to_sid(&sidtab
, &newcontext
, out_sid
);
1465 read_unlock(&policy_rwlock
);
1466 context_destroy(&newcontext
);
1472 * security_transition_sid - Compute the SID for a new subject/object.
1473 * @ssid: source security identifier
1474 * @tsid: target security identifier
1475 * @tclass: target security class
1476 * @out_sid: security identifier for new subject/object
1478 * Compute a SID to use for labeling a new subject or object in the
1479 * class @tclass based on a SID pair (@ssid, @tsid).
1480 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1481 * if insufficient memory is available, or %0 if the new SID was
1482 * computed successfully.
1484 int security_transition_sid(u32 ssid
,
1489 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
,
1493 int security_transition_sid_user(u32 ssid
,
1498 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_TRANSITION
,
1503 * security_member_sid - Compute the SID for member selection.
1504 * @ssid: source security identifier
1505 * @tsid: target security identifier
1506 * @tclass: target security class
1507 * @out_sid: security identifier for selected member
1509 * Compute a SID to use when selecting a member of a polyinstantiated
1510 * object of class @tclass based on a SID pair (@ssid, @tsid).
1511 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1512 * if insufficient memory is available, or %0 if the SID was
1513 * computed successfully.
1515 int security_member_sid(u32 ssid
,
1520 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_MEMBER
, out_sid
,
1525 * security_change_sid - Compute the SID for object relabeling.
1526 * @ssid: source security identifier
1527 * @tsid: target security identifier
1528 * @tclass: target security class
1529 * @out_sid: security identifier for selected member
1531 * Compute a SID to use for relabeling an object of class @tclass
1532 * based on a SID pair (@ssid, @tsid).
1533 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1534 * if insufficient memory is available, or %0 if the SID was
1535 * computed successfully.
1537 int security_change_sid(u32 ssid
,
1542 return security_compute_sid(ssid
, tsid
, tclass
, AVTAB_CHANGE
, out_sid
,
1546 /* Clone the SID into the new SID table. */
1547 static int clone_sid(u32 sid
,
1548 struct context
*context
,
1551 struct sidtab
*s
= arg
;
1553 if (sid
> SECINITSID_NUM
)
1554 return sidtab_insert(s
, sid
, context
);
1559 static inline int convert_context_handle_invalid_context(struct context
*context
)
1563 if (selinux_enforcing
) {
1569 if (!context_struct_to_string(context
, &s
, &len
)) {
1571 "SELinux: Context %s would be invalid if enforcing\n",
1579 struct convert_context_args
{
1580 struct policydb
*oldp
;
1581 struct policydb
*newp
;
1585 * Convert the values in the security context
1586 * structure `c' from the values specified
1587 * in the policy `p->oldp' to the values specified
1588 * in the policy `p->newp'. Verify that the
1589 * context is valid under the new policy.
1591 static int convert_context(u32 key
,
1595 struct convert_context_args
*args
;
1596 struct context oldc
;
1597 struct ocontext
*oc
;
1598 struct mls_range
*range
;
1599 struct role_datum
*role
;
1600 struct type_datum
*typdatum
;
1601 struct user_datum
*usrdatum
;
1606 if (key
<= SECINITSID_NUM
)
1613 s
= kstrdup(c
->str
, GFP_KERNEL
);
1618 rc
= string_to_context_struct(args
->newp
, NULL
, s
,
1619 c
->len
, &ctx
, SECSID_NULL
);
1623 "SELinux: Context %s became valid (mapped).\n",
1625 /* Replace string with mapped representation. */
1627 memcpy(c
, &ctx
, sizeof(*c
));
1629 } else if (rc
== -EINVAL
) {
1630 /* Retain string representation for later mapping. */
1634 /* Other error condition, e.g. ENOMEM. */
1636 "SELinux: Unable to map context %s, rc = %d.\n",
1642 rc
= context_cpy(&oldc
, c
);
1648 /* Convert the user. */
1649 usrdatum
= hashtab_search(args
->newp
->p_users
.table
,
1650 args
->oldp
->p_user_val_to_name
[c
->user
- 1]);
1653 c
->user
= usrdatum
->value
;
1655 /* Convert the role. */
1656 role
= hashtab_search(args
->newp
->p_roles
.table
,
1657 args
->oldp
->p_role_val_to_name
[c
->role
- 1]);
1660 c
->role
= role
->value
;
1662 /* Convert the type. */
1663 typdatum
= hashtab_search(args
->newp
->p_types
.table
,
1664 args
->oldp
->p_type_val_to_name
[c
->type
- 1]);
1667 c
->type
= typdatum
->value
;
1669 /* Convert the MLS fields if dealing with MLS policies */
1670 if (args
->oldp
->mls_enabled
&& args
->newp
->mls_enabled
) {
1671 rc
= mls_convert_context(args
->oldp
, args
->newp
, c
);
1674 } else if (args
->oldp
->mls_enabled
&& !args
->newp
->mls_enabled
) {
1676 * Switching between MLS and non-MLS policy:
1677 * free any storage used by the MLS fields in the
1678 * context for all existing entries in the sidtab.
1680 mls_context_destroy(c
);
1681 } else if (!args
->oldp
->mls_enabled
&& args
->newp
->mls_enabled
) {
1683 * Switching between non-MLS and MLS policy:
1684 * ensure that the MLS fields of the context for all
1685 * existing entries in the sidtab are filled in with a
1686 * suitable default value, likely taken from one of the
1689 oc
= args
->newp
->ocontexts
[OCON_ISID
];
1690 while (oc
&& oc
->sid
[0] != SECINITSID_UNLABELED
)
1693 printk(KERN_ERR
"SELinux: unable to look up"
1694 " the initial SIDs list\n");
1697 range
= &oc
->context
[0].range
;
1698 rc
= mls_range_set(c
, range
);
1703 /* Check the validity of the new context. */
1704 if (!policydb_context_isvalid(args
->newp
, c
)) {
1705 rc
= convert_context_handle_invalid_context(&oldc
);
1710 context_destroy(&oldc
);
1715 /* Map old representation to string and save it. */
1716 if (context_struct_to_string(&oldc
, &s
, &len
))
1718 context_destroy(&oldc
);
1723 "SELinux: Context %s became invalid (unmapped).\n",
1729 static void security_load_policycaps(void)
1731 selinux_policycap_netpeer
= ebitmap_get_bit(&policydb
.policycaps
,
1732 POLICYDB_CAPABILITY_NETPEER
);
1733 selinux_policycap_openperm
= ebitmap_get_bit(&policydb
.policycaps
,
1734 POLICYDB_CAPABILITY_OPENPERM
);
1737 extern void selinux_complete_init(void);
1738 static int security_preserve_bools(struct policydb
*p
);
1741 * security_load_policy - Load a security policy configuration.
1742 * @data: binary policy data
1743 * @len: length of data in bytes
1745 * Load a new set of security policy configuration data,
1746 * validate it and convert the SID table as necessary.
1747 * This function will flush the access vector cache after
1748 * loading the new policy.
1750 int security_load_policy(void *data
, size_t len
)
1752 struct policydb oldpolicydb
, newpolicydb
;
1753 struct sidtab oldsidtab
, newsidtab
;
1754 struct selinux_mapping
*oldmap
, *map
= NULL
;
1755 struct convert_context_args args
;
1759 struct policy_file file
= { data
, len
}, *fp
= &file
;
1761 if (!ss_initialized
) {
1763 rc
= policydb_read(&policydb
, fp
);
1765 avtab_cache_destroy();
1769 rc
= selinux_set_mapping(&policydb
, secclass_map
,
1771 ¤t_mapping_size
);
1773 policydb_destroy(&policydb
);
1774 avtab_cache_destroy();
1778 rc
= policydb_load_isids(&policydb
, &sidtab
);
1780 policydb_destroy(&policydb
);
1781 avtab_cache_destroy();
1785 security_load_policycaps();
1787 seqno
= ++latest_granting
;
1788 selinux_complete_init();
1789 avc_ss_reset(seqno
);
1790 selnl_notify_policyload(seqno
);
1791 selinux_netlbl_cache_invalidate();
1792 selinux_xfrm_notify_policyload();
1797 sidtab_hash_eval(&sidtab
, "sids");
1800 rc
= policydb_read(&newpolicydb
, fp
);
1804 /* If switching between different policy types, log MLS status */
1805 if (policydb
.mls_enabled
&& !newpolicydb
.mls_enabled
)
1806 printk(KERN_INFO
"SELinux: Disabling MLS support...\n");
1807 else if (!policydb
.mls_enabled
&& newpolicydb
.mls_enabled
)
1808 printk(KERN_INFO
"SELinux: Enabling MLS support...\n");
1810 rc
= policydb_load_isids(&newpolicydb
, &newsidtab
);
1812 printk(KERN_ERR
"SELinux: unable to load the initial SIDs\n");
1813 policydb_destroy(&newpolicydb
);
1817 rc
= selinux_set_mapping(&newpolicydb
, secclass_map
, &map
, &map_size
);
1821 rc
= security_preserve_bools(&newpolicydb
);
1823 printk(KERN_ERR
"SELinux: unable to preserve booleans\n");
1827 /* Clone the SID table. */
1828 sidtab_shutdown(&sidtab
);
1830 rc
= sidtab_map(&sidtab
, clone_sid
, &newsidtab
);
1835 * Convert the internal representations of contexts
1836 * in the new SID table.
1838 args
.oldp
= &policydb
;
1839 args
.newp
= &newpolicydb
;
1840 rc
= sidtab_map(&newsidtab
, convert_context
, &args
);
1842 printk(KERN_ERR
"SELinux: unable to convert the internal"
1843 " representation of contexts in the new SID"
1848 /* Save the old policydb and SID table to free later. */
1849 memcpy(&oldpolicydb
, &policydb
, sizeof policydb
);
1850 sidtab_set(&oldsidtab
, &sidtab
);
1852 /* Install the new policydb and SID table. */
1853 write_lock_irq(&policy_rwlock
);
1854 memcpy(&policydb
, &newpolicydb
, sizeof policydb
);
1855 sidtab_set(&sidtab
, &newsidtab
);
1856 security_load_policycaps();
1857 oldmap
= current_mapping
;
1858 current_mapping
= map
;
1859 current_mapping_size
= map_size
;
1860 seqno
= ++latest_granting
;
1861 write_unlock_irq(&policy_rwlock
);
1863 /* Free the old policydb and SID table. */
1864 policydb_destroy(&oldpolicydb
);
1865 sidtab_destroy(&oldsidtab
);
1868 avc_ss_reset(seqno
);
1869 selnl_notify_policyload(seqno
);
1870 selinux_netlbl_cache_invalidate();
1871 selinux_xfrm_notify_policyload();
1877 sidtab_destroy(&newsidtab
);
1878 policydb_destroy(&newpolicydb
);
1884 * security_port_sid - Obtain the SID for a port.
1885 * @protocol: protocol number
1886 * @port: port number
1887 * @out_sid: security identifier
1889 int security_port_sid(u8 protocol
, u16 port
, u32
*out_sid
)
1894 read_lock(&policy_rwlock
);
1896 c
= policydb
.ocontexts
[OCON_PORT
];
1898 if (c
->u
.port
.protocol
== protocol
&&
1899 c
->u
.port
.low_port
<= port
&&
1900 c
->u
.port
.high_port
>= port
)
1907 rc
= sidtab_context_to_sid(&sidtab
,
1913 *out_sid
= c
->sid
[0];
1915 *out_sid
= SECINITSID_PORT
;
1919 read_unlock(&policy_rwlock
);
1924 * security_netif_sid - Obtain the SID for a network interface.
1925 * @name: interface name
1926 * @if_sid: interface SID
1928 int security_netif_sid(char *name
, u32
*if_sid
)
1933 read_lock(&policy_rwlock
);
1935 c
= policydb
.ocontexts
[OCON_NETIF
];
1937 if (strcmp(name
, c
->u
.name
) == 0)
1943 if (!c
->sid
[0] || !c
->sid
[1]) {
1944 rc
= sidtab_context_to_sid(&sidtab
,
1949 rc
= sidtab_context_to_sid(&sidtab
,
1955 *if_sid
= c
->sid
[0];
1957 *if_sid
= SECINITSID_NETIF
;
1960 read_unlock(&policy_rwlock
);
1964 static int match_ipv6_addrmask(u32
*input
, u32
*addr
, u32
*mask
)
1968 for (i
= 0; i
< 4; i
++)
1969 if (addr
[i
] != (input
[i
] & mask
[i
])) {
1978 * security_node_sid - Obtain the SID for a node (host).
1979 * @domain: communication domain aka address family
1981 * @addrlen: address length in bytes
1982 * @out_sid: security identifier
1984 int security_node_sid(u16 domain
,
1992 read_lock(&policy_rwlock
);
1998 if (addrlen
!= sizeof(u32
)) {
2003 addr
= *((u32
*)addrp
);
2005 c
= policydb
.ocontexts
[OCON_NODE
];
2007 if (c
->u
.node
.addr
== (addr
& c
->u
.node
.mask
))
2015 if (addrlen
!= sizeof(u64
) * 2) {
2019 c
= policydb
.ocontexts
[OCON_NODE6
];
2021 if (match_ipv6_addrmask(addrp
, c
->u
.node6
.addr
,
2029 *out_sid
= SECINITSID_NODE
;
2035 rc
= sidtab_context_to_sid(&sidtab
,
2041 *out_sid
= c
->sid
[0];
2043 *out_sid
= SECINITSID_NODE
;
2047 read_unlock(&policy_rwlock
);
2054 * security_get_user_sids - Obtain reachable SIDs for a user.
2055 * @fromsid: starting SID
2056 * @username: username
2057 * @sids: array of reachable SIDs for user
2058 * @nel: number of elements in @sids
2060 * Generate the set of SIDs for legal security contexts
2061 * for a given user that can be reached by @fromsid.
2062 * Set *@sids to point to a dynamically allocated
2063 * array containing the set of SIDs. Set *@nel to the
2064 * number of elements in the array.
2067 int security_get_user_sids(u32 fromsid
,
2072 struct context
*fromcon
, usercon
;
2073 u32
*mysids
= NULL
, *mysids2
, sid
;
2074 u32 mynel
= 0, maxnel
= SIDS_NEL
;
2075 struct user_datum
*user
;
2076 struct role_datum
*role
;
2077 struct ebitmap_node
*rnode
, *tnode
;
2083 if (!ss_initialized
)
2086 read_lock(&policy_rwlock
);
2088 context_init(&usercon
);
2090 fromcon
= sidtab_search(&sidtab
, fromsid
);
2096 user
= hashtab_search(policydb
.p_users
.table
, username
);
2101 usercon
.user
= user
->value
;
2103 mysids
= kcalloc(maxnel
, sizeof(*mysids
), GFP_ATOMIC
);
2109 ebitmap_for_each_positive_bit(&user
->roles
, rnode
, i
) {
2110 role
= policydb
.role_val_to_struct
[i
];
2111 usercon
.role
= i
+ 1;
2112 ebitmap_for_each_positive_bit(&role
->types
, tnode
, j
) {
2113 usercon
.type
= j
+ 1;
2115 if (mls_setup_user_range(fromcon
, user
, &usercon
))
2118 rc
= sidtab_context_to_sid(&sidtab
, &usercon
, &sid
);
2121 if (mynel
< maxnel
) {
2122 mysids
[mynel
++] = sid
;
2125 mysids2
= kcalloc(maxnel
, sizeof(*mysids2
), GFP_ATOMIC
);
2130 memcpy(mysids2
, mysids
, mynel
* sizeof(*mysids2
));
2133 mysids
[mynel
++] = sid
;
2139 read_unlock(&policy_rwlock
);
2145 mysids2
= kcalloc(mynel
, sizeof(*mysids2
), GFP_KERNEL
);
2151 for (i
= 0, j
= 0; i
< mynel
; i
++) {
2152 rc
= avc_has_perm_noaudit(fromsid
, mysids
[i
],
2153 SECCLASS_PROCESS
, /* kernel value */
2154 PROCESS__TRANSITION
, AVC_STRICT
,
2157 mysids2
[j
++] = mysids
[i
];
2169 * security_genfs_sid - Obtain a SID for a file in a filesystem
2170 * @fstype: filesystem type
2171 * @path: path from root of mount
2172 * @sclass: file security class
2173 * @sid: SID for path
2175 * Obtain a SID to use for a file in a filesystem that
2176 * cannot support xattr or use a fixed labeling behavior like
2177 * transition SIDs or task SIDs.
2179 int security_genfs_sid(const char *fstype
,
2186 struct genfs
*genfs
;
2188 int rc
= 0, cmp
= 0;
2190 while (path
[0] == '/' && path
[1] == '/')
2193 read_lock(&policy_rwlock
);
2195 sclass
= unmap_class(orig_sclass
);
2197 for (genfs
= policydb
.genfs
; genfs
; genfs
= genfs
->next
) {
2198 cmp
= strcmp(fstype
, genfs
->fstype
);
2203 if (!genfs
|| cmp
) {
2204 *sid
= SECINITSID_UNLABELED
;
2209 for (c
= genfs
->head
; c
; c
= c
->next
) {
2210 len
= strlen(c
->u
.name
);
2211 if ((!c
->v
.sclass
|| sclass
== c
->v
.sclass
) &&
2212 (strncmp(c
->u
.name
, path
, len
) == 0))
2217 *sid
= SECINITSID_UNLABELED
;
2223 rc
= sidtab_context_to_sid(&sidtab
,
2232 read_unlock(&policy_rwlock
);
2237 * security_fs_use - Determine how to handle labeling for a filesystem.
2238 * @fstype: filesystem type
2239 * @behavior: labeling behavior
2240 * @sid: SID for filesystem (superblock)
2242 int security_fs_use(
2244 unsigned int *behavior
,
2250 read_lock(&policy_rwlock
);
2252 c
= policydb
.ocontexts
[OCON_FSUSE
];
2254 if (strcmp(fstype
, c
->u
.name
) == 0)
2260 *behavior
= c
->v
.behavior
;
2262 rc
= sidtab_context_to_sid(&sidtab
,
2270 rc
= security_genfs_sid(fstype
, "/", SECCLASS_DIR
, sid
);
2272 *behavior
= SECURITY_FS_USE_NONE
;
2275 *behavior
= SECURITY_FS_USE_GENFS
;
2280 read_unlock(&policy_rwlock
);
2284 int security_get_bools(int *len
, char ***names
, int **values
)
2286 int i
, rc
= -ENOMEM
;
2288 read_lock(&policy_rwlock
);
2292 *len
= policydb
.p_bools
.nprim
;
2298 *names
= kcalloc(*len
, sizeof(char *), GFP_ATOMIC
);
2302 *values
= kcalloc(*len
, sizeof(int), GFP_ATOMIC
);
2306 for (i
= 0; i
< *len
; i
++) {
2308 (*values
)[i
] = policydb
.bool_val_to_struct
[i
]->state
;
2309 name_len
= strlen(policydb
.p_bool_val_to_name
[i
]) + 1;
2310 (*names
)[i
] = kmalloc(sizeof(char) * name_len
, GFP_ATOMIC
);
2313 strncpy((*names
)[i
], policydb
.p_bool_val_to_name
[i
], name_len
);
2314 (*names
)[i
][name_len
- 1] = 0;
2318 read_unlock(&policy_rwlock
);
2322 for (i
= 0; i
< *len
; i
++)
2330 int security_set_bools(int len
, int *values
)
2333 int lenp
, seqno
= 0;
2334 struct cond_node
*cur
;
2336 write_lock_irq(&policy_rwlock
);
2338 lenp
= policydb
.p_bools
.nprim
;
2344 for (i
= 0; i
< len
; i
++) {
2345 if (!!values
[i
] != policydb
.bool_val_to_struct
[i
]->state
) {
2346 audit_log(current
->audit_context
, GFP_ATOMIC
,
2347 AUDIT_MAC_CONFIG_CHANGE
,
2348 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2349 policydb
.p_bool_val_to_name
[i
],
2351 policydb
.bool_val_to_struct
[i
]->state
,
2352 audit_get_loginuid(current
),
2353 audit_get_sessionid(current
));
2356 policydb
.bool_val_to_struct
[i
]->state
= 1;
2358 policydb
.bool_val_to_struct
[i
]->state
= 0;
2361 for (cur
= policydb
.cond_list
; cur
; cur
= cur
->next
) {
2362 rc
= evaluate_cond_node(&policydb
, cur
);
2367 seqno
= ++latest_granting
;
2370 write_unlock_irq(&policy_rwlock
);
2372 avc_ss_reset(seqno
);
2373 selnl_notify_policyload(seqno
);
2374 selinux_xfrm_notify_policyload();
2379 int security_get_bool_value(int bool)
2384 read_lock(&policy_rwlock
);
2386 len
= policydb
.p_bools
.nprim
;
2392 rc
= policydb
.bool_val_to_struct
[bool]->state
;
2394 read_unlock(&policy_rwlock
);
2398 static int security_preserve_bools(struct policydb
*p
)
2400 int rc
, nbools
= 0, *bvalues
= NULL
, i
;
2401 char **bnames
= NULL
;
2402 struct cond_bool_datum
*booldatum
;
2403 struct cond_node
*cur
;
2405 rc
= security_get_bools(&nbools
, &bnames
, &bvalues
);
2408 for (i
= 0; i
< nbools
; i
++) {
2409 booldatum
= hashtab_search(p
->p_bools
.table
, bnames
[i
]);
2411 booldatum
->state
= bvalues
[i
];
2413 for (cur
= p
->cond_list
; cur
; cur
= cur
->next
) {
2414 rc
= evaluate_cond_node(p
, cur
);
2421 for (i
= 0; i
< nbools
; i
++)
2430 * security_sid_mls_copy() - computes a new sid based on the given
2431 * sid and the mls portion of mls_sid.
2433 int security_sid_mls_copy(u32 sid
, u32 mls_sid
, u32
*new_sid
)
2435 struct context
*context1
;
2436 struct context
*context2
;
2437 struct context newcon
;
2442 if (!ss_initialized
|| !policydb
.mls_enabled
) {
2447 context_init(&newcon
);
2449 read_lock(&policy_rwlock
);
2450 context1
= sidtab_search(&sidtab
, sid
);
2452 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
2458 context2
= sidtab_search(&sidtab
, mls_sid
);
2460 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
2466 newcon
.user
= context1
->user
;
2467 newcon
.role
= context1
->role
;
2468 newcon
.type
= context1
->type
;
2469 rc
= mls_context_cpy(&newcon
, context2
);
2473 /* Check the validity of the new context. */
2474 if (!policydb_context_isvalid(&policydb
, &newcon
)) {
2475 rc
= convert_context_handle_invalid_context(&newcon
);
2480 rc
= sidtab_context_to_sid(&sidtab
, &newcon
, new_sid
);
2484 if (!context_struct_to_string(&newcon
, &s
, &len
)) {
2485 audit_log(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2486 "security_sid_mls_copy: invalid context %s", s
);
2491 read_unlock(&policy_rwlock
);
2492 context_destroy(&newcon
);
2498 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2499 * @nlbl_sid: NetLabel SID
2500 * @nlbl_type: NetLabel labeling protocol type
2501 * @xfrm_sid: XFRM SID
2504 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2505 * resolved into a single SID it is returned via @peer_sid and the function
2506 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2507 * returns a negative value. A table summarizing the behavior is below:
2509 * | function return | @sid
2510 * ------------------------------+-----------------+-----------------
2511 * no peer labels | 0 | SECSID_NULL
2512 * single peer label | 0 | <peer_label>
2513 * multiple, consistent labels | 0 | <peer_label>
2514 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2517 int security_net_peersid_resolve(u32 nlbl_sid
, u32 nlbl_type
,
2522 struct context
*nlbl_ctx
;
2523 struct context
*xfrm_ctx
;
2525 /* handle the common (which also happens to be the set of easy) cases
2526 * right away, these two if statements catch everything involving a
2527 * single or absent peer SID/label */
2528 if (xfrm_sid
== SECSID_NULL
) {
2529 *peer_sid
= nlbl_sid
;
2532 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2533 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2535 if (nlbl_sid
== SECSID_NULL
|| nlbl_type
== NETLBL_NLTYPE_UNLABELED
) {
2536 *peer_sid
= xfrm_sid
;
2540 /* we don't need to check ss_initialized here since the only way both
2541 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2542 * security server was initialized and ss_initialized was true */
2543 if (!policydb
.mls_enabled
) {
2544 *peer_sid
= SECSID_NULL
;
2548 read_lock(&policy_rwlock
);
2550 nlbl_ctx
= sidtab_search(&sidtab
, nlbl_sid
);
2552 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
2553 __func__
, nlbl_sid
);
2557 xfrm_ctx
= sidtab_search(&sidtab
, xfrm_sid
);
2559 printk(KERN_ERR
"SELinux: %s: unrecognized SID %d\n",
2560 __func__
, xfrm_sid
);
2564 rc
= (mls_context_cmp(nlbl_ctx
, xfrm_ctx
) ? 0 : -EACCES
);
2567 read_unlock(&policy_rwlock
);
2569 /* at present NetLabel SIDs/labels really only carry MLS
2570 * information so if the MLS portion of the NetLabel SID
2571 * matches the MLS portion of the labeled XFRM SID/label
2572 * then pass along the XFRM SID as it is the most
2574 *peer_sid
= xfrm_sid
;
2576 *peer_sid
= SECSID_NULL
;
2580 static int get_classes_callback(void *k
, void *d
, void *args
)
2582 struct class_datum
*datum
= d
;
2583 char *name
= k
, **classes
= args
;
2584 int value
= datum
->value
- 1;
2586 classes
[value
] = kstrdup(name
, GFP_ATOMIC
);
2587 if (!classes
[value
])
2593 int security_get_classes(char ***classes
, int *nclasses
)
2597 read_lock(&policy_rwlock
);
2599 *nclasses
= policydb
.p_classes
.nprim
;
2600 *classes
= kcalloc(*nclasses
, sizeof(**classes
), GFP_ATOMIC
);
2604 rc
= hashtab_map(policydb
.p_classes
.table
, get_classes_callback
,
2608 for (i
= 0; i
< *nclasses
; i
++)
2609 kfree((*classes
)[i
]);
2614 read_unlock(&policy_rwlock
);
2618 static int get_permissions_callback(void *k
, void *d
, void *args
)
2620 struct perm_datum
*datum
= d
;
2621 char *name
= k
, **perms
= args
;
2622 int value
= datum
->value
- 1;
2624 perms
[value
] = kstrdup(name
, GFP_ATOMIC
);
2631 int security_get_permissions(char *class, char ***perms
, int *nperms
)
2633 int rc
= -ENOMEM
, i
;
2634 struct class_datum
*match
;
2636 read_lock(&policy_rwlock
);
2638 match
= hashtab_search(policydb
.p_classes
.table
, class);
2640 printk(KERN_ERR
"SELinux: %s: unrecognized class %s\n",
2646 *nperms
= match
->permissions
.nprim
;
2647 *perms
= kcalloc(*nperms
, sizeof(**perms
), GFP_ATOMIC
);
2651 if (match
->comdatum
) {
2652 rc
= hashtab_map(match
->comdatum
->permissions
.table
,
2653 get_permissions_callback
, *perms
);
2658 rc
= hashtab_map(match
->permissions
.table
, get_permissions_callback
,
2664 read_unlock(&policy_rwlock
);
2668 read_unlock(&policy_rwlock
);
2669 for (i
= 0; i
< *nperms
; i
++)
2675 int security_get_reject_unknown(void)
2677 return policydb
.reject_unknown
;
2680 int security_get_allow_unknown(void)
2682 return policydb
.allow_unknown
;
2686 * security_policycap_supported - Check for a specific policy capability
2687 * @req_cap: capability
2690 * This function queries the currently loaded policy to see if it supports the
2691 * capability specified by @req_cap. Returns true (1) if the capability is
2692 * supported, false (0) if it isn't supported.
2695 int security_policycap_supported(unsigned int req_cap
)
2699 read_lock(&policy_rwlock
);
2700 rc
= ebitmap_get_bit(&policydb
.policycaps
, req_cap
);
2701 read_unlock(&policy_rwlock
);
2706 struct selinux_audit_rule
{
2708 struct context au_ctxt
;
2711 void selinux_audit_rule_free(void *vrule
)
2713 struct selinux_audit_rule
*rule
= vrule
;
2716 context_destroy(&rule
->au_ctxt
);
2721 int selinux_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
2723 struct selinux_audit_rule
*tmprule
;
2724 struct role_datum
*roledatum
;
2725 struct type_datum
*typedatum
;
2726 struct user_datum
*userdatum
;
2727 struct selinux_audit_rule
**rule
= (struct selinux_audit_rule
**)vrule
;
2732 if (!ss_initialized
)
2736 case AUDIT_SUBJ_USER
:
2737 case AUDIT_SUBJ_ROLE
:
2738 case AUDIT_SUBJ_TYPE
:
2739 case AUDIT_OBJ_USER
:
2740 case AUDIT_OBJ_ROLE
:
2741 case AUDIT_OBJ_TYPE
:
2742 /* only 'equals' and 'not equals' fit user, role, and type */
2743 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
2746 case AUDIT_SUBJ_SEN
:
2747 case AUDIT_SUBJ_CLR
:
2748 case AUDIT_OBJ_LEV_LOW
:
2749 case AUDIT_OBJ_LEV_HIGH
:
2750 /* we do not allow a range, indicated by the presense of '-' */
2751 if (strchr(rulestr
, '-'))
2755 /* only the above fields are valid */
2759 tmprule
= kzalloc(sizeof(struct selinux_audit_rule
), GFP_KERNEL
);
2763 context_init(&tmprule
->au_ctxt
);
2765 read_lock(&policy_rwlock
);
2767 tmprule
->au_seqno
= latest_granting
;
2770 case AUDIT_SUBJ_USER
:
2771 case AUDIT_OBJ_USER
:
2772 userdatum
= hashtab_search(policydb
.p_users
.table
, rulestr
);
2776 tmprule
->au_ctxt
.user
= userdatum
->value
;
2778 case AUDIT_SUBJ_ROLE
:
2779 case AUDIT_OBJ_ROLE
:
2780 roledatum
= hashtab_search(policydb
.p_roles
.table
, rulestr
);
2784 tmprule
->au_ctxt
.role
= roledatum
->value
;
2786 case AUDIT_SUBJ_TYPE
:
2787 case AUDIT_OBJ_TYPE
:
2788 typedatum
= hashtab_search(policydb
.p_types
.table
, rulestr
);
2792 tmprule
->au_ctxt
.type
= typedatum
->value
;
2794 case AUDIT_SUBJ_SEN
:
2795 case AUDIT_SUBJ_CLR
:
2796 case AUDIT_OBJ_LEV_LOW
:
2797 case AUDIT_OBJ_LEV_HIGH
:
2798 rc
= mls_from_string(rulestr
, &tmprule
->au_ctxt
, GFP_ATOMIC
);
2802 read_unlock(&policy_rwlock
);
2805 selinux_audit_rule_free(tmprule
);
2814 /* Check to see if the rule contains any selinux fields */
2815 int selinux_audit_rule_known(struct audit_krule
*rule
)
2819 for (i
= 0; i
< rule
->field_count
; i
++) {
2820 struct audit_field
*f
= &rule
->fields
[i
];
2822 case AUDIT_SUBJ_USER
:
2823 case AUDIT_SUBJ_ROLE
:
2824 case AUDIT_SUBJ_TYPE
:
2825 case AUDIT_SUBJ_SEN
:
2826 case AUDIT_SUBJ_CLR
:
2827 case AUDIT_OBJ_USER
:
2828 case AUDIT_OBJ_ROLE
:
2829 case AUDIT_OBJ_TYPE
:
2830 case AUDIT_OBJ_LEV_LOW
:
2831 case AUDIT_OBJ_LEV_HIGH
:
2839 int selinux_audit_rule_match(u32 sid
, u32 field
, u32 op
, void *vrule
,
2840 struct audit_context
*actx
)
2842 struct context
*ctxt
;
2843 struct mls_level
*level
;
2844 struct selinux_audit_rule
*rule
= vrule
;
2848 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2849 "selinux_audit_rule_match: missing rule\n");
2853 read_lock(&policy_rwlock
);
2855 if (rule
->au_seqno
< latest_granting
) {
2856 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2857 "selinux_audit_rule_match: stale rule\n");
2862 ctxt
= sidtab_search(&sidtab
, sid
);
2864 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
2865 "selinux_audit_rule_match: unrecognized SID %d\n",
2871 /* a field/op pair that is not caught here will simply fall through
2874 case AUDIT_SUBJ_USER
:
2875 case AUDIT_OBJ_USER
:
2878 match
= (ctxt
->user
== rule
->au_ctxt
.user
);
2880 case Audit_not_equal
:
2881 match
= (ctxt
->user
!= rule
->au_ctxt
.user
);
2885 case AUDIT_SUBJ_ROLE
:
2886 case AUDIT_OBJ_ROLE
:
2889 match
= (ctxt
->role
== rule
->au_ctxt
.role
);
2891 case Audit_not_equal
:
2892 match
= (ctxt
->role
!= rule
->au_ctxt
.role
);
2896 case AUDIT_SUBJ_TYPE
:
2897 case AUDIT_OBJ_TYPE
:
2900 match
= (ctxt
->type
== rule
->au_ctxt
.type
);
2902 case Audit_not_equal
:
2903 match
= (ctxt
->type
!= rule
->au_ctxt
.type
);
2907 case AUDIT_SUBJ_SEN
:
2908 case AUDIT_SUBJ_CLR
:
2909 case AUDIT_OBJ_LEV_LOW
:
2910 case AUDIT_OBJ_LEV_HIGH
:
2911 level
= ((field
== AUDIT_SUBJ_SEN
||
2912 field
== AUDIT_OBJ_LEV_LOW
) ?
2913 &ctxt
->range
.level
[0] : &ctxt
->range
.level
[1]);
2916 match
= mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2919 case Audit_not_equal
:
2920 match
= !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2924 match
= (mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2926 !mls_level_eq(&rule
->au_ctxt
.range
.level
[0],
2930 match
= mls_level_dom(&rule
->au_ctxt
.range
.level
[0],
2934 match
= (mls_level_dom(level
,
2935 &rule
->au_ctxt
.range
.level
[0]) &&
2936 !mls_level_eq(level
,
2937 &rule
->au_ctxt
.range
.level
[0]));
2940 match
= mls_level_dom(level
,
2941 &rule
->au_ctxt
.range
.level
[0]);
2947 read_unlock(&policy_rwlock
);
2951 static int (*aurule_callback
)(void) = audit_update_lsm_rules
;
2953 static int aurule_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
2954 u16
class, u32 perms
, u32
*retained
)
2958 if (event
== AVC_CALLBACK_RESET
&& aurule_callback
)
2959 err
= aurule_callback();
2963 static int __init
aurule_init(void)
2967 err
= avc_add_callback(aurule_avc_callback
, AVC_CALLBACK_RESET
,
2968 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
2970 panic("avc_add_callback() failed, error %d\n", err
);
2974 __initcall(aurule_init
);
2976 #ifdef CONFIG_NETLABEL
2978 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2979 * @secattr: the NetLabel packet security attributes
2980 * @sid: the SELinux SID
2983 * Attempt to cache the context in @ctx, which was derived from the packet in
2984 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2985 * already been initialized.
2988 static void security_netlbl_cache_add(struct netlbl_lsm_secattr
*secattr
,
2993 sid_cache
= kmalloc(sizeof(*sid_cache
), GFP_ATOMIC
);
2994 if (sid_cache
== NULL
)
2996 secattr
->cache
= netlbl_secattr_cache_alloc(GFP_ATOMIC
);
2997 if (secattr
->cache
== NULL
) {
3003 secattr
->cache
->free
= kfree
;
3004 secattr
->cache
->data
= sid_cache
;
3005 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
3009 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
3010 * @secattr: the NetLabel packet security attributes
3011 * @sid: the SELinux SID
3014 * Convert the given NetLabel security attributes in @secattr into a
3015 * SELinux SID. If the @secattr field does not contain a full SELinux
3016 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
3017 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3018 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3019 * conversion for future lookups. Returns zero on success, negative values on
3023 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr
*secattr
,
3027 struct context
*ctx
;
3028 struct context ctx_new
;
3030 if (!ss_initialized
) {
3035 read_lock(&policy_rwlock
);
3037 if (secattr
->flags
& NETLBL_SECATTR_CACHE
) {
3038 *sid
= *(u32
*)secattr
->cache
->data
;
3040 } else if (secattr
->flags
& NETLBL_SECATTR_SECID
) {
3041 *sid
= secattr
->attr
.secid
;
3043 } else if (secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) {
3044 ctx
= sidtab_search(&sidtab
, SECINITSID_NETMSG
);
3046 goto netlbl_secattr_to_sid_return
;
3048 context_init(&ctx_new
);
3049 ctx_new
.user
= ctx
->user
;
3050 ctx_new
.role
= ctx
->role
;
3051 ctx_new
.type
= ctx
->type
;
3052 mls_import_netlbl_lvl(&ctx_new
, secattr
);
3053 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
3054 if (ebitmap_netlbl_import(&ctx_new
.range
.level
[0].cat
,
3055 secattr
->attr
.mls
.cat
) != 0)
3056 goto netlbl_secattr_to_sid_return
;
3057 memcpy(&ctx_new
.range
.level
[1].cat
,
3058 &ctx_new
.range
.level
[0].cat
,
3059 sizeof(ctx_new
.range
.level
[0].cat
));
3061 if (mls_context_isvalid(&policydb
, &ctx_new
) != 1)
3062 goto netlbl_secattr_to_sid_return_cleanup
;
3064 rc
= sidtab_context_to_sid(&sidtab
, &ctx_new
, sid
);
3066 goto netlbl_secattr_to_sid_return_cleanup
;
3068 security_netlbl_cache_add(secattr
, *sid
);
3070 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
3076 netlbl_secattr_to_sid_return
:
3077 read_unlock(&policy_rwlock
);
3079 netlbl_secattr_to_sid_return_cleanup
:
3080 ebitmap_destroy(&ctx_new
.range
.level
[0].cat
);
3081 goto netlbl_secattr_to_sid_return
;
3085 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3086 * @sid: the SELinux SID
3087 * @secattr: the NetLabel packet security attributes
3090 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3091 * Returns zero on success, negative values on failure.
3094 int security_netlbl_sid_to_secattr(u32 sid
, struct netlbl_lsm_secattr
*secattr
)
3097 struct context
*ctx
;
3099 if (!ss_initialized
)
3102 read_lock(&policy_rwlock
);
3103 ctx
= sidtab_search(&sidtab
, sid
);
3106 goto netlbl_sid_to_secattr_failure
;
3108 secattr
->domain
= kstrdup(policydb
.p_type_val_to_name
[ctx
->type
- 1],
3110 if (secattr
->domain
== NULL
) {
3112 goto netlbl_sid_to_secattr_failure
;
3114 secattr
->attr
.secid
= sid
;
3115 secattr
->flags
|= NETLBL_SECATTR_DOMAIN_CPY
| NETLBL_SECATTR_SECID
;
3116 mls_export_netlbl_lvl(ctx
, secattr
);
3117 rc
= mls_export_netlbl_cat(ctx
, secattr
);
3119 goto netlbl_sid_to_secattr_failure
;
3120 read_unlock(&policy_rwlock
);
3124 netlbl_sid_to_secattr_failure
:
3125 read_unlock(&policy_rwlock
);
3128 #endif /* CONFIG_NETLABEL */