SELinux: rename selinux_netlabel.h to netlabel.h
[firewire-audio.git] / security / selinux / ss / services.c
blobf4129f589313a5dd4e235dd18c15d9d90e7e181a
1 /*
2 * Implementation of the security services.
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for NetLabel
20 * Updated: Chad Sellers <csellers@tresys.com>
22 * Added validation of kernel classes and permissions
24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/spinlock.h>
36 #include <linux/rcupdate.h>
37 #include <linux/errno.h>
38 #include <linux/in.h>
39 #include <linux/sched.h>
40 #include <linux/audit.h>
41 #include <linux/mutex.h>
42 #include <net/sock.h>
43 #include <net/netlabel.h>
45 #include "flask.h"
46 #include "avc.h"
47 #include "avc_ss.h"
48 #include "security.h"
49 #include "context.h"
50 #include "policydb.h"
51 #include "sidtab.h"
52 #include "services.h"
53 #include "conditional.h"
54 #include "mls.h"
55 #include "objsec.h"
56 #include "netlabel.h"
57 #include "xfrm.h"
58 #include "ebitmap.h"
60 extern void selnl_notify_policyload(u32 seqno);
61 unsigned int policydb_loaded_version;
64 * This is declared in avc.c
66 extern const struct selinux_class_perm selinux_class_perm;
68 static DEFINE_RWLOCK(policy_rwlock);
69 #define POLICY_RDLOCK read_lock(&policy_rwlock)
70 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
71 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
72 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
74 static DEFINE_MUTEX(load_mutex);
75 #define LOAD_LOCK mutex_lock(&load_mutex)
76 #define LOAD_UNLOCK mutex_unlock(&load_mutex)
78 static struct sidtab sidtab;
79 struct policydb policydb;
80 int ss_initialized = 0;
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
86 * occurs.
88 static u32 latest_granting = 0;
90 /* Forward declaration. */
91 static int context_struct_to_string(struct context *context, char **scontext,
92 u32 *scontext_len);
95 * Return the boolean value of a constraint expression
96 * when it is applied to the specified source and target
97 * security contexts.
99 * xcontext is a special beast... It is used by the validatetrans rules
100 * only. For these rules, scontext is the context before the transition,
101 * tcontext is the context after the transition, and xcontext is the context
102 * of the process performing the transition. All other callers of
103 * constraint_expr_eval should pass in NULL for xcontext.
105 static int constraint_expr_eval(struct context *scontext,
106 struct context *tcontext,
107 struct context *xcontext,
108 struct constraint_expr *cexpr)
110 u32 val1, val2;
111 struct context *c;
112 struct role_datum *r1, *r2;
113 struct mls_level *l1, *l2;
114 struct constraint_expr *e;
115 int s[CEXPR_MAXDEPTH];
116 int sp = -1;
118 for (e = cexpr; e; e = e->next) {
119 switch (e->expr_type) {
120 case CEXPR_NOT:
121 BUG_ON(sp < 0);
122 s[sp] = !s[sp];
123 break;
124 case CEXPR_AND:
125 BUG_ON(sp < 1);
126 sp--;
127 s[sp] &= s[sp+1];
128 break;
129 case CEXPR_OR:
130 BUG_ON(sp < 1);
131 sp--;
132 s[sp] |= s[sp+1];
133 break;
134 case CEXPR_ATTR:
135 if (sp == (CEXPR_MAXDEPTH-1))
136 return 0;
137 switch (e->attr) {
138 case CEXPR_USER:
139 val1 = scontext->user;
140 val2 = tcontext->user;
141 break;
142 case CEXPR_TYPE:
143 val1 = scontext->type;
144 val2 = tcontext->type;
145 break;
146 case CEXPR_ROLE:
147 val1 = scontext->role;
148 val2 = tcontext->role;
149 r1 = policydb.role_val_to_struct[val1 - 1];
150 r2 = policydb.role_val_to_struct[val2 - 1];
151 switch (e->op) {
152 case CEXPR_DOM:
153 s[++sp] = ebitmap_get_bit(&r1->dominates,
154 val2 - 1);
155 continue;
156 case CEXPR_DOMBY:
157 s[++sp] = ebitmap_get_bit(&r2->dominates,
158 val1 - 1);
159 continue;
160 case CEXPR_INCOMP:
161 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
162 val2 - 1) &&
163 !ebitmap_get_bit(&r2->dominates,
164 val1 - 1) );
165 continue;
166 default:
167 break;
169 break;
170 case CEXPR_L1L2:
171 l1 = &(scontext->range.level[0]);
172 l2 = &(tcontext->range.level[0]);
173 goto mls_ops;
174 case CEXPR_L1H2:
175 l1 = &(scontext->range.level[0]);
176 l2 = &(tcontext->range.level[1]);
177 goto mls_ops;
178 case CEXPR_H1L2:
179 l1 = &(scontext->range.level[1]);
180 l2 = &(tcontext->range.level[0]);
181 goto mls_ops;
182 case CEXPR_H1H2:
183 l1 = &(scontext->range.level[1]);
184 l2 = &(tcontext->range.level[1]);
185 goto mls_ops;
186 case CEXPR_L1H1:
187 l1 = &(scontext->range.level[0]);
188 l2 = &(scontext->range.level[1]);
189 goto mls_ops;
190 case CEXPR_L2H2:
191 l1 = &(tcontext->range.level[0]);
192 l2 = &(tcontext->range.level[1]);
193 goto mls_ops;
194 mls_ops:
195 switch (e->op) {
196 case CEXPR_EQ:
197 s[++sp] = mls_level_eq(l1, l2);
198 continue;
199 case CEXPR_NEQ:
200 s[++sp] = !mls_level_eq(l1, l2);
201 continue;
202 case CEXPR_DOM:
203 s[++sp] = mls_level_dom(l1, l2);
204 continue;
205 case CEXPR_DOMBY:
206 s[++sp] = mls_level_dom(l2, l1);
207 continue;
208 case CEXPR_INCOMP:
209 s[++sp] = mls_level_incomp(l2, l1);
210 continue;
211 default:
212 BUG();
213 return 0;
215 break;
216 default:
217 BUG();
218 return 0;
221 switch (e->op) {
222 case CEXPR_EQ:
223 s[++sp] = (val1 == val2);
224 break;
225 case CEXPR_NEQ:
226 s[++sp] = (val1 != val2);
227 break;
228 default:
229 BUG();
230 return 0;
232 break;
233 case CEXPR_NAMES:
234 if (sp == (CEXPR_MAXDEPTH-1))
235 return 0;
236 c = scontext;
237 if (e->attr & CEXPR_TARGET)
238 c = tcontext;
239 else if (e->attr & CEXPR_XTARGET) {
240 c = xcontext;
241 if (!c) {
242 BUG();
243 return 0;
246 if (e->attr & CEXPR_USER)
247 val1 = c->user;
248 else if (e->attr & CEXPR_ROLE)
249 val1 = c->role;
250 else if (e->attr & CEXPR_TYPE)
251 val1 = c->type;
252 else {
253 BUG();
254 return 0;
257 switch (e->op) {
258 case CEXPR_EQ:
259 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
260 break;
261 case CEXPR_NEQ:
262 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
263 break;
264 default:
265 BUG();
266 return 0;
268 break;
269 default:
270 BUG();
271 return 0;
275 BUG_ON(sp != 0);
276 return s[0];
280 * Compute access vectors based on a context structure pair for
281 * the permissions in a particular class.
283 static int context_struct_compute_av(struct context *scontext,
284 struct context *tcontext,
285 u16 tclass,
286 u32 requested,
287 struct av_decision *avd)
289 struct constraint_node *constraint;
290 struct role_allow *ra;
291 struct avtab_key avkey;
292 struct avtab_node *node;
293 struct class_datum *tclass_datum;
294 struct ebitmap *sattr, *tattr;
295 struct ebitmap_node *snode, *tnode;
296 unsigned int i, j;
299 * Remap extended Netlink classes for old policy versions.
300 * Do this here rather than socket_type_to_security_class()
301 * in case a newer policy version is loaded, allowing sockets
302 * to remain in the correct class.
304 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
305 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
306 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
307 tclass = SECCLASS_NETLINK_SOCKET;
309 if (!tclass || tclass > policydb.p_classes.nprim) {
310 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
311 tclass);
312 return -EINVAL;
314 tclass_datum = policydb.class_val_to_struct[tclass - 1];
317 * Initialize the access vectors to the default values.
319 avd->allowed = 0;
320 avd->decided = 0xffffffff;
321 avd->auditallow = 0;
322 avd->auditdeny = 0xffffffff;
323 avd->seqno = latest_granting;
326 * If a specific type enforcement rule was defined for
327 * this permission check, then use it.
329 avkey.target_class = tclass;
330 avkey.specified = AVTAB_AV;
331 sattr = &policydb.type_attr_map[scontext->type - 1];
332 tattr = &policydb.type_attr_map[tcontext->type - 1];
333 ebitmap_for_each_bit(sattr, snode, i) {
334 if (!ebitmap_node_get_bit(snode, i))
335 continue;
336 ebitmap_for_each_bit(tattr, tnode, j) {
337 if (!ebitmap_node_get_bit(tnode, j))
338 continue;
339 avkey.source_type = i + 1;
340 avkey.target_type = j + 1;
341 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
342 node != NULL;
343 node = avtab_search_node_next(node, avkey.specified)) {
344 if (node->key.specified == AVTAB_ALLOWED)
345 avd->allowed |= node->datum.data;
346 else if (node->key.specified == AVTAB_AUDITALLOW)
347 avd->auditallow |= node->datum.data;
348 else if (node->key.specified == AVTAB_AUDITDENY)
349 avd->auditdeny &= node->datum.data;
352 /* Check conditional av table for additional permissions */
353 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
359 * Remove any permissions prohibited by a constraint (this includes
360 * the MLS policy).
362 constraint = tclass_datum->constraints;
363 while (constraint) {
364 if ((constraint->permissions & (avd->allowed)) &&
365 !constraint_expr_eval(scontext, tcontext, NULL,
366 constraint->expr)) {
367 avd->allowed = (avd->allowed) & ~(constraint->permissions);
369 constraint = constraint->next;
373 * If checking process transition permission and the
374 * role is changing, then check the (current_role, new_role)
375 * pair.
377 if (tclass == SECCLASS_PROCESS &&
378 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
379 scontext->role != tcontext->role) {
380 for (ra = policydb.role_allow; ra; ra = ra->next) {
381 if (scontext->role == ra->role &&
382 tcontext->role == ra->new_role)
383 break;
385 if (!ra)
386 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
387 PROCESS__DYNTRANSITION);
390 return 0;
393 static int security_validtrans_handle_fail(struct context *ocontext,
394 struct context *ncontext,
395 struct context *tcontext,
396 u16 tclass)
398 char *o = NULL, *n = NULL, *t = NULL;
399 u32 olen, nlen, tlen;
401 if (context_struct_to_string(ocontext, &o, &olen) < 0)
402 goto out;
403 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
404 goto out;
405 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
406 goto out;
407 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
408 "security_validate_transition: denied for"
409 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
410 o, n, t, policydb.p_class_val_to_name[tclass-1]);
411 out:
412 kfree(o);
413 kfree(n);
414 kfree(t);
416 if (!selinux_enforcing)
417 return 0;
418 return -EPERM;
421 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
422 u16 tclass)
424 struct context *ocontext;
425 struct context *ncontext;
426 struct context *tcontext;
427 struct class_datum *tclass_datum;
428 struct constraint_node *constraint;
429 int rc = 0;
431 if (!ss_initialized)
432 return 0;
434 POLICY_RDLOCK;
437 * Remap extended Netlink classes for old policy versions.
438 * Do this here rather than socket_type_to_security_class()
439 * in case a newer policy version is loaded, allowing sockets
440 * to remain in the correct class.
442 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
443 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
444 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
445 tclass = SECCLASS_NETLINK_SOCKET;
447 if (!tclass || tclass > policydb.p_classes.nprim) {
448 printk(KERN_ERR "security_validate_transition: "
449 "unrecognized class %d\n", tclass);
450 rc = -EINVAL;
451 goto out;
453 tclass_datum = policydb.class_val_to_struct[tclass - 1];
455 ocontext = sidtab_search(&sidtab, oldsid);
456 if (!ocontext) {
457 printk(KERN_ERR "security_validate_transition: "
458 " unrecognized SID %d\n", oldsid);
459 rc = -EINVAL;
460 goto out;
463 ncontext = sidtab_search(&sidtab, newsid);
464 if (!ncontext) {
465 printk(KERN_ERR "security_validate_transition: "
466 " unrecognized SID %d\n", newsid);
467 rc = -EINVAL;
468 goto out;
471 tcontext = sidtab_search(&sidtab, tasksid);
472 if (!tcontext) {
473 printk(KERN_ERR "security_validate_transition: "
474 " unrecognized SID %d\n", tasksid);
475 rc = -EINVAL;
476 goto out;
479 constraint = tclass_datum->validatetrans;
480 while (constraint) {
481 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
482 constraint->expr)) {
483 rc = security_validtrans_handle_fail(ocontext, ncontext,
484 tcontext, tclass);
485 goto out;
487 constraint = constraint->next;
490 out:
491 POLICY_RDUNLOCK;
492 return rc;
496 * security_compute_av - Compute access vector decisions.
497 * @ssid: source security identifier
498 * @tsid: target security identifier
499 * @tclass: target security class
500 * @requested: requested permissions
501 * @avd: access vector decisions
503 * Compute a set of access vector decisions based on the
504 * SID pair (@ssid, @tsid) for the permissions in @tclass.
505 * Return -%EINVAL if any of the parameters are invalid or %0
506 * if the access vector decisions were computed successfully.
508 int security_compute_av(u32 ssid,
509 u32 tsid,
510 u16 tclass,
511 u32 requested,
512 struct av_decision *avd)
514 struct context *scontext = NULL, *tcontext = NULL;
515 int rc = 0;
517 if (!ss_initialized) {
518 avd->allowed = 0xffffffff;
519 avd->decided = 0xffffffff;
520 avd->auditallow = 0;
521 avd->auditdeny = 0xffffffff;
522 avd->seqno = latest_granting;
523 return 0;
526 POLICY_RDLOCK;
528 scontext = sidtab_search(&sidtab, ssid);
529 if (!scontext) {
530 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
531 ssid);
532 rc = -EINVAL;
533 goto out;
535 tcontext = sidtab_search(&sidtab, tsid);
536 if (!tcontext) {
537 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
538 tsid);
539 rc = -EINVAL;
540 goto out;
543 rc = context_struct_compute_av(scontext, tcontext, tclass,
544 requested, avd);
545 out:
546 POLICY_RDUNLOCK;
547 return rc;
551 * Write the security context string representation of
552 * the context structure `context' into a dynamically
553 * allocated string of the correct size. Set `*scontext'
554 * to point to this string and set `*scontext_len' to
555 * the length of the string.
557 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
559 char *scontextp;
561 *scontext = NULL;
562 *scontext_len = 0;
564 /* Compute the size of the context. */
565 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
566 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
567 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
568 *scontext_len += mls_compute_context_len(context);
570 /* Allocate space for the context; caller must free this space. */
571 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
572 if (!scontextp) {
573 return -ENOMEM;
575 *scontext = scontextp;
578 * Copy the user name, role name and type name into the context.
580 sprintf(scontextp, "%s:%s:%s",
581 policydb.p_user_val_to_name[context->user - 1],
582 policydb.p_role_val_to_name[context->role - 1],
583 policydb.p_type_val_to_name[context->type - 1]);
584 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
585 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
586 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
588 mls_sid_to_context(context, &scontextp);
590 *scontextp = 0;
592 return 0;
595 #include "initial_sid_to_string.h"
598 * security_sid_to_context - Obtain a context for a given SID.
599 * @sid: security identifier, SID
600 * @scontext: security context
601 * @scontext_len: length in bytes
603 * Write the string representation of the context associated with @sid
604 * into a dynamically allocated string of the correct size. Set @scontext
605 * to point to this string and set @scontext_len to the length of the string.
607 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
609 struct context *context;
610 int rc = 0;
612 *scontext = NULL;
613 *scontext_len = 0;
615 if (!ss_initialized) {
616 if (sid <= SECINITSID_NUM) {
617 char *scontextp;
619 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
620 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
621 if (!scontextp) {
622 rc = -ENOMEM;
623 goto out;
625 strcpy(scontextp, initial_sid_to_string[sid]);
626 *scontext = scontextp;
627 goto out;
629 printk(KERN_ERR "security_sid_to_context: called before initial "
630 "load_policy on unknown SID %d\n", sid);
631 rc = -EINVAL;
632 goto out;
634 POLICY_RDLOCK;
635 context = sidtab_search(&sidtab, sid);
636 if (!context) {
637 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
638 "%d\n", sid);
639 rc = -EINVAL;
640 goto out_unlock;
642 rc = context_struct_to_string(context, scontext, scontext_len);
643 out_unlock:
644 POLICY_RDUNLOCK;
645 out:
646 return rc;
650 static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
652 char *scontext2;
653 struct context context;
654 struct role_datum *role;
655 struct type_datum *typdatum;
656 struct user_datum *usrdatum;
657 char *scontextp, *p, oldc;
658 int rc = 0;
660 if (!ss_initialized) {
661 int i;
663 for (i = 1; i < SECINITSID_NUM; i++) {
664 if (!strcmp(initial_sid_to_string[i], scontext)) {
665 *sid = i;
666 goto out;
669 *sid = SECINITSID_KERNEL;
670 goto out;
672 *sid = SECSID_NULL;
674 /* Copy the string so that we can modify the copy as we parse it.
675 The string should already by null terminated, but we append a
676 null suffix to the copy to avoid problems with the existing
677 attr package, which doesn't view the null terminator as part
678 of the attribute value. */
679 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
680 if (!scontext2) {
681 rc = -ENOMEM;
682 goto out;
684 memcpy(scontext2, scontext, scontext_len);
685 scontext2[scontext_len] = 0;
687 context_init(&context);
688 *sid = SECSID_NULL;
690 POLICY_RDLOCK;
692 /* Parse the security context. */
694 rc = -EINVAL;
695 scontextp = (char *) scontext2;
697 /* Extract the user. */
698 p = scontextp;
699 while (*p && *p != ':')
700 p++;
702 if (*p == 0)
703 goto out_unlock;
705 *p++ = 0;
707 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
708 if (!usrdatum)
709 goto out_unlock;
711 context.user = usrdatum->value;
713 /* Extract role. */
714 scontextp = p;
715 while (*p && *p != ':')
716 p++;
718 if (*p == 0)
719 goto out_unlock;
721 *p++ = 0;
723 role = hashtab_search(policydb.p_roles.table, scontextp);
724 if (!role)
725 goto out_unlock;
726 context.role = role->value;
728 /* Extract type. */
729 scontextp = p;
730 while (*p && *p != ':')
731 p++;
732 oldc = *p;
733 *p++ = 0;
735 typdatum = hashtab_search(policydb.p_types.table, scontextp);
736 if (!typdatum)
737 goto out_unlock;
739 context.type = typdatum->value;
741 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
742 if (rc)
743 goto out_unlock;
745 if ((p - scontext2) < scontext_len) {
746 rc = -EINVAL;
747 goto out_unlock;
750 /* Check the validity of the new context. */
751 if (!policydb_context_isvalid(&policydb, &context)) {
752 rc = -EINVAL;
753 goto out_unlock;
755 /* Obtain the new sid. */
756 rc = sidtab_context_to_sid(&sidtab, &context, sid);
757 out_unlock:
758 POLICY_RDUNLOCK;
759 context_destroy(&context);
760 kfree(scontext2);
761 out:
762 return rc;
766 * security_context_to_sid - Obtain a SID for a given security context.
767 * @scontext: security context
768 * @scontext_len: length in bytes
769 * @sid: security identifier, SID
771 * Obtains a SID associated with the security context that
772 * has the string representation specified by @scontext.
773 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
774 * memory is available, or 0 on success.
776 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
778 return security_context_to_sid_core(scontext, scontext_len,
779 sid, SECSID_NULL);
783 * security_context_to_sid_default - Obtain a SID for a given security context,
784 * falling back to specified default if needed.
786 * @scontext: security context
787 * @scontext_len: length in bytes
788 * @sid: security identifier, SID
789 * @def_sid: default SID to assign on errror
791 * Obtains a SID associated with the security context that
792 * has the string representation specified by @scontext.
793 * The default SID is passed to the MLS layer to be used to allow
794 * kernel labeling of the MLS field if the MLS field is not present
795 * (for upgrading to MLS without full relabel).
796 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
797 * memory is available, or 0 on success.
799 int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
801 return security_context_to_sid_core(scontext, scontext_len,
802 sid, def_sid);
805 static int compute_sid_handle_invalid_context(
806 struct context *scontext,
807 struct context *tcontext,
808 u16 tclass,
809 struct context *newcontext)
811 char *s = NULL, *t = NULL, *n = NULL;
812 u32 slen, tlen, nlen;
814 if (context_struct_to_string(scontext, &s, &slen) < 0)
815 goto out;
816 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
817 goto out;
818 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
819 goto out;
820 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
821 "security_compute_sid: invalid context %s"
822 " for scontext=%s"
823 " tcontext=%s"
824 " tclass=%s",
825 n, s, t, policydb.p_class_val_to_name[tclass-1]);
826 out:
827 kfree(s);
828 kfree(t);
829 kfree(n);
830 if (!selinux_enforcing)
831 return 0;
832 return -EACCES;
835 static int security_compute_sid(u32 ssid,
836 u32 tsid,
837 u16 tclass,
838 u32 specified,
839 u32 *out_sid)
841 struct context *scontext = NULL, *tcontext = NULL, newcontext;
842 struct role_trans *roletr = NULL;
843 struct avtab_key avkey;
844 struct avtab_datum *avdatum;
845 struct avtab_node *node;
846 int rc = 0;
848 if (!ss_initialized) {
849 switch (tclass) {
850 case SECCLASS_PROCESS:
851 *out_sid = ssid;
852 break;
853 default:
854 *out_sid = tsid;
855 break;
857 goto out;
860 context_init(&newcontext);
862 POLICY_RDLOCK;
864 scontext = sidtab_search(&sidtab, ssid);
865 if (!scontext) {
866 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
867 ssid);
868 rc = -EINVAL;
869 goto out_unlock;
871 tcontext = sidtab_search(&sidtab, tsid);
872 if (!tcontext) {
873 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
874 tsid);
875 rc = -EINVAL;
876 goto out_unlock;
879 /* Set the user identity. */
880 switch (specified) {
881 case AVTAB_TRANSITION:
882 case AVTAB_CHANGE:
883 /* Use the process user identity. */
884 newcontext.user = scontext->user;
885 break;
886 case AVTAB_MEMBER:
887 /* Use the related object owner. */
888 newcontext.user = tcontext->user;
889 break;
892 /* Set the role and type to default values. */
893 switch (tclass) {
894 case SECCLASS_PROCESS:
895 /* Use the current role and type of process. */
896 newcontext.role = scontext->role;
897 newcontext.type = scontext->type;
898 break;
899 default:
900 /* Use the well-defined object role. */
901 newcontext.role = OBJECT_R_VAL;
902 /* Use the type of the related object. */
903 newcontext.type = tcontext->type;
906 /* Look for a type transition/member/change rule. */
907 avkey.source_type = scontext->type;
908 avkey.target_type = tcontext->type;
909 avkey.target_class = tclass;
910 avkey.specified = specified;
911 avdatum = avtab_search(&policydb.te_avtab, &avkey);
913 /* If no permanent rule, also check for enabled conditional rules */
914 if(!avdatum) {
915 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
916 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
917 if (node->key.specified & AVTAB_ENABLED) {
918 avdatum = &node->datum;
919 break;
924 if (avdatum) {
925 /* Use the type from the type transition/member/change rule. */
926 newcontext.type = avdatum->data;
929 /* Check for class-specific changes. */
930 switch (tclass) {
931 case SECCLASS_PROCESS:
932 if (specified & AVTAB_TRANSITION) {
933 /* Look for a role transition rule. */
934 for (roletr = policydb.role_tr; roletr;
935 roletr = roletr->next) {
936 if (roletr->role == scontext->role &&
937 roletr->type == tcontext->type) {
938 /* Use the role transition rule. */
939 newcontext.role = roletr->new_role;
940 break;
944 break;
945 default:
946 break;
949 /* Set the MLS attributes.
950 This is done last because it may allocate memory. */
951 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
952 if (rc)
953 goto out_unlock;
955 /* Check the validity of the context. */
956 if (!policydb_context_isvalid(&policydb, &newcontext)) {
957 rc = compute_sid_handle_invalid_context(scontext,
958 tcontext,
959 tclass,
960 &newcontext);
961 if (rc)
962 goto out_unlock;
964 /* Obtain the sid for the context. */
965 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
966 out_unlock:
967 POLICY_RDUNLOCK;
968 context_destroy(&newcontext);
969 out:
970 return rc;
974 * security_transition_sid - Compute the SID for a new subject/object.
975 * @ssid: source security identifier
976 * @tsid: target security identifier
977 * @tclass: target security class
978 * @out_sid: security identifier for new subject/object
980 * Compute a SID to use for labeling a new subject or object in the
981 * class @tclass based on a SID pair (@ssid, @tsid).
982 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
983 * if insufficient memory is available, or %0 if the new SID was
984 * computed successfully.
986 int security_transition_sid(u32 ssid,
987 u32 tsid,
988 u16 tclass,
989 u32 *out_sid)
991 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
995 * security_member_sid - Compute the SID for member selection.
996 * @ssid: source security identifier
997 * @tsid: target security identifier
998 * @tclass: target security class
999 * @out_sid: security identifier for selected member
1001 * Compute a SID to use when selecting a member of a polyinstantiated
1002 * object of class @tclass based on a SID pair (@ssid, @tsid).
1003 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1004 * if insufficient memory is available, or %0 if the SID was
1005 * computed successfully.
1007 int security_member_sid(u32 ssid,
1008 u32 tsid,
1009 u16 tclass,
1010 u32 *out_sid)
1012 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1016 * security_change_sid - Compute the SID for object relabeling.
1017 * @ssid: source security identifier
1018 * @tsid: target security identifier
1019 * @tclass: target security class
1020 * @out_sid: security identifier for selected member
1022 * Compute a SID to use for relabeling an object of class @tclass
1023 * based on a SID pair (@ssid, @tsid).
1024 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1025 * if insufficient memory is available, or %0 if the SID was
1026 * computed successfully.
1028 int security_change_sid(u32 ssid,
1029 u32 tsid,
1030 u16 tclass,
1031 u32 *out_sid)
1033 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1037 * Verify that each kernel class that is defined in the
1038 * policy is correct
1040 static int validate_classes(struct policydb *p)
1042 int i, j;
1043 struct class_datum *cladatum;
1044 struct perm_datum *perdatum;
1045 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1046 u16 class_val;
1047 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1048 const char *def_class, *def_perm, *pol_class;
1049 struct symtab *perms;
1051 for (i = 1; i < kdefs->cts_len; i++) {
1052 def_class = kdefs->class_to_string[i];
1053 if (i > p->p_classes.nprim) {
1054 printk(KERN_INFO
1055 "security: class %s not defined in policy\n",
1056 def_class);
1057 continue;
1059 pol_class = p->p_class_val_to_name[i-1];
1060 if (strcmp(pol_class, def_class)) {
1061 printk(KERN_ERR
1062 "security: class %d is incorrect, found %s but should be %s\n",
1063 i, pol_class, def_class);
1064 return -EINVAL;
1067 for (i = 0; i < kdefs->av_pts_len; i++) {
1068 class_val = kdefs->av_perm_to_string[i].tclass;
1069 perm_val = kdefs->av_perm_to_string[i].value;
1070 def_perm = kdefs->av_perm_to_string[i].name;
1071 if (class_val > p->p_classes.nprim)
1072 continue;
1073 pol_class = p->p_class_val_to_name[class_val-1];
1074 cladatum = hashtab_search(p->p_classes.table, pol_class);
1075 BUG_ON(!cladatum);
1076 perms = &cladatum->permissions;
1077 nprim = 1 << (perms->nprim - 1);
1078 if (perm_val > nprim) {
1079 printk(KERN_INFO
1080 "security: permission %s in class %s not defined in policy\n",
1081 def_perm, pol_class);
1082 continue;
1084 perdatum = hashtab_search(perms->table, def_perm);
1085 if (perdatum == NULL) {
1086 printk(KERN_ERR
1087 "security: permission %s in class %s not found in policy\n",
1088 def_perm, pol_class);
1089 return -EINVAL;
1091 pol_val = 1 << (perdatum->value - 1);
1092 if (pol_val != perm_val) {
1093 printk(KERN_ERR
1094 "security: permission %s in class %s has incorrect value\n",
1095 def_perm, pol_class);
1096 return -EINVAL;
1099 for (i = 0; i < kdefs->av_inherit_len; i++) {
1100 class_val = kdefs->av_inherit[i].tclass;
1101 if (class_val > p->p_classes.nprim)
1102 continue;
1103 pol_class = p->p_class_val_to_name[class_val-1];
1104 cladatum = hashtab_search(p->p_classes.table, pol_class);
1105 BUG_ON(!cladatum);
1106 if (!cladatum->comdatum) {
1107 printk(KERN_ERR
1108 "security: class %s should have an inherits clause but does not\n",
1109 pol_class);
1110 return -EINVAL;
1112 tmp = kdefs->av_inherit[i].common_base;
1113 common_pts_len = 0;
1114 while (!(tmp & 0x01)) {
1115 common_pts_len++;
1116 tmp >>= 1;
1118 perms = &cladatum->comdatum->permissions;
1119 for (j = 0; j < common_pts_len; j++) {
1120 def_perm = kdefs->av_inherit[i].common_pts[j];
1121 if (j >= perms->nprim) {
1122 printk(KERN_INFO
1123 "security: permission %s in class %s not defined in policy\n",
1124 def_perm, pol_class);
1125 continue;
1127 perdatum = hashtab_search(perms->table, def_perm);
1128 if (perdatum == NULL) {
1129 printk(KERN_ERR
1130 "security: permission %s in class %s not found in policy\n",
1131 def_perm, pol_class);
1132 return -EINVAL;
1134 if (perdatum->value != j + 1) {
1135 printk(KERN_ERR
1136 "security: permission %s in class %s has incorrect value\n",
1137 def_perm, pol_class);
1138 return -EINVAL;
1142 return 0;
1145 /* Clone the SID into the new SID table. */
1146 static int clone_sid(u32 sid,
1147 struct context *context,
1148 void *arg)
1150 struct sidtab *s = arg;
1152 return sidtab_insert(s, sid, context);
1155 static inline int convert_context_handle_invalid_context(struct context *context)
1157 int rc = 0;
1159 if (selinux_enforcing) {
1160 rc = -EINVAL;
1161 } else {
1162 char *s;
1163 u32 len;
1165 context_struct_to_string(context, &s, &len);
1166 printk(KERN_ERR "security: context %s is invalid\n", s);
1167 kfree(s);
1169 return rc;
1172 struct convert_context_args {
1173 struct policydb *oldp;
1174 struct policydb *newp;
1178 * Convert the values in the security context
1179 * structure `c' from the values specified
1180 * in the policy `p->oldp' to the values specified
1181 * in the policy `p->newp'. Verify that the
1182 * context is valid under the new policy.
1184 static int convert_context(u32 key,
1185 struct context *c,
1186 void *p)
1188 struct convert_context_args *args;
1189 struct context oldc;
1190 struct role_datum *role;
1191 struct type_datum *typdatum;
1192 struct user_datum *usrdatum;
1193 char *s;
1194 u32 len;
1195 int rc;
1197 args = p;
1199 rc = context_cpy(&oldc, c);
1200 if (rc)
1201 goto out;
1203 rc = -EINVAL;
1205 /* Convert the user. */
1206 usrdatum = hashtab_search(args->newp->p_users.table,
1207 args->oldp->p_user_val_to_name[c->user - 1]);
1208 if (!usrdatum) {
1209 goto bad;
1211 c->user = usrdatum->value;
1213 /* Convert the role. */
1214 role = hashtab_search(args->newp->p_roles.table,
1215 args->oldp->p_role_val_to_name[c->role - 1]);
1216 if (!role) {
1217 goto bad;
1219 c->role = role->value;
1221 /* Convert the type. */
1222 typdatum = hashtab_search(args->newp->p_types.table,
1223 args->oldp->p_type_val_to_name[c->type - 1]);
1224 if (!typdatum) {
1225 goto bad;
1227 c->type = typdatum->value;
1229 rc = mls_convert_context(args->oldp, args->newp, c);
1230 if (rc)
1231 goto bad;
1233 /* Check the validity of the new context. */
1234 if (!policydb_context_isvalid(args->newp, c)) {
1235 rc = convert_context_handle_invalid_context(&oldc);
1236 if (rc)
1237 goto bad;
1240 context_destroy(&oldc);
1241 out:
1242 return rc;
1243 bad:
1244 context_struct_to_string(&oldc, &s, &len);
1245 context_destroy(&oldc);
1246 printk(KERN_ERR "security: invalidating context %s\n", s);
1247 kfree(s);
1248 goto out;
1251 extern void selinux_complete_init(void);
1254 * security_load_policy - Load a security policy configuration.
1255 * @data: binary policy data
1256 * @len: length of data in bytes
1258 * Load a new set of security policy configuration data,
1259 * validate it and convert the SID table as necessary.
1260 * This function will flush the access vector cache after
1261 * loading the new policy.
1263 int security_load_policy(void *data, size_t len)
1265 struct policydb oldpolicydb, newpolicydb;
1266 struct sidtab oldsidtab, newsidtab;
1267 struct convert_context_args args;
1268 u32 seqno;
1269 int rc = 0;
1270 struct policy_file file = { data, len }, *fp = &file;
1272 LOAD_LOCK;
1274 if (!ss_initialized) {
1275 avtab_cache_init();
1276 if (policydb_read(&policydb, fp)) {
1277 LOAD_UNLOCK;
1278 avtab_cache_destroy();
1279 return -EINVAL;
1281 if (policydb_load_isids(&policydb, &sidtab)) {
1282 LOAD_UNLOCK;
1283 policydb_destroy(&policydb);
1284 avtab_cache_destroy();
1285 return -EINVAL;
1287 /* Verify that the kernel defined classes are correct. */
1288 if (validate_classes(&policydb)) {
1289 printk(KERN_ERR
1290 "security: the definition of a class is incorrect\n");
1291 LOAD_UNLOCK;
1292 sidtab_destroy(&sidtab);
1293 policydb_destroy(&policydb);
1294 avtab_cache_destroy();
1295 return -EINVAL;
1297 policydb_loaded_version = policydb.policyvers;
1298 ss_initialized = 1;
1299 seqno = ++latest_granting;
1300 LOAD_UNLOCK;
1301 selinux_complete_init();
1302 avc_ss_reset(seqno);
1303 selnl_notify_policyload(seqno);
1304 selinux_netlbl_cache_invalidate();
1305 selinux_xfrm_notify_policyload();
1306 return 0;
1309 #if 0
1310 sidtab_hash_eval(&sidtab, "sids");
1311 #endif
1313 if (policydb_read(&newpolicydb, fp)) {
1314 LOAD_UNLOCK;
1315 return -EINVAL;
1318 sidtab_init(&newsidtab);
1320 /* Verify that the kernel defined classes are correct. */
1321 if (validate_classes(&newpolicydb)) {
1322 printk(KERN_ERR
1323 "security: the definition of a class is incorrect\n");
1324 rc = -EINVAL;
1325 goto err;
1328 /* Clone the SID table. */
1329 sidtab_shutdown(&sidtab);
1330 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1331 rc = -ENOMEM;
1332 goto err;
1335 /* Convert the internal representations of contexts
1336 in the new SID table and remove invalid SIDs. */
1337 args.oldp = &policydb;
1338 args.newp = &newpolicydb;
1339 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1341 /* Save the old policydb and SID table to free later. */
1342 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1343 sidtab_set(&oldsidtab, &sidtab);
1345 /* Install the new policydb and SID table. */
1346 POLICY_WRLOCK;
1347 memcpy(&policydb, &newpolicydb, sizeof policydb);
1348 sidtab_set(&sidtab, &newsidtab);
1349 seqno = ++latest_granting;
1350 policydb_loaded_version = policydb.policyvers;
1351 POLICY_WRUNLOCK;
1352 LOAD_UNLOCK;
1354 /* Free the old policydb and SID table. */
1355 policydb_destroy(&oldpolicydb);
1356 sidtab_destroy(&oldsidtab);
1358 avc_ss_reset(seqno);
1359 selnl_notify_policyload(seqno);
1360 selinux_netlbl_cache_invalidate();
1361 selinux_xfrm_notify_policyload();
1363 return 0;
1365 err:
1366 LOAD_UNLOCK;
1367 sidtab_destroy(&newsidtab);
1368 policydb_destroy(&newpolicydb);
1369 return rc;
1374 * security_port_sid - Obtain the SID for a port.
1375 * @domain: communication domain aka address family
1376 * @type: socket type
1377 * @protocol: protocol number
1378 * @port: port number
1379 * @out_sid: security identifier
1381 int security_port_sid(u16 domain,
1382 u16 type,
1383 u8 protocol,
1384 u16 port,
1385 u32 *out_sid)
1387 struct ocontext *c;
1388 int rc = 0;
1390 POLICY_RDLOCK;
1392 c = policydb.ocontexts[OCON_PORT];
1393 while (c) {
1394 if (c->u.port.protocol == protocol &&
1395 c->u.port.low_port <= port &&
1396 c->u.port.high_port >= port)
1397 break;
1398 c = c->next;
1401 if (c) {
1402 if (!c->sid[0]) {
1403 rc = sidtab_context_to_sid(&sidtab,
1404 &c->context[0],
1405 &c->sid[0]);
1406 if (rc)
1407 goto out;
1409 *out_sid = c->sid[0];
1410 } else {
1411 *out_sid = SECINITSID_PORT;
1414 out:
1415 POLICY_RDUNLOCK;
1416 return rc;
1420 * security_netif_sid - Obtain the SID for a network interface.
1421 * @name: interface name
1422 * @if_sid: interface SID
1423 * @msg_sid: default SID for received packets
1425 int security_netif_sid(char *name,
1426 u32 *if_sid,
1427 u32 *msg_sid)
1429 int rc = 0;
1430 struct ocontext *c;
1432 POLICY_RDLOCK;
1434 c = policydb.ocontexts[OCON_NETIF];
1435 while (c) {
1436 if (strcmp(name, c->u.name) == 0)
1437 break;
1438 c = c->next;
1441 if (c) {
1442 if (!c->sid[0] || !c->sid[1]) {
1443 rc = sidtab_context_to_sid(&sidtab,
1444 &c->context[0],
1445 &c->sid[0]);
1446 if (rc)
1447 goto out;
1448 rc = sidtab_context_to_sid(&sidtab,
1449 &c->context[1],
1450 &c->sid[1]);
1451 if (rc)
1452 goto out;
1454 *if_sid = c->sid[0];
1455 *msg_sid = c->sid[1];
1456 } else {
1457 *if_sid = SECINITSID_NETIF;
1458 *msg_sid = SECINITSID_NETMSG;
1461 out:
1462 POLICY_RDUNLOCK;
1463 return rc;
1466 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1468 int i, fail = 0;
1470 for(i = 0; i < 4; i++)
1471 if(addr[i] != (input[i] & mask[i])) {
1472 fail = 1;
1473 break;
1476 return !fail;
1480 * security_node_sid - Obtain the SID for a node (host).
1481 * @domain: communication domain aka address family
1482 * @addrp: address
1483 * @addrlen: address length in bytes
1484 * @out_sid: security identifier
1486 int security_node_sid(u16 domain,
1487 void *addrp,
1488 u32 addrlen,
1489 u32 *out_sid)
1491 int rc = 0;
1492 struct ocontext *c;
1494 POLICY_RDLOCK;
1496 switch (domain) {
1497 case AF_INET: {
1498 u32 addr;
1500 if (addrlen != sizeof(u32)) {
1501 rc = -EINVAL;
1502 goto out;
1505 addr = *((u32 *)addrp);
1507 c = policydb.ocontexts[OCON_NODE];
1508 while (c) {
1509 if (c->u.node.addr == (addr & c->u.node.mask))
1510 break;
1511 c = c->next;
1513 break;
1516 case AF_INET6:
1517 if (addrlen != sizeof(u64) * 2) {
1518 rc = -EINVAL;
1519 goto out;
1521 c = policydb.ocontexts[OCON_NODE6];
1522 while (c) {
1523 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1524 c->u.node6.mask))
1525 break;
1526 c = c->next;
1528 break;
1530 default:
1531 *out_sid = SECINITSID_NODE;
1532 goto out;
1535 if (c) {
1536 if (!c->sid[0]) {
1537 rc = sidtab_context_to_sid(&sidtab,
1538 &c->context[0],
1539 &c->sid[0]);
1540 if (rc)
1541 goto out;
1543 *out_sid = c->sid[0];
1544 } else {
1545 *out_sid = SECINITSID_NODE;
1548 out:
1549 POLICY_RDUNLOCK;
1550 return rc;
1553 #define SIDS_NEL 25
1556 * security_get_user_sids - Obtain reachable SIDs for a user.
1557 * @fromsid: starting SID
1558 * @username: username
1559 * @sids: array of reachable SIDs for user
1560 * @nel: number of elements in @sids
1562 * Generate the set of SIDs for legal security contexts
1563 * for a given user that can be reached by @fromsid.
1564 * Set *@sids to point to a dynamically allocated
1565 * array containing the set of SIDs. Set *@nel to the
1566 * number of elements in the array.
1569 int security_get_user_sids(u32 fromsid,
1570 char *username,
1571 u32 **sids,
1572 u32 *nel)
1574 struct context *fromcon, usercon;
1575 u32 *mysids, *mysids2, sid;
1576 u32 mynel = 0, maxnel = SIDS_NEL;
1577 struct user_datum *user;
1578 struct role_datum *role;
1579 struct av_decision avd;
1580 struct ebitmap_node *rnode, *tnode;
1581 int rc = 0, i, j;
1583 if (!ss_initialized) {
1584 *sids = NULL;
1585 *nel = 0;
1586 goto out;
1589 POLICY_RDLOCK;
1591 fromcon = sidtab_search(&sidtab, fromsid);
1592 if (!fromcon) {
1593 rc = -EINVAL;
1594 goto out_unlock;
1597 user = hashtab_search(policydb.p_users.table, username);
1598 if (!user) {
1599 rc = -EINVAL;
1600 goto out_unlock;
1602 usercon.user = user->value;
1604 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1605 if (!mysids) {
1606 rc = -ENOMEM;
1607 goto out_unlock;
1610 ebitmap_for_each_bit(&user->roles, rnode, i) {
1611 if (!ebitmap_node_get_bit(rnode, i))
1612 continue;
1613 role = policydb.role_val_to_struct[i];
1614 usercon.role = i+1;
1615 ebitmap_for_each_bit(&role->types, tnode, j) {
1616 if (!ebitmap_node_get_bit(tnode, j))
1617 continue;
1618 usercon.type = j+1;
1620 if (mls_setup_user_range(fromcon, user, &usercon))
1621 continue;
1623 rc = context_struct_compute_av(fromcon, &usercon,
1624 SECCLASS_PROCESS,
1625 PROCESS__TRANSITION,
1626 &avd);
1627 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1628 continue;
1629 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1630 if (rc) {
1631 kfree(mysids);
1632 goto out_unlock;
1634 if (mynel < maxnel) {
1635 mysids[mynel++] = sid;
1636 } else {
1637 maxnel += SIDS_NEL;
1638 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1639 if (!mysids2) {
1640 rc = -ENOMEM;
1641 kfree(mysids);
1642 goto out_unlock;
1644 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1645 kfree(mysids);
1646 mysids = mysids2;
1647 mysids[mynel++] = sid;
1652 *sids = mysids;
1653 *nel = mynel;
1655 out_unlock:
1656 POLICY_RDUNLOCK;
1657 out:
1658 return rc;
1662 * security_genfs_sid - Obtain a SID for a file in a filesystem
1663 * @fstype: filesystem type
1664 * @path: path from root of mount
1665 * @sclass: file security class
1666 * @sid: SID for path
1668 * Obtain a SID to use for a file in a filesystem that
1669 * cannot support xattr or use a fixed labeling behavior like
1670 * transition SIDs or task SIDs.
1672 int security_genfs_sid(const char *fstype,
1673 char *path,
1674 u16 sclass,
1675 u32 *sid)
1677 int len;
1678 struct genfs *genfs;
1679 struct ocontext *c;
1680 int rc = 0, cmp = 0;
1682 POLICY_RDLOCK;
1684 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1685 cmp = strcmp(fstype, genfs->fstype);
1686 if (cmp <= 0)
1687 break;
1690 if (!genfs || cmp) {
1691 *sid = SECINITSID_UNLABELED;
1692 rc = -ENOENT;
1693 goto out;
1696 for (c = genfs->head; c; c = c->next) {
1697 len = strlen(c->u.name);
1698 if ((!c->v.sclass || sclass == c->v.sclass) &&
1699 (strncmp(c->u.name, path, len) == 0))
1700 break;
1703 if (!c) {
1704 *sid = SECINITSID_UNLABELED;
1705 rc = -ENOENT;
1706 goto out;
1709 if (!c->sid[0]) {
1710 rc = sidtab_context_to_sid(&sidtab,
1711 &c->context[0],
1712 &c->sid[0]);
1713 if (rc)
1714 goto out;
1717 *sid = c->sid[0];
1718 out:
1719 POLICY_RDUNLOCK;
1720 return rc;
1724 * security_fs_use - Determine how to handle labeling for a filesystem.
1725 * @fstype: filesystem type
1726 * @behavior: labeling behavior
1727 * @sid: SID for filesystem (superblock)
1729 int security_fs_use(
1730 const char *fstype,
1731 unsigned int *behavior,
1732 u32 *sid)
1734 int rc = 0;
1735 struct ocontext *c;
1737 POLICY_RDLOCK;
1739 c = policydb.ocontexts[OCON_FSUSE];
1740 while (c) {
1741 if (strcmp(fstype, c->u.name) == 0)
1742 break;
1743 c = c->next;
1746 if (c) {
1747 *behavior = c->v.behavior;
1748 if (!c->sid[0]) {
1749 rc = sidtab_context_to_sid(&sidtab,
1750 &c->context[0],
1751 &c->sid[0]);
1752 if (rc)
1753 goto out;
1755 *sid = c->sid[0];
1756 } else {
1757 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1758 if (rc) {
1759 *behavior = SECURITY_FS_USE_NONE;
1760 rc = 0;
1761 } else {
1762 *behavior = SECURITY_FS_USE_GENFS;
1766 out:
1767 POLICY_RDUNLOCK;
1768 return rc;
1771 int security_get_bools(int *len, char ***names, int **values)
1773 int i, rc = -ENOMEM;
1775 POLICY_RDLOCK;
1776 *names = NULL;
1777 *values = NULL;
1779 *len = policydb.p_bools.nprim;
1780 if (!*len) {
1781 rc = 0;
1782 goto out;
1785 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1786 if (!*names)
1787 goto err;
1789 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1790 if (!*values)
1791 goto err;
1793 for (i = 0; i < *len; i++) {
1794 size_t name_len;
1795 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1796 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1797 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1798 if (!(*names)[i])
1799 goto err;
1800 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1801 (*names)[i][name_len - 1] = 0;
1803 rc = 0;
1804 out:
1805 POLICY_RDUNLOCK;
1806 return rc;
1807 err:
1808 if (*names) {
1809 for (i = 0; i < *len; i++)
1810 kfree((*names)[i]);
1812 kfree(*values);
1813 goto out;
1817 int security_set_bools(int len, int *values)
1819 int i, rc = 0;
1820 int lenp, seqno = 0;
1821 struct cond_node *cur;
1823 POLICY_WRLOCK;
1825 lenp = policydb.p_bools.nprim;
1826 if (len != lenp) {
1827 rc = -EFAULT;
1828 goto out;
1831 for (i = 0; i < len; i++) {
1832 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1833 audit_log(current->audit_context, GFP_ATOMIC,
1834 AUDIT_MAC_CONFIG_CHANGE,
1835 "bool=%s val=%d old_val=%d auid=%u",
1836 policydb.p_bool_val_to_name[i],
1837 !!values[i],
1838 policydb.bool_val_to_struct[i]->state,
1839 audit_get_loginuid(current->audit_context));
1841 if (values[i]) {
1842 policydb.bool_val_to_struct[i]->state = 1;
1843 } else {
1844 policydb.bool_val_to_struct[i]->state = 0;
1848 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1849 rc = evaluate_cond_node(&policydb, cur);
1850 if (rc)
1851 goto out;
1854 seqno = ++latest_granting;
1856 out:
1857 POLICY_WRUNLOCK;
1858 if (!rc) {
1859 avc_ss_reset(seqno);
1860 selnl_notify_policyload(seqno);
1861 selinux_xfrm_notify_policyload();
1863 return rc;
1866 int security_get_bool_value(int bool)
1868 int rc = 0;
1869 int len;
1871 POLICY_RDLOCK;
1873 len = policydb.p_bools.nprim;
1874 if (bool >= len) {
1875 rc = -EFAULT;
1876 goto out;
1879 rc = policydb.bool_val_to_struct[bool]->state;
1880 out:
1881 POLICY_RDUNLOCK;
1882 return rc;
1886 * security_sid_mls_copy() - computes a new sid based on the given
1887 * sid and the mls portion of mls_sid.
1889 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1891 struct context *context1;
1892 struct context *context2;
1893 struct context newcon;
1894 char *s;
1895 u32 len;
1896 int rc = 0;
1898 if (!ss_initialized || !selinux_mls_enabled) {
1899 *new_sid = sid;
1900 goto out;
1903 context_init(&newcon);
1905 POLICY_RDLOCK;
1906 context1 = sidtab_search(&sidtab, sid);
1907 if (!context1) {
1908 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1909 "%d\n", sid);
1910 rc = -EINVAL;
1911 goto out_unlock;
1914 context2 = sidtab_search(&sidtab, mls_sid);
1915 if (!context2) {
1916 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1917 "%d\n", mls_sid);
1918 rc = -EINVAL;
1919 goto out_unlock;
1922 newcon.user = context1->user;
1923 newcon.role = context1->role;
1924 newcon.type = context1->type;
1925 rc = mls_context_cpy(&newcon, context2);
1926 if (rc)
1927 goto out_unlock;
1929 /* Check the validity of the new context. */
1930 if (!policydb_context_isvalid(&policydb, &newcon)) {
1931 rc = convert_context_handle_invalid_context(&newcon);
1932 if (rc)
1933 goto bad;
1936 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1937 goto out_unlock;
1939 bad:
1940 if (!context_struct_to_string(&newcon, &s, &len)) {
1941 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1942 "security_sid_mls_copy: invalid context %s", s);
1943 kfree(s);
1946 out_unlock:
1947 POLICY_RDUNLOCK;
1948 context_destroy(&newcon);
1949 out:
1950 return rc;
1953 struct selinux_audit_rule {
1954 u32 au_seqno;
1955 struct context au_ctxt;
1958 void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1960 if (rule) {
1961 context_destroy(&rule->au_ctxt);
1962 kfree(rule);
1966 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1967 struct selinux_audit_rule **rule)
1969 struct selinux_audit_rule *tmprule;
1970 struct role_datum *roledatum;
1971 struct type_datum *typedatum;
1972 struct user_datum *userdatum;
1973 int rc = 0;
1975 *rule = NULL;
1977 if (!ss_initialized)
1978 return -ENOTSUPP;
1980 switch (field) {
1981 case AUDIT_SUBJ_USER:
1982 case AUDIT_SUBJ_ROLE:
1983 case AUDIT_SUBJ_TYPE:
1984 case AUDIT_OBJ_USER:
1985 case AUDIT_OBJ_ROLE:
1986 case AUDIT_OBJ_TYPE:
1987 /* only 'equals' and 'not equals' fit user, role, and type */
1988 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1989 return -EINVAL;
1990 break;
1991 case AUDIT_SUBJ_SEN:
1992 case AUDIT_SUBJ_CLR:
1993 case AUDIT_OBJ_LEV_LOW:
1994 case AUDIT_OBJ_LEV_HIGH:
1995 /* we do not allow a range, indicated by the presense of '-' */
1996 if (strchr(rulestr, '-'))
1997 return -EINVAL;
1998 break;
1999 default:
2000 /* only the above fields are valid */
2001 return -EINVAL;
2004 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2005 if (!tmprule)
2006 return -ENOMEM;
2008 context_init(&tmprule->au_ctxt);
2010 POLICY_RDLOCK;
2012 tmprule->au_seqno = latest_granting;
2014 switch (field) {
2015 case AUDIT_SUBJ_USER:
2016 case AUDIT_OBJ_USER:
2017 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2018 if (!userdatum)
2019 rc = -EINVAL;
2020 else
2021 tmprule->au_ctxt.user = userdatum->value;
2022 break;
2023 case AUDIT_SUBJ_ROLE:
2024 case AUDIT_OBJ_ROLE:
2025 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2026 if (!roledatum)
2027 rc = -EINVAL;
2028 else
2029 tmprule->au_ctxt.role = roledatum->value;
2030 break;
2031 case AUDIT_SUBJ_TYPE:
2032 case AUDIT_OBJ_TYPE:
2033 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2034 if (!typedatum)
2035 rc = -EINVAL;
2036 else
2037 tmprule->au_ctxt.type = typedatum->value;
2038 break;
2039 case AUDIT_SUBJ_SEN:
2040 case AUDIT_SUBJ_CLR:
2041 case AUDIT_OBJ_LEV_LOW:
2042 case AUDIT_OBJ_LEV_HIGH:
2043 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2044 break;
2047 POLICY_RDUNLOCK;
2049 if (rc) {
2050 selinux_audit_rule_free(tmprule);
2051 tmprule = NULL;
2054 *rule = tmprule;
2056 return rc;
2059 int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
2060 struct selinux_audit_rule *rule,
2061 struct audit_context *actx)
2063 struct context *ctxt;
2064 struct mls_level *level;
2065 int match = 0;
2067 if (!rule) {
2068 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2069 "selinux_audit_rule_match: missing rule\n");
2070 return -ENOENT;
2073 POLICY_RDLOCK;
2075 if (rule->au_seqno < latest_granting) {
2076 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2077 "selinux_audit_rule_match: stale rule\n");
2078 match = -ESTALE;
2079 goto out;
2082 ctxt = sidtab_search(&sidtab, sid);
2083 if (!ctxt) {
2084 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2085 "selinux_audit_rule_match: unrecognized SID %d\n",
2086 sid);
2087 match = -ENOENT;
2088 goto out;
2091 /* a field/op pair that is not caught here will simply fall through
2092 without a match */
2093 switch (field) {
2094 case AUDIT_SUBJ_USER:
2095 case AUDIT_OBJ_USER:
2096 switch (op) {
2097 case AUDIT_EQUAL:
2098 match = (ctxt->user == rule->au_ctxt.user);
2099 break;
2100 case AUDIT_NOT_EQUAL:
2101 match = (ctxt->user != rule->au_ctxt.user);
2102 break;
2104 break;
2105 case AUDIT_SUBJ_ROLE:
2106 case AUDIT_OBJ_ROLE:
2107 switch (op) {
2108 case AUDIT_EQUAL:
2109 match = (ctxt->role == rule->au_ctxt.role);
2110 break;
2111 case AUDIT_NOT_EQUAL:
2112 match = (ctxt->role != rule->au_ctxt.role);
2113 break;
2115 break;
2116 case AUDIT_SUBJ_TYPE:
2117 case AUDIT_OBJ_TYPE:
2118 switch (op) {
2119 case AUDIT_EQUAL:
2120 match = (ctxt->type == rule->au_ctxt.type);
2121 break;
2122 case AUDIT_NOT_EQUAL:
2123 match = (ctxt->type != rule->au_ctxt.type);
2124 break;
2126 break;
2127 case AUDIT_SUBJ_SEN:
2128 case AUDIT_SUBJ_CLR:
2129 case AUDIT_OBJ_LEV_LOW:
2130 case AUDIT_OBJ_LEV_HIGH:
2131 level = ((field == AUDIT_SUBJ_SEN ||
2132 field == AUDIT_OBJ_LEV_LOW) ?
2133 &ctxt->range.level[0] : &ctxt->range.level[1]);
2134 switch (op) {
2135 case AUDIT_EQUAL:
2136 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2137 level);
2138 break;
2139 case AUDIT_NOT_EQUAL:
2140 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2141 level);
2142 break;
2143 case AUDIT_LESS_THAN:
2144 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2145 level) &&
2146 !mls_level_eq(&rule->au_ctxt.range.level[0],
2147 level));
2148 break;
2149 case AUDIT_LESS_THAN_OR_EQUAL:
2150 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2151 level);
2152 break;
2153 case AUDIT_GREATER_THAN:
2154 match = (mls_level_dom(level,
2155 &rule->au_ctxt.range.level[0]) &&
2156 !mls_level_eq(level,
2157 &rule->au_ctxt.range.level[0]));
2158 break;
2159 case AUDIT_GREATER_THAN_OR_EQUAL:
2160 match = mls_level_dom(level,
2161 &rule->au_ctxt.range.level[0]);
2162 break;
2166 out:
2167 POLICY_RDUNLOCK;
2168 return match;
2171 static int (*aurule_callback)(void) = NULL;
2173 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2174 u16 class, u32 perms, u32 *retained)
2176 int err = 0;
2178 if (event == AVC_CALLBACK_RESET && aurule_callback)
2179 err = aurule_callback();
2180 return err;
2183 static int __init aurule_init(void)
2185 int err;
2187 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2188 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2189 if (err)
2190 panic("avc_add_callback() failed, error %d\n", err);
2192 return err;
2194 __initcall(aurule_init);
2196 void selinux_audit_set_callback(int (*callback)(void))
2198 aurule_callback = callback;
2202 * security_skb_extlbl_sid - Determine the external label of a packet
2203 * @skb: the packet
2204 * @base_sid: the SELinux SID to use as a context for MLS only external labels
2205 * @sid: the packet's SID
2207 * Description:
2208 * Check the various different forms of external packet labeling and determine
2209 * the external SID for the packet.
2212 void security_skb_extlbl_sid(struct sk_buff *skb, u32 base_sid, u32 *sid)
2214 u32 xfrm_sid;
2215 u32 nlbl_sid;
2217 selinux_skb_xfrm_sid(skb, &xfrm_sid);
2218 if (selinux_netlbl_skbuff_getsid(skb,
2219 (xfrm_sid == SECSID_NULL ?
2220 base_sid : xfrm_sid),
2221 &nlbl_sid) != 0)
2222 nlbl_sid = SECSID_NULL;
2224 *sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
2227 #ifdef CONFIG_NETLABEL
2229 * NetLabel cache structure
2231 #define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
2232 #define NETLBL_CACHE_T_NONE 0
2233 #define NETLBL_CACHE_T_SID 1
2234 #define NETLBL_CACHE_T_MLS 2
2235 struct selinux_netlbl_cache {
2236 u32 type;
2237 union {
2238 u32 sid;
2239 struct mls_range mls_label;
2240 } data;
2244 * security_netlbl_cache_free - Free the NetLabel cached data
2245 * @data: the data to free
2247 * Description:
2248 * This function is intended to be used as the free() callback inside the
2249 * netlbl_lsm_cache structure.
2252 static void security_netlbl_cache_free(const void *data)
2254 struct selinux_netlbl_cache *cache;
2256 if (data == NULL)
2257 return;
2259 cache = NETLBL_CACHE(data);
2260 switch (cache->type) {
2261 case NETLBL_CACHE_T_MLS:
2262 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2263 break;
2265 kfree(data);
2269 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2270 * @secattr: the NetLabel packet security attributes
2271 * @ctx: the SELinux context
2273 * Description:
2274 * Attempt to cache the context in @ctx, which was derived from the packet in
2275 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2276 * already been initialized.
2279 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2280 struct context *ctx)
2282 struct selinux_netlbl_cache *cache = NULL;
2284 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2285 if (secattr->cache == NULL)
2286 return;
2288 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2289 if (cache == NULL)
2290 return;
2292 cache->type = NETLBL_CACHE_T_MLS;
2293 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2294 &ctx->range.level[0].cat) != 0)
2295 return;
2296 cache->data.mls_label.level[1].cat.highbit =
2297 cache->data.mls_label.level[0].cat.highbit;
2298 cache->data.mls_label.level[1].cat.node =
2299 cache->data.mls_label.level[0].cat.node;
2300 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2301 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2303 secattr->cache->free = security_netlbl_cache_free;
2304 secattr->cache->data = (void *)cache;
2305 secattr->flags |= NETLBL_SECATTR_CACHE;
2309 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2310 * @secattr: the NetLabel packet security attributes
2311 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2312 * @sid: the SELinux SID
2314 * Description:
2315 * Convert the given NetLabel security attributes in @secattr into a
2316 * SELinux SID. If the @secattr field does not contain a full SELinux
2317 * SID/context then use the context in @base_sid as the foundation. If
2318 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2319 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2320 * SID conversion for future lookups. Returns zero on success, negative
2321 * values on failure.
2324 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2325 u32 base_sid,
2326 u32 *sid)
2328 int rc = -EIDRM;
2329 struct context *ctx;
2330 struct context ctx_new;
2331 struct selinux_netlbl_cache *cache;
2333 if (!ss_initialized) {
2334 *sid = SECSID_NULL;
2335 return 0;
2338 POLICY_RDLOCK;
2340 if (secattr->flags & NETLBL_SECATTR_CACHE) {
2341 cache = NETLBL_CACHE(secattr->cache->data);
2342 switch (cache->type) {
2343 case NETLBL_CACHE_T_SID:
2344 *sid = cache->data.sid;
2345 rc = 0;
2346 break;
2347 case NETLBL_CACHE_T_MLS:
2348 ctx = sidtab_search(&sidtab, base_sid);
2349 if (ctx == NULL)
2350 goto netlbl_secattr_to_sid_return;
2352 ctx_new.user = ctx->user;
2353 ctx_new.role = ctx->role;
2354 ctx_new.type = ctx->type;
2355 ctx_new.range.level[0].sens =
2356 cache->data.mls_label.level[0].sens;
2357 ctx_new.range.level[0].cat.highbit =
2358 cache->data.mls_label.level[0].cat.highbit;
2359 ctx_new.range.level[0].cat.node =
2360 cache->data.mls_label.level[0].cat.node;
2361 ctx_new.range.level[1].sens =
2362 cache->data.mls_label.level[1].sens;
2363 ctx_new.range.level[1].cat.highbit =
2364 cache->data.mls_label.level[1].cat.highbit;
2365 ctx_new.range.level[1].cat.node =
2366 cache->data.mls_label.level[1].cat.node;
2368 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2369 break;
2370 default:
2371 goto netlbl_secattr_to_sid_return;
2373 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2374 ctx = sidtab_search(&sidtab, base_sid);
2375 if (ctx == NULL)
2376 goto netlbl_secattr_to_sid_return;
2378 ctx_new.user = ctx->user;
2379 ctx_new.role = ctx->role;
2380 ctx_new.type = ctx->type;
2381 mls_import_netlbl_lvl(&ctx_new, secattr);
2382 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
2383 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2384 secattr->mls_cat) != 0)
2385 goto netlbl_secattr_to_sid_return;
2386 ctx_new.range.level[1].cat.highbit =
2387 ctx_new.range.level[0].cat.highbit;
2388 ctx_new.range.level[1].cat.node =
2389 ctx_new.range.level[0].cat.node;
2390 } else {
2391 ebitmap_init(&ctx_new.range.level[0].cat);
2392 ebitmap_init(&ctx_new.range.level[1].cat);
2394 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2395 goto netlbl_secattr_to_sid_return_cleanup;
2397 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2398 if (rc != 0)
2399 goto netlbl_secattr_to_sid_return_cleanup;
2401 security_netlbl_cache_add(secattr, &ctx_new);
2403 ebitmap_destroy(&ctx_new.range.level[0].cat);
2404 } else {
2405 *sid = SECSID_NULL;
2406 rc = 0;
2409 netlbl_secattr_to_sid_return:
2410 POLICY_RDUNLOCK;
2411 return rc;
2412 netlbl_secattr_to_sid_return_cleanup:
2413 ebitmap_destroy(&ctx_new.range.level[0].cat);
2414 goto netlbl_secattr_to_sid_return;
2418 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2419 * @sid: the SELinux SID
2420 * @secattr: the NetLabel packet security attributes
2422 * Description:
2423 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2424 * Returns zero on success, negative values on failure.
2427 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
2429 int rc = -ENOENT;
2430 struct context *ctx;
2432 netlbl_secattr_init(secattr);
2434 if (!ss_initialized)
2435 return 0;
2437 POLICY_RDLOCK;
2438 ctx = sidtab_search(&sidtab, sid);
2439 if (ctx == NULL)
2440 goto netlbl_sid_to_secattr_failure;
2441 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2442 GFP_ATOMIC);
2443 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2444 mls_export_netlbl_lvl(ctx, secattr);
2445 rc = mls_export_netlbl_cat(ctx, secattr);
2446 if (rc != 0)
2447 goto netlbl_sid_to_secattr_failure;
2448 POLICY_RDUNLOCK;
2450 return 0;
2452 netlbl_sid_to_secattr_failure:
2453 POLICY_RDUNLOCK;
2454 netlbl_secattr_destroy(secattr);
2455 return rc;
2457 #endif /* CONFIG_NETLABEL */